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