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 | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 31 | bool PIC16AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 32 | printInstruction(MI); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | /// runOnMachineFunction - This uses the printInstruction() |
| 37 | /// method to print assembly for each instruction. |
| 38 | /// |
| 39 | bool PIC16AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 40 | this->MF = &MF; |
| 41 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 42 | // This calls the base class function required to be called at beginning |
| 43 | // of runOnMachineFunction. |
| 44 | SetupMachineFunction(MF); |
| 45 | |
| 46 | // Get the mangled name. |
| 47 | const Function *F = MF.getFunction(); |
| 48 | CurrentFnName = Mang->getValueName(F); |
| 49 | |
| 50 | // Emit the function variables. |
| 51 | emitFunctionData(MF); |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 52 | const char *codeSection = PAN::getCodeSectionName(CurrentFnName).c_str(); |
| 53 | |
| 54 | const Section *fCodeSection = TAI->getNamedSection(codeSection, |
| 55 | SectionFlags::Code); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 56 | O << "\n"; |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 57 | // Start the Code Section. |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 58 | SwitchToSection (fCodeSection); |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 59 | |
| 60 | // Emit the frame address of the function at the beginning of code. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 61 | O << "\tretlw low(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; |
| 62 | O << "\tretlw high(" << PAN::getFrameLabel(CurrentFnName) << ")\n"; |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 63 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 64 | // Emit function start label. |
| 65 | O << CurrentFnName << ":\n"; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 66 | |
| 67 | // Print out code for the function. |
| 68 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 69 | I != E; ++I) { |
| 70 | // Print a label for the basic block. |
| 71 | if (I != MF.begin()) { |
| 72 | printBasicBlockLabel(I, true); |
| 73 | O << '\n'; |
| 74 | } |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 75 | |
| 76 | // For emitting line directives, we need to keep track of the current |
| 77 | // source line. When it changes then only emit the line directive. |
| 78 | unsigned CurLine = 0; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 79 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 80 | II != E; ++II) { |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 81 | // Emit the line directive if source line changed. |
| 82 | const DebugLoc DL = II->getDebugLoc(); |
| 83 | if (!DL.isUnknown()) { |
| 84 | unsigned line = MF.getDebugLocTuple(DL).Line; |
| 85 | if (line != CurLine) { |
| 86 | O << "\t.line " << line << "\n"; |
| 87 | CurLine = line; |
| 88 | } |
| 89 | } |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 90 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 91 | // Print the assembly for the instruction. |
Sanjiv Gupta | c1fa70c | 2009-04-08 06:24:04 +0000 | [diff] [blame] | 92 | printMachineInstruction(II); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | return false; // we didn't modify anything. |
| 96 | } |
| 97 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 98 | /// createPIC16CodePrinterPass - Returns a pass that prints the PIC16 |
| 99 | /// assembly code for a MachineFunction to the given output stream, |
| 100 | /// using the given target machine description. This should work |
| 101 | /// regardless of whether the function is in SSA form. |
| 102 | /// |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 103 | FunctionPass *llvm::createPIC16CodePrinterPass(raw_ostream &o, |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 104 | PIC16TargetMachine &tm, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 105 | CodeGenOpt::Level OptLevel, |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 106 | bool verbose) { |
| 107 | return new PIC16AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 110 | |
| 111 | // printOperand - print operand of insn. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 112 | void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 113 | const MachineOperand &MO = MI->getOperand(opNum); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 114 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 115 | switch (MO.getType()) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 116 | case MachineOperand::MO_Register: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 117 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 118 | O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 119 | else |
| 120 | assert(0 && "not implemented"); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 121 | return; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 122 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 123 | case MachineOperand::MO_Immediate: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 124 | O << (int)MO.getImm(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 125 | return; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 126 | |
Sanjiv Gupta | 2bdf490 | 2009-04-20 16:59:35 +0000 | [diff] [blame] | 127 | case MachineOperand::MO_GlobalAddress: { |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 128 | O << Mang->getValueName(MO.getGlobal()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 129 | break; |
Sanjiv Gupta | 2bdf490 | 2009-04-20 16:59:35 +0000 | [diff] [blame] | 130 | } |
| 131 | case MachineOperand::MO_ExternalSymbol: { |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 132 | const char *Sname = MO.getSymbolName(); |
| 133 | |
| 134 | // If its a libcall name, record it to decls section. |
| 135 | if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) { |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 136 | LibcallDecls.push_back(Sname); |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | O << Sname; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 140 | break; |
Sanjiv Gupta | 2bdf490 | 2009-04-20 16:59:35 +0000 | [diff] [blame] | 141 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 142 | case MachineOperand::MO_MachineBasicBlock: |
| 143 | printBasicBlockLabel(MO.getMBB()); |
| 144 | return; |
| 145 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 146 | default: |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 147 | assert(0 && " Operand type not supported."); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 151 | void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { |
| 152 | int CC = (int)MI->getOperand(opNum).getImm(); |
| 153 | O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); |
| 154 | } |
| 155 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 156 | void PIC16AsmPrinter::printLibcallDecls(void) { |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 157 | // If no libcalls used, return. |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 158 | if (LibcallDecls.empty()) return; |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 159 | |
Sanjiv Gupta | 4291857 | 2009-05-12 06:52:41 +0000 | [diff] [blame] | 160 | O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n"; |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 161 | // Remove duplicate entries. |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 162 | LibcallDecls.sort(); |
| 163 | LibcallDecls.unique(); |
| 164 | for (std::list<const char*>::const_iterator I = LibcallDecls.begin(); |
| 165 | I != LibcallDecls.end(); I++) { |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 166 | O << TAI->getExternDirective() << *I << "\n"; |
Sanjiv Gupta | e0b4b0e | 2009-05-11 08:52:04 +0000 | [diff] [blame] | 167 | O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n"; |
| 168 | O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n"; |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 169 | } |
Sanjiv Gupta | 4291857 | 2009-05-12 06:52:41 +0000 | [diff] [blame] | 170 | O << TAI->getCommentString() << "External decls for libcalls - END." <<"\n"; |
Sanjiv Gupta | 0608b49 | 2009-05-11 06:01:38 +0000 | [diff] [blame] | 171 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 172 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 173 | bool PIC16AsmPrinter::doInitialization (Module &M) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 174 | bool Result = AsmPrinter::doInitialization(M); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 175 | // FIXME:: This is temporary solution to generate the include file. |
| 176 | // The processor should be passed to llc as in input and the header file |
| 177 | // should be generated accordingly. |
| 178 | O << "\t#include P16F1937.INC\n"; |
Sanjiv Gupta | 5274a4a | 2009-04-02 18:03:10 +0000 | [diff] [blame] | 179 | MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 180 | assert(MMI); |
| 181 | DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>(); |
| 182 | assert(DW && "Dwarf Writer is not available"); |
| 183 | DW->BeginModule(&M, MMI, O, this, TAI); |
| 184 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 185 | // Set the section names for all globals. |
| 186 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 187 | I != E; ++I) { |
| 188 | I->setSection(TAI->SectionForGlobal(I)->getName()); |
| 189 | } |
| 190 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 191 | EmitFunctionDecls(M); |
| 192 | EmitUndefinedVars(M); |
| 193 | EmitDefinedVars(M); |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 194 | EmitIData(M); |
| 195 | EmitUData(M); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 196 | EmitRomData(M); |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 197 | EmitAutos(M); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 198 | return Result; |
| 199 | } |
| 200 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 201 | // Emit extern decls for functions imported from other modules, and emit |
| 202 | // global declarations for function defined in this module and which are |
| 203 | // available to other modules. |
| 204 | void PIC16AsmPrinter::EmitFunctionDecls (Module &M) { |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 205 | // Emit declarations for external functions. |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 206 | O << TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n"; |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 207 | for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) { |
| 208 | std::string Name = Mang->getValueName(I); |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 209 | if (Name.compare("@abort") == 0) |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 210 | continue; |
Sanjiv Gupta | 85be408 | 2009-04-14 02:49:52 +0000 | [diff] [blame] | 211 | |
| 212 | // If it is llvm intrinsic call then don't emit |
| 213 | if (Name.find("llvm.") != std::string::npos) |
| 214 | continue; |
| 215 | |
Sanjiv Gupta | af3fdb5 | 2009-05-10 16:18:39 +0000 | [diff] [blame] | 216 | if (! (I->isDeclaration() || I->hasExternalLinkage())) |
| 217 | continue; |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 218 | |
| 219 | const char *directive = I->isDeclaration() ? TAI->getExternDirective() : |
| 220 | TAI->getGlobalDirective(); |
| 221 | |
| 222 | O << directive << Name << "\n"; |
| 223 | O << directive << PAN::getRetvalLabel(Name) << "\n"; |
| 224 | O << directive << PAN::getArgsLabel(Name) << "\n"; |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 225 | } |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 226 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 227 | O << TAI->getCommentString() << "Function Declarations - END." <<"\n"; |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 228 | } |
Sanjiv Gupta | 2cc7531 | 2009-02-10 04:20:26 +0000 | [diff] [blame] | 229 | |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 230 | // Emit variables imported from other Modules. |
| 231 | void PIC16AsmPrinter::EmitUndefinedVars (Module &M) |
| 232 | { |
| 233 | std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDecls->Items; |
| 234 | if (! Items.size()) return; |
| 235 | |
| 236 | O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n"; |
| 237 | for (unsigned j = 0; j < Items.size(); j++) { |
| 238 | O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n"; |
| 239 | } |
| 240 | O << TAI->getCommentString() << "Imported Variables - END" << "\n"; |
| 241 | } |
| 242 | |
| 243 | // Emit variables defined in this module and are available to other modules. |
| 244 | void PIC16AsmPrinter::EmitDefinedVars (Module &M) |
| 245 | { |
| 246 | std::vector<const GlobalVariable*> Items = PTAI->ExternalVarDefs->Items; |
| 247 | if (! Items.size()) return; |
| 248 | |
| 249 | O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n"; |
| 250 | for (unsigned j = 0; j < Items.size(); j++) { |
| 251 | O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n"; |
| 252 | } |
| 253 | O << TAI->getCommentString() << "Exported Variables - END" << "\n"; |
| 254 | } |
| 255 | |
| 256 | // Emit initialized data placed in ROM. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 257 | void PIC16AsmPrinter::EmitRomData (Module &M) |
| 258 | { |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 259 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 260 | std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items; |
| 261 | if (! Items.size()) return; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 262 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 263 | // Print ROData ection. |
| 264 | O << "\n"; |
| 265 | SwitchToSection(PTAI->ROSection->S_); |
| 266 | for (unsigned j = 0; j < Items.size(); j++) { |
| 267 | O << Mang->getValueName(Items[j]); |
| 268 | Constant *C = Items[j]->getInitializer(); |
| 269 | int AddrSpace = Items[j]->getType()->getAddressSpace(); |
| 270 | EmitGlobalConstant(C, AddrSpace); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 271 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 274 | bool PIC16AsmPrinter::doFinalization(Module &M) { |
Sanjiv Gupta | ad6585b | 2009-05-13 15:13:17 +0000 | [diff] [blame] | 275 | printLibcallDecls(); |
Sanjiv Gupta | a57bc3b | 2009-05-22 13:58:45 +0000 | [diff] [blame^] | 276 | EmitVarDebugInfo(M); |
| 277 | O << "\t" << ".EOF\n"; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 278 | O << "\t" << "END\n"; |
| 279 | bool Result = AsmPrinter::doFinalization(M); |
| 280 | return Result; |
| 281 | } |
| 282 | |
| 283 | void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) { |
| 284 | const Function *F = MF.getFunction(); |
| 285 | std::string FuncName = Mang->getValueName(F); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 286 | const TargetData *TD = TM.getTargetData(); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 287 | // Emit the data section name. |
| 288 | O << "\n"; |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 289 | const char *SectionName = PAN::getFrameSectionName(CurrentFnName).c_str(); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 290 | |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 291 | const Section *fPDataSection = TAI->getNamedSection(SectionName, |
Sanjiv Gupta | 2bdf490 | 2009-04-20 16:59:35 +0000 | [diff] [blame] | 292 | SectionFlags::Writeable); |
| 293 | SwitchToSection(fPDataSection); |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 294 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 295 | // Emit function frame label |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 296 | O << PAN::getFrameLabel(CurrentFnName) << ":\n"; |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 297 | |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 298 | const Type *RetType = F->getReturnType(); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 299 | unsigned RetSize = 0; |
| 300 | if (RetType->getTypeID() != Type::VoidTyID) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 301 | RetSize = TD->getTypeAllocSize(RetType); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 302 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 303 | //Emit function return value space |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 304 | // FIXME: Do not emit RetvalLable when retsize is zero. To do this |
| 305 | // we will need to avoid printing a global directive for Retval label |
| 306 | // in emitExternandGloblas. |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 307 | if(RetSize > 0) |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 308 | O << PAN::getRetvalLabel(CurrentFnName) << " RES " << RetSize << "\n"; |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 309 | else |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 310 | O << PAN::getRetvalLabel(CurrentFnName) << ": \n"; |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 311 | |
| 312 | // Emit variable to hold the space for function arguments |
| 313 | unsigned ArgSize = 0; |
| 314 | for (Function::const_arg_iterator argi = F->arg_begin(), |
| 315 | arge = F->arg_end(); argi != arge ; ++argi) { |
| 316 | const Type *Ty = argi->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 317 | ArgSize += TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 318 | } |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 319 | |
| 320 | O << PAN::getArgsLabel(CurrentFnName) << " RES " << ArgSize << "\n"; |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 321 | |
Sanjiv Gupta | 85be408 | 2009-04-14 02:49:52 +0000 | [diff] [blame] | 322 | // Emit temporary space |
| 323 | int TempSize = PTLI->GetTmpSize(); |
| 324 | if (TempSize > 0 ) |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 325 | O << PAN::getTempdataLabel(CurrentFnName) << " RES " << TempSize <<"\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 326 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 327 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 328 | void PIC16AsmPrinter::EmitIData (Module &M) { |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 329 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 330 | // Print all IDATA sections. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 331 | std::vector <PIC16Section *>IDATASections = PTAI->IDATASections; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 332 | for (unsigned i = 0; i < IDATASections.size(); i++) { |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 333 | O << "\n"; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 334 | SwitchToSection(IDATASections[i]->S_); |
| 335 | std::vector<const GlobalVariable*> Items = IDATASections[i]->Items; |
| 336 | for (unsigned j = 0; j < Items.size(); j++) { |
| 337 | std::string Name = Mang->getValueName(Items[j]); |
| 338 | Constant *C = Items[j]->getInitializer(); |
| 339 | int AddrSpace = Items[j]->getType()->getAddressSpace(); |
| 340 | O << Name; |
| 341 | EmitGlobalConstant(C, AddrSpace); |
| 342 | } |
| 343 | } |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 344 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 345 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 346 | void PIC16AsmPrinter::EmitUData (Module &M) { |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 347 | const TargetData *TD = TM.getTargetData(); |
| 348 | |
| 349 | // Print all BSS sections. |
Sanjiv Gupta | 211f362 | 2009-05-10 05:23:47 +0000 | [diff] [blame] | 350 | std::vector <PIC16Section *>BSSSections = PTAI->BSSSections; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 351 | for (unsigned i = 0; i < BSSSections.size(); i++) { |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 352 | O << "\n"; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 353 | SwitchToSection(BSSSections[i]->S_); |
| 354 | std::vector<const GlobalVariable*> Items = BSSSections[i]->Items; |
| 355 | for (unsigned j = 0; j < Items.size(); j++) { |
| 356 | std::string Name = Mang->getValueName(Items[j]); |
| 357 | Constant *C = Items[j]->getInitializer(); |
| 358 | const Type *Ty = C->getType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 359 | unsigned Size = TD->getTypeAllocSize(Ty); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 360 | |
| 361 | O << Name << " " <<"RES"<< " " << Size ; |
| 362 | O << "\n"; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 367 | void PIC16AsmPrinter::EmitAutos (Module &M) |
| 368 | { |
| 369 | // Section names for all globals are already set. |
| 370 | |
Sanjiv Gupta | 2364cfe | 2009-05-12 17:07:27 +0000 | [diff] [blame] | 371 | const TargetData *TD = TM.getTargetData(); |
| 372 | |
| 373 | // Now print all Autos sections. |
| 374 | std::vector <PIC16Section *>AutosSections = PTAI->AutosSections; |
| 375 | for (unsigned i = 0; i < AutosSections.size(); i++) { |
| 376 | O << "\n"; |
| 377 | SwitchToSection(AutosSections[i]->S_); |
| 378 | std::vector<const GlobalVariable*> Items = AutosSections[i]->Items; |
| 379 | for (unsigned j = 0; j < Items.size(); j++) { |
| 380 | std::string VarName = Mang->getValueName(Items[j]); |
| 381 | Constant *C = Items[j]->getInitializer(); |
| 382 | const Type *Ty = C->getType(); |
| 383 | unsigned Size = TD->getTypeAllocSize(Ty); |
| 384 | // Emit memory reserve directive. |
| 385 | O << VarName << " RES " << Size << "\n"; |
| 386 | } |
| 387 | } |
| 388 | } |
Sanjiv Gupta | a57bc3b | 2009-05-22 13:58:45 +0000 | [diff] [blame^] | 389 | |
| 390 | void PIC16AsmPrinter::EmitVarDebugInfo(Module &M) { |
| 391 | GlobalVariable *Root = M.getGlobalVariable("llvm.dbg.global_variables"); |
| 392 | if (!Root) |
| 393 | return; |
| 394 | |
| 395 | O << TAI->getCommentString() << " Debug Information:"; |
| 396 | Constant *RootC = cast<Constant>(*Root->use_begin()); |
| 397 | for (Value::use_iterator UI = RootC->use_begin(), UE = Root->use_end(); |
| 398 | UI != UE; ++UI) { |
| 399 | for (Value::use_iterator UUI = UI->use_begin(), UUE = UI->use_end(); |
| 400 | UUI != UUE; ++UUI) { |
| 401 | DIGlobalVariable DIGV(cast<GlobalVariable>(*UUI)); |
| 402 | DIType Ty = DIGV.getType(); |
| 403 | unsigned short TypeNo = 0; |
| 404 | bool HasAux = false; |
| 405 | int Aux[20] = { 0 }; |
| 406 | std::string TypeName = ""; |
| 407 | std::string VarName = TAI->getGlobalPrefix()+DIGV.getGlobal()->getName(); |
| 408 | DbgInfo.PopulateDebugInfo(Ty, TypeNo, HasAux, Aux, TypeName); |
| 409 | // Emit debug info only if type information is availaible. |
| 410 | if (TypeNo != PIC16Dbg::T_NULL) { |
| 411 | O << "\n\t.type " << VarName << ", " << TypeNo; |
| 412 | short ClassNo = DbgInfo.getClass(DIGV); |
| 413 | O << "\n\t.class " << VarName << ", " << ClassNo; |
| 414 | if (HasAux) { |
| 415 | if (TypeName != "") { |
| 416 | // Emit debug info for structure and union objects after |
| 417 | // .dim directive supports structure/union tag name in aux entry. |
| 418 | /* O << "\n\t.dim " << VarName << ", 1," << TypeName; |
| 419 | for (int i = 0; i<20; i++) |
| 420 | O << "," << Aux[i];*/ |
| 421 | } |
| 422 | else { |
| 423 | O << "\n\t.dim " << VarName << ", 1" ; |
| 424 | for (int i = 0; i<20; i++) |
| 425 | O << "," << Aux[i]; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | O << "\n"; |
| 432 | } |
| 433 | |