-- GHDL Run Time (GRT) - VHDL files subprograms. -- Copyright (C) 2002 - 2014 Tristan Gingold -- -- GHDL is free software; you can redistribute it and/or modify it under -- the terms of the GNU General Public License as published by the Free -- Software Foundation; either version 2, or (at your option) any later -- version. -- -- GHDL 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 GCC; see the file COPYING. If not, write to the Free -- Software Foundation, 59 Temple Place - Suite 330, Boston, MA -- 02111-1307, USA. -- -- As a special exception, if other files instantiate generics from this -- unit, or you link this unit with other files to produce an executable, -- this unit does not by itself cause the resulting executable to be -- covered by the GNU General Public License. This exception does not -- however invalidate any other reasons why the executable file might be -- covered by the GNU Public License. with Grt.Stdio; use Grt.Stdio; with Grt.C; use Grt.C; with Grt.Table; with System; use System; pragma Elaborate_All (Grt.Table); package body Grt.Files_Operations is subtype C_Files is Grt.Stdio.FILEs; Auto_Flush : constant Boolean := False; type File_Entry_Type is record -- The corresponding C stream. Stream : C_Files; Signature : Ghdl_C_String; -- Open kind: r, a or w. Kind : Character; Is_Text : Boolean; -- True if the file entry is used. Is_Alive : Boolean; end record; package Files_Table is new Grt.Table (Table_Component_Type => File_Entry_Type, Table_Index_Type => Ghdl_File_Index, Table_Low_Bound => 1, Table_Initial => 2); -- Get the C stream for INDEX. procedure Get_File (Index : Ghdl_File_Index; Res : out C_Files; Status : out Op_Status) is begin if Index not in Files_Table.First .. Files_Table.Last then Status := Op_Bad_Index; else Status := Op_Ok; Res := Files_Table.Table (Index).Stream; end if; end Get_File; -- Assume INDEX is correct. function Is_Open (Index : Ghdl_File_Index) return Boolean is begin return Files_Table.Table (Index).Stream /= NULL_Stream; end Is_Open; -- Assume INDEX is correct. function Get_Kind (Index : Ghdl_File_Index) return Character is begin return Files_Table.Table (Index).Kind; end Get_Kind; procedure Check_File_Mode (Index : Ghdl_File_Index; Is_Text : Boolean; Status : out Op_Status) is begin if Files_Table.Table (Index).Is_Text /= Is_Text then Status := Op_Bad_Mode; else Status := Op_Ok; end if; end Check_File_Mode; procedure Check_Read (Index : Ghdl_File_Index; Is_Text : Boolean; Status : out Op_Status) is begin Check_File_Mode (Index, Is_Text, Status); if Status /= Op_Ok then return; end if; -- LRM08 5.5.2 File operations -- It is an error if the access mode of the file object is write-only -- or if the file object is not open. if not Is_Open (Index) then Status := Op_Not_Open; return; end if; if Get_Kind (Index) /= 'r' then Status := Op_Read_Write_File; return; end if; Status := Op_Ok; end Check_Read; procedure Check_Write (Index : Ghdl_File_Index; Is_Text : Boolean; Status : out Op_Status) is begin Check_File_Mode (Index, Is_Text, Status); if Status /= Op_Ok then return; end if; -- LRM08 5.5.2 File operations -- It is an error if the access mode of the file object is read-only -- or if the file object is not open. if not Is_Open (Index) then Status := Op_Not_Open; return; end if; if Get_Kind (Index) = 'r' then Status := Op_Write_Read_File; return; end if; Status := Op_Ok; end Check_Write; function Create_File (Is_Text : Boolean; Kind : Character; Sig : Ghdl_C_String) return Ghdl_File_Index is begin Files_Table.Append ((Stream => NULL_Stream, Signature => Sig, Kind => Kind, Is_Text => Is_Text, Is_Alive => True)); return Files_Table.Last; end Create_File; procedure Destroy_File (Is_Text : Boolean; Index : Ghdl_File_Index; Status : out Op_Status) is Cstream : C_Files; begin Get_File (Index, Cstream, Status); if Status /= Op_Ok then return; end if; if Cstream /= NULL_Stream then Status := Op_Not_Closed; return; end if; Check_File_Mode (Index, Is_Text, Status); if Status /= Op_Ok then return; end if; -- Cleanup. Files_Table.Table (Index).Is_Alive := False; if Index = Files_Table.Last then while Files_Table.Last >= Files_Table.First and then Files_Table.Table (Files_Table.Last).Is_Alive = False loop Files_Table.Decrement_Last; end loop; end if; end Destroy_File; function Ghdl_Text_File_Elaborate return Ghdl_File_Index is begin return Create_File (True, ' ', null); end Ghdl_Text_File_Elaborate; function Ghdl_File_Elaborate (Sig : Ghdl_C_String) return Ghdl_File_Index is begin return Create_File (False, ' ', Sig); end Ghdl_File_Elaborate; procedure Ghdl_Text_File_Finalize (File : Ghdl_File_Index; Status : out Op_Status) is begin Destroy_File (True, File, Status); end Ghdl_Text_File_Finalize; procedure Ghdl_File_Finalize (File : Ghdl_File_Index; Status : out Op_Status) is begin Destroy_File (False, File, Status); end Ghdl_File_Finalize; procedure Ghdl_File_Endfile (File : Ghdl_File_Index; Status : out Op_Status) is Stream : C_Files; C : int; begin Get_File (File, Stream, Status); if Status /= Op_Ok then return; end if; -- LRM93 3.4.1 File Operations -- LRM08 5.5.2 File Operations -- It is an error if ENDFILE is called on a file object that is not -- open. if Stream = NULL_Stream then Status := Op_Not_Open; return; end if; -- Default: returns True. Status := Op_End_Of_File; -- LRM93 3.4.1 File Operations -- LRM08 5.5.2 File Operations -- Function ENDFILE always returns TRUE for an open file object whose -- access mode is write-only. if Get_Kind (File) /= 'r' then return; end if; if feof (Stream) /= 0 then return; end if; C := fgetc (Stream); if C < 0 then return; end if; if ungetc (C, Stream) /= C then Status := Op_Ungetc_Error; return; end if; Status := Op_Ok; return; end Ghdl_File_Endfile; Sig_Header : constant String := "#GHDL-BINARY-FILE-0.0" & Nl; Std_Output_Name : constant String := "STD_OUTPUT" & NUL; Std_Input_Name : constant String := "STD_INPUT" & NUL; procedure File_Open (File : Ghdl_File_Index; Mode : Ghdl_I32; Name : Ghdl_C_String; Status : out Op_Status) is Str_Mode : String (1 .. 3); F : C_Files; Sig : Ghdl_C_String; Sig_Len : Natural; Kind : Character; begin Get_File (File, F, Status); if Status /= Op_Ok then return; end if; if F /= NULL_Stream then -- File was already open. Status := Op_Not_Closed; return; end if; case Mode is when Read_Mode => Kind := 'r'; when Write_Mode => Kind := 'w'; when Append_Mode => Kind := 'a'; when others => -- Bad mode, cannot happen. Status := Op_Bad_Mode; return; end case; if Strcmp (Name, To_Ghdl_C_String (Std_Input_Name'Address)) = 0 then if Mode /= Read_Mode then Status := Op_Mode_Error; return; end if; F := stdin; elsif Strcmp (Name, To_Ghdl_C_String (Std_Output_Name'Address)) = 0 then if Mode /= Write_Mode then Status := Op_Mode_Error; return; end if; F := stdout; else Str_Mode (1) := Kind; if Files_Table.Table (File).Is_Text then Str_Mode (2) := NUL; else Str_Mode (2) := 'b'; Str_Mode (3) := NUL; end if; F := fopen (To_Address (Name), Str_Mode'Address); if F = NULL_Stream then Status := Op_Name_Error; return; end if; -- if Grt.Options.Unbuffered_Writes and Mode /= Read_Mode then -- setbuf (F, NULL_voids); -- end if; end if; Sig := Files_Table.Table (File).Signature; if Sig /= null then Sig_Len := strlen (Sig); case Mode is when Write_Mode => if fwrite (Sig_Header'Address, 1, Sig_Header'Length, F) /= Sig_Header'Length then Status := Op_Write_Error; return; end if; if fwrite (Sig (1)'Address, 1, size_t (Sig_Len), F) /= size_t (Sig_Len) then Status := Op_Write_Error; return; end if; when Read_Mode => declare Hdr : String (1 .. Sig_Header'Length); Sig_Buf : String (1 .. Sig_Len); begin if fread (Hdr'Address, 1, Hdr'Length, F) /= Hdr'Length then Status := Op_Read_Error; return; end if; if Hdr /= Sig_Header then Status := Op_Signature_Error; return; end if; if fread (Sig_Buf'Address, 1, Sig_Buf'Length, F) /= Sig_Buf'Length then Status := Op_Read_Error; return; end if; if Sig_Buf /= Sig (1 .. Sig_Len) then Status := Op_Signature_Error; return; end if; end; when Append_Mode => null; when others => null; end case; end if; Files_Table.Table (File).Stream := F; Files_Table.Table (File).Kind := Kind; Status := Op_Ok; end File_Open; procedure Ghdl_Text_File_Open (File : Ghdl_File_Index; Mode : Ghdl_I32; Name : Ghdl_C_String; Status : out Op_Status) is begin Check_File_Mode (File, True, Status); if Status /= Op_Ok then return; end if; File_Open (File, Mode, Name, Status); end Ghdl_Text_File_Open; procedure Ghdl_File_Open (File : Ghdl_File_Index; Mode : Ghdl_I32; Name : Ghdl_C_String; Status : out Op_Status) is begin Check_File_Mode (File, False, Status); if Status /= Op_Ok then return; end if; File_Open (File, Mode, Name, Status); end Ghdl_File_Open; procedure Ghdl_Text_Write (File : Ghdl_File_Index; Str : Std_String_Ptr; Status : out Op_Status) is Res : C_Files; Len : size_t; R : size_t; begin Get_File (File, Res, Status); if Status /= Op_Ok then return; end if; Check_Write (File, True, Status); if Status /= Op_Ok then return; end if; Len := size_t (Str.Bounds.Dim_1.Length); if Len = 0 then Status := Op_Ok; return; end if; R := fwrite (Str.Base (0)'Address, Len, 1, Res); if R /= 1 then Status := Op_Write_Error; return; end if; if Auto_Flush then fflush (Res); end if; Status := Op_Ok; end Ghdl_Text_Write; procedure Ghdl_Write_Scalar (File : Ghdl_File_Index; Ptr : Ghdl_Ptr; Length : Ghdl_Index_Type; Status : out Op_Status) is Res : C_Files; R : size_t; begin Get_File (File, Res, Status); if Status /= Op_Ok then return; end if; Check_Write (File, False, Status); if Status /= Op_Ok then return; end if; R := fwrite (System.Address (Ptr), size_t (Length), 1, Res); if R /= 1 then Status := Op_Write_Error; return; end if; if Auto_Flush then fflush (Res); end if; Status := Op_Ok; end Ghdl_Write_Scalar; procedure Ghdl_Read_Scalar (File : Ghdl_File_Index; Ptr : Ghdl_Ptr; Length : Ghdl_Index_Type; Status : out Op_Status) is Res : C_Files; R : size_t; begin Get_File (File, Res, Status); if Status /= Op_Ok then return; end if; Check_Read (File, False, Status); if Status /= Op_Ok then return; end if; R := fread (System.Address (Ptr), size_t (Length), 1, Res); if R /= 1 then Status := Op_Read_Error; return; end if; Status := Op_Ok; end Ghdl_Read_Scalar; procedure Ghdl_Text_Read_Length (File : Ghdl_File_Index; Str : Std_String_Ptr; Status : out Op_Status; Length : out Std_Integer) is Stream : C_Files; C : int; Len : Ghdl_Index_Type; begin Length := 0; Get_File (File, Stream, Status); if Status /= Op_Ok then return; end if; Check_Read (File, True, Status); if Status /= Op_Ok then return; end if; Len := Str.Bounds.Dim_1.Length; -- Read until EOL (or EOF). -- Store as much as possible. for I in Ghdl_Index_Type loop C := fgetc (Stream); if C < 0 then Length := Std_Integer (I); Status := Op_End_Of_File; return; end if; if I < Len then Str.Base (I) := Character'Val (C); end if; -- End of line is '\n' or LF or character # 10. if C = 10 then Length := Std_Integer (I + 1); Status := Op_Ok; return; end if; end loop; Length := 0; Status := Op_Ok; end Ghdl_Text_Read_Length; procedure Ghdl_Untruncated_Text_Read (File : Ghdl_File_Index; Buf : Ghdl_C_String; Len : in out Std_Integer; Status : out Op_Status) is Stream : C_Files; Max_Len : int; begin Get_File (File, Stream, Status); if Status /= Op_Ok then return; end if; Check_Read (File, True, Status); if Status /= Op_Ok then return; end if; Max_Len := int (Len); if fgets (To_Address (Buf), Max_Len, Stream) = Null_Address then Status := Op_End_Of_File; return; end if; -- Compute the length. Len := Std_Integer (strlen (Buf)); Status := Op_Ok; end Ghdl_Untruncated_Text_Read; procedure File_Close (File : Ghdl_File_Index; Is_Text : Boolean; Status : out Op_Status) is Stream : C_Files; begin Get_File (File, Stream, Status); if Status /= Op_Ok then return; end if; Check_File_Mode (File, Is_Text, Status); if Status /= Op_Ok then return; end if; -- LRM 3.4.1 File Operations -- If F is not associated with an external file, then FILE_CLOSE has -- no effect. if Stream = NULL_Stream then Status := Op_Ok; return; end if; if fclose (Stream) /= 0 then Status := Op_Close_Error; return; end if; Files_Table.Table (File).Stream := NULL_Stream; Status := Op_Ok; end File_Close; procedure Ghdl_Text_File_Close (File : Ghdl_File_Index; Status : out Op_Status) is begin File_Close (File, True, Status); end Ghdl_Text_File_Close; procedure Ghdl_File_Close (File : Ghdl_File_Index; Status : out Op_Status) is begin File_Close (File, False, Status); end Ghdl_File_Close; procedure Ghdl_File_Flush (File : Ghdl_File_Index; Status : out Op_Status) is Stream : C_Files; begin Get_File (File, Stream, Status); if Status /= Op_Ok then return; end if; -- LRM08 5.5.2 File Operations -- For the WRITE and FLUSH procedures, it is an error if the access -- mode of the file object is read-only or if the file is not open. if Stream = NULL_Stream then Status := Op_Not_Open; return; end if; if Get_Kind (File) = 'r' then Status := Op_Write_Read_File; return; end if; fflush (Stream); Status := Op_Ok; end Ghdl_File_Flush; end Grt.Files_Operations; ef='#n452'>452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <dlfcn.h>
#include <limits.h>
#include <errno.h>
#include <algorithm>
#include "kernel/rtlil.h"
#include "kernel/register.h"
#include "kernel/log.h"
bool fgetline(FILE *f, std::string &buffer)
{
buffer = "";
char block[4096];
while (1) {
if (fgets(block, 4096, f) == NULL)
return false;
buffer += block;
if (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r')) {
while (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r'))
buffer.resize(buffer.size()-1);
return true;
}
}
}
static void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command)
{
if (command == "auto") {
if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
command = "verilog";
else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
command = "ilang";
else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".ys")
command = "script";
else if (filename == "-")
command = "script";
else
log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename.c_str());
}
if (command == "script") {
log("\n-- Executing script file `%s' --\n", filename.c_str());
FILE *f = stdin;
if (filename != "-")
f = fopen(filename.c_str(), "r");
if (f == NULL)
log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
std::string command;
while (fgetline(f, command)) {
while (!command.empty() && command[command.size()-1] == '\\') {
std::string next_line;
if (!fgetline(f, next_line))
break;
command.resize(command.size()-1);
command += next_line;
}
Pass::call(design, command);
}
if (!command.empty())
Pass::call(design, command);
if (filename != "-")
fclose(f);
if (backend_command != NULL && *backend_command == "auto")
*backend_command = "";
return;
}
if (filename == "-") {
log("\n-- Parsing stdin using frontend `%s' --\n", command.c_str());
} else {
log("\n-- Parsing `%s' using frontend `%s' --\n", filename.c_str(), command.c_str());
}
Frontend::frontend_call(design, NULL, filename, command);
}
static void run_pass(std::string command, RTLIL::Design *design)
{
log("\n-- Running pass `%s' --\n", command.c_str());
Pass::call(design, command);
}
static void run_backend(std::string filename, std::string command, RTLIL::Design *design)
{
if (command == "auto") {
if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
command = "verilog";
else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
command = "ilang";
else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".blif")
command = "blif";
else if (filename == "-")
command = "ilang";
else if (filename.empty())
return;
else
log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename.c_str());
}
if (filename.empty())
filename = "-";
if (filename == "-") {
log("\n-- Writing to stdout using backend `%s' --\n", command.c_str());
} else {
log("\n-- Writing to `%s' using backend `%s' --\n", filename.c_str(), command.c_str());
}
Backend::backend_call(design, NULL, filename, command);
}
static char *readline_cmd_generator(const char *text, int state)
{
static std::map<std::string, Pass*>::iterator it;
static int len;
if (!state) {
it = REGISTER_INTERN::pass_register.begin();
len = strlen(text);
}
for (; it != REGISTER_INTERN::pass_register.end(); it++) {
if (it->first.substr(0, len) == text)
return strdup((it++)->first.c_str());
}
return NULL;
}
static char *readline_obj_generator(const char *text, int state)
{
static std::vector<char*> obj_names;
static size_t idx;
if (!state)
{
idx = 0;
obj_names.clear();
RTLIL::Design *design = yosys_get_design();
int len = strlen(text);
if (design->selected_active_module.empty())
{
for (auto &it : design->modules)
if (RTLIL::unescape_id(it.first).substr(0, len) == text)
obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
}
else
if (design->modules.count(design->selected_active_module) > 0)
{
RTLIL::Module *module = design->modules.at(design->selected_active_module);
for (auto &it : module->wires)
if (RTLIL::unescape_id(it.first).substr(0, len) == text)
obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
for (auto &it : module->memories)
if (RTLIL::unescape_id(it.first).substr(0, len) == text)
obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
for (auto &it : module->cells)
if (RTLIL::unescape_id(it.first).substr(0, len) == text)
obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
for (auto &it : module->processes)
if (RTLIL::unescape_id(it.first).substr(0, len) == text)
obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
}
std::sort(obj_names.begin(), obj_names.end());
}
if (idx < obj_names.size())
return strdup(obj_names[idx++]);
idx = 0;
obj_names.clear();
return NULL;
}
static char **readline_completion(const char *text, int start, int)
{
if (start == 0)
return rl_completion_matches(text, readline_cmd_generator);
if (strncmp(rl_line_buffer, "read_", 5) && strncmp(rl_line_buffer, "write_", 6))
return rl_completion_matches(text, readline_obj_generator);
return NULL;
}
const char *create_prompt(RTLIL::Design *design, int recursion_counter)
{
static char buffer[100];
std::string str = "\n";
if (recursion_counter > 1)
str += stringf("(%d) ", recursion_counter);
str += "yosys";
if (!design->selected_active_module.empty())
str += stringf(" [%s]", RTLIL::id2cstr(design->selected_active_module));
if (!design->selection_stack.back().full_selection) {
if (design->selected_active_module.empty())
str += "*";
else if (design->selection_stack.back().selected_modules.size() != 1 || design->selection_stack.back().selected_members.size() != 0 ||
design->selection_stack.back().selected_modules.count(design->selected_active_module) == 0)
str += "*";
}
snprintf(buffer, 100, "%s> ", str.c_str());
return buffer;
}
static void shell(RTLIL::Design *design)
{
static int recursion_counter = 0;
recursion_counter++;
log_cmd_error_throw = true;
rl_readline_name = "yosys";
rl_attempted_completion_function = readline_completion;
rl_basic_word_break_characters = " \t\n";
char *command = NULL;
while ((command = readline(create_prompt(design, recursion_counter))) != NULL)
{
if (command[strspn(command, " \t\r\n")] == 0)
continue;
add_history(command);
char *p = command + strspn(command, " \t\r\n");
if (!strncmp(p, "exit", 4)) {
p += 4;
p += strspn(p, " \t\r\n");
if (*p == 0)
break;
}
try {
assert(design->selection_stack.size() == 1);
Pass::call(design, command);
} catch (int) {
while (design->selection_stack.size() > 1)
design->selection_stack.pop_back();
log_reset_stack();
}
}
if (command == NULL)
printf("exit\n");
recursion_counter--;
log_cmd_error_throw = false;
}
struct ShellPass : public Pass {
ShellPass() : Pass("shell", "enter interactive command mode") { }
virtual void help() {
log("\n");
log(" shell\n");
log("\n");
log("This command enters the interactive command mode. This can be useful\n");
log("in a script to interrupt the script at a certain point and allow for\n");
log("interactive inspection or manual synthesis of the design at this point.\n");
log("\n");
log("The command prompt of the interactive shell indicates the current\n");
log("selection (see 'help select'):\n");
log("\n");
log(" yosys>\n");
log(" the entire design is selected\n");
log("\n");
log(" yosys*>\n");
log(" only part of the design is selected\n");
log("\n");
log(" yosys [modname]>\n");
log(" the entire module 'modname' is selected using 'select -module modname'\n");
log("\n");
log(" yosys [modname]*>\n");
log(" only part of current module 'modname' is selected\n");
log("\n");
log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
log("do not terminate yosys but return to the command prompt.\n");
log("\n");
log("This command is the default action if nothing else has been specified\n");
log("on the command line.\n");
log("\n");
log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
log("\n");
}
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
extra_args(args, 1, design, false);
shell(design);
}
} ShellPass;
struct HistoryPass : public Pass {
HistoryPass() : Pass("history", "show last interactive commands") { }
virtual void help() {
log("\n");
log(" history\n");
log("\n");
log("This command prints all commands in the shell history buffer. This are\n");
log("all commands executed in an interactive session, but not the commands\n");
log("from executed scripts.\n");
log("\n");
}
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
extra_args(args, 1, design, false);
for(HIST_ENTRY **list = history_list(); *list != NULL; list++)
log("%s\n", (*list)->line);
}
} HistoryPass;
struct ScriptPass : public Pass {
ScriptPass() : Pass("script", "execute commands from script file") { }
virtual void help() {
log("\n");
log(" script <filename>\n");
log("\n");
log("This command executes the yosys commands in the specified file.\n");
log("\n");
}
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
if (args.size() < 2)
log_cmd_error("Missing script file.\n");
if (args.size() > 2)
extra_args(args, 1, design, false);
run_frontend(args[1], "script", design, NULL);
}
} ScriptPass;
#ifdef YOSYS_ENABLE_TCL
static Tcl_Interp *yosys_tcl_interp = NULL;
static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
{
std::vector<std::string> args;
for (int i = 1; i < argc; i++)
args.push_back(argv[i]);
if (args.size() >= 1 && args[0] == "-import") {
for (auto &it : REGISTER_INTERN::pass_register) {
std::string tcl_command_name = it.first;
if (tcl_command_name == "proc")
tcl_command_name = "procs";
Tcl_CmdInfo info;
if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) {
log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str());
} else {
std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str());
Tcl_Eval(interp, tcl_script.c_str());
}
}
return TCL_OK;
}
if (args.size() == 1) {
Pass::call(yosys_get_design(), args[0]);
return TCL_OK;
}
Pass::call(yosys_get_design(), args);
return TCL_OK;
}
extern Tcl_Interp *yosys_get_tcl_interp()
{
if (yosys_tcl_interp == NULL) {
yosys_tcl_interp = Tcl_CreateInterp();
Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL);
}
return yosys_tcl_interp;
}
struct TclPass : public Pass {
TclPass() : Pass("tcl", "execute a TCL script file") { }
virtual void help() {
log("\n");
log(" tcl <filename>\n");
log("\n");
log("This command executes the tcl commands in the specified file.\n");
log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
log("\n");
log("The tcl command 'yosys -import' can be used to import all yosys\n");
log("commands directly as tcl commands to the tcl shell. The yosys\n");
log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
log("to avoid a name collision with the tcl builting command 'proc'.\n");
log("\n");
}
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
if (args.size() < 2)
log_cmd_error("Missing script file.\n");
if (args.size() > 2)
extra_args(args, 1, design, false);
if (Tcl_EvalFile(yosys_get_tcl_interp(), args[1].c_str()) != TCL_OK)
log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
}
} TclPass;
#endif
static RTLIL::Design *yosys_design = NULL;
extern RTLIL::Design *yosys_get_design()
{
return yosys_design;
}
#if defined(__linux__)
std::string proc_self_dirname ()
{
char path [PATH_MAX];
ssize_t buflen = readlink("/proc/self/exe", path, sizeof(path));
if (buflen < 0) {
log_cmd_error("readlink(\"/proc/self/exe\") failed: %s", strerror(errno));
log_abort();
}
while (buflen > 0 && path[buflen-1] != '/')
buflen--;
return std::string(path, buflen);
}
#elif defined(__APPLE__)