Implement $ftell, $fseek and $rewind system functions.
[iverilog.git] / pform.cc
blob01a16aab49e0fbbe55909ae57ca0ec0d8c984111
1 /*
2 * Copyright (c) 1998-2007 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: pform.cc,v 1.148 2007/06/12 04:05:45 steve Exp $"
21 #endif
23 # include "config.h"
25 # include "compiler.h"
26 # include "pform.h"
27 # include "parse_misc.h"
28 # include "parse_api.h"
29 # include "PEvent.h"
30 # include "PUdp.h"
31 # include "PGenerate.h"
32 # include "PSpec.h"
33 # include <list>
34 # include <map>
35 # include <assert.h>
36 # include <stack>
37 # include <typeinfo>
38 # include <sstream>
40 map<perm_string,Module*> pform_modules;
41 map<perm_string,PUdp*> pform_primitives;
44 * The lexor accesses the vl_* variables.
46 string vl_file = "";
48 extern int VLparse();
50 /* This tracks the current module being processed. There can only be
51 exactly one module currently being parsed, since verilog does not
52 allow nested module definitions. */
53 static Module*pform_cur_module = 0;
55 bool pform_library_flag = false;
57 /* increment this for generate schemes within a module, and set it
58 to zero when a new module starts. */
59 static unsigned scope_generate_counter = 1;
61 /* This tracks the current generate scheme being processed. This is
62 always within a module. */
63 static PGenerate*pform_cur_generate = 0;
65 static NetNet::Type pform_default_nettype = NetNet::WIRE;
68 * These variables track the current time scale, as well as where the
69 * timescale was set. This supports warnings about tangled timescales.
71 static int pform_time_unit = 0;
72 static int pform_time_prec = 0;
74 static char*pform_timescale_file = 0;
75 static unsigned pform_timescale_line = 0;
78 * The scope stack and the following functions handle the processing
79 * of scope. As I enter a scope, the push function is called, and as I
80 * leave a scope the pop function is called. Entering tasks, functions
81 * and named blocks causes scope to be pushed and popped. The module
82 * name is not included in this scope stack.
84 * The hier_name function, therefore, converts the name to the scoped
85 * name within the module currently in progress. It never includes an
86 * instance name.
88 * The scope stack does not include any scope created by a generate
89 * scheme.
92 static pform_name_t scope_stack;
94 void pform_push_scope(char*name)
96 scope_stack.push_back(name_component_t(lex_strings.make(name)));
99 void pform_pop_scope()
101 scope_stack.pop_back();
104 static pform_name_t hier_name(const char*tail)
106 pform_name_t name = scope_stack;
107 name.push_back(name_component_t(lex_strings.make(tail)));
108 return name;
111 static PWire*get_wire_in_module(const pform_name_t&name)
113 /* Note that if we are processing a generate, then the
114 scope depth will be empty because generate schemes
115 cannot be within sub-scopes. Only directly in
116 modules. */
117 if (pform_cur_generate)
118 return pform_cur_generate->get_wire(name);
120 return pform_cur_module->get_wire(name);
123 void pform_set_default_nettype(NetNet::Type type,
124 const char*file, unsigned lineno)
126 pform_default_nettype = type;
128 if (pform_cur_module) {
129 cerr << file<<":"<<lineno << ": error: "
130 << "`default_nettype directives must appear" << endl;
131 cerr << file<<":"<<lineno << ": : "
132 << "outside module definitions. The containing" << endl;
133 cerr << file<<":"<<lineno << ": : "
134 << "module " << pform_cur_module->mod_name()
135 << " starts on line "
136 << pform_cur_module->get_line() << "." << endl;
137 error_count += 1;
142 * The lexor calls this function to set the active timescale when it
143 * detects a `timescale directive. The function saves the directive
144 * values (for use by modules) and if warnings are enabled checks to
145 * see if some modules have no timescale.
147 void pform_set_timescale(int unit, int prec,
148 const char*file, unsigned lineno)
150 bool first_flag = true;
152 assert(unit >= prec);
153 pform_time_unit = unit;
154 pform_time_prec = prec;
156 if (pform_timescale_file) {
157 free(pform_timescale_file);
158 first_flag = false;
161 pform_timescale_file = strdup(file);
162 pform_timescale_line = lineno;
164 if (warn_timescale && first_flag && (pform_modules.size() > 0)) {
165 cerr << file << ":" << lineno << ": warning: "
166 << "Some modules have no timescale. This may cause"
167 << endl;
168 cerr << file << ":" << lineno << ": : "
169 << "confusing timing results. Affected modules are:"
170 << endl;
172 map<perm_string,Module*>::iterator mod;
173 for (mod = pform_modules.begin()
174 ; mod != pform_modules.end() ; mod++) {
175 const Module*mp = (*mod).second;
177 cerr << file << ":" << lineno << ": : "
178 << " -- module " << (*mod).first
179 << " declared here: " << mp->get_line() << endl;
185 verinum* pform_verinum_with_size(verinum*siz, verinum*val,
186 const char*file, unsigned lineno)
188 assert(siz->is_defined());
189 unsigned long size = siz->as_ulong();
191 verinum::V pad;
193 switch (val->get(val->len()-1)) {
194 case verinum::Vz:
195 pad = verinum::Vz;
196 break;
197 case verinum::Vx:
198 pad = verinum::Vx;
199 break;
200 default:
201 pad = verinum::V0;
202 break;
205 verinum*res = new verinum(pad, size, true);
207 unsigned copy = val->len();
208 if (res->len() < copy)
209 copy = res->len();
211 for (unsigned idx = 0 ; idx < copy ; idx += 1) {
212 res->set(idx, val->get(idx));
215 res->has_sign(val->has_sign());
217 bool trunc_flag = false;
218 for (unsigned idx = copy ; idx < val->len() ; idx += 1) {
219 if (val->get(idx) != pad) {
220 trunc_flag = true;
221 break;
225 if (trunc_flag) {
226 cerr << file << ":" << lineno << ": warning: Numeric constant "
227 << "truncated to " << copy << " bits." << endl;
230 delete siz;
231 delete val;
232 return res;
235 void pform_startmodule(const char*name, const char*file, unsigned lineno,
236 svector<named_pexpr_t*>*attr)
238 assert( pform_cur_module == 0 );
240 perm_string lex_name = lex_strings.make(name);
241 pform_cur_module = new Module(lex_name);
242 pform_cur_module->time_unit = pform_time_unit;
243 pform_cur_module->time_precision = pform_time_prec;
244 pform_cur_module->default_nettype = pform_default_nettype;
246 pform_cur_module->set_file(file);
247 pform_cur_module->set_lineno(lineno);
248 pform_cur_module->library_flag = pform_library_flag;
250 /* The generate scheme numbering starts with *1*, not
251 zero. That's just the way it is, thanks to the standard. */
252 scope_generate_counter = 1;
254 if (warn_timescale && pform_timescale_file
255 && (strcmp(pform_timescale_file,file) != 0)) {
257 cerr << pform_cur_module->get_line() << ": warning: "
258 << "timescale for " << name
259 << " inherited from another file." << endl;
260 cerr << pform_timescale_file << ":" << pform_timescale_line
261 << ": ...: The inherited timescale is here." << endl;
263 if (attr) {
264 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
265 named_pexpr_t*tmp = (*attr)[idx];
266 pform_cur_module->attributes[tmp->name] = tmp->parm;
272 * This function is called by the parser to make a simple port
273 * reference. This is a name without a .X(...), so the internal name
274 * should be generated to be the same as the X.
276 Module::port_t* pform_module_port_reference(char*name,
277 const char*file,
278 unsigned lineno)
280 Module::port_t*ptmp = new Module::port_t;
281 PEIdent*tmp = new PEIdent(lex_strings.make(name));
282 tmp->set_file(file);
283 tmp->set_lineno(lineno);
284 ptmp->name = lex_strings.make(name);
285 ptmp->expr = svector<PEIdent*>(1);
286 ptmp->expr[0] = tmp;
288 return ptmp;
291 void pform_module_set_ports(svector<Module::port_t*>*ports)
293 assert(pform_cur_module);
295 /* The parser parses ``module foo()'' as having one
296 unconnected port, but it is really a module with no
297 ports. Fix it up here. */
298 if (ports && (ports->count() == 1) && ((*ports)[0] == 0)) {
299 delete ports;
300 ports = 0;
303 if (ports != 0) {
304 pform_cur_module->ports = *ports;
305 delete ports;
309 void pform_endmodule(const char*name)
311 assert(pform_cur_module);
312 perm_string mod_name = pform_cur_module->mod_name();
313 assert(strcmp(name, mod_name) == 0);
315 map<perm_string,Module*>::const_iterator test =
316 pform_modules.find(mod_name);
318 if (test != pform_modules.end()) {
319 ostringstream msg;
320 msg << "Module " << name << " was already declared here: "
321 << (*test).second->get_line() << endl;
322 VLerror(msg.str().c_str());
323 pform_cur_module = 0;
324 return;
326 pform_modules[mod_name] = pform_cur_module;
327 pform_cur_module = 0;
330 void pform_genvars(list<perm_string>*names)
332 list<perm_string>::const_iterator cur;
333 for (cur = names->begin(); cur != names->end() ; *cur++) {
334 pform_cur_module->genvars.push_back( *cur );
337 delete names;
340 void pform_start_generate_for(const struct vlltype&li,
341 char*ident1, PExpr*init,
342 PExpr*test,
343 char*ident2, PExpr*next)
345 PGenerate*gen = new PGenerate(scope_generate_counter++);
347 gen->set_file(li.text);
348 gen->set_lineno(li.first_line);
350 // For now, assume that generates do not nest.
351 gen->parent = pform_cur_generate;
352 pform_cur_generate = gen;
354 pform_cur_generate->scheme_type = PGenerate::GS_LOOP;
356 pform_cur_generate->loop_index = lex_strings.make(ident1);
357 pform_cur_generate->loop_init = init;
358 pform_cur_generate->loop_test = test;
359 pform_cur_generate->loop_step = next;
361 delete[]ident1;
362 delete[]ident2;
365 void pform_start_generate_if(const struct vlltype&li, PExpr*test)
367 PGenerate*gen = new PGenerate(scope_generate_counter++);
369 gen->set_file(li.text);
370 gen->set_lineno(li.first_line);
372 // For now, assume that generates do not nest.
373 gen->parent = pform_cur_generate;
374 pform_cur_generate = gen;
376 pform_cur_generate->scheme_type = PGenerate::GS_CONDIT;
378 pform_cur_generate->loop_init = 0;
379 pform_cur_generate->loop_test = test;
380 pform_cur_generate->loop_step = 0;
383 void pform_start_generate_else(const struct vlltype&li)
385 assert(pform_cur_generate);
386 assert(pform_cur_generate->scheme_type == PGenerate::GS_CONDIT);
388 PGenerate*cur = pform_cur_generate;
389 pform_endgenerate();
391 PGenerate*gen = new PGenerate(scope_generate_counter++);
393 gen->set_file(li.text);
394 gen->set_lineno(li.first_line);
396 // For now, assume that generates do not nest.
397 gen->parent = pform_cur_generate;
398 pform_cur_generate = gen;
400 pform_cur_generate->scheme_type = PGenerate::GS_ELSE;
402 pform_cur_generate->loop_init = 0;
403 pform_cur_generate->loop_test = cur->loop_test;
404 pform_cur_generate->loop_step = 0;
407 void pform_generate_block_name(char*name)
409 assert(pform_cur_generate != 0);
410 assert(pform_cur_generate->scope_name == 0);
411 pform_cur_generate->scope_name = lex_strings.make(name);
412 delete[]name;
415 void pform_endgenerate()
417 assert(pform_cur_generate != 0);
418 assert(pform_cur_module);
420 // If there is no explicit block name, then use a default
421 // internal name.
422 if (pform_cur_generate->scope_name == 0) {
423 char tmp[16];
424 snprintf(tmp, sizeof tmp, "$gen%d", pform_cur_generate->id_number);
425 pform_cur_generate->scope_name = lex_strings.make(tmp);
428 PGenerate*cur = pform_cur_generate;
429 pform_cur_generate = cur->parent;
431 if (pform_cur_generate != 0)
432 pform_cur_generate->generates.push_back(cur);
433 else
434 pform_cur_module->generate_schemes.push_back(cur);
437 bool pform_expression_is_constant(const PExpr*ex)
439 return ex->is_constant(pform_cur_module);
442 MIN_TYP_MAX min_typ_max_flag = TYP;
443 unsigned min_typ_max_warn = 10;
445 PExpr* pform_select_mtm_expr(PExpr*min, PExpr*typ, PExpr*max)
447 PExpr*res = 0;
449 switch (min_typ_max_flag) {
450 case MIN:
451 res = min;
452 delete typ;
453 delete max;
454 break;
455 case TYP:
456 res = typ;
457 delete min;
458 delete max;
459 break;
460 case MAX:
461 res = max;
462 delete min;
463 delete typ;
464 break;
467 if (min_typ_max_warn > 0) {
468 cerr << res->get_line() << ": warning: choosing ";
469 switch (min_typ_max_flag) {
470 case MIN:
471 cerr << "min";
472 break;
473 case TYP:
474 cerr << "typ";
475 break;
476 case MAX:
477 cerr << "max";
478 break;
481 cerr << " expression." << endl;
482 min_typ_max_warn -= 1;
485 return res;
488 static void process_udp_table(PUdp*udp, list<string>*table,
489 const char*file, unsigned lineno)
491 const bool synchronous_flag = udp->sequential;
493 /* Interpret and check the table entry strings, to make sure
494 they correspond to the inputs, output and output type. Make
495 up vectors for the fully interpreted result that can be
496 placed in the PUdp object.
498 The table strings are made up by the parser to be two or
499 three substrings seperated by ';', i.e.:
501 0101:1:1 (synchronous device entry)
502 0101:0 (combinational device entry)
504 The parser doesn't check that we got the right kind here,
505 so this loop must watch out. */
506 svector<string> input (table->size());
507 svector<char> current (table->size());
508 svector<char> output (table->size());
509 { unsigned idx = 0;
510 for (list<string>::iterator cur = table->begin()
511 ; cur != table->end()
512 ; cur ++, idx += 1) {
513 string tmp = *cur;
515 /* Pull the input values from the string. */
516 assert(tmp.find(':') == (udp->ports.count() - 1));
517 input[idx] = tmp.substr(0, udp->ports.count()-1);
518 tmp = tmp.substr(udp->ports.count()-1);
520 assert(tmp[0] == ':');
522 /* If this is a synchronous device, get the current
523 output string. */
524 if (synchronous_flag) {
525 if (tmp.size() != 4) {
526 cerr << file<<":"<<lineno << ": error: "
527 << "Invalid table format for"
528 << " sequential primitive." << endl;
529 error_count += 1;
530 break;
532 assert(tmp.size() == 4);
533 current[idx] = tmp[1];
534 tmp = tmp.substr(2);
536 } else if (tmp.size() != 2) {
537 cerr << file<<":"<<lineno << ": error: "
538 << "Invalid table format for"
539 << " combinational primitive." << endl;
540 error_count += 1;
541 break;
544 /* Finally, extract the desired output. */
545 assert(tmp.size() == 2);
546 output[idx] = tmp[1];
550 udp->tinput = input;
551 udp->tcurrent = current;
552 udp->toutput = output;
555 void pform_make_udp(perm_string name, list<string>*parms,
556 svector<PWire*>*decl, list<string>*table,
557 Statement*init_expr,
558 const char*file, unsigned lineno)
560 unsigned local_errors = 0;
561 assert(parms->size() > 0);
563 /* Put the declarations into a map, so that I can check them
564 off with the parameters in the list. If the port is already
565 in the map, merge the port type. I will rebuild a list
566 of parameters for the PUdp object. */
567 map<string,PWire*> defs;
568 for (unsigned idx = 0 ; idx < decl->count() ; idx += 1) {
570 pform_name_t pname = (*decl)[idx]->path();
571 string port_name = peek_tail_name(pname).str();
573 if (PWire*cur = defs[port_name]) {
574 bool rc = true;
575 assert((*decl)[idx]);
576 if ((*decl)[idx]->get_port_type() != NetNet::PIMPLICIT) {
577 rc = cur->set_port_type((*decl)[idx]->get_port_type());
578 assert(rc);
580 if ((*decl)[idx]->get_wire_type() != NetNet::IMPLICIT) {
581 rc = cur->set_wire_type((*decl)[idx]->get_wire_type());
582 assert(rc);
585 } else {
586 defs[port_name] = (*decl)[idx];
591 /* Put the parameters into a vector of wire descriptions. Look
592 in the map for the definitions of the name. In this loop,
593 the parms list in the list of ports in the port list of the
594 UDP declaration, and the defs map maps that name to a
595 PWire* created by an input or output declaration. */
596 svector<PWire*> pins (parms->size());
597 svector<string> pin_names (parms->size());
598 { list<string>::iterator cur;
599 unsigned idx;
600 for (cur = parms->begin(), idx = 0
601 ; cur != parms->end()
602 ; idx++, cur++) {
603 pins[idx] = defs[*cur];
604 pin_names[idx] = *cur;
608 /* Check that the output is an output and the inputs are
609 inputs. I can also make sure that only the single output is
610 declared a register, if anything. The possible errors are:
612 -- an input port (not the first) is missing an input
613 declaration.
615 -- An input port is declared output.
618 assert(pins.count() > 0);
619 do {
620 if (pins[0] == 0) {
621 cerr << file<<":"<<lineno << ": error: "
622 << "Output port of primitive " << name
623 << " missing output declaration." << endl;
624 cerr << file<<":"<<lineno << ": : "
625 << "Try: output " << pin_names[0] << ";"
626 << endl;
627 error_count += 1;
628 local_errors += 1;
629 break;
631 if (pins[0]->get_port_type() != NetNet::POUTPUT) {
632 cerr << file<<":"<<lineno << ": error: "
633 << "The first port of a primitive"
634 << " must be an output." << endl;
635 cerr << file<<":"<<lineno << ": : "
636 << "Try: output " << pin_names[0] << ";"
637 << endl;
638 error_count += 1;
639 local_errors += 1;
640 break;;
642 } while (0);
644 for (unsigned idx = 1 ; idx < pins.count() ; idx += 1) {
645 if (pins[idx] == 0) {
646 cerr << file<<":"<<lineno << ": error: "
647 << "Port " << (idx+1)
648 << " of primitive " << name << " missing"
649 << " input declaration." << endl;
650 cerr << file<<":"<<lineno << ": : "
651 << "Try: input " << pin_names[idx] << ";"
652 << endl;
653 error_count += 1;
654 local_errors += 1;
655 continue;
657 if (pins[idx]->get_port_type() != NetNet::PINPUT) {
658 cerr << file<<":"<<lineno << ": error: "
659 << "Input port " << (idx+1)
660 << " of primitive " << name
661 << " has an output (or missing) declaration." << endl;
662 cerr << file<<":"<<lineno << ": : "
663 << "Note that only the first port can be an output."
664 << endl;
665 cerr << file<<":"<<lineno << ": : "
666 << "Try \"input " << name << ";\""
667 << endl;
668 error_count += 1;
669 local_errors += 1;
670 continue;
673 if (pins[idx]->get_wire_type() == NetNet::REG) {
674 cerr << file<<":"<<lineno << ": error: "
675 << "Port " << (idx+1)
676 << " of primitive " << name << " is an input port"
677 << " with a reg declaration." << endl;
678 cerr << file<<":"<<lineno << ": : "
679 << "primitive inputs cannot be reg."
680 << endl;
681 error_count += 1;
682 local_errors += 1;
683 continue;
687 if (local_errors > 0) {
688 delete parms;
689 delete decl;
690 delete table;
691 delete init_expr;
692 return;
696 /* Verify the "initial" statement, if present, to be sure that
697 it only assigns to the output and the output is
698 registered. Then save the initial value that I get. */
699 verinum::V init = verinum::Vx;
700 if (init_expr) {
701 // XXXX
702 assert(pins[0]->get_wire_type() == NetNet::REG);
704 PAssign*pa = dynamic_cast<PAssign*>(init_expr);
705 assert(pa);
707 const PEIdent*id = dynamic_cast<const PEIdent*>(pa->lval());
708 assert(id);
710 // XXXX
711 //assert(id->name() == pins[0]->name());
713 const PENumber*np = dynamic_cast<const PENumber*>(pa->rval());
714 assert(np);
716 init = np->value()[0];
719 // Put the primitive into the primitives table
720 if (pform_primitives[name]) {
721 VLerror("UDP primitive already exists.");
723 } else {
724 PUdp*udp = new PUdp(name, parms->size());
726 // Detect sequential udp.
727 if (pins[0]->get_wire_type() == NetNet::REG)
728 udp->sequential = true;
730 // Make the port list for the UDP
731 for (unsigned idx = 0 ; idx < pins.count() ; idx += 1)
732 udp->ports[idx] = peek_tail_name(pins[idx]->path());
734 process_udp_table(udp, table, file, lineno);
735 udp->initial = init;
737 pform_primitives[name] = udp;
741 /* Delete the excess tables and lists from the parser. */
742 delete parms;
743 delete decl;
744 delete table;
745 delete init_expr;
748 void pform_make_udp(perm_string name, bool synchronous_flag,
749 perm_string out_name, PExpr*init_expr,
750 list<perm_string>*parms, list<string>*table,
751 const char*file, unsigned lineno)
754 svector<PWire*> pins(parms->size() + 1);
756 /* Make the PWire for the output port. */
757 pins[0] = new PWire(hier_name(out_name),
758 synchronous_flag? NetNet::REG : NetNet::WIRE,
759 NetNet::POUTPUT,
760 IVL_VT_LOGIC);
761 pins[0]->set_file(file);
762 pins[0]->set_lineno(lineno);
764 /* Make the PWire objects for the input ports. */
765 { list<perm_string>::iterator cur;
766 unsigned idx;
767 for (cur = parms->begin(), idx = 1
768 ; cur != parms->end()
769 ; idx += 1, cur++) {
770 assert(idx < pins.count());
771 pins[idx] = new PWire(hier_name(*cur),
772 NetNet::WIRE,
773 NetNet::PINPUT,
774 IVL_VT_LOGIC);
775 pins[idx]->set_file(file);
776 pins[idx]->set_lineno(lineno);
778 assert(idx == pins.count());
781 /* Verify the initial expression, if present, to be sure that
782 it only assigns to the output and the output is
783 registered. Then save the initial value that I get. */
784 verinum::V init = verinum::Vx;
785 if (init_expr) {
786 // XXXX
787 assert(pins[0]->get_wire_type() == NetNet::REG);
789 PAssign*pa = dynamic_cast<PAssign*>(init_expr);
790 assert(pa);
792 const PEIdent*id = dynamic_cast<const PEIdent*>(pa->lval());
793 assert(id);
795 // XXXX
796 //assert(id->name() == pins[0]->name());
798 const PENumber*np = dynamic_cast<const PENumber*>(pa->rval());
799 assert(np);
801 init = np->value()[0];
804 // Put the primitive into the primitives table
805 if (pform_primitives[name]) {
806 VLerror("UDP primitive already exists.");
808 } else {
809 PUdp*udp = new PUdp(name, pins.count());
811 // Detect sequential udp.
812 udp->sequential = synchronous_flag;
814 // Make the port list for the UDP
815 for (unsigned idx = 0 ; idx < pins.count() ; idx += 1)
816 udp->ports[idx] = peek_tail_name(pins[idx]->path());
818 assert(udp);
819 assert(table);
820 process_udp_table(udp, table, file, lineno);
821 udp->initial = init;
823 pform_primitives[name] = udp;
826 delete parms;
827 delete table;
828 delete init_expr;
832 * This function is used to set the net range for a port that uses
833 * the new (1364-2001) list_of_port_declarations, but omitted a
834 * register/wire/etc. that would have triggered it to be set elsewhere.
837 * Since implicitly defined list of port declarations are no longer
838 * considered fully defined we no longer need this routine to force
839 * them to be fully defined.
841 void pform_set_net_range(const char* name)
843 PWire*cur = get_wire_in_module(hier_name(name));
844 if (cur == 0) {
845 VLerror("error: name is not a valid net.");
846 return;
848 cur->set_net_range();
853 * This function attaches a range to a given name. The function is
854 * only called by the parser within the scope of the net declaration,
855 * and the name that I receive only has the tail component.
857 static void pform_set_net_range(const char* name,
858 const svector<PExpr*>*range,
859 bool signed_flag,
860 ivl_variable_type_t dt,
861 PWSRType rt)
863 PWire*cur = get_wire_in_module(hier_name(name));
864 if (cur == 0) {
865 VLerror("error: name is not a valid net.");
866 return;
869 if (range == 0) {
870 /* This is the special case that we really mean a
871 scalar. Set a fake range. */
872 cur->set_range(0, 0, rt);
874 } else {
875 assert(range->count() == 2);
876 assert((*range)[0]);
877 assert((*range)[1]);
878 cur->set_range((*range)[0], (*range)[1], rt);
880 cur->set_signed(signed_flag);
882 if (dt != IVL_VT_NO_TYPE)
883 cur->set_data_type(dt);
886 void pform_set_net_range(list<perm_string>*names,
887 svector<PExpr*>*range,
888 bool signed_flag,
889 ivl_variable_type_t dt,
890 PWSRType rt)
892 assert((range == 0) || (range->count() == 2));
894 for (list<perm_string>::iterator cur = names->begin()
895 ; cur != names->end()
896 ; cur ++ ) {
897 perm_string txt = *cur;
898 pform_set_net_range(txt, range, signed_flag, dt, rt);
901 delete names;
902 if (range)
903 delete range;
907 * This is invoked to make a named event. This is the declaration of
908 * the event, and not necessarily the use of it.
910 static void pform_make_event(perm_string name, const char*fn, unsigned ln)
912 PEvent*event = new PEvent(name);
913 event->set_file(fn);
914 event->set_lineno(ln);
915 pform_cur_module->events[name] = event;
918 void pform_make_events(list<perm_string>*names, const char*fn, unsigned ln)
920 list<perm_string>::iterator cur;
921 for (cur = names->begin() ; cur != names->end() ; cur++) {
922 perm_string txt = *cur;
923 pform_make_event(txt, fn, ln);
926 delete names;
930 * pform_makegates is called when a list of gates (with the same type)
931 * are ready to be instantiated. The function runs through the list of
932 * gates and calls the pform_makegate function to make the individual gate.
934 void pform_makegate(PGBuiltin::Type type,
935 struct str_pair_t str,
936 svector<PExpr*>* delay,
937 const lgate&info,
938 svector<named_pexpr_t*>*attr)
940 if (info.parms_by_name) {
941 cerr << info.file << ":" << info.lineno << ": Gates do not "
942 "have port names." << endl;
943 error_count += 1;
944 return;
947 perm_string dev_name = lex_strings.make(info.name);
948 PGBuiltin*cur = new PGBuiltin(type, dev_name, info.parms, delay);
949 if (info.range[0])
950 cur->set_range(info.range[0], info.range[1]);
952 if (attr) {
953 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
954 named_pexpr_t*tmp = (*attr)[idx];
955 cur->attributes[tmp->name] = tmp->parm;
959 cur->strength0(str.str0);
960 cur->strength1(str.str1);
961 cur->set_file(info.file);
962 cur->set_lineno(info.lineno);
964 if (pform_cur_generate)
965 pform_cur_generate->add_gate(cur);
966 else
967 pform_cur_module->add_gate(cur);
970 void pform_makegates(PGBuiltin::Type type,
971 struct str_pair_t str,
972 svector<PExpr*>*delay,
973 svector<lgate>*gates,
974 svector<named_pexpr_t*>*attr)
976 for (unsigned idx = 0 ; idx < gates->count() ; idx += 1) {
977 pform_makegate(type, str, delay, (*gates)[idx], attr);
980 if (attr) {
981 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
982 named_pexpr_t*cur = (*attr)[idx];
983 delete cur;
985 delete attr;
988 delete gates;
992 * A module is different from a gate in that there are different
993 * constraints, and sometimes different syntax. The X_modgate
994 * functions handle the instantiations of modules (and UDP objects) by
995 * making PGModule objects.
997 * The first pform_make_modgate handles the case of a module
998 * instantiated with ports passed by position. The "wires" is an
999 * ordered array of port expressions.
1001 * The second pform_make_modgate handles the case of a module
1002 * instantiated with ports passed by name. The "bind" argument is the
1003 * ports matched with names.
1005 static void pform_make_modgate(perm_string type,
1006 perm_string name,
1007 struct parmvalue_t*overrides,
1008 svector<PExpr*>*wires,
1009 PExpr*msb, PExpr*lsb,
1010 const char*fn, unsigned ln)
1012 PGModule*cur = new PGModule(type, name, wires);
1013 cur->set_file(fn);
1014 cur->set_lineno(ln);
1015 cur->set_range(msb,lsb);
1017 if (overrides && overrides->by_name) {
1018 unsigned cnt = overrides->by_name->count();
1019 named<PExpr*>*byname = new named<PExpr*>[cnt];
1021 for (unsigned idx = 0 ; idx < cnt ; idx += 1) {
1022 named_pexpr_t*curp = (*overrides->by_name)[idx];
1023 byname[idx].name = curp->name;
1024 byname[idx].parm = curp->parm;
1027 cur->set_parameters(byname, cnt);
1029 } else if (overrides && overrides->by_order) {
1030 cur->set_parameters(overrides->by_order);
1033 if (pform_cur_generate)
1034 pform_cur_generate->add_gate(cur);
1035 else
1036 pform_cur_module->add_gate(cur);
1039 static void pform_make_modgate(perm_string type,
1040 perm_string name,
1041 struct parmvalue_t*overrides,
1042 svector<named_pexpr_t*>*bind,
1043 PExpr*msb, PExpr*lsb,
1044 const char*fn, unsigned ln)
1046 unsigned npins = bind->count();
1047 named<PExpr*>*pins = new named<PExpr*>[npins];
1048 for (unsigned idx = 0 ; idx < npins ; idx += 1) {
1049 named_pexpr_t*curp = (*bind)[idx];
1050 pins[idx].name = curp->name;
1051 pins[idx].parm = curp->parm;
1054 PGModule*cur = new PGModule(type, name, pins, npins);
1055 cur->set_file(fn);
1056 cur->set_lineno(ln);
1057 cur->set_range(msb,lsb);
1059 if (overrides && overrides->by_name) {
1060 unsigned cnt = overrides->by_name->count();
1061 named<PExpr*>*byname = new named<PExpr*>[cnt];
1063 for (unsigned idx = 0 ; idx < cnt ; idx += 1) {
1064 named_pexpr_t*curp = (*overrides->by_name)[idx];
1065 byname[idx].name = curp->name;
1066 byname[idx].parm = curp->parm;
1069 cur->set_parameters(byname, cnt);
1071 } else if (overrides && overrides->by_order) {
1073 cur->set_parameters(overrides->by_order);
1077 if (pform_cur_generate)
1078 pform_cur_generate->add_gate(cur);
1079 else
1080 pform_cur_module->add_gate(cur);
1083 void pform_make_modgates(perm_string type,
1084 struct parmvalue_t*overrides,
1085 svector<lgate>*gates)
1088 for (unsigned idx = 0 ; idx < gates->count() ; idx += 1) {
1089 lgate cur = (*gates)[idx];
1090 perm_string cur_name = lex_strings.make(cur.name);
1092 if (cur.parms_by_name) {
1093 pform_make_modgate(type, cur_name, overrides,
1094 cur.parms_by_name,
1095 cur.range[0], cur.range[1],
1096 cur.file, cur.lineno);
1098 } else if (cur.parms) {
1100 /* If there are no parameters, the parser will be
1101 tricked into thinking it is one empty
1102 parameter. This fixes that. */
1103 if ((cur.parms->count() == 1) && (cur.parms[0][0] == 0)) {
1104 delete cur.parms;
1105 cur.parms = new svector<PExpr*>(0);
1107 pform_make_modgate(type, cur_name, overrides,
1108 cur.parms,
1109 cur.range[0], cur.range[1],
1110 cur.file, cur.lineno);
1112 } else {
1113 svector<PExpr*>*wires = new svector<PExpr*>(0);
1114 pform_make_modgate(type, cur_name, overrides,
1115 wires,
1116 cur.range[0], cur.range[1],
1117 cur.file, cur.lineno);
1121 delete gates;
1124 static PGAssign* pform_make_pgassign(PExpr*lval, PExpr*rval,
1125 svector<PExpr*>*del,
1126 struct str_pair_t str)
1128 svector<PExpr*>*wires = new svector<PExpr*>(2);
1129 (*wires)[0] = lval;
1130 (*wires)[1] = rval;
1132 PGAssign*cur;
1134 if (del == 0)
1135 cur = new PGAssign(wires);
1136 else
1137 cur = new PGAssign(wires, del);
1139 cur->strength0(str.str0);
1140 cur->strength1(str.str1);
1142 if (pform_cur_generate)
1143 pform_cur_generate->add_gate(cur);
1144 else
1145 pform_cur_module->add_gate(cur);
1147 return cur;
1150 void pform_make_pgassign_list(svector<PExpr*>*alist,
1151 svector<PExpr*>*del,
1152 struct str_pair_t str,
1153 const char* fn,
1154 unsigned lineno)
1156 PGAssign*tmp;
1157 for (unsigned idx = 0 ; idx < alist->count()/2 ; idx += 1) {
1158 tmp = pform_make_pgassign((*alist)[2*idx],
1159 (*alist)[2*idx+1],
1160 del, str);
1161 tmp->set_file(fn);
1162 tmp->set_lineno(lineno);
1167 * this function makes the initial assignment to a register as given
1168 * in the source. It handles the case where a reg variable is assigned
1169 * where it it declared:
1171 * reg foo = <expr>;
1173 * This is equivalent to the combination of statements:
1175 * reg foo;
1176 * initial foo = <expr>;
1178 * and that is how it is parsed. This syntax is not part of the
1179 * IEEE1364-1995 standard, but is approved by OVI as enhancement
1180 * BTF-B14.
1182 void pform_make_reginit(const struct vlltype&li,
1183 const char*name, PExpr*expr)
1185 const pform_name_t sname = hier_name(name);
1186 PWire*cur = pform_cur_module->get_wire(sname);
1187 if (cur == 0) {
1188 VLerror(li, "internal error: reginit to non-register?");
1189 delete expr;
1190 return;
1193 PEIdent*lval = new PEIdent(sname);
1194 lval->set_file(li.text);
1195 lval->set_lineno(li.first_line);
1196 PAssign*ass = new PAssign(lval, expr);
1197 ass->set_file(li.text);
1198 ass->set_lineno(li.first_line);
1199 PProcess*top = new PProcess(PProcess::PR_INITIAL, ass);
1200 top->set_file(li.text);
1201 top->set_lineno(li.first_line);
1203 pform_cur_module->add_behavior(top);
1207 * This function is used by the parser when I have port definition of
1208 * the form like this:
1210 * input wire signed [7:0] nm;
1212 * The port_type, type, signed_flag and range are known all at once,
1213 * so we can create the PWire object all at once instead of piecemeal
1214 * as is done for the old method.
1216 void pform_module_define_port(const struct vlltype&li,
1217 const char*nm,
1218 NetNet::PortType port_type,
1219 NetNet::Type type,
1220 bool signed_flag,
1221 svector<PExpr*>*range,
1222 svector<named_pexpr_t*>*attr)
1224 pform_name_t name = hier_name(nm);
1225 PWire*cur = pform_cur_module->get_wire(name);
1226 if (cur) {
1227 ostringstream msg;
1228 msg << nm << " definition conflicts with "
1229 << "definition at " << cur->get_line()
1230 << ".";
1231 VLerror(msg.str().c_str());
1232 return;
1236 cur = new PWire(name, type, port_type, IVL_VT_LOGIC);
1237 cur->set_file(li.text);
1238 cur->set_lineno(li.first_line);
1240 cur->set_signed(signed_flag);
1242 if (range == 0) {
1243 cur->set_range(0, 0, (type == NetNet::IMPLICIT) ? SR_PORT :
1244 SR_BOTH);
1246 } else {
1247 assert(range->count() == 2);
1248 assert((*range)[0]);
1249 assert((*range)[1]);
1250 cur->set_range((*range)[0], (*range)[1],
1251 (type == NetNet::IMPLICIT) ? SR_PORT : SR_BOTH);
1254 if (attr) {
1255 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1256 named_pexpr_t*tmp = (*attr)[idx];
1257 cur->attributes[tmp->name] = tmp->parm;
1260 pform_cur_module->add_wire(cur);
1264 * This function makes a single signal (a wire, a reg, etc) as
1265 * requested by the parser. The name is unscoped, so I attach the
1266 * current scope to it (with the scoped_name function) and I try to
1267 * resolve it with an existing PWire in the scope.
1269 * The wire might already exist because of an implicit declaration in
1270 * a module port, i.e.:
1272 * module foo (bar...
1274 * reg bar;
1276 * The output (or other port direction indicator) may or may not have
1277 * been seen already, so I do not do any checking with it yet. But I
1278 * do check to see if the name has already been declared, as this
1279 * function is called for every declaration.
1283 * this is the basic form of pform_makwire. This takes a single simple
1284 * name, port type, net type, data type, and attributes, and creates
1285 * the variable/net. Other forms of pform_makewire ultimately call
1286 * this one to create the wire and stash it.
1288 void pform_makewire(const vlltype&li, const char*nm,
1289 NetNet::Type type, NetNet::PortType pt,
1290 ivl_variable_type_t dt,
1291 svector<named_pexpr_t*>*attr)
1293 pform_name_t name = hier_name(nm);
1295 PWire*cur = get_wire_in_module(name);
1297 // If this is not implicit ("implicit" meaning we don't know
1298 // what the type is yet) then set thay type now.
1299 if (cur && type != NetNet::IMPLICIT) {
1300 bool rc = cur->set_wire_type(type);
1301 if (rc == false) {
1302 ostringstream msg;
1303 msg << nm << " definition conflicts with "
1304 << "definition at " << cur->get_line()
1305 << ".";
1306 VLerror(msg.str().c_str());
1307 cerr << "XXXX type=" << type <<", curtype=" << cur->get_wire_type() << endl;
1312 bool new_wire_flag = false;
1313 if (! cur) {
1314 new_wire_flag = true;
1315 cur = new PWire(name, type, pt, dt);
1318 cur->set_file(li.text);
1319 cur->set_lineno(li.first_line);
1321 switch (dt) {
1322 case IVL_VT_REAL:
1323 cur->set_data_type(dt);
1324 cur->set_range(0, 0, SR_NET);
1325 cur->set_signed(true);
1326 break;
1327 default:
1328 break;
1331 if (attr) {
1332 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1333 named_pexpr_t*tmp = (*attr)[idx];
1334 cur->attributes[tmp->name] = tmp->parm;
1338 if (new_wire_flag) {
1339 if (pform_cur_generate)
1340 pform_cur_generate->add_wire(cur);
1341 else
1342 pform_cur_module->add_wire(cur);
1347 * This form takes a list of names and some type information, and
1348 * generates a bunch of variables/nets. We use the basic
1349 * pform_makewire above.
1351 void pform_makewire(const vlltype&li,
1352 svector<PExpr*>*range,
1353 bool signed_flag,
1354 list<perm_string>*names,
1355 NetNet::Type type,
1356 NetNet::PortType pt,
1357 ivl_variable_type_t dt,
1358 svector<named_pexpr_t*>*attr,
1359 PWSRType rt)
1361 for (list<perm_string>::iterator cur = names->begin()
1362 ; cur != names->end()
1363 ; cur ++ ) {
1364 perm_string txt = *cur;
1365 pform_makewire(li, txt, type, pt, dt, attr);
1366 /* This has already been done for real variables. */
1367 if (dt != IVL_VT_REAL) {
1368 pform_set_net_range(txt, range, signed_flag, dt, rt);
1372 delete names;
1373 if (range)
1374 delete range;
1378 * This form makes nets with delays and continuous assignments.
1380 void pform_makewire(const vlltype&li,
1381 svector<PExpr*>*range,
1382 bool signed_flag,
1383 svector<PExpr*>*delay,
1384 str_pair_t str,
1385 net_decl_assign_t*decls,
1386 NetNet::Type type,
1387 ivl_variable_type_t dt)
1389 net_decl_assign_t*first = decls->next;
1390 decls->next = 0;
1392 while (first) {
1393 net_decl_assign_t*next = first->next;
1395 pform_makewire(li, first->name, type, NetNet::NOT_A_PORT, dt, 0);
1396 /* This has already been done for real variables. */
1397 if (dt != IVL_VT_REAL) {
1398 pform_set_net_range(first->name, range, signed_flag, dt,
1399 SR_NET);
1402 perm_string first_name = lex_strings.make(first->name);
1403 pform_name_t name = hier_name(first_name);
1404 PWire*cur = get_wire_in_module(name);
1405 if (cur != 0) {
1406 PEIdent*lval = new PEIdent(first_name);
1407 lval->set_file(li.text);
1408 lval->set_lineno(li.first_line);
1409 PGAssign*ass = pform_make_pgassign(lval, first->expr,
1410 delay, str);
1411 ass->set_file(li.text);
1412 ass->set_lineno(li.first_line);
1415 free(first->name);
1416 delete first;
1417 first = next;
1421 void pform_set_port_type(perm_string nm, NetNet::PortType pt,
1422 const char*file, unsigned lineno)
1424 pform_name_t name = hier_name(nm);
1425 PWire*cur = pform_cur_module->get_wire(name);
1426 if (cur == 0) {
1427 cur = new PWire(name, NetNet::IMPLICIT, NetNet::PIMPLICIT, IVL_VT_LOGIC);
1428 cur->set_file(file);
1429 cur->set_lineno(lineno);
1430 pform_cur_module->add_wire(cur);
1433 switch (cur->get_port_type()) {
1434 case NetNet::PIMPLICIT:
1435 if (! cur->set_port_type(pt))
1436 VLerror("error setting port direction.");
1437 break;
1439 case NetNet::NOT_A_PORT:
1440 cerr << file << ":" << lineno << ": error: "
1441 << "port " << nm << " is not in the port list."
1442 << endl;
1443 error_count += 1;
1444 break;
1446 default:
1447 cerr << file << ":" << lineno << ": error: "
1448 << "port " << nm << " already has a port declaration."
1449 << endl;
1450 error_count += 1;
1451 break;
1457 * This function is called by the parser to create task ports. The
1458 * resulting wire (which should be a register) is put into a list to
1459 * be packed into the task parameter list.
1461 * It is possible that the wire (er, register) was already created,
1462 * but we know that if the name matches it is a part of the current
1463 * task, so in that case I just assign direction to it.
1465 * The following example demonstrates some of the issues:
1467 * task foo;
1468 * input a;
1469 * reg a, b;
1470 * input b;
1471 * [...]
1472 * endtask
1474 * This function is called when the parser matches the "input a" and
1475 * the "input b" statements. For ``a'', this function is called before
1476 * the wire is declared as a register, so I create the foo.a
1477 * wire. For ``b'', I will find that there is already a foo.b and I
1478 * just set the port direction. In either case, the ``reg a, b''
1479 * statement is caught by the block_item non-terminal and processed
1480 * there.
1482 * Ports are implicitly type reg, because it must be possible for the
1483 * port to act as an l-value in a procedural assignment. It is obvious
1484 * for output and inout ports that the type is reg, because the task
1485 * only contains behavior (no structure) to a procedural assignment is
1486 * the *only* way to affect the output. It is less obvious for input
1487 * ports, but in practice an input port receives its value as if by a
1488 * procedural assignment from the calling behavior.
1490 * This function also handles the input ports of function
1491 * definitions. Input ports to function definitions have the same
1492 * constraints as those of tasks, so this works fine. Functions have
1493 * no output or inout ports.
1495 svector<PWire*>*pform_make_task_ports(NetNet::PortType pt,
1496 ivl_variable_type_t vtype,
1497 bool signed_flag,
1498 svector<PExpr*>*range,
1499 list<perm_string>*names,
1500 const char* file,
1501 unsigned lineno)
1503 assert(names);
1504 svector<PWire*>*res = new svector<PWire*>(0);
1505 for (list<perm_string>::iterator cur = names->begin()
1506 ; cur != names->end() ; cur ++ ) {
1508 perm_string txt = *cur;
1509 pform_name_t name = hier_name(txt);
1511 /* Look for a preexisting wire. If it exists, set the
1512 port direction. If not, create it. */
1513 PWire*curw = pform_cur_module->get_wire(name);
1514 if (curw) {
1515 curw->set_port_type(pt);
1516 } else {
1517 curw = new PWire(name, NetNet::IMPLICIT_REG, pt, vtype);
1518 curw->set_file(file);
1519 curw->set_lineno(lineno);
1520 pform_cur_module->add_wire(curw);
1523 curw->set_signed(signed_flag);
1525 /* If there is a range involved, it needs to be set. */
1526 if (range)
1527 curw->set_range((*range)[0], (*range)[1], SR_PORT);
1529 svector<PWire*>*tmp = new svector<PWire*>(*res, curw);
1531 delete res;
1532 res = tmp;
1535 if (range)
1536 delete range;
1537 delete names;
1538 return res;
1541 void pform_set_task(perm_string name, PTask*task)
1543 pform_cur_module->add_task(name, task);
1547 void pform_set_function(perm_string name, PFunction*func)
1549 pform_cur_module->add_function(name, func);
1552 void pform_set_attrib(perm_string name, perm_string key, char*value)
1554 pform_name_t path = hier_name(name);
1556 if (PWire*cur = pform_cur_module->get_wire(path)) {
1557 cur->attributes[key] = new PEString(value);
1559 } else if (PGate*cur = pform_cur_module->get_gate(name)) {
1560 cur->attributes[key] = new PEString(value);
1562 } else {
1563 free(value);
1564 VLerror("Unable to match name for setting attribute.");
1570 * Set the attribute of a TYPE. This is different from an object in
1571 * that this applies to every instantiation of the given type.
1573 void pform_set_type_attrib(perm_string name, const string&key,
1574 char*value)
1576 map<perm_string,PUdp*>::const_iterator udp = pform_primitives.find(name);
1577 if (udp == pform_primitives.end()) {
1578 VLerror("type name is not (yet) defined.");
1579 free(value);
1580 return;
1583 (*udp).second ->attributes[key] = new PEString(value);
1587 * This function attaches a memory index range to an existing
1588 * register. (The named wire must be a register.
1590 void pform_set_reg_idx(const char*name, PExpr*l, PExpr*r)
1592 PWire*cur = pform_cur_module->get_wire(hier_name(name));
1593 if (cur == 0) {
1594 VLerror("internal error: name is not a valid memory for index.");
1595 return;
1598 cur->set_memory_idx(l, r);
1601 void pform_set_parameter(perm_string name, bool signed_flag,
1602 svector<PExpr*>*range, PExpr*expr)
1604 assert(expr);
1605 pform_cur_module->parameters[name].expr = expr;
1607 if (range) {
1608 assert(range->count() == 2);
1609 assert((*range)[0]);
1610 assert((*range)[1]);
1611 pform_cur_module->parameters[name].msb = (*range)[0];
1612 pform_cur_module->parameters[name].lsb = (*range)[1];
1613 } else {
1614 pform_cur_module->parameters[name].msb = 0;
1615 pform_cur_module->parameters[name].lsb = 0;
1617 pform_cur_module->parameters[name].signed_flag = signed_flag;
1619 pform_cur_module->param_names.push_back(name);
1622 void pform_set_localparam(perm_string name, bool signed_flag,
1623 svector<PExpr*>*range, PExpr*expr)
1625 assert(expr);
1626 pform_cur_module->localparams[name].expr = expr;
1628 if (range) {
1629 assert(range->count() == 2);
1630 assert((*range)[0]);
1631 assert((*range)[1]);
1632 pform_cur_module->localparams[name].msb = (*range)[0];
1633 pform_cur_module->localparams[name].lsb = (*range)[1];
1634 } else {
1635 pform_cur_module->localparams[name].msb = 0;
1636 pform_cur_module->localparams[name].lsb = 0;
1638 pform_cur_module->localparams[name].signed_flag = signed_flag;
1641 void pform_set_specparam(perm_string name, PExpr*expr)
1643 assert(expr);
1644 pform_cur_module->specparams[name] = expr;
1647 void pform_set_defparam(const pform_name_t&name, PExpr*expr)
1649 assert(expr);
1650 pform_cur_module->defparms[name] = expr;
1654 * Specify paths.
1656 extern PSpecPath* pform_make_specify_path(const struct vlltype&li,
1657 list<perm_string>*src, char pol,
1658 bool full_flag, list<perm_string>*dst)
1660 PSpecPath*path = new PSpecPath(src->size(), dst->size());
1661 path->set_file(li.text);
1662 path->set_lineno(li.first_line);
1664 unsigned idx;
1665 list<perm_string>::const_iterator cur;
1667 idx = 0;
1668 for (idx = 0, cur = src->begin() ; cur != src->end() ; idx++, cur++) {
1669 path->src[idx] = *cur;
1671 assert(idx == path->src.size());
1672 delete src;
1674 for (idx = 0, cur = dst->begin() ; cur != dst->end() ; idx++, cur++) {
1675 path->dst[idx] = *cur;
1677 assert(idx == path->dst.size());
1678 delete dst;
1680 return path;
1683 extern PSpecPath*pform_make_specify_edge_path(const struct vlltype&li,
1684 int edge_flag, /*posedge==true */
1685 list<perm_string>*src, char pol,
1686 bool full_flag, list<perm_string>*dst,
1687 PExpr*data_source_expression)
1689 PSpecPath*tmp = pform_make_specify_path(li, src, pol, full_flag, dst);
1690 tmp->edge = edge_flag;
1691 tmp->data_source_expression = data_source_expression;
1692 return tmp;
1695 extern PSpecPath* pform_assign_path_delay(PSpecPath*path, svector<PExpr*>*del)
1697 if (path == 0)
1698 return 0;
1700 assert(path->delays.size() == 0);
1702 path->delays.resize(del->count());
1703 for (unsigned idx = 0 ; idx < path->delays.size() ; idx += 1)
1704 path->delays[idx] = (*del)[idx];
1706 delete del;
1708 return path;
1712 extern void pform_module_specify_path(PSpecPath*obj)
1714 if (obj == 0)
1715 return;
1716 pform_cur_module->specify_paths.push_back(obj);
1719 void pform_set_port_type(const struct vlltype&li,
1720 list<perm_string>*names,
1721 svector<PExpr*>*range,
1722 bool signed_flag,
1723 NetNet::PortType pt)
1725 for (list<perm_string>::iterator cur = names->begin()
1726 ; cur != names->end()
1727 ; cur ++ ) {
1728 perm_string txt = *cur;
1729 pform_set_port_type(txt, pt, li.text, li.first_line);
1730 pform_set_net_range(txt, range, signed_flag, IVL_VT_NO_TYPE,
1731 SR_PORT);
1734 delete names;
1735 if (range)
1736 delete range;
1739 static void pform_set_reg_integer(const char*nm)
1741 pform_name_t name = hier_name(nm);
1742 PWire*cur = pform_cur_module->get_wire(name);
1743 if (cur == 0) {
1744 cur = new PWire(name, NetNet::INTEGER,
1745 NetNet::NOT_A_PORT,
1746 IVL_VT_LOGIC);
1747 cur->set_signed(true);
1748 pform_cur_module->add_wire(cur);
1749 } else {
1750 bool rc = cur->set_wire_type(NetNet::INTEGER);
1751 assert(rc);
1752 cur->set_data_type(IVL_VT_LOGIC);
1753 cur->set_signed(true);
1755 assert(cur);
1757 cur->set_range(new PENumber(new verinum(integer_width-1, integer_width)),
1758 new PENumber(new verinum((uint64_t)0, integer_width)),
1759 SR_NET);
1760 cur->set_signed(true);
1763 void pform_set_reg_integer(list<perm_string>*names)
1765 for (list<perm_string>::iterator cur = names->begin()
1766 ; cur != names->end()
1767 ; cur ++ ) {
1768 perm_string txt = *cur;
1769 pform_set_reg_integer(txt);
1771 delete names;
1774 static void pform_set_reg_time(const char*nm)
1776 pform_name_t name = hier_name(nm);
1777 PWire*cur = pform_cur_module->get_wire(name);
1778 if (cur == 0) {
1779 cur = new PWire(name, NetNet::REG, NetNet::NOT_A_PORT, IVL_VT_LOGIC);
1780 pform_cur_module->add_wire(cur);
1781 } else {
1782 bool rc = cur->set_wire_type(NetNet::REG);
1783 assert(rc);
1784 rc = cur->set_data_type(IVL_VT_LOGIC);
1785 assert(rc);
1787 assert(cur);
1789 cur->set_range(new PENumber(new verinum(TIME_WIDTH-1, integer_width)),
1790 new PENumber(new verinum((uint64_t)0, integer_width)),
1791 SR_NET);
1794 void pform_set_reg_time(list<perm_string>*names)
1796 for (list<perm_string>::iterator cur = names->begin()
1797 ; cur != names->end()
1798 ; cur ++ ) {
1799 perm_string txt = *cur;
1800 pform_set_reg_time(txt);
1802 delete names;
1805 svector<PWire*>* pform_make_udp_input_ports(list<perm_string>*names)
1807 svector<PWire*>*out = new svector<PWire*>(names->size());
1809 unsigned idx = 0;
1810 for (list<perm_string>::iterator cur = names->begin()
1811 ; cur != names->end()
1812 ; cur ++ ) {
1813 perm_string txt = *cur;
1814 pform_name_t tmp;
1815 tmp.push_back(name_component_t(txt));
1816 PWire*pp = new PWire(tmp,
1817 NetNet::IMPLICIT,
1818 NetNet::PINPUT,
1819 IVL_VT_LOGIC);
1820 (*out)[idx] = pp;
1821 idx += 1;
1824 delete names;
1825 return out;
1828 PProcess* pform_make_behavior(PProcess::Type type, Statement*st,
1829 svector<named_pexpr_t*>*attr)
1831 PProcess*pp = new PProcess(type, st);
1833 if (attr) {
1834 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1835 named_pexpr_t*tmp = (*attr)[idx];
1836 pp->attributes[tmp->name] = tmp->parm;
1838 delete attr;
1841 if (pform_cur_generate)
1842 pform_cur_generate->add_behavior(pp);
1843 else
1844 pform_cur_module->add_behavior(pp);
1846 return pp;
1850 FILE*vl_input = 0;
1851 extern void reset_lexor();
1853 int pform_parse(const char*path, FILE*file)
1855 vl_file = path;
1856 if (file == 0) {
1858 if (strcmp(path, "-") == 0)
1859 vl_input = stdin;
1860 else
1861 vl_input = fopen(path, "r");
1862 if (vl_input == 0) {
1863 cerr << "Unable to open " <<vl_file << "." << endl;
1864 return 11;
1867 } else {
1868 vl_input = file;
1871 reset_lexor();
1872 error_count = 0;
1873 warn_count = 0;
1874 int rc = VLparse();
1876 if (file == 0)
1877 fclose(vl_input);
1879 if (rc) {
1880 cerr << "I give up." << endl;
1881 error_count += 1;
1884 return error_count;
1887 void pform_error_nested_modules()
1889 assert( pform_cur_module != 0 );
1890 cerr << pform_cur_module->get_line() << ": error: original module "
1891 "(" << pform_cur_module->mod_name() << ") defined here." << endl;