blob: ca8630c3cb9284138dde7c2fb35e90d4cde4844f [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86ATTAsmPrinter.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 AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86ATTAsmPrinter.h"
17#include "X86.h"
18#include "X86TargetMachine.h"
19#include "llvm/Module.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 X86ATTAsmPrinter::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";
Chris Lattner3249bfc2005-07-12 02:35:36 +000036 emitAlignment(4); // FIXME: This should be parameterized somewhere.
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037 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.
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 }
Nate Begeman73213f62005-07-12 01:37:28 +000056 if (!forDarwin)
57 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +000058
59 // We didn't modify anything.
60 return false;
61}
62
63void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
64 const MRegisterInfo &RI = *TM.getRegisterInfo();
65 switch (MO.getType()) {
66 case MachineOperand::MO_VirtualRegister:
67 case MachineOperand::MO_MachineRegister:
68 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
69 "Virtual registers should not make it this far!");
70 O << '%';
71 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
72 O << (char)tolower(*Name);
73 return;
74
75 case MachineOperand::MO_SignExtendedImmed:
76 case MachineOperand::MO_UnextendedImmed:
77 O << '$' << (int)MO.getImmedValue();
78 return;
79 case MachineOperand::MO_MachineBasicBlock: {
80 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
81 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
82 << "_" << MBBOp->getNumber () << "\t# "
83 << MBBOp->getBasicBlock ()->getName ();
84 return;
85 }
86 case MachineOperand::MO_PCRelativeDisp:
87 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
88 abort ();
89 return;
90 case MachineOperand::MO_GlobalAddress: {
Nate Begeman72b286b2005-07-08 00:23:26 +000091 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
92 if (forDarwin) {
93 if (!isCallOp) O << '$';
94 GlobalValue *GV = MO.getGlobal();
95 std::string Name = Mang->getValueName(GV);
96
97 // Dynamically-resolved functions need a stub for the function. Be
98 // wary however not to output $stub for external functions whose addresses
99 // are taken. Those should be emitted as $non_lazy_ptr below.
100 Function *F = dyn_cast<Function>(GV);
101 if (F && isCallOp && F->isExternal()) {
102 FnStubs.insert(Name);
103 O << "L" << Name << "$stub";
Nate Begemand3a490a2005-07-12 18:34:58 +0000104 } else if (GV->hasLinkOnceLinkage()) {
105 // Link-once, External, or Weakly-linked global variables need
106 // non-lazily-resolved stubs
Nate Begeman72b286b2005-07-08 00:23:26 +0000107 LinkOnceStubs.insert(Name);
108 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000109 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
Nate Begeman72b286b2005-07-08 00:23:26 +0000110 GVStubs.insert(Name);
111 O << "L" << Name << "$non_lazy_ptr";
Nate Begemand3a490a2005-07-12 18:34:58 +0000112 } else {
113 O << Mang->getValueName(GV);
Nate Begeman72b286b2005-07-08 00:23:26 +0000114 }
Nate Begemand3a490a2005-07-12 18:34:58 +0000115 int Offset = MO.getOffset();
116 if (Offset > 0)
117 O << "+" << Offset;
118 else if (Offset < 0)
119 O << Offset;
Nate Begeman72b286b2005-07-08 00:23:26 +0000120 return;
121 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122 if (!isCallOp) O << '$';
123 O << Mang->getValueName(MO.getGlobal());
124 int Offset = MO.getOffset();
125 if (Offset > 0)
126 O << "+" << Offset;
127 else if (Offset < 0)
128 O << Offset;
129 return;
130 }
131 case MachineOperand::MO_ExternalSymbol:
Nate Begeman72b286b2005-07-08 00:23:26 +0000132 if (isCallOp && forDarwin) {
133 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
134 FnStubs.insert(Name);
135 O << "L" << Name << "$stub";
136 return;
137 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000138 if (!isCallOp) O << '$';
139 O << GlobalPrefix << MO.getSymbolName();
140 return;
141 default:
142 O << "<unknown operand type>"; return;
143 }
144}
145
146void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
147 assert(isMem(MI, Op) && "Invalid memory reference!");
148
149 const MachineOperand &BaseReg = MI->getOperand(Op);
150 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
151 const MachineOperand &IndexReg = MI->getOperand(Op+2);
152 const MachineOperand &DispSpec = MI->getOperand(Op+3);
153
154 if (BaseReg.isFrameIndex()) {
155 O << "[frame slot #" << BaseReg.getFrameIndex();
156 if (DispSpec.getImmedValue())
157 O << " + " << DispSpec.getImmedValue();
158 O << "]";
159 return;
160 } else if (BaseReg.isConstantPoolIndex()) {
161 O << ".CPI" << CurrentFnName << "_"
162 << BaseReg.getConstantPoolIndex();
163 if (DispSpec.getImmedValue())
164 O << "+" << DispSpec.getImmedValue();
165 if (IndexReg.getReg()) {
166 O << "(,";
167 printOp(IndexReg);
168 if (ScaleVal != 1)
169 O << "," << ScaleVal;
170 O << ")";
171 }
172 return;
173 }
174
175 if (DispSpec.isGlobalAddress()) {
176 printOp(DispSpec, true);
177 } else {
178 int DispVal = DispSpec.getImmedValue();
179 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
180 O << DispVal;
181 }
182
183 if (IndexReg.getReg() || BaseReg.getReg()) {
184 O << "(";
185 if (BaseReg.getReg())
186 printOp(BaseReg);
187
188 if (IndexReg.getReg()) {
189 O << ",";
190 printOp(IndexReg);
191 if (ScaleVal != 1)
192 O << "," << ScaleVal;
193 }
194
195 O << ")";
196 }
197}
198
199/// printMachineInstruction -- Print out a single X86 LLVM instruction
200/// MI in Intel syntax to the current output stream.
201///
202void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
203 ++EmittedInsts;
204 // Call the autogenerated instruction printer routines.
205 printInstruction(MI);
206}
207
208// Include the auto-generated portion of the assembly writer.
209#include "X86GenAsmWriter.inc"
210