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 EmitConstantPool(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 (HasDotTypeDotSizeDirective)
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) {
63 unsigned char value = MI->getOperand(Op).getImmedValue();
64 assert(value <= 7 && "Invalid ssecc argument!");
66 case 0: O << "eq"; break;
67 case 1: O << "lt"; break;
68 case 2: O << "le"; break;
69 case 3: O << "unord"; break;
70 case 4: O << "neq"; break;
71 case 5: O << "nlt"; break;
72 case 6: O << "nle"; break;
73 case 7: O << "ord"; break;
77 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
78 const char *Modifier) {
79 const MRegisterInfo &RI = *TM.getRegisterInfo();
80 switch (MO.getType()) {
81 case MachineOperand::MO_VirtualRegister:
82 if (Value *V = MO.getVRegValueOrNull()) {
83 O << "<" << V->getName() << ">";
87 case MachineOperand::MO_MachineRegister:
88 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
89 // Bug Workaround: See note in Printer::doInitialization about %.
90 O << "%" << RI.get(MO.getReg()).Name;
92 O << "%reg" << MO.getReg();
95 case MachineOperand::MO_SignExtendedImmed:
96 case MachineOperand::MO_UnextendedImmed:
97 O << (int)MO.getImmedValue();
99 case MachineOperand::MO_MachineBasicBlock: {
100 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
101 O << PrivateGlobalPrefix << "BB"
102 << Mang->getValueName(MBBOp->getParent()->getFunction())
103 << "_" << MBBOp->getNumber () << "\t# "
104 << MBBOp->getBasicBlock ()->getName ();
107 case MachineOperand::MO_PCRelativeDisp:
108 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
111 case MachineOperand::MO_GlobalAddress: {
112 if (!Modifier || strcmp(Modifier, "call") || strcmp(Modifier, "mem"))
114 O << Mang->getValueName(MO.getGlobal());
115 int Offset = MO.getOffset();
117 O << " + " << Offset;
122 case MachineOperand::MO_ExternalSymbol:
123 O << GlobalPrefix << MO.getSymbolName();
126 O << "<unknown operand type>"; return;
130 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
131 assert(isMem(MI, Op) && "Invalid memory reference!");
133 const MachineOperand &BaseReg = MI->getOperand(Op);
134 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
135 const MachineOperand &IndexReg = MI->getOperand(Op+2);
136 const MachineOperand &DispSpec = MI->getOperand(Op+3);
138 if (BaseReg.isFrameIndex()) {
139 O << "[frame slot #" << BaseReg.getFrameIndex();
140 if (DispSpec.getImmedValue())
141 O << " + " << DispSpec.getImmedValue();
144 } else if (BaseReg.isConstantPoolIndex()) {
145 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
146 << BaseReg.getConstantPoolIndex();
148 if (IndexReg.getReg()) {
151 O << ScaleVal << "*";
155 if (DispSpec.getImmedValue())
156 O << " + " << DispSpec.getImmedValue();
162 bool NeedPlus = false;
163 if (BaseReg.getReg()) {
164 printOp(BaseReg, "mem");
168 if (IndexReg.getReg()) {
169 if (NeedPlus) O << " + ";
171 O << ScaleVal << "*";
176 if (DispSpec.isGlobalAddress()) {
179 printOp(DispSpec, "mem");
181 int DispVal = DispSpec.getImmedValue();
182 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
197 /// printMachineInstruction -- Print out a single X86 LLVM instruction
198 /// MI in Intel syntax to the current output stream.
200 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
203 // Call the autogenerated instruction printer routines.
204 printInstruction(MI);
207 bool X86IntelAsmPrinter::doInitialization(Module &M) {
208 X86SharedAsmPrinter::doInitialization(M);
209 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
211 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
212 // instruction as a reference to the register named sp, and if you try to
213 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
214 // before being looked up in the symbol table. This creates spurious
215 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
216 // mode, and decorate all register names with percent signs.
217 O << "\t.intel_syntax\n";
221 // Include the auto-generated portion of the assembly writer.
222 #include "X86GenAsmWriter1.inc"