1 //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file contains a printer that converts from our internal representation
12 // of machine-dependent LLVM code to GAS-format ARM assembly language.
14 //===----------------------------------------------------------------------===//
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/TargetAsmInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #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"
39 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
41 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
42 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
43 : AsmPrinter(O, TM, T) {
46 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
53 virtual const char *getPassName() const {
54 return "ARM Assembly Printer";
57 void printAddrMode1(const MachineInstr *MI, int opNum);
59 void printMemRegImm(const MachineInstr *MI, int opNum,
60 const char *Modifier = NULL) {
61 const MachineOperand &MO1 = MI->getOperand(opNum);
62 const MachineOperand &MO2 = MI->getOperand(opNum + 1);
63 assert(MO1.isImmediate());
65 if (Modifier != NULL) {
66 assert(strcmp(Modifier, "arith") == 0);
70 if (MO2.isConstantPoolIndex()) {
71 printOperand(MI, opNum + 1);
72 } else if (MO2.isRegister()) {
75 printOperand(MI, opNum + 1);
77 printOperand(MI, opNum);
81 assert(0 && "Invalid Operand Type");
85 void printOperand(const MachineInstr *MI, int opNum);
86 void printMemOperand(const MachineInstr *MI, int opNum,
87 const char *Modifier = 0);
88 void printCCOperand(const MachineInstr *MI, int opNum);
90 bool printInstruction(const MachineInstr *MI); // autogenerated.
91 bool runOnMachineFunction(MachineFunction &F);
92 bool doInitialization(Module &M);
93 bool doFinalization(Module &M);
95 } // end of anonymous namespace
97 #include "ARMGenAsmWriter.inc"
99 /// createARMCodePrinterPass - Returns a pass that prints the ARM
100 /// assembly code for a MachineFunction to the given output stream,
101 /// using the given target machine description. This should work
102 /// regardless of whether the function is in SSA form.
104 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
106 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
109 /// runOnMachineFunction - This uses the printMachineInstruction()
110 /// method to print assembly for each instruction.
112 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
113 SetupMachineFunction(MF);
116 // Print out constants referenced by the function
117 EmitConstantPool(MF.getConstantPool());
119 // Print out jump tables referenced by the function
120 EmitJumpTableInfo(MF.getJumpTableInfo());
122 // Print out labels for the function.
123 const Function *F = MF.getFunction();
124 switch (F->getLinkage()) {
125 default: assert(0 && "Unknown linkage type!");
126 case Function::InternalLinkage:
127 SwitchToTextSection("\t.text", F);
129 case Function::ExternalLinkage:
130 SwitchToTextSection("\t.text", F);
131 O << "\t.globl\t" << CurrentFnName << "\n";
133 case Function::WeakLinkage:
134 case Function::LinkOnceLinkage:
135 assert(0 && "Not implemented");
139 O << CurrentFnName << ":\n";
141 // Print out code for the function.
142 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
144 // Print a label for the basic block.
145 if (I != MF.begin()) {
146 printBasicBlockLabel(I, true);
149 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
151 // Print the assembly for the instruction.
153 printInstruction(II);
160 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
161 const MachineOperand &MO1 = MI->getOperand(opNum);
163 if(MO1.isImmediate()) {
164 printOperand(MI, opNum);
166 assert(MO1.isRegister());
167 printOperand(MI, opNum);
171 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
172 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);
179 assert(0 && "not implemented");
181 case MachineOperand::MO_Immediate:
182 O << "#" << (int)MO.getImmedValue();
184 case MachineOperand::MO_MachineBasicBlock:
185 printBasicBlockLabel(MO.getMachineBasicBlock());
187 case MachineOperand::MO_GlobalAddress: {
188 GlobalValue *GV = MO.getGlobal();
189 std::string Name = Mang->getValueName(GV);
193 case MachineOperand::MO_ExternalSymbol:
194 assert(0 && "not implemented");
197 case MachineOperand::MO_ConstantPoolIndex:
198 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
199 << '_' << MO.getConstantPoolIndex();
202 O << "<unknown operand type>"; abort (); break;
206 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
207 const char *Modifier) {
208 assert(0 && "not implemented");
211 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
212 int CC = (int)MI->getOperand(opNum).getImmedValue();
213 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
216 bool ARMAsmPrinter::doInitialization(Module &M) {
217 AsmPrinter::doInitialization(M);
218 return false; // success
221 bool ARMAsmPrinter::doFinalization(Module &M) {
222 const TargetData *TD = TM.getTargetData();
224 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
226 if (!I->hasInitializer()) // External global require no code
229 if (EmitSpecialLLVMGlobal(I))
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());
238 switch (I->getLinkage()) {
240 assert(0 && "Unknown linkage type!");
242 case GlobalValue::ExternalLinkage:
243 O << "\t.globl " << name << "\n";
245 case GlobalValue::InternalLinkage:
249 assert (!C->isNullValue());
250 SwitchToDataSection(".data", I);
252 EmitAlignment(Align, I);
253 O << "\t.type " << name << ", %object\n";
254 O << "\t.size " << name << ", " << Size << "\n";
256 EmitGlobalConstant(C);
258 AsmPrinter::doFinalization(M);
259 return false; // success