blob: 957deba45230806b017955d448345624bcfbc0f3 [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"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/Statistic.h"
31using namespace llvm;
32
33STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
35namespace {
36 struct IA64AsmPrinter : public AsmPrinter {
37 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
38
Owen Anderson847b99b2008-08-21 00:14:44 +000039 IA64AsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 : AsmPrinter(O, TM, T) {
41 }
42
43 virtual const char *getPassName() const {
44 return "IA64 Assembly Printer";
45 }
46
47 /// printInstruction - This method is automatically generated by tablegen
48 /// from the instruction set description. This method returns true if the
49 /// machine instruction was sufficiently described to print it, otherwise it
50 /// returns false.
51 bool printInstruction(const MachineInstr *MI);
52
53 // This method is used by the tablegen'erated instruction printer.
54 void printOperand(const MachineInstr *MI, unsigned OpNo){
55 const MachineOperand &MO = MI->getOperand(OpNo);
56 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman1e57df32008-02-10 18:45:23 +000057 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
58 "Not physref??");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 //XXX Bug Workaround: See note in Printer::doInitialization about %.
Bill Wendling8eeb9792008-02-26 21:11:01 +000060 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 } else {
62 printOp(MO);
63 }
64 }
65
66 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000067 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 if(val>=128) val=val-256; // if negative, flip sign
69 O << val;
70 }
71 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000072 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 if(val>=8192) val=val-16384; // if negative, flip sign
74 O << val;
75 }
76 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000077 int val=(unsigned int)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 if(val>=2097152) val=val-4194304; // if negative, flip sign
79 O << val;
80 }
81 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +000082 O << (uint64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 }
84 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
85// XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
86// errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
87// emit movl rX = @gprel(CPI<whatever);;
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000088// add rX = rX, r1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089// this gives us 64 bits instead of 22 (for the add long imm) to play
90// with, which shuts up the linker. The problem is that the constant
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000091// pool entries aren't immediates at this stage, so we check here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092// If it's an immediate, print it the old fashioned way. If it's
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +000093// not, we print it as a constant pool index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 if(MI->getOperand(OpNo).isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000095 O << (int64_t)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 } else { // this is a constant pool reference: FIXME: assert this
97 printOp(MI->getOperand(OpNo));
98 }
99 }
100
101 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
102 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
103 }
104
105 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
106 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
107 }
108
109 std::string getSectionForFunction(const Function &F) const;
110
111 void printMachineInstruction(const MachineInstr *MI);
112 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000113 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 bool runOnMachineFunction(MachineFunction &F);
115 bool doInitialization(Module &M);
116 bool doFinalization(Module &M);
117 };
118} // end of anonymous namespace
119
120
121// Include the auto-generated portion of the assembly writer.
122#include "IA64GenAsmWriter.inc"
123
124
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000125// Substitute old hook with new one temporary
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const {
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000127 return TAI->SectionForGlobal(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128}
129
130/// runOnMachineFunction - This uses the printMachineInstruction()
131/// method to print assembly for each instruction.
132///
133bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
134 SetupMachineFunction(MF);
135 O << "\n\n";
136
137 // Print out constants referenced by the function
138 EmitConstantPool(MF.getConstantPool());
139
140 const Function *F = MF.getFunction();
141 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
142
143 // Print out labels for the function.
144 EmitAlignment(5);
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000145 O << "\t.global\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000146
147 printVisibility(CurrentFnName, F->getVisibility());
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 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.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000156 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000157 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 O << '\n';
159 }
160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
161 II != E; ++II) {
162 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 printMachineInstruction(II);
164 }
165 }
166
167 // We didn't modify anything.
168 return false;
169}
170
171void IA64AsmPrinter::printOp(const MachineOperand &MO,
172 bool isBRCALLinsn /* = false */) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000173 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 switch (MO.getType()) {
175 case MachineOperand::MO_Register:
Bill Wendling8eeb9792008-02-26 21:11:01 +0000176 O << RI.get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 return;
178
179 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000180 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 return;
182 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000183 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return;
185 case MachineOperand::MO_ConstantPoolIndex: {
186 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
Chris Lattner6017d482007-12-30 23:10:15 +0000187 << "CPI" << getFunctionNumber() << "_" << MO.getIndex() << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 return;
189 }
190
191 case MachineOperand::MO_GlobalAddress: {
192
193 // functions need @ltoff(@fptr(fn_name)) form
194 GlobalValue *GV = MO.getGlobal();
195 Function *F = dyn_cast<Function>(GV);
196
197 bool Needfptr=false; // if we're computing an address @ltoff(X), do
198 // we need to decorate it so it becomes
199 // @ltoff(@fptr(X)) ?
200 if (F && !isBRCALLinsn /*&& F->isDeclaration()*/)
201 Needfptr=true;
202
203 // if this is the target of a call instruction, we should define
204 // the function somewhere (GNU gas has no problem without this, but
205 // Intel ias rightly complains of an 'undefined symbol')
206
207 if (F /*&& isBRCALLinsn*/ && F->isDeclaration())
208 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
209 else
210 if (GV->isDeclaration()) // e.g. stuff like 'stdin'
211 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
212
213 if (!isBRCALLinsn)
214 O << "@ltoff(";
215 if (Needfptr)
216 O << "@fptr(";
217 O << Mang->getValueName(MO.getGlobal());
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000218
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 if (Needfptr && !isBRCALLinsn)
220 O << "#))"; // close both fptr( and ltoff(
221 else {
222 if (Needfptr)
223 O << "#)"; // close only fptr(
224 if (!isBRCALLinsn)
225 O << "#)"; // close only ltoff(
226 }
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000227
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 int Offset = MO.getOffset();
229 if (Offset > 0)
230 O << " + " << Offset;
231 else if (Offset < 0)
232 O << " - " << -Offset;
233 return;
234 }
235 case MachineOperand::MO_ExternalSymbol:
236 O << MO.getSymbolName();
237 ExternalFunctionNames.insert(MO.getSymbolName());
238 return;
239 default:
240 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
241 }
242}
243
244/// printMachineInstruction -- Print out a single IA64 LLVM instruction
245/// MI to the current output stream.
246///
247void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
248 ++EmittedInsts;
249
250 // Call the autogenerated instruction printer routines.
251 printInstruction(MI);
252}
253
254bool IA64AsmPrinter::doInitialization(Module &M) {
Dan Gohman4a558a32007-07-25 19:33:14 +0000255 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256
257 O << "\n.ident \"LLVM-ia64\"\n\n"
258 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
259 << "\t.radix C\n"
260 << "\t.psr abi64\n"; // we only support 64 bits for now
Dan Gohman4a558a32007-07-25 19:33:14 +0000261 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262}
263
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000264void IA64AsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000266
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000267 if (!GVar->hasInitializer())
268 return; // External global require no code
269
270 // Check to see if this is a special global used by LLVM, if so, emit it.
271 if (EmitSpecialLLVMGlobal(GVar))
272 return;
273
274 O << "\n\n";
275 std::string SectionName = TAI->SectionForGlobal(GVar);
276 std::string name = Mang->getValueName(GVar);
277 Constant *C = GVar->getInitializer();
278 unsigned Size = TD->getABITypeSize(C->getType());
279 unsigned Align = TD->getPreferredAlignmentLog(GVar);
280
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000281 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000282
283 SwitchToDataSection(SectionName.c_str());
284
285 if (C->isNullValue() && !GVar->hasSection()) {
286 if (!GVar->isThreadLocal() &&
287 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
288 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
289
290 if (GVar->hasInternalLinkage()) {
291 O << "\t.lcomm " << name << "#," << Size
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000292 << ',' << (1 << Align);
293 O << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000294 } else {
295 O << "\t.common " << name << "#," << Size
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000296 << ',' << (1 << Align);
297 O << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000298 }
299
300 return;
301 }
302 }
303
304 switch (GVar->getLinkage()) {
305 case GlobalValue::LinkOnceLinkage:
306 case GlobalValue::CommonLinkage:
307 case GlobalValue::WeakLinkage:
308 // Nonnull linkonce -> weak
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000309 O << "\t.weak " << name << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000310 break;
311 case GlobalValue::AppendingLinkage:
312 // FIXME: appending linkage variables should go into a section of
313 // their name or something. For now, just emit them as external.
314 case GlobalValue::ExternalLinkage:
315 // If external or appending, declare as a global symbol
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000316 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000317 // FALL THROUGH
318 case GlobalValue::InternalLinkage:
319 break;
320 case GlobalValue::GhostLinkage:
321 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
322 abort();
323 case GlobalValue::DLLImportLinkage:
324 cerr << "DLLImport linkage is not supported by this target!\n";
325 abort();
326 case GlobalValue::DLLExportLinkage:
327 cerr << "DLLExport linkage is not supported by this target!\n";
328 abort();
329 default:
330 assert(0 && "Unknown linkage type!");
331 }
332
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000333 EmitAlignment(Align, GVar);
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000334
335 if (TAI->hasDotTypeDotSizeDirective()) {
336 O << "\t.type " << name << ",@object\n";
Anton Korobeynikov19eeb152008-08-07 09:52:54 +0000337 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000338 }
339
Owen Anderson847b99b2008-08-21 00:14:44 +0000340 O << name << ":\n";
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000341 EmitGlobalConstant(C);
342}
343
344
345bool IA64AsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 // Print out module-level global variables here.
347 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
348 I != E; ++I)
Anton Korobeynikov9c53fec2008-08-07 09:52:35 +0000349 printModuleLevelGV(I);
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000350
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";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 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";
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 // 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";
Anton Korobeynikovc7fcf352008-08-07 09:52:13 +0000366
Dan Gohman4a558a32007-07-25 19:33:14 +0000367 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368}
369
370/// createIA64CodePrinterPass - Returns a pass that prints the IA64
371/// assembly code for a MachineFunction to the given output stream, using
372/// the given target machine description.
373///
Owen Anderson847b99b2008-08-21 00:14:44 +0000374FunctionPass *llvm::createIA64CodePrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 IA64TargetMachine &tm) {
376 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());
377}