blob: 9cf7c29d77d1d919e315f8463dfb30c57705ec75 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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'.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86IntelAsmPrinter.h"
17#include "X86.h"
18#include "llvm/Module.h"
19#include "llvm/Assembly/Writer.h"
20#include "llvm/Support/Mangler.h"
21using namespace llvm;
22using namespace x86;
23
24/// runOnMachineFunction - This uses the printMachineInstruction()
25/// method to print assembly for each instruction.
26///
27bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28 setupMachineFunction(MF);
29 O << "\n\n";
30
31 // Print out constants referenced by the function
32 printConstantPool(MF.getConstantPool());
33
34 // Print out labels for the function.
35 O << "\t.text\n";
36 emitAlignment(4);
37 O << "\t.globl\t" << CurrentFnName << "\n";
38 if (!forCygwin && !forDarwin)
39 O << "\t.type\t" << CurrentFnName << ", @function\n";
40 O << CurrentFnName << ":\n";
41
42 // Print out code for the function.
43 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
44 I != E; ++I) {
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();
50 II != E; ++II) {
51 // Print the assembly for the instruction.
52 O << "\t";
53 printMachineInstruction(II);
54 }
55 }
56
57 // We didn't modify anything.
58 return false;
59}
60
Nate Begeman6c7cb292005-07-14 22:52:25 +000061void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op,
62 MVT::ValueType VT) {
63 unsigned char value = MI->getOperand(Op).getImmedValue();
64 assert(value <= 7 && "Invalid ssecc argument!");
65 switch (value) {
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;
74 }
75}
76
Chris Lattnerb36cbd02005-07-01 22:44:09 +000077void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
78 bool elideOffsetKeyword /* = false */) {
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() << ">";
84 return;
85 }
86 // FALLTHROUGH
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;
91 else
92 O << "%reg" << MO.getReg();
93 return;
94
95 case MachineOperand::MO_SignExtendedImmed:
96 case MachineOperand::MO_UnextendedImmed:
97 O << (int)MO.getImmedValue();
98 return;
99 case MachineOperand::MO_MachineBasicBlock: {
100 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
101 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
102 << "_" << MBBOp->getNumber () << "\t# "
103 << MBBOp->getBasicBlock ()->getName ();
104 return;
105 }
106 case MachineOperand::MO_PCRelativeDisp:
107 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
108 abort ();
109 return;
110 case MachineOperand::MO_GlobalAddress: {
111 if (!elideOffsetKeyword)
112 O << "OFFSET ";
113 O << Mang->getValueName(MO.getGlobal());
114 int Offset = MO.getOffset();
115 if (Offset > 0)
116 O << " + " << Offset;
117 else if (Offset < 0)
118 O << " - " << -Offset;
119 return;
120 }
121 case MachineOperand::MO_ExternalSymbol:
122 O << GlobalPrefix << MO.getSymbolName();
123 return;
124 default:
125 O << "<unknown operand type>"; return;
126 }
127}
128
129void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
130 assert(isMem(MI, Op) && "Invalid memory reference!");
131
132 const MachineOperand &BaseReg = MI->getOperand(Op);
133 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
134 const MachineOperand &IndexReg = MI->getOperand(Op+2);
135 const MachineOperand &DispSpec = MI->getOperand(Op+3);
136
137 if (BaseReg.isFrameIndex()) {
138 O << "[frame slot #" << BaseReg.getFrameIndex();
139 if (DispSpec.getImmedValue())
140 O << " + " << DispSpec.getImmedValue();
141 O << "]";
142 return;
143 } else if (BaseReg.isConstantPoolIndex()) {
144 O << "[.CPI" << CurrentFnName << "_"
145 << BaseReg.getConstantPoolIndex();
146
147 if (IndexReg.getReg()) {
148 O << " + ";
149 if (ScaleVal != 1)
150 O << ScaleVal << "*";
151 printOp(IndexReg);
152 }
153
154 if (DispSpec.getImmedValue())
155 O << " + " << DispSpec.getImmedValue();
156 O << "]";
157 return;
158 }
159
160 O << "[";
161 bool NeedPlus = false;
162 if (BaseReg.getReg()) {
163 printOp(BaseReg, true);
164 NeedPlus = true;
165 }
166
167 if (IndexReg.getReg()) {
168 if (NeedPlus) O << " + ";
169 if (ScaleVal != 1)
170 O << ScaleVal << "*";
171 printOp(IndexReg);
172 NeedPlus = true;
173 }
174
175 if (DispSpec.isGlobalAddress()) {
176 if (NeedPlus)
177 O << " + ";
178 printOp(DispSpec, true);
179 } else {
180 int DispVal = DispSpec.getImmedValue();
181 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
182 if (NeedPlus)
183 if (DispVal > 0)
184 O << " + ";
185 else {
186 O << " - ";
187 DispVal = -DispVal;
188 }
189 O << DispVal;
190 }
191 }
192 O << "]";
193}
194
195
196/// printMachineInstruction -- Print out a single X86 LLVM instruction
197/// MI in Intel syntax to the current output stream.
198///
199void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
200 ++EmittedInsts;
201
202 // Call the autogenerated instruction printer routines.
203 printInstruction(MI);
204}
205
206bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner6a709a42005-07-03 17:34:39 +0000207 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000208 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
209 //
210 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
211 // instruction as a reference to the register named sp, and if you try to
212 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
213 // before being looked up in the symbol table. This creates spurious
214 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
215 // mode, and decorate all register names with percent signs.
216 O << "\t.intel_syntax\n";
217 return false;
218}
219
220// Include the auto-generated portion of the assembly writer.
221#include "X86GenAsmWriter1.inc"