blob: 64bdfc6b6e9002495e2fa992b116b36ab12d6881 [file] [log] [blame]
Duraid Madina9b9d45f2005-03-17 18:17:03 +00001//===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
2//
3// 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.
7//
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 excellent 'ias' assembler is bundled with
15// 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;
Duraid Madina9b9d45f2005-03-17 18:17:03 +000040
41 IA64SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
42 : AsmPrinter(O, TM) { }
43
44 void printConstantPool(MachineConstantPool *MCP);
45 bool doFinalization(Module &M);
46 };
47}
48
49static bool isScale(const MachineOperand &MO) {
50 return MO.isImmediate() &&
51 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
52 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
53}
54
55static bool isMem(const MachineInstr *MI, unsigned Op) {
56 if (MI->getOperand(Op).isFrameIndex()) return true;
57 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
58 return Op+4 <= MI->getNumOperands() &&
59 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
60 MI->getOperand(Op+2).isRegister() && (MI->getOperand(Op+3).isImmediate() ||
61 MI->getOperand(Op+3).isGlobalAddress());
62}
63
64// SwitchSection - Switch to the specified section of the executable if we are
65// not already in it!
66//
67static void SwitchSection(std::ostream &OS, std::string &CurSection,
68 const char *NewSection) {
69 if (CurSection != NewSection) {
70 CurSection = NewSection;
71 if (!CurSection.empty())
72 OS << "\t" << NewSection << "\n";
73 }
74}
75
76/// printConstantPool - Print to the current output stream assembly
77/// representations of the constants in the constant pool MCP. This is
78/// used to print out constants which have been "spilled to memory" by
79/// the code generator.
80///
81void IA64SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
82 const std::vector<Constant*> &CP = MCP->getConstants();
83 const TargetData &TD = TM.getTargetData();
84
85 if (CP.empty()) return;
86
Duraid Madinaae8bd732005-03-28 15:21:43 +000087 O << "\n\t.section .data, \"aw\", \"progbits\"\n";
88 // FIXME: would be nice to have rodata (no 'w') when appropriate?
Duraid Madina9b9d45f2005-03-17 18:17:03 +000089 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
90 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
91 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
92 << *CP[i] << "\n";
93 emitGlobalConstant(CP[i]);
94 }
95}
96
97bool IA64SharedAsmPrinter::doFinalization(Module &M) {
98 const TargetData &TD = TM.getTargetData();
99 std::string CurSection;
100
101 // Print out module-level global variables here.
Alkis Evlogimenos12cf3852005-03-19 09:22:17 +0000102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
103 I != E; ++I)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000104 if (I->hasInitializer()) { // External global require no code
105 O << "\n\n";
106 std::string name = Mang->getValueName(I);
107 Constant *C = I->getInitializer();
108 unsigned Size = TD.getTypeSize(C->getType());
109 unsigned Align = TD.getTypeAlignmentShift(C->getType());
110
111 if (C->isNullValue() &&
112 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
113 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
114 SwitchSection(O, CurSection, ".data");
Duraid Madina1f867b12005-03-31 07:40:24 +0000115 if (I->hasInternalLinkage()) {
116 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
117 << "," << (1 << Align);
118 O << "\t\t// ";
119 } else {
120 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
121 << "," << (1 << Align);
122 O << "\t\t// ";
123 }
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000124 WriteAsOperand(O, I, true, true, &M);
125 O << "\n";
126 } else {
127 switch (I->getLinkage()) {
128 case GlobalValue::LinkOnceLinkage:
129 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
130 // Nonnull linkonce -> weak
131 O << "\t.weak " << name << "\n";
132 SwitchSection(O, CurSection, "");
133 O << "\t.section\t.llvm.linkonce.d." << name
134 << ", \"aw\", \"progbits\"\n";
135 break;
136 case GlobalValue::AppendingLinkage:
137 // FIXME: appending linkage variables should go into a section of
138 // their name or something. For now, just emit them as external.
139 case GlobalValue::ExternalLinkage:
140 // If external or appending, declare as a global symbol
141 O << "\t.global " << name << "\n";
142 // FALL THROUGH
143 case GlobalValue::InternalLinkage:
144 if (C->isNullValue())
Duraid Madina32c46f32005-04-02 12:30:47 +0000145 SwitchSection(O, CurSection, ".bss");
146 // FIXME? in ia64-land .bss means "nobits" (i.e. uninitialized)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000147 else
148 SwitchSection(O, CurSection, ".data");
149 break;
150 case GlobalValue::GhostLinkage:
151 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
152 abort();
153 }
154
155 emitAlignment(Align);
156 O << "\t.type " << name << ",@object\n";
157 O << "\t.size " << name << "," << Size << "\n";
158 O << name << ":\t\t\t\t// ";
159 WriteAsOperand(O, I, true, true, &M);
160 O << " = ";
161 WriteAsOperand(O, C, false, false, &M);
162 O << "\n";
163 emitGlobalConstant(C);
164 }
165 }
166
167 // we print out ".global X \n .type X, @function" for each external function
168 O << "\n\n// br.call targets referenced (and not defined) above: \n";
169 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
170 e = ExternalFunctionNames.end(); i!=e; ++i) {
171 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
172 }
173 O << "\n\n";
174
Duraid Madinaae8bd732005-03-28 15:21:43 +0000175 // we print out ".global X \n .type X, @object" for each external object
176 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
177 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
178 e = ExternalObjectNames.end(); i!=e; ++i) {
179 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
180 }
181 O << "\n\n";
182
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000183 AsmPrinter::doFinalization(M);
184 return false; // success
185}
186
187namespace {
188 struct IA64AsmPrinter : public IA64SharedAsmPrinter {
189 IA64AsmPrinter(std::ostream &O, TargetMachine &TM)
190 : IA64SharedAsmPrinter(O, TM) {
191
192 CommentString = "//";
Duraid Madina32c46f32005-04-02 12:30:47 +0000193 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
194 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
195 Data32bitsDirective = "\tdata4.ua\t"; // properly
196 Data64bitsDirective = "\tdata8.ua\t";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000197 ZeroDirective = "\t.skip\t";
198 AsciiDirective = "\tstring\t";
199
Duraid Madina32c46f32005-04-02 12:30:47 +0000200 GlobalVarAddrPrefix="";
201 GlobalVarAddrSuffix="";
202 FunctionAddrPrefix="@fptr(";
203 FunctionAddrSuffix=")";
204
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000205 }
206
207 virtual const char *getPassName() const {
208 return "IA64 Assembly Printer";
209 }
210
211 /// printInstruction - This method is automatically generated by tablegen
212 /// from the instruction set description. This method returns true if the
213 /// machine instruction was sufficiently described to print it, otherwise it
214 /// returns false.
215 bool printInstruction(const MachineInstr *MI);
216
217 // This method is used by the tablegen'erated instruction printer.
218 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
219 const MachineOperand &MO = MI->getOperand(OpNo);
220 if (MO.getType() == MachineOperand::MO_MachineRegister) {
221 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
222 //XXX Bug Workaround: See note in Printer::doInitialization about %.
223 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
224 } else {
225 printOp(MO);
226 }
227 }
228
229 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
230 MVT::ValueType VT) {
231 O << (short)MI->getOperand(OpNo).getImmedValue();
232 }
233 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
234 MVT::ValueType VT) {
235 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
236 }
237 void printS21ImmOperand(const MachineInstr *MI, unsigned OpNo,
238 MVT::ValueType VT) {
239 O << (int)MI->getOperand(OpNo).getImmedValue(); // FIXME (21, not 32!)
240 }
241 void printS32ImmOperand(const MachineInstr *MI, unsigned OpNo,
242 MVT::ValueType VT) {
243 O << (int)MI->getOperand(OpNo).getImmedValue();
244 }
245 void printU32ImmOperand(const MachineInstr *MI, unsigned OpNo,
246 MVT::ValueType VT) {
247 O << (unsigned int)MI->getOperand(OpNo).getImmedValue();
248 }
249 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
250 MVT::ValueType VT) {
251 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
252 }
253
254 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
255 MVT::ValueType VT) {
256 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
257 }
258
259 void printMachineInstruction(const MachineInstr *MI);
260 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
261 bool runOnMachineFunction(MachineFunction &F);
262 bool doInitialization(Module &M);
263 };
264} // end of anonymous namespace
265
266
267// Include the auto-generated portion of the assembly writer.
268#include "IA64GenAsmWriter.inc"
269
270
271/// runOnMachineFunction - This uses the printMachineInstruction()
272/// method to print assembly for each instruction.
273///
274bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
275 setupMachineFunction(MF);
276 O << "\n\n";
277
278 // Print out constants referenced by the function
279 printConstantPool(MF.getConstantPool());
280
281 // Print out labels for the function.
282 O << "\n\t.section .text, \"ax\", \"progbits\"\n";
283 // ^^ means "Allocated instruXions in mem, initialized"
Duraid Madinaae8bd732005-03-28 15:21:43 +0000284 emitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000285 O << "\t.global\t" << CurrentFnName << "\n";
286 O << "\t.type\t" << CurrentFnName << ", @function\n";
287 O << CurrentFnName << ":\n";
288
289 // Print out code for the function.
290 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
291 I != E; ++I) {
292 // Print a label for the basic block if there are any predecessors.
293 if (I->pred_begin() != I->pred_end())
294 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
295 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
296 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
297 II != E; ++II) {
298 // Print the assembly for the instruction.
299 O << "\t";
300 printMachineInstruction(II);
301 }
302 }
303
304 // We didn't modify anything.
305 return false;
306}
307
308void IA64AsmPrinter::printOp(const MachineOperand &MO,
309 bool isBRCALLinsn /* = false */) {
310 const MRegisterInfo &RI = *TM.getRegisterInfo();
311 switch (MO.getType()) {
312 case MachineOperand::MO_VirtualRegister:
313 if (Value *V = MO.getVRegValueOrNull()) {
314 O << "<" << V->getName() << ">";
315 return;
316 }
317 // FALLTHROUGH
318 case MachineOperand::MO_MachineRegister:
319 case MachineOperand::MO_CCRegister: {
320 O << RI.get(MO.getReg()).Name;
321 return;
322 }
323
324 case MachineOperand::MO_SignExtendedImmed:
325 case MachineOperand::MO_UnextendedImmed:
326 O << /*(unsigned int)*/MO.getImmedValue();
327 return;
328 case MachineOperand::MO_MachineBasicBlock: {
329 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
330 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
331 << "_" << MBBOp->getNumber () << "\t// "
332 << MBBOp->getBasicBlock ()->getName ();
333 return;
334 }
335 case MachineOperand::MO_PCRelativeDisp:
336 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
337 abort ();
338 return;
339
340 case MachineOperand::MO_ConstantPoolIndex: {
341 O << "@gprel(.CPI" << CurrentFnName << "_"
342 << MO.getConstantPoolIndex() << ")";
343 return;
344 }
345
346 case MachineOperand::MO_GlobalAddress: {
347
348 // functions need @ltoff(@fptr(fn_name)) form
349 GlobalValue *GV = MO.getGlobal();
350 Function *F = dyn_cast<Function>(GV);
351
352 bool Needfptr=false; // if we're computing an address @ltoff(X), do
353 // we need to decorate it so it becomes
354 // @ltoff(@fptr(X)) ?
Duraid Madina1f867b12005-03-31 07:40:24 +0000355 if(F && !isBRCALLinsn /*&& F->isExternal()*/)
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 Needfptr=true;
357
358 // if this is the target of a call instruction, we should define
359 // the function somewhere (GNU gas has no problem without this, but
360 // Intel ias rightly complains of an 'undefined symbol')
361
Duraid Madinaae8bd732005-03-28 15:21:43 +0000362 if(F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000363 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000364 else
365 if(GV->isExternal()) // e.g. stuff like 'stdin'
366 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
367
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000368 if (!isBRCALLinsn)
369 O << "@ltoff(";
370 if (Needfptr)
371 O << "@fptr(";
372 O << Mang->getValueName(MO.getGlobal());
373 if (Needfptr)
374 O << ")"; // close fptr(
375 if (!isBRCALLinsn)
376 O << ")"; // close ltoff(
377 int Offset = MO.getOffset();
378 if (Offset > 0)
379 O << " + " << Offset;
380 else if (Offset < 0)
381 O << " - " << -Offset;
382 return;
383 }
384 case MachineOperand::MO_ExternalSymbol:
385 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000386 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000387 return;
388 default:
389 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
390 }
391}
392
393/// printMachineInstruction -- Print out a single IA64 LLVM instruction
394/// MI to the current output stream.
395///
396void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
397
398 ++EmittedInsts;
399
400 // Call the autogenerated instruction printer routines.
401 printInstruction(MI);
402}
403
404bool IA64AsmPrinter::doInitialization(Module &M) {
405 AsmPrinter::doInitialization(M);
406
Duraid Madinaae8bd732005-03-28 15:21:43 +0000407 O << "\n.ident \"LLVM-ia64\"\n\n"
408 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000409 << "\t.radix C\n"
410 << "\t.psr abi64\n"; // we only support 64 bits for now
411 return false;
412}
413
414/// createIA64CodePrinterPass - Returns a pass that prints the IA64
415/// assembly code for a MachineFunction to the given output stream, using
416/// the given target machine description.
417///
418FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
419 return new IA64AsmPrinter(o, tm);
420}
421
422