blob: 4c950fc24cd535962617c15c7a2abc94d866aad1 [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");
115 if (I->hasInternalLinkage())
Duraid Madinaae8bd732005-03-28 15:21:43 +0000116// FIXME O << "\t.local " << name << "\n";
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000117
118 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
119 << "," << (1 << Align);
120 O << "\t\t// ";
121 WriteAsOperand(O, I, true, true, &M);
122 O << "\n";
123 } else {
124 switch (I->getLinkage()) {
125 case GlobalValue::LinkOnceLinkage:
126 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
127 // Nonnull linkonce -> weak
128 O << "\t.weak " << name << "\n";
129 SwitchSection(O, CurSection, "");
130 O << "\t.section\t.llvm.linkonce.d." << name
131 << ", \"aw\", \"progbits\"\n";
132 break;
133 case GlobalValue::AppendingLinkage:
134 // FIXME: appending linkage variables should go into a section of
135 // their name or something. For now, just emit them as external.
136 case GlobalValue::ExternalLinkage:
137 // If external or appending, declare as a global symbol
138 O << "\t.global " << name << "\n";
139 // FALL THROUGH
140 case GlobalValue::InternalLinkage:
141 if (C->isNullValue())
142 SwitchSection(O, CurSection, ".data"); // FIXME: this was
143 // '.bss', but in ia64-land .bss means "nobits" (i.e. uninitialized)
144 // hmm.
145 else
146 SwitchSection(O, CurSection, ".data");
147 break;
148 case GlobalValue::GhostLinkage:
149 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
150 abort();
151 }
152
153 emitAlignment(Align);
154 O << "\t.type " << name << ",@object\n";
155 O << "\t.size " << name << "," << Size << "\n";
156 O << name << ":\t\t\t\t// ";
157 WriteAsOperand(O, I, true, true, &M);
158 O << " = ";
159 WriteAsOperand(O, C, false, false, &M);
160 O << "\n";
161 emitGlobalConstant(C);
162 }
163 }
164
165 // we print out ".global X \n .type X, @function" for each external function
166 O << "\n\n// br.call targets referenced (and not defined) above: \n";
167 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
168 e = ExternalFunctionNames.end(); i!=e; ++i) {
169 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
170 }
171 O << "\n\n";
172
Duraid Madinaae8bd732005-03-28 15:21:43 +0000173 // we print out ".global X \n .type X, @object" for each external object
174 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
175 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
176 e = ExternalObjectNames.end(); i!=e; ++i) {
177 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
178 }
179 O << "\n\n";
180
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000181 AsmPrinter::doFinalization(M);
182 return false; // success
183}
184
185namespace {
186 struct IA64AsmPrinter : public IA64SharedAsmPrinter {
187 IA64AsmPrinter(std::ostream &O, TargetMachine &TM)
188 : IA64SharedAsmPrinter(O, TM) {
189
190 CommentString = "//";
191 Data8bitsDirective = "\tdata1\t";
192 Data16bitsDirective = "\tdata2\t";
193 Data32bitsDirective = "\tdata4\t";
194 Data64bitsDirective = "\tdata8\t";
195 ZeroDirective = "\t.skip\t";
196 AsciiDirective = "\tstring\t";
197
198 }
199
200 virtual const char *getPassName() const {
201 return "IA64 Assembly Printer";
202 }
203
204 /// printInstruction - This method is automatically generated by tablegen
205 /// from the instruction set description. This method returns true if the
206 /// machine instruction was sufficiently described to print it, otherwise it
207 /// returns false.
208 bool printInstruction(const MachineInstr *MI);
209
210 // This method is used by the tablegen'erated instruction printer.
211 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
212 const MachineOperand &MO = MI->getOperand(OpNo);
213 if (MO.getType() == MachineOperand::MO_MachineRegister) {
214 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
215 //XXX Bug Workaround: See note in Printer::doInitialization about %.
216 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
217 } else {
218 printOp(MO);
219 }
220 }
221
222 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
223 MVT::ValueType VT) {
224 O << (short)MI->getOperand(OpNo).getImmedValue();
225 }
226 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
227 MVT::ValueType VT) {
228 O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
229 }
230 void printS21ImmOperand(const MachineInstr *MI, unsigned OpNo,
231 MVT::ValueType VT) {
232 O << (int)MI->getOperand(OpNo).getImmedValue(); // FIXME (21, not 32!)
233 }
234 void printS32ImmOperand(const MachineInstr *MI, unsigned OpNo,
235 MVT::ValueType VT) {
236 O << (int)MI->getOperand(OpNo).getImmedValue();
237 }
238 void printU32ImmOperand(const MachineInstr *MI, unsigned OpNo,
239 MVT::ValueType VT) {
240 O << (unsigned int)MI->getOperand(OpNo).getImmedValue();
241 }
242 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
243 MVT::ValueType VT) {
244 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
245 }
246
247 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
248 MVT::ValueType VT) {
249 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
250 }
251
252 void printMachineInstruction(const MachineInstr *MI);
253 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
254 bool runOnMachineFunction(MachineFunction &F);
255 bool doInitialization(Module &M);
256 };
257} // end of anonymous namespace
258
259
260// Include the auto-generated portion of the assembly writer.
261#include "IA64GenAsmWriter.inc"
262
263
264/// runOnMachineFunction - This uses the printMachineInstruction()
265/// method to print assembly for each instruction.
266///
267bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
268 setupMachineFunction(MF);
269 O << "\n\n";
270
271 // Print out constants referenced by the function
272 printConstantPool(MF.getConstantPool());
273
274 // Print out labels for the function.
275 O << "\n\t.section .text, \"ax\", \"progbits\"\n";
276 // ^^ means "Allocated instruXions in mem, initialized"
Duraid Madinaae8bd732005-03-28 15:21:43 +0000277 emitAlignment(5);
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000278 O << "\t.global\t" << CurrentFnName << "\n";
279 O << "\t.type\t" << CurrentFnName << ", @function\n";
280 O << CurrentFnName << ":\n";
281
282 // Print out code for the function.
283 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
284 I != E; ++I) {
285 // Print a label for the basic block if there are any predecessors.
286 if (I->pred_begin() != I->pred_end())
287 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
288 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
289 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
290 II != E; ++II) {
291 // Print the assembly for the instruction.
292 O << "\t";
293 printMachineInstruction(II);
294 }
295 }
296
297 // We didn't modify anything.
298 return false;
299}
300
301void IA64AsmPrinter::printOp(const MachineOperand &MO,
302 bool isBRCALLinsn /* = false */) {
303 const MRegisterInfo &RI = *TM.getRegisterInfo();
304 switch (MO.getType()) {
305 case MachineOperand::MO_VirtualRegister:
306 if (Value *V = MO.getVRegValueOrNull()) {
307 O << "<" << V->getName() << ">";
308 return;
309 }
310 // FALLTHROUGH
311 case MachineOperand::MO_MachineRegister:
312 case MachineOperand::MO_CCRegister: {
313 O << RI.get(MO.getReg()).Name;
314 return;
315 }
316
317 case MachineOperand::MO_SignExtendedImmed:
318 case MachineOperand::MO_UnextendedImmed:
319 O << /*(unsigned int)*/MO.getImmedValue();
320 return;
321 case MachineOperand::MO_MachineBasicBlock: {
322 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
323 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
324 << "_" << MBBOp->getNumber () << "\t// "
325 << MBBOp->getBasicBlock ()->getName ();
326 return;
327 }
328 case MachineOperand::MO_PCRelativeDisp:
329 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
330 abort ();
331 return;
332
333 case MachineOperand::MO_ConstantPoolIndex: {
334 O << "@gprel(.CPI" << CurrentFnName << "_"
335 << MO.getConstantPoolIndex() << ")";
336 return;
337 }
338
339 case MachineOperand::MO_GlobalAddress: {
340
341 // functions need @ltoff(@fptr(fn_name)) form
342 GlobalValue *GV = MO.getGlobal();
343 Function *F = dyn_cast<Function>(GV);
344
345 bool Needfptr=false; // if we're computing an address @ltoff(X), do
346 // we need to decorate it so it becomes
347 // @ltoff(@fptr(X)) ?
348 if(F && !isBRCALLinsn && F->isExternal())
349 Needfptr=true;
350
351 // if this is the target of a call instruction, we should define
352 // the function somewhere (GNU gas has no problem without this, but
353 // Intel ias rightly complains of an 'undefined symbol')
354
Duraid Madinaae8bd732005-03-28 15:21:43 +0000355 if(F /*&& isBRCALLinsn*/ && F->isExternal())
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000356 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
Duraid Madinaae8bd732005-03-28 15:21:43 +0000357 else
358 if(GV->isExternal()) // e.g. stuff like 'stdin'
359 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
360
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000361 if (!isBRCALLinsn)
362 O << "@ltoff(";
363 if (Needfptr)
364 O << "@fptr(";
365 O << Mang->getValueName(MO.getGlobal());
366 if (Needfptr)
367 O << ")"; // close fptr(
368 if (!isBRCALLinsn)
369 O << ")"; // close ltoff(
370 int Offset = MO.getOffset();
371 if (Offset > 0)
372 O << " + " << Offset;
373 else if (Offset < 0)
374 O << " - " << -Offset;
375 return;
376 }
377 case MachineOperand::MO_ExternalSymbol:
378 O << MO.getSymbolName();
Duraid Madinaae8bd732005-03-28 15:21:43 +0000379 ExternalFunctionNames.insert(MO.getSymbolName());
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000380 return;
381 default:
382 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
383 }
384}
385
386/// printMachineInstruction -- Print out a single IA64 LLVM instruction
387/// MI to the current output stream.
388///
389void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
390
391 ++EmittedInsts;
392
393 // Call the autogenerated instruction printer routines.
394 printInstruction(MI);
395}
396
397bool IA64AsmPrinter::doInitialization(Module &M) {
398 AsmPrinter::doInitialization(M);
399
Duraid Madinaae8bd732005-03-28 15:21:43 +0000400 O << "\n.ident \"LLVM-ia64\"\n\n"
401 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
Duraid Madina9b9d45f2005-03-17 18:17:03 +0000402 << "\t.radix C\n"
403 << "\t.psr abi64\n"; // we only support 64 bits for now
404 return false;
405}
406
407/// createIA64CodePrinterPass - Returns a pass that prints the IA64
408/// assembly code for a MachineFunction to the given output stream, using
409/// the given target machine description.
410///
411FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
412 return new IA64AsmPrinter(o, tm);
413}
414
415