blob: 5fcc86f50e795e38c9dc89e259a4a348db2e02a0 [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00003// 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 Brukman4633f1c2005-04-21 23:13:11 +00007//
Duraid Madina9b9d45f2005-03-17 18:17:03 +00008//===----------------------------------------------------------------------===//
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 Madinaf2db9b82005-10-28 17:46:35 +000014// report in bugzilla. FYI, the not too bad 'ias' assembler is bundled with
Duraid Madina9b9d45f2005-03-17 18:17:03 +000015// 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 Lattner3c61f702005-03-24 05:12:48 +000022#include "llvm/Type.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000023#include "llvm/Assembly/Writer.h"
24#include "llvm/CodeGen/AsmPrinter.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000027#include "llvm/Target/TargetMachine.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000028#include "llvm/Support/Mangler.h"
29#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000031using namespace llvm;
32
33namespace {
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Jim Laskey563321a2006-09-06 18:34:40 +000035
36 struct IA64AsmPrinter : public AsmPrinter {
37 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
38
Jim Laskeya0f3d172006-09-07 22:06:40 +000039 IA64AsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000040 : AsmPrinter(O, TM, T) {
41 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +000042
43 virtual const char *getPassName() const {
44 return "IA64 Assembly Printer";
45 }
46
47 /// printInstruction - This method is automatically generated by tablegen
48 /// from the instruction set description. This method returns true if the
49 /// machine instruction was sufficiently described to print it, otherwise it
50 /// returns false.
51 bool printInstruction(const MachineInstr *MI);
52
53 // This method is used by the tablegen'erated instruction printer.
Nate Begeman391c5d22005-11-30 18:54:35 +000054 void printOperand(const MachineInstr *MI, unsigned OpNo){
Duraid Madina9b9d45f2005-03-17 18:17:03 +000055 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner2d90ac72006-05-04 18:05:43 +000056 if (MO.getType() == MachineOperand::MO_Register) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000057 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
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 Brukman4633f1c2005-04-21 23:13:11 +000064
Nate Begeman391c5d22005-11-30 18:54:35 +000065 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000066 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
67 if(val>=128) val=val-256; // if negative, flip sign
68 O << val;
69 }
Nate Begeman391c5d22005-11-30 18:54:35 +000070 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000071 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
72 if(val>=8192) val=val-16384; // if negative, flip sign
73 O << val;
74 }
Nate Begeman391c5d22005-11-30 18:54:35 +000075 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +000076 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
77 if(val>=2097152) val=val-4194304; // if negative, flip sign
78 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +000079 }
Nate Begeman391c5d22005-11-30 18:54:35 +000080 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
82 }
Nate Begeman391c5d22005-11-30 18:54:35 +000083 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +000084// 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()) {
94 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
95 } else { // this is a constant pool reference: FIXME: assert this
96 printOp(MI->getOperand(OpNo));
97 }
98 }
99
Nate Begeman391c5d22005-11-30 18:54:35 +0000100 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000101 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000102 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000103
Nate Begeman391c5d22005-11-30 18:54:35 +0000104 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000105 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000106 }
107
108 void printMachineInstruction(const MachineInstr *MI);
109 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000110 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000111 bool doInitialization(Module &M);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000112 bool doFinalization(Module &M);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000113 };
114} // end of anonymous namespace
115
116
117// Include the auto-generated portion of the assembly writer.
118#include "IA64GenAsmWriter.inc"
119
120
121/// runOnMachineFunction - This uses the printMachineInstruction()
122/// method to print assembly for each instruction.
123///
124bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000125 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000126 O << "\n\n";
127
128 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000129 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000130
131 // Print out labels for the function.
Chris Lattner4632d7a2006-05-09 04:59:56 +0000132 SwitchToTextSection("\n\t.section .text, \"ax\", \"progbits\"\n",
133 MF.getFunction());
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000134 // ^^ means "Allocated instruXions in mem, initialized"
Chris Lattner8b8b9512005-11-21 07:51:23 +0000135 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000136 O << "\t.global\t" << CurrentFnName << "\n";
137 O << "\t.type\t" << CurrentFnName << ", @function\n";
138 O << CurrentFnName << ":\n";
139
140 // Print out code for the function.
141 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
142 I != E; ++I) {
143 // Print a label for the basic block if there are any predecessors.
Nate Begemancdf38c42006-05-02 05:37:32 +0000144 if (I->pred_begin() != I->pred_end()) {
145 printBasicBlockLabel(I, true);
146 O << '\n';
147 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000148 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
149 II != E; ++II) {
150 // Print the assembly for the instruction.
151 O << "\t";
152 printMachineInstruction(II);
153 }
154 }
155
156 // We didn't modify anything.
157 return false;
158}
159
160void IA64AsmPrinter::printOp(const MachineOperand &MO,
Chris Lattner63b3d712006-05-04 17:21:20 +0000161 bool isBRCALLinsn /* = false */) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000162 const MRegisterInfo &RI = *TM.getRegisterInfo();
163 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000164 case MachineOperand::MO_Register:
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000165 O << RI.get(MO.getReg()).Name;
166 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000167
Chris Lattner63b3d712006-05-04 17:21:20 +0000168 case MachineOperand::MO_Immediate:
169 O << MO.getImmedValue();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000170 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000171 case MachineOperand::MO_MachineBasicBlock:
172 printBasicBlockLabel(MO.getMachineBasicBlock());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000173 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174 case MachineOperand::MO_ConstantPoolIndex: {
Jim Laskey563321a2006-09-06 18:34:40 +0000175 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
176 << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000177 << MO.getConstantPoolIndex() << ")";
178 return;
179 }
180
181 case MachineOperand::MO_GlobalAddress: {
182
183 // functions need @ltoff(@fptr(fn_name)) form
184 GlobalValue *GV = MO.getGlobal();
185 Function *F = dyn_cast<Function>(GV);
186
187 bool Needfptr=false; // if we're computing an address @ltoff(X), do
188 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000189 // @ltoff(@fptr(X)) ?
190 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000191 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000192
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000193 // if this is the target of a call instruction, we should define
194 // the function somewhere (GNU gas has no problem without this, but
195 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000196
Misha Brukman7847fca2005-04-22 17:54:37 +0000197 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000198 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000199 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000200 if (GV->isExternal()) // e.g. stuff like 'stdin'
201 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000202
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000203 if (!isBRCALLinsn)
204 O << "@ltoff(";
205 if (Needfptr)
206 O << "@fptr(";
207 O << Mang->getValueName(MO.getGlobal());
Duraid Madina00d3d442006-02-16 13:12:57 +0000208
209 if (Needfptr && !isBRCALLinsn)
210 O << "#))"; // close both fptr( and ltoff(
211 else {
212 if (Needfptr)
213 O << "#)"; // close only fptr(
214 if (!isBRCALLinsn)
215 O << "#)"; // close only ltoff(
216 }
217
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000218 int Offset = MO.getOffset();
219 if (Offset > 0)
220 O << " + " << Offset;
221 else if (Offset < 0)
222 O << " - " << -Offset;
223 return;
224 }
225 case MachineOperand::MO_ExternalSymbol:
226 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000227 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000228 return;
229 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000230 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000231 }
232}
233
234/// printMachineInstruction -- Print out a single IA64 LLVM instruction
235/// MI to the current output stream.
236///
237void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000238 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000240 // Call the autogenerated instruction printer routines.
241 printInstruction(MI);
242}
243
244bool IA64AsmPrinter::doInitialization(Module &M) {
245 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246
Duraid Madinaae8bd732005-03-28 15:21:43 +0000247 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000248 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000249 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000250 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000251 return false;
252}
253
Chris Lattner42a76cd2005-11-21 08:40:17 +0000254bool IA64AsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000255 const TargetData *TD = TM.getTargetData();
Chris Lattner42a76cd2005-11-21 08:40:17 +0000256
257 // Print out module-level global variables here.
258 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
259 I != E; ++I)
260 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000261 // Check to see if this is a special global used by LLVM, if so, emit it.
262 if (EmitSpecialLLVMGlobal(I))
263 continue;
264
Chris Lattner42a76cd2005-11-21 08:40:17 +0000265 O << "\n\n";
266 std::string name = Mang->getValueName(I);
267 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000268 unsigned Size = TD->getTypeSize(C->getType());
269 unsigned Align = TD->getTypeAlignmentShift(C->getType());
Chris Lattner42a76cd2005-11-21 08:40:17 +0000270
271 if (C->isNullValue() &&
272 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
273 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000274 SwitchToDataSection(".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000275 if (I->hasInternalLinkage()) {
Owen Andersona69571c2006-05-03 01:29:57 +0000276 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000277 << "," << (1 << Align);
278 O << "\t\t// ";
279 } else {
Owen Andersona69571c2006-05-03 01:29:57 +0000280 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000281 << "," << (1 << Align);
282 O << "\t\t// ";
283 }
284 WriteAsOperand(O, I, true, true, &M);
285 O << "\n";
286 } else {
287 switch (I->getLinkage()) {
288 case GlobalValue::LinkOnceLinkage:
289 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
290 // Nonnull linkonce -> weak
291 O << "\t.weak " << name << "\n";
292 O << "\t.section\t.llvm.linkonce.d." << name
293 << ", \"aw\", \"progbits\"\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000294 SwitchToDataSection("", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000295 break;
296 case GlobalValue::AppendingLinkage:
297 // FIXME: appending linkage variables should go into a section of
298 // their name or something. For now, just emit them as external.
299 case GlobalValue::ExternalLinkage:
300 // If external or appending, declare as a global symbol
301 O << "\t.global " << name << "\n";
302 // FALL THROUGH
303 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000304 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000305 break;
306 case GlobalValue::GhostLinkage:
307 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
308 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000309 case GlobalValue::DLLImportLinkage:
310 std::cerr << "DLLImport linkage is not supported by this target!\n";
311 abort();
312 case GlobalValue::DLLExportLinkage:
313 std::cerr << "DLLExport linkage is not supported by this target!\n";
314 abort();
315 default:
316 assert(0 && "Unknown linkage type!");
Chris Lattner42a76cd2005-11-21 08:40:17 +0000317 }
318
319 EmitAlignment(Align);
320 O << "\t.type " << name << ",@object\n";
321 O << "\t.size " << name << "," << Size << "\n";
322 O << name << ":\t\t\t\t// ";
323 WriteAsOperand(O, I, true, true, &M);
324 O << " = ";
325 WriteAsOperand(O, C, false, false, &M);
326 O << "\n";
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
347 AsmPrinter::doFinalization(M);
348 return false; // success
349}
350
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000351/// createIA64CodePrinterPass - Returns a pass that prints the IA64
352/// assembly code for a MachineFunction to the given output stream, using
353/// the given target machine description.
354///
Evan Chengc4c62572006-03-13 23:20:37 +0000355FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
356 IA64TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000357 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000358}
359
360