1 //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 "X86TargetAsmInfo.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"
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
32 std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const {
33 // Intel asm always emits functions to _text.
37 /// runOnMachineFunction - This uses the printMachineInstruction()
38 /// method to print assembly for each instruction.
40 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
41 SetupMachineFunction(MF);
44 // Print out constants referenced by the function
45 EmitConstantPool(MF.getConstantPool());
47 // Print out labels for the function.
48 const Function *F = MF.getFunction();
49 unsigned CC = F->getCallingConv();
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>();
56 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
58 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
60 unsigned FnAlign = OptimizeForSize ? 1 : 4;
61 switch (F->getLinkage()) {
62 default: assert(0 && "Unsupported linkage type!");
63 case Function::InternalLinkage:
64 EmitAlignment(FnAlign);
66 case Function::DLLExportLinkage:
67 DLLExportedFns.insert(CurrentFnName);
69 case Function::ExternalLinkage:
70 O << "\tpublic " << CurrentFnName << "\n";
71 EmitAlignment(FnAlign);
75 O << CurrentFnName << "\tproc near\n";
77 // Print out code for the function.
78 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
80 // Print a label for the basic block if there are any predecessors.
81 if (!I->pred_empty()) {
82 printBasicBlockLabel(I, true, true);
85 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
87 // Print the assembly for the instruction.
88 printMachineInstruction(II);
92 // Print out jump tables referenced by the function.
93 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
95 O << CurrentFnName << "\tendp\n";
97 // We didn't modify anything.
101 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
102 unsigned char value = MI->getOperand(Op).getImm();
103 assert(value <= 7 && "Invalid ssecc argument!");
105 case 0: O << "eq"; break;
106 case 1: O << "lt"; break;
107 case 2: O << "le"; break;
108 case 3: O << "unord"; break;
109 case 4: O << "neq"; break;
110 case 5: O << "nlt"; break;
111 case 6: O << "nle"; break;
112 case 7: O << "ord"; break;
116 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
117 const char *Modifier) {
118 switch (MO.getType()) {
119 case MachineOperand::MO_Register: {
120 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
121 unsigned Reg = MO.getReg();
122 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
123 MVT 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);
128 O << TRI->getAsmName(Reg);
130 O << "reg" << MO.getReg();
133 case MachineOperand::MO_Immediate:
136 case MachineOperand::MO_MachineBasicBlock:
137 printBasicBlockLabel(MO.getMBB());
139 case MachineOperand::MO_JumpTableIndex: {
140 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
141 if (!isMemOp) O << "OFFSET ";
142 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
143 << "_" << MO.getIndex();
146 case MachineOperand::MO_ConstantPoolIndex: {
147 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
148 if (!isMemOp) O << "OFFSET ";
149 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
150 << getFunctionNumber() << "_" << MO.getIndex();
151 int Offset = MO.getOffset();
153 O << " + " << Offset;
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);
165 X86SharedAsmPrinter::decorateName(Name, GV);
167 if (!isMemOp && !isCallOp) O << "OFFSET ";
168 if (GV->hasDLLImportLinkage()) {
169 // FIXME: This should be fixed with full support of stdcall & fastcall
174 int Offset = MO.getOffset();
176 O << " + " << Offset;
181 case MachineOperand::MO_ExternalSymbol: {
182 bool isCallOp = Modifier && !strcmp(Modifier, "call");
183 if (!isCallOp) O << "OFFSET ";
184 O << TAI->getGlobalPrefix() << MO.getSymbolName();
188 O << "<unknown operand type>"; return;
192 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
193 const char *Modifier) {
194 assert(isMem(MI, Op) && "Invalid memory reference!");
196 const MachineOperand &BaseReg = MI->getOperand(Op);
197 int ScaleVal = MI->getOperand(Op+1).getImm();
198 const MachineOperand &IndexReg = MI->getOperand(Op+2);
199 const MachineOperand &DispSpec = MI->getOperand(Op+3);
202 bool NeedPlus = false;
203 if (BaseReg.getReg()) {
204 printOp(BaseReg, Modifier);
208 if (IndexReg.getReg()) {
209 if (NeedPlus) O << " + ";
211 O << ScaleVal << "*";
212 printOp(IndexReg, Modifier);
216 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
217 DispSpec.isJumpTableIndex()) {
220 printOp(DispSpec, "mem");
222 int DispVal = DispSpec.getImm();
223 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
238 void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
239 const MachineBasicBlock *MBB) const {
240 if (!TAI->getSetDirective())
243 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
244 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
245 printBasicBlockLabel(MBB, false, false, false);
246 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
249 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
250 O << "\"L" << getFunctionNumber() << "$pb\"\n";
251 O << "\"L" << getFunctionNumber() << "$pb\":";
254 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
256 unsigned Reg = MO.getReg();
258 default: return true; // Unknown mode.
259 case 'b': // Print QImode register
260 Reg = getX86SubSuperRegister(Reg, MVT::i8);
262 case 'h': // Print QImode high register
263 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
265 case 'w': // Print HImode register
266 Reg = getX86SubSuperRegister(Reg, MVT::i16);
268 case 'k': // Print SImode register
269 Reg = getX86SubSuperRegister(Reg, MVT::i32);
273 O << '%' << TRI->getAsmName(Reg);
277 /// PrintAsmOperand - Print out an operand for an inline asm expression.
279 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
281 const char *ExtraCode) {
282 // Does this asm operand have a single letter operand modifier?
283 if (ExtraCode && ExtraCode[0]) {
284 if (ExtraCode[1] != 0) return true; // Unknown modifier.
286 switch (ExtraCode[0]) {
287 default: return true; // Unknown modifier.
288 case 'b': // Print QImode register
289 case 'h': // Print QImode high register
290 case 'w': // Print HImode register
291 case 'k': // Print SImode register
292 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
296 printOperand(MI, OpNo);
300 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
303 const char *ExtraCode) {
304 if (ExtraCode && ExtraCode[0])
305 return true; // Unknown modifier.
306 printMemReference(MI, OpNo);
310 /// printMachineInstruction -- Print out a single X86 LLVM instruction
311 /// MI in Intel syntax to the current output stream.
313 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
316 // Call the autogenerated instruction printer routines.
317 printInstruction(MI);
320 bool X86IntelAsmPrinter::doInitialization(Module &M) {
321 bool Result = X86SharedAsmPrinter::doInitialization(M);
323 Mang->markCharUnacceptable('.');
325 O << "\t.686\n\t.model flat\n\n";
327 // Emit declarations for external functions.
328 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
329 if (I->isDeclaration()) {
330 std::string Name = Mang->getValueName(I);
331 X86SharedAsmPrinter::decorateName(Name, I);
334 if (I->hasDLLImportLinkage()) {
337 O << Name << ":near\n";
340 // Emit declarations for external globals. Note that VC++ always declares
341 // external globals to have type byte, and if that's good enough for VC++...
342 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
344 if (I->isDeclaration()) {
345 std::string Name = Mang->getValueName(I);
348 if (I->hasDLLImportLinkage()) {
351 O << Name << ":byte\n";
358 bool X86IntelAsmPrinter::doFinalization(Module &M) {
359 const TargetData *TD = TM.getTargetData();
361 // Print out module-level global variables here.
362 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
364 if (I->isDeclaration()) continue; // External global require no code
366 // Check to see if this is a special global used by LLVM, if so, emit it.
367 if (EmitSpecialLLVMGlobal(I))
370 std::string name = Mang->getValueName(I);
371 Constant *C = I->getInitializer();
372 unsigned Align = TD->getPreferredAlignmentLog(I);
373 bool bCustomSegment = false;
375 switch (I->getLinkage()) {
376 case GlobalValue::CommonLinkage:
377 case GlobalValue::LinkOnceLinkage:
378 case GlobalValue::WeakLinkage:
379 SwitchToDataSection("");
380 O << name << "?\tsegment common 'COMMON'\n";
381 bCustomSegment = true;
382 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
383 // are also available.
385 case GlobalValue::AppendingLinkage:
386 SwitchToDataSection("");
387 O << name << "?\tsegment public 'DATA'\n";
388 bCustomSegment = true;
389 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
390 // are also available.
392 case GlobalValue::DLLExportLinkage:
393 DLLExportedGVs.insert(name);
395 case GlobalValue::ExternalLinkage:
396 O << "\tpublic " << name << "\n";
398 case GlobalValue::InternalLinkage:
399 SwitchToDataSection(TAI->getDataSection(), I);
402 assert(0 && "Unknown linkage type!");
406 EmitAlignment(Align, I);
408 O << name << ":\t\t\t\t" << TAI->getCommentString()
409 << " " << I->getName() << '\n';
411 EmitGlobalConstant(C);
414 O << name << "?\tends\n";
417 // Output linker support code for dllexported globals
418 if (!DLLExportedGVs.empty() ||
419 !DLLExportedFns.empty()) {
420 SwitchToDataSection("");
421 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
422 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
423 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
424 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
425 O << "_drectve\t segment info alias('.drectve')\n";
428 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
429 e = DLLExportedGVs.end();
431 O << "\t db ' /EXPORT:" << *i << ",data'\n";
434 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
435 e = DLLExportedFns.end();
437 O << "\t db ' /EXPORT:" << *i << "'\n";
440 if (!DLLExportedGVs.empty() ||
441 !DLLExportedFns.empty()) {
442 O << "_drectve\t ends\n";
445 // Bypass X86SharedAsmPrinter::doFinalization().
446 bool Result = AsmPrinter::doFinalization(M);
447 SwitchToDataSection("");
452 void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
453 unsigned NumElts = CVA->getNumOperands();
455 // ML does not have escape sequences except '' for '. It also has a maximum
456 // string length of 255.
458 bool inString = false;
459 for (unsigned i = 0; i < NumElts; i++) {
460 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
464 if (n >= 32 && n <= 127) {
491 len += 1 + (n > 9) + (n > 99);
512 // Include the auto-generated portion of the assembly writer.
513 #include "X86GenAsmWriter1.inc"