blob: 5bc8b243723cd08e0a7be1220dc4c0f647ebd111 [file] [log] [blame]
Chris Lattnerb9740462005-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"
Evan Cheng5588de92006-02-18 00:15:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000022using namespace llvm;
23using namespace x86;
24
25/// runOnMachineFunction - This uses the printMachineInstruction()
26/// method to print assembly for each instruction.
27///
28bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng30d7b702006-03-07 02:02:57 +000029 // Let PassManager know we need debug information and relay
30 // the MachineDebugInfo address on to DwarfWriter.
31 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
32
Chris Lattner99946fb2005-11-21 07:51:23 +000033 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000034 O << "\n\n";
35
Evan Cheng30d7b702006-03-07 02:02:57 +000036 // Emit pre-function debug information.
37 DW.BeginFunction(MF);
38
Chris Lattnerb9740462005-07-01 22:44:09 +000039 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000040 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000041
42 // Print out labels for the function.
Chris Lattner050bf2f2005-11-21 07:16:34 +000043 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner99946fb2005-11-21 07:51:23 +000044 EmitAlignment(4);
Chris Lattnerb9740462005-07-01 22:44:09 +000045 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac6cb462005-11-21 23:06:54 +000046 if (HasDotTypeDotSizeDirective)
Chris Lattnerb9740462005-07-01 22:44:09 +000047 O << "\t.type\t" << CurrentFnName << ", @function\n";
48 O << CurrentFnName << ":\n";
49
50 // Print out code for the function.
51 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
52 I != E; ++I) {
53 // Print a label for the basic block if there are any predecessors.
54 if (I->pred_begin() != I->pred_end())
Chris Lattnerd3656272005-11-21 07:43:59 +000055 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
56 << ":\t"
Chris Lattnerb9740462005-07-01 22:44:09 +000057 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
58 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
59 II != E; ++II) {
60 // Print the assembly for the instruction.
61 O << "\t";
62 printMachineInstruction(II);
63 }
64 }
65
Evan Cheng30d7b702006-03-07 02:02:57 +000066 // Emit post-function debug information.
67 DW.EndFunction(MF);
68
Chris Lattnerb9740462005-07-01 22:44:09 +000069 // We didn't modify anything.
70 return false;
71}
72
Nate Begeman6f8c1ac2005-11-30 18:54:35 +000073void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-07-14 22:52:25 +000074 unsigned char value = MI->getOperand(Op).getImmedValue();
75 assert(value <= 7 && "Invalid ssecc argument!");
76 switch (value) {
77 case 0: O << "eq"; break;
78 case 1: O << "lt"; break;
79 case 2: O << "le"; break;
80 case 3: O << "unord"; break;
81 case 4: O << "neq"; break;
82 case 5: O << "nlt"; break;
83 case 6: O << "nle"; break;
84 case 7: O << "ord"; break;
85 }
86}
87
Chris Lattnerd62a3bf2006-02-06 23:41:19 +000088void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
89 const char *Modifier) {
Chris Lattnerb9740462005-07-01 22:44:09 +000090 const MRegisterInfo &RI = *TM.getRegisterInfo();
91 switch (MO.getType()) {
92 case MachineOperand::MO_VirtualRegister:
93 if (Value *V = MO.getVRegValueOrNull()) {
94 O << "<" << V->getName() << ">";
95 return;
96 }
97 // FALLTHROUGH
98 case MachineOperand::MO_MachineRegister:
99 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
100 // Bug Workaround: See note in Printer::doInitialization about %.
101 O << "%" << RI.get(MO.getReg()).Name;
102 else
103 O << "%reg" << MO.getReg();
104 return;
105
106 case MachineOperand::MO_SignExtendedImmed:
107 case MachineOperand::MO_UnextendedImmed:
108 O << (int)MO.getImmedValue();
109 return;
110 case MachineOperand::MO_MachineBasicBlock: {
111 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattnerd3656272005-11-21 07:43:59 +0000112 O << PrivateGlobalPrefix << "BB"
113 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb9740462005-07-01 22:44:09 +0000114 << "_" << MBBOp->getNumber () << "\t# "
115 << MBBOp->getBasicBlock ()->getName ();
116 return;
117 }
118 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerde02d772006-01-22 23:41:00 +0000119 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
Chris Lattnerb9740462005-07-01 22:44:09 +0000120 abort ();
121 return;
Evan Cheng75b87832006-02-26 08:28:12 +0000122 case MachineOperand::MO_ConstantPoolIndex: {
123 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
124 if (!isMemOp) O << "OFFSET ";
125 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
126 << MO.getConstantPoolIndex();
127 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
128 O << "-\"L" << getFunctionNumber() << "$pb\"";
129 int Offset = MO.getOffset();
130 if (Offset > 0)
131 O << " + " << Offset;
132 else if (Offset < 0)
133 O << Offset;
134 O << "]";
135 return;
136 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000137 case MachineOperand::MO_GlobalAddress: {
Evan Cheng5588de92006-02-18 00:15:05 +0000138 bool isCallOp = Modifier && !strcmp(Modifier, "call");
139 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
140 if (!isMemOp && !isCallOp) O << "OFFSET ";
Evan Cheng73136df2006-02-22 20:19:42 +0000141 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
Evan Cheng5588de92006-02-18 00:15:05 +0000142 GlobalValue *GV = MO.getGlobal();
143 std::string Name = Mang->getValueName(GV);
144 if (!isMemOp && !isCallOp) O << '$';
145 // Link-once, External, or Weakly-linked global variables need
146 // non-lazily-resolved stubs
147 if (GV->isExternal() || GV->hasWeakLinkage() ||
148 GV->hasLinkOnceLinkage()) {
149 // Dynamically-resolved functions need a stub for the function.
150 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
151 FnStubs.insert(Name);
152 O << "L" << Name << "$stub";
153 } else {
154 GVStubs.insert(Name);
155 O << "L" << Name << "$non_lazy_ptr";
Evan Cheng5588de92006-02-18 00:15:05 +0000156 }
157 } else {
158 O << Mang->getValueName(GV);
159 }
Evan Cheng1f342c22006-02-23 02:43:52 +0000160 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
161 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng5588de92006-02-18 00:15:05 +0000162 } else
163 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb9740462005-07-01 22:44:09 +0000164 int Offset = MO.getOffset();
165 if (Offset > 0)
166 O << " + " << Offset;
167 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000168 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000169 return;
170 }
Evan Cheng5588de92006-02-18 00:15:05 +0000171 case MachineOperand::MO_ExternalSymbol: {
172 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng73136df2006-02-22 20:19:42 +0000173 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
174 std::string Name(GlobalPrefix);
175 Name += MO.getSymbolName();
Evan Cheng5588de92006-02-18 00:15:05 +0000176 FnStubs.insert(Name);
177 O << "L" << Name << "$stub";
178 return;
179 }
Evan Cheng73136df2006-02-22 20:19:42 +0000180 if (!isCallOp) O << "OFFSET ";
Chris Lattnerb9740462005-07-01 22:44:09 +0000181 O << GlobalPrefix << MO.getSymbolName();
182 return;
Evan Cheng5588de92006-02-18 00:15:05 +0000183 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000184 default:
185 O << "<unknown operand type>"; return;
186 }
187}
188
189void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
190 assert(isMem(MI, Op) && "Invalid memory reference!");
191
192 const MachineOperand &BaseReg = MI->getOperand(Op);
193 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
194 const MachineOperand &IndexReg = MI->getOperand(Op+2);
195 const MachineOperand &DispSpec = MI->getOperand(Op+3);
196
197 if (BaseReg.isFrameIndex()) {
198 O << "[frame slot #" << BaseReg.getFrameIndex();
199 if (DispSpec.getImmedValue())
200 O << " + " << DispSpec.getImmedValue();
201 O << "]";
202 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000203 }
204
205 O << "[";
206 bool NeedPlus = false;
207 if (BaseReg.getReg()) {
Evan Cheng5a766802006-02-07 08:38:37 +0000208 printOp(BaseReg, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000209 NeedPlus = true;
210 }
211
212 if (IndexReg.getReg()) {
213 if (NeedPlus) O << " + ";
214 if (ScaleVal != 1)
215 O << ScaleVal << "*";
216 printOp(IndexReg);
217 NeedPlus = true;
218 }
219
Evan Cheng75b87832006-02-26 08:28:12 +0000220 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Chris Lattnerb9740462005-07-01 22:44:09 +0000221 if (NeedPlus)
222 O << " + ";
Evan Cheng5a766802006-02-07 08:38:37 +0000223 printOp(DispSpec, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000224 } else {
225 int DispVal = DispSpec.getImmedValue();
226 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
227 if (NeedPlus)
228 if (DispVal > 0)
229 O << " + ";
230 else {
231 O << " - ";
232 DispVal = -DispVal;
233 }
234 O << DispVal;
235 }
236 }
237 O << "]";
238}
239
Evan Cheng5588de92006-02-18 00:15:05 +0000240void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
243}
Chris Lattnerb9740462005-07-01 22:44:09 +0000244
245/// printMachineInstruction -- Print out a single X86 LLVM instruction
246/// MI in Intel syntax to the current output stream.
247///
248void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
249 ++EmittedInsts;
250
251 // Call the autogenerated instruction printer routines.
252 printInstruction(MI);
253}
254
255bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9f6ce0e2005-07-03 17:34:39 +0000256 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb9740462005-07-01 22:44:09 +0000257 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
258 //
259 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
260 // instruction as a reference to the register named sp, and if you try to
261 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
262 // before being looked up in the symbol table. This creates spurious
263 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
264 // mode, and decorate all register names with percent signs.
265 O << "\t.intel_syntax\n";
266 return false;
267}
268
269// Include the auto-generated portion of the assembly writer.
270#include "X86GenAsmWriter1.inc"