blob: b25b0cc2c1855a75a670e57c38ef1c0595bac666 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===-- PIC16.h - Top-level interface for PIC16 representation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Sanjiv Gupta2d4e7f72008-05-14 06:50:01 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Sanjiv Gupta0e687712008-05-13 09:02:57 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the entry points for global functions defined in
11// the LLVM PIC16 back-end.
12//
13//===----------------------------------------------------------------------===//
14
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000015#ifndef LLVM_TARGET_PIC16_H
16#define LLVM_TARGET_PIC16_H
Sanjiv Gupta0e687712008-05-13 09:02:57 +000017
Bill Wendling98a366d2009-04-29 23:29:43 +000018#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta1b046942009-01-13 19:18:47 +000019#include <iosfwd>
20#include <cassert>
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000021#include <string>
Sanjiv Gupta1b046942009-01-13 19:18:47 +000022
Sanjiv Gupta0e687712008-05-13 09:02:57 +000023namespace llvm {
24 class PIC16TargetMachine;
Sanjiv Gupta0e687712008-05-13 09:02:57 +000025 class FunctionPass;
26 class MachineCodeEmitter;
Owen Andersoncb371882008-08-21 00:14:44 +000027 class raw_ostream;
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028
Sanjiv Gupta1b046942009-01-13 19:18:47 +000029namespace PIC16CC {
30 enum CondCodes {
31 EQ,
32 NE,
33 LT,
34 LE,
35 GT,
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000036 GE,
37 ULT,
38 UGT,
39 ULE,
40 UGE
Sanjiv Gupta1b046942009-01-13 19:18:47 +000041 };
42}
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000043 // A Central object to manage all ABI naming conventions.
44 class PIC16ABINames {
45 public:
46 // Map the name of the symbol to its section name.
47 // Current ABI:
48 // ------------------------------------------------------
49 // Global variables do not have any '.' in their names.
50 // they are prefixed with @
51 // These are maily function names and global variable names.
52 // -------------------------------------------------------
53 // Functions and auto variables.
54 // Names are mangled as <prefix><funcname>.<id>.<varname>
55 // Where prefix is a special char '@' and id is any one of
56 // the following
57 // .auto. - an automatic var of a function.
58 // .temp. - temproray data of a function.
59 // .ret. - return value label for a function.
60 // .frame. - Frame label for a function where retval, args
61 // and temps are stored.
62 // .args. - Label used to pass arguments to a direct call.
63 // Example - Function name: @foo
64 // Its frame: @foo.frame.
65 // Its retval: @foo.ret.
66 // Its local vars: @foo.auto.a
67 // Its temp data: @foo.temp.
68 // Its arg passing: @foo.args.
69 //----------------------------------------------
70 // Libcall - compiler generated libcall names must have a .lib.
71 // This id will be used to emit extern decls for libcalls.
72 // Example - libcall name: @sra_i8.lib.
73 // To pass args: @sra_i8.args.
74 // To return val: @sra_i8.ret.
75 //----------------------------------------------
76
77 enum IDs {
78 PREFIX_SYMBOL,
79
80 FUNC_AUTOS,
81 FUNC_FRAME,
82 FUNC_RET,
83 FUNC_ARGS,
84 FUNC_TEMPS,
85
86 LIBCALL,
87
88 FRAME_SECTION,
89 AUTOS_SECTION
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +000090 };
Sanjiv Guptad8d27f42009-05-06 08:02:01 +000091
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +000092 inline static const char *getIDName(IDs id) {
93 switch (id) {
94 default: assert(0 && "Unknown id");
95 case PREFIX_SYMBOL: return "@";
96 case FUNC_AUTOS: return ".auto.";
97 case FUNC_FRAME: return ".frame.";
98 case FUNC_TEMPS: return ".temp.";
99 case FUNC_ARGS: return ".args.";
100 case FUNC_RET: return ".ret.";
101 case FRAME_SECTION: return "fpdata";
102 case AUTOS_SECTION: return "fadata";
103 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000104 }
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000105
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000106 inline static IDs getID(const std::string &Sym) {
107 if (Sym.find(getIDName(FUNC_TEMPS)))
108 return FUNC_TEMPS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000109
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000110 if (Sym.find(getIDName(FUNC_FRAME)))
111 return FUNC_FRAME;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000112
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000113 if (Sym.find(getIDName(FUNC_RET)))
114 return FUNC_RET;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000115
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000116 if (Sym.find(getIDName(FUNC_ARGS)))
117 return FUNC_ARGS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000118
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000119 if (Sym.find(getIDName(FUNC_AUTOS)))
120 return FUNC_AUTOS;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000121
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000122 if (Sym.find(getIDName(LIBCALL)))
123 return LIBCALL;
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000124
Sanjiv Gupta573eb5e2009-05-08 04:50:14 +0000125 // It does not have any ID. So its a global.
126 assert (0 && "Could not determine ID symbol type");
127 }
128
129 // Get func name from a mangled name.
130 // In all cases func name is the first component before a '.'.
131 static inline std::string getFuncNameForSym(const std::string &Sym) {
132 const char *prefix = getIDName (PREFIX_SYMBOL);
133
134 // If this name has a prefix, func name start after prfix in that case.
135 size_t func_name_start = 0;
136 if (Sym.find(prefix, 0, strlen(prefix)) != std::string::npos)
137 func_name_start = strlen(prefix);
138
139 // Position of the . after func name. That's where func name ends.
140 size_t func_name_end = Sym.find ('.', func_name_start);
141
142 return Sym.substr (func_name_start, func_name_end);
143 }
144
145 // Form a section name given the section type and func name.
146 static std::string
147 getSectionNameForFunc (const std::string &Fname, const IDs sec_id) {
148 std::string sec_id_string = getIDName(sec_id);
149 return sec_id_string + "." + Fname + ".#";
150 }
151
152 // Get the section for the given external symbol names.
153 // This tries to find the type (ID) of the symbol from its mangled name
154 // and return appropriate section name for it.
155 static inline std::string getSectionNameForSym(const std::string &Sym) {
156 std::string SectionName;
157
158 IDs id = getID (Sym);
159 std::string Fname = getFuncNameForSym (Sym);
160
161 switch (id) {
162 default : assert (0 && "Could not determine external symbol type");
163 case FUNC_FRAME:
164 case FUNC_RET:
165 case FUNC_TEMPS:
166 case FUNC_ARGS: {
167 return getSectionNameForFunc (Fname, FRAME_SECTION);
168 }
169 case FUNC_AUTOS: {
170 return getSectionNameForFunc (Fname, AUTOS_SECTION);
171 }
172 }
173 }
174 }; // class PIC16ABINames.
175
176
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000177
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000178
179 inline static const char *PIC16CondCodeToString(PIC16CC::CondCodes CC) {
180 switch (CC) {
181 default: assert(0 && "Unknown condition code");
182 case PIC16CC::NE: return "ne";
183 case PIC16CC::EQ: return "eq";
184 case PIC16CC::LT: return "lt";
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000185 case PIC16CC::ULT: return "lt";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000186 case PIC16CC::LE: return "le";
187 case PIC16CC::GT: return "gt";
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000188 case PIC16CC::UGT: return "gt";
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000189 case PIC16CC::GE: return "ge";
190 }
191 }
192
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000193 inline static bool isSignedComparison(PIC16CC::CondCodes CC) {
194 switch (CC) {
195 default: assert(0 && "Unknown condition code");
196 case PIC16CC::NE:
197 case PIC16CC::EQ:
198 case PIC16CC::LT:
199 case PIC16CC::LE:
200 case PIC16CC::GE:
201 case PIC16CC::GT:
202 return true;
203 case PIC16CC::ULT:
204 case PIC16CC::UGT:
205 case PIC16CC::ULE:
206 case PIC16CC::UGE:
207 return false; // condition codes for unsigned comparison.
208 }
209 }
210
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000211
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000212
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000213 FunctionPass *createPIC16ISelDag(PIC16TargetMachine &TM);
Owen Andersoncb371882008-08-21 00:14:44 +0000214 FunctionPass *createPIC16CodePrinterPass(raw_ostream &OS,
Bill Wendling57f0db82009-02-24 08:30:20 +0000215 PIC16TargetMachine &TM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000216 CodeGenOpt::Level OptLevel,
217 bool Verbose);
Sanjiv Guptad8d27f42009-05-06 08:02:01 +0000218 // Banksel optimzer pass.
219 FunctionPass *createPIC16MemSelOptimizerPass();
220 std::string getSectionNameForSym(const std::string &Sym);
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000221} // end namespace llvm;
222
223// Defines symbolic names for PIC16 registers. This defines a mapping from
224// register name to register number.
225#include "PIC16GenRegisterNames.inc"
226
227// Defines symbolic names for the PIC16 instructions.
228#include "PIC16GenInstrNames.inc"
229
230#endif