LLVM API Documentation
00001 //===-- PIC16AsmPrinter.cpp - PIC16 LLVM assembly writer ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a printer that converts from our internal representation 00011 // of machine-dependent LLVM code to PIC16 assembly language. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "PIC16AsmPrinter.h" 00016 #include "MCSectionPIC16.h" 00017 #include "PIC16TargetAsmInfo.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/Function.h" 00020 #include "llvm/Module.h" 00021 #include "llvm/CodeGen/DwarfWriter.h" 00022 #include "llvm/CodeGen/MachineFrameInfo.h" 00023 #include "llvm/CodeGen/DwarfWriter.h" 00024 #include "llvm/CodeGen/MachineModuleInfo.h" 00025 #include "llvm/MC/MCStreamer.h" 00026 #include "llvm/Target/TargetRegistry.h" 00027 #include "llvm/Target/TargetLoweringObjectFile.h" 00028 #include "llvm/Support/ErrorHandling.h" 00029 #include "llvm/Support/FormattedStream.h" 00030 #include "llvm/Support/Mangler.h" 00031 #include <cstring> 00032 using namespace llvm; 00033 00034 #include "PIC16GenAsmWriter.inc" 00035 00036 PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, 00037 const TargetAsmInfo *T, bool V) 00038 : AsmPrinter(O, TM, T, V), DbgInfo(O, T) { 00039 PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering()); 00040 PTAI = static_cast<const PIC16TargetAsmInfo*>(T); 00041 PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering(); 00042 } 00043 00044 bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00045 printInstruction(MI); 00046 return true; 00047 } 00048 00049 /// runOnMachineFunction - This emits the frame section, autos section and 00050 /// assembly for each instruction. Also takes care of function begin debug 00051 /// directive and file begin debug directive (if required) for the function. 00052 /// 00053 bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00054 this->MF = &MF; 00055 00056 // This calls the base class function required to be called at beginning 00057 // of runOnMachineFunction. 00058 SetupMachineFunction(MF); 00059 00060 // Get the mangled name. 00061 const Function *F = MF.getFunction(); 00062 CurrentFnName = Mang->getMangledName(F); 00063 00064 // Emit the function frame (args and temps). 00065 EmitFunctionFrame(MF); 00066 00067 DbgInfo.BeginFunction(MF); 00068 00069 // Emit the autos section of function. 00070 EmitAutos(CurrentFnName); 00071 00072 // Now emit the instructions of function in its code section. 00073 const MCSection *fCodeSection = 00074 getObjFileLowering().getSectionForFunction(CurrentFnName); 00075 // Start the Code Section. 00076 O << "\n"; 00077 OutStreamer.SwitchSection(fCodeSection); 00078 00079 // Emit the frame address of the function at the beginning of code. 00080 O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; 00081 O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; 00082 00083 // Emit function start label. 00084 O << CurrentFnName << ":\n"; 00085 00086 DebugLoc CurDL; 00087 O << "\n"; 00088 // Print out code for the function. 00089 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00090 I != E; ++I) { 00091 00092 // Print a label for the basic block. 00093 if (I != MF.begin()) { 00094 printBasicBlockLabel(I, true); 00095 O << '\n'; 00096 } 00097 00098 // Print a basic block. 00099 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00100 II != E; ++II) { 00101 00102 // Emit the line directive if source line changed. 00103 const DebugLoc DL = II->getDebugLoc(); 00104 if (!DL.isUnknown() && DL != CurDL) { 00105 DbgInfo.ChangeDebugLoc(MF, DL); 00106 CurDL = DL; 00107 } 00108 00109 // Print the assembly for the instruction. 00110 printMachineInstruction(II); 00111 } 00112 } 00113 00114 // Emit function end debug directives. 00115 DbgInfo.EndFunction(MF); 00116 00117 return false; // we didn't modify anything. 00118 } 00119 00120 00121 // printOperand - print operand of insn. 00122 void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 00123 const MachineOperand &MO = MI->getOperand(opNum); 00124 00125 switch (MO.getType()) { 00126 case MachineOperand::MO_Register: 00127 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 00128 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; 00129 else 00130 llvm_unreachable("not implemented"); 00131 return; 00132 00133 case MachineOperand::MO_Immediate: 00134 O << (int)MO.getImm(); 00135 return; 00136 00137 case MachineOperand::MO_GlobalAddress: { 00138 std::string Sname = Mang->getMangledName(MO.getGlobal()); 00139 // FIXME: currently we do not have a memcpy def coming in the module 00140 // by any chance, as we do not link in those as .bc lib. So these calls 00141 // are always external and it is safe to emit an extern. 00142 if (PAN::isMemIntrinsic(Sname)) { 00143 LibcallDecls.push_back(createESName(Sname)); 00144 } 00145 00146 O << Sname; 00147 break; 00148 } 00149 case MachineOperand::MO_ExternalSymbol: { 00150 const char *Sname = MO.getSymbolName(); 00151 00152 // If its a libcall name, record it to decls section. 00153 if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) { 00154 LibcallDecls.push_back(Sname); 00155 } 00156 00157 // Record a call to intrinsic to print the extern declaration for it. 00158 std::string Sym = Sname; 00159 if (PAN::isMemIntrinsic(Sym)) { 00160 Sym = PAN::addPrefix(Sym); 00161 LibcallDecls.push_back(createESName(Sym)); 00162 } 00163 00164 O << Sym; 00165 break; 00166 } 00167 case MachineOperand::MO_MachineBasicBlock: 00168 printBasicBlockLabel(MO.getMBB()); 00169 return; 00170 00171 default: 00172 llvm_unreachable(" Operand type not supported."); 00173 } 00174 } 00175 00176 /// printCCOperand - Print the cond code operand. 00177 /// 00178 void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { 00179 int CC = (int)MI->getOperand(opNum).getImm(); 00180 O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); 00181 } 00182 00183 // This function is used to sort the decls list. 00184 // should return true if s1 should come before s2. 00185 static bool is_before(const char *s1, const char *s2) { 00186 return strcmp(s1, s2) <= 0; 00187 } 00188 00189 // This is used by list::unique below. 00190 // unique will filter out duplicates if it knows them. 00191 static bool is_duplicate(const char *s1, const char *s2) { 00192 return !strcmp(s1, s2); 00193 } 00194 00195 /// printLibcallDecls - print the extern declarations for compiler 00196 /// intrinsics. 00197 /// 00198 void PIC16AsmPrinter::printLibcallDecls() { 00199 // If no libcalls used, return. 00200 if (LibcallDecls.empty()) return; 00201 00202 O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n"; 00203 // Remove duplicate entries. 00204 LibcallDecls.sort(is_before); 00205 LibcallDecls.unique(is_duplicate); 00206 00207 for (std::list<const char*>::const_iterator I = LibcallDecls.begin(); 00208 I != LibcallDecls.end(); I++) { 00209 O << TAI->getExternDirective() << *I << "\n"; 00210 O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n"; 00211 O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n"; 00212 } 00213 O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n"; 00214 } 00215 00216 /// doInitialization - Perfrom Module level initializations here. 00217 /// One task that we do here is to sectionize all global variables. 00218 /// The MemSelOptimizer pass depends on the sectionizing. 00219 /// 00220 bool PIC16AsmPrinter::doInitialization(Module &M) { 00221 bool Result = AsmPrinter::doInitialization(M); 00222 00223 // FIXME:: This is temporary solution to generate the include file. 00224 // The processor should be passed to llc as in input and the header file 00225 // should be generated accordingly. 00226 O << "\n\t#include P16F1937.INC\n"; 00227 00228 // Set the section names for all globals. 00229 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 00230 I != E; ++I) 00231 if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) { 00232 const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM); 00233 00234 I->setSection(((const MCSectionPIC16*)S)->getName()); 00235 } 00236 00237 DbgInfo.BeginModule(M); 00238 EmitFunctionDecls(M); 00239 EmitUndefinedVars(M); 00240 EmitDefinedVars(M); 00241 EmitIData(M); 00242 EmitUData(M); 00243 EmitRomData(M); 00244 return Result; 00245 } 00246 00247 /// Emit extern decls for functions imported from other modules, and emit 00248 /// global declarations for function defined in this module and which are 00249 /// available to other modules. 00250 /// 00251 void PIC16AsmPrinter::EmitFunctionDecls(Module &M) { 00252 // Emit declarations for external functions. 00253 O <<"\n"<<TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n"; 00254 for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) { 00255 if (I->isIntrinsic()) 00256 continue; 00257 00258 std::string Name = Mang->getMangledName(I); 00259 if (Name.compare("@abort") == 0) 00260 continue; 00261 00262 if (!I->isDeclaration() && !I->hasExternalLinkage()) 00263 continue; 00264 00265 // Do not emit memcpy, memset, and memmove here. 00266 // Calls to these routines can be generated in two ways, 00267 // 1. User calling the standard lib function 00268 // 2. Codegen generating these calls for llvm intrinsics. 00269 // In the first case a prototype is alread availale, while in 00270 // second case the call is via and externalsym and the prototype is missing. 00271 // So declarations for these are currently always getting printing by 00272 // tracking both kind of references in printInstrunction. 00273 if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue; 00274 00275 const char *directive = I->isDeclaration() ? TAI->getExternDirective() : 00276 TAI->getGlobalDirective(); 00277 00278 O << directive << Name << "\n"; 00279 O << directive << PAN::getRetvalLabel(Name) << "\n"; 00280 O << directive << PAN::getArgsLabel(Name) << "\n"; 00281 } 00282 00283 O << TAI->getCommentString() << "Function Declarations - END." <<"\n"; 00284 } 00285 00286 // Emit variables imported from other Modules. 00287 void PIC16AsmPrinter::EmitUndefinedVars(Module &M) { 00288 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items; 00289 if (!Items.size()) return; 00290 00291 O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n"; 00292 for (unsigned j = 0; j < Items.size(); j++) { 00293 O << TAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n"; 00294 } 00295 O << TAI->getCommentString() << "Imported Variables - END" << "\n"; 00296 } 00297 00298 // Emit variables defined in this module and are available to other modules. 00299 void PIC16AsmPrinter::EmitDefinedVars(Module &M) { 00300 std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items; 00301 if (!Items.size()) return; 00302 00303 O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n"; 00304 for (unsigned j = 0; j < Items.size(); j++) { 00305 O << TAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n"; 00306 } 00307 O << TAI->getCommentString() << "Exported Variables - END" << "\n"; 00308 } 00309 00310 // Emit initialized data placed in ROM. 00311 void PIC16AsmPrinter::EmitRomData(Module &M) { 00312 // Print ROM Data section. 00313 const std::vector<PIC16Section*> &ROSections = PTOF->ROSections; 00314 for (unsigned i = 0; i < ROSections.size(); i++) { 00315 const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items; 00316 if (!Items.size()) continue; 00317 O << "\n"; 00318 OutStreamer.SwitchSection(PTOF->ROSections[i]->S_); 00319 for (unsigned j = 0; j < Items.size(); j++) { 00320 O << Mang->getMangledName(Items[j]); 00321 Constant *C = Items[j]->getInitializer(); 00322 int AddrSpace = Items[j]->getType()->getAddressSpace(); 00323 EmitGlobalConstant(C, AddrSpace); 00324 } 00325 } 00326 } 00327 00328 bool PIC16AsmPrinter::doFinalization(Module &M) { 00329 printLibcallDecls(); 00330 EmitRemainingAutos(); 00331 DbgInfo.EndModule(M); 00332 O << "\n\t" << "END\n"; 00333 return AsmPrinter::doFinalization(M); 00334 } 00335 00336 void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) { 00337 const Function *F = MF.getFunction(); 00338 std::string FuncName = Mang->getMangledName(F); 00339 const TargetData *TD = TM.getTargetData(); 00340 // Emit the data section name. 00341 O << "\n"; 00342 00343 const MCSection *fPDataSection = 00344 getObjFileLowering().getSectionForFunctionFrame(CurrentFnName); 00345 OutStreamer.SwitchSection(fPDataSection); 00346 00347 // Emit function frame label 00348 O << PAN::getFrameLabel(CurrentFnName) << ":\n"; 00349 00350 const Type *RetType = F->getReturnType(); 00351 unsigned RetSize = 0; 00352 if (RetType->getTypeID() != Type::VoidTyID) 00353 RetSize = TD->getTypeAllocSize(RetType); 00354 00355 //Emit function return value space 00356 // FIXME: Do not emit RetvalLable when retsize is zero. To do this 00357 // we will need to avoid printing a global directive for Retval label 00358 // in emitExternandGloblas. 00359 if(RetSize > 0) 00360 O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n"; 00361 else 00362 O << PAN::getRetvalLabel(CurrentFnName) << ": \n"; 00363 00364 // Emit variable to hold the space for function arguments 00365 unsigned ArgSize = 0; 00366 for (Function::const_arg_iterator argi = F->arg_begin(), 00367 arge = F->arg_end(); argi != arge ; ++argi) { 00368 const Type *Ty = argi->getType(); 00369 ArgSize += TD->getTypeAllocSize(Ty); 00370 } 00371 00372 O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n"; 00373 00374 // Emit temporary space 00375 int TempSize = PTLI->GetTmpSize(); 00376 if (TempSize > 0) 00377 O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n'; 00378 } 00379 00380 void PIC16AsmPrinter::EmitIData(Module &M) { 00381 00382 // Print all IDATA sections. 00383 const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections; 00384 for (unsigned i = 0; i < IDATASections.size(); i++) { 00385 O << "\n"; 00386 if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos) 00387 continue; 00388 OutStreamer.SwitchSection(IDATASections[i]->S_); 00389 std::vector<const GlobalVariable*> Items = IDATASections[i]->Items; 00390 for (unsigned j = 0; j < Items.size(); j++) { 00391 std::string Name = Mang->getMangledName(Items[j]); 00392 Constant *C = Items[j]->getInitializer(); 00393 int AddrSpace = Items[j]->getType()->getAddressSpace(); 00394 O << Name; 00395 EmitGlobalConstant(C, AddrSpace); 00396 } 00397 } 00398 } 00399 00400 void PIC16AsmPrinter::EmitUData(Module &M) { 00401 const TargetData *TD = TM.getTargetData(); 00402 00403 // Print all BSS sections. 00404 const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections; 00405 for (unsigned i = 0; i < BSSSections.size(); i++) { 00406 O << "\n"; 00407 OutStreamer.SwitchSection(BSSSections[i]->S_); 00408 std::vector<const GlobalVariable*> Items = BSSSections[i]->Items; 00409 for (unsigned j = 0; j < Items.size(); j++) { 00410 std::string Name = Mang->getMangledName(Items[j]); 00411 Constant *C = Items[j]->getInitializer(); 00412 const Type *Ty = C->getType(); 00413 unsigned Size = TD->getTypeAllocSize(Ty); 00414 00415 O << Name << " RES " << Size << "\n"; 00416 } 00417 } 00418 } 00419 00420 void PIC16AsmPrinter::EmitAutos(std::string FunctName) { 00421 // Section names for all globals are already set. 00422 const TargetData *TD = TM.getTargetData(); 00423 00424 // Now print Autos section for this function. 00425 std::string SectionName = PAN::getAutosSectionName(FunctName); 00426 const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections; 00427 for (unsigned i = 0; i < AutosSections.size(); i++) { 00428 O << "\n"; 00429 if (AutosSections[i]->S_->getName() == SectionName) { 00430 // Set the printing status to true 00431 AutosSections[i]->setPrintedStatus(true); 00432 OutStreamer.SwitchSection(AutosSections[i]->S_); 00433 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items; 00434 for (unsigned j = 0; j < Items.size(); j++) { 00435 std::string VarName = Mang->getMangledName(Items[j]); 00436 Constant *C = Items[j]->getInitializer(); 00437 const Type *Ty = C->getType(); 00438 unsigned Size = TD->getTypeAllocSize(Ty); 00439 // Emit memory reserve directive. 00440 O << VarName << " RES " << Size << "\n"; 00441 } 00442 break; 00443 } 00444 } 00445 } 00446 00447 // Print autos that were not printed during the code printing of functions. 00448 // As the functions might themselves would have got deleted by the optimizer. 00449 void PIC16AsmPrinter::EmitRemainingAutos() { 00450 const TargetData *TD = TM.getTargetData(); 00451 00452 // Now print Autos section for this function. 00453 std::vector <PIC16Section *>AutosSections = PTOF->AutosSections; 00454 for (unsigned i = 0; i < AutosSections.size(); i++) { 00455 00456 // if the section is already printed then don't print again 00457 if (AutosSections[i]->isPrinted()) 00458 continue; 00459 00460 // Set status as printed 00461 AutosSections[i]->setPrintedStatus(true); 00462 00463 O << "\n"; 00464 OutStreamer.SwitchSection(AutosSections[i]->S_); 00465 const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items; 00466 for (unsigned j = 0; j < Items.size(); j++) { 00467 std::string VarName = Mang->getMangledName(Items[j]); 00468 Constant *C = Items[j]->getInitializer(); 00469 const Type *Ty = C->getType(); 00470 unsigned Size = TD->getTypeAllocSize(Ty); 00471 // Emit memory reserve directive. 00472 O << VarName << " RES " << Size << "\n"; 00473 } 00474 } 00475 } 00476 00477 00478 extern "C" void LLVMInitializePIC16AsmPrinter() { 00479 RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target); 00480 } 00481 00482