LLVM API Documentation
00001 //===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file exposes functions that may be used with BuildMI from the 00011 // MachineInstrBuilder.h file to handle X86'isms in a clean way. 00012 // 00013 // The BuildMem function may be used with the BuildMI function to add entire 00014 // memory references in a single, typed, function call. X86 memory references 00015 // can be very complex expressions (described in the README), so wrapping them 00016 // up behind an easier to use interface makes sense. Descriptions of the 00017 // functions are included below. 00018 // 00019 // For reference, the order of operands for memory references is: 00020 // (Operand), Base, Scale, Index, Displacement. 00021 // 00022 //===----------------------------------------------------------------------===// 00023 00024 #ifndef X86INSTRBUILDER_H 00025 #define X86INSTRBUILDER_H 00026 00027 #include "llvm/CodeGen/MachineFrameInfo.h" 00028 #include "llvm/CodeGen/MachineInstrBuilder.h" 00029 #include "llvm/CodeGen/PseudoSourceValue.h" 00030 00031 namespace llvm { 00032 00033 /// X86AddressMode - This struct holds a generalized full x86 address mode. 00034 /// The base register can be a frame index, which will eventually be replaced 00035 /// with BP or SP and Disp being offsetted accordingly. The displacement may 00036 /// also include the offset of a global value. 00037 struct X86AddressMode { 00038 enum { 00039 RegBase, 00040 FrameIndexBase 00041 } BaseType; 00042 00043 union { 00044 unsigned Reg; 00045 int FrameIndex; 00046 } Base; 00047 00048 unsigned Scale; 00049 unsigned IndexReg; 00050 unsigned Disp; 00051 GlobalValue *GV; 00052 unsigned GVOpFlags; 00053 00054 X86AddressMode() 00055 : BaseType(RegBase), Scale(1), IndexReg(0), Disp(0), GV(0), GVOpFlags(0) { 00056 Base.Reg = 0; 00057 } 00058 }; 00059 00060 /// addDirectMem - This function is used to add a direct memory reference to the 00061 /// current instruction -- that is, a dereference of an address in a register, 00062 /// with no scale, index or displacement. An example is: DWORD PTR [EAX]. 00063 /// 00064 static inline const MachineInstrBuilder & 00065 addDirectMem(const MachineInstrBuilder &MIB, unsigned Reg) { 00066 // Because memory references are always represented with four 00067 // values, this adds: Reg, [1, NoReg, 0] to the instruction. 00068 return MIB.addReg(Reg).addImm(1).addReg(0).addImm(0); 00069 } 00070 00071 static inline const MachineInstrBuilder & 00072 addLeaOffset(const MachineInstrBuilder &MIB, int Offset) { 00073 return MIB.addImm(1).addReg(0).addImm(Offset); 00074 } 00075 00076 static inline const MachineInstrBuilder & 00077 addOffset(const MachineInstrBuilder &MIB, int Offset) { 00078 return addLeaOffset(MIB, Offset).addReg(0); 00079 } 00080 00081 /// addRegOffset - This function is used to add a memory reference of the form 00082 /// [Reg + Offset], i.e., one with no scale or index, but with a 00083 /// displacement. An example is: DWORD PTR [EAX + 4]. 00084 /// 00085 static inline const MachineInstrBuilder & 00086 addRegOffset(const MachineInstrBuilder &MIB, 00087 unsigned Reg, bool isKill, int Offset) { 00088 return addOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset); 00089 } 00090 00091 static inline const MachineInstrBuilder & 00092 addLeaRegOffset(const MachineInstrBuilder &MIB, 00093 unsigned Reg, bool isKill, int Offset) { 00094 return addLeaOffset(MIB.addReg(Reg, getKillRegState(isKill)), Offset); 00095 } 00096 00097 /// addRegReg - This function is used to add a memory reference of the form: 00098 /// [Reg + Reg]. 00099 static inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB, 00100 unsigned Reg1, bool isKill1, 00101 unsigned Reg2, bool isKill2) { 00102 return MIB.addReg(Reg1, getKillRegState(isKill1)).addImm(1) 00103 .addReg(Reg2, getKillRegState(isKill2)).addImm(0); 00104 } 00105 00106 static inline const MachineInstrBuilder & 00107 addLeaAddress(const MachineInstrBuilder &MIB, const X86AddressMode &AM) { 00108 assert (AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8); 00109 00110 if (AM.BaseType == X86AddressMode::RegBase) 00111 MIB.addReg(AM.Base.Reg); 00112 else if (AM.BaseType == X86AddressMode::FrameIndexBase) 00113 MIB.addFrameIndex(AM.Base.FrameIndex); 00114 else 00115 assert (0); 00116 MIB.addImm(AM.Scale).addReg(AM.IndexReg); 00117 if (AM.GV) 00118 return MIB.addGlobalAddress(AM.GV, AM.Disp, AM.GVOpFlags); 00119 else 00120 return MIB.addImm(AM.Disp); 00121 } 00122 00123 static inline const MachineInstrBuilder & 00124 addFullAddress(const MachineInstrBuilder &MIB, 00125 const X86AddressMode &AM) { 00126 return addLeaAddress(MIB, AM).addReg(0); 00127 } 00128 00129 /// addFrameReference - This function is used to add a reference to the base of 00130 /// an abstract object on the stack frame of the current function. This 00131 /// reference has base register as the FrameIndex offset until it is resolved. 00132 /// This allows a constant offset to be specified as well... 00133 /// 00134 static inline const MachineInstrBuilder & 00135 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) { 00136 MachineInstr *MI = MIB; 00137 MachineFunction &MF = *MI->getParent()->getParent(); 00138 MachineFrameInfo &MFI = *MF.getFrameInfo(); 00139 const TargetInstrDesc &TID = MI->getDesc(); 00140 unsigned Flags = 0; 00141 if (TID.mayLoad()) 00142 Flags |= MachineMemOperand::MOLoad; 00143 if (TID.mayStore()) 00144 Flags |= MachineMemOperand::MOStore; 00145 MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FI), 00146 Flags, 00147 MFI.getObjectOffset(FI) + Offset, 00148 MFI.getObjectSize(FI), 00149 MFI.getObjectAlignment(FI)); 00150 return addOffset(MIB.addFrameIndex(FI), Offset) 00151 .addMemOperand(MMO); 00152 } 00153 00154 /// addConstantPoolReference - This function is used to add a reference to the 00155 /// base of a constant value spilled to the per-function constant pool. The 00156 /// reference uses the abstract ConstantPoolIndex which is retained until 00157 /// either machine code emission or assembly output. In PIC mode on x86-32, 00158 /// the GlobalBaseReg parameter can be used to make this a 00159 /// GlobalBaseReg-relative reference. 00160 /// 00161 static inline const MachineInstrBuilder & 00162 addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, 00163 unsigned GlobalBaseReg, unsigned char OpFlags) { 00164 //FIXME: factor this 00165 return MIB.addReg(GlobalBaseReg).addImm(1).addReg(0) 00166 .addConstantPoolIndex(CPI, 0, OpFlags).addReg(0); 00167 } 00168 00169 } // End llvm namespace 00170 00171 #endif