1 //===-- PIC16.h - Top-level interface for PIC16 representation --*- 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 entry points for global functions defined in
11 // the LLVM PIC16 back-end.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TARGET_PIC16_H
16 #define LLVM_TARGET_PIC16_H
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Target/TargetMachine.h"
27 class PIC16TargetMachine;
29 class MachineCodeEmitter;
30 class formatted_raw_ostream;
46 // A Central class to manage all ABI naming conventions.
47 // PAN - [P]ic16 [A]BI [N]ames
50 // Map the name of the symbol to its section name.
52 // -----------------------------------------------------
53 // ALL Names are prefixed with the symobl '@'.
54 // ------------------------------------------------------
55 // Global variables do not have any '.' in their names.
56 // These are maily function names and global variable names.
58 // -------------------------------------------------------
59 // Functions and auto variables.
60 // Names are mangled as <prefix><funcname>.<tag>.<varname>
61 // Where <prefix> is '@' and <tag> is any one of
63 // .auto. - an automatic var of a function.
64 // .temp. - temproray data of a function.
65 // .ret. - return value label for a function.
66 // .frame. - Frame label for a function where retval, args
67 // and temps are stored.
68 // .args. - Label used to pass arguments to a direct call.
69 // Example - Function name: @foo
70 // Its frame: @foo.frame.
71 // Its retval: @foo.ret.
72 // Its local vars: @foo.auto.a
73 // Its temp data: @foo.temp.
74 // Its arg passing: @foo.args.
75 //----------------------------------------------
76 // Libcall - compiler generated libcall names must start with .lib.
77 // This id will be used to emit extern decls for libcalls.
78 // Example - libcall name: @.lib.sra.i8
79 // To pass args: @.lib.sra.i8.args.
80 // To return val: @.lib.sra.i8.ret.
81 //----------------------------------------------
83 // uninitialized globals - @udata.<num>.#
84 // initialized globals - @idata.<num>.#
85 // Function frame - @<func>.frame_section.
86 // Function autos - @<func>.autos_section.
87 // Declarations - Enclosed in comments. No section for them.
88 //----------------------------------------------------------
90 // Tags used to mangle different names.
108 // Textual names of the tags.
109 inline static const char *getTagName(TAGS tag) {
112 case PREFIX_SYMBOL: return "@";
113 case AUTOS_LABEL: return ".auto.";
114 case FRAME_LABEL: return ".frame.";
115 case TEMPS_LABEL: return ".temp.";
116 case ARGS_LABEL: return ".args.";
117 case RET_LABEL: return ".ret.";
118 case LIBCALL: return ".lib.";
119 case FRAME_SECTION: return ".frame_section.";
120 case AUTOS_SECTION: return ".autos_section.";
121 case CODE_SECTION: return ".code_section.";
125 // Get tag type for the Symbol.
126 inline static TAGS getSymbolTag(const std::string &Sym) {
127 if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
130 if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
133 if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
136 if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
139 if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
142 if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
145 // It does not have any Tag. So its a true global or static local.
146 if (Sym.find(".") == std::string::npos)
149 // If a . is there, then it may be static local.
150 // We should mangle these as well in clang.
151 if (Sym.find(".") != std::string::npos)
154 assert (0 && "Could not determine Symbol's tag");
155 return PREFIX_SYMBOL; // Silence warning when assertions are turned off.
158 // addPrefix - add prefix symbol to a name if there isn't one already.
159 inline static std::string addPrefix (const std::string &Name) {
160 std::string prefix = getTagName (PREFIX_SYMBOL);
162 // If this name already has a prefix, nothing to do.
163 if (Name.compare(0, prefix.size(), prefix) == 0)
166 return prefix + Name;
169 // Get mangled func name from a mangled sym name.
170 // In all cases func name is the first component before a '.'.
171 static inline std::string getFuncNameForSym(const std::string &Sym1) {
172 assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
174 std::string Sym = addPrefix(Sym1);
176 // Position of the . after func name. That's where func name ends.
177 size_t func_name_end = Sym.find ('.');
179 return Sym.substr (0, func_name_end);
182 // Get Frame start label for a func.
183 static std::string getFrameLabel(const std::string &Func) {
184 std::string Func1 = addPrefix(Func);
185 std::string tag = getTagName(FRAME_LABEL);
189 static std::string getRetvalLabel(const std::string &Func) {
190 std::string Func1 = addPrefix(Func);
191 std::string tag = getTagName(RET_LABEL);
195 static std::string getArgsLabel(const std::string &Func) {
196 std::string Func1 = addPrefix(Func);
197 std::string tag = getTagName(ARGS_LABEL);
201 static std::string getTempdataLabel(const std::string &Func) {
202 std::string Func1 = addPrefix(Func);
203 std::string tag = getTagName(TEMPS_LABEL);
207 static std::string getFrameSectionName(const std::string &Func) {
208 std::string Func1 = addPrefix(Func);
209 std::string tag = getTagName(FRAME_SECTION);
210 return Func1 + tag + "# UDATA_OVR";
213 static std::string getAutosSectionName(const std::string &Func) {
214 std::string Func1 = addPrefix(Func);
215 std::string tag = getTagName(AUTOS_SECTION);
216 return Func1 + tag + "# UDATA_OVR";
219 static std::string getCodeSectionName(const std::string &Func) {
220 std::string Func1 = addPrefix(Func);
221 std::string tag = getTagName(CODE_SECTION);
222 return Func1 + tag + "# CODE";
225 // udata, romdata and idata section names are generated by a given number.
227 static std::string getUdataSectionName(unsigned num,
228 std::string prefix = "") {
229 std::ostringstream o;
230 o << getTagName(PREFIX_SYMBOL) << prefix << "udata." << num
235 static std::string getRomdataSectionName(unsigned num,
236 std::string prefix = "") {
237 std::ostringstream o;
238 o << getTagName(PREFIX_SYMBOL) << prefix << "romdata." << num
243 static std::string getIdataSectionName(unsigned num,
244 std::string prefix = "") {
245 std::ostringstream o;
246 o << getTagName(PREFIX_SYMBOL) << prefix << "idata." << num
251 inline static bool isLocalName (const std::string &Name) {
252 if (getSymbolTag(Name) == AUTOS_LABEL)
258 // FIXME: currently we track both @memcpy and memcpy, as
259 // the first one is generated by clang, and the second one by codegen
260 // while lowering intrinsics. One we fix codegen to use RTLIB, we can
261 // have only @memcpy here.
262 inline static bool isMemIntrinsic (const std::string &Name) {
263 if (Name.compare("@memcpy") == 0 || Name.compare("memcpy") == 0 ||
264 Name.compare("@memset") == 0 || Name.compare("memset") == 0 ||
265 Name.compare("@memmove") == 0 || Name.compare("memmove") == 0) {
272 inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
273 if (! isLocalName(Var)) return false;
275 std::string Func1 = addPrefix(Func);
276 // Extract func name of the varilable.
277 const std::string &fname = getFuncNameForSym(Var);
279 if (fname.compare(Func1) == 0)
286 // Get the section for the given external symbol names.
287 // This tries to find the type (Tag) of the symbol from its mangled name
288 // and return appropriate section name for it.
289 static inline std::string getSectionNameForSym(const std::string &Sym1) {
290 std::string Sym = addPrefix(Sym1);
292 std::string SectionName;
294 std::string Fname = getFuncNameForSym (Sym);
295 TAGS id = getSymbolTag (Sym);
298 default : assert (0 && "Could not determine external symbol type");
303 return getFrameSectionName(Fname);
306 return getAutosSectionName(Fname);
313 // External symbol names require memory to live till the program end.
314 // So we have to allocate it and keep.
315 inline static const char *createESName (const std::string &name) {
316 char *tmpName = new char[name.size() + 1];
317 strcpy (tmpName, name.c_str());
323 inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
325 default: llvm_unreachable("Unknown condition code");
326 case PIC16CC::NE: return "ne";
327 case PIC16CC::EQ: return "eq";
328 case PIC16CC::LT: return "lt";
329 case PIC16CC::ULT: return "lt";
330 case PIC16CC::LE: return "le";
331 case PIC16CC::ULE: return "le";
332 case PIC16CC::GT: return "gt";
333 case PIC16CC::UGT: return "gt";
334 case PIC16CC::GE: return "ge";
335 case PIC16CC::UGE: return "ge";
339 inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
341 default: llvm_unreachable("Unknown condition code");
353 return false; // condition codes for unsigned comparison.
359 FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
360 // Banksel optimizer pass.
361 FunctionPass *createPIC16MemSelOptimizerPass();
363 extern Target ThePIC16Target;
364 extern Target TheCooperTarget;
366 } // end namespace llvm;
368 // Defines symbolic names for PIC16 registers. This defines a mapping from
369 // register name to register number.
370 #include "PIC16GenRegisterNames.inc"
372 // Defines symbolic names for the PIC16 instructions.
373 #include "PIC16GenInstrNames.inc"