blob: c6d662678b4fb8c9ae23e751960443a10e5e05e9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
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
14// report in bugzilla. FYI, the not too bad 'ias' assembler is bundled with
15// the Intel C/C++ compiler for Itanium Linux.
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asm-printer"
20#include "IA64.h"
21#include "IA64TargetMachine.h"
22#include "llvm/Module.h"
23#include "llvm/Type.h"
24#include "llvm/CodeGen/AsmPrinter.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/Target/TargetAsmInfo.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
29#include "llvm/ADT/Statistic.h"
30using namespace llvm;
31
32STATISTIC(EmittedInsts, "Number of machine instrs printed");
33
34namespace {
35 struct IA64AsmPrinter : public AsmPrinter {
36 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
37
38 IA64AsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
39 : AsmPrinter(O, TM, T) {
40 }
41
42 virtual const char *getPassName() const {
43 return "IA64 Assembly Printer";
44 }
45
46 /// printInstruction - This method is automatically generated by tablegen
47 /// from the instruction set description. This method returns true if the
48 /// machine instruction was sufficiently described to print it, otherwise it
49 /// returns false.
50 bool printInstruction(const MachineInstr *MI);
51
52 // This method is used by the tablegen'erated instruction printer.
53 void printOperand(const MachineInstr *MI, unsigned OpNo){
54 const MachineOperand &MO = MI->getOperand(OpNo);
55 if (MO.getType() == MachineOperand::MO_Register) {
56 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
57 //XXX Bug Workaround: See note in Printer::doInitialization about %.
58 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
59 } else {
60 printOp(MO);
61 }
62 }
63
64 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000065 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 if(val>=128) val=val-256; // if negative, flip sign
67 O << val;
68 }
69 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000070 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 if(val>=8192) val=val-16384; // if negative, flip sign
72 O << val;
73 }
74 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000075 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 if(val>=2097152) val=val-4194304; // if negative, flip sign
77 O << val;
78 }
79 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000080 O << (uint64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 }
82 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
83// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
84// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
85// emit movl rX = @gprel(CPI<whatever);;
86// add rX = rX, r1;
87// this gives us 64 bits instead of 22 (for the add long imm) to play
88// with, which shuts up the linker. The problem is that the constant
89// pool entries aren't immediates at this stage, so we check here.
90// If it's an immediate, print it the old fashioned way. If it's
91// not, we print it as a constant pool index.
92 if(MI->getOperand(OpNo).isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000093 O << (int64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 } else { // this is a constant pool reference: FIXME: assert this
95 printOp(MI->getOperand(OpNo));
96 }
97 }
98
99 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
100 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
101 }
102
103 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
104 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
105 }
106
107 std::string getSectionForFunction(const Function &F) const;
108
109 void printMachineInstruction(const MachineInstr *MI);
110 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
111 bool runOnMachineFunction(MachineFunction &F);
112 bool doInitialization(Module &M);
113 bool doFinalization(Module &M);
114 };
115} // end of anonymous namespace
116
117
118// Include the auto-generated portion of the assembly writer.
119#include "IA64GenAsmWriter.inc"
120
121
122std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const {
123 // This means "Allocated instruXions in mem, initialized".
124 return "\n\t.section .text, \"ax\", \"progbits\"\n";
125}
126
127/// runOnMachineFunction - This uses the printMachineInstruction()
128/// method to print assembly for each instruction.
129///
130bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
131 SetupMachineFunction(MF);
132 O << "\n\n";
133
134 // Print out constants referenced by the function
135 EmitConstantPool(MF.getConstantPool());
136
137 const Function *F = MF.getFunction();
138 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
139
140 // Print out labels for the function.
141 EmitAlignment(5);
142 O << "\t.global\t" << CurrentFnName << "\n";
143 O << "\t.type\t" << CurrentFnName << ", @function\n";
144 O << CurrentFnName << ":\n";
145
146 // Print out code for the function.
147 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
148 I != E; ++I) {
149 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000150 if (!I->pred_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 printBasicBlockLabel(I, true);
152 O << '\n';
153 }
154 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
155 II != E; ++II) {
156 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 printMachineInstruction(II);
158 }
159 }
160
161 // We didn't modify anything.
162 return false;
163}
164
165void IA64AsmPrinter::printOp(const MachineOperand &MO,
166 bool isBRCALLinsn /* = false */) {
167 const MRegisterInfo &RI = *TM.getRegisterInfo();
168 switch (MO.getType()) {
169 case MachineOperand::MO_Register:
170 O << RI.get(MO.getReg()).Name;
171 return;
172
173 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000174 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 return;
176 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000177 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 return;
179 case MachineOperand::MO_ConstantPoolIndex: {
180 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
Chris Lattner6017d482007-12-30 23:10:15 +0000181 << "CPI" << getFunctionNumber() << "_" << MO.getIndex() << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 return;
183 }
184
185 case MachineOperand::MO_GlobalAddress: {
186
187 // functions need @ltoff(@fptr(fn_name)) form
188 GlobalValue *GV = MO.getGlobal();
189 Function *F = dyn_cast<Function>(GV);
190
191 bool Needfptr=false; // if we're computing an address @ltoff(X), do
192 // we need to decorate it so it becomes
193 // @ltoff(@fptr(X)) ?
194 if (F && !isBRCALLinsn /*&& F->isDeclaration()*/)
195 Needfptr=true;
196
197 // if this is the target of a call instruction, we should define
198 // the function somewhere (GNU gas has no problem without this, but
199 // Intel ias rightly complains of an 'undefined symbol')
200
201 if (F /*&& isBRCALLinsn*/ && F->isDeclaration())
202 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
203 else
204 if (GV->isDeclaration()) // e.g. stuff like 'stdin'
205 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
206
207 if (!isBRCALLinsn)
208 O << "@ltoff(";
209 if (Needfptr)
210 O << "@fptr(";
211 O << Mang->getValueName(MO.getGlobal());
212
213 if (Needfptr && !isBRCALLinsn)
214 O << "#))"; // close both fptr( and ltoff(
215 else {
216 if (Needfptr)
217 O << "#)"; // close only fptr(
218 if (!isBRCALLinsn)
219 O << "#)"; // close only ltoff(
220 }
221
222 int Offset = MO.getOffset();
223 if (Offset > 0)
224 O << " + " << Offset;
225 else if (Offset < 0)
226 O << " - " << -Offset;
227 return;
228 }
229 case MachineOperand::MO_ExternalSymbol:
230 O << MO.getSymbolName();
231 ExternalFunctionNames.insert(MO.getSymbolName());
232 return;
233 default:
234 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
235 }
236}
237
238/// printMachineInstruction -- Print out a single IA64 LLVM instruction
239/// MI to the current output stream.
240///
241void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
242 ++EmittedInsts;
243
244 // Call the autogenerated instruction printer routines.
245 printInstruction(MI);
246}
247
248bool IA64AsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000249 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
251 O << "\n.ident \"LLVM-ia64\"\n\n"
252 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
253 << "\t.radix C\n"
254 << "\t.psr abi64\n"; // we only support 64 bits for now
Dan Gohman4a558a32007-07-25 19:33:14 +0000255 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256}
257
258bool IA64AsmPrinter::doFinalization(Module &M) {
259 const TargetData *TD = TM.getTargetData();
260
261 // Print out module-level global variables here.
262 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
263 I != E; ++I)
264 if (I->hasInitializer()) { // External global require no code
265 // Check to see if this is a special global used by LLVM, if so, emit it.
266 if (EmitSpecialLLVMGlobal(I))
267 continue;
268
269 O << "\n\n";
270 std::string name = Mang->getValueName(I);
271 Constant *C = I->getInitializer();
Duncan Sands8157ef42007-11-05 00:04:43 +0000272 unsigned Size = TD->getABITypeSize(C->getType());
Duncan Sands935686e2008-01-29 06:23:44 +0000273 unsigned Align = TD->getPreferredAlignmentLog(I);
274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (C->isNullValue() &&
276 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
277 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
278 SwitchToDataSection(".data", I);
279 if (I->hasInternalLinkage()) {
Duncan Sands8157ef42007-11-05 00:04:43 +0000280 O << "\t.lcomm " << name << "#," << TD->getABITypeSize(C->getType())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 << "," << (1 << Align);
282 O << "\n";
283 } else {
Duncan Sands8157ef42007-11-05 00:04:43 +0000284 O << "\t.common " << name << "#," << TD->getABITypeSize(C->getType())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 << "," << (1 << Align);
286 O << "\n";
287 }
288 } else {
289 switch (I->getLinkage()) {
290 case GlobalValue::LinkOnceLinkage:
291 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
292 // Nonnull linkonce -> weak
293 O << "\t.weak " << name << "\n";
294 O << "\t.section\t.llvm.linkonce.d." << name
295 << ", \"aw\", \"progbits\"\n";
296 SwitchToDataSection("", I);
297 break;
298 case GlobalValue::AppendingLinkage:
299 // FIXME: appending linkage variables should go into a section of
300 // their name or something. For now, just emit them as external.
301 case GlobalValue::ExternalLinkage:
302 // If external or appending, declare as a global symbol
303 O << "\t.global " << name << "\n";
304 // FALL THROUGH
305 case GlobalValue::InternalLinkage:
306 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
307 break;
308 case GlobalValue::GhostLinkage:
309 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
310 abort();
311 case GlobalValue::DLLImportLinkage:
312 cerr << "DLLImport linkage is not supported by this target!\n";
313 abort();
314 case GlobalValue::DLLExportLinkage:
315 cerr << "DLLExport linkage is not supported by this target!\n";
316 abort();
317 default:
318 assert(0 && "Unknown linkage type!");
319 }
320
321 EmitAlignment(Align);
322 O << "\t.type " << name << ",@object\n";
323 O << "\t.size " << name << "," << Size << "\n";
324 O << name << ":\t\t\t\t// " << *C << "\n";
325 EmitGlobalConstant(C);
326 }
327 }
328
329 // we print out ".global X \n .type X, @function" for each external function
330 O << "\n\n// br.call targets referenced (and not defined) above: \n";
331 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
332 e = ExternalFunctionNames.end(); i!=e; ++i) {
333 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
334 }
335 O << "\n\n";
336
337 // we print out ".global X \n .type X, @object" for each external object
338 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
339 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
340 e = ExternalObjectNames.end(); i!=e; ++i) {
341 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
342 }
343 O << "\n\n";
344
Dan Gohman4a558a32007-07-25 19:33:14 +0000345 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346}
347
348/// createIA64CodePrinterPass - Returns a pass that prints the IA64
349/// assembly code for a MachineFunction to the given output stream, using
350/// the given target machine description.
351///
352FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
353 IA64TargetMachine &tm) {
354 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
355}
356
357