blob: c6052d93b02de8379b2ad0bd68d776bd1feb5c20 [file] [log] [blame]
Chris Lattnerb9740462005-07-01 22:44:09 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Intel format assembly language.
12// This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86IntelAsmPrinter.h"
17#include "X86.h"
18#include "llvm/Module.h"
19#include "llvm/Assembly/Writer.h"
20#include "llvm/Support/Mangler.h"
Evan Cheng5588de92006-02-18 00:15:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000022using namespace llvm;
Chris Lattnerb9740462005-07-01 22:44:09 +000023
24/// runOnMachineFunction - This uses the printMachineInstruction()
25/// method to print assembly for each instruction.
26///
27bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga4a4ceb2006-03-07 02:23:26 +000028 if (forDarwin) {
29 // Let PassManager know we need debug information and relay
30 // the MachineDebugInfo address on to DwarfWriter.
31 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
32 }
Evan Cheng30d7b702006-03-07 02:02:57 +000033
Chris Lattner99946fb2005-11-21 07:51:23 +000034 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000035 O << "\n\n";
36
37 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000038 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000039
40 // Print out labels for the function.
Chris Lattner050bf2f2005-11-21 07:16:34 +000041 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner99946fb2005-11-21 07:51:23 +000042 EmitAlignment(4);
Chris Lattnerb9740462005-07-01 22:44:09 +000043 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac6cb462005-11-21 23:06:54 +000044 if (HasDotTypeDotSizeDirective)
Chris Lattnerb9740462005-07-01 22:44:09 +000045 O << "\t.type\t" << CurrentFnName << ", @function\n";
46 O << CurrentFnName << ":\n";
Jim Laskeyc0d65182006-04-07 20:44:42 +000047
48 if (forDarwin) {
49 // Emit pre-function debug information.
50 DW.BeginFunction(&MF);
51 }
Chris Lattnerb9740462005-07-01 22:44:09 +000052
53 // Print out code for the function.
54 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
55 I != E; ++I) {
56 // Print a label for the basic block if there are any predecessors.
57 if (I->pred_begin() != I->pred_end())
Chris Lattnerd3656272005-11-21 07:43:59 +000058 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
59 << ":\t"
Chris Lattnerb9740462005-07-01 22:44:09 +000060 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
61 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
62 II != E; ++II) {
63 // Print the assembly for the instruction.
64 O << "\t";
65 printMachineInstruction(II);
66 }
67 }
68
Evan Chenga4a4ceb2006-03-07 02:23:26 +000069 if (forDarwin) {
70 // Emit post-function debug information.
Jim Laskeycf0166f2006-03-23 18:09:44 +000071 DW.EndFunction();
Evan Chenga4a4ceb2006-03-07 02:23:26 +000072 }
Evan Cheng30d7b702006-03-07 02:02:57 +000073
Chris Lattnerb9740462005-07-01 22:44:09 +000074 // We didn't modify anything.
75 return false;
76}
77
Nate Begeman6f8c1ac2005-11-30 18:54:35 +000078void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-07-14 22:52:25 +000079 unsigned char value = MI->getOperand(Op).getImmedValue();
80 assert(value <= 7 && "Invalid ssecc argument!");
81 switch (value) {
82 case 0: O << "eq"; break;
83 case 1: O << "lt"; break;
84 case 2: O << "le"; break;
85 case 3: O << "unord"; break;
86 case 4: O << "neq"; break;
87 case 5: O << "nlt"; break;
88 case 6: O << "nle"; break;
89 case 7: O << "ord"; break;
90 }
91}
92
Chris Lattnerd62a3bf2006-02-06 23:41:19 +000093void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
94 const char *Modifier) {
Chris Lattnerb9740462005-07-01 22:44:09 +000095 const MRegisterInfo &RI = *TM.getRegisterInfo();
96 switch (MO.getType()) {
97 case MachineOperand::MO_VirtualRegister:
98 if (Value *V = MO.getVRegValueOrNull()) {
99 O << "<" << V->getName() << ">";
100 return;
101 }
102 // FALLTHROUGH
103 case MachineOperand::MO_MachineRegister:
104 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
105 // Bug Workaround: See note in Printer::doInitialization about %.
106 O << "%" << RI.get(MO.getReg()).Name;
107 else
108 O << "%reg" << MO.getReg();
109 return;
110
111 case MachineOperand::MO_SignExtendedImmed:
112 case MachineOperand::MO_UnextendedImmed:
113 O << (int)MO.getImmedValue();
114 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000115 case MachineOperand::MO_MachineBasicBlock:
116 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb9740462005-07-01 22:44:09 +0000117 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000118 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerde02d772006-01-22 23:41:00 +0000119 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
Chris Lattnerb9740462005-07-01 22:44:09 +0000120 abort ();
121 return;
Evan Cheng75b87832006-02-26 08:28:12 +0000122 case MachineOperand::MO_ConstantPoolIndex: {
123 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
124 if (!isMemOp) O << "OFFSET ";
125 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
126 << MO.getConstantPoolIndex();
127 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
128 O << "-\"L" << getFunctionNumber() << "$pb\"";
129 int Offset = MO.getOffset();
130 if (Offset > 0)
131 O << " + " << Offset;
132 else if (Offset < 0)
133 O << Offset;
134 O << "]";
135 return;
136 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000137 case MachineOperand::MO_GlobalAddress: {
Evan Cheng5588de92006-02-18 00:15:05 +0000138 bool isCallOp = Modifier && !strcmp(Modifier, "call");
139 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
140 if (!isMemOp && !isCallOp) O << "OFFSET ";
Evan Cheng73136df2006-02-22 20:19:42 +0000141 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Evan Cheng5588de92006-02-18 00:15:05 +0000142 GlobalValue *GV = MO.getGlobal();
143 std::string Name = Mang->getValueName(GV);
144 if (!isMemOp && !isCallOp) O << '$';
145 // Link-once, External, or Weakly-linked global variables need
146 // non-lazily-resolved stubs
147 if (GV->isExternal() || GV->hasWeakLinkage() ||
148 GV->hasLinkOnceLinkage()) {
149 // Dynamically-resolved functions need a stub for the function.
150 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
151 FnStubs.insert(Name);
152 O << "L" << Name << "$stub";
153 } else {
154 GVStubs.insert(Name);
155 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng5588de92006-02-18 00:15:05 +0000156 }
157 } else {
158 O << Mang->getValueName(GV);
159 }
Evan Cheng1f342c22006-02-23 02:43:52 +0000160 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
161 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng5588de92006-02-18 00:15:05 +0000162 } else
163 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb9740462005-07-01 22:44:09 +0000164 int Offset = MO.getOffset();
165 if (Offset > 0)
166 O << " + " << Offset;
167 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000168 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000169 return;
170 }
Evan Cheng5588de92006-02-18 00:15:05 +0000171 case MachineOperand::MO_ExternalSymbol: {
172 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng73136df2006-02-22 20:19:42 +0000173 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
174 std::string Name(GlobalPrefix);
175 Name += MO.getSymbolName();
Evan Cheng5588de92006-02-18 00:15:05 +0000176 FnStubs.insert(Name);
177 O << "L" << Name << "$stub";
178 return;
179 }
Evan Cheng73136df2006-02-22 20:19:42 +0000180 if (!isCallOp) O << "OFFSET ";
Chris Lattnerb9740462005-07-01 22:44:09 +0000181 O << GlobalPrefix << MO.getSymbolName();
182 return;
Evan Cheng5588de92006-02-18 00:15:05 +0000183 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000184 default:
185 O << "<unknown operand type>"; return;
186 }
187}
188
189void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
190 assert(isMem(MI, Op) && "Invalid memory reference!");
191
192 const MachineOperand &BaseReg = MI->getOperand(Op);
193 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
194 const MachineOperand &IndexReg = MI->getOperand(Op+2);
195 const MachineOperand &DispSpec = MI->getOperand(Op+3);
196
197 if (BaseReg.isFrameIndex()) {
198 O << "[frame slot #" << BaseReg.getFrameIndex();
199 if (DispSpec.getImmedValue())
200 O << " + " << DispSpec.getImmedValue();
201 O << "]";
202 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000203 }
204
205 O << "[";
206 bool NeedPlus = false;
207 if (BaseReg.getReg()) {
Evan Cheng5a766802006-02-07 08:38:37 +0000208 printOp(BaseReg, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000209 NeedPlus = true;
210 }
211
212 if (IndexReg.getReg()) {
213 if (NeedPlus) O << " + ";
214 if (ScaleVal != 1)
215 O << ScaleVal << "*";
216 printOp(IndexReg);
217 NeedPlus = true;
218 }
219
Evan Cheng75b87832006-02-26 08:28:12 +0000220 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Chris Lattnerb9740462005-07-01 22:44:09 +0000221 if (NeedPlus)
222 O << " + ";
Evan Cheng5a766802006-02-07 08:38:37 +0000223 printOp(DispSpec, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000224 } else {
225 int DispVal = DispSpec.getImmedValue();
226 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
227 if (NeedPlus)
228 if (DispVal > 0)
229 O << " + ";
230 else {
231 O << " - ";
232 DispVal = -DispVal;
233 }
234 O << DispVal;
235 }
236 }
237 O << "]";
238}
239
Evan Cheng5588de92006-02-18 00:15:05 +0000240void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
243}
Chris Lattnerb9740462005-07-01 22:44:09 +0000244
Evan Chengb244b802006-04-28 23:11:40 +0000245bool X86IntelAsmPrinter::printAsmMRegsiter(const MachineOperand &MO,
246 const char Mode) {
247 const MRegisterInfo &RI = *TM.getRegisterInfo();
248 unsigned Reg = MO.getReg();
249 const char *Name = RI.get(Reg).Name;
250 switch (Mode) {
251 default: return true; // Unknown mode.
252 case 'b': // Print QImode register
253 switch (Reg) {
254 default: return true;
255 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
256 Name = "AL";
257 break;
258 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
259 Name = "DL";
260 break;
261 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
262 Name = "CL";
263 break;
264 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
265 Name = "BL";
266 break;
267 case X86::ESI:
268 Name = "SIL";
269 break;
270 case X86::EDI:
271 Name = "DIL";
272 break;
273 case X86::EBP:
274 Name = "BPL";
275 break;
276 case X86::ESP:
277 Name = "SPL";
278 break;
279 }
280 break;
281 case 'h': // Print QImode high register
282 switch (Reg) {
283 default: return true;
284 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
285 Name = "AL";
286 break;
287 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
288 Name = "DL";
289 break;
290 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
291 Name = "CL";
292 break;
293 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
294 Name = "BL";
295 break;
296 }
297 break;
298 case 'w': // Print HImode register
299 switch (Reg) {
300 default: return true;
301 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
302 Name = "AX";
303 break;
304 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
305 Name = "DX";
306 break;
307 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
308 Name = "CX";
309 break;
310 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
311 Name = "BX";
312 break;
313 case X86::ESI:
314 Name = "SI";
315 break;
316 case X86::EDI:
317 Name = "DI";
318 break;
319 case X86::EBP:
320 Name = "BP";
321 break;
322 case X86::ESP:
323 Name = "SP";
324 break;
325 }
326 break;
327 case 'k': // Print SImode register
328 switch (Reg) {
329 default: return true;
330 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
331 Name = "EAX";
332 break;
333 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
334 Name = "EDX";
335 break;
336 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
337 Name = "ECX";
338 break;
339 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
340 Name = "EBX";
341 break;
342 case X86::ESI:
343 Name = "ESI";
344 break;
345 case X86::EDI:
346 Name = "EDI";
347 break;
348 case X86::EBP:
349 Name = "EBP";
350 break;
351 case X86::ESP:
352 Name = "ESP";
353 break;
354 }
355 break;
356 }
357
358 O << '%' << Name;
359 return false;
360}
361
Evan Cheng68a44dc2006-04-28 21:19:05 +0000362/// PrintAsmOperand - Print out an operand for an inline asm expression.
363///
364bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
365 unsigned AsmVariant,
366 const char *ExtraCode) {
367 // Does this asm operand have a single letter operand modifier?
368 if (ExtraCode && ExtraCode[0]) {
369 if (ExtraCode[1] != 0) return true; // Unknown modifier.
370
371 switch (ExtraCode[0]) {
372 default: return true; // Unknown modifier.
Evan Chengb244b802006-04-28 23:11:40 +0000373 case 'b': // Print QImode register
374 case 'h': // Print QImode high register
375 case 'w': // Print HImode register
376 case 'k': // Print SImode register
377 return printAsmMRegsiter(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng68a44dc2006-04-28 21:19:05 +0000378 }
379 }
380
381 printOperand(MI, OpNo);
382 return false;
383}
384
385bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
386 unsigned OpNo,
387 unsigned AsmVariant,
388 const char *ExtraCode) {
389 if (ExtraCode && ExtraCode[0])
390 return true; // Unknown modifier.
391 printMemReference(MI, OpNo);
392 return false;
393}
394
Chris Lattnerb9740462005-07-01 22:44:09 +0000395/// printMachineInstruction -- Print out a single X86 LLVM instruction
396/// MI in Intel syntax to the current output stream.
397///
398void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
399 ++EmittedInsts;
400
401 // Call the autogenerated instruction printer routines.
402 printInstruction(MI);
403}
404
405bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9f6ce0e2005-07-03 17:34:39 +0000406 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb9740462005-07-01 22:44:09 +0000407 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
408 //
409 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
410 // instruction as a reference to the register named sp, and if you try to
411 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
412 // before being looked up in the symbol table. This creates spurious
413 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
414 // mode, and decorate all register names with percent signs.
415 O << "\t.intel_syntax\n";
416 return false;
417}
418
419// Include the auto-generated portion of the assembly writer.
420#include "X86GenAsmWriter1.inc"