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