X86: Call ulldiv and ftol2 on Windows instead of their libgcc eqivilents.
[oota-llvm.git] / lib / Target / PIC16 / PIC16ABINames.h
1 //===-- PIC16ABINames.h - PIC16 Naming conventios for ABI----- --*- 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 functions to manage ABI Naming conventions for PIC16. 
11 // 
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_PIC16ABINAMES_H
16 #define LLVM_TARGET_PIC16ABINAMES_H
17
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Target/TargetMachine.h"
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 formatted_raw_ostream;
30
31   // A Central class to manage all ABI naming conventions.
32   // PAN - [P]ic16 [A]BI [N]ames
33   class PAN {
34     public:
35     // Map the name of the symbol to its section name.
36     // Current ABI:
37     // -----------------------------------------------------
38     // ALL Names are prefixed with the symobl '@'.
39     // ------------------------------------------------------
40     // Global variables do not have any '.' in their names.
41     // These are maily function names and global variable names.
42     // Example - @foo,  @i
43     // Static local variables - @<func>.<var>
44     // -------------------------------------------------------
45     // Functions and auto variables.
46     // Names are mangled as <prefix><funcname>.<tag>.<varname>
47     // Where <prefix> is '@' and <tag> is any one of
48     // the following
49     // .auto. - an automatic var of a function.
50     // .temp. - temproray data of a function.
51     // .ret.  - return value label for a function.
52     // .frame. - Frame label for a function where retval, args
53     //           and temps are stored.
54     // .args. - Label used to pass arguments to a direct call.
55     // Example - Function name:   @foo
56     //           Its frame:       @foo.frame.
57     //           Its retval:      @foo.ret.
58     //           Its local vars:  @foo.auto.a
59     //           Its temp data:   @foo.temp.
60     //           Its arg passing: @foo.args.
61     //----------------------------------------------
62     // Libcall - compiler generated libcall names must start with .lib.
63     //           This id will be used to emit extern decls for libcalls.
64     // Example - libcall name:   @.lib.sra.i8
65     //           To pass args:   @.lib.sra.i8.args.
66     //           To return val:  @.lib.sra.i8.ret.
67     //----------------------------------------------
68     // SECTION Names
69     // uninitialized globals - @udata.<num>.#
70     // initialized globals - @idata.<num>.#
71     // Program memory data - @romdata.#
72     // Variables with user defined section name - <user_defined_section>
73     // Variables with user defined address - @<var>.user_section.<address>.#
74     // Function frame - @<func>.frame_section.
75     // Function autos - @<func>.autos_section.
76     // Overlay sections - @<color>.##
77     // Declarations - Enclosed in comments. No section for them.
78     //----------------------------------------------------------
79     
80     // Tags used to mangle different names. 
81     enum TAGS {
82       PREFIX_SYMBOL,
83       GLOBAL,
84       STATIC_LOCAL,
85       AUTOS_LABEL,
86       FRAME_LABEL,
87       RET_LABEL,
88       ARGS_LABEL,
89       TEMPS_LABEL,
90       
91       LIBCALL,
92       
93       FRAME_SECTION,
94       AUTOS_SECTION,
95       CODE_SECTION,
96       USER_SECTION
97     };
98
99     // Textual names of the tags.
100     inline static const char *getTagName(TAGS tag) {
101       switch (tag) {
102       default: return "";
103       case PREFIX_SYMBOL:    return "@";
104       case AUTOS_LABEL:       return ".auto.";
105       case FRAME_LABEL:       return ".frame.";
106       case TEMPS_LABEL:       return ".temp.";
107       case ARGS_LABEL:       return ".args.";
108       case RET_LABEL:       return ".ret.";
109       case LIBCALL:       return ".lib.";
110       case FRAME_SECTION:       return ".frame_section.";
111       case AUTOS_SECTION:       return ".autos_section.";
112       case CODE_SECTION:       return ".code_section.";
113       case USER_SECTION:       return ".user_section.";
114       }
115     }
116
117     // Get tag type for the Symbol.
118     inline static TAGS getSymbolTag(const std::string &Sym) {
119       if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
120         return TEMPS_LABEL;
121
122       if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
123         return FRAME_LABEL;
124
125       if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
126         return RET_LABEL;
127
128       if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
129         return ARGS_LABEL;
130
131       if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
132         return AUTOS_LABEL;
133
134       if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
135         return LIBCALL;
136
137       // It does not have any Tag. So its a true global or static local.
138       if (Sym.find(".") == std::string::npos) 
139         return GLOBAL;
140       
141       // If a . is there, then it may be static local.
142       // We should mangle these as well in clang.
143       if (Sym.find(".") != std::string::npos) 
144         return STATIC_LOCAL;
145  
146       assert (0 && "Could not determine Symbol's tag");
147       return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
148     }
149
150     // addPrefix - add prefix symbol to a name if there isn't one already.
151     inline static std::string addPrefix (const std::string &Name) {
152       std::string prefix = getTagName (PREFIX_SYMBOL);
153
154       // If this name already has a prefix, nothing to do.
155       if (Name.compare(0, prefix.size(), prefix) == 0)
156         return Name;
157
158       return prefix + Name;
159     }
160
161     // Get mangled func name from a mangled sym name.
162     // In all cases func name is the first component before a '.'.
163     static inline std::string getFuncNameForSym(const std::string &Sym1) {
164       assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
165
166       std::string Sym = addPrefix(Sym1);
167
168       // Position of the . after func name. That's where func name ends.
169       size_t func_name_end = Sym.find ('.');
170
171       return Sym.substr (0, func_name_end);
172     }
173
174     // Get Frame start label for a func.
175     static std::string getFrameLabel(const std::string &Func) {
176       std::string Func1 = addPrefix(Func);
177       std::string tag = getTagName(FRAME_LABEL);
178       return Func1 + tag;
179     }
180
181     // Get the retval label for the given function.
182     static std::string getRetvalLabel(const std::string &Func) {
183       std::string Func1 = addPrefix(Func);
184       std::string tag = getTagName(RET_LABEL);
185       return Func1 + tag;
186     }
187
188     // Get the argument label for the given function.
189     static std::string getArgsLabel(const std::string &Func) {
190       std::string Func1 = addPrefix(Func);
191       std::string tag = getTagName(ARGS_LABEL);
192       return Func1 + tag;
193     }
194
195     // Get the tempdata label for the given function.
196     static std::string getTempdataLabel(const std::string &Func) {
197       std::string Func1 = addPrefix(Func);
198       std::string tag = getTagName(TEMPS_LABEL);
199       return Func1 + tag;
200     }
201
202     static std::string getFrameSectionName(const std::string &Func) {
203       std::string Func1 = addPrefix(Func);
204       std::string tag = getTagName(FRAME_SECTION);
205       return Func1 + tag + "#";
206     }
207
208     static std::string getAutosSectionName(const std::string &Func) {
209       std::string Func1 = addPrefix(Func);
210       std::string tag = getTagName(AUTOS_SECTION);
211       return Func1 + tag + "#";
212     }
213
214     static std::string getCodeSectionName(const std::string &Func) {
215       std::string Func1 = addPrefix(Func);
216       std::string tag = getTagName(CODE_SECTION);
217       return Func1 + tag + "#";
218     }
219
220     static std::string getUserSectionName(const std::string &Name) {
221       std::string sname = addPrefix(Name);;
222       std::string tag = getTagName(USER_SECTION);
223       return sname + tag + "#";
224     }
225
226     // udata, romdata and idata section names are generated by a given number.
227     // @udata.<num>.# 
228     static std::string getUdataSectionName(unsigned num, 
229                                            std::string prefix = "") {
230        std::ostringstream o;
231        o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num 
232          << ".#"; 
233        return o.str(); 
234     }
235
236     static std::string getRomdataSectionName() {
237       return "romdata.#";
238     }
239
240     static std::string getSharedUDataSectionName() {
241        std::ostringstream o;
242        o << getTagName(PREFIX_SYMBOL)  << "udata_shr" << ".#";
243        return o.str();
244     }
245
246     static std::string getRomdataSectionName(unsigned num,
247                                              std::string prefix = "") {
248        std::ostringstream o;
249        o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num 
250          << ".#";
251        return o.str();
252     }
253
254     static std::string getIdataSectionName(unsigned num,
255                                            std::string prefix = "") {
256        std::ostringstream o;
257        o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num 
258          << ".#"; 
259        return o.str(); 
260     }
261
262     inline static bool isLocalName (const std::string &Name) {
263       if (getSymbolTag(Name) == AUTOS_LABEL)
264         return true;
265
266       return false;
267     }
268
269
270     inline static bool isMemIntrinsic (const std::string &Name) {
271       if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
272           Name.compare("@memmove") == 0) {
273         return true;
274       }
275       
276       return false;
277     }
278
279     // Currently names of libcalls are assigned during TargetLowering
280     // object construction. There is no provision to change the when the 
281     // code for a function IL function being generated. 
282     // So we have to change these names while printing assembly.
283     // We need to do that mainly for names related to intrinsics. This
284     // function returns true if a name needs to be cloned. 
285     inline static bool isIntrinsicStuff(const std::string &Name) {
286       // Return true if the name contains LIBCALL marker, or a MemIntrinisc.
287       // these are mainly ARGS_LABEL, RET_LABEL, and the LIBCALL name itself.
288       if ((Name.find(getTagName(LIBCALL)) != std::string::npos) 
289           || isMemIntrinsic(Name))
290         return true;
291  
292       return false;
293     }
294
295     // Rename the name for IL.
296     inline static std::string Rename(const std::string &Name) {
297       std::string Newname;
298       // If its a label (LIBCALL+Func+LABEL), change it to
299       // (LIBCALL+Func+IL+LABEL).
300       TAGS id = getSymbolTag(Name);
301       if (id == ARGS_LABEL || id == RET_LABEL) {
302         std::size_t pos = Name.find(getTagName(id));
303         Newname = Name.substr(0, pos) + ".IL" + getTagName(id);
304         return Newname;
305       }
306  
307       // Else, just append IL to name. 
308       return Name + ".IL";
309    }
310     
311     
312    
313
314     inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
315       if (! isLocalName(Var)) return false;
316
317       std::string Func1 = addPrefix(Func);
318       // Extract func name of the varilable.
319       const std::string &fname = getFuncNameForSym(Var);
320
321       if (fname.compare(Func1) == 0)
322         return true;
323
324       return false;
325     }
326
327
328     // Get the section for the given external symbol names.
329     // This tries to find the type (Tag) of the symbol from its mangled name
330     // and return appropriate section name for it.
331     static inline std::string getSectionNameForSym(const std::string &Sym1) {
332       std::string Sym = addPrefix(Sym1);
333
334       std::string SectionName;
335  
336       std::string Fname = getFuncNameForSym (Sym);
337       TAGS id = getSymbolTag (Sym);
338
339       switch (id) {
340         default : assert (0 && "Could not determine external symbol type");
341         case FRAME_LABEL:
342         case RET_LABEL:
343         case TEMPS_LABEL:
344         case ARGS_LABEL:  {
345           return getFrameSectionName(Fname);
346         }
347         case AUTOS_LABEL: {
348           return getAutosSectionName(Fname);
349         }
350       }
351     }
352
353     /// Return Overlay Name for the section.
354     /// The ABI Convention is: @<Color>.##.<section_tag>
355     /// The section_tag is retrieved from the SectName parameter and
356     /// and Color is passed in parameter.
357     static inline std::string  getOverlayName(std::string SectName, int Color) {
358       // FIXME: Only autos_section and frame_section are colored.
359       // So check and assert if the passed SectName does not have AUTOS_SECTION
360       // or FRAME_SECTION tag in it.
361       std::ostringstream o;
362       o << getTagName(PREFIX_SYMBOL) << Color << ".##" 
363         << SectName.substr(SectName.find("."));
364
365       return o.str();
366     } 
367
368     // Return true if the current function is an ISR
369     inline static bool isISR(const std::string SectName) {
370        if (SectName.find("interrupt") != std::string::npos)
371          return true;
372
373        return false;
374     }
375
376     // Return the address for ISR starts in rom.
377     inline static std::string getISRAddr(void) {
378       return "0x4";
379     }
380
381     // Returns the name of clone of a function.
382     static std::string getCloneFnName(const std::string &Func) {
383        return (Func + ".IL");
384     }
385
386     // Returns the name of clone of a variable.
387     static std::string getCloneVarName(const std::string &Fn, 
388                                        const std::string &Var) {
389       std::string cloneVarName = Var;
390       // These vars are named like fun.auto.var.
391       // Just replace the function name, with clone function name.
392       std::string cloneFnName = getCloneFnName(Fn);
393       cloneVarName.replace(cloneVarName.find(Fn), Fn.length(), cloneFnName);
394       return cloneVarName;
395     }
396   }; // class PAN.
397 } // end namespace llvm;
398
399 #endif