1 #ifndef IVL_pform_H
2 #define IVL_pform_H
3 /*
4  * Copyright (c) 1998-2020 Stephen Williams (steve@icarus.com)
5  *
6  *    This source code is free software; you can redistribute it
7  *    and/or modify it in source code form under the terms of the GNU
8  *    General Public License as published by the Free Software
9  *    Foundation; either version 2 of the License, or (at your option)
10  *    any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 # include  "netlist.h"
23 # include  "HName.h"
24 # include  "named.h"
25 # include  "Module.h"
26 # include  "Statement.h"
27 # include  "AStatement.h"
28 # include  "PGate.h"
29 # include  "PExpr.h"
30 # include  "PTask.h"
31 # include  "PUdp.h"
32 # include  "PWire.h"
33 # include  "verinum.h"
34 # include  "discipline.h"
35 # include  <iostream>
36 # include  <string>
37 # include  <list>
38 # include  <memory>
39 # include  <cstdio>
40 
41 /*
42  * These classes implement the parsed form (P-form for short) of the
43  * original Verilog source. the parser generates the pform for the
44  * convenience of later processing steps.
45  */
46 
47 
48 /*
49  * Wire objects represent the named wires (of various flavor) declared
50  * in the source.
51  *
52  * Gate objects are the functional modules that are connected together
53  * by wires.
54  *
55  * Wires and gates, connected by joints, represent a netlist. The
56  * netlist is therefore a representation of the desired circuit.
57  */
58 class PGate;
59 class PExpr;
60 class PPackage;
61 class PSpecPath;
62 class PClass;
63 class PPackage;
64 struct vlltype;
65 
66 /*
67  * The min:typ:max expression s selected at parse time using the
68  * enumeration. When the compiler makes a choice, it also prints a
69  * warning if min_typ_max_warn > 0.
70  */
71 extern enum MIN_TYP_MAX { MIN, TYP, MAX } min_typ_max_flag;
72 extern unsigned min_typ_max_warn;
73 PExpr* pform_select_mtm_expr(PExpr*min, PExpr*typ, PExpr*max);
74 
75 /*
76  * This flag is true if the lexor thinks we are in a library source
77  * file.
78  */
79 extern bool pform_library_flag;
80 
81 /*
82  * These type are lexical types -- that is, types that are used as
83  * lexical values to decorate the parse tree during parsing. They are
84  * not in any way preserved once parsing is done.
85  */
86 
87 /* This is information about port name information for named port
88    connections. */
89 
90 
91 struct parmvalue_t {
92       list<PExpr*>*by_order;
93       list<named_pexpr_t>*by_name;
94 };
95 
96 struct str_pair_t { ivl_drive_t str0, str1; };
97 
98 
99 struct net_decl_assign_t {
100       perm_string name;
101       PExpr*expr;
102       struct net_decl_assign_t*next;
103 };
104 
105 /* The lgate is gate instantiation information. */
106 struct lgate {
107       explicit inline lgate(int =0)
108       : parms(0), parms_by_name(0), file(NULL), lineno(0)
109       { }
110 
111       string name;
112       list<PExpr*>*parms;
113       list<named_pexpr_t>*parms_by_name;
114 
115       pform_range_t range;
116 
117       const char* file;
118       unsigned lineno;
119 };
120 
121 extern std::list<pform_range_t>* make_range_from_width(uint64_t wid);
122 extern std::list<pform_range_t>* copy_range(std::list<pform_range_t>* orig);
123 
124   /* Use this function to transform the parted form of the attribute
125      list to the attribute map that is used later. */
126 extern void pform_bind_attributes(map<perm_string,PExpr*>&attributes,
127 				  list<named_pexpr_t>*attr,
128 				  bool keep_attr =false);
129 
130   /* The lexor calls this function to change the default nettype. */
131 extern void pform_set_default_nettype(NetNet::Type net,
132 				     const char*file,
133 				     unsigned lineno);
134 
135   /* Return true if currently processing a program block. This can be
136      used to reject statements that cannot exist in program blocks. */
137 extern bool pform_in_program_block(void);
138 
139   /* Return true if currently processing an interface. This can be
140      used to reject statements that cannot exist in interfaces. */
141 extern bool pform_in_interface(void);
142 
143 /*
144  * Look for the given wire in the current lexical scope. If the wire
145  * (including variables of any type) cannot be found in the current
146  * scope, then return 0.
147  */
148 extern PWire* pform_get_wire_in_scope(perm_string name);
149 
150 extern PWire* pform_get_make_wire_in_scope(const struct vlltype&li,
151                                            perm_string name,
152                                            NetNet::Type net_type,
153                                            NetNet::PortType port_type,
154                                            ivl_variable_type_t vt_type);
155 
156 /*
157  * The parser uses startmodule and endmodule together to build up a
158  * module as it parses it. The startmodule tells the pform code that a
159  * module has been noticed in the source file and the following events
160  * are to apply to the scope of that module. The endmodule causes the
161  * pform to close up and finish the named module.
162  *
163  * The program_block flag indicates that the module is actually a program
164  * block. The is_interface flag indicates that the module is actually
165  * an interface. These flags have implications during parse and during
166  * elaboration/code generation.
167  */
168 extern void pform_startmodule(const struct vlltype&loc, const char*name,
169 			      bool program_block, bool is_interface,
170 			      LexicalScope::lifetime_t lifetime,
171 			      list<named_pexpr_t>*attr);
172 extern void pform_module_set_ports(vector<Module::port_t*>*);
173 extern void pform_set_scope_timescale(const struct vlltype&loc);
174 
175 /* These functions are used when we have a complete port definition, either
176    in an ansi style or non-ansi style declaration. In this case, we have
177    everything needed to define the port, all in one place. */
178 extern void pform_module_define_port(const struct vlltype&li,
179 				     perm_string name,
180 				     NetNet::PortType,
181 				     NetNet::Type type,
182 				     data_type_t*vtype,
183 				     list<named_pexpr_t>*attr,
184 				     bool keep_attr =false);
185 extern void pform_module_define_port(const struct vlltype&li,
186 				     list<pform_port_t>*ports,
187 				     NetNet::PortType,
188 				     NetNet::Type type,
189 				     data_type_t*vtype,
190 				     list<named_pexpr_t>*attr);
191 
192 extern Module::port_t* pform_module_port_reference(perm_string name,
193 						   const char*file,
194 						   unsigned lineno);
195 extern void pform_endmodule(const char*, bool inside_celldefine,
196                             Module::UCDriveType uc_drive_def);
197 
198 extern void pform_start_class_declaration(const struct vlltype&loc,
199 					  class_type_t*type,
200 					  data_type_t*base_type,
201 					  std::list<PExpr*>*base_exprs,
202 					  LexicalScope::lifetime_t lifetime);
203 extern void pform_class_property(const struct vlltype&loc,
204 				 property_qualifier_t pq,
205 				 data_type_t*data_type,
206 				 std::list<decl_assignment_t*>*decls);
207 extern void pform_set_this_class(const struct vlltype&loc, PTaskFunc*net);
208 extern void pform_set_constructor_return(PFunction*net);
209 
210 extern void pform_end_class_declaration(const struct vlltype&loc);
211 
212 extern void pform_make_udp(perm_string name, list<perm_string>*parms,
213 			   std::vector<PWire*>*decl, list<string>*table,
214 			   Statement*init,
215 			   const char*file, unsigned lineno);
216 
217 extern void pform_make_udp(perm_string name,
218 			   bool sync_flag, perm_string out_name,
219 			   PExpr*sync_init,
220 			   list<perm_string>*parms,
221 			   list<string>*table,
222 			   const char*file, unsigned lineno);
223 /*
224  * Package related functions.
225  */
226 extern void pform_start_package_declaration(const struct vlltype&loc,
227 					    const char*type,
228 					    LexicalScope::lifetime_t lifetime);
229 extern void pform_end_package_declaration(const struct vlltype&loc);
230 extern void pform_package_import(const struct vlltype&loc,
231 				 PPackage*pkg, const char*ident);
232 
233 extern PExpr* pform_package_ident(const struct vlltype&loc,
234 				  PPackage*pkg, pform_name_t*ident);
235 
236 /*
237  * Interface related functions.
238  */
239 extern void pform_start_modport_item(const struct vlltype&loc, const char*name);
240 extern void pform_end_modport_item(const struct vlltype&loc);
241 extern void pform_add_modport_port(const struct vlltype&loc,
242 	                           NetNet::PortType port_type,
243 	                           perm_string name, PExpr*expr);
244 
245 /*
246  * This creates an identifier aware of names that may have been
247  * imported from other packages.
248  */
249 extern PEIdent* pform_new_ident(const struct vlltype&loc, const pform_name_t&name);
250 
251 extern PTrigger* pform_new_trigger(const struct vlltype&loc, PPackage*pkg,
252 				   const pform_name_t&name);
253 
254 /*
255  * Enter/exit name scopes. The push_scope function pushes the scope
256  * name string onto the scope hierarchy. The pop pulls it off and
257  * deletes it. Thus, the string pushed must be allocated.
258  */
259 extern void pform_pop_scope();
260 
261 /*
262  * Peek at the current (most recently active) scope.
263  */
264 extern LexicalScope* pform_peek_scope();
265 
266 extern PClass* pform_push_class_scope(const struct vlltype&loc, perm_string name,
267 				      LexicalScope::lifetime_t lifetime);
268 
269 extern PFunction*pform_push_constructor_scope(const struct vlltype&loc);
270 
271 extern PPackage* pform_push_package_scope(const struct vlltype&loc, perm_string name,
272 					  LexicalScope::lifetime_t lifetime);
273 
274 extern PTask*pform_push_task_scope(const struct vlltype&loc, char*name,
275 				   LexicalScope::lifetime_t lifetime);
276 
277 extern PFunction*pform_push_function_scope(const struct vlltype&loc, const char*name,
278 					   LexicalScope::lifetime_t lifetime);
279 
280 extern PBlock*pform_push_block_scope(const struct vlltype&loc, char*name,
281 				     PBlock::BL_TYPE tt);
282 
283 extern void pform_put_behavior_in_scope(AProcess*proc);
284 
285 extern verinum* pform_verinum_with_size(verinum*s, verinum*val,
286 					const char*file, unsigned lineno);
287 
288 /*
289  * This function takes the list of names as new genvars to declare in
290  * the current module or generate scope.
291  */
292 extern void pform_genvars(const struct vlltype&li, list<perm_string>*names);
293 
294 extern void pform_start_generate_for(const struct vlltype&li,
295 				     bool local_index,
296 				     char*ident1,
297 				     PExpr*init,
298 				     PExpr*test,
299 				     char*ident2,
300 				     PExpr*next);
301 extern void pform_start_generate_if(const struct vlltype&li, PExpr*test);
302 extern void pform_start_generate_else(const struct vlltype&li);
303 extern void pform_start_generate_case(const struct vlltype&lp, PExpr*test);
304 extern void pform_start_generate_nblock(const struct vlltype&lp, char*name);
305 extern void pform_generate_case_item(const struct vlltype&lp, list<PExpr*>*test);
306 extern void pform_generate_block_name(char*name);
307 extern void pform_endgenerate(bool end_conditional);
308 
309 /*
310  * This function returns the lexically containing generate scheme, if
311  * there is one. The parser may use this to check if we are within a
312  * generate scheme.
313  */
314 extern PGenerate* pform_parent_generate(void);
315 
316 extern void pform_set_typedef(perm_string name, data_type_t*data_type,
317 			      std::list<pform_range_t>*unp_ranges);
318 
319 extern void pform_set_type_referenced(const struct vlltype&loc, const char*name);
320 
321 /*
322  * This function makes a PECallFunction of the named function.
323  */
324 extern PECallFunction* pform_make_call_function(const struct vlltype&loc,
325 						const pform_name_t&name,
326 						const std::list<PExpr*>&parms);
327 extern PCallTask* pform_make_call_task(const struct vlltype&loc,
328 				       const pform_name_t&name,
329 				       const std::list<PExpr*>&parms);
330 
331 extern void pform_make_foreach_declarations(const struct vlltype&loc,
332 					    std::list<perm_string>*loop_vars);
333 extern PForeach* pform_make_foreach(const struct vlltype&loc,
334 				    char*ident,
335 				    std::list<perm_string>*loop_vars,
336 				    Statement*stmt);
337 
338 /*
339  * The makewire functions announce to the pform code new wires. These
340  * go into a module that is currently opened.
341  */
342 extern void pform_makewire(const struct vlltype&li, perm_string name,
343 			   NetNet::Type type,
344 			   NetNet::PortType pt,
345 			   ivl_variable_type_t,
346 			   list<named_pexpr_t>*attr);
347 
348 /* This form handles simple declarations */
349 extern void pform_makewire(const struct vlltype&li,
350 			   list<pform_range_t>*range,
351 			   bool signed_flag,
352 			   list<perm_string>*names,
353 			   NetNet::Type type,
354 			   NetNet::PortType,
355 			   ivl_variable_type_t,
356 			   list<named_pexpr_t>*attr,
357 			   PWSRType rt = SR_NET);
358 
359 /* This form handles assignment declarations. */
360 extern void pform_makewire(const struct vlltype&li,
361 			   list<PExpr*>*delay,
362 			   str_pair_t str,
363 			   net_decl_assign_t*assign_list,
364 			   NetNet::Type type,
365 			   data_type_t*data_type);
366 
367 extern void pform_makewire(const struct vlltype&li,
368 			   std::list<PExpr*>*delay,
369 			   str_pair_t str,
370 			   std::list<decl_assignment_t*>*assign_list,
371 			   NetNet::Type type,
372 			   data_type_t*data_type);
373 
374 /* This form handles nets declared as structures. (See pform_struct_type.cc) */
375 extern void pform_makewire(const struct vlltype&li,
376 			   struct_type_t*struct_type,
377 			   NetNet::PortType,
378 			   list<perm_string>*names,
379 			   list<named_pexpr_t>*attr);
380 
381 extern void pform_make_var_init(const struct vlltype&li,
382 				perm_string name, PExpr*expr);
383 
384 /* This function is used when we have an incomplete port definition in
385    a non-ansi style declaration. Look up the names of the wires, and set
386    the port type, i.e. input, output or inout, and, if specified, the
387    range and signedness. If the wire does not exist, create it. */
388 extern void pform_set_port_type(const struct vlltype&li,
389 				list<pform_port_t>*ports,
390 				NetNet::PortType,
391 				data_type_t*dt,
392 				list<named_pexpr_t>*attr);
393 
394 extern void pform_set_reg_idx(perm_string name,
395 			      std::list<pform_range_t>*indices);
396 
397 extern void pform_set_data_type(const struct vlltype&li, data_type_t*, list<perm_string>*names, NetNet::Type net_type, list<named_pexpr_t>*attr);
398 
399 extern void pform_set_struct_type(const struct vlltype&li, struct_type_t*struct_type, std::list<perm_string>*names, NetNet::Type net_type, std::list<named_pexpr_t>*attr);
400 
401 extern void pform_set_string_type(const struct vlltype&li, const string_type_t*string_type, std::list<perm_string>*names, NetNet::Type net_type, std::list<named_pexpr_t>*attr);
402 
403 extern void pform_set_class_type(const struct vlltype&li, class_type_t*class_type, std::list<perm_string>*names, NetNet::Type net_type, std::list<named_pexpr_t>*addr);
404 
405 
406   /* pform_set_attrib and pform_set_type_attrib exist to support the
407      $attribute syntax, which can only set string values to
408      attributes. The functions keep the value strings that are
409      passed in. */
410 extern void pform_set_attrib(perm_string name, perm_string key,
411 			     char*value);
412 extern void pform_set_type_attrib(perm_string name, const string&key,
413 				  char*value);
414 
415 extern LexicalScope::range_t* pform_parameter_value_range(bool exclude_flag,
416 						    bool low_open, PExpr*low_expr,
417 						    bool hig_open, PExpr*hig_expr);
418 
419 extern void pform_set_parameter(const struct vlltype&loc,
420 				perm_string name,
421 				ivl_variable_type_t type,
422 				bool signed_flag,
423 				list<pform_range_t>*range,
424 				PExpr*expr, LexicalScope::range_t*value_range);
425 extern void pform_set_localparam(const struct vlltype&loc,
426 				 perm_string name,
427 				 ivl_variable_type_t type,
428 				 bool signed_flag,
429 				 list<pform_range_t>*range,
430 				 PExpr*expr);
431 extern void pform_set_specparam(const struct vlltype&loc,
432 				 perm_string name,
433 				 list<pform_range_t>*range,
434 				 PExpr*expr);
435 extern void pform_set_defparam(const pform_name_t&name, PExpr*expr);
436 
437 extern void pform_set_param_from_type(const struct vlltype&loc,
438                                       const data_type_t *data_type,
439                                       const char *name,
440                                       list<pform_range_t> *&param_range,
441                                       bool &param_signed,
442                                       ivl_variable_type_t &param_type);
443 
444 /*
445  * Functions related to specify blocks.
446  */
447 extern PSpecPath*pform_make_specify_path(const struct vlltype&li,
448 					 list<perm_string>*src, char pol,
449 					 bool full_flag, list<perm_string>*dst);
450 extern PSpecPath*pform_make_specify_edge_path(const struct vlltype&li,
451 					 int edge_flag, /*posedge==true */
452 					 list<perm_string>*src, char pol,
453 					 bool full_flag, list<perm_string>*dst,
454 					 PExpr*data_source_expression);
455 extern PSpecPath*pform_assign_path_delay(PSpecPath*obj, list<PExpr*>*delays);
456 
457 extern void pform_module_specify_path(PSpecPath*obj);
458 
459 /*
460  * pform_make_behavior creates processes that are declared with always
461  * or initial items.
462  */
463 extern PProcess*  pform_make_behavior(ivl_process_type_t, Statement*,
464 				      list<named_pexpr_t>*attr);
465 extern void pform_mc_translate_on(bool flag);
466 
467 extern std::vector<PWire*>* pform_make_udp_input_ports(list<perm_string>*);
468 
469 extern void pform_make_events(list<perm_string>*names,
470 			      const char*file, unsigned lineno);
471 /*
472  * Make real datum objects.
473  */
474 extern void pform_make_reals(list<perm_string>*names,
475 			     const char*file, unsigned lineno);
476 
477 /*
478  * The makegate function creates a new gate (which need not have a
479  * name) and connects it to the specified wires.
480  */
481 extern void pform_makegates(const struct vlltype&loc,
482 			    PGBuiltin::Type type,
483 			    struct str_pair_t str,
484 			    list<PExpr*>*delay,
485 			    svector<lgate>*gates,
486 			    list<named_pexpr_t>*attr);
487 
488 extern void pform_make_modgates(const struct vlltype&loc,
489 				perm_string type,
490 				struct parmvalue_t*overrides,
491 				svector<lgate>*gates,
492 				list<named_pexpr_t>*attr);
493 
494 /* Make a continuous assignment node, with optional bit- or part- select. */
495 extern void pform_make_pgassign_list(list<PExpr*>*alist,
496 				     list<PExpr*>*del,
497 				     struct str_pair_t str,
498 				     const char* fn, unsigned lineno);
499 
500 /* Given a port type and a list of names, make a list of wires that
501    can be used as task port information. */
502 extern std::vector<pform_tf_port_t>*pform_make_task_ports(const struct vlltype&loc,
503 					     NetNet::PortType pt,
504 					     ivl_variable_type_t vtype,
505 					     bool signed_flag,
506 					     list<pform_range_t>*range,
507 					     list<perm_string>*names,
508 					     bool isint = false);
509 
510 extern std::vector<pform_tf_port_t>*pform_make_task_ports(const struct vlltype&loc,
511 					     NetNet::PortType pt,
512 					     data_type_t*vtype,
513 					     list<perm_string>*names);
514 
515 /*
516  * The parser uses this function to convert a unary
517  * increment/decrement expression to the equivalent compressed
518  * assignment statement.
519  */
520 extern PAssign* pform_compressed_assign_from_inc_dec(const struct vlltype&loc,
521 						     PExpr*exp);
522 
523 /*
524  * The parser uses this function to convert a genvar increment/decrement
525  * expression to the equivalent binary add/subtract expression.
526  */
527 extern PExpr* pform_genvar_inc_dec(const struct vlltype&loc, const char*name,
528                                    bool inc_flag);
529 
530 /*
531  * These are functions that the outside-the-parser code uses the do
532  * interesting things to the Verilog. The parse function reads and
533  * parses the source file and places all the modules it finds into the
534  * mod list. The dump function dumps a module to the output stream.
535  */
536 extern void pform_dump(ostream&out, Module*mod);
537 
538 /* ** pform_discipline.cc
539  * Functions for handling the parse of natures and disciplines. These
540  * functions are in pform_disciplines.cc
541  */
542 
543 extern void pform_start_nature(const char*name);
544 extern void pform_end_nature(const struct vlltype&loc);
545 
546 extern void pform_nature_access(const struct vlltype&loc, const char*name);
547 
548 extern void pform_start_discipline(const char*name);
549 extern void pform_end_discipline(const struct vlltype&loc);
550 
551 extern void pform_discipline_domain(const struct vlltype&loc, ivl_dis_domain_t use_domain);
552 extern void pform_discipline_potential(const struct vlltype&loc, const char*name);
553 extern void pform_discipline_flow(const struct vlltype&loc, const char*name);
554 
555 extern void pform_attach_discipline(const struct vlltype&loc,
556 				    ivl_discipline_t discipline, list<perm_string>*names);
557 
558 extern void pform_dump(ostream&out, const ivl_nature_s*);
559 extern void pform_dump(ostream&out, const ivl_discipline_s*);
560 
561 /* ** pform_analog.cc
562 */
563 extern void pform_make_analog_behavior(const struct vlltype&loc,
564 				       ivl_process_type_t type, Statement*st);
565 
566 extern AContrib*pform_contribution_statement(const struct vlltype&loc,
567 					     PExpr*lval, PExpr*rval);
568 
569 extern PExpr* pform_make_branch_probe_expression(const struct vlltype&loc,
570 						 char*name, char*n1, char*n2);
571 
572 extern PExpr* pform_make_branch_probe_expression(const struct vlltype&loc,
573 						 char*name, char*branch);
574 
575 /*
576  * Parse configuration file with format <key>=<value>, where key
577  * is the hierarchical name of a valid parameter name and value
578  * is the value user wants to assign to. The value should be constant.
579  */
580 extern void parm_to_defparam_list(const string&param);
581 
582 /*
583  * Tasks to set the timeunit or timeprecision for SystemVerilog.
584  */
585 extern bool get_time_unit(const char*cp, int &unit);
586 extern int  pform_get_timeunit();
587 extern void pform_set_timeunit(const char*txt, bool initial_decl);
588 extern void pform_set_timeprec(const char*txt, bool initial_decl);
589 /*
590  * Flags to determine whether this is an initial declaration.
591  */
592 extern bool allow_timeunit_decl;
593 extern bool allow_timeprec_decl;
594 
595 #endif /* IVL_pform_H */
596