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.
35 SwitchSection("\t.text\n", MF.getFunction());
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 << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
49 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
50 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
52 // Print the assembly for the instruction.
54 printMachineInstruction(II);
58 // We didn't modify anything.
62 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
64 unsigned char value = MI->getOperand(Op).getImmedValue();
65 assert(value <= 7 && "Invalid ssecc argument!");
67 case 0: O << "eq"; break;
68 case 1: O << "lt"; break;
69 case 2: O << "le"; break;
70 case 3: O << "unord"; break;
71 case 4: O << "neq"; break;
72 case 5: O << "nlt"; break;
73 case 6: O << "nle"; break;
74 case 7: O << "ord"; break;
78 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
79 bool elideOffsetKeyword /* = false */) {
80 const MRegisterInfo &RI = *TM.getRegisterInfo();
81 switch (MO.getType()) {
82 case MachineOperand::MO_VirtualRegister:
83 if (Value *V = MO.getVRegValueOrNull()) {
84 O << "<" << V->getName() << ">";
88 case MachineOperand::MO_MachineRegister:
89 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
90 // Bug Workaround: See note in Printer::doInitialization about %.
91 O << "%" << RI.get(MO.getReg()).Name;
93 O << "%reg" << MO.getReg();
96 case MachineOperand::MO_SignExtendedImmed:
97 case MachineOperand::MO_UnextendedImmed:
98 O << (int)MO.getImmedValue();
100 case MachineOperand::MO_MachineBasicBlock: {
101 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
102 O << PrivateGlobalPrefix << "BB"
103 << Mang->getValueName(MBBOp->getParent()->getFunction())
104 << "_" << MBBOp->getNumber () << "\t# "
105 << MBBOp->getBasicBlock ()->getName ();
108 case MachineOperand::MO_PCRelativeDisp:
109 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
112 case MachineOperand::MO_GlobalAddress: {
113 if (!elideOffsetKeyword)
115 O << Mang->getValueName(MO.getGlobal());
116 int Offset = MO.getOffset();
118 O << " + " << Offset;
120 O << " - " << -Offset;
123 case MachineOperand::MO_ExternalSymbol:
124 O << GlobalPrefix << MO.getSymbolName();
127 O << "<unknown operand type>"; return;
131 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
132 assert(isMem(MI, Op) && "Invalid memory reference!");
134 const MachineOperand &BaseReg = MI->getOperand(Op);
135 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
136 const MachineOperand &IndexReg = MI->getOperand(Op+2);
137 const MachineOperand &DispSpec = MI->getOperand(Op+3);
139 if (BaseReg.isFrameIndex()) {
140 O << "[frame slot #" << BaseReg.getFrameIndex();
141 if (DispSpec.getImmedValue())
142 O << " + " << DispSpec.getImmedValue();
145 } else if (BaseReg.isConstantPoolIndex()) {
146 O << "[" << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
147 << BaseReg.getConstantPoolIndex();
149 if (IndexReg.getReg()) {
152 O << ScaleVal << "*";
156 if (DispSpec.getImmedValue())
157 O << " + " << DispSpec.getImmedValue();
163 bool NeedPlus = false;
164 if (BaseReg.getReg()) {
165 printOp(BaseReg, true);
169 if (IndexReg.getReg()) {
170 if (NeedPlus) O << " + ";
172 O << ScaleVal << "*";
177 if (DispSpec.isGlobalAddress()) {
180 printOp(DispSpec, true);
182 int DispVal = DispSpec.getImmedValue();
183 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
198 /// printMachineInstruction -- Print out a single X86 LLVM instruction
199 /// MI in Intel syntax to the current output stream.
201 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
204 // Call the autogenerated instruction printer routines.
205 printInstruction(MI);
208 bool X86IntelAsmPrinter::doInitialization(Module &M) {
209 X86SharedAsmPrinter::doInitialization(M);
210 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
212 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
213 // instruction as a reference to the register named sp, and if you try to
214 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
215 // before being looked up in the symbol table. This creates spurious
216 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
217 // mode, and decorate all register names with percent signs.
218 O << "\t.intel_syntax\n";
222 // Include the auto-generated portion of the assembly writer.
223 #include "X86GenAsmWriter1.inc"