| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 1 | //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===// | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 2 | // | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 7 | // | 
| Duraid Madina | 91ed0a1 | 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 | f221c26 | 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 | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 15 | // the Intel C/C++ compiler for Itanium Linux. | 
|  | 16 | // | 
|  | 17 | //===----------------------------------------------------------------------===// | 
|  | 18 |  | 
| Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "asm-printer" | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 20 | #include "IA64.h" | 
|  | 21 | #include "IA64TargetMachine.h" | 
|  | 22 | #include "llvm/Module.h" | 
| Chris Lattner | fed2a42 | 2005-03-24 05:12:48 +0000 | [diff] [blame] | 23 | #include "llvm/Type.h" | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/AsmPrinter.h" | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
| Jim Laskey | a6211dc | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmInfo.h" | 
| Jim Laskey | 261779b | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Mangler.h" | 
|  | 29 | #include "llvm/ADT/Statistic.h" | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 30 | using namespace llvm; | 
|  | 31 |  | 
| Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 32 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); | 
|  | 33 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 34 | namespace { | 
| Jim Laskey | a6211dc | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 35 | struct IA64AsmPrinter : public AsmPrinter { | 
|  | 36 | std::set<std::string> ExternalFunctionNames, ExternalObjectNames; | 
|  | 37 |  | 
| Jim Laskey | 261779b | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 38 | IA64AsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) | 
| Jim Laskey | a6211dc | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 39 | : AsmPrinter(O, TM, T) { | 
|  | 40 | } | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | virtual const char *getPassName() const { | 
|  | 43 | return "IA64 Assembly Printer"; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | /// printInstruction - This method is automatically generated by tablegen | 
|  | 47 | /// from the instruction set description.  This method returns true if the | 
|  | 48 | /// machine instruction was sufficiently described to print it, otherwise it | 
|  | 49 | /// returns false. | 
|  | 50 | bool printInstruction(const MachineInstr *MI); | 
|  | 51 |  | 
|  | 52 | // This method is used by the tablegen'erated instruction printer. | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 53 | void printOperand(const MachineInstr *MI, unsigned OpNo){ | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 54 | const MachineOperand &MO = MI->getOperand(OpNo); | 
| Chris Lattner | 10b71c0 | 2006-05-04 18:05:43 +0000 | [diff] [blame] | 55 | if (MO.getType() == MachineOperand::MO_Register) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 56 | assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && | 
|  | 57 | "Not physref??"); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 58 | //XXX Bug Workaround: See note in Printer::doInitialization about %. | 
|  | 59 | O << TM.getRegisterInfo()->get(MO.getReg()).Name; | 
|  | 60 | } else { | 
|  | 61 | printOp(MO); | 
|  | 62 | } | 
|  | 63 | } | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 64 |  | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 65 | void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 66 | int val=(unsigned int)MI->getOperand(OpNo).getImm(); | 
| Duraid Madina | a7abda3 | 2005-04-07 12:34:36 +0000 | [diff] [blame] | 67 | if(val>=128) val=val-256; // if negative, flip sign | 
|  | 68 | O << val; | 
|  | 69 | } | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 70 | void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 71 | int val=(unsigned int)MI->getOperand(OpNo).getImm(); | 
| Duraid Madina | a7abda3 | 2005-04-07 12:34:36 +0000 | [diff] [blame] | 72 | if(val>=8192) val=val-16384; // if negative, flip sign | 
|  | 73 | O << val; | 
|  | 74 | } | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 75 | void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 76 | int val=(unsigned int)MI->getOperand(OpNo).getImm(); | 
| Duraid Madina | fb43ef7 | 2005-04-11 05:55:56 +0000 | [diff] [blame] | 77 | if(val>=2097152) val=val-4194304; // if negative, flip sign | 
|  | 78 | O << val; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 79 | } | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 80 | void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 81 | O << (uint64_t)MI->getOperand(OpNo).getImm(); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 82 | } | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 83 | void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Duraid Madina | f221c26 | 2005-10-28 17:46:35 +0000 | [diff] [blame] | 84 | // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker | 
|  | 85 | // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now | 
|  | 86 | // emit movl rX = @gprel(CPI<whatever);; | 
|  | 87 | //      add  rX = rX, r1; | 
|  | 88 | // this gives us 64 bits instead of 22 (for the add long imm) to play | 
|  | 89 | // with, which shuts up the linker. The problem is that the constant | 
|  | 90 | // pool entries aren't immediates at this stage, so we check here. | 
|  | 91 | // If it's an immediate, print it the old fashioned way. If it's | 
|  | 92 | // not, we print it as a constant pool index. | 
|  | 93 | if(MI->getOperand(OpNo).isImmediate()) { | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 94 | O << (int64_t)MI->getOperand(OpNo).getImm(); | 
| Duraid Madina | f221c26 | 2005-10-28 17:46:35 +0000 | [diff] [blame] | 95 | } else { // this is a constant pool reference: FIXME: assert this | 
|  | 96 | printOp(MI->getOperand(OpNo)); | 
|  | 97 | } | 
|  | 98 | } | 
|  | 99 |  | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 100 | void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Duraid Madina | f221c26 | 2005-10-28 17:46:35 +0000 | [diff] [blame] | 101 | printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction | 
| Duraid Madina | 0a7c2b9 | 2005-04-14 10:08:01 +0000 | [diff] [blame] | 102 | } | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 103 |  | 
| Nate Begeman | 6f8c1ac | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 104 | void printCallOperand(const MachineInstr *MI, unsigned OpNo) { | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 105 | printOp(MI->getOperand(OpNo), true); // this is a br.call instruction | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Chris Lattner | 38e2c8a | 2006-10-05 02:51:36 +0000 | [diff] [blame] | 108 | std::string getSectionForFunction(const Function &F) const; | 
|  | 109 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 110 | void printMachineInstruction(const MachineInstr *MI); | 
|  | 111 | void printOp(const MachineOperand &MO, bool isBRCALLinsn= false); | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 112 | bool runOnMachineFunction(MachineFunction &F); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 113 | bool doInitialization(Module &M); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 114 | bool doFinalization(Module &M); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 115 | }; | 
|  | 116 | } // end of anonymous namespace | 
|  | 117 |  | 
|  | 118 |  | 
|  | 119 | // Include the auto-generated portion of the assembly writer. | 
|  | 120 | #include "IA64GenAsmWriter.inc" | 
|  | 121 |  | 
|  | 122 |  | 
| Chris Lattner | 38e2c8a | 2006-10-05 02:51:36 +0000 | [diff] [blame] | 123 | std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const { | 
|  | 124 | // This means "Allocated instruXions in mem, initialized". | 
|  | 125 | return "\n\t.section .text, \"ax\", \"progbits\"\n"; | 
|  | 126 | } | 
|  | 127 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 128 | /// runOnMachineFunction - This uses the printMachineInstruction() | 
|  | 129 | /// method to print assembly for each instruction. | 
|  | 130 | /// | 
|  | 131 | bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) { | 
| Chris Lattner | 99946fb | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 132 | SetupMachineFunction(MF); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 133 | O << "\n\n"; | 
|  | 134 |  | 
|  | 135 | // Print out constants referenced by the function | 
| Chris Lattner | b55de47 | 2005-11-21 08:38:26 +0000 | [diff] [blame] | 136 | EmitConstantPool(MF.getConstantPool()); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 137 |  | 
| Chris Lattner | 38e2c8a | 2006-10-05 02:51:36 +0000 | [diff] [blame] | 138 | const Function *F = MF.getFunction(); | 
|  | 139 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); | 
|  | 140 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 141 | // Print out labels for the function. | 
| Chris Lattner | 99946fb | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 142 | EmitAlignment(5); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 143 | O << "\t.global\t" << CurrentFnName << "\n"; | 
|  | 144 | O << "\t.type\t" << CurrentFnName << ", @function\n"; | 
|  | 145 | O << CurrentFnName << ":\n"; | 
|  | 146 |  | 
|  | 147 | // Print out code for the function. | 
|  | 148 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); | 
|  | 149 | I != E; ++I) { | 
|  | 150 | // Print a label for the basic block if there are any predecessors. | 
| Dan Gohman | c731c97 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 151 | if (!I->pred_empty()) { | 
| Nate Begeman | b9d4f83 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 152 | printBasicBlockLabel(I, true); | 
|  | 153 | O << '\n'; | 
|  | 154 | } | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 155 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); | 
|  | 156 | II != E; ++II) { | 
|  | 157 | // Print the assembly for the instruction. | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 158 | printMachineInstruction(II); | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | // We didn't modify anything. | 
|  | 163 | return false; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | void IA64AsmPrinter::printOp(const MachineOperand &MO, | 
| Chris Lattner | fef7a2d | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 167 | bool isBRCALLinsn /* = false */) { | 
| Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame^] | 168 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 169 | switch (MO.getType()) { | 
| Chris Lattner | 10b71c0 | 2006-05-04 18:05:43 +0000 | [diff] [blame] | 170 | case MachineOperand::MO_Register: | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 171 | O << RI.get(MO.getReg()).Name; | 
|  | 172 | return; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 173 |  | 
| Chris Lattner | fef7a2d | 2006-05-04 17:21:20 +0000 | [diff] [blame] | 174 | case MachineOperand::MO_Immediate: | 
| Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 175 | O << MO.getImm(); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 176 | return; | 
| Nate Begeman | 4ca2ea5 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 177 | case MachineOperand::MO_MachineBasicBlock: | 
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 178 | printBasicBlockLabel(MO.getMBB()); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 179 | return; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 180 | case MachineOperand::MO_ConstantPoolIndex: { | 
| Jim Laskey | a6211dc | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 181 | O << "@gprel(" << TAI->getPrivateGlobalPrefix() | 
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 182 | << "CPI" << getFunctionNumber() << "_" << MO.getIndex() << ")"; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 183 | return; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | case MachineOperand::MO_GlobalAddress: { | 
|  | 187 |  | 
|  | 188 | // functions need @ltoff(@fptr(fn_name)) form | 
|  | 189 | GlobalValue *GV = MO.getGlobal(); | 
|  | 190 | Function *F = dyn_cast<Function>(GV); | 
|  | 191 |  | 
|  | 192 | bool Needfptr=false; // if we're computing an address @ltoff(X), do | 
|  | 193 | // we need to decorate it so it becomes | 
| Misha Brukman | e73e76d | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 194 | // @ltoff(@fptr(X)) ? | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 195 | if (F && !isBRCALLinsn /*&& F->isDeclaration()*/) | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 196 | Needfptr=true; | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 197 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 198 | // if this is the target of a call instruction, we should define | 
|  | 199 | // the function somewhere (GNU gas has no problem without this, but | 
|  | 200 | // Intel ias rightly complains of an 'undefined symbol') | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 201 |  | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 202 | if (F /*&& isBRCALLinsn*/ && F->isDeclaration()) | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 203 | ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal())); | 
| Duraid Madina | bdbf8ba | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 204 | else | 
| Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 205 | if (GV->isDeclaration()) // e.g. stuff like 'stdin' | 
| Misha Brukman | e73e76d | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 206 | ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal())); | 
| Duraid Madina | bdbf8ba | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 207 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 208 | if (!isBRCALLinsn) | 
|  | 209 | O << "@ltoff("; | 
|  | 210 | if (Needfptr) | 
|  | 211 | O << "@fptr("; | 
|  | 212 | O << Mang->getValueName(MO.getGlobal()); | 
| Duraid Madina | 36a2ee2 | 2006-02-16 13:12:57 +0000 | [diff] [blame] | 213 |  | 
|  | 214 | if (Needfptr && !isBRCALLinsn) | 
|  | 215 | O << "#))"; // close both fptr( and ltoff( | 
|  | 216 | else { | 
|  | 217 | if (Needfptr) | 
|  | 218 | O << "#)"; // close only fptr( | 
|  | 219 | if (!isBRCALLinsn) | 
|  | 220 | O << "#)"; // close only ltoff( | 
|  | 221 | } | 
|  | 222 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 223 | int Offset = MO.getOffset(); | 
|  | 224 | if (Offset > 0) | 
|  | 225 | O << " + " << Offset; | 
|  | 226 | else if (Offset < 0) | 
|  | 227 | O << " - " << -Offset; | 
|  | 228 | return; | 
|  | 229 | } | 
|  | 230 | case MachineOperand::MO_ExternalSymbol: | 
|  | 231 | O << MO.getSymbolName(); | 
| Duraid Madina | bdbf8ba | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 232 | ExternalFunctionNames.insert(MO.getSymbolName()); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 233 | return; | 
|  | 234 | default: | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 235 | O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 236 | } | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | /// printMachineInstruction -- Print out a single IA64 LLVM instruction | 
|  | 240 | /// MI to the current output stream. | 
|  | 241 | /// | 
|  | 242 | void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) { | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 243 | ++EmittedInsts; | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 244 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 245 | // Call the autogenerated instruction printer routines. | 
|  | 246 | printInstruction(MI); | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | bool IA64AsmPrinter::doInitialization(Module &M) { | 
| Dan Gohman | cf0a534 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 250 | bool Result = AsmPrinter::doInitialization(M); | 
| Misha Brukman | 89b8c8d | 2005-04-21 23:13:11 +0000 | [diff] [blame] | 251 |  | 
| Duraid Madina | bdbf8ba | 2005-03-28 15:21:43 +0000 | [diff] [blame] | 252 | O << "\n.ident \"LLVM-ia64\"\n\n" | 
| Misha Brukman | e73e76d | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 253 | << "\t.psr    lsb\n"  // should be "msb" on HP-UX, for starters | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 254 | << "\t.radix  C\n" | 
| Misha Brukman | e73e76d | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 255 | << "\t.psr    abi64\n"; // we only support 64 bits for now | 
| Dan Gohman | cf0a534 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 256 | return Result; | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 259 | bool IA64AsmPrinter::doFinalization(Module &M) { | 
| Owen Anderson | 20a631f | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 260 | const TargetData *TD = TM.getTargetData(); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 261 |  | 
|  | 262 | // Print out module-level global variables here. | 
|  | 263 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); | 
|  | 264 | I != E; ++I) | 
|  | 265 | if (I->hasInitializer()) {   // External global require no code | 
| Chris Lattner | e363fdf | 2006-03-09 06:14:35 +0000 | [diff] [blame] | 266 | // Check to see if this is a special global used by LLVM, if so, emit it. | 
|  | 267 | if (EmitSpecialLLVMGlobal(I)) | 
|  | 268 | continue; | 
|  | 269 |  | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 270 | O << "\n\n"; | 
|  | 271 | std::string name = Mang->getValueName(I); | 
|  | 272 | Constant *C = I->getInitializer(); | 
| Duncan Sands | 283207a | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 273 | unsigned Size = TD->getABITypeSize(C->getType()); | 
| Duncan Sands | 05837ed | 2008-01-29 06:23:44 +0000 | [diff] [blame] | 274 | unsigned Align = TD->getPreferredAlignmentLog(I); | 
|  | 275 |  | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 276 | if (C->isNullValue() && | 
|  | 277 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || | 
|  | 278 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { | 
| Chris Lattner | 8488ba2 | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 279 | SwitchToDataSection(".data", I); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 280 | if (I->hasInternalLinkage()) { | 
| Duncan Sands | 283207a | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 281 | O << "\t.lcomm " << name << "#," << TD->getABITypeSize(C->getType()) | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 282 | << "," << (1 << Align); | 
| Chris Lattner | 81cf22d | 2006-12-06 06:13:25 +0000 | [diff] [blame] | 283 | O << "\n"; | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 284 | } else { | 
| Duncan Sands | 283207a | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 285 | O << "\t.common " << name << "#," << TD->getABITypeSize(C->getType()) | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 286 | << "," << (1 << Align); | 
| Chris Lattner | 81cf22d | 2006-12-06 06:13:25 +0000 | [diff] [blame] | 287 | O << "\n"; | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 288 | } | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 289 | } else { | 
|  | 290 | switch (I->getLinkage()) { | 
|  | 291 | case GlobalValue::LinkOnceLinkage: | 
|  | 292 | case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak. | 
|  | 293 | // Nonnull linkonce -> weak | 
|  | 294 | O << "\t.weak " << name << "\n"; | 
|  | 295 | O << "\t.section\t.llvm.linkonce.d." << name | 
|  | 296 | << ", \"aw\", \"progbits\"\n"; | 
| Chris Lattner | 8488ba2 | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 297 | SwitchToDataSection("", I); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 298 | break; | 
|  | 299 | case GlobalValue::AppendingLinkage: | 
|  | 300 | // FIXME: appending linkage variables should go into a section of | 
|  | 301 | // their name or something.  For now, just emit them as external. | 
|  | 302 | case GlobalValue::ExternalLinkage: | 
|  | 303 | // If external or appending, declare as a global symbol | 
|  | 304 | O << "\t.global " << name << "\n"; | 
|  | 305 | // FALL THROUGH | 
|  | 306 | case GlobalValue::InternalLinkage: | 
| Chris Lattner | 8488ba2 | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 307 | SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 308 | break; | 
|  | 309 | case GlobalValue::GhostLinkage: | 
| Bill Wendling | 9bfb1e1 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 310 | cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n"; | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 311 | abort(); | 
| Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 312 | case GlobalValue::DLLImportLinkage: | 
| Bill Wendling | 9bfb1e1 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 313 | cerr << "DLLImport linkage is not supported by this target!\n"; | 
| Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 314 | abort(); | 
|  | 315 | case GlobalValue::DLLExportLinkage: | 
| Bill Wendling | 9bfb1e1 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 316 | cerr << "DLLExport linkage is not supported by this target!\n"; | 
| Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 317 | abort(); | 
|  | 318 | default: | 
|  | 319 | assert(0 && "Unknown linkage type!"); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
|  | 322 | EmitAlignment(Align); | 
|  | 323 | O << "\t.type " << name << ",@object\n"; | 
|  | 324 | O << "\t.size " << name << "," << Size << "\n"; | 
| Chris Lattner | 81cf22d | 2006-12-06 06:13:25 +0000 | [diff] [blame] | 325 | O << name << ":\t\t\t\t// " << *C << "\n"; | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 326 | EmitGlobalConstant(C); | 
|  | 327 | } | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | // we print out ".global X \n .type X, @function" for each external function | 
|  | 331 | O << "\n\n// br.call targets referenced (and not defined) above: \n"; | 
|  | 332 | for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(), | 
|  | 333 | e = ExternalFunctionNames.end(); i!=e; ++i) { | 
|  | 334 | O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n"; | 
|  | 335 | } | 
|  | 336 | O << "\n\n"; | 
|  | 337 |  | 
|  | 338 | // we print out ".global X \n .type X, @object" for each external object | 
|  | 339 | O << "\n\n// (external) symbols referenced (and not defined) above: \n"; | 
|  | 340 | for (std::set<std::string>::iterator i = ExternalObjectNames.begin(), | 
|  | 341 | e = ExternalObjectNames.end(); i!=e; ++i) { | 
|  | 342 | O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n"; | 
|  | 343 | } | 
|  | 344 | O << "\n\n"; | 
|  | 345 |  | 
| Dan Gohman | cf0a534 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 346 | return AsmPrinter::doFinalization(M); | 
| Chris Lattner | b9db67a | 2005-11-21 08:40:17 +0000 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 349 | /// createIA64CodePrinterPass - Returns a pass that prints the IA64 | 
|  | 350 | /// assembly code for a MachineFunction to the given output stream, using | 
|  | 351 | /// the given target machine description. | 
|  | 352 | /// | 
| Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 353 | FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o, | 
|  | 354 | IA64TargetMachine &tm) { | 
| Jim Laskey | 261779b | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 355 | return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo()); | 
| Duraid Madina | 91ed0a1 | 2005-03-17 18:17:03 +0000 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
|  | 358 |  |