blob: d779104701c9e1c130354ae3afe87648f6468253 [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"
Duraid Madina9b9d45f2005-03-17 18:17:03 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Mangler.h"
28#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000029#include <iostream>
Duraid Madina9b9d45f2005-03-17 18:17:03 +000030using namespace llvm;
31
32namespace {
33 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34
Chris Lattner42a76cd2005-11-21 08:40:17 +000035 struct IA64AsmPrinter : public AsmPrinter {
Duraid Madinaae8bd732005-03-28 15:21:43 +000036 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
Misha Brukman4633f1c2005-04-21 23:13:11 +000037
Chris Lattner42a76cd2005-11-21 08:40:17 +000038 IA64AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000039 CommentString = "//";
Duraid Madina32c46f32005-04-02 12:30:47 +000040 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
41 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
42 Data32bitsDirective = "\tdata4.ua\t"; // properly
43 Data64bitsDirective = "\tdata8.ua\t";
Duraid Madina9b9d45f2005-03-17 18:17:03 +000044 ZeroDirective = "\t.skip\t";
45 AsciiDirective = "\tstring\t";
46
Duraid Madina32c46f32005-04-02 12:30:47 +000047 GlobalVarAddrPrefix="";
48 GlobalVarAddrSuffix="";
49 FunctionAddrPrefix="@fptr(";
50 FunctionAddrSuffix=")";
Chris Lattner3dfbe2e2005-11-21 08:38:26 +000051
52 // FIXME: would be nice to have rodata (no 'w') when appropriate?
53 ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +000054 }
55
56 virtual const char *getPassName() const {
57 return "IA64 Assembly Printer";
58 }
59
60 /// printInstruction - This method is automatically generated by tablegen
61 /// from the instruction set description. This method returns true if the
62 /// machine instruction was sufficiently described to print it, otherwise it
63 /// returns false.
64 bool printInstruction(const MachineInstr *MI);
65
66 // This method is used by the tablegen'erated instruction printer.
Nate Begeman391c5d22005-11-30 18:54:35 +000067 void printOperand(const MachineInstr *MI, unsigned OpNo){
Duraid Madina9b9d45f2005-03-17 18:17:03 +000068 const MachineOperand &MO = MI->getOperand(OpNo);
69 if (MO.getType() == MachineOperand::MO_MachineRegister) {
70 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
71 //XXX Bug Workaround: See note in Printer::doInitialization about %.
72 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
73 } else {
74 printOp(MO);
75 }
76 }
Misha Brukman4633f1c2005-04-21 23:13:11 +000077
Nate Begeman391c5d22005-11-30 18:54:35 +000078 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000079 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
80 if(val>=128) val=val-256; // if negative, flip sign
81 O << val;
82 }
Nate Begeman391c5d22005-11-30 18:54:35 +000083 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000084 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
85 if(val>=8192) val=val-16384; // if negative, flip sign
86 O << val;
87 }
Nate Begeman391c5d22005-11-30 18:54:35 +000088 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +000089 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
90 if(val>=2097152) val=val-4194304; // if negative, flip sign
91 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +000092 }
Nate Begeman391c5d22005-11-30 18:54:35 +000093 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000094 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
95 }
Nate Begeman391c5d22005-11-30 18:54:35 +000096 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +000097// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
98// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
99// emit movl rX = @gprel(CPI<whatever);;
100// add rX = rX, r1;
101// this gives us 64 bits instead of 22 (for the add long imm) to play
102// with, which shuts up the linker. The problem is that the constant
103// pool entries aren't immediates at this stage, so we check here.
104// If it's an immediate, print it the old fashioned way. If it's
105// not, we print it as a constant pool index.
106 if(MI->getOperand(OpNo).isImmediate()) {
107 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
108 } else { // this is a constant pool reference: FIXME: assert this
109 printOp(MI->getOperand(OpNo));
110 }
111 }
112
Nate Begeman391c5d22005-11-30 18:54:35 +0000113 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000114 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000115 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000116
Nate Begeman391c5d22005-11-30 18:54:35 +0000117 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000118 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000119 }
120
121 void printMachineInstruction(const MachineInstr *MI);
122 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000123 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000124 bool doInitialization(Module &M);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000125 bool doFinalization(Module &M);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000126 };
127} // end of anonymous namespace
128
129
130// Include the auto-generated portion of the assembly writer.
131#include "IA64GenAsmWriter.inc"
132
133
134/// runOnMachineFunction - This uses the printMachineInstruction()
135/// method to print assembly for each instruction.
136///
137bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000138 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000139 O << "\n\n";
140
141 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000142 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000143
144 // Print out labels for the function.
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000145 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction());
146 // ^^ means "Allocated instruXions in mem, initialized"
Chris Lattner8b8b9512005-11-21 07:51:23 +0000147 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000148 O << "\t.global\t" << CurrentFnName << "\n";
149 O << "\t.type\t" << CurrentFnName << ", @function\n";
150 O << CurrentFnName << ":\n";
151
152 // Print out code for the function.
153 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
154 I != E; ++I) {
155 // Print a label for the basic block if there are any predecessors.
156 if (I->pred_begin() != I->pred_end())
Chris Lattner2ec30b52005-11-21 07:39:22 +0000157 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_"
158 << I->getNumber() << ":\t"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000159 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
161 II != E; ++II) {
162 // Print the assembly for the instruction.
163 O << "\t";
164 printMachineInstruction(II);
165 }
166 }
167
168 // We didn't modify anything.
169 return false;
170}
171
172void IA64AsmPrinter::printOp(const MachineOperand &MO,
173 bool isBRCALLinsn /* = false */) {
174 const MRegisterInfo &RI = *TM.getRegisterInfo();
175 switch (MO.getType()) {
176 case MachineOperand::MO_VirtualRegister:
177 if (Value *V = MO.getVRegValueOrNull()) {
178 O << "<" << V->getName() << ">";
179 return;
180 }
181 // FALLTHROUGH
182 case MachineOperand::MO_MachineRegister:
183 case MachineOperand::MO_CCRegister: {
184 O << RI.get(MO.getReg()).Name;
185 return;
186 }
187
188 case MachineOperand::MO_SignExtendedImmed:
189 case MachineOperand::MO_UnextendedImmed:
190 O << /*(unsigned int)*/MO.getImmedValue();
191 return;
192 case MachineOperand::MO_MachineBasicBlock: {
193 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner2ec30b52005-11-21 07:39:22 +0000194 O << PrivateGlobalPrefix << "LBB"
195 << Mang->getValueName(MBBOp->getParent()->getFunction())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000196 << "_" << MBBOp->getNumber () << "\t// "
197 << MBBOp->getBasicBlock ()->getName ();
198 return;
199 }
200 case MachineOperand::MO_PCRelativeDisp:
201 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
202 abort ();
203 return;
204
205 case MachineOperand::MO_ConstantPoolIndex: {
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000206 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000207 << MO.getConstantPoolIndex() << ")";
208 return;
209 }
210
211 case MachineOperand::MO_GlobalAddress: {
212
213 // functions need @ltoff(@fptr(fn_name)) form
214 GlobalValue *GV = MO.getGlobal();
215 Function *F = dyn_cast<Function>(GV);
216
217 bool Needfptr=false; // if we're computing an address @ltoff(X), do
218 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000219 // @ltoff(@fptr(X)) ?
220 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000221 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000222
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000223 // if this is the target of a call instruction, we should define
224 // the function somewhere (GNU gas has no problem without this, but
225 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000226
Misha Brukman7847fca2005-04-22 17:54:37 +0000227 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000228 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000229 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000230 if (GV->isExternal()) // e.g. stuff like 'stdin'
231 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000232
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000233 if (!isBRCALLinsn)
234 O << "@ltoff(";
235 if (Needfptr)
236 O << "@fptr(";
237 O << Mang->getValueName(MO.getGlobal());
Duraid Madina00d3d442006-02-16 13:12:57 +0000238
239 if (Needfptr && !isBRCALLinsn)
240 O << "#))"; // close both fptr( and ltoff(
241 else {
242 if (Needfptr)
243 O << "#)"; // close only fptr(
244 if (!isBRCALLinsn)
245 O << "#)"; // close only ltoff(
246 }
247
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000248 int Offset = MO.getOffset();
249 if (Offset > 0)
250 O << " + " << Offset;
251 else if (Offset < 0)
252 O << " - " << -Offset;
253 return;
254 }
255 case MachineOperand::MO_ExternalSymbol:
256 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000257 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000258 return;
259 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000261 }
262}
263
264/// printMachineInstruction -- Print out a single IA64 LLVM instruction
265/// MI to the current output stream.
266///
267void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000268 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000270 // Call the autogenerated instruction printer routines.
271 printInstruction(MI);
272}
273
274bool IA64AsmPrinter::doInitialization(Module &M) {
275 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000276
Duraid Madinaae8bd732005-03-28 15:21:43 +0000277 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000278 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000279 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000280 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000281 return false;
282}
283
Chris Lattner42a76cd2005-11-21 08:40:17 +0000284bool IA64AsmPrinter::doFinalization(Module &M) {
285 const TargetData &TD = TM.getTargetData();
286
287 // Print out module-level global variables here.
288 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
289 I != E; ++I)
290 if (I->hasInitializer()) { // External global require no code
291 O << "\n\n";
292 std::string name = Mang->getValueName(I);
293 Constant *C = I->getInitializer();
294 unsigned Size = TD.getTypeSize(C->getType());
295 unsigned Align = TD.getTypeAlignmentShift(C->getType());
296
297 if (C->isNullValue() &&
298 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
299 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
300 SwitchSection(".data", I);
301 if (I->hasInternalLinkage()) {
Duraid Madina00d3d442006-02-16 13:12:57 +0000302 O << "\t.lcomm " << name << "#," << TD.getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000303 << "," << (1 << Align);
304 O << "\t\t// ";
305 } else {
Duraid Madina00d3d442006-02-16 13:12:57 +0000306 O << "\t.common " << name << "#," << TD.getTypeSize(C->getType())
Chris Lattner42a76cd2005-11-21 08:40:17 +0000307 << "," << (1 << Align);
308 O << "\t\t// ";
309 }
310 WriteAsOperand(O, I, true, true, &M);
311 O << "\n";
312 } else {
313 switch (I->getLinkage()) {
314 case GlobalValue::LinkOnceLinkage:
315 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
316 // Nonnull linkonce -> weak
317 O << "\t.weak " << name << "\n";
318 O << "\t.section\t.llvm.linkonce.d." << name
319 << ", \"aw\", \"progbits\"\n";
320 SwitchSection("", I);
321 break;
322 case GlobalValue::AppendingLinkage:
323 // FIXME: appending linkage variables should go into a section of
324 // their name or something. For now, just emit them as external.
325 case GlobalValue::ExternalLinkage:
326 // If external or appending, declare as a global symbol
327 O << "\t.global " << name << "\n";
328 // FALL THROUGH
329 case GlobalValue::InternalLinkage:
330 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
331 break;
332 case GlobalValue::GhostLinkage:
333 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
334 abort();
335 }
336
337 EmitAlignment(Align);
338 O << "\t.type " << name << ",@object\n";
339 O << "\t.size " << name << "," << Size << "\n";
340 O << name << ":\t\t\t\t// ";
341 WriteAsOperand(O, I, true, true, &M);
342 O << " = ";
343 WriteAsOperand(O, C, false, false, &M);
344 O << "\n";
345 EmitGlobalConstant(C);
346 }
347 }
348
349 // we print out ".global X \n .type X, @function" for each external function
350 O << "\n\n// br.call targets referenced (and not defined) above: \n";
351 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
352 e = ExternalFunctionNames.end(); i!=e; ++i) {
353 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
354 }
355 O << "\n\n";
356
357 // we print out ".global X \n .type X, @object" for each external object
358 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
359 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
360 e = ExternalObjectNames.end(); i!=e; ++i) {
361 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
362 }
363 O << "\n\n";
364
365 AsmPrinter::doFinalization(M);
366 return false; // success
367}
368
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000369/// createIA64CodePrinterPass - Returns a pass that prints the IA64
370/// assembly code for a MachineFunction to the given output stream, using
371/// the given target machine description.
372///
373FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
374 return new IA64AsmPrinter(o, tm);
375}
376
377