1 /*
2  * Copyright (c) 2012-2020 Stephen Williams (steve@icarus.com)
3  *
4  *    This source code is free software; you can redistribute it
5  *    and/or modify it in source code form under the terms of the GNU
6  *    General Public License as published by the Free Software
7  *    Foundation; either version 2 of the License, or (at your option)
8  *    any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "PExpr.h"
21 # include  "pform_types.h"
22 # include  "netlist.h"
23 # include  "netclass.h"
24 # include  "netdarray.h"
25 # include  "netenum.h"
26 # include  "netqueue.h"
27 # include  "netparray.h"
28 # include  "netscalar.h"
29 # include  "netstruct.h"
30 # include  "netvector.h"
31 # include  "netmisc.h"
32 # include  <typeinfo>
33 # include  "ivl_assert.h"
34 
35 using namespace std;
36 
37 /*
38  * Some types have a list of ranges that need to be elaborated. This
39  * function elaborates the ranges referenced by "dims" into the vector
40  * "ranges".
41  */
elaborate_array_ranges(Design * des,NetScope * scope,vector<netrange_t> & ranges,const list<pform_range_t> * dims)42 static void elaborate_array_ranges(Design*des, NetScope*scope,
43 				   vector<netrange_t>&ranges,
44 				   const list<pform_range_t>*dims)
45 {
46       if (dims == 0)
47 	    return;
48 
49       for (list<pform_range_t>::const_iterator cur = dims->begin()
50 		 ; cur != dims->end() ; ++ cur) {
51 
52 	    NetExpr*me = elab_and_eval(des, scope, cur->first, 0, true);
53 
54 	    NetExpr*le = elab_and_eval(des, scope, cur->second, 0, true);
55 
56 	      /* If elaboration failed for either expression, we
57 		 should have already reported the error, so just
58 		 skip the following evaluation to recover. */
59 
60 	    long mnum = 0, lnum = 0;
61 	    if ( me && ! eval_as_long(mnum, me) ) {
62 		  assert(0);
63 		  des->errors += 1;
64 	    }
65 
66 	    if ( le && ! eval_as_long(lnum, le) ) {
67 		  assert(0);
68 		  des->errors += 1;
69 	    }
70 
71 	    ranges.push_back(netrange_t(mnum, lnum));
72       }
73 }
74 
75 /*
76  * Elaborations of types may vary depending on the scope that it is
77  * done in, so keep a per-scope cache of the results.
78  */
elaborate_type(Design * des,NetScope * scope)79 ivl_type_s* data_type_t::elaborate_type(Design*des, NetScope*scope)
80 {
81 	// User-defined types must be elaborated in the context
82 	// where they were defined.
83       if (!name.nil())
84 	    scope = scope->find_typedef_scope(des, this);
85 
86       ivl_assert(*this, scope);
87       Definitions*use_definitions = scope;
88 
89       map<Definitions*,ivl_type_s*>::iterator pos = cache_type_elaborate_.lower_bound(use_definitions);
90 	  if (pos != cache_type_elaborate_.end() && pos->first == use_definitions)
91 	     return pos->second;
92 
93       ivl_type_s*tmp = elaborate_type_raw(des, scope);
94       cache_type_elaborate_.insert(pos, pair<NetScope*,ivl_type_s*>(scope, tmp));
95       return tmp;
96 }
97 
elaborate_type_raw(Design * des,NetScope *) const98 ivl_type_s* data_type_t::elaborate_type_raw(Design*des, NetScope*) const
99 {
100       cerr << get_fileline() << ": internal error: "
101 	   << "Elaborate method not implemented for " << typeid(*this).name()
102 	   << "." << endl;
103       des->errors += 1;
104       return 0;
105 }
106 
elaborate_type_raw(Design * des,NetScope *) const107 ivl_type_s* atom2_type_t::elaborate_type_raw(Design*des, NetScope*) const
108 {
109       switch (type_code) {
110 	  case 64:
111 	    if (signed_flag)
112 		  return &netvector_t::atom2s64;
113 	    else
114 		  return &netvector_t::atom2u64;
115 
116 	  case 32:
117 	    if (signed_flag)
118 		  return &netvector_t::atom2s32;
119 	    else
120 		  return &netvector_t::atom2u32;
121 
122 	  case 16:
123 	    if (signed_flag)
124 		  return &netvector_t::atom2s16;
125 	    else
126 		  return &netvector_t::atom2u16;
127 
128 	  case 8:
129 	    if (signed_flag)
130 		  return &netvector_t::atom2s8;
131 	    else
132 		  return &netvector_t::atom2u8;
133 
134 	  default:
135 	    cerr << get_fileline() << ": internal error: "
136 		 << "atom2_type_t type_code=" << type_code << "." << endl;
137 	    des->errors += 1;
138 	    return 0;
139       }
140 }
141 
elaborate_type_raw(Design *,NetScope *) const142 ivl_type_s* class_type_t::elaborate_type_raw(Design*, NetScope*) const
143 {
144       ivl_assert(*this, save_elaborated_type);
145       return save_elaborated_type;
146 }
147 
148 /*
149  * elaborate_type_raw for enumerations is actually mostly performed
150  * during scope elaboration so that the enumeration literals are
151  * available at the right time. At that time, the netenum_t* object is
152  * stashed in the scope so that I can retrieve it here.
153  */
elaborate_type_raw(Design *,NetScope * scope) const154 ivl_type_s* enum_type_t::elaborate_type_raw(Design*, NetScope*scope) const
155 {
156       ivl_assert(*this, scope);
157       ivl_type_s*tmp = scope->enumeration_for_key(this);
158       if (tmp == 0 && scope->unit()) {
159 	    tmp = scope->unit()->enumeration_for_key(this);
160 }
161       return tmp;
162 }
163 
elaborate_type_raw(Design * des,NetScope * scope) const164 ivl_type_s* vector_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
165 {
166       vector<netrange_t> packed;
167       elaborate_array_ranges(des, scope, packed, pdims.get());
168 
169       netvector_t*tmp = new netvector_t(packed, base_type);
170       tmp->set_signed(signed_flag);
171       tmp->set_isint(integer_flag);
172 
173       return tmp;
174 }
175 
elaborate_type_raw(Design *,NetScope *) const176 ivl_type_s* real_type_t::elaborate_type_raw(Design*, NetScope*) const
177 {
178       switch (type_code) {
179 	  case REAL:
180 	    return &netreal_t::type_real;
181 	  case SHORTREAL:
182 	    return &netreal_t::type_shortreal;
183       }
184       return 0;
185 }
186 
elaborate_type_raw(Design *,NetScope *) const187 ivl_type_s* string_type_t::elaborate_type_raw(Design*, NetScope*) const
188 {
189       return &netstring_t::type_string;
190 }
191 
elaborate_type_raw(Design * des,NetScope * scope) const192 ivl_type_s* parray_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
193 {
194       vector<netrange_t>packed;
195       elaborate_array_ranges(des, scope, packed, dims.get());
196 
197       ivl_type_t etype = base_type->elaborate_type(des, scope);
198 
199       return new netparray_t(packed, etype);
200 }
201 
elaborate_type_raw(Design * des,NetScope * scope) const202 netstruct_t* struct_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
203 {
204       netstruct_t*res = new netstruct_t;
205 
206       res->packed(packed_flag);
207 
208       if (union_flag)
209 	    res->union_flag(true);
210 
211       for (list<struct_member_t*>::iterator cur = members->begin()
212 		 ; cur != members->end() ; ++ cur) {
213 
214 	      // Elaborate the type of the member.
215 	    struct_member_t*curp = *cur;
216 	    ivl_type_t mem_vec = curp->type->elaborate_type(des, scope);
217 	    if (mem_vec == 0)
218 		  continue;
219 
220 	      // There may be several names that are the same type:
221 	      //   <data_type> name1, name2, ...;
222 	      // Process all the member, and give them a type.
223 	    for (list<decl_assignment_t*>::iterator cur_name = curp->names->begin()
224 		       ; cur_name != curp->names->end() ;  ++ cur_name) {
225 		  decl_assignment_t*namep = *cur_name;
226 
227 		  netstruct_t::member_t memb;
228 		  memb.name = namep->name;
229 		  memb.net_type = mem_vec;
230 		  res->append_member(des, memb);
231 	    }
232       }
233 
234       return res;
235 }
236 
elaborate_type_raw(Design * des,NetScope * scope) const237 ivl_type_s* uarray_type_t::elaborate_type_raw(Design*des, NetScope*scope) const
238 {
239 
240       ivl_type_t btype = base_type->elaborate_type(des, scope);
241 
242       assert(dims->size() >= 1);
243       list<pform_range_t>::const_iterator cur = dims->begin();
244 
245 	// Special case: if the dimension is nil:nil, this is a
246 	// dynamic array. Note that we only know how to handle dynamic
247 	// arrays with 1 dimension at a time.
248       if (cur->first==0 && cur->second==0) {
249 	    assert(dims->size()==1);
250 	    ivl_type_s*res = new netdarray_t(btype);
251 	    return res;
252       }
253 
254 	// Special case: if the dimension is null:nil. this is a queue.
255       if (dynamic_cast<PENull*>(cur->first)) {
256 	    cerr << get_fileline() << ": sorry: "
257 		 << "SV queues inside classes are not yet supported." << endl;
258 	    des->errors += 1;
259 
260 	      // FIXME: Need to set the max size if cur->second is defined
261 	    ivl_type_s*res = new netqueue_t(btype, -1);
262 	    return res;
263       }
264 
265       vector<netrange_t> dimensions;
266       bool dimensions_ok = evaluate_ranges(des, scope, this, dimensions, *dims);
267 
268       if (!dimensions_ok) {
269 	    cerr << get_fileline() << " : warning: "
270 		 << "Bad dimensions for type here." << endl;
271       }
272 
273       ivl_assert(*this, btype);
274       ivl_type_s*res = new netuarray_t(dimensions, btype);
275       return res;
276 }
277