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 #include "X86IntelAsmPrinter.h"
17 #include "X86TargetAsmInfo.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Module.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetOptions.h"
28 std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const {
29 // Intel asm always emits functions to _text.
33 /// runOnMachineFunction - This uses the printMachineInstruction()
34 /// method to print assembly for each instruction.
36 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
37 SetupMachineFunction(MF);
40 // Print out constants referenced by the function
41 EmitConstantPool(MF.getConstantPool());
43 // Print out labels for the function.
44 const Function *F = MF.getFunction();
45 unsigned CC = F->getCallingConv();
47 // Populate function information map. Actually, We don't want to populate
48 // non-stdcall or non-fastcall functions' information right now.
49 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
50 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
52 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
54 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
56 switch (F->getLinkage()) {
57 default: assert(0 && "Unsupported linkage type!");
58 case Function::InternalLinkage:
61 case Function::DLLExportLinkage:
62 DLLExportedFns.insert(CurrentFnName);
64 case Function::ExternalLinkage:
65 O << "\tpublic " << CurrentFnName << "\n";
70 O << CurrentFnName << "\tproc near\n";
72 // Print out code for the function.
73 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
75 // Print a label for the basic block if there are any predecessors.
76 if (I->pred_begin() != I->pred_end()) {
77 printBasicBlockLabel(I, true);
80 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
82 // Print the assembly for the instruction.
84 printMachineInstruction(II);
88 // Print out jump tables referenced by the function.
89 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
91 O << CurrentFnName << "\tendp\n";
93 // We didn't modify anything.
97 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
98 unsigned char value = MI->getOperand(Op).getImmedValue();
99 assert(value <= 7 && "Invalid ssecc argument!");
101 case 0: O << "eq"; break;
102 case 1: O << "lt"; break;
103 case 2: O << "le"; break;
104 case 3: O << "unord"; break;
105 case 4: O << "neq"; break;
106 case 5: O << "nlt"; break;
107 case 6: O << "nle"; break;
108 case 7: O << "ord"; break;
112 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
113 const char *Modifier) {
114 const MRegisterInfo &RI = *TM.getRegisterInfo();
115 switch (MO.getType()) {
116 case MachineOperand::MO_Register: {
117 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
118 unsigned Reg = MO.getReg();
119 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
120 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
121 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
122 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
123 Reg = getX86SubSuperRegister(Reg, VT);
125 O << RI.get(Reg).Name;
127 O << "reg" << MO.getReg();
130 case MachineOperand::MO_Immediate:
131 O << MO.getImmedValue();
133 case MachineOperand::MO_MachineBasicBlock:
134 printBasicBlockLabel(MO.getMachineBasicBlock());
136 case MachineOperand::MO_JumpTableIndex: {
137 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
138 if (!isMemOp) O << "OFFSET ";
139 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
140 << "_" << MO.getJumpTableIndex();
143 case MachineOperand::MO_ConstantPoolIndex: {
144 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
145 if (!isMemOp) O << "OFFSET ";
146 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
147 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
148 int Offset = MO.getOffset();
150 O << " + " << Offset;
156 case MachineOperand::MO_GlobalAddress: {
157 bool isCallOp = Modifier && !strcmp(Modifier, "call");
158 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
159 GlobalValue *GV = MO.getGlobal();
160 std::string Name = Mang->getValueName(GV);
162 X86SharedAsmPrinter::decorateName(Name, GV);
164 if (!isMemOp && !isCallOp) O << "OFFSET ";
165 if (GV->hasDLLImportLinkage()) {
166 // FIXME: This should be fixed with full support of stdcall & fastcall
171 int Offset = MO.getOffset();
173 O << " + " << Offset;
178 case MachineOperand::MO_ExternalSymbol: {
179 bool isCallOp = Modifier && !strcmp(Modifier, "call");
180 if (!isCallOp) O << "OFFSET ";
181 O << TAI->getGlobalPrefix() << MO.getSymbolName();
185 O << "<unknown operand type>"; return;
189 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
190 const char *Modifier) {
191 assert(isMem(MI, Op) && "Invalid memory reference!");
193 const MachineOperand &BaseReg = MI->getOperand(Op);
194 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
195 const MachineOperand &IndexReg = MI->getOperand(Op+2);
196 const MachineOperand &DispSpec = MI->getOperand(Op+3);
198 if (BaseReg.isFrameIndex()) {
199 O << "[frame slot #" << BaseReg.getFrameIndex();
200 if (DispSpec.getImmedValue())
201 O << " + " << DispSpec.getImmedValue();
207 bool NeedPlus = false;
208 if (BaseReg.getReg()) {
209 printOp(BaseReg, Modifier);
213 if (IndexReg.getReg()) {
214 if (NeedPlus) O << " + ";
216 O << ScaleVal << "*";
217 printOp(IndexReg, Modifier);
221 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
222 DispSpec.isJumpTableIndex()) {
225 printOp(DispSpec, "mem");
227 int DispVal = DispSpec.getImmedValue();
228 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
242 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
243 O << "\"L" << getFunctionNumber() << "$pb\"\n";
244 O << "\"L" << getFunctionNumber() << "$pb\":";
247 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
249 const MRegisterInfo &RI = *TM.getRegisterInfo();
250 unsigned Reg = MO.getReg();
252 default: return true; // Unknown mode.
253 case 'b': // Print QImode register
254 Reg = getX86SubSuperRegister(Reg, MVT::i8);
256 case 'h': // Print QImode high register
257 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
259 case 'w': // Print HImode register
260 Reg = getX86SubSuperRegister(Reg, MVT::i16);
262 case 'k': // Print SImode register
263 Reg = getX86SubSuperRegister(Reg, MVT::i32);
267 O << '%' << RI.get(Reg).Name;
271 /// PrintAsmOperand - Print out an operand for an inline asm expression.
273 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
275 const char *ExtraCode) {
276 // Does this asm operand have a single letter operand modifier?
277 if (ExtraCode && ExtraCode[0]) {
278 if (ExtraCode[1] != 0) return true; // Unknown modifier.
280 switch (ExtraCode[0]) {
281 default: return true; // Unknown modifier.
282 case 'b': // Print QImode register
283 case 'h': // Print QImode high register
284 case 'w': // Print HImode register
285 case 'k': // Print SImode register
286 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
290 printOperand(MI, OpNo);
294 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
297 const char *ExtraCode) {
298 if (ExtraCode && ExtraCode[0])
299 return true; // Unknown modifier.
300 printMemReference(MI, OpNo);
304 /// printMachineInstruction -- Print out a single X86 LLVM instruction
305 /// MI in Intel syntax to the current output stream.
307 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
310 // See if a truncate instruction can be turned into a nop.
311 switch (MI->getOpcode()) {
313 case X86::TRUNC_64to32:
314 case X86::TRUNC_64to16:
315 case X86::TRUNC_32to16:
316 case X86::TRUNC_32to8:
317 case X86::TRUNC_16to8:
318 case X86::TRUNC_32_to8:
319 case X86::TRUNC_16_to8: {
320 const MachineOperand &MO0 = MI->getOperand(0);
321 const MachineOperand &MO1 = MI->getOperand(1);
322 unsigned Reg0 = MO0.getReg();
323 unsigned Reg1 = MO1.getReg();
324 unsigned Opc = MI->getOpcode();
325 if (Opc == X86::TRUNC_64to32)
326 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
327 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
328 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
330 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
331 O << TAI->getCommentString() << " TRUNCATE ";
336 case X86::PsMOVZX64rr32:
337 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
341 // Call the autogenerated instruction printer routines.
342 printInstruction(MI);
345 bool X86IntelAsmPrinter::doInitialization(Module &M) {
346 X86SharedAsmPrinter::doInitialization(M);
348 Mang->markCharUnacceptable('.');
350 O << "\t.686\n\t.model flat\n\n";
352 // Emit declarations for external functions.
353 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
354 if (I->isExternal()) {
355 std::string Name = Mang->getValueName(I);
356 X86SharedAsmPrinter::decorateName(Name, I);
359 if (I->hasDLLImportLinkage()) {
362 O << Name << ":near\n";
365 // Emit declarations for external globals. Note that VC++ always declares
366 // external globals to have type byte, and if that's good enough for VC++...
367 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
369 if (I->isExternal()) {
370 std::string Name = Mang->getValueName(I);
373 if (I->hasDLLImportLinkage()) {
376 O << Name << ":byte\n";
383 bool X86IntelAsmPrinter::doFinalization(Module &M) {
384 const TargetData *TD = TM.getTargetData();
386 // Print out module-level global variables here.
387 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
389 if (I->isExternal()) continue; // External global require no code
391 // Check to see if this is a special global used by LLVM, if so, emit it.
392 if (EmitSpecialLLVMGlobal(I))
395 std::string name = Mang->getValueName(I);
396 Constant *C = I->getInitializer();
397 unsigned Align = TD->getPreferredAlignmentLog(I);
398 bool bCustomSegment = false;
400 switch (I->getLinkage()) {
401 case GlobalValue::LinkOnceLinkage:
402 case GlobalValue::WeakLinkage:
403 SwitchToDataSection("");
404 O << name << "?\tsegment common 'COMMON'\n";
405 bCustomSegment = true;
406 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
407 // are also available.
409 case GlobalValue::AppendingLinkage:
410 SwitchToDataSection("");
411 O << name << "?\tsegment public 'DATA'\n";
412 bCustomSegment = true;
413 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
414 // are also available.
416 case GlobalValue::DLLExportLinkage:
417 DLLExportedGVs.insert(name);
419 case GlobalValue::ExternalLinkage:
420 O << "\tpublic " << name << "\n";
422 case GlobalValue::InternalLinkage:
423 SwitchToDataSection(TAI->getDataSection(), I);
426 assert(0 && "Unknown linkage type!");
430 EmitAlignment(Align, I);
432 O << name << ":\t\t\t\t" << TAI->getCommentString()
433 << " " << I->getName() << '\n';
435 EmitGlobalConstant(C);
438 O << name << "?\tends\n";
441 // Output linker support code for dllexported globals
442 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
443 (DLLExportedFns.begin() != DLLExportedFns.end())) {
444 SwitchToDataSection("");
445 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
446 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
447 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
448 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
449 O << "_drectve\t segment info alias('.drectve')\n";
452 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
453 e = DLLExportedGVs.end();
455 O << "\t db ' /EXPORT:" << *i << ",data'\n";
458 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
459 e = DLLExportedFns.end();
461 O << "\t db ' /EXPORT:" << *i << "'\n";
464 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
465 (DLLExportedFns.begin() != DLLExportedFns.end())) {
466 O << "_drectve\t ends\n";
469 // Bypass X86SharedAsmPrinter::doFinalization().
470 AsmPrinter::doFinalization(M);
471 SwitchToDataSection("");
473 return false; // success
476 void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
477 unsigned NumElts = CVA->getNumOperands();
479 // ML does not have escape sequences except '' for '. It also has a maximum
480 // string length of 255.
482 bool inString = false;
483 for (unsigned i = 0; i < NumElts; i++) {
484 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
488 if (n >= 32 && n <= 127) {
515 len += 1 + (n > 9) + (n > 99);
536 // Include the auto-generated portion of the assembly writer.
537 #include "X86GenAsmWriter1.inc"