1 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
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 the XAS-format XCore assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreTargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetRegistry.h"
33 #include "llvm/Support/Mangler.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/StringExtras.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/MathExtras.h"
44 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
46 static cl::opt
<unsigned> MaxThreads("xcore-max-threads", cl::Optional
,
47 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
49 cl::value_desc("number"),
53 class VISIBILITY_HIDDEN XCoreAsmPrinter
: public AsmPrinter
{
55 const XCoreSubtarget
&Subtarget
;
57 explicit XCoreAsmPrinter(formatted_raw_ostream
&O
, TargetMachine
&TM
,
58 const TargetAsmInfo
*T
, bool V
)
59 : AsmPrinter(O
, TM
, T
, V
), DW(0),
60 Subtarget(TM
.getSubtarget
<XCoreSubtarget
>()) {}
62 virtual const char *getPassName() const {
63 return "XCore Assembly Printer";
66 void printMemOperand(const MachineInstr
*MI
, int opNum
);
67 void printOperand(const MachineInstr
*MI
, int opNum
);
68 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
69 unsigned AsmVariant
, const char *ExtraCode
);
71 void emitGlobalDirective(const std::string
&name
);
72 void emitExternDirective(const std::string
&name
);
74 void emitArrayBound(const std::string
&name
, const GlobalVariable
*GV
);
75 virtual void PrintGlobalVariable(const GlobalVariable
*GV
);
77 void emitFunctionStart(MachineFunction
&MF
);
78 void emitFunctionEnd(MachineFunction
&MF
);
80 bool printInstruction(const MachineInstr
*MI
); // autogenerated.
81 void printMachineInstruction(const MachineInstr
*MI
);
82 bool runOnMachineFunction(MachineFunction
&F
);
83 bool doInitialization(Module
&M
);
85 void getAnalysisUsage(AnalysisUsage
&AU
) const {
86 AsmPrinter::getAnalysisUsage(AU
);
88 AU
.addRequired
<MachineModuleInfo
>();
89 AU
.addRequired
<DwarfWriter
>();
92 } // end of anonymous namespace
94 #include "XCoreGenAsmWriter.inc"
96 void XCoreAsmPrinter::
97 emitGlobalDirective(const std::string
&name
)
99 O
<< TAI
->getGlobalDirective() << name
;
103 void XCoreAsmPrinter::
104 emitExternDirective(const std::string
&name
)
106 O
<< "\t.extern\t" << name
;
110 void XCoreAsmPrinter::
111 emitArrayBound(const std::string
&name
, const GlobalVariable
*GV
)
113 assert(((GV
->hasExternalLinkage() ||
114 GV
->hasWeakLinkage()) ||
115 GV
->hasLinkOnceLinkage()) && "Unexpected linkage");
116 if (const ArrayType
*ATy
= dyn_cast
<ArrayType
>(
117 cast
<PointerType
>(GV
->getType())->getElementType()))
119 O
<< TAI
->getGlobalDirective() << name
<< ".globound" << "\n";
120 O
<< TAI
->getSetDirective() << name
<< ".globound" << ","
121 << ATy
->getNumElements() << "\n";
122 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage()) {
123 // TODO Use COMDAT groups for LinkOnceLinkage
124 O
<< TAI
->getWeakDefDirective() << name
<< ".globound" << "\n";
129 void XCoreAsmPrinter::PrintGlobalVariable(const GlobalVariable
*GV
) {
130 // Check to see if this is a special global used by LLVM, if so, emit it.
131 if (!GV
->hasInitializer() ||
132 EmitSpecialLLVMGlobal(GV
))
135 const TargetData
*TD
= TM
.getTargetData();
137 SwitchToSection(getObjFileLowering().SectionForGlobal(GV
, Mang
, TM
));
139 std::string name
= Mang
->getMangledName(GV
);
140 Constant
*C
= GV
->getInitializer();
141 unsigned Align
= (unsigned)TD
->getPreferredTypeAlignmentShift(C
->getType());
143 // Mark the start of the global
144 O
<< "\t.cc_top " << name
<< ".data," << name
<< "\n";
146 switch (GV
->getLinkage()) {
147 case GlobalValue::AppendingLinkage
:
148 llvm_report_error("AppendingLinkage is not supported by this target!");
149 case GlobalValue::LinkOnceAnyLinkage
:
150 case GlobalValue::LinkOnceODRLinkage
:
151 case GlobalValue::WeakAnyLinkage
:
152 case GlobalValue::WeakODRLinkage
:
153 case GlobalValue::ExternalLinkage
:
154 emitArrayBound(name
, GV
);
155 emitGlobalDirective(name
);
156 // TODO Use COMDAT groups for LinkOnceLinkage
157 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage()) {
158 O
<< TAI
->getWeakDefDirective() << name
<< "\n";
161 case GlobalValue::InternalLinkage
:
162 case GlobalValue::PrivateLinkage
:
163 case GlobalValue::LinkerPrivateLinkage
:
165 case GlobalValue::GhostLinkage
:
166 llvm_unreachable("Should not have any unmaterialized functions!");
167 case GlobalValue::DLLImportLinkage
:
168 llvm_unreachable("DLLImport linkage is not supported by this target!");
169 case GlobalValue::DLLExportLinkage
:
170 llvm_unreachable("DLLExport linkage is not supported by this target!");
172 llvm_unreachable("Unknown linkage type!");
175 EmitAlignment(Align
, GV
, 2);
177 unsigned Size
= TD
->getTypeAllocSize(C
->getType());
178 if (GV
->isThreadLocal()) {
181 if (TAI
->hasDotTypeDotSizeDirective()) {
182 O
<< "\t.type " << name
<< ",@object\n";
183 O
<< "\t.size " << name
<< "," << Size
<< "\n";
187 EmitGlobalConstant(C
);
188 if (GV
->isThreadLocal()) {
189 for (unsigned i
= 1; i
< MaxThreads
; ++i
) {
190 EmitGlobalConstant(C
);
194 // The ABI requires that unsigned scalar types smaller than 32 bits
195 // are are padded to 32 bits.
199 // Mark the end of the global
200 O
<< "\t.cc_bottom " << name
<< ".data\n";
203 /// Emit the directives on the start of functions
204 void XCoreAsmPrinter::emitFunctionStart(MachineFunction
&MF
) {
205 // Print out the label for the function.
206 const Function
*F
= MF
.getFunction();
208 SwitchToSection(getObjFileLowering().SectionForGlobal(F
, Mang
, TM
));
210 // Mark the start of the function
211 O
<< "\t.cc_top " << CurrentFnName
<< ".function," << CurrentFnName
<< "\n";
213 switch (F
->getLinkage()) {
214 default: llvm_unreachable("Unknown linkage type!");
215 case Function::InternalLinkage
: // Symbols default to internal.
216 case Function::PrivateLinkage
:
217 case Function::LinkerPrivateLinkage
:
219 case Function::ExternalLinkage
:
220 emitGlobalDirective(CurrentFnName
);
222 case Function::LinkOnceAnyLinkage
:
223 case Function::LinkOnceODRLinkage
:
224 case Function::WeakAnyLinkage
:
225 case Function::WeakODRLinkage
:
226 // TODO Use COMDAT groups for LinkOnceLinkage
227 O
<< TAI
->getGlobalDirective() << CurrentFnName
<< "\n";
228 O
<< TAI
->getWeakDefDirective() << CurrentFnName
<< "\n";
231 // (1 << 1) byte aligned
232 EmitAlignment(MF
.getAlignment(), F
, 1);
233 if (TAI
->hasDotTypeDotSizeDirective()) {
234 O
<< "\t.type " << CurrentFnName
<< ",@function\n";
236 O
<< CurrentFnName
<< ":\n";
239 /// Emit the directives on the end of functions
240 void XCoreAsmPrinter::
241 emitFunctionEnd(MachineFunction
&MF
)
243 // Mark the end of the function
244 O
<< "\t.cc_bottom " << CurrentFnName
<< ".function\n";
247 /// runOnMachineFunction - This uses the printMachineInstruction()
248 /// method to print assembly for each instruction.
250 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction
&MF
)
254 SetupMachineFunction(MF
);
256 // Print out constants referenced by the function
257 EmitConstantPool(MF
.getConstantPool());
259 // Print out jump tables referenced by the function
260 EmitJumpTableInfo(MF
.getJumpTableInfo(), MF
);
262 // Emit the function start directives
263 emitFunctionStart(MF
);
265 // Emit pre-function debug information.
266 DW
->BeginFunction(&MF
);
268 // Print out code for the function.
269 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
272 // Print a label for the basic block.
273 if (I
!= MF
.begin()) {
274 printBasicBlockLabel(I
, true , true);
278 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
280 // Print the assembly for the instruction.
282 printMachineInstruction(II
);
285 // Each Basic Block is separated by a newline
289 // Emit function end directives
292 // Emit post-function debug information.
293 DW
->EndFunction(&MF
);
295 // We didn't modify anything.
299 void XCoreAsmPrinter::printMemOperand(const MachineInstr
*MI
, int opNum
)
301 printOperand(MI
, opNum
);
303 if (MI
->getOperand(opNum
+1).isImm()
304 && MI
->getOperand(opNum
+1).getImm() == 0)
308 printOperand(MI
, opNum
+1);
311 void XCoreAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
) {
312 const MachineOperand
&MO
= MI
->getOperand(opNum
);
313 switch (MO
.getType()) {
314 case MachineOperand::MO_Register
:
315 if (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()))
316 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).AsmName
;
318 llvm_unreachable("not implemented");
320 case MachineOperand::MO_Immediate
:
323 case MachineOperand::MO_MachineBasicBlock
:
324 printBasicBlockLabel(MO
.getMBB());
326 case MachineOperand::MO_GlobalAddress
:
327 O
<< Mang
->getMangledName(MO
.getGlobal());
329 case MachineOperand::MO_ExternalSymbol
:
330 O
<< MO
.getSymbolName();
332 case MachineOperand::MO_ConstantPoolIndex
:
333 O
<< TAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
334 << '_' << MO
.getIndex();
336 case MachineOperand::MO_JumpTableIndex
:
337 O
<< TAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
338 << '_' << MO
.getIndex();
341 llvm_unreachable("not implemented");
345 /// PrintAsmOperand - Print out an operand for an inline asm expression.
347 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
349 const char *ExtraCode
) {
350 printOperand(MI
, OpNo
);
354 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
357 // Check for mov mnemonic
358 unsigned src
, dst
, srcSR
, dstSR
;
359 if (TM
.getInstrInfo()->isMoveInstr(*MI
, src
, dst
, srcSR
, dstSR
)) {
361 O
<< TM
.getRegisterInfo()->get(dst
).AsmName
;
363 O
<< TM
.getRegisterInfo()->get(src
).AsmName
;
367 if (printInstruction(MI
)) {
370 llvm_unreachable("Unhandled instruction in asm writer!");
373 bool XCoreAsmPrinter::doInitialization(Module
&M
) {
374 bool Result
= AsmPrinter::doInitialization(M
);
375 DW
= getAnalysisIfAvailable
<DwarfWriter
>();
382 // Force static initialization.
383 extern "C" void LLVMInitializeXCoreTarget() {
384 RegisterTargetMachine
<XCoreTargetMachine
> X(TheXCoreTarget
);
385 RegisterAsmPrinter
<XCoreAsmPrinter
> Y(TheXCoreTarget
);