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