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/Target/TargetMachine.h"
26 class PIC16TargetMachine;
28 class MachineCodeEmitter;
45 // A Central class to manage all ABI naming conventions.
46 // PAN - [P]ic16 [A]BI [N]ames
49 // Map the name of the symbol to its section name.
51 // -----------------------------------------------------
52 // ALL Names are prefixed with the symobl '@'.
53 // ------------------------------------------------------
54 // Global variables do not have any '.' in their names.
55 // These are maily function names and global variable names.
57 // -------------------------------------------------------
58 // Functions and auto variables.
59 // Names are mangled as <prefix><funcname>.<tag>.<varname>
60 // Where <prefix> is '@' and <tag> is any one of
62 // .auto. - an automatic var of a function.
63 // .temp. - temproray data of a function.
64 // .ret. - return value label for a function.
65 // .frame. - Frame label for a function where retval, args
66 // and temps are stored.
67 // .args. - Label used to pass arguments to a direct call.
68 // Example - Function name: @foo
69 // Its frame: @foo.frame.
70 // Its retval: @foo.ret.
71 // Its local vars: @foo.auto.a
72 // Its temp data: @foo.temp.
73 // Its arg passing: @foo.args.
74 //----------------------------------------------
75 // Libcall - compiler generated libcall names must start with .lib.
76 // This id will be used to emit extern decls for libcalls.
77 // Example - libcall name: @.lib.sra.i8
78 // To pass args: @.lib.sra.i8.args.
79 // To return val: @.lib.sra.i8.ret.
80 //----------------------------------------------
82 // uninitialized globals - @udata.<num>.#
83 // initialized globals - @idata.<num>.#
84 // Function frame - @<func>.frame_section.
85 // Function autos - @<func>.autos_section.
86 // Declarations - @section.0
87 //----------------------------------------------------------
89 // Tags used to mangle different names.
107 // Textual names of the tags.
108 inline static const char *getTagName(TAGS tag) {
111 case PREFIX_SYMBOL: return "@";
112 case AUTOS_LABEL: return ".auto.";
113 case FRAME_LABEL: return ".frame.";
114 case TEMPS_LABEL: return ".temp.";
115 case ARGS_LABEL: return ".args.";
116 case RET_LABEL: return ".ret.";
117 case LIBCALL: return ".lib.";
118 case FRAME_SECTION: return ".frame_section.";
119 case AUTOS_SECTION: return ".autos_section.";
120 case CODE_SECTION: return ".code_section.";
124 // Get tag type for the Symbol.
125 inline static TAGS getSymbolTag(const std::string &Sym) {
126 if (Sym.find(getTagName(TEMPS_LABEL)) != std::string::npos)
129 if (Sym.find(getTagName(FRAME_LABEL)) != std::string::npos)
132 if (Sym.find(getTagName(RET_LABEL)) != std::string::npos)
135 if (Sym.find(getTagName(ARGS_LABEL)) != std::string::npos)
138 if (Sym.find(getTagName(AUTOS_LABEL)) != std::string::npos)
141 if (Sym.find(getTagName(LIBCALL)) != std::string::npos)
144 // It does not have any Tag. So its a true global or static local.
145 if (Sym.find(".") == std::string::npos)
148 // If a . is there, then it may be static local.
149 // We should mangle these as well in clang.
150 if (Sym.find(".") != std::string::npos)
153 assert (0 && "Could not determine Symbol's tag");
156 // addPrefix - add prefix symbol to a name if there isn't one already.
157 inline static std::string addPrefix (const std::string &Name) {
158 std::string prefix = getTagName (PREFIX_SYMBOL);
160 // If this name already has a prefix, nothing to do.
161 if (Name.compare(0, prefix.size(), prefix) == 0)
164 return prefix + Name;
167 // Get mangled func name from a mangled sym name.
168 // In all cases func name is the first component before a '.'.
169 static inline std::string getFuncNameForSym(const std::string &Sym1) {
170 assert (getSymbolTag(Sym1) != GLOBAL && "not belongs to a function");
172 std::string Sym = addPrefix(Sym1);
174 // Position of the . after func name. That's where func name ends.
175 size_t func_name_end = Sym.find ('.');
177 return Sym.substr (0, func_name_end);
180 // Get Frame start label for a func.
181 static std::string getFrameLabel(const std::string &Func) {
182 std::string Func1 = addPrefix(Func);
183 std::string tag = getTagName(FRAME_LABEL);
187 static std::string getRetvalLabel(const std::string &Func) {
188 std::string Func1 = addPrefix(Func);
189 std::string tag = getTagName(RET_LABEL);
193 static std::string getArgsLabel(const std::string &Func) {
194 std::string Func1 = addPrefix(Func);
195 std::string tag = getTagName(ARGS_LABEL);
199 static std::string getTempdataLabel(const std::string &Func) {
200 std::string Func1 = addPrefix(Func);
201 std::string tag = getTagName(TEMPS_LABEL);
205 static std::string getFrameSectionName(const std::string &Func) {
206 std::string Func1 = addPrefix(Func);
207 std::string tag = getTagName(FRAME_SECTION);
208 return Func1 + tag + "# UDATA_OVR";
211 static std::string getAutosSectionName(const std::string &Func) {
212 std::string Func1 = addPrefix(Func);
213 std::string tag = getTagName(AUTOS_SECTION);
214 return Func1 + tag + "# UDATA_OVR";
217 static std::string getCodeSectionName(const std::string &Func) {
218 std::string Func1 = addPrefix(Func);
219 std::string tag = getTagName(CODE_SECTION);
220 return Func1 + tag + "# CODE";
223 // udata and idata section names are generated by a given number.
225 static std::string getUdataSectionName(unsigned num) {
226 std::ostringstream o;
227 o << getTagName(PREFIX_SYMBOL) << "udata." << num << ".# UDATA";
231 static std::string getIdataSectionName(unsigned num) {
232 std::ostringstream o;
233 o << getTagName(PREFIX_SYMBOL) << "idata." << num << ".# IDATA";
237 inline static bool isLocalName (const std::string &Name) {
238 if (getSymbolTag(Name) == AUTOS_LABEL)
244 inline static bool isLocalToFunc (std::string &Func, std::string &Var) {
245 if (! isLocalName(Var)) return false;
247 std::string Func1 = addPrefix(Func);
248 // Extract func name of the varilable.
249 const std::string &fname = getFuncNameForSym(Var);
251 if (fname.compare(Func1) == 0)
258 // Get the section for the given external symbol names.
259 // This tries to find the type (Tag) of the symbol from its mangled name
260 // and return appropriate section name for it.
261 static inline std::string getSectionNameForSym(const std::string &Sym1) {
262 std::string Sym = addPrefix(Sym1);
264 std::string SectionName;
266 std::string Fname = getFuncNameForSym (Sym);
267 TAGS id = getSymbolTag (Sym);
270 default : assert (0 && "Could not determine external symbol type");
275 return getFrameSectionName(Fname);
278 return getAutosSectionName(Fname);
285 // External symbol names require memory to live till the program end.
286 // So we have to allocate it and keep.
287 inline static const char *createESName (const std::string &name) {
288 char *tmpName = new char[name.size() + 1];
289 strcpy (tmpName, name.c_str());
295 inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
297 default: assert(0 && "Unknown condition code");
298 case PIC16CC::NE: return "ne";
299 case PIC16CC::EQ: return "eq";
300 case PIC16CC::LT: return "lt";
301 case PIC16CC::ULT: return "lt";
302 case PIC16CC::LE: return "le";
303 case PIC16CC::ULE: return "le";
304 case PIC16CC::GT: return "gt";
305 case PIC16CC::UGT: return "gt";
306 case PIC16CC::GE: return "ge";
307 case PIC16CC::UGE: return "ge";
311 inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
313 default: assert(0 && "Unknown condition code");
325 return false; // condition codes for unsigned comparison.
331 FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
332 FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS,
333 PIC16TargetMachine &TM,
334 CodeGenOpt::Level OptLevel,
336 // Banksel optimzer pass.
337 FunctionPass *createPIC16MemSelOptimizerPass();
338 } // end namespace llvm;
340 // Defines symbolic names for PIC16 registers. This defines a mapping from
341 // register name to register number.
342 #include "PIC16GenRegisterNames.inc"
344 // Defines symbolic names for the PIC16 instructions.
345 #include "PIC16GenInstrNames.inc"