blob: 6c46b45456294219725a51ae2f33e4cc717caa6e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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#define DEBUG_TYPE "asm-printer"
17#include "X86IntelAsmPrinter.h"
18#include "X86TargetAsmInfo.h"
19#include "X86.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
24#include "llvm/Support/Mangler.h"
25#include "llvm/Target/TargetAsmInfo.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/ADT/Statistic.h"
28using namespace llvm;
29
30STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const {
33 // Intel asm always emits functions to _text.
34 return "_text";
35}
36
37/// runOnMachineFunction - This uses the printMachineInstruction()
38/// method to print assembly for each instruction.
39///
40bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
41 SetupMachineFunction(MF);
42 O << "\n\n";
43
44 // Print out constants referenced by the function
45 EmitConstantPool(MF.getConstantPool());
46
47 // Print out labels for the function.
48 const Function *F = MF.getFunction();
49 unsigned CC = F->getCallingConv();
50
51 // Populate function information map. Actually, We don't want to populate
52 // non-stdcall or non-fastcall functions' information right now.
53 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
54 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
55
56 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
57
58 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
59
60 switch (F->getLinkage()) {
61 default: assert(0 && "Unsupported linkage type!");
62 case Function::InternalLinkage:
63 EmitAlignment(4);
64 break;
65 case Function::DLLExportLinkage:
66 DLLExportedFns.insert(CurrentFnName);
67 //FALLS THROUGH
68 case Function::ExternalLinkage:
69 O << "\tpublic " << CurrentFnName << "\n";
70 EmitAlignment(4);
71 break;
72 }
73
74 O << CurrentFnName << "\tproc near\n";
75
76 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
78 I != E; ++I) {
79 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +000080 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +000081 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 O << '\n';
83 }
84 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
85 II != E; ++II) {
86 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 printMachineInstruction(II);
88 }
89 }
90
91 // Print out jump tables referenced by the function.
92 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
93
94 O << CurrentFnName << "\tendp\n";
95
96 // We didn't modify anything.
97 return false;
98}
99
100void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000101 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 assert(value <= 7 && "Invalid ssecc argument!");
103 switch (value) {
104 case 0: O << "eq"; break;
105 case 1: O << "lt"; break;
106 case 2: O << "le"; break;
107 case 3: O << "unord"; break;
108 case 4: O << "neq"; break;
109 case 5: O << "nlt"; break;
110 case 6: O << "nle"; break;
111 case 7: O << "ord"; break;
112 }
113}
114
115void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
116 const char *Modifier) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000117 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 switch (MO.getType()) {
119 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000120 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 unsigned Reg = MO.getReg();
122 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
123 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
124 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
125 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
126 Reg = getX86SubSuperRegister(Reg, VT);
127 }
Bill Wendling8eeb9792008-02-26 21:11:01 +0000128 O << RI.get(Reg).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 } else
130 O << "reg" << MO.getReg();
131 return;
132 }
133 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000134 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 return;
136 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000137 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 return;
139 case MachineOperand::MO_JumpTableIndex: {
140 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
141 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000142 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000143 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return;
145 }
146 case MachineOperand::MO_ConstantPoolIndex: {
147 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
148 if (!isMemOp) O << "OFFSET ";
149 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000150 << getFunctionNumber() << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 int Offset = MO.getOffset();
152 if (Offset > 0)
153 O << " + " << Offset;
154 else if (Offset < 0)
155 O << Offset;
156 O << "]";
157 return;
158 }
159 case MachineOperand::MO_GlobalAddress: {
160 bool isCallOp = Modifier && !strcmp(Modifier, "call");
161 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
162 GlobalValue *GV = MO.getGlobal();
163 std::string Name = Mang->getValueName(GV);
164
165 X86SharedAsmPrinter::decorateName(Name, GV);
166
167 if (!isMemOp && !isCallOp) O << "OFFSET ";
168 if (GV->hasDLLImportLinkage()) {
169 // FIXME: This should be fixed with full support of stdcall & fastcall
170 // CC's
171 O << "__imp_";
172 }
173 O << Name;
174 int Offset = MO.getOffset();
175 if (Offset > 0)
176 O << " + " << Offset;
177 else if (Offset < 0)
178 O << Offset;
179 return;
180 }
181 case MachineOperand::MO_ExternalSymbol: {
182 bool isCallOp = Modifier && !strcmp(Modifier, "call");
183 if (!isCallOp) O << "OFFSET ";
184 O << TAI->getGlobalPrefix() << MO.getSymbolName();
185 return;
186 }
187 default:
188 O << "<unknown operand type>"; return;
189 }
190}
191
192void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
193 const char *Modifier) {
194 assert(isMem(MI, Op) && "Invalid memory reference!");
195
196 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 const MachineOperand &IndexReg = MI->getOperand(Op+2);
199 const MachineOperand &DispSpec = MI->getOperand(Op+3);
200
201 O << "[";
202 bool NeedPlus = false;
203 if (BaseReg.getReg()) {
204 printOp(BaseReg, Modifier);
205 NeedPlus = true;
206 }
207
208 if (IndexReg.getReg()) {
209 if (NeedPlus) O << " + ";
210 if (ScaleVal != 1)
211 O << ScaleVal << "*";
212 printOp(IndexReg, Modifier);
213 NeedPlus = true;
214 }
215
216 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
217 DispSpec.isJumpTableIndex()) {
218 if (NeedPlus)
219 O << " + ";
220 printOp(DispSpec, "mem");
221 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000222 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000224 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 if (DispVal > 0)
226 O << " + ";
227 else {
228 O << " - ";
229 DispVal = -DispVal;
230 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000231 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 O << DispVal;
233 }
234 }
235 O << "]";
236}
237
Evan Cheng6fb06762007-11-09 01:32:10 +0000238void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
239 const MachineBasicBlock *MBB) const {
240 if (!TAI->getSetDirective())
241 return;
242
243 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
244 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000245 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000246 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
247}
248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng477013c2007-10-14 05:57:21 +0000250 O << "\"L" << getFunctionNumber() << "$pb\"\n";
251 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252}
253
254bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
255 const char Mode) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000256 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 unsigned Reg = MO.getReg();
258 switch (Mode) {
259 default: return true; // Unknown mode.
260 case 'b': // Print QImode register
261 Reg = getX86SubSuperRegister(Reg, MVT::i8);
262 break;
263 case 'h': // Print QImode high register
264 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
265 break;
266 case 'w': // Print HImode register
267 Reg = getX86SubSuperRegister(Reg, MVT::i16);
268 break;
269 case 'k': // Print SImode register
270 Reg = getX86SubSuperRegister(Reg, MVT::i32);
271 break;
272 }
273
Bill Wendling8eeb9792008-02-26 21:11:01 +0000274 O << '%' << RI.get(Reg).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 return false;
276}
277
278/// PrintAsmOperand - Print out an operand for an inline asm expression.
279///
280bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
281 unsigned AsmVariant,
282 const char *ExtraCode) {
283 // Does this asm operand have a single letter operand modifier?
284 if (ExtraCode && ExtraCode[0]) {
285 if (ExtraCode[1] != 0) return true; // Unknown modifier.
286
287 switch (ExtraCode[0]) {
288 default: return true; // Unknown modifier.
289 case 'b': // Print QImode register
290 case 'h': // Print QImode high register
291 case 'w': // Print HImode register
292 case 'k': // Print SImode register
293 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
294 }
295 }
296
297 printOperand(MI, OpNo);
298 return false;
299}
300
301bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
302 unsigned OpNo,
303 unsigned AsmVariant,
304 const char *ExtraCode) {
305 if (ExtraCode && ExtraCode[0])
306 return true; // Unknown modifier.
307 printMemReference(MI, OpNo);
308 return false;
309}
310
311/// printMachineInstruction -- Print out a single X86 LLVM instruction
312/// MI in Intel syntax to the current output stream.
313///
314void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
315 ++EmittedInsts;
316
317 // See if a truncate instruction can be turned into a nop.
318 switch (MI->getOpcode()) {
319 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 case X86::PsMOVZX64rr32:
321 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
322 break;
323 }
324
325 // Call the autogenerated instruction printer routines.
326 printInstruction(MI);
327}
328
329bool X86IntelAsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000330 bool Result = X86SharedAsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331
332 Mang->markCharUnacceptable('.');
333
334 O << "\t.686\n\t.model flat\n\n";
335
336 // Emit declarations for external functions.
337 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
338 if (I->isDeclaration()) {
339 std::string Name = Mang->getValueName(I);
340 X86SharedAsmPrinter::decorateName(Name, I);
341
342 O << "\textern " ;
343 if (I->hasDLLImportLinkage()) {
344 O << "__imp_";
345 }
346 O << Name << ":near\n";
347 }
348
349 // Emit declarations for external globals. Note that VC++ always declares
350 // external globals to have type byte, and if that's good enough for VC++...
351 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
352 I != E; ++I) {
353 if (I->isDeclaration()) {
354 std::string Name = Mang->getValueName(I);
355
356 O << "\textern " ;
357 if (I->hasDLLImportLinkage()) {
358 O << "__imp_";
359 }
360 O << Name << ":byte\n";
361 }
362 }
363
Dan Gohman4a558a32007-07-25 19:33:14 +0000364 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365}
366
367bool X86IntelAsmPrinter::doFinalization(Module &M) {
368 const TargetData *TD = TM.getTargetData();
369
370 // Print out module-level global variables here.
371 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
372 I != E; ++I) {
373 if (I->isDeclaration()) continue; // External global require no code
374
375 // Check to see if this is a special global used by LLVM, if so, emit it.
376 if (EmitSpecialLLVMGlobal(I))
377 continue;
378
379 std::string name = Mang->getValueName(I);
380 Constant *C = I->getInitializer();
381 unsigned Align = TD->getPreferredAlignmentLog(I);
382 bool bCustomSegment = false;
383
384 switch (I->getLinkage()) {
385 case GlobalValue::LinkOnceLinkage:
386 case GlobalValue::WeakLinkage:
387 SwitchToDataSection("");
388 O << name << "?\tsegment common 'COMMON'\n";
389 bCustomSegment = true;
390 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
391 // are also available.
392 break;
393 case GlobalValue::AppendingLinkage:
394 SwitchToDataSection("");
395 O << name << "?\tsegment public 'DATA'\n";
396 bCustomSegment = true;
397 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
398 // are also available.
399 break;
400 case GlobalValue::DLLExportLinkage:
401 DLLExportedGVs.insert(name);
402 // FALL THROUGH
403 case GlobalValue::ExternalLinkage:
404 O << "\tpublic " << name << "\n";
405 // FALL THROUGH
406 case GlobalValue::InternalLinkage:
407 SwitchToDataSection(TAI->getDataSection(), I);
408 break;
409 default:
410 assert(0 && "Unknown linkage type!");
411 }
412
413 if (!bCustomSegment)
414 EmitAlignment(Align, I);
415
416 O << name << ":\t\t\t\t" << TAI->getCommentString()
417 << " " << I->getName() << '\n';
418
419 EmitGlobalConstant(C);
420
421 if (bCustomSegment)
422 O << name << "?\tends\n";
423 }
424
425 // Output linker support code for dllexported globals
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000426 if (!DLLExportedGVs.empty() ||
427 !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 SwitchToDataSection("");
429 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
430 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
431 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
432 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
433 O << "_drectve\t segment info alias('.drectve')\n";
434 }
435
436 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
437 e = DLLExportedGVs.end();
438 i != e; ++i) {
439 O << "\t db ' /EXPORT:" << *i << ",data'\n";
440 }
441
442 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
443 e = DLLExportedFns.end();
444 i != e; ++i) {
445 O << "\t db ' /EXPORT:" << *i << "'\n";
446 }
447
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000448 if (!DLLExportedGVs.empty() ||
449 !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 O << "_drectve\t ends\n";
451 }
452
453 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000454 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 SwitchToDataSection("");
456 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000457 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458}
459
460void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
461 unsigned NumElts = CVA->getNumOperands();
462 if (NumElts) {
463 // ML does not have escape sequences except '' for '. It also has a maximum
464 // string length of 255.
465 unsigned len = 0;
466 bool inString = false;
467 for (unsigned i = 0; i < NumElts; i++) {
468 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
469 if (len == 0)
470 O << "\tdb ";
471
472 if (n >= 32 && n <= 127) {
473 if (!inString) {
474 if (len > 0) {
475 O << ",'";
476 len += 2;
477 } else {
478 O << "'";
479 len++;
480 }
481 inString = true;
482 }
483 if (n == '\'') {
484 O << "'";
485 len++;
486 }
487 O << char(n);
488 } else {
489 if (inString) {
490 O << "'";
491 len++;
492 inString = false;
493 }
494 if (len > 0) {
495 O << ",";
496 len++;
497 }
498 O << n;
499 len += 1 + (n > 9) + (n > 99);
500 }
501
502 if (len > 60) {
503 if (inString) {
504 O << "'";
505 inString = false;
506 }
507 O << "\n";
508 len = 0;
509 }
510 }
511
512 if (len > 0) {
513 if (inString)
514 O << "'";
515 O << "\n";
516 }
517 }
518}
519
520// Include the auto-generated portion of the assembly writer.
521#include "X86GenAsmWriter1.inc"