blob: 0ef22be2e64d2a5209ffb05d0e16339e30032a60 [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";
47 }
48
49 /// We name each basic block in a Function with a unique number, so
50 /// that we can consistently refer to them later. This is cleared
51 /// at the beginning of each call to runOnMachineFunction().
52 ///
53 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
55
56 virtual const char *getPassName() const {
57 return "ARM Assembly Printer";
58 }
59
60 void printOperand(const MachineInstr *MI, int opNum);
61 void printMemOperand(const MachineInstr *MI, int opNum,
62 const char *Modifier = 0);
63 void printCCOperand(const MachineInstr *MI, int opNum);
64
65 bool printInstruction(const MachineInstr *MI); // autogenerated.
66 bool runOnMachineFunction(MachineFunction &F);
67 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 };
70} // end of anonymous namespace
71
72#include "ARMGenAsmWriter.inc"
73
74/// createARMCodePrinterPass - Returns a pass that prints the ARM
75/// assembly code for a MachineFunction to the given output stream,
76/// using the given target machine description. This should work
77/// regardless of whether the function is in SSA form.
78///
79FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
80 TargetMachine &tm) {
81 return new ARMAsmPrinter(o, tm);
82}
83
84/// runOnMachineFunction - This uses the printMachineInstruction()
85/// method to print assembly for each instruction.
86///
87bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Rafael Espindola4b442b52006-05-23 02:48:20 +000088 SetupMachineFunction(MF);
89 O << "\n\n";
90
91 // Print out constants referenced by the function
92 EmitConstantPool(MF.getConstantPool());
93
94 // Print out jump tables referenced by the function
95 EmitJumpTableInfo(MF.getJumpTableInfo());
96
97 // Print out labels for the function.
98 const Function *F = MF.getFunction();
99 switch (F->getLinkage()) {
100 default: assert(0 && "Unknown linkage type!");
101 case Function::InternalLinkage:
102 SwitchToTextSection("\t.text", F);
103 break;
104 case Function::ExternalLinkage:
105 SwitchToTextSection("\t.text", F);
106 O << "\t.globl\t" << CurrentFnName << "\n";
107 break;
108 case Function::WeakLinkage:
109 case Function::LinkOnceLinkage:
110 assert(0 && "Not implemented");
111 break;
112 }
113 EmitAlignment(4, F);
114 O << CurrentFnName << ":\n";
115
116 // Print out code for the function.
117 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 I != E; ++I) {
119 // Print a label for the basic block.
120 if (I != MF.begin()) {
121 printBasicBlockLabel(I, true);
122 O << '\n';
123 }
124 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
125 II != E; ++II) {
126 // Print the assembly for the instruction.
127 O << "\t";
128 printInstruction(II);
129 }
130 }
131
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000132 return false;
133}
134
135void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000136 const MachineOperand &MO = MI->getOperand (opNum);
137 const MRegisterInfo &RI = *TM.getRegisterInfo();
138 switch (MO.getType()) {
139 case MachineOperand::MO_Register:
140 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
141 O << LowercaseString (RI.get(MO.getReg()).Name);
142 else
143 assert(0 && "not implemented");
144 break;
145 case MachineOperand::MO_Immediate:
146 O << "#" << (int)MO.getImmedValue();
147 break;
148 case MachineOperand::MO_MachineBasicBlock:
149 assert(0 && "not implemented");
150 abort();
151 return;
152 case MachineOperand::MO_GlobalAddress:
153 assert(0 && "not implemented");
154 abort();
155 break;
156 case MachineOperand::MO_ExternalSymbol:
157 assert(0 && "not implemented");
158 abort();
159 break;
160 case MachineOperand::MO_ConstantPoolIndex:
161 assert(0 && "not implemented");
162 abort();
163 break;
164 default:
165 O << "<unknown operand type>"; abort (); break;
166 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000167}
168
169void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
170 const char *Modifier) {
171 assert(0 && "not implemented");
172}
173
174void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
175 assert(0 && "not implemented");
176}
177
178bool ARMAsmPrinter::doInitialization(Module &M) {
179 Mang = new Mangler(M);
180 return false; // success
181}
182
183bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000184 AsmPrinter::doFinalization(M);
185 return false; // success
186}