1 //===-- PIC16ABINames.h - PIC16 Naming conventios for ABI----- --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the functions to manage ABI Naming conventions for PIC16.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TARGET_PIC16ABINAMES_H
16 #define LLVM_TARGET_PIC16ABINAMES_H
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Target/TargetMachine.h"
26 class PIC16TargetMachine;
28 class MachineCodeEmitter;
29 class formatted_raw_ostream;
31 // A Central class to manage all ABI naming conventions.
32 // PAN - [P]ic16 [A]BI [N]ames
35 // Map the name of the symbol to its section name.
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.
43 // -------------------------------------------------------
44 // Functions and auto variables.
45 // Names are mangled as <prefix><funcname>.<tag>.<varname>
46 // Where <prefix> is '@' and <tag> is any one of
48 // .auto. - an automatic var of a function.
49 // .temp. - temproray data of a function.
50 // .ret. - return value label for a function.
51 // .frame. - Frame label for a function where retval, args
52 // and temps are stored.
53 // .args. - Label used to pass arguments to a direct call.
54 // Example - Function name: @foo
55 // Its frame: @foo.frame.
56 // Its retval: @foo.ret.
57 // Its local vars: @foo.auto.a
58 // Its temp data: @foo.temp.
59 // Its arg passing: @foo.args.
60 //----------------------------------------------
61 // Libcall - compiler generated libcall names must start with .lib.
62 // This id will be used to emit extern decls for libcalls.
63 // Example - libcall name: @.lib.sra.i8
64 // To pass args: @.lib.sra.i8.args.
65 // To return val: @.lib.sra.i8.ret.
66 //----------------------------------------------
68 // uninitialized globals - @udata.<num>.#
69 // initialized globals - @idata.<num>.#
70 // Function frame - @<func>.frame_section.
71 // Function autos - @<func>.autos_section.
72 // Declarations - Enclosed in comments. No section for them.
73 //----------------------------------------------------------
75 // Tags used to mangle different names.
94 // Textual names of the tags.
95 inline static const char *getTagName(TAGS tag) {
98 case PREFIX_SYMBOL: return "@";
99 case AUTOS_LABEL: return ".auto.";
100 case FRAME_LABEL: return ".frame.";
101 case TEMPS_LABEL: return ".temp.";
102 case ARGS_LABEL: return ".args.";
103 case RET_LABEL: return ".ret.";
104 case LIBCALL: return ".lib.";
105 case FRAME_SECTION: return ".frame_section.";
106 case AUTOS_SECTION: return ".autos_section.";
107 case CODE_SECTION: return ".code_section.";
108 case USER_SECTION: return ".user_section.";
112 // Get tag type for the Symbol.
113 inline static TAGS getSymbolTag(const std::string &Sym) {
114 if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
117 if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
120 if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
123 if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
126 if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
129 if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
132 // It does not have any Tag. So its a true global or static local.
133 if (Sym.find(".") == std::string::npos)
136 // If a . is there, then it may be static local.
137 // We should mangle these as well in clang.
138 if (Sym.find(".") != std::string::npos)
141 assert (0 && "Could not determine Symbol's tag");
142 return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
145 // addPrefix - add prefix symbol to a name if there isn't one already.
146 inline static std::string addPrefix (const std::string &Name) {
147 std::string prefix = getTagName (PREFIX_SYMBOL);
149 // If this name already has a prefix, nothing to do.
150 if (Name.compare(0, prefix.size(), prefix) == 0)
153 return prefix + Name;
156 // Get mangled func name from a mangled sym name.
157 // In all cases func name is the first component before a '.'.
158 static inline std::string getFuncNameForSym(const std::string &Sym1) {
159 assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
161 std::string Sym = addPrefix(Sym1);
163 // Position of the . after func name. That's where func name ends.
164 size_t func_name_end = Sym.find ('.');
166 return Sym.substr (0, func_name_end);
169 // Get Frame start label for a func.
170 static std::string getFrameLabel(const std::string &Func) {
171 std::string Func1 = addPrefix(Func);
172 std::string tag = getTagName(FRAME_LABEL);
176 static std::string getRetvalLabel(const std::string &Func) {
177 std::string Func1 = addPrefix(Func);
178 std::string tag = getTagName(RET_LABEL);
182 static std::string getArgsLabel(const std::string &Func) {
183 std::string Func1 = addPrefix(Func);
184 std::string tag = getTagName(ARGS_LABEL);
188 static std::string getTempdataLabel(const std::string &Func) {
189 std::string Func1 = addPrefix(Func);
190 std::string tag = getTagName(TEMPS_LABEL);
194 static std::string getFrameSectionName(const std::string &Func) {
195 std::string Func1 = addPrefix(Func);
196 std::string tag = getTagName(FRAME_SECTION);
197 return Func1 + tag + "#";
200 static std::string getAutosSectionName(const std::string &Func) {
201 std::string Func1 = addPrefix(Func);
202 std::string tag = getTagName(AUTOS_SECTION);
203 return Func1 + tag + "#";
206 static std::string getCodeSectionName(const std::string &Func) {
207 std::string Func1 = addPrefix(Func);
208 std::string tag = getTagName(CODE_SECTION);
209 return Func1 + tag + "#";
212 static std::string getUserSectionName(const std::string &Name) {
213 std::string sname = addPrefix(Name);;
214 std::string tag = getTagName(USER_SECTION);
215 return sname + tag + "#";
218 // udata, romdata and idata section names are generated by a given number.
220 static std::string getUdataSectionName(unsigned num,
221 std::string prefix = "") {
222 std::ostringstream o;
223 o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num
228 static std::string getRomdataSectionName() {
232 static std::string getRomdataSectionName(unsigned num,
233 std::string prefix = "") {
234 std::ostringstream o;
235 o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num
240 static std::string getIdataSectionName(unsigned num,
241 std::string prefix = "") {
242 std::ostringstream o;
243 o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num
248 inline static bool isLocalName (const std::string &Name) {
249 if (getSymbolTag(Name) == AUTOS_LABEL)
255 inline static bool isMemIntrinsic (const std::string &Name) {
256 if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
257 Name.compare("@memmove") == 0) {
264 inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
265 if (! isLocalName(Var)) return false;
267 std::string Func1 = addPrefix(Func);
268 // Extract func name of the varilable.
269 const std::string &fname = getFuncNameForSym(Var);
271 if (fname.compare(Func1) == 0)
278 // Get the section for the given external symbol names.
279 // This tries to find the type (Tag) of the symbol from its mangled name
280 // and return appropriate section name for it.
281 static inline std::string getSectionNameForSym(const std::string &Sym1) {
282 std::string Sym = addPrefix(Sym1);
284 std::string SectionName;
286 std::string Fname = getFuncNameForSym (Sym);
287 TAGS id = getSymbolTag (Sym);
290 default : assert (0 && "Could not determine external symbol type");
295 return getFrameSectionName(Fname);
298 return getAutosSectionName(Fname);
303 } // end namespace llvm;