Avoid compiler warning (in -Asserts mode)
[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 formatted_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 isMemIntrinsic (const std::string &Name) {
259       if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
260           Name.compare("@memmove") == 0) {
261         return true;
262       }
263       
264       return false;
265     }
266
267     inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
268       if (! isLocalName(Var)) return false;
269
270       std::string Func1 = addPrefix(Func);
271       // Extract func name of the varilable.
272       const std::string &fname = getFuncNameForSym(Var);
273
274       if (fname.compare(Func1) == 0)
275         return true;
276
277       return false;
278     }
279
280
281     // Get the section for the given external symbol names.
282     // This tries to find the type (Tag) of the symbol from its mangled name
283     // and return appropriate section name for it.
284     static inline std::string getSectionNameForSym(const std::string &Sym1) {
285       std::string Sym = addPrefix(Sym1);
286
287       std::string SectionName;
288  
289       std::string Fname = getFuncNameForSym (Sym);
290       TAGS id = getSymbolTag (Sym);
291
292       switch (id) {
293         default : assert (0 && "Could not determine external symbol type");
294         case FRAME_LABEL:
295         case RET_LABEL:
296         case TEMPS_LABEL:
297         case ARGS_LABEL:  {
298           return getFrameSectionName(Fname);
299         }
300         case AUTOS_LABEL: {
301           return getAutosSectionName(Fname);
302         }
303       }
304     }
305   }; // class PAN.
306
307
308   // External symbol names require memory to live till the program end.
309   // So we have to allocate it and keep.
310   inline static const char *createESName (const std::string &name) {
311     char *tmpName = new char[name.size() + 1];
312     strcpy (tmpName, name.c_str());
313     return tmpName;
314   }
315
316
317
318   inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
319     switch (CC) {
320     default: llvm_unreachable("Unknown condition code");
321     case PIC16CC::NE:  return "ne";
322     case PIC16CC::EQ:   return "eq";
323     case PIC16CC::LT:   return "lt";
324     case PIC16CC::ULT:   return "lt";
325     case PIC16CC::LE:  return "le";
326     case PIC16CC::ULE:  return "le";
327     case PIC16CC::GT:  return "gt";
328     case PIC16CC::UGT:  return "gt";
329     case PIC16CC::GE:   return "ge";
330     case PIC16CC::UGE:   return "ge";
331     }
332   }
333
334   inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
335     switch (CC) {
336     default: llvm_unreachable("Unknown condition code");
337     case PIC16CC::NE:  
338     case PIC16CC::EQ: 
339     case PIC16CC::LT:
340     case PIC16CC::LE:
341     case PIC16CC::GE:
342     case PIC16CC::GT:
343       return true;
344     case PIC16CC::ULT:
345     case PIC16CC::UGT:
346     case PIC16CC::ULE:
347     case PIC16CC::UGE:
348       return false;   // condition codes for unsigned comparison. 
349     }
350   }
351
352
353
354   FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
355   // Banksel optimizer pass.
356   FunctionPass *createPIC16MemSelOptimizerPass();
357
358   extern Target ThePIC16Target;
359   extern Target TheCooperTarget;
360   
361 } // end namespace llvm;
362
363 // Defines symbolic names for PIC16 registers.  This defines a mapping from
364 // register name to register number.
365 #include "PIC16GenRegisterNames.inc"
366
367 // Defines symbolic names for the PIC16 instructions.
368 #include "PIC16GenInstrNames.inc"
369
370 #endif