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 "PIC16AsmPrinter.h"
16 #include "PIC16TargetAsmInfo.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Module.h"
20 #include "llvm/CodeGen/DwarfWriter.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "PIC16GenAsmWriter.inc"
34 PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
35 const TargetAsmInfo *T, bool V)
36 : AsmPrinter(O, TM, T, V), DbgInfo(O, T) {
37 PTLI = static_cast<const PIC16TargetLowering*>(TM.getTargetLowering());
38 PTAI = static_cast<const PIC16TargetAsmInfo*>(T);
39 PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering();
42 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
47 /// runOnMachineFunction - This emits the frame section, autos section and
48 /// assembly for each instruction. Also takes care of function begin debug
49 /// directive and file begin debug directive (if required) for the function.
51 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
54 // This calls the base class function required to be called at beginning
55 // of runOnMachineFunction.
56 SetupMachineFunction(MF);
58 // Get the mangled name.
59 const Function *F = MF.getFunction();
60 CurrentFnName = Mang->getMangledName(F);
62 // Emit the function frame (args and temps).
63 EmitFunctionFrame(MF);
65 DbgInfo.BeginFunction(MF);
67 // Emit the autos section of function.
68 EmitAutos(CurrentFnName);
70 // Now emit the instructions of function in its code section.
71 std::string T = PAN::getCodeSectionName(CurrentFnName);
72 const char *codeSection = T.c_str();
74 const Section *fCodeSection =
75 getObjFileLowering().getOrCreateSection(codeSection, false,
77 // Start the Code Section.
79 SwitchToSection(fCodeSection);
81 // Emit the frame address of the function at the beginning of code.
82 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
83 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
85 // Emit function start label.
86 O << CurrentFnName << ":\n";
90 // Print out code for the function.
91 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
94 // Print a label for the basic block.
95 if (I != MF.begin()) {
96 printBasicBlockLabel(I, true);
100 // Print a basic block.
101 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
104 // Emit the line directive if source line changed.
105 const DebugLoc DL = II->getDebugLoc();
106 if (!DL.isUnknown() && DL != CurDL) {
107 DbgInfo.ChangeDebugLoc(MF, DL);
111 // Print the assembly for the instruction.
112 printMachineInstruction(II);
116 // Emit function end debug directives.
117 DbgInfo.EndFunction(MF);
119 return false; // we didn't modify anything.
123 // printOperand - print operand of insn.
124 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
125 const MachineOperand &MO = MI->getOperand(opNum);
127 switch (MO.getType()) {
128 case MachineOperand::MO_Register:
129 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
130 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
132 llvm_unreachable("not implemented");
135 case MachineOperand::MO_Immediate:
136 O << (int)MO.getImm();
139 case MachineOperand::MO_GlobalAddress: {
140 O << Mang->getMangledName(MO.getGlobal());
143 case MachineOperand::MO_ExternalSymbol: {
144 const char *Sname = MO.getSymbolName();
146 // If its a libcall name, record it to decls section.
147 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
148 LibcallDecls.push_back(Sname);
154 case MachineOperand::MO_MachineBasicBlock:
155 printBasicBlockLabel(MO.getMBB());
159 llvm_unreachable(" Operand type not supported.");
163 /// printCCOperand - Print the cond code operand.
165 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
166 int CC = (int)MI->getOperand(opNum).getImm();
167 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
170 // This function is used to sort the decls list.
171 // should return true if s1 should come before s2.
172 static bool is_before(const char *s1, const char *s2) {
173 std::string str1 = s1;
174 std::string str2 = s2;
175 int i = str1.compare(str2);
176 // Return true if s1 is smaller or equal.
177 if (i <= 0) return true;
178 // false if s1 should come after s2.
182 // This is used by list::unique below.
183 // unique will filter out duplicates if it knows them.
184 static bool is_duplicate(const char *s1, const char *s2) {
185 std::string str1 = s1;
186 std::string str2 = s2;
190 /// printLibcallDecls - print the extern declarations for compiler
193 void PIC16AsmPrinter::printLibcallDecls(void) {
194 // If no libcalls used, return.
195 if (LibcallDecls.empty()) return;
197 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
198 // Remove duplicate entries.
199 LibcallDecls.sort(is_before);
200 LibcallDecls.unique(is_duplicate);
202 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
203 I != LibcallDecls.end(); I++) {
204 O << TAI->getExternDirective() << *I << "\n";
205 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
206 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
208 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
211 /// doInitialization - Perfrom Module level initializations here.
212 /// One task that we do here is to sectionize all global variables.
213 /// The MemSelOptimizer pass depends on the sectionizing.
215 bool PIC16AsmPrinter::doInitialization(Module &M) {
216 bool Result = AsmPrinter::doInitialization(M);
218 // FIXME:: This is temporary solution to generate the include file.
219 // The processor should be passed to llc as in input and the header file
220 // should be generated accordingly.
221 O << "\n\t#include P16F1937.INC\n";
223 // Set the section names for all globals.
224 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
226 I->setSection(getObjFileLowering().SectionForGlobal(I, Mang,TM)->getName());
228 DbgInfo.BeginModule(M);
229 EmitFunctionDecls(M);
230 EmitUndefinedVars(M);
238 /// Emit extern decls for functions imported from other modules, and emit
239 /// global declarations for function defined in this module and which are
240 /// available to other modules.
242 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
243 // Emit declarations for external functions.
244 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
245 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
246 if (I->isIntrinsic())
249 std::string Name = Mang->getMangledName(I);
250 if (Name.compare("@abort") == 0)
253 if (!I->isDeclaration() && !I->hasExternalLinkage())
256 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
257 TAI->getGlobalDirective();
259 O << directive << Name << "\n";
260 O << directive << PAN::getRetvalLabel(Name) << "\n";
261 O << directive << PAN::getArgsLabel(Name) << "\n";
264 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
267 // Emit variables imported from other Modules.
268 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
269 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items;
270 if (!Items.size()) return;
272 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
273 for (unsigned j = 0; j < Items.size(); j++) {
274 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
276 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
279 // Emit variables defined in this module and are available to other modules.
280 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
281 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items;
282 if (!Items.size()) return;
284 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
285 for (unsigned j = 0; j < Items.size(); j++) {
286 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
288 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
291 // Emit initialized data placed in ROM.
292 void PIC16AsmPrinter::EmitRomData(Module &M) {
293 // Print ROM Data section.
294 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections;
295 for (unsigned i = 0; i < ROSections.size(); i++) {
296 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
297 if (!Items.size()) continue;
299 SwitchToSection(PTOF->ROSections[i]->S_);
300 for (unsigned j = 0; j < Items.size(); j++) {
301 O << Mang->getMangledName(Items[j]);
302 Constant *C = Items[j]->getInitializer();
303 int AddrSpace = Items[j]->getType()->getAddressSpace();
304 EmitGlobalConstant(C, AddrSpace);
309 bool PIC16AsmPrinter::doFinalization(Module &M) {
311 EmitRemainingAutos();
312 DbgInfo.EndModule(M);
313 O << "\n\t" << "END\n";
314 return AsmPrinter::doFinalization(M);
317 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
318 const Function *F = MF.getFunction();
319 std::string FuncName = Mang->getMangledName(F);
320 const TargetData *TD = TM.getTargetData();
321 // Emit the data section name.
323 std::string T = PAN::getFrameSectionName(CurrentFnName);
324 const char *SectionName = T.c_str();
326 const Section *fPDataSection =
327 getObjFileLowering().getOrCreateSection(SectionName, false,
328 SectionKind::DataRel);
329 SwitchToSection(fPDataSection);
331 // Emit function frame label
332 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
334 const Type *RetType = F->getReturnType();
335 unsigned RetSize = 0;
336 if (RetType->getTypeID() != Type::VoidTyID)
337 RetSize = TD->getTypeAllocSize(RetType);
339 //Emit function return value space
340 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
341 // we will need to avoid printing a global directive for Retval label
342 // in emitExternandGloblas.
344 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
346 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
348 // Emit variable to hold the space for function arguments
349 unsigned ArgSize = 0;
350 for (Function::const_arg_iterator argi = F->arg_begin(),
351 arge = F->arg_end(); argi != arge ; ++argi) {
352 const Type *Ty = argi->getType();
353 ArgSize += TD->getTypeAllocSize(Ty);
356 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
358 // Emit temporary space
359 int TempSize = PTLI->GetTmpSize();
361 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
364 void PIC16AsmPrinter::EmitIData(Module &M) {
366 // Print all IDATA sections.
367 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections;
368 for (unsigned i = 0; i < IDATASections.size(); i++) {
370 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
372 SwitchToSection(IDATASections[i]->S_);
373 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
374 for (unsigned j = 0; j < Items.size(); j++) {
375 std::string Name = Mang->getMangledName(Items[j]);
376 Constant *C = Items[j]->getInitializer();
377 int AddrSpace = Items[j]->getType()->getAddressSpace();
379 EmitGlobalConstant(C, AddrSpace);
384 void PIC16AsmPrinter::EmitUData(Module &M) {
385 const TargetData *TD = TM.getTargetData();
387 // Print all BSS sections.
388 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections;
389 for (unsigned i = 0; i < BSSSections.size(); i++) {
391 SwitchToSection(BSSSections[i]->S_);
392 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
393 for (unsigned j = 0; j < Items.size(); j++) {
394 std::string Name = Mang->getMangledName(Items[j]);
395 Constant *C = Items[j]->getInitializer();
396 const Type *Ty = C->getType();
397 unsigned Size = TD->getTypeAllocSize(Ty);
399 O << Name << " RES " << Size << "\n";
404 void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
405 // Section names for all globals are already set.
406 const TargetData *TD = TM.getTargetData();
408 // Now print Autos section for this function.
409 std::string SectionName = PAN::getAutosSectionName(FunctName);
410 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections;
411 for (unsigned i = 0; i < AutosSections.size(); i++) {
413 if (AutosSections[i]->S_->getName() == SectionName) {
414 // Set the printing status to true
415 AutosSections[i]->setPrintedStatus(true);
416 SwitchToSection(AutosSections[i]->S_);
417 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
418 for (unsigned j = 0; j < Items.size(); j++) {
419 std::string VarName = Mang->getMangledName(Items[j]);
420 Constant *C = Items[j]->getInitializer();
421 const Type *Ty = C->getType();
422 unsigned Size = TD->getTypeAllocSize(Ty);
423 // Emit memory reserve directive.
424 O << VarName << " RES " << Size << "\n";
431 // Print autos that were not printed during the code printing of functions.
432 // As the functions might themselves would have got deleted by the optimizer.
433 void PIC16AsmPrinter::EmitRemainingAutos() {
434 const TargetData *TD = TM.getTargetData();
436 // Now print Autos section for this function.
437 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections;
438 for (unsigned i = 0; i < AutosSections.size(); i++) {
440 // if the section is already printed then don't print again
441 if (AutosSections[i]->isPrinted())
444 // Set status as printed
445 AutosSections[i]->setPrintedStatus(true);
448 SwitchToSection(AutosSections[i]->S_);
449 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
450 for (unsigned j = 0; j < Items.size(); j++) {
451 std::string VarName = Mang->getMangledName(Items[j]);
452 Constant *C = Items[j]->getInitializer();
453 const Type *Ty = C->getType();
454 unsigned Size = TD->getTypeAllocSize(Ty);
455 // Emit memory reserve directive.
456 O << VarName << " RES " << Size << "\n";
462 extern "C" void LLVMInitializePIC16Target() {
463 // Register the targets
464 RegisterTargetMachine<PIC16TargetMachine> A(ThePIC16Target);
465 RegisterTargetMachine<CooperTargetMachine> B(TheCooperTarget);
466 RegisterAsmPrinter<PIC16AsmPrinter> C(ThePIC16Target);
467 RegisterAsmPrinter<PIC16AsmPrinter> D(TheCooperTarget);