1 /*
2  * Copyright (c) 2002-2010 Stephen Williams (steve@icarus.com)
3  *
4  *    This source code is free software; you can redistribute it
5  *    and/or modify it in source code form under the terms of the GNU
6  *    General Public License as published by the Free Software
7  *    Foundation; either version 2 of the License, or (at your option)
8  *    any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 # include  "config.h"
21 # include  "util.h"
22 # include  "PExpr.h"
23 # include  "netlist.h"
24 # include  <iostream>
25 # include  <cassert>
26 
27 /*
28  * The evaluate_attributes function evaluates the attribute
29  * expressions from the map, and returns a table in a form suitable
30  * for passing to netlist devices.
31  */
32 
evaluate_attributes(const map<perm_string,PExpr * > & att,unsigned & natt,Design * des,NetScope * scope)33 attrib_list_t* evaluate_attributes(const map<perm_string,PExpr*>&att,
34 				   unsigned&natt,
35 				   Design*des, NetScope*scope)
36 {
37       natt = att.size();
38       if (natt == 0)
39 	    return 0;
40 
41       attrib_list_t*table = new attrib_list_t [natt];
42 
43       unsigned idx = 0;
44 
45       typedef map<perm_string,PExpr*>::const_iterator iter_t;
46       for (iter_t cur = att.begin() ;  cur != att.end() ;  ++ cur , idx += 1) {
47 	    table[idx].key = (*cur).first;
48 	    PExpr*exp = (*cur).second;
49 
50 	      /* If the attribute value is given in the source, then
51 		 evaluate it as a constant. If the value is not
52 		 given, then assume the value is 1. */
53 	    verinum*tmp = 0;
54 	    if (exp) {
55 		  tmp = exp->eval_const(des, scope);
56                   if (tmp == 0) {
57 			cerr << exp->get_fileline() << ": error: ``"
58 			     << *exp << "'' is not a constant expression."
59 			     << endl;
60 			des->errors += 1;
61                   }
62             }
63 	    if (tmp == 0)
64 		  tmp = new verinum(1);
65 
66 	    assert(tmp);
67 
68 	    table[idx].val = *tmp;
69 	    delete tmp;
70       }
71 
72       assert(idx == natt);
73       return table;
74 }
75