llvm_unreachable->llvm_unreachable(0), LLVM_UNREACHABLE->llvm_unreachable.
[oota-llvm.git] / lib / Target / PIC16 / PIC16.h
1 //===-- PIC16.h - Top-level interface for PIC16 representation --*- C++ -*-===//
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 the entry points for global functions defined in 
11 // the LLVM PIC16 back-end.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_PIC16_H
16 #define LLVM_TARGET_PIC16_H
17
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include <iosfwd>
21 #include <cassert>
22 #include <sstream>
23 #include <cstring>
24 #include <string>
25
26 namespace llvm {
27   class PIC16TargetMachine;
28   class FunctionPass;
29   class MachineCodeEmitter;
30   class raw_ostream;
31
32 namespace PIC16CC {
33   enum CondCodes {
34     EQ,
35     NE,
36     LT,
37     LE,
38     GT,
39     GE,
40     ULT,
41     UGT,
42     ULE,
43     UGE
44   };
45 }
46   // A Central class to manage all ABI naming conventions.
47   // PAN - [P]ic16 [A]BI [N]ames
48   class PAN {
49     public:
50     // Map the name of the symbol to its section name.
51     // Current ABI:
52     // -----------------------------------------------------
53     // ALL Names are prefixed with the symobl '@'.
54     // ------------------------------------------------------
55     // Global variables do not have any '.' in their names.
56     // These are maily function names and global variable names.
57     // Example - @foo,  @i
58     // -------------------------------------------------------
59     // Functions and auto variables.
60     // Names are mangled as <prefix><funcname>.<tag>.<varname>
61     // Where <prefix> is '@' and <tag> is any one of
62     // the following
63     // .auto. - an automatic var of a function.
64     // .temp. - temproray data of a function.
65     // .ret.  - return value label for a function.
66     // .frame. - Frame label for a function where retval, args
67     //           and temps are stored.
68     // .args. - Label used to pass arguments to a direct call.
69     // Example - Function name:   @foo
70     //           Its frame:       @foo.frame.
71     //           Its retval:      @foo.ret.
72     //           Its local vars:  @foo.auto.a
73     //           Its temp data:   @foo.temp.
74     //           Its arg passing: @foo.args.
75     //----------------------------------------------
76     // Libcall - compiler generated libcall names must start with .lib.
77     //           This id will be used to emit extern decls for libcalls.
78     // Example - libcall name:   @.lib.sra.i8
79     //           To pass args:   @.lib.sra.i8.args.
80     //           To return val:  @.lib.sra.i8.ret.
81     //----------------------------------------------
82     // SECTION Names
83     // uninitialized globals - @udata.<num>.#
84     // initialized globals - @idata.<num>.#
85     // Function frame - @<func>.frame_section.
86     // Function autos - @<func>.autos_section.
87     // Declarations - Enclosed in comments. No section for them.
88     //----------------------------------------------------------
89     
90     // Tags used to mangle different names. 
91     enum TAGS {
92       PREFIX_SYMBOL,
93       GLOBAL,
94       STATIC_LOCAL,
95       AUTOS_LABEL,
96       FRAME_LABEL,
97       RET_LABEL,
98       ARGS_LABEL,
99       TEMPS_LABEL,
100       
101       LIBCALL,
102       
103       FRAME_SECTION,
104       AUTOS_SECTION,
105       CODE_SECTION
106     };
107
108     // Textual names of the tags.
109     inline static const char *getTagName(TAGS tag) {
110       switch (tag) {
111       default: return "";
112       case PREFIX_SYMBOL:    return "@";
113       case AUTOS_LABEL:       return ".auto.";
114       case FRAME_LABEL:       return ".frame.";
115       case TEMPS_LABEL:       return ".temp.";
116       case ARGS_LABEL:       return ".args.";
117       case RET_LABEL:       return ".ret.";
118       case LIBCALL:       return ".lib.";
119       case FRAME_SECTION:       return ".frame_section.";
120       case AUTOS_SECTION:       return ".autos_section.";
121       case CODE_SECTION:       return ".code_section.";
122       }
123     }
124
125     // Get tag type for the Symbol.
126     inline static TAGS getSymbolTag(const std::string &Sym) {
127       if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
128         return TEMPS_LABEL;
129
130       if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
131         return FRAME_LABEL;
132
133       if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
134         return RET_LABEL;
135
136       if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
137         return ARGS_LABEL;
138
139       if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
140         return AUTOS_LABEL;
141
142       if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
143         return LIBCALL;
144
145       // It does not have any Tag. So its a true global or static local.
146       if (Sym.find(".") == std::string::npos) 
147         return GLOBAL;
148       
149       // If a . is there, then it may be static local.
150       // We should mangle these as well in clang.
151       if (Sym.find(".") != std::string::npos) 
152         return STATIC_LOCAL;
153  
154       assert (0 && "Could not determine Symbol's tag");
155       return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
156     }
157
158     // addPrefix - add prefix symbol to a name if there isn't one already.
159     inline static std::string addPrefix (const std::string &Name) {
160       std::string prefix = getTagName (PREFIX_SYMBOL);
161
162       // If this name already has a prefix, nothing to do.
163       if (Name.compare(0, prefix.size(), prefix) == 0)
164         return Name;
165
166       return prefix + Name;
167     }
168
169     // Get mangled func name from a mangled sym name.
170     // In all cases func name is the first component before a '.'.
171     static inline std::string getFuncNameForSym(const std::string &Sym1) {
172       assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
173
174       std::string Sym = addPrefix(Sym1);
175
176       // Position of the . after func name. That's where func name ends.
177       size_t func_name_end = Sym.find ('.');
178
179       return Sym.substr (0, func_name_end);
180     }
181
182     // Get Frame start label for a func.
183     static std::string getFrameLabel(const std::string &Func) {
184       std::string Func1 = addPrefix(Func);
185       std::string tag = getTagName(FRAME_LABEL);
186       return Func1 + tag;
187     }
188
189     static std::string getRetvalLabel(const std::string &Func) {
190       std::string Func1 = addPrefix(Func);
191       std::string tag = getTagName(RET_LABEL);
192       return Func1 + tag;
193     }
194
195     static std::string getArgsLabel(const std::string &Func) {
196       std::string Func1 = addPrefix(Func);
197       std::string tag = getTagName(ARGS_LABEL);
198       return Func1 + tag;
199     }
200
201     static std::string getTempdataLabel(const std::string &Func) {
202       std::string Func1 = addPrefix(Func);
203       std::string tag = getTagName(TEMPS_LABEL);
204       return Func1 + tag;
205     }
206
207     static std::string getFrameSectionName(const std::string &Func) {
208       std::string Func1 = addPrefix(Func);
209       std::string tag = getTagName(FRAME_SECTION);
210       return Func1 + tag + "# UDATA_OVR";
211     }
212
213     static std::string getAutosSectionName(const std::string &Func) {
214       std::string Func1 = addPrefix(Func);
215       std::string tag = getTagName(AUTOS_SECTION);
216       return Func1 + tag + "# UDATA_OVR";
217     }
218
219     static std::string getCodeSectionName(const std::string &Func) {
220       std::string Func1 = addPrefix(Func);
221       std::string tag = getTagName(CODE_SECTION);
222       return Func1 + tag + "# CODE";
223     }
224
225     // udata, romdata and idata section names are generated by a given number.
226     // @udata.<num>.# 
227     static std::string getUdataSectionName(unsigned num, 
228                                            std::string prefix = "") {
229        std::ostringstream o;
230        o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num 
231          << ".# UDATA"; 
232        return o.str(); 
233     }
234
235     static std::string getRomdataSectionName(unsigned num,
236                                              std::string prefix = "") {
237        std::ostringstream o;
238        o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num 
239          << ".# ROMDATA";
240        return o.str();
241     }
242
243     static std::string getIdataSectionName(unsigned num,
244                                            std::string prefix = "") {
245        std::ostringstream o;
246        o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num 
247          << ".# IDATA"; 
248        return o.str(); 
249     }
250
251     inline static bool isLocalName (const std::string &Name) {
252       if (getSymbolTag(Name) == AUTOS_LABEL)
253         return true;
254
255       return false;
256     }
257
258     inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
259       if (! isLocalName(Var)) return false;
260
261       std::string Func1 = addPrefix(Func);
262       // Extract func name of the varilable.
263       const std::string &fname = getFuncNameForSym(Var);
264
265       if (fname.compare(Func1) == 0)
266         return true;
267
268       return false;
269     }
270
271
272     // Get the section for the given external symbol names.
273     // This tries to find the type (Tag) of the symbol from its mangled name
274     // and return appropriate section name for it.
275     static inline std::string getSectionNameForSym(const std::string &Sym1) {
276       std::string Sym = addPrefix(Sym1);
277
278       std::string SectionName;
279  
280       std::string Fname = getFuncNameForSym (Sym);
281       TAGS id = getSymbolTag (Sym);
282
283       switch (id) {
284         default : assert (0 && "Could not determine external symbol type");
285         case FRAME_LABEL:
286         case RET_LABEL:
287         case TEMPS_LABEL:
288         case ARGS_LABEL:  {
289           return getFrameSectionName(Fname);
290         }
291         case AUTOS_LABEL: {
292           return getAutosSectionName(Fname);
293         }
294       }
295     }
296   }; // class PAN.
297
298
299   // External symbol names require memory to live till the program end.
300   // So we have to allocate it and keep.
301   inline static const char *createESName (const std::string &name) {
302     char *tmpName = new char[name.size() + 1];
303     strcpy (tmpName, name.c_str());
304     return tmpName;
305   }
306
307
308
309   inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
310     switch (CC) {
311     default: llvm_unreachable("Unknown condition code");
312     case PIC16CC::NE:  return "ne";
313     case PIC16CC::EQ:   return "eq";
314     case PIC16CC::LT:   return "lt";
315     case PIC16CC::ULT:   return "lt";
316     case PIC16CC::LE:  return "le";
317     case PIC16CC::ULE:  return "le";
318     case PIC16CC::GT:  return "gt";
319     case PIC16CC::UGT:  return "gt";
320     case PIC16CC::GE:   return "ge";
321     case PIC16CC::UGE:   return "ge";
322     }
323   }
324
325   inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
326     switch (CC) {
327     default: llvm_unreachable("Unknown condition code");
328     case PIC16CC::NE:  
329     case PIC16CC::EQ: 
330     case PIC16CC::LT:
331     case PIC16CC::LE:
332     case PIC16CC::GE:
333     case PIC16CC::GT:
334       return true;
335     case PIC16CC::ULT:
336     case PIC16CC::UGT:
337     case PIC16CC::ULE:
338     case PIC16CC::UGE:
339       return false;   // condition codes for unsigned comparison. 
340     }
341   }
342
343
344
345   FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
346   FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS, 
347                                            PIC16TargetMachine &TM,
348                                            bool Verbose);
349   // Banksel optimzer pass.
350   FunctionPass *createPIC16MemSelOptimizerPass();
351 } // end namespace llvm;
352
353 // Defines symbolic names for PIC16 registers.  This defines a mapping from
354 // register name to register number.
355 #include "PIC16GenRegisterNames.inc"
356
357 // Defines symbolic names for the PIC16 instructions.
358 #include "PIC16GenInstrNames.inc"
359
360 #endif