1 //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "asm-printer"
21 #include "IA64TargetMachine.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/Target/TargetAsmInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Support/Mangler.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/Statistic.h"
34 STATISTIC(EmittedInsts, "Number of machine instrs printed");
37 struct IA64AsmPrinter : public AsmPrinter {
38 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
40 IA64AsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
41 : AsmPrinter(O, TM, T) {
44 virtual const char *getPassName() const {
45 return "IA64 Assembly Printer";
48 /// printInstruction - This method is automatically generated by tablegen
49 /// from the instruction set description. This method returns true if the
50 /// machine instruction was sufficiently described to print it, otherwise it
52 bool printInstruction(const MachineInstr *MI);
54 // This method is used by the tablegen'erated instruction printer.
55 void printOperand(const MachineInstr *MI, unsigned OpNo){
56 const MachineOperand &MO = MI->getOperand(OpNo);
57 if (MO.getType() == MachineOperand::MO_Register) {
58 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
60 //XXX Bug Workaround: See note in Printer::doInitialization about %.
61 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
67 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
68 int val=(unsigned int)MI->getOperand(OpNo).getImm();
69 if(val>=128) val=val-256; // if negative, flip sign
72 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
73 int val=(unsigned int)MI->getOperand(OpNo).getImm();
74 if(val>=8192) val=val-16384; // if negative, flip sign
77 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
78 int val=(unsigned int)MI->getOperand(OpNo).getImm();
79 if(val>=2097152) val=val-4194304; // if negative, flip sign
82 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
83 O << (uint64_t)MI->getOperand(OpNo).getImm();
85 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
86 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
87 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
88 // emit movl rX = @gprel(CPI<whatever);;
90 // this gives us 64 bits instead of 22 (for the add long imm) to play
91 // with, which shuts up the linker. The problem is that the constant
92 // pool entries aren't immediates at this stage, so we check here.
93 // If it's an immediate, print it the old fashioned way. If it's
94 // not, we print it as a constant pool index.
95 if (MI->getOperand(OpNo).isImm()) {
96 O << (int64_t)MI->getOperand(OpNo).getImm();
97 } else { // this is a constant pool reference: FIXME: assert this
98 printOp(MI->getOperand(OpNo));
102 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
103 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
106 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
107 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
110 void printMachineInstruction(const MachineInstr *MI);
111 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
112 void printModuleLevelGV(const GlobalVariable* GVar);
113 bool runOnMachineFunction(MachineFunction &F);
114 bool doInitialization(Module &M);
115 bool doFinalization(Module &M);
117 } // end of anonymous namespace
120 // Include the auto-generated portion of the assembly writer.
121 #include "IA64GenAsmWriter.inc"
123 /// runOnMachineFunction - This uses the printMachineInstruction()
124 /// method to print assembly for each instruction.
126 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
127 SetupMachineFunction(MF);
130 // Print out constants referenced by the function
131 EmitConstantPool(MF.getConstantPool());
133 const Function *F = MF.getFunction();
134 SwitchToSection(TAI->SectionForGlobal(F));
136 // Print out labels for the function.
138 O << "\t.global\t" << CurrentFnName << '\n';
140 printVisibility(CurrentFnName, F->getVisibility());
142 O << "\t.type\t" << CurrentFnName << ", @function\n";
143 O << CurrentFnName << ":\n";
145 // Print out code for the function.
146 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
148 // Print a label for the basic block if there are any predecessors.
149 if (!I->pred_empty()) {
150 printBasicBlockLabel(I, true, true);
153 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
155 // Print the assembly for the instruction.
156 printMachineInstruction(II);
160 // We didn't modify anything.
164 void IA64AsmPrinter::printOp(const MachineOperand &MO,
165 bool isBRCALLinsn /* = false */) {
166 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
167 switch (MO.getType()) {
168 case MachineOperand::MO_Register:
169 O << RI.get(MO.getReg()).AsmName;
172 case MachineOperand::MO_Immediate:
175 case MachineOperand::MO_MachineBasicBlock:
176 printBasicBlockLabel(MO.getMBB());
178 case MachineOperand::MO_ConstantPoolIndex: {
179 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
180 << "CPI" << getFunctionNumber() << "_" << MO.getIndex() << ")";
184 case MachineOperand::MO_GlobalAddress: {
186 // functions need @ltoff(@fptr(fn_name)) form
187 GlobalValue *GV = MO.getGlobal();
188 Function *F = dyn_cast<Function>(GV);
190 bool Needfptr=false; // if we're computing an address @ltoff(X), do
191 // we need to decorate it so it becomes
192 // @ltoff(@fptr(X)) ?
193 if (F && !isBRCALLinsn /*&& F->isDeclaration()*/)
196 // if this is the target of a call instruction, we should define
197 // the function somewhere (GNU gas has no problem without this, but
198 // Intel ias rightly complains of an 'undefined symbol')
200 if (F /*&& isBRCALLinsn*/ && F->isDeclaration())
201 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
203 if (GV->isDeclaration()) // e.g. stuff like 'stdin'
204 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
210 O << Mang->getValueName(MO.getGlobal());
212 if (Needfptr && !isBRCALLinsn)
213 O << "#))"; // close both fptr( and ltoff(
216 O << "#)"; // close only fptr(
218 O << "#)"; // close only ltoff(
221 int Offset = MO.getOffset();
223 O << " + " << Offset;
225 O << " - " << -Offset;
228 case MachineOperand::MO_ExternalSymbol:
229 O << MO.getSymbolName();
230 ExternalFunctionNames.insert(MO.getSymbolName());
233 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
237 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
238 /// MI to the current output stream.
240 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
243 // Call the autogenerated instruction printer routines.
244 printInstruction(MI);
247 bool IA64AsmPrinter::doInitialization(Module &M) {
248 bool Result = AsmPrinter::doInitialization(M);
250 O << "\n.ident \"LLVM-ia64\"\n\n"
251 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
253 << "\t.psr abi64\n"; // we only support 64 bits for now
257 void IA64AsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
258 const TargetData *TD = TM.getTargetData();
260 if (!GVar->hasInitializer())
261 return; // External global require no code
263 // Check to see if this is a special global used by LLVM, if so, emit it.
264 if (EmitSpecialLLVMGlobal(GVar))
268 std::string name = Mang->getValueName(GVar);
269 Constant *C = GVar->getInitializer();
270 unsigned Size = TD->getTypePaddedSize(C->getType());
271 unsigned Align = TD->getPreferredAlignmentLog(GVar);
273 printVisibility(name, GVar->getVisibility());
275 SwitchToSection(TAI->SectionForGlobal(GVar));
277 if (C->isNullValue() && !GVar->hasSection()) {
278 if (!GVar->isThreadLocal() &&
279 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
280 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
282 if (GVar->hasLocalLinkage()) {
283 O << "\t.lcomm " << name << "#," << Size
284 << ',' << (1 << Align);
287 O << "\t.common " << name << "#," << Size
288 << ',' << (1 << Align);
296 switch (GVar->getLinkage()) {
297 case GlobalValue::LinkOnceLinkage:
298 case GlobalValue::CommonLinkage:
299 case GlobalValue::WeakLinkage:
300 // Nonnull linkonce -> weak
301 O << "\t.weak " << name << '\n';
303 case GlobalValue::AppendingLinkage:
304 // FIXME: appending linkage variables should go into a section of
305 // their name or something. For now, just emit them as external.
306 case GlobalValue::ExternalLinkage:
307 // If external or appending, declare as a global symbol
308 O << TAI->getGlobalDirective() << name << '\n';
310 case GlobalValue::InternalLinkage:
311 case GlobalValue::PrivateLinkage:
313 case GlobalValue::GhostLinkage:
314 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
316 case GlobalValue::DLLImportLinkage:
317 cerr << "DLLImport linkage is not supported by this target!\n";
319 case GlobalValue::DLLExportLinkage:
320 cerr << "DLLExport linkage is not supported by this target!\n";
323 assert(0 && "Unknown linkage type!");
326 EmitAlignment(Align, GVar);
328 if (TAI->hasDotTypeDotSizeDirective()) {
329 O << "\t.type " << name << ",@object\n";
330 O << "\t.size " << name << ',' << Size << '\n';
334 EmitGlobalConstant(C);
338 bool IA64AsmPrinter::doFinalization(Module &M) {
339 // Print out module-level global variables here.
340 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
342 printModuleLevelGV(I);
344 // we print out ".global X \n .type X, @function" for each external function
345 O << "\n\n// br.call targets referenced (and not defined) above: \n";
346 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
347 e = ExternalFunctionNames.end(); i!=e; ++i) {
348 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
352 // we print out ".global X \n .type X, @object" for each external object
353 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
354 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
355 e = ExternalObjectNames.end(); i!=e; ++i) {
356 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
360 return AsmPrinter::doFinalization(M);
363 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
364 /// assembly code for a MachineFunction to the given output stream, using
365 /// the given target machine description.
367 FunctionPass *llvm::createIA64CodePrinterPass(raw_ostream &o,
368 IA64TargetMachine &tm) {
369 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());