blob: adf444d29a671f4626b29feedd0ab88aa262baf3 [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"
21#include "llvm/Assembly/Writer.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineInstr.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>
34#include <iostream>
35using namespace llvm;
36
37namespace {
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
40 struct ARMAsmPrinter : public AsmPrinter {
41 ARMAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
42 Data16bitsDirective = "\t.half\t";
43 Data32bitsDirective = "\t.word\t";
44 Data64bitsDirective = 0;
45 ZeroDirective = "\t.skip\t";
Rafael Espindola1ed3af12006-08-01 18:53:10 +000046 CommentString = "#";
47 ConstantPoolSection = "\t.text\n";
Rafael Espindolaa1334cd2006-05-26 10:56:17 +000048 AlignmentIsInBytes = false;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000049 }
50
51 /// We name each basic block in a Function with a unique number, so
52 /// that we can consistently refer to them later. This is cleared
53 /// at the beginning of each call to runOnMachineFunction().
54 ///
55 typedef std::map<const Value *, unsigned> ValueMapTy;
56 ValueMapTy NumberForBB;
57
58 virtual const char *getPassName() const {
59 return "ARM Assembly Printer";
60 }
61
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000062 void printMemRegImm(const MachineInstr *MI, int opNum,
63 const char *Modifier = NULL) {
64 const MachineOperand &MO1 = MI->getOperand(opNum);
65 const MachineOperand &MO2 = MI->getOperand(opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000066 assert(MO1.isImmediate());
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000067 bool arith = false;
68 if (Modifier != NULL) {
69 assert(strcmp(Modifier, "arith") == 0);
70 arith = true;
71 }
Rafael Espindola1ed3af12006-08-01 18:53:10 +000072
73 if (MO2.isConstantPoolIndex()) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000074 printOperand(MI, opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000075 } else if (MO2.isRegister()) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000076 if(!arith)
77 O << '[';
78 printOperand(MI, opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000079 O << ", ";
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000080 printOperand(MI, opNum);
81 if(!arith)
82 O << ']';
Rafael Espindola1ed3af12006-08-01 18:53:10 +000083 } else {
84 assert(0 && "Invalid Operand Type");
85 }
Rafael Espindolaa4e64352006-07-11 11:36:48 +000086 }
87
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000088 void printOperand(const MachineInstr *MI, int opNum);
89 void printMemOperand(const MachineInstr *MI, int opNum,
90 const char *Modifier = 0);
91 void printCCOperand(const MachineInstr *MI, int opNum);
92
93 bool printInstruction(const MachineInstr *MI); // autogenerated.
94 bool runOnMachineFunction(MachineFunction &F);
95 bool doInitialization(Module &M);
96 bool doFinalization(Module &M);
97 };
98} // end of anonymous namespace
99
100#include "ARMGenAsmWriter.inc"
101
102/// createARMCodePrinterPass - Returns a pass that prints the ARM
103/// assembly code for a MachineFunction to the given output stream,
104/// using the given target machine description. This should work
105/// regardless of whether the function is in SSA form.
106///
107FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
108 TargetMachine &tm) {
109 return new ARMAsmPrinter(o, tm);
110}
111
112/// runOnMachineFunction - This uses the printMachineInstruction()
113/// method to print assembly for each instruction.
114///
115bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000116 SetupMachineFunction(MF);
117 O << "\n\n";
118
119 // Print out constants referenced by the function
120 EmitConstantPool(MF.getConstantPool());
121
122 // Print out jump tables referenced by the function
123 EmitJumpTableInfo(MF.getJumpTableInfo());
124
125 // Print out labels for the function.
126 const Function *F = MF.getFunction();
127 switch (F->getLinkage()) {
128 default: assert(0 && "Unknown linkage type!");
129 case Function::InternalLinkage:
130 SwitchToTextSection("\t.text", F);
131 break;
132 case Function::ExternalLinkage:
133 SwitchToTextSection("\t.text", F);
134 O << "\t.globl\t" << CurrentFnName << "\n";
135 break;
136 case Function::WeakLinkage:
137 case Function::LinkOnceLinkage:
138 assert(0 && "Not implemented");
139 break;
140 }
Rafael Espindolaa1334cd2006-05-26 10:56:17 +0000141 EmitAlignment(2, F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000142 O << CurrentFnName << ":\n";
143
144 // Print out code for the function.
145 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
146 I != E; ++I) {
147 // Print a label for the basic block.
148 if (I != MF.begin()) {
149 printBasicBlockLabel(I, true);
150 O << '\n';
151 }
152 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
153 II != E; ++II) {
154 // Print the assembly for the instruction.
155 O << "\t";
156 printInstruction(II);
157 }
158 }
159
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000160 return false;
161}
162
163void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000164 const MachineOperand &MO = MI->getOperand (opNum);
165 const MRegisterInfo &RI = *TM.getRegisterInfo();
166 switch (MO.getType()) {
167 case MachineOperand::MO_Register:
168 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
169 O << LowercaseString (RI.get(MO.getReg()).Name);
170 else
171 assert(0 && "not implemented");
172 break;
173 case MachineOperand::MO_Immediate:
174 O << "#" << (int)MO.getImmedValue();
175 break;
176 case MachineOperand::MO_MachineBasicBlock:
177 assert(0 && "not implemented");
178 abort();
179 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000180 case MachineOperand::MO_GlobalAddress: {
181 GlobalValue *GV = MO.getGlobal();
182 std::string Name = Mang->getValueName(GV);
183 O << Name;
184 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000185 break;
186 case MachineOperand::MO_ExternalSymbol:
187 assert(0 && "not implemented");
188 abort();
189 break;
190 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000191 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
192 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000193 break;
194 default:
195 O << "<unknown operand type>"; abort (); break;
196 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000197}
198
199void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
200 const char *Modifier) {
201 assert(0 && "not implemented");
202}
203
204void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
205 assert(0 && "not implemented");
206}
207
208bool ARMAsmPrinter::doInitialization(Module &M) {
209 Mang = new Mangler(M);
210 return false; // success
211}
212
213bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000214 const TargetData *TD = TM.getTargetData();
215
216 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
217 I != E; ++I) {
218 if (!I->hasInitializer()) // External global require no code
219 continue;
220
221 if (EmitSpecialLLVMGlobal(I))
222 continue;
223
224 O << "\n\n";
225 std::string name = Mang->getValueName(I);
226 Constant *C = I->getInitializer();
227 unsigned Size = TD->getTypeSize(C->getType());
228 unsigned Align = TD->getTypeAlignment(C->getType());
229
Rafael Espindola6d581e82006-07-31 20:38:13 +0000230 switch (I->getLinkage()) {
231 default:
232 assert(0 && "Unknown linkage type!");
233 break;
234 case GlobalValue::ExternalLinkage:
235 O << "\t.globl " << name << "\n";
236 break;
237 case GlobalValue::InternalLinkage:
238 break;
239 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000240
241 assert (!C->isNullValue());
242 SwitchToDataSection(".data", I);
243
244 EmitAlignment(Align, I);
245 O << "\t.type " << name << ", %object\n";
246 O << "\t.size " << name << ", " << Size << "\n";
247 O << name << ":\n";
248 EmitGlobalConstant(C);
249 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000250 AsmPrinter::doFinalization(M);
251 return false; // success
252}