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"
18 #include "llvm/Module.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/Support/Mangler.h"
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
27 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28 setupMachineFunction(MF);
31 // Print out constants referenced by the function
32 printConstantPool(MF.getConstantPool());
34 // Print out labels for the function.
37 O << "\t.globl\t" << CurrentFnName << "\n";
38 if (!forCygwin && !forDarwin)
39 O << "\t.type\t" << CurrentFnName << ", @function\n";
40 O << CurrentFnName << ":\n";
42 // Print out code for the function.
43 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
45 // Print a label for the basic block if there are any predecessors.
46 if (I->pred_begin() != I->pred_end())
47 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
48 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
49 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
51 // Print the assembly for the instruction.
53 printMachineInstruction(II);
57 // We didn't modify anything.
61 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
62 bool elideOffsetKeyword /* = false */) {
63 const MRegisterInfo &RI = *TM.getRegisterInfo();
64 switch (MO.getType()) {
65 case MachineOperand::MO_VirtualRegister:
66 if (Value *V = MO.getVRegValueOrNull()) {
67 O << "<" << V->getName() << ">";
71 case MachineOperand::MO_MachineRegister:
72 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
73 // Bug Workaround: See note in Printer::doInitialization about %.
74 O << "%" << RI.get(MO.getReg()).Name;
76 O << "%reg" << MO.getReg();
79 case MachineOperand::MO_SignExtendedImmed:
80 case MachineOperand::MO_UnextendedImmed:
81 O << (int)MO.getImmedValue();
83 case MachineOperand::MO_MachineBasicBlock: {
84 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
85 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
86 << "_" << MBBOp->getNumber () << "\t# "
87 << MBBOp->getBasicBlock ()->getName ();
90 case MachineOperand::MO_PCRelativeDisp:
91 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
94 case MachineOperand::MO_GlobalAddress: {
95 if (!elideOffsetKeyword)
97 O << Mang->getValueName(MO.getGlobal());
98 int Offset = MO.getOffset();
100 O << " + " << Offset;
102 O << " - " << -Offset;
105 case MachineOperand::MO_ExternalSymbol:
106 O << GlobalPrefix << MO.getSymbolName();
109 O << "<unknown operand type>"; return;
113 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
114 assert(isMem(MI, Op) && "Invalid memory reference!");
116 const MachineOperand &BaseReg = MI->getOperand(Op);
117 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
118 const MachineOperand &IndexReg = MI->getOperand(Op+2);
119 const MachineOperand &DispSpec = MI->getOperand(Op+3);
121 if (BaseReg.isFrameIndex()) {
122 O << "[frame slot #" << BaseReg.getFrameIndex();
123 if (DispSpec.getImmedValue())
124 O << " + " << DispSpec.getImmedValue();
127 } else if (BaseReg.isConstantPoolIndex()) {
128 O << "[.CPI" << CurrentFnName << "_"
129 << BaseReg.getConstantPoolIndex();
131 if (IndexReg.getReg()) {
134 O << ScaleVal << "*";
138 if (DispSpec.getImmedValue())
139 O << " + " << DispSpec.getImmedValue();
145 bool NeedPlus = false;
146 if (BaseReg.getReg()) {
147 printOp(BaseReg, true);
151 if (IndexReg.getReg()) {
152 if (NeedPlus) O << " + ";
154 O << ScaleVal << "*";
159 if (DispSpec.isGlobalAddress()) {
162 printOp(DispSpec, true);
164 int DispVal = DispSpec.getImmedValue();
165 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
180 /// printMachineInstruction -- Print out a single X86 LLVM instruction
181 /// MI in Intel syntax to the current output stream.
183 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
186 // Call the autogenerated instruction printer routines.
187 printInstruction(MI);
190 bool X86IntelAsmPrinter::doInitialization(Module &M) {
191 X86SharedAsmPrinter::doInitialization(M);
192 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
194 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
195 // instruction as a reference to the register named sp, and if you try to
196 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
197 // before being looked up in the symbol table. This creates spurious
198 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
199 // mode, and decorate all register names with percent signs.
200 O << "\t.intel_syntax\n";
204 // Include the auto-generated portion of the assembly writer.
205 #include "X86GenAsmWriter1.inc"