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/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/Mangler.h"
28 #include "llvm/ADT/Statistic.h"
32 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
34 struct IA64AsmPrinter : public AsmPrinter {
35 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
37 IA64AsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
38 : AsmPrinter(O, TM, T) {
41 virtual const char *getPassName() const {
42 return "IA64 Assembly Printer";
45 /// printInstruction - This method is automatically generated by tablegen
46 /// from the instruction set description. This method returns true if the
47 /// machine instruction was sufficiently described to print it, otherwise it
49 bool printInstruction(const MachineInstr *MI);
51 // This method is used by the tablegen'erated instruction printer.
52 void printOperand(const MachineInstr *MI, unsigned OpNo){
53 const MachineOperand &MO = MI->getOperand(OpNo);
54 if (MO.getType() == MachineOperand::MO_Register) {
55 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
56 //XXX Bug Workaround: See note in Printer::doInitialization about %.
57 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
63 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
64 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
65 if(val>=128) val=val-256; // if negative, flip sign
68 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
69 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
70 if(val>=8192) val=val-16384; // if negative, flip sign
73 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
74 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
75 if(val>=2097152) val=val-4194304; // if negative, flip sign
78 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
79 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
81 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
82 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
83 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
84 // emit movl rX = @gprel(CPI<whatever);;
86 // this gives us 64 bits instead of 22 (for the add long imm) to play
87 // with, which shuts up the linker. The problem is that the constant
88 // pool entries aren't immediates at this stage, so we check here.
89 // If it's an immediate, print it the old fashioned way. If it's
90 // not, we print it as a constant pool index.
91 if(MI->getOperand(OpNo).isImmediate()) {
92 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
93 } else { // this is a constant pool reference: FIXME: assert this
94 printOp(MI->getOperand(OpNo));
98 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
99 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
102 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
103 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
106 std::string getSectionForFunction(const Function &F) const;
108 void printMachineInstruction(const MachineInstr *MI);
109 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
110 bool runOnMachineFunction(MachineFunction &F);
111 bool doInitialization(Module &M);
112 bool doFinalization(Module &M);
114 } // end of anonymous namespace
117 // Include the auto-generated portion of the assembly writer.
118 #include "IA64GenAsmWriter.inc"
121 std::string IA64AsmPrinter::getSectionForFunction(const Function &F) const {
122 // This means "Allocated instruXions in mem, initialized".
123 return "\n\t.section .text, \"ax\", \"progbits\"\n";
126 /// runOnMachineFunction - This uses the printMachineInstruction()
127 /// method to print assembly for each instruction.
129 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
130 SetupMachineFunction(MF);
133 // Print out constants referenced by the function
134 EmitConstantPool(MF.getConstantPool());
136 const Function *F = MF.getFunction();
137 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
139 // Print out labels for the function.
141 O << "\t.global\t" << CurrentFnName << "\n";
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_begin() != I->pred_end()) {
150 printBasicBlockLabel(I, true);
153 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
155 // Print the assembly for the instruction.
157 printMachineInstruction(II);
161 // We didn't modify anything.
165 void IA64AsmPrinter::printOp(const MachineOperand &MO,
166 bool isBRCALLinsn /* = false */) {
167 const MRegisterInfo &RI = *TM.getRegisterInfo();
168 switch (MO.getType()) {
169 case MachineOperand::MO_Register:
170 O << RI.get(MO.getReg()).Name;
173 case MachineOperand::MO_Immediate:
174 O << MO.getImmedValue();
176 case MachineOperand::MO_MachineBasicBlock:
177 printBasicBlockLabel(MO.getMachineBasicBlock());
179 case MachineOperand::MO_ConstantPoolIndex: {
180 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
181 << "CPI" << getFunctionNumber() << "_"
182 << MO.getConstantPoolIndex() << ")";
186 case MachineOperand::MO_GlobalAddress: {
188 // functions need @ltoff(@fptr(fn_name)) form
189 GlobalValue *GV = MO.getGlobal();
190 Function *F = dyn_cast<Function>(GV);
192 bool Needfptr=false; // if we're computing an address @ltoff(X), do
193 // we need to decorate it so it becomes
194 // @ltoff(@fptr(X)) ?
195 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
198 // if this is the target of a call instruction, we should define
199 // the function somewhere (GNU gas has no problem without this, but
200 // Intel ias rightly complains of an 'undefined symbol')
202 if (F /*&& isBRCALLinsn*/ && F->isExternal())
203 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
205 if (GV->isExternal()) // e.g. stuff like 'stdin'
206 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
212 O << Mang->getValueName(MO.getGlobal());
214 if (Needfptr && !isBRCALLinsn)
215 O << "#))"; // close both fptr( and ltoff(
218 O << "#)"; // close only fptr(
220 O << "#)"; // close only ltoff(
223 int Offset = MO.getOffset();
225 O << " + " << Offset;
227 O << " - " << -Offset;
230 case MachineOperand::MO_ExternalSymbol:
231 O << MO.getSymbolName();
232 ExternalFunctionNames.insert(MO.getSymbolName());
235 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
239 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
240 /// MI to the current output stream.
242 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
245 // Call the autogenerated instruction printer routines.
246 printInstruction(MI);
249 bool IA64AsmPrinter::doInitialization(Module &M) {
250 AsmPrinter::doInitialization(M);
252 O << "\n.ident \"LLVM-ia64\"\n\n"
253 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
255 << "\t.psr abi64\n"; // we only support 64 bits for now
259 bool IA64AsmPrinter::doFinalization(Module &M) {
260 const TargetData *TD = TM.getTargetData();
262 // Print out module-level global variables here.
263 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
265 if (I->hasInitializer()) { // External global require no code
266 // Check to see if this is a special global used by LLVM, if so, emit it.
267 if (EmitSpecialLLVMGlobal(I))
271 std::string name = Mang->getValueName(I);
272 Constant *C = I->getInitializer();
273 unsigned Size = TD->getTypeSize(C->getType());
274 unsigned Align = TD->getTypeAlignmentShift(C->getType());
276 if (C->isNullValue() &&
277 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
278 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
279 SwitchToDataSection(".data", I);
280 if (I->hasInternalLinkage()) {
281 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
282 << "," << (1 << Align);
285 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
286 << "," << (1 << Align);
290 switch (I->getLinkage()) {
291 case GlobalValue::LinkOnceLinkage:
292 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
293 // Nonnull linkonce -> weak
294 O << "\t.weak " << name << "\n";
295 O << "\t.section\t.llvm.linkonce.d." << name
296 << ", \"aw\", \"progbits\"\n";
297 SwitchToDataSection("", I);
299 case GlobalValue::AppendingLinkage:
300 // FIXME: appending linkage variables should go into a section of
301 // their name or something. For now, just emit them as external.
302 case GlobalValue::ExternalLinkage:
303 // If external or appending, declare as a global symbol
304 O << "\t.global " << name << "\n";
306 case GlobalValue::InternalLinkage:
307 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
309 case GlobalValue::GhostLinkage:
310 cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
312 case GlobalValue::DLLImportLinkage:
313 cerr << "DLLImport linkage is not supported by this target!\n";
315 case GlobalValue::DLLExportLinkage:
316 cerr << "DLLExport linkage is not supported by this target!\n";
319 assert(0 && "Unknown linkage type!");
322 EmitAlignment(Align);
323 O << "\t.type " << name << ",@object\n";
324 O << "\t.size " << name << "," << Size << "\n";
325 O << name << ":\t\t\t\t// " << *C << "\n";
326 EmitGlobalConstant(C);
330 // we print out ".global X \n .type X, @function" for each external function
331 O << "\n\n// br.call targets referenced (and not defined) above: \n";
332 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
333 e = ExternalFunctionNames.end(); i!=e; ++i) {
334 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
338 // we print out ".global X \n .type X, @object" for each external object
339 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
340 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
341 e = ExternalObjectNames.end(); i!=e; ++i) {
342 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
346 AsmPrinter::doFinalization(M);
347 return false; // success
350 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
351 /// assembly code for a MachineFunction to the given output stream, using
352 /// the given target machine description.
354 FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,
355 IA64TargetMachine &tm) {
356 return new IA64AsmPrinter(o, tm, tm.getTargetAsmInfo());