Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +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 | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 15 | #include "PIC16AsmPrinter.h" |
| 16 | #include "PIC16TargetAsmInfo.h" |
Bill Wendling | cb819f1 | 2009-02-18 23:12:06 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Module.h" |
Bill Wendling | cb819f1 | 2009-02-18 23:12:06 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/DwarfWriter.h" |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Bill Wendling | cb819f1 | 2009-02-18 23:12:06 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include "llvm/Support/Mangler.h" |
Sanjiv Gupta | 5274a4a | 2009-04-02 18:03:10 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/DwarfWriter.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 29 | #include "PIC16GenAsmWriter.inc" |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 30 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 31 | inline static bool isLocalToFunc (std::string &FuncName, std::string &VarName) { |
| 32 | if (VarName.find(FuncName + ".auto.") != std::string::npos |
| 33 | || VarName.find(FuncName + ".arg.") != std::string::npos) |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 34 | return true; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 35 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
| 39 | inline static bool isLocalName (std::string &Name) { |
| 40 | if (Name.find(".auto.") != std::string::npos |
| 41 | || Name.find(".arg.") != std::string::npos) |
| 42 | return true; |
| 43 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 46 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 47 | bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 48 | std::string NewBank = ""; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 49 | unsigned Operands = MI->getNumOperands(); |
| 50 | if (Operands > 1) { |
| 51 | // Global address or external symbol should be second operand from last |
| 52 | // if we want to print banksel for it. |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 53 | unsigned BankSelVar = Operands - 2; |
| 54 | // In cases where an instruction has a def or use defined in td file, |
| 55 | // that def or use becomes a machine instruction operand. |
| 56 | // eg. addfw_1 instruction defines STATUS register. So the machine |
| 57 | // instruction for it has MO_Register Operand as its last operand. |
| 58 | while ((MI->getOperand(BankSelVar + 1).getType() == |
| 59 | MachineOperand::MO_Register) && (BankSelVar > 0)) |
| 60 | BankSelVar--; |
| 61 | const MachineOperand &Op = MI->getOperand(BankSelVar); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 62 | unsigned OpType = Op.getType(); |
| 63 | if (OpType == MachineOperand::MO_GlobalAddress || |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 64 | OpType == MachineOperand::MO_ExternalSymbol) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 65 | if (OpType == MachineOperand::MO_GlobalAddress ) |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 66 | NewBank = Op.getGlobal()->getSection(); |
| 67 | else { |
| 68 | // External Symbol is generated for temp data. Temp data in in |
| 69 | // fdata.<functionname>.# section. |
| 70 | NewBank = "fdata." + CurrentFnName +".#"; |
| 71 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 72 | // Operand after global address or external symbol should be banksel. |
| 73 | // Value 1 for this operand means we need to generate banksel else do not |
| 74 | // generate banksel. |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 75 | const MachineOperand &BS = MI->getOperand(BankSelVar+1); |
| 76 | // If Section names are same then the variables are in same section. |
| 77 | // This is not true for external variables as section names for global |
| 78 | // variables in all files are same at this time. For eg. initialized |
| 79 | // data in put in idata.# section in all files. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 80 | if (((int)BS.getImm() == 1) && |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 81 | ((Op.isGlobal() && Op.getGlobal()->hasExternalLinkage()) || |
| 82 | (NewBank.compare(CurBank) != 0))) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 83 | O << "\tbanksel "; |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 84 | printOperand(MI, BankSelVar); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 85 | O << "\n"; |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 86 | CurBank = NewBank; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | printInstruction(MI); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /// runOnMachineFunction - This uses the printInstruction() |
| 95 | /// method to print assembly for each instruction. |
| 96 | /// |
| 97 | bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 98 | this->MF = &MF; |
| 99 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 100 | // This calls the base class function required to be called at beginning |
| 101 | // of runOnMachineFunction. |
| 102 | SetupMachineFunction(MF); |
| 103 | |
| 104 | // Get the mangled name. |
| 105 | const Function *F = MF.getFunction(); |
| 106 | CurrentFnName = Mang->getValueName(F); |
| 107 | |
| 108 | // Emit the function variables. |
| 109 | emitFunctionData(MF); |
| 110 | std::string codeSection; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 111 | codeSection = "code." + CurrentFnName + ".# " + "CODE"; |
| 112 | const Section *fCodeSection = TAI->getNamedSection(codeSection.c_str(), |
| 113 | SectionFlags::Code); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 114 | O << "\n"; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 115 | SwitchToSection (fCodeSection); |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 116 | |
| 117 | // Emit the frame address of the function at the beginning of code. |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 118 | O << CurrentFnName << ":\n"; |
| 119 | O << " retlw low(" << CurrentFnName << ".frame)\n"; |
| 120 | O << " retlw high(" << CurrentFnName << ".frame)\n"; |
| 121 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 122 | |
| 123 | // Print out code for the function. |
| 124 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 125 | I != E; ++I) { |
| 126 | // Print a label for the basic block. |
| 127 | if (I != MF.begin()) { |
| 128 | printBasicBlockLabel(I, true); |
| 129 | O << '\n'; |
| 130 | } |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 131 | CurBank = ""; |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 132 | |
| 133 | // For emitting line directives, we need to keep track of the current |
| 134 | // source line. When it changes then only emit the line directive. |
| 135 | unsigned CurLine = 0; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 136 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 137 | II != E; ++II) { |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 138 | // Emit the line directive if source line changed. |
| 139 | const DebugLoc DL = II->getDebugLoc(); |
| 140 | if (!DL.isUnknown()) { |
| 141 | unsigned line = MF.getDebugLocTuple(DL).Line; |
| 142 | if (line != CurLine) { |
| 143 | O << "\t.line " << line << "\n"; |
| 144 | CurLine = line; |
| 145 | } |
| 146 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 147 | // Print the assembly for the instruction. |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 148 | printMachineInstruction(II); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return false; // we didn't modify anything. |
| 152 | } |
| 153 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 154 | /// createPIC16CodePrinterPass - Returns a pass that prints the PIC16 |
| 155 | /// assembly code for a MachineFunction to the given output stream, |
| 156 | /// using the given target machine description. This should work |
| 157 | /// regardless of whether the function is in SSA form. |
| 158 | /// |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 159 | FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o, |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 160 | PIC16TargetMachine &tm, |
Evan Cheng | 42bf74b | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 161 | bool fast, bool verbose) { |
| 162 | return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 165 | void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 166 | const MachineOperand &MO = MI->getOperand(opNum); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 167 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 168 | switch (MO.getType()) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 169 | case MachineOperand::MO_Register: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 170 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 171 | O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 172 | else |
| 173 | assert(0 && "not implemented"); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 174 | return; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 175 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 176 | case MachineOperand::MO_Immediate: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 177 | O << (int)MO.getImm(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 178 | return; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 179 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 180 | case MachineOperand::MO_GlobalAddress: |
| 181 | O << Mang->getValueName(MO.getGlobal()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 182 | break; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 183 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 184 | case MachineOperand::MO_ExternalSymbol: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 185 | O << MO.getSymbolName(); |
| 186 | break; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 187 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 188 | case MachineOperand::MO_MachineBasicBlock: |
| 189 | printBasicBlockLabel(MO.getMBB()); |
| 190 | return; |
| 191 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 192 | default: |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 193 | assert(0 && " Operand type not supported."); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 197 | void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { |
| 198 | int CC = (int)MI->getOperand(opNum).getImm(); |
| 199 | O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); |
| 200 | } |
| 201 | |
| 202 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 203 | bool PIC16AsmPrinter::doInitialization (Module &M) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 204 | bool Result = AsmPrinter::doInitialization(M); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 205 | // FIXME:: This is temporary solution to generate the include file. |
| 206 | // The processor should be passed to llc as in input and the header file |
| 207 | // should be generated accordingly. |
| 208 | O << "\t#include P16F1937.INC\n"; |
Sanjiv Gupta | 5274a4a | 2009-04-02 18:03:10 +0000 | [diff] [blame] | 209 | MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 210 | assert(MMI); |
| 211 | DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>(); |
| 212 | assert(DW && "Dwarf Writer is not available"); |
| 213 | DW->BeginModule(&M, MMI, O, this, TAI); |
| 214 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 215 | EmitExternsAndGlobals (M); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 216 | EmitInitData (M); |
| 217 | EmitUnInitData(M); |
| 218 | EmitRomData(M); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 219 | return Result; |
| 220 | } |
| 221 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 222 | void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) { |
| 223 | // Emit declarations for external functions. |
| 224 | O << "section.0" <<"\n"; |
| 225 | for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) { |
| 226 | std::string Name = Mang->getValueName(I); |
| 227 | if (Name.compare("abort") == 0) |
| 228 | continue; |
| 229 | if (I->isDeclaration()) { |
| 230 | O << "\textern " <<Name << "\n"; |
| 231 | O << "\textern " << Name << ".retval\n"; |
| 232 | O << "\textern " << Name << ".args\n"; |
| 233 | } |
| 234 | else if (I->hasExternalLinkage()) { |
| 235 | O << "\tglobal " << Name << "\n"; |
| 236 | O << "\tglobal " << Name << ".retval\n"; |
| 237 | O << "\tglobal " << Name << ".args\n"; |
| 238 | } |
| 239 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 240 | |
| 241 | // Emit header file to include declaration of library functions |
| 242 | O << "\t#include C16IntrinsicCalls.INC\n"; |
| 243 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 244 | // Emit declarations for external globals. |
| 245 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 246 | I != E; I++) { |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 247 | // Any variables reaching here with ".auto." in its name is a local scope |
| 248 | // variable and should not be printed in global data section. |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 249 | std::string Name = Mang->getValueName(I); |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 250 | if (isLocalName (Name)) |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 251 | continue; |
| 252 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 253 | if (I->isDeclaration()) |
| 254 | O << "\textern "<< Name << "\n"; |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 255 | else if (I->hasCommonLinkage() || I->hasExternalLinkage()) |
Sanjiv Gupta | a2d8b06 | 2009-02-06 18:24:59 +0000 | [diff] [blame] | 256 | O << "\tglobal "<< Name << "\n"; |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 259 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 260 | void PIC16AsmPrinter::EmitInitData (Module &M) { |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 261 | SwitchToSection(TAI->getDataSection()); |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 262 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 263 | I != E; ++I) { |
| 264 | if (!I->hasInitializer()) // External global require no code. |
| 265 | continue; |
| 266 | |
| 267 | Constant *C = I->getInitializer(); |
| 268 | const PointerType *PtrTy = I->getType(); |
| 269 | int AddrSpace = PtrTy->getAddressSpace(); |
| 270 | |
| 271 | if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::RAM_SPACE)) { |
| 272 | |
| 273 | if (EmitSpecialLLVMGlobal(I)) |
| 274 | continue; |
| 275 | |
| 276 | // Any variables reaching here with "." in its name is a local scope |
| 277 | // variable and should not be printed in global data section. |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 278 | std::string Name = Mang->getValueName(I); |
| 279 | if (isLocalName(Name)) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 280 | continue; |
| 281 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 282 | I->setSection(TAI->getDataSection()->getName()); |
| 283 | O << Name; |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 284 | EmitGlobalConstant(C, AddrSpace); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 289 | void PIC16AsmPrinter::EmitRomData (Module &M) |
| 290 | { |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 291 | SwitchToSection(TAI->getReadOnlySection()); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 292 | IsRomData = true; |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 293 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 294 | I != E; ++I) { |
| 295 | if (!I->hasInitializer()) // External global require no code. |
| 296 | continue; |
| 297 | |
| 298 | Constant *C = I->getInitializer(); |
| 299 | const PointerType *PtrTy = I->getType(); |
| 300 | int AddrSpace = PtrTy->getAddressSpace(); |
| 301 | if ((!C->isNullValue()) && (AddrSpace == PIC16ISD::ROM_SPACE)) { |
| 302 | |
| 303 | if (EmitSpecialLLVMGlobal(I)) |
| 304 | continue; |
| 305 | |
| 306 | // Any variables reaching here with "." in its name is a local scope |
| 307 | // variable and should not be printed in global data section. |
| 308 | std::string name = Mang->getValueName(I); |
| 309 | if (name.find(".") != std::string::npos) |
| 310 | continue; |
| 311 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 312 | I->setSection(TAI->getReadOnlySection()->getName()); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 313 | O << name; |
Sanjiv Gupta | c8d7bc8 | 2009-01-30 04:25:10 +0000 | [diff] [blame] | 314 | EmitGlobalConstant(C, AddrSpace); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 315 | O << "\n"; |
| 316 | } |
| 317 | } |
| 318 | IsRomData = false; |
| 319 | } |
| 320 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 321 | void PIC16AsmPrinter::EmitUnInitData (Module &M) |
| 322 | { |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 323 | SwitchToSection(TAI->getBSSSection_()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 324 | const TargetData *TD = TM.getTargetData(); |
| 325 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 326 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 327 | I != E; ++I) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 328 | if (!I->hasInitializer()) // External global require no code. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 329 | continue; |
| 330 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 331 | Constant *C = I->getInitializer(); |
| 332 | if (C->isNullValue()) { |
| 333 | |
| 334 | if (EmitSpecialLLVMGlobal(I)) |
| 335 | continue; |
| 336 | |
| 337 | // Any variables reaching here with "." in its name is a local scope |
| 338 | // variable and should not be printed in global data section. |
| 339 | std::string name = Mang->getValueName(I); |
| 340 | if (name.find(".") != std::string::npos) |
| 341 | continue; |
| 342 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 343 | I->setSection(TAI->getBSSSection_()->getName()); |
| 344 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 345 | const Type *Ty = C->getType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 346 | unsigned Size = TD->getTypePaddedSize(Ty); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 347 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 348 | O << name << " " <<"RES"<< " " << Size ; |
| 349 | O << "\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 350 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 353 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 354 | bool PIC16AsmPrinter::doFinalization(Module &M) { |
| 355 | O << "\t" << "END\n"; |
| 356 | bool Result = AsmPrinter::doFinalization(M); |
| 357 | return Result; |
| 358 | } |
| 359 | |
| 360 | void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) { |
| 361 | const Function *F = MF.getFunction(); |
| 362 | std::string FuncName = Mang->getValueName(F); |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 363 | Module *M = const_cast<Module *>(F->getParent()); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 364 | const TargetData *TD = TM.getTargetData(); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 365 | unsigned FrameSize = 0; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 366 | // Emit the data section name. |
| 367 | O << "\n"; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 368 | std::string SectionName = "fdata." + CurrentFnName + ".# " + "UDATA"; |
| 369 | |
| 370 | const Section *fDataSection = TAI->getNamedSection(SectionName.c_str(), |
| 371 | SectionFlags::Writeable); |
| 372 | SwitchToSection(fDataSection); |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 373 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 374 | |
| 375 | // Emit function frame label |
| 376 | O << CurrentFnName << ".frame:\n"; |
| 377 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 378 | const Type *RetType = F->getReturnType(); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 379 | unsigned RetSize = 0; |
| 380 | if (RetType->getTypeID() != Type::VoidTyID) |
| 381 | RetSize = TD->getTypePaddedSize(RetType); |
| 382 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 383 | //Emit function return value space |
| 384 | if(RetSize > 0) |
| 385 | O << CurrentFnName << ".retval RES " << RetSize << "\n"; |
| 386 | else |
| 387 | O << CurrentFnName << ".retval:\n"; |
| 388 | |
| 389 | // Emit variable to hold the space for function arguments |
| 390 | unsigned ArgSize = 0; |
| 391 | for (Function::const_arg_iterator argi = F->arg_begin(), |
| 392 | arge = F->arg_end(); argi != arge ; ++argi) { |
| 393 | const Type *Ty = argi->getType(); |
| 394 | ArgSize += TD->getTypePaddedSize(Ty); |
| 395 | } |
| 396 | O << CurrentFnName << ".args RES " << ArgSize << "\n"; |
| 397 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 398 | // Emit the function variables. |
| 399 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 400 | // In PIC16 all the function arguments and local variables are global. |
| 401 | // Therefore to get the variable belonging to this function entire |
| 402 | // global list will be traversed and variables belonging to this function |
| 403 | // will be emitted in the current data section. |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 404 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 405 | I != E; ++I) { |
| 406 | std::string VarName = Mang->getValueName(I); |
| 407 | |
| 408 | // The variables of a function are of form FuncName.* . If this variable |
| 409 | // does not belong to this function then continue. |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 410 | // Static local varilabes of a function does not have .auto. in their |
| 411 | // name. They are not printed as part of function data but module |
| 412 | // level global data. |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 413 | if (! isLocalToFunc(FuncName, VarName)) |
| 414 | continue; |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 415 | |
Sanjiv Gupta | d076570 | 2009-03-12 02:10:45 +0000 | [diff] [blame] | 416 | I->setSection("fdata." + CurrentFnName + ".#"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 417 | Constant *C = I->getInitializer(); |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 418 | const Type *Ty = C->getType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 419 | unsigned Size = TD->getTypePaddedSize(Ty); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 420 | FrameSize += Size; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 421 | // Emit memory reserve directive. |
| 422 | O << VarName << " RES " << Size << "\n"; |
| 423 | } |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 424 | |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 425 | int TempSize = PTLI->GetTmpSize(); |
| 426 | if (TempSize > 0 ) |
| 427 | O << CurrentFnName << ".tmp RES " << TempSize <<"\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 428 | } |