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