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