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 //===----------------------------------------------------------------------===//
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/MachineFunctionPass.h"
26 #include "llvm/Target/TargetAsmInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
32 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
35 struct IA64AsmPrinter
: public AsmPrinter
{
36 std::set
<std::string
> ExternalFunctionNames
, ExternalObjectNames
;
38 IA64AsmPrinter(std::ostream
&O
, TargetMachine
&TM
, const TargetAsmInfo
*T
)
39 : AsmPrinter(O
, TM
, T
) {
42 virtual const char *getPassName() const {
43 return "IA64 Assembly Printer";
46 /// printInstruction - This method is automatically generated by tablegen
47 /// from the instruction set description. This method returns true if the
48 /// machine instruction was sufficiently described to print it, otherwise it
50 bool printInstruction(const MachineInstr
*MI
);
52 // This method is used by the tablegen'erated instruction printer.
53 void printOperand(const MachineInstr
*MI
, unsigned OpNo
){
54 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
55 if (MO
.getType() == MachineOperand::MO_Register
) {
56 assert(MRegisterInfo::isPhysicalRegister(MO
.getReg())&&"Not physref??");
57 //XXX Bug Workaround: See note in Printer::doInitialization about %.
58 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).Name
;
64 void printS8ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
65 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
66 if(val
>=128) val
=val
-256; // if negative, flip sign
69 void printS14ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
70 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
71 if(val
>=8192) val
=val
-16384; // if negative, flip sign
74 void printS22ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
75 int val
=(unsigned int)MI
->getOperand(OpNo
).getImmedValue();
76 if(val
>=2097152) val
=val
-4194304; // if negative, flip sign
79 void printU64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
80 O
<< (uint64_t)MI
->getOperand(OpNo
).getImmedValue();
82 void printS64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
83 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
84 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
85 // emit movl rX = @gprel(CPI<whatever);;
87 // this gives us 64 bits instead of 22 (for the add long imm) to play
88 // with, which shuts up the linker. The problem is that the constant
89 // pool entries aren't immediates at this stage, so we check here.
90 // If it's an immediate, print it the old fashioned way. If it's
91 // not, we print it as a constant pool index.
92 if(MI
->getOperand(OpNo
).isImmediate()) {
93 O
<< (int64_t)MI
->getOperand(OpNo
).getImmedValue();
94 } else { // this is a constant pool reference: FIXME: assert this
95 printOp(MI
->getOperand(OpNo
));
99 void printGlobalOperand(const MachineInstr
*MI
, unsigned OpNo
) {
100 printOp(MI
->getOperand(OpNo
), false); // this is NOT a br.call instruction
103 void printCallOperand(const MachineInstr
*MI
, unsigned OpNo
) {
104 printOp(MI
->getOperand(OpNo
), true); // this is a br.call instruction
107 std::string
getSectionForFunction(const Function
&F
) const;
109 void printMachineInstruction(const MachineInstr
*MI
);
110 void printOp(const MachineOperand
&MO
, bool isBRCALLinsn
= false);
111 bool runOnMachineFunction(MachineFunction
&F
);
112 bool doInitialization(Module
&M
);
113 bool doFinalization(Module
&M
);
115 } // end of anonymous namespace
118 // Include the auto-generated portion of the assembly writer.
119 #include "IA64GenAsmWriter.inc"
122 std::string
IA64AsmPrinter::getSectionForFunction(const Function
&F
) const {
123 // This means "Allocated instruXions in mem, initialized".
124 return "\n\t.section .text, \"ax\", \"progbits\"\n";
127 /// runOnMachineFunction - This uses the printMachineInstruction()
128 /// method to print assembly for each instruction.
130 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
131 SetupMachineFunction(MF
);
134 // Print out constants referenced by the function
135 EmitConstantPool(MF
.getConstantPool());
137 const Function
*F
= MF
.getFunction();
138 SwitchToTextSection(getSectionForFunction(*F
).c_str(), F
);
140 // Print out labels for the function.
142 O
<< "\t.global\t" << CurrentFnName
<< "\n";
143 O
<< "\t.type\t" << CurrentFnName
<< ", @function\n";
144 O
<< CurrentFnName
<< ":\n";
146 // Print out code for the function.
147 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
149 // Print a label for the basic block if there are any predecessors.
150 if (I
->pred_begin() != I
->pred_end()) {
151 printBasicBlockLabel(I
, true);
154 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
156 // Print the assembly for the instruction.
158 printMachineInstruction(II
);
162 // We didn't modify anything.
166 void IA64AsmPrinter::printOp(const MachineOperand
&MO
,
167 bool isBRCALLinsn
/* = false */) {
168 const MRegisterInfo
&RI
= *TM
.getRegisterInfo();
169 switch (MO
.getType()) {
170 case MachineOperand::MO_Register
:
171 O
<< RI
.get(MO
.getReg()).Name
;
174 case MachineOperand::MO_Immediate
:
175 O
<< MO
.getImmedValue();
177 case MachineOperand::MO_MachineBasicBlock
:
178 printBasicBlockLabel(MO
.getMachineBasicBlock());
180 case MachineOperand::MO_ConstantPoolIndex
: {
181 O
<< "@gprel(" << TAI
->getPrivateGlobalPrefix()
182 << "CPI" << getFunctionNumber() << "_"
183 << MO
.getConstantPoolIndex() << ")";
187 case MachineOperand::MO_GlobalAddress
: {
189 // functions need @ltoff(@fptr(fn_name)) form
190 GlobalValue
*GV
= MO
.getGlobal();
191 Function
*F
= dyn_cast
<Function
>(GV
);
193 bool Needfptr
=false; // if we're computing an address @ltoff(X), do
194 // we need to decorate it so it becomes
195 // @ltoff(@fptr(X)) ?
196 if (F
&& !isBRCALLinsn
/*&& F->isDeclaration()*/)
199 // if this is the target of a call instruction, we should define
200 // the function somewhere (GNU gas has no problem without this, but
201 // Intel ias rightly complains of an 'undefined symbol')
203 if (F
/*&& isBRCALLinsn*/ && F
->isDeclaration())
204 ExternalFunctionNames
.insert(Mang
->getValueName(MO
.getGlobal()));
206 if (GV
->isDeclaration()) // e.g. stuff like 'stdin'
207 ExternalObjectNames
.insert(Mang
->getValueName(MO
.getGlobal()));
213 O
<< Mang
->getValueName(MO
.getGlobal());
215 if (Needfptr
&& !isBRCALLinsn
)
216 O
<< "#))"; // close both fptr( and ltoff(
219 O
<< "#)"; // close only fptr(
221 O
<< "#)"; // close only ltoff(
224 int Offset
= MO
.getOffset();
226 O
<< " + " << Offset
;
228 O
<< " - " << -Offset
;
231 case MachineOperand::MO_ExternalSymbol
:
232 O
<< MO
.getSymbolName();
233 ExternalFunctionNames
.insert(MO
.getSymbolName());
236 O
<< "<AsmPrinter: unknown operand type: " << MO
.getType() << " >"; return;
240 /// printMachineInstruction -- Print out a single IA64 LLVM instruction
241 /// MI to the current output stream.
243 void IA64AsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
246 // Call the autogenerated instruction printer routines.
247 printInstruction(MI
);
250 bool IA64AsmPrinter::doInitialization(Module
&M
) {
251 bool Result
= AsmPrinter::doInitialization(M
);
253 O
<< "\n.ident \"LLVM-ia64\"\n\n"
254 << "\t.psr lsb\n" // should be "msb" on HP-UX, for starters
256 << "\t.psr abi64\n"; // we only support 64 bits for now
260 bool IA64AsmPrinter::doFinalization(Module
&M
) {
261 const TargetData
*TD
= TM
.getTargetData();
263 // Print out module-level global variables here.
264 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
266 if (I
->hasInitializer()) { // External global require no code
267 // Check to see if this is a special global used by LLVM, if so, emit it.
268 if (EmitSpecialLLVMGlobal(I
))
272 std::string name
= Mang
->getValueName(I
);
273 Constant
*C
= I
->getInitializer();
274 unsigned Size
= TD
->getTypeSize(C
->getType());
275 unsigned Align
= TD
->getPreferredTypeAlignmentShift(C
->getType());
277 if (C
->isNullValue() &&
278 (I
->hasLinkOnceLinkage() || I
->hasInternalLinkage() ||
279 I
->hasWeakLinkage() /* FIXME: Verify correct */)) {
280 SwitchToDataSection(".data", I
);
281 if (I
->hasInternalLinkage()) {
282 O
<< "\t.lcomm " << name
<< "#," << TD
->getTypeSize(C
->getType())
283 << "," << (1 << Align
);
286 O
<< "\t.common " << name
<< "#," << TD
->getTypeSize(C
->getType())
287 << "," << (1 << Align
);
291 switch (I
->getLinkage()) {
292 case GlobalValue::LinkOnceLinkage
:
293 case GlobalValue::WeakLinkage
: // FIXME: Verify correct for weak.
294 // Nonnull linkonce -> weak
295 O
<< "\t.weak " << name
<< "\n";
296 O
<< "\t.section\t.llvm.linkonce.d." << name
297 << ", \"aw\", \"progbits\"\n";
298 SwitchToDataSection("", I
);
300 case GlobalValue::AppendingLinkage
:
301 // FIXME: appending linkage variables should go into a section of
302 // their name or something. For now, just emit them as external.
303 case GlobalValue::ExternalLinkage
:
304 // If external or appending, declare as a global symbol
305 O
<< "\t.global " << name
<< "\n";
307 case GlobalValue::InternalLinkage
:
308 SwitchToDataSection(C
->isNullValue() ? ".bss" : ".data", I
);
310 case GlobalValue::GhostLinkage
:
311 cerr
<< "GhostLinkage cannot appear in IA64AsmPrinter!\n";
313 case GlobalValue::DLLImportLinkage
:
314 cerr
<< "DLLImport linkage is not supported by this target!\n";
316 case GlobalValue::DLLExportLinkage
:
317 cerr
<< "DLLExport linkage is not supported by this target!\n";
320 assert(0 && "Unknown linkage type!");
323 EmitAlignment(Align
);
324 O
<< "\t.type " << name
<< ",@object\n";
325 O
<< "\t.size " << name
<< "," << Size
<< "\n";
326 O
<< name
<< ":\t\t\t\t// " << *C
<< "\n";
327 EmitGlobalConstant(C
);
331 // we print out ".global X \n .type X, @function" for each external function
332 O
<< "\n\n// br.call targets referenced (and not defined) above: \n";
333 for (std::set
<std::string
>::iterator i
= ExternalFunctionNames
.begin(),
334 e
= ExternalFunctionNames
.end(); i
!=e
; ++i
) {
335 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @function\n";
339 // we print out ".global X \n .type X, @object" for each external object
340 O
<< "\n\n// (external) symbols referenced (and not defined) above: \n";
341 for (std::set
<std::string
>::iterator i
= ExternalObjectNames
.begin(),
342 e
= ExternalObjectNames
.end(); i
!=e
; ++i
) {
343 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @object\n";
347 return AsmPrinter::doFinalization(M
);
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());