Add missing #include for "strlen" which is used inline in this header. Fixes
[oota-llvm.git] / lib / Target / PIC16 / PIC16.h
1 //===-- PIC16.h - Top-level interface for PIC16 representation --*- 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 entry points for global functions defined in 
11 // the LLVM PIC16 back-end.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_PIC16_H
16 #define LLVM_TARGET_PIC16_H
17
18 #include "llvm/Target/TargetMachine.h"
19 #include <iosfwd>
20 #include <cassert>
21 #include <cstring>
22 #include <string>
23
24 namespace llvm {
25   class PIC16TargetMachine;
26   class FunctionPass;
27   class MachineCodeEmitter;
28   class raw_ostream;
29
30 namespace PIC16CC {
31   enum CondCodes {
32     EQ,
33     NE,
34     LT,
35     LE,
36     GT,
37     GE,
38     ULT,
39     UGT,
40     ULE,
41     UGE
42   };
43 }
44   // A Central object to manage all ABI naming conventions.
45   class PIC16ABINames {
46     public:
47     // Map the name of the symbol to its section name.
48     // Current ABI:
49     // ------------------------------------------------------
50     // Global variables do not have any '.' in their names.
51     // they are prefixed with @
52     // These are maily function names and global variable names.
53     // -------------------------------------------------------
54     // Functions and auto variables.
55     // Names are mangled as <prefix><funcname>.<id>.<varname>
56     // Where prefix is a special char '@' and id is any one of
57     // the following
58     // .auto. - an automatic var of a function.
59     // .temp. - temproray data of a function.
60     // .ret.  - return value label for a function.
61     // .frame. - Frame label for a function where retval, args
62     //           and temps are stored.
63     // .args. - Label used to pass arguments to a direct call.
64     // Example - Function name:   @foo
65     //           Its frame:       @foo.frame.
66     //           Its retval:      @foo.ret.
67     //           Its local vars:  @foo.auto.a
68     //           Its temp data:   @foo.temp.
69     //           Its arg passing: @foo.args.
70     //----------------------------------------------
71     // Libcall - compiler generated libcall names must have a .lib.
72     //           This id will be used to emit extern decls for libcalls.
73     // Example - libcall name:   @sra_i8.lib.
74     //           To pass args:   @sra_i8.args.
75     //           To return val:  @sra_i8.ret.
76     //----------------------------------------------
77     
78     enum IDs {
79       PREFIX_SYMBOL,
80
81       FUNC_AUTOS,
82       FUNC_FRAME,
83       FUNC_RET,
84       FUNC_ARGS,
85       FUNC_TEMPS,
86       
87       LIBCALL,
88       
89       FRAME_SECTION,
90       AUTOS_SECTION
91     };
92
93     inline static const char *getIDName(IDs id) {
94       switch (id) {
95       default: assert(0 && "Unknown id");
96       case PREFIX_SYMBOL:    return "@";
97       case FUNC_AUTOS:       return ".auto.";
98       case FUNC_FRAME:       return ".frame.";
99       case FUNC_TEMPS:       return ".temp.";
100       case FUNC_ARGS:       return ".args.";
101       case FUNC_RET:       return ".ret.";
102       case FRAME_SECTION:       return "fpdata";
103       case AUTOS_SECTION:       return "fadata";
104       }
105     }
106
107     inline static IDs getID(const std::string &Sym) {
108       if (Sym.find(getIDName(FUNC_TEMPS)))
109         return FUNC_TEMPS;
110
111       if (Sym.find(getIDName(FUNC_FRAME)))
112         return FUNC_FRAME;
113
114       if (Sym.find(getIDName(FUNC_RET)))
115         return FUNC_RET;
116
117       if (Sym.find(getIDName(FUNC_ARGS)))
118         return FUNC_ARGS;
119
120       if (Sym.find(getIDName(FUNC_AUTOS)))
121         return FUNC_AUTOS;
122
123       if (Sym.find(getIDName(LIBCALL)))
124         return LIBCALL;
125
126       // It does not have any ID. So its a global.
127       assert (0 && "Could not determine ID symbol type");
128     }
129
130     // Get func name from a mangled name.
131     // In all cases func name is the first component before a '.'.
132     static inline std::string getFuncNameForSym(const std::string &Sym) {
133       const char *prefix = getIDName (PREFIX_SYMBOL);
134
135       // If this name has a prefix, func name start after prfix in that case.
136       size_t func_name_start = 0;
137       if (Sym.find(prefix, 0, strlen(prefix)) != std::string::npos)
138         func_name_start = strlen(prefix);
139
140       // Position of the . after func name. That's where func name ends.
141       size_t func_name_end = Sym.find ('.', func_name_start);
142
143       return Sym.substr (func_name_start, func_name_end);
144     }
145
146     // Form a section name given the section type and func name.
147     static std::string
148     getSectionNameForFunc (const std::string &Fname, const IDs sec_id) {
149       std::string sec_id_string = getIDName(sec_id);
150       return sec_id_string + "." + Fname + ".#";
151     }
152
153     // Get the section for the given external symbol names.
154     // This tries to find the type (ID) of the symbol from its mangled name
155     // and return appropriate section name for it.
156     static inline std::string getSectionNameForSym(const std::string &Sym) {
157       std::string SectionName;
158  
159       IDs id = getID (Sym);
160       std::string Fname = getFuncNameForSym (Sym);
161
162       switch (id) {
163         default : assert (0 && "Could not determine external symbol type");
164         case FUNC_FRAME:
165         case FUNC_RET:
166         case FUNC_TEMPS:
167         case FUNC_ARGS:  {
168           return getSectionNameForFunc (Fname, FRAME_SECTION);
169         }
170         case FUNC_AUTOS: {
171           return getSectionNameForFunc (Fname, AUTOS_SECTION);
172         }
173       }
174     }
175   }; // class PIC16ABINames.
176
177
178
179
180   inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
181     switch (CC) {
182     default: assert(0 && "Unknown condition code");
183     case PIC16CC::NE:  return "ne";
184     case PIC16CC::EQ:   return "eq";
185     case PIC16CC::LT:   return "lt";
186     case PIC16CC::ULT:   return "lt";
187     case PIC16CC::LE:  return "le";
188     case PIC16CC::GT:  return "gt";
189     case PIC16CC::UGT:  return "gt";
190     case PIC16CC::GE:   return "ge";
191     }
192   }
193
194   inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
195     switch (CC) {
196     default: assert(0 && "Unknown condition code");
197     case PIC16CC::NE:  
198     case PIC16CC::EQ: 
199     case PIC16CC::LT:
200     case PIC16CC::LE:
201     case PIC16CC::GE:
202     case PIC16CC::GT:
203       return true;
204     case PIC16CC::ULT:
205     case PIC16CC::UGT:
206     case PIC16CC::ULE:
207     case PIC16CC::UGE:
208       return false;   // condition codes for unsigned comparison. 
209     }
210   }
211
212
213
214   FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
215   FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS, 
216                                            PIC16TargetMachine &TM,
217                                            CodeGenOpt::Level OptLevel,
218                                            bool Verbose);
219   // Banksel optimzer pass.
220   FunctionPass *createPIC16MemSelOptimizerPass();
221   std::string getSectionNameForSym(const std::string &Sym);
222 } // end namespace llvm;
223
224 // Defines symbolic names for PIC16 registers.  This defines a mapping from
225 // register name to register number.
226 #include "PIC16GenRegisterNames.inc"
227
228 // Defines symbolic names for the PIC16 instructions.
229 #include "PIC16GenInstrNames.inc"
230
231 #endif