aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/CodeGen/MInstBuilder.h
blob: 6ff5777503acabd3dc303273a7cdddcbd52cb678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//===-- CodeGen/MInstBuilder.h - Simplify creation of MInstcn's -*- C++ -*-===//
//
// This file exposes a function named BuildMInst that is useful for dramatically
// simplifying how MInstruction's are created.  Instead of using code like this:
//
//   M = new MInstruction(BB, X86::ADDrr32, DestReg);
//   M->addOperand(Arg0Reg, MOperand::Register);
//   M->addOperand(Arg1Reg, MOperand::Register);
//
// we can now use code like this:
//
//   M = BuildMInst(BB, X86::ADDrr8, DestReg).addReg(Arg0Reg).addReg(Arg1Reg);
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_MINSTBUILDER_H
#define LLVM_CODEGEN_MINSTBUILDER_H

#include "llvm/CodeGen/MInstruction.h"

struct MInstructionBuilder { 
  MInstruction *MI;

  MInstructionBuilder(MInstruction *mi) : MI(mi) {}

  /// Allow automatic conversion to the machine instruction we are working on.
  ///
  operator MInstruction*() const { return MI; }

  /// addReg - Add a new register operand...
  ///
  MInstructionBuilder &addReg(unsigned RegNo) {
    MI->addOperand(RegNo, MOperand::Register);
    return *this;
  }

  /// addSImm - Add a new sign extended immediate operand...
  ///
  MInstructionBuilder &addSImm(int Val) {
    MI->addOperand(Val, MOperand::SignExtImmediate);
    return *this;
  }

  /// addZImm - Add a new zero extended immediate operand...
  ///
  MInstructionBuilder &addZImm(unsigned Val) {
    MI->addOperand(Val, MOperand::ZeroExtImmediate);
    return *this;
  }

  /// addPCDisp - Add a PC Relative Displacement operand...
  ///
  MInstructionBuilder &addPCDisp(int Disp) {
    MI->addOperand(Disp, MOperand::PCRelativeDisp);
    return *this;
  }
};

/// BuildMInst - Builder interface.  Specify how to create the initial
/// instruction itself.
///
inline MInstructionBuilder BuildMInst(unsigned Opcode, unsigned DestReg = 0) {
  return MInstructionBuilder(new MInstruction(Opcode, DestReg));
}

inline MInstructionBuilder BuildMInst(MBasicBlock *BB, unsigned Opcode,
                               unsigned DestReg = 0) {
  return MInstructionBuilder(new MInstruction(BB, Opcode, DestReg));
}
                                
#endif