1 //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
20 #include "IA64TargetMachine.h"
21 #include "llvm/Module.h"
22 #include "llvm/Type.h"
23 #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"
35 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37 struct IA64SharedAsmPrinter : public AsmPrinter {
39 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
41 IA64SharedAsmPrinter(std::ostream &O, TargetMachine &TM)
42 : AsmPrinter(O, TM) { }
44 void printConstantPool(MachineConstantPool *MCP);
45 bool doFinalization(Module &M);
49 static bool isScale(const MachineOperand &MO) {
50 return MO.isImmediate() &&
51 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
52 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
55 static 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());
64 // switchSection - Switch to the specified section of the executable if we are
67 static 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";
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.
81 void IA64SharedAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
82 const std::vector<Constant*> &CP = MCP->getConstants();
83 const TargetData &TD = TM.getTargetData();
85 if (CP.empty()) return;
87 O << "\n\t.section .data, \"aw\", \"progbits\"\n";
88 // FIXME: would be nice to have rodata (no 'w') when appropriate?
89 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
90 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
91 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
92 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
93 emitGlobalConstant(CP[i]);
97 bool IA64SharedAsmPrinter::doFinalization(Module &M) {
98 const TargetData &TD = TM.getTargetData();
99 std::string CurSection;
101 // Print out module-level global variables here.
102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
104 if (I->hasInitializer()) { // External global require no code
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());
111 if (C->isNullValue() &&
112 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
113 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
114 switchSection(O, CurSection, ".data");
115 if (I->hasInternalLinkage()) {
116 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
117 << "," << (1 << Align);
120 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
121 << "," << (1 << Align);
124 WriteAsOperand(O, I, true, true, &M);
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";
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";
143 case GlobalValue::InternalLinkage:
144 if (C->isNullValue())
145 switchSection(O, CurSection, ".bss");
147 switchSection(O, CurSection, ".data");
149 case GlobalValue::GhostLinkage:
150 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
154 emitAlignment(Align);
155 O << "\t.type " << name << ",@object\n";
156 O << "\t.size " << name << "," << Size << "\n";
157 O << name << ":\t\t\t\t// ";
158 WriteAsOperand(O, I, true, true, &M);
160 WriteAsOperand(O, C, false, false, &M);
162 emitGlobalConstant(C);
166 // we print out ".global X \n .type X, @function" for each external function
167 O << "\n\n// br.call targets referenced (and not defined) above: \n";
168 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
169 e = ExternalFunctionNames.end(); i!=e; ++i) {
170 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
174 // we print out ".global X \n .type X, @object" for each external object
175 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
176 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
177 e = ExternalObjectNames.end(); i!=e; ++i) {
178 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
182 AsmPrinter::doFinalization(M);
183 return false; // success
187 struct IA64AsmPrinter : public IA64SharedAsmPrinter {
188 IA64AsmPrinter(std::ostream &O, TargetMachine &TM)
189 : IA64SharedAsmPrinter(O, TM) {
191 CommentString = "//";
192 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
193 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
194 Data32bitsDirective = "\tdata4.ua\t"; // properly
195 Data64bitsDirective = "\tdata8.ua\t";
196 ZeroDirective = "\t.skip\t";
197 AsciiDirective = "\tstring\t";
199 GlobalVarAddrPrefix="";
200 GlobalVarAddrSuffix="";
201 FunctionAddrPrefix="@fptr(";
202 FunctionAddrSuffix=")";
206 virtual const char *getPassName() const {
207 return "IA64 Assembly Printer";
210 /// printInstruction - This method is automatically generated by tablegen
211 /// from the instruction set description. This method returns true if the
212 /// machine instruction was sufficiently described to print it, otherwise it
214 bool printInstruction(const MachineInstr *MI);
216 // This method is used by the tablegen'erated instruction printer.
217 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
218 const MachineOperand &MO = MI->getOperand(OpNo);
219 if (MO.getType() == MachineOperand::MO_MachineRegister) {
220 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
221 //XXX Bug Workaround: See note in Printer::doInitialization about %.
222 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
228 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo,
230 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
231 if(val>=128) val=val-256; // if negative, flip sign
234 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo,
236 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
237 if(val>=8192) val=val-16384; // if negative, flip sign
240 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo,
242 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
243 if(val>=2097152) val=val-4194304; // if negative, flip sign
246 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
248 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
250 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo,
252 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
253 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
254 // emit movl rX = @gprel(CPI<whatever);;
256 // this gives us 64 bits instead of 22 (for the add long imm) to play
257 // with, which shuts up the linker. The problem is that the constant
258 // pool entries aren't immediates at this stage, so we check here.
259 // If it's an immediate, print it the old fashioned way. If it's
260 // not, we print it as a constant pool index.
261 if(MI->getOperand(OpNo).isImmediate()) {
262 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
263 } else { // this is a constant pool reference: FIXME: assert this
264 printOp(MI->getOperand(OpNo));
268 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo,
270 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
273 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
275 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
278 void printMachineInstruction(const MachineInstr *MI);
279 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
280 bool runOnMachineFunction(MachineFunction &F);
281 bool doInitialization(Module &M);
283 } // end of anonymous namespace
286 // Include the auto-generated portion of the assembly writer.
287 #include "IA64GenAsmWriter.inc"
290 /// runOnMachineFunction - This uses the printMachineInstruction()
291 /// method to print assembly for each instruction.
293 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
294 setupMachineFunction(MF);
297 // Print out constants referenced by the function
298 printConstantPool(MF.getConstantPool());
300 // Print out labels for the function.
301 O << "\n\t.section .text, \"ax\", \"progbits\"\n";
302 // ^^ means "Allocated instruXions in mem, initialized"
304 O << "\t.global\t" << CurrentFnName << "\n";
305 O << "\t.type\t" << CurrentFnName << ", @function\n";
306 O << CurrentFnName << ":\n";
308 // Print out code for the function.
309 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
311 // Print a label for the basic block if there are any predecessors.
312 if (I->pred_begin() != I->pred_end())
313 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
314 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
315 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
317 // Print the assembly for the instruction.
319 printMachineInstruction(II);
323 // We didn't modify anything.
327 void IA64AsmPrinter::printOp(const MachineOperand &MO,
328 bool isBRCALLinsn /* = false */) {
329 const MRegisterInfo &RI = *TM.getRegisterInfo();
330 switch (MO.getType()) {
331 case MachineOperand::MO_VirtualRegister:
332 if (Value *V = MO.getVRegValueOrNull()) {
333 O << "<" << V->getName() << ">";
337 case MachineOperand::MO_MachineRegister:
338 case MachineOperand::MO_CCRegister: {
339 O << RI.get(MO.getReg()).Name;
343 case MachineOperand::MO_SignExtendedImmed:
344 case MachineOperand::MO_UnextendedImmed:
345 O << /*(unsigned int)*/MO.getImmedValue();
347 case MachineOperand::MO_MachineBasicBlock: {
348 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
349 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
350 << "_" << MBBOp->getNumber () << "\t// "
351 << MBBOp->getBasicBlock ()->getName ();
354 case MachineOperand::MO_PCRelativeDisp:
355 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
359 case MachineOperand::MO_ConstantPoolIndex: {
360 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
361 << MO.getConstantPoolIndex() << ")";
365 case MachineOperand::MO_GlobalAddress: {
367 // functions need @ltoff(@fptr(fn_name)) form
368 GlobalValue *GV = MO.getGlobal();
369 Function *F = dyn_cast<Function>(GV);
371 bool Needfptr=false; // if we're computing an address @ltoff(X), do
372 // we need to decorate it so it becomes
373 // @ltoff(@fptr(X)) ?
374 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
377 // if this is the target of a call instruction, we should define
378 // the function somewhere (GNU gas has no problem without this, but
379 // Intel ias rightly complains of an 'undefined symbol')
381 if (F /*&& isBRCALLinsn*/ && F->isExternal())
382 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
384 if (GV->isExternal()) // e.g. stuff like 'stdin'
385 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
391 O << Mang->getValueName(MO.getGlobal());
393 O << ")"; // close fptr(
395 O << ")"; // close ltoff(
396 int Offset = MO.getOffset();
398 O << " + " << Offset;
400 O << " - " << -Offset;
403 case MachineOperand::MO_ExternalSymbol:
404 O << MO.getSymbolName();
405 ExternalFunctionNames.insert(MO.getSymbolName());
408 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
412 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
413 /// MI to the current output stream.
415 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
419 // Call the autogenerated instruction printer routines.
420 printInstruction(MI);
423 bool IA64AsmPrinter::doInitialization(Module &M) {
424 AsmPrinter::doInitialization(M);
426 O << "\n.ident \"LLVM-ia64\"\n\n"
427 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
429 << "\t.psr abi64\n"; // we only support 64 bits for now
433 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
434 /// assembly code for a MachineFunction to the given output stream, using
435 /// the given target machine description.
437 FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
438 return new IA64AsmPrinter(o, tm);