blob: 91a1069757e038b30f2290c7488ce50dcda9b282 [file] [log] [blame]
Chris Lattnerc682b4a2002-11-17 21:03:35 +00001//===-- X86InstrBuilder.h - Functions to aid building x86 insts -*- C++ -*-===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattnerc682b4a2002-11-17 21:03:35 +00009//
10// This file exposes functions that may be used with BuildMI from the
11// MachineInstrBuilder.h file to handle X86'isms in a clean way.
12//
13// The BuildMem function may be used with the BuildMI function to add entire
14// memory references in a single, typed, function call. X86 memory references
15// can be very complex expressions (described in the README), so wrapping them
16// up behind an easier to use interface makes sense. Descriptions of the
17// functions are included below.
18//
Brian Gaekeeaeacc52002-12-13 09:28:50 +000019// For reference, the order of operands for memory references is:
20// (Operand), Base, Scale, Index, Displacement.
21//
Chris Lattnerc682b4a2002-11-17 21:03:35 +000022//===----------------------------------------------------------------------===//
23
24#ifndef X86INSTRBUILDER_H
25#define X86INSTRBUILDER_H
26
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28
Brian Gaeke960707c2003-11-11 22:41:34 +000029namespace llvm {
30
Reid Spencer8aca0b42004-08-30 00:13:26 +000031/// X86AddressMode - This struct holds a generalized full x86 address mode.
32/// The base register can be a frame index, which will eventually be replaced
Chris Lattnerdf7b9842004-10-15 04:43:20 +000033/// with BP or SP and Disp being offsetted accordingly. The displacement may
34/// also include the offset of a global value.
Reid Spencer8aca0b42004-08-30 00:13:26 +000035struct X86AddressMode {
Chris Lattnerb93409f2005-01-17 23:25:45 +000036 enum {
37 RegBase,
38 FrameIndexBase,
39 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +000040
Chris Lattnerb93409f2005-01-17 23:25:45 +000041 union {
42 unsigned Reg;
43 int FrameIndex;
44 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +000045
Chris Lattnerb93409f2005-01-17 23:25:45 +000046 unsigned Scale;
47 unsigned IndexReg;
48 unsigned Disp;
49 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +000050
Chris Lattnerb93409f2005-01-17 23:25:45 +000051 X86AddressMode() : BaseType(RegBase), Scale(1), IndexReg(0), Disp(0), GV(0) {
52 Base.Reg = 0;
53 }
Reid Spencer8aca0b42004-08-30 00:13:26 +000054};
55
Chris Lattnerc682b4a2002-11-17 21:03:35 +000056/// addDirectMem - This function is used to add a direct memory reference to the
Chris Lattneref5a8f92002-12-28 20:26:58 +000057/// current instruction -- that is, a dereference of an address in a register,
58/// with no scale, index or displacement. An example is: DWORD PTR [EAX].
59///
Chris Lattnerc682b4a2002-11-17 21:03:35 +000060inline const MachineInstrBuilder &addDirectMem(const MachineInstrBuilder &MIB,
61 unsigned Reg) {
Brian Gaekeeaeacc52002-12-13 09:28:50 +000062 // Because memory references are always represented with four
63 // values, this adds: Reg, [1, NoReg, 0] to the instruction.
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000064 return MIB.addReg(Reg).addZImm(1).addReg(0).addImm(0);
Chris Lattnerc682b4a2002-11-17 21:03:35 +000065}
66
Misha Brukman4ea94a42002-11-22 22:42:12 +000067
Chris Lattneref5a8f92002-12-28 20:26:58 +000068/// addRegOffset - This function is used to add a memory reference of the form
69/// [Reg + Offset], i.e., one with no scale or index, but with a
70/// displacement. An example is: DWORD PTR [EAX + 4].
71///
Misha Brukman4ea94a42002-11-22 22:42:12 +000072inline const MachineInstrBuilder &addRegOffset(const MachineInstrBuilder &MIB,
Chris Lattneref5a8f92002-12-28 20:26:58 +000073 unsigned Reg, int Offset) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000074 return MIB.addReg(Reg).addZImm(1).addReg(0).addImm(Offset);
Misha Brukman4ea94a42002-11-22 22:42:12 +000075}
76
Chris Lattnere7228732005-01-02 02:38:18 +000077/// addRegReg - This function is used to add a memory reference of the form:
78/// [Reg + Reg].
79inline const MachineInstrBuilder &addRegReg(const MachineInstrBuilder &MIB,
80 unsigned Reg1, unsigned Reg2) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000081 return MIB.addReg(Reg1).addZImm(1).addReg(Reg2).addImm(0);
Chris Lattnere7228732005-01-02 02:38:18 +000082}
83
Chris Lattner4b3514c2004-02-25 06:01:07 +000084inline const MachineInstrBuilder &addFullAddress(const MachineInstrBuilder &MIB,
Reid Spencer8aca0b42004-08-30 00:13:26 +000085 const X86AddressMode &AM) {
86 assert (AM.Scale == 1 || AM.Scale == 2 || AM.Scale == 4 || AM.Scale == 8);
87
88 if (AM.BaseType == X86AddressMode::RegBase)
89 MIB.addReg(AM.Base.Reg);
90 else if (AM.BaseType == X86AddressMode::FrameIndexBase)
91 MIB.addFrameIndex(AM.Base.FrameIndex);
92 else
93 assert (0);
Chris Lattnerdf7b9842004-10-15 04:43:20 +000094 MIB.addZImm(AM.Scale).addReg(AM.IndexReg);
95 if (AM.GV)
Chris Lattner940cc972006-05-04 01:15:02 +000096 return MIB.addGlobalAddress(AM.GV, AM.Disp);
Chris Lattnerdf7b9842004-10-15 04:43:20 +000097 else
Chris Lattnerfef7a2d2006-05-04 17:21:20 +000098 return MIB.addImm(AM.Disp);
Chris Lattner4b3514c2004-02-25 06:01:07 +000099}
100
Chris Lattneref5a8f92002-12-28 20:26:58 +0000101/// addFrameReference - This function is used to add a reference to the base of
102/// an abstract object on the stack frame of the current function. This
Chris Lattner78785632003-01-13 00:45:53 +0000103/// reference has base register as the FrameIndex offset until it is resolved.
104/// This allows a constant offset to be specified as well...
Chris Lattneref5a8f92002-12-28 20:26:58 +0000105///
106inline const MachineInstrBuilder &
Chris Lattner78785632003-01-13 00:45:53 +0000107addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000108 return MIB.addFrameIndex(FI).addZImm(1).addReg(0).addImm(Offset);
Chris Lattner78785632003-01-13 00:45:53 +0000109}
110
111/// addConstantPoolReference - This function is used to add a reference to the
112/// base of a constant value spilled to the per-function constant pool. The
113/// reference has base register ConstantPoolIndex offset which is retained until
114/// either machine code emission or assembly output. This allows an optional
115/// offset to be added as well.
116///
117inline const MachineInstrBuilder &
118addConstantPoolReference(const MachineInstrBuilder &MIB, unsigned CPI,
Misha Brukman53d84902003-10-22 03:10:26 +0000119 int Offset = 0) {
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000120 return MIB.addConstantPoolIndex(CPI).addZImm(1).addReg(0).addImm(Offset);
Chris Lattneref5a8f92002-12-28 20:26:58 +0000121}
122
Brian Gaeke960707c2003-11-11 22:41:34 +0000123} // End llvm namespace
124
Chris Lattnerc682b4a2002-11-17 21:03:35 +0000125#endif