Fix parsing of hierarchical identifiers
[iverilog.git] / sys_funcs.cc
blob0776abddbaefd364228fa630abc350dcc0af0cd3
1 /*
2 * Copyright (c) 2004 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: sys_funcs.cc,v 1.7 2005/07/11 16:56:51 steve Exp $"
21 #endif
23 # include "config.h"
24 # include "compiler.h"
25 # include <stdio.h>
28 * Manage the information about system functions. This information is
29 * collected from the sources before elaboration and made available
30 * via the lookup_sys_func function.
33 static const struct sfunc_return_type sfunc_table[] = {
34 { "$realtime", IVL_VT_REAL, 0, 0 },
35 { "$bitstoreal", IVL_VT_REAL, 0, 0 },
36 { "$itor", IVL_VT_REAL, 0, 0 },
37 { "$realtobits", IVL_VT_LOGIC, 64, 0 },
38 { "$time", IVL_VT_LOGIC, 64, 0 },
39 { "$stime", IVL_VT_LOGIC, 32, 0 },
40 { "$simtime", IVL_VT_LOGIC, 64, 0 },
41 { 0, IVL_VT_LOGIC, 32, 0 }
44 struct sfunc_return_type_cell : sfunc_return_type {
45 struct sfunc_return_type_cell*next;
48 static struct sfunc_return_type_cell*sfunc_stack = 0;
50 const struct sfunc_return_type* lookup_sys_func(const char*name)
52 /* First, try to find then name in the function stack. */
53 struct sfunc_return_type_cell*cur = sfunc_stack;
54 while (cur) {
55 if (strcmp(cur->name, name) == 0)
56 return cur;
58 cur = cur->next;
61 /* Next, look in the core table. */
62 unsigned idx = 0;
63 while (sfunc_table[idx].name) {
65 if (strcmp(sfunc_table[idx].name, name) == 0)
66 return sfunc_table + idx;
68 idx += 1;
71 /* No luck finding, so return the trailer, which give a
72 default description. */
73 return sfunc_table + idx;
77 * This function loads a system functions descriptor file with the
78 * format:
80 * <name> <type> [<arguments>]
82 int load_sys_func_table(const char*path)
84 struct sfunc_return_type_cell*cell;
85 FILE*fd = fopen(path, "r");
87 if (fd == 0) {
88 if (verbose_flag) {
89 fprintf(stderr, "%s: Unable to open System Function Table file.\n", path);
91 return -1;
94 if (verbose_flag) {
95 fprintf(stderr, "%s: Processing System Function Table file.\n", path);
98 char buf[256];
99 while (fgets(buf, sizeof buf, fd)) {
100 char*name = buf + strspn(buf, " \t\r\n");
102 /* Skip empty lines. */
103 if (name[0] == 0)
104 continue;
105 /* Skip comment lines. */
106 if (name[0] == '#')
107 continue;
109 char*cp = name + strcspn(name, " \t\r\n");
110 if (cp[0]) *cp++ = 0;
112 cp += strspn(cp, " \t\r\n");
114 char*stype = cp;
115 if (stype[0] == 0) {
116 fprintf(stderr, "%s:%s: No function type?\n",
117 path, name);
118 continue;
121 cp = stype + strcspn(stype, " \t\r\n");
122 if (cp[0]) *cp++ = 0;
124 if (strcmp(stype,"vpiSysFuncReal") == 0) {
125 cell = new struct sfunc_return_type_cell;
126 cell->name = lex_strings.add(name);
127 cell->type = IVL_VT_REAL;
128 cell->wid = 0;
129 cell->signed_flag = true;
130 cell->next = sfunc_stack;
131 sfunc_stack = cell;
132 continue;
135 if (strcmp(stype,"vpiSysFuncInt") == 0) {
136 cell = new struct sfunc_return_type_cell;
137 cell->name = lex_strings.add(name);
138 cell->type = IVL_VT_LOGIC;
139 cell->wid = 32;
140 cell->signed_flag = true;
141 cell->next = sfunc_stack;
142 sfunc_stack = cell;
143 continue;
146 /* If this is a sized integer, then parse the additional
147 arguments, the width (decimal) and the optional
148 signed/unsigned flag. */
149 if (strcmp(stype,"vpiSysFuncSized") == 0) {
150 cp += strspn(cp, " \t\r\n");
151 char*swidth = cp;
152 unsigned width = 32;
153 bool signed_flag = false;
155 cp = swidth + strcspn(swidth, " \t\r\n");
156 if (cp[0]) *cp++ = 0;
158 width = strtoul(swidth, 0, 10);
160 cp += strspn(cp, " \t\r\n");
161 char*flag = cp;
163 while (flag[0]) {
164 cp = flag + strcspn(flag, " \t\r\n");
165 if (cp[0]) *cp++ = 0;
167 if (strcmp(flag,"signed") == 0) {
168 signed_flag = true;
170 } else if (strcmp(flag,"unsigned") == 0) {
171 signed_flag = false;
174 flag = cp + strspn(cp, " \t\r\n");
177 cell = new struct sfunc_return_type_cell;
178 cell->name = lex_strings.add(name);
179 cell->type = IVL_VT_LOGIC;
180 cell->wid = width;
181 cell->signed_flag = signed_flag;
182 cell->next = sfunc_stack;
183 sfunc_stack = cell;
184 continue;
187 fprintf(stderr, "%s:%s: Unknown type: %s\n",
188 path, name, stype);
191 return 0;
195 * $Log: sys_funcs.cc,v $
196 * Revision 1.7 2005/07/11 16:56:51 steve
197 * Remove NetVariable and ivl_variable_t structures.
199 * Revision 1.6 2004/10/04 01:10:55 steve
200 * Clean up spurious trailing white space.
202 * Revision 1.5 2004/03/17 17:07:12 steve
203 * Protect ident pragma.
205 * Revision 1.4 2004/03/13 05:12:36 steve
206 * standard conpliant stdio.h.
208 * Revision 1.3 2004/03/11 06:02:58 steve
209 * Verbose details for sft parsing.
211 * Revision 1.2 2004/03/10 04:51:24 steve
212 * Add support for system function table files.
214 * Revision 1.1 2004/03/09 04:29:42 steve
215 * Separate out the lookup_sys_func table, for eventual
216 * support for function type tables.
218 * Remove ipal compile flags.