blob: d998fdff010ce1b26ce1195f97821ecd40bd051d [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 Espindolaa4e64352006-07-11 11:36:48 +000062 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
Rafael Espindola1ed3af12006-08-01 18:53:10 +000063 const MachineOperand &MO1 = MI->getOperand(OpNo);
64 const MachineOperand &MO2 = MI->getOperand(OpNo + 1);
65 assert(MO1.isImmediate());
66
67 if (MO2.isConstantPoolIndex()) {
68 printOperand(MI, OpNo + 1);
69 } else if (MO2.isRegister()) {
70 O << '[';
71 printOperand(MI, OpNo + 1);
72 O << ", ";
73 printOperand(MI, OpNo);
74 O << ']';
75 } else {
76 assert(0 && "Invalid Operand Type");
77 }
Rafael Espindolaa4e64352006-07-11 11:36:48 +000078 }
79
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000080 void printOperand(const MachineInstr *MI, int opNum);
81 void printMemOperand(const MachineInstr *MI, int opNum,
82 const char *Modifier = 0);
83 void printCCOperand(const MachineInstr *MI, int opNum);
84
85 bool printInstruction(const MachineInstr *MI); // autogenerated.
86 bool runOnMachineFunction(MachineFunction &F);
87 bool doInitialization(Module &M);
88 bool doFinalization(Module &M);
89 };
90} // end of anonymous namespace
91
92#include "ARMGenAsmWriter.inc"
93
94/// createARMCodePrinterPass - Returns a pass that prints the ARM
95/// assembly code for a MachineFunction to the given output stream,
96/// using the given target machine description. This should work
97/// regardless of whether the function is in SSA form.
98///
99FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
100 TargetMachine &tm) {
101 return new ARMAsmPrinter(o, tm);
102}
103
104/// runOnMachineFunction - This uses the printMachineInstruction()
105/// method to print assembly for each instruction.
106///
107bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000108 SetupMachineFunction(MF);
109 O << "\n\n";
110
111 // Print out constants referenced by the function
112 EmitConstantPool(MF.getConstantPool());
113
114 // Print out jump tables referenced by the function
115 EmitJumpTableInfo(MF.getJumpTableInfo());
116
117 // Print out labels for the function.
118 const Function *F = MF.getFunction();
119 switch (F->getLinkage()) {
120 default: assert(0 && "Unknown linkage type!");
121 case Function::InternalLinkage:
122 SwitchToTextSection("\t.text", F);
123 break;
124 case Function::ExternalLinkage:
125 SwitchToTextSection("\t.text", F);
126 O << "\t.globl\t" << CurrentFnName << "\n";
127 break;
128 case Function::WeakLinkage:
129 case Function::LinkOnceLinkage:
130 assert(0 && "Not implemented");
131 break;
132 }
Rafael Espindolaa1334cd2006-05-26 10:56:17 +0000133 EmitAlignment(2, F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000134 O << CurrentFnName << ":\n";
135
136 // Print out code for the function.
137 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
138 I != E; ++I) {
139 // Print a label for the basic block.
140 if (I != MF.begin()) {
141 printBasicBlockLabel(I, true);
142 O << '\n';
143 }
144 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
145 II != E; ++II) {
146 // Print the assembly for the instruction.
147 O << "\t";
148 printInstruction(II);
149 }
150 }
151
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000152 return false;
153}
154
155void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000156 const MachineOperand &MO = MI->getOperand (opNum);
157 const MRegisterInfo &RI = *TM.getRegisterInfo();
158 switch (MO.getType()) {
159 case MachineOperand::MO_Register:
160 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
161 O << LowercaseString (RI.get(MO.getReg()).Name);
162 else
163 assert(0 && "not implemented");
164 break;
165 case MachineOperand::MO_Immediate:
166 O << "#" << (int)MO.getImmedValue();
167 break;
168 case MachineOperand::MO_MachineBasicBlock:
169 assert(0 && "not implemented");
170 abort();
171 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000172 case MachineOperand::MO_GlobalAddress: {
173 GlobalValue *GV = MO.getGlobal();
174 std::string Name = Mang->getValueName(GV);
175 O << Name;
176 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000177 break;
178 case MachineOperand::MO_ExternalSymbol:
179 assert(0 && "not implemented");
180 abort();
181 break;
182 case MachineOperand::MO_ConstantPoolIndex:
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000183 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber()
184 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000185 break;
186 default:
187 O << "<unknown operand type>"; abort (); break;
188 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000189}
190
191void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
192 const char *Modifier) {
193 assert(0 && "not implemented");
194}
195
196void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
197 assert(0 && "not implemented");
198}
199
200bool ARMAsmPrinter::doInitialization(Module &M) {
201 Mang = new Mangler(M);
202 return false; // success
203}
204
205bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000206 const TargetData *TD = TM.getTargetData();
207
208 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
209 I != E; ++I) {
210 if (!I->hasInitializer()) // External global require no code
211 continue;
212
213 if (EmitSpecialLLVMGlobal(I))
214 continue;
215
216 O << "\n\n";
217 std::string name = Mang->getValueName(I);
218 Constant *C = I->getInitializer();
219 unsigned Size = TD->getTypeSize(C->getType());
220 unsigned Align = TD->getTypeAlignment(C->getType());
221
Rafael Espindola6d581e82006-07-31 20:38:13 +0000222 switch (I->getLinkage()) {
223 default:
224 assert(0 && "Unknown linkage type!");
225 break;
226 case GlobalValue::ExternalLinkage:
227 O << "\t.globl " << name << "\n";
228 break;
229 case GlobalValue::InternalLinkage:
230 break;
231 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000232
233 assert (!C->isNullValue());
234 SwitchToDataSection(".data", I);
235
236 EmitAlignment(Align, I);
237 O << "\t.type " << name << ", %object\n";
238 O << "\t.size " << name << ", " << Size << "\n";
239 O << name << ":\n";
240 EmitGlobalConstant(C);
241 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000242 AsmPrinter::doFinalization(M);
243 return false; // success
244}