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/Support/Mangler.h"
28 #include "llvm/ADT/Statistic.h"
33 Statistic
<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35 struct IA64AsmPrinter
: public AsmPrinter
{
36 std::set
<std::string
> ExternalFunctionNames
, ExternalObjectNames
;
38 IA64AsmPrinter(std::ostream
&O
, TargetMachine
&TM
) : AsmPrinter(O
, TM
) {
40 Data8bitsDirective
= "\tdata1\t"; // FIXME: check that we are
41 Data16bitsDirective
= "\tdata2.ua\t"; // disabling auto-alignment
42 Data32bitsDirective
= "\tdata4.ua\t"; // properly
43 Data64bitsDirective
= "\tdata8.ua\t";
44 ZeroDirective
= "\t.skip\t";
45 AsciiDirective
= "\tstring\t";
47 GlobalVarAddrPrefix
="";
48 GlobalVarAddrSuffix
="";
49 FunctionAddrPrefix
="@fptr(";
50 FunctionAddrSuffix
=")";
52 // FIXME: would be nice to have rodata (no 'w') when appropriate?
53 ConstantPoolSection
= "\n\t.section .data, \"aw\", \"progbits\"\n";
56 virtual const char *getPassName() const {
57 return "IA64 Assembly Printer";
60 /// printInstruction - This method is automatically generated by tablegen
61 /// from the instruction set description. This method returns true if the
62 /// machine instruction was sufficiently described to print it, otherwise it
64 bool printInstruction(const MachineInstr
*MI
);
66 // This method is used by the tablegen'erated instruction printer.
67 void printOperand(const MachineInstr
*MI
, unsigned OpNo
){
68 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
69 if (MO
.getType() == MachineOperand::MO_MachineRegister
) {
70 assert(MRegisterInfo::isPhysicalRegister(MO
.getReg())&&"Not physref??");
71 //XXX Bug Workaround: See note in Printer::doInitialization about %.
72 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).Name
;
78 void printS8ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
79 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
80 if(val
>=128) val
=val
-256; // if negative, flip sign
83 void printS14ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
84 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
85 if(val
>=8192) val
=val
-16384; // if negative, flip sign
88 void printS22ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
89 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
90 if(val
>=2097152) val
=val
-4194304; // if negative, flip sign
93 void printU64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
94 O
<< (uint64_t)MI
->getOperand(OpNo
).getImmedValue();
96 void printS64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
97 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
98 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
99 // emit movl rX = @gprel(CPI<whatever);;
101 // this gives us 64 bits instead of 22 (for the add long imm) to play
102 // with, which shuts up the linker. The problem is that the constant
103 // pool entries aren't immediates at this stage, so we check here.
104 // If it's an immediate, print it the old fashioned way. If it's
105 // not, we print it as a constant pool index.
106 if(MI
->getOperand(OpNo
).isImmediate()) {
107 O
<< (int64_t)MI
->getOperand(OpNo
).getImmedValue();
108 } else { // this is a constant pool reference: FIXME: assert this
109 printOp(MI
->getOperand(OpNo
));
113 void printGlobalOperand(const MachineInstr
*MI
, unsigned OpNo
) {
114 printOp(MI
->getOperand(OpNo
), false); // this is NOT a br.call instruction
117 void printCallOperand(const MachineInstr
*MI
, unsigned OpNo
) {
118 printOp(MI
->getOperand(OpNo
), true); // this is a br.call instruction
121 void printMachineInstruction(const MachineInstr
*MI
);
122 void printOp(const MachineOperand
&MO
, bool isBRCALLinsn
= false);
123 bool runOnMachineFunction(MachineFunction
&F
);
124 bool doInitialization(Module
&M
);
125 bool doFinalization(Module
&M
);
127 } // end of anonymous namespace
130 // Include the auto-generated portion of the assembly writer.
131 #include "IA64GenAsmWriter.inc"
134 /// runOnMachineFunction - This uses the printMachineInstruction()
135 /// method to print assembly for each instruction.
137 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
138 SetupMachineFunction(MF
);
141 // Print out constants referenced by the function
142 EmitConstantPool(MF
.getConstantPool());
144 // Print out labels for the function.
145 SwitchSection("\n\t.section .text, \"ax\", \"progbits\"\n", MF
.getFunction());
146 // ^^ means "Allocated instruXions in mem, initialized"
148 O
<< "\t.global\t" << CurrentFnName
<< "\n";
149 O
<< "\t.type\t" << CurrentFnName
<< ", @function\n";
150 O
<< CurrentFnName
<< ":\n";
152 // Print out code for the function.
153 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
155 // Print a label for the basic block if there are any predecessors.
156 if (I
->pred_begin() != I
->pred_end())
157 O
<< PrivateGlobalPrefix
<< "LBB" << CurrentFnName
<< "_"
158 << I
->getNumber() << ":\t"
159 << CommentString
<< " " << I
->getBasicBlock()->getName() << "\n";
160 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
162 // Print the assembly for the instruction.
164 printMachineInstruction(II
);
168 // We didn't modify anything.
172 void IA64AsmPrinter::printOp(const MachineOperand
&MO
,
173 bool isBRCALLinsn
/* = false */) {
174 const MRegisterInfo
&RI
= *TM
.getRegisterInfo();
175 switch (MO
.getType()) {
176 case MachineOperand::MO_VirtualRegister
:
177 if (Value
*V
= MO
.getVRegValueOrNull()) {
178 O
<< "<" << V
->getName() << ">";
182 case MachineOperand::MO_MachineRegister
:
183 case MachineOperand::MO_CCRegister
: {
184 O
<< RI
.get(MO
.getReg()).Name
;
188 case MachineOperand::MO_SignExtendedImmed
:
189 case MachineOperand::MO_UnextendedImmed
:
190 O
<< /*(unsigned int)*/MO
.getImmedValue();
192 case MachineOperand::MO_MachineBasicBlock
: {
193 MachineBasicBlock
*MBBOp
= MO
.getMachineBasicBlock();
194 O
<< PrivateGlobalPrefix
<< "LBB"
195 << Mang
->getValueName(MBBOp
->getParent()->getFunction())
196 << "_" << MBBOp
->getNumber () << "\t// "
197 << MBBOp
->getBasicBlock ()->getName ();
200 case MachineOperand::MO_PCRelativeDisp
:
201 std::cerr
<< "Shouldn't use addPCDisp() when building IA64 MachineInstrs";
205 case MachineOperand::MO_ConstantPoolIndex
: {
206 O
<< "@gprel(" << PrivateGlobalPrefix
<< "CPI" << getFunctionNumber() << "_"
207 << MO
.getConstantPoolIndex() << ")";
211 case MachineOperand::MO_GlobalAddress
: {
213 // functions need @ltoff(@fptr(fn_name)) form
214 GlobalValue
*GV
= MO
.getGlobal();
215 Function
*F
= dyn_cast
<Function
>(GV
);
217 bool Needfptr
=false; // if we're computing an address @ltoff(X), do
218 // we need to decorate it so it becomes
219 // @ltoff(@fptr(X)) ?
220 if (F
&& !isBRCALLinsn
/*&& F->isExternal()*/)
223 // if this is the target of a call instruction, we should define
224 // the function somewhere (GNU gas has no problem without this, but
225 // Intel ias rightly complains of an 'undefined symbol')
227 if (F
/*&& isBRCALLinsn*/ && F
->isExternal())
228 ExternalFunctionNames
.insert(Mang
->getValueName(MO
.getGlobal()));
230 if (GV
->isExternal()) // e.g. stuff like 'stdin'
231 ExternalObjectNames
.insert(Mang
->getValueName(MO
.getGlobal()));
237 O
<< Mang
->getValueName(MO
.getGlobal());
239 if (Needfptr
&& !isBRCALLinsn
)
240 O
<< "#))"; // close both fptr( and ltoff(
243 O
<< "#)"; // close only fptr(
245 O
<< "#)"; // close only ltoff(
248 int Offset
= MO
.getOffset();
250 O
<< " + " << Offset
;
252 O
<< " - " << -Offset
;
255 case MachineOperand::MO_ExternalSymbol
:
256 O
<< MO
.getSymbolName();
257 ExternalFunctionNames
.insert(MO
.getSymbolName());
260 O
<< "<AsmPrinter: unknown operand type: " << MO
.getType() << " >"; return;
264 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
265 /// MI to the current output stream.
267 void IA64AsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
270 // Call the autogenerated instruction printer routines.
271 printInstruction(MI
);
274 bool IA64AsmPrinter::doInitialization(Module
&M
) {
275 AsmPrinter::doInitialization(M
);
277 O
<< "\n.ident \"LLVM-ia64\"\n\n"
278 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
280 << "\t.psr abi64\n"; // we only support 64 bits for now
284 bool IA64AsmPrinter::doFinalization(Module
&M
) {
285 const TargetData
&TD
= TM
.getTargetData();
287 // Print out module-level global variables here.
288 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
290 if (I
->hasInitializer()) { // External global require no code
291 // Check to see if this is a special global used by LLVM, if so, emit it.
292 if (EmitSpecialLLVMGlobal(I
))
296 std::string name
= Mang
->getValueName(I
);
297 Constant
*C
= I
->getInitializer();
298 unsigned Size
= TD
.getTypeSize(C
->getType());
299 unsigned Align
= TD
.getTypeAlignmentShift(C
->getType());
301 if (C
->isNullValue() &&
302 (I
->hasLinkOnceLinkage() || I
->hasInternalLinkage() ||
303 I
->hasWeakLinkage() /* FIXME: Verify correct */)) {
304 SwitchSection(".data", I
);
305 if (I
->hasInternalLinkage()) {
306 O
<< "\t.lcomm " << name
<< "#," << TD
.getTypeSize(C
->getType())
307 << "," << (1 << Align
);
310 O
<< "\t.common " << name
<< "#," << TD
.getTypeSize(C
->getType())
311 << "," << (1 << Align
);
314 WriteAsOperand(O
, I
, true, true, &M
);
317 switch (I
->getLinkage()) {
318 case GlobalValue::LinkOnceLinkage
:
319 case GlobalValue::WeakLinkage
: // FIXME: Verify correct for weak.
320 // Nonnull linkonce -> weak
321 O
<< "\t.weak " << name
<< "\n";
322 O
<< "\t.section\t.llvm.linkonce.d." << name
323 << ", \"aw\", \"progbits\"\n";
324 SwitchSection("", I
);
326 case GlobalValue::AppendingLinkage
:
327 // FIXME: appending linkage variables should go into a section of
328 // their name or something. For now, just emit them as external.
329 case GlobalValue::ExternalLinkage
:
330 // If external or appending, declare as a global symbol
331 O
<< "\t.global " << name
<< "\n";
333 case GlobalValue::InternalLinkage
:
334 SwitchSection(C
->isNullValue() ? ".bss" : ".data", I
);
336 case GlobalValue::GhostLinkage
:
337 std::cerr
<< "GhostLinkage cannot appear in IA64AsmPrinter!\n";
341 EmitAlignment(Align
);
342 O
<< "\t.type " << name
<< ",@object\n";
343 O
<< "\t.size " << name
<< "," << Size
<< "\n";
344 O
<< name
<< ":\t\t\t\t// ";
345 WriteAsOperand(O
, I
, true, true, &M
);
347 WriteAsOperand(O
, C
, false, false, &M
);
349 EmitGlobalConstant(C
);
353 // we print out ".global X \n .type X, @function" for each external function
354 O
<< "\n\n// br.call targets referenced (and not defined) above: \n";
355 for (std::set
<std::string
>::iterator i
= ExternalFunctionNames
.begin(),
356 e
= ExternalFunctionNames
.end(); i
!=e
; ++i
) {
357 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @function\n";
361 // we print out ".global X \n .type X, @object" for each external object
362 O
<< "\n\n// (external) symbols referenced (and not defined) above: \n";
363 for (std::set
<std::string
>::iterator i
= ExternalObjectNames
.begin(),
364 e
= ExternalObjectNames
.end(); i
!=e
; ++i
) {
365 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @object\n";
369 AsmPrinter::doFinalization(M
);
370 return false; // success
373 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
374 /// assembly code for a MachineFunction to the given output stream, using
375 /// the given target machine description.
377 FunctionPass
*llvm::createIA64CodePrinterPass(std::ostream
&o
,
378 IA64TargetMachine
&tm
) {
379 return new IA64AsmPrinter(o
, tm
);