1 //===-- IA64AsmPrinter.cpp - Print out IA64 LLVM as assembly --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/Target/TargetAsmInfo.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/ADT/Statistic.h"
33 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
36 class IA64AsmPrinter
: public AsmPrinter
{
37 std::set
<std::string
> ExternalFunctionNames
, ExternalObjectNames
;
39 explicit IA64AsmPrinter(raw_ostream
&O
, TargetMachine
&TM
,
40 const TargetAsmInfo
*T
, CodeGenOpt::Level OL
,
42 : AsmPrinter(O
, TM
, T
, OL
, V
) {}
44 virtual const char *getPassName() const {
45 return "IA64 Assembly Printer";
48 /// printInstruction - This method is automatically generated by tablegen
49 /// from the instruction set description. This method returns true if the
50 /// machine instruction was sufficiently described to print it, otherwise it
52 bool printInstruction(const MachineInstr
*MI
);
54 // This method is used by the tablegen'erated instruction printer.
55 void printOperand(const MachineInstr
*MI
, unsigned OpNo
){
56 const MachineOperand
&MO
= MI
->getOperand(OpNo
);
57 if (MO
.getType() == MachineOperand::MO_Register
) {
58 assert(TargetRegisterInfo::isPhysicalRegister(MO
.getReg()) &&
60 //XXX Bug Workaround: See note in Printer::doInitialization about %.
61 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).AsmName
;
67 void printS8ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
68 int val
=(unsigned int)MI
->getOperand(OpNo
).getImm();
69 if(val
>=128) val
=val
-256; // if negative, flip sign
72 void printS14ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
73 int val
=(unsigned int)MI
->getOperand(OpNo
).getImm();
74 if(val
>=8192) val
=val
-16384; // if negative, flip sign
77 void printS22ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
78 int val
=(unsigned int)MI
->getOperand(OpNo
).getImm();
79 if(val
>=2097152) val
=val
-4194304; // if negative, flip sign
82 void printU64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
83 O
<< (uint64_t)MI
->getOperand(OpNo
).getImm();
85 void printS64ImmOperand(const MachineInstr
*MI
, unsigned OpNo
) {
86 // XXX : nasty hack to avoid GPREL22 "relocation truncated to fit" linker
87 // errors - instead of add rX = @gprel(CPI<whatever>), r1;; we now
88 // emit movl rX = @gprel(CPI<whatever);;
90 // this gives us 64 bits instead of 22 (for the add long imm) to play
91 // with, which shuts up the linker. The problem is that the constant
92 // pool entries aren't immediates at this stage, so we check here.
93 // If it's an immediate, print it the old fashioned way. If it's
94 // not, we print it as a constant pool index.
95 if (MI
->getOperand(OpNo
).isImm()) {
96 O
<< (int64_t)MI
->getOperand(OpNo
).getImm();
97 } else { // this is a constant pool reference: FIXME: assert this
98 printOp(MI
->getOperand(OpNo
));
102 void printGlobalOperand(const MachineInstr
*MI
, unsigned OpNo
) {
103 printOp(MI
->getOperand(OpNo
), false); // this is NOT a br.call instruction
106 void printCallOperand(const MachineInstr
*MI
, unsigned OpNo
) {
107 printOp(MI
->getOperand(OpNo
), true); // this is a br.call instruction
110 void printMachineInstruction(const MachineInstr
*MI
);
111 void printOp(const MachineOperand
&MO
, bool isBRCALLinsn
= false);
112 void printModuleLevelGV(const GlobalVariable
* GVar
);
113 bool runOnMachineFunction(MachineFunction
&F
);
114 bool doInitialization(Module
&M
);
115 bool doFinalization(Module
&M
);
117 } // end of anonymous namespace
120 // Include the auto-generated portion of the assembly writer.
121 #include "IA64GenAsmWriter.inc"
123 /// runOnMachineFunction - This uses the printMachineInstruction()
124 /// method to print assembly for each instruction.
126 bool IA64AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
129 SetupMachineFunction(MF
);
132 // Print out constants referenced by the function
133 EmitConstantPool(MF
.getConstantPool());
135 const Function
*F
= MF
.getFunction();
136 SwitchToSection(TAI
->SectionForGlobal(F
));
138 // Print out labels for the function.
140 O
<< "\t.global\t" << CurrentFnName
<< '\n';
142 printVisibility(CurrentFnName
, F
->getVisibility());
144 O
<< "\t.type\t" << CurrentFnName
<< ", @function\n";
145 O
<< CurrentFnName
<< ":\n";
147 // Print out code for the function.
148 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
150 // Print a label for the basic block if there are any predecessors.
151 if (!I
->pred_empty()) {
152 printBasicBlockLabel(I
, true, true);
155 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
157 // 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 TargetRegisterInfo
&RI
= *TM
.getRegisterInfo();
169 switch (MO
.getType()) {
170 case MachineOperand::MO_Register
:
171 O
<< RI
.get(MO
.getReg()).AsmName
;
174 case MachineOperand::MO_Immediate
:
177 case MachineOperand::MO_MachineBasicBlock
:
178 printBasicBlockLabel(MO
.getMBB());
180 case MachineOperand::MO_ConstantPoolIndex
: {
181 O
<< "@gprel(" << TAI
->getPrivateGlobalPrefix()
182 << "CPI" << getFunctionNumber() << "_" << MO
.getIndex() << ")";
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->isDeclaration()*/)
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
->isDeclaration())
203 ExternalFunctionNames
.insert(Mang
->getValueName(MO
.getGlobal()));
205 if (GV
->isDeclaration()) // 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 bool Result
= 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 void IA64AsmPrinter::printModuleLevelGV(const GlobalVariable
* GVar
) {
260 const TargetData
*TD
= TM
.getTargetData();
262 if (!GVar
->hasInitializer())
263 return; // External global require no code
265 // Check to see if this is a special global used by LLVM, if so, emit it.
266 if (EmitSpecialLLVMGlobal(GVar
))
270 std::string name
= Mang
->getValueName(GVar
);
271 Constant
*C
= GVar
->getInitializer();
272 unsigned Size
= TD
->getTypeAllocSize(C
->getType());
273 unsigned Align
= TD
->getPreferredAlignmentLog(GVar
);
275 printVisibility(name
, GVar
->getVisibility());
277 SwitchToSection(TAI
->SectionForGlobal(GVar
));
279 if (C
->isNullValue() && !GVar
->hasSection()) {
280 if (!GVar
->isThreadLocal() &&
281 (GVar
->hasLocalLinkage() || GVar
->isWeakForLinker())) {
282 if (Size
== 0) Size
= 1; // .comm Foo, 0 is undefined, avoid it.
284 if (GVar
->hasLocalLinkage()) {
285 O
<< "\t.lcomm " << name
<< "#," << Size
286 << ',' << (1 << Align
);
289 O
<< "\t.common " << name
<< "#," << Size
290 << ',' << (1 << Align
);
298 switch (GVar
->getLinkage()) {
299 case GlobalValue::LinkOnceAnyLinkage
:
300 case GlobalValue::LinkOnceODRLinkage
:
301 case GlobalValue::CommonLinkage
:
302 case GlobalValue::WeakAnyLinkage
:
303 case GlobalValue::WeakODRLinkage
:
304 // Nonnull linkonce -> weak
305 O
<< "\t.weak " << name
<< '\n';
307 case GlobalValue::AppendingLinkage
:
308 // FIXME: appending linkage variables should go into a section of
309 // their name or something. For now, just emit them as external.
310 case GlobalValue::ExternalLinkage
:
311 // If external or appending, declare as a global symbol
312 O
<< TAI
->getGlobalDirective() << name
<< '\n';
314 case GlobalValue::InternalLinkage
:
315 case GlobalValue::PrivateLinkage
:
317 case GlobalValue::GhostLinkage
:
318 cerr
<< "GhostLinkage cannot appear in IA64AsmPrinter!\n";
320 case GlobalValue::DLLImportLinkage
:
321 cerr
<< "DLLImport linkage is not supported by this target!\n";
323 case GlobalValue::DLLExportLinkage
:
324 cerr
<< "DLLExport linkage is not supported by this target!\n";
327 assert(0 && "Unknown linkage type!");
330 EmitAlignment(Align
, GVar
);
332 if (TAI
->hasDotTypeDotSizeDirective()) {
333 O
<< "\t.type " << name
<< ",@object\n";
334 O
<< "\t.size " << name
<< ',' << Size
<< '\n';
338 EmitGlobalConstant(C
);
342 bool IA64AsmPrinter::doFinalization(Module
&M
) {
343 // Print out module-level global variables here.
344 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
346 printModuleLevelGV(I
);
348 // we print out ".global X \n .type X, @function" for each external function
349 O
<< "\n\n// br.call targets referenced (and not defined) above: \n";
350 for (std::set
<std::string
>::iterator i
= ExternalFunctionNames
.begin(),
351 e
= ExternalFunctionNames
.end(); i
!=e
; ++i
) {
352 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @function\n";
356 // we print out ".global X \n .type X, @object" for each external object
357 O
<< "\n\n// (external) symbols referenced (and not defined) above: \n";
358 for (std::set
<std::string
>::iterator i
= ExternalObjectNames
.begin(),
359 e
= ExternalObjectNames
.end(); i
!=e
; ++i
) {
360 O
<< "\t.global " << *i
<< "\n\t.type " << *i
<< ", @object\n";
364 return AsmPrinter::doFinalization(M
);
367 /// createIA64CodePrinterPass - Returns a pass that prints the IA64
368 /// assembly code for a MachineFunction to the given output stream, using
369 /// the given target machine description.
371 FunctionPass
*llvm::createIA64CodePrinterPass(raw_ostream
&o
,
372 IA64TargetMachine
&tm
,
373 CodeGenOpt::Level OptLevel
,
375 return new IA64AsmPrinter(o
, tm
, tm
.getTargetAsmInfo(), OptLevel
, verbose
);