LLVM API Documentation
00001 //===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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/MachineInstrBuilder.h" 00028 00029 namespace llvm { 00030 00031 /// X86AddressMode - This struct holds a generalized full x86 address mode. 00032 /// The base register can be a frame index, which will eventually be replaced 00033 /// with BP or SP and Disp being offsetted accordingly. The displacement may 00034 /// also include the offset of a global value. 00035 struct X86AddressMode { 00036 enum { 00037 UnknownBase, 00038 RegBase, 00039 FrameIndexBase, 00040 } BaseType; 00041 00042 union { 00043 unsigned Reg; 00044 int FrameIndex; 00045 } Base; 00046 00047 unsigned Scale; 00048 unsigned IndexReg; 00049 unsigned Disp; 00050 GlobalValue *GV; 00051 00052 X86AddressMode() : BaseType(UnknownBase), GV(NULL) {} 00053 }; 00054 00055 /// addDirectMem - This function is used to add a direct memory reference to the 00056 /// current instruction -- that is, a dereference of an address in a register, 00057 /// with no scale, index or displacement. An example is: DWORD PTR [EAX]. 00058 /// 00059 inline const MachineInstrBuilder &addDirectMem(const MachineInstrBuilder &MIB, 00060 unsigned Reg) { 00061 // Because memory references are always represented with four 00062 // values, this adds: Reg, [1, NoReg, 0] to the instruction. 00063 return MIB.addReg(Reg).addZImm(1).addReg(0).addSImm(0); 00064 } 00065 00066 00067 /// addRegOffset - This function is used to add a memory reference of the form 00068 /// [Reg + Offset], i.e., one with no scale or index, but with a 00069 /// displacement. An example is: DWORD PTR [EAX + 4]. 00070 /// 00071 inline const MachineInstrBuilder &addRegOffset(const MachineInstrBuilder &MIB, 00072 unsigned Reg, int Offset) { 00073 return MIB.addReg(Reg).addZImm(1).addReg(0).addSImm(Offset); 00074 } 00075 00076 inline const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB, 00077 const X86AddressMode &AM) { 00078 assert (AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8); 00079 00080 if (AM.BaseType == X86AddressMode::RegBase) 00081 MIB.addReg(AM.Base.Reg); 00082 else if (AM.BaseType == X86AddressMode::FrameIndexBase) 00083 MIB.addFrameIndex(AM.Base.FrameIndex); 00084 else 00085 assert (0); 00086 MIB.addZImm(AM.Scale).addReg(AM.IndexReg); 00087 if (AM.GV) 00088 return MIB.addGlobalAddress(AM.GV, false, AM.Disp); 00089 else 00090 return MIB.addSImm(AM.Disp); 00091 } 00092 00093 /// addFrameReference - This function is used to add a reference to the base of 00094 /// an abstract object on the stack frame of the current function. This 00095 /// reference has base register as the FrameIndex offset until it is resolved. 00096 /// This allows a constant offset to be specified as well... 00097 /// 00098 inline const MachineInstrBuilder & 00099 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) { 00100 return MIB.addFrameIndex(FI).addZImm(1).addReg(0).addSImm(Offset); 00101 } 00102 00103 /// addConstantPoolReference - This function is used to add a reference to the 00104 /// base of a constant value spilled to the per-function constant pool. The 00105 /// reference has base register ConstantPoolIndex offset which is retained until 00106 /// either machine code emission or assembly output. This allows an optional 00107 /// offset to be added as well. 00108 /// 00109 inline const MachineInstrBuilder & 00110 addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI, 00111 int Offset = 0) { 00112 return MIB.addConstantPoolIndex(CPI).addZImm(1).addReg(0).addSImm(Offset); 00113 } 00114 00115 } // End llvm namespace 00116 00117 #endif