blob: fd85b8bc9bd64b8256d0e6c99e93e2e88fc710f8 [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
Chris Lattneredad2b72006-10-05 02:51:36 +0000108 std::string getSectionForFunction(const Function &F) const;
109
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000110 void printMachineInstruction(const MachineInstr *MI);
111 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000112 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000113 bool doInitialization(Module &M);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000114 bool doFinalization(Module &M);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000115 };
116} // end of anonymous namespace
117
118
119// Include the auto-generated portion of the assembly writer.
120#include "IA64GenAsmWriter.inc"
121
122
Chris Lattneredad2b72006-10-05 02:51:36 +0000123std::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 Madina9b9d45f2005-03-17 18:17:03 +0000128/// runOnMachineFunction - This uses the printMachineInstruction()
129/// method to print assembly for each instruction.
130///
131bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000132 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000133 O << "\n\n";
134
135 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000136 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000137
Chris Lattneredad2b72006-10-05 02:51:36 +0000138 const Function *F = MF.getFunction();
139 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
140
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000141 // Print out labels for the function.
Chris Lattner8b8b9512005-11-21 07:51:23 +0000142 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000143 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.
Nate Begemancdf38c42006-05-02 05:37:32 +0000151 if (I->pred_begin() != I->pred_end()) {
152 printBasicBlockLabel(I, true);
153 O << '\n';
154 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000155 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
156 II != E; ++II) {
157 // Print the assembly for the instruction.
158 O << "\t";
159 printMachineInstruction(II);
160 }
161 }
162
163 // We didn't modify anything.
164 return false;
165}
166
167void IA64AsmPrinter::printOp(const MachineOperand &MO,
Chris Lattner63b3d712006-05-04 17:21:20 +0000168 bool isBRCALLinsn /* = false */) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000169 const MRegisterInfo &RI = *TM.getRegisterInfo();
170 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000171 case MachineOperand::MO_Register:
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000172 O << RI.get(MO.getReg()).Name;
173 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000174
Chris Lattner63b3d712006-05-04 17:21:20 +0000175 case MachineOperand::MO_Immediate:
176 O << MO.getImmedValue();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000177 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000178 case MachineOperand::MO_MachineBasicBlock:
179 printBasicBlockLabel(MO.getMachineBasicBlock());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000180 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000181 case MachineOperand::MO_ConstantPoolIndex: {
Jim Laskey563321a2006-09-06 18:34:40 +0000182 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
183 << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000184 << MO.getConstantPoolIndex() << ")";
185 return;
186 }
187
188 case MachineOperand::MO_GlobalAddress: {
189
190 // functions need @ltoff(@fptr(fn_name)) form
191 GlobalValue *GV = MO.getGlobal();
192 Function *F = dyn_cast<Function>(GV);
193
194 bool Needfptr=false; // if we're computing an address @ltoff(X), do
195 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000196 // @ltoff(@fptr(X)) ?
197 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000198 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000199
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000200 // if this is the target of a call instruction, we should define
201 // the function somewhere (GNU gas has no problem without this, but
202 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000203
Misha Brukman7847fca2005-04-22 17:54:37 +0000204 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000205 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000206 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000207 if (GV->isExternal()) // e.g. stuff like 'stdin'
208 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000209
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000210 if (!isBRCALLinsn)
211 O << "@ltoff(";
212 if (Needfptr)
213 O << "@fptr(";
214 O << Mang->getValueName(MO.getGlobal());
Duraid Madina00d3d442006-02-16 13:12:57 +0000215
216 if (Needfptr && !isBRCALLinsn)
217 O << "#))"; // close both fptr( and ltoff(
218 else {
219 if (Needfptr)
220 O << "#)"; // close only fptr(
221 if (!isBRCALLinsn)
222 O << "#)"; // close only ltoff(
223 }
224
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000225 int Offset = MO.getOffset();
226 if (Offset > 0)
227 O << " + " << Offset;
228 else if (Offset < 0)
229 O << " - " << -Offset;
230 return;
231 }
232 case MachineOperand::MO_ExternalSymbol:
233 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000234 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000235 return;
236 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000237 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000238 }
239}
240
241/// printMachineInstruction -- Print out a single IA64 LLVM instruction
242/// MI to the current output stream.
243///
244void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000245 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000247 // Call the autogenerated instruction printer routines.
248 printInstruction(MI);
249}
250
251bool IA64AsmPrinter::doInitialization(Module &M) {
252 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253
Duraid Madinaae8bd732005-03-28 15:21:43 +0000254 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000255 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000256 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000257 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000258 return false;
259}
260
Chris Lattner42a76cd2005-11-21 08:40:17 +0000261bool IA64AsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000262 const TargetData *TD = TM.getTargetData();
Chris Lattner42a76cd2005-11-21 08:40:17 +0000263
264 // Print out module-level global variables here.
265 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
266 I != E; ++I)
267 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000268 // Check to see if this is a special global used by LLVM, if so, emit it.
269 if (EmitSpecialLLVMGlobal(I))
270 continue;
271
Chris Lattner42a76cd2005-11-21 08:40:17 +0000272 O << "\n\n";
273 std::string name = Mang->getValueName(I);
274 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000275 unsigned Size = TD->getTypeSize(C->getType());
276 unsigned Align = TD->getTypeAlignmentShift(C->getType());
Chris Lattner42a76cd2005-11-21 08:40:17 +0000277
278 if (C->isNullValue() &&
279 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
280 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000281 SwitchToDataSection(".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000282 if (I->hasInternalLinkage()) {
Owen Andersona69571c2006-05-03 01:29:57 +0000283 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000284 << "," << (1 << Align);
285 O << "\t\t// ";
286 } else {
Owen Andersona69571c2006-05-03 01:29:57 +0000287 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000288 << "," << (1 << Align);
289 O << "\t\t// ";
290 }
291 WriteAsOperand(O, I, true, true, &M);
292 O << "\n";
293 } else {
294 switch (I->getLinkage()) {
295 case GlobalValue::LinkOnceLinkage:
296 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
297 // Nonnull linkonce -> weak
298 O << "\t.weak " << name << "\n";
299 O << "\t.section\t.llvm.linkonce.d." << name
300 << ", \"aw\", \"progbits\"\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000301 SwitchToDataSection("", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000302 break;
303 case GlobalValue::AppendingLinkage:
304 // FIXME: appending linkage variables should go into a section of
305 // their name or something. For now, just emit them as external.
306 case GlobalValue::ExternalLinkage:
307 // If external or appending, declare as a global symbol
308 O << "\t.global " << name << "\n";
309 // FALL THROUGH
310 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000311 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000312 break;
313 case GlobalValue::GhostLinkage:
314 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
315 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000316 case GlobalValue::DLLImportLinkage:
317 std::cerr << "DLLImport linkage is not supported by this target!\n";
318 abort();
319 case GlobalValue::DLLExportLinkage:
320 std::cerr << "DLLExport linkage is not supported by this target!\n";
321 abort();
322 default:
323 assert(0 && "Unknown linkage type!");
Chris Lattner42a76cd2005-11-21 08:40:17 +0000324 }
325
326 EmitAlignment(Align);
327 O << "\t.type " << name << ",@object\n";
328 O << "\t.size " << name << "," << Size << "\n";
329 O << name << ":\t\t\t\t// ";
330 WriteAsOperand(O, I, true, true, &M);
331 O << " = ";
332 WriteAsOperand(O, C, false, false, &M);
333 O << "\n";
334 EmitGlobalConstant(C);
335 }
336 }
337
338 // we print out ".global X \n .type X, @function" for each external function
339 O << "\n\n// br.call targets referenced (and not defined) above: \n";
340 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
341 e = ExternalFunctionNames.end(); i!=e; ++i) {
342 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
343 }
344 O << "\n\n";
345
346 // we print out ".global X \n .type X, @object" for each external object
347 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
348 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
349 e = ExternalObjectNames.end(); i!=e; ++i) {
350 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
351 }
352 O << "\n\n";
353
354 AsmPrinter::doFinalization(M);
355 return false; // success
356}
357
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000358/// createIA64CodePrinterPass - Returns a pass that prints the IA64
359/// assembly code for a MachineFunction to the given output stream, using
360/// the given target machine description.
361///
Evan Chengc4c62572006-03-13 23:20:37 +0000362FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
363 IA64TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000364 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000365}
366
367