Skip to content

Commit

Permalink
-y flag for libyosys Python scripts
Browse files Browse the repository at this point in the history
This adds a Python equivalent to the `-c` option, where scripts importing `libyosys` can be imported and used.

Most of the work for this was already done to enable Python passes a couple years back, so this is a relatively small changeset.
  • Loading branch information
donn committed Aug 27, 2024
1 parent ff41a95 commit b047cb7
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions kernel/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ int main(int argc, char **argv)
std::string topmodule = "";
std::string perffile = "";
bool scriptfile_tcl = false;
bool scriptfile_python = false;
bool print_banner = true;
bool print_stats = true;
bool call_abort = false;
Expand Down Expand Up @@ -305,6 +306,11 @@ int main(int argc, char **argv)
printf("\n");
printf(" -C\n");
printf(" enters TCL interatcive shell mode\n");
#endif
#ifdef WITH_PYTHON
printf("\n");
printf(" -y python_scriptfile\n");
printf(" execute the python script");
#endif
printf("\n");
printf(" -p command\n");
Expand Down Expand Up @@ -379,7 +385,7 @@ int main(int argc, char **argv)
}

int opt;
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)
while ((opt = getopt(argc, argv, "MXAQTVCSgm:f:Hh:b:o:p:l:L:qv:tds:c:y:W:w:e:r:D:P:E:x:B:")) != -1)
{
switch (opt)
{
Expand Down Expand Up @@ -464,11 +470,19 @@ int main(int argc, char **argv)
case 's':
scriptfile = optarg;
scriptfile_tcl = false;
scriptfile_python = false;
run_shell = false;
break;
case 'c':
scriptfile = optarg;
scriptfile_tcl = true;
scriptfile_python = false;
run_shell = false;
break;
case 'y':
scriptfile = optarg;
scriptfile_tcl = false;
scriptfile_python = true;
run_shell = false;
break;
case 'W':
Expand Down Expand Up @@ -607,8 +621,9 @@ int main(int argc, char **argv)
run_pass(vdef_cmd);
}

if (scriptfile.empty() || !scriptfile_tcl) {
// Without a TCL script, arguments following '--' are also treated as frontend files
if (scriptfile.empty() || (!scriptfile_tcl && !scriptfile_python)) {
// Without a TCL or Python script, arguments following '--' are also
// treated as frontend files
for (int i = optind; i < argc; ++i)
frontend_files.push_back(argv[i]);
}
Expand Down Expand Up @@ -636,7 +651,28 @@ int main(int argc, char **argv)
if (Tcl_EvalFile(interp, scriptfile.c_str()) != TCL_OK)
log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
#else
log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
log_error("Can't execute TCL script: this version of yosys is not built with TCL support enabled.\n");
#endif
} else if (scriptfile_python) {
#ifdef WITH_PYTHON
PyObject *sys = PyImport_ImportModule("sys");
PyObject *new_argv = PyList_New(argc - optind + 1);
PyList_SetItem(new_argv, 0, PyUnicode_FromString(scriptfile.c_str()));
for (int i = optind; i < argc; ++i)
PyList_SetItem(new_argv, i - optind + 1, PyUnicode_FromString(argv[i]));

PyObject *old_argv = PyObject_GetAttrString(sys, "argv");
PyObject_SetAttrString(sys, "argv", new_argv);
Py_DECREF(old_argv);

FILE *scriptfp = fopen(scriptfile.c_str(), "r");
if (PyRun_SimpleFile(scriptfp, scriptfile.c_str()) != 0) {
log_error("Python interpreter encountered an error:\n");
log_flush();
PyErr_Print();
}
#else
log_error("Can't execute Python script: this version of yosys is not built with Python support enabled.\n");
#endif
} else
run_frontend(scriptfile, "script");
Expand Down

0 comments on commit b047cb7

Please sign in to comment.