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 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
126 switch (F->getLinkage()) {
127 default: assert(0 && "Unknown linkage type!");
128 case Function::InternalLinkage:
130 case Function::ExternalLinkage:
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 &Arg = MI->getOperand(opNum);
162 const MachineOperand &Shift = MI->getOperand(opNum + 1);
163 const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
165 if(Arg.isImmediate()) {
166 assert(Shift.getImmedValue() == 0);
167 printOperand(MI, opNum);
169 assert(Arg.isRegister());
170 printOperand(MI, opNum);
171 if(Shift.isRegister() || Shift.getImmedValue() != 0) {
172 const char *s = NULL;
173 switch(ShiftType.getImmedValue()) {
191 printOperand(MI, opNum + 1);
196 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
197 const MachineOperand &MO = MI->getOperand (opNum);
198 const MRegisterInfo &RI = *TM.getRegisterInfo();
199 switch (MO.getType()) {
200 case MachineOperand::MO_Register:
201 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
202 O << LowercaseString (RI.get(MO.getReg()).Name);
204 assert(0 && "not implemented");
206 case MachineOperand::MO_Immediate:
207 O << "#" << (int)MO.getImmedValue();
209 case MachineOperand::MO_MachineBasicBlock:
210 printBasicBlockLabel(MO.getMachineBasicBlock());
212 case MachineOperand::MO_GlobalAddress: {
213 GlobalValue *GV = MO.getGlobal();
214 std::string Name = Mang->getValueName(GV);
218 case MachineOperand::MO_ExternalSymbol:
219 assert(0 && "not implemented");
222 case MachineOperand::MO_ConstantPoolIndex:
223 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
224 << '_' << MO.getConstantPoolIndex();
227 O << "<unknown operand type>"; abort (); break;
231 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
232 const char *Modifier) {
233 assert(0 && "not implemented");
236 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
237 int CC = (int)MI->getOperand(opNum).getImmedValue();
238 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
241 bool ARMAsmPrinter::doInitialization(Module &M) {
242 AsmPrinter::doInitialization(M);
243 return false; // success
246 bool ARMAsmPrinter::doFinalization(Module &M) {
247 const TargetData *TD = TM.getTargetData();
249 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
251 if (!I->hasInitializer()) // External global require no code
254 if (EmitSpecialLLVMGlobal(I))
258 std::string name = Mang->getValueName(I);
259 Constant *C = I->getInitializer();
260 unsigned Size = TD->getTypeSize(C->getType());
261 unsigned Align = TD->getTypeAlignment(C->getType());
263 switch (I->getLinkage()) {
265 assert(0 && "Unknown linkage type!");
267 case GlobalValue::ExternalLinkage:
268 O << "\t.globl " << name << "\n";
270 case GlobalValue::InternalLinkage:
274 assert (!C->isNullValue());
275 SwitchToDataSection(".data", I);
277 EmitAlignment(Align, I);
278 O << "\t.type " << name << ", %object\n";
279 O << "\t.size " << name << ", " << Size << "\n";
281 EmitGlobalConstant(C);
283 AsmPrinter::doFinalization(M);
284 return false; // success