/
main.c
471 lines (407 loc) · 13 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
const char COPYRIGHT[] =
"Copyright (c) 1999-2011 Stephen Williams (steve@icarus.com)";
/*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "config.h"
# include "version_base.h"
# include "version_tag.h"
const char NOTICE[] =
" This program is free software; you can redistribute it and/or modify\n"
" it under the terms of the GNU General Public License as published by\n"
" the Free Software Foundation; either version 2 of the License, or\n"
" (at your option) any later version.\n"
"\n"
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n"
"\n"
" You should have received a copy of the GNU General Public License along\n"
" with this program; if not, write to the Free Software Foundation, Inc.,\n"
" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
;
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <string.h>
# include <ctype.h>
#if defined(HAVE_GETOPT_H)
# include <getopt.h>
#endif
# include "globals.h"
# include "ivl_alloc.h"
#if defined(__MINGW32__) && !defined(HAVE_GETOPT_H)
extern int getopt(int argc, char*argv[], const char*fmt);
extern int optind;
extern const char*optarg;
#endif
/* Path to the dependency file, if there is one. */
char *dep_path = NULL;
/* Dependency file output mode */
char dep_mode = 'a';
/* verbose flag */
int verbose_flag = 0;
/* Path to vhdlpp */
char *vhdlpp_path = 0;
/* vhdlpp work directory */
char *vhdlpp_work = 0;
char**vhdlpp_libdir = 0;
unsigned vhdlpp_libdir_cnt = 0;
/*
* Keep in source_list an array of pointers to file names. The array
* is terminated by a pointer to null.
*/
static char**source_list = 0;
static unsigned source_cnt = 0;
void add_source_file(const char*name)
{
if (source_list == 0) {
source_list = calloc(2, sizeof(char*));
source_list[0] = strdup(name);
source_list[1] = 0;
source_cnt = 1;
} else {
source_list = realloc(source_list, sizeof(char*) * (source_cnt+2));
source_list[source_cnt+0] = strdup(name);
source_list[source_cnt+1] = 0;
source_cnt += 1;
}
}
char**include_dir = 0;
unsigned include_cnt = 0;
int relative_include = 0;
int line_direct_flag = 0;
unsigned error_count = 0;
FILE *depend_file = NULL;
static int flist_read_flags(const char*path)
{
char line_buf[2048];
FILE*fd = fopen(path, "r");
if (fd == 0) {
fprintf(stderr, "%s: unable to open for reading.\n", path);
return -1;
}
while (fgets(line_buf, sizeof line_buf, fd) != 0) {
/* Skip leading white space. */
char*cp = line_buf + strspn(line_buf, " \t\r\b\f");
/* Remove trailing white space. */
char*tail = cp + strlen(cp);
char*arg;
while (tail > cp) {
if (! isspace((int)tail[-1]))
break;
tail -= 1;
tail[0] = 0;
}
/* Skip empty lines */
if (*cp == 0)
continue;
/* Skip comment lines */
if (cp[0] == '#')
continue;
/* The arg points to the argument to the keyword. */
arg = strchr(cp, ':');
if (arg) *arg++ = 0;
if (strcmp(cp,"D") == 0) {
char*val = strchr(arg, '=');
const char *valo = "1";
if (val) {
*val++ = 0;
valo = val;
}
define_macro(arg, valo, 0, 0);
} else if (strcmp(cp,"I") == 0) {
include_dir = realloc(include_dir,
(include_cnt+1)*sizeof(char*));
include_dir[include_cnt] = strdup(arg);
include_cnt += 1;
} else if (strcmp(cp,"keyword") == 0) {
char*buf = malloc(strlen(arg) + 2);
buf[0] = '`';
strcpy(buf+1, optarg);
define_macro(optarg, buf, 1, 0);
free(buf);
} else if ((strcmp(cp,"Ma") == 0)
|| (strcmp(cp,"Mi") == 0)
|| (strcmp(cp,"Mm") == 0)
|| (strcmp(cp,"Mp") == 0)) {
if (dep_path) {
fprintf(stderr, "duplicate -M flag.\n");
} else {
dep_mode = cp[1];
dep_path = strdup(arg);
}
} else if (strcmp(cp,"relative include") == 0) {
if (strcmp(arg, "true") == 0) {
relative_include = 1;
} else {
relative_include = 0;
}
} else if (strcmp(cp,"vhdlpp") == 0) {
if (vhdlpp_path) {
fprintf(stderr, "Ignore multiple vhdlpp flags\n");
} else {
vhdlpp_path = strdup(arg);
}
} else if (strcmp(cp,"vhdlpp-work") == 0) {
if (vhdlpp_work) {
fprintf(stderr, "Ignore duplicate vhdlpp-work flags\n");
} else {
vhdlpp_work = strdup(arg);
}
} else if (strcmp(cp,"vhdlpp-libdir") == 0) {
vhdlpp_libdir = realloc(vhdlpp_libdir,
(vhdlpp_libdir_cnt+1)*sizeof(char*));
vhdlpp_libdir[vhdlpp_libdir_cnt] = strdup(arg);
vhdlpp_libdir_cnt += 1;
} else {
fprintf(stderr, "%s: Invalid keyword %s\n", path, cp);
}
}
fclose(fd);
return 0;
}
/*
* This function reads from a file a list of file names. Each name
* starts with the first non-space character, and ends with the last
* non-space character. Spaces in the middle are OK.
*/
static int flist_read_names(const char*path)
{
char line_buf[2048];
FILE*fd = fopen(path, "r");
if (fd == 0) {
fprintf(stderr, "%s: unable to open for reading.\n", path);
return 1;
}
while (fgets(line_buf, sizeof line_buf, fd) != 0) {
char*cp = line_buf + strspn(line_buf, " \t\r\b\f");
char*tail = cp + strlen(cp);
while (tail > cp) {
if (! isspace((int)tail[-1]))
break;
tail -= 1;
tail[0] = 0;
}
if (cp < tail)
add_source_file(cp);
}
fclose(fd);
return 0;
}
int main(int argc, char*argv[])
{
int opt, idx;
unsigned lp;
const char*flist_path = 0;
unsigned flag_errors = 0;
char*out_path = 0;
FILE*out;
char*precomp_out_path = 0;
FILE*precomp_out = NULL;
/* Define preprocessor keywords that I plan to just pass. */
/* From 1364-2005 Chapter 19. */
define_macro("begin_keywords", "`begin_keywords", 1, 0);
define_macro("celldefine", "`celldefine", 1, 0);
define_macro("default_nettype", "`default_nettype", 1, 0);
define_macro("end_keywords", "`end_keywords", 1, 0);
define_macro("endcelldefine", "`endcelldefine", 1, 0);
define_macro("line", "`line", 1, 0);
define_macro("nounconnected_drive", "`nounconnected_drive", 1, 0);
define_macro("pragma", "`pragma", 1, 0);
define_macro("resetall", "`resetall", 1, 0);
define_macro("timescale", "`timescale", 1, 0);
define_macro("unconnected_drive", "`unconnected_drive", 1, 0);
/* From 1364-2005 Annex D. */
define_macro("default_decay_time", "`default_decay_time", 1, 0);
define_macro("default_trireg_strength", "`default_trireg_strength", 1, 0);
define_macro("delay_mode_distributed", "`delay_mode_distributed", 1, 0);
define_macro("delay_mode_path", "`delay_mode_path", 1, 0);
define_macro("delay_mode_unit", "`delay_mode_unit", 1, 0);
define_macro("delay_mode_zero", "`delay_mode_zero", 1, 0);
/* From other places. */
define_macro("disable_portfaults", "`disable_portfaults", 1, 0);
define_macro("enable_portfaults", "`enable_portfaults", 1, 0);
define_macro("endprotect", "`endprotect", 1, 0);
define_macro("nosuppress_faults", "`nosuppress_faults", 1, 0);
define_macro("protect", "`protect", 1, 0);
define_macro("suppress_faults", "`suppress_faults", 1, 0);
define_macro("uselib", "`uselib", 1, 0);
include_cnt = 2;
include_dir = malloc(include_cnt*sizeof(char*));
include_dir[0] = 0; /* 0 is reserved for the current files path. */
include_dir[1] = strdup(".");
while ((opt=getopt(argc, argv, "F:f:K:Lo:p:P:vV")) != EOF) switch (opt) {
case 'F':
flist_read_flags(optarg);
break;
case 'f':
if (flist_path) {
fprintf(stderr, "%s: duplicate -f flag\n", argv[0]);
flag_errors += 1;
}
flist_path = optarg;
break;
case 'K': {
char*buf = malloc(strlen(optarg) + 2);
buf[0] = '`';
strcpy(buf+1, optarg);
define_macro(optarg, buf, 1, 0);
free(buf);
break;
}
case 'L':
line_direct_flag = 1;
break;
case 'o':
if (out_path) {
fprintf(stderr, "duplicate -o flag.\n");
} else {
out_path = optarg;
}
break;
case 'p':
if (precomp_out_path) {
fprintf(stderr, "duplicate -p flag.\n");
} else {
precomp_out_path = optarg;
}
break;
case 'P': {
FILE*src = fopen(optarg, "rb");
if (src == 0) {
perror(optarg);
exit(1);
}
load_precompiled_defines(src);
fclose(src);
break;
}
case 'v':
fprintf(stderr, "Icarus Verilog Preprocessor version "
VERSION " (" VERSION_TAG ")\n\n");
fprintf(stderr, "%s\n\n", COPYRIGHT);
fputs(NOTICE, stderr);
verbose_flag = 1;
break;
case 'V':
fprintf(stdout, "Icarus Verilog Preprocessor version "
VERSION " (" VERSION_TAG ")\n\n");
fprintf(stdout, "%s\n", COPYRIGHT);
fputs(NOTICE, stdout);
return 0;
default:
flag_errors += 1;
break;
}
if (flag_errors) {
fprintf(stderr, "\nUsage: %s [-v][-L][-F<fil>][-f<fil>] <file>...\n"
" -F<fil> - Get defines and includes from file\n"
" -f<fil> - Read the sources listed in the file\n"
" -K<def> - Define a keyword macro that I just pass\n"
" -L - Emit line number directives\n"
" -o<fil> - Send the output to <fil>\n"
" -p<fil> - Write precompiled defines to <fil>\n"
" -P<fil> - Read precompiled defines from <fil>\n"
" -v - Verbose\n"
" -V - Print version information and quit\n",
argv[0]);
return flag_errors;
}
/* Collect the file names on the command line in the source
file list, then if there is a file list, read more file
names from there. */
for (idx = optind ; idx < argc ; idx += 1)
add_source_file(argv[idx]);
if (flist_path) {
int rc = flist_read_names(flist_path);
if (rc != 0)
return rc;
}
/* Figure out what to use for an output file. Write to stdout
if no path is specified. */
if (out_path) {
out = fopen(out_path, "w");
if (out == 0) {
perror(out_path);
exit(1);
}
} else {
out = stdout;
}
if (precomp_out_path) {
precomp_out = fopen(precomp_out_path, "wb");
if (precomp_out == 0) {
if (out_path) fclose(out);
perror(precomp_out_path);
exit(1);
}
}
if (dep_path) {
depend_file = fopen(dep_path, "a");
if (depend_file == 0) {
if (out_path) fclose(out);
if (precomp_out) fclose(precomp_out);
perror(dep_path);
exit(1);
}
}
if (source_cnt == 0) {
fprintf(stderr, "%s: No input files given.\n", argv[0]);
if (out_path) fclose(out);
if (depend_file) fclose(depend_file);
if (precomp_out) fclose(precomp_out);
return 1;
}
if (vhdlpp_work == 0) {
vhdlpp_work = strdup("ivl_vhdl_work");
}
/* Pass to the lexical analyzer the list of input file, and
start scanning. */
reset_lexor(out, source_list);
if (yylex()) {
if (out_path) fclose(out);
if (depend_file) fclose(depend_file);
if (precomp_out) fclose(precomp_out);
return -1;
}
destroy_lexor();
if (depend_file) fclose(depend_file);
if (precomp_out) {
dump_precompiled_defines(precomp_out);
fclose(precomp_out);
}
if (out_path) fclose(out);
/* Free the source and include directory lists. */
for (lp = 0; lp < source_cnt; lp += 1) {
free(source_list[lp]);
}
free(source_list);
for (lp = 0; lp < include_cnt; lp += 1) {
free(include_dir[lp]);
}
free(include_dir);
/* Free the VHDL library directories, the path and work directory. */
for (lp = 0; lp < vhdlpp_libdir_cnt; lp += 1) {
free(vhdlpp_libdir[lp]);
}
free(vhdlpp_libdir);
free(vhdlpp_path);
free(vhdlpp_work);
free_macros();
return error_count;
}