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