blob: 2b91870e42ac41fc81177fc400b46948b7d67cc1 [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"
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) {
Chris Lattner99946fb2005-11-21 07:51:23 +000028 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000029 O << "\n\n";
30
31 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000032 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000033
34 // Print out labels for the function.
Chris Lattner050bf2f2005-11-21 07:16:34 +000035 SwitchSection("\t.text\n", MF.getFunction());
Chris Lattner99946fb2005-11-21 07:51:23 +000036 EmitAlignment(4);
Chris Lattnerb9740462005-07-01 22:44:09 +000037 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattnerac6cb462005-11-21 23:06:54 +000038 if (HasDotTypeDotSizeDirective)
Chris Lattnerb9740462005-07-01 22:44:09 +000039 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())
Chris Lattnerd3656272005-11-21 07:43:59 +000047 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
48 << ":\t"
Chris Lattnerb9740462005-07-01 22:44:09 +000049 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
50 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
51 II != E; ++II) {
52 // Print the assembly for the instruction.
53 O << "\t";
54 printMachineInstruction(II);
55 }
56 }
57
58 // We didn't modify anything.
59 return false;
60}
61
Nate Begeman6f8c1ac2005-11-30 18:54:35 +000062void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-07-14 22:52:25 +000063 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 Lattnerb9740462005-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();
Chris Lattnerd3656272005-11-21 07:43:59 +0000101 O << PrivateGlobalPrefix << "BB"
102 << Mang->getValueName(MBBOp->getParent()->getFunction())
Chris Lattnerb9740462005-07-01 22:44:09 +0000103 << "_" << MBBOp->getNumber () << "\t# "
104 << MBBOp->getBasicBlock ()->getName ();
105 return;
106 }
107 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerde02d772006-01-22 23:41:00 +0000108 assert(0 && "Shouldn't use addPCDisp() when building X86 MachineInstrs");
Chris Lattnerb9740462005-07-01 22:44:09 +0000109 abort ();
110 return;
111 case MachineOperand::MO_GlobalAddress: {
112 if (!elideOffsetKeyword)
113 O << "OFFSET ";
114 O << Mang->getValueName(MO.getGlobal());
115 int Offset = MO.getOffset();
116 if (Offset > 0)
117 O << " + " << Offset;
118 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000119 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000120 return;
121 }
122 case MachineOperand::MO_ExternalSymbol:
123 O << GlobalPrefix << MO.getSymbolName();
124 return;
125 default:
126 O << "<unknown operand type>"; return;
127 }
128}
129
130void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
131 assert(isMem(MI, Op) && "Invalid memory reference!");
132
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);
137
138 if (BaseReg.isFrameIndex()) {
139 O << "[frame slot #" << BaseReg.getFrameIndex();
140 if (DispSpec.getImmedValue())
141 O << " + " << DispSpec.getImmedValue();
142 O << "]";
143 return;
144 } else if (BaseReg.isConstantPoolIndex()) {
Chris Lattner8a5f3c12005-11-21 08:32:23 +0000145 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb9740462005-07-01 22:44:09 +0000146 << BaseReg.getConstantPoolIndex();
147
148 if (IndexReg.getReg()) {
149 O << " + ";
150 if (ScaleVal != 1)
151 O << ScaleVal << "*";
152 printOp(IndexReg);
153 }
154
155 if (DispSpec.getImmedValue())
156 O << " + " << DispSpec.getImmedValue();
157 O << "]";
158 return;
159 }
160
161 O << "[";
162 bool NeedPlus = false;
163 if (BaseReg.getReg()) {
164 printOp(BaseReg, true);
165 NeedPlus = true;
166 }
167
168 if (IndexReg.getReg()) {
169 if (NeedPlus) O << " + ";
170 if (ScaleVal != 1)
171 O << ScaleVal << "*";
172 printOp(IndexReg);
173 NeedPlus = true;
174 }
175
176 if (DispSpec.isGlobalAddress()) {
177 if (NeedPlus)
178 O << " + ";
179 printOp(DispSpec, true);
180 } else {
181 int DispVal = DispSpec.getImmedValue();
182 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
183 if (NeedPlus)
184 if (DispVal > 0)
185 O << " + ";
186 else {
187 O << " - ";
188 DispVal = -DispVal;
189 }
190 O << DispVal;
191 }
192 }
193 O << "]";
194}
195
196
197/// printMachineInstruction -- Print out a single X86 LLVM instruction
198/// MI in Intel syntax to the current output stream.
199///
200void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
201 ++EmittedInsts;
202
203 // Call the autogenerated instruction printer routines.
204 printInstruction(MI);
205}
206
207bool X86IntelAsmPrinter::doInitialization(Module &M) {
Chris Lattner9f6ce0e2005-07-03 17:34:39 +0000208 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerb9740462005-07-01 22:44:09 +0000209 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
210 //
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";
218 return false;
219}
220
221// Include the auto-generated portion of the assembly writer.
222#include "X86GenAsmWriter1.inc"