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