blob: c323433b9a64b9e6322a1ef9abf88aac315c5b1d [file] [log] [blame]
Misha Brukmancd603132003-06-02 03:28:00 +00001//===-- X86/X86CodeEmitter.cpp - Convert X86 code to machine code ---------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner40ead952002-12-02 21:24:12 +00009//
10// This file contains the pass that transforms the X86 machine instructions into
Chris Lattnere72e4452004-11-20 23:55:15 +000011// relocatable machine code.
Chris Lattner40ead952002-12-02 21:24:12 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "X86TargetMachine.h"
Chris Lattnere72e4452004-11-20 23:55:15 +000016#include "X86Relocations.h"
Chris Lattnerea1ddab2002-12-03 06:34:06 +000017#include "X86.h"
Chris Lattner40ead952002-12-02 21:24:12 +000018#include "llvm/PassManager.h"
19#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattner5ae99fe2002-12-28 20:24:48 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner76041ce2002-12-02 21:44:34 +000021#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner655239c2003-12-20 10:20:19 +000022#include "llvm/CodeGen/Passes.h"
Chris Lattnerc01d1232003-10-20 03:42:58 +000023#include "llvm/Function.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Evan Cheng5e8b5552006-02-18 00:57:10 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000026#include <iostream>
Chris Lattner65b05ce2003-12-12 07:11:18 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner40ead952002-12-02 21:24:12 +000029namespace {
Chris Lattner302de592003-06-06 04:00:05 +000030 Statistic<>
31 NumEmitted("x86-emitter", "Number of machine instructions emitted");
Chris Lattner04b0b302003-06-01 23:23:50 +000032}
33
Chris Lattner04b0b302003-06-01 23:23:50 +000034namespace {
Chris Lattner5ae99fe2002-12-28 20:24:48 +000035 class Emitter : public MachineFunctionPass {
36 const X86InstrInfo *II;
Chris Lattner8f04b092002-12-02 21:56:18 +000037 MachineCodeEmitter &MCE;
Nate Begeman37efe672006-04-22 18:53:45 +000038 std::vector<std::pair<MachineBasicBlock *, unsigned> > BBRefs;
Chris Lattnerea1ddab2002-12-03 06:34:06 +000039 public:
Alkis Evlogimenos39c20052004-03-09 03:34:53 +000040 explicit Emitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
41 Emitter(MachineCodeEmitter &mce, const X86InstrInfo& ii)
42 : II(&ii), MCE(mce) {}
Chris Lattner40ead952002-12-02 21:24:12 +000043
Chris Lattner5ae99fe2002-12-28 20:24:48 +000044 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner76041ce2002-12-02 21:44:34 +000045
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000046 virtual const char *getPassName() const {
47 return "X86 Machine Code Emitter";
48 }
49
Alkis Evlogimenos39c20052004-03-09 03:34:53 +000050 void emitInstruction(const MachineInstr &MI);
51
Chris Lattnerea1ddab2002-12-03 06:34:06 +000052 private:
Nate Begeman37efe672006-04-22 18:53:45 +000053 void emitPCRelativeBlockAddress(MachineBasicBlock *MBB);
Chris Lattner16fe6f52004-11-16 04:21:18 +000054 void emitPCRelativeValue(unsigned Address);
Chris Lattner16cb6f82005-05-19 05:54:33 +000055 void emitGlobalAddressForCall(GlobalValue *GV, bool isTailCall);
Chris Lattner8cce7cd2004-10-15 04:53:13 +000056 void emitGlobalAddressForPtr(GlobalValue *GV, int Disp = 0);
Evan Cheng74cb0642006-06-22 00:02:55 +000057 void emitExternalSymbolAddress(const char *ES, bool isPCRelative);
Chris Lattner04b0b302003-06-01 23:23:50 +000058
Chris Lattner0e576292006-05-04 00:42:08 +000059 void emitDisplacementField(const MachineOperand *RelocOp, int DispVal);
60
Chris Lattnerea1ddab2002-12-03 06:34:06 +000061 void emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeField);
62 void emitSIBByte(unsigned SS, unsigned Index, unsigned Base);
63 void emitConstant(unsigned Val, unsigned Size);
64
65 void emitMemModRMByte(const MachineInstr &MI,
66 unsigned Op, unsigned RegOpcodeField);
67
Chris Lattner40ead952002-12-02 21:24:12 +000068 };
69}
70
Chris Lattner81b6ed72005-07-11 05:17:48 +000071/// createX86CodeEmitterPass - Return a pass that emits the collected X86 code
72/// to the specified MCE object.
73FunctionPass *llvm::createX86CodeEmitterPass(MachineCodeEmitter &MCE) {
74 return new Emitter(MCE);
Chris Lattner40ead952002-12-02 21:24:12 +000075}
Chris Lattner76041ce2002-12-02 21:44:34 +000076
Chris Lattner5ae99fe2002-12-28 20:24:48 +000077bool Emitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000078 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
79 MF.getTarget().getRelocationModel() != Reloc::Static) &&
80 "JIT relocation model must be set to static or default!");
Chris Lattnerd029cd22004-06-02 05:55:25 +000081 II = ((X86TargetMachine&)MF.getTarget()).getInstrInfo();
Chris Lattner76041ce2002-12-02 21:44:34 +000082
Chris Lattner43b429b2006-05-02 18:27:26 +000083 do {
84 BBRefs.clear();
Chris Lattner43b429b2006-05-02 18:27:26 +000085
86 MCE.startFunction(MF);
Chris Lattner93e5c282006-05-03 17:21:32 +000087 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
88 MBB != E; ++MBB) {
89 MCE.StartMachineBasicBlock(MBB);
90 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
91 I != E; ++I)
92 emitInstruction(*I);
93 }
Chris Lattner43b429b2006-05-02 18:27:26 +000094 } while (MCE.finishFunction(MF));
Chris Lattner04b0b302003-06-01 23:23:50 +000095
Chris Lattner608c1892006-05-02 17:16:20 +000096 // Resolve all forward branches now.
Chris Lattner04b0b302003-06-01 23:23:50 +000097 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
Chris Lattnerb4432f32006-05-03 17:10:41 +000098 unsigned Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
Chris Lattner04b0b302003-06-01 23:23:50 +000099 unsigned Ref = BBRefs[i].second;
Chris Lattner608c1892006-05-02 17:16:20 +0000100 *((unsigned*)(intptr_t)Ref) = Location-Ref-4;
Chris Lattner04b0b302003-06-01 23:23:50 +0000101 }
102 BBRefs.clear();
Chris Lattner76041ce2002-12-02 21:44:34 +0000103 return false;
104}
105
Chris Lattnere72e4452004-11-20 23:55:15 +0000106/// emitPCRelativeValue - Emit a 32-bit PC relative address.
107///
108void Emitter::emitPCRelativeValue(unsigned Address) {
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000109 MCE.emitWordLE(Address-MCE.getCurrentPCValue()-4);
Chris Lattnere72e4452004-11-20 23:55:15 +0000110}
111
Chris Lattnerb4432f32006-05-03 17:10:41 +0000112/// emitPCRelativeBlockAddress - This method keeps track of the information
113/// necessary to resolve the address of this block later and emits a dummy
114/// value.
Chris Lattner04b0b302003-06-01 23:23:50 +0000115///
Nate Begeman37efe672006-04-22 18:53:45 +0000116void Emitter::emitPCRelativeBlockAddress(MachineBasicBlock *MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +0000117 // Remember where this reference was and where it is to so we can
118 // deal with it later.
119 BBRefs.push_back(std::make_pair(MBB, MCE.getCurrentPCValue()));
120 MCE.emitWordLE(0);
Chris Lattner04b0b302003-06-01 23:23:50 +0000121}
122
Chris Lattner04b0b302003-06-01 23:23:50 +0000123/// emitGlobalAddressForCall - Emit the specified address to the code stream
124/// assuming this is part of a function call, which is PC relative.
125///
Chris Lattner16cb6f82005-05-19 05:54:33 +0000126void Emitter::emitGlobalAddressForCall(GlobalValue *GV, bool isTailCall) {
Chris Lattner5a032de2006-05-03 20:30:20 +0000127 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Chris Lattner16cb6f82005-05-19 05:54:33 +0000128 X86::reloc_pcrel_word, GV, 0,
129 !isTailCall /*Doesn'tNeedStub*/));
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000130 MCE.emitWordLE(0);
Chris Lattner04b0b302003-06-01 23:23:50 +0000131}
132
133/// emitGlobalAddress - Emit the specified address to the code stream assuming
134/// this is part of a "take the address of a global" instruction, which is not
135/// PC relative.
136///
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000137void Emitter::emitGlobalAddressForPtr(GlobalValue *GV, int Disp /* = 0 */) {
Chris Lattner5a032de2006-05-03 20:30:20 +0000138 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Chris Lattnere72e4452004-11-20 23:55:15 +0000139 X86::reloc_absolute_word, GV));
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000140 MCE.emitWordLE(Disp); // The relocated value will be added to the displacement
Chris Lattner04b0b302003-06-01 23:23:50 +0000141}
142
Chris Lattnere72e4452004-11-20 23:55:15 +0000143/// emitExternalSymbolAddress - Arrange for the address of an external symbol to
144/// be emitted to the current location in the function, and allow it to be PC
145/// relative.
Evan Cheng74cb0642006-06-22 00:02:55 +0000146void Emitter::emitExternalSymbolAddress(const char *ES, bool isPCRelative) {
Chris Lattner5a032de2006-05-03 20:30:20 +0000147 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattnere72e4452004-11-20 23:55:15 +0000148 isPCRelative ? X86::reloc_pcrel_word : X86::reloc_absolute_word, ES));
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000149 MCE.emitWordLE(0);
Chris Lattnere72e4452004-11-20 23:55:15 +0000150}
Chris Lattner04b0b302003-06-01 23:23:50 +0000151
Chris Lattnerff3261a2003-06-03 15:31:23 +0000152/// N86 namespace - Native X86 Register numbers... used by X86 backend.
153///
154namespace N86 {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000155 enum {
156 EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7
157 };
158}
159
160
161// getX86RegNum - This function maps LLVM register identifiers to their X86
162// specific numbering, which is used in various places encoding instructions.
163//
164static unsigned getX86RegNum(unsigned RegNo) {
165 switch(RegNo) {
166 case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
167 case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
168 case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
169 case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
170 case X86::ESP: case X86::SP: case X86::AH: return N86::ESP;
171 case X86::EBP: case X86::BP: case X86::CH: return N86::EBP;
172 case X86::ESI: case X86::SI: case X86::DH: return N86::ESI;
173 case X86::EDI: case X86::DI: case X86::BH: return N86::EDI;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000174
175 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
176 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
177 return RegNo-X86::ST0;
Evan Cheng576c1412006-02-14 21:45:24 +0000178
179 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
180 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7:
181 return RegNo-X86::XMM0;
182
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000183 default:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000184 assert(MRegisterInfo::isVirtualRegister(RegNo) &&
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000185 "Unknown physical register!");
186 assert(0 && "Register allocator hasn't allocated reg correctly yet!");
187 return 0;
188 }
189}
190
191inline static unsigned char ModRMByte(unsigned Mod, unsigned RegOpcode,
192 unsigned RM) {
193 assert(Mod < 4 && RegOpcode < 8 && RM < 8 && "ModRM Fields out of range!");
194 return RM | (RegOpcode << 3) | (Mod << 6);
195}
196
197void Emitter::emitRegModRMByte(unsigned ModRMReg, unsigned RegOpcodeFld){
198 MCE.emitByte(ModRMByte(3, RegOpcodeFld, getX86RegNum(ModRMReg)));
199}
200
201void Emitter::emitSIBByte(unsigned SS, unsigned Index, unsigned Base) {
202 // SIB byte is in the same format as the ModRMByte...
203 MCE.emitByte(ModRMByte(SS, Index, Base));
204}
205
206void Emitter::emitConstant(unsigned Val, unsigned Size) {
207 // Output the constant in little endian byte order...
208 for (unsigned i = 0; i != Size; ++i) {
209 MCE.emitByte(Val & 255);
210 Val >>= 8;
211 }
212}
213
Chris Lattner0e576292006-05-04 00:42:08 +0000214/// isDisp8 - Return true if this signed displacement fits in a 8-bit
215/// sign-extended field.
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000216static bool isDisp8(int Value) {
217 return Value == (signed char)Value;
218}
219
Chris Lattner0e576292006-05-04 00:42:08 +0000220void Emitter::emitDisplacementField(const MachineOperand *RelocOp,
221 int DispVal) {
222 // If this is a simple integer displacement that doesn't require a relocation,
223 // emit it now.
224 if (!RelocOp) {
225 emitConstant(DispVal, 4);
226 return;
227 }
228
229 // Otherwise, this is something that requires a relocation. Emit it as such
230 // now.
231 if (RelocOp->isGlobalAddress()) {
232 emitGlobalAddressForPtr(RelocOp->getGlobal(), RelocOp->getOffset());
233 } else {
234 assert(0 && "Unknown value to relocate!");
235 }
236}
237
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000238void Emitter::emitMemModRMByte(const MachineInstr &MI,
239 unsigned Op, unsigned RegOpcodeField) {
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000240 const MachineOperand &Op3 = MI.getOperand(Op+3);
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000241 int DispVal = 0;
Chris Lattner0e576292006-05-04 00:42:08 +0000242 const MachineOperand *DispForReloc = 0;
243
244 // Figure out what sort of displacement we have to handle here.
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000245 if (Op3.isGlobalAddress()) {
Chris Lattner0e576292006-05-04 00:42:08 +0000246 DispForReloc = &Op3;
Evan Cheng140a4c42006-02-26 09:12:34 +0000247 } else if (Op3.isConstantPoolIndex()) {
248 DispVal += MCE.getConstantPoolEntryAddress(Op3.getConstantPoolIndex());
249 DispVal += Op3.getOffset();
Nate Begeman37efe672006-04-22 18:53:45 +0000250 } else if (Op3.isJumpTableIndex()) {
251 DispVal += MCE.getJumpTableEntryAddress(Op3.getJumpTableIndex());
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000252 } else {
253 DispVal = Op3.getImmedValue();
254 }
255
Chris Lattner07306de2004-10-17 07:49:45 +0000256 const MachineOperand &Base = MI.getOperand(Op);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000257 const MachineOperand &Scale = MI.getOperand(Op+1);
258 const MachineOperand &IndexReg = MI.getOperand(Op+2);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000259
Evan Cheng140a4c42006-02-26 09:12:34 +0000260 unsigned BaseReg = Base.getReg();
Chris Lattner07306de2004-10-17 07:49:45 +0000261
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000262 // Is a SIB byte needed?
Chris Lattner07306de2004-10-17 07:49:45 +0000263 if (IndexReg.getReg() == 0 && BaseReg != X86::ESP) {
264 if (BaseReg == 0) { // Just a displacement?
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000265 // Emit special case [disp32] encoding
266 MCE.emitByte(ModRMByte(0, RegOpcodeField, 5));
Chris Lattner0e576292006-05-04 00:42:08 +0000267
268 emitDisplacementField(DispForReloc, DispVal);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000269 } else {
Chris Lattner07306de2004-10-17 07:49:45 +0000270 unsigned BaseRegNo = getX86RegNum(BaseReg);
Chris Lattner0e576292006-05-04 00:42:08 +0000271 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000272 // Emit simple indirect register encoding... [EAX] f.e.
273 MCE.emitByte(ModRMByte(0, RegOpcodeField, BaseRegNo));
Chris Lattner0e576292006-05-04 00:42:08 +0000274 } else if (!DispForReloc && isDisp8(DispVal)) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000275 // Emit the disp8 encoding... [REG+disp8]
276 MCE.emitByte(ModRMByte(1, RegOpcodeField, BaseRegNo));
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000277 emitConstant(DispVal, 1);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000278 } else {
279 // Emit the most general non-SIB encoding: [REG+disp32]
Chris Lattner20671842002-12-13 05:05:05 +0000280 MCE.emitByte(ModRMByte(2, RegOpcodeField, BaseRegNo));
Chris Lattner0e576292006-05-04 00:42:08 +0000281 emitDisplacementField(DispForReloc, DispVal);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000282 }
283 }
284
285 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
286 assert(IndexReg.getReg() != X86::ESP && "Cannot use ESP as index reg!");
287
288 bool ForceDisp32 = false;
Brian Gaeke95780cc2002-12-13 07:56:18 +0000289 bool ForceDisp8 = false;
Chris Lattner07306de2004-10-17 07:49:45 +0000290 if (BaseReg == 0) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000291 // If there is no base register, we emit the special case SIB byte with
292 // MOD=0, BASE=5, to JUST get the index, scale, and displacement.
293 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
294 ForceDisp32 = true;
Chris Lattner0e576292006-05-04 00:42:08 +0000295 } else if (DispForReloc) {
296 // Emit the normal disp32 encoding.
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000297 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
298 ForceDisp32 = true;
Chris Lattner07306de2004-10-17 07:49:45 +0000299 } else if (DispVal == 0 && BaseReg != X86::EBP) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000300 // Emit no displacement ModR/M byte
301 MCE.emitByte(ModRMByte(0, RegOpcodeField, 4));
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000302 } else if (isDisp8(DispVal)) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000303 // Emit the disp8 encoding...
304 MCE.emitByte(ModRMByte(1, RegOpcodeField, 4));
Brian Gaeke95780cc2002-12-13 07:56:18 +0000305 ForceDisp8 = true; // Make sure to force 8 bit disp if Base=EBP
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000306 } else {
307 // Emit the normal disp32 encoding...
308 MCE.emitByte(ModRMByte(2, RegOpcodeField, 4));
309 }
310
311 // Calculate what the SS field value should be...
312 static const unsigned SSTable[] = { ~0, 0, 1, ~0, 2, ~0, ~0, ~0, 3 };
313 unsigned SS = SSTable[Scale.getImmedValue()];
314
Chris Lattner07306de2004-10-17 07:49:45 +0000315 if (BaseReg == 0) {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000316 // Handle the SIB byte for the case where there is no base. The
317 // displacement has already been output.
318 assert(IndexReg.getReg() && "Index register must be specified!");
319 emitSIBByte(SS, getX86RegNum(IndexReg.getReg()), 5);
320 } else {
Chris Lattner07306de2004-10-17 07:49:45 +0000321 unsigned BaseRegNo = getX86RegNum(BaseReg);
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000322 unsigned IndexRegNo;
323 if (IndexReg.getReg())
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000324 IndexRegNo = getX86RegNum(IndexReg.getReg());
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000325 else
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000326 IndexRegNo = 4; // For example [ESP+1*<noreg>+4]
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000327 emitSIBByte(SS, IndexRegNo, BaseRegNo);
328 }
329
330 // Do we need to output a displacement?
Chris Lattner0e576292006-05-04 00:42:08 +0000331 if (ForceDisp8) {
332 emitConstant(DispVal, 1);
333 } else if (DispVal != 0 || ForceDisp32) {
334 emitDisplacementField(DispForReloc, DispVal);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000335 }
336 }
337}
338
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000339static unsigned sizeOfImm(const TargetInstrDescriptor &Desc) {
340 switch (Desc.TSFlags & X86II::ImmMask) {
341 case X86II::Imm8: return 1;
342 case X86II::Imm16: return 2;
343 case X86II::Imm32: return 4;
344 default: assert(0 && "Immediate size not set!");
345 return 0;
346 }
347}
348
Alkis Evlogimenosf6e81562004-03-09 03:30:12 +0000349void Emitter::emitInstruction(const MachineInstr &MI) {
Chris Lattner302de592003-06-06 04:00:05 +0000350 NumEmitted++; // Keep track of the # of mi's emitted
351
Chris Lattner76041ce2002-12-02 21:44:34 +0000352 unsigned Opcode = MI.getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000353 const TargetInstrDescriptor &Desc = II->get(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000354
Chris Lattner915e5e52004-02-12 17:53:22 +0000355 // Emit the repeat opcode prefix as needed.
356 if ((Desc.TSFlags & X86II::Op0Mask) == X86II::REP) MCE.emitByte(0xF3);
357
Nate Begemanf63be7d2005-07-06 18:59:04 +0000358 // Emit the operand size opcode prefix as needed.
359 if (Desc.TSFlags & X86II::OpSize) MCE.emitByte(0x66);
360
Chris Lattner5ada8df2002-12-25 05:09:21 +0000361 switch (Desc.TSFlags & X86II::Op0Mask) {
362 case X86II::TB:
363 MCE.emitByte(0x0F); // Two-byte opcode prefix
364 break;
Evan Chengee50a1a2006-02-14 21:52:51 +0000365 case X86II::REP: break; // already handled.
366 case X86II::XS: // F3 0F
367 MCE.emitByte(0xF3);
368 MCE.emitByte(0x0F);
369 break;
370 case X86II::XD: // F2 0F
371 MCE.emitByte(0xF2);
372 MCE.emitByte(0x0F);
373 break;
Chris Lattner5ada8df2002-12-25 05:09:21 +0000374 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
375 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
Chris Lattnere831b6b2003-01-13 00:33:59 +0000376 MCE.emitByte(0xD8+
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000377 (((Desc.TSFlags & X86II::Op0Mask)-X86II::D8)
378 >> X86II::Op0Shift));
Chris Lattner5ada8df2002-12-25 05:09:21 +0000379 break; // Two-byte opcode prefix
Chris Lattnere831b6b2003-01-13 00:33:59 +0000380 default: assert(0 && "Invalid prefix!");
381 case 0: break; // No prefix!
Chris Lattner5ada8df2002-12-25 05:09:21 +0000382 }
Chris Lattner76041ce2002-12-02 21:44:34 +0000383
Chris Lattner5ae99fe2002-12-28 20:24:48 +0000384 unsigned char BaseOpcode = II->getBaseOpcodeFor(Opcode);
Chris Lattner76041ce2002-12-02 21:44:34 +0000385 switch (Desc.TSFlags & X86II::FormMask) {
Chris Lattnere831b6b2003-01-13 00:33:59 +0000386 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
Chris Lattner5ada8df2002-12-25 05:09:21 +0000387 case X86II::Pseudo:
Chris Lattnerdabbc982006-01-28 18:19:37 +0000388#ifndef NDEBUG
389 switch (Opcode) {
390 default:
391 assert(0 && "psuedo instructions should be removed before code emission");
392 case X86::IMPLICIT_USE:
393 case X86::IMPLICIT_DEF:
Evan Cheng069287d2006-05-16 07:21:53 +0000394 case X86::IMPLICIT_DEF_GR8:
395 case X86::IMPLICIT_DEF_GR16:
396 case X86::IMPLICIT_DEF_GR32:
Chris Lattnerdabbc982006-01-28 18:19:37 +0000397 case X86::IMPLICIT_DEF_FR32:
398 case X86::IMPLICIT_DEF_FR64:
Evan Chenga9f2a712006-03-22 02:52:03 +0000399 case X86::IMPLICIT_DEF_VR64:
400 case X86::IMPLICIT_DEF_VR128:
Chris Lattnerdabbc982006-01-28 18:19:37 +0000401 case X86::FP_REG_KILL:
402 break;
403 }
404#endif
Chris Lattner5ada8df2002-12-25 05:09:21 +0000405 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000406
Chris Lattner76041ce2002-12-02 21:44:34 +0000407 case X86II::RawFrm:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000408 MCE.emitByte(BaseOpcode);
Evan Cheng22f71312006-06-21 23:37:07 +0000409 if (Desc.numOperands == 1) {
Alkis Evlogimenosf6e81562004-03-09 03:30:12 +0000410 const MachineOperand &MO = MI.getOperand(0);
Brian Gaeke09015d92004-05-14 06:54:58 +0000411 if (MO.isMachineBasicBlock()) {
412 emitPCRelativeBlockAddress(MO.getMachineBasicBlock());
Chris Lattnere831b6b2003-01-13 00:33:59 +0000413 } else if (MO.isGlobalAddress()) {
Chris Lattner16cb6f82005-05-19 05:54:33 +0000414 bool isTailCall = Opcode == X86::TAILJMPd ||
415 Opcode == X86::TAILJMPr || Opcode == X86::TAILJMPm;
416 emitGlobalAddressForCall(MO.getGlobal(), isTailCall);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000417 } else if (MO.isExternalSymbol()) {
Evan Cheng74cb0642006-06-22 00:02:55 +0000418 emitExternalSymbolAddress(MO.getSymbolName(), true);
Chris Lattnere47f4ff2004-04-13 17:18:51 +0000419 } else if (MO.isImmediate()) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000420 emitConstant(MO.getImmedValue(), sizeOfImm(Desc));
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000421 } else {
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000422 assert(0 && "Unknown RawFrm operand!");
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000423 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000424 }
425 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000426
427 case X86II::AddRegFrm:
428 MCE.emitByte(BaseOpcode + getX86RegNum(MI.getOperand(0).getReg()));
429 if (MI.getNumOperands() == 2) {
Alkis Evlogimenosf6e81562004-03-09 03:30:12 +0000430 const MachineOperand &MO1 = MI.getOperand(1);
Chris Lattner4efeab22006-05-04 01:26:39 +0000431 if (MO1.isGlobalAddress()) {
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000432 assert(sizeOfImm(Desc) == 4 &&
433 "Don't know how to emit non-pointer values!");
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000434 emitGlobalAddressForPtr(MO1.getGlobal(), MO1.getOffset());
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000435 } else if (MO1.isExternalSymbol()) {
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000436 assert(sizeOfImm(Desc) == 4 &&
437 "Don't know how to emit non-pointer values!");
Evan Cheng74cb0642006-06-22 00:02:55 +0000438 emitExternalSymbolAddress(MO1.getSymbolName(), false);
Nate Begeman37efe672006-04-22 18:53:45 +0000439 } else if (MO1.isJumpTableIndex()) {
440 assert(sizeOfImm(Desc) == 4 &&
441 "Don't know how to emit non-pointer values!");
442 emitConstant(MCE.getJumpTableEntryAddress(MO1.getJumpTableIndex()), 4);
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000443 } else {
444 emitConstant(MO1.getImmedValue(), sizeOfImm(Desc));
Chris Lattnere831b6b2003-01-13 00:33:59 +0000445 }
446 }
447 break;
448
449 case X86II::MRMDestReg: {
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000450 MCE.emitByte(BaseOpcode);
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000451 emitRegModRMByte(MI.getOperand(0).getReg(),
452 getX86RegNum(MI.getOperand(1).getReg()));
453 if (MI.getNumOperands() == 3)
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000454 emitConstant(MI.getOperand(2).getImmedValue(), sizeOfImm(Desc));
Chris Lattner9dedbcc2003-05-06 21:31:47 +0000455 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000456 }
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000457 case X86II::MRMDestMem:
458 MCE.emitByte(BaseOpcode);
459 emitMemModRMByte(MI, 0, getX86RegNum(MI.getOperand(4).getReg()));
Chris Lattner42df4612004-07-17 20:26:14 +0000460 if (MI.getNumOperands() == 6)
461 emitConstant(MI.getOperand(5).getImmedValue(), sizeOfImm(Desc));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000462 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000463
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000464 case X86II::MRMSrcReg:
465 MCE.emitByte(BaseOpcode);
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000466 emitRegModRMByte(MI.getOperand(1).getReg(),
467 getX86RegNum(MI.getOperand(0).getReg()));
468 if (MI.getNumOperands() == 3)
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000469 emitConstant(MI.getOperand(2).getImmedValue(), sizeOfImm(Desc));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000470 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000471
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000472 case X86II::MRMSrcMem:
473 MCE.emitByte(BaseOpcode);
Chris Lattner5b672522004-02-17 07:40:44 +0000474 emitMemModRMByte(MI, 1, getX86RegNum(MI.getOperand(0).getReg()));
475 if (MI.getNumOperands() == 2+4)
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000476 emitConstant(MI.getOperand(5).getImmedValue(), sizeOfImm(Desc));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000477 break;
478
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000479 case X86II::MRM0r: case X86II::MRM1r:
480 case X86II::MRM2r: case X86II::MRM3r:
481 case X86II::MRM4r: case X86II::MRM5r:
482 case X86II::MRM6r: case X86II::MRM7r:
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000483 MCE.emitByte(BaseOpcode);
484 emitRegModRMByte(MI.getOperand(0).getReg(),
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000485 (Desc.TSFlags & X86II::FormMask)-X86II::MRM0r);
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000486
Chris Lattnerd9096832002-12-15 08:01:39 +0000487 if (MI.getOperand(MI.getNumOperands()-1).isImmediate()) {
Chris Lattner39a83dc2004-11-16 18:40:52 +0000488 emitConstant(MI.getOperand(MI.getNumOperands()-1).getImmedValue(),
489 sizeOfImm(Desc));
Chris Lattnerea1ddab2002-12-03 06:34:06 +0000490 }
491 break;
Chris Lattnere831b6b2003-01-13 00:33:59 +0000492
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000493 case X86II::MRM0m: case X86II::MRM1m:
494 case X86II::MRM2m: case X86II::MRM3m:
495 case X86II::MRM4m: case X86II::MRM5m:
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000496 case X86II::MRM6m: case X86II::MRM7m:
Chris Lattnere831b6b2003-01-13 00:33:59 +0000497 MCE.emitByte(BaseOpcode);
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000498 emitMemModRMByte(MI, 0, (Desc.TSFlags & X86II::FormMask)-X86II::MRM0m);
Chris Lattnere831b6b2003-01-13 00:33:59 +0000499
500 if (MI.getNumOperands() == 5) {
Chris Lattnercc0d2f52004-02-17 18:23:55 +0000501 if (MI.getOperand(4).isImmediate())
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000502 emitConstant(MI.getOperand(4).getImmedValue(), sizeOfImm(Desc));
Chris Lattnercc0d2f52004-02-17 18:23:55 +0000503 else if (MI.getOperand(4).isGlobalAddress())
Chris Lattner8cce7cd2004-10-15 04:53:13 +0000504 emitGlobalAddressForPtr(MI.getOperand(4).getGlobal(),
505 MI.getOperand(4).getOffset());
Nate Begeman67977ad2006-05-03 04:52:47 +0000506 else if (MI.getOperand(4).isJumpTableIndex())
507 emitConstant(MCE.getJumpTableEntryAddress(MI.getOperand(4)
508 .getJumpTableIndex()), 4);
Chris Lattnercc0d2f52004-02-17 18:23:55 +0000509 else
510 assert(0 && "Unknown operand!");
Chris Lattnere831b6b2003-01-13 00:33:59 +0000511 }
512 break;
Evan Cheng3c55c542006-02-01 06:13:50 +0000513
514 case X86II::MRMInitReg:
515 MCE.emitByte(BaseOpcode);
516 emitRegModRMByte(MI.getOperand(0).getReg(),
517 getX86RegNum(MI.getOperand(0).getReg()));
518 break;
Chris Lattner76041ce2002-12-02 21:44:34 +0000519 }
520}