Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 1 | //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to GAS-format Sparc V8 assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SparcV8.h" |
| 16 | #include "SparcV8InstrInfo.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/AsmPrinter.h" |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineInstr.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | #include "llvm/Support/Mangler.h" |
Brian Gaeke | 74dfcf1 | 2004-09-02 02:37:43 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
| 28 | #include "llvm/ADT/StringExtras.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
Jim Laskey | b8df7c2 | 2005-08-17 20:04:34 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 31 | #include <cctype> |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 36 | |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 37 | struct SparcV8AsmPrinter : public AsmPrinter { |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame] | 38 | SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { |
| 39 | Data16bitsDirective = "\t.half\t"; |
| 40 | Data32bitsDirective = "\t.word\t"; |
| 41 | Data64bitsDirective = "\t.xword\t"; |
Chris Lattner | 967abf3 | 2005-12-17 07:17:08 +0000 | [diff] [blame] | 42 | ZeroDirective = 0; // no .zero or .space! |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame] | 43 | } |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 44 | |
| 45 | /// We name each basic block in a Function with a unique number, so |
| 46 | /// that we can consistently refer to them later. This is cleared |
| 47 | /// at the beginning of each call to runOnMachineFunction(). |
| 48 | /// |
| 49 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 50 | ValueMapTy NumberForBB; |
| 51 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 52 | virtual const char *getPassName() const { |
| 53 | return "SparcV8 Assembly Printer"; |
| 54 | } |
| 55 | |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 56 | void printOperand(const MachineInstr *MI, int opNum); |
Chris Lattner | bc83fd9 | 2005-12-17 20:04:49 +0000 | [diff] [blame] | 57 | void printMemOperand(const MachineInstr *MI, int opNum); |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 58 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 59 | bool runOnMachineFunction(MachineFunction &F); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 60 | bool doInitialization(Module &M); |
| 61 | bool doFinalization(Module &M); |
| 62 | }; |
| 63 | } // end of anonymous namespace |
| 64 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 65 | #include "SparcV8GenAsmWriter.inc" |
| 66 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 67 | /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 |
| 68 | /// assembly code for a MachineFunction to the given output stream, |
| 69 | /// using the given target machine description. This should work |
| 70 | /// regardless of whether the function is in SSA form. |
| 71 | /// |
| 72 | FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, |
| 73 | TargetMachine &tm) { |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 74 | return new SparcV8AsmPrinter(o, tm); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 77 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 78 | /// method to print assembly for each instruction. |
| 79 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 80 | bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 81 | SetupMachineFunction(MF); |
| 82 | |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame] | 83 | // Print out constants referenced by the function |
| 84 | EmitConstantPool(MF.getConstantPool()); |
| 85 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 86 | // BBNumber is used here so that a given Printer will never give two |
| 87 | // BBs the same name. (If you have a better way, please let me know!) |
| 88 | static unsigned BBNumber = 0; |
| 89 | |
| 90 | O << "\n\n"; |
| 91 | // What's my mangled name? |
| 92 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 93 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 94 | // Print out labels for the function. |
| 95 | O << "\t.text\n"; |
| 96 | O << "\t.align 16\n"; |
| 97 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 98 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 99 | O << CurrentFnName << ":\n"; |
| 100 | |
| 101 | // Number each basic block so that we can consistently refer to them |
| 102 | // in PC-relative references. |
| 103 | NumberForBB.clear(); |
| 104 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 105 | I != E; ++I) { |
| 106 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 107 | } |
| 108 | |
| 109 | // Print out code for the function. |
| 110 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 111 | I != E; ++I) { |
| 112 | // Print a label for the basic block. |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 113 | O << ".LBB" << Mang->getValueName(MF.getFunction ()) |
| 114 | << "_" << I->getNumber () << ":\t! " |
| 115 | << I->getBasicBlock ()->getName () << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 116 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 117 | II != E; ++II) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 118 | // Print the assembly for the instruction. |
Chris Lattner | 0d8fcd3 | 2005-12-17 06:54:41 +0000 | [diff] [blame] | 119 | O << "\t"; |
| 120 | printInstruction(II); |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 121 | ++EmittedInsts; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | // We didn't modify anything. |
| 126 | return false; |
| 127 | } |
| 128 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 129 | void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 130 | const MachineOperand &MO = MI->getOperand (opNum); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 131 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 132 | bool CloseParen = false; |
| 133 | if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { |
| 134 | O << "%hi("; |
| 135 | CloseParen = true; |
Misha Brukman | f54ef97 | 2004-06-24 23:38:20 +0000 | [diff] [blame] | 136 | } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate()) |
| 137 | { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 138 | O << "%lo("; |
| 139 | CloseParen = true; |
| 140 | } |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 141 | switch (MO.getType()) { |
| 142 | case MachineOperand::MO_VirtualRegister: |
| 143 | if (Value *V = MO.getVRegValueOrNull()) { |
| 144 | O << "<" << V->getName() << ">"; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 145 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 146 | } |
| 147 | // FALLTHROUGH |
| 148 | case MachineOperand::MO_MachineRegister: |
| 149 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 150 | O << "%" << LowercaseString (RI.get(MO.getReg()).Name); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 151 | else |
| 152 | O << "%reg" << MO.getReg(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 153 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 154 | |
| 155 | case MachineOperand::MO_SignExtendedImmed: |
| 156 | case MachineOperand::MO_UnextendedImmed: |
| 157 | O << (int)MO.getImmedValue(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 158 | break; |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 159 | case MachineOperand::MO_MachineBasicBlock: { |
| 160 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 161 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 162 | << "_" << MBBOp->getNumber () << "\t! " |
| 163 | << MBBOp->getBasicBlock ()->getName (); |
| 164 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 165 | } |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 166 | case MachineOperand::MO_PCRelativeDisp: |
| 167 | std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; |
| 168 | abort (); |
| 169 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 170 | case MachineOperand::MO_GlobalAddress: |
| 171 | O << Mang->getValueName(MO.getGlobal()); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 172 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 173 | case MachineOperand::MO_ExternalSymbol: |
| 174 | O << MO.getSymbolName(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 175 | break; |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame] | 176 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame] | 177 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" |
| 178 | << MO.getConstantPoolIndex(); |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame] | 179 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 180 | default: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 181 | O << "<unknown operand type>"; abort (); break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 182 | } |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 183 | if (CloseParen) O << ")"; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | bc83fd9 | 2005-12-17 20:04:49 +0000 | [diff] [blame] | 186 | void SparcV8AsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) { |
| 187 | printOperand(MI, opNum); |
| 188 | O << "+"; |
Chris Lattner | e1389ad | 2005-12-18 02:27:00 +0000 | [diff] [blame^] | 189 | if (MI->getOperand(opNum+1).getType() == MachineOperand::MO_GlobalAddress) { |
| 190 | O << "%lo("; |
| 191 | printOperand(MI, opNum+1); |
| 192 | O << ")"; |
| 193 | } else { |
| 194 | printOperand(MI, opNum+1); |
| 195 | } |
Chris Lattner | bc83fd9 | 2005-12-17 20:04:49 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 199 | bool SparcV8AsmPrinter::doInitialization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 200 | Mang = new Mangler(M); |
| 201 | return false; // success |
| 202 | } |
| 203 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 204 | bool SparcV8AsmPrinter::doFinalization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 205 | const TargetData &TD = TM.getTargetData(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 206 | |
| 207 | // Print out module-level global variables here. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 208 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 209 | if (I->hasInitializer()) { // External global require no code |
| 210 | O << "\n\n"; |
| 211 | std::string name = Mang->getValueName(I); |
| 212 | Constant *C = I->getInitializer(); |
| 213 | unsigned Size = TD.getTypeSize(C->getType()); |
| 214 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 215 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 216 | if (C->isNullValue() && |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 217 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 218 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 219 | SwitchSection(".data", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 220 | if (I->hasInternalLinkage()) |
| 221 | O << "\t.local " << name << "\n"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 222 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 223 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 224 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 225 | O << "\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 226 | WriteAsOperand(O, I, true, true, &M); |
| 227 | O << "\n"; |
| 228 | } else { |
| 229 | switch (I->getLinkage()) { |
| 230 | case GlobalValue::LinkOnceLinkage: |
| 231 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 232 | // Nonnull linkonce -> weak |
| 233 | O << "\t.weak " << name << "\n"; |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 234 | SwitchSection("", I); |
| 235 | O << "\t.section\t\".llvm.linkonce.d." << name |
| 236 | << "\",\"aw\",@progbits\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 237 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 238 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 239 | case GlobalValue::AppendingLinkage: |
| 240 | // FIXME: appending linkage variables should go into a section of |
| 241 | // their name or something. For now, just emit them as external. |
| 242 | case GlobalValue::ExternalLinkage: |
| 243 | // If external or appending, declare as a global symbol |
| 244 | O << "\t.globl " << name << "\n"; |
| 245 | // FALL THROUGH |
| 246 | case GlobalValue::InternalLinkage: |
| 247 | if (C->isNullValue()) |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 248 | SwitchSection(".bss", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 249 | else |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 250 | SwitchSection(".data", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 251 | break; |
Misha Brukman | c11c44f | 2004-11-19 21:49:19 +0000 | [diff] [blame] | 252 | case GlobalValue::GhostLinkage: |
| 253 | std::cerr << "Should not have any unmaterialized functions!\n"; |
| 254 | abort(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | O << "\t.align " << Align << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 258 | O << "\t.type " << name << ",#object\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 259 | O << "\t.size " << name << "," << Size << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 260 | O << name << ":\t\t\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 261 | WriteAsOperand(O, I, true, true, &M); |
| 262 | O << " = "; |
| 263 | WriteAsOperand(O, C, false, false, &M); |
| 264 | O << "\n"; |
Chris Lattner | 967abf3 | 2005-12-17 07:17:08 +0000 | [diff] [blame] | 265 | EmitGlobalConstant(C); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 269 | AsmPrinter::doFinalization(M); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 270 | return false; // success |
| 271 | } |