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/Support/Mangler.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/raw_ostream.h"
41 STATISTIC(EmittedInsts
, "Number of machine instrs printed");
43 static cl::opt
<std::string
> FileDirective("xcore-file-directive", cl::Optional
,
44 cl::desc("Output a file directive into the assembly file"),
46 cl::value_desc("filename"),
49 static cl::opt
<unsigned> MaxThreads("xcore-max-threads", cl::Optional
,
50 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
52 cl::value_desc("number"),
56 class VISIBILITY_HIDDEN XCoreAsmPrinter
: public AsmPrinter
{
58 const XCoreSubtarget
&Subtarget
;
60 XCoreAsmPrinter(raw_ostream
&O
, XCoreTargetMachine
&TM
,
61 const TargetAsmInfo
*T
, bool F
, bool V
)
62 : AsmPrinter(O
, TM
, T
, F
, V
), DW(0),
63 Subtarget(*TM
.getSubtargetImpl()) {}
65 virtual const char *getPassName() const {
66 return "XCore Assembly Printer";
69 void printMemOperand(const MachineInstr
*MI
, int opNum
);
70 void printOperand(const MachineInstr
*MI
, int opNum
);
71 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
72 unsigned AsmVariant
, const char *ExtraCode
);
74 void emitFileDirective(const std::string
&filename
);
75 void emitGlobalDirective(const std::string
&name
);
76 void emitExternDirective(const std::string
&name
);
78 void emitArrayBound(const std::string
&name
, const GlobalVariable
*GV
);
79 void emitGlobal(const GlobalVariable
*GV
);
81 void emitFunctionStart(MachineFunction
&MF
);
82 void emitFunctionEnd(MachineFunction
&MF
);
84 bool printInstruction(const MachineInstr
*MI
); // autogenerated.
85 void printMachineInstruction(const MachineInstr
*MI
);
86 bool runOnMachineFunction(MachineFunction
&F
);
87 bool doInitialization(Module
&M
);
88 bool doFinalization(Module
&M
);
90 void getAnalysisUsage(AnalysisUsage
&AU
) const {
91 AsmPrinter::getAnalysisUsage(AU
);
93 AU
.addRequired
<MachineModuleInfo
>();
94 AU
.addRequired
<DwarfWriter
>();
97 } // end of anonymous namespace
99 #include "XCoreGenAsmWriter.inc"
101 /// createXCoreCodePrinterPass - Returns a pass that prints the XCore
102 /// assembly code for a MachineFunction to the given output stream,
103 /// using the given target machine description. This should work
104 /// regardless of whether the function is in SSA form.
106 FunctionPass
*llvm::createXCoreCodePrinterPass(raw_ostream
&o
,
107 XCoreTargetMachine
&tm
,
108 bool fast
, bool verbose
) {
109 return new XCoreAsmPrinter(o
, tm
, tm
.getTargetAsmInfo(), fast
, verbose
);
112 // PrintEscapedString - Print each character of the specified string, escaping
113 // it if it is not printable or if it is an escape char.
114 static void PrintEscapedString(const std::string
&Str
, raw_ostream
&Out
) {
115 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
116 unsigned char C
= Str
[i
];
117 if (isprint(C
) && C
!= '"' && C
!= '\\') {
121 << (char) ((C
/16 < 10) ? ( C
/16 +'0') : ( C
/16 -10+'A'))
122 << (char)(((C
&15) < 10) ? ((C
&15)+'0') : ((C
&15)-10+'A'));
127 void XCoreAsmPrinter::
128 emitFileDirective(const std::string
&name
)
131 PrintEscapedString(name
, O
);
135 void XCoreAsmPrinter::
136 emitGlobalDirective(const std::string
&name
)
138 O
<< TAI
->getGlobalDirective() << name
;
142 void XCoreAsmPrinter::
143 emitExternDirective(const std::string
&name
)
145 O
<< "\t.extern\t" << name
;
149 void XCoreAsmPrinter::
150 emitArrayBound(const std::string
&name
, const GlobalVariable
*GV
)
152 assert(((GV
->hasExternalLinkage() ||
153 GV
->hasWeakLinkage()) ||
154 GV
->hasLinkOnceLinkage()) && "Unexpected linkage");
155 if (const ArrayType
*ATy
= dyn_cast
<ArrayType
>(
156 cast
<PointerType
>(GV
->getType())->getElementType()))
158 O
<< TAI
->getGlobalDirective() << name
<< ".globound" << "\n";
159 O
<< TAI
->getSetDirective() << name
<< ".globound" << ","
160 << ATy
->getNumElements() << "\n";
161 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage()) {
162 // TODO Use COMDAT groups for LinkOnceLinkage
163 O
<< TAI
->getWeakDefDirective() << name
<< ".globound" << "\n";
168 void XCoreAsmPrinter::
169 emitGlobal(const GlobalVariable
*GV
)
171 const TargetData
*TD
= TM
.getTargetData();
173 if (GV
->hasInitializer()) {
174 // Check to see if this is a special global used by LLVM, if so, emit it.
175 if (EmitSpecialLLVMGlobal(GV
))
178 SwitchToSection(TAI
->SectionForGlobal(GV
));
180 std::string name
= Mang
->getValueName(GV
);
181 Constant
*C
= GV
->getInitializer();
182 unsigned Align
= (unsigned)TD
->getPreferredTypeAlignmentShift(C
->getType());
184 // Mark the start of the global
185 O
<< "\t.cc_top " << name
<< ".data," << name
<< "\n";
187 switch (GV
->getLinkage()) {
188 case GlobalValue::AppendingLinkage
:
189 cerr
<< "AppendingLinkage is not supported by this target!\n";
191 case GlobalValue::LinkOnceAnyLinkage
:
192 case GlobalValue::LinkOnceODRLinkage
:
193 case GlobalValue::WeakAnyLinkage
:
194 case GlobalValue::WeakODRLinkage
:
195 case GlobalValue::ExternalLinkage
:
196 emitArrayBound(name
, GV
);
197 emitGlobalDirective(name
);
198 // TODO Use COMDAT groups for LinkOnceLinkage
199 if (GV
->hasWeakLinkage() || GV
->hasLinkOnceLinkage()) {
200 O
<< TAI
->getWeakDefDirective() << name
<< "\n";
203 case GlobalValue::InternalLinkage
:
204 case GlobalValue::PrivateLinkage
:
206 case GlobalValue::GhostLinkage
:
207 cerr
<< "Should not have any unmaterialized functions!\n";
209 case GlobalValue::DLLImportLinkage
:
210 cerr
<< "DLLImport linkage is not supported by this target!\n";
212 case GlobalValue::DLLExportLinkage
:
213 cerr
<< "DLLExport linkage is not supported by this target!\n";
216 assert(0 && "Unknown linkage type!");
219 EmitAlignment(Align
, GV
, 2);
221 unsigned Size
= TD
->getTypePaddedSize(C
->getType());
222 if (GV
->isThreadLocal()) {
225 if (TAI
->hasDotTypeDotSizeDirective()) {
226 O
<< "\t.type " << name
<< ",@object\n";
227 O
<< "\t.size " << name
<< "," << Size
<< "\n";
231 EmitGlobalConstant(C
);
232 if (GV
->isThreadLocal()) {
233 for (unsigned i
= 1; i
< MaxThreads
; ++i
) {
234 EmitGlobalConstant(C
);
238 // The ABI requires that unsigned scalar types smaller than 32 bits
239 // are are padded to 32 bits.
243 // Mark the end of the global
244 O
<< "\t.cc_bottom " << name
<< ".data\n";
246 if (GV
->hasExternalWeakLinkage())
247 ExtWeakSymbols
.insert(GV
);
251 /// Emit the directives on the start of functions
252 void XCoreAsmPrinter::
253 emitFunctionStart(MachineFunction
&MF
)
255 // Print out the label for the function.
256 const Function
*F
= MF
.getFunction();
258 SwitchToSection(TAI
->SectionForGlobal(F
));
260 // Mark the start of the function
261 O
<< "\t.cc_top " << CurrentFnName
<< ".function," << CurrentFnName
<< "\n";
263 switch (F
->getLinkage()) {
264 default: assert(0 && "Unknown linkage type!");
265 case Function::InternalLinkage
: // Symbols default to internal.
266 case Function::PrivateLinkage
:
268 case Function::ExternalLinkage
:
269 emitGlobalDirective(CurrentFnName
);
271 case Function::LinkOnceAnyLinkage
:
272 case Function::LinkOnceODRLinkage
:
273 case Function::WeakAnyLinkage
:
274 case Function::WeakODRLinkage
:
275 // TODO Use COMDAT groups for LinkOnceLinkage
276 O
<< TAI
->getGlobalDirective() << CurrentFnName
<< "\n";
277 O
<< TAI
->getWeakDefDirective() << CurrentFnName
<< "\n";
280 // (1 << 1) byte aligned
281 EmitAlignment(1, F
, 1);
282 if (TAI
->hasDotTypeDotSizeDirective()) {
283 O
<< "\t.type " << CurrentFnName
<< ",@function\n";
285 O
<< CurrentFnName
<< ":\n";
288 /// Emit the directives on the end of functions
289 void XCoreAsmPrinter::
290 emitFunctionEnd(MachineFunction
&MF
)
292 // Mark the end of the function
293 O
<< "\t.cc_bottom " << CurrentFnName
<< ".function\n";
296 /// runOnMachineFunction - This uses the printMachineInstruction()
297 /// method to print assembly for each instruction.
299 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction
&MF
)
303 SetupMachineFunction(MF
);
305 // Print out constants referenced by the function
306 EmitConstantPool(MF
.getConstantPool());
308 // Print out jump tables referenced by the function
309 EmitJumpTableInfo(MF
.getJumpTableInfo(), MF
);
311 // Emit the function start directives
312 emitFunctionStart(MF
);
314 // Emit pre-function debug information.
315 DW
->BeginFunction(&MF
);
317 // Print out code for the function.
318 for (MachineFunction::const_iterator I
= MF
.begin(), E
= MF
.end();
321 // Print a label for the basic block.
322 if (I
!= MF
.begin()) {
323 printBasicBlockLabel(I
, true , true);
327 for (MachineBasicBlock::const_iterator II
= I
->begin(), E
= I
->end();
329 // Print the assembly for the instruction.
331 printMachineInstruction(II
);
334 // Each Basic Block is separated by a newline
338 // Emit function end directives
341 // Emit post-function debug information.
342 DW
->EndFunction(&MF
);
344 // We didn't modify anything.
348 void XCoreAsmPrinter::printMemOperand(const MachineInstr
*MI
, int opNum
)
350 printOperand(MI
, opNum
);
352 if (MI
->getOperand(opNum
+1).isImm()
353 && MI
->getOperand(opNum
+1).getImm() == 0)
357 printOperand(MI
, opNum
+1);
360 void XCoreAsmPrinter::printOperand(const MachineInstr
*MI
, int opNum
) {
361 const MachineOperand
&MO
= MI
->getOperand(opNum
);
362 switch (MO
.getType()) {
363 case MachineOperand::MO_Register
:
364 if (TargetRegisterInfo::isPhysicalRegister(MO
.getReg()))
365 O
<< TM
.getRegisterInfo()->get(MO
.getReg()).AsmName
;
367 assert(0 && "not implemented");
369 case MachineOperand::MO_Immediate
:
372 case MachineOperand::MO_MachineBasicBlock
:
373 printBasicBlockLabel(MO
.getMBB());
375 case MachineOperand::MO_GlobalAddress
:
377 const GlobalValue
*GV
= MO
.getGlobal();
378 O
<< Mang
->getValueName(GV
);
379 if (GV
->hasExternalWeakLinkage())
380 ExtWeakSymbols
.insert(GV
);
383 case MachineOperand::MO_ExternalSymbol
:
384 O
<< MO
.getSymbolName();
386 case MachineOperand::MO_ConstantPoolIndex
:
387 O
<< TAI
->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
388 << '_' << MO
.getIndex();
390 case MachineOperand::MO_JumpTableIndex
:
391 O
<< TAI
->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
392 << '_' << MO
.getIndex();
395 assert(0 && "not implemented");
399 /// PrintAsmOperand - Print out an operand for an inline asm expression.
401 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
403 const char *ExtraCode
) {
404 printOperand(MI
, OpNo
);
408 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr
*MI
) {
411 // Check for mov mnemonic
412 unsigned src
, dst
, srcSR
, dstSR
;
413 if (TM
.getInstrInfo()->isMoveInstr(*MI
, src
, dst
, srcSR
, dstSR
)) {
415 O
<< TM
.getRegisterInfo()->get(dst
).AsmName
;
417 O
<< TM
.getRegisterInfo()->get(src
).AsmName
;
421 if (printInstruction(MI
)) {
424 assert(0 && "Unhandled instruction in asm writer!");
427 bool XCoreAsmPrinter::doInitialization(Module
&M
) {
428 bool Result
= AsmPrinter::doInitialization(M
);
430 if (!FileDirective
.empty()) {
431 emitFileDirective(FileDirective
);
434 // Print out type strings for external functions here
435 for (Module::const_iterator I
= M
.begin(), E
= M
.end();
437 if (I
->isDeclaration() && !I
->isIntrinsic()) {
438 switch (I
->getLinkage()) {
440 assert(0 && "Unexpected linkage");
441 case Function::ExternalWeakLinkage
:
442 ExtWeakSymbols
.insert(I
);
444 case Function::ExternalLinkage
:
450 // Emit initial debug information.
451 DW
= getAnalysisIfAvailable
<DwarfWriter
>();
452 assert(DW
&& "Dwarf Writer is not available");
453 DW
->BeginModule(&M
, getAnalysisIfAvailable
<MachineModuleInfo
>(),
458 bool XCoreAsmPrinter::doFinalization(Module
&M
) {
460 // Print out module-level global variables.
461 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
466 // Emit final debug information.
469 return AsmPrinter::doFinalization(M
);