Skip to content

Commit

Permalink
enhanced_codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Myrausman committed Nov 14, 2024
1 parent 383cbca commit 787dd69
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 32 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,37 @@ make EXTENSIONS='rv*_i rv*_m'
Which will print the following log:

```
Running with args : ['./parse.py', '-c', '-chisel', '-sverilog', '-rust', '-latex', 'rv32_i', 'rv64_i', 'rv_i', 'rv64_m', 'rv_m']
Running with args : ['./parse.py', '-c', '-go', '-chisel', '-sverilog', '-rust', '-latex', '-spinalhdl', 'rv32_i', 'rv64_i', 'rv_i', 'rv64_m', 'rv_m']
Extensions selected : ['rv32_i', 'rv64_i', 'rv_i', 'rv64_m', 'rv_m']
INFO:: encoding.out.h generated successfully
INFO:: inst.chisel generated successfully
INFO:: inst.spinalhdl generated successfully
INFO:: inst.sverilog generated successfully
INFO:: inst.rs generated successfully
INFO:: inst.go generated successfully
INFO:: instr-table.tex generated successfully
INFO:: priv-instr-table.tex generated successfully
```

If you only want a specific artifact you can use one or more of the following targets : `c`, `rust`, `chisel`, `sverilog`, `latex`
If you only want a specific artifact you can use one or more of the following targets : `c`, `rust`, `chisel`, `sverilog`, `latex`.
For example, if you want to generate the `c` based artifact with extensions as shown earlier, you can use the following command:

```bash
./parse.py -c EXTENSIONS='rv*_i rv*_m'
```
Which will print the following log:

```
Running with args : ['./parse.py', '-c', 'EXTENSIONS=rv*_i rv*_m']
Extensions selected : ['EXTENSIONS=rv*_i rv*_m']
INFO:: encoding.out.h generated successfully
```

or you can also use the `make` command as:

```bash
make encoding.out.h EXTENSIONS='rv*_i rv*_m'
```
You can use the `clean` target to remove all artifacts.

## Adding a new extension
Expand Down
16 changes: 8 additions & 8 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import pprint
import sys

from c_utils import make_c
from chisel_utils import make_chisel
from constants import emitted_pseudo_ops
from go_utils import make_go
from latex_utils import make_latex_table, make_priv_latex_table
from rust_utils import make_rust
from shared_utils import add_segmented_vls_insn, create_inst_dict
from sverilog_utils import make_sverilog
from utils.c_utils import make_c
from utils.chisel_utils import make_chisel
from utils.constants import emitted_pseudo_ops
from utils.go_utils import make_go
from utils.latex_utils import make_latex_table, make_priv_latex_table
from utils.rust_utils import make_rust
from utils.shared_utils import add_segmented_vls_insn, create_inst_dict
from utils.sverilog_utils import make_sverilog

LOG_FORMAT = "%(levelname)s:: %(message)s"
LOG_LEVEL = logging.INFO
Expand Down
16 changes: 8 additions & 8 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest
from unittest.mock import Mock, patch

from shared_utils import (
from utils.shared_utils import (
InstrDict,
check_arg_lut,
check_overlapping_bits,
Expand Down Expand Up @@ -105,7 +105,7 @@ def setUp(self):
self.logger = logging.getLogger()
self.logger.disabled = True

@patch.dict("shared_utils.arg_lut", {"rd": (11, 7), "rs1": (19, 15)})
@patch.dict("utils.shared_utils.arg_lut", {"rd": (11, 7), "rs1": (19, 15)})
def test_check_arg_lut(self):
"""Test argument lookup table checking"""
encoding_args = initialize_encoding()
Expand All @@ -116,7 +116,7 @@ def test_check_arg_lut(self):
self.assertEqual(encoding_args[31 - 11 : 31 - 6], ["rd"] * 5)
self.assertEqual(encoding_args[31 - 19 : 31 - 14], ["rs1"] * 5)

@patch.dict("shared_utils.arg_lut", {"rs1": (19, 15)})
@patch.dict("utils.shared_utils.arg_lut", {"rs1": (19, 15)})
def test_handle_arg_lut_mapping(self):
"""Test handling of argument mappings"""
# Valid mapping
Expand Down Expand Up @@ -175,15 +175,15 @@ def setUp(self):
self.logger.disabled = True
# Create a patch for arg_lut
self.arg_lut_patcher = patch.dict(
"shared_utils.arg_lut", {"rd": (11, 7), "imm20": (31, 12)}
"utils.shared_utils.arg_lut", {"rd": (11, 7), "imm20": (31, 12)}
)
self.arg_lut_patcher.start()

def tearDown(self):
self.arg_lut_patcher.stop()

@patch("shared_utils.fixed_ranges")
@patch("shared_utils.single_fixed")
@patch("utils.shared_utils.fixed_ranges")
@patch("utils.shared_utils.single_fixed")
def test_process_enc_line(self, mock_single_fixed: Mock, mock_fixed_ranges: Mock):
"""Test processing of encoding lines"""
# Setup mock return values
Expand All @@ -204,7 +204,7 @@ def test_process_enc_line(self, mock_single_fixed: Mock, mock_fixed_ranges: Mock
self.assertIn("imm20", data["variable_fields"])

@patch("os.path.exists")
@patch("shared_utils.logging.error")
@patch("utils.shared_utils.logging.error")
def test_find_extension_file(self, mock_logging: Mock, mock_exists: Mock):
"""Test extension file finding"""
# Test successful case - file exists in main directory
Expand Down Expand Up @@ -235,7 +235,7 @@ def test_process_standard_instructions(self):
instr_dict: InstrDict = {}
file_name = "rv32i"

with patch("shared_utils.process_enc_line") as mock_process_enc:
with patch("utils.shared_utils.process_enc_line") as mock_process_enc:
# Setup mock return values
mock_process_enc.side_effect = [
("add", {"extension": ["rv32i"], "encoding": "encoding1"}),
Expand Down
8 changes: 5 additions & 3 deletions c_utils.py → utils/c_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os
import pprint

from constants import causes, csrs, csrs32
from shared_utils import InstrDict, arg_lut
from utils.constants import causes, csrs, csrs32
from utils.shared_utils import InstrDict, arg_lut

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down Expand Up @@ -43,7 +43,9 @@ def make_c(instr_dict: InstrDict):
mask = ((1 << (end - begin + 1)) - 1) << begin
arg_str += f"#define INSN_FIELD_{sanitized_name.upper()} {hex(mask)}\n"

with open(f"{os.path.dirname(__file__)}/encoding.h", "r", encoding="utf-8") as file:
with open(
os.path.join(os.path.dirname(__file__), "../encoding.h"), "r", encoding="utf-8"
) as file:
enc_header = file.read()

commit = os.popen('git log -1 --format="format:%h"').read()
Expand Down
4 changes: 2 additions & 2 deletions chisel_utils.py → utils/chisel_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import pprint

from constants import causes, csrs, csrs32
from shared_utils import InstrDict, instr_dict_2_extensions
from utils.constants import causes, csrs, csrs32
from utils.shared_utils import InstrDict, instr_dict_2_extensions

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion go_utils.py → utils/go_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import sys

from shared_utils import InstrDict, signed
from utils.shared_utils import InstrDict, signed

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down
4 changes: 2 additions & 2 deletions latex_utils.py → utils/latex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import pprint
from typing import TextIO

from constants import latex_fixed_fields, latex_inst_type, latex_mapping
from shared_utils import InstrDict, arg_lut, create_inst_dict
from utils.constants import latex_fixed_fields, latex_inst_type, latex_mapping
from utils.shared_utils import InstrDict, arg_lut, create_inst_dict

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down
4 changes: 2 additions & 2 deletions rust_utils.py → utils/rust_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import pprint

from constants import causes, csrs, csrs32
from shared_utils import InstrDict
from utils.constants import causes, csrs, csrs32
from utils.shared_utils import InstrDict

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down
7 changes: 5 additions & 2 deletions shared_utils.py → utils/shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from itertools import chain
from typing import Dict, Optional, TypedDict

from constants import (
from utils.constants import (
arg_lut,
fixed_ranges,
imported_regex,
Expand Down Expand Up @@ -575,7 +575,10 @@ def create_inst_dict(
if include_pseudo_ops is None:
include_pseudo_ops = []

opcodes_dir = os.path.dirname(os.path.realpath(__file__)) + "/extensions"
script_path = os.path.realpath(__file__)
dir_path, _ = os.path.split(script_path)
opcodes_dir = os.path.dirname(dir_path) + "/extensions"
print(opcodes_dir, "############")
instr_dict: InstrDict = {}

file_names = [
Expand Down
4 changes: 2 additions & 2 deletions sverilog_utils.py → utils/sverilog_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import pprint
from pathlib import Path

from constants import csrs, csrs32
from shared_utils import InstrDict
from utils.constants import csrs, csrs32
from utils.shared_utils import InstrDict

pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
Expand Down

0 comments on commit 787dd69

Please sign in to comment.