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 // 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
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 //----------------------------------------------
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 //----------------------------------------------------------
80 // Tags used to mangle different names.
99 // Textual names of the tags.
100 inline static const char *getTagName(TAGS tag) {
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.";
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)
122 if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
125 if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
128 if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
131 if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
134 if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
137 // It does not have any Tag. So its a true global or static local.
138 if (Sym.find(".") == std::string::npos)
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)
146 assert (0 && "Could not determine Symbol's tag");
147 return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
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);
154 // If this name already has a prefix, nothing to do.
155 if (Name.compare(0, prefix.size(), prefix) == 0)
158 return prefix + Name;
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");
166 std::string Sym = addPrefix(Sym1);
168 // Position of the . after func name. That's where func name ends.
169 size_t func_name_end = Sym.find ('.');
171 return Sym.substr (0, func_name_end);
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);
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);
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);
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);
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 + "#";
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 + "#";
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 + "#";
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 + "#";
226 // udata, romdata and idata section names are generated by a given number.
228 static std::string getUdataSectionName(unsigned num,
229 std::string prefix = "") {
230 std::ostringstream o;
231 o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num
236 static std::string getRomdataSectionName() {
240 static std::string getSharedUDataSectionName() {
241 std::ostringstream o;
242 o << getTagName(PREFIX_SYMBOL) << "udata_shr" << ".#";
246 static std::string getRomdataSectionName(unsigned num,
247 std::string prefix = "") {
248 std::ostringstream o;
249 o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num
254 static std::string getIdataSectionName(unsigned num,
255 std::string prefix = "") {
256 std::ostringstream o;
257 o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num
262 inline static bool isLocalName (const std::string &Name) {
263 if (getSymbolTag(Name) == AUTOS_LABEL)
270 inline static bool isMemIntrinsic (const std::string &Name) {
271 if (Name.compare("@memcpy") == 0 || Name.compare("@memset") == 0 ||
272 Name.compare("@memmove") == 0) {
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))
295 // Rename the name for IL.
296 inline static std::string Rename(const std::string &Name) {
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);
307 // Else, just append IL to name.
314 inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
315 if (! isLocalName(Var)) return false;
317 std::string Func1 = addPrefix(Func);
318 // Extract func name of the varilable.
319 const std::string &fname = getFuncNameForSym(Var);
321 if (fname.compare(Func1) == 0)
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);
334 std::string SectionName;
336 std::string Fname = getFuncNameForSym (Sym);
337 TAGS id = getSymbolTag (Sym);
340 default : assert (0 && "Could not determine external symbol type");
345 return getFrameSectionName(Fname);
348 return getAutosSectionName(Fname);
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("."));
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)
376 // Return the address for ISR starts in rom.
377 inline static std::string getISRAddr(void) {
381 // Returns the name of clone of a function.
382 static std::string getCloneFnName(const std::string &Func) {
383 return (Func + ".IL");
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);
397 } // end namespace llvm;