add a version of the APFloat constructor that initializes to 0.0
[llvm/avr.git] / lib / Target / XCore / AsmPrinter / XCoreAsmPrinter.cpp
blobfe45ec3524e85a7d57d942369d910aa4f7b2804e
1 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
16 #include "XCore.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreMCAsmInfo.h"
20 #include "XCoreTargetMachine.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/Mangler.h"
41 #include "llvm/Support/MathExtras.h"
42 #include <algorithm>
43 #include <cctype>
44 using namespace llvm;
46 STATISTIC(EmittedInsts, "Number of machine instrs printed");
48 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
49 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
50 cl::Hidden,
51 cl::value_desc("number"),
52 cl::init(8));
54 namespace {
55 class VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter {
56 DwarfWriter *DW;
57 const XCoreSubtarget &Subtarget;
58 public:
59 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
60 const MCAsmInfo *T, bool V)
61 : AsmPrinter(O, TM, T, V), DW(0),
62 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
64 virtual const char *getPassName() const {
65 return "XCore Assembly Printer";
68 void printMemOperand(const MachineInstr *MI, int opNum);
69 void printOperand(const MachineInstr *MI, int opNum);
70 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
71 unsigned AsmVariant, const char *ExtraCode);
73 void emitGlobalDirective(const std::string &name);
74 void emitExternDirective(const std::string &name);
76 void emitArrayBound(const std::string &name, const GlobalVariable *GV);
77 virtual void PrintGlobalVariable(const GlobalVariable *GV);
79 void emitFunctionStart(MachineFunction &MF);
80 void emitFunctionEnd(MachineFunction &MF);
82 void printInstruction(const MachineInstr *MI); // autogenerated.
83 static const char *getRegisterName(unsigned RegNo);
85 void printMachineInstruction(const MachineInstr *MI);
86 bool runOnMachineFunction(MachineFunction &F);
87 bool doInitialization(Module &M);
89 void getAnalysisUsage(AnalysisUsage &AU) const {
90 AsmPrinter::getAnalysisUsage(AU);
91 AU.setPreservesAll();
92 AU.addRequired<MachineModuleInfo>();
93 AU.addRequired<DwarfWriter>();
96 } // end of anonymous namespace
98 #include "XCoreGenAsmWriter.inc"
100 void XCoreAsmPrinter::
101 emitGlobalDirective(const std::string &name)
103 O << MAI->getGlobalDirective() << name;
104 O << "\n";
107 void XCoreAsmPrinter::
108 emitExternDirective(const std::string &name)
110 O << "\t.extern\t" << name;
111 O << '\n';
114 void XCoreAsmPrinter::
115 emitArrayBound(const std::string &name, const GlobalVariable *GV)
117 assert(((GV->hasExternalLinkage() ||
118 GV->hasWeakLinkage()) ||
119 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
120 if (const ArrayType *ATy = dyn_cast<ArrayType>(
121 cast<PointerType>(GV->getType())->getElementType()))
123 O << MAI->getGlobalDirective() << name << ".globound" << "\n";
124 O << MAI->getSetDirective() << name << ".globound" << ","
125 << ATy->getNumElements() << "\n";
126 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
127 // TODO Use COMDAT groups for LinkOnceLinkage
128 O << MAI->getWeakDefDirective() << name << ".globound" << "\n";
133 void XCoreAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
134 // Check to see if this is a special global used by LLVM, if so, emit it.
135 if (!GV->hasInitializer() ||
136 EmitSpecialLLVMGlobal(GV))
137 return;
139 const TargetData *TD = TM.getTargetData();
141 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
143 std::string name = Mang->getMangledName(GV);
144 Constant *C = GV->getInitializer();
145 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
147 // Mark the start of the global
148 O << "\t.cc_top " << name << ".data," << name << "\n";
150 switch (GV->getLinkage()) {
151 case GlobalValue::AppendingLinkage:
152 llvm_report_error("AppendingLinkage is not supported by this target!");
153 case GlobalValue::LinkOnceAnyLinkage:
154 case GlobalValue::LinkOnceODRLinkage:
155 case GlobalValue::WeakAnyLinkage:
156 case GlobalValue::WeakODRLinkage:
157 case GlobalValue::ExternalLinkage:
158 emitArrayBound(name, GV);
159 emitGlobalDirective(name);
160 // TODO Use COMDAT groups for LinkOnceLinkage
161 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
162 O << MAI->getWeakDefDirective() << name << "\n";
164 // FALL THROUGH
165 case GlobalValue::InternalLinkage:
166 case GlobalValue::PrivateLinkage:
167 case GlobalValue::LinkerPrivateLinkage:
168 break;
169 case GlobalValue::GhostLinkage:
170 llvm_unreachable("Should not have any unmaterialized functions!");
171 case GlobalValue::DLLImportLinkage:
172 llvm_unreachable("DLLImport linkage is not supported by this target!");
173 case GlobalValue::DLLExportLinkage:
174 llvm_unreachable("DLLExport linkage is not supported by this target!");
175 default:
176 llvm_unreachable("Unknown linkage type!");
179 EmitAlignment(Align, GV, 2);
181 unsigned Size = TD->getTypeAllocSize(C->getType());
182 if (GV->isThreadLocal()) {
183 Size *= MaxThreads;
185 if (MAI->hasDotTypeDotSizeDirective()) {
186 O << "\t.type " << name << ",@object\n";
187 O << "\t.size " << name << "," << Size << "\n";
189 O << name << ":\n";
191 EmitGlobalConstant(C);
192 if (GV->isThreadLocal()) {
193 for (unsigned i = 1; i < MaxThreads; ++i) {
194 EmitGlobalConstant(C);
197 if (Size < 4) {
198 // The ABI requires that unsigned scalar types smaller than 32 bits
199 // are are padded to 32 bits.
200 EmitZeros(4 - Size);
203 // Mark the end of the global
204 O << "\t.cc_bottom " << name << ".data\n";
207 /// Emit the directives on the start of functions
208 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
209 // Print out the label for the function.
210 const Function *F = MF.getFunction();
212 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
214 // Mark the start of the function
215 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
217 switch (F->getLinkage()) {
218 default: llvm_unreachable("Unknown linkage type!");
219 case Function::InternalLinkage: // Symbols default to internal.
220 case Function::PrivateLinkage:
221 case Function::LinkerPrivateLinkage:
222 break;
223 case Function::ExternalLinkage:
224 emitGlobalDirective(CurrentFnName);
225 break;
226 case Function::LinkOnceAnyLinkage:
227 case Function::LinkOnceODRLinkage:
228 case Function::WeakAnyLinkage:
229 case Function::WeakODRLinkage:
230 // TODO Use COMDAT groups for LinkOnceLinkage
231 O << MAI->getGlobalDirective() << CurrentFnName << "\n";
232 O << MAI->getWeakDefDirective() << CurrentFnName << "\n";
233 break;
235 // (1 << 1) byte aligned
236 EmitAlignment(MF.getAlignment(), F, 1);
237 if (MAI->hasDotTypeDotSizeDirective()) {
238 O << "\t.type " << CurrentFnName << ",@function\n";
240 O << CurrentFnName << ":\n";
243 /// Emit the directives on the end of functions
244 void XCoreAsmPrinter::
245 emitFunctionEnd(MachineFunction &MF)
247 // Mark the end of the function
248 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
251 /// runOnMachineFunction - This uses the printMachineInstruction()
252 /// method to print assembly for each instruction.
254 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
256 this->MF = &MF;
258 SetupMachineFunction(MF);
260 // Print out constants referenced by the function
261 EmitConstantPool(MF.getConstantPool());
263 // Print out jump tables referenced by the function
264 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
266 // Emit the function start directives
267 emitFunctionStart(MF);
269 // Emit pre-function debug information.
270 DW->BeginFunction(&MF);
272 // Print out code for the function.
273 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
274 I != E; ++I) {
276 // Print a label for the basic block.
277 if (I != MF.begin()) {
278 EmitBasicBlockStart(I);
279 O << '\n';
282 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
283 II != E; ++II) {
284 // Print the assembly for the instruction.
285 printMachineInstruction(II);
288 // Each Basic Block is separated by a newline
289 O << '\n';
292 // Emit function end directives
293 emitFunctionEnd(MF);
295 // Emit post-function debug information.
296 DW->EndFunction(&MF);
298 // We didn't modify anything.
299 return false;
302 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
304 printOperand(MI, opNum);
306 if (MI->getOperand(opNum+1).isImm()
307 && MI->getOperand(opNum+1).getImm() == 0)
308 return;
310 O << "+";
311 printOperand(MI, opNum+1);
314 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
315 const MachineOperand &MO = MI->getOperand(opNum);
316 switch (MO.getType()) {
317 case MachineOperand::MO_Register:
318 O << getRegisterName(MO.getReg());
319 break;
320 case MachineOperand::MO_Immediate:
321 O << MO.getImm();
322 break;
323 case MachineOperand::MO_MachineBasicBlock:
324 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
325 break;
326 case MachineOperand::MO_GlobalAddress:
327 O << Mang->getMangledName(MO.getGlobal());
328 break;
329 case MachineOperand::MO_ExternalSymbol:
330 O << MO.getSymbolName();
331 break;
332 case MachineOperand::MO_ConstantPoolIndex:
333 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
334 << '_' << MO.getIndex();
335 break;
336 case MachineOperand::MO_JumpTableIndex:
337 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
338 << '_' << MO.getIndex();
339 break;
340 default:
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,
348 unsigned AsmVariant,
349 const char *ExtraCode) {
350 printOperand(MI, OpNo);
351 return false;
354 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
355 ++EmittedInsts;
357 processDebugLoc(MI->getDebugLoc());
359 // Check for mov mnemonic
360 unsigned src, dst, srcSR, dstSR;
361 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
362 O << "\tmov " << getRegisterName(dst) << ", ";
363 O << getRegisterName(src) << '\n';
364 return;
366 printInstruction(MI);
367 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
368 EmitComments(*MI);
369 O << '\n';
372 bool XCoreAsmPrinter::doInitialization(Module &M) {
373 bool Result = AsmPrinter::doInitialization(M);
374 DW = getAnalysisIfAvailable<DwarfWriter>();
376 return Result;
379 // Force static initialization.
380 extern "C" void LLVMInitializeXCoreAsmPrinter() {
381 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);