blob: dbb470ba3c7659ef9ecf6d6d24630a8e45e1d303 [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
Chris Lattner42a76cd2005-11-21 08:40:17 +000036 struct IA64AsmPrinter : public AsmPrinter {
Duraid Madinaae8bd732005-03-28 15:21:43 +000037 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
Misha Brukman4633f1c2005-04-21 23:13:11 +000038
Chris Lattner42a76cd2005-11-21 08:40:17 +000039 IA64AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +000040 CommentString = "//";
Duraid Madina32c46f32005-04-02 12:30:47 +000041 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
42 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
43 Data32bitsDirective = "\tdata4.ua\t"; // properly
44 Data64bitsDirective = "\tdata8.ua\t";
Duraid Madina9b9d45f2005-03-17 18:17:03 +000045 ZeroDirective = "\t.skip\t";
46 AsciiDirective = "\tstring\t";
47
Duraid Madina32c46f32005-04-02 12:30:47 +000048 GlobalVarAddrPrefix="";
49 GlobalVarAddrSuffix="";
50 FunctionAddrPrefix="@fptr(";
51 FunctionAddrSuffix=")";
Chris Lattner3dfbe2e2005-11-21 08:38:26 +000052
53 // FIXME: would be nice to have rodata (no 'w') when appropriate?
54 ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +000055 }
56
57 virtual const char *getPassName() const {
58 return "IA64 Assembly Printer";
59 }
60
61 /// printInstruction - This method is automatically generated by tablegen
62 /// from the instruction set description. This method returns true if the
63 /// machine instruction was sufficiently described to print it, otherwise it
64 /// returns false.
65 bool printInstruction(const MachineInstr *MI);
66
67 // This method is used by the tablegen'erated instruction printer.
68 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
69 const MachineOperand &MO = MI->getOperand(OpNo);
70 if (MO.getType() == MachineOperand::MO_MachineRegister) {
71 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
72 //XXX Bug Workaround: See note in Printer::doInitialization about %.
73 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
74 } else {
75 printOp(MO);
76 }
77 }
Misha Brukman4633f1c2005-04-21 23:13:11 +000078
Duraid Madinae6a0b6c2005-04-07 12:34:36 +000079 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo,
80 MVT::ValueType VT) {
81 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
82 if(val>=128) val=val-256; // if negative, flip sign
83 O << val;
84 }
85 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo,
86 MVT::ValueType VT) {
87 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
88 if(val>=8192) val=val-16384; // if negative, flip sign
89 O << val;
90 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +000091 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo,
Duraid Madina9b9d45f2005-03-17 18:17:03 +000092 MVT::ValueType VT) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +000093 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
94 if(val>=2097152) val=val-4194304; // if negative, flip sign
95 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +000096 }
97 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
98 MVT::ValueType VT) {
99 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
100 }
Duraid Madina1ce0c012005-04-14 10:08:01 +0000101 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo,
102 MVT::ValueType VT) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000103// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
104// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
105// emit movl rX = @gprel(CPI<whatever);;
106// add rX = rX, r1;
107// this gives us 64 bits instead of 22 (for the add long imm) to play
108// with, which shuts up the linker. The problem is that the constant
109// pool entries aren't immediates at this stage, so we check here.
110// If it's an immediate, print it the old fashioned way. If it's
111// not, we print it as a constant pool index.
112 if(MI->getOperand(OpNo).isImmediate()) {
113 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
114 } else { // this is a constant pool reference: FIXME: assert this
115 printOp(MI->getOperand(OpNo));
116 }
117 }
118
119 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo,
120 MVT::ValueType VT) {
121 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000122 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000123
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000124 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
125 MVT::ValueType VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000126 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000127 }
128
129 void printMachineInstruction(const MachineInstr *MI);
130 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000131 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000132 bool doInitialization(Module &M);
Chris Lattner42a76cd2005-11-21 08:40:17 +0000133 bool doFinalization(Module &M);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000134 };
135} // end of anonymous namespace
136
137
138// Include the auto-generated portion of the assembly writer.
139#include "IA64GenAsmWriter.inc"
140
141
142/// runOnMachineFunction - This uses the printMachineInstruction()
143/// method to print assembly for each instruction.
144///
145bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000146 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000147 O << "\n\n";
148
149 // Print out constants referenced by the function
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000150 EmitConstantPool(MF.getConstantPool());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000151
152 // Print out labels for the function.
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000153 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction());
154 // ^^ means "Allocated instruXions in mem, initialized"
Chris Lattner8b8b9512005-11-21 07:51:23 +0000155 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000156 O << "\t.global\t" << CurrentFnName << "\n";
157 O << "\t.type\t" << CurrentFnName << ", @function\n";
158 O << CurrentFnName << ":\n";
159
160 // Print out code for the function.
161 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
162 I != E; ++I) {
163 // Print a label for the basic block if there are any predecessors.
164 if (I->pred_begin() != I->pred_end())
Chris Lattner2ec30b52005-11-21 07:39:22 +0000165 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_"
166 << I->getNumber() << ":\t"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000167 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
168 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
169 II != E; ++II) {
170 // Print the assembly for the instruction.
171 O << "\t";
172 printMachineInstruction(II);
173 }
174 }
175
176 // We didn't modify anything.
177 return false;
178}
179
180void IA64AsmPrinter::printOp(const MachineOperand &MO,
181 bool isBRCALLinsn /* = false */) {
182 const MRegisterInfo &RI = *TM.getRegisterInfo();
183 switch (MO.getType()) {
184 case MachineOperand::MO_VirtualRegister:
185 if (Value *V = MO.getVRegValueOrNull()) {
186 O << "<" << V->getName() << ">";
187 return;
188 }
189 // FALLTHROUGH
190 case MachineOperand::MO_MachineRegister:
191 case MachineOperand::MO_CCRegister: {
192 O << RI.get(MO.getReg()).Name;
193 return;
194 }
195
196 case MachineOperand::MO_SignExtendedImmed:
197 case MachineOperand::MO_UnextendedImmed:
198 O << /*(unsigned int)*/MO.getImmedValue();
199 return;
200 case MachineOperand::MO_MachineBasicBlock: {
201 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner2ec30b52005-11-21 07:39:22 +0000202 O << PrivateGlobalPrefix << "LBB"
203 << Mang->getValueName(MBBOp->getParent()->getFunction())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000204 << "_" << MBBOp->getNumber () << "\t// "
205 << MBBOp->getBasicBlock ()->getName ();
206 return;
207 }
208 case MachineOperand::MO_PCRelativeDisp:
209 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
210 abort ();
211 return;
212
213 case MachineOperand::MO_ConstantPoolIndex: {
Chris Lattner3dfbe2e2005-11-21 08:38:26 +0000214 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000215 << MO.getConstantPoolIndex() << ")";
216 return;
217 }
218
219 case MachineOperand::MO_GlobalAddress: {
220
221 // functions need @ltoff(@fptr(fn_name)) form
222 GlobalValue *GV = MO.getGlobal();
223 Function *F = dyn_cast<Function>(GV);
224
225 bool Needfptr=false; // if we're computing an address @ltoff(X), do
226 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000227 // @ltoff(@fptr(X)) ?
228 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000229 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000230
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000231 // if this is the target of a call instruction, we should define
232 // the function somewhere (GNU gas has no problem without this, but
233 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000234
Misha Brukman7847fca2005-04-22 17:54:37 +0000235 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000236 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000237 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000238 if (GV->isExternal()) // e.g. stuff like 'stdin'
239 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000240
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000241 if (!isBRCALLinsn)
242 O << "@ltoff(";
243 if (Needfptr)
244 O << "@fptr(";
245 O << Mang->getValueName(MO.getGlobal());
246 if (Needfptr)
247 O << ")"; // close fptr(
248 if (!isBRCALLinsn)
249 O << ")"; // close ltoff(
250 int Offset = MO.getOffset();
251 if (Offset > 0)
252 O << " + " << Offset;
253 else if (Offset < 0)
254 O << " - " << -Offset;
255 return;
256 }
257 case MachineOperand::MO_ExternalSymbol:
258 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000259 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000260 return;
261 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000262 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000263 }
264}
265
266/// printMachineInstruction -- Print out a single IA64 LLVM instruction
267/// MI to the current output stream.
268///
269void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000270 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000271
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000272 // Call the autogenerated instruction printer routines.
273 printInstruction(MI);
274}
275
276bool IA64AsmPrinter::doInitialization(Module &M) {
277 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000278
Duraid Madinaae8bd732005-03-28 15:21:43 +0000279 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000280 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000281 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000282 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000283 return false;
284}
285
Chris Lattner42a76cd2005-11-21 08:40:17 +0000286bool IA64AsmPrinter::doFinalization(Module &M) {
287 const TargetData &TD = TM.getTargetData();
288
289 // Print out module-level global variables here.
290 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
291 I != E; ++I)
292 if (I->hasInitializer()) { // External global require no code
293 O << "\n\n";
294 std::string name = Mang->getValueName(I);
295 Constant *C = I->getInitializer();
296 unsigned Size = TD.getTypeSize(C->getType());
297 unsigned Align = TD.getTypeAlignmentShift(C->getType());
298
299 if (C->isNullValue() &&
300 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
301 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
302 SwitchSection(".data", I);
303 if (I->hasInternalLinkage()) {
304 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
305 << "," << (1 << Align);
306 O << "\t\t// ";
307 } else {
308 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
309 << "," << (1 << Align);
310 O << "\t\t// ";
311 }
312 WriteAsOperand(O, I, true, true, &M);
313 O << "\n";
314 } else {
315 switch (I->getLinkage()) {
316 case GlobalValue::LinkOnceLinkage:
317 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
318 // Nonnull linkonce -> weak
319 O << "\t.weak " << name << "\n";
320 O << "\t.section\t.llvm.linkonce.d." << name
321 << ", \"aw\", \"progbits\"\n";
322 SwitchSection("", I);
323 break;
324 case GlobalValue::AppendingLinkage:
325 // FIXME: appending linkage variables should go into a section of
326 // their name or something. For now, just emit them as external.
327 case GlobalValue::ExternalLinkage:
328 // If external or appending, declare as a global symbol
329 O << "\t.global " << name << "\n";
330 // FALL THROUGH
331 case GlobalValue::InternalLinkage:
332 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
333 break;
334 case GlobalValue::GhostLinkage:
335 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
336 abort();
337 }
338
339 EmitAlignment(Align);
340 O << "\t.type " << name << ",@object\n";
341 O << "\t.size " << name << "," << Size << "\n";
342 O << name << ":\t\t\t\t// ";
343 WriteAsOperand(O, I, true, true, &M);
344 O << " = ";
345 WriteAsOperand(O, C, false, false, &M);
346 O << "\n";
347 EmitGlobalConstant(C);
348 }
349 }
350
351 // we print out ".global X \n .type X, @function" for each external function
352 O << "\n\n// br.call targets referenced (and not defined) above: \n";
353 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
354 e = ExternalFunctionNames.end(); i!=e; ++i) {
355 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
356 }
357 O << "\n\n";
358
359 // we print out ".global X \n .type X, @object" for each external object
360 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
361 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
362 e = ExternalObjectNames.end(); i!=e; ++i) {
363 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
364 }
365 O << "\n\n";
366
367 AsmPrinter::doFinalization(M);
368 return false; // success
369}
370
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000371/// createIA64CodePrinterPass - Returns a pass that prints the IA64
372/// assembly code for a MachineFunction to the given output stream, using
373/// the given target machine description.
374///
375FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
376 return new IA64AsmPrinter(o, tm);
377}
378
379