blob: 98c37904cfd7d92634e327928c3860259269c3c2 [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"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000027#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Support/Mangler.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/ADT/StringExtras.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/MathExtras.h"
34#include <cctype>
35#include <iostream>
36using namespace llvm;
37
38namespace {
39 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40
Jim Laskey563321a2006-09-06 18:34:40 +000041 struct VISIBILITY_HIDDEN ARMTargetAsmInfo : public TargetAsmInfo {
42 ARMTargetAsmInfo() {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000043 Data16bitsDirective = "\t.half\t";
44 Data32bitsDirective = "\t.word\t";
45 Data64bitsDirective = 0;
46 ZeroDirective = "\t.skip\t";
Rafael Espindola755be9b2006-08-25 17:55:16 +000047 CommentString = "@";
Rafael Espindola1ed3af12006-08-01 18:53:10 +000048 ConstantPoolSection = "\t.text\n";
Rafael Espindolaa1334cd2006-05-26 10:56:17 +000049 AlignmentIsInBytes = false;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000050 }
Jim Laskey563321a2006-09-06 18:34:40 +000051 };
52
53 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
54 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, TargetAsmInfo *T)
55 : AsmPrinter(O, TM, T) {
56 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000057
58 /// We name each basic block in a Function with a unique number, so
59 /// that we can consistently refer to them later. This is cleared
60 /// at the beginning of each call to runOnMachineFunction().
61 ///
62 typedef std::map<const Value *, unsigned> ValueMapTy;
63 ValueMapTy NumberForBB;
64
65 virtual const char *getPassName() const {
66 return "ARM Assembly Printer";
67 }
68
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000069 void printMemRegImm(const MachineInstr *MI, int opNum,
70 const char *Modifier = NULL) {
71 const MachineOperand &MO1 = MI->getOperand(opNum);
72 const MachineOperand &MO2 = MI->getOperand(opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000073 assert(MO1.isImmediate());
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000074 bool arith = false;
75 if (Modifier != NULL) {
76 assert(strcmp(Modifier, "arith") == 0);
77 arith = true;
78 }
Rafael Espindola1ed3af12006-08-01 18:53:10 +000079
80 if (MO2.isConstantPoolIndex()) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000081 printOperand(MI, opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000082 } else if (MO2.isRegister()) {
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000083 if(!arith)
84 O << '[';
85 printOperand(MI, opNum + 1);
Rafael Espindola1ed3af12006-08-01 18:53:10 +000086 O << ", ";
Rafael Espindolaf3a335c2006-08-17 17:09:40 +000087 printOperand(MI, opNum);
88 if(!arith)
89 O << ']';
Rafael Espindola1ed3af12006-08-01 18:53:10 +000090 } else {
91 assert(0 && "Invalid Operand Type");
92 }
Rafael Espindolaa4e64352006-07-11 11:36:48 +000093 }
94
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000095 void printOperand(const MachineInstr *MI, int opNum);
96 void printMemOperand(const MachineInstr *MI, int opNum,
97 const char *Modifier = 0);
98 void printCCOperand(const MachineInstr *MI, int opNum);
99
100 bool printInstruction(const MachineInstr *MI); // autogenerated.
101 bool runOnMachineFunction(MachineFunction &F);
102 bool doInitialization(Module &M);
103 bool doFinalization(Module &M);
104 };
105} // end of anonymous namespace
106
107#include "ARMGenAsmWriter.inc"
108
109/// createARMCodePrinterPass - Returns a pass that prints the ARM
110/// assembly code for a MachineFunction to the given output stream,
111/// using the given target machine description. This should work
112/// regardless of whether the function is in SSA form.
113///
114FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
115 TargetMachine &tm) {
Jim Laskey563321a2006-09-06 18:34:40 +0000116 ARMTargetAsmInfo *TAI = new ARMTargetAsmInfo();
117 return new ARMAsmPrinter(o, tm, TAI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000118}
119
120/// runOnMachineFunction - This uses the printMachineInstruction()
121/// method to print assembly for each instruction.
122///
123bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +0000124 SetupMachineFunction(MF);
125 O << "\n\n";
126
127 // Print out constants referenced by the function
128 EmitConstantPool(MF.getConstantPool());
129
130 // Print out jump tables referenced by the function
131 EmitJumpTableInfo(MF.getJumpTableInfo());
132
133 // Print out labels for the function.
134 const Function *F = MF.getFunction();
135 switch (F->getLinkage()) {
136 default: assert(0 && "Unknown linkage type!");
137 case Function::InternalLinkage:
138 SwitchToTextSection("\t.text", F);
139 break;
140 case Function::ExternalLinkage:
141 SwitchToTextSection("\t.text", F);
142 O << "\t.globl\t" << CurrentFnName << "\n";
143 break;
144 case Function::WeakLinkage:
145 case Function::LinkOnceLinkage:
146 assert(0 && "Not implemented");
147 break;
148 }
Rafael Espindolaa1334cd2006-05-26 10:56:17 +0000149 EmitAlignment(2, F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000150 O << CurrentFnName << ":\n";
151
152 // Print out code for the function.
153 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
154 I != E; ++I) {
155 // Print a label for the basic block.
156 if (I != MF.begin()) {
157 printBasicBlockLabel(I, true);
158 O << '\n';
159 }
160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
161 II != E; ++II) {
162 // Print the assembly for the instruction.
163 O << "\t";
164 printInstruction(II);
165 }
166 }
167
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000168 return false;
169}
170
171void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000172 const MachineOperand &MO = MI->getOperand (opNum);
173 const MRegisterInfo &RI = *TM.getRegisterInfo();
174 switch (MO.getType()) {
175 case MachineOperand::MO_Register:
176 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
177 O << LowercaseString (RI.get(MO.getReg()).Name);
178 else
179 assert(0 && "not implemented");
180 break;
181 case MachineOperand::MO_Immediate:
182 O << "#" << (int)MO.getImmedValue();
183 break;
184 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000185 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000186 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000187 case MachineOperand::MO_GlobalAddress: {
188 GlobalValue *GV = MO.getGlobal();
189 std::string Name = Mang->getValueName(GV);
190 O << Name;
191 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000192 break;
193 case MachineOperand::MO_ExternalSymbol:
194 assert(0 && "not implemented");
195 abort();
196 break;
197 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000198 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000199 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000200 break;
201 default:
202 O << "<unknown operand type>"; abort (); break;
203 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000204}
205
206void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
207 const char *Modifier) {
208 assert(0 && "not implemented");
209}
210
211void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000212 int CC = (int)MI->getOperand(opNum).getImmedValue();
213 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000214}
215
216bool ARMAsmPrinter::doInitialization(Module &M) {
217 Mang = new Mangler(M);
218 return false; // success
219}
220
221bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000222 const TargetData *TD = TM.getTargetData();
223
224 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
225 I != E; ++I) {
226 if (!I->hasInitializer()) // External global require no code
227 continue;
228
229 if (EmitSpecialLLVMGlobal(I))
230 continue;
231
232 O << "\n\n";
233 std::string name = Mang->getValueName(I);
234 Constant *C = I->getInitializer();
235 unsigned Size = TD->getTypeSize(C->getType());
236 unsigned Align = TD->getTypeAlignment(C->getType());
237
Rafael Espindola6d581e82006-07-31 20:38:13 +0000238 switch (I->getLinkage()) {
239 default:
240 assert(0 && "Unknown linkage type!");
241 break;
242 case GlobalValue::ExternalLinkage:
243 O << "\t.globl " << name << "\n";
244 break;
245 case GlobalValue::InternalLinkage:
246 break;
247 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000248
249 assert (!C->isNullValue());
250 SwitchToDataSection(".data", I);
251
252 EmitAlignment(Align, I);
253 O << "\t.type " << name << ", %object\n";
254 O << "\t.size " << name << ", " << Size << "\n";
255 O << name << ":\n";
256 EmitGlobalConstant(C);
257 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000258 AsmPrinter::doFinalization(M);
259 return false; // success
260}