Fix version check for ATTRIBUTE_GCC_DUMP_PRINTF
[official-gcc.git] / gcc / brig / brigfrontend / brig-to-generic.h
bloba3eb4329a8221e52654a491f781e4b4f38eb8bc6
1 /* brig-to-generic.h -- brig to gcc generic conversion
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
4 for General Processor Tech.
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef BRIG_TO_GENERIC_H
23 #define BRIG_TO_GENERIC_H
25 #include <string>
26 #include <map>
27 #include <vector>
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"
39 struct reg_decl_index_entry;
41 /* Converts an HSAIL BRIG input to GENERIC. This class holds global state
42 for the translation process. Handling of the smaller pieces of BRIG data
43 is delegated to various handler classes declared in
44 brig-code-entry-handlers.h. */
46 class brig_to_generic
48 public:
49 typedef std::map<const BrigDirectiveVariable *, tree> variable_index;
51 private:
52 typedef std::map<std::string, size_t> var_offset_table;
53 typedef std::map<const BrigBase *, std::string> name_index;
55 public:
56 brig_to_generic ();
58 void analyze (const char *brig_blob);
59 void parse (const char *brig_blob);
61 void write_globals ();
63 std::string get_string (size_t entry_offset) const;
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;
69 void append_global (tree g);
71 tree function_decl (const std::string &name);
72 void add_function_decl (const std::string &name, tree func_decl);
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 void add_decl_call (tree call);
79 void start_function (tree f);
80 void finish_function ();
82 void append_private_variable (const std::string &name, size_t size,
83 size_t alignment);
85 size_t
86 private_variable_segment_offset (const std::string &name) const;
88 bool
89 has_private_variable (const std::string &name) const;
91 size_t private_variable_size (const std::string &name) const;
93 template <typename T>
94 std::string
95 get_mangled_name_tmpl (const T *brigVar) const;
97 std::string get_mangled_name (const BrigDirectiveFbarrier *fbar) const
98 { return get_mangled_name_tmpl (fbar); }
99 std::string get_mangled_name (const BrigDirectiveVariable *var) const
100 { return get_mangled_name_tmpl (var); }
101 std::string get_mangled_name (const BrigDirectiveExecutable *func) const;
103 size_t private_segment_size () const;
105 brig_function *get_finished_function (tree func_decl);
107 void add_group_variable (const std::string &name, size_t size,
108 size_t alignment, bool function_scope);
110 void add_reg_used_as_type (const BrigOperandRegister &brig_reg,
111 tree operand_type);
113 static tree s_fp16_type;
114 static tree s_fp32_type;
115 static tree s_fp64_type;
117 /* The default rounding mode that should be used for float instructions.
118 This can be set in each BRIG module header. */
119 BrigRound8_t m_default_float_rounding_mode;
121 /* The currently built function. */
122 brig_function *m_cf;
124 /* Stores the module and function scope group variable offsets. */
125 group_variable_offset_index m_module_group_variables;
127 /* The name of the currently handled BRIG module. */
128 std::string m_module_name;
130 /* Set to true if the compilation is in 'analyze' phase. */
131 bool m_analyzing;
133 /* Accumulates the total group segment usage. */
134 size_t m_total_group_segment_usage;
136 /* Statistics about register uses per function. */
137 std::map<std::string, regs_use_index> m_fn_regs_use_index;
139 private:
141 void find_brig_sections ();
142 /* The BRIG blob and its different sections of the file currently being
143 parsed. */
144 const char *m_brig;
145 const char *m_data;
146 size_t m_data_size;
147 const char *m_operand;
148 size_t m_operand_size;
149 const char *m_code;
150 size_t m_code_size;
152 tree m_globals;
154 label_index m_global_variables;
156 /* Calls to declarations to be fixed in the end of processing to call
157 defs instead. */
158 std::vector<tree> m_decl_call;
160 /* The size of each private variable, including the alignment padding. */
161 std::map<std::string, size_t> m_private_data_sizes;
163 /* And private. */
164 size_t m_next_private_offset;
165 var_offset_table m_private_offsets;
167 /* Name index for declared functions. */
168 label_index m_function_index;
170 /* Stores all processed kernels in order. */
171 std::vector<brig_function *> m_kernels;
173 /* Stores all already processed functions from the translation unit
174 for some interprocedural analysis. */
175 std::map<std::string, brig_function *> m_finished_functions;
177 /* The original dump file. */
178 FILE *m_dump_file;
180 /* The original dump file flags. */
181 dump_flags_t m_dump_flags;
184 /* Produce a "mangled name" for the given brig variable. The mangling is used
185 to make unique global symbol names for module and function scope variables.
186 The templated version is suitable for most of the variable types. Functions
187 and kernels (BrigDirectiveExecutable) are handled with a specialized
188 get_mangled_name() version. */
190 template <typename T>
191 std::string
192 brig_to_generic::get_mangled_name_tmpl (const T *brigVar) const
194 std::string var_name = get_string (brigVar->name).substr (1);
196 /* Mangle the variable name using the function name and the module name
197 in case of a function scope variable. */
198 if (m_cf != NULL
199 && m_cf->has_function_scope_var (&brigVar->base))
200 var_name = m_cf->m_name + "." + var_name;
202 if (brigVar->linkage == BRIG_LINKAGE_MODULE)
203 var_name = "gccbrig." + m_module_name + "." + var_name;
204 return var_name;
207 /* An interface to organize the different types of BRIG element handlers. */
209 class brig_entry_handler
211 public:
212 brig_entry_handler (brig_to_generic &parent) : m_parent (parent)
216 /* Handles the brig_code data at the given pointer and adds it to the
217 currently built tree. Returns the number of consumed bytes; */
218 virtual size_t operator () (const BrigBase *base) = 0;
220 protected:
221 brig_to_generic &m_parent;
224 tree call_builtin (tree pdecl, int nargs, tree rettype, ...);
226 tree build_resize_convert_view (tree destination_type, tree source);
227 tree build_reinterpret_to_uint (tree source);
229 tree build_stmt (enum tree_code code, ...);
231 tree get_unsigned_int_type (tree type);
233 tree get_scalar_unsigned_int_type (tree type);
234 void set_externally_visible (tree decl);
236 void set_inline (tree decl);
238 void dump_function (FILE *dump_file, brig_function *f);
240 #endif