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 static const char *ARMCondCodeToString(ARMCC::CondCodes CC) {
43 default: assert(0 && "Unknown condition code");
44 case ARMCC::EQ: return "eq";
45 case ARMCC::NE: return "ne";
46 case ARMCC::CS: return "cs";
47 case ARMCC::CC: return "cc";
48 case ARMCC::MI: return "mi";
49 case ARMCC::PL: return "pl";
50 case ARMCC::VS: return "vs";
51 case ARMCC::VC: return "vc";
52 case ARMCC::HI: return "hi";
53 case ARMCC::LS: return "ls";
54 case ARMCC::GE: return "ge";
55 case ARMCC::LT: return "lt";
56 case ARMCC::GT: return "gt";
57 case ARMCC::LE: return "le";
58 case ARMCC::AL: return "al";
62 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
63 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
64 : AsmPrinter(O, TM, T) {
67 /// We name each basic block in a Function with a unique number, so
68 /// that we can consistently refer to them later. This is cleared
69 /// at the beginning of each call to runOnMachineFunction().
71 typedef std::map<const Value *, unsigned> ValueMapTy;
72 ValueMapTy NumberForBB;
74 virtual const char *getPassName() const {
75 return "ARM Assembly Printer";
78 void printAddrMode1(const MachineInstr *MI, int opNum);
79 void printAddrMode2(const MachineInstr *MI, int opNum);
80 void printAddrMode5(const MachineInstr *MI, int opNum);
81 void printOperand(const MachineInstr *MI, int opNum);
82 void printMemOperand(const MachineInstr *MI, int opNum,
83 const char *Modifier = 0);
84 void printCCOperand(const MachineInstr *MI, int opNum);
86 bool printInstruction(const MachineInstr *MI); // autogenerated.
87 bool runOnMachineFunction(MachineFunction &F);
88 bool doInitialization(Module &M);
89 bool doFinalization(Module &M);
91 } // end of anonymous namespace
93 #include "ARMGenAsmWriter.inc"
95 /// createARMCodePrinterPass - Returns a pass that prints the ARM
96 /// assembly code for a MachineFunction to the given output stream,
97 /// using the given target machine description. This should work
98 /// regardless of whether the function is in SSA form.
100 FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
102 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
105 /// runOnMachineFunction - This uses the printMachineInstruction()
106 /// method to print assembly for each instruction.
108 bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
109 SetupMachineFunction(MF);
112 // Print out constants referenced by the function
113 EmitConstantPool(MF.getConstantPool());
115 // Print out jump tables referenced by the function
116 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
118 // Print out labels for the function.
119 const Function *F = MF.getFunction();
120 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
122 switch (F->getLinkage()) {
123 default: assert(0 && "Unknown linkage type!");
124 case Function::InternalLinkage:
126 case Function::ExternalLinkage:
127 O << "\t.globl\t" << CurrentFnName << "\n";
129 case Function::WeakLinkage:
130 case Function::LinkOnceLinkage:
131 assert(0 && "Not implemented");
135 O << CurrentFnName << ":\n";
137 // Print out code for the function.
138 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
140 // Print a label for the basic block.
141 if (I != MF.begin()) {
142 printBasicBlockLabel(I, true);
145 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
147 // Print the assembly for the instruction.
149 printInstruction(II);
156 void ARMAsmPrinter::printAddrMode1(const MachineInstr *MI, int opNum) {
157 const MachineOperand &Arg = MI->getOperand(opNum);
158 const MachineOperand &Shift = MI->getOperand(opNum + 1);
159 const MachineOperand &ShiftType = MI->getOperand(opNum + 2);
161 if(Arg.isImmediate()) {
162 assert(Shift.getImmedValue() == 0);
163 printOperand(MI, opNum);
165 assert(Arg.isRegister());
166 printOperand(MI, opNum);
167 if(Shift.isRegister() || Shift.getImmedValue() != 0) {
168 const char *s = NULL;
169 switch(ShiftType.getImmedValue()) {
187 printOperand(MI, opNum + 1);
192 void ARMAsmPrinter::printAddrMode2(const MachineInstr *MI, int opNum) {
193 const MachineOperand &Arg = MI->getOperand(opNum);
194 const MachineOperand &Offset = MI->getOperand(opNum + 1);
195 assert(Offset.isImmediate());
197 if (Arg.isConstantPoolIndex()) {
198 assert(Offset.getImmedValue() == 0);
199 printOperand(MI, opNum);
201 assert(Arg.isRegister());
203 printOperand(MI, opNum);
205 printOperand(MI, opNum + 1);
210 void ARMAsmPrinter::printAddrMode5(const MachineInstr *MI, int opNum) {
211 const MachineOperand &Arg = MI->getOperand(opNum);
212 const MachineOperand &Offset = MI->getOperand(opNum + 1);
213 assert(Offset.isImmediate());
215 if (Arg.isConstantPoolIndex()) {
216 assert(Offset.getImmedValue() == 0);
217 printOperand(MI, opNum);
219 assert(Arg.isRegister());
221 printOperand(MI, opNum);
223 printOperand(MI, opNum + 1);
228 void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
229 const MachineOperand &MO = MI->getOperand (opNum);
230 const MRegisterInfo &RI = *TM.getRegisterInfo();
231 switch (MO.getType()) {
232 case MachineOperand::MO_Register:
233 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
234 O << LowercaseString (RI.get(MO.getReg()).Name);
236 assert(0 && "not implemented");
238 case MachineOperand::MO_Immediate:
239 O << "#" << (int)MO.getImmedValue();
241 case MachineOperand::MO_MachineBasicBlock:
242 printBasicBlockLabel(MO.getMachineBasicBlock());
244 case MachineOperand::MO_GlobalAddress: {
245 GlobalValue *GV = MO.getGlobal();
246 std::string Name = Mang->getValueName(GV);
250 case MachineOperand::MO_ExternalSymbol:
251 O << TAI->getGlobalPrefix() << MO.getSymbolName();
253 case MachineOperand::MO_ConstantPoolIndex:
254 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
255 << '_' << MO.getConstantPoolIndex();
258 O << "<unknown operand type>"; abort (); break;
262 void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
263 const char *Modifier) {
264 assert(0 && "not implemented");
267 void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
268 int CC = (int)MI->getOperand(opNum).getImmedValue();
269 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
272 bool ARMAsmPrinter::doInitialization(Module &M) {
273 AsmPrinter::doInitialization(M);
274 return false; // success
277 bool ARMAsmPrinter::doFinalization(Module &M) {
278 const TargetData *TD = TM.getTargetData();
280 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
282 if (!I->hasInitializer()) // External global require no code
285 if (EmitSpecialLLVMGlobal(I))
289 std::string name = Mang->getValueName(I);
290 Constant *C = I->getInitializer();
291 unsigned Size = TD->getTypeSize(C->getType());
292 unsigned Align = TD->getTypeAlignment(C->getType());
294 if (C->isNullValue() &&
295 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
296 I->hasWeakLinkage())) {
297 SwitchToDataSection(".data", I);
298 if (I->hasInternalLinkage())
299 O << "\t.local " << name << "\n";
301 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
302 << "," << (unsigned)TD->getTypeAlignment(C->getType());
304 O << TAI->getCommentString() << " ";
305 WriteAsOperand(O, I, true, true, &M);
308 switch (I->getLinkage()) {
310 assert(0 && "Unknown linkage type!");
312 case GlobalValue::ExternalLinkage:
313 O << "\t.globl " << name << "\n";
315 case GlobalValue::InternalLinkage:
319 if (C->isNullValue())
320 SwitchToDataSection(".bss", I);
322 SwitchToDataSection(".data", I);
324 EmitAlignment(Align, I);
325 O << "\t.type " << name << ", %object\n";
326 O << "\t.size " << name << ", " << Size << "\n";
328 EmitGlobalConstant(C);
332 AsmPrinter::doFinalization(M);
333 return false; // success