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 printMemRegImm(const MachineInstr *MI, int opNum,
58 const char *Modifier = NULL) {
59 const MachineOperand &MO1 = MI->getOperand(opNum);
60 const MachineOperand &MO2 = MI->getOperand(opNum + 1);
61 assert(MO1.isImmediate());
63 if (Modifier != NULL) {
64 assert(strcmp(Modifier, "arith") == 0);
68 if (MO2.isConstantPoolIndex()) {
69 printOperand(MI, opNum + 1);
70 } else if (MO2.isRegister()) {
73 printOperand(MI, opNum + 1);
75 printOperand(MI, opNum);
79 assert(0 && "Invalid Operand Type");
83 void printOperand(const MachineInstr *MI, int opNum);
84 void printMemOperand(const MachineInstr *MI, int opNum,
85 const char *Modifier = 0);
86 void printCCOperand(const MachineInstr *MI, int opNum);
88 bool printInstruction(const MachineInstr *MI); // autogenerated.
89 bool runOnMachineFunction(MachineFunction &F);
90 bool doInitialization(Module &M);
91 bool doFinalization(Module &M);
93 } // end of anonymous namespace
95 #include "ARMGenAsmWriter.inc"
97 /// createARMCodePrinterPass - Returns a pass that prints the ARM
98 /// assembly code for a MachineFunction to the given output stream,
99 /// using the given target machine description. This should work
100 /// regardless of whether the function is in SSA form.
102 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
104 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
107 /// runOnMachineFunction - This uses the printMachineInstruction()
108 /// method to print assembly for each instruction.
110 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
111 SetupMachineFunction(MF);
114 // Print out constants referenced by the function
115 EmitConstantPool(MF.getConstantPool());
117 // Print out jump tables referenced by the function
118 EmitJumpTableInfo(MF.getJumpTableInfo());
120 // Print out labels for the function.
121 const Function *F = MF.getFunction();
122 switch (F->getLinkage()) {
123 default: assert(0 && "Unknown linkage type!");
124 case Function::InternalLinkage:
125 SwitchToTextSection("\t.text", F);
127 case Function::ExternalLinkage:
128 SwitchToTextSection("\t.text", F);
129 O << "\t.globl\t" << CurrentFnName << "\n";
131 case Function::WeakLinkage:
132 case Function::LinkOnceLinkage:
133 assert(0 && "Not implemented");
137 O << CurrentFnName << ":\n";
139 // Print out code for the function.
140 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
142 // Print a label for the basic block.
143 if (I != MF.begin()) {
144 printBasicBlockLabel(I, true);
147 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
149 // Print the assembly for the instruction.
151 printInstruction(II);
158 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
159 const MachineOperand &MO = MI->getOperand (opNum);
160 const MRegisterInfo &RI = *TM.getRegisterInfo();
161 switch (MO.getType()) {
162 case MachineOperand::MO_Register:
163 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
164 O << LowercaseString (RI.get(MO.getReg()).Name);
166 assert(0 && "not implemented");
168 case MachineOperand::MO_Immediate:
169 O << "#" << (int)MO.getImmedValue();
171 case MachineOperand::MO_MachineBasicBlock:
172 printBasicBlockLabel(MO.getMachineBasicBlock());
174 case MachineOperand::MO_GlobalAddress: {
175 GlobalValue *GV = MO.getGlobal();
176 std::string Name = Mang->getValueName(GV);
180 case MachineOperand::MO_ExternalSymbol:
181 assert(0 && "not implemented");
184 case MachineOperand::MO_ConstantPoolIndex:
185 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
186 << '_' << MO.getConstantPoolIndex();
189 O << "<unknown operand type>"; abort (); break;
193 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
194 const char *Modifier) {
195 assert(0 && "not implemented");
198 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
199 int CC = (int)MI->getOperand(opNum).getImmedValue();
200 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
203 bool ARMAsmPrinter::doInitialization(Module &M) {
204 Mang = new Mangler(M);
205 return false; // success
208 bool ARMAsmPrinter::doFinalization(Module &M) {
209 const TargetData *TD = TM.getTargetData();
211 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
213 if (!I->hasInitializer()) // External global require no code
216 if (EmitSpecialLLVMGlobal(I))
220 std::string name = Mang->getValueName(I);
221 Constant *C = I->getInitializer();
222 unsigned Size = TD->getTypeSize(C->getType());
223 unsigned Align = TD->getTypeAlignment(C->getType());
225 switch (I->getLinkage()) {
227 assert(0 && "Unknown linkage type!");
229 case GlobalValue::ExternalLinkage:
230 O << "\t.globl " << name << "\n";
232 case GlobalValue::InternalLinkage:
236 assert (!C->isNullValue());
237 SwitchToDataSection(".data", I);
239 EmitAlignment(Align, I);
240 O << "\t.type " << name << ", %object\n";
241 O << "\t.size " << name << ", " << Size << "\n";
243 EmitGlobalConstant(C);
245 AsmPrinter::doFinalization(M);
246 return false; // success