Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 1 | //===-- PIC16AsmPrinter.cpp - PIC16 LLVM assembly writer ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to PIC16 assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PIC16AsmPrinter.h" |
Chris Lattner | c077621 | 2009-08-15 06:13:40 +0000 | [diff] [blame] | 16 | #include "MCSectionPIC16.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 17 | #include "PIC16MCAsmInfo.h" |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/CodeGen/DwarfWriter.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/DwarfWriter.h" |
| 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCStreamer.h" |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegistry.h" |
| 27 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/FormattedStream.h" |
| 30 | #include "llvm/Support/Mangler.h" |
| 31 | #include <cstring> |
| 32 | using namespace llvm; |
| 33 | |
| 34 | #include "PIC16GenAsmWriter.inc" |
| 35 | |
| 36 | PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 37 | const MCAsmInfo *T, bool V) |
Chris Lattner | e4d110b | 2009-08-22 20:56:12 +0000 | [diff] [blame] | 38 | : AsmPrinter(O, TM, T, V), DbgInfo(O, T) { |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 39 | PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering()); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 40 | PMAI = static_cast<const PIC16MCAsmInfo*>(T); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 41 | PTOF = (PIC16TargetObjectFile*)&PTLI->getObjFileLowering(); |
| 42 | } |
| 43 | |
| 44 | bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Chris Lattner | c5ea263 | 2009-09-09 23:14:36 +0000 | [diff] [blame^] | 45 | processDebugLoc(MI->getDebugLoc()); |
| 46 | |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 47 | printInstruction(MI); |
Chris Lattner | c5ea263 | 2009-09-09 23:14:36 +0000 | [diff] [blame^] | 48 | |
| 49 | if (VerboseAsm && !MI->getDebugLoc().isUnknown()) |
| 50 | EmitComments(*MI); |
| 51 | O << '\n'; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /// runOnMachineFunction - This emits the frame section, autos section and |
| 56 | /// assembly for each instruction. Also takes care of function begin debug |
| 57 | /// directive and file begin debug directive (if required) for the function. |
| 58 | /// |
| 59 | bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 60 | this->MF = &MF; |
| 61 | |
| 62 | // This calls the base class function required to be called at beginning |
| 63 | // of runOnMachineFunction. |
| 64 | SetupMachineFunction(MF); |
| 65 | |
| 66 | // Get the mangled name. |
| 67 | const Function *F = MF.getFunction(); |
| 68 | CurrentFnName = Mang->getMangledName(F); |
| 69 | |
| 70 | // Emit the function frame (args and temps). |
| 71 | EmitFunctionFrame(MF); |
| 72 | |
| 73 | DbgInfo.BeginFunction(MF); |
| 74 | |
| 75 | // Emit the autos section of function. |
Chris Lattner | ec3579f | 2009-08-21 23:08:09 +0000 | [diff] [blame] | 76 | EmitAutos(CurrentFnName); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 77 | |
| 78 | // Now emit the instructions of function in its code section. |
| 79 | const MCSection *fCodeSection = |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 80 | getObjFileLowering().getSectionForFunction(CurrentFnName); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 81 | // Start the Code Section. |
| 82 | O << "\n"; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 83 | OutStreamer.SwitchSection(fCodeSection); |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 84 | |
| 85 | // Emit the frame address of the function at the beginning of code. |
| 86 | O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; |
| 87 | O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 88 | |
| 89 | // Emit function start label. |
| 90 | O << CurrentFnName << ":\n"; |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 91 | |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 92 | DebugLoc CurDL; |
| 93 | O << "\n"; |
| 94 | // Print out code for the function. |
| 95 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 96 | I != E; ++I) { |
| 97 | |
| 98 | // Print a label for the basic block. |
| 99 | if (I != MF.begin()) { |
| 100 | printBasicBlockLabel(I, true); |
| 101 | O << '\n'; |
| 102 | } |
| 103 | |
| 104 | // Print a basic block. |
| 105 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 106 | II != E; ++II) { |
| 107 | |
| 108 | // Emit the line directive if source line changed. |
| 109 | const DebugLoc DL = II->getDebugLoc(); |
| 110 | if (!DL.isUnknown() && DL != CurDL) { |
| 111 | DbgInfo.ChangeDebugLoc(MF, DL); |
| 112 | CurDL = DL; |
| 113 | } |
| 114 | |
| 115 | // Print the assembly for the instruction. |
| 116 | printMachineInstruction(II); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Emit function end debug directives. |
| 121 | DbgInfo.EndFunction(MF); |
| 122 | |
| 123 | return false; // we didn't modify anything. |
| 124 | } |
| 125 | |
| 126 | |
| 127 | // printOperand - print operand of insn. |
| 128 | void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
| 129 | const MachineOperand &MO = MI->getOperand(opNum); |
| 130 | |
| 131 | switch (MO.getType()) { |
| 132 | case MachineOperand::MO_Register: |
| 133 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 134 | O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; |
| 135 | else |
| 136 | llvm_unreachable("not implemented"); |
| 137 | return; |
| 138 | |
| 139 | case MachineOperand::MO_Immediate: |
| 140 | O << (int)MO.getImm(); |
| 141 | return; |
| 142 | |
| 143 | case MachineOperand::MO_GlobalAddress: { |
| 144 | std::string Sname = Mang->getMangledName(MO.getGlobal()); |
| 145 | // FIXME: currently we do not have a memcpy def coming in the module |
| 146 | // by any chance, as we do not link in those as .bc lib. So these calls |
| 147 | // are always external and it is safe to emit an extern. |
| 148 | if (PAN::isMemIntrinsic(Sname)) { |
| 149 | LibcallDecls.push_back(createESName(Sname)); |
| 150 | } |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 151 | |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 152 | O << Sname; |
| 153 | break; |
| 154 | } |
| 155 | case MachineOperand::MO_ExternalSymbol: { |
| 156 | const char *Sname = MO.getSymbolName(); |
| 157 | |
| 158 | // If its a libcall name, record it to decls section. |
| 159 | if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) { |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 160 | LibcallDecls.push_back(Sname); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Record a call to intrinsic to print the extern declaration for it. |
| 164 | std::string Sym = Sname; |
| 165 | if (PAN::isMemIntrinsic(Sym)) { |
| 166 | Sym = PAN::addPrefix(Sym); |
| 167 | LibcallDecls.push_back(createESName(Sym)); |
| 168 | } |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 169 | |
| 170 | O << Sym; |
| 171 | break; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 172 | } |
| 173 | case MachineOperand::MO_MachineBasicBlock: |
| 174 | printBasicBlockLabel(MO.getMBB()); |
| 175 | return; |
| 176 | |
| 177 | default: |
| 178 | llvm_unreachable(" Operand type not supported."); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /// printCCOperand - Print the cond code operand. |
| 183 | /// |
| 184 | void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { |
| 185 | int CC = (int)MI->getOperand(opNum).getImm(); |
| 186 | O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); |
| 187 | } |
| 188 | |
| 189 | // This function is used to sort the decls list. |
| 190 | // should return true if s1 should come before s2. |
| 191 | static bool is_before(const char *s1, const char *s2) { |
| 192 | return strcmp(s1, s2) <= 0; |
| 193 | } |
| 194 | |
| 195 | // This is used by list::unique below. |
| 196 | // unique will filter out duplicates if it knows them. |
| 197 | static bool is_duplicate(const char *s1, const char *s2) { |
| 198 | return !strcmp(s1, s2); |
| 199 | } |
| 200 | |
| 201 | /// printLibcallDecls - print the extern declarations for compiler |
| 202 | /// intrinsics. |
| 203 | /// |
| 204 | void PIC16AsmPrinter::printLibcallDecls() { |
| 205 | // If no libcalls used, return. |
| 206 | if (LibcallDecls.empty()) return; |
| 207 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 208 | O << MAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 209 | // Remove duplicate entries. |
| 210 | LibcallDecls.sort(is_before); |
| 211 | LibcallDecls.unique(is_duplicate); |
| 212 | |
| 213 | for (std::list<const char*>::const_iterator I = LibcallDecls.begin(); |
| 214 | I != LibcallDecls.end(); I++) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 215 | O << MAI->getExternDirective() << *I << "\n"; |
| 216 | O << MAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n"; |
| 217 | O << MAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 219 | O << MAI->getCommentString() << "External decls for libcalls - END." <<"\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /// doInitialization - Perfrom Module level initializations here. |
| 223 | /// One task that we do here is to sectionize all global variables. |
| 224 | /// The MemSelOptimizer pass depends on the sectionizing. |
| 225 | /// |
| 226 | bool PIC16AsmPrinter::doInitialization(Module &M) { |
| 227 | bool Result = AsmPrinter::doInitialization(M); |
| 228 | |
| 229 | // FIXME:: This is temporary solution to generate the include file. |
| 230 | // The processor should be passed to llc as in input and the header file |
| 231 | // should be generated accordingly. |
| 232 | O << "\n\t#include P16F1937.INC\n"; |
| 233 | |
| 234 | // Set the section names for all globals. |
| 235 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 236 | I != E; ++I) |
| 237 | if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage()) { |
| 238 | const MCSection *S = getObjFileLowering().SectionForGlobal(I, Mang, TM); |
| 239 | |
| 240 | I->setSection(((const MCSectionPIC16*)S)->getName()); |
| 241 | } |
| 242 | |
| 243 | DbgInfo.BeginModule(M); |
| 244 | EmitFunctionDecls(M); |
| 245 | EmitUndefinedVars(M); |
| 246 | EmitDefinedVars(M); |
| 247 | EmitIData(M); |
| 248 | EmitUData(M); |
| 249 | EmitRomData(M); |
| 250 | return Result; |
| 251 | } |
| 252 | |
| 253 | /// Emit extern decls for functions imported from other modules, and emit |
| 254 | /// global declarations for function defined in this module and which are |
| 255 | /// available to other modules. |
| 256 | /// |
| 257 | void PIC16AsmPrinter::EmitFunctionDecls(Module &M) { |
| 258 | // Emit declarations for external functions. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 259 | O <<"\n"<<MAI->getCommentString() << "Function Declarations - BEGIN." <<"\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 260 | for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) { |
| 261 | if (I->isIntrinsic()) |
| 262 | continue; |
| 263 | |
| 264 | std::string Name = Mang->getMangledName(I); |
| 265 | if (Name.compare("@abort") == 0) |
| 266 | continue; |
| 267 | |
| 268 | if (!I->isDeclaration() && !I->hasExternalLinkage()) |
| 269 | continue; |
| 270 | |
| 271 | // Do not emit memcpy, memset, and memmove here. |
| 272 | // Calls to these routines can be generated in two ways, |
| 273 | // 1. User calling the standard lib function |
| 274 | // 2. Codegen generating these calls for llvm intrinsics. |
| 275 | // In the first case a prototype is alread availale, while in |
| 276 | // second case the call is via and externalsym and the prototype is missing. |
| 277 | // So declarations for these are currently always getting printing by |
| 278 | // tracking both kind of references in printInstrunction. |
| 279 | if (I->isDeclaration() && PAN::isMemIntrinsic(Name)) continue; |
| 280 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 281 | const char *directive = I->isDeclaration() ? MAI->getExternDirective() : |
| 282 | MAI->getGlobalDirective(); |
Chris Lattner | 35a27c8 | 2009-08-21 23:12:15 +0000 | [diff] [blame] | 283 | |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 284 | O << directive << Name << "\n"; |
| 285 | O << directive << PAN::getRetvalLabel(Name) << "\n"; |
| 286 | O << directive << PAN::getArgsLabel(Name) << "\n"; |
| 287 | } |
| 288 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 289 | O << MAI->getCommentString() << "Function Declarations - END." <<"\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // Emit variables imported from other Modules. |
| 293 | void PIC16AsmPrinter::EmitUndefinedVars(Module &M) { |
| 294 | std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDecls->Items; |
| 295 | if (!Items.size()) return; |
| 296 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 297 | O << "\n" << MAI->getCommentString() << "Imported Variables - BEGIN" << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 298 | for (unsigned j = 0; j < Items.size(); j++) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 299 | O << MAI->getExternDirective() << Mang->getMangledName(Items[j]) << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 301 | O << MAI->getCommentString() << "Imported Variables - END" << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Emit variables defined in this module and are available to other modules. |
| 305 | void PIC16AsmPrinter::EmitDefinedVars(Module &M) { |
| 306 | std::vector<const GlobalVariable*> Items = PTOF->ExternalVarDefs->Items; |
| 307 | if (!Items.size()) return; |
| 308 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 309 | O << "\n" << MAI->getCommentString() << "Exported Variables - BEGIN" << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 310 | for (unsigned j = 0; j < Items.size(); j++) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 311 | O << MAI->getGlobalDirective() << Mang->getMangledName(Items[j]) << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 313 | O << MAI->getCommentString() << "Exported Variables - END" << "\n"; |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Emit initialized data placed in ROM. |
| 317 | void PIC16AsmPrinter::EmitRomData(Module &M) { |
| 318 | // Print ROM Data section. |
| 319 | const std::vector<PIC16Section*> &ROSections = PTOF->ROSections; |
| 320 | for (unsigned i = 0; i < ROSections.size(); i++) { |
| 321 | const std::vector<const GlobalVariable*> &Items = ROSections[i]->Items; |
| 322 | if (!Items.size()) continue; |
| 323 | O << "\n"; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 324 | OutStreamer.SwitchSection(PTOF->ROSections[i]->S_); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 325 | for (unsigned j = 0; j < Items.size(); j++) { |
| 326 | O << Mang->getMangledName(Items[j]); |
| 327 | Constant *C = Items[j]->getInitializer(); |
| 328 | int AddrSpace = Items[j]->getType()->getAddressSpace(); |
| 329 | EmitGlobalConstant(C, AddrSpace); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | bool PIC16AsmPrinter::doFinalization(Module &M) { |
| 335 | printLibcallDecls(); |
| 336 | EmitRemainingAutos(); |
| 337 | DbgInfo.EndModule(M); |
| 338 | O << "\n\t" << "END\n"; |
| 339 | return AsmPrinter::doFinalization(M); |
| 340 | } |
| 341 | |
| 342 | void PIC16AsmPrinter::EmitFunctionFrame(MachineFunction &MF) { |
| 343 | const Function *F = MF.getFunction(); |
| 344 | std::string FuncName = Mang->getMangledName(F); |
| 345 | const TargetData *TD = TM.getTargetData(); |
| 346 | // Emit the data section name. |
| 347 | O << "\n"; |
Chris Lattner | ec3579f | 2009-08-21 23:08:09 +0000 | [diff] [blame] | 348 | |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 349 | const MCSection *fPDataSection = |
Chris Lattner | ec3579f | 2009-08-21 23:08:09 +0000 | [diff] [blame] | 350 | getObjFileLowering().getSectionForFunctionFrame(CurrentFnName); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 351 | OutStreamer.SwitchSection(fPDataSection); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 352 | |
| 353 | // Emit function frame label |
| 354 | O << PAN::getFrameLabel(CurrentFnName) << ":\n"; |
| 355 | |
| 356 | const Type *RetType = F->getReturnType(); |
| 357 | unsigned RetSize = 0; |
| 358 | if (RetType->getTypeID() != Type::VoidTyID) |
| 359 | RetSize = TD->getTypeAllocSize(RetType); |
| 360 | |
| 361 | //Emit function return value space |
| 362 | // FIXME: Do not emit RetvalLable when retsize is zero. To do this |
| 363 | // we will need to avoid printing a global directive for Retval label |
| 364 | // in emitExternandGloblas. |
| 365 | if(RetSize > 0) |
| 366 | O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n"; |
| 367 | else |
| 368 | O << PAN::getRetvalLabel(CurrentFnName) << ": \n"; |
| 369 | |
| 370 | // Emit variable to hold the space for function arguments |
| 371 | unsigned ArgSize = 0; |
| 372 | for (Function::const_arg_iterator argi = F->arg_begin(), |
| 373 | arge = F->arg_end(); argi != arge ; ++argi) { |
| 374 | const Type *Ty = argi->getType(); |
| 375 | ArgSize += TD->getTypeAllocSize(Ty); |
| 376 | } |
| 377 | |
| 378 | O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n"; |
| 379 | |
| 380 | // Emit temporary space |
| 381 | int TempSize = PTLI->GetTmpSize(); |
| 382 | if (TempSize > 0) |
| 383 | O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize << '\n'; |
| 384 | } |
| 385 | |
| 386 | void PIC16AsmPrinter::EmitIData(Module &M) { |
| 387 | |
| 388 | // Print all IDATA sections. |
| 389 | const std::vector<PIC16Section*> &IDATASections = PTOF->IDATASections; |
| 390 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
| 391 | O << "\n"; |
| 392 | if (IDATASections[i]->S_->getName().find("llvm.") != std::string::npos) |
| 393 | continue; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 394 | OutStreamer.SwitchSection(IDATASections[i]->S_); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 395 | std::vector<const GlobalVariable*> Items = IDATASections[i]->Items; |
| 396 | for (unsigned j = 0; j < Items.size(); j++) { |
| 397 | std::string Name = Mang->getMangledName(Items[j]); |
| 398 | Constant *C = Items[j]->getInitializer(); |
| 399 | int AddrSpace = Items[j]->getType()->getAddressSpace(); |
| 400 | O << Name; |
| 401 | EmitGlobalConstant(C, AddrSpace); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void PIC16AsmPrinter::EmitUData(Module &M) { |
| 407 | const TargetData *TD = TM.getTargetData(); |
| 408 | |
| 409 | // Print all BSS sections. |
| 410 | const std::vector<PIC16Section*> &BSSSections = PTOF->BSSSections; |
| 411 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
| 412 | O << "\n"; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 413 | OutStreamer.SwitchSection(BSSSections[i]->S_); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 414 | std::vector<const GlobalVariable*> Items = BSSSections[i]->Items; |
| 415 | for (unsigned j = 0; j < Items.size(); j++) { |
| 416 | std::string Name = Mang->getMangledName(Items[j]); |
| 417 | Constant *C = Items[j]->getInitializer(); |
| 418 | const Type *Ty = C->getType(); |
| 419 | unsigned Size = TD->getTypeAllocSize(Ty); |
| 420 | |
| 421 | O << Name << " RES " << Size << "\n"; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | ec3579f | 2009-08-21 23:08:09 +0000 | [diff] [blame] | 426 | void PIC16AsmPrinter::EmitAutos(std::string FunctName) { |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 427 | // Section names for all globals are already set. |
| 428 | const TargetData *TD = TM.getTargetData(); |
| 429 | |
| 430 | // Now print Autos section for this function. |
Chris Lattner | ec3579f | 2009-08-21 23:08:09 +0000 | [diff] [blame] | 431 | std::string SectionName = PAN::getAutosSectionName(FunctName); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 432 | const std::vector<PIC16Section*> &AutosSections = PTOF->AutosSections; |
| 433 | for (unsigned i = 0; i < AutosSections.size(); i++) { |
| 434 | O << "\n"; |
| 435 | if (AutosSections[i]->S_->getName() == SectionName) { |
| 436 | // Set the printing status to true |
| 437 | AutosSections[i]->setPrintedStatus(true); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 438 | OutStreamer.SwitchSection(AutosSections[i]->S_); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 439 | const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items; |
| 440 | for (unsigned j = 0; j < Items.size(); j++) { |
| 441 | std::string VarName = Mang->getMangledName(Items[j]); |
| 442 | Constant *C = Items[j]->getInitializer(); |
| 443 | const Type *Ty = C->getType(); |
| 444 | unsigned Size = TD->getTypeAllocSize(Ty); |
| 445 | // Emit memory reserve directive. |
| 446 | O << VarName << " RES " << Size << "\n"; |
| 447 | } |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // Print autos that were not printed during the code printing of functions. |
| 454 | // As the functions might themselves would have got deleted by the optimizer. |
| 455 | void PIC16AsmPrinter::EmitRemainingAutos() { |
| 456 | const TargetData *TD = TM.getTargetData(); |
| 457 | |
| 458 | // Now print Autos section for this function. |
| 459 | std::vector <PIC16Section *>AutosSections = PTOF->AutosSections; |
| 460 | for (unsigned i = 0; i < AutosSections.size(); i++) { |
| 461 | |
| 462 | // if the section is already printed then don't print again |
| 463 | if (AutosSections[i]->isPrinted()) |
| 464 | continue; |
| 465 | |
| 466 | // Set status as printed |
| 467 | AutosSections[i]->setPrintedStatus(true); |
| 468 | |
| 469 | O << "\n"; |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 470 | OutStreamer.SwitchSection(AutosSections[i]->S_); |
Sanjiv Gupta | 2780609 | 2009-08-13 16:37:05 +0000 | [diff] [blame] | 471 | const std::vector<const GlobalVariable*> &Items = AutosSections[i]->Items; |
| 472 | for (unsigned j = 0; j < Items.size(); j++) { |
| 473 | std::string VarName = Mang->getMangledName(Items[j]); |
| 474 | Constant *C = Items[j]->getInitializer(); |
| 475 | const Type *Ty = C->getType(); |
| 476 | unsigned Size = TD->getTypeAllocSize(Ty); |
| 477 | // Emit memory reserve directive. |
| 478 | O << VarName << " RES " << Size << "\n"; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | extern "C" void LLVMInitializePIC16AsmPrinter() { |
| 485 | RegisterAsmPrinter<PIC16AsmPrinter> X(ThePIC16Target); |
| 486 | } |
| 487 | |
| 488 | |