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