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