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/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/MathExtras.h"
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40 static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
42 default: assert(0 && "Unknown condition code");
43 case ARMCC::EQ: return "eq";
44 case ARMCC::NE: return "ne";
45 case ARMCC::CS: return "cs";
46 case ARMCC::CC: return "cc";
47 case ARMCC::MI: return "mi";
48 case ARMCC::PL: return "pl";
49 case ARMCC::VS: return "vs";
50 case ARMCC::VC: return "vc";
51 case ARMCC::HI: return "hi";
52 case ARMCC::LS: return "ls";
53 case ARMCC::GE: return "ge";
54 case ARMCC::LT: return "lt";
55 case ARMCC::GT: return "gt";
56 case ARMCC::LE: return "le";
57 case ARMCC::AL: return "al";
61 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
62 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
63 : AsmPrinter(O, TM, T) {
66 std::set<std::string> ExtWeakSymbols;
68 /// We name each basic block in a Function with a unique number, so
69 /// that we can consistently refer to them later. This is cleared
70 /// at the beginning of each call to runOnMachineFunction().
72 typedef std::map<const Value *, unsigned> ValueMapTy;
73 ValueMapTy NumberForBB;
75 virtual const char *getPassName() const {
76 return "ARM Assembly Printer";
79 void printAddrMode1(const MachineInstr *MI, int opNum);
80 void printAddrMode2(const MachineInstr *MI, int opNum);
81 void printAddrMode5(const MachineInstr *MI, int opNum);
82 void printOperand(const MachineInstr *MI, int opNum);
83 void printMemOperand(const MachineInstr *MI, int opNum,
84 const char *Modifier = 0);
85 void printCCOperand(const MachineInstr *MI, int opNum);
87 bool printInstruction(const MachineInstr *MI); // autogenerated.
88 bool runOnMachineFunction(MachineFunction &F);
89 bool doInitialization(Module &M);
90 bool doFinalization(Module &M);
92 } // end of anonymous namespace
94 #include "ARMGenAsmWriter.inc"
96 /// createARMCodePrinterPass - Returns a pass that prints the ARM
97 /// assembly code for a MachineFunction to the given output stream,
98 /// using the given target machine description. This should work
99 /// regardless of whether the function is in SSA form.
101 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
103 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
106 /// runOnMachineFunction - This uses the printMachineInstruction()
107 /// method to print assembly for each instruction.
109 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
110 SetupMachineFunction(MF);
113 // Print out constants referenced by the function
114 EmitConstantPool(MF.getConstantPool());
116 // Print out jump tables referenced by the function
117 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
119 // Print out labels for the function.
120 const Function *F = MF.getFunction();
121 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
123 switch (F->getLinkage()) {
124 default: assert(0 && "Unknown linkage type!");
125 case Function::InternalLinkage:
127 case Function::ExternalLinkage:
128 O << "\t.globl\t" << CurrentFnName << "\n";
130 case Function::WeakLinkage:
131 case Function::LinkOnceLinkage:
132 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
136 O << CurrentFnName << ":\n";
138 // Print out code for the function.
139 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
141 // Print a label for the basic block.
142 if (I != MF.begin()) {
143 printBasicBlockLabel(I, true);
146 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
148 // Print the assembly for the instruction.
150 printInstruction(II);
157 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
158 const MachineOperand &Arg = MI->getOperand(opNum);
159 const MachineOperand &Shift = MI->getOperand(opNum + 1);
160 const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
162 if(Arg.isImmediate()) {
163 assert(Shift.getImmedValue() == 0);
164 printOperand(MI, opNum);
166 assert(Arg.isRegister());
167 printOperand(MI, opNum);
168 if(Shift.isRegister() || Shift.getImmedValue() != 0) {
169 const char *s = NULL;
170 switch(ShiftType.getImmedValue()) {
188 printOperand(MI, opNum + 1);
193 void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
194 const MachineOperand &Arg = MI->getOperand(opNum);
195 const MachineOperand &Offset = MI->getOperand(opNum + 1);
196 assert(Offset.isImmediate());
198 if (Arg.isConstantPoolIndex()) {
199 assert(Offset.getImmedValue() == 0);
200 printOperand(MI, opNum);
202 assert(Arg.isRegister());
204 printOperand(MI, opNum);
206 printOperand(MI, opNum + 1);
211 void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
212 const MachineOperand &Arg = MI->getOperand(opNum);
213 const MachineOperand &Offset = MI->getOperand(opNum + 1);
214 assert(Offset.isImmediate());
216 if (Arg.isConstantPoolIndex()) {
217 assert(Offset.getImmedValue() == 0);
218 printOperand(MI, opNum);
220 assert(Arg.isRegister());
222 printOperand(MI, opNum);
224 printOperand(MI, opNum + 1);
229 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
230 const MachineOperand &MO = MI->getOperand (opNum);
231 const MRegisterInfo &RI = *TM.getRegisterInfo();
232 switch (MO.getType()) {
233 case MachineOperand::MO_Register:
234 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
235 O << LowercaseString (RI.get(MO.getReg()).Name);
237 assert(0 && "not implemented");
239 case MachineOperand::MO_Immediate:
240 O << "#" << (int)MO.getImmedValue();
242 case MachineOperand::MO_MachineBasicBlock:
243 printBasicBlockLabel(MO.getMachineBasicBlock());
245 case MachineOperand::MO_GlobalAddress: {
246 GlobalValue *GV = MO.getGlobal();
247 std::string Name = Mang->getValueName(GV);
249 if (GV->hasExternalWeakLinkage()) {
250 ExtWeakSymbols.insert(Name);
254 case MachineOperand::MO_ExternalSymbol:
255 O << TAI->getGlobalPrefix() << MO.getSymbolName();
257 case MachineOperand::MO_ConstantPoolIndex:
258 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
259 << '_' << MO.getConstantPoolIndex();
262 O << "<unknown operand type>"; abort (); break;
266 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
267 const char *Modifier) {
268 assert(0 && "not implemented");
271 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
272 int CC = (int)MI->getOperand(opNum).getImmedValue();
273 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
276 bool ARMAsmPrinter::doInitialization(Module &M) {
277 AsmPrinter::doInitialization(M);
278 return false; // success
281 bool ARMAsmPrinter::doFinalization(Module &M) {
282 const TargetData *TD = TM.getTargetData();
284 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
286 if (!I->hasInitializer()) // External global require no code
289 if (EmitSpecialLLVMGlobal(I))
293 std::string name = Mang->getValueName(I);
294 Constant *C = I->getInitializer();
295 unsigned Size = TD->getTypeSize(C->getType());
296 unsigned Align = TD->getTypeAlignment(C->getType());
298 if (C->isNullValue() &&
299 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
300 I->hasWeakLinkage())) {
301 SwitchToDataSection(".data", I);
302 if (I->hasInternalLinkage())
303 O << "\t.local " << name << "\n";
305 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
306 << "," << (unsigned)TD->getTypeAlignment(C->getType());
309 switch (I->getLinkage()) {
311 assert(0 && "Unknown linkage type!");
313 case GlobalValue::ExternalLinkage:
314 O << "\t.globl " << name << "\n";
316 case GlobalValue::InternalLinkage:
320 if (C->isNullValue())
321 SwitchToDataSection(".bss", I);
323 SwitchToDataSection(".data", I);
325 EmitAlignment(Align, I);
326 O << "\t.type " << name << ", %object\n";
327 O << "\t.size " << name << ", " << Size << "\n";
329 EmitGlobalConstant(C);
333 if (ExtWeakSymbols.begin() != ExtWeakSymbols.end())
334 SwitchToDataSection("");
335 for (std::set<std::string>::iterator i = ExtWeakSymbols.begin(),
336 e = ExtWeakSymbols.end(); i != e; ++i) {
337 O << TAI->getWeakRefDirective() << *i << "\n";
340 AsmPrinter::doFinalization(M);
341 return false; // success