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/MachineFunctionPass.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetAsmInfo.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36 struct VISIBILITY_HIDDEN IA64TargetAsmInfo : public TargetAsmInfo {
39 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
40 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
41 Data32bitsDirective = "\tdata4.ua\t"; // properly
42 Data64bitsDirective = "\tdata8.ua\t";
43 ZeroDirective = "\t.skip\t";
44 AsciiDirective = "\tstring\t";
46 GlobalVarAddrPrefix="";
47 GlobalVarAddrSuffix="";
48 FunctionAddrPrefix="@fptr(";
49 FunctionAddrSuffix=")";
51 // FIXME: would be nice to have rodata (no 'w') when appropriate?
52 ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
56 struct IA64AsmPrinter : public AsmPrinter {
57 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
59 IA64AsmPrinter(std::ostream &O, TargetMachine &TM, TargetAsmInfo *T)
60 : AsmPrinter(O, TM, T) {
63 virtual const char *getPassName() const {
64 return "IA64 Assembly Printer";
67 /// printInstruction - This method is automatically generated by tablegen
68 /// from the instruction set description. This method returns true if the
69 /// machine instruction was sufficiently described to print it, otherwise it
71 bool printInstruction(const MachineInstr *MI);
73 // This method is used by the tablegen'erated instruction printer.
74 void printOperand(const MachineInstr *MI, unsigned OpNo){
75 const MachineOperand &MO = MI->getOperand(OpNo);
76 if (MO.getType() == MachineOperand::MO_Register) {
77 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
78 //XXX Bug Workaround: See note in Printer::doInitialization about %.
79 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
85 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo) {
86 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
87 if(val>=128) val=val-256; // if negative, flip sign
90 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo) {
91 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
92 if(val>=8192) val=val-16384; // if negative, flip sign
95 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo) {
96 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
97 if(val>=2097152) val=val-4194304; // if negative, flip sign
100 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
101 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
103 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo) {
104 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
105 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
106 // emit movl rX = @gprel(CPI<whatever);;
108 // this gives us 64 bits instead of 22 (for the add long imm) to play
109 // with, which shuts up the linker. The problem is that the constant
110 // pool entries aren't immediates at this stage, so we check here.
111 // If it's an immediate, print it the old fashioned way. If it's
112 // not, we print it as a constant pool index.
113 if(MI->getOperand(OpNo).isImmediate()) {
114 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
115 } else { // this is a constant pool reference: FIXME: assert this
116 printOp(MI->getOperand(OpNo));
120 void printGlobalOperand(const MachineInstr *MI, unsigned OpNo) {
121 printOp(MI->getOperand(OpNo), false); // this is NOT a br.call instruction
124 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
125 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
128 void printMachineInstruction(const MachineInstr *MI);
129 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
130 bool runOnMachineFunction(MachineFunction &F);
131 bool doInitialization(Module &M);
132 bool doFinalization(Module &M);
134 } // end of anonymous namespace
137 // Include the auto-generated portion of the assembly writer.
138 #include "IA64GenAsmWriter.inc"
141 /// runOnMachineFunction - This uses the printMachineInstruction()
142 /// method to print assembly for each instruction.
144 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
145 SetupMachineFunction(MF);
148 // Print out constants referenced by the function
149 EmitConstantPool(MF.getConstantPool());
151 // Print out labels for the function.
152 SwitchToTextSection("\n\t.section .text, \"ax\", \"progbits\"\n",
154 // ^^ means "Allocated instruXions in mem, initialized"
156 O << "\t.global\t" << CurrentFnName << "\n";
157 O << "\t.type\t" << CurrentFnName << ", @function\n";
158 O << CurrentFnName << ":\n";
160 // Print out code for the function.
161 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
163 // Print a label for the basic block if there are any predecessors.
164 if (I->pred_begin() != I->pred_end()) {
165 printBasicBlockLabel(I, true);
168 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
170 // Print the assembly for the instruction.
172 printMachineInstruction(II);
176 // We didn't modify anything.
180 void IA64AsmPrinter::printOp(const MachineOperand &MO,
181 bool isBRCALLinsn /* = false */) {
182 const MRegisterInfo &RI = *TM.getRegisterInfo();
183 switch (MO.getType()) {
184 case MachineOperand::MO_Register:
185 O << RI.get(MO.getReg()).Name;
188 case MachineOperand::MO_Immediate:
189 O << MO.getImmedValue();
191 case MachineOperand::MO_MachineBasicBlock:
192 printBasicBlockLabel(MO.getMachineBasicBlock());
194 case MachineOperand::MO_ConstantPoolIndex: {
195 O << "@gprel(" << TAI->getPrivateGlobalPrefix()
196 << "CPI" << getFunctionNumber() << "_"
197 << MO.getConstantPoolIndex() << ")";
201 case MachineOperand::MO_GlobalAddress: {
203 // functions need @ltoff(@fptr(fn_name)) form
204 GlobalValue *GV = MO.getGlobal();
205 Function *F = dyn_cast<Function>(GV);
207 bool Needfptr=false; // if we're computing an address @ltoff(X), do
208 // we need to decorate it so it becomes
209 // @ltoff(@fptr(X)) ?
210 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
213 // if this is the target of a call instruction, we should define
214 // the function somewhere (GNU gas has no problem without this, but
215 // Intel ias rightly complains of an 'undefined symbol')
217 if (F /*&& isBRCALLinsn*/ && F->isExternal())
218 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
220 if (GV->isExternal()) // e.g. stuff like 'stdin'
221 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
227 O << Mang->getValueName(MO.getGlobal());
229 if (Needfptr && !isBRCALLinsn)
230 O << "#))"; // close both fptr( and ltoff(
233 O << "#)"; // close only fptr(
235 O << "#)"; // close only ltoff(
238 int Offset = MO.getOffset();
240 O << " + " << Offset;
242 O << " - " << -Offset;
245 case MachineOperand::MO_ExternalSymbol:
246 O << MO.getSymbolName();
247 ExternalFunctionNames.insert(MO.getSymbolName());
250 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
254 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
255 /// MI to the current output stream.
257 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
260 // Call the autogenerated instruction printer routines.
261 printInstruction(MI);
264 bool IA64AsmPrinter::doInitialization(Module &M) {
265 AsmPrinter::doInitialization(M);
267 O << "\n.ident \"LLVM-ia64\"\n\n"
268 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
270 << "\t.psr abi64\n"; // we only support 64 bits for now
274 bool IA64AsmPrinter::doFinalization(Module &M) {
275 const TargetData *TD = TM.getTargetData();
277 // Print out module-level global variables here.
278 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
280 if (I->hasInitializer()) { // External global require no code
281 // Check to see if this is a special global used by LLVM, if so, emit it.
282 if (EmitSpecialLLVMGlobal(I))
286 std::string name = Mang->getValueName(I);
287 Constant *C = I->getInitializer();
288 unsigned Size = TD->getTypeSize(C->getType());
289 unsigned Align = TD->getTypeAlignmentShift(C->getType());
291 if (C->isNullValue() &&
292 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
293 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
294 SwitchToDataSection(".data", I);
295 if (I->hasInternalLinkage()) {
296 O << "\t.lcomm " << name << "#," << TD->getTypeSize(C->getType())
297 << "," << (1 << Align);
300 O << "\t.common " << name << "#," << TD->getTypeSize(C->getType())
301 << "," << (1 << Align);
304 WriteAsOperand(O, I, true, true, &M);
307 switch (I->getLinkage()) {
308 case GlobalValue::LinkOnceLinkage:
309 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
310 // Nonnull linkonce -> weak
311 O << "\t.weak " << name << "\n";
312 O << "\t.section\t.llvm.linkonce.d." << name
313 << ", \"aw\", \"progbits\"\n";
314 SwitchToDataSection("", I);
316 case GlobalValue::AppendingLinkage:
317 // FIXME: appending linkage variables should go into a section of
318 // their name or something. For now, just emit them as external.
319 case GlobalValue::ExternalLinkage:
320 // If external or appending, declare as a global symbol
321 O << "\t.global " << name << "\n";
323 case GlobalValue::InternalLinkage:
324 SwitchToDataSection(C->isNullValue() ? ".bss" : ".data", I);
326 case GlobalValue::GhostLinkage:
327 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
331 EmitAlignment(Align);
332 O << "\t.type " << name << ",@object\n";
333 O << "\t.size " << name << "," << Size << "\n";
334 O << name << ":\t\t\t\t// ";
335 WriteAsOperand(O, I, true, true, &M);
337 WriteAsOperand(O, C, false, false, &M);
339 EmitGlobalConstant(C);
343 // we print out ".global X \n .type X, @function" for each external function
344 O << "\n\n// br.call targets referenced (and not defined) above: \n";
345 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
346 e = ExternalFunctionNames.end(); i!=e; ++i) {
347 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
351 // we print out ".global X \n .type X, @object" for each external object
352 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
353 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
354 e = ExternalObjectNames.end(); i!=e; ++i) {
355 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
359 AsmPrinter::doFinalization(M);
360 return false; // success
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(std::ostream &o,
368 IA64TargetMachine &tm) {
369 IA64TargetAsmInfo *TAI = new IA64TargetAsmInfo();
370 return new IA64AsmPrinter(o, tm, TAI);