2 * Copyright (c) 2004-2008 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)
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
21 # include "compiler.h"
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.
32 static const struct sfunc_return_type sfunc_table
[] = {
33 { "$realtime", IVL_VT_REAL
, 1, 0 },
34 { "$bitstoreal", IVL_VT_REAL
, 1, 0 },
35 { "$itor", IVL_VT_REAL
, 1, 0 },
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 }
43 struct sfunc_return_type_cell
: sfunc_return_type
{
44 struct sfunc_return_type_cell
*next
;
47 static struct sfunc_return_type_cell
*sfunc_stack
= 0;
49 const struct sfunc_return_type
* lookup_sys_func(const char*name
)
51 /* First, try to find then name in the function stack. */
52 struct sfunc_return_type_cell
*cur
= sfunc_stack
;
54 if (strcmp(cur
->name
, name
) == 0)
60 /* Next, look in the core table. */
62 while (sfunc_table
[idx
].name
) {
64 if (strcmp(sfunc_table
[idx
].name
, name
) == 0)
65 return sfunc_table
+ idx
;
70 /* No luck finding, so return the trailer, which give a
71 default description. */
72 return sfunc_table
+ idx
;
76 * This function loads a system functions descriptor file with the
79 * <name> <type> [<arguments>]
81 int load_sys_func_table(const char*path
)
83 struct sfunc_return_type_cell
*cell
;
84 FILE*fd
= fopen(path
, "r");
88 fprintf(stderr
, "%s: Unable to open System Function Table file.\n", path
);
94 fprintf(stderr
, "%s: Processing System Function Table file.\n", path
);
98 while (fgets(buf
, sizeof buf
, fd
)) {
99 char*name
= buf
+ strspn(buf
, " \t\r\n");
101 /* Skip empty lines. */
104 /* Skip comment lines. */
108 char*cp
= name
+ strcspn(name
, " \t\r\n");
109 if (cp
[0]) *cp
++ = 0;
111 cp
+= strspn(cp
, " \t\r\n");
115 fprintf(stderr
, "%s:%s: No function type?\n",
120 cp
= stype
+ strcspn(stype
, " \t\r\n");
121 if (cp
[0]) *cp
++ = 0;
123 if (strcmp(stype
,"vpiSysFuncReal") == 0) {
124 cell
= new struct sfunc_return_type_cell
;
125 cell
->name
= lex_strings
.add(name
);
126 cell
->type
= IVL_VT_REAL
;
128 cell
->signed_flag
= true;
129 cell
->next
= sfunc_stack
;
134 if (strcmp(stype
,"vpiSysFuncInt") == 0) {
135 cell
= new struct sfunc_return_type_cell
;
136 cell
->name
= lex_strings
.add(name
);
137 cell
->type
= IVL_VT_LOGIC
;
139 cell
->signed_flag
= true;
140 cell
->next
= sfunc_stack
;
145 /* If this is a sized integer, then parse the additional
146 arguments, the width (decimal) and the optional
147 signed/unsigned flag. */
148 if (strcmp(stype
,"vpiSysFuncSized") == 0) {
149 cp
+= strspn(cp
, " \t\r\n");
152 bool signed_flag
= false;
154 cp
= swidth
+ strcspn(swidth
, " \t\r\n");
155 if (cp
[0]) *cp
++ = 0;
157 width
= strtoul(swidth
, 0, 10);
159 cp
+= strspn(cp
, " \t\r\n");
163 cp
= flag
+ strcspn(flag
, " \t\r\n");
164 if (cp
[0]) *cp
++ = 0;
166 if (strcmp(flag
,"signed") == 0) {
169 } else if (strcmp(flag
,"unsigned") == 0) {
173 flag
= cp
+ strspn(cp
, " \t\r\n");
176 cell
= new struct sfunc_return_type_cell
;
177 cell
->name
= lex_strings
.add(name
);
178 cell
->type
= IVL_VT_LOGIC
;
180 cell
->signed_flag
= signed_flag
;
181 cell
->next
= sfunc_stack
;
186 fprintf(stderr
, "%s:%s: Unknown type: %s\n",