blob: 3b7fb4c148b2ba83a119492a538c87a08c84037d [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
8#include "X86TargetMachine.h"
Chris Lattnerea1ddab2002-12-03 06:34:06 +00009#include "X86.h"
Chris Lattner40ead952002-12-02 21:24:12 +000010#include "llvm/PassManager.h"
11#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattner5ae99fe2002-12-28 20:24:48 +000012#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner76041ce2002-12-02 21:44:34 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerdbf30f72002-12-04 06:45:19 +000014#include "llvm/Value.h"
Chris Lattner40ead952002-12-02 21:24:12 +000015
16namespace {
Chris Lattner04b0b302003-06-01 23:23:50 +000017 class JITResolver {
18 MachineCodeEmitter &MCE;
19
20 // LazyCodeGenMap - Keep track of call sites for functions that are to be
21 // lazily resolved.
22 std::map<unsigned, Function*> LazyCodeGenMap;
23
24 // LazyResolverMap - Keep track of the lazy resolver created for a
25 // particular function so that we can reuse them if necessary.
26 std::map<Function*, unsigned> LazyResolverMap;
27 public:
28 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {}
29 unsigned getLazyResolver(Function *F);
30 unsigned addFunctionReference(unsigned Address, Function *F);
31
32 private:
33 unsigned emitStubForFunction(Function *F);
34 static void CompilationCallback();
35 unsigned resolveFunctionReference(unsigned RetAddr);
36 };
37
38 JITResolver *TheJITResolver;
39}
40
41
42/// addFunctionReference - This method is called when we need to emit the
43/// address of a function that has not yet been emitted, so we don't know the
44/// address. Instead, we emit a call to the CompilationCallback method, and
45/// keep track of where we are.
46///
47unsigned JITResolver::addFunctionReference(unsigned Address, Function *F) {
48 LazyCodeGenMap[Address] = F;
49 return (intptr_t)&JITResolver::CompilationCallback;
50}
51
52unsigned JITResolver::resolveFunctionReference(unsigned RetAddr) {
53 std::map<unsigned, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
54 assert(I != LazyCodeGenMap.end() && "Not in map!");
55 Function *F = I->second;
56 LazyCodeGenMap.erase(I);
57 return MCE.forceCompilationOf(F);
58}
59
60unsigned JITResolver::getLazyResolver(Function *F) {
61 std::map<Function*, unsigned>::iterator I = LazyResolverMap.lower_bound(F);
62 if (I != LazyResolverMap.end() && I->first == F) return I->second;
63
64//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
65
66 unsigned Stub = emitStubForFunction(F);
67 LazyResolverMap.insert(I, std::make_pair(F, Stub));
68 return Stub;
69}
70
71void JITResolver::CompilationCallback() {
72 unsigned *StackPtr = (unsigned*)__builtin_frame_address(0);
Misha Brukmanbc80b222003-06-02 04:13:58 +000073 unsigned RetAddr = (unsigned)(intptr_t)__builtin_return_address(0);
Chris Lattner04b0b302003-06-01 23:23:50 +000074
75 assert(StackPtr[1] == RetAddr &&
76 "Could not find return address on the stack!");
77 bool isStub = ((unsigned char*)RetAddr)[0] == 0xCD; // Interrupt marker?
78
79 // The call instruction should have pushed the return value onto the stack...
80 RetAddr -= 4; // Backtrack to the reference itself...
81
82#if 0
83 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << RetAddr
84 << " ESP=0x" << (unsigned)StackPtr << std::dec
85 << ": Resolving call to function: "
86 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n");
87#endif
88
89 // Sanity check to make sure this really is a call instruction...
90 assert(((unsigned char*)RetAddr)[-1] == 0xE8 && "Not a call instr!");
91
92 unsigned NewVal = TheJITResolver->resolveFunctionReference(RetAddr);
93
94 // Rewrite the call target... so that we don't fault every time we execute
95 // the call.
96 *(unsigned*)RetAddr = NewVal-RetAddr-4;
97
98 if (isStub) {
99 // If this is a stub, rewrite the call into an unconditional branch
100 // instruction so that two return addresses are not pushed onto the stack
101 // when the requested function finally gets called. This also makes the
102 // 0xCD byte (interrupt) dead, so the marker doesn't effect anything.
103 ((unsigned char*)RetAddr)[-1] = 0xE9;
104 }
105
106 // Change the return address to reexecute the call instruction...
107 StackPtr[1] -= 5;
108}
109
110/// emitStubForFunction - This method is used by the JIT when it needs to emit
111/// the address of a function for a function whose code has not yet been
112/// generated. In order to do this, it generates a stub which jumps to the lazy
113/// function compiler, which will eventually get fixed to call the function
114/// directly.
115///
116unsigned JITResolver::emitStubForFunction(Function *F) {
117 MCE.startFunctionStub(*F, 6);
118 MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination...
119
120 unsigned Address = addFunctionReference(MCE.getCurrentPCValue(), F);
121 MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
122
123 MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub!
124 return (intptr_t)MCE.finishFunctionStub(*F);
125}
126
127
128
129namespace {
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000130 class Emitter : public MachineFunctionPass {
131 const X86InstrInfo *II;
Chris Lattner8f04b092002-12-02 21:56:18 +0000132 MachineCodeEmitter &MCE;
Chris Lattner04b0b302003-06-01 23:23:50 +0000133 std::map<BasicBlock*, unsigned> BasicBlockAddrs;
134 std::vector<std::pair<BasicBlock*, unsigned> > BBRefs;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000135 public:
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000136 Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
Chris Lattner40ead952002-12-02 21:24:12 +0000137
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000138 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner76041ce2002-12-02 21:44:34 +0000139
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000140 virtual const char *getPassName() const {
141 return "X86 Machine Code Emitter";
142 }
143
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000144 private:
Chris Lattner76041ce2002-12-02 21:44:34 +0000145 void emitBasicBlock(MachineBasicBlock &MBB);
146 void emitInstruction(MachineInstr &MI);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000147
Chris Lattner04b0b302003-06-01 23:23:50 +0000148 void emitPCRelativeBlockAddress(BasicBlock *BB);
149 void emitMaybePCRelativeValue(unsigned Address, bool isPCRelative);
150 void emitGlobalAddressForCall(GlobalValue *GV);
151 void emitGlobalAddressForPtr(GlobalValue *GV);
152
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000153 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
154 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
155 void emitConstant(unsigned Val, unsigned Size);
156
157 void emitMemModRMByte(const MachineInstr &MI,
158 unsigned Op, unsigned RegOpcodeField);
159
Chris Lattner40ead952002-12-02 21:24:12 +0000160 };
161}
162
Chris Lattner40ead952002-12-02 21:24:12 +0000163/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
164/// machine code emitted. This uses a MAchineCodeEmitter object to handle
165/// actually outputting the machine code and resolving things like the address
166/// of functions. This method should returns true if machine code emission is
167/// not supported.
168///
169bool X86TargetMachine::addPassesToEmitMachineCode(PassManager &PM,
170 MachineCodeEmitter &MCE) {
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000171 PM.add(new Emitter(MCE));
Chris Lattner40ead952002-12-02 21:24:12 +0000172 return false;
173}
Chris Lattner76041ce2002-12-02 21:44:34 +0000174
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000175bool Emitter::runOnMachineFunction(MachineFunction &MF) {
176 II = &((X86TargetMachine&)MF.getTarget()).getInstrInfo();
Chris Lattner76041ce2002-12-02 21:44:34 +0000177
178 MCE.startFunction(MF);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000179 MCE.emitConstantPool(MF.getConstantPool());
Chris Lattner76041ce2002-12-02 21:44:34 +0000180 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
181 emitBasicBlock(*I);
182 MCE.finishFunction(MF);
Chris Lattner04b0b302003-06-01 23:23:50 +0000183
184 // Resolve all forward branches now...
185 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
186 unsigned Location = BasicBlockAddrs[BBRefs[i].first];
187 unsigned Ref = BBRefs[i].second;
188 *(unsigned*)Ref = Location-Ref-4;
189 }
190 BBRefs.clear();
191 BasicBlockAddrs.clear();
Chris Lattner76041ce2002-12-02 21:44:34 +0000192 return false;
193}
194
195void Emitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000196 if (uint64_t Addr = MCE.getCurrentPCValue())
197 BasicBlockAddrs[MBB.getBasicBlock()] = Addr;
198
Chris Lattner76041ce2002-12-02 21:44:34 +0000199 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
200 emitInstruction(**I);
201}
202
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000203
Chris Lattner04b0b302003-06-01 23:23:50 +0000204/// emitPCRelativeBlockAddress - This method emits the PC relative address of
205/// the specified basic block, or if the basic block hasn't been emitted yet
206/// (because this is a forward branch), it keeps track of the information
207/// necessary to resolve this address later (and emits a dummy value).
208///
209void Emitter::emitPCRelativeBlockAddress(BasicBlock *BB) {
210 // FIXME: Emit backward branches directly
211 BBRefs.push_back(std::make_pair(BB, MCE.getCurrentPCValue()));
212 MCE.emitWord(0); // Emit a dummy value
213}
214
215/// emitMaybePCRelativeValue - Emit a 32-bit address which may be PC relative.
216///
217void Emitter::emitMaybePCRelativeValue(unsigned Address, bool isPCRelative) {
218 if (isPCRelative)
219 MCE.emitWord(Address-MCE.getCurrentPCValue()-4);
220 else
221 MCE.emitWord(Address);
222}
223
224/// emitGlobalAddressForCall - Emit the specified address to the code stream
225/// assuming this is part of a function call, which is PC relative.
226///
227void Emitter::emitGlobalAddressForCall(GlobalValue *GV) {
228 // Get the address from the backend...
229 unsigned Address = MCE.getGlobalValueAddress(GV);
230
231 // If the machine code emitter doesn't know what the address IS yet, we have
232 // to take special measures.
233 //
234 if (Address == 0) {
235 // FIXME: this is JIT specific!
236 if (TheJITResolver == 0)
237 TheJITResolver = new JITResolver(MCE);
238 Address = TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
239 (Function*)GV);
240 }
241 emitMaybePCRelativeValue(Address, true);
242}
243
244/// emitGlobalAddress - Emit the specified address to the code stream assuming
245/// this is part of a "take the address of a global" instruction, which is not
246/// PC relative.
247///
248void Emitter::emitGlobalAddressForPtr(GlobalValue *GV) {
249 // Get the address from the backend...
250 unsigned Address = MCE.getGlobalValueAddress(GV);
251
252 // If the machine code emitter doesn't know what the address IS yet, we have
253 // to take special measures.
254 //
255 if (Address == 0) {
256 // FIXME: this is JIT specific!
257 if (TheJITResolver == 0)
258 TheJITResolver = new JITResolver(MCE);
259 Address = TheJITResolver->getLazyResolver((Function*)GV);
260 }
261
262 emitMaybePCRelativeValue(Address, false);
263}
264
265
266
267
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000268namespace N86 { // Native X86 Register numbers...
269 enum {
270 EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7
271 };
272}
273
274
275// getX86RegNum - This function maps LLVM register identifiers to their X86
276// specific numbering, which is used in various places encoding instructions.
277//
278static unsigned getX86RegNum(unsigned RegNo) {
279 switch(RegNo) {
280 case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
281 case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
282 case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
283 case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
284 case X86::ESP: case X86::SP: case X86::AH: return N86::ESP;
285 case X86::EBP: case X86::BP: case X86::CH: return N86::EBP;
286 case X86::ESI: case X86::SI: case X86::DH: return N86::ESI;
287 case X86::EDI: case X86::DI: case X86::BH: return N86::EDI;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000288
289 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
290 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
291 return RegNo-X86::ST0;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000292 default:
293 assert(RegNo >= MRegisterInfo::FirstVirtualRegister &&
294 "Unknown physical register!");
295 assert(0 && "Register allocator hasn't allocated reg correctly yet!");
296 return 0;
297 }
298}
299
300inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
301 unsigned RM) {
302 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
303 return RM | (RegOpcode << 3) | (Mod << 6);
304}
305
306void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
307 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
308}
309
310void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
311 // SIB byte is in the same format as the ModRMByte...
312 MCE.emitByte(ModRMByte(SS, Index, Base));
313}
314
315void Emitter::emitConstant(unsigned Val, unsigned Size) {
316 // Output the constant in little endian byte order...
317 for (unsigned i = 0; i != Size; ++i) {
318 MCE.emitByte(Val & 255);
319 Val >>= 8;
320 }
321}
322
323static bool isDisp8(int Value) {
324 return Value == (signed char)Value;
325}
326
327void Emitter::emitMemModRMByte(const MachineInstr &MI,
328 unsigned Op, unsigned RegOpcodeField) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000329 const MachineOperand &Disp = MI.getOperand(Op+3);
330 if (MI.getOperand(Op).isConstantPoolIndex()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000331 // Emit a direct address reference [disp32] where the displacement of the
332 // constant pool entry is controlled by the MCE.
Chris Lattnere831b6b2003-01-13 00:33:59 +0000333 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
334 unsigned Index = MI.getOperand(Op).getConstantPoolIndex();
Chris Lattner04b0b302003-06-01 23:23:50 +0000335 unsigned Address = MCE.getConstantPoolEntryAddress(Index);
336 MCE.emitWord(Address+Disp.getImmedValue());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000337 return;
338 }
339
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000340 const MachineOperand &BaseReg = MI.getOperand(Op);
341 const MachineOperand &Scale = MI.getOperand(Op+1);
342 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000343
344 // Is a SIB byte needed?
345 if (IndexReg.getReg() == 0 && BaseReg.getReg() != X86::ESP) {
346 if (BaseReg.getReg() == 0) { // Just a displacement?
347 // Emit special case [disp32] encoding
348 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
349 emitConstant(Disp.getImmedValue(), 4);
350 } else {
351 unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
352 if (Disp.getImmedValue() == 0 && BaseRegNo != N86::EBP) {
353 // Emit simple indirect register encoding... [EAX] f.e.
354 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
355 } else if (isDisp8(Disp.getImmedValue())) {
356 // Emit the disp8 encoding... [REG+disp8]
357 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
358 emitConstant(Disp.getImmedValue(), 1);
359 } else {
360 // Emit the most general non-SIB encoding: [REG+disp32]
Chris Lattner20671842002-12-13 05:05:05 +0000361 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000362 emitConstant(Disp.getImmedValue(), 4);
363 }
364 }
365
366 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
367 assert(IndexReg.getReg() != X86::ESP && "Cannot use ESP as index reg!");
368
369 bool ForceDisp32 = false;
Brian Gaeke95780cc2002-12-13 07:56:18 +0000370 bool ForceDisp8 = false;
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000371 if (BaseReg.getReg() == 0) {
372 // If there is no base register, we emit the special case SIB byte with
373 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
374 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
375 ForceDisp32 = true;
Brian Gaeke95780cc2002-12-13 07:56:18 +0000376 } else if (Disp.getImmedValue() == 0 && BaseReg.getReg() != X86::EBP) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000377 // Emit no displacement ModR/M byte
378 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
379 } else if (isDisp8(Disp.getImmedValue())) {
380 // Emit the disp8 encoding...
381 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
Brian Gaeke95780cc2002-12-13 07:56:18 +0000382 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000383 } else {
384 // Emit the normal disp32 encoding...
385 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
386 }
387
388 // Calculate what the SS field value should be...
389 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
390 unsigned SS = SSTable[Scale.getImmedValue()];
391
392 if (BaseReg.getReg() == 0) {
393 // Handle the SIB byte for the case where there is no base. The
394 // displacement has already been output.
395 assert(IndexReg.getReg() && "Index register must be specified!");
396 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
397 } else {
398 unsigned BaseRegNo = getX86RegNum(BaseReg.getReg());
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000399 unsigned IndexRegNo;
400 if (IndexReg.getReg())
401 IndexRegNo = getX86RegNum(IndexReg.getReg());
402 else
403 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000404 emitSIBByte(SS, IndexRegNo, BaseRegNo);
405 }
406
407 // Do we need to output a displacement?
Brian Gaeke95780cc2002-12-13 07:56:18 +0000408 if (Disp.getImmedValue() != 0 || ForceDisp32 || ForceDisp8) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000409 if (!ForceDisp32 && isDisp8(Disp.getImmedValue()))
410 emitConstant(Disp.getImmedValue(), 1);
411 else
412 emitConstant(Disp.getImmedValue(), 4);
413 }
414 }
415}
416
Chris Lattner04b0b302003-06-01 23:23:50 +0000417static unsigned sizeOfPtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000418 switch (Desc.TSFlags & X86II::ArgMask) {
419 case X86II::Arg8: return 1;
420 case X86II::Arg16: return 2;
421 case X86II::Arg32: return 4;
Chris Lattner5ada8df2002-12-25 05:09:21 +0000422 case X86II::ArgF32: return 4;
423 case X86II::ArgF64: return 8;
424 case X86II::ArgF80: return 10;
Chris Lattnera6a382c2002-12-13 03:50:13 +0000425 default: assert(0 && "Memory size not set!");
Chris Lattnerdf642e12002-12-20 04:12:48 +0000426 return 0;
Misha Brukman5000e432002-12-13 02:13:15 +0000427 }
428}
429
Chris Lattner76041ce2002-12-02 21:44:34 +0000430void Emitter::emitInstruction(MachineInstr &MI) {
431 unsigned Opcode = MI.getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000432 const TargetInstrDescriptor &Desc = II->get(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000433
434 // Emit instruction prefixes if neccesary
435 if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);// Operand size...
Chris Lattner5ada8df2002-12-25 05:09:21 +0000436
437 switch (Desc.TSFlags & X86II::Op0Mask) {
438 case X86II::TB:
439 MCE.emitByte(0x0F); // Two-byte opcode prefix
440 break;
441 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
442 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
Chris Lattnere831b6b2003-01-13 00:33:59 +0000443 MCE.emitByte(0xD8+
444 (((Desc.TSFlags & X86II::Op0Mask)-X86II::D8)
445 >> X86II::Op0Shift));
Chris Lattner5ada8df2002-12-25 05:09:21 +0000446 break; // Two-byte opcode prefix
Chris Lattnere831b6b2003-01-13 00:33:59 +0000447 default: assert(0 && "Invalid prefix!");
448 case 0: break; // No prefix!
Chris Lattner5ada8df2002-12-25 05:09:21 +0000449 }
Chris Lattner76041ce2002-12-02 21:44:34 +0000450
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000451 unsigned char BaseOpcode = II->getBaseOpcodeFor(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000452 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000453 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner5ada8df2002-12-25 05:09:21 +0000454 case X86II::Pseudo:
Chris Lattnerc2489032003-05-07 19:21:28 +0000455 if (Opcode != X86::IMPLICIT_USE)
Chris Lattner9dedbcc2003-05-06 21:31:47 +0000456 std::cerr << "X86 Machine Code Emitter: No 'form', not emitting: " << MI;
Chris Lattner5ada8df2002-12-25 05:09:21 +0000457 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000458
Chris Lattner76041ce2002-12-02 21:44:34 +0000459 case X86II::RawFrm:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000460 MCE.emitByte(BaseOpcode);
Chris Lattner8f04b092002-12-02 21:56:18 +0000461 if (MI.getNumOperands() == 1) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000462 MachineOperand &MO = MI.getOperand(0);
463 if (MO.isPCRelativeDisp()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000464 // Conditional branch... FIXME: this should use an MBB destination!
465 emitPCRelativeBlockAddress(cast<BasicBlock>(MO.getVRegValue()));
Chris Lattnere831b6b2003-01-13 00:33:59 +0000466 } else if (MO.isGlobalAddress()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000467 assert(MO.isPCRelative() && "Call target is not PC Relative?");
468 emitGlobalAddressForCall(MO.getGlobal());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000469 } else if (MO.isExternalSymbol()) {
Chris Lattner04b0b302003-06-01 23:23:50 +0000470 unsigned Address = MCE.getGlobalValueAddress(MO.getSymbolName());
471 assert(Address && "Unknown external symbol!");
472 emitMaybePCRelativeValue(Address, MO.isPCRelative());
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000473 } else {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000474 assert(0 && "Unknown RawFrm operand!");
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000475 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000476 }
477 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000478
479 case X86II::AddRegFrm:
480 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(0).getReg()));
481 if (MI.getNumOperands() == 2) {
482 MachineOperand &MO1 = MI.getOperand(1);
483 if (MO1.isImmediate() || MO1.getVRegValueOrNull() ||
484 MO1.isGlobalAddress() || MO1.isExternalSymbol()) {
485 unsigned Size = sizeOfPtr(Desc);
486 if (Value *V = MO1.getVRegValueOrNull()) {
487 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000488 emitGlobalAddressForPtr(cast<GlobalValue>(V));
Chris Lattnere831b6b2003-01-13 00:33:59 +0000489 } else if (MO1.isGlobalAddress()) {
490 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000491 assert(!MO1.isPCRelative() && "Function pointer ref is PC relative?");
492 emitGlobalAddressForPtr(MO1.getGlobal());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000493 } else if (MO1.isExternalSymbol()) {
494 assert(Size == 4 && "Don't know how to emit non-pointer values!");
Chris Lattner04b0b302003-06-01 23:23:50 +0000495
496 unsigned Address = MCE.getGlobalValueAddress(MO1.getSymbolName());
497 assert(Address && "Unknown external symbol!");
498 emitMaybePCRelativeValue(Address, MO1.isPCRelative());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000499 } else {
500 emitConstant(MO1.getImmedValue(), Size);
501 }
502 }
503 }
504 break;
505
506 case X86II::MRMDestReg: {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000507 MCE.emitByte(BaseOpcode);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000508 MachineOperand &SrcOp = MI.getOperand(1+II->isTwoAddrInstr(Opcode));
509 emitRegModRMByte(MI.getOperand(0).getReg(), getX86RegNum(SrcOp.getReg()));
510 if (MI.getNumOperands() == 4)
511 emitConstant(MI.getOperand(3).getImmedValue(), sizeOfPtr(Desc));
Chris Lattner9dedbcc2003-05-06 21:31:47 +0000512 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000513 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000514 case X86II::MRMDestMem:
515 MCE.emitByte(BaseOpcode);
516 emitMemModRMByte(MI, 0, getX86RegNum(MI.getOperand(4).getReg()));
517 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000518
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000519 case X86II::MRMSrcReg:
520 MCE.emitByte(BaseOpcode);
521 emitRegModRMByte(MI.getOperand(MI.getNumOperands()-1).getReg(),
522 getX86RegNum(MI.getOperand(0).getReg()));
523 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000524
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000525 case X86II::MRMSrcMem:
526 MCE.emitByte(BaseOpcode);
527 emitMemModRMByte(MI, MI.getNumOperands()-4,
528 getX86RegNum(MI.getOperand(0).getReg()));
529 break;
530
531 case X86II::MRMS0r: case X86II::MRMS1r:
532 case X86II::MRMS2r: case X86II::MRMS3r:
533 case X86II::MRMS4r: case X86II::MRMS5r:
534 case X86II::MRMS6r: case X86II::MRMS7r:
535 MCE.emitByte(BaseOpcode);
536 emitRegModRMByte(MI.getOperand(0).getReg(),
537 (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0r);
538
Chris Lattnerd9096832002-12-15 08:01:39 +0000539 if (MI.getOperand(MI.getNumOperands()-1).isImmediate()) {
Misha Brukman5000e432002-12-13 02:13:15 +0000540 unsigned Size = sizeOfPtr(Desc);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000541 emitConstant(MI.getOperand(MI.getNumOperands()-1).getImmedValue(), Size);
542 }
543 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000544
545 case X86II::MRMS0m: case X86II::MRMS1m:
546 case X86II::MRMS2m: case X86II::MRMS3m:
547 case X86II::MRMS4m: case X86II::MRMS5m:
548 case X86II::MRMS6m: case X86II::MRMS7m:
549 MCE.emitByte(BaseOpcode);
550 emitMemModRMByte(MI, 0, (Desc.TSFlags & X86II::FormMask)-X86II::MRMS0m);
551
552 if (MI.getNumOperands() == 5) {
553 unsigned Size = sizeOfPtr(Desc);
554 emitConstant(MI.getOperand(4).getImmedValue(), Size);
555 }
556 break;
Chris Lattner76041ce2002-12-02 21:44:34 +0000557 }
558}