make intel asmprinter use TRI::getAsmName instead of TRI::getName like
[llvm/avr.git] / lib / Target / X86 / AsmPrinter / X86IntelAsmPrinter.cpp
blob484b5e91cdb8dc2695804d6db206c8f31a1c68fe
1 //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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'.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asm-printer"
17 #include "X86IntelAsmPrinter.h"
18 #include "X86InstrInfo.h"
19 #include "X86MCAsmInfo.h"
20 #include "X86.h"
21 #include "llvm/CallingConv.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/CodeGen/DwarfWriter.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/Mangler.h"
36 using namespace llvm;
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
40 static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
41 const TargetData *TD) {
42 X86MachineFunctionInfo Info;
43 uint64_t Size = 0;
45 switch (F->getCallingConv()) {
46 case CallingConv::X86_StdCall:
47 Info.setDecorationStyle(StdCall);
48 break;
49 case CallingConv::X86_FastCall:
50 Info.setDecorationStyle(FastCall);
51 break;
52 default:
53 return Info;
56 unsigned argNum = 1;
57 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
58 AI != AE; ++AI, ++argNum) {
59 const Type* Ty = AI->getType();
61 // 'Dereference' type in case of byval parameter attribute
62 if (F->paramHasAttr(argNum, Attribute::ByVal))
63 Ty = cast<PointerType>(Ty)->getElementType();
65 // Size should be aligned to DWORD boundary
66 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
69 // We're not supporting tooooo huge arguments :)
70 Info.setBytesToPopOnReturn((unsigned int)Size);
71 return Info;
75 /// decorateName - Query FunctionInfoMap and use this information for various
76 /// name decoration.
77 void X86IntelAsmPrinter::decorateName(std::string &Name,
78 const GlobalValue *GV) {
79 const Function *F = dyn_cast<Function>(GV);
80 if (!F) return;
82 // We don't want to decorate non-stdcall or non-fastcall functions right now
83 CallingConv::ID CC = F->getCallingConv();
84 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
85 return;
87 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
89 const X86MachineFunctionInfo *Info;
90 if (info_item == FunctionInfoMap.end()) {
91 // Calculate apropriate function info and populate map
92 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
93 Info = &FunctionInfoMap[F];
94 } else {
95 Info = &info_item->second;
98 const FunctionType *FT = F->getFunctionType();
99 switch (Info->getDecorationStyle()) {
100 case None:
101 break;
102 case StdCall:
103 // "Pure" variadic functions do not receive @0 suffix.
104 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
105 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
106 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
107 break;
108 case FastCall:
109 // "Pure" variadic functions do not receive @0 suffix.
110 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
111 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
112 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
114 if (Name[0] == '_')
115 Name[0] = '@';
116 else
117 Name = '@' + Name;
119 break;
120 default:
121 llvm_unreachable("Unsupported DecorationStyle");
125 /// runOnMachineFunction - This uses the printMachineInstruction()
126 /// method to print assembly for each instruction.
128 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
129 this->MF = &MF;
130 SetupMachineFunction(MF);
131 O << "\n\n";
133 // Print out constants referenced by the function
134 EmitConstantPool(MF.getConstantPool());
136 // Print out labels for the function.
137 const Function *F = MF.getFunction();
138 CallingConv::ID CC = F->getCallingConv();
139 unsigned FnAlign = MF.getAlignment();
141 // Populate function information map. Actually, We don't want to populate
142 // non-stdcall or non-fastcall functions' information right now.
143 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
144 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
146 decorateName(CurrentFnName, F);
148 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
150 switch (F->getLinkage()) {
151 default: llvm_unreachable("Unsupported linkage type!");
152 case Function::LinkOnceAnyLinkage:
153 case Function::LinkOnceODRLinkage:
154 case Function::WeakAnyLinkage:
155 case Function::WeakODRLinkage:
157 case Function::PrivateLinkage:
158 case Function::LinkerPrivateLinkage:
159 case Function::InternalLinkage:
160 EmitAlignment(FnAlign);
161 break;
162 case Function::DLLExportLinkage:
163 DLLExportedFns.insert(CurrentFnName);
164 //FALLS THROUGH
165 case Function::ExternalLinkage:
166 O << "\tpublic " << CurrentFnName << "\n";
167 EmitAlignment(FnAlign);
168 break;
171 O << CurrentFnName << "\tproc near\n";
173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
175 I != E; ++I) {
176 // Print a label for the basic block if there are any predecessors.
177 if (!I->pred_empty()) {
178 EmitBasicBlockStart(I);
179 O << '\n';
181 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182 II != E; ++II) {
183 // Print the assembly for the instruction.
184 printMachineInstruction(II);
188 // Print out jump tables referenced by the function.
189 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
191 O << CurrentFnName << "\tendp\n";
193 // We didn't modify anything.
194 return false;
197 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
198 unsigned char value = MI->getOperand(Op).getImm();
199 assert(value <= 7 && "Invalid ssecc argument!");
200 switch (value) {
201 case 0: O << "eq"; break;
202 case 1: O << "lt"; break;
203 case 2: O << "le"; break;
204 case 3: O << "unord"; break;
205 case 4: O << "neq"; break;
206 case 5: O << "nlt"; break;
207 case 6: O << "nle"; break;
208 case 7: O << "ord"; break;
212 static void PrintRegName(raw_ostream &O, StringRef RegName) {
213 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
214 O << (char)toupper(RegName[i]);
217 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
218 const char *Modifier) {
219 switch (MO.getType()) {
220 case MachineOperand::MO_Register: {
221 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()));
222 unsigned Reg = MO.getReg();
223 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
224 EVT VT = (strcmp(Modifier,"subreg64") == 0) ?
225 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
226 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
227 Reg = getX86SubSuperRegister(Reg, VT);
229 PrintRegName(O, TRI->getAsmName(Reg));
230 return;
232 case MachineOperand::MO_Immediate:
233 O << MO.getImm();
234 return;
235 case MachineOperand::MO_JumpTableIndex: {
236 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
237 if (!isMemOp) O << "OFFSET ";
238 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
239 << "_" << MO.getIndex();
240 return;
242 case MachineOperand::MO_ConstantPoolIndex: {
243 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
244 if (!isMemOp) O << "OFFSET ";
245 O << "[" << MAI->getPrivateGlobalPrefix() << "CPI"
246 << getFunctionNumber() << "_" << MO.getIndex();
247 printOffset(MO.getOffset());
248 O << "]";
249 return;
251 case MachineOperand::MO_GlobalAddress: {
252 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
253 GlobalValue *GV = MO.getGlobal();
254 std::string Name = Mang->getMangledName(GV);
255 decorateName(Name, GV);
257 if (!isMemOp) O << "OFFSET ";
259 // Handle dllimport linkage.
260 // FIXME: This should be fixed with full support of stdcall & fastcall
261 // CC's
262 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
263 O << "__imp_";
265 O << Name;
266 printOffset(MO.getOffset());
267 return;
269 case MachineOperand::MO_ExternalSymbol: {
270 O << MAI->getGlobalPrefix() << MO.getSymbolName();
271 return;
273 default:
274 O << "<unknown operand type>"; return;
278 void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
279 const MachineOperand &MO = MI->getOperand(OpNo);
280 switch (MO.getType()) {
281 default: llvm_unreachable("Unknown pcrel immediate operand");
282 case MachineOperand::MO_Immediate:
283 O << MO.getImm();
284 return;
285 case MachineOperand::MO_MachineBasicBlock:
286 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
287 return;
289 case MachineOperand::MO_GlobalAddress: {
290 GlobalValue *GV = MO.getGlobal();
291 std::string Name = Mang->getMangledName(GV);
292 decorateName(Name, GV);
294 // Handle dllimport linkage.
295 // FIXME: This should be fixed with full support of stdcall & fastcall
296 // CC's
297 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
298 O << "__imp_";
299 O << Name;
300 printOffset(MO.getOffset());
301 return;
304 case MachineOperand::MO_ExternalSymbol:
305 O << MAI->getGlobalPrefix() << MO.getSymbolName();
306 return;
311 void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
312 unsigned Op,
313 const char *Modifier) {
314 const MachineOperand &BaseReg = MI->getOperand(Op);
315 int ScaleVal = MI->getOperand(Op+1).getImm();
316 const MachineOperand &IndexReg = MI->getOperand(Op+2);
317 const MachineOperand &DispSpec = MI->getOperand(Op+3);
319 O << "[";
320 bool NeedPlus = false;
321 if (BaseReg.getReg()) {
322 printOp(BaseReg, Modifier);
323 NeedPlus = true;
326 if (IndexReg.getReg()) {
327 if (NeedPlus) O << " + ";
328 if (ScaleVal != 1)
329 O << ScaleVal << "*";
330 printOp(IndexReg, Modifier);
331 NeedPlus = true;
334 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
335 DispSpec.isJTI()) {
336 if (NeedPlus)
337 O << " + ";
338 printOp(DispSpec, "mem");
339 } else {
340 int DispVal = DispSpec.getImm();
341 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
342 if (NeedPlus) {
343 if (DispVal > 0)
344 O << " + ";
345 else {
346 O << " - ";
347 DispVal = -DispVal;
350 O << DispVal;
353 O << "]";
356 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
357 const char *Modifier) {
358 assert(isMem(MI, Op) && "Invalid memory reference!");
359 MachineOperand Segment = MI->getOperand(Op+4);
360 if (Segment.getReg()) {
361 printOperand(MI, Op+4, Modifier);
362 O << ':';
364 printLeaMemReference(MI, Op, Modifier);
367 void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
368 const MachineBasicBlock *MBB) const {
369 if (!MAI->getSetDirective())
370 return;
372 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
373 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
374 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
375 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
378 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
379 O << "L" << getFunctionNumber() << "$pb\n";
380 O << "L" << getFunctionNumber() << "$pb:";
383 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
384 const char Mode) {
385 unsigned Reg = MO.getReg();
386 switch (Mode) {
387 default: return true; // Unknown mode.
388 case 'b': // Print QImode register
389 Reg = getX86SubSuperRegister(Reg, MVT::i8);
390 break;
391 case 'h': // Print QImode high register
392 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
393 break;
394 case 'w': // Print HImode register
395 Reg = getX86SubSuperRegister(Reg, MVT::i16);
396 break;
397 case 'k': // Print SImode register
398 Reg = getX86SubSuperRegister(Reg, MVT::i32);
399 break;
402 PrintRegName(O, TRI->getAsmName(Reg));
403 return false;
406 /// PrintAsmOperand - Print out an operand for an inline asm expression.
408 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
409 unsigned AsmVariant,
410 const char *ExtraCode) {
411 // Does this asm operand have a single letter operand modifier?
412 if (ExtraCode && ExtraCode[0]) {
413 if (ExtraCode[1] != 0) return true; // Unknown modifier.
415 switch (ExtraCode[0]) {
416 default: return true; // Unknown modifier.
417 case 'b': // Print QImode register
418 case 'h': // Print QImode high register
419 case 'w': // Print HImode register
420 case 'k': // Print SImode register
421 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
425 printOperand(MI, OpNo);
426 return false;
429 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
430 unsigned OpNo,
431 unsigned AsmVariant,
432 const char *ExtraCode) {
433 if (ExtraCode && ExtraCode[0])
434 return true; // Unknown modifier.
435 printMemReference(MI, OpNo);
436 return false;
439 /// printMachineInstruction -- Print out a single X86 LLVM instruction
440 /// MI in Intel syntax to the current output stream.
442 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
443 ++EmittedInsts;
445 processDebugLoc(MI->getDebugLoc());
447 // Call the autogenerated instruction printer routines.
448 printInstruction(MI);
450 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
451 EmitComments(*MI);
452 O << '\n';
455 bool X86IntelAsmPrinter::doInitialization(Module &M) {
456 bool Result = AsmPrinter::doInitialization(M);
458 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
460 // Emit declarations for external functions.
461 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
462 if (I->isDeclaration()) {
463 std::string Name = Mang->getMangledName(I);
464 decorateName(Name, I);
466 O << "\tEXTERN " ;
467 if (I->hasDLLImportLinkage()) {
468 O << "__imp_";
470 O << Name << ":near\n";
473 // Emit declarations for external globals. Note that VC++ always declares
474 // external globals to have type byte, and if that's good enough for VC++...
475 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
476 I != E; ++I) {
477 if (I->isDeclaration()) {
478 std::string Name = Mang->getMangledName(I);
480 O << "\tEXTERN " ;
481 if (I->hasDLLImportLinkage()) {
482 O << "__imp_";
484 O << Name << ":byte\n";
488 return Result;
491 void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
492 // Check to see if this is a special global used by LLVM, if so, emit it.
493 if (GV->isDeclaration() ||
494 EmitSpecialLLVMGlobal(GV))
495 return;
497 const TargetData *TD = TM.getTargetData();
499 std::string name = Mang->getMangledName(GV);
500 Constant *C = GV->getInitializer();
501 unsigned Align = TD->getPreferredAlignmentLog(GV);
502 bool bCustomSegment = false;
504 switch (GV->getLinkage()) {
505 case GlobalValue::CommonLinkage:
506 case GlobalValue::LinkOnceAnyLinkage:
507 case GlobalValue::LinkOnceODRLinkage:
508 case GlobalValue::WeakAnyLinkage:
509 case GlobalValue::WeakODRLinkage:
510 // FIXME: make a MCSection.
511 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
512 bCustomSegment = true;
513 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
514 // are also available.
515 break;
516 case GlobalValue::AppendingLinkage:
517 // FIXME: make a MCSection.
518 O << name << "?\tSEGMENT PARA public 'DATA'\n";
519 bCustomSegment = true;
520 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
521 // are also available.
522 break;
523 case GlobalValue::DLLExportLinkage:
524 DLLExportedGVs.insert(name);
525 // FALL THROUGH
526 case GlobalValue::ExternalLinkage:
527 O << "\tpublic " << name << "\n";
528 // FALL THROUGH
529 case GlobalValue::InternalLinkage:
530 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
531 break;
532 default:
533 llvm_unreachable("Unknown linkage type!");
536 if (!bCustomSegment)
537 EmitAlignment(Align, GV);
539 O << name << ":";
540 if (VerboseAsm)
541 O.PadToColumn(MAI->getCommentColumn());
542 O << MAI->getCommentString()
543 << " " << GV->getName();
544 O << '\n';
546 EmitGlobalConstant(C);
548 if (bCustomSegment)
549 O << name << "?\tends\n";
552 bool X86IntelAsmPrinter::doFinalization(Module &M) {
553 // Output linker support code for dllexported globals
554 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
555 O << "; WARNING: The following code is valid only with MASM v8.x"
556 << "and (possible) higher\n"
557 << "; This version of MASM is usually shipped with Microsoft "
558 << "Visual Studio 2005\n"
559 << "; or (possible) further versions. Unfortunately, there is no "
560 << "way to support\n"
561 << "; dllexported symbols in the earlier versions of MASM in fully "
562 << "automatic way\n\n";
563 O << "_drectve\t segment info alias('.drectve')\n";
565 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
566 e = DLLExportedGVs.end();
567 i != e; ++i)
568 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
570 for (StringSet<>::iterator i = DLLExportedFns.begin(),
571 e = DLLExportedFns.end();
572 i != e; ++i)
573 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
575 O << "_drectve\t ends\n";
578 // Bypass X86SharedAsmPrinter::doFinalization().
579 bool Result = AsmPrinter::doFinalization(M);
580 O << "\tend\n";
581 return Result;
584 void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
585 unsigned NumElts = CVA->getNumOperands();
586 if (NumElts == 0) return;
588 // ML does not have escape sequences except '' for '. It also has a maximum
589 // string length of 255.
590 unsigned len = 0;
591 bool inString = false;
592 for (unsigned i = 0; i < NumElts; i++) {
593 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
594 if (len == 0)
595 O << "\tdb ";
597 if (n >= 32 && n <= 127) {
598 if (!inString) {
599 if (len > 0) {
600 O << ",'";
601 len += 2;
602 } else {
603 O << "'";
604 len++;
606 inString = true;
608 if (n == '\'') {
609 O << "'";
610 len++;
612 O << char(n);
613 } else {
614 if (inString) {
615 O << "'";
616 len++;
617 inString = false;
619 if (len > 0) {
620 O << ",";
621 len++;
623 O << n;
624 len += 1 + (n > 9) + (n > 99);
627 if (len > 60) {
628 if (inString) {
629 O << "'";
630 inString = false;
632 O << "\n";
633 len = 0;
637 if (len > 0) {
638 if (inString)
639 O << "'";
640 O << "\n";
644 // Include the auto-generated portion of the assembly writer.
645 #include "X86GenAsmWriter1.inc"