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