blob: c2a7b93e769e9b00ddbc9f050b05382fcce7c7e9 [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"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/ValueTypes.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Support/Mangler.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/CommandLine.h"
32using namespace llvm;
33
34namespace {
35 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36
37 struct IA64SharedAsmPrinter : public AsmPrinter {
38
Duraid Madinaae8bd732005-03-28 15:21:43 +000039 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
Misha Brukman4633f1c2005-04-21 23:13:11 +000040
Duraid Madina9b9d45f2005-03-17 18:17:03 +000041 IA64SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
42 : AsmPrinter(O, TM) { }
43
44 void printConstantPool(MachineConstantPool *MCP);
45 bool doFinalization(Module &M);
46 };
47}
48
Duraid Madina9b9d45f2005-03-17 18:17:03 +000049/// printConstantPool - Print to the current output stream assembly
50/// representations of the constants in the constant pool MCP. This is
51/// used to print out constants which have been "spilled to memory" by
52/// the code generator.
53///
54void IA64SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
55 const std::vector<Constant*> &CP = MCP->getConstants();
56 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +000057
Duraid Madina9b9d45f2005-03-17 18:17:03 +000058 if (CP.empty()) return;
59
Chris Lattner4bfa3a32005-11-21 07:26:04 +000060 // FIXME: would be nice to have rodata (no 'w') when appropriate?
61 SwitchSection("\n\t.section .data, \"aw\", \"progbits\"\n", 0);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000062 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000063 EmitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
Chris Lattner81a994e2005-11-21 06:51:52 +000064 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
65 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +000066 EmitGlobalConstant(CP[i]);
Duraid Madina9b9d45f2005-03-17 18:17:03 +000067 }
68}
69
70bool IA64SharedAsmPrinter::doFinalization(Module &M) {
71 const TargetData &TD = TM.getTargetData();
Duraid Madina9b9d45f2005-03-17 18:17:03 +000072
73 // Print out module-level global variables here.
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +000074 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
75 I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +000076 if (I->hasInitializer()) { // External global require no code
77 O << "\n\n";
78 std::string name = Mang->getValueName(I);
79 Constant *C = I->getInitializer();
80 unsigned Size = TD.getTypeSize(C->getType());
81 unsigned Align = TD.getTypeAlignmentShift(C->getType());
82
Misha Brukman4633f1c2005-04-21 23:13:11 +000083 if (C->isNullValue() &&
Duraid Madina9b9d45f2005-03-17 18:17:03 +000084 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
85 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4bfa3a32005-11-21 07:26:04 +000086 SwitchSection(".data", I);
Duraid Madina1f867b12005-03-31 07:40:24 +000087 if (I->hasInternalLinkage()) {
Misha Brukman7847fca2005-04-22 17:54:37 +000088 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
89 << "," << (1 << Align);
90 O << "\t\t// ";
91 } else {
92 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
93 << "," << (1 << Align);
94 O << "\t\t// ";
95 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +000096 WriteAsOperand(O, I, true, true, &M);
97 O << "\n";
98 } else {
99 switch (I->getLinkage()) {
100 case GlobalValue::LinkOnceLinkage:
101 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
102 // Nonnull linkonce -> weak
103 O << "\t.weak " << name << "\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000104 O << "\t.section\t.llvm.linkonce.d." << name
Misha Brukman7847fca2005-04-22 17:54:37 +0000105 << ", \"aw\", \"progbits\"\n";
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000106 SwitchSection("", I);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000107 break;
108 case GlobalValue::AppendingLinkage:
109 // FIXME: appending linkage variables should go into a section of
110 // their name or something. For now, just emit them as external.
111 case GlobalValue::ExternalLinkage:
112 // If external or appending, declare as a global symbol
113 O << "\t.global " << name << "\n";
114 // FALL THROUGH
115 case GlobalValue::InternalLinkage:
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000116 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000117 break;
118 case GlobalValue::GhostLinkage:
119 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
120 abort();
121 }
122
Chris Lattner8b8b9512005-11-21 07:51:23 +0000123 EmitAlignment(Align);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000124 O << "\t.type " << name << ",@object\n";
125 O << "\t.size " << name << "," << Size << "\n";
126 O << name << ":\t\t\t\t// ";
127 WriteAsOperand(O, I, true, true, &M);
128 O << " = ";
129 WriteAsOperand(O, C, false, false, &M);
130 O << "\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000131 EmitGlobalConstant(C);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000132 }
133 }
134
135 // we print out ".global X \n .type X, @function" for each external function
136 O << "\n\n// br.call targets referenced (and not defined) above: \n";
137 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
138 e = ExternalFunctionNames.end(); i!=e; ++i) {
139 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
140 }
141 O << "\n\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000142
Duraid Madinaae8bd732005-03-28 15:21:43 +0000143 // we print out ".global X \n .type X, @object" for each external object
144 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
145 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
146 e = ExternalObjectNames.end(); i!=e; ++i) {
147 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
148 }
149 O << "\n\n";
150
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000151 AsmPrinter::doFinalization(M);
152 return false; // success
153}
154
155namespace {
156 struct IA64AsmPrinter : public IA64SharedAsmPrinter {
157 IA64AsmPrinter(std::ostream &O, TargetMachine &TM)
158 : IA64SharedAsmPrinter(O, TM) {
159
160 CommentString = "//";
Duraid Madina32c46f32005-04-02 12:30:47 +0000161 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
162 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
163 Data32bitsDirective = "\tdata4.ua\t"; // properly
164 Data64bitsDirective = "\tdata8.ua\t";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000165 ZeroDirective = "\t.skip\t";
166 AsciiDirective = "\tstring\t";
167
Duraid Madina32c46f32005-04-02 12:30:47 +0000168 GlobalVarAddrPrefix="";
169 GlobalVarAddrSuffix="";
170 FunctionAddrPrefix="@fptr(";
171 FunctionAddrSuffix=")";
172
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000173 }
174
175 virtual const char *getPassName() const {
176 return "IA64 Assembly Printer";
177 }
178
179 /// printInstruction - This method is automatically generated by tablegen
180 /// from the instruction set description. This method returns true if the
181 /// machine instruction was sufficiently described to print it, otherwise it
182 /// returns false.
183 bool printInstruction(const MachineInstr *MI);
184
185 // This method is used by the tablegen'erated instruction printer.
186 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
187 const MachineOperand &MO = MI->getOperand(OpNo);
188 if (MO.getType() == MachineOperand::MO_MachineRegister) {
189 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
190 //XXX Bug Workaround: See note in Printer::doInitialization about %.
191 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
192 } else {
193 printOp(MO);
194 }
195 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000196
Duraid Madinae6a0b6c2005-04-07 12:34:36 +0000197 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo,
198 MVT::ValueType VT) {
199 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
200 if(val>=128) val=val-256; // if negative, flip sign
201 O << val;
202 }
203 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo,
204 MVT::ValueType VT) {
205 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
206 if(val>=8192) val=val-16384; // if negative, flip sign
207 O << val;
208 }
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000209 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo,
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000210 MVT::ValueType VT) {
Duraid Madina5ef2ec92005-04-11 05:55:56 +0000211 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
212 if(val>=2097152) val=val-4194304; // if negative, flip sign
213 O << val;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000214 }
215 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
216 MVT::ValueType VT) {
217 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
218 }
Duraid Madina1ce0c012005-04-14 10:08:01 +0000219 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo,
220 MVT::ValueType VT) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000221// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
222// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
223// emit movl rX = @gprel(CPI<whatever);;
224// add rX = rX, r1;
225// this gives us 64 bits instead of 22 (for the add long imm) to play
226// with, which shuts up the linker. The problem is that the constant
227// pool entries aren't immediates at this stage, so we check here.
228// If it's an immediate, print it the old fashioned way. If it's
229// not, we print it as a constant pool index.
230 if(MI->getOperand(OpNo).isImmediate()) {
231 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
232 } else { // this is a constant pool reference: FIXME: assert this
233 printOp(MI->getOperand(OpNo));
234 }
235 }
236
237 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo,
238 MVT::ValueType VT) {
239 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
Duraid Madina1ce0c012005-04-14 10:08:01 +0000240 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000241
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000242 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
243 MVT::ValueType VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000245 }
246
247 void printMachineInstruction(const MachineInstr *MI);
248 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000249 bool runOnMachineFunction(MachineFunction &F);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000250 bool doInitialization(Module &M);
251 };
252} // end of anonymous namespace
253
254
255// Include the auto-generated portion of the assembly writer.
256#include "IA64GenAsmWriter.inc"
257
258
259/// runOnMachineFunction - This uses the printMachineInstruction()
260/// method to print assembly for each instruction.
261///
262bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000263 SetupMachineFunction(MF);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000264 O << "\n\n";
265
266 // Print out constants referenced by the function
267 printConstantPool(MF.getConstantPool());
268
269 // Print out labels for the function.
Chris Lattner4bfa3a32005-11-21 07:26:04 +0000270 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction());
271 // ^^ means "Allocated instruXions in mem, initialized"
Chris Lattner8b8b9512005-11-21 07:51:23 +0000272 EmitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000273 O << "\t.global\t" << CurrentFnName << "\n";
274 O << "\t.type\t" << CurrentFnName << ", @function\n";
275 O << CurrentFnName << ":\n";
276
277 // Print out code for the function.
278 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
279 I != E; ++I) {
280 // Print a label for the basic block if there are any predecessors.
281 if (I->pred_begin() != I->pred_end())
Chris Lattner2ec30b52005-11-21 07:39:22 +0000282 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_"
283 << I->getNumber() << ":\t"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000284 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
285 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
286 II != E; ++II) {
287 // Print the assembly for the instruction.
288 O << "\t";
289 printMachineInstruction(II);
290 }
291 }
292
293 // We didn't modify anything.
294 return false;
295}
296
297void IA64AsmPrinter::printOp(const MachineOperand &MO,
298 bool isBRCALLinsn /* = false */) {
299 const MRegisterInfo &RI = *TM.getRegisterInfo();
300 switch (MO.getType()) {
301 case MachineOperand::MO_VirtualRegister:
302 if (Value *V = MO.getVRegValueOrNull()) {
303 O << "<" << V->getName() << ">";
304 return;
305 }
306 // FALLTHROUGH
307 case MachineOperand::MO_MachineRegister:
308 case MachineOperand::MO_CCRegister: {
309 O << RI.get(MO.getReg()).Name;
310 return;
311 }
312
313 case MachineOperand::MO_SignExtendedImmed:
314 case MachineOperand::MO_UnextendedImmed:
315 O << /*(unsigned int)*/MO.getImmedValue();
316 return;
317 case MachineOperand::MO_MachineBasicBlock: {
318 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner2ec30b52005-11-21 07:39:22 +0000319 O << PrivateGlobalPrefix << "LBB"
320 << Mang->getValueName(MBBOp->getParent()->getFunction())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000321 << "_" << MBBOp->getNumber () << "\t// "
322 << MBBOp->getBasicBlock ()->getName ();
323 return;
324 }
325 case MachineOperand::MO_PCRelativeDisp:
326 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
327 abort ();
328 return;
329
330 case MachineOperand::MO_ConstantPoolIndex: {
Chris Lattner81a994e2005-11-21 06:51:52 +0000331 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000332 << MO.getConstantPoolIndex() << ")";
333 return;
334 }
335
336 case MachineOperand::MO_GlobalAddress: {
337
338 // functions need @ltoff(@fptr(fn_name)) form
339 GlobalValue *GV = MO.getGlobal();
340 Function *F = dyn_cast<Function>(GV);
341
342 bool Needfptr=false; // if we're computing an address @ltoff(X), do
343 // we need to decorate it so it becomes
Misha Brukman7847fca2005-04-22 17:54:37 +0000344 // @ltoff(@fptr(X)) ?
345 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000346 Needfptr=true;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000347
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000348 // if this is the target of a call instruction, we should define
349 // the function somewhere (GNU gas has no problem without this, but
350 // Intel ias rightly complains of an 'undefined symbol')
Misha Brukman4633f1c2005-04-21 23:13:11 +0000351
Misha Brukman7847fca2005-04-22 17:54:37 +0000352 if (F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000353 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000354 else
Misha Brukman7847fca2005-04-22 17:54:37 +0000355 if (GV->isExternal()) // e.g. stuff like 'stdin'
356 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000357
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000358 if (!isBRCALLinsn)
359 O << "@ltoff(";
360 if (Needfptr)
361 O << "@fptr(";
362 O << Mang->getValueName(MO.getGlobal());
363 if (Needfptr)
364 O << ")"; // close fptr(
365 if (!isBRCALLinsn)
366 O << ")"; // close ltoff(
367 int Offset = MO.getOffset();
368 if (Offset > 0)
369 O << " + " << Offset;
370 else if (Offset < 0)
371 O << " - " << -Offset;
372 return;
373 }
374 case MachineOperand::MO_ExternalSymbol:
375 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000376 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000377 return;
378 default:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000379 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000380 }
381}
382
383/// printMachineInstruction -- Print out a single IA64 LLVM instruction
384/// MI to the current output stream.
385///
386void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000387 ++EmittedInsts;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000388
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000389 // Call the autogenerated instruction printer routines.
390 printInstruction(MI);
391}
392
393bool IA64AsmPrinter::doInitialization(Module &M) {
394 AsmPrinter::doInitialization(M);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000395
Duraid Madinaae8bd732005-03-28 15:21:43 +0000396 O << "\n.ident \"LLVM-ia64\"\n\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000397 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000398 << "\t.radix C\n"
Misha Brukman7847fca2005-04-22 17:54:37 +0000399 << "\t.psr abi64\n"; // we only support 64 bits for now
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000400 return false;
401}
402
403/// createIA64CodePrinterPass - Returns a pass that prints the IA64
404/// assembly code for a MachineFunction to the given output stream, using
405/// the given target machine description.
406///
407FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
408 return new IA64AsmPrinter(o, tm);
409}
410
411