1 //===-- PIC16AsmPrinter.cpp - PIC16 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 PIC16 assembly language.
13 //===----------------------------------------------------------------------===//
15 #include "PIC16ABINames.h"
16 #include "PIC16AsmPrinter.h"
17 #include "PIC16Section.h"
18 #include "PIC16MCAsmInfo.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/DwarfWriter.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Target/Mangler.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FormattedStream.h"
36 #include "PIC16GenAsmWriter.inc"
38 PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
40 : AsmPrinter(O, TM, Streamer), DbgInfo(O, TM.getMCAsmInfo()) {
41 PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
42 PMAI = static_cast<const PIC16MCAsmInfo*>(TM.getMCAsmInfo());
43 PTOF = (PIC16TargetObjectFile *)&PTLI->getObjFileLowering();
46 void PIC16AsmPrinter::EmitInstruction(const MachineInstr *MI) {
47 printInstruction(MI, O);
48 OutStreamer.AddBlankLine();
51 static int getFunctionColor(const Function *F) {
52 if (F->hasSection()) {
53 std::string Sectn = F->getSection();
54 std::string StrToFind = "Overlay=";
55 std::string::size_type Pos = Sectn.find(StrToFind);
57 // Retreive the color number if the key is found.
58 if (Pos != std::string::npos) {
59 Pos += StrToFind.length();
60 std::string Color = "";
61 char c = Sectn.at(Pos);
62 // A Color can only consist of digits.
63 while (c >= '0' && c<= '9') {
66 if (Pos >= Sectn.length())
70 return atoi(Color.c_str());
74 // Color was not set for function, so return -1.
78 // Color the Auto section of the given function.
79 void PIC16AsmPrinter::ColorAutoSection(const Function *F) {
80 std::string SectionName = PAN::getAutosSectionName(CurrentFnSym->getName());
81 PIC16Section* Section = PTOF->findPIC16Section(SectionName);
82 if (Section != NULL) {
83 int Color = getFunctionColor(F);
85 Section->setColor(Color);
90 /// runOnMachineFunction - This emits the frame section, autos section and
91 /// assembly for each instruction. Also takes care of function begin debug
92 /// directive and file begin debug directive (if required) for the function.
94 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
95 // This calls the base class function required to be called at beginning
96 // of runOnMachineFunction.
97 SetupMachineFunction(MF);
99 // Put the color information from function to its auto section.
100 const Function *F = MF.getFunction();
103 // Emit the function frame (args and temps).
104 EmitFunctionFrame(MF);
106 DbgInfo.BeginFunction(MF);
108 // Now emit the instructions of function in its code section.
109 const MCSection *fCodeSection =
110 getObjFileLowering().SectionForCode(CurrentFnSym->getName(),
111 PAN::isISR(F->getSection()));
113 // Start the Code Section.
115 OutStreamer.SwitchSection(fCodeSection);
117 // Emit the frame address of the function at the beginning of code.
118 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
119 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnSym->getName()) << ")\n";
121 // Emit function start label.
122 O << *CurrentFnSym << ":\n";
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
130 // Print a label for the basic block.
131 if (I != MF.begin()) {
132 EmitBasicBlockStart(I);
135 // Print a basic block.
136 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 // Emit the line directive if source line changed.
140 const DebugLoc DL = II->getDebugLoc();
141 if (!DL.isUnknown() && DL != CurDL) {
142 DbgInfo.ChangeDebugLoc(MF, DL);
146 // Print the assembly for the instruction.
151 // Emit function end debug directives.
152 DbgInfo.EndFunction(MF);
154 return false; // we didn't modify anything.
158 // printOperand - print operand of insn.
159 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum,
161 const MachineOperand &MO = MI->getOperand(opNum);
162 const Function *F = MI->getParent()->getParent()->getFunction();
164 switch (MO.getType()) {
165 case MachineOperand::MO_Register:
167 // For indirect load/store insns, the fsr name is printed as INDF.
168 std::string RegName = getRegisterName(MO.getReg());
169 if ((MI->getOpcode() == PIC16::load_indirect) ||
170 (MI->getOpcode() == PIC16::store_indirect))
171 RegName.replace (0, 3, "INDF");
176 case MachineOperand::MO_Immediate:
177 O << (int)MO.getImm();
180 case MachineOperand::MO_GlobalAddress: {
181 MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
182 // FIXME: currently we do not have a memcpy def coming in the module
183 // by any chance, as we do not link in those as .bc lib. So these calls
184 // are always external and it is safe to emit an extern.
185 if (PAN::isMemIntrinsic(Sym->getName()))
186 LibcallDecls.insert(Sym->getName());
191 case MachineOperand::MO_ExternalSymbol: {
192 const char *Sname = MO.getSymbolName();
193 std::string Printname = Sname;
195 // Intrinsic stuff needs to be renamed if we are printing IL fn.
196 if (PAN::isIntrinsicStuff(Printname)) {
197 if (PAN::isISR(F->getSection())) {
198 Printname = PAN::Rename(Sname);
200 // Record these decls, we need to print them in asm as extern.
201 LibcallDecls.insert(Printname);
207 case MachineOperand::MO_MachineBasicBlock:
208 O << *MO.getMBB()->getSymbol();
212 llvm_unreachable(" Operand type not supported.");
216 /// printCCOperand - Print the cond code operand.
218 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
220 int CC = (int)MI->getOperand(opNum).getImm();
221 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
224 /// printLibcallDecls - print the extern declarations for compiler
227 void PIC16AsmPrinter::printLibcallDecls() {
228 // If no libcalls used, return.
229 if (LibcallDecls.empty()) return;
231 O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
233 for (std::set<std::string>::const_iterator I = LibcallDecls.begin(),
234 E = LibcallDecls.end(); I != E; I++) {
235 O << MAI->getExternDirective() << *I << "\n";
237 O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n";
240 /// doInitialization - Perform Module level initializations here.
241 /// One task that we do here is to sectionize all global variables.
242 /// The MemSelOptimizer pass depends on the sectionizing.
244 bool PIC16AsmPrinter::doInitialization(Module &M) {
245 bool Result = AsmPrinter::doInitialization(M);
247 // Every asmbly contains these std headers.
248 O << "\n#include p16f1xxx.inc";
249 O << "\n#include stdmacros.inc";
251 // Set the section names for all globals.
252 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
255 // Record External Var Decls.
256 if (I->isDeclaration()) {
257 ExternalVarDecls.push_back(I);
261 // Record Exteranl Var Defs.
262 if (I->hasExternalLinkage() || I->hasCommonLinkage()) {
263 ExternalVarDefs.push_back(I);
266 // Sectionify actual data.
267 if (!I->hasAvailableExternallyLinkage()) {
268 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
270 I->setSection(((const PIC16Section *)S)->getName());
274 DbgInfo.BeginModule(M);
275 EmitFunctionDecls(M);
276 EmitUndefinedVars(M);
286 /// Emit extern decls for functions imported from other modules, and emit
287 /// global declarations for function defined in this module and which are
288 /// available to other modules.
290 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
291 // Emit declarations for external functions.
292 O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
293 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
294 if (I->isIntrinsic() || I->getName() == "@abort")
297 if (!I->isDeclaration() && !I->hasExternalLinkage())
300 MCSymbol *Sym = Mang->getSymbol(I);
302 // Do not emit memcpy, memset, and memmove here.
303 // Calls to these routines can be generated in two ways,
304 // 1. User calling the standard lib function
305 // 2. Codegen generating these calls for llvm intrinsics.
306 // In the first case a prototype is alread availale, while in
307 // second case the call is via and externalsym and the prototype is missing.
308 // So declarations for these are currently always getting printing by
309 // tracking both kind of references in printInstrunction.
310 if (I->isDeclaration() && PAN::isMemIntrinsic(Sym->getName())) continue;
312 const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
313 MAI->getGlobalDirective();
315 O << directive << Sym->getName() << "\n";
316 O << directive << PAN::getRetvalLabel(Sym->getName()) << "\n";
317 O << directive << PAN::getArgsLabel(Sym->getName()) << "\n";
320 O << MAI->getCommentString() << "Function Declarations - END." <<"\n";
323 // Emit variables imported from other Modules.
324 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
325 std::vector<const GlobalVariable*> Items = ExternalVarDecls;
326 if (!Items.size()) return;
328 O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
329 for (unsigned j = 0; j < Items.size(); j++)
330 O << MAI->getExternDirective() << *Mang->getSymbol(Items[j]) << "\n";
331 O << MAI->getCommentString() << "Imported Variables - END" << "\n";
334 // Emit variables defined in this module and are available to other modules.
335 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
336 std::vector<const GlobalVariable*> Items = ExternalVarDefs;
337 if (!Items.size()) return;
339 O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
340 for (unsigned j = 0; j < Items.size(); j++)
341 O << MAI->getGlobalDirective() << *Mang->getSymbol(Items[j]) << "\n";
342 O << MAI->getCommentString() << "Exported Variables - END" << "\n";
345 // Emit initialized data placed in ROM.
346 void PIC16AsmPrinter::EmitRomData(Module &M) {
347 EmitSingleSection(PTOF->ROMDATASection());
350 // Emit Shared section udata.
351 void PIC16AsmPrinter::EmitSharedUdata(Module &M) {
352 EmitSingleSection(PTOF->SHAREDUDATASection());
355 bool PIC16AsmPrinter::doFinalization(Module &M) {
358 DbgInfo.EndModule(M);
359 O << "\n\t" << "END\n";
360 return AsmPrinter::doFinalization(M);
363 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
364 const Function *F = MF.getFunction();
365 const TargetData *TD = TM.getTargetData();
366 // Emit the data section name.
369 PIC16Section *fPDataSection =
370 const_cast<PIC16Section *>(getObjFileLowering().
371 SectionForFrame(CurrentFnSym->getName()));
373 fPDataSection->setColor(getFunctionColor(F));
374 OutStreamer.SwitchSection(fPDataSection);
376 // Emit function frame label
377 O << PAN::getFrameLabel(CurrentFnSym->getName()) << ":\n";
379 const Type *RetType = F->getReturnType();
380 unsigned RetSize = 0;
381 if (RetType->getTypeID() != Type::VoidTyID)
382 RetSize = TD->getTypeAllocSize(RetType);
384 //Emit function return value space
385 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
386 // we will need to avoid printing a global directive for Retval label
387 // in emitExternandGloblas.
389 O << PAN::getRetvalLabel(CurrentFnSym->getName())
390 << " RES " << RetSize << "\n";
392 O << PAN::getRetvalLabel(CurrentFnSym->getName()) << ": \n";
394 // Emit variable to hold the space for function arguments
395 unsigned ArgSize = 0;
396 for (Function::const_arg_iterator argi = F->arg_begin(),
397 arge = F->arg_end(); argi != arge ; ++argi) {
398 const Type *Ty = argi->getType();
399 ArgSize += TD->getTypeAllocSize(Ty);
402 O << PAN::getArgsLabel(CurrentFnSym->getName()) << " RES " << ArgSize << "\n";
404 // Emit temporary space
405 int TempSize = PTLI->GetTmpSize();
407 O << PAN::getTempdataLabel(CurrentFnSym->getName()) << " RES "
412 void PIC16AsmPrinter::EmitInitializedDataSection(const PIC16Section *S) {
413 /// Emit Section header.
414 OutStreamer.SwitchSection(S);
416 std::vector<const GlobalVariable*> Items = S->Items;
417 for (unsigned j = 0; j < Items.size(); j++) {
418 Constant *C = Items[j]->getInitializer();
419 int AddrSpace = Items[j]->getType()->getAddressSpace();
420 O << *Mang->getSymbol(Items[j]);
421 EmitGlobalConstant(C, AddrSpace);
425 // Print all IDATA sections.
426 void PIC16AsmPrinter::EmitIData(Module &M) {
427 EmitSectionList (M, PTOF->IDATASections());
430 void PIC16AsmPrinter::
431 EmitUninitializedDataSection(const PIC16Section *S) {
432 const TargetData *TD = TM.getTargetData();
433 OutStreamer.SwitchSection(S);
434 std::vector<const GlobalVariable*> Items = S->Items;
435 for (unsigned j = 0; j < Items.size(); j++) {
436 Constant *C = Items[j]->getInitializer();
437 const Type *Ty = C->getType();
438 unsigned Size = TD->getTypeAllocSize(Ty);
439 O << *Mang->getSymbol(Items[j]) << " RES " << Size << "\n";
443 // Print all UDATA sections.
444 void PIC16AsmPrinter::EmitUData(Module &M) {
445 EmitSectionList (M, PTOF->UDATASections());
448 // Print all USER sections.
449 void PIC16AsmPrinter::EmitUserSections(Module &M) {
450 EmitSectionList (M, PTOF->USERSections());
453 // Print all AUTO sections.
454 void PIC16AsmPrinter::EmitAllAutos(Module &M) {
455 EmitSectionList (M, PTOF->AUTOSections());
458 extern "C" void LLVMInitializePIC16AsmPrinter() {
459 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
462 // Emit one data section using correct section emitter based on section type.
463 void PIC16AsmPrinter::EmitSingleSection(const PIC16Section *S) {
464 if (S == NULL) return;
466 switch (S->getType()) {
467 default: llvm_unreachable ("unknow user section type");
471 EmitUninitializedDataSection(S);
475 EmitInitializedDataSection(S);
480 // Emit a list of sections.
481 void PIC16AsmPrinter::
482 EmitSectionList(Module &M, const std::vector<PIC16Section *> &SList) {
483 for (unsigned i = 0; i < SList.size(); i++) {
484 // Exclude llvm specific metadata sections.
485 if (SList[i]->getName().find("llvm.") != std::string::npos)
488 EmitSingleSection(SList[i]);