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