blob: 6ce530db4c07f1d092283c39964bc15bf9f3eab8 [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) {
Dan Gohman1e57df32008-02-10 18:45:23 +000056 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
57 "Not physref??");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 //XXX Bug Workaround: See note in Printer::doInitialization about %.
Bill Wendling8eeb9792008-02-26 21:11:01 +000059 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 } else {
61 printOp(MO);
62 }
63 }
64
65 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000066 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 if(val>=128) val=val-256; // if negative, flip sign
68 O << val;
69 }
70 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000071 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 if(val>=8192) val=val-16384; // if negative, flip sign
73 O << val;
74 }
75 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000076 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 if(val>=2097152) val=val-4194304; // if negative, flip sign
78 O << val;
79 }
80 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000081 O << (uint64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 }
83 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
84// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
85// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
86// emit movl rX = @gprel(CPI<whatever);;
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000087// add rX = rX, r1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088// this gives us 64 bits instead of 22 (for the add long imm) to play
89// with, which shuts up the linker. The problem is that the constant
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000090// pool entries aren't immediates at this stage, so we check here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091// If it's an immediate, print it the old fashioned way. If it's
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000092// not, we print it as a constant pool index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 if(MI->getOperand(OpNo).isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000094 O << (int64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 } else { // this is a constant pool reference: FIXME: assert this
96 printOp(MI->getOperand(OpNo));
97 }
98 }
99
100 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
101 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
102 }
103
104 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
105 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
106 }
107
108 std::string getSectionForFunction(const Function &F) const;
109
110 void printMachineInstruction(const MachineInstr *MI);
111 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000112 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 bool runOnMachineFunction(MachineFunction &F);
114 bool doInitialization(Module &M);
115 bool doFinalization(Module &M);
116 };
117} // end of anonymous namespace
118
119
120// Include the auto-generated portion of the assembly writer.
121#include "IA64GenAsmWriter.inc"
122
123
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000124// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000126 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127}
128
129/// runOnMachineFunction - This uses the printMachineInstruction()
130/// method to print assembly for each instruction.
131///
132bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
133 SetupMachineFunction(MF);
134 O << "\n\n";
135
136 // Print out constants referenced by the function
137 EmitConstantPool(MF.getConstantPool());
138
139 const Function *F = MF.getFunction();
140 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
141
142 // Print out labels for the function.
143 EmitAlignment(5);
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000144 O << "\t.global\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000145
146 printVisibility(CurrentFnName, F->getVisibility());
147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 O << "\t.type\t" << CurrentFnName << ", @function\n";
149 O << CurrentFnName << ":\n";
150
151 // Print out code for the function.
152 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
153 I != E; ++I) {
154 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000155 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000156 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 O << '\n';
158 }
159 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
160 II != E; ++II) {
161 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 printMachineInstruction(II);
163 }
164 }
165
166 // We didn't modify anything.
167 return false;
168}
169
170void IA64AsmPrinter::printOp(const MachineOperand &MO,
171 bool isBRCALLinsn /* = false */) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000172 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 switch (MO.getType()) {
174 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +0000175 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 return;
177
178 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000179 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 return;
181 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000182 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 return;
184 case MachineOperand::MO_ConstantPoolIndex: {
185 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
Chris Lattner6017d482007-12-30 23:10:15 +0000186 << "CPI" << getFunctionNumber() << "_" << MO.getIndex() << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 return;
188 }
189
190 case MachineOperand::MO_GlobalAddress: {
191
192 // functions need @ltoff(@fptr(fn_name)) form
193 GlobalValue *GV = MO.getGlobal();
194 Function *F = dyn_cast<Function>(GV);
195
196 bool Needfptr=false; // if we're computing an address @ltoff(X), do
197 // we need to decorate it so it becomes
198 // @ltoff(@fptr(X)) ?
199 if (F && !isBRCALLinsn /*&& F->isDeclaration()*/)
200 Needfptr=true;
201
202 // if this is the target of a call instruction, we should define
203 // the function somewhere (GNU gas has no problem without this, but
204 // Intel ias rightly complains of an 'undefined symbol')
205
206 if (F /*&& isBRCALLinsn*/ && F->isDeclaration())
207 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
208 else
209 if (GV->isDeclaration()) // e.g. stuff like 'stdin'
210 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
211
212 if (!isBRCALLinsn)
213 O << "@ltoff(";
214 if (Needfptr)
215 O << "@fptr(";
216 O << Mang->getValueName(MO.getGlobal());
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 if (Needfptr && !isBRCALLinsn)
219 O << "#))"; // close both fptr( and ltoff(
220 else {
221 if (Needfptr)
222 O << "#)"; // close only fptr(
223 if (!isBRCALLinsn)
224 O << "#)"; // close only ltoff(
225 }
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 int Offset = MO.getOffset();
228 if (Offset > 0)
229 O << " + " << Offset;
230 else if (Offset < 0)
231 O << " - " << -Offset;
232 return;
233 }
234 case MachineOperand::MO_ExternalSymbol:
235 O << MO.getSymbolName();
236 ExternalFunctionNames.insert(MO.getSymbolName());
237 return;
238 default:
239 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
240 }
241}
242
243/// printMachineInstruction -- Print out a single IA64 LLVM instruction
244/// MI to the current output stream.
245///
246void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
247 ++EmittedInsts;
248
249 // Call the autogenerated instruction printer routines.
250 printInstruction(MI);
251}
252
253bool IA64AsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000254 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
256 O << "\n.ident \"LLVM-ia64\"\n\n"
257 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
258 << "\t.radix C\n"
259 << "\t.psr abi64\n"; // we only support 64 bits for now
Dan Gohman4a558a32007-07-25 19:33:14 +0000260 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261}
262
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000263void IA64AsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000265
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000266 if (!GVar->hasInitializer())
267 return; // External global require no code
268
269 // Check to see if this is a special global used by LLVM, if so, emit it.
270 if (EmitSpecialLLVMGlobal(GVar))
271 return;
272
273 O << "\n\n";
274 std::string SectionName = TAI->SectionForGlobal(GVar);
275 std::string name = Mang->getValueName(GVar);
276 Constant *C = GVar->getInitializer();
277 unsigned Size = TD->getABITypeSize(C->getType());
278 unsigned Align = TD->getPreferredAlignmentLog(GVar);
279
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000280 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000281
282 SwitchToDataSection(SectionName.c_str());
283
284 if (C->isNullValue() && !GVar->hasSection()) {
285 if (!GVar->isThreadLocal() &&
286 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
287 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
288
289 if (GVar->hasInternalLinkage()) {
290 O << "\t.lcomm " << name << "#," << Size
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000291 << ',' << (1 << Align);
292 O << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000293 } else {
294 O << "\t.common " << name << "#," << Size
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000295 << ',' << (1 << Align);
296 O << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000297 }
298
299 return;
300 }
301 }
302
303 switch (GVar->getLinkage()) {
304 case GlobalValue::LinkOnceLinkage:
305 case GlobalValue::CommonLinkage:
306 case GlobalValue::WeakLinkage:
307 // Nonnull linkonce -> weak
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000308 O << "\t.weak " << name << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000309 break;
310 case GlobalValue::AppendingLinkage:
311 // FIXME: appending linkage variables should go into a section of
312 // their name or something. For now, just emit them as external.
313 case GlobalValue::ExternalLinkage:
314 // If external or appending, declare as a global symbol
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000315 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000316 // FALL THROUGH
317 case GlobalValue::InternalLinkage:
318 break;
319 case GlobalValue::GhostLinkage:
320 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
321 abort();
322 case GlobalValue::DLLImportLinkage:
323 cerr << "DLLImport linkage is not supported by this target!\n";
324 abort();
325 case GlobalValue::DLLExportLinkage:
326 cerr << "DLLExport linkage is not supported by this target!\n";
327 abort();
328 default:
329 assert(0 && "Unknown linkage type!");
330 }
331
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000332 EmitAlignment(Align, GVar);
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000333
334 if (TAI->hasDotTypeDotSizeDirective()) {
335 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000336 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000337 }
338
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000339 O << name << ":\t\t\t\t// " << *C << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000340 EmitGlobalConstant(C);
341}
342
343
344bool IA64AsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Print out module-level global variables here.
346 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
347 I != E; ++I)
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000348 printModuleLevelGV(I);
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000349
350 // we print out ".global X \n .type X, @function" for each external function
351 O << "\n\n// br.call targets referenced (and not defined) above: \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
353 e = ExternalFunctionNames.end(); i!=e; ++i) {
354 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
355 }
356 O << "\n\n";
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 // we print out ".global X \n .type X, @object" for each external object
359 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
360 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
361 e = ExternalObjectNames.end(); i!=e; ++i) {
362 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
363 }
364 O << "\n\n";
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000365
Dan Gohman4a558a32007-07-25 19:33:14 +0000366 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367}
368
369/// 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,
374 IA64TargetMachine &tm) {
375 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
376}