blob: 41289540ce129eefa0d1e2c6367b02cafed3d51a [file] [log] [blame]
Misha Brukmancd603132003-06-02 03:28:00 +00001//===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
Chris Lattner40ead952002-12-02 21:24:12 +00002//
3// This file contains the pass that transforms the X86 machine instructions into
4// actual executable machine code.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattnercb533582003-08-03 21:14:38 +00008#define DEBUG_TYPE "jit"
Chris Lattner40ead952002-12-02 21:24:12 +00009#include "X86TargetMachine.h"
Chris Lattnerea1ddab2002-12-03 06:34:06 +000010#include "X86.h"
Chris Lattner40ead952002-12-02 21:24:12 +000011#include "llvm/PassManager.h"
12#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattner5ae99fe2002-12-28 20:24:48 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner76041ce2002-12-02 21:44:34 +000014#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdbf30f72002-12-04 06:45:19 +000015#include "llvm/Value.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000016#include "Support/Debug.h"
Chris Lattner302de592003-06-06 04:00:05 +000017#include "Support/Statistic.h"
John Criswell7a73b802003-06-30 21:59:07 +000018#include "Config/alloca.h"
Chris Lattner40ead952002-12-02 21:24:12 +000019
20namespace {
Chris Lattner302de592003-06-06 04:00:05 +000021 Statistic<>
22 NumEmitted("x86-emitter", "Number of machine instructions emitted");
23
Chris Lattner04b0b302003-06-01 23:23:50 +000024 class JITResolver {
25 MachineCodeEmitter &MCE;
26
27 // LazyCodeGenMap - Keep track of call sites for functions that are to be
28 // lazily resolved.
29 std::map<unsigned, Function*> LazyCodeGenMap;
30
31 // LazyResolverMap - Keep track of the lazy resolver created for a
32 // particular function so that we can reuse them if necessary.
33 std::map<Function*, unsigned> LazyResolverMap;
34 public:
35 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {}
36 unsigned getLazyResolver(Function *F);
37 unsigned addFunctionReference(unsigned Address, Function *F);
38
39 private:
40 unsigned emitStubForFunction(Function *F);
41 static void CompilationCallback();
42 unsigned resolveFunctionReference(unsigned RetAddr);
43 };
44
45 JITResolver *TheJITResolver;
46}
47
48
49/// addFunctionReference - This method is called when we need to emit the
50/// address of a function that has not yet been emitted, so we don't know the
51/// address. Instead, we emit a call to the CompilationCallback method, and
52/// keep track of where we are.
53///
54unsigned JITResolver::addFunctionReference(unsigned Address, Function *F) {
55 LazyCodeGenMap[Address] = F;
56 return (intptr_t)&JITResolver::CompilationCallback;
57}
58
59unsigned JITResolver::resolveFunctionReference(unsigned RetAddr) {
60 std::map<unsigned, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
61 assert(I != LazyCodeGenMap.end() && "Not in map!");
62 Function *F = I->second;
63 LazyCodeGenMap.erase(I);
64 return MCE.forceCompilationOf(F);
65}
66
67unsigned JITResolver::getLazyResolver(Function *F) {
68 std::map<Function*, unsigned>::iterator I = LazyResolverMap.lower_bound(F);
69 if (I != LazyResolverMap.end() && I->first == F) return I->second;
70
71//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
72
73 unsigned Stub = emitStubForFunction(F);
74 LazyResolverMap.insert(I, std::make_pair(F, Stub));
75 return Stub;
76}
77
78void JITResolver::CompilationCallback() {
79 unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
Misha Brukmanbc80b222003-06-02 04:13:58 +000080 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(0);
Chris Lattner04b0b302003-06-01 23:23:50 +000081 assert(StackPtr[1] == RetAddr &&
82 "Could not find return address on the stack!");
Chris Lattner30d002b2003-06-06 18:25:33 +000083
84 // It's a stub if there is an interrupt marker after the call...
85 bool isStub = ((unsigned char*)(intptr_t)RetAddr)[0] == 0xCD;
Chris Lattner04b0b302003-06-01 23:23:50 +000086
Chris Lattner302de592003-06-06 04:00:05 +000087 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
88 // pointer elimination has been performed. Having a variable sized alloca
89 // disables frame pointer elimination currently, even if it's dead. This is a
90 // gross hack.
91 alloca(10+isStub);
92 // FIXME FIXME FIXME FIXME
93
Chris Lattner04b0b302003-06-01 23:23:50 +000094 // The call instruction should have pushed the return value onto the stack...
95 RetAddr -= 4; // Backtrack to the reference itself...
96
97#if 0
98 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << RetAddr
99 << " ESP=0x" << (unsigned)StackPtr << std::dec
100 << ": Resolving call to function: "
101 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
102#endif
103
104 // Sanity check to make sure this really is a call instruction...
Chris Lattner30d002b2003-06-06 18:25:33 +0000105 assert(((unsigned char*)(intptr_t)RetAddr)[-1] == 0xE8 &&"Not a call instr!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000106
107 unsigned NewVal = TheJITResolver->resolveFunctionReference(RetAddr);
108
109 // Rewrite the call target... so that we don't fault every time we execute
110 // the call.
Chris Lattner30d002b2003-06-06 18:25:33 +0000111 *(unsigned*)(intptr_t)RetAddr = NewVal-RetAddr-4;
Chris Lattner04b0b302003-06-01 23:23:50 +0000112
113 if (isStub) {
114 // If this is a stub, rewrite the call into an unconditional branch
115 // instruction so that two return addresses are not pushed onto the stack
116 // when the requested function finally gets called. This also makes the
117 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
Chris Lattner30d002b2003-06-06 18:25:33 +0000118 ((unsigned char*)(intptr_t)RetAddr)[-1] = 0xE9;
Chris Lattner04b0b302003-06-01 23:23:50 +0000119 }
120
121 // Change the return address to reexecute the call instruction...
122 StackPtr[1] -= 5;
123}
124
125/// emitStubForFunction - This method is used by the JIT when it needs to emit
126/// the address of a function for a function whose code has not yet been
127/// generated. In order to do this, it generates a stub which jumps to the lazy
128/// function compiler, which will eventually get fixed to call the function
129/// directly.
130///
131unsigned JITResolver::emitStubForFunction(Function *F) {
132 MCE.startFunctionStub(*F, 6);
133 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
134
135 unsigned Address = addFunctionReference(MCE.getCurrentPCValue(), F);
136 MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
137
138 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
139 return (intptr_t)MCE.finishFunctionStub(*F);
140}
141
142
143
144namespace {
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000145 class Emitter : public MachineFunctionPass {
146 const X86InstrInfo *II;
Chris Lattner8f04b092002-12-02 21:56:18 +0000147 MachineCodeEmitter &MCE;
Chris Lattnerdee12632003-07-26 23:06:00 +0000148 std::map<const BasicBlock*, unsigned> BasicBlockAddrs;
149 std::vector<std::pair<const BasicBlock*, unsigned> > BBRefs;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000150 public:
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000151 Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
Chris Lattner40ead952002-12-02 21:24:12 +0000152
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000153 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner76041ce2002-12-02 21:44:34 +0000154
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000155 virtual const char *getPassName() const {
156 return "X86 Machine Code Emitter";
157 }
158
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000159 private:
Chris Lattner76041ce2002-12-02 21:44:34 +0000160 void emitBasicBlock(MachineBasicBlock &MBB);
161 void emitInstruction(MachineInstr &MI);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000162
Chris Lattner04b0b302003-06-01 23:23:50 +0000163 void emitPCRelativeBlockAddress(BasicBlock *BB);
164 void emitMaybePCRelativeValue(unsigned Address, bool isPCRelative);
165 void emitGlobalAddressForCall(GlobalValue *GV);
166 void emitGlobalAddressForPtr(GlobalValue *GV);
167
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000168 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
169 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
170 void emitConstant(unsigned Val, unsigned Size);
171
172 void emitMemModRMByte(const MachineInstr &MI,
173 unsigned Op, unsigned RegOpcodeField);
174
Chris Lattner40ead952002-12-02 21:24:12 +0000175 };
176}
177
Chris Lattner40ead952002-12-02 21:24:12 +0000178/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
179/// machine code emitted. This uses a MAchineCodeEmitter object to handle
180/// actually outputting the machine code and resolving things like the address
181/// of functions. This method should returns true if machine code emission is
182/// not supported.
183///
184bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
185 MachineCodeEmitter &MCE) {
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000186 PM.add(new Emitter(MCE));
Chris Lattner40ead952002-12-02 21:24:12 +0000187 return false;
188}
Chris Lattner76041ce2002-12-02 21:44:34 +0000189
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000190bool Emitter::runOnMachineFunction(MachineFunction &MF) {
191 II = &((X86TargetMachine&)MF.getTarget()).getInstrInfo();
Chris Lattner76041ce2002-12-02 21:44:34 +0000192
193 MCE.startFunction(MF);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000194 MCE.emitConstantPool(MF.getConstantPool());
Chris Lattner76041ce2002-12-02 21:44:34 +0000195 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
196 emitBasicBlock(*I);
197 MCE.finishFunction(MF);
Chris Lattner04b0b302003-06-01 23:23:50 +0000198
199 // Resolve all forward branches now...
200 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
201 unsigned Location = BasicBlockAddrs[BBRefs[i].first];
202 unsigned Ref = BBRefs[i].second;
Chris Lattner30d002b2003-06-06 18:25:33 +0000203 *(unsigned*)(intptr_t)Ref = Location-Ref-4;
Chris Lattner04b0b302003-06-01 23:23:50 +0000204 }
205 BBRefs.clear();
206 BasicBlockAddrs.clear();
Chris Lattner76041ce2002-12-02 21:44:34 +0000207 return false;
208}
209
210void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000211 if (uint64_t Addr = MCE.getCurrentPCValue())
212 BasicBlockAddrs[MBB.getBasicBlock()] = Addr;
213
Chris Lattner76041ce2002-12-02 21:44:34 +0000214 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
215 emitInstruction(**I);
216}
217
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000218
Chris Lattner04b0b302003-06-01 23:23:50 +0000219/// emitPCRelativeBlockAddress - This method emits the PC relative address of
220/// the specified basic block, or if the basic block hasn't been emitted yet
221/// (because this is a forward branch), it keeps track of the information
222/// necessary to resolve this address later (and emits a dummy value).
223///
224void Emitter::emitPCRelativeBlockAddress(BasicBlock *BB) {
225 // FIXME: Emit backward branches directly
226 BBRefs.push_back(std::make_pair(BB, MCE.getCurrentPCValue()));
227 MCE.emitWord(0); // Emit a dummy value
228}
229
230/// emitMaybePCRelativeValue - Emit a 32-bit address which may be PC relative.
231///
232void Emitter::emitMaybePCRelativeValue(unsigned Address, bool isPCRelative) {
233 if (isPCRelative)
234 MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
235 else
236 MCE.emitWord(Address);
237}
238
239/// emitGlobalAddressForCall - Emit the specified address to the code stream
240/// assuming this is part of a function call, which is PC relative.
241///
242void Emitter::emitGlobalAddressForCall(GlobalValue *GV) {
243 // Get the address from the backend...
244 unsigned Address = MCE.getGlobalValueAddress(GV);
245
246 // If the machine code emitter doesn't know what the address IS yet, we have
247 // to take special measures.
248 //
249 if (Address == 0) {
250 // FIXME: this is JIT specific!
251 if (TheJITResolver == 0)
252 TheJITResolver = new JITResolver(MCE);
253 Address = TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
254 (Function*)GV);
255 }
256 emitMaybePCRelativeValue(Address, true);
257}
258
259/// emitGlobalAddress - Emit the specified address to the code stream assuming
260/// this is part of a "take the address of a global" instruction, which is not
261/// PC relative.
262///
263void Emitter::emitGlobalAddressForPtr(GlobalValue *GV) {
264 // Get the address from the backend...
265 unsigned Address = MCE.getGlobalValueAddress(GV);
266
267 // If the machine code emitter doesn't know what the address IS yet, we have
268 // to take special measures.
269 //
270 if (Address == 0) {
271 // FIXME: this is JIT specific!
272 if (TheJITResolver == 0)
273 TheJITResolver = new JITResolver(MCE);
274 Address = TheJITResolver->getLazyResolver((Function*)GV);
275 }
276
277 emitMaybePCRelativeValue(Address, false);
278}
279
280
281
Chris Lattnerff3261a2003-06-03 15:31:23 +0000282/// N86 namespace - Native X86 Register numbers... used by X86 backend.
283///
284namespace N86 {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000285 enum {
286 EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7
287 };
288}
289
290
291// getX86RegNum - This function maps LLVM register identifiers to their X86
292// specific numbering, which is used in various places encoding instructions.
293//
294static unsigned getX86RegNum(unsigned RegNo) {
295 switch(RegNo) {
296 case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
297 case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
298 case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
299 case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
300 case X86::ESP: case X86::SP: case X86::AH: return N86::ESP;
301 case X86::EBP: case X86::BP: case X86::CH: return N86::EBP;
302 case X86::ESI: case X86::SI: case X86::DH: return N86::ESI;
303 case X86::EDI: case X86::DI: case X86::BH: return N86::EDI;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000304
305 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
306 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
307 return RegNo-X86::ST0;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000308 default:
309 assert(RegNo >= MRegisterInfo::FirstVirtualRegister &&
310 "Unknown physical register!");
311 assert(0 && "Register allocator hasn't allocated reg correctly yet!");
312 return 0;
313 }
314}
315
316inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
317 unsigned RM) {
318 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
319 return RM | (RegOpcode << 3) | (Mod << 6);
320}
321
322void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
323 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
324}
325
326void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
327 // SIB byte is in the same format as the ModRMByte...
328 MCE.emitByte(ModRMByte(SS, Index, Base));
329}
330
331void Emitter::emitConstant(unsigned Val, unsigned Size) {
332 // Output the constant in little endian byte order...
333 for (unsigned i = 0; i != Size; ++i) {
334 MCE.emitByte(Val & 255);
335 Val >>= 8;
336 }
337}
338
339static bool isDisp8(int Value) {
340 return Value == (signed char)Value;
341}
342
343void Emitter::emitMemModRMByte(const MachineInstr &MI,
344 unsigned Op, unsigned RegOpcodeField) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000345 const MachineOperand &Disp = MI.getOperand(Op+3);
346 if (MI.getOperand(Op).isConstantPoolIndex()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000347 // Emit a direct address reference [disp32] where the displacement of the
348 // constant pool entry is controlled by the MCE.
Chris Lattnere831b6b2003-01-13 00:33:59 +0000349 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
350 unsigned Index = MI.getOperand(Op).getConstantPoolIndex();
Chris Lattner04b0b302003-06-01 23:23:50 +0000351 unsigned Address = MCE.getConstantPoolEntryAddress(Index);
352 MCE.emitWord(Address+Disp.getImmedValue());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000353 return;
354 }
355
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000356 const MachineOperand &BaseReg = MI.getOperand(Op);
357 const MachineOperand &Scale = MI.getOperand(Op+1);
358 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000359
360 // Is a SIB byte needed?
361 if (IndexReg.getReg() == 0 && BaseReg.getReg() != X86::ESP) {
362 if (BaseReg.getReg() == 0) { // Just a displacement?
363 // Emit special case [disp32] encoding
364 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
365 emitConstant(Disp.getImmedValue(), 4);
366 } else {
367 unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
368 if (Disp.getImmedValue() == 0 && BaseRegNo != N86::EBP) {
369 // Emit simple indirect register encoding... [EAX] f.e.
370 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
371 } else if (isDisp8(Disp.getImmedValue())) {
372 // Emit the disp8 encoding... [REG+disp8]
373 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
374 emitConstant(Disp.getImmedValue(), 1);
375 } else {
376 // Emit the most general non-SIB encoding: [REG+disp32]
Chris Lattner20671842002-12-13 05:05:05 +0000377 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000378 emitConstant(Disp.getImmedValue(), 4);
379 }
380 }
381
382 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
383 assert(IndexReg.getReg() != X86::ESP && "Cannot use ESP as index reg!");
384
385 bool ForceDisp32 = false;
Brian Gaeke95780cc2002-12-13 07:56:18 +0000386 bool ForceDisp8 = false;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000387 if (BaseReg.getReg() == 0) {
388 // If there is no base register, we emit the special case SIB byte with
389 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
390 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
391 ForceDisp32 = true;
Brian Gaeke95780cc2002-12-13 07:56:18 +0000392 } else if (Disp.getImmedValue() == 0 && BaseReg.getReg() != X86::EBP) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000393 // Emit no displacement ModR/M byte
394 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
395 } else if (isDisp8(Disp.getImmedValue())) {
396 // Emit the disp8 encoding...
397 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
Brian Gaeke95780cc2002-12-13 07:56:18 +0000398 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000399 } else {
400 // Emit the normal disp32 encoding...
401 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
402 }
403
404 // Calculate what the SS field value should be...
405 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
406 unsigned SS = SSTable[Scale.getImmedValue()];
407
408 if (BaseReg.getReg() == 0) {
409 // Handle the SIB byte for the case where there is no base. The
410 // displacement has already been output.
411 assert(IndexReg.getReg() && "Index register must be specified!");
412 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
413 } else {
414 unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000415 unsigned IndexRegNo;
416 if (IndexReg.getReg())
417 IndexRegNo = getX86RegNum(IndexReg.getReg());
418 else
419 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000420 emitSIBByte(SS, IndexRegNo, BaseRegNo);
421 }
422
423 // Do we need to output a displacement?
Brian Gaeke95780cc2002-12-13 07:56:18 +0000424 if (Disp.getImmedValue() != 0 || ForceDisp32 || ForceDisp8) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000425 if (!ForceDisp32 && isDisp8(Disp.getImmedValue()))
426 emitConstant(Disp.getImmedValue(), 1);
427 else
428 emitConstant(Disp.getImmedValue(), 4);
429 }
430 }
431}
432
Chris Lattner04b0b302003-06-01 23:23:50 +0000433static unsigned sizeOfPtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000434 switch (Desc.TSFlags & X86II::ArgMask) {
435 case X86II::Arg8: return 1;
436 case X86II::Arg16: return 2;
437 case X86II::Arg32: return 4;
Chris Lattner5ada8df2002-12-25 05:09:21 +0000438 case X86II::ArgF32: return 4;
439 case X86II::ArgF64: return 8;
440 case X86II::ArgF80: return 10;
Chris Lattnera6a382c2002-12-13 03:50:13 +0000441 default: assert(0 && "Memory size not set!");
Chris Lattnerdf642e12002-12-20 04:12:48 +0000442 return 0;
Misha Brukman5000e432002-12-13 02:13:15 +0000443 }
444}
445
Chris Lattner76041ce2002-12-02 21:44:34 +0000446void Emitter::emitInstruction(MachineInstr &MI) {
Chris Lattner302de592003-06-06 04:00:05 +0000447 NumEmitted++; // Keep track of the # of mi's emitted
448
Chris Lattner76041ce2002-12-02 21:44:34 +0000449 unsigned Opcode = MI.getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000450 const TargetInstrDescriptor &Desc = II->get(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000451
452 // Emit instruction prefixes if neccesary
453 if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
Chris Lattner5ada8df2002-12-25 05:09:21 +0000454
455 switch (Desc.TSFlags & X86II::Op0Mask) {
456 case X86II::TB:
457 MCE.emitByte(0x0F); // Two-byte opcode prefix
458 break;
459 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
460 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
Chris Lattnere831b6b2003-01-13 00:33:59 +0000461 MCE.emitByte(0xD8+
462 (((Desc.TSFlags & X86II::Op0Mask)-X86II::D8)
463 >> X86II::Op0Shift));
Chris Lattner5ada8df2002-12-25 05:09:21 +0000464 break; // Two-byte opcode prefix
Chris Lattnere831b6b2003-01-13 00:33:59 +0000465 default: assert(0 && "Invalid prefix!");
466 case 0: break; // No prefix!
Chris Lattner5ada8df2002-12-25 05:09:21 +0000467 }
Chris Lattner76041ce2002-12-02 21:44:34 +0000468
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000469 unsigned char BaseOpcode = II->getBaseOpcodeFor(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000470 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000471 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner5ada8df2002-12-25 05:09:21 +0000472 case X86II::Pseudo:
Chris Lattnerc2489032003-05-07 19:21:28 +0000473 if (Opcode != X86::IMPLICIT_USE)
Chris Lattner9dedbcc2003-05-06 21:31:47 +0000474 std::cerr << "X86 Machine Code Emitter: No 'form', not emitting: " << MI;
Chris Lattner5ada8df2002-12-25 05:09:21 +0000475 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000476
Chris Lattner76041ce2002-12-02 21:44:34 +0000477 case X86II::RawFrm:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000478 MCE.emitByte(BaseOpcode);
Chris Lattner8f04b092002-12-02 21:56:18 +0000479 if (MI.getNumOperands() == 1) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000480 MachineOperand &MO = MI.getOperand(0);
481 if (MO.isPCRelativeDisp()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000482 // Conditional branch... FIXME: this should use an MBB destination!
483 emitPCRelativeBlockAddress(cast<BasicBlock>(MO.getVRegValue()));
Chris Lattnere831b6b2003-01-13 00:33:59 +0000484 } else if (MO.isGlobalAddress()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000485 assert(MO.isPCRelative() && "Call target is not PC Relative?");
486 emitGlobalAddressForCall(MO.getGlobal());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000487 } else if (MO.isExternalSymbol()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000488 unsigned Address = MCE.getGlobalValueAddress(MO.getSymbolName());
489 assert(Address && "Unknown external symbol!");
490 emitMaybePCRelativeValue(Address, MO.isPCRelative());
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000491 } else {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000492 assert(0 && "Unknown RawFrm operand!");
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000493 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000494 }
495 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000496
497 case X86II::AddRegFrm:
498 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(0).getReg()));
499 if (MI.getNumOperands() == 2) {
500 MachineOperand &MO1 = MI.getOperand(1);
501 if (MO1.isImmediate() || MO1.getVRegValueOrNull() ||
502 MO1.isGlobalAddress() || MO1.isExternalSymbol()) {
503 unsigned Size = sizeOfPtr(Desc);
504 if (Value *V = MO1.getVRegValueOrNull()) {
505 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000506 emitGlobalAddressForPtr(cast<GlobalValue>(V));
Chris Lattnere831b6b2003-01-13 00:33:59 +0000507 } else if (MO1.isGlobalAddress()) {
508 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000509 assert(!MO1.isPCRelative() && "Function pointer ref is PC relative?");
510 emitGlobalAddressForPtr(MO1.getGlobal());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000511 } else if (MO1.isExternalSymbol()) {
512 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000513
514 unsigned Address = MCE.getGlobalValueAddress(MO1.getSymbolName());
515 assert(Address && "Unknown external symbol!");
516 emitMaybePCRelativeValue(Address, MO1.isPCRelative());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000517 } else {
518 emitConstant(MO1.getImmedValue(), Size);
519 }
520 }
521 }
522 break;
523
524 case X86II::MRMDestReg: {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000525 MCE.emitByte(BaseOpcode);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000526 MachineOperand &SrcOp = MI.getOperand(1+II->isTwoAddrInstr(Opcode));
527 emitRegModRMByte(MI.getOperand(0).getReg(), getX86RegNum(SrcOp.getReg()));
528 if (MI.getNumOperands() == 4)
529 emitConstant(MI.getOperand(3).getImmedValue(), sizeOfPtr(Desc));
Chris Lattner9dedbcc2003-05-06 21:31:47 +0000530 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000531 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000532 case X86II::MRMDestMem:
533 MCE.emitByte(BaseOpcode);
534 emitMemModRMByte(MI, 0, getX86RegNum(MI.getOperand(4).getReg()));
535 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000536
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000537 case X86II::MRMSrcReg:
538 MCE.emitByte(BaseOpcode);
539 emitRegModRMByte(MI.getOperand(MI.getNumOperands()-1).getReg(),
540 getX86RegNum(MI.getOperand(0).getReg()));
541 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000542
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000543 case X86II::MRMSrcMem:
544 MCE.emitByte(BaseOpcode);
545 emitMemModRMByte(MI, MI.getNumOperands()-4,
546 getX86RegNum(MI.getOperand(0).getReg()));
547 break;
548
549 case X86II::MRMS0r: case X86II::MRMS1r:
550 case X86II::MRMS2r: case X86II::MRMS3r:
551 case X86II::MRMS4r: case X86II::MRMS5r:
552 case X86II::MRMS6r: case X86II::MRMS7r:
553 MCE.emitByte(BaseOpcode);
554 emitRegModRMByte(MI.getOperand(0).getReg(),
555 (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0r);
556
Chris Lattnerd9096832002-12-15 08:01:39 +0000557 if (MI.getOperand(MI.getNumOperands()-1).isImmediate()) {
Misha Brukman5000e432002-12-13 02:13:15 +0000558 unsigned Size = sizeOfPtr(Desc);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000559 emitConstant(MI.getOperand(MI.getNumOperands()-1).getImmedValue(), Size);
560 }
561 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000562
563 case X86II::MRMS0m: case X86II::MRMS1m:
564 case X86II::MRMS2m: case X86II::MRMS3m:
565 case X86II::MRMS4m: case X86II::MRMS5m:
566 case X86II::MRMS6m: case X86II::MRMS7m:
567 MCE.emitByte(BaseOpcode);
568 emitMemModRMByte(MI, 0, (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0m);
569
570 if (MI.getNumOperands() == 5) {
571 unsigned Size = sizeOfPtr(Desc);
572 emitConstant(MI.getOperand(4).getImmedValue(), Size);
573 }
574 break;
Chris Lattner76041ce2002-12-02 21:44:34 +0000575 }
576}