blob: 00e9e457144b1c038feac0e963100c0ee685ae62 [file] [log] [blame]
Benjamin Kramere62d85d2009-10-15 19:46:34 +00001//===-- 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
25namespace 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 // -------------------------------------------------------
44 // Functions and auto variables.
45 // Names are mangled as <prefix><funcname>.<tag>.<varname>
46 // Where <prefix> is '@' and <tag> is any one of
47 // the following
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 //----------------------------------------------
67 // SECTION Names
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 //----------------------------------------------------------
74
75 // Tags used to mangle different names.
76 enum TAGS {
77 PREFIX_SYMBOL,
78 GLOBAL,
79 STATIC_LOCAL,
80 AUTOS_LABEL,
81 FRAME_LABEL,
82 RET_LABEL,
83 ARGS_LABEL,
84 TEMPS_LABEL,
85
86 LIBCALL,
87
88 FRAME_SECTION,
89 AUTOS_SECTION,
90 CODE_SECTION,
91 USER_SECTION
92 };
93
94 // Textual names of the tags.
95 inline static const char *getTagName(TAGS tag) {
96 switch (tag) {
97 default: return "";
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.";
109 }
110 }
111
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)
115 return TEMPS_LABEL;
116
117 if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
118 return FRAME_LABEL;
119
120 if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
121 return RET_LABEL;
122
123 if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
124 return ARGS_LABEL;
125
126 if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
127 return AUTOS_LABEL;
128
129 if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
130 return LIBCALL;
131
132 // It does not have any Tag. So its a true global or static local.
133 if (Sym.find(".") == std::string::npos)
134 return GLOBAL;
135
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)
139 return STATIC_LOCAL;
140
141 assert (0 && "Could not determine Symbol's tag");
142 return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
143 }
144
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);
148
149 // If this name already has a prefix, nothing to do.
150 if (Name.compare(0, prefix.size(), prefix) == 0)
151 return Name;
152
153 return prefix + Name;
154 }
155
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");
160
161 std::string Sym = addPrefix(Sym1);
162
163 // Position of the . after func name. That's where func name ends.
164 size_t func_name_end = Sym.find ('.');
165
166 return Sym.substr (0, func_name_end);
167 }
168
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);
173 return Func1 + tag;
174 }
175
176 static std::string getRetvalLabel(const std::string &Func) {
177 std::string Func1 = addPrefix(Func);
178 std::string tag = getTagName(RET_LABEL);
179 return Func1 + tag;
180 }
181
182 static std::string getArgsLabel(const std::string &Func) {
183 std::string Func1 = addPrefix(Func);
184 std::string tag = getTagName(ARGS_LABEL);
185 return Func1 + tag;
186 }
187
188 static std::string getTempdataLabel(const std::string &Func) {
189 std::string Func1 = addPrefix(Func);
190 std::string tag = getTagName(TEMPS_LABEL);
191 return Func1 + tag;
192 }
193
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 + "#";
198 }
199
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 + "#";
204 }
205
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 + "#";
210 }
211
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 + "#";
216 }
217
218 // udata, romdata and idata section names are generated by a given number.
219 // @udata.<num>.#
220 static std::string getUdataSectionName(unsigned num,
221 std::string prefix = "") {
222 std::ostringstream o;
223 o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num
224 << ".#";
225 return o.str();
226 }
227
228 static std::string getRomdataSectionName() {
229 return "romdata.#";
230 }
231
232 static std::string getRomdataSectionName(unsigned num,
233 std::string prefix = "") {
234 std::ostringstream o;
235 o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num
236 << ".#";
237 return o.str();
238 }
239
240 static std::string getIdataSectionName(unsigned num,
241 std::string prefix = "") {
242 std::ostringstream o;
243 o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num
244 << ".#";
245 return o.str();
246 }
247
248 inline static bool isLocalName (const std::string &Name) {
249 if (getSymbolTag(Name) == AUTOS_LABEL)
250 return true;
251
252 return false;
253 }
254
255 inline static bool isMemIntrinsic (const std::string &Name) {
256 if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
257 Name.compare("@memmove") == 0) {
258 return true;
259 }
260
261 return false;
262 }
263
264 inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
265 if (! isLocalName(Var)) return false;
266
267 std::string Func1 = addPrefix(Func);
268 // Extract func name of the varilable.
269 const std::string &fname = getFuncNameForSym(Var);
270
271 if (fname.compare(Func1) == 0)
272 return true;
273
274 return false;
275 }
276
277
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);
283
284 std::string SectionName;
285
286 std::string Fname = getFuncNameForSym (Sym);
287 TAGS id = getSymbolTag (Sym);
288
289 switch (id) {
290 default : assert (0 && "Could not determine external symbol type");
291 case FRAME_LABEL:
292 case RET_LABEL:
293 case TEMPS_LABEL:
294 case ARGS_LABEL: {
295 return getFrameSectionName(Fname);
296 }
297 case AUTOS_LABEL: {
298 return getAutosSectionName(Fname);
299 }
300 }
301 }
302 }; // class PAN.
303} // end namespace llvm;
304
305#endif