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/CodeGen/ValueTypes.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Support/CommandLine.h"
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36 struct IA64AsmPrinter : public AsmPrinter {
37 std::set<std::string> ExternalFunctionNames, ExternalObjectNames;
39 IA64AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
41 Data8bitsDirective = "\tdata1\t"; // FIXME: check that we are
42 Data16bitsDirective = "\tdata2.ua\t"; // disabling auto-alignment
43 Data32bitsDirective = "\tdata4.ua\t"; // properly
44 Data64bitsDirective = "\tdata8.ua\t";
45 ZeroDirective = "\t.skip\t";
46 AsciiDirective = "\tstring\t";
48 GlobalVarAddrPrefix="";
49 GlobalVarAddrSuffix="";
50 FunctionAddrPrefix="@fptr(";
51 FunctionAddrSuffix=")";
53 // FIXME: would be nice to have rodata (no 'w') when appropriate?
54 ConstantPoolSection = "\n\t.section .data, \"aw\", \"progbits\"\n";
57 virtual const char *getPassName() const {
58 return "IA64 Assembly Printer";
61 /// printInstruction - This method is automatically generated by tablegen
62 /// from the instruction set description. This method returns true if the
63 /// machine instruction was sufficiently described to print it, otherwise it
65 bool printInstruction(const MachineInstr *MI);
67 // This method is used by the tablegen'erated instruction printer.
68 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
69 const MachineOperand &MO = MI->getOperand(OpNo);
70 if (MO.getType() == MachineOperand::MO_MachineRegister) {
71 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
72 //XXX Bug Workaround: See note in Printer::doInitialization about %.
73 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
79 void printS8ImmOperand(const MachineInstr *MI, unsigned OpNo,
81 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
82 if(val>=128) val=val-256; // if negative, flip sign
85 void printS14ImmOperand(const MachineInstr *MI, unsigned OpNo,
87 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
88 if(val>=8192) val=val-16384; // if negative, flip sign
91 void printS22ImmOperand(const MachineInstr *MI, unsigned OpNo,
93 int val=(unsigned int)MI->getOperand(OpNo).getImmedValue();
94 if(val>=2097152) val=val-4194304; // if negative, flip sign
97 void printU64ImmOperand(const MachineInstr *MI, unsigned OpNo,
99 O << (uint64_t)MI->getOperand(OpNo).getImmedValue();
101 void printS64ImmOperand(const MachineInstr *MI, unsigned OpNo,
103 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
104 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
105 // emit movl rX = @gprel(CPI<whatever);;
107 // this gives us 64 bits instead of 22 (for the add long imm) to play
108 // with, which shuts up the linker. The problem is that the constant
109 // pool entries aren't immediates at this stage, so we check here.
110 // If it's an immediate, print it the old fashioned way. If it's
111 // not, we print it as a constant pool index.
112 if(MI->getOperand(OpNo).isImmediate()) {
113 O << (int64_t)MI->getOperand(OpNo).getImmedValue();
114 } else { // this is a constant pool reference: FIXME: assert this
115 printOp(MI->getOperand(OpNo));
119 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,
126 printOp(MI->getOperand(OpNo), true); // this is a br.call instruction
129 void printMachineInstruction(const MachineInstr *MI);
130 void printOp(const MachineOperand &MO, bool isBRCALLinsn= false);
131 bool runOnMachineFunction(MachineFunction &F);
132 bool doInitialization(Module &M);
133 bool doFinalization(Module &M);
135 } // end of anonymous namespace
138 // Include the auto-generated portion of the assembly writer.
139 #include "IA64GenAsmWriter.inc"
142 /// runOnMachineFunction - This uses the printMachineInstruction()
143 /// method to print assembly for each instruction.
145 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
146 SetupMachineFunction(MF);
149 // Print out constants referenced by the function
150 EmitConstantPool(MF.getConstantPool());
152 // Print out labels for the function.
153 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF.getFunction());
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 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_"
166 << I->getNumber() << ":\t"
167 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
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_VirtualRegister:
185 if (Value *V = MO.getVRegValueOrNull()) {
186 O << "<" << V->getName() << ">";
190 case MachineOperand::MO_MachineRegister:
191 case MachineOperand::MO_CCRegister: {
192 O << RI.get(MO.getReg()).Name;
196 case MachineOperand::MO_SignExtendedImmed:
197 case MachineOperand::MO_UnextendedImmed:
198 O << /*(unsigned int)*/MO.getImmedValue();
200 case MachineOperand::MO_MachineBasicBlock: {
201 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
202 O << PrivateGlobalPrefix << "LBB"
203 << Mang->getValueName(MBBOp->getParent()->getFunction())
204 << "_" << MBBOp->getNumber () << "\t// "
205 << MBBOp->getBasicBlock ()->getName ();
208 case MachineOperand::MO_PCRelativeDisp:
209 std::cerr << "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
213 case MachineOperand::MO_ConstantPoolIndex: {
214 O << "@gprel(" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
215 << MO.getConstantPoolIndex() << ")";
219 case MachineOperand::MO_GlobalAddress: {
221 // functions need @ltoff(@fptr(fn_name)) form
222 GlobalValue *GV = MO.getGlobal();
223 Function *F = dyn_cast<Function>(GV);
225 bool Needfptr=false; // if we're computing an address @ltoff(X), do
226 // we need to decorate it so it becomes
227 // @ltoff(@fptr(X)) ?
228 if (F && !isBRCALLinsn /*&& F->isExternal()*/)
231 // if this is the target of a call instruction, we should define
232 // the function somewhere (GNU gas has no problem without this, but
233 // Intel ias rightly complains of an 'undefined symbol')
235 if (F /*&& isBRCALLinsn*/ && F->isExternal())
236 ExternalFunctionNames.insert(Mang->getValueName(MO.getGlobal()));
238 if (GV->isExternal()) // e.g. stuff like 'stdin'
239 ExternalObjectNames.insert(Mang->getValueName(MO.getGlobal()));
245 O << Mang->getValueName(MO.getGlobal());
247 O << ")"; // close fptr(
249 O << ")"; // close ltoff(
250 int Offset = MO.getOffset();
252 O << " + " << Offset;
254 O << " - " << -Offset;
257 case MachineOperand::MO_ExternalSymbol:
258 O << MO.getSymbolName();
259 ExternalFunctionNames.insert(MO.getSymbolName());
262 O << "<AsmPrinter: unknown operand type: " << MO.getType() << " >"; return;
266 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
267 /// MI to the current output stream.
269 void IA64AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
272 // Call the autogenerated instruction printer routines.
273 printInstruction(MI);
276 bool IA64AsmPrinter::doInitialization(Module &M) {
277 AsmPrinter::doInitialization(M);
279 O << "\n.ident \"LLVM-ia64\"\n\n"
280 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
282 << "\t.psr abi64\n"; // we only support 64 bits for now
286 bool IA64AsmPrinter::doFinalization(Module &M) {
287 const TargetData &TD = TM.getTargetData();
289 // Print out module-level global variables here.
290 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
292 if (I->hasInitializer()) { // External global require no code
294 std::string name = Mang->getValueName(I);
295 Constant *C = I->getInitializer();
296 unsigned Size = TD.getTypeSize(C->getType());
297 unsigned Align = TD.getTypeAlignmentShift(C->getType());
299 if (C->isNullValue() &&
300 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
301 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
302 SwitchSection(".data", I);
303 if (I->hasInternalLinkage()) {
304 O << "\t.lcomm " << name << "," << TD.getTypeSize(C->getType())
305 << "," << (1 << Align);
308 O << "\t.common " << name << "," << TD.getTypeSize(C->getType())
309 << "," << (1 << Align);
312 WriteAsOperand(O, I, true, true, &M);
315 switch (I->getLinkage()) {
316 case GlobalValue::LinkOnceLinkage:
317 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
318 // Nonnull linkonce -> weak
319 O << "\t.weak " << name << "\n";
320 O << "\t.section\t.llvm.linkonce.d." << name
321 << ", \"aw\", \"progbits\"\n";
322 SwitchSection("", I);
324 case GlobalValue::AppendingLinkage:
325 // FIXME: appending linkage variables should go into a section of
326 // their name or something. For now, just emit them as external.
327 case GlobalValue::ExternalLinkage:
328 // If external or appending, declare as a global symbol
329 O << "\t.global " << name << "\n";
331 case GlobalValue::InternalLinkage:
332 SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
334 case GlobalValue::GhostLinkage:
335 std::cerr << "GhostLinkage cannot appear in IA64AsmPrinter!\n";
339 EmitAlignment(Align);
340 O << "\t.type " << name << ",@object\n";
341 O << "\t.size " << name << "," << Size << "\n";
342 O << name << ":\t\t\t\t// ";
343 WriteAsOperand(O, I, true, true, &M);
345 WriteAsOperand(O, C, false, false, &M);
347 EmitGlobalConstant(C);
351 // we print out ".global X \n .type X, @function" for each external function
352 O << "\n\n// br.call targets referenced (and not defined) above: \n";
353 for (std::set<std::string>::iterator i = ExternalFunctionNames.begin(),
354 e = ExternalFunctionNames.end(); i!=e; ++i) {
355 O << "\t.global " << *i << "\n\t.type " << *i << ", @function\n";
359 // we print out ".global X \n .type X, @object" for each external object
360 O << "\n\n// (external) symbols referenced (and not defined) above: \n";
361 for (std::set<std::string>::iterator i = ExternalObjectNames.begin(),
362 e = ExternalObjectNames.end(); i!=e; ++i) {
363 O << "\t.global " << *i << "\n\t.type " << *i << ", @object\n";
367 AsmPrinter::doFinalization(M);
368 return false; // success
371 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
372 /// assembly code for a MachineFunction to the given output stream, using
373 /// the given target machine description.
375 FunctionPass *llvm::createIA64CodePrinterPass(std::ostream &o,TargetMachine &tm){
376 return new IA64AsmPrinter(o, tm);