source: vis_dev/vl2mv-2.3/src/parser/vl_traverse.c @ 106

Last change on this file since 106 was 18, checked in by cecile, 13 years ago

vl2mv added

File size: 62.9 KB
Line 
1/*
2
3  Copyright (c) 1992, 1993
4        Regents of the University of California
5  All rights reserved.
6
7  Use and copying of this software and preparation of derivative works
8  based upon this software are permitted.  However, any distribution of
9  this software or derivative works must include the above copyright
10  notice.
11
12  This software is made available AS IS, and neither the Electronics
13  Research Laboratory or the Universify of California make any
14  warranty about the software, its performance or its conformity to
15  any specification.
16
17  Author: Szu-Tsung Cheng, stcheng@ic.Berkeley.EDU
18          10/92
19          10/93
20
21  $Id: vl_traverse.c,v 1.4 2009/03/09 20:25:58 fabio Exp $
22
23
24*/
25
26
27#include "util.h"
28#include "st.h"
29#include "list.h"
30#include "array.h"
31#include "set.h"
32#include "stack.h"
33#include "vl_types.h"
34#include "vl_defs.h"
35#include "vlr_int.h"
36#include "graph.h"
37#include "vl_fg_defs.h"
38#include "vl_fg_types.h"
39#include "vl_flowgraph.h"
40#include "vl_traverse.h"
41#include "vl_copy.h"
42#include "vl_create.h"
43#include "vl_write_util.h"
44#include "dataflow.h"
45#include "vl_write.h"
46#include "verilog.h"
47#include <stdarg.h>
48
49int vlTimedSystem = 0;
50
51extern vl_desc *mod_list;
52
53extern int dflow_analysis;
54
55extern int dflow_context;
56
57extern int vlPhase;
58
59extern int implicitClocking;
60
61extern int WarningLevel;
62
63extern vl_module *vl_encloseModule;
64
65int use_libraryGate;
66
67int Loop_Unrolling=1;
68
69int Repeat_Unrolling=1;
70
71int    PrtEnable=0;
72
73
74int indexAssignment=0;
75
76
77int aliasSP=0;
78st_table *aliasStack[20];
79
80vl_procstmt *vl_currentProcStmt;
81int in_reset_stmt=0;
82static int lhs_id=0;
83static int StpTrace=0;
84
85static void assoc_formal_actual ARGS((vl_task *task_dfn,
86                                      lsList formal, lsList actual));
87static void vl_expand_Q ARGS((FILE *file, lsList tags));
88static void record_program_location ARGS((vl_procstmt *procstmt));
89static void register_clock ARGS((FILE *file, vl_event_expr *event));
90static void check_task_arg ARGS((int ok, vl_task *task_dfn));
91static void rec_expand_Q ARGS((int num_Q, array_t *q_array,
92                               char *bit_str, lsList tags,
93                               lsGen gen, lsHandle *phandle));
94
95
96void vl_fprintf_stmt(FILE* file, ...)
97{
98    char *fmt;
99    va_list args;
100
101    va_start(args, file);
102    fmt = va_arg(args, char*);
103    if (PrtEnable) (void)vfprintf(file, fmt, args);
104    va_end(args);
105}
106
107enum st_retval vl_step_desc(char *name, char *value, char *arg)
108
109{
110    if (((typestructPtr)value)->type == ModDecl) {
111        vl_step_module(stdout, (vl_module*)value);
112    } else if (((typestructPtr)value)->type == CombPrimDecl ||
113               ((typestructPtr)value)->type == SeqPrimDecl) {
114        vl_step_primitive(stdout, (vl_primitive*)value);
115    }
116
117    name = name;
118    arg = arg;
119    return ST_CONTINUE;
120}
121
122void vl_step_module(FILE *file, vl_module *mod)
123{
124    if (mod == NULL) return;
125
126    ASSERT(mod->type==ModDecl, "Expecting ModDecl");
127
128    if (mod->visited) return;
129
130    vl_currentModule = mod;
131    StpTRACE("printing module\n");
132    vl_fprintf_stmt(file, "module ");
133    vl_step_id_range(file, mod->name);
134    vl_step_ports(file, mod->ports);
135    vl_fprintf_stmt(file, ";\n");
136    vl_step_mod_item_list(file, mod->mod_items);
137    vl_fprintf_stmt(file, "endmodule\n\n");
138    vl_currentModule = NIL(vl_module);
139    mod->visited = 1;
140}
141
142
143void vl_step_primitive(FILE *file, vl_primitive *prim)
144{
145    if (prim == NULL) return;
146
147    ASSERT((prim->type==CombPrimDecl || prim->type==SeqPrimDecl),
148           "Expecting PrimDecl");
149    StpTRACE("Printing primitive\n");
150    vl_fprintf_stmt(file, "primitive ");
151    vl_step_id_range(file, prim->name);
152    vl_step_ports(file, prim->ports);
153    vl_fprintf_stmt(file, ";\n");
154    vl_step_mod_item_list(file, prim->decls);
155    vl_step_prim_table(file, prim->type, prim->entries);
156    vl_fprintf_stmt(file, "endprimitive\n\n");
157}
158
159
160void vl_step_prim_table(FILE *file, short type, lsList entries)
161{
162    vl_prim_entry *e;
163    int i;
164    lsHandle handle;
165    lsGen gen;
166
167    if (entries == NULL) return;
168
169    StpTRACE("Printing primitive table\n");
170    vl_fprintf_stmt(file, "table\n");
171    for(gen = lsStart(entries); lsNext(gen, (lsGeneric*)&e, &handle)
172                                    != LS_NOMORE; ) {
173        for (i=0; i<10; i++) {
174            if (e->inputs[i] == PrimNone)
175                break;
176            else
177                vl_fprintf_stmt(file, "%s ", vl_get_prim_symbol(e->inputs[i]));
178        }
179        if (type == SeqPrimDecl)
180            vl_fprintf_stmt(file, ": %s", vl_get_prim_symbol(e->state));
181
182        vl_fprintf_stmt(file, ": %s\n", vl_get_prim_symbol(e->next_state));
183    }
184    (void) lsFinish(gen);
185    vl_fprintf_stmt(file, "endtable\n");
186}
187
188
189void vl_step_basicdecl(FILE *file, vl_decl *decl)
190{
191
192    StpTRACE("Printing Basic decl\n");
193    switch(decl->type) {
194    case RealDecl:
195        vl_fprintf_stmt(file, "real ");
196        vl_step_id_list(file, decl->ids);
197        break;
198    case EventDecl:
199        vl_fprintf_stmt(file, "event ");
200        vl_step_id_list(file, decl->ids);
201        break;
202    case IntDecl:
203        vl_fprintf_stmt(file, "integer ");
204        vl_step_id_range_list(file, decl->ids);
205        break;
206    case TimeDecl:
207        vl_fprintf_stmt(file, "time ");
208        vl_step_id_range_list(file, decl->ids);
209        break;
210    default:
211        internal_error("Unexpected Basic Decl type");   break;
212    }
213    vl_fprintf_stmt(file, ";\n");
214}
215
216
217void vl_step_rangedecl(FILE *file, vl_rangedecl *decl)
218{
219    StpTRACE("Printing Range decl\n");
220    switch(decl->type) {
221    case InputDecl:
222        vl_fprintf_stmt(file, "input ");        break;
223    case OutputDecl:
224        vl_fprintf_stmt(file, "output ");       break;
225    case InoutDecl:
226        vl_fprintf_stmt(file, "inout ");        break;
227    case RegDecl:
228        vl_fprintf_stmt(file, "reg ");  break;
229    default:
230        internal_error("Unexpected Range Decl");        break;
231    }
232    vl_step_range(file, decl->range);
233    if (decl->type == RegDecl)
234        vl_step_id_range_list(file, decl->ids);
235    else
236        vl_step_id_list(file, decl->ids);
237    vl_fprintf_stmt(file, ";\n");
238}
239
240void vl_step_paramdecl(FILE *file, vl_paramdecl *decl)
241{
242
243    StpTRACE("Printing Parameter decl\n");
244    switch(decl->type) {
245    case ParamDecl:
246        vl_fprintf_stmt(file, "parameter ");    break;
247    case DefparamDecl:
248        vl_fprintf_stmt(file, "defparam ");     break;
249    default:
250        internal_error("Unexpected Param Decl");        break;
251    }
252
253    {
254        vl_bassign_stmt *assign;
255        lsHandle handle;
256        lsGen gen;
257
258        gen = lsStart(decl->assigns);
259        if (lsNext(gen, (lsGeneric*)&assign, &handle) == LS_OK) {
260            vl_step_bassign_stmt(file, assign);
261            while(lsNext(gen, (lsGeneric*)&assign, &handle) != LS_NOMORE) {
262                vl_fprintf_stmt(file, ", ");
263                vl_step_bassign_stmt(file, assign);
264            }
265        }
266    }
267    vl_fprintf_stmt(file, ";\n");
268}
269
270void vl_step_netdecl(FILE *file, vl_netdecl *decl)
271{
272
273    StpTRACE("Printing Net decl\n");
274    switch(decl->type) {
275    case WireDecl:
276        vl_fprintf_stmt(file, "wire");  break;
277    case TriDecl:
278        vl_fprintf_stmt(file, "tri");   break;
279    case Tri0Decl:
280        vl_fprintf_stmt(file, "tri0");  break;
281    case Tri1Decl:
282        vl_fprintf_stmt(file, "tri1");  break;
283    case Supply0Decl:
284        vl_fprintf_stmt(file, "supply0");       break;
285    case Supply1Decl:
286        vl_fprintf_stmt(file, "supply1");       break;
287    case WandDecl:
288        vl_fprintf_stmt(file, "wand");  break;
289    case TriandDecl:
290        vl_fprintf_stmt(file, "triand");        break;
291    case WorDecl:
292        vl_fprintf_stmt(file, "wor");   break;
293    case TriorDecl:
294        vl_fprintf_stmt(file, "trior"); break;
295    case TriregDecl:
296        vl_fprintf_stmt(file, "trireg");        break;
297    default:
298        internal_error("Unexpected Net Decl");  break;
299    }
300    vl_step_strength(file, decl->strength);
301    vl_step_range(file, decl->range);
302    vl_step_delay(file, decl->delay);
303    vl_step_net_list(file, decl->ids);
304    vl_fprintf_stmt(file, ";\n");
305
306}
307
308void vl_step_task(FILE *file, vl_task *task)
309{
310
311    StpTRACE("Printing Task\n");
312    ASSERT(task->type == TaskDecl, "Unexpected Task Type");
313
314    vl_fprintf_stmt(file, "\ntask ");
315    vl_step_id_range(file, task->name);
316    vl_fprintf_stmt(file, ";\n");
317    vl_step_decl_list(file, task->decls);
318    vl_step_stmt_list(file, task->stmts);
319    vl_fprintf_stmt(file, "endtask\n");
320}
321
322void vl_step_function(FILE *file, vl_function *func)
323{
324    int old_dflow_analysis;
325    vl_function *old_function;
326    vl_module *old_module;
327    vl_module *old_enclosemodule;
328
329    StpTRACE("Printing Function\n");
330
331
332    old_dflow_analysis = dflow_analysis;
333    dflow_analysis = 0;
334    dataflow_init();
335
336    vl_fprintf_stmt(file, "\nfunc ");
337    switch(func->type) {
338    case IntFuncDecl:
339        st_insert(vl_currentModule->func_st, func->name->name, (char*)func);
340        vl_fprintf_stmt(file, "integer ");
341        break;
342    case RealFuncDecl:
343        st_insert(vl_currentModule->func_st, func->name->name, (char*)func);
344        vl_fprintf_stmt(file, "real ");
345        break;
346    case RangeFuncDecl:
347        st_insert(vl_currentModule->func_st, func->name->name, (char*)func);
348        vl_step_range(file, func->range);
349        break;
350    default:
351        internal_error("Unexpected Function Type");
352    }
353    vl_step_id_range(file, func->name);
354    vl_fprintf_stmt(file, ";\n");
355    old_module = vl_currentModule;
356    old_function = vl_currentFunction;
357    old_enclosemodule = vl_encloseModule;
358    vl_encloseModule = vl_currentModule;
359    vl_currentModule = (vl_module*)func;
360    vl_currentFunction = func;
361    vl_step_decl_list(file, func->decls);
362    vl_step_stmt_list(file, func->stmts);
363    vl_currentModule = old_module;
364    vl_currentFunction = old_function;
365    vl_encloseModule = old_enclosemodule;
366    vl_fprintf_stmt(file, "endfunction\n");
367
368    dflow_analysis = old_dflow_analysis;
369    dataflow_end();
370}
371
372void vl_step_gate_inst_list(FILE *file, vl_gate_inst_list *gatelist)
373{
374
375    StpTRACE("Printing Gate Instance List\n");
376    switch(gatelist->type) {
377    case AndGate:
378        vl_fprintf_stmt(file, "and");   break;
379    case NandGate:
380        vl_fprintf_stmt(file, "nand");  break;
381    case OrGate:
382        vl_fprintf_stmt(file, "or");    break;
383    case NorGate:
384        vl_fprintf_stmt(file, "nor");   break;
385    case XorGate:
386        vl_fprintf_stmt(file, "xor");   break;
387    case XnorGate:
388        vl_fprintf_stmt(file, "xnor");  break;
389    case BufGate:
390        vl_fprintf_stmt(file, "buf");   break;
391    case Bufif0Gate:
392        vl_fprintf_stmt(file, "bufif0");        break;
393    case Bufif1Gate:
394        vl_fprintf_stmt(file, "bufif1");        break;
395    case NotGate:
396        vl_fprintf_stmt(file, "not");   break;
397    case Notif0Gate:
398        vl_fprintf_stmt(file, "notif0");        break;
399    case Notif1Gate:
400        vl_fprintf_stmt(file, "notif1");        break;
401    case PulldownGate:
402        vl_fprintf_stmt(file, "pulldown");      break;
403    case PullupGate:
404        vl_fprintf_stmt(file, "pullup");        break;
405    case NmosGate:
406        vl_fprintf_stmt(file, "nmos");  break;
407    case RnmosGate:
408        vl_fprintf_stmt(file, "rnmos"); break;
409    case PmosGate:
410        vl_fprintf_stmt(file, "pmos");  break;
411    case RpmosGate:
412        vl_fprintf_stmt(file, "rpmos"); break;
413    case CmosGate:
414        vl_fprintf_stmt(file, "cmos");  break;
415    case RcmosGate:
416        vl_fprintf_stmt(file, "rcmos"); break;
417    case TranGate:
418        vl_fprintf_stmt(file, "tran");  break;
419    case RtranGate:
420        vl_fprintf_stmt(file, "rtran"); break;
421    case Tranif0Gate:
422        vl_fprintf_stmt(file, "tranif0");       break;
423    case Rtranif0Gate:
424        vl_fprintf_stmt(file, "rtranif0");      break;
425    case Tranif1Gate:
426        vl_fprintf_stmt(file, "tranif1");       break;
427    case Rtranif1Gate:
428        vl_fprintf_stmt(file, "rtranif1");      break;
429    default:
430        internal_error("Unexpected Gate Type"); break;
431    }
432    vl_fprintf_stmt(file, " ");
433    vl_step_strength(file, gatelist->strength);
434    vl_step_delay(file, gatelist->delays);
435    vl_step_gates(file, gatelist->gates);
436    vl_fprintf_stmt(file, ";\n");
437}
438
439void vl_step_gates(FILE *file, lsList gates)
440{
441    vl_gate_inst *gate;
442    lsHandle handle;
443    lsGen gen;
444
445    StpTRACE("Printing Gate Instances\n");
446    gen = lsStart(gates);
447    if (lsNext(gen, (lsGeneric*)&gate, &handle) == LS_OK) {
448        vl_step_gate(file, gate);
449        while(lsNext(gen, (lsGeneric*)&gate, &handle) != LS_NOMORE) {
450            vl_fprintf_stmt(file, ", ");
451            vl_step_gate(file, gate);
452        }
453    }
454    (void) lsFinish(gen);
455}
456
457void vl_step_prim_inst_list(FILE *file, vl_mod_prim_inst_list *primlist)
458{
459
460    StpTRACE("Printing Primitive Instance List\n");
461
462    vl_step_id_range(file, primlist->name);
463    vl_step_strength(file, primlist->strength);
464    vl_step_delay(file, primlist->delays);
465    vl_step_prims(file, primlist->mps);
466    vl_fprintf_stmt(file, ";\n");
467}
468
469void vl_step_prims(FILE *file, lsList prims)
470{
471    vl_mod_prim_inst *prim;
472    lsHandle handle;
473    lsGen gen;
474
475    StpTRACE("Printing Primitive Instances\n");
476    gen = lsStart(prims);
477    if (lsNext(gen, (lsGeneric*)&prim, &handle) == LS_OK) {
478        vl_step_prim(file, prim);
479        while(lsNext(gen, (lsGeneric*)&prim, &handle) != LS_NOMORE) {
480            vl_fprintf_stmt(file, ", ");
481            vl_step_prim(file, prim);
482        }
483    }
484    (void) lsFinish(gen);
485}
486
487void vl_step_mod_inst_list(FILE *file, vl_mod_prim_inst_list *modinstlist)
488{
489    vl_module *result;
490
491    StpTRACE("Printing Module Instance List\n");
492
493    result = vl_chk_param_dup_master(modinstlist);
494    if (result) modinstlist->name = result->name;
495
496    vl_step_id_range(file, modinstlist->name);
497    vl_fprintf_stmt(file, " ");
498    vl_step_param_vals(file, (lsList)modinstlist->delays);
499    vl_step_mod_insts(file, modinstlist->mps);
500    vl_fprintf_stmt(file, ";\n");
501}
502
503void vl_step_mod_insts(FILE *file, lsList modinsts)
504{
505    vl_mod_prim_inst *modinst;
506    lsHandle handle;
507    lsGen gen;
508
509    StpTRACE("Printing Module Instances\n");
510    gen = lsStart(modinsts);
511    if (lsNext(gen, (lsGeneric*)&modinst, &handle) == LS_OK) {
512        vl_step_modinst(file, modinst);
513        while(lsNext(gen, (lsGeneric*)&modinst, &handle) != LS_NOMORE) {
514            vl_fprintf_stmt(file, ", ");
515            vl_step_modinst(file, modinst);
516        }
517    }
518    (void) lsFinish(gen);
519}
520
521
522void vl_step_procstmt(FILE *file, vl_procstmt *pstmt)
523{
524    int old_dflow_context;
525    graph_t *fg_FlowGraph;
526    edge_t *back_edge;
527
528    StpTRACE("Printing Process stmt\n");
529    old_dflow_context = dflow_context;
530    dataflow_init();
531
532    fg_FlowGraph = fg_new_graph();
533
534    vl_currentProcStmt = pstmt;
535
536    switch(pstmt->type) {
537    case AlwaysStmt: {
538        lsNewEnd(vl_currentModule->flow_graphs, (lsGeneric)fg_FlowGraph, 0);
539
540
541        back_edge = fg_new_always_block();
542
543
544        dflow_context |= DFLOW_Always;
545        vl_fprintf_stmt(file, "always\n");
546        break;
547    }
548    case InitialStmt:
549        dflow_context |= DFLOW_Initial;
550        if (pstmt->flags & TimedInitStmt) {
551            lsNewEnd(vl_currentModule->flow_graphs,(lsGeneric)fg_FlowGraph,0);
552
553            fg_new_always_block();
554
555            pstmt->type = AlwaysStmt;
556            pstmt->flags |= PseudoAlways;
557        } else {
558            in_reset_stmt = 1;
559            vl_currentModule->flags |= WithInitial;
560        }
561        vl_fprintf_stmt(file, "initial\n");
562        break;
563    default:
564        internal_error("Unexpected Process Statement Type");    break;
565    }
566
567
568    vl_step_stmt(file, pstmt->stmt);
569    in_reset_stmt = 0;
570    vl_currentProcStmt = NIL(vl_procstmt);
571
572    dflow_context = old_dflow_context;
573    dataflow_end();
574
575
576    if (pstmt->flags & PseudoAlways) {
577        pstmt->fg_info = (char*)fg_pseudo_sink();
578        ((fg_node_info*)((vertex_t*)pstmt->fg_info)->user_data)->data =
579            (char*)pstmt;
580    } else {
581        fg_closeloop();
582    }
583
584    if (pstmt->type == AlwaysStmt && !(pstmt->flags & PseudoAlways)) {
585        assert(fg_check_cyclic());
586    }
587
588    fg_clean_fg();
589}
590
591int vl_step_begin_end_stmt(FILE *file, vl_begin_end_stmt *bestmt)
592{
593    int step_flag;
594
595    StpTRACE("Printing beginend\n");
596    ASSERT(bestmt->type == BeginEndStmt, "Unexpected BeginEndStmt Type");
597
598    vl_fprintf_stmt(file, "begin\n");
599    vl_step_decl_list(file, bestmt->decls);
600    step_flag = vl_step_stmt_list(file, bestmt->stmts);
601    vl_fprintf_stmt(file, "end\n");
602
603    return step_flag;
604}
605
606int vl_step_if_else_stmt(FILE *file, vl_if_else_stmt *stmt)
607{
608    set_t *if_set = NIL(set_t), *else_set = NIL(set_t);
609    set_t *new_set = NIL(set_t);
610    int step_flag_if = 0, step_flag_else = 0;
611    vertex_t *old_Srce, *old_Sink;
612    vertex_t *old_CMPSrce, *old_CMPSink;
613    vertex_t *ctrl_vtx, *sink_vtx;
614    vertex_t *cmp_ctrl_vtx, *cmp_sink_vtx;
615    vertex_t *src, *sink;
616    vertex_t *cmp_src, *cmp_sink;
617
618    StpTRACE("Printing if_else_stmt\n");
619    ASSERT(stmt->type == IfElseStmt, "Unexpected IfElseStmt Type");
620
621    if (vl_currentFunction)
622
623        vl_encloseModule->syndrome_width++;
624    else {
625
626        if (!in_reset_stmt)
627            vl_currentModule->syndrome_width++;
628        else
629            vl_currentModule->rst_syndrome_width++;
630    }
631
632
633
634    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
635        fg_new_ifelse_block(&old_Srce, &old_Sink, &ctrl_vtx, &sink_vtx,
636                            &old_CMPSrce,&old_CMPSink,
637                            &cmp_ctrl_vtx,&cmp_sink_vtx);
638        ((fg_node_info*)ctrl_vtx->user_data)->data = (char*)stmt->cond;
639        stmt->cond->fg_info1 = (char*)ctrl_vtx;
640        stmt->cond->fg_info2 = (char*)sink_vtx;
641        fg_record_SrcSink(&src, &sink, &cmp_src, &cmp_sink);
642    }
643
644    vl_fprintf_stmt(file, "if (");
645    vl_step_expr(file, stmt->cond);
646    vl_fprintf_stmt(file, ")\n");
647
648
649    dataflow_dup_set();
650    step_flag_if = vl_step_stmt(file, stmt->if_stmt);
651    if_set = dataflow_pop_set();
652
653
654    if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
655        stmt->cond->fg_aux1 = (char*)fg_closeloop();
656
657
658    if (stmt->else_stmt) {
659        vl_fprintf_stmt(file, "else\n");
660
661
662        if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
663            fg_restore_SrcSink(src, sink, cmp_src, cmp_sink);
664
665        dataflow_dup_set();
666        step_flag_else = vl_step_stmt(file, stmt->else_stmt);
667        else_set = dataflow_pop_set();
668
669        new_set = dataflow_merge_set(if_set, else_set);
670        dataflow_replace_set(new_set);
671
672
673        if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
674            stmt->cond->fg_aux2 = (char*)fg_closeloop();
675    } else {
676        if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
677
678            fg_restore_SrcSink(src, sink, cmp_src, cmp_sink);
679
680            stmt->cond->fg_aux2 = (char*)fg_closeloop();
681        }
682    }
683
684
685
686    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
687        fg_restore_SrcSink(src, sink, cmp_src, cmp_sink);
688        fg_reduce_simple_branches_back_block(old_Srce, old_Sink,
689                                             old_CMPSrce, old_CMPSink);
690    }
691
692    return step_flag_if | step_flag_else;
693}
694
695int vl_step_case_stmt(FILE *file, vl_case_stmt *stmt)
696{
697    int step_flag = 0;
698    vertex_t *old_Srce, *old_Sink;
699    vertex_t *ctrl_vtx, *sink_vtx;
700    vertex_t *old_CMPSrce, *old_CMPSink;
701    vertex_t *ctrl_CMPvtx, *sink_CMPvtx;
702
703    StpTRACE("Printing case_stmt\n");
704
705
706
707    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
708        fg_new_case_block(&old_Srce, &old_Sink, &ctrl_vtx, &sink_vtx,
709                          &old_CMPSrce, &old_CMPSink,
710                          &ctrl_CMPvtx, &sink_CMPvtx);
711        ((fg_node_info*)ctrl_vtx->user_data)->data = (char*)stmt->cond;
712        stmt->cond->fg_info1 = (char*)ctrl_vtx;
713        stmt->cond->fg_info2 = (char*)sink_vtx;
714    }
715
716    switch(stmt->type) {
717    case CaseStmt:
718        vl_fprintf_stmt(file, "case (");        break;
719    case CasexStmt:
720        vl_fprintf_stmt(file, "casex (");       break;
721    case CasezStmt:
722        vl_fprintf_stmt(file, "casez (");       break;
723    default:
724        internal_error("Unexpected Case Type"); break;
725    }
726    vl_step_expr(file, stmt->cond);
727    vl_fprintf_stmt(file, ")\n");
728
729    {
730        vl_case_item *item;
731        lsHandle handle;
732        lsGen gen;
733        vertex_t *cur_src, *cur_sink;
734        vertex_t *cur_cmp_src, *cur_cmp_sink;
735        edge_t *ctrl_edge;
736
737        if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
738            fg_record_SrcSink(&cur_src, &cur_sink,
739                              &cur_cmp_src, &cur_cmp_sink);
740        gen = lsStart(stmt->case_items);
741        while(lsNext(gen, (lsGeneric*)&item, &handle) != LS_NOMORE) {
742
743            if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
744                ctrl_edge = fg_new_case_tag_block(cur_src, cur_sink,
745                                                  cur_cmp_src, cur_cmp_sink);
746                ((fg_arc_info*)ctrl_edge->user_data)->data = (char*)item;
747                item->fg_info = (char*)ctrl_edge;
748            }
749
750            dataflow_dup_set();
751            step_flag |= vl_step_case_item(file, item);
752            if (item->type != DefaultItem)
753                dataflow_free_set(dataflow_pop_set());
754            else
755                dataflow_replace_set(dataflow_pop_set());
756
757
758            fg_closeloop();
759        }
760        if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
761            fg_restore_SrcSink(cur_src, cur_sink, cur_cmp_src, cur_cmp_sink);
762    }
763    vl_fprintf_stmt(file, "endcase\n");
764
765    if (!in_reset_stmt && vlPhase==1 && fg_not_empty())
766        fg_reduce_simple_branches_back_block(old_Srce, old_Sink,
767                                             old_CMPSrce, old_CMPSink);
768
769    return step_flag;
770}
771
772void vl_step_forever_stmt(FILE *file, vl_forever_stmt *stmt)
773{
774    StpTRACE("Printing forever_stmt\n");
775    ASSERT(stmt->type == ForeverStmt, "Unexpected ForeverStmt Type");
776
777    vl_fprintf_stmt(file, "forever\n");
778    vl_step_stmt(file, stmt->stmt);
779}
780
781void vl_step_repeat_stmt(FILE *file, vl_repeat_stmt *stmt)
782{
783    StpTRACE("Printing repeat_stmt\n");
784    ASSERT(stmt->type == RepeatStmt, "Unexpected RepeatStmt Type");
785
786    vl_fprintf_stmt(file, "repeat (");
787    vl_step_expr(file, stmt->count);
788    vl_fprintf_stmt(file, ")\n");
789    vl_step_stmt(file, stmt->stmt);
790}
791
792void vl_step_while_stmt(FILE *file, vl_while_stmt *stmt)
793{
794    vertex_t *old_src, *old_sink, *ctrl_vtx, *sink_vtx,
795             *old_CMPsrc, *old_CMPsink, *cmp_ctrl_vtx, *cmp_sink_vtx;
796    vertex_t *src, *sink, *cmp_src, *cmp_sink;
797    int contain_pause;
798
799    StpTRACE("Printing while_stmt\n");
800    ASSERT(stmt->type == WhileStmt, "Unexpected WhileStmt Type");
801
802    if (!in_reset_stmt) vl_currentModule->syndrome_width++;
803
804
805    if (!in_reset_stmt &&vlPhase==1 && fg_not_empty()) {
806        fg_new_loop_block(&old_src, &old_sink, &ctrl_vtx, &sink_vtx,
807                         &old_CMPsrc, &old_CMPsink,
808                         &cmp_ctrl_vtx, &cmp_sink_vtx);
809        ((fg_node_info*)ctrl_vtx->user_data)->data = (char*)stmt->cond;
810        stmt->cond->fg_info1 = (char*)ctrl_vtx;
811        stmt->cond->fg_info2 = (char*)sink_vtx;
812        fg_record_SrcSink(&src, &sink, &cmp_src, &cmp_sink);
813    }
814
815    vl_fprintf_stmt(file, "while (");
816    vl_step_expr(file, stmt->cond);
817    vl_fprintf_stmt(file, ")\n");
818    vl_step_stmt(file, stmt->stmt);
819
820
821    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
822        fg_closeloop();
823        fg_restore_SrcSink(src, sink, cmp_src, cmp_sink);
824        contain_pause =
825            fg_reduce_simple_branches_back_block(old_src, old_sink,
826                                                 old_CMPsrc, old_CMPsink);
827        if (!contain_pause) {
828            char buf[MAXSTRLEN];
829            yylineno = -1;
830            sprintf(buf, "%s: there exist delay free for loop(s)",
831                    vl_currentModule->name->name);
832            compile_error(buf);
833        }
834        fg_force_edge(sink_vtx, ctrl_vtx, cmp_sink_vtx, cmp_ctrl_vtx);
835        fg_restore_SrcSink(ctrl_vtx, old_sink, cmp_ctrl_vtx, old_CMPsink);
836    }
837}
838
839void vl_step_for_stmt(FILE *file, vl_for_stmt *stmt)
840{
841    vertex_t *old_src, *old_sink, *ctrl_vtx, *sink_vtx,
842             *old_CMPsrc, *old_CMPsink, *cmp_ctrl_vtx, *cmp_sink_vtx;
843    vertex_t *src, *sink, *cmp_src, *cmp_sink;
844    int contain_pause;
845
846    StpTRACE("Printing for_stmt\n");
847    ASSERT(stmt->type == ForStmt, "Unexpected ForStmt Type");
848
849    if (!in_reset_stmt) vl_currentModule->syndrome_width++;
850
851    vl_fprintf_stmt(file, "for (");
852    vl_step_bassign_stmt(file, stmt->init);
853    vl_fprintf_stmt(file, "; ");
854
855
856    if (!in_reset_stmt &&vlPhase==1 && fg_not_empty()) {
857        fg_new_loop_block(&old_src, &old_sink, &ctrl_vtx, &sink_vtx,
858                         &old_CMPsrc, &old_CMPsink,
859                         &cmp_ctrl_vtx, &cmp_sink_vtx);
860        ((fg_node_info*)ctrl_vtx->user_data)->data = (char*)stmt->cond;
861        stmt->cond->fg_info1 = (char*)ctrl_vtx;
862        stmt->cond->fg_info2 = (char*)sink_vtx;
863        fg_record_SrcSink(&src, &sink, &cmp_src, &cmp_sink);
864    }
865    vl_step_expr(file, stmt->cond);
866    vl_fprintf_stmt(file, "; ");
867
868    vl_step_bassign_stmt(file, stmt->end);
869    vl_fprintf_stmt(file, ")\n");
870    vl_step_stmt(file, stmt->stmt);
871
872
873    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
874        fg_closeloop();
875        fg_restore_SrcSink(src, sink, cmp_src, cmp_sink);
876        contain_pause =
877            fg_reduce_simple_branches_back_block(old_src, old_sink,
878                                                 old_CMPsrc, old_CMPsink);
879        if (!contain_pause) {
880            char buf[MAXSTRLEN];
881            yylineno = -1;
882            sprintf(buf, "%s: there exist delay free for loop(s)",
883                    vl_currentModule->name->name);
884            compile_error(buf);
885        }
886        fg_force_edge(sink_vtx, ctrl_vtx, cmp_sink_vtx, cmp_ctrl_vtx);
887        fg_restore_SrcSink(ctrl_vtx, old_sink, cmp_ctrl_vtx, old_CMPsink);
888    }
889}
890
891void vl_step_delay_control_stmt(FILE *file, vl_delay_control_stmt *stmt)
892{
893    vertex_t *pause_node;
894
895    StpTRACE("Printing delay_control_stmt\n");
896    ASSERT(stmt->type == DelayControlStmt, "Unexpected DelayControlStmt Type");
897
898    if (implicitClocking && !vlTimedSystem) {
899
900        char buf[MAXSTRLEN];
901        yylineno = -1;
902        if (vl_currentModule)
903            sprintf(buf, "%s: 'always'-statement contains delay which contradicts with implicit clocking",
904                    vl_currentModule->name->name);
905        else
906            sprintf(buf, "delay(s) in implicit clocking");
907        compile_error(buf);
908    }
909
910    vl_step_delay(file, stmt->delay);
911
912
913    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
914        pause_node = fg_new_pause_node();
915        ((fg_node_info*)pause_node->user_data)->data = (char*)stmt;
916        stmt->fg_info = (char*)pause_node;
917    }
918
919    vl_step_stmt(file, stmt->stmt);
920}
921
922int vl_step_event_control_stmt(FILE *file, vl_event_control_stmt *stmt)
923{
924    int step_flag = 0;
925    vertex_t *pause_node;
926
927    StpTRACE("Printing event_control_stmt\n");
928    ASSERT(stmt->type == EventControlStmt, "Unexpected EventControlStmt Type");
929
930    vl_fprintf_stmt(file, "@(");
931    step_flag = vl_step_event_expr(file, stmt->event);
932    vl_fprintf_stmt(file, ")");
933
934
935    if (!in_reset_stmt && vlPhase==1 && fg_not_empty()) {
936        pause_node = fg_new_pause_node();
937        ((fg_node_info*)pause_node->user_data)->data = (char*)stmt;
938        stmt->fg_info = (char*)pause_node;
939    }
940
941    vl_step_stmt(file, stmt->stmt);
942
943    return step_flag;
944}
945
946
947int vl_step_bassign_stmt(FILE *file, vl_bassign_stmt *bassign)
948{
949    int step_flag = 0;
950    vertex_t *assign_vtx;
951
952    StpTRACE("Printing bassign_stmt\n");
953
954
955    if (!in_reset_stmt && vlPhase == 1 && fg_not_empty()) {
956        assign_vtx = fg_new_assign_node();
957        bassign->fg_info = (char*)assign_vtx;
958    }
959
960    if (bassign->type == AssignStmt) {
961        vl_fprintf_stmt(file, "assign ");
962    }
963
964    lhs_id = 1;
965    vl_step_lval(file, bassign->lhs);
966    lhs_id = 0;
967
968    switch(bassign->type) {
969    case AssignStmt:
970    case BassignStmt:
971        vl_fprintf_stmt(file, " = ");
972        break;
973    case NbassignStmt:
974        vl_fprintf_stmt(file, " <= ");
975        collect_latch(bassign->lhs->name->name);
976        break;
977    case DelayBassignStmt:
978        vl_fprintf_stmt(file, " = ");
979        vl_step_delay(file, (vl_delay *)bassign->control);
980        break;
981    case DelayNbassignStmt:
982        vl_fprintf_stmt(file, " <= ");
983        vl_step_delay(file, (vl_delay *)bassign->control);
984        collect_latch(bassign->lhs->name->name);
985        break;
986    case EventBassignStmt:
987        vl_fprintf_stmt(file, " = @(");
988        step_flag =
989            vl_step_event_expr(file, (vl_event_expr *)bassign->control);
990        vl_fprintf_stmt(file, ")");
991        break;
992    case EventNbassignStmt:
993        vl_fprintf_stmt(file, " <= @(");
994        step_flag =
995            vl_step_event_expr(file, (vl_event_expr *)bassign->control);
996        vl_fprintf_stmt(file, ")");
997        collect_latch(bassign->lhs->name->name);
998        break;
999    default:
1000        internal_error("Unexpected Assign Type");       break;
1001    }
1002    vl_step_expr(file, bassign->rhs);
1003
1004    return step_flag;
1005}
1006
1007void vl_step_wait_stmt(FILE *file, vl_wait_stmt *stmt)
1008{
1009   vl_begin_end_stmt *faked_stmt;
1010
1011    StpTRACE("Printing wait_stmt\n");
1012    ASSERT(stmt->type == WaitStmt, "Unexpected WaitStmt Type");
1013
1014    faked_stmt = vl_fake_wait_stmt(file, stmt);
1015    vl_fprintf_stmt(file, "wait (");
1016    vl_step_expr(file, stmt->cond);
1017    vl_fprintf_stmt(file, ")\n");
1018    vl_step_stmt(file, stmt->stmt);
1019    stmt->stmt = faked_stmt;
1020    stmt->cond = NIL(vl_expr);
1021}
1022
1023void vl_step_fork_join_stmt(FILE *file, vl_fork_join_stmt *stmt)
1024{
1025
1026    StpTRACE("Printing fork_join_stmt\n");
1027    ASSERT(stmt->type == ForkJoinStmt, "Unexpected ForkJoinStmt Type");
1028
1029    vl_fprintf_stmt(file, "fork\n");
1030    vl_step_decl_list(file, stmt->decls);
1031    vl_step_stmt_list(file, stmt->stmts);
1032    vl_fprintf_stmt(file, "join\n");
1033}
1034
1035
1036void vl_step_task_enable_stmt(FILE *file, vl_task_enable_stmt *stmt)
1037{
1038    vl_task *task_dfn;
1039    lsGen stmtgen;
1040    lsHandle stmthandle;
1041    lsList unrolled_stmt;
1042    void *task_stmt;
1043    void *new_stmt;
1044
1045    StpTRACE("Printing task_enable_stmt\n");
1046    ASSERT(((stmt->type == TaskEnableStmt) ||
1047            (stmt->type == SysTaskEnableStmt)),
1048           "Unexpected TaskEnableStmt Type");
1049
1050    vl_step_id_range(file, stmt->name);
1051    if (!st_lookup(vl_currentModule->task_st, stmt->name->name,
1052                   (char**)&task_dfn)) {
1053        char buf[MAXSTRLEN];
1054        sprintf(buf, "Undefined task/function '%s'", stmt->name->name);
1055        yylineno = -1;
1056        compile_error(buf);
1057    }
1058    if (stmt->args) {
1059        vl_fprintf_stmt(file, " (");
1060        vl_step_expr_list(file, stmt->args);
1061        vl_fprintf_stmt(file, " )");
1062
1063
1064        assoc_formal_actual(task_dfn, task_dfn->io_lst, stmt->args);
1065    }
1066
1067
1068    aliasStack[aliasSP++] = task_dfn->sig_st;
1069    unrolled_stmt = lsCreate();
1070    for (stmtgen = lsStart(task_dfn->stmts);
1071         lsNext(stmtgen, (lsGeneric*)&task_stmt, &stmthandle) != LS_NOMORE; ) {
1072        new_stmt = vl_copy_stmt(task_stmt);
1073        lsNewEnd(unrolled_stmt, (lsGeneric)new_stmt, 0);
1074        vl_step_stmt(file, new_stmt);
1075    }
1076    lsFinish(stmtgen);
1077    aliasSP--;
1078    ((vl_begin_end_stmt*)stmt)->type  = BeginEndStmt;
1079    ((vl_begin_end_stmt*)stmt)->flags = 0;
1080    ((vl_begin_end_stmt*)stmt)->name  = NIL(vl_id_range);
1081    ((vl_begin_end_stmt*)stmt)->decls = NIL(void);
1082    ((vl_begin_end_stmt*)stmt)->sig_st= NIL(st_table);
1083    ((vl_begin_end_stmt*)stmt)->stmts = unrolled_stmt;
1084
1085    vl_fprintf_stmt(file, ";\n");
1086}
1087
1088void vl_step_disable_stmt(FILE *file, vl_disable_stmt *stmt)
1089{
1090    StpTRACE("Printing disable_stmt\n");
1091    ASSERT(stmt->type == DisableStmt, "Unexpected DisableStmt Type");
1092
1093    vl_fprintf_stmt(file, "disable ");
1094    vl_step_id_range(file, stmt->name);
1095    vl_fprintf_stmt(file, ";\n");
1096}
1097
1098void vl_step_deassign_stmt(FILE *file, vl_deassign_stmt *stmt)
1099{
1100    StpTRACE("Printing deassign_stmt\n");
1101    ASSERT(stmt->type == DeassignStmt, "Unexpected DeassignStmt Type");
1102
1103    if (!in_reset_stmt && vlPhase == 1 && fg_not_empty()) {
1104        vertex_t *assign_vtx;
1105        assign_vtx = fg_new_assign_node();
1106        stmt->fg_info = (char*)assign_vtx;
1107    }
1108
1109    vl_fprintf_stmt(file, "deassign ");
1110    vl_step_lval(file, stmt->lhs);
1111    vl_fprintf_stmt(file, ";\n");
1112}
1113
1114int vl_step_case_item(FILE *file, vl_case_item *item)
1115{
1116    int step_flag = 0;
1117
1118    StpTRACE("Printing case_item\n");
1119    switch(item->type) {
1120    case CaseItem:
1121        if (vl_currentFunction)
1122
1123            vl_encloseModule->syndrome_width++;
1124        else {
1125
1126            if (!in_reset_stmt)
1127                vl_currentModule->syndrome_width++;
1128            else
1129                vl_currentModule->rst_syndrome_width++;
1130        }
1131        vl_expand_Q(file, item->exprs);
1132        vl_step_expr_list(file, item->exprs);
1133        vl_fprintf_stmt(file, ": ");
1134        break;
1135    case DefaultItem:
1136        vl_fprintf_stmt(file, "default: ");
1137        break;
1138    default:
1139        internal_error("Unexpected CaseItem Type");
1140        break;
1141    }
1142    step_flag = vl_step_stmt(file, item->stmt);
1143
1144    return step_flag;
1145}
1146
1147int vl_step_event_expr(FILE *file, vl_event_expr *event)
1148{
1149    int step_flag = 0;
1150
1151    StpTRACE("Printing event_expr\n");
1152    if (event == NIL(vl_event_expr)) return 0;
1153    switch(event->type) {
1154    case OrEventExpr:  {
1155        vl_event_expr *e;
1156        lsHandle handle;
1157        lsGen gen;
1158
1159        gen = lsStart(event->list);
1160        if (lsNext(gen, (lsGeneric*)&e, &handle) == LS_OK) {
1161            vl_step_event_expr(file, e);
1162            while(lsNext(gen, (lsGeneric*)&e, &handle) != LS_NOMORE) {
1163                vl_fprintf_stmt(file, " or ");
1164                vl_step_event_expr(file, e);
1165            }
1166        }
1167        (void) lsFinish(gen);
1168
1169        return step_flag;
1170    }
1171    case NegedgeEventExpr:
1172        vl_fprintf_stmt(file, "negedge ");
1173        record_program_location(vl_currentProcStmt);
1174        step_flag = SYNCHRONIZED;
1175        vl_step_expr(file, event->expr);
1176        register_clock(file, event);
1177        break;
1178    case PosedgeEventExpr:
1179        vl_fprintf_stmt(file, "posedge ");
1180        if (vl_currentProcStmt) record_program_location(vl_currentProcStmt);
1181        step_flag = SYNCHRONIZED;
1182        vl_step_expr(file, event->expr);
1183        if (vl_currentProcStmt) register_clock(file, event);
1184        break;
1185    case EdgeEventExpr:
1186        vl_fprintf_stmt(file, "edge ");
1187        record_program_location(vl_currentProcStmt);
1188        step_flag = SYNCHRONIZED;
1189        vl_step_expr(file, event->expr);
1190        register_clock(file, event);
1191        break;
1192    case EventExpr:
1193        vl_step_expr(file, event->expr);
1194        break;
1195    default:
1196        internal_error("Unexpected EventExpr Type");
1197        break;
1198    }
1199
1200    return step_flag;
1201}
1202
1203void vl_step_lval(FILE *file, vl_lval *lval)
1204{
1205    StpTRACE("Printing lval\n");
1206
1207    switch(lval->type) {
1208    case IDExpr:
1209        vl_step_id_range(file, lval->name);
1210        break;
1211    case BitSelExpr:
1212    case PartSelExpr:
1213        vl_step_id_range(file, lval->name);
1214        vl_step_range(file, lval->range);
1215        break;
1216    case ConcatExpr: {
1217        vl_expr *e;
1218        lsHandle handle;
1219        lsGen gen;
1220
1221        vl_fprintf_stmt(file, "{");
1222        gen = lsStart(lval->concat);
1223        if (lsNext(gen, (lsGeneric*)&e, &handle) == LS_OK) {
1224            vl_step_expr(file, e);
1225            while(lsNext(gen, (lsGeneric*)&e, &handle) != LS_NOMORE) {
1226                vl_fprintf_stmt(file, ", ");
1227                vl_step_expr(file, e);
1228            }
1229        }
1230        vl_fprintf_stmt(file, "}");
1231        (void) lsFinish(gen);
1232
1233        break;
1234    }
1235    default:
1236        internal_error("Unexpected Lval Type");
1237        break;
1238    }
1239}
1240
1241void vl_step_expr(FILE *file, vl_expr *expr)
1242{
1243    vl_id_range *id_sym;
1244
1245    if (!expr) return;
1246    StpTRACE("Printing expr\n");
1247    switch (expr->type) {
1248    case BitExpr:
1249        vl_fprintf_stmt(file, "'b%s", expr->u.exprs.e3);
1250        break;
1251    case IntExpr:
1252        vl_fprintf_stmt(file, "%d", expr->u.intval);
1253        break;
1254    case RealExpr:
1255        vl_fprintf_stmt(file, "%f", expr->u.realval);
1256        break;
1257    case IDExpr:
1258
1259        if (!vl_currentPrimitive && !vl_currentFunction)
1260            if (vl_currentModule->param_st)
1261                if (st_lookup(vl_currentModule->param_st, expr->u.name->name,
1262                              (char**)&id_sym)) {
1263                    vl_free_id(expr->u.name);
1264
1265                    expr->type = IntExpr;
1266                    expr->u.intval = (int)(id_sym->mpg_master_exp);
1267                    vl_fprintf_stmt(file, "%d", expr->u.intval);
1268
1269                    break;
1270                }
1271
1272        vl_step_id_range(file, expr->u.name);
1273        break;
1274    case BitSelExpr:    case PartSelExpr:
1275        vl_step_id_range(file, expr->u.idrng);
1276        break;
1277    case ConcatExpr: {
1278
1279        vl_expr *e;
1280        lsHandle handle;
1281        lsGen gen;
1282
1283        vl_fprintf_stmt(file, "{");
1284        gen = lsStart((lsList)(expr->u.exprs.e1));
1285        if (lsNext(gen, (lsGeneric*)&e, &handle) == LS_OK) {
1286            vl_step_expr(file, e);
1287            while(lsNext(gen, (lsGeneric*)&e, &handle) != LS_NOMORE) {
1288                vl_fprintf_stmt(file, ", ");
1289                vl_step_expr(file, e);
1290            }
1291        }
1292        vl_fprintf_stmt(file, "}");
1293        (void) lsFinish(gen);
1294        break;
1295    }
1296    case MinTypMaxExpr: {
1297
1298        vl_fprintf_stmt(file, "(");
1299        vl_step_expr(file, expr->u.exprs.e1);
1300        if (expr->u.exprs.e2) {
1301            vl_fprintf_stmt(file, ":");
1302            vl_step_expr(file, expr->u.exprs.e2);
1303        }
1304        if (expr->u.exprs.e3) {
1305            vl_fprintf_stmt(file, ":");
1306            vl_step_expr(file, expr->u.exprs.e3);
1307        }
1308        vl_fprintf_stmt(file, ")");
1309        break;
1310    }
1311    case StringExpr:
1312        vl_fprintf_stmt(file, "%s", expr->u.string);
1313        break;
1314    case FuncExpr:
1315        vl_step_id_range(file, expr->u.func_call.name);
1316        if (expr->u.func_call.args) {
1317            vl_fprintf_stmt(file, " (");
1318            vl_step_expr_list(file, expr->u.func_call.args);
1319            vl_fprintf_stmt(file, ") ");
1320        }
1321        break;
1322    case UplusExpr:    case UminusExpr:
1323    case UnotExpr:    case UcomplExpr:
1324
1325    case UandExpr:    case UnandExpr:
1326    case UorExpr:    case UnorExpr:
1327    case UxorExpr:    case UxnorExpr:
1328        vl_fprintf_stmt(file, "%s", vl_get_expr_op(expr->type));
1329        vl_step_expr(file, expr->u.exprs.e1);
1330        break;
1331
1332    case BplusExpr:    case BminusExpr:
1333    case BtimesExpr:    case BdivExpr:
1334    case BremExpr:    case Beq2Expr:
1335    case Bneq2Expr:    case Beq3Expr:
1336    case Bneq3Expr:    case BlandExpr:
1337    case BlorExpr:    case BltExpr:
1338    case BleExpr:   case BgtExpr:
1339    case BgeExpr:    case BandExpr:
1340    case BorExpr:    case BxorExpr:
1341    case BxnorExpr:    case BlshiftExpr:
1342    case BrshiftExpr:
1343        vl_step_expr(file, expr->u.exprs.e1);
1344        vl_fprintf_stmt(file, " %s ", vl_get_expr_op(expr->type));
1345        vl_step_expr(file, expr->u.exprs.e2);
1346        break;
1347
1348    case TcondExpr:
1349        vl_step_expr(file, expr->u.exprs.e1);
1350        vl_fprintf_stmt(file, " ? ");
1351        vl_step_expr(file, expr->u.exprs.e2);
1352        vl_fprintf_stmt(file, " : ");
1353        vl_step_expr(file, expr->u.exprs.e3);
1354        break;
1355    case NondExpr:
1356        vl_fprintf_stmt(file, "$ND ( ");
1357        vl_step_expr_list(file, expr->u.expr_list);
1358        vl_fprintf_stmt(file, " ) ");
1359        break;
1360    }
1361}
1362
1363
1364void vl_step_expr_list(FILE *file, lsList exprs)
1365{
1366    void *expr;
1367    lsHandle handle;
1368    lsGen gen;
1369
1370    StpTRACE("Printing Expression List\n");
1371    if (lsLength(exprs) <= 0) return;
1372    gen = lsStart(exprs);
1373    if (lsNext(gen, (lsGeneric*)&expr, &handle) == LS_OK) {
1374        vl_step_expr(file, expr);
1375        while(lsNext(gen, (lsGeneric*)&expr, &handle) != LS_NOMORE) {
1376            vl_fprintf_stmt(file, ",");
1377            vl_step_expr(file, expr);
1378        }
1379    }
1380    (void) lsFinish(gen);
1381
1382}
1383
1384void vl_step_range(FILE *file, vl_range *range)
1385{
1386    StpTRACE("Printing range\n");
1387
1388    if (range == NIL(vl_range) ||
1389        (range->left == NIL(vl_expr) && range->right == NIL(vl_expr))) return;
1390    vl_fprintf_stmt(file, "[");
1391    vl_step_expr(file, range->left);
1392    if (range->right) {
1393        vl_fprintf_stmt(file, " : ");
1394        vl_step_expr(file, range->right);
1395    }
1396    vl_fprintf_stmt(file, "]");
1397}
1398
1399
1400void vl_step_delay(FILE *file, vl_delay *delay)
1401{
1402    StpTRACE("Printing delay\n");
1403    if (delay == NIL(vl_delay)) return;
1404    vl_fprintf_stmt(file, " #(");
1405    vl_step_expr(file, delay->delay1);
1406    if (delay->delay2) {
1407        vl_fprintf_stmt(file, ", ");
1408        vl_step_expr(file, delay->delay2);
1409        if (delay->delay3) {
1410            vl_fprintf_stmt(file, ", ");
1411            vl_step_expr(file, delay->delay3);
1412        }
1413    }
1414    vl_fprintf_stmt(file, ") ");
1415}
1416
1417
1418void vl_step_id_range(FILE *file, vl_id_range *id_range)
1419{
1420    StpTRACE("Printing id[range]\n");
1421
1422    if (id_range == NIL(vl_id_range)) return;
1423
1424    vl_fprintf_stmt(file, "%s ", id_range->name);
1425    if (id_range->range != NIL(vl_range))
1426        vl_step_range(file, id_range->range);
1427
1428
1429
1430    if (dflow_analysis) {
1431        dataflow_analysis(dflow_analysis, id_range, !lhs_id, lhs_id);
1432    }
1433}
1434
1435
1436void vl_step_id_list(FILE *file, lsList ids)
1437{
1438    vl_id_range *id;
1439    lsHandle handle;
1440    lsGen gen;
1441
1442    gen = lsStart(ids);
1443    if (lsNext(gen, (lsGeneric*)&id, &handle) == LS_OK) {
1444        vl_step_id_range(file, id);
1445        while(lsNext(gen, (lsGeneric*)&id, &handle) != LS_NOMORE) {
1446            vl_fprintf_stmt(file, ", ");
1447            vl_step_id_range(file, id);
1448        }
1449    }
1450    (void) lsFinish(gen);
1451}
1452
1453
1454void vl_step_id_range_list(FILE *file, lsList ids)
1455{
1456    vl_id_range *id;
1457    lsHandle handle;
1458    lsGen gen;
1459
1460    if (ids == (lsList)0) return;
1461    gen = lsStart(ids);
1462    if (lsNext(gen, (lsGeneric*)&id, &handle) == LS_OK) {
1463        vl_step_id_range(file, id);
1464        while(lsNext(gen, (lsGeneric*)&id, &handle) != LS_NOMORE) {
1465            vl_fprintf_stmt(file, ", ");
1466            vl_step_id_range(file, id);
1467        }
1468    }
1469    (void) lsFinish(gen);
1470}
1471
1472
1473void vl_step_net_list(FILE *file, lsList nets)
1474{
1475    void *net;
1476    lsHandle handle;
1477    lsGen gen;
1478
1479    gen = lsStart(nets);
1480    if (lsNext(gen, (lsGeneric*)&net, &handle) == LS_OK) {
1481        vl_step_net(file, net);
1482        while(lsNext(gen, (lsGeneric*)&net, &handle) != LS_NOMORE) {
1483            vl_fprintf_stmt(file, ", ");
1484            vl_step_net(file, net);
1485        }
1486    }
1487    (void) lsFinish(gen);
1488}
1489
1490
1491void vl_step_net(FILE *file, void *net)
1492{
1493    switch(((vl_expr *)net)->type) {
1494    case BassignStmt:
1495        vl_step_bassign_stmt(file, net);           break;
1496    case NbassignStmt: {
1497        char buf[MAXSTRLEN];
1498        yylineno = ((vl_bassign_stmt*)net)->lineno;
1499        sprintf(buf, ":%s: invalid continuous assignment",
1500                vl_currentModule->name->name);
1501        compile_error(buf);
1502        break;
1503    }
1504    case IDExpr:
1505        vl_step_expr(file, net);                   break;
1506    default:
1507        vl_step_id_range(file, (vl_id_range*)net); break;
1508    }
1509}
1510
1511
1512void vl_step_ports (FILE *file, lsList ports)
1513{
1514    void *item;
1515    lsHandle handle;
1516    lsGen gen;
1517
1518    vl_fprintf_stmt(file, "(");
1519
1520    if (ports != (lsList)0) {
1521        gen = lsStart(ports);
1522        if (lsNext(gen, (lsGeneric*)&item, &handle) == LS_OK) {
1523            vl_step_port(file, item);
1524            while (lsNext(gen, (lsGeneric*)&item, &handle) != LS_NOMORE) {
1525                vl_fprintf_stmt(file, ",");
1526                vl_step_port(file, item);
1527            }
1528        }
1529        (void) lsFinish(gen);
1530    }
1531    vl_fprintf_stmt(file, ")");
1532}
1533
1534void vl_step_port(FILE *file, vl_portPtr port)
1535{
1536    if (port->type == NamedPort) {
1537        vl_fprintf_stmt(file, ".");
1538        vl_step_id_range(file, port->id);
1539        vl_fprintf_stmt(file, "(");
1540    }
1541
1542    vl_step_id_range_list(file, port->port_exp);
1543
1544    if (port->type == NamedPort) {
1545        vl_fprintf_stmt(file, ")");
1546    }
1547}
1548
1549
1550void vl_step_connects (FILE *file, lsList connects)
1551{
1552    void *item;
1553    lsHandle handle;
1554    lsGen gen;
1555
1556
1557    if (connects != (lsList)0) {
1558        gen = lsStart(connects);
1559        if (lsNext(gen, (lsGeneric*)&item, &handle) == LS_OK) {
1560            vl_step_port_connect(file, (vl_port_connectPtr)item);
1561            while (lsNext(gen, (lsGeneric*)&item, &handle) != LS_NOMORE) {
1562                vl_fprintf_stmt(file, ",");
1563                vl_step_port_connect(file, (vl_port_connectPtr)item);
1564            }
1565        }
1566        (void) lsFinish(gen);
1567    }
1568}
1569
1570void vl_step_port_connect(FILE *file, vl_port_connectPtr connect)
1571{
1572    if (connect->type == NamedConnect) {
1573        vl_fprintf_stmt(file, ".");
1574        vl_step_id_range(file, connect->id);
1575        vl_fprintf_stmt(file, "(");
1576    }
1577
1578    vl_step_expr(file, connect->expr);
1579
1580    if (connect->type == NamedConnect) {
1581        vl_fprintf_stmt(file, ")");
1582    }
1583}
1584
1585
1586void vl_step_mod_item_list (FILE *file, lsList mitems)
1587{
1588    void *item;
1589    lsHandle handle;
1590    lsGen gen;
1591
1592    StpTRACE("Printing Module Items\n");
1593    if (mitems != (lsList)0) {
1594        gen = lsStart(mitems);
1595        if (lsNext(gen, (lsGeneric*)&item, &handle) == LS_OK) {
1596            vl_step_mod_item(file, item);
1597            while(lsNext(gen, (lsGeneric*)&item, &handle) != LS_NOMORE) {
1598                vl_step_mod_item(file, item);
1599            }
1600        }
1601        (void) lsFinish(gen);
1602    }
1603}
1604
1605
1606void vl_step_mod_item(FILE *file, void *item)
1607{
1608    switch(((vl_decl *)item)->type) {
1609
1610    case RealDecl:      case EventDecl:
1611        vl_step_basicdecl(file, (vl_decl *)item);
1612        break;
1613
1614    case IntDecl:       case TimeDecl:
1615        vl_step_basicdecl(file, (vl_decl *)item);
1616        break;
1617
1618    case InputDecl:     case OutputDecl:
1619    case InoutDecl:     case RegDecl:
1620        vl_step_rangedecl(file, (vl_rangedecl *)item);
1621        break;
1622
1623    case ParamDecl:     case DefparamDecl:
1624        vl_step_paramdecl(file, (vl_paramdecl *)item);
1625        break;
1626
1627    case WireDecl:      case TriDecl:
1628    case Tri0Decl:      case Tri1Decl:
1629    case Supply0Decl:   case Supply1Decl:
1630    case WandDecl:      case TriandDecl:
1631    case WorDecl:       case TriorDecl:
1632    case TriregDecl:
1633        vl_step_netdecl(file, (vl_netdecl *)item);
1634        break;
1635
1636    case ContAssign:
1637        vl_step_cont_assign(file, (vl_cont_assign *)item);
1638        break;
1639
1640    case TaskDecl:
1641        vl_step_task(file, (vl_task *)item);
1642        break;
1643
1644    case IntFuncDecl:   case RealFuncDecl:
1645    case RangeFuncDecl:
1646        vl_step_function(file, (vl_function *)item);
1647        break;
1648
1649    case AndGate:       case NandGate:
1650    case OrGate:        case NorGate:
1651    case XorGate:       case XnorGate:
1652    case BufGate:       case Bufif0Gate:
1653    case Bufif1Gate:    case NotGate:
1654    case Notif0Gate:    case Notif1Gate:
1655    case PulldownGate:  case PullupGate:
1656    case NmosGate:      case RnmosGate:
1657    case PmosGate:      case RpmosGate:
1658    case CmosGate:      case RcmosGate:
1659    case TranGate:      case RtranGate:
1660    case Tranif0Gate:   case Rtranif0Gate:
1661    case Tranif1Gate:   case Rtranif1Gate:
1662        vl_step_gate_inst_list(file, (vl_gate_inst_list *)item);
1663        break;
1664
1665    case TeslaTimerGate:
1666
1667        break;
1668
1669    case ModInst: {
1670        char *mp;
1671
1672        if (st_lookup(vl_description->mp_st,
1673                       ((vl_mod_prim_inst_list *)item)->name->name,
1674                       &mp)) {
1675            if (((typestructPtr)mp)->type == CombPrimDecl ||
1676                ((typestructPtr)mp)->type == SeqPrimDecl)
1677                goto step_prim_inst;
1678        }
1679        }
1680
1681        vl_step_mod_inst_list(file, (vl_mod_prim_inst_list *)item);
1682        break;
1683
1684step_prim_inst:
1685
1686    case PrimInst:
1687        vl_step_prim_inst_list(file, (vl_mod_prim_inst_list *)item);
1688        break;
1689
1690    case AlwaysStmt:
1691    case InitialStmt:
1692        vl_step_procstmt(file, (vl_procstmt *)item);
1693        break;
1694
1695    default:
1696        internal_error("Unexpected Module Item");
1697        break;
1698    }
1699}
1700
1701
1702void vl_step_decl_list (FILE *file, void *decls)
1703{
1704    void *decl;
1705    lsHandle handle;
1706    lsGen gen;
1707
1708    StpTRACE("Printing Decls\n");
1709    if (decls == NIL(void)) return;
1710    gen = lsStart(decls);
1711    if (lsNext(gen, (lsGeneric*)&decl, &handle) == LS_OK) {
1712        vl_step_decl(file, decl);
1713        while(lsNext(gen, (lsGeneric*)&decl, &handle) != LS_NOMORE) {
1714            vl_step_decl(file, decl);
1715        }
1716    }
1717    (void) lsFinish(gen);
1718
1719}
1720
1721int vl_step_stmt_list (FILE *file, void *stmts)
1722{
1723    void *stmt;
1724    lsHandle handle;
1725    lsGen gen;
1726    int step_flag=0;
1727
1728    StpTRACE("Printing Statements\n");
1729    gen = lsStart(stmts);
1730    if (lsNext(gen, (lsGeneric*)&stmt, &handle) == LS_OK) {
1731        step_flag = vl_step_stmt(file, stmt);
1732        while(lsNext(gen, (lsGeneric*)&stmt, &handle) != LS_NOMORE) {
1733            step_flag |= vl_step_stmt(file, stmt);
1734        }
1735    }
1736    (void) lsFinish(gen);
1737
1738    return step_flag;
1739}
1740
1741void vl_step_strength (FILE *file, int s)
1742{
1743    vl_fprintf_stmt(file, "%s", vl_get_strength(s));
1744}
1745
1746
1747void vl_step_gate (FILE *file, vl_gate_inst *gate)
1748{
1749    vl_step_id_range(file, gate->name);
1750    vl_fprintf_stmt(file, "(");
1751    vl_step_expr_list(file, gate->terms);
1752    vl_fprintf_stmt(file, ")");
1753}
1754
1755void vl_step_prim (FILE *file, vl_mod_prim_inst *prim)
1756{
1757    vl_step_id_range(file, prim->name);
1758    vl_fprintf_stmt(file, "(");
1759    vl_step_expr_list(file, prim->ports);
1760    vl_fprintf_stmt(file, ")");
1761}
1762
1763void vl_step_param_vals (FILE *file, lsList param_vals)
1764{
1765    if (param_vals == (lsList)0) return;
1766
1767    vl_fprintf_stmt(file, "#(");
1768    if (!((vl_delay*)param_vals)->delay1 &&
1769        !((vl_delay*)param_vals)->delay2 &&
1770         ((vl_delay*)param_vals)->delay3)
1771        vl_step_expr_list(file, (lsList)((vl_delay*)param_vals)->delay3);
1772    else if ( ((vl_delay*)param_vals)->delay1 &&
1773             !((vl_delay*)param_vals)->delay2)
1774        vl_step_expr(file, ((vl_delay*)param_vals)->delay1);
1775    vl_fprintf_stmt(file, ")");
1776}
1777
1778void vl_step_modinst (FILE *file, vl_mod_prim_inst *mod)
1779{
1780    vl_step_id_range(file, mod->name);
1781
1782    vl_fprintf_stmt(file, " (");
1783    vl_step_connects(file, mod->ports);
1784    vl_fprintf_stmt(file, ") ");
1785}
1786
1787int vl_step_stmt (FILE *file, void *stmt)
1788{
1789    int step_flag=0;
1790
1791
1792    if (stmt == NIL(void)) {
1793        vl_fprintf_stmt(file, ";\n");
1794        return step_flag;
1795    }
1796
1797    switch(((vl_bassign_stmt *)stmt)->type) {
1798    case BeginEndStmt:
1799        step_flag = vl_step_begin_end_stmt(file, (vl_begin_end_stmt *)stmt);
1800        break;
1801    case IfElseStmt:
1802        step_flag = vl_step_if_else_stmt(file, (vl_if_else_stmt *)stmt);
1803        break;
1804    case CaseStmt:
1805    case CasexStmt:
1806    case CasezStmt:
1807        step_flag = vl_step_case_stmt(file, (vl_case_stmt *)stmt);
1808        break;
1809    case CaseItem:
1810        step_flag = vl_step_stmt(file, ((vl_case_item *)stmt)->stmt);
1811        break;
1812    case DefaultItem:
1813        step_flag = vl_step_stmt(file, ((vl_case_item *)stmt)->stmt);
1814        break;
1815    case ForeverStmt:
1816        vl_step_forever_stmt(file, (vl_forever_stmt *)stmt);
1817        break;
1818    case RepeatStmt:
1819        if (!Repeat_Unrolling) {
1820            vl_step_repeat_stmt(file, (vl_repeat_stmt *)stmt);
1821        } else {
1822            int i, up_bnd;
1823            lsList unrolled_stmt;
1824            void *new_stmt;
1825
1826            unrolled_stmt = lsCreate();
1827            up_bnd = vl_eval_expr(((vl_repeat_stmt*)stmt)->count);
1828            for (i=0; i<up_bnd; i++) {
1829                new_stmt = (void*)vl_copy_stmt(((vl_repeat_stmt*)stmt)->stmt);
1830                lsNewEnd(unrolled_stmt, (lsGeneric)new_stmt, 0);
1831                vl_step_stmt(file, new_stmt);
1832            }
1833            ((vl_begin_end_stmt*)stmt)->type  = BeginEndStmt;
1834            ((vl_begin_end_stmt*)stmt)->flags = 0;
1835            ((vl_begin_end_stmt*)stmt)->name  = NIL(vl_id_range);
1836            ((vl_begin_end_stmt*)stmt)->decls = NIL(void);
1837            ((vl_begin_end_stmt*)stmt)->sig_st= NIL(st_table);
1838            ((vl_begin_end_stmt*)stmt)->stmts = unrolled_stmt;
1839        }
1840        break;
1841    case WhileStmt:
1842        vl_step_while_stmt(file, (vl_while_stmt *)stmt);
1843        break;
1844    case ForStmt:
1845        if (!Loop_Unrolling) {
1846            vl_step_for_stmt(file, (vl_for_stmt *)stmt);
1847        } else {
1848            vl_id_range *idx;
1849            char *name, *val;
1850            lsList unrolled_stmt;
1851            void *new_stmt;
1852            vl_expr *ival;
1853            vl_lval *idx_lhs;
1854            vl_bassign_stmt *assign_idx=NULL;
1855
1856            idx = ((vl_for_stmt*)stmt)->init->lhs->name;
1857            st_insert(vl_currentModule->param_st, idx->name, (char*)idx);
1858            unrolled_stmt = lsCreate();
1859
1860            for (idx->mpg_master_exp =
1861                     (void*)vl_eval_expr(((vl_for_stmt*)stmt)->init->rhs);
1862                 vl_eval_expr(((vl_for_stmt*)stmt)->cond);
1863                 idx->mpg_master_exp =
1864                     (void*)vl_eval_expr(((vl_for_stmt*)stmt)->end->rhs)) {
1865                idx_lhs = vl_create_lval(IDExpr, vl_copy_id_range(idx),
1866                                         NIL(vl_range),(lsList)0);
1867                ival = vl_create_expr(IntExpr, (int)idx->mpg_master_exp,
1868                                      (double)0.0,
1869                                      NIL(vl_expr),NIL(vl_expr),NIL(vl_expr));
1870
1871                new_stmt = (void*)vl_copy_stmt(((vl_for_stmt*)stmt)->stmt);
1872                if (indexAssignment) {
1873                    assign_idx = vl_create_bassign_stmt(BassignStmt, idx_lhs,
1874                                                        NIL(void), ival);
1875                    lsNewEnd(unrolled_stmt, (lsGeneric)assign_idx, 0);
1876                }
1877                lsNewEnd(unrolled_stmt, (lsGeneric)new_stmt, 0);
1878                if (indexAssignment)
1879                    vl_step_stmt(file, assign_idx);
1880                vl_step_stmt(file, new_stmt);
1881            }
1882
1883            ((vl_begin_end_stmt*)stmt)->type  = BeginEndStmt;
1884            ((vl_begin_end_stmt*)stmt)->flags = 0;
1885            ((vl_begin_end_stmt*)stmt)->name  = NIL(vl_id_range);
1886            ((vl_begin_end_stmt*)stmt)->decls = NIL(void);
1887            ((vl_begin_end_stmt*)stmt)->sig_st= NIL(st_table);
1888            ((vl_begin_end_stmt*)stmt)->stmts = unrolled_stmt;
1889
1890            name = idx->name;
1891            st_delete(vl_currentModule->param_st, &name, &val);
1892        }
1893        break;
1894    case DelayControlStmt:
1895        vl_step_delay_control_stmt(file, (vl_delay_control_stmt *)stmt);
1896        break;
1897    case EventControlStmt:
1898        step_flag = vl_step_event_control_stmt(file,
1899                                               (vl_event_control_stmt *)stmt);
1900        break;
1901    case AssignStmt:
1902    case BassignStmt:
1903    case NbassignStmt:
1904    case DelayBassignStmt:
1905    case DelayNbassignStmt:
1906    case EventBassignStmt:
1907    case EventNbassignStmt:
1908        step_flag = vl_step_bassign_stmt(file, (vl_bassign_stmt *)stmt);
1909        vl_fprintf_stmt(file, ";\n");
1910        break;
1911    case WaitStmt:
1912        vl_step_wait_stmt(file, (vl_wait_stmt *)stmt);
1913        break;
1914    case ForkJoinStmt:
1915        vl_step_fork_join_stmt(file, (vl_fork_join_stmt *)stmt);
1916        break;
1917    case TaskEnableStmt:
1918    case SysTaskEnableStmt:
1919        vl_step_task_enable_stmt(file, (vl_task_enable_stmt *)stmt);
1920        break;
1921    case DisableStmt:
1922        vl_step_disable_stmt(file, (vl_disable_stmt *)stmt);
1923        break;
1924    case DeassignStmt:
1925        vl_step_deassign_stmt(file, (vl_deassign_stmt *)stmt);
1926        break;
1927    case SendEventStmt:
1928        vl_fprintf_stmt(file, "-> ");
1929        vl_step_id_range(file, ((vl_send_event_stmt *)stmt)->name);
1930        if (vlPhase == 1 && fg_not_empty())
1931            ((vl_send_event_stmt*)stmt)->fg_info = (char*)fg_new_assign_node();
1932        vl_fprintf_stmt(file, ";\n");
1933        break;
1934    default: {
1935        char msg[MAXSTRLEN];
1936        sprintf(msg, "vl_step_stmt:Unexpected Statement Type %d",
1937                ((vl_bassign_stmt *)stmt)->type);
1938        internal_error(msg);
1939    }
1940    }
1941
1942    return step_flag;
1943}
1944
1945void vl_step_decl (FILE *file, void *decl)
1946{
1947    switch(((vl_decl *)decl)->type) {
1948    case RealDecl:    case EventDecl:
1949    case IntDecl:     case TimeDecl:
1950        vl_step_basicdecl(file, (vl_decl *)decl);
1951        break;
1952
1953    case InputDecl:    case OutputDecl:
1954    case InoutDecl:    case RegDecl:
1955        vl_step_rangedecl(file, (vl_rangedecl *)decl);
1956        break;
1957    case ParamDecl:    case DefparamDecl:
1958        vl_step_paramdecl(file, (vl_paramdecl *)decl);
1959        break;
1960    case WireDecl:      case TriDecl:
1961    case Tri0Decl:      case Tri1Decl:
1962    case Supply0Decl:   case Supply1Decl:
1963    case WandDecl:      case TriandDecl:
1964    case WorDecl:       case TriorDecl:
1965    case TriregDecl:
1966        vl_step_netdecl(file, (vl_netdecl *)decl);
1967        break;
1968    default:
1969        internal_error("Unexprected Declaration Type");
1970        break;
1971    }
1972}
1973
1974void vl_step_cont_assign (FILE *file, vl_cont_assign *assign)
1975{
1976    vl_fprintf_stmt(file, "assign ");
1977    vl_step_strength(file, assign->strength);
1978    vl_step_delay(file, assign->delay);
1979    vl_step_net_list(file, assign->assigns);
1980    vl_fprintf_stmt(file, ";\n");
1981}
1982
1983
1984
1985char *vl_get_prim_symbol(unsigned char sym)
1986{
1987    switch(sym) {
1988    case PrimNone: return("");
1989    case Prim0: return("0");
1990    case Prim1: return("1");
1991    case PrimX: return("X");
1992    case PrimQ: return("?");
1993    case PrimR: return("R");
1994    case PrimF: return("F");
1995    case PrimP: return("P");
1996    case PrimN: return("N");
1997    case PrimS: return("*");
1998    case PrimM: return("-");
1999    case PrimB: return("B");
2000    case Prim0X: return("(0X)");
2001    case Prim1X: return("(1X)");
2002    case PrimX0: return("(X0)");
2003    case PrimX1: return("(X1)");
2004    case PrimXB: return("(XB)");
2005    case PrimBX: return("(BX)");
2006    case PrimBB: return("(BB)");
2007    case PrimQ0: return("(?0)");
2008    case PrimQ1: return("(?1)");
2009    case PrimQB: return("(?B)");
2010    default: {
2011        char msg[MAXSTRLEN];
2012        sprintf(msg, "Unexpected primitive symbol type %d", sym);
2013        internal_error(msg); return(NULL);
2014    }
2015    }
2016}
2017
2018
2019char *vl_get_strength(int type)
2020{
2021    switch(type) {
2022    case 0:
2023        return("\0");
2024    case Supply0Supply1:
2025        return("(supply0, supply1)");
2026    case Supply0Strong1:
2027        return("(supply0, strong1)");
2028    case Supply0Pull1:
2029        return("(supply0, pull1)");
2030    case Supply0Weak1:
2031        return("(supply0, weak1)");
2032    case Supply0HighZ1:
2033        return("(supply0, highz1)");
2034    case Strong0Supply1:
2035        return("(strong0, supply1)");
2036    case Strong0Strong1:
2037        return("(strong0, strong1)");
2038    case Strong0Pull1:
2039        return("(strong0, pull1)");
2040    case Strong0Weak1:
2041        return("(strong0, weak1)");
2042    case Strong0HighZ1:
2043        return("(strong0, highz1)");
2044    case Pull0Supply1:
2045        return("(pull0, supply1)");
2046    case Pull0Strong1:
2047        return("(pull0, strong1)");
2048    case Pull0Pull1:
2049        return("(pull0, pull1)");
2050    case Pull0Weak1:
2051        return("(pull0, weak1)");
2052    case Pull0HighZ1:
2053        return("(pull0, highz1)");
2054    case Weak0Supply1:
2055        return("(weak0, supply1)");
2056    case Weak0Strong1:
2057        return("(weak0, strong1)");
2058    case Weak0Pull1:
2059        return("(weak0, pull1)");
2060    case Weak0Weak1:
2061        return("(weak0, weak1)");
2062    case Weak0HighZ1:
2063        return("(weak0, highz1)");
2064    case HighZ0Supply1:
2065        return("(highz0, supply1)");
2066    case HighZ0Strong1:
2067        return("(highz0, strong1)");
2068    case HighZ0Pull1:
2069        return("(highz0, pull1)");
2070    case HighZ0Weak1:
2071        return("(highz0, weak1)");
2072    case HighZ0HighZ1:
2073        return("(highz0, highz1)");
2074    case Supply1Supply0:
2075        return("(supply1, supply0)");
2076    case Supply1Strong0:
2077        return("(supply1, strong0)");
2078    case Supply1Pull0:
2079        return("(supply1, pull0)");
2080    case Supply1Weak0:
2081        return("(supply1, weak0)");
2082    case Supply1HighZ0:
2083        return("(supply1, highz0)");
2084    case Strong1Supply0:
2085        return("(strong1, supply0)");
2086    case Strong1Strong0:
2087        return("(strong1, strong0)");
2088    case Strong1Pull0:
2089        return("(strong1, pull0)");
2090    case Strong1Weak0:
2091        return("(strong1, weak0)");
2092    case Strong1HighZ0:
2093        return("(strong1, highz0)");
2094    case Pull1Supply0:
2095        return("(pull1, supply0)");
2096    case Pull1Strong0:
2097        return("(pull1, strong0)");
2098    case Pull1Pull0:
2099        return("(pull1, pull0)");
2100    case Pull1Weak0:
2101        return("(pull1, weak0)");
2102    case Pull1HighZ0:
2103        return("(pull1, highz0)");
2104    case Weak1Supply0:
2105        return("(weak1, supply0)");
2106    case Weak1Strong0:
2107        return("(weak1, strong0)");
2108    case Weak1Pull0:
2109        return("(weak1, pull0)");
2110    case Weak1Weak0:
2111        return("(weak1, weak0)");
2112    case Weak1HighZ0:
2113        return("(weak1, highz0)");
2114    case HighZ1Supply0:
2115        return("(highz1, supply0)");
2116    case HighZ1Strong0:
2117        return("(highz1, strong0)");
2118    case HighZ1Pull0:
2119        return("(highz1, pull0)");
2120    case HighZ1Weak0:
2121        return("(highz1, weak0)");
2122    case HighZ1HighZ0:
2123        return("(highz1, highz0)");
2124    case Small:
2125        return("(small)");
2126    case Medium:
2127        return("(medium)");
2128    case Large:
2129        return("(large)");
2130    default:
2131        {
2132        char err_msg[MAXSTRLEN];
2133
2134        sprintf(err_msg, "Unexpected strength type %d", type);
2135        internal_error(err_msg); return(NULL);
2136    }
2137    }
2138}
2139
2140
2141char *vl_get_expr_op(int type)
2142{
2143    switch(type) {
2144
2145    case IDExpr:         case BitSelExpr:
2146    case PartSelExpr:    case ConcatExpr:
2147    case MinTypMaxExpr:  case IntExpr:
2148    case RealExpr:       case StringExpr:
2149    case FuncExpr:
2150        return("");
2151    case UplusExpr:
2152        return("+");
2153    case UminusExpr:
2154        return("-");
2155    case UnotExpr:
2156        return("!");
2157    case UcomplExpr:
2158        return("~");
2159    case UandExpr:
2160        return("&");
2161    case UnandExpr:
2162        return("~&");
2163    case UorExpr:
2164        return("|");
2165    case UnorExpr:
2166        return("~|");
2167    case UxorExpr:
2168        return("^");
2169    case UxnorExpr:
2170        return("~^");
2171    case BplusExpr:
2172        return("+");
2173    case BminusExpr:
2174        return("-");
2175    case BtimesExpr:
2176        return("*");
2177    case BdivExpr:
2178        return("/");
2179    case BremExpr:
2180        return("%");
2181    case Beq2Expr:
2182        return("==");
2183    case Bneq2Expr:
2184        return("!=");
2185    case Beq3Expr:
2186        return("===");
2187    case Bneq3Expr:
2188        return("!==");
2189    case BlandExpr:
2190        return("&&");
2191    case BlorExpr:
2192        return("||");
2193    case BltExpr:
2194        return("<");
2195    case BleExpr:
2196        return("<=");
2197    case BgtExpr:
2198        return(">");
2199    case BgeExpr:
2200        return(">=");
2201    case BandExpr:
2202        return("&");
2203    case BorExpr:
2204        return("|");
2205    case BxorExpr:
2206        return("^");
2207    case BxnorExpr:
2208        return("~^");
2209    case BlshiftExpr:
2210        return("<<");
2211    case BrshiftExpr:
2212        return(">>");
2213
2214    case TcondExpr:
2215        return("?");
2216    default:
2217        internal_error("Unexpected expression type");   return(NULL);
2218    }
2219}
2220
2221static void record_program_location(vl_procstmt *procstmt)
2222{
2223    set_add(new_label(), procstmt->pc->values);
2224}
2225
2226char *new_label()
2227{
2228    static int label_count = 0;
2229    static char buf[MAXSTRLEN];
2230
2231    sprintf(buf, "%s%d", LABEL, label_count++);
2232    return buf;
2233}
2234
2235
2236static void register_clock(FILE *file, vl_event_expr *event)
2237{
2238    if (event->expr->type != IDExpr) return;
2239
2240
2241
2242    if (!vl_currentModule->clk)
2243        vl_currentModule->clk = event;
2244    else if (strcmp(vl_currentModule->clk->expr->u.name->name,
2245                    event->expr->u.name->name) &&
2246             implicitClocking && !vlTimedSystem) {
2247        char msg[MAXSTRLEN];
2248
2249        sprintf(msg, "'%s':%s is assumed to be clock, %s is reused as clock",
2250                vl_currentModule->name->name,
2251                vl_currentModule->clk->expr->u.name->name,
2252                event->expr->u.name->name);
2253        Translate_Warning(msg);
2254    }
2255
2256
2257    file = file;
2258}
2259
2260
2261vl_begin_end_stmt *vl_fake_wait_stmt(FILE *file, vl_wait_stmt *wait_stmt)
2262{
2263    lsList assign_list;
2264    vl_bassign_stmt *assignment;
2265    vl_expr *cond_expr;
2266    void *wait_ctrl_stmt;
2267    vl_expr *id_expr;
2268    vl_event_expr *event_expr;
2269    vl_event_control_stmt *edge_stmt;
2270    vl_expr *ncond_expr;
2271    lsList stmt_list;
2272    vl_if_else_stmt *if_else_stmt;
2273    vl_begin_end_stmt *retval;
2274
2275    cond_expr = wait_stmt->cond;
2276    wait_ctrl_stmt = wait_stmt->stmt;
2277
2278
2279    assignment =
2280        vl_create_bassign_stmt(AssignStmt,
2281            vl_create_lval(IDExpr,
2282                           vl_create_id_range(vlStrdup(new_termname()),
2283                                              NIL(vl_range)),
2284                           NIL(vl_range), (lsList)0),
2285                               NIL(void),
2286                               cond_expr);
2287    assign_list = lsCreate();
2288    lsNewEnd(assign_list, (lsGeneric)assignment, 0);
2289
2290
2291    lsNewBegin(vl_currentModule->mod_items,
2292               (lsGeneric)vl_create_cont_assign_stmt(0, NIL(vl_delay),
2293                                                     assign_list),
2294               0);
2295
2296
2297    id_expr = vl_create_expr(IDExpr, 0, (double)0.0,
2298                             assignment->lhs->name, NIL(vl_expr),NIL(vl_expr));
2299    event_expr = vl_create_event_expr(PosedgeEventExpr, id_expr);
2300    edge_stmt = vl_create_event_control_stmt(event_expr, NIL(typestruct));
2301    ncond_expr = vl_create_expr(UnotExpr, 0, (double)0.0,
2302                               id_expr, NIL(vl_expr), NIL(vl_expr));
2303    if_else_stmt =
2304        vl_create_if_else_stmt(ncond_expr, edge_stmt, NIL(typestruct));
2305    vl_step_if_else_stmt(file, if_else_stmt);
2306    stmt_list = lsCreate();
2307    lsNewEnd(stmt_list, (lsGeneric)if_else_stmt, 0);
2308    lsNewEnd(stmt_list, (lsGeneric)wait_ctrl_stmt, 0);
2309
2310    retval = vl_create_begin_end_stmt(NIL(vl_id_range), LS_NH,
2311                                               stmt_list);
2312
2313    return retval;
2314}
2315
2316
2317static void assoc_formal_actual(vl_task *task_dfn,
2318                                lsList formal, lsList actual)
2319{
2320    lsGen agen, fgen;
2321    lsHandle ahandle, fhandle;
2322    char *formal_arg;
2323    vl_expr *actual_arg;
2324
2325
2326    if (lsLength(formal) != lsLength(actual)) {
2327        compile_error("Initialize task with improper argument(s)");
2328    }
2329
2330    for (fgen=lsStart(formal), agen=lsStart(actual);
2331         lsNext(fgen, (lsGeneric*)&formal_arg, &fhandle) != LS_NOMORE &&
2332         lsNext(agen, (lsGeneric*)&actual_arg, &ahandle) != LS_NOMORE; ) {
2333        char *dummy_formal, *dummy_actual;
2334
2335        check_task_arg(actual_arg->type == IDExpr ||
2336                       actual_arg->type == PartSelExpr ||
2337                       actual_arg->type == BitSelExpr,
2338                       task_dfn);
2339        dummy_formal = formal_arg;
2340        st_delete(task_dfn->sig_st, &dummy_formal, &dummy_actual);
2341        st_insert(task_dfn->sig_st, formal_arg, actual_arg->u.name->name);
2342    }
2343    lsFinish(fgen);
2344    lsFinish(agen);
2345}
2346
2347
2348vl_module *vl_chk_param_dup_master(vl_mod_prim_inst_list *modinstlist)
2349{
2350    vl_module *retval;
2351    vl_delay *param_vals;
2352    vl_id_range *pval;
2353    lsList delay_list=(lsList)0;
2354    lsGen delay_gen=(lsGen)0;
2355    lsHandle delay_handle;
2356    int num_params=0;
2357    int i;
2358    vl_module *master, *new_module;
2359    char *pname;
2360    lsGen pgen;
2361    lsHandle phandle;
2362    vl_expr *de;
2363    int need_new_master;
2364    int rtvl;
2365
2366    if (modinstlist->delays == NIL(vl_delay)) return NIL(vl_module);
2367
2368    param_vals = modinstlist->delays;
2369    if (!param_vals->delay1 && !param_vals->delay2 && param_vals->delay3) {
2370        delay_list = (lsList)param_vals->delay3;
2371        delay_gen = lsStart(delay_list);
2372        num_params = lsLength(delay_list);
2373    } else if (param_vals->delay1 && !param_vals->delay2) {
2374        delay_list = (lsList)0;
2375        num_params = 1;
2376    }
2377
2378    rtvl = st_lookup(vl_description->mp_st,
2379                       modinstlist->name->name, (char**)&master);
2380    assert(rtvl);
2381    if (lsLength(master->param_list)==0) {
2382        if (WarningLevel >= 1) {
2383            char buf[MAXSTRLEN];
2384            yylineno = -1;
2385            sprintf(buf,
2386                    "%s:Giving parameter to a module(%s) without parameter",
2387                    vl_currentModule->name->name, master->name->name);
2388            compile_error(buf);
2389        } else return NIL(vl_module);
2390    }
2391
2392
2393    for (pgen=lsStart(master->param_list), need_new_master=0, i=0;
2394         lsNext(pgen, (lsGeneric*)&pname, &phandle)!=LS_NOMORE &&
2395             i<num_params; ) {
2396        int retval = st_lookup(master->param_st, pname, (char**)&pval);
2397        assert(retval);
2398        if (delay_list) {
2399            int rtvl2 = lsNext(delay_gen,(lsGeneric*)&de,&delay_handle);
2400            assert(rtvl2 != LS_NOMORE);
2401            i++;
2402        } else {
2403            de = param_vals->delay1;
2404        }
2405        if ((int)(pval->mpg_master_exp) != vl_eval_expr(de)) need_new_master=1;
2406    }
2407    lsFinish(pgen);
2408    if (delay_list) lsFinish(delay_gen);
2409
2410    retval = master;
2411
2412
2413    if (need_new_master) {
2414        char buf[MAXSTRLEN];
2415        vl_module *orig_module;
2416
2417
2418        sprintf(buf, "%s%04x", master->name->name, ++master->dup_count);
2419        new_module = vl_copy_rename_module(master, buf);
2420
2421
2422        if (delay_list) delay_gen = lsStart(delay_list);
2423        for (pgen=lsStart(new_module->param_list), need_new_master=0, i=0;
2424             lsNext(pgen, (lsGeneric*)&pname, &phandle)!=LS_NOMORE &&
2425                 i<num_params; ) {
2426            int retval = st_lookup(new_module->param_st, pname, (char**)&pval);
2427            assert(retval);
2428            if (delay_list) {
2429                int rtvl2 = lsNext(delay_gen,(lsGeneric*)&de,&delay_handle);
2430                assert(rtvl2 != LS_NOMORE);
2431                i++;
2432            } else {
2433                de = param_vals->delay1;
2434            }
2435            *(int*)&(pval->mpg_master_exp) = vl_eval_expr(de);
2436        }
2437        lsFinish(pgen);
2438        if (delay_list) lsFinish(delay_gen);
2439
2440        orig_module = vl_currentModule;
2441        vl_currentModule = new_module;
2442        vl_step_module(stdout, new_module);
2443        vl_currentModule = orig_module;
2444
2445        retval = new_module;
2446    }
2447
2448    return retval;
2449}
2450
2451
2452
2453static void vl_expand_Q(FILE *file, lsList tags)
2454{
2455    lsHandle handle;
2456    lsGen gen;
2457    vl_expr *expr;
2458    unsigned int i;
2459    int num_Q;
2460    char *bit_str;
2461    array_t *q_array;
2462
2463    for (gen=lsStart(tags);
2464         lsNext(gen,(lsGeneric*)&expr,&handle)!=LS_NOMORE; ) {
2465        if (expr->type != BitExpr) continue;
2466        if (strstr((char*)expr->u.exprs.e3,"?")) {
2467            for (i=0, num_Q=0; i<strlen((char*)expr->u.exprs.e3); i++)
2468                if (((char*)expr->u.exprs.e3)[i] == '?') num_Q++;
2469
2470            bit_str = vlStrdup((char*)expr->u.exprs.e3);
2471            q_array = array_alloc(int, 0);
2472            for (i=0; i<strlen((char*)expr->u.exprs.e3); i++)
2473                if (((char*)expr->u.exprs.e3)[i] == '?')
2474                    array_insert_last(int, q_array, i);
2475
2476            rec_expand_Q(num_Q-1, q_array, bit_str, tags, gen, &handle);
2477
2478            array_free(q_array);
2479            vl_chk_free(bit_str);
2480        }
2481    }
2482    lsFinish(gen);
2483}
2484
2485
2486
2487static void rec_expand_Q(int num_Q,
2488                         array_t *q_array,
2489                         char *bit_str,
2490                         lsList tags,
2491                         lsGen gen,
2492                         lsHandle *phandle)
2493{
2494    int b0, b1;
2495    vl_expr *new_expr;
2496    int ith;
2497
2498    if (num_Q == -1) {
2499        encodebit(bit_str, &b1, &b0);
2500        new_expr = vl_create_expr(BitExpr, 0, (double)0.0,
2501                                  (void*)b1, (void*)b0,
2502                                  (void*)vlStrdup(bit_str));
2503        lsInBefore(gen, (lsGeneric)new_expr, phandle);
2504    } else {
2505        ith = array_fetch(int, q_array, num_Q);
2506        bit_str[ith] = '1';
2507        rec_expand_Q(num_Q-1, q_array, bit_str, tags, gen, phandle);
2508        bit_str[ith] = '0';
2509        rec_expand_Q(num_Q-1, q_array, bit_str, tags, gen, phandle);
2510    }
2511}
2512
2513
2514static void check_task_arg(int ok, vl_task *task_dfn)
2515{
2516    char buf[MAXSTRLEN];
2517
2518    if (ok) return;
2519
2520    sprintf(buf, "%s: invoking task '%s' with non-simple id argiment(s); cannot be handled by vl2mv",
2521            vl_currentModule->name->name, task_dfn->name->name);
2522    yylineno = -1;
2523    compile_error(buf);
2524}
Note: See TracBrowser for help on using the repository browser.