]>
Commit | Line | Data |
---|---|---|
5fd1486c | 1 | /* brig-to-generic.h -- brig to gcc generic conversion |
68edb9ba | 2 | Copyright (C) 2016-2017 Free Software Foundation, Inc. |
5fd1486c PJ |
3 | Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com> |
4 | for General Processor Tech. | |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #ifndef BRIG_TO_GENERIC_H | |
23 | #define BRIG_TO_GENERIC_H | |
24 | ||
25 | #include <string> | |
26 | #include <map> | |
27 | #include <vector> | |
28 | ||
29 | #include "config.h" | |
30 | #include "system.h" | |
31 | #include "ansidecl.h" | |
32 | #include "coretypes.h" | |
33 | #include "opts.h" | |
34 | #include "tree.h" | |
35 | #include "tree-iterator.h" | |
36 | #include "hsa-brig-format.h" | |
37 | #include "brig-function.h" | |
38 | ||
39 | ||
40 | struct reg_decl_index_entry; | |
41 | ||
42 | /* Converts an HSAIL BRIG input to GENERIC. This class holds global state | |
43 | for the translation process. Handling of the smaller pieces of BRIG data | |
44 | is delegated to various handler classes declared in | |
45 | brig-code-entry-handlers.h. */ | |
46 | ||
47 | class brig_to_generic | |
48 | { | |
49 | public: | |
50 | typedef std::map<const BrigDirectiveVariable *, tree> variable_index; | |
51 | ||
52 | private: | |
53 | typedef std::map<std::string, size_t> var_offset_table; | |
54 | typedef std::map<const BrigBase *, std::string> name_index; | |
55 | ||
56 | public: | |
57 | brig_to_generic (); | |
58 | ||
59 | void parse (const char *brig_blob); | |
60 | ||
61 | void write_globals (); | |
62 | ||
63 | std::string get_string (size_t entry_offset) const; | |
64 | ||
65 | const BrigData *get_brig_data_entry (size_t entry_offset) const; | |
66 | const BrigBase *get_brig_operand_entry (size_t entry_offset) const; | |
67 | const BrigBase *get_brig_code_entry (size_t entry_offset) const; | |
68 | ||
69 | void append_global (tree g); | |
70 | ||
71 | tree function_decl (const std::string &name); | |
72 | void add_function_decl (const std::string &name, tree func_decl); | |
73 | ||
74 | tree global_variable (const std::string &name) const; | |
75 | void add_global_variable (const std::string &name, tree var_decl); | |
76 | void add_host_def_var_ptr (const std::string &name, tree var_decl); | |
77 | ||
78 | void start_function (tree f); | |
79 | void finish_function (); | |
80 | ||
81 | void append_group_variable (const std::string &name, size_t size, | |
82 | size_t alignment); | |
83 | ||
84 | void append_private_variable (const std::string &name, size_t size, | |
85 | size_t alignment); | |
86 | ||
87 | size_t group_variable_segment_offset (const std::string &name) const; | |
88 | ||
89 | bool | |
90 | has_group_variable (const std::string &name) const; | |
91 | ||
92 | size_t | |
93 | private_variable_segment_offset (const std::string &name) const; | |
94 | ||
95 | bool | |
96 | has_private_variable (const std::string &name) const; | |
97 | ||
98 | size_t private_variable_size (const std::string &name) const; | |
99 | ||
100 | template <typename T> | |
101 | std::string | |
102 | get_mangled_name_tmpl (const T *brigVar) const; | |
103 | ||
104 | std::string get_mangled_name (const BrigDirectiveFbarrier *fbar) const | |
105 | { return get_mangled_name_tmpl (fbar); } | |
106 | std::string get_mangled_name (const BrigDirectiveVariable *var) const | |
107 | { return get_mangled_name_tmpl (var); } | |
108 | std::string get_mangled_name (const BrigDirectiveExecutable *func) const; | |
109 | ||
110 | size_t group_segment_size () const; | |
111 | size_t private_segment_size () const; | |
112 | ||
113 | brig_function *get_finished_function (tree func_decl); | |
114 | ||
115 | static tree s_fp16_type; | |
116 | static tree s_fp32_type; | |
117 | static tree s_fp64_type; | |
118 | ||
119 | /* The default rounding mode that should be used for float instructions. | |
120 | This can be set in each BRIG module header. */ | |
121 | BrigRound8_t m_default_float_rounding_mode; | |
122 | ||
123 | /* The currently built function. */ | |
124 | brig_function *m_cf; | |
125 | ||
126 | /* The name of the currently handled BRIG module. */ | |
127 | std::string m_module_name; | |
128 | ||
129 | private: | |
130 | /* The BRIG blob and its different sections of the file currently being | |
131 | parsed. */ | |
132 | const char *m_brig; | |
133 | const char *m_data; | |
134 | size_t m_data_size; | |
135 | const char *m_operand; | |
136 | size_t m_operand_size; | |
137 | const char *m_code; | |
138 | size_t m_code_size; | |
139 | ||
140 | tree m_globals; | |
141 | ||
142 | label_index m_global_variables; | |
143 | ||
144 | /* The size of each private variable, including the alignment padding. */ | |
145 | std::map<std::string, size_t> m_private_data_sizes; | |
146 | ||
147 | /* The same for group variables. */ | |
148 | size_t m_next_group_offset; | |
149 | var_offset_table m_group_offsets; | |
150 | ||
151 | /* And private. */ | |
152 | size_t m_next_private_offset; | |
153 | var_offset_table m_private_offsets; | |
154 | ||
155 | /* Name index for declared functions. */ | |
156 | label_index m_function_index; | |
157 | ||
158 | /* Stores all processed kernels in order. */ | |
159 | std::vector<brig_function *> m_kernels; | |
160 | ||
161 | /* Stores all already processed functions from the translation unit | |
162 | for some interprocedural analysis. */ | |
163 | std::map<std::string, brig_function *> m_finished_functions; | |
164 | ||
165 | /* The parsed BRIG blobs. Owned and will be deleted after use. */ | |
166 | std::vector<const char *> m_brig_blobs; | |
167 | ||
168 | /* The original dump file. */ | |
169 | FILE *m_dump_file; | |
170 | ||
171 | /* The original dump file flags. */ | |
172 | int m_dump_flags; | |
173 | }; | |
174 | ||
175 | /* Produce a "mangled name" for the given brig variable. The mangling is used | |
176 | to make unique global symbol names for module and function scope variables. | |
177 | The templated version is suitable for most of the variable types. Functions | |
178 | and kernels (BrigDirectiveExecutable) are handled with a specialized | |
179 | get_mangled_name() version. */ | |
180 | ||
181 | template <typename T> | |
182 | std::string | |
183 | brig_to_generic::get_mangled_name_tmpl (const T *brigVar) const | |
184 | { | |
185 | std::string var_name = get_string (brigVar->name).substr (1); | |
186 | ||
187 | /* Mangle the variable name using the function name and the module name | |
188 | in case of a function scope variable. */ | |
189 | if (m_cf != NULL | |
190 | && m_cf->has_function_scope_var (&brigVar->base)) | |
191 | var_name = m_cf->m_name + "." + var_name; | |
192 | ||
193 | if (brigVar->linkage == BRIG_LINKAGE_MODULE) | |
194 | var_name = "gccbrig." + m_module_name + "." + var_name; | |
195 | return var_name; | |
196 | } | |
197 | ||
198 | /* An interface to organize the different types of BRIG element handlers. */ | |
199 | ||
200 | class brig_entry_handler | |
201 | { | |
202 | public: | |
203 | brig_entry_handler (brig_to_generic &parent) : m_parent (parent) | |
204 | { | |
205 | } | |
206 | ||
207 | /* Handles the brig_code data at the given pointer and adds it to the | |
208 | currently built tree. Returns the number of consumed bytes; */ | |
209 | virtual size_t operator () (const BrigBase *base) = 0; | |
210 | ||
211 | protected: | |
212 | brig_to_generic &m_parent; | |
213 | }; | |
214 | ||
215 | tree call_builtin (tree pdecl, int nargs, tree rettype, ...); | |
216 | ||
217 | tree build_reinterpret_cast (tree destination_type, tree source); | |
218 | ||
219 | tree build_stmt (enum tree_code code, ...); | |
220 | ||
221 | tree get_unsigned_int_type (tree type); | |
222 | ||
223 | void dump_function (FILE *dump_file, brig_function *f); | |
224 | ||
225 | #endif |