Careful not to lose bits parameter add expression
[iverilog.git] / pform.cc
blob1f14597a06da8b1e63ff25e89204bf10b22738b2
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 attaches a range to a given name. The function is
833 * only called by the parser within the scope of the net declaration,
834 * and the name that I receive only has the tail component.
836 static void pform_set_net_range(const char* name,
837 const svector<PExpr*>*range,
838 bool signed_flag,
839 ivl_variable_type_t dt)
841 PWire*cur = get_wire_in_module(hier_name(name));
842 if (cur == 0) {
843 VLerror("error: name is not a valid net.");
844 return;
847 if (range == 0) {
848 /* This is the special case that we really mean a
849 scalar. Set a fake range. */
850 cur->set_range(0, 0);
852 } else {
853 assert(range->count() == 2);
854 assert((*range)[0]);
855 assert((*range)[1]);
856 cur->set_range((*range)[0], (*range)[1]);
858 cur->set_signed(signed_flag);
860 if (dt != IVL_VT_NO_TYPE)
861 cur->set_data_type(dt);
864 void pform_set_net_range(list<perm_string>*names,
865 svector<PExpr*>*range,
866 bool signed_flag,
867 ivl_variable_type_t dt)
869 assert((range == 0) || (range->count() == 2));
871 for (list<perm_string>::iterator cur = names->begin()
872 ; cur != names->end()
873 ; cur ++ ) {
874 perm_string txt = *cur;
875 pform_set_net_range(txt, range, signed_flag, dt);
878 delete names;
879 if (range)
880 delete range;
884 * This is invoked to make a named event. This is the declaration of
885 * the event, and not necessarily the use of it.
887 static void pform_make_event(perm_string name, const char*fn, unsigned ln)
889 PEvent*event = new PEvent(name);
890 event->set_file(fn);
891 event->set_lineno(ln);
892 pform_cur_module->events[name] = event;
895 void pform_make_events(list<perm_string>*names, const char*fn, unsigned ln)
897 list<perm_string>::iterator cur;
898 for (cur = names->begin() ; cur != names->end() ; cur++) {
899 perm_string txt = *cur;
900 pform_make_event(txt, fn, ln);
903 delete names;
907 * pform_makegates is called when a list of gates (with the same type)
908 * are ready to be instantiated. The function runs through the list of
909 * gates and calls the pform_makegate function to make the individual gate.
911 void pform_makegate(PGBuiltin::Type type,
912 struct str_pair_t str,
913 svector<PExpr*>* delay,
914 const lgate&info,
915 svector<named_pexpr_t*>*attr)
917 if (info.parms_by_name) {
918 cerr << info.file << ":" << info.lineno << ": Gates do not "
919 "have port names." << endl;
920 error_count += 1;
921 return;
924 perm_string dev_name = lex_strings.make(info.name);
925 PGBuiltin*cur = new PGBuiltin(type, dev_name, info.parms, delay);
926 if (info.range[0])
927 cur->set_range(info.range[0], info.range[1]);
929 if (attr) {
930 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
931 named_pexpr_t*tmp = (*attr)[idx];
932 cur->attributes[tmp->name] = tmp->parm;
936 cur->strength0(str.str0);
937 cur->strength1(str.str1);
938 cur->set_file(info.file);
939 cur->set_lineno(info.lineno);
941 if (pform_cur_generate)
942 pform_cur_generate->add_gate(cur);
943 else
944 pform_cur_module->add_gate(cur);
947 void pform_makegates(PGBuiltin::Type type,
948 struct str_pair_t str,
949 svector<PExpr*>*delay,
950 svector<lgate>*gates,
951 svector<named_pexpr_t*>*attr)
953 for (unsigned idx = 0 ; idx < gates->count() ; idx += 1) {
954 pform_makegate(type, str, delay, (*gates)[idx], attr);
957 if (attr) {
958 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
959 named_pexpr_t*cur = (*attr)[idx];
960 delete cur;
962 delete attr;
965 delete gates;
969 * A module is different from a gate in that there are different
970 * constraints, and sometimes different syntax. The X_modgate
971 * functions handle the instantiations of modules (and UDP objects) by
972 * making PGModule objects.
974 * The first pform_make_modgate handles the case of a module
975 * instantiated with ports passed by position. The "wires" is an
976 * ordered array of port expressions.
978 * The second pform_make_modgate handles the case of a module
979 * instantiated with ports passed by name. The "bind" argument is the
980 * ports matched with names.
982 static void pform_make_modgate(perm_string type,
983 perm_string name,
984 struct parmvalue_t*overrides,
985 svector<PExpr*>*wires,
986 PExpr*msb, PExpr*lsb,
987 const char*fn, unsigned ln)
989 PGModule*cur = new PGModule(type, name, wires);
990 cur->set_file(fn);
991 cur->set_lineno(ln);
992 cur->set_range(msb,lsb);
994 if (overrides && overrides->by_name) {
995 unsigned cnt = overrides->by_name->count();
996 named<PExpr*>*byname = new named<PExpr*>[cnt];
998 for (unsigned idx = 0 ; idx < cnt ; idx += 1) {
999 named_pexpr_t*curp = (*overrides->by_name)[idx];
1000 byname[idx].name = curp->name;
1001 byname[idx].parm = curp->parm;
1004 cur->set_parameters(byname, cnt);
1006 } else if (overrides && overrides->by_order) {
1007 cur->set_parameters(overrides->by_order);
1010 if (pform_cur_generate)
1011 pform_cur_generate->add_gate(cur);
1012 else
1013 pform_cur_module->add_gate(cur);
1016 static void pform_make_modgate(perm_string type,
1017 perm_string name,
1018 struct parmvalue_t*overrides,
1019 svector<named_pexpr_t*>*bind,
1020 PExpr*msb, PExpr*lsb,
1021 const char*fn, unsigned ln)
1023 unsigned npins = bind->count();
1024 named<PExpr*>*pins = new named<PExpr*>[npins];
1025 for (unsigned idx = 0 ; idx < npins ; idx += 1) {
1026 named_pexpr_t*curp = (*bind)[idx];
1027 pins[idx].name = curp->name;
1028 pins[idx].parm = curp->parm;
1031 PGModule*cur = new PGModule(type, name, pins, npins);
1032 cur->set_file(fn);
1033 cur->set_lineno(ln);
1034 cur->set_range(msb,lsb);
1036 if (overrides && overrides->by_name) {
1037 unsigned cnt = overrides->by_name->count();
1038 named<PExpr*>*byname = new named<PExpr*>[cnt];
1040 for (unsigned idx = 0 ; idx < cnt ; idx += 1) {
1041 named_pexpr_t*curp = (*overrides->by_name)[idx];
1042 byname[idx].name = curp->name;
1043 byname[idx].parm = curp->parm;
1046 cur->set_parameters(byname, cnt);
1048 } else if (overrides && overrides->by_order) {
1050 cur->set_parameters(overrides->by_order);
1054 if (pform_cur_generate)
1055 pform_cur_generate->add_gate(cur);
1056 else
1057 pform_cur_module->add_gate(cur);
1060 void pform_make_modgates(perm_string type,
1061 struct parmvalue_t*overrides,
1062 svector<lgate>*gates)
1065 for (unsigned idx = 0 ; idx < gates->count() ; idx += 1) {
1066 lgate cur = (*gates)[idx];
1067 perm_string cur_name = lex_strings.make(cur.name);
1069 if (cur.parms_by_name) {
1070 pform_make_modgate(type, cur_name, overrides,
1071 cur.parms_by_name,
1072 cur.range[0], cur.range[1],
1073 cur.file, cur.lineno);
1075 } else if (cur.parms) {
1077 /* If there are no parameters, the parser will be
1078 tricked into thinking it is one empty
1079 parameter. This fixes that. */
1080 if ((cur.parms->count() == 1) && (cur.parms[0][0] == 0)) {
1081 delete cur.parms;
1082 cur.parms = new svector<PExpr*>(0);
1084 pform_make_modgate(type, cur_name, overrides,
1085 cur.parms,
1086 cur.range[0], cur.range[1],
1087 cur.file, cur.lineno);
1089 } else {
1090 svector<PExpr*>*wires = new svector<PExpr*>(0);
1091 pform_make_modgate(type, cur_name, overrides,
1092 wires,
1093 cur.range[0], cur.range[1],
1094 cur.file, cur.lineno);
1098 delete gates;
1101 static PGAssign* pform_make_pgassign(PExpr*lval, PExpr*rval,
1102 svector<PExpr*>*del,
1103 struct str_pair_t str)
1105 svector<PExpr*>*wires = new svector<PExpr*>(2);
1106 (*wires)[0] = lval;
1107 (*wires)[1] = rval;
1109 PGAssign*cur;
1111 if (del == 0)
1112 cur = new PGAssign(wires);
1113 else
1114 cur = new PGAssign(wires, del);
1116 cur->strength0(str.str0);
1117 cur->strength1(str.str1);
1119 if (pform_cur_generate)
1120 pform_cur_generate->add_gate(cur);
1121 else
1122 pform_cur_module->add_gate(cur);
1124 return cur;
1127 void pform_make_pgassign_list(svector<PExpr*>*alist,
1128 svector<PExpr*>*del,
1129 struct str_pair_t str,
1130 const char* fn,
1131 unsigned lineno)
1133 PGAssign*tmp;
1134 for (unsigned idx = 0 ; idx < alist->count()/2 ; idx += 1) {
1135 tmp = pform_make_pgassign((*alist)[2*idx],
1136 (*alist)[2*idx+1],
1137 del, str);
1138 tmp->set_file(fn);
1139 tmp->set_lineno(lineno);
1144 * this function makes the initial assignment to a register as given
1145 * in the source. It handles the case where a reg variable is assigned
1146 * where it it declared:
1148 * reg foo = <expr>;
1150 * This is equivalent to the combination of statements:
1152 * reg foo;
1153 * initial foo = <expr>;
1155 * and that is how it is parsed. This syntax is not part of the
1156 * IEEE1364-1995 standard, but is approved by OVI as enhancement
1157 * BTF-B14.
1159 void pform_make_reginit(const struct vlltype&li,
1160 const char*name, PExpr*expr)
1162 const pform_name_t sname = hier_name(name);
1163 PWire*cur = pform_cur_module->get_wire(sname);
1164 if (cur == 0) {
1165 VLerror(li, "internal error: reginit to non-register?");
1166 delete expr;
1167 return;
1170 PEIdent*lval = new PEIdent(sname);
1171 lval->set_file(li.text);
1172 lval->set_lineno(li.first_line);
1173 PAssign*ass = new PAssign(lval, expr);
1174 ass->set_file(li.text);
1175 ass->set_lineno(li.first_line);
1176 PProcess*top = new PProcess(PProcess::PR_INITIAL, ass);
1177 top->set_file(li.text);
1178 top->set_lineno(li.first_line);
1180 pform_cur_module->add_behavior(top);
1184 * This function is used by the parser when I have port definition of
1185 * the form like this:
1187 * input wire signed [7:0] nm;
1189 * The port_type, type, signed_flag and range are known all at once,
1190 * so we can create the PWire object all at once instead of piecemeal
1191 * as is done for the old method.
1193 void pform_module_define_port(const struct vlltype&li,
1194 const char*nm,
1195 NetNet::PortType port_type,
1196 NetNet::Type type,
1197 bool signed_flag,
1198 svector<PExpr*>*range,
1199 svector<named_pexpr_t*>*attr)
1201 pform_name_t name = hier_name(nm);
1202 PWire*cur = pform_cur_module->get_wire(name);
1203 if (cur) {
1204 ostringstream msg;
1205 msg << nm << " definition conflicts with "
1206 << "definition at " << cur->get_line()
1207 << ".";
1208 VLerror(msg.str().c_str());
1209 return;
1213 cur = new PWire(name, type, port_type, IVL_VT_LOGIC);
1214 cur->set_file(li.text);
1215 cur->set_lineno(li.first_line);
1217 cur->set_signed(signed_flag);
1219 if (range == 0) {
1220 cur->set_range(0, 0);
1222 } else {
1223 assert(range->count() == 2);
1224 assert((*range)[0]);
1225 assert((*range)[1]);
1226 cur->set_range((*range)[0], (*range)[1]);
1229 if (attr) {
1230 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1231 named_pexpr_t*tmp = (*attr)[idx];
1232 cur->attributes[tmp->name] = tmp->parm;
1235 pform_cur_module->add_wire(cur);
1239 * This function makes a single signal (a wire, a reg, etc) as
1240 * requested by the parser. The name is unscoped, so I attach the
1241 * current scope to it (with the scoped_name function) and I try to
1242 * resolve it with an existing PWire in the scope.
1244 * The wire might already exist because of an implicit declaration in
1245 * a module port, i.e.:
1247 * module foo (bar...
1249 * reg bar;
1251 * The output (or other port direction indicator) may or may not have
1252 * been seen already, so I do not do any checking with it yet. But I
1253 * do check to see if the name has already been declared, as this
1254 * function is called for every declaration.
1258 * this is the basic form of pform_makwire. This takes a single simple
1259 * name, port type, net type, data type, and attributes, and creates
1260 * the variable/net. Other forms of pform_makewire ultimately call
1261 * this one to create the wire and stash it.
1263 void pform_makewire(const vlltype&li, const char*nm,
1264 NetNet::Type type, NetNet::PortType pt,
1265 ivl_variable_type_t dt,
1266 svector<named_pexpr_t*>*attr)
1268 pform_name_t name = hier_name(nm);
1270 PWire*cur = get_wire_in_module(name);
1272 // If this is not implicit ("implicit" meaning we don't know
1273 // what the type is yet) then set thay type now.
1274 if (cur && type != NetNet::IMPLICIT) {
1275 bool rc = cur->set_wire_type(type);
1276 if (rc == false) {
1277 ostringstream msg;
1278 msg << nm << " definition conflicts with "
1279 << "definition at " << cur->get_line()
1280 << ".";
1281 VLerror(msg.str().c_str());
1282 cerr << "XXXX type=" << type <<", curtype=" << cur->get_wire_type() << endl;
1286 if (cur) {
1287 cur->set_file(li.text);
1288 cur->set_lineno(li.first_line);
1289 return;
1292 cur = new PWire(name, type, pt, dt);
1293 cur->set_file(li.text);
1294 cur->set_lineno(li.first_line);
1296 if (attr) {
1297 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1298 named_pexpr_t*tmp = (*attr)[idx];
1299 cur->attributes[tmp->name] = tmp->parm;
1303 if (pform_cur_generate)
1304 pform_cur_generate->add_wire(cur);
1305 else
1306 pform_cur_module->add_wire(cur);
1310 * This form takes a list of names and some type information, and
1311 * generates a bunch of variables/nets. We hse the basic
1312 * pform_makewire above.
1314 void pform_makewire(const vlltype&li,
1315 svector<PExpr*>*range,
1316 bool signed_flag,
1317 list<perm_string>*names,
1318 NetNet::Type type,
1319 NetNet::PortType pt,
1320 ivl_variable_type_t dt,
1321 svector<named_pexpr_t*>*attr)
1323 for (list<perm_string>::iterator cur = names->begin()
1324 ; cur != names->end()
1325 ; cur ++ ) {
1326 perm_string txt = *cur;
1327 pform_makewire(li, txt, type, pt, dt, attr);
1328 pform_set_net_range(txt, range, signed_flag, dt);
1331 delete names;
1332 if (range)
1333 delete range;
1337 * This form makes nets with delays and continuous assignments.
1339 void pform_makewire(const vlltype&li,
1340 svector<PExpr*>*range,
1341 bool signed_flag,
1342 svector<PExpr*>*delay,
1343 str_pair_t str,
1344 net_decl_assign_t*decls,
1345 NetNet::Type type,
1346 ivl_variable_type_t dt)
1348 net_decl_assign_t*first = decls->next;
1349 decls->next = 0;
1351 while (first) {
1352 net_decl_assign_t*next = first->next;
1354 pform_makewire(li, first->name, type, NetNet::NOT_A_PORT, dt, 0);
1355 pform_set_net_range(first->name, range, signed_flag, dt);
1357 perm_string first_name = lex_strings.make(first->name);
1358 pform_name_t name = hier_name(first_name);
1359 PWire*cur = get_wire_in_module(name);
1360 if (cur != 0) {
1361 PEIdent*lval = new PEIdent(first_name);
1362 lval->set_file(li.text);
1363 lval->set_lineno(li.first_line);
1364 PGAssign*ass = pform_make_pgassign(lval, first->expr,
1365 delay, str);
1366 ass->set_file(li.text);
1367 ass->set_lineno(li.first_line);
1370 free(first->name);
1371 delete first;
1372 first = next;
1376 void pform_set_port_type(perm_string nm, NetNet::PortType pt,
1377 const char*file, unsigned lineno)
1379 pform_name_t name = hier_name(nm);
1380 PWire*cur = pform_cur_module->get_wire(name);
1381 if (cur == 0) {
1382 cur = new PWire(name, NetNet::IMPLICIT, NetNet::PIMPLICIT, IVL_VT_LOGIC);
1383 cur->set_file(file);
1384 cur->set_lineno(lineno);
1385 pform_cur_module->add_wire(cur);
1388 switch (cur->get_port_type()) {
1389 case NetNet::PIMPLICIT:
1390 if (! cur->set_port_type(pt))
1391 VLerror("error setting port direction.");
1392 break;
1394 case NetNet::NOT_A_PORT:
1395 cerr << file << ":" << lineno << ": error: "
1396 << "port " << nm << " is not in the port list."
1397 << endl;
1398 error_count += 1;
1399 break;
1401 default:
1402 cerr << file << ":" << lineno << ": error: "
1403 << "port " << nm << " already has a port declaration."
1404 << endl;
1405 error_count += 1;
1406 break;
1412 * This function is called by the parser to create task ports. The
1413 * resulting wire (which should be a register) is put into a list to
1414 * be packed into the task parameter list.
1416 * It is possible that the wire (er, register) was already created,
1417 * but we know that if the name matches it is a part of the current
1418 * task, so in that case I just assign direction to it.
1420 * The following example demonstrates some of the issues:
1422 * task foo;
1423 * input a;
1424 * reg a, b;
1425 * input b;
1426 * [...]
1427 * endtask
1429 * This function is called when the parser matches the "input a" and
1430 * the "input b" statements. For ``a'', this function is called before
1431 * the wire is declared as a register, so I create the foo.a
1432 * wire. For ``b'', I will find that there is already a foo.b and I
1433 * just set the port direction. In either case, the ``reg a, b''
1434 * statement is caught by the block_item non-terminal and processed
1435 * there.
1437 * Ports are implicitly type reg, because it must be possible for the
1438 * port to act as an l-value in a procedural assignment. It is obvious
1439 * for output and inout ports that the type is reg, because the task
1440 * only contains behavior (no structure) to a procedural assignment is
1441 * the *only* way to affect the output. It is less obvious for input
1442 * ports, but in practice an input port receives its value as if by a
1443 * procedural assignment from the calling behavior.
1445 * This function also handles the input ports of function
1446 * definitions. Input ports to function definitions have the same
1447 * constraints as those of tasks, so this works fine. Functions have
1448 * no output or inout ports.
1450 svector<PWire*>*pform_make_task_ports(NetNet::PortType pt,
1451 ivl_variable_type_t vtype,
1452 bool signed_flag,
1453 svector<PExpr*>*range,
1454 list<perm_string>*names,
1455 const char* file,
1456 unsigned lineno)
1458 assert(names);
1459 svector<PWire*>*res = new svector<PWire*>(0);
1460 for (list<perm_string>::iterator cur = names->begin()
1461 ; cur != names->end() ; cur ++ ) {
1463 perm_string txt = *cur;
1464 pform_name_t name = hier_name(txt);
1466 /* Look for a preexisting wire. If it exists, set the
1467 port direction. If not, create it. */
1468 PWire*curw = pform_cur_module->get_wire(name);
1469 if (curw) {
1470 curw->set_port_type(pt);
1471 } else {
1472 curw = new PWire(name, NetNet::IMPLICIT_REG, pt, vtype);
1473 curw->set_file(file);
1474 curw->set_lineno(lineno);
1475 pform_cur_module->add_wire(curw);
1478 curw->set_signed(signed_flag);
1480 /* If there is a range involved, it needs to be set. */
1481 if (range)
1482 curw->set_range((*range)[0], (*range)[1]);
1484 svector<PWire*>*tmp = new svector<PWire*>(*res, curw);
1486 delete res;
1487 res = tmp;
1490 if (range)
1491 delete range;
1492 delete names;
1493 return res;
1496 void pform_set_task(perm_string name, PTask*task)
1498 pform_cur_module->add_task(name, task);
1502 void pform_set_function(perm_string name, PFunction*func)
1504 pform_cur_module->add_function(name, func);
1507 void pform_set_attrib(perm_string name, perm_string key, char*value)
1509 pform_name_t path = hier_name(name);
1511 if (PWire*cur = pform_cur_module->get_wire(path)) {
1512 cur->attributes[key] = new PEString(value);
1514 } else if (PGate*cur = pform_cur_module->get_gate(name)) {
1515 cur->attributes[key] = new PEString(value);
1517 } else {
1518 free(value);
1519 VLerror("Unable to match name for setting attribute.");
1525 * Set the attribute of a TYPE. This is different from an object in
1526 * that this applies to every instantiation of the given type.
1528 void pform_set_type_attrib(perm_string name, const string&key,
1529 char*value)
1531 map<perm_string,PUdp*>::const_iterator udp = pform_primitives.find(name);
1532 if (udp == pform_primitives.end()) {
1533 VLerror("type name is not (yet) defined.");
1534 free(value);
1535 return;
1538 (*udp).second ->attributes[key] = new PEString(value);
1542 * This function attaches a memory index range to an existing
1543 * register. (The named wire must be a register.
1545 void pform_set_reg_idx(const char*name, PExpr*l, PExpr*r)
1547 PWire*cur = pform_cur_module->get_wire(hier_name(name));
1548 if (cur == 0) {
1549 VLerror("internal error: name is not a valid memory for index.");
1550 return;
1553 cur->set_memory_idx(l, r);
1556 void pform_set_parameter(perm_string name, bool signed_flag,
1557 svector<PExpr*>*range, PExpr*expr)
1559 assert(expr);
1560 pform_cur_module->parameters[name].expr = expr;
1562 if (range) {
1563 assert(range->count() == 2);
1564 assert((*range)[0]);
1565 assert((*range)[1]);
1566 pform_cur_module->parameters[name].msb = (*range)[0];
1567 pform_cur_module->parameters[name].lsb = (*range)[1];
1568 } else {
1569 pform_cur_module->parameters[name].msb = 0;
1570 pform_cur_module->parameters[name].lsb = 0;
1572 pform_cur_module->parameters[name].signed_flag = signed_flag;
1574 pform_cur_module->param_names.push_back(name);
1577 void pform_set_localparam(perm_string name, bool signed_flag,
1578 svector<PExpr*>*range, PExpr*expr)
1580 assert(expr);
1581 pform_cur_module->localparams[name].expr = expr;
1583 if (range) {
1584 assert(range->count() == 2);
1585 assert((*range)[0]);
1586 assert((*range)[1]);
1587 pform_cur_module->localparams[name].msb = (*range)[0];
1588 pform_cur_module->localparams[name].lsb = (*range)[1];
1589 } else {
1590 pform_cur_module->localparams[name].msb = 0;
1591 pform_cur_module->localparams[name].lsb = 0;
1593 pform_cur_module->localparams[name].signed_flag = signed_flag;
1596 void pform_set_specparam(perm_string name, PExpr*expr)
1598 assert(expr);
1599 pform_cur_module->specparams[name] = expr;
1602 void pform_set_defparam(const pform_name_t&name, PExpr*expr)
1604 assert(expr);
1605 pform_cur_module->defparms[name] = expr;
1609 * Specify paths.
1611 extern PSpecPath* pform_make_specify_path(const struct vlltype&li,
1612 list<perm_string>*src, char pol,
1613 bool full_flag, list<perm_string>*dst)
1615 PSpecPath*path = new PSpecPath(src->size(), dst->size());
1616 path->set_file(li.text);
1617 path->set_lineno(li.first_line);
1619 unsigned idx;
1620 list<perm_string>::const_iterator cur;
1622 idx = 0;
1623 for (idx = 0, cur = src->begin() ; cur != src->end() ; idx++, cur++) {
1624 path->src[idx] = *cur;
1626 assert(idx == path->src.size());
1627 delete src;
1629 for (idx = 0, cur = dst->begin() ; cur != dst->end() ; idx++, cur++) {
1630 path->dst[idx] = *cur;
1632 assert(idx == path->dst.size());
1633 delete dst;
1635 return path;
1638 extern PSpecPath*pform_make_specify_edge_path(const struct vlltype&li,
1639 int edge_flag, /*posedge==true */
1640 list<perm_string>*src, char pol,
1641 bool full_flag, list<perm_string>*dst,
1642 PExpr*data_source_expression)
1644 PSpecPath*tmp = pform_make_specify_path(li, src, pol, full_flag, dst);
1645 tmp->edge = edge_flag;
1646 tmp->data_source_expression = data_source_expression;
1647 return tmp;
1650 extern PSpecPath* pform_assign_path_delay(PSpecPath*path, svector<PExpr*>*del)
1652 if (path == 0)
1653 return 0;
1655 assert(path->delays.size() == 0);
1657 path->delays.resize(del->count());
1658 for (unsigned idx = 0 ; idx < path->delays.size() ; idx += 1)
1659 path->delays[idx] = (*del)[idx];
1661 delete del;
1663 return path;
1667 extern void pform_module_specify_path(PSpecPath*obj)
1669 if (obj == 0)
1670 return;
1671 pform_cur_module->specify_paths.push_back(obj);
1674 void pform_set_port_type(const struct vlltype&li,
1675 list<perm_string>*names,
1676 svector<PExpr*>*range,
1677 bool signed_flag,
1678 NetNet::PortType pt)
1680 for (list<perm_string>::iterator cur = names->begin()
1681 ; cur != names->end()
1682 ; cur ++ ) {
1683 perm_string txt = *cur;
1684 pform_set_port_type(txt, pt, li.text, li.first_line);
1685 if (range)
1686 pform_set_net_range(txt, range, signed_flag, IVL_VT_NO_TYPE);
1689 delete names;
1690 if (range)
1691 delete range;
1694 static void pform_set_reg_integer(const char*nm)
1696 pform_name_t name = hier_name(nm);
1697 PWire*cur = pform_cur_module->get_wire(name);
1698 if (cur == 0) {
1699 cur = new PWire(name, NetNet::INTEGER,
1700 NetNet::NOT_A_PORT,
1701 IVL_VT_LOGIC);
1702 cur->set_signed(true);
1703 pform_cur_module->add_wire(cur);
1704 } else {
1705 bool rc = cur->set_wire_type(NetNet::INTEGER);
1706 assert(rc);
1707 cur->set_data_type(IVL_VT_LOGIC);
1708 cur->set_signed(true);
1710 assert(cur);
1712 cur->set_range(new PENumber(new verinum(integer_width-1, integer_width)),
1713 new PENumber(new verinum((uint64_t)0, integer_width)));
1714 cur->set_signed(true);
1717 void pform_set_reg_integer(list<perm_string>*names)
1719 for (list<perm_string>::iterator cur = names->begin()
1720 ; cur != names->end()
1721 ; cur ++ ) {
1722 perm_string txt = *cur;
1723 pform_set_reg_integer(txt);
1725 delete names;
1728 static void pform_set_reg_time(const char*nm)
1730 pform_name_t name = hier_name(nm);
1731 PWire*cur = pform_cur_module->get_wire(name);
1732 if (cur == 0) {
1733 cur = new PWire(name, NetNet::REG, NetNet::NOT_A_PORT, IVL_VT_LOGIC);
1734 pform_cur_module->add_wire(cur);
1735 } else {
1736 bool rc = cur->set_wire_type(NetNet::REG);
1737 assert(rc);
1738 rc = cur->set_data_type(IVL_VT_LOGIC);
1739 assert(rc);
1741 assert(cur);
1743 cur->set_range(new PENumber(new verinum(TIME_WIDTH-1, integer_width)),
1744 new PENumber(new verinum((uint64_t)0, integer_width)));
1747 void pform_set_reg_time(list<perm_string>*names)
1749 for (list<perm_string>::iterator cur = names->begin()
1750 ; cur != names->end()
1751 ; cur ++ ) {
1752 perm_string txt = *cur;
1753 pform_set_reg_time(txt);
1755 delete names;
1758 svector<PWire*>* pform_make_udp_input_ports(list<perm_string>*names)
1760 svector<PWire*>*out = new svector<PWire*>(names->size());
1762 unsigned idx = 0;
1763 for (list<perm_string>::iterator cur = names->begin()
1764 ; cur != names->end()
1765 ; cur ++ ) {
1766 perm_string txt = *cur;
1767 pform_name_t tmp;
1768 tmp.push_back(name_component_t(txt));
1769 PWire*pp = new PWire(tmp,
1770 NetNet::IMPLICIT,
1771 NetNet::PINPUT,
1772 IVL_VT_LOGIC);
1773 (*out)[idx] = pp;
1774 idx += 1;
1777 delete names;
1778 return out;
1781 PProcess* pform_make_behavior(PProcess::Type type, Statement*st,
1782 svector<named_pexpr_t*>*attr)
1784 PProcess*pp = new PProcess(type, st);
1786 if (attr) {
1787 for (unsigned idx = 0 ; idx < attr->count() ; idx += 1) {
1788 named_pexpr_t*tmp = (*attr)[idx];
1789 pp->attributes[tmp->name] = tmp->parm;
1791 delete attr;
1794 if (pform_cur_generate)
1795 pform_cur_generate->add_behavior(pp);
1796 else
1797 pform_cur_module->add_behavior(pp);
1799 return pp;
1803 FILE*vl_input = 0;
1804 extern void reset_lexor();
1806 int pform_parse(const char*path, FILE*file)
1808 vl_file = path;
1809 if (file == 0) {
1811 if (strcmp(path, "-") == 0)
1812 vl_input = stdin;
1813 else
1814 vl_input = fopen(path, "r");
1815 if (vl_input == 0) {
1816 cerr << "Unable to open " <<vl_file << "." << endl;
1817 return 11;
1820 } else {
1821 vl_input = file;
1824 reset_lexor();
1825 error_count = 0;
1826 warn_count = 0;
1827 int rc = VLparse();
1829 if (file == 0)
1830 fclose(vl_input);
1832 if (rc) {
1833 cerr << "I give up." << endl;
1834 error_count += 1;
1837 return error_count;
1842 * $Log: pform.cc,v $
1843 * Revision 1.148 2007/06/12 04:05:45 steve
1844 * Put instantiated modules in the proper generated scope.
1846 * Revision 1.147 2007/06/02 03:42:13 steve
1847 * Properly evaluate scope path expressions.
1849 * Revision 1.146 2007/05/24 04:07:12 steve
1850 * Rework the heirarchical identifier parse syntax and pform
1851 * to handle more general combinations of heirarch and bit selects.
1853 * Revision 1.145 2007/04/26 03:06:22 steve
1854 * Rework hname_t to use perm_strings.
1856 * Revision 1.144 2007/04/19 02:52:53 steve
1857 * Add support for -v flag in command file.
1859 * Revision 1.143 2007/04/13 02:34:35 steve
1860 * Parse edge sensitive paths without edge specifier.
1862 * Revision 1.142 2007/03/07 04:24:59 steve
1863 * Make integer width controllable.
1865 * Revision 1.141 2007/03/05 05:59:10 steve
1866 * Handle processes within generate loops.
1868 * Revision 1.140 2007/02/12 01:52:21 steve
1869 * Parse all specify paths to pform.
1871 * Revision 1.139 2007/01/27 05:36:11 steve
1872 * Fix padding of x when literal is sized and unsigned.
1874 * Revision 1.138 2007/01/16 05:44:15 steve
1875 * Major rework of array handling. Memories are replaced with the
1876 * more general concept of arrays. The NetMemory and NetEMemory
1877 * classes are removed from the ivl core program, and the IVL_LPM_RAM
1878 * lpm type is removed from the ivl_target API.
1880 * Revision 1.137 2006/09/23 04:57:19 steve
1881 * Basic support for specify timing.
1883 * Revision 1.136 2006/08/08 05:11:37 steve
1884 * Handle 64bit delay constants.
1886 * Revision 1.135 2006/04/10 00:37:42 steve
1887 * Add support for generate loops w/ wires and gates.
1889 * Revision 1.134 2006/03/30 05:22:34 steve
1890 * task/function ports can have types.
1892 * Revision 1.133 2005/07/11 16:56:51 steve
1893 * Remove NetVariable and ivl_variable_t structures.
1895 * Revision 1.132 2005/07/07 16:22:49 steve
1896 * Generalize signals to carry types.
1898 * Revision 1.131 2005/05/06 00:25:13 steve
1899 * Handle synthesis of concatenation expressions.
1901 * Revision 1.130 2004/12/11 02:31:27 steve
1902 * Rework of internals to carry vectors through nexus instead
1903 * of single bits. Make the ivl, tgt-vvp and vvp initial changes
1904 * down this path.
1906 * Revision 1.129 2004/10/04 01:10:55 steve
1907 * Clean up spurious trailing white space.
1909 * Revision 1.128 2004/08/26 04:02:04 steve
1910 * Add support for localparam ranges.
1912 * Revision 1.127 2004/06/13 04:56:55 steve
1913 * Add support for the default_nettype directive.
1915 * Revision 1.126 2004/05/31 23:34:39 steve
1916 * Rewire/generalize parsing an elaboration of
1917 * function return values to allow for better
1918 * speed and more type support.
1920 * Revision 1.125 2004/05/25 19:21:07 steve
1921 * More identifier lists use perm_strings.
1923 * Revision 1.124 2004/03/08 00:10:30 steve
1924 * Verilog2001 new style port declartions for primitives.
1926 * Revision 1.123 2004/02/20 18:53:35 steve
1927 * Addtrbute keys are perm_strings.
1929 * Revision 1.122 2004/02/20 06:22:58 steve
1930 * parameter keys are per_strings.
1932 * Revision 1.121 2004/02/19 06:57:10 steve
1933 * Memory and Event names use perm_string.
1935 * Revision 1.120 2004/02/18 17:11:57 steve
1936 * Use perm_strings for named langiage items.
1938 * Revision 1.119 2004/02/15 17:48:28 steve
1939 * Better error checking of primitive tables.
1941 * Revision 1.118 2003/07/04 03:57:19 steve
1942 * Allow attributes on Verilog 2001 port declarations.
1944 * Revision 1.117 2003/06/24 01:38:03 steve
1945 * Various warnings fixed.
1947 * Revision 1.116 2003/06/20 00:53:19 steve
1948 * Module attributes from the parser
1949 * through to elaborated form.
1951 * Revision 1.115 2003/06/13 19:10:46 steve
1952 * Properly manage real variables in subscopes.
1954 * Revision 1.114 2003/06/13 00:27:09 steve
1955 * Task/functions can have signed ports.
1957 * Revision 1.113 2003/04/28 17:50:57 steve
1958 * More 2001 port declaration support.
1960 * Revision 1.112 2003/04/14 03:39:15 steve
1961 * Break sized constants into a size token
1962 * and a based numeric constant.
1964 * Revision 1.111 2003/03/06 04:37:12 steve
1965 * lex_strings.add module names earlier.
1967 * Revision 1.110 2003/03/01 06:25:30 steve
1968 * Add the lex_strings string handler, and put
1969 * scope names and system task/function names
1970 * into this table. Also, permallocate event
1971 * names from the beginning.
1973 * Revision 1.109 2003/02/27 06:45:11 steve
1974 * specparams as far as pform.
1976 * Revision 1.108 2003/02/02 19:02:39 steve
1977 * Add support for signed ports and nets.
1979 * Revision 1.107 2003/01/26 21:15:59 steve
1980 * Rework expression parsing and elaboration to
1981 * accommodate real/realtime values and expressions.
1983 * Revision 1.106 2003/01/17 05:47:30 steve
1984 * Missed a case of setting line on an PEident.
1986 * Revision 1.105 2003/01/16 21:44:19 steve
1987 * Detect duplicate module declarations.
1989 * Revision 1.104 2003/01/14 21:16:18 steve
1990 * Move strstream to ostringstream for compatibility.
1992 * Revision 1.103 2003/01/10 03:08:13 steve
1993 * Spelling fixes.
1995 * Revision 1.102 2002/09/01 03:01:48 steve
1996 * Properly cast signedness of parameters with ranges.
1998 * Revision 1.101 2002/08/19 02:39:17 steve
1999 * Support parameters with defined ranges.
2001 * Revision 1.100 2002/08/12 01:35:00 steve
2002 * conditional ident string using autoconfig.
2004 * Revision 1.99 2002/06/21 04:59:35 steve
2005 * Carry integerness throughout the compilation.
2007 * Revision 1.98 2002/05/26 01:39:02 steve
2008 * Carry Verilog 2001 attributes with processes,
2009 * all the way through to the ivl_target API.
2011 * Divide signal reference counts between rval
2012 * and lval references.
2014 * Revision 1.97 2002/05/24 04:36:23 steve
2015 * Verilog 2001 attriubtes on nets/wires.
2017 * Revision 1.96 2002/05/23 03:08:51 steve
2018 * Add language support for Verilog-2001 attribute
2019 * syntax. Hook this support into existing $attribute
2020 * handling, and add number and void value types.
2022 * Add to the ivl_target API new functions for access
2023 * of complex attributes attached to gates.
2025 * Revision 1.95 2002/05/20 02:06:01 steve
2026 * Add ranges and signed to port list declarations.
2028 * Revision 1.94 2002/05/19 23:37:28 steve
2029 * Parse port_declaration_lists from the 2001 Standard.
2031 * Revision 1.93 2002/04/18 18:38:37 steve
2032 * Fix first_file test for timescale warning.
2034 * Revision 1.92 2002/04/15 00:04:23 steve
2035 * Timescale warnings.
2037 * Revision 1.91 2002/04/12 02:57:08 steve
2038 * Detect mismatches in reg as module items and ports.
2040 * Revision 1.90 2002/01/31 04:10:15 steve
2041 * Detect duplicate port declarations.
2043 * Revision 1.89 2002/01/26 05:28:28 steve
2044 * Detect scalar/vector declarion mismatch.
2046 * Revision 1.88 2002/01/12 04:03:39 steve
2047 * Drive strengths for continuous assignments.
2049 * Revision 1.87 2001/12/07 05:03:13 steve
2050 * Support integer for function return value.
2052 * Revision 1.86 2001/12/03 04:47:15 steve
2053 * Parser and pform use hierarchical names as hname_t
2054 * objects instead of encoded strings.
2056 * Revision 1.85 2001/11/29 17:37:51 steve
2057 * Properly parse net_decl assignments with delays.
2059 * Revision 1.84 2001/11/10 02:08:49 steve
2060 * Coerse input to inout when assigned to.
2062 * Revision 1.83 2001/10/31 03:11:15 steve
2063 * detect module ports not declared within the module.
2065 * Revision 1.82 2001/10/21 01:55:24 steve
2066 * Error messages for missing UDP port declarations.
2068 * Revision 1.81 2001/10/21 00:42:48 steve
2069 * Module types in pform are char* instead of string.
2071 * Revision 1.80 2001/10/20 23:02:40 steve
2072 * Add automatic module libraries.
2074 * Revision 1.79 2001/10/20 05:21:51 steve
2075 * Scope/module names are char* instead of string.
2077 * Revision 1.78 2001/07/25 03:10:49 steve
2078 * Create a config.h.in file to hold all the config
2079 * junk, and support gcc 3.0. (Stephan Boettcher)
2081 * Revision 1.77 2001/05/25 02:21:34 steve
2082 * Detect input and input ports declared as reg.
2084 * Revision 1.76 2001/05/20 15:03:25 steve
2085 * Deleted wrong time when -Tmax is selected.
2087 * Revision 1.75 2001/04/28 23:18:08 steve
2088 * UDP instances need not have user supplied names.
2090 * Revision 1.74 2001/02/17 05:15:33 steve
2091 * Allow task ports to be given real types.
2093 * Revision 1.73 2001/01/15 00:47:01 steve
2094 * Pass scope type information to the target module.
2096 * Revision 1.72 2001/01/14 23:04:56 steve
2097 * Generalize the evaluation of floating point delays, and
2098 * get it working with delay assignment statements.
2100 * Allow parameters to be referenced by hierarchical name.
2102 * Revision 1.71 2001/01/10 05:32:44 steve
2103 * Match memories within task scopes. (PR#101)
2105 * Revision 1.70 2001/01/06 06:31:59 steve
2106 * declaration initialization for time variables.
2108 * Revision 1.69 2001/01/06 02:29:36 steve
2109 * Support arrays of integers.
2111 * Revision 1.68 2000/12/11 00:31:43 steve
2112 * Add support for signed reg variables,
2113 * simulate in t-vvm signed comparisons.
2115 * Revision 1.67 2000/11/30 17:31:42 steve
2116 * Change LineInfo to store const C strings.