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"
30 #include "PIC16GenAsmWriter.inc"
32 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
37 /// runOnMachineFunction - This emits the frame section, autos section and
38 /// assembly for each instruction. Also takes care of function begin debug
39 /// directive and file begin debug directive (if required) for the function.
41 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
44 // This calls the base class function required to be called at beginning
45 // of runOnMachineFunction.
46 SetupMachineFunction(MF);
48 // Get the mangled name.
49 const Function *F = MF.getFunction();
50 CurrentFnName = Mang->getMangledName(F);
52 // Emit the function frame (args and temps).
53 EmitFunctionFrame(MF);
55 DbgInfo.BeginFunction(MF);
57 // Emit the autos section of function.
58 EmitAutos(CurrentFnName);
60 // Now emit the instructions of function in its code section.
61 const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str();
63 const Section *fCodeSection = TAI->getNamedSection(codeSection,
65 // Start the Code Section.
67 SwitchToSection(fCodeSection);
69 // Emit the frame address of the function at the beginning of code.
70 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
71 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n";
73 // Emit function start label.
74 O << CurrentFnName << ":\n";
78 // Print out code for the function.
79 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
82 // Print a label for the basic block.
83 if (I != MF.begin()) {
84 printBasicBlockLabel(I, true);
88 // Print a basic block.
89 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
92 // Emit the line directive if source line changed.
93 const DebugLoc DL = II->getDebugLoc();
94 if (!DL.isUnknown() && DL != CurDL) {
95 DbgInfo.ChangeDebugLoc(MF, DL);
99 // Print the assembly for the instruction.
100 printMachineInstruction(II);
104 // Emit function end debug directives.
105 DbgInfo.EndFunction(MF);
107 return false; // we didn't modify anything.
110 /// createPIC16CodePrinterPass - Returns a pass that prints the PIC16
111 /// assembly code for a MachineFunction to the given output stream,
112 /// using the given target machine description. This should work
113 /// regardless of whether the function is in SSA form.
115 FunctionPass *llvm::createPIC16CodePrinterPass(formatted_raw_ostream &o,
118 return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
122 // printOperand - print operand of insn.
123 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
124 const MachineOperand &MO = MI->getOperand(opNum);
126 switch (MO.getType()) {
127 case MachineOperand::MO_Register:
128 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
129 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
131 llvm_unreachable("not implemented");
134 case MachineOperand::MO_Immediate:
135 O << (int)MO.getImm();
138 case MachineOperand::MO_GlobalAddress: {
139 O << Mang->getMangledName(MO.getGlobal());
142 case MachineOperand::MO_ExternalSymbol: {
143 const char *Sname = MO.getSymbolName();
145 // If its a libcall name, record it to decls section.
146 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
147 LibcallDecls.push_back(Sname);
153 case MachineOperand::MO_MachineBasicBlock:
154 printBasicBlockLabel(MO.getMBB());
158 llvm_unreachable(" Operand type not supported.");
162 /// printCCOperand - Print the cond code operand.
164 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
165 int CC = (int)MI->getOperand(opNum).getImm();
166 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
169 /// printLibcallDecls - print the extern declarations for compiler
172 void PIC16AsmPrinter::printLibcallDecls(void) {
173 // If no libcalls used, return.
174 if (LibcallDecls.empty()) return;
176 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
177 // Remove duplicate entries.
179 LibcallDecls.unique();
180 for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
181 I != LibcallDecls.end(); I++) {
182 O << TAI->getExternDirective() << *I << "\n";
183 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
184 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
186 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n";
189 /// doInitialization - Perfrom Module level initializations here.
190 /// One task that we do here is to sectionize all global variables.
191 /// The MemSelOptimizer pass depends on the sectionizing.
193 bool PIC16AsmPrinter::doInitialization(Module &M) {
194 bool Result = AsmPrinter::doInitialization(M);
196 // FIXME:: This is temporary solution to generate the include file.
197 // The processor should be passed to llc as in input and the header file
198 // should be generated accordingly.
199 O << "\n\t#include P16F1937.INC\n";
201 // Set the section names for all globals.
202 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
204 I->setSection(TAI->SectionForGlobal(I)->getName());
207 DbgInfo.BeginModule(M);
208 EmitFunctionDecls(M);
209 EmitUndefinedVars(M);
217 /// Emit extern decls for functions imported from other modules, and emit
218 /// global declarations for function defined in this module and which are
219 /// available to other modules.
221 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) {
222 // Emit declarations for external functions.
223 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n";
224 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
225 std::string Name = Mang->getMangledName(I);
226 if (Name.compare("@abort") == 0)
229 // If it is llvm intrinsic call then don't emit
230 if (Name.find("llvm.") != std::string::npos)
233 if (!I->isDeclaration() && !I->hasExternalLinkage())
236 const char *directive = I->isDeclaration() ? TAI->getExternDirective() :
237 TAI->getGlobalDirective();
239 O << directive << Name << "\n";
240 O << directive << PAN::getRetvalLabel(Name) << "\n";
241 O << directive << PAN::getArgsLabel(Name) << "\n";
244 O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
247 // Emit variables imported from other Modules.
248 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) {
249 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items;
250 if (!Items.size()) return;
252 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n";
253 for (unsigned j = 0; j < Items.size(); j++) {
254 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n";
256 O << TAI->getCommentString() << "Imported Variables - END" << "\n";
259 // Emit variables defined in this module and are available to other modules.
260 void PIC16AsmPrinter::EmitDefinedVars(Module &M) {
261 std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items;
262 if (!Items.size()) return;
264 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n";
265 for (unsigned j = 0; j < Items.size(); j++) {
266 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n";
268 O << TAI->getCommentString() << "Exported Variables - END" << "\n";
271 // Emit initialized data placed in ROM.
272 void PIC16AsmPrinter::EmitRomData(Module &M) {
273 // Print ROM Data section.
274 const std::vector<PIC16Section*> &ROSections = PTAI->ROSections;
275 for (unsigned i = 0; i < ROSections.size(); i++) {
276 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items;
277 if (!Items.size()) continue;
279 SwitchToSection(PTAI->ROSections[i]->S_);
280 for (unsigned j = 0; j < Items.size(); j++) {
281 O << Mang->getMangledName(Items[j]);
282 Constant *C = Items[j]->getInitializer();
283 int AddrSpace = Items[j]->getType()->getAddressSpace();
284 EmitGlobalConstant(C, AddrSpace);
289 bool PIC16AsmPrinter::doFinalization(Module &M) {
291 EmitRemainingAutos();
292 DbgInfo.EndModule(M);
293 O << "\n\t" << "END\n";
294 bool Result = AsmPrinter::doFinalization(M);
298 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) {
299 const Function *F = MF.getFunction();
300 std::string FuncName = Mang->getMangledName(F);
301 const TargetData *TD = TM.getTargetData();
302 // Emit the data section name.
304 const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str();
306 const Section *fPDataSection = TAI->getNamedSection(SectionName,
307 SectionFlags::Writeable);
308 SwitchToSection(fPDataSection);
310 // Emit function frame label
311 O << PAN::getFrameLabel(CurrentFnName) << ":\n";
313 const Type *RetType = F->getReturnType();
314 unsigned RetSize = 0;
315 if (RetType->getTypeID() != Type::VoidTyID)
316 RetSize = TD->getTypeAllocSize(RetType);
318 //Emit function return value space
319 // FIXME: Do not emit RetvalLable when retsize is zero. To do this
320 // we will need to avoid printing a global directive for Retval label
321 // in emitExternandGloblas.
323 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n";
325 O << PAN::getRetvalLabel(CurrentFnName) << ": \n";
327 // Emit variable to hold the space for function arguments
328 unsigned ArgSize = 0;
329 for (Function::const_arg_iterator argi = F->arg_begin(),
330 arge = F->arg_end(); argi != arge ; ++argi) {
331 const Type *Ty = argi->getType();
332 ArgSize += TD->getTypeAllocSize(Ty);
335 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n";
337 // Emit temporary space
338 int TempSize = PTLI->GetTmpSize();
340 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n';
343 void PIC16AsmPrinter::EmitIData(Module &M) {
345 // Print all IDATA sections.
346 const std::vector<PIC16Section*> &IDATASections = PTAI->IDATASections;
347 for (unsigned i = 0; i < IDATASections.size(); i++) {
349 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos)
351 SwitchToSection(IDATASections[i]->S_);
352 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items;
353 for (unsigned j = 0; j < Items.size(); j++) {
354 std::string Name = Mang->getMangledName(Items[j]);
355 Constant *C = Items[j]->getInitializer();
356 int AddrSpace = Items[j]->getType()->getAddressSpace();
358 EmitGlobalConstant(C, AddrSpace);
363 void PIC16AsmPrinter::EmitUData(Module &M) {
364 const TargetData *TD = TM.getTargetData();
366 // Print all BSS sections.
367 const std::vector<PIC16Section*> &BSSSections = PTAI->BSSSections;
368 for (unsigned i = 0; i < BSSSections.size(); i++) {
370 SwitchToSection(BSSSections[i]->S_);
371 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items;
372 for (unsigned j = 0; j < Items.size(); j++) {
373 std::string Name = Mang->getMangledName(Items[j]);
374 Constant *C = Items[j]->getInitializer();
375 const Type *Ty = C->getType();
376 unsigned Size = TD->getTypeAllocSize(Ty);
378 O << Name << " RES " << Size << "\n";
383 void PIC16AsmPrinter::EmitAutos(std::string FunctName) {
384 // Section names for all globals are already set.
385 const TargetData *TD = TM.getTargetData();
387 // Now print Autos section for this function.
388 std::string SectionName = PAN::getAutosSectionName(FunctName);
389 const std::vector<PIC16Section*> &AutosSections = PTAI->AutosSections;
390 for (unsigned i = 0; i < AutosSections.size(); i++) {
392 if (AutosSections[i]->S_->getName() == SectionName) {
393 // Set the printing status to true
394 AutosSections[i]->setPrintedStatus(true);
395 SwitchToSection(AutosSections[i]->S_);
396 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
397 for (unsigned j = 0; j < Items.size(); j++) {
398 std::string VarName = Mang->getMangledName(Items[j]);
399 Constant *C = Items[j]->getInitializer();
400 const Type *Ty = C->getType();
401 unsigned Size = TD->getTypeAllocSize(Ty);
402 // Emit memory reserve directive.
403 O << VarName << " RES " << Size << "\n";
410 // Print autos that were not printed during the code printing of functions.
411 // As the functions might themselves would have got deleted by the optimizer.
412 void PIC16AsmPrinter::EmitRemainingAutos() {
413 const TargetData *TD = TM.getTargetData();
415 // Now print Autos section for this function.
416 std::vector <PIC16Section *>AutosSections = PTAI->AutosSections;
417 for (unsigned i = 0; i < AutosSections.size(); i++) {
419 // if the section is already printed then don't print again
420 if (AutosSections[i]->isPrinted())
423 // Set status as printed
424 AutosSections[i]->setPrintedStatus(true);
427 SwitchToSection(AutosSections[i]->S_);
428 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items;
429 for (unsigned j = 0; j < Items.size(); j++) {
430 std::string VarName = Mang->getMangledName(Items[j]);
431 Constant *C = Items[j]->getInitializer();
432 const Type *Ty = C->getType();
433 unsigned Size = TD->getTypeAllocSize(Ty);
434 // Emit memory reserve directive.
435 O << VarName << " RES " << Size << "\n";