blob: efb20d150ed69ded21dc8e6e9465a885c1fefee0 [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"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Mangler.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/MathExtras.h"
32#include <cctype>
33#include <iostream>
34using namespace llvm;
35
36namespace {
37 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
39 struct ARMAsmPrinter : public AsmPrinter {
40 ARMAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
41 Data16bitsDirective = "\t.half\t";
42 Data32bitsDirective = "\t.word\t";
43 Data64bitsDirective = 0;
44 ZeroDirective = "\t.skip\t";
45 CommentString = "!";
46 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
Rafael Espindolaa1334cd2006-05-26 10:56:17 +000047 AlignmentIsInBytes = false;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000048 }
49
50 /// We name each basic block in a Function with a unique number, so
51 /// that we can consistently refer to them later. This is cleared
52 /// at the beginning of each call to runOnMachineFunction().
53 ///
54 typedef std::map<const Value *, unsigned> ValueMapTy;
55 ValueMapTy NumberForBB;
56
57 virtual const char *getPassName() const {
58 return "ARM Assembly Printer";
59 }
60
Rafael Espindolaa4e64352006-07-11 11:36:48 +000061 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
62 printOperand(MI, OpNo + 1);
63 O << ", ";
64 printOperand(MI, OpNo);
65 }
66
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000067 void printOperand(const MachineInstr *MI, int opNum);
68 void printMemOperand(const MachineInstr *MI, int opNum,
69 const char *Modifier = 0);
70 void printCCOperand(const MachineInstr *MI, int opNum);
71
72 bool printInstruction(const MachineInstr *MI); // autogenerated.
73 bool runOnMachineFunction(MachineFunction &F);
74 bool doInitialization(Module &M);
75 bool doFinalization(Module &M);
76 };
77} // end of anonymous namespace
78
79#include "ARMGenAsmWriter.inc"
80
81/// createARMCodePrinterPass - Returns a pass that prints the ARM
82/// assembly code for a MachineFunction to the given output stream,
83/// using the given target machine description. This should work
84/// regardless of whether the function is in SSA form.
85///
86FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
87 TargetMachine &tm) {
88 return new ARMAsmPrinter(o, tm);
89}
90
91/// runOnMachineFunction - This uses the printMachineInstruction()
92/// method to print assembly for each instruction.
93///
94bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +000095 SetupMachineFunction(MF);
96 O << "\n\n";
97
98 // Print out constants referenced by the function
99 EmitConstantPool(MF.getConstantPool());
100
101 // Print out jump tables referenced by the function
102 EmitJumpTableInfo(MF.getJumpTableInfo());
103
104 // Print out labels for the function.
105 const Function *F = MF.getFunction();
106 switch (F->getLinkage()) {
107 default: assert(0 && "Unknown linkage type!");
108 case Function::InternalLinkage:
109 SwitchToTextSection("\t.text", F);
110 break;
111 case Function::ExternalLinkage:
112 SwitchToTextSection("\t.text", F);
113 O << "\t.globl\t" << CurrentFnName << "\n";
114 break;
115 case Function::WeakLinkage:
116 case Function::LinkOnceLinkage:
117 assert(0 && "Not implemented");
118 break;
119 }
Rafael Espindolaa1334cd2006-05-26 10:56:17 +0000120 EmitAlignment(2, F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000121 O << CurrentFnName << ":\n";
122
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 // Print a label for the basic block.
127 if (I != MF.begin()) {
128 printBasicBlockLabel(I, true);
129 O << '\n';
130 }
131 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
132 II != E; ++II) {
133 // Print the assembly for the instruction.
134 O << "\t";
135 printInstruction(II);
136 }
137 }
138
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000139 return false;
140}
141
142void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000143 const MachineOperand &MO = MI->getOperand (opNum);
144 const MRegisterInfo &RI = *TM.getRegisterInfo();
145 switch (MO.getType()) {
146 case MachineOperand::MO_Register:
147 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
148 O << LowercaseString (RI.get(MO.getReg()).Name);
149 else
150 assert(0 && "not implemented");
151 break;
152 case MachineOperand::MO_Immediate:
153 O << "#" << (int)MO.getImmedValue();
154 break;
155 case MachineOperand::MO_MachineBasicBlock:
156 assert(0 && "not implemented");
157 abort();
158 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000159 case MachineOperand::MO_GlobalAddress: {
160 GlobalValue *GV = MO.getGlobal();
161 std::string Name = Mang->getValueName(GV);
162 O << Name;
163 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000164 break;
165 case MachineOperand::MO_ExternalSymbol:
166 assert(0 && "not implemented");
167 abort();
168 break;
169 case MachineOperand::MO_ConstantPoolIndex:
170 assert(0 && "not implemented");
171 abort();
172 break;
173 default:
174 O << "<unknown operand type>"; abort (); break;
175 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000176}
177
178void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
179 const char *Modifier) {
180 assert(0 && "not implemented");
181}
182
183void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
184 assert(0 && "not implemented");
185}
186
187bool ARMAsmPrinter::doInitialization(Module &M) {
188 Mang = new Mangler(M);
189 return false; // success
190}
191
192bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000193 AsmPrinter::doFinalization(M);
194 return false; // success
195}