Add MCInstBuilder, a utility class to simplify MCInst creation similar to MachineInst...
[oota-llvm.git] / include / llvm / MC / MCInstBuilder.h
1 //===-- llvm/MC/MCInstBuilder.h - Simplify creation of MCInsts --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the MCInstBuilder class for convenient creation of
11 // MCInsts.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_MC_MCINSTBUILDER_H
16 #define LLVM_MC_MCINSTBUILDER_H
17
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCStreamer.h"
20
21 namespace llvm {
22
23 class MCInstBuilder {
24   MCInst Inst;
25
26 public:
27   /// \brief Create a new MCInstBuilder for an MCInst with a specific opcode.
28   MCInstBuilder(unsigned Opcode) {
29     Inst.setOpcode(Opcode);
30   }
31
32   /// \brief Add a new register operand.
33   MCInstBuilder &addReg(unsigned Reg) {
34     Inst.addOperand(MCOperand::CreateReg(Reg));
35     return *this;
36   }
37
38   /// \brief Add a new integer immediate operand.
39   MCInstBuilder &addImm(int64_t Val) {
40     Inst.addOperand(MCOperand::CreateImm(Val));
41     return *this;
42   }
43
44   /// \brief Add a new floating point immediate operand.
45   MCInstBuilder &addFPImm(double Val) {
46     Inst.addOperand(MCOperand::CreateFPImm(Val));
47     return *this;
48   }
49
50   /// \brief Add a new MCExpr operand.
51   MCInstBuilder &addExpr(const MCExpr *Val) {
52     Inst.addOperand(MCOperand::CreateExpr(Val));
53     return *this;
54   }
55
56   /// \brief Add a new MCInst operand.
57   MCInstBuilder &addInst(const MCInst *Val) {
58     Inst.addOperand(MCOperand::CreateInst(Val));
59     return *this;
60   }
61
62   /// \brief Emit the built instruction to an MCStreamer.
63   void emit(MCStreamer &OutStreamer) {
64     OutStreamer.EmitInstruction(Inst);
65   }
66 };
67
68 } // end namespace llvm
69
70 #endif