blob: 291073e04cda137981dae49139fe2b39ab51c74b [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the "Instituto Nokia de Tecnologia" and
6// is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains a printer that converts from our internal representation
12// of machine-dependent LLVM code to GAS-format ARM assembly language.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ARM.h"
17#include "ARMInstrInfo.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000021#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineInstr.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000026#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/StringExtras.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/MathExtras.h"
33#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000034using namespace llvm;
35
36namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000037 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000038
Rafael Espindolae931a372006-11-02 15:00:02 +000039 static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
40 switch (CC) {
41 default: assert(0 && "Unknown condition code");
42 case ARMCC::EQ: return "eq";
43 case ARMCC::NE: return "ne";
44 case ARMCC::CS: return "cs";
45 case ARMCC::CC: return "cc";
46 case ARMCC::MI: return "mi";
47 case ARMCC::PL: return "pl";
48 case ARMCC::VS: return "vs";
49 case ARMCC::VC: return "vc";
50 case ARMCC::HI: return "hi";
51 case ARMCC::LS: return "ls";
52 case ARMCC::GE: return "ge";
53 case ARMCC::LT: return "lt";
54 case ARMCC::GT: return "gt";
55 case ARMCC::LE: return "le";
56 case ARMCC::AL: return "al";
57 }
58 }
59
Jim Laskey563321a2006-09-06 18:34:40 +000060 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
Jim Laskeya0f3d172006-09-07 22:06:40 +000061 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000062 : AsmPrinter(O, TM, T) {
63 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000064
65 /// We name each basic block in a Function with a unique number, so
66 /// that we can consistently refer to them later. This is cleared
67 /// at the beginning of each call to runOnMachineFunction().
68 ///
69 typedef std::map<const Value *, unsigned> ValueMapTy;
70 ValueMapTy NumberForBB;
71
72 virtual const char *getPassName() const {
73 return "ARM Assembly Printer";
74 }
75
Rafael Espindola7cca7c52006-09-11 17:25:40 +000076 void printAddrMode1(const MachineInstr *MI, int opNum);
Rafael Espindola6e8c6492006-11-08 17:07:32 +000077 void printAddrMode2(const MachineInstr *MI, int opNum);
Rafael Espindola32bd5f42006-10-17 18:04:53 +000078 void printAddrMode5(const MachineInstr *MI, int opNum);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000079 void printOperand(const MachineInstr *MI, int opNum);
80 void printMemOperand(const MachineInstr *MI, int opNum,
81 const char *Modifier = 0);
82 void printCCOperand(const MachineInstr *MI, int opNum);
83
84 bool printInstruction(const MachineInstr *MI); // autogenerated.
85 bool runOnMachineFunction(MachineFunction &F);
86 bool doInitialization(Module &M);
87 bool doFinalization(Module &M);
88 };
89} // end of anonymous namespace
90
91#include "ARMGenAsmWriter.inc"
92
93/// createARMCodePrinterPass - Returns a pass that prints the ARM
94/// assembly code for a MachineFunction to the given output stream,
95/// using the given target machine description. This should work
96/// regardless of whether the function is in SSA form.
97///
98FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
99 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000100 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000101}
102
103/// runOnMachineFunction - This uses the printMachineInstruction()
104/// method to print assembly for each instruction.
105///
106bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000107 SetupMachineFunction(MF);
108 O << "\n\n";
109
110 // Print out constants referenced by the function
111 EmitConstantPool(MF.getConstantPool());
112
113 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000114 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000115
116 // Print out labels for the function.
117 const Function *F = MF.getFunction();
Chris Lattner6f6f6992006-10-05 02:49:23 +0000118 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
119
Rafael Espindola4b442b52006-05-23 02:48:20 +0000120 switch (F->getLinkage()) {
121 default: assert(0 && "Unknown linkage type!");
122 case Function::InternalLinkage:
Rafael Espindola4b442b52006-05-23 02:48:20 +0000123 break;
124 case Function::ExternalLinkage:
Rafael Espindola4b442b52006-05-23 02:48:20 +0000125 O << "\t.globl\t" << CurrentFnName << "\n";
126 break;
127 case Function::WeakLinkage:
128 case Function::LinkOnceLinkage:
Rafael Espindola392b1b22006-12-06 13:35:10 +0000129 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000130 break;
131 }
Rafael Espindolaa1334cd2006-05-26 10:56:17 +0000132 EmitAlignment(2, F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000133 O << CurrentFnName << ":\n";
134
135 // Print out code for the function.
136 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
137 I != E; ++I) {
138 // Print a label for the basic block.
139 if (I != MF.begin()) {
140 printBasicBlockLabel(I, true);
141 O << '\n';
142 }
143 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
144 II != E; ++II) {
145 // Print the assembly for the instruction.
146 O << "\t";
147 printInstruction(II);
148 }
149 }
150
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000151 return false;
152}
153
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000154void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000155 const MachineOperand &Arg = MI->getOperand(opNum);
156 const MachineOperand &Shift = MI->getOperand(opNum + 1);
157 const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000158
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000159 if(Arg.isImmediate()) {
160 assert(Shift.getImmedValue() == 0);
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000161 printOperand(MI, opNum);
162 } else {
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000163 assert(Arg.isRegister());
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000164 printOperand(MI, opNum);
Rafael Espindola3ad5e5c2006-09-13 12:09:43 +0000165 if(Shift.isRegister() || Shift.getImmedValue() != 0) {
166 const char *s = NULL;
167 switch(ShiftType.getImmedValue()) {
168 case ARMShift::LSL:
169 s = ", lsl ";
170 break;
171 case ARMShift::LSR:
172 s = ", lsr ";
173 break;
174 case ARMShift::ASR:
175 s = ", asr ";
176 break;
177 case ARMShift::ROR:
178 s = ", ror ";
179 break;
180 case ARMShift::RRX:
181 s = ", rrx ";
182 break;
183 }
184 O << s;
185 printOperand(MI, opNum + 1);
186 }
Rafael Espindola7cca7c52006-09-11 17:25:40 +0000187 }
188}
189
Rafael Espindola6e8c6492006-11-08 17:07:32 +0000190void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
191 const MachineOperand &Arg = MI->getOperand(opNum);
192 const MachineOperand &Offset = MI->getOperand(opNum + 1);
193 assert(Offset.isImmediate());
194
195 if (Arg.isConstantPoolIndex()) {
196 assert(Offset.getImmedValue() == 0);
197 printOperand(MI, opNum);
198 } else {
199 assert(Arg.isRegister());
200 O << '[';
201 printOperand(MI, opNum);
202 O << ", ";
203 printOperand(MI, opNum + 1);
204 O << ']';
205 }
206}
207
Rafael Espindola32bd5f42006-10-17 18:04:53 +0000208void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
209 const MachineOperand &Arg = MI->getOperand(opNum);
210 const MachineOperand &Offset = MI->getOperand(opNum + 1);
211 assert(Offset.isImmediate());
212
213 if (Arg.isConstantPoolIndex()) {
214 assert(Offset.getImmedValue() == 0);
215 printOperand(MI, opNum);
216 } else {
217 assert(Arg.isRegister());
218 O << '[';
219 printOperand(MI, opNum);
220 O << ", ";
221 printOperand(MI, opNum + 1);
222 O << ']';
223 }
224}
225
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000226void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000227 const MachineOperand &MO = MI->getOperand (opNum);
228 const MRegisterInfo &RI = *TM.getRegisterInfo();
229 switch (MO.getType()) {
230 case MachineOperand::MO_Register:
231 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
232 O << LowercaseString (RI.get(MO.getReg()).Name);
233 else
234 assert(0 && "not implemented");
235 break;
236 case MachineOperand::MO_Immediate:
237 O << "#" << (int)MO.getImmedValue();
238 break;
239 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000240 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000241 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000242 case MachineOperand::MO_GlobalAddress: {
243 GlobalValue *GV = MO.getGlobal();
244 std::string Name = Mang->getValueName(GV);
245 O << Name;
Rafael Espindola392b1b22006-12-06 13:35:10 +0000246 if (GV->hasExternalWeakLinkage()) {
Rafael Espindola15404d02006-12-18 03:37:18 +0000247 ExtWeakSymbols.insert(GV);
Rafael Espindola392b1b22006-12-06 13:35:10 +0000248 }
Rafael Espindola84b19be2006-07-16 01:02:57 +0000249 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000250 break;
251 case MachineOperand::MO_ExternalSymbol:
Rafael Espindola0505be02006-10-16 21:10:32 +0000252 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000253 break;
254 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000255 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000256 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000257 break;
258 default:
259 O << "<unknown operand type>"; abort (); break;
260 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000261}
262
263void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
264 const char *Modifier) {
265 assert(0 && "not implemented");
266}
267
268void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000269 int CC = (int)MI->getOperand(opNum).getImmedValue();
270 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000271}
272
273bool ARMAsmPrinter::doInitialization(Module &M) {
Rafael Espindolaff59d222006-09-11 12:49:38 +0000274 AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000275 return false; // success
276}
277
278bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000279 const TargetData *TD = TM.getTargetData();
280
281 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
282 I != E; ++I) {
283 if (!I->hasInitializer()) // External global require no code
284 continue;
285
286 if (EmitSpecialLLVMGlobal(I))
287 continue;
288
289 O << "\n\n";
290 std::string name = Mang->getValueName(I);
291 Constant *C = I->getInitializer();
292 unsigned Size = TD->getTypeSize(C->getType());
Rafael Espindola1c411de2006-12-07 22:38:06 +0000293 unsigned Align = Log2_32(TD->getTypeAlignment(C->getType()));
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000294
Rafael Espindolab97809c2006-10-19 13:30:40 +0000295 if (C->isNullValue() &&
Rafael Espindola7367d052006-12-07 18:33:58 +0000296 !I->hasSection() &&
Rafael Espindolab97809c2006-10-19 13:30:40 +0000297 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
298 I->hasWeakLinkage())) {
299 SwitchToDataSection(".data", I);
300 if (I->hasInternalLinkage())
301 O << "\t.local " << name << "\n";
302
Rafael Espindola1c411de2006-12-07 22:38:06 +0000303 O << "\t.comm " << name << "," << Size
Rafael Espindola796cbd82006-12-10 02:53:14 +0000304 << "," << (unsigned) (1 << Align);
Rafael Espindolab97809c2006-10-19 13:30:40 +0000305 O << "\n";
306 } else {
307 switch (I->getLinkage()) {
308 default:
309 assert(0 && "Unknown linkage type!");
310 break;
311 case GlobalValue::ExternalLinkage:
312 O << "\t.globl " << name << "\n";
313 break;
314 case GlobalValue::InternalLinkage:
315 break;
316 }
317
Rafael Espindola13666262006-12-08 21:24:58 +0000318 if (I->hasSection() &&
319 (I->getSection() == ".ctors" ||
320 I->getSection() == ".dtors")) {
321 std::string SectionName = ".section " + I->getSection();
322
Rafael Espindola0ec729e2006-12-08 22:06:02 +0000323 SectionName += ",\"aw\",%progbits";
Rafael Espindola13666262006-12-08 21:24:58 +0000324
325 SwitchToDataSection(SectionName.c_str());
326 } else {
327 SwitchToDataSection(TAI->getDataSection(), I);
328 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000329
330 EmitAlignment(Align, I);
331 O << "\t.type " << name << ", %object\n";
332 O << "\t.size " << name << ", " << Size << "\n";
333 O << name << ":\n";
334 EmitGlobalConstant(C);
Rafael Espindola6d581e82006-07-31 20:38:13 +0000335 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000336 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000337
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000338 AsmPrinter::doFinalization(M);
339 return false; // success
340}