From 2450e6be2223b4895bfa239364996c882c0229af Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 25 Nov 2022 16:18:02 +0100 Subject: [PATCH] Add TCL interactive shell mode --- kernel/driver.cc | 30 +++++++++++++++++++++++++----- kernel/yosys.cc | 12 +++++++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/kernel/driver.cc b/kernel/driver.cc index 6d996783e..aa90802c9 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -202,6 +202,11 @@ extern char *yosys_argv0; extern char yosys_path[PATH_MAX]; }; #endif +#ifdef YOSYS_ENABLE_TCL +namespace Yosys { + extern int yosys_tcl_iterp_init(Tcl_Interp *interp); +}; +#endif int main(int argc, char **argv) { @@ -221,6 +226,7 @@ int main(int argc, char **argv) bool call_abort = false; bool timing_details = false; bool run_shell = true; + bool run_tcl_shell = false; bool mode_v = false; bool mode_q = false; @@ -284,6 +290,9 @@ int main(int argc, char **argv) printf("\n"); printf(" -c tcl_scriptfile\n"); printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n"); + printf("\n"); + printf(" -C\n"); + printf(" enters TCL interatcive shell mode\n"); #endif printf("\n"); printf(" -p command\n"); @@ -358,7 +367,7 @@ int main(int argc, char **argv) } int opt; - while ((opt = getopt(argc, argv, "MXAQTVSgm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:r:D:P:E:x:B:")) != -1) + while ((opt = getopt(argc, argv, "MXAQTVCSgm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:r:D:P:E:x:B:")) != -1) { switch (opt) { @@ -496,6 +505,9 @@ int main(int argc, char **argv) case 'B': perffile = optarg; break; + case 'C': + run_tcl_shell = true; + break; default: fprintf(stderr, "Run '%s -h' for help.\n", argv[0]); exit(1); @@ -570,10 +582,18 @@ int main(int argc, char **argv) for (auto it = passes_commands.begin(); it != passes_commands.end(); it++) run_pass(*it); - if (run_shell) - shell(yosys_design); - else - run_backend(output_filename, backend_command); + if (run_tcl_shell) { +#ifdef YOSYS_ENABLE_TCL + Tcl_Main(argc, argv, yosys_tcl_iterp_init); +#else + log_error("Can't exectue TCL shell: this version of yosys is not built with TCL support enabled.\n"); +#endif + } else { + if (run_shell) + shell(yosys_design); + else + run_backend(output_filename, backend_command); + } yosys_design->check(); for (auto it : saved_designs) diff --git a/kernel/yosys.cc b/kernel/yosys.cc index a56a066fe..4a22d0a7b 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -740,13 +740,19 @@ static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *a return TCL_OK; } +int yosys_tcl_iterp_init(Tcl_Interp *interp) +{ + if (Tcl_Init(interp)!=TCL_OK) + log_warning("Tcl_Init() call failed - %s\n",Tcl_ErrnoMsg(Tcl_GetErrno())); + Tcl_CreateCommand(interp, "yosys", tcl_yosys_cmd, NULL, NULL); + return TCL_OK ; +} + extern Tcl_Interp *yosys_get_tcl_interp() { if (yosys_tcl_interp == NULL) { yosys_tcl_interp = Tcl_CreateInterp(); - if (Tcl_Init(yosys_tcl_interp)!=TCL_OK) - log_warning("Tcl_Init() call failed - %s\n",Tcl_ErrnoMsg(Tcl_GetErrno())); - Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL); + yosys_tcl_iterp_init(yosys_tcl_interp); } return yosys_tcl_interp; }