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