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