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