1 //
2 //  Copyright (C) 2013-2018  Nick Gasson
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef _COMMON_H
19 #define _COMMON_H
20 
21 #include "tree.h"
22 
23 //
24 // Various utility functions
25 //
26 
27 int64_t assume_int(tree_t t);
28 void range_bounds(range_t r, int64_t *low, int64_t *high);
29 tree_t call_builtin(const char *builtin, type_t type, ...);
30 bool folded_int(tree_t t, int64_t *i);
31 bool folded_real(tree_t t, double *d);
32 bool folded_bool(tree_t t, bool *b);
33 bool folded_length(range_t r, int64_t *l);
34 bool folded_enum(tree_t t, unsigned *pos);
35 bool folded_bounds(range_t r, int64_t *low, int64_t *high);
36 bool folded_bounds_real(range_t r, double *low, double *high);
37 tree_t get_int_lit(tree_t t, int64_t i);
38 tree_t get_enum_lit(tree_t t, int pos);
39 tree_t get_real_lit(tree_t t, double r);
40 const char *package_signal_path_name(ident_t i);
41 bool parse_value(type_t type, const char *str, int64_t *value);
42 tree_t make_ref(tree_t to);
43 int record_field_to_net(type_t type, ident_t name);
44 tree_t find_record_field(tree_t rref);
45 class_t class_of(tree_t t);
46 bool class_has_type(class_t c);
47 const char *class_str(class_t c);
48 tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name);
49 type_t array_aggregate_type(type_t array, int from_dim);
50 tree_t make_default_value(type_t type, const loc_t *loc);
51 unsigned bits_for_range(int64_t low, int64_t high);
52 unsigned array_dimension(type_t a);
53 type_t index_type_of(type_t type, int dim);
54 range_kind_t direction_of(type_t type, unsigned dim);
55 range_t range_of(type_t type, unsigned dim);
56 tree_t str_to_literal(const char *start, const char *end, type_t type);
57 int64_t rebase_index(type_t array_type, int dim, int64_t value);
58 bool loc_eq(const loc_t *a, const loc_t *b);
59 void loc_write(const loc_t *loc, fbuf_t *f, ident_wr_ctx_t ctx);
60 void loc_read(loc_t *loc, fbuf_t *f, ident_rd_ctx_t ctx);
61 char *vcode_file_name(ident_t unit_name);
62 bool pack_needs_cgen(tree_t t);
63 ident_t mangle_func(tree_t decl, const char *prefix);
64 
65 const char *fmt_time_r(char *buf, size_t len, uint64_t t);
66 const char *fmt_time(uint64_t t);
67 
68 //
69 // Utility typedefs
70 //
71 
72 typedef unsigned (*tree_formals_t)(tree_t t);
73 typedef tree_t (*tree_formal_t)(tree_t t, unsigned n);
74 typedef unsigned (*tree_actuals_t)(tree_t t);
75 typedef tree_t (*tree_actual_t)(tree_t t, unsigned n);
76 
77 typedef enum {
78    WAITS_NO    = 0x0,
79    WAITS_MAYBE = 0x1,
80    WAITS_YES   = 0x3,
81 } wait_level_t;
82 
83 typedef enum {
84    IMPURE_FILE   = 0x1,
85    IMPURE_SHARED = 0x2,
86 } impure_io_t;
87 
88 //
89 // VHDL standard revisions
90 //
91 
92 typedef enum {
93    STD_87,
94    STD_93,
95    STD_00,
96    STD_02,
97    STD_08
98 } vhdl_standard_t;
99 
100 vhdl_standard_t standard(void);
101 void set_standard(vhdl_standard_t s);
102 const char *standard_text(vhdl_standard_t s);
103 
104 //
105 // Disable some pedantic rule checks
106 //
107 
108 #define RELAX_PREFER_EXPLICT  (1 << 0)
109 #define RELAX_LOCALLY_STATIC  (1 << 1)
110 #define RELAX_UNIVERSAL_BOUND (1 << 2)
111 #define RELAX_PURE_FILES      (1 << 3)
112 
113 int relax_rules(void);
114 void set_relax_rules(int mask);
115 
116 //
117 // Pre-defined attributes
118 //
119 
120 typedef enum {
121    ATTR_LAST_EVENT,
122    ATTR_EVENT,
123    ATTR_ACTIVE,
124    ATTR_LAST_VALUE,
125    ATTR_PATH_NAME,
126    ATTR_INSTANCE_NAME,
127    ATTR_DELAYED,
128    ATTR_STABLE,
129    ATTR_QUIET,
130    ATTR_TRANSACTION,
131    ATTR_LENGTH,
132    ATTR_LEFT,
133    ATTR_LOW,
134    ATTR_HIGH,
135    ATTR_RIGHT,
136    ATTR_ASCENDING,
137    ATTR_IMAGE,
138    ATTR_LAST_ACTIVE,
139    ATTR_DRIVING,
140    ATTR_DRIVING_VALUE,
141    ATTR_VALUE,
142    ATTR_SUCC,
143    ATTR_PRED,
144    ATTR_LEFTOF,
145    ATTR_RIGHTOF,
146    ATTR_POS,
147    ATTR_VAL
148 } predef_attr_t;
149 
150 //
151 // Shared interned strings
152 //
153 
154 #ifndef COMMON_IMPL
155 #define GLOBAL extern const
156 #else
157 #define GLOBAL
158 #endif
159 
160 GLOBAL ident_t builtin_i;
161 GLOBAL ident_t std_standard_i;
162 GLOBAL ident_t formal_i;
163 GLOBAL ident_t elab_copy_i;
164 GLOBAL ident_t all_i;
165 GLOBAL ident_t protected_i;
166 GLOBAL ident_t inst_name_i;
167 GLOBAL ident_t fst_dir_i;
168 GLOBAL ident_t scope_pop_i;
169 GLOBAL ident_t partial_map_i;
170 GLOBAL ident_t fst_data_i;
171 GLOBAL ident_t std_logic_i;
172 GLOBAL ident_t std_ulogic_i;
173 GLOBAL ident_t std_bool_i;
174 GLOBAL ident_t std_char_i;
175 GLOBAL ident_t std_bit_i;
176 GLOBAL ident_t natural_i;
177 GLOBAL ident_t positive_i;
178 GLOBAL ident_t unsigned_i;
179 GLOBAL ident_t signed_i;
180 GLOBAL ident_t foreign_i;
181 GLOBAL ident_t nested_i;
182 GLOBAL ident_t drives_all_i;
183 GLOBAL ident_t driver_init_i;
184 GLOBAL ident_t GLOBAL_i;
185 GLOBAL ident_t mangled_i;
186 GLOBAL ident_t null_range_i;
187 GLOBAL ident_t deferred_i;
188 GLOBAL ident_t prot_field_i;
189 GLOBAL ident_t stmt_tag_i;
190 GLOBAL ident_t cond_tag_i;
191 GLOBAL ident_t sub_cond_i;
192 GLOBAL ident_t static_i;
193 GLOBAL ident_t range_var_i;
194 GLOBAL ident_t work_i;
195 GLOBAL ident_t wait_level_i;
196 GLOBAL ident_t impure_io_i;
197 GLOBAL ident_t simple_name_i;
198 GLOBAL ident_t std_i;
199 GLOBAL ident_t nnets_i;
200 GLOBAL ident_t thunk_i;
201 
202 void intern_strings();
203 
204 #endif  // _COMMON_H
205