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