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