blob: 1f4aa9680db5ddb521e295ef5402607bf1aff0cb [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===-- PIC16.h - Top-level interface for PIC16 representation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Sanjiv Gupta2d4e7f72008-05-14 06:50:01 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Sanjiv Gupta0e687712008-05-13 09:02:57 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the entry points for global functions defined in
11// the LLVM PIC16 back-end.
12//
13//===----------------------------------------------------------------------===//
14
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000015#ifndef LLVM_TARGET_PIC16_H
16#define LLVM_TARGET_PIC16_H
Sanjiv Gupta0e687712008-05-13 09:02:57 +000017
Bill Wendling98a366d2009-04-29 23:29:43 +000018#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000019#include <iosfwd>
20#include <cassert>
Nick Lewycky4a228862009-05-08 06:22:25 +000021#include <cstring>
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000022#include <string>
Sanjiv Gupta1b046942009-01-13 19:18:47 +000023
Sanjiv Gupta0e687712008-05-13 09:02:57 +000024namespace llvm {
25 class PIC16TargetMachine;
Sanjiv Gupta0e687712008-05-13 09:02:57 +000026 class FunctionPass;
27 class MachineCodeEmitter;
Owen Andersoncb371882008-08-21 00:14:44 +000028 class raw_ostream;
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029
Sanjiv Gupta1b046942009-01-13 19:18:47 +000030namespace PIC16CC {
31 enum CondCodes {
32 EQ,
33 NE,
34 LT,
35 LE,
36 GT,
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000037 GE,
38 ULT,
39 UGT,
40 ULE,
41 UGE
Sanjiv Gupta1b046942009-01-13 19:18:47 +000042 };
43}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000044 // A Central object to manage all ABI naming conventions.
45 class PIC16ABINames {
46 public:
47 // Map the name of the symbol to its section name.
48 // Current ABI:
49 // ------------------------------------------------------
50 // Global variables do not have any '.' in their names.
51 // they are prefixed with @
52 // These are maily function names and global variable names.
53 // -------------------------------------------------------
54 // Functions and auto variables.
55 // Names are mangled as <prefix><funcname>.<id>.<varname>
56 // Where prefix is a special char '@' and id is any one of
57 // the following
58 // .auto. - an automatic var of a function.
59 // .temp. - temproray data of a function.
60 // .ret. - return value label for a function.
61 // .frame. - Frame label for a function where retval, args
62 // and temps are stored.
63 // .args. - Label used to pass arguments to a direct call.
64 // Example - Function name: @foo
65 // Its frame: @foo.frame.
66 // Its retval: @foo.ret.
67 // Its local vars: @foo.auto.a
68 // Its temp data: @foo.temp.
69 // Its arg passing: @foo.args.
70 //----------------------------------------------
71 // Libcall - compiler generated libcall names must have a .lib.
72 // This id will be used to emit extern decls for libcalls.
73 // Example - libcall name: @sra_i8.lib.
74 // To pass args: @sra_i8.args.
75 // To return val: @sra_i8.ret.
76 //----------------------------------------------
77
78 enum IDs {
79 PREFIX_SYMBOL,
80
81 FUNC_AUTOS,
82 FUNC_FRAME,
83 FUNC_RET,
84 FUNC_ARGS,
85 FUNC_TEMPS,
86
87 LIBCALL,
88
89 FRAME_SECTION,
90 AUTOS_SECTION
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +000091 };
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000092
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +000093 inline static const char *getIDName(IDs id) {
94 switch (id) {
95 default: assert(0 && "Unknown id");
96 case PREFIX_SYMBOL: return "@";
97 case FUNC_AUTOS: return ".auto.";
98 case FUNC_FRAME: return ".frame.";
99 case FUNC_TEMPS: return ".temp.";
100 case FUNC_ARGS: return ".args.";
101 case FUNC_RET: return ".ret.";
102 case FRAME_SECTION: return "fpdata";
103 case AUTOS_SECTION: return "fadata";
104 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000105 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000106
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000107 inline static IDs getID(const std::string &Sym) {
108 if (Sym.find(getIDName(FUNC_TEMPS)))
109 return FUNC_TEMPS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000110
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000111 if (Sym.find(getIDName(FUNC_FRAME)))
112 return FUNC_FRAME;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000113
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000114 if (Sym.find(getIDName(FUNC_RET)))
115 return FUNC_RET;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000116
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000117 if (Sym.find(getIDName(FUNC_ARGS)))
118 return FUNC_ARGS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000119
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000120 if (Sym.find(getIDName(FUNC_AUTOS)))
121 return FUNC_AUTOS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000122
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000123 if (Sym.find(getIDName(LIBCALL)))
124 return LIBCALL;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000125
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000126 // It does not have any ID. So its a global.
127 assert (0 && "Could not determine ID symbol type");
128 }
129
130 // Get func name from a mangled name.
131 // In all cases func name is the first component before a '.'.
132 static inline std::string getFuncNameForSym(const std::string &Sym) {
133 const char *prefix = getIDName (PREFIX_SYMBOL);
134
135 // If this name has a prefix, func name start after prfix in that case.
136 size_t func_name_start = 0;
137 if (Sym.find(prefix, 0, strlen(prefix)) != std::string::npos)
138 func_name_start = strlen(prefix);
139
140 // Position of the . after func name. That's where func name ends.
141 size_t func_name_end = Sym.find ('.', func_name_start);
142
143 return Sym.substr (func_name_start, func_name_end);
144 }
145
146 // Form a section name given the section type and func name.
147 static std::string
148 getSectionNameForFunc (const std::string &Fname, const IDs sec_id) {
149 std::string sec_id_string = getIDName(sec_id);
150 return sec_id_string + "." + Fname + ".#";
151 }
152
153 // Get the section for the given external symbol names.
154 // This tries to find the type (ID) of the symbol from its mangled name
155 // and return appropriate section name for it.
156 static inline std::string getSectionNameForSym(const std::string &Sym) {
157 std::string SectionName;
158
159 IDs id = getID (Sym);
160 std::string Fname = getFuncNameForSym (Sym);
161
162 switch (id) {
163 default : assert (0 && "Could not determine external symbol type");
164 case FUNC_FRAME:
165 case FUNC_RET:
166 case FUNC_TEMPS:
167 case FUNC_ARGS: {
168 return getSectionNameForFunc (Fname, FRAME_SECTION);
169 }
170 case FUNC_AUTOS: {
171 return getSectionNameForFunc (Fname, AUTOS_SECTION);
172 }
173 }
174 }
175 }; // class PIC16ABINames.
176
177
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000178
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000179
180 inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
181 switch (CC) {
182 default: assert(0 && "Unknown condition code");
183 case PIC16CC::NE: return "ne";
184 case PIC16CC::EQ: return "eq";
185 case PIC16CC::LT: return "lt";
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000186 case PIC16CC::ULT: return "lt";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000187 case PIC16CC::LE: return "le";
188 case PIC16CC::GT: return "gt";
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000189 case PIC16CC::UGT: return "gt";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000190 case PIC16CC::GE: return "ge";
191 }
192 }
193
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000194 inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
195 switch (CC) {
196 default: assert(0 && "Unknown condition code");
197 case PIC16CC::NE:
198 case PIC16CC::EQ:
199 case PIC16CC::LT:
200 case PIC16CC::LE:
201 case PIC16CC::GE:
202 case PIC16CC::GT:
203 return true;
204 case PIC16CC::ULT:
205 case PIC16CC::UGT:
206 case PIC16CC::ULE:
207 case PIC16CC::UGE:
208 return false; // condition codes for unsigned comparison.
209 }
210 }
211
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000212
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000213
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000214 FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
Owen Andersoncb371882008-08-21 00:14:44 +0000215 FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS,
Bill Wendling57f0db82009-02-24 08:30:20 +0000216 PIC16TargetMachine &TM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000217 CodeGenOpt::Level OptLevel,
218 bool Verbose);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000219 // Banksel optimzer pass.
220 FunctionPass *createPIC16MemSelOptimizerPass();
221 std::string getSectionNameForSym(const std::string &Sym);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000222} // end namespace llvm;
223
224// Defines symbolic names for PIC16 registers. This defines a mapping from
225// register name to register number.
226#include "PIC16GenRegisterNames.inc"
227
228// Defines symbolic names for the PIC16 instructions.
229#include "PIC16GenInstrNames.inc"
230
231#endif