Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 203 lines (168 sloc) 5.309 kb
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
1 /*
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
2 * Copyright (c) 2004-2010 Stephen Williams (steve@icarus.com)
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19
20 # include "config.h"
21 # include "compiler.h"
1993bf6 @caryr Remove malloc.h support and for C++ files use <c...> include files.
caryr authored
22 # include <cstdio>
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored
23 # include <cstring>
24 # include <cstdlib>
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
25
26 /*
27 * Manage the information about system functions. This information is
28 * collected from the sources before elaboration and made available
29 * via the lookup_sys_func function.
30 */
31
32 static const struct sfunc_return_type sfunc_table[] = {
1f7957c @steveicarus Fix type handling of real-value system functions in nets
steveicarus authored
33 { "$realtime", IVL_VT_REAL, 1, 0 },
34 { "$bitstoreal", IVL_VT_REAL, 1, 0 },
35 { "$itor", IVL_VT_REAL, 1, 0 },
b9799cf Remove NetVariable and ivl_variable_t structures.
steve authored
36 { "$realtobits", IVL_VT_LOGIC, 64, 0 },
37 { "$time", IVL_VT_LOGIC, 64, 0 },
38 { "$stime", IVL_VT_LOGIC, 32, 0 },
39 { "$simtime", IVL_VT_LOGIC, 64, 0 },
40 { 0, IVL_VT_LOGIC, 32, 0 }
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
41 };
42
28719e8 Add support for system function table files.
steve authored
43 struct sfunc_return_type_cell : sfunc_return_type {
44 struct sfunc_return_type_cell*next;
45 };
46
47 static struct sfunc_return_type_cell*sfunc_stack = 0;
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
48
d98c925 @caryr Some compiler cleanup and minor memory leak fixes.
caryr authored
49 void cleanup_sys_func_table()
50 {
51 struct sfunc_return_type_cell *next, *cur = sfunc_stack;
52 while (cur) {
53 next = cur->next;
54 delete cur;
55 cur = next;
56 }
57 }
58
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
59 const struct sfunc_return_type* lookup_sys_func(const char*name)
60 {
e9fda22 @ldoolitt Spelling fixes
ldoolitt authored
61 /* First, try to find the name in the function stack. */
28719e8 Add support for system function table files.
steve authored
62 struct sfunc_return_type_cell*cur = sfunc_stack;
63 while (cur) {
64 if (strcmp(cur->name, name) == 0)
65 return cur;
66
67 cur = cur->next;
68 }
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
69
28719e8 Add support for system function table files.
steve authored
70 /* Next, look in the core table. */
71 unsigned idx = 0;
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
72 while (sfunc_table[idx].name) {
73
74 if (strcmp(sfunc_table[idx].name, name) == 0)
75 return sfunc_table + idx;
76
77 idx += 1;
78 }
79
28719e8 Add support for system function table files.
steve authored
80 /* No luck finding, so return the trailer, which give a
a0ed5e9 Separate out the lookup_sys_func table, for eventual
steve authored
81 default description. */
82 return sfunc_table + idx;
83 }
84
85 /*
28719e8 Add support for system function table files.
steve authored
86 * This function loads a system functions descriptor file with the
87 * format:
88 *
89 * <name> <type> [<arguments>]
90 */
91 int load_sys_func_table(const char*path)
92 {
93 struct sfunc_return_type_cell*cell;
94 FILE*fd = fopen(path, "r");
95
0ef1eca Verbose details for sft parsing.
steve authored
96 if (fd == 0) {
97 if (verbose_flag) {
98 fprintf(stderr, "%s: Unable to open System Function Table file.\n", path);
99 }
28719e8 Add support for system function table files.
steve authored
100 return -1;
0ef1eca Verbose details for sft parsing.
steve authored
101 }
102
103 if (verbose_flag) {
104 fprintf(stderr, "%s: Processing System Function Table file.\n", path);
105 }
28719e8 Add support for system function table files.
steve authored
106
107 char buf[256];
108 while (fgets(buf, sizeof buf, fd)) {
109 char*name = buf + strspn(buf, " \t\r\n");
110
111 /* Skip empty lines. */
112 if (name[0] == 0)
113 continue;
114 /* Skip comment lines. */
115 if (name[0] == '#')
116 continue;
117
118 char*cp = name + strcspn(name, " \t\r\n");
119 if (cp[0]) *cp++ = 0;
120
121 cp += strspn(cp, " \t\r\n");
122
123 char*stype = cp;
124 if (stype[0] == 0) {
125 fprintf(stderr, "%s:%s: No function type?\n",
126 path, name);
127 continue;
128 }
129
130 cp = stype + strcspn(stype, " \t\r\n");
131 if (cp[0]) *cp++ = 0;
132
133 if (strcmp(stype,"vpiSysFuncReal") == 0) {
134 cell = new struct sfunc_return_type_cell;
135 cell->name = lex_strings.add(name);
b9799cf Remove NetVariable and ivl_variable_t structures.
steve authored
136 cell->type = IVL_VT_REAL;
1f7957c @steveicarus Fix type handling of real-value system functions in nets
steveicarus authored
137 cell->wid = 1;
28719e8 Add support for system function table files.
steve authored
138 cell->signed_flag = true;
139 cell->next = sfunc_stack;
140 sfunc_stack = cell;
141 continue;
142 }
143
144 if (strcmp(stype,"vpiSysFuncInt") == 0) {
145 cell = new struct sfunc_return_type_cell;
146 cell->name = lex_strings.add(name);
b9799cf Remove NetVariable and ivl_variable_t structures.
steve authored
147 cell->type = IVL_VT_LOGIC;
28719e8 Add support for system function table files.
steve authored
148 cell->wid = 32;
149 cell->signed_flag = true;
150 cell->next = sfunc_stack;
151 sfunc_stack = cell;
152 continue;
153 }
154
155 /* If this is a sized integer, then parse the additional
156 arguments, the width (decimal) and the optional
157 signed/unsigned flag. */
158 if (strcmp(stype,"vpiSysFuncSized") == 0) {
159 cp += strspn(cp, " \t\r\n");
160 char*swidth = cp;
161 unsigned width = 32;
162 bool signed_flag = false;
163
164 cp = swidth + strcspn(swidth, " \t\r\n");
165 if (cp[0]) *cp++ = 0;
166
167 width = strtoul(swidth, 0, 10);
168
169 cp += strspn(cp, " \t\r\n");
170 char*flag = cp;
171
172 while (flag[0]) {
173 cp = flag + strcspn(flag, " \t\r\n");
174 if (cp[0]) *cp++ = 0;
e4ae832 Clean up spurious trailing white space.
steve authored
175
28719e8 Add support for system function table files.
steve authored
176 if (strcmp(flag,"signed") == 0) {
177 signed_flag = true;
178
179 } else if (strcmp(flag,"unsigned") == 0) {
180 signed_flag = false;
181 }
182
183 flag = cp + strspn(cp, " \t\r\n");
184 }
185
186 cell = new struct sfunc_return_type_cell;
187 cell->name = lex_strings.add(name);
b9799cf Remove NetVariable and ivl_variable_t structures.
steve authored
188 cell->type = IVL_VT_LOGIC;
28719e8 Add support for system function table files.
steve authored
189 cell->wid = width;
190 cell->signed_flag = signed_flag;
191 cell->next = sfunc_stack;
192 sfunc_stack = cell;
193 continue;
194 }
195
196 fprintf(stderr, "%s:%s: Unknown type: %s\n",
197 path, name, stype);
198 }
d98c925 @caryr Some compiler cleanup and minor memory leak fixes.
caryr authored
199 fclose(fd);
28719e8 Add support for system function table files.
steve authored
200
201 return 0;
202 }
Something went wrong with that request. Please try again.