1 //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
3 // The LLVM Compiler Infrastructure
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.
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<X86FunctionInfo>();
56 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
58 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
60 switch (F->getLinkage()) {
61 default: assert(0 && "Unsupported linkage type!");
62 case Function::InternalLinkage:
65 case Function::DLLExportLinkage:
66 DLLExportedFns.insert(CurrentFnName);
68 case Function::ExternalLinkage:
69 O << "\tpublic " << CurrentFnName << "\n";
74 O << CurrentFnName << "\tproc near\n";
76 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
79 // Print a label for the basic block if there are any predecessors.
80 if (I->pred_begin() != I->pred_end()) {
81 printBasicBlockLabel(I, true);
84 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
86 // 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).getImmedValue();
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 const MRegisterInfo &RI = *TM.getRegisterInfo();
119 switch (MO.getType()) {
120 case MachineOperand::MO_Register: {
121 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
122 unsigned Reg = MO.getReg();
123 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
124 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
125 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
126 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
127 Reg = getX86SubSuperRegister(Reg, VT);
129 O << RI.get(Reg).Name;
131 O << "reg" << MO.getReg();
134 case MachineOperand::MO_Immediate:
135 O << MO.getImmedValue();
137 case MachineOperand::MO_MachineBasicBlock:
138 printBasicBlockLabel(MO.getMachineBasicBlock());
140 case MachineOperand::MO_JumpTableIndex: {
141 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
142 if (!isMemOp) O << "OFFSET ";
143 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
144 << "_" << MO.getJumpTableIndex();
147 case MachineOperand::MO_ConstantPoolIndex: {
148 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
149 if (!isMemOp) O << "OFFSET ";
150 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
151 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
152 int Offset = MO.getOffset();
154 O << " + " << Offset;
160 case MachineOperand::MO_GlobalAddress: {
161 bool isCallOp = Modifier && !strcmp(Modifier, "call");
162 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
163 GlobalValue *GV = MO.getGlobal();
164 std::string Name = Mang->getValueName(GV);
166 X86SharedAsmPrinter::decorateName(Name, GV);
168 if (!isMemOp && !isCallOp) O << "OFFSET ";
169 if (GV->hasDLLImportLinkage()) {
170 // FIXME: This should be fixed with full support of stdcall & fastcall
175 int Offset = MO.getOffset();
177 O << " + " << Offset;
182 case MachineOperand::MO_ExternalSymbol: {
183 bool isCallOp = Modifier && !strcmp(Modifier, "call");
184 if (!isCallOp) O << "OFFSET ";
185 O << TAI->getGlobalPrefix() << MO.getSymbolName();
189 O << "<unknown operand type>"; return;
193 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
194 const char *Modifier) {
195 assert(isMem(MI, Op) && "Invalid memory reference!");
197 const MachineOperand &BaseReg = MI->getOperand(Op);
198 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
199 const MachineOperand &IndexReg = MI->getOperand(Op+2);
200 const MachineOperand &DispSpec = MI->getOperand(Op+3);
202 if (BaseReg.isFrameIndex()) {
203 O << "[frame slot #" << BaseReg.getFrameIndex();
204 if (DispSpec.getImmedValue())
205 O << " + " << DispSpec.getImmedValue();
211 bool NeedPlus = false;
212 if (BaseReg.getReg()) {
213 printOp(BaseReg, Modifier);
217 if (IndexReg.getReg()) {
218 if (NeedPlus) O << " + ";
220 O << ScaleVal << "*";
221 printOp(IndexReg, Modifier);
225 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
226 DispSpec.isJumpTableIndex()) {
229 printOp(DispSpec, "mem");
231 int DispVal = DispSpec.getImmedValue();
232 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
246 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
247 O << "\"L" << getFunctionNumber() << "$pb\"\n";
248 O << "\"L" << getFunctionNumber() << "$pb\":";
251 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
253 const MRegisterInfo &RI = *TM.getRegisterInfo();
254 unsigned Reg = MO.getReg();
256 default: return true; // Unknown mode.
257 case 'b': // Print QImode register
258 Reg = getX86SubSuperRegister(Reg, MVT::i8);
260 case 'h': // Print QImode high register
261 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
263 case 'w': // Print HImode register
264 Reg = getX86SubSuperRegister(Reg, MVT::i16);
266 case 'k': // Print SImode register
267 Reg = getX86SubSuperRegister(Reg, MVT::i32);
271 O << '%' << RI.get(Reg).Name;
275 /// PrintAsmOperand - Print out an operand for an inline asm expression.
277 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279 const char *ExtraCode) {
280 // Does this asm operand have a single letter operand modifier?
281 if (ExtraCode && ExtraCode[0]) {
282 if (ExtraCode[1] != 0) return true; // Unknown modifier.
284 switch (ExtraCode[0]) {
285 default: return true; // Unknown modifier.
286 case 'b': // Print QImode register
287 case 'h': // Print QImode high register
288 case 'w': // Print HImode register
289 case 'k': // Print SImode register
290 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
294 printOperand(MI, OpNo);
298 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
301 const char *ExtraCode) {
302 if (ExtraCode && ExtraCode[0])
303 return true; // Unknown modifier.
304 printMemReference(MI, OpNo);
308 /// printMachineInstruction -- Print out a single X86 LLVM instruction
309 /// MI in Intel syntax to the current output stream.
311 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
314 // See if a truncate instruction can be turned into a nop.
315 switch (MI->getOpcode()) {
317 case X86::TRUNC_64to32:
318 case X86::TRUNC_64to16:
319 case X86::TRUNC_32to16:
320 case X86::TRUNC_32to8:
321 case X86::TRUNC_16to8:
322 case X86::TRUNC_32_to8:
323 case X86::TRUNC_16_to8: {
324 const MachineOperand &MO0 = MI->getOperand(0);
325 const MachineOperand &MO1 = MI->getOperand(1);
326 unsigned Reg0 = MO0.getReg();
327 unsigned Reg1 = MO1.getReg();
328 unsigned Opc = MI->getOpcode();
329 if (Opc == X86::TRUNC_64to32)
330 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
331 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
332 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
334 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
335 O << TAI->getCommentString() << " TRUNCATE ";
340 case X86::PsMOVZX64rr32:
341 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
345 // Call the autogenerated instruction printer routines.
346 printInstruction(MI);
349 bool X86IntelAsmPrinter::doInitialization(Module &M) {
350 X86SharedAsmPrinter::doInitialization(M);
352 Mang->markCharUnacceptable('.');
354 O << "\t.686\n\t.model flat\n\n";
356 // Emit declarations for external functions.
357 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
358 if (I->isExternal()) {
359 std::string Name = Mang->getValueName(I);
360 X86SharedAsmPrinter::decorateName(Name, I);
363 if (I->hasDLLImportLinkage()) {
366 O << Name << ":near\n";
369 // Emit declarations for external globals. Note that VC++ always declares
370 // external globals to have type byte, and if that's good enough for VC++...
371 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
373 if (I->isExternal()) {
374 std::string Name = Mang->getValueName(I);
377 if (I->hasDLLImportLinkage()) {
380 O << Name << ":byte\n";
387 bool X86IntelAsmPrinter::doFinalization(Module &M) {
388 const TargetData *TD = TM.getTargetData();
390 // Print out module-level global variables here.
391 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
393 if (I->isExternal()) continue; // External global require no code
395 // Check to see if this is a special global used by LLVM, if so, emit it.
396 if (EmitSpecialLLVMGlobal(I))
399 std::string name = Mang->getValueName(I);
400 Constant *C = I->getInitializer();
401 unsigned Align = TD->getPreferredAlignmentLog(I);
402 bool bCustomSegment = false;
404 switch (I->getLinkage()) {
405 case GlobalValue::LinkOnceLinkage:
406 case GlobalValue::WeakLinkage:
407 SwitchToDataSection("");
408 O << name << "?\tsegment common 'COMMON'\n";
409 bCustomSegment = true;
410 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
411 // are also available.
413 case GlobalValue::AppendingLinkage:
414 SwitchToDataSection("");
415 O << name << "?\tsegment public 'DATA'\n";
416 bCustomSegment = true;
417 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
418 // are also available.
420 case GlobalValue::DLLExportLinkage:
421 DLLExportedGVs.insert(name);
423 case GlobalValue::ExternalLinkage:
424 O << "\tpublic " << name << "\n";
426 case GlobalValue::InternalLinkage:
427 SwitchToDataSection(TAI->getDataSection(), I);
430 assert(0 && "Unknown linkage type!");
434 EmitAlignment(Align, I);
436 O << name << ":\t\t\t\t" << TAI->getCommentString()
437 << " " << I->getName() << '\n';
439 EmitGlobalConstant(C);
442 O << name << "?\tends\n";
445 // Output linker support code for dllexported globals
446 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
447 (DLLExportedFns.begin() != DLLExportedFns.end())) {
448 SwitchToDataSection("");
449 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
450 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
451 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
452 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
453 O << "_drectve\t segment info alias('.drectve')\n";
456 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
457 e = DLLExportedGVs.end();
459 O << "\t db ' /EXPORT:" << *i << ",data'\n";
462 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
463 e = DLLExportedFns.end();
465 O << "\t db ' /EXPORT:" << *i << "'\n";
468 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
469 (DLLExportedFns.begin() != DLLExportedFns.end())) {
470 O << "_drectve\t ends\n";
473 // Bypass X86SharedAsmPrinter::doFinalization().
474 AsmPrinter::doFinalization(M);
475 SwitchToDataSection("");
477 return false; // success
480 void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
481 unsigned NumElts = CVA->getNumOperands();
483 // ML does not have escape sequences except '' for '. It also has a maximum
484 // string length of 255.
486 bool inString = false;
487 for (unsigned i = 0; i < NumElts; i++) {
488 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
492 if (n >= 32 && n <= 127) {
519 len += 1 + (n > 9) + (n > 99);
540 // Include the auto-generated portion of the assembly writer.
541 #include "X86GenAsmWriter1.inc"