Merge branch 'master' into verilog-ams
[sverilog.git] / sys_funcs.cc
bloba82ac0b629f6015e636a6b75846a923d7fa97e9e
1 /*
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)
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
20 # include "config.h"
21 # include "compiler.h"
22 # include <stdio.h>
23 # include <cstring>
24 # include <cstdlib>
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;
53 while (cur) {
54 if (strcmp(cur->name, name) == 0)
55 return cur;
57 cur = cur->next;
60 /* Next, look in the core table. */
61 unsigned idx = 0;
62 while (sfunc_table[idx].name) {
64 if (strcmp(sfunc_table[idx].name, name) == 0)
65 return sfunc_table + idx;
67 idx += 1;
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
77 * format:
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");
86 if (fd == 0) {
87 if (verbose_flag) {
88 fprintf(stderr, "%s: Unable to open System Function Table file.\n", path);
90 return -1;
93 if (verbose_flag) {
94 fprintf(stderr, "%s: Processing System Function Table file.\n", path);
97 char buf[256];
98 while (fgets(buf, sizeof buf, fd)) {
99 char*name = buf + strspn(buf, " \t\r\n");
101 /* Skip empty lines. */
102 if (name[0] == 0)
103 continue;
104 /* Skip comment lines. */
105 if (name[0] == '#')
106 continue;
108 char*cp = name + strcspn(name, " \t\r\n");
109 if (cp[0]) *cp++ = 0;
111 cp += strspn(cp, " \t\r\n");
113 char*stype = cp;
114 if (stype[0] == 0) {
115 fprintf(stderr, "%s:%s: No function type?\n",
116 path, name);
117 continue;
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;
127 cell->wid = 1;
128 cell->signed_flag = true;
129 cell->next = sfunc_stack;
130 sfunc_stack = cell;
131 continue;
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;
138 cell->wid = 32;
139 cell->signed_flag = true;
140 cell->next = sfunc_stack;
141 sfunc_stack = cell;
142 continue;
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");
150 char*swidth = cp;
151 unsigned width = 32;
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");
160 char*flag = cp;
162 while (flag[0]) {
163 cp = flag + strcspn(flag, " \t\r\n");
164 if (cp[0]) *cp++ = 0;
166 if (strcmp(flag,"signed") == 0) {
167 signed_flag = true;
169 } else if (strcmp(flag,"unsigned") == 0) {
170 signed_flag = false;
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;
179 cell->wid = width;
180 cell->signed_flag = signed_flag;
181 cell->next = sfunc_stack;
182 sfunc_stack = cell;
183 continue;
186 fprintf(stderr, "%s:%s: Unknown type: %s\n",
187 path, name, stype);
190 return 0;