iverilog/driver/main.c

640 lines
14 KiB
C
Raw Normal View History

2000-04-21 08:41:02 +02:00
/*
* Copyright (c) 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#if !defined(WINNT)
2001-05-20 20:22:02 +02:00
#ident "$Id: main.c,v 1.13 2001/05/20 18:22:02 steve Exp $"
2000-04-21 08:41:02 +02:00
#endif
const char HELP[] =
2001-05-17 05:14:26 +02:00
"Usage: iverilog [-ESv] [-B base] [-C path] [-c cmdfile]
[-D macro[=defn]] [-I includedir] [-m module]
[-N file] [-o filename] [-p flag=value]
[-s topmodule] [-t target] [-T min|typ|max]
[-W class] source_file(s)
See man page for details.";
#define MAXSIZE 4096
2000-04-21 08:41:02 +02:00
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
2000-04-21 08:41:02 +02:00
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
#include <windows.h>
#endif
2000-09-30 05:20:47 +02:00
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#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
#if !defined(WIFEXITED)
2001-05-20 20:22:02 +02:00
# define WIFEXITED(rc) ((rc&0x7f) == 0)
#endif
#if !defined(WEXITSTATUS)
2001-05-20 20:22:02 +02:00
# define WEXITSTATUS(rc) (rc>>8)
#endif
2000-04-21 08:41:02 +02:00
#ifndef IVL_ROOT
# define IVL_ROOT "."
#endif
#ifndef RDYNAMIC
# define RDYNAMIC "-rdynamic"
#endif
# include "globals.h"
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
const char *sep = "\\";
#else
const char *sep = "/";
#endif
2000-04-21 08:41:02 +02:00
const char*base = IVL_ROOT;
2000-07-29 19:58:20 +02:00
const char*mtm = 0;
2001-05-20 17:09:39 +02:00
const char*opath = "a.out" EXEEXT;
const char*npath = 0;
2000-04-21 08:41:02 +02:00
const char*targ = "vvm";
2000-04-23 23:14:32 +02:00
const char*start = 0;
char warning_flags[16] = "";
char*inc_list = 0;
char*def_list = 0;
2000-05-17 05:53:29 +02:00
char*mod_list = 0;
char*command_filename = 0;
2000-04-29 03:20:14 +02:00
char*f_list = 0;
int synth_flag = 0;
2000-04-21 08:41:02 +02:00
int verbose_flag = 0;
int command_file = 0;
int inside_c_comment = 0;
FILE *fp;
2000-04-21 08:41:02 +02:00
char line[MAXSIZE];
char tmp[MAXSIZE];
2000-04-21 08:41:02 +02:00
/*
* This is the default target type. It looks up the bits that are
* needed to run the command from the configuration file (which is
* already parsed for us) so we can handle must of the generic cases.
*/
static int t_default(char*cmd, unsigned ncmd)
2000-04-22 00:51:38 +02:00
{
int rc;
const char*pattern;
2000-04-22 00:51:38 +02:00
pattern = lookup_pattern("<ivl>");
if (pattern == 0) {
fprintf(stderr, "No such target: %s\n", targ);
return -1;
}
tmp[0] = ' ';
tmp[1] = '|';
tmp[2] = ' ';
rc = build_string(tmp+3, sizeof tmp - 3, pattern);
cmd = realloc(cmd, ncmd+3+rc+1);
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
{
char *t;
for (t = tmp; *t; t++)
{
if (*t == '/') *t = '\\';
}
}
#endif
strcpy(cmd+ncmd, tmp);
2000-04-22 00:51:38 +02:00
if (verbose_flag)
printf("translate: %s\n", cmd);
2000-04-22 00:51:38 +02:00
rc = system(cmd);
if (rc != 0) {
if (WIFEXITED(rc))
return WEXITSTATUS(rc);
fprintf(stderr, "Command signaled: %s\n", cmd);
return -1;
}
return 0;
2000-04-22 00:51:38 +02:00
}
2000-04-21 08:41:02 +02:00
/*
* This function handles the vvm target. After preprocessing, run the
* ivl translator to get C++, then run g++ to make an executable
* program out of that.
*/
static int t_vvm(char*cmd, unsigned ncmd)
2000-04-21 08:41:02 +02:00
{
int rc;
const char*pattern = lookup_pattern("<ivl>");
if (pattern == 0) {
fprintf(stderr, "No such target: %s\n", targ);
return -1;
}
tmp[0] = ' ';
tmp[1] = '|';
tmp[2] = ' ';
rc = build_string(tmp+3, sizeof tmp - 3, pattern);
cmd = realloc(cmd, ncmd+3+rc+1);
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
{
char *t;
for (t = tmp; *t; t++)
{
if (*t == '/') *t = '\\';
}
}
#endif
strcpy(cmd+ncmd, tmp);
2001-05-20 17:09:39 +02:00
2000-04-21 08:41:02 +02:00
if (verbose_flag)
printf("translate: %s\n", cmd);
2000-04-21 08:41:02 +02:00
rc = system(cmd);
2000-04-21 08:41:02 +02:00
if (rc != 0) {
if (WIFEXITED(rc)) {
fprintf(stderr, "errors translating Verilog program.\n");
return WEXITSTATUS(rc);
} else {
fprintf(stderr, "Command signaled: %s\n", cmd);
return -1;
}
2000-04-21 08:41:02 +02:00
}
sprintf(tmp, "%s " RDYNAMIC " -fno-exceptions -o %s -I%s "
2000-10-28 19:27:59 +02:00
"-L%s %s.cc -lvvm -lvpip %s", CXX, opath, IVL_INC, IVL_LIB,
2000-05-02 01:55:22 +02:00
opath, DLLIB);
2000-04-21 08:41:02 +02:00
if (verbose_flag)
printf("compile: %s\n", tmp);
2000-04-21 08:41:02 +02:00
rc = system(tmp);
2000-04-21 08:41:02 +02:00
if (rc != 0) {
if (WIFEXITED(rc)) {
fprintf(stderr, "errors compiling translated program.\n");
return WEXITSTATUS(rc);
} else {
fprintf(stderr, "Command signaled: %s\n", tmp);
return -1;
}
2000-04-21 08:41:02 +02:00
}
sprintf(tmp, "%s.cc", opath);
unlink(tmp);
2000-04-21 08:41:02 +02:00
return 0;
}
static int t_xnf(char*cmd, unsigned ncmd)
2000-04-21 08:41:02 +02:00
{
int rc;
2001-05-20 17:09:39 +02:00
sprintf(tmp, " | %s%sepivl %s -o %s -txnf -Fcprop -Fsynth -Fsyn-rules "
"-Fnodangle -Fxnfio", base, sep,warning_flags, opath);
rc = strlen(tmp);
cmd = realloc(cmd, ncmd+rc+1);
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
{
char *t;
for (t = tmp; *t; t++)
{
if (*t == '/') *t = '\\';
}
}
#endif
strcpy(cmd+ncmd, tmp);
ncmd += rc;
2000-04-29 03:20:14 +02:00
if (f_list) {
rc = strlen(f_list);
cmd = realloc(cmd, ncmd+rc+1);
2000-05-14 21:41:52 +02:00
strcpy(cmd+ncmd, f_list);
2000-04-29 03:20:14 +02:00
ncmd += rc;
}
2000-07-29 19:58:20 +02:00
if (mtm) {
sprintf(tmp, " -T%s", mtm);
rc = strlen(tmp);
cmd = realloc(cmd, ncmd+rc+1);
strcpy(cmd+ncmd, tmp);
ncmd += rc;
}
if (npath) {
sprintf(tmp, " -N%s", npath);
rc = strlen(tmp);
cmd = realloc(cmd, ncmd+rc+1);
strcpy(cmd+ncmd, tmp);
ncmd += rc;
}
2000-04-23 23:14:32 +02:00
if (start) {
sprintf(tmp, " -s%s", start);
rc = strlen(tmp);
cmd = realloc(cmd, ncmd+rc+1);
strcpy(cmd+ncmd, tmp);
ncmd += rc;
2000-04-23 23:14:32 +02:00
}
sprintf(tmp, " -- -");
rc = strlen(tmp);
cmd = realloc(cmd, ncmd+rc+1);
strcpy(cmd+ncmd, tmp);
ncmd += rc;
2000-04-21 08:41:02 +02:00
if (verbose_flag)
printf("translate: %s\n", cmd);
rc = system(cmd);
if (rc != 0) {
if (WIFEXITED(rc)) {
fprintf(stderr, "errors translating Verilog program.\n");
return WEXITSTATUS(rc);
}
fprintf(stderr, "Command signaled: %s\n", cmd);
return -1;
}
2000-04-21 08:41:02 +02:00
return 0;
2000-04-21 08:41:02 +02:00
}
static void process_warning_switch(const char*name)
{
if (warning_flags[0] == 0)
strcpy(warning_flags, "-W");
if (strcmp(name,"all") == 0) {
strcat(warning_flags, "i");
} else if (strcmp(name,"implicit") == 0) {
if (! strchr(warning_flags+2, 'i'))
strcat(warning_flags, "i");
}
}
2000-04-21 08:41:02 +02:00
int main(int argc, char **argv)
{
const char*config_path = 0;
char*cmd;
unsigned ncmd;
2000-04-21 08:41:02 +02:00
int e_flag = 0;
int opt, idx;
char*cp;
2001-02-01 18:12:22 +01:00
while ((opt = getopt(argc, argv, "B:C:c:D:Ef:hI:m:N::o:p:Ss:T:t:vW:")) != EOF) {
2000-04-21 08:41:02 +02:00
switch (opt) {
case 'B':
base = optarg;
break;
case 'C':
config_path = optarg;
break;
case 'c':
command_filename = malloc(strlen(optarg)+1);
strcat(command_filename, optarg);
break;
case 'D':
if (def_list == 0) {
def_list = malloc(strlen(" -D")+strlen(optarg)+1);
strcpy(def_list, " -D");
strcat(def_list, optarg);
} else {
def_list = realloc(def_list, strlen(def_list)
+ strlen(" -D")
+ strlen(optarg) + 1);
strcat(def_list, " -D");
strcat(def_list, optarg);
}
break;
2000-04-21 08:41:02 +02:00
case 'E':
e_flag = 1;
break;
2000-04-29 03:20:14 +02:00
case 'f':
2001-01-20 20:02:04 +01:00
fprintf(stderr, "warning: The -f flag is moved to -p\n");
case 'p':
2000-04-29 03:20:14 +02:00
if (f_list == 0) {
2001-01-20 20:02:04 +01:00
f_list = malloc(strlen(" -p")+strlen(optarg)+1);
strcpy(f_list, " -p");
2000-04-29 03:20:14 +02:00
strcat(f_list, optarg);
} else {
f_list = realloc(f_list, strlen(f_list) +
2001-01-20 20:02:04 +01:00
strlen(" -p") +
2000-04-29 03:20:14 +02:00
strlen(optarg) + 1);
2001-01-20 20:02:04 +01:00
strcat(f_list, " -p");
2000-04-29 03:20:14 +02:00
strcat(f_list, optarg);
}
break;
case 'h':
fprintf(stderr, "%s\n", HELP);
return 1;
case 'I':
if (inc_list == 0) {
inc_list = malloc(strlen(" -I")+strlen(optarg)+1);
strcpy(inc_list, " -I");
strcat(inc_list, optarg);
} else {
inc_list = realloc(inc_list, strlen(inc_list)
+ strlen(" -I")
+ strlen(optarg) + 1);
strcat(inc_list, " -I");
strcat(inc_list, optarg);
}
break;
2000-05-17 05:53:29 +02:00
case 'm':
if (mod_list == 0) {
mod_list = malloc(strlen(" -m")+strlen(optarg)+1);
strcpy(mod_list, " -m");
strcat(mod_list, optarg);
} else {
mod_list = realloc(mod_list, strlen(mod_list)
+ strlen(" -m")
+ strlen(optarg) + 1);
strcat(mod_list, " -m");
strcat(mod_list, optarg);
}
break;
case 'N':
npath = optarg;
break;
2000-04-21 08:41:02 +02:00
case 'o':
opath = optarg;
break;
case 'S':
synth_flag = 1;
break;
2000-04-23 23:14:32 +02:00
case 's':
start = optarg;
break;
2000-07-29 19:58:20 +02:00
case 'T':
if (strcmp(optarg,"min") == 0) {
mtm = "min";
} else if (strcmp(optarg,"typ") == 0) {
mtm = "typ";
} else if (strcmp(optarg,"max") == 0) {
mtm = "max";
} else {
fprintf(stderr, "%s: invalid -T%s argument\n",
argv[0], optarg);
return 1;
}
break;
2000-04-21 08:41:02 +02:00
case 't':
targ = optarg;
break;
case 'v':
verbose_flag = 1;
break;
case 'W':
process_warning_switch(optarg);
break;
2000-04-21 08:41:02 +02:00
case '?':
default:
return 1;
}
}
if ((optind == argc) && !command_filename) {
2000-04-23 23:14:32 +02:00
fprintf(stderr, "%s: No input files.\n", argv[0]);
fprintf(stderr, "%s\n", HELP);
2000-04-23 23:14:32 +02:00
return 1;
}
2001-05-20 17:09:39 +02:00
#ifdef __MINGW32__
{
char basepath[1024],*s;
GetModuleFileName(NULL,basepath,1024);
/* Get to the end. Search back twice for backslashes */
s = basepath + strlen(basepath);
while (*s != '\\') s--; s--;
while (*s != '\\') s--;
strcpy(s,"\\lib\\ivl");
base = basepath;
}
#endif
/* Load the iverilog.conf file to get our substitution
strings. */
{ char path[1024];
FILE*fd;
if (config_path) {
strcpy(path, config_path);
} else {
2001-05-20 17:09:39 +02:00
sprintf(path, "%s%siverilog.conf", base,sep);
}
fd = fopen(path, "r");
if (fd == 0) {
fprintf(stderr, "Config file \"%s\" not found\n",path);
return 1;
}
reset_lexor(fd);
yyparse();
}
/* Start building the preprocess command line. */
2001-05-20 17:09:39 +02:00
sprintf(tmp, "%s%sivlpp %s%s", base,sep,
verbose_flag?" -v":"",
e_flag?"":" -L");
ncmd = strlen(tmp);
cmd = malloc(ncmd + 1);
2000-04-26 23:11:41 +02:00
strcpy(cmd, tmp);
if (inc_list) {
cmd = realloc(cmd, ncmd + strlen(inc_list) + 1);
strcat(cmd, inc_list);
ncmd += strlen(inc_list);
}
if (def_list) {
cmd = realloc(cmd, ncmd + strlen(def_list) + 1);
strcat(cmd, def_list);
ncmd += strlen(def_list);
}
/* If user supplied a command file, retain its contents -- this
process supersedes command line source files. */
if (command_filename != 0) {
if (( fp = fopen(command_filename, "r")) == NULL ) {
fprintf(stderr, "%s: Can't open %s\n",
argv[0], command_filename);
return 1;
} else {
/* Process file and skip over commented-out lines.
Skips over c-like comment as well as '//' or
'#' lines. */
sprintf(tmp, "");
while (fgets(line, MAXSIZE, fp) != NULL) {
if ( strstr(line, "*/") != NULL ) {
inside_c_comment = 0;
continue;
}
if (inside_c_comment || (strstr(line, "/*") != NULL)) {
inside_c_comment = 1;
continue;
}
if ( (line[0] != '/' || line[1] != '/')
&& line[0] != '#' ) {
strcat (tmp, " ");
strncat (tmp, line, (strlen(line)-1));
}
}
fclose(fp);
}
cmd = realloc(cmd, ncmd+strlen(tmp)+1);
2000-04-26 23:11:41 +02:00
strcpy(cmd+ncmd, tmp);
ncmd += strlen(tmp);
2000-04-21 08:41:02 +02:00
} else {
/* Add all verilog source files to the preprocess
command line. */
for (idx = optind ; idx < argc ; idx += 1) {
sprintf(tmp, " %s", argv[idx]);
cmd = realloc(cmd, ncmd+strlen(tmp)+1);
strcpy(cmd+ncmd, tmp);
ncmd += strlen(tmp);
}
}
2000-04-21 08:41:02 +02:00
/* If the -E flag was given on the command line, then all we
do is run the preprocessor and put the output where the
user wants it. */
if (e_flag) {
int rc;
2000-04-21 08:41:02 +02:00
if (strcmp(opath,"-") != 0) {
sprintf(tmp, " > %s", opath);
cmd = realloc(cmd, ncmd+strlen(tmp)+1);
strcpy(cmd+ncmd, tmp);
ncmd += strlen(tmp);
2000-04-21 08:41:02 +02:00
}
2000-04-21 08:41:02 +02:00
if (verbose_flag)
printf("preprocess: %s\n", cmd);
2000-04-21 08:41:02 +02:00
rc = system(cmd);
if (rc != 0) {
if (WIFEXITED(rc)) {
fprintf(stderr, "errors preprocessing Verilog program.\n");
return WEXITSTATUS(rc);
}
fprintf(stderr, "Command signaled: %s\n", cmd);
return -1;
}
return 0;
2000-04-21 08:41:02 +02:00
}
if (strcmp(targ,"vvm") == 0)
return t_vvm(cmd, ncmd);
2000-04-21 08:41:02 +02:00
else if (strcmp(targ,"xnf") == 0)
return t_xnf(cmd, ncmd);
2000-04-21 08:41:02 +02:00
else {
return t_default(cmd, ncmd);
2000-04-21 08:41:02 +02:00
}
return 0;
}
/*
* $Log: main.c,v $
2001-05-20 20:22:02 +02:00
* Revision 1.13 2001/05/20 18:22:02 steve
* Fix WIFEXITED macro.
*
* Revision 1.12 2001/05/20 18:06:57 steve
* local declares if the header is missing.
*
2001-05-20 17:09:39 +02:00
* Revision 1.11 2001/05/20 15:09:40 steve
* Mingw32 support (Venkat Iyer)
*
2001-05-17 05:14:26 +02:00
* Revision 1.10 2001/05/17 03:14:26 steve
* Update help message.
*
* Revision 1.9 2001/04/26 16:04:39 steve
* Handle missing or uninstalled .conf files.
*
2001-02-01 18:12:22 +01:00
* Revision 1.8 2001/02/01 17:12:22 steve
* Forgot to actually allow the -p flag.
*
2001-01-20 20:02:04 +01:00
* Revision 1.7 2001/01/20 19:02:05 steve
* Switch hte -f flag to the -p flag.
*
* Revision 1.6 2001/01/20 02:15:51 steve
* apologize for not supporting non-constant delays.
*
* Revision 1.5 2000/11/30 02:50:54 steve
* Add command file (-c) support from Nadim Shaikli.
*
2000-10-28 19:27:59 +02:00
* Revision 1.4 2000/10/28 17:28:16 steve
* Split vpip for everybody.
*
* Revision 1.3 2000/10/28 03:47:25 steve
* Use the conf file to generate the vvm ivl string.
*
* Revision 1.2 2000/10/28 03:45:47 steve
* Use the conf file to generate the vvm ivl string.
*
* Revision 1.1 2000/10/08 22:36:56 steve
* iverilog with an iverilog.conf configuration file.
2000-04-21 08:41:02 +02:00
*
*/