1 //===-- CodeGen/MInstBuilder.h - Simplify creation of MInstcn's -*- C++ -*-===//
3 // This file exposes a function named BuildMInst that is useful for dramatically
4 // simplifying how MInstruction's are created. Instead of using code like this:
6 // M = new MInstruction(BB, X86::ADDrr32, DestReg);
7 // M->addOperand(Arg0Reg, MOperand::Register);
8 // M->addOperand(Arg1Reg, MOperand::Register);
10 // we can now use code like this:
12 // M = BuildMInst(BB, X86::ADDrr8, DestReg).addReg(Arg0Reg).addReg(Arg1Reg);
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_MINSTBUILDER_H
17 #define LLVM_CODEGEN_MINSTBUILDER_H
19 #include "llvm/CodeGen/MInstruction.h"
21 struct MInstructionBuilder {
24 MInstructionBuilder(MInstruction *mi) : MI(mi) {}
26 /// Allow automatic conversion to the machine instruction we are working on.
28 operator MInstruction*() const { return MI; }
30 /// addReg - Add a new register operand...
32 MInstructionBuilder &addReg(unsigned RegNo) {
33 MI->addOperand(RegNo, MOperand::Register);
37 /// addSImm - Add a new sign extended immediate operand...
39 MInstructionBuilder &addSImm(int Val) {
40 MI->addOperand(Val, MOperand::SignExtImmediate);
44 /// addZImm - Add a new zero extended immediate operand...
46 MInstructionBuilder &addZImm(unsigned Val) {
47 MI->addOperand(Val, MOperand::ZeroExtImmediate);
51 /// addPCDisp - Add a PC Relative Displacement operand...
53 MInstructionBuilder &addPCDisp(int Disp) {
54 MI->addOperand(Disp, MOperand::PCRelativeDisp);
59 /// BuildMInst - Builder interface. Specify how to create the initial
60 /// instruction itself.
62 inline MInstructionBuilder BuildMInst(unsigned Opcode, unsigned DestReg = 0) {
63 return MInstructionBuilder(new MInstruction(Opcode, DestReg));
66 inline MInstructionBuilder BuildMInst(MBasicBlock *BB, unsigned Opcode,
67 unsigned DestReg = 0) {
68 return MInstructionBuilder(new MInstruction(BB, Opcode, DestReg));