Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 1 | //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===// |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 2 | // |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Duraid Madina and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 7 | // |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to assembly accepted by the GNU binutils 'gas' |
| 12 | // assembler. The Intel 'ias' and HP-UX 'as' assemblers *may* choke on this |
| 13 | // output, but if so that's a bug I'd like to hear about: please file a bug |
Duraid Madina | f2db9b8 | 2005-10-28 17:46:35 +0000 | [diff] [blame] | 14 | // report in bugzilla. FYI, the not too bad 'ias' assembler is bundled with |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 15 | // the Intel C/C++ compiler for Itanium Linux. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "IA64.h" |
| 20 | #include "IA64TargetMachine.h" |
| 21 | #include "llvm/Module.h" |
Chris Lattner | 3c61f70 | 2005-03-24 05:12:48 +0000 | [diff] [blame] | 22 | #include "llvm/Type.h" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 23 | #include "llvm/Assembly/Writer.h" |
| 24 | #include "llvm/CodeGen/AsmPrinter.h" |
| 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/ValueTypes.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Support/Mangler.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 36 | |
| 37 | struct IA64SharedAsmPrinter : public AsmPrinter { |
| 38 | |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 39 | std::set<std::string> ExternalFunctionNames, ExternalObjectNames; |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 40 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 41 | IA64SharedAsmPrinter(std::ostream &O, TargetMachine &TM) |
| 42 | : AsmPrinter(O, TM) { } |
| 43 | |
| 44 | void printConstantPool(MachineConstantPool *MCP); |
| 45 | bool doFinalization(Module &M); |
| 46 | }; |
| 47 | } |
| 48 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 49 | /// printConstantPool - Print to the current output stream assembly |
| 50 | /// representations of the constants in the constant pool MCP. This is |
| 51 | /// used to print out constants which have been "spilled to memory" by |
| 52 | /// the code generator. |
| 53 | /// |
| 54 | void IA64SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
| 55 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 56 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 57 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 58 | if (CP.empty()) return; |
| 59 | |
Chris Lattner | 4bfa3a3 | 2005-11-21 07:26:04 +0000 | [diff] [blame] | 60 | // FIXME: would be nice to have rodata (no 'w') when appropriate? |
| 61 | SwitchSection("\n\t.section .data, \"aw\", \"progbits\"\n", 0); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 63 | EmitAlignment(TD.getTypeAlignmentShift(CP[i]->getType())); |
Chris Lattner | 81a994e | 2005-11-21 06:51:52 +0000 | [diff] [blame] | 64 | O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i |
| 65 | << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n"; |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 66 | EmitGlobalConstant(CP[i]); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | bool IA64SharedAsmPrinter::doFinalization(Module &M) { |
| 71 | const TargetData &TD = TM.getTargetData(); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 72 | |
| 73 | // Print out module-level global variables here. |
Alkis Evlogimenos | 12cf385 | 2005-03-19 09:22:17 +0000 | [diff] [blame] | 74 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 75 | I != E; ++I) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 76 | if (I->hasInitializer()) { // External global require no code |
| 77 | O << "\n\n"; |
| 78 | std::string name = Mang->getValueName(I); |
| 79 | Constant *C = I->getInitializer(); |
| 80 | unsigned Size = TD.getTypeSize(C->getType()); |
| 81 | unsigned Align = TD.getTypeAlignmentShift(C->getType()); |
| 82 | |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 83 | if (C->isNullValue() && |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 84 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 85 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
Chris Lattner | 4bfa3a3 | 2005-11-21 07:26:04 +0000 | [diff] [blame] | 86 | SwitchSection(".data", I); |
Duraid Madina | 1f867b1 | 2005-03-31 07:40:24 +0000 | [diff] [blame] | 87 | if (I->hasInternalLinkage()) { |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 88 | O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType()) |
| 89 | << "," << (1 << Align); |
| 90 | O << "\t\t// "; |
| 91 | } else { |
| 92 | O << "\t.common " << name << "," << TD.getTypeSize(C->getType()) |
| 93 | << "," << (1 << Align); |
| 94 | O << "\t\t// "; |
| 95 | } |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 96 | WriteAsOperand(O, I, true, true, &M); |
| 97 | O << "\n"; |
| 98 | } else { |
| 99 | switch (I->getLinkage()) { |
| 100 | case GlobalValue::LinkOnceLinkage: |
| 101 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 102 | // Nonnull linkonce -> weak |
| 103 | O << "\t.weak " << name << "\n"; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 104 | O << "\t.section\t.llvm.linkonce.d." << name |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 105 | << ", \"aw\", \"progbits\"\n"; |
Chris Lattner | 4bfa3a3 | 2005-11-21 07:26:04 +0000 | [diff] [blame] | 106 | SwitchSection("", I); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 107 | break; |
| 108 | case GlobalValue::AppendingLinkage: |
| 109 | // FIXME: appending linkage variables should go into a section of |
| 110 | // their name or something. For now, just emit them as external. |
| 111 | case GlobalValue::ExternalLinkage: |
| 112 | // If external or appending, declare as a global symbol |
| 113 | O << "\t.global " << name << "\n"; |
| 114 | // FALL THROUGH |
| 115 | case GlobalValue::InternalLinkage: |
Chris Lattner | 4bfa3a3 | 2005-11-21 07:26:04 +0000 | [diff] [blame] | 116 | SwitchSection(C->isNullValue() ? ".bss" : ".data", I); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 117 | break; |
| 118 | case GlobalValue::GhostLinkage: |
| 119 | std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n"; |
| 120 | abort(); |
| 121 | } |
| 122 | |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 123 | EmitAlignment(Align); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 124 | O << "\t.type " << name << ",@object\n"; |
| 125 | O << "\t.size " << name << "," << Size << "\n"; |
| 126 | O << name << ":\t\t\t\t// "; |
| 127 | WriteAsOperand(O, I, true, true, &M); |
| 128 | O << " = "; |
| 129 | WriteAsOperand(O, C, false, false, &M); |
| 130 | O << "\n"; |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 131 | EmitGlobalConstant(C); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | // we print out ".global X \n .type X, @function" for each external function |
| 136 | O << "\n\n// br.call targets referenced (and not defined) above: \n"; |
| 137 | for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(), |
| 138 | e = ExternalFunctionNames.end(); i!=e; ++i) { |
| 139 | O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n"; |
| 140 | } |
| 141 | O << "\n\n"; |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 142 | |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 143 | // we print out ".global X \n .type X, @object" for each external object |
| 144 | O << "\n\n// (external) symbols referenced (and not defined) above: \n"; |
| 145 | for (std::set<std::string>::iterator i = ExternalObjectNames.begin(), |
| 146 | e = ExternalObjectNames.end(); i!=e; ++i) { |
| 147 | O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n"; |
| 148 | } |
| 149 | O << "\n\n"; |
| 150 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 151 | AsmPrinter::doFinalization(M); |
| 152 | return false; // success |
| 153 | } |
| 154 | |
| 155 | namespace { |
| 156 | struct IA64AsmPrinter : public IA64SharedAsmPrinter { |
| 157 | IA64AsmPrinter(std::ostream &O, TargetMachine &TM) |
| 158 | : IA64SharedAsmPrinter(O, TM) { |
| 159 | |
| 160 | CommentString = "//"; |
Duraid Madina | 32c46f3 | 2005-04-02 12:30:47 +0000 | [diff] [blame] | 161 | Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are |
| 162 | Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment |
| 163 | Data32bitsDirective = "\tdata4.ua\t"; // properly |
| 164 | Data64bitsDirective = "\tdata8.ua\t"; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 165 | ZeroDirective = "\t.skip\t"; |
| 166 | AsciiDirective = "\tstring\t"; |
| 167 | |
Duraid Madina | 32c46f3 | 2005-04-02 12:30:47 +0000 | [diff] [blame] | 168 | GlobalVarAddrPrefix=""; |
| 169 | GlobalVarAddrSuffix=""; |
| 170 | FunctionAddrPrefix="@fptr("; |
| 171 | FunctionAddrSuffix=")"; |
| 172 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | virtual const char *getPassName() const { |
| 176 | return "IA64 Assembly Printer"; |
| 177 | } |
| 178 | |
| 179 | /// printInstruction - This method is automatically generated by tablegen |
| 180 | /// from the instruction set description. This method returns true if the |
| 181 | /// machine instruction was sufficiently described to print it, otherwise it |
| 182 | /// returns false. |
| 183 | bool printInstruction(const MachineInstr *MI); |
| 184 | |
| 185 | // This method is used by the tablegen'erated instruction printer. |
| 186 | void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){ |
| 187 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 188 | if (MO.getType() == MachineOperand::MO_MachineRegister) { |
| 189 | assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??"); |
| 190 | //XXX Bug Workaround: See note in Printer::doInitialization about %. |
| 191 | O << TM.getRegisterInfo()->get(MO.getReg()).Name; |
| 192 | } else { |
| 193 | printOp(MO); |
| 194 | } |
| 195 | } |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 196 | |
Duraid Madina | e6a0b6c | 2005-04-07 12:34:36 +0000 | [diff] [blame] | 197 | void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 198 | MVT::ValueType VT) { |
| 199 | int val=(unsigned int)MI->getOperand(OpNo).getImmedValue(); |
| 200 | if(val>=128) val=val-256; // if negative, flip sign |
| 201 | O << val; |
| 202 | } |
| 203 | void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 204 | MVT::ValueType VT) { |
| 205 | int val=(unsigned int)MI->getOperand(OpNo).getImmedValue(); |
| 206 | if(val>=8192) val=val-16384; // if negative, flip sign |
| 207 | O << val; |
| 208 | } |
Duraid Madina | 5ef2ec9 | 2005-04-11 05:55:56 +0000 | [diff] [blame] | 209 | void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo, |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 210 | MVT::ValueType VT) { |
Duraid Madina | 5ef2ec9 | 2005-04-11 05:55:56 +0000 | [diff] [blame] | 211 | int val=(unsigned int)MI->getOperand(OpNo).getImmedValue(); |
| 212 | if(val>=2097152) val=val-4194304; // if negative, flip sign |
| 213 | O << val; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 214 | } |
| 215 | void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 216 | MVT::ValueType VT) { |
| 217 | O << (uint64_t)MI->getOperand(OpNo).getImmedValue(); |
| 218 | } |
Duraid Madina | 1ce0c01 | 2005-04-14 10:08:01 +0000 | [diff] [blame] | 219 | void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo, |
| 220 | MVT::ValueType VT) { |
Duraid Madina | f2db9b8 | 2005-10-28 17:46:35 +0000 | [diff] [blame] | 221 | // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker |
| 222 | // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now |
| 223 | // emit movl rX = @gprel(CPI<whatever);; |
| 224 | // add rX = rX, r1; |
| 225 | // this gives us 64 bits instead of 22 (for the add long imm) to play |
| 226 | // with, which shuts up the linker. The problem is that the constant |
| 227 | // pool entries aren't immediates at this stage, so we check here. |
| 228 | // If it's an immediate, print it the old fashioned way. If it's |
| 229 | // not, we print it as a constant pool index. |
| 230 | if(MI->getOperand(OpNo).isImmediate()) { |
| 231 | O << (int64_t)MI->getOperand(OpNo).getImmedValue(); |
| 232 | } else { // this is a constant pool reference: FIXME: assert this |
| 233 | printOp(MI->getOperand(OpNo)); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void printGlobalOperand(const MachineInstr *MI, unsigned OpNo, |
| 238 | MVT::ValueType VT) { |
| 239 | printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction |
Duraid Madina | 1ce0c01 | 2005-04-14 10:08:01 +0000 | [diff] [blame] | 240 | } |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 241 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 242 | void printCallOperand(const MachineInstr *MI, unsigned OpNo, |
| 243 | MVT::ValueType VT) { |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 244 | printOp(MI->getOperand(OpNo), true); // this is a br.call instruction |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void printMachineInstruction(const MachineInstr *MI); |
| 248 | void printOp(const MachineOperand &MO, bool isBRCALLinsn= false); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 249 | bool runOnMachineFunction(MachineFunction &F); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 250 | bool doInitialization(Module &M); |
| 251 | }; |
| 252 | } // end of anonymous namespace |
| 253 | |
| 254 | |
| 255 | // Include the auto-generated portion of the assembly writer. |
| 256 | #include "IA64GenAsmWriter.inc" |
| 257 | |
| 258 | |
| 259 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 260 | /// method to print assembly for each instruction. |
| 261 | /// |
| 262 | bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 263 | SetupMachineFunction(MF); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 264 | O << "\n\n"; |
| 265 | |
| 266 | // Print out constants referenced by the function |
| 267 | printConstantPool(MF.getConstantPool()); |
| 268 | |
| 269 | // Print out labels for the function. |
Chris Lattner | 4bfa3a3 | 2005-11-21 07:26:04 +0000 | [diff] [blame] | 270 | SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction()); |
| 271 | // ^^ means "Allocated instruXions in mem, initialized" |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame^] | 272 | EmitAlignment(5); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 273 | O << "\t.global\t" << CurrentFnName << "\n"; |
| 274 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 275 | O << CurrentFnName << ":\n"; |
| 276 | |
| 277 | // Print out code for the function. |
| 278 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 279 | I != E; ++I) { |
| 280 | // Print a label for the basic block if there are any predecessors. |
| 281 | if (I->pred_begin() != I->pred_end()) |
Chris Lattner | 2ec30b5 | 2005-11-21 07:39:22 +0000 | [diff] [blame] | 282 | O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" |
| 283 | << I->getNumber() << ":\t" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 284 | << CommentString << " " << I->getBasicBlock()->getName() << "\n"; |
| 285 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 286 | II != E; ++II) { |
| 287 | // Print the assembly for the instruction. |
| 288 | O << "\t"; |
| 289 | printMachineInstruction(II); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // We didn't modify anything. |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | void IA64AsmPrinter::printOp(const MachineOperand &MO, |
| 298 | bool isBRCALLinsn /* = false */) { |
| 299 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 300 | switch (MO.getType()) { |
| 301 | case MachineOperand::MO_VirtualRegister: |
| 302 | if (Value *V = MO.getVRegValueOrNull()) { |
| 303 | O << "<" << V->getName() << ">"; |
| 304 | return; |
| 305 | } |
| 306 | // FALLTHROUGH |
| 307 | case MachineOperand::MO_MachineRegister: |
| 308 | case MachineOperand::MO_CCRegister: { |
| 309 | O << RI.get(MO.getReg()).Name; |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | case MachineOperand::MO_SignExtendedImmed: |
| 314 | case MachineOperand::MO_UnextendedImmed: |
| 315 | O << /*(unsigned int)*/MO.getImmedValue(); |
| 316 | return; |
| 317 | case MachineOperand::MO_MachineBasicBlock: { |
| 318 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
Chris Lattner | 2ec30b5 | 2005-11-21 07:39:22 +0000 | [diff] [blame] | 319 | O << PrivateGlobalPrefix << "LBB" |
| 320 | << Mang->getValueName(MBBOp->getParent()->getFunction()) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 321 | << "_" << MBBOp->getNumber () << "\t// " |
| 322 | << MBBOp->getBasicBlock ()->getName (); |
| 323 | return; |
| 324 | } |
| 325 | case MachineOperand::MO_PCRelativeDisp: |
| 326 | std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs"; |
| 327 | abort (); |
| 328 | return; |
| 329 | |
| 330 | case MachineOperand::MO_ConstantPoolIndex: { |
Chris Lattner | 81a994e | 2005-11-21 06:51:52 +0000 | [diff] [blame] | 331 | O << "@gprel(" << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 332 | << MO.getConstantPoolIndex() << ")"; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | case MachineOperand::MO_GlobalAddress: { |
| 337 | |
| 338 | // functions need @ltoff(@fptr(fn_name)) form |
| 339 | GlobalValue *GV = MO.getGlobal(); |
| 340 | Function *F = dyn_cast<Function>(GV); |
| 341 | |
| 342 | bool Needfptr=false; // if we're computing an address @ltoff(X), do |
| 343 | // we need to decorate it so it becomes |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 344 | // @ltoff(@fptr(X)) ? |
| 345 | if (F && !isBRCALLinsn /*&& F->isExternal()*/) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 346 | Needfptr=true; |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 347 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 348 | // if this is the target of a call instruction, we should define |
| 349 | // the function somewhere (GNU gas has no problem without this, but |
| 350 | // Intel ias rightly complains of an 'undefined symbol') |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 351 | |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 352 | if (F /*&& isBRCALLinsn*/ && F->isExternal()) |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 353 | ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal())); |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 354 | else |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 355 | if (GV->isExternal()) // e.g. stuff like 'stdin' |
| 356 | ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal())); |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 357 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 358 | if (!isBRCALLinsn) |
| 359 | O << "@ltoff("; |
| 360 | if (Needfptr) |
| 361 | O << "@fptr("; |
| 362 | O << Mang->getValueName(MO.getGlobal()); |
| 363 | if (Needfptr) |
| 364 | O << ")"; // close fptr( |
| 365 | if (!isBRCALLinsn) |
| 366 | O << ")"; // close ltoff( |
| 367 | int Offset = MO.getOffset(); |
| 368 | if (Offset > 0) |
| 369 | O << " + " << Offset; |
| 370 | else if (Offset < 0) |
| 371 | O << " - " << -Offset; |
| 372 | return; |
| 373 | } |
| 374 | case MachineOperand::MO_ExternalSymbol: |
| 375 | O << MO.getSymbolName(); |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 376 | ExternalFunctionNames.insert(MO.getSymbolName()); |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 377 | return; |
| 378 | default: |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 379 | O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return; |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | /// printMachineInstruction -- Print out a single IA64 LLVM instruction |
| 384 | /// MI to the current output stream. |
| 385 | /// |
| 386 | void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 387 | ++EmittedInsts; |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 388 | |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 389 | // Call the autogenerated instruction printer routines. |
| 390 | printInstruction(MI); |
| 391 | } |
| 392 | |
| 393 | bool IA64AsmPrinter::doInitialization(Module &M) { |
| 394 | AsmPrinter::doInitialization(M); |
Misha Brukman | 4633f1c | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 395 | |
Duraid Madina | ae8bd73 | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 396 | O << "\n.ident \"LLVM-ia64\"\n\n" |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 397 | << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 398 | << "\t.radix C\n" |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 399 | << "\t.psr abi64\n"; // we only support 64 bits for now |
Duraid Madina | 9b9d45f | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 400 | return false; |
| 401 | } |
| 402 | |
| 403 | /// createIA64CodePrinterPass - Returns a pass that prints the IA64 |
| 404 | /// assembly code for a MachineFunction to the given output stream, using |
| 405 | /// the given target machine description. |
| 406 | /// |
| 407 | FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){ |
| 408 | return new IA64AsmPrinter(o, tm); |
| 409 | } |
| 410 | |
| 411 | |