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 "PIC16MachineFunctionInfo.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetRegistry.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/ADT/SmallString.h"
36 #include "PIC16GenAsmWriter.inc"
38 PIC16AsmPrinter::PIC16AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
39 : AsmPrinter(TM, Streamer), DbgInfo(Streamer, TM.getMCAsmInfo()) {
40 PMAI = static_cast<const PIC16MCAsmInfo*>(TM.getMCAsmInfo());
41 PTOF = &getObjFileLowering();
44 void PIC16AsmPrinter::EmitInstruction(const MachineInstr *MI) {
46 raw_svector_ostream OS(Str);
47 printInstruction(MI, OS);
49 OutStreamer.EmitRawText(OS.str());
52 static int getFunctionColor(const Function *F) {
53 if (F->hasSection()) {
54 std::string Sectn = F->getSection();
55 std::string StrToFind = "Overlay=";
56 std::string::size_type Pos = Sectn.find(StrToFind);
58 // Retreive the color number if the key is found.
59 if (Pos != std::string::npos) {
60 Pos += StrToFind.length();
61 std::string Color = "";
62 char c = Sectn.at(Pos);
63 // A Color can only consist of digits.
64 while (c >= '0' && c<= '9') {
67 if (Pos >= Sectn.length())
71 return atoi(Color.c_str());
75 // Color was not set for function, so return -1.
79 // Color the Auto section of the given function.
80 void PIC16AsmPrinter::ColorAutoSection(const Function *F) {
81 std::string SectionName = PAN::getAutosSectionName(CurrentFnSym->getName());
82 PIC16Section* Section = PTOF->findPIC16Section(SectionName);
83 if (Section != NULL) {
84 int Color = getFunctionColor(F);
86 Section->setColor(Color);
91 /// runOnMachineFunction - This emits the frame section, autos section and
92 /// assembly for each instruction. Also takes care of function begin debug
93 /// directive and file begin debug directive (if required) for the function.
95 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
96 // This calls the base class function required to be called at beginning
97 // of runOnMachineFunction.
98 SetupMachineFunction(MF);
100 // Put the color information from function to its auto section.
101 const Function *F = MF.getFunction();
104 // Emit the function frame (args and temps).
105 EmitFunctionFrame(MF);
107 DbgInfo.BeginFunction(MF);
109 // Now emit the instructions of function in its code section.
110 const MCSection *fCodeSection =
111 getObjFileLowering().SectionForCode(CurrentFnSym->getName(),
112 PAN::isISR(F->getSection()));
114 // Start the Code Section.
115 OutStreamer.SwitchSection(fCodeSection);
117 // Emit the frame address of the function at the beginning of code.
118 OutStreamer.EmitRawText("\tretlw low(" +
119 Twine(PAN::getFrameLabel(CurrentFnSym->getName())) +
121 OutStreamer.EmitRawText("\tretlw high(" +
122 Twine(PAN::getFrameLabel(CurrentFnSym->getName())) +
125 // Emit function start label.
126 OutStreamer.EmitLabel(CurrentFnSym);
129 // Print out code for the function.
130 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
133 // Print a label for the basic block.
135 EmitBasicBlockStart(I);
137 // Print a basic block.
138 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
140 // Emit the line directive if source line changed.
141 DebugLoc DL = II->getDebugLoc();
142 if (!DL.isUnknown() && DL != CurDL) {
143 DbgInfo.ChangeDebugLoc(MF, DL);
147 // Print the assembly for the instruction.
152 // Emit function end debug directives.
153 DbgInfo.EndFunction(MF);
155 return false; // we didn't modify anything.
159 // printOperand - print operand of insn.
160 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum,
162 const MachineOperand &MO = MI->getOperand(opNum);
163 const Function *F = MI->getParent()->getParent()->getFunction();
165 switch (MO.getType()) {
166 case MachineOperand::MO_Register:
168 // For indirect load/store insns, the fsr name is printed as INDF.
169 std::string RegName = getRegisterName(MO.getReg());
170 if ((MI->getOpcode() == PIC16::load_indirect) ||
171 (MI->getOpcode() == PIC16::store_indirect))
172 RegName.replace (0, 3, "INDF");
177 case MachineOperand::MO_Immediate:
178 O << (int)MO.getImm();
181 case MachineOperand::MO_GlobalAddress: {
182 MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
183 // FIXME: currently we do not have a memcpy def coming in the module
184 // by any chance, as we do not link in those as .bc lib. So these calls
185 // are always external and it is safe to emit an extern.
186 if (PAN::isMemIntrinsic(Sym->getName()))
187 LibcallDecls.insert(Sym->getName());
192 case MachineOperand::MO_ExternalSymbol: {
193 const char *Sname = MO.getSymbolName();
194 std::string Printname = Sname;
196 // Intrinsic stuff needs to be renamed if we are printing IL fn.
197 if (PAN::isIntrinsicStuff(Printname)) {
198 if (PAN::isISR(F->getSection())) {
199 Printname = PAN::Rename(Sname);
201 // Record these decls, we need to print them in asm as extern.
202 LibcallDecls.insert(Printname);
208 case MachineOperand::MO_MachineBasicBlock:
209 O << *MO.getMBB()->getSymbol();
213 llvm_unreachable(" Operand type not supported.");
217 /// printCCOperand - Print the cond code operand.
219 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
221 int CC = (int)MI->getOperand(opNum).getImm();
222 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
225 /// printLibcallDecls - print the extern declarations for compiler
228 void PIC16AsmPrinter::printLibcallDecls() {
229 // If no libcalls used, return.
230 if (LibcallDecls.empty()) return;
232 OutStreamer.AddComment("External decls for libcalls - BEGIN");
233 OutStreamer.AddBlankLine();
235 for (std::set<std::string>::const_iterator I = LibcallDecls.begin(),
236 E = LibcallDecls.end(); I != E; I++)
237 OutStreamer.EmitRawText(MAI->getExternDirective() + Twine(*I));
239 OutStreamer.AddComment("External decls for libcalls - END");
240 OutStreamer.AddBlankLine();
243 /// doInitialization - Perform Module level initializations here.
244 /// One task that we do here is to sectionize all global variables.
245 /// The MemSelOptimizer pass depends on the sectionizing.
247 bool PIC16AsmPrinter::doInitialization(Module &M) {
248 bool Result = AsmPrinter::doInitialization(M);
250 // Every asmbly contains these std headers.
251 OutStreamer.EmitRawText(StringRef("\n#include p16f1xxx.inc"));
252 OutStreamer.EmitRawText(StringRef("#include stdmacros.inc"));
254 // Set the section names for all globals.
255 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
258 // Record External Var Decls.
259 if (I->isDeclaration()) {
260 ExternalVarDecls.push_back(I);
264 // Record Exteranl Var Defs.
265 if (I->hasExternalLinkage() || I->hasCommonLinkage()) {
266 ExternalVarDefs.push_back(I);
269 // Sectionify actual data.
270 if (!I->hasAvailableExternallyLinkage()) {
271 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM);
273 I->setSection(((const PIC16Section *)S)->getName());
277 DbgInfo.BeginModule(M);
278 EmitFunctionDecls(M);
279 EmitUndefinedVars(M);
289 /// Emit extern decls for functions imported from other modules, and emit
290 /// global declarations for function defined in this module and which are
291 /// available to other modules.
293 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
294 // Emit declarations for external functions.
295 OutStreamer.AddComment("Function Declarations - BEGIN");
296 OutStreamer.AddBlankLine();
297 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
298 if (I->isIntrinsic() || I->getName() == "@abort")
301 if (!I->isDeclaration() && !I->hasExternalLinkage())
304 MCSymbol *Sym = Mang->getSymbol(I);
306 // Do not emit memcpy, memset, and memmove here.
307 // Calls to these routines can be generated in two ways,
308 // 1. User calling the standard lib function
309 // 2. Codegen generating these calls for llvm intrinsics.
310 // In the first case a prototype is alread availale, while in
311 // second case the call is via and externalsym and the prototype is missing.
312 // So declarations for these are currently always getting printing by
313 // tracking both kind of references in printInstrunction.
314 if (I->isDeclaration() && PAN::isMemIntrinsic(Sym->getName())) continue;
316 const char *directive = I->isDeclaration() ? MAI->getExternDirective() :
317 MAI->getGlobalDirective();
319 OutStreamer.EmitRawText(directive + Twine(Sym->getName()));
320 OutStreamer.EmitRawText(directive +
321 Twine(PAN::getRetvalLabel(Sym->getName())));
322 OutStreamer.EmitRawText(directive +
323 Twine(PAN::getArgsLabel(Sym->getName())));
326 OutStreamer.AddComment("Function Declarations - END");
327 OutStreamer.AddBlankLine();
331 // Emit variables imported from other Modules.
332 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
333 std::vector<const GlobalVariable*> Items = ExternalVarDecls;
334 if (!Items.size()) return;
336 OutStreamer.AddComment("Imported Variables - BEGIN");
337 OutStreamer.AddBlankLine();
338 for (unsigned j = 0; j < Items.size(); j++)
339 OutStreamer.EmitRawText(MAI->getExternDirective() +
340 Twine(Mang->getSymbol(Items[j])->getName()));
342 OutStreamer.AddComment("Imported Variables - END");
343 OutStreamer.AddBlankLine();
346 // Emit variables defined in this module and are available to other modules.
347 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
348 std::vector<const GlobalVariable*> Items = ExternalVarDefs;
349 if (!Items.size()) return;
351 OutStreamer.AddComment("Exported Variables - BEGIN");
352 OutStreamer.AddBlankLine();
354 for (unsigned j = 0; j < Items.size(); j++)
355 OutStreamer.EmitRawText(MAI->getGlobalDirective() +
356 Twine(Mang->getSymbol(Items[j])->getName()));
357 OutStreamer.AddComment("Exported Variables - END");
358 OutStreamer.AddBlankLine();
361 // Emit initialized data placed in ROM.
362 void PIC16AsmPrinter::EmitRomData(Module &M) {
363 EmitSingleSection(PTOF->ROMDATASection());
366 // Emit Shared section udata.
367 void PIC16AsmPrinter::EmitSharedUdata(Module &M) {
368 EmitSingleSection(PTOF->SHAREDUDATASection());
371 bool PIC16AsmPrinter::doFinalization(Module &M) {
374 DbgInfo.EndModule(M);
375 OutStreamer.EmitRawText(StringRef("\tEND"));
376 return AsmPrinter::doFinalization(M);
379 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
380 const Function *F = MF.getFunction();
381 const TargetData *TD = TM.getTargetData();
382 PIC16MachineFunctionInfo *FuncInfo = MF.getInfo<PIC16MachineFunctionInfo>();
384 // Emit the data section name.
386 PIC16Section *fPDataSection =
387 const_cast<PIC16Section *>(getObjFileLowering().
388 SectionForFrame(CurrentFnSym->getName()));
390 fPDataSection->setColor(getFunctionColor(F));
391 OutStreamer.SwitchSection(fPDataSection);
393 // Emit function frame label
394 OutStreamer.EmitRawText(PAN::getFrameLabel(CurrentFnSym->getName()) +
397 const Type *RetType = F->getReturnType();
398 unsigned RetSize = 0;
399 if (RetType->getTypeID() != Type::VoidTyID)
400 RetSize = TD->getTypeAllocSize(RetType);
402 //Emit function return value space
403 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
404 // we will need to avoid printing a global directive for Retval label
405 // in emitExternandGloblas.
407 OutStreamer.EmitRawText(PAN::getRetvalLabel(CurrentFnSym->getName()) +
408 Twine(" RES ") + Twine(RetSize));
410 OutStreamer.EmitRawText(PAN::getRetvalLabel(CurrentFnSym->getName()) +
413 // Emit variable to hold the space for function arguments
414 unsigned ArgSize = 0;
415 for (Function::const_arg_iterator argi = F->arg_begin(),
416 arge = F->arg_end(); argi != arge ; ++argi) {
417 const Type *Ty = argi->getType();
418 ArgSize += TD->getTypeAllocSize(Ty);
421 OutStreamer.EmitRawText(PAN::getArgsLabel(CurrentFnSym->getName()) +
422 Twine(" RES ") + Twine(ArgSize));
424 // Emit temporary space
425 int TempSize = FuncInfo->getTmpSize();
427 OutStreamer.EmitRawText(PAN::getTempdataLabel(CurrentFnSym->getName()) +
428 Twine(" RES ") + Twine(TempSize));
432 void PIC16AsmPrinter::EmitInitializedDataSection(const PIC16Section *S) {
433 /// Emit Section header.
434 OutStreamer.SwitchSection(S);
436 std::vector<const GlobalVariable*> Items = S->Items;
437 for (unsigned j = 0; j < Items.size(); j++) {
438 Constant *C = Items[j]->getInitializer();
439 int AddrSpace = Items[j]->getType()->getAddressSpace();
440 OutStreamer.EmitRawText(Mang->getSymbol(Items[j])->getName());
441 EmitGlobalConstant(C, AddrSpace);
445 // Print all IDATA sections.
446 void PIC16AsmPrinter::EmitIData(Module &M) {
447 EmitSectionList (M, PTOF->IDATASections());
450 void PIC16AsmPrinter::
451 EmitUninitializedDataSection(const PIC16Section *S) {
452 const TargetData *TD = TM.getTargetData();
453 OutStreamer.SwitchSection(S);
454 std::vector<const GlobalVariable*> Items = S->Items;
455 for (unsigned j = 0; j < Items.size(); j++) {
456 Constant *C = Items[j]->getInitializer();
457 const Type *Ty = C->getType();
458 unsigned Size = TD->getTypeAllocSize(Ty);
459 OutStreamer.EmitRawText(Mang->getSymbol(Items[j])->getName() +
460 Twine(" RES ") + Twine(Size));
464 // Print all UDATA sections.
465 void PIC16AsmPrinter::EmitUData(Module &M) {
466 EmitSectionList (M, PTOF->UDATASections());
469 // Print all USER sections.
470 void PIC16AsmPrinter::EmitUserSections(Module &M) {
471 EmitSectionList (M, PTOF->USERSections());
474 // Print all AUTO sections.
475 void PIC16AsmPrinter::EmitAllAutos(Module &M) {
476 EmitSectionList (M, PTOF->AUTOSections());
479 extern "C" void LLVMInitializePIC16AsmPrinter() {
480 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target);
483 // Emit one data section using correct section emitter based on section type.
484 void PIC16AsmPrinter::EmitSingleSection(const PIC16Section *S) {
485 if (S == NULL) return;
487 switch (S->getType()) {
488 default: llvm_unreachable ("unknow user section type");
492 EmitUninitializedDataSection(S);
496 EmitInitializedDataSection(S);
501 // Emit a list of sections.
502 void PIC16AsmPrinter::
503 EmitSectionList(Module &M, const std::vector<PIC16Section *> &SList) {
504 for (unsigned i = 0; i < SList.size(); i++) {
505 // Exclude llvm specific metadata sections.
506 if (SList[i]->getName().find("llvm.") != std::string::npos)
508 OutStreamer.AddBlankLine();
509 EmitSingleSection(SList[i]);