Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 1 | //===-- PIC16.h - Top-level interface for PIC16 representation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Sanjiv Gupta | 2d4e7f7 | 2008-05-14 06:50:01 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the entry points for global functions defined in |
| 11 | // the LLVM PIC16 back-end. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 15 | #ifndef LLVM_TARGET_PIC16_H |
| 16 | #define LLVM_TARGET_PIC16_H |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 17 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 19 | #include <iosfwd> |
| 20 | #include <cassert> |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 21 | #include <string> |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 22 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | class PIC16TargetMachine; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 25 | class FunctionPass; |
| 26 | class MachineCodeEmitter; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 27 | class raw_ostream; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 28 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 29 | namespace PIC16CC { |
| 30 | enum CondCodes { |
| 31 | EQ, |
| 32 | NE, |
| 33 | LT, |
| 34 | LE, |
| 35 | GT, |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 36 | GE, |
| 37 | ULT, |
| 38 | UGT, |
| 39 | ULE, |
| 40 | UGE |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 41 | }; |
| 42 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 43 | // A Central object to manage all ABI naming conventions. |
| 44 | class PIC16ABINames { |
| 45 | public: |
| 46 | // Map the name of the symbol to its section name. |
| 47 | // Current ABI: |
| 48 | // ------------------------------------------------------ |
| 49 | // Global variables do not have any '.' in their names. |
| 50 | // they are prefixed with @ |
| 51 | // These are maily function names and global variable names. |
| 52 | // ------------------------------------------------------- |
| 53 | // Functions and auto variables. |
| 54 | // Names are mangled as <prefix><funcname>.<id>.<varname> |
| 55 | // Where prefix is a special char '@' and id is any one of |
| 56 | // the following |
| 57 | // .auto. - an automatic var of a function. |
| 58 | // .temp. - temproray data of a function. |
| 59 | // .ret. - return value label for a function. |
| 60 | // .frame. - Frame label for a function where retval, args |
| 61 | // and temps are stored. |
| 62 | // .args. - Label used to pass arguments to a direct call. |
| 63 | // Example - Function name: @foo |
| 64 | // Its frame: @foo.frame. |
| 65 | // Its retval: @foo.ret. |
| 66 | // Its local vars: @foo.auto.a |
| 67 | // Its temp data: @foo.temp. |
| 68 | // Its arg passing: @foo.args. |
| 69 | //---------------------------------------------- |
| 70 | // Libcall - compiler generated libcall names must have a .lib. |
| 71 | // This id will be used to emit extern decls for libcalls. |
| 72 | // Example - libcall name: @sra_i8.lib. |
| 73 | // To pass args: @sra_i8.args. |
| 74 | // To return val: @sra_i8.ret. |
| 75 | //---------------------------------------------- |
| 76 | |
| 77 | enum IDs { |
| 78 | PREFIX_SYMBOL, |
| 79 | |
| 80 | FUNC_AUTOS, |
| 81 | FUNC_FRAME, |
| 82 | FUNC_RET, |
| 83 | FUNC_ARGS, |
| 84 | FUNC_TEMPS, |
| 85 | |
| 86 | LIBCALL, |
| 87 | |
| 88 | FRAME_SECTION, |
| 89 | AUTOS_SECTION |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 90 | }; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 91 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 92 | inline static const char *getIDName(IDs id) { |
| 93 | switch (id) { |
| 94 | default: assert(0 && "Unknown id"); |
| 95 | case PREFIX_SYMBOL: return "@"; |
| 96 | case FUNC_AUTOS: return ".auto."; |
| 97 | case FUNC_FRAME: return ".frame."; |
| 98 | case FUNC_TEMPS: return ".temp."; |
| 99 | case FUNC_ARGS: return ".args."; |
| 100 | case FUNC_RET: return ".ret."; |
| 101 | case FRAME_SECTION: return "fpdata"; |
| 102 | case AUTOS_SECTION: return "fadata"; |
| 103 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 104 | } |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 105 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 106 | inline static IDs getID(const std::string &Sym) { |
| 107 | if (Sym.find(getIDName(FUNC_TEMPS))) |
| 108 | return FUNC_TEMPS; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 109 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 110 | if (Sym.find(getIDName(FUNC_FRAME))) |
| 111 | return FUNC_FRAME; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 112 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 113 | if (Sym.find(getIDName(FUNC_RET))) |
| 114 | return FUNC_RET; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 115 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 116 | if (Sym.find(getIDName(FUNC_ARGS))) |
| 117 | return FUNC_ARGS; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 118 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 119 | if (Sym.find(getIDName(FUNC_AUTOS))) |
| 120 | return FUNC_AUTOS; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 121 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 122 | if (Sym.find(getIDName(LIBCALL))) |
| 123 | return LIBCALL; |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 124 | |
Sanjiv Gupta | 573eb5e | 2009-05-08 04:50:14 +0000 | [diff] [blame^] | 125 | // It does not have any ID. So its a global. |
| 126 | assert (0 && "Could not determine ID symbol type"); |
| 127 | } |
| 128 | |
| 129 | // Get func name from a mangled name. |
| 130 | // In all cases func name is the first component before a '.'. |
| 131 | static inline std::string getFuncNameForSym(const std::string &Sym) { |
| 132 | const char *prefix = getIDName (PREFIX_SYMBOL); |
| 133 | |
| 134 | // If this name has a prefix, func name start after prfix in that case. |
| 135 | size_t func_name_start = 0; |
| 136 | if (Sym.find(prefix, 0, strlen(prefix)) != std::string::npos) |
| 137 | func_name_start = strlen(prefix); |
| 138 | |
| 139 | // Position of the . after func name. That's where func name ends. |
| 140 | size_t func_name_end = Sym.find ('.', func_name_start); |
| 141 | |
| 142 | return Sym.substr (func_name_start, func_name_end); |
| 143 | } |
| 144 | |
| 145 | // Form a section name given the section type and func name. |
| 146 | static std::string |
| 147 | getSectionNameForFunc (const std::string &Fname, const IDs sec_id) { |
| 148 | std::string sec_id_string = getIDName(sec_id); |
| 149 | return sec_id_string + "." + Fname + ".#"; |
| 150 | } |
| 151 | |
| 152 | // Get the section for the given external symbol names. |
| 153 | // This tries to find the type (ID) of the symbol from its mangled name |
| 154 | // and return appropriate section name for it. |
| 155 | static inline std::string getSectionNameForSym(const std::string &Sym) { |
| 156 | std::string SectionName; |
| 157 | |
| 158 | IDs id = getID (Sym); |
| 159 | std::string Fname = getFuncNameForSym (Sym); |
| 160 | |
| 161 | switch (id) { |
| 162 | default : assert (0 && "Could not determine external symbol type"); |
| 163 | case FUNC_FRAME: |
| 164 | case FUNC_RET: |
| 165 | case FUNC_TEMPS: |
| 166 | case FUNC_ARGS: { |
| 167 | return getSectionNameForFunc (Fname, FRAME_SECTION); |
| 168 | } |
| 169 | case FUNC_AUTOS: { |
| 170 | return getSectionNameForFunc (Fname, AUTOS_SECTION); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | }; // class PIC16ABINames. |
| 175 | |
| 176 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 177 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 178 | |
| 179 | inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) { |
| 180 | switch (CC) { |
| 181 | default: assert(0 && "Unknown condition code"); |
| 182 | case PIC16CC::NE: return "ne"; |
| 183 | case PIC16CC::EQ: return "eq"; |
| 184 | case PIC16CC::LT: return "lt"; |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 185 | case PIC16CC::ULT: return "lt"; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 186 | case PIC16CC::LE: return "le"; |
| 187 | case PIC16CC::GT: return "gt"; |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 188 | case PIC16CC::UGT: return "gt"; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 189 | case PIC16CC::GE: return "ge"; |
| 190 | } |
| 191 | } |
| 192 | |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 193 | inline static bool isSignedComparison(PIC16CC::CondCodes CC) { |
| 194 | switch (CC) { |
| 195 | default: assert(0 && "Unknown condition code"); |
| 196 | case PIC16CC::NE: |
| 197 | case PIC16CC::EQ: |
| 198 | case PIC16CC::LT: |
| 199 | case PIC16CC::LE: |
| 200 | case PIC16CC::GE: |
| 201 | case PIC16CC::GT: |
| 202 | return true; |
| 203 | case PIC16CC::ULT: |
| 204 | case PIC16CC::UGT: |
| 205 | case PIC16CC::ULE: |
| 206 | case PIC16CC::UGE: |
| 207 | return false; // condition codes for unsigned comparison. |
| 208 | } |
| 209 | } |
| 210 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 211 | |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 212 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 213 | FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM); |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 214 | FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS, |
Bill Wendling | 57f0db8 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 215 | PIC16TargetMachine &TM, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 216 | CodeGenOpt::Level OptLevel, |
| 217 | bool Verbose); |
Sanjiv Gupta | d8d27f4 | 2009-05-06 08:02:01 +0000 | [diff] [blame] | 218 | // Banksel optimzer pass. |
| 219 | FunctionPass *createPIC16MemSelOptimizerPass(); |
| 220 | std::string getSectionNameForSym(const std::string &Sym); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 221 | } // end namespace llvm; |
| 222 | |
| 223 | // Defines symbolic names for PIC16 registers. This defines a mapping from |
| 224 | // register name to register number. |
| 225 | #include "PIC16GenRegisterNames.inc" |
| 226 | |
| 227 | // Defines symbolic names for the PIC16 instructions. |
| 228 | #include "PIC16GenInstrNames.inc" |
| 229 | |
| 230 | #endif |