blob: d3cb56dd45c4903e59103ca753dfe3cf07679f19 [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"
26#include "llvm/CodeGen/ValueTypes.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/CommandLine.h"
31using namespace llvm;
32
33namespace {
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
36 struct IA64SharedAsmPrinter : public AsmPrinter {
37
Duraid Madinaae8bd732005-03-28 15:21:43 +000038 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
Misha Brukman4633f1c2005-04-21 23:13:11 +000039
Duraid Madina9b9d45f2005-03-17 18:17:03 +000040 IA64SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
41 : AsmPrinter(O, TM) { }
42
Duraid Madina9b9d45f2005-03-17 18:17:03 +000043 bool doFinalization(Module &M);
44 };
45}
46
Duraid Madina9b9d45f2005-03-17 18:17:03 +000047bool IA64SharedAsmPrinter::doFinalization(Module &M) {
48 const TargetData &TD = TM.getTargetData();
Duraid Madina9b9d45f2005-03-17 18:17:03 +000049
50 // Print out module-level global variables here.
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +000051 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
52 I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +000053 if (I->hasInitializer()) { // External global require no code
54 O << "\n\n";
55 std::string name = Mang->getValueName(I);
56 Constant *C = I->getInitializer();
57 unsigned Size = TD.getTypeSize(C->getType());
58 unsigned Align = TD.getTypeAlignmentShift(C->getType());
59
Misha Brukman4633f1c2005-04-21 23:13:11 +000060 if (C->isNullValue() &&
Duraid Madina9b9d45f2005-03-17 18:17:03 +000061 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
62 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4bfa3a32005-11-21 07:26:04 +000063 SwitchSection(".data", I);
Duraid Madina1f867b12005-03-31 07:40:24 +000064 if (I->hasInternalLinkage()) {
Misha Brukman7847fca2005-04-22 17:54:37 +000065 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
66 << "," << (1 << Align);
67 O << "\t\t// ";
68 } else {
69 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
70 << "," << (1 << Align);
71 O << "\t\t// ";
72 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +000073 WriteAsOperand(O, I, true, true, &M);
74 O << "\n";
75 } else {
76 switch (I->getLinkage()) {
77 case GlobalValue::LinkOnceLinkage:
78 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
79 // Nonnull linkonce -> weak
80 O << "\t.weak " << name << "\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +000081 O << "\t.section\t.llvm.linkonce.d." << name
Misha Brukman7847fca2005-04-22 17:54:37 +000082 << ", \"aw\", \"progbits\"\n";
Chris Lattner4bfa3a32005-11-21 07:26:04 +000083 SwitchSection("", I);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000084 break;
85 case GlobalValue::AppendingLinkage:
86 // FIXME: appending linkage variables should go into a section of
87 // their name or something. For now, just emit them as external.
88 case GlobalValue::ExternalLinkage:
89 // If external or appending, declare as a global symbol
90 O << "\t.global " << name << "\n";
91 // FALL THROUGH
92 case GlobalValue::InternalLinkage:
Chris Lattner4bfa3a32005-11-21 07:26:04 +000093 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000094 break;
95 case GlobalValue::GhostLinkage:
96 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
97 abort();
98 }
99
Chris Lattner8b8b9512005-11-21 07:51:23 +0000100 EmitAlignment(Align);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000101 O << "\t.type " << name << ",@object\n";
102 O << "\t.size " << name << "," << Size << "\n";
103 O << name << ":\t\t\t\t// ";
104 WriteAsOperand(O, I, true, true, &M);
105 O << " = ";
106 WriteAsOperand(O, C, false, false, &M);
107 O << "\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000108 EmitGlobalConstant(C);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000109 }
110 }
111
112 // we print out ".global X \n .type X, @function" for each external function
113 O << "\n\n// br.call targets referenced (and not defined) above: \n";
114 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
115 e = ExternalFunctionNames.end(); i!=e; ++i) {
116 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
117 }
118 O << "\n\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000119
Duraid Madinaae8bd732005-03-28 15:21:43 +0000120 // we print out ".global X \n .type X, @object" for each external object
121 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
122 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
123 e = ExternalObjectNames.end(); i!=e; ++i) {
124 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
125 }
126 O << "\n\n";
127
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000128 AsmPrinter::doFinalization(M);
129 return false; // success
130}
131
132namespace {
133 struct IA64AsmPrinter : public IA64SharedAsmPrinter {
134 IA64AsmPrinter(std::ostream &O, TargetMachine &TM)
135 : IA64SharedAsmPrinter(O, TM) {
136
137 CommentString = "//";
Duraid Madina32c46f32005-04-02 12:30:47 +0000138 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
139 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
140 Data32bitsDirective = "\tdata4.ua\t"; // properly
141 Data64bitsDirective = "\tdata8.ua\t";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000142 ZeroDirective = "\t.skip\t";
143 AsciiDirective = "\tstring\t";
144
Duraid Madina32c46f32005-04-02 12:30:47 +0000145 GlobalVarAddrPrefix="";
146 GlobalVarAddrSuffix="";
147 FunctionAddrPrefix="@fptr(";
148 FunctionAddrSuffix=")";
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000149
150 // FIXME: would be nice to have rodata (no 'w') when appropriate?
151 ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000152 }
153
154 virtual const char *getPassName() const {
155 return "IA64 Assembly Printer";
156 }
157
158 /// printInstruction - This method is automatically generated by tablegen
159 /// from the instruction set description. This method returns true if the
160 /// machine instruction was sufficiently described to print it, otherwise it
161 /// returns false.
162 bool printInstruction(const MachineInstr *MI);
163
164 // This method is used by the tablegen'erated instruction printer.
165 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
166 const MachineOperand &MO = MI->getOperand(OpNo);
167 if (MO.getType() == MachineOperand::MO_MachineRegister) {
168 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
169 //XXX Bug Workaround: See note in Printer::doInitialization about %.
170 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
171 } else {
172 printOp(MO);
173 }
174 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000175
Duraid Madinae6a0b6c2005-04-07 12:34:36 +0000176 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo,
177 MVT::ValueType VT) {
178 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
179 if(val>=128) val=val-256; // if negative, flip sign
180 O << val;
181 }
182 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo,
183 MVT::ValueType VT) {
184 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
185 if(val>=8192) val=val-16384; // if negative, flip sign
186 O << val;
187 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000188 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000189 MVT::ValueType VT) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000190 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
191 if(val>=2097152) val=val-4194304; // if negative, flip sign
192 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000193 }
194 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
195 MVT::ValueType VT) {
196 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
197 }
Duraid Madina1ce0c012005-04-14 10:08:01 +0000198 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo,
199 MVT::ValueType VT) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000200// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
201// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
202// emit movl rX = @gprel(CPI<whatever);;
203// add rX = rX, r1;
204// this gives us 64 bits instead of 22 (for the add long imm) to play
205// with, which shuts up the linker. The problem is that the constant
206// pool entries aren't immediates at this stage, so we check here.
207// If it's an immediate, print it the old fashioned way. If it's
208// not, we print it as a constant pool index.
209 if(MI->getOperand(OpNo).isImmediate()) {
210 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
211 } else { // this is a constant pool reference: FIXME: assert this
212 printOp(MI->getOperand(OpNo));
213 }
214 }
215
216 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo,
217 MVT::ValueType VT) {
218 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000219 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000220
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000221 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
222 MVT::ValueType VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000223 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000224 }
225
226 void printMachineInstruction(const MachineInstr *MI);
227 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000228 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000229 bool doInitialization(Module &M);
230 };
231} // end of anonymous namespace
232
233
234// Include the auto-generated portion of the assembly writer.
235#include "IA64GenAsmWriter.inc"
236
237
238/// runOnMachineFunction - This uses the printMachineInstruction()
239/// method to print assembly for each instruction.
240///
241bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000242 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000243 O << "\n\n";
244
245 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000246 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000247
248 // Print out labels for the function.
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000249 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction());
250 // ^^ means "Allocated instruXions in mem, initialized"
Chris Lattner8b8b9512005-11-21 07:51:23 +0000251 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000252 O << "\t.global\t" << CurrentFnName << "\n";
253 O << "\t.type\t" << CurrentFnName << ", @function\n";
254 O << CurrentFnName << ":\n";
255
256 // Print out code for the function.
257 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
258 I != E; ++I) {
259 // Print a label for the basic block if there are any predecessors.
260 if (I->pred_begin() != I->pred_end())
Chris Lattner2ec30b52005-11-21 07:39:22 +0000261 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_"
262 << I->getNumber() << ":\t"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000263 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
264 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
265 II != E; ++II) {
266 // Print the assembly for the instruction.
267 O << "\t";
268 printMachineInstruction(II);
269 }
270 }
271
272 // We didn't modify anything.
273 return false;
274}
275
276void IA64AsmPrinter::printOp(const MachineOperand &MO,
277 bool isBRCALLinsn /* = false */) {
278 const MRegisterInfo &RI = *TM.getRegisterInfo();
279 switch (MO.getType()) {
280 case MachineOperand::MO_VirtualRegister:
281 if (Value *V = MO.getVRegValueOrNull()) {
282 O << "<" << V->getName() << ">";
283 return;
284 }
285 // FALLTHROUGH
286 case MachineOperand::MO_MachineRegister:
287 case MachineOperand::MO_CCRegister: {
288 O << RI.get(MO.getReg()).Name;
289 return;
290 }
291
292 case MachineOperand::MO_SignExtendedImmed:
293 case MachineOperand::MO_UnextendedImmed:
294 O << /*(unsigned int)*/MO.getImmedValue();
295 return;
296 case MachineOperand::MO_MachineBasicBlock: {
297 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner2ec30b52005-11-21 07:39:22 +0000298 O << PrivateGlobalPrefix << "LBB"
299 << Mang->getValueName(MBBOp->getParent()->getFunction())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000300 << "_" << MBBOp->getNumber () << "\t// "
301 << MBBOp->getBasicBlock ()->getName ();
302 return;
303 }
304 case MachineOperand::MO_PCRelativeDisp:
305 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
306 abort ();
307 return;
308
309 case MachineOperand::MO_ConstantPoolIndex: {
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000310 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000311 << MO.getConstantPoolIndex() << ")";
312 return;
313 }
314
315 case MachineOperand::MO_GlobalAddress: {
316
317 // functions need @ltoff(@fptr(fn_name)) form
318 GlobalValue *GV = MO.getGlobal();
319 Function *F = dyn_cast<Function>(GV);
320
321 bool Needfptr=false; // if we're computing an address @ltoff(X), do
322 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000323 // @ltoff(@fptr(X)) ?
324 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000325 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000326
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000327 // if this is the target of a call instruction, we should define
328 // the function somewhere (GNU gas has no problem without this, but
329 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000330
Misha Brukman7847fca2005-04-22 17:54:37 +0000331 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000332 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000333 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000334 if (GV->isExternal()) // e.g. stuff like 'stdin'
335 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000336
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000337 if (!isBRCALLinsn)
338 O << "@ltoff(";
339 if (Needfptr)
340 O << "@fptr(";
341 O << Mang->getValueName(MO.getGlobal());
342 if (Needfptr)
343 O << ")"; // close fptr(
344 if (!isBRCALLinsn)
345 O << ")"; // close ltoff(
346 int Offset = MO.getOffset();
347 if (Offset > 0)
348 O << " + " << Offset;
349 else if (Offset < 0)
350 O << " - " << -Offset;
351 return;
352 }
353 case MachineOperand::MO_ExternalSymbol:
354 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000355 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 return;
357 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000358 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000359 }
360}
361
362/// printMachineInstruction -- Print out a single IA64 LLVM instruction
363/// MI to the current output stream.
364///
365void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000366 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000367
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000368 // Call the autogenerated instruction printer routines.
369 printInstruction(MI);
370}
371
372bool IA64AsmPrinter::doInitialization(Module &M) {
373 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000374
Duraid Madinaae8bd732005-03-28 15:21:43 +0000375 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000376 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000377 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000378 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000379 return false;
380}
381
382/// createIA64CodePrinterPass - Returns a pass that prints the IA64
383/// assembly code for a MachineFunction to the given output stream, using
384/// the given target machine description.
385///
386FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
387 return new IA64AsmPrinter(o, tm);
388}
389
390