From 45f45f73b7f00facfc01965fa101422c2d68b6a2 Mon Sep 17 00:00:00 2001 From: steve Date: Wed, 9 Dec 1998 04:02:47 +0000 Subject: [PATCH] Support the include directive. --- lexor.lex | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++---- main.cc | 16 ++++------ pform.cc | 20 +++++++++++-- pform.h | 7 +++-- 4 files changed, 109 insertions(+), 21 deletions(-) diff --git a/lexor.lex b/lexor.lex index 70db0c98a..bdd4cb7b3 100644 --- a/lexor.lex +++ b/lexor.lex @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #if !defined(WINNT) -#ident "$Id: lexor.lex,v 1.5 1998/11/25 02:35:53 steve Exp $" +#ident "$Id: lexor.lex,v 1.6 1998/12/09 04:02:47 steve Exp $" #endif //# define YYSTYPE lexval @@ -30,7 +30,7 @@ # include extern FILE*vl_input; -extern const char*vl_file; +extern string vl_file; # define YY_USER_INIT reset_lexor(); # define yylval VLlval @@ -38,17 +38,18 @@ extern YYLTYPE yylloc; static void reset_lexor(); static int check_identifier(const char*name); +static void ppinclude_filename(); +static void ppdo_include(); static verinum*make_sized_binary(const char*txt); static verinum*make_sized_octal(const char*txt); static verinum*make_sized_hex(const char*txt); %} -%option noyywrap - %x CCOMMENT %x CSTRING %s UDPTABLE +%x PPINCLUDE %% @@ -138,6 +139,20 @@ static verinum*make_sized_hex(const char*txt); delete[]bits; return NUMBER; } +`include { + BEGIN(PPINCLUDE); } + +\"[^\"]*\" { + ppinclude_filename(); } + +[ \t\b\f\r] { ; } + +\n { + BEGIN(0); + yylloc.first_line += 1; + ppdo_include(); } + + . { cerr << yylloc.first_line << ": unmatched character ("; if (isgraph(yytext[0])) cerr << yytext[0]; @@ -449,9 +464,71 @@ static verinum*make_sized_hex(const char*txt) return new verinum(bits, size); } +struct include_stack_t { + string path; + FILE*file; + unsigned lineno; + YY_BUFFER_STATE yybs; + + struct include_stack_t*next; +}; + +static include_stack_t*include_stack = 0; + +static string ppinclude_path; + +static void ppinclude_filename() +{ + ppinclude_path = yytext+1; + ppinclude_path = ppinclude_path.substr(0, ppinclude_path.length()-1); +} + +static void ppdo_include() +{ + FILE*file = fopen(ppinclude_path.c_str(), "r"); + if (file == 0) { + cerr << ppinclude_path << ": Unable to open include file." << endl; + return; + } + + include_stack_t*isp = new include_stack_t; + isp->path = vl_file; + isp->file = vl_input; + isp->lineno = yylloc.first_line; + isp->next = include_stack; + isp->yybs = YY_CURRENT_BUFFER; + + vl_file = ppinclude_path; + vl_input = file; + yylloc.first_line = 1; + yylloc.text = vl_file.c_str(); + yy_switch_to_buffer(yy_new_buffer(vl_input, YY_BUF_SIZE)); + include_stack = isp; +} + +static int yywrap() +{ + include_stack_t*isp = include_stack; + if (isp == 0) + return 1; + + yy_delete_buffer(YY_CURRENT_BUFFER); + fclose(vl_input); + + vl_input = isp->file; + vl_file = isp->path; + yy_switch_to_buffer(isp->yybs); + yylloc.first_line = isp->lineno; + yylloc.text = vl_file.c_str(); + include_stack = isp->next; + delete isp; + return 0; +} + static void reset_lexor() { yyrestart(vl_input); + include_stack = 0; yylloc.first_line = 1; - yylloc.text = vl_file; + yylloc.text = vl_file.c_str(); } diff --git a/main.cc b/main.cc index 8ea184545..19f994767 100644 --- a/main.cc +++ b/main.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #if !defined(WINNT) -#ident "$Id: main.cc,v 1.9 1998/12/07 04:53:17 steve Exp $" +#ident "$Id: main.cc,v 1.10 1998/12/09 04:02:47 steve Exp $" #endif # include @@ -32,7 +32,6 @@ extern void pform_parse(); -const char*vl_file = ""; const char*target = "verilog"; string start_module = ""; @@ -151,18 +150,10 @@ int main(int argc, char*argv[]) return 1; } - /* Open the input (source) file. */ - vl_file = argv[optind]; - FILE*input = fopen(vl_file, "r"); - if (input == 0) { - cerr << "Unable to open " < modules; map primitives; - int rc = pform_parse(input, modules, primitives); + int rc = pform_parse(argv[optind], modules, primitives); if (rc) { return rc; @@ -231,6 +222,9 @@ int main(int argc, char*argv[]) /* * $Log: main.cc,v $ + * Revision 1.10 1998/12/09 04:02:47 steve + * Support the include directive. + * * Revision 1.9 1998/12/07 04:53:17 steve * Generate OBUF or IBUF attributes (and the gates * to garry them) where a wire is a pad. This involved diff --git a/pform.cc b/pform.cc index 1c14fa2dc..07c4c8a31 100644 --- a/pform.cc +++ b/pform.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #if !defined(WINNT) -#ident "$Id: pform.cc,v 1.6 1998/12/01 00:42:14 steve Exp $" +#ident "$Id: pform.cc,v 1.7 1998/12/09 04:02:47 steve Exp $" #endif # include "pform.h" @@ -28,6 +28,11 @@ # include # include +/* + * The lexor accesses the vl_* variables. + */ +string vl_file = ""; + extern int VLparse(); static Module*cur_module = 0; @@ -463,10 +468,16 @@ Statement* pform_make_calltask(string*name, list*parms) } FILE*vl_input = 0; -int pform_parse(FILE*input, map&modules, +int pform_parse(const char*path, map&modules, map&prim) { - vl_input = input; + vl_file = path; + vl_input = fopen(path, "r"); + if (vl_input == 0) { + cerr << "Unable to open " <&modules, /* * $Log: pform.cc,v $ + * Revision 1.7 1998/12/09 04:02:47 steve + * Support the include directive. + * * Revision 1.6 1998/12/01 00:42:14 steve * Elaborate UDP devices, * Support UDP type attributes, and diff --git a/pform.h b/pform.h index 76a448f4e..d83337e0a 100644 --- a/pform.h +++ b/pform.h @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #if !defined(WINNT) -#ident "$Id: pform.h,v 1.4 1998/12/01 00:42:14 steve Exp $" +#ident "$Id: pform.h,v 1.5 1998/12/09 04:02:47 steve Exp $" #endif # include "netlist.h" @@ -126,12 +126,15 @@ extern void pform_make_pgassign(const string&lval, PExpr*sel, PExpr*rval); * parses the source file and places all the modules it finds into the * mod list. The dump function dumps a module to the output stream. */ -extern int pform_parse(FILE*, map&mod, +extern int pform_parse(const char*path, map&mod, map&prim); extern void pform_dump(ostream&out, Module*mod); /* * $Log: pform.h,v $ + * Revision 1.5 1998/12/09 04:02:47 steve + * Support the include directive. + * * Revision 1.4 1998/12/01 00:42:14 steve * Elaborate UDP devices, * Support UDP type attributes, and