Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 1 | //===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This file contains a printer that converts from our internal representation |
| 12 | // of machine-dependent LLVM code to GAS-format ARM assembly language. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "ARM.h" |
| 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" |
Rafael Espindola | b01c4bb | 2006-07-27 11:38:51 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 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" |
| 33 | #include <cctype> |
| 34 | #include <iostream> |
| 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 39 | |
| 40 | struct ARMAsmPrinter : public AsmPrinter { |
| 41 | ARMAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { |
| 42 | Data16bitsDirective = "\t.half\t"; |
| 43 | Data32bitsDirective = "\t.word\t"; |
| 44 | Data64bitsDirective = 0; |
| 45 | ZeroDirective = "\t.skip\t"; |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 46 | CommentString = "#"; |
| 47 | ConstantPoolSection = "\t.text\n"; |
Rafael Espindola | a1334cd | 2006-05-26 10:56:17 +0000 | [diff] [blame] | 48 | AlignmentIsInBytes = false; |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// We name each basic block in a Function with a unique number, so |
| 52 | /// that we can consistently refer to them later. This is cleared |
| 53 | /// at the beginning of each call to runOnMachineFunction(). |
| 54 | /// |
| 55 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 56 | ValueMapTy NumberForBB; |
| 57 | |
| 58 | virtual const char *getPassName() const { |
| 59 | return "ARM Assembly Printer"; |
| 60 | } |
| 61 | |
Rafael Espindola | f3a335c | 2006-08-17 17:09:40 +0000 | [diff] [blame] | 62 | void printMemRegImm(const MachineInstr *MI, int opNum, |
| 63 | const char *Modifier = NULL) { |
| 64 | const MachineOperand &MO1 = MI->getOperand(opNum); |
| 65 | const MachineOperand &MO2 = MI->getOperand(opNum + 1); |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 66 | assert(MO1.isImmediate()); |
Rafael Espindola | f3a335c | 2006-08-17 17:09:40 +0000 | [diff] [blame] | 67 | bool arith = false; |
| 68 | if (Modifier != NULL) { |
| 69 | assert(strcmp(Modifier, "arith") == 0); |
| 70 | arith = true; |
| 71 | } |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 72 | |
| 73 | if (MO2.isConstantPoolIndex()) { |
Rafael Espindola | f3a335c | 2006-08-17 17:09:40 +0000 | [diff] [blame] | 74 | printOperand(MI, opNum + 1); |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 75 | } else if (MO2.isRegister()) { |
Rafael Espindola | f3a335c | 2006-08-17 17:09:40 +0000 | [diff] [blame] | 76 | if(!arith) |
| 77 | O << '['; |
| 78 | printOperand(MI, opNum + 1); |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 79 | O << ", "; |
Rafael Espindola | f3a335c | 2006-08-17 17:09:40 +0000 | [diff] [blame] | 80 | printOperand(MI, opNum); |
| 81 | if(!arith) |
| 82 | O << ']'; |
Rafael Espindola | 1ed3af1 | 2006-08-01 18:53:10 +0000 | [diff] [blame] | 83 | } else { |
| 84 | assert(0 && "Invalid Operand Type"); |
| 85 | } |
Rafael Espindola | a4e6435 | 2006-07-11 11:36:48 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 88 | void printOperand(const MachineInstr *MI, int opNum); |
| 89 | void printMemOperand(const MachineInstr *MI, int opNum, |
| 90 | const char *Modifier = 0); |
| 91 | void printCCOperand(const MachineInstr *MI, int opNum); |
| 92 | |
| 93 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
| 94 | bool runOnMachineFunction(MachineFunction &F); |
| 95 | bool doInitialization(Module &M); |
| 96 | bool doFinalization(Module &M); |
| 97 | }; |
| 98 | } // end of anonymous namespace |
| 99 | |
| 100 | #include "ARMGenAsmWriter.inc" |
| 101 | |
| 102 | /// createARMCodePrinterPass - Returns a pass that prints the ARM |
| 103 | /// assembly code for a MachineFunction to the given output stream, |
| 104 | /// using the given target machine description. This should work |
| 105 | /// regardless of whether the function is in SSA form. |
| 106 | /// |
| 107 | FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o, |
| 108 | TargetMachine &tm) { |
| 109 | return new ARMAsmPrinter(o, tm); |
| 110 | } |
| 111 | |
| 112 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 113 | /// method to print assembly for each instruction. |
| 114 | /// |
| 115 | bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 116 | SetupMachineFunction(MF); |
| 117 | O << "\n\n"; |
| 118 | |
| 119 | // Print out constants referenced by the function |
| 120 | EmitConstantPool(MF.getConstantPool()); |
| 121 | |
| 122 | // Print out jump tables referenced by the function |
| 123 | EmitJumpTableInfo(MF.getJumpTableInfo()); |
| 124 | |
| 125 | // Print out labels for the function. |
| 126 | const Function *F = MF.getFunction(); |
| 127 | switch (F->getLinkage()) { |
| 128 | default: assert(0 && "Unknown linkage type!"); |
| 129 | case Function::InternalLinkage: |
| 130 | SwitchToTextSection("\t.text", F); |
| 131 | break; |
| 132 | case Function::ExternalLinkage: |
| 133 | SwitchToTextSection("\t.text", F); |
| 134 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 135 | break; |
| 136 | case Function::WeakLinkage: |
| 137 | case Function::LinkOnceLinkage: |
| 138 | assert(0 && "Not implemented"); |
| 139 | break; |
| 140 | } |
Rafael Espindola | a1334cd | 2006-05-26 10:56:17 +0000 | [diff] [blame] | 141 | EmitAlignment(2, F); |
Rafael Espindola | 4b442b5 | 2006-05-23 02:48:20 +0000 | [diff] [blame] | 142 | O << CurrentFnName << ":\n"; |
| 143 | |
| 144 | // Print out code for the function. |
| 145 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 146 | I != E; ++I) { |
| 147 | // Print a label for the basic block. |
| 148 | if (I != MF.begin()) { |
| 149 | printBasicBlockLabel(I, true); |
| 150 | O << '\n'; |
| 151 | } |
| 152 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 153 | II != E; ++II) { |
| 154 | // Print the assembly for the instruction. |
| 155 | O << "\t"; |
| 156 | printInstruction(II); |
| 157 | } |
| 158 | } |
| 159 | |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Rafael Espindola | 2f99b6b | 2006-05-25 12:57:06 +0000 | [diff] [blame] | 164 | const MachineOperand &MO = MI->getOperand (opNum); |
| 165 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 166 | switch (MO.getType()) { |
| 167 | case MachineOperand::MO_Register: |
| 168 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 169 | O << LowercaseString (RI.get(MO.getReg()).Name); |
| 170 | else |
| 171 | assert(0 && "not implemented"); |
| 172 | break; |
| 173 | case MachineOperand::MO_Immediate: |
| 174 | O << "#" << (int)MO.getImmedValue(); |
| 175 | break; |
| 176 | case MachineOperand::MO_MachineBasicBlock: |
Rafael Espindola | 687bc49 | 2006-08-24 13:45:55 +0000 | [diff] [blame] | 177 | printBasicBlockLabel(MO.getMachineBasicBlock()); |
Rafael Espindola | 2f99b6b | 2006-05-25 12:57:06 +0000 | [diff] [blame] | 178 | return; |
Rafael Espindola | 84b19be | 2006-07-16 01:02:57 +0000 | [diff] [blame] | 179 | case MachineOperand::MO_GlobalAddress: { |
| 180 | GlobalValue *GV = MO.getGlobal(); |
| 181 | std::string Name = Mang->getValueName(GV); |
| 182 | O << Name; |
| 183 | } |
Rafael Espindola | 2f99b6b | 2006-05-25 12:57:06 +0000 | [diff] [blame] | 184 | break; |
| 185 | case MachineOperand::MO_ExternalSymbol: |
| 186 | assert(0 && "not implemented"); |
| 187 | abort(); |
| 188 | break; |
| 189 | case MachineOperand::MO_ConstantPoolIndex: |
Rafael Espindola | 06c1e7e | 2006-08-01 12:58:43 +0000 | [diff] [blame] | 190 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() |
| 191 | << '_' << MO.getConstantPoolIndex(); |
Rafael Espindola | 2f99b6b | 2006-05-25 12:57:06 +0000 | [diff] [blame] | 192 | break; |
| 193 | default: |
| 194 | O << "<unknown operand type>"; abort (); break; |
| 195 | } |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void ARMAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 199 | const char *Modifier) { |
| 200 | assert(0 && "not implemented"); |
| 201 | } |
| 202 | |
| 203 | void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { |
Rafael Espindola | 6f602de | 2006-08-24 16:13:15 +0000 | [diff] [blame^] | 204 | int CC = (int)MI->getOperand(opNum).getImmedValue(); |
| 205 | O << ARMCondCodeToString((ARMCC::CondCodes)CC); |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool ARMAsmPrinter::doInitialization(Module &M) { |
| 209 | Mang = new Mangler(M); |
| 210 | return false; // success |
| 211 | } |
| 212 | |
| 213 | bool ARMAsmPrinter::doFinalization(Module &M) { |
Rafael Espindola | b01c4bb | 2006-07-27 11:38:51 +0000 | [diff] [blame] | 214 | const TargetData *TD = TM.getTargetData(); |
| 215 | |
| 216 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 217 | I != E; ++I) { |
| 218 | if (!I->hasInitializer()) // External global require no code |
| 219 | continue; |
| 220 | |
| 221 | if (EmitSpecialLLVMGlobal(I)) |
| 222 | continue; |
| 223 | |
| 224 | O << "\n\n"; |
| 225 | std::string name = Mang->getValueName(I); |
| 226 | Constant *C = I->getInitializer(); |
| 227 | unsigned Size = TD->getTypeSize(C->getType()); |
| 228 | unsigned Align = TD->getTypeAlignment(C->getType()); |
| 229 | |
Rafael Espindola | 6d581e8 | 2006-07-31 20:38:13 +0000 | [diff] [blame] | 230 | switch (I->getLinkage()) { |
| 231 | default: |
| 232 | assert(0 && "Unknown linkage type!"); |
| 233 | break; |
| 234 | case GlobalValue::ExternalLinkage: |
| 235 | O << "\t.globl " << name << "\n"; |
| 236 | break; |
| 237 | case GlobalValue::InternalLinkage: |
| 238 | break; |
| 239 | } |
Rafael Espindola | b01c4bb | 2006-07-27 11:38:51 +0000 | [diff] [blame] | 240 | |
| 241 | assert (!C->isNullValue()); |
| 242 | SwitchToDataSection(".data", I); |
| 243 | |
| 244 | EmitAlignment(Align, I); |
| 245 | O << "\t.type " << name << ", %object\n"; |
| 246 | O << "\t.size " << name << ", " << Size << "\n"; |
| 247 | O << name << ":\n"; |
| 248 | EmitGlobalConstant(C); |
| 249 | } |
Rafael Espindola | 7bc59bc | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 250 | AsmPrinter::doFinalization(M); |
| 251 | return false; // success |
| 252 | } |