blob: 6915cbc5d95f8ae3cd4f171de58fcfee54bb211a [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/CodeGen/AsmPrinter.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000026#include "llvm/Target/TargetMachine.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000027#include "llvm/Support/Mangler.h"
28#include "llvm/ADT/Statistic.h"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000029using namespace llvm;
30
31namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000032 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
Jim Laskey563321a2006-09-06 18:34:40 +000033
34 struct IA64AsmPrinter : public AsmPrinter {
35 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
36
Jim Laskeya0f3d172006-09-07 22:06:40 +000037 IA64AsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000038 : AsmPrinter(O, TM, T) {
39 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +000040
41 virtual const char *getPassName() const {
42 return "IA64 Assembly Printer";
43 }
44
45 /// printInstruction - This method is automatically generated by tablegen
46 /// from the instruction set description. This method returns true if the
47 /// machine instruction was sufficiently described to print it, otherwise it
48 /// returns false.
49 bool printInstruction(const MachineInstr *MI);
50
51 // This method is used by the tablegen'erated instruction printer.
Nate Begeman391c5d22005-11-30 18:54:35 +000052 void printOperand(const MachineInstr *MI, unsigned OpNo){
Duraid Madina9b9d45f2005-03-17 18:17:03 +000053 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner2d90ac72006-05-04 18:05:43 +000054 if (MO.getType() == MachineOperand::MO_Register) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000055 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
56 //XXX Bug Workaround: See note in Printer::doInitialization about %.
57 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
58 } else {
59 printOp(MO);
60 }
61 }
Misha Brukman4633f1c2005-04-21 23:13:11 +000062
Nate Begeman391c5d22005-11-30 18:54:35 +000063 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000064 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
65 if(val>=128) val=val-256; // if negative, flip sign
66 O << val;
67 }
Nate Begeman391c5d22005-11-30 18:54:35 +000068 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000069 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
70 if(val>=8192) val=val-16384; // if negative, flip sign
71 O << val;
72 }
Nate Begeman391c5d22005-11-30 18:54:35 +000073 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +000074 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
75 if(val>=2097152) val=val-4194304; // if negative, flip sign
76 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +000077 }
Nate Begeman391c5d22005-11-30 18:54:35 +000078 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000079 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
80 }
Nate Begeman391c5d22005-11-30 18:54:35 +000081 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +000082// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
83// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
84// emit movl rX = @gprel(CPI<whatever);;
85// add rX = rX, r1;
86// this gives us 64 bits instead of 22 (for the add long imm) to play
87// with, which shuts up the linker. The problem is that the constant
88// pool entries aren't immediates at this stage, so we check here.
89// If it's an immediate, print it the old fashioned way. If it's
90// not, we print it as a constant pool index.
91 if(MI->getOperand(OpNo).isImmediate()) {
92 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
93 } else { // this is a constant pool reference: FIXME: assert this
94 printOp(MI->getOperand(OpNo));
95 }
96 }
97
Nate Begeman391c5d22005-11-30 18:54:35 +000098 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +000099 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000100 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000101
Nate Begeman391c5d22005-11-30 18:54:35 +0000102 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000103 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000104 }
105
Chris Lattneredad2b72006-10-05 02:51:36 +0000106 std::string getSectionForFunction(const Function &F) const;
107
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000108 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
Chris Lattneredad2b72006-10-05 02:51:36 +0000121std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const {
122 // This means "Allocated instruXions in mem, initialized".
123 return "\n\t.section .text, \"ax\", \"progbits\"\n";
124}
125
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000126/// runOnMachineFunction - This uses the printMachineInstruction()
127/// method to print assembly for each instruction.
128///
129bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000130 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000131 O << "\n\n";
132
133 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000134 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000135
Chris Lattneredad2b72006-10-05 02:51:36 +0000136 const Function *F = MF.getFunction();
137 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
138
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000139 // Print out labels for the function.
Chris Lattner8b8b9512005-11-21 07:51:23 +0000140 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000141 O << "\t.global\t" << CurrentFnName << "\n";
142 O << "\t.type\t" << CurrentFnName << ", @function\n";
143 O << CurrentFnName << ":\n";
144
145 // Print out code for the function.
146 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
147 I != E; ++I) {
148 // Print a label for the basic block if there are any predecessors.
Nate Begemancdf38c42006-05-02 05:37:32 +0000149 if (I->pred_begin() != I->pred_end()) {
150 printBasicBlockLabel(I, true);
151 O << '\n';
152 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000153 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
154 II != E; ++II) {
155 // Print the assembly for the instruction.
156 O << "\t";
157 printMachineInstruction(II);
158 }
159 }
160
161 // We didn't modify anything.
162 return false;
163}
164
165void IA64AsmPrinter::printOp(const MachineOperand &MO,
Chris Lattner63b3d712006-05-04 17:21:20 +0000166 bool isBRCALLinsn /* = false */) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000167 const MRegisterInfo &RI = *TM.getRegisterInfo();
168 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000169 case MachineOperand::MO_Register:
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000170 O << RI.get(MO.getReg()).Name;
171 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000172
Chris Lattner63b3d712006-05-04 17:21:20 +0000173 case MachineOperand::MO_Immediate:
174 O << MO.getImmedValue();
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000175 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000176 case MachineOperand::MO_MachineBasicBlock:
177 printBasicBlockLabel(MO.getMachineBasicBlock());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000178 return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000179 case MachineOperand::MO_ConstantPoolIndex: {
Jim Laskey563321a2006-09-06 18:34:40 +0000180 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
181 << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000182 << MO.getConstantPoolIndex() << ")";
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 Brukman7847fca2005-04-22 17:54:37 +0000194 // @ltoff(@fptr(X)) ?
195 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000196 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000197
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000198 // 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 Brukman4633f1c2005-04-21 23:13:11 +0000201
Misha Brukman7847fca2005-04-22 17:54:37 +0000202 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000203 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000204 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000205 if (GV->isExternal()) // e.g. stuff like 'stdin'
206 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000207
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000208 if (!isBRCALLinsn)
209 O << "@ltoff(";
210 if (Needfptr)
211 O << "@fptr(";
212 O << Mang->getValueName(MO.getGlobal());
Duraid Madina00d3d442006-02-16 13:12:57 +0000213
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 Madina9b9d45f2005-03-17 18:17:03 +0000223 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 Madinaae8bd732005-03-28 15:21:43 +0000232 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000233 return;
234 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000235 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000236 }
237}
238
239/// printMachineInstruction -- Print out a single IA64 LLVM instruction
240/// MI to the current output stream.
241///
242void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000243 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000245 // Call the autogenerated instruction printer routines.
246 printInstruction(MI);
247}
248
249bool IA64AsmPrinter::doInitialization(Module &M) {
250 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000251
Duraid Madinaae8bd732005-03-28 15:21:43 +0000252 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000253 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000254 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000255 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000256 return false;
257}
258
Chris Lattner42a76cd2005-11-21 08:40:17 +0000259bool IA64AsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000260 const TargetData *TD = TM.getTargetData();
Chris Lattner42a76cd2005-11-21 08:40:17 +0000261
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 Lattner04f96742006-03-09 06:14:35 +0000266 // Check to see if this is a special global used by LLVM, if so, emit it.
267 if (EmitSpecialLLVMGlobal(I))
268 continue;
269
Chris Lattner42a76cd2005-11-21 08:40:17 +0000270 O << "\n\n";
271 std::string name = Mang->getValueName(I);
272 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000273 unsigned Size = TD->getTypeSize(C->getType());
274 unsigned Align = TD->getTypeAlignmentShift(C->getType());
Chris Lattner42a76cd2005-11-21 08:40:17 +0000275
276 if (C->isNullValue() &&
277 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
278 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000279 SwitchToDataSection(".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000280 if (I->hasInternalLinkage()) {
Owen Andersona69571c2006-05-03 01:29:57 +0000281 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000282 << "," << (1 << Align);
Chris Lattnerd17aa4b2006-12-06 06:13:25 +0000283 O << "\n";
Chris Lattner42a76cd2005-11-21 08:40:17 +0000284 } else {
Owen Andersona69571c2006-05-03 01:29:57 +0000285 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000286 << "," << (1 << Align);
Chris Lattnerd17aa4b2006-12-06 06:13:25 +0000287 O << "\n";
Chris Lattner42a76cd2005-11-21 08:40:17 +0000288 }
Chris Lattner42a76cd2005-11-21 08:40:17 +0000289 } 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 Lattner4632d7a2006-05-09 04:59:56 +0000297 SwitchToDataSection("", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000298 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 Lattner4632d7a2006-05-09 04:59:56 +0000307 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000308 break;
309 case GlobalValue::GhostLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000310 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
Chris Lattner42a76cd2005-11-21 08:40:17 +0000311 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000312 case GlobalValue::DLLImportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000313 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000314 abort();
315 case GlobalValue::DLLExportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000316 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000317 abort();
318 default:
319 assert(0 && "Unknown linkage type!");
Chris Lattner42a76cd2005-11-21 08:40:17 +0000320 }
321
322 EmitAlignment(Align);
323 O << "\t.type " << name << ",@object\n";
324 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnerd17aa4b2006-12-06 06:13:25 +0000325 O << name << ":\t\t\t\t// " << *C << "\n";
Chris Lattner42a76cd2005-11-21 08:40:17 +0000326 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
346 AsmPrinter::doFinalization(M);
347 return false; // success
348}
349
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000350/// 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 Chengc4c62572006-03-13 23:20:37 +0000354FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
355 IA64TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000356 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000357}
358
359