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 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asm-printer"
18 #include "ARMInstrInfo.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.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"
37 STATISTIC(EmittedInsts, "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 /// We name each basic block in a Function with a unique number, so
67 /// that we can consistently refer to them later. This is cleared
68 /// at the beginning of each call to runOnMachineFunction().
70 typedef std::map<const Value *, unsigned> ValueMapTy;
71 ValueMapTy NumberForBB;
73 virtual const char *getPassName() const {
74 return "ARM Assembly Printer";
77 void printAddrMode1(const MachineInstr *MI, int opNum);
78 void printAddrMode2(const MachineInstr *MI, int opNum);
79 void printAddrMode5(const MachineInstr *MI, int opNum);
80 void printOperand(const MachineInstr *MI, int opNum);
81 void printMemOperand(const MachineInstr *MI, int opNum,
82 const char *Modifier = 0);
83 void printCCOperand(const MachineInstr *MI, int opNum);
85 bool printInstruction(const MachineInstr *MI); // autogenerated.
86 bool runOnMachineFunction(MachineFunction &F);
87 bool doInitialization(Module &M);
88 bool doFinalization(Module &M);
90 } // end of anonymous namespace
92 #include "ARMGenAsmWriter.inc"
94 /// createARMCodePrinterPass - Returns a pass that prints the ARM
95 /// assembly code for a MachineFunction to the given output stream,
96 /// using the given target machine description. This should work
97 /// regardless of whether the function is in SSA form.
99 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
101 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
104 /// runOnMachineFunction - This uses the printMachineInstruction()
105 /// method to print assembly for each instruction.
107 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
108 SetupMachineFunction(MF);
111 // Print out constants referenced by the function
112 EmitConstantPool(MF.getConstantPool());
114 const std::vector<MachineConstantPoolEntry>
115 &CP = MF.getConstantPool()->getConstants();
116 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
117 MachineConstantPoolEntry CPE = CP[i];
118 if (!CPE.isMachineConstantPoolEntry()){
119 Constant *CV = CPE.Val.ConstVal;
120 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
121 if (GV->hasExternalWeakLinkage()) {
122 ExtWeakSymbols.insert(GV);
128 // Print out jump tables referenced by the function
129 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
131 // Print out labels for the function.
132 const Function *F = MF.getFunction();
133 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
135 switch (F->getLinkage()) {
136 default: assert(0 && "Unknown linkage type!");
137 case Function::InternalLinkage:
139 case Function::ExternalLinkage:
140 O << "\t.globl\t" << CurrentFnName << "\n";
142 case Function::WeakLinkage:
143 case Function::LinkOnceLinkage:
144 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
148 O << CurrentFnName << ":\n";
150 // Print out code for the function.
151 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
153 // Print a label for the basic block.
154 if (I != MF.begin()) {
155 printBasicBlockLabel(I, true);
158 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
160 // Print the assembly for the instruction.
163 printInstruction(II);
170 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
171 const MachineOperand &Arg = MI->getOperand(opNum);
172 const MachineOperand &Shift = MI->getOperand(opNum + 1);
173 const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
175 if(Arg.isImmediate()) {
176 assert(Shift.getImmedValue() == 0);
177 printOperand(MI, opNum);
179 assert(Arg.isRegister());
180 printOperand(MI, opNum);
181 if(Shift.isRegister() || Shift.getImmedValue() != 0) {
182 const char *s = NULL;
183 switch(ShiftType.getImmedValue()) {
201 printOperand(MI, opNum + 1);
206 void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
207 const MachineOperand &Arg = MI->getOperand(opNum);
208 const MachineOperand &Offset = MI->getOperand(opNum + 1);
209 assert(Offset.isImmediate());
211 if (Arg.isConstantPoolIndex()) {
212 assert(Offset.getImmedValue() == 0);
213 printOperand(MI, opNum);
215 assert(Arg.isRegister());
217 printOperand(MI, opNum);
219 printOperand(MI, opNum + 1);
224 void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
225 const MachineOperand &Arg = MI->getOperand(opNum);
226 const MachineOperand &Offset = MI->getOperand(opNum + 1);
227 assert(Offset.isImmediate());
229 if (Arg.isConstantPoolIndex()) {
230 assert(Offset.getImmedValue() == 0);
231 printOperand(MI, opNum);
233 assert(Arg.isRegister());
235 printOperand(MI, opNum);
237 printOperand(MI, opNum + 1);
242 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
243 const MachineOperand &MO = MI->getOperand (opNum);
244 const MRegisterInfo &RI = *TM.getRegisterInfo();
245 switch (MO.getType()) {
246 case MachineOperand::MO_Register:
247 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
248 O << LowercaseString (RI.get(MO.getReg()).Name);
250 assert(0 && "not implemented");
252 case MachineOperand::MO_Immediate:
253 O << "#" << (int)MO.getImmedValue();
255 case MachineOperand::MO_MachineBasicBlock:
256 printBasicBlockLabel(MO.getMachineBasicBlock());
258 case MachineOperand::MO_GlobalAddress: {
259 GlobalValue *GV = MO.getGlobal();
260 std::string Name = Mang->getValueName(GV);
262 if (GV->hasExternalWeakLinkage()) {
263 ExtWeakSymbols.insert(GV);
267 case MachineOperand::MO_ExternalSymbol:
268 O << TAI->getGlobalPrefix() << MO.getSymbolName();
270 case MachineOperand::MO_ConstantPoolIndex:
271 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
272 << '_' << MO.getConstantPoolIndex();
275 O << "<unknown operand type>"; abort (); break;
279 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
280 const char *Modifier) {
281 assert(0 && "not implemented");
284 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
285 int CC = (int)MI->getOperand(opNum).getImmedValue();
286 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
289 bool ARMAsmPrinter::doInitialization(Module &M) {
290 AsmPrinter::doInitialization(M);
291 return false; // success
294 bool ARMAsmPrinter::doFinalization(Module &M) {
295 const TargetData *TD = TM.getTargetData();
297 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
299 if (!I->hasInitializer()) // External global require no code
302 if (EmitSpecialLLVMGlobal(I))
306 std::string name = Mang->getValueName(I);
307 Constant *C = I->getInitializer();
308 unsigned Size = TD->getTypeSize(C->getType());
309 unsigned Align = Log2_32(TD->getTypeAlignment(C->getType()));
311 if (C->isNullValue() &&
313 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
314 I->hasWeakLinkage())) {
315 SwitchToDataSection(".data", I);
316 if (I->hasInternalLinkage())
317 O << "\t.local " << name << "\n";
319 O << "\t.comm " << name << "," << Size
320 << "," << (unsigned) (1 << Align);
323 switch (I->getLinkage()) {
325 assert(0 && "Unknown linkage type!");
327 case GlobalValue::ExternalLinkage:
328 O << "\t.globl " << name << "\n";
330 case GlobalValue::InternalLinkage:
334 if (I->hasSection() &&
335 (I->getSection() == ".ctors" ||
336 I->getSection() == ".dtors")) {
337 std::string SectionName = ".section " + I->getSection();
339 SectionName += ",\"aw\",%progbits";
341 SwitchToDataSection(SectionName.c_str());
343 SwitchToDataSection(TAI->getDataSection(), I);
346 EmitAlignment(Align, I);
347 O << "\t.type " << name << ", %object\n";
348 O << "\t.size " << name << ", " << Size << "\n";
350 EmitGlobalConstant(C);
354 AsmPrinter::doFinalization(M);
355 return false; // success