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