diff --git a/.gitignore b/.gitignore index 0d28edc..cf3feec 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,4 @@ dist riscv_assembler.egg-info build .pytest_cache/ -__pycache__/ deprc_setup.py \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..b042f06 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = . src \ No newline at end of file diff --git a/src/riscv_assembler/__pycache__/__init__.cpython-38.pyc b/src/riscv_assembler/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..a1664f0 Binary files /dev/null and b/src/riscv_assembler/__pycache__/__init__.cpython-38.pyc differ diff --git a/src/riscv_assembler/__pycache__/convert.cpython-38.pyc b/src/riscv_assembler/__pycache__/convert.cpython-38.pyc new file mode 100644 index 0000000..456c6d2 Binary files /dev/null and b/src/riscv_assembler/__pycache__/convert.cpython-38.pyc differ diff --git a/src/riscv_assembler/__pycache__/instr_arr.cpython-38.pyc b/src/riscv_assembler/__pycache__/instr_arr.cpython-38.pyc new file mode 100644 index 0000000..18b4a22 Binary files /dev/null and b/src/riscv_assembler/__pycache__/instr_arr.cpython-38.pyc differ diff --git a/src/riscv_assembler/__pycache__/parse.cpython-38.pyc b/src/riscv_assembler/__pycache__/parse.cpython-38.pyc new file mode 100644 index 0000000..cf5d258 Binary files /dev/null and b/src/riscv_assembler/__pycache__/parse.cpython-38.pyc differ diff --git a/src/riscv_assembler/convert.py b/src/riscv_assembler/convert.py index b27299d..619f559 100644 --- a/src/riscv_assembler/convert.py +++ b/src/riscv_assembler/convert.py @@ -12,8 +12,8 @@ - Update tests ''' -from src.riscv_assembler.instr_arr import * -from src.riscv_assembler.parse import * +from riscv_assembler.instr_arr import * +from riscv_assembler.parse import * __all__ = ['AssemblyConverter'] diff --git a/src/riscv_assembler/instr_arr.py b/src/riscv_assembler/instr_arr.py index 04c6d7b..91f4aa2 100644 --- a/src/riscv_assembler/instr_arr.py +++ b/src/riscv_assembler/instr_arr.py @@ -292,6 +292,32 @@ def organize(self, tokens): return BadInstructionError() +def JUMP(x : str, line_num : int) -> int: + raise NotImplementedError() + + # search forward + skip_labels = 0 + for i in range(line_num, len(self.code)): + if x+":" == self.code[i]: + jump_size = (i - line_num - skip_labels) * 4 # how many instructions to jump ahead + return jump_size + + if self.code[i][-1] == ':': + skip_labels += 1 + + # search backward + skip_labels = 0 + for i in range(line_num, -1, -1): + # substruct correct label itself + if self.code[i][-1] == ':': + skip_labels += 1 + + if x+":" == self.code[i]: + jump_size = (i - line_num + skip_labels) * 4 # how many instructions to jump behind + return jump_size + + raise Exception("Address not found!") + def register_map(): path = Path(__file__).parent / "data/reg_map.dat" rmap = {} diff --git a/src/riscv_assembler/parse.py b/src/riscv_assembler/parse.py index 3dc7449..13dd736 100644 --- a/src/riscv_assembler/parse.py +++ b/src/riscv_assembler/parse.py @@ -1,4 +1,4 @@ -from src.riscv_assembler.instr_arr import * +from riscv_assembler.instr_arr import * from types import FunctionType as function __all__ = ['Parser'] @@ -19,8 +19,8 @@ class _Parser: def __call__(self, *args) -> list: if _Parser.is_file(*args): - return self.read_file(*args) - return [self.interpret(x) for x in args[0].split("\n")] + return _Parser.interpret_file(_Parser.read_file(*args)) + return [_Parser.interpret(_Parser.tokenize(x)) for x in args[0].split("\n") if len(_Parser.tokenize(x)) > 0] @staticmethod def is_file(x : str) -> bool: @@ -56,6 +56,8 @@ def handle_specific_instr(x : list) -> list: if len(x[0]) == 2 and (x[0] in S_instr or x[0] in I_instr): y = x[-1].split('('); y[1] = y[1].replace(')','') return x[0:-1] + y + elif 'requires jump' == 5: + ... return x @@ -64,15 +66,26 @@ def handle_specific_instr(x : list) -> list: ''' @staticmethod def read_file(file : str) -> list: - code = [] - file = open(file, "r") + '''code = [] + file = open(file, "r") + line = file.readline() + while line != "": + tokens = _Parser.tokenize(line) + code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0] + line = file.readline() + file.close() + return code''' + with open(file) as f: + return f.readlines() - line = file.readline() - while line != "": + @staticmethod + def interpret_file(code : list) -> list: + int_code = [] + for line in code: tokens = _Parser.tokenize(line) - code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0] - line = file.readline() - return code + int_code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0] + + return int_code ''' Tokenize a given line @@ -105,35 +118,6 @@ def determine_type(tk : str) -> function: for i in range(len(instr_sets)): if tk in instr_sets[i]: return parsers[i] - raise Exception("Bad Instruction Provided!") - - ''' - Calculate jump - ''' - def calc_jump(self, x : str, line_num : int) -> int: - raise NotImplementedError() - - # search forward - skip_labels = 0 - for i in range(line_num, len(self.code)): - if x+":" == self.code[i]: - jump_size = (i - line_num - skip_labels) * 4 # how many instructions to jump ahead - return jump_size - - if self.code[i][-1] == ':': - skip_labels += 1 - - # search backward - skip_labels = 0 - for i in range(line_num, -1, -1): - # substruct correct label itself - if self.code[i][-1] == ':': - skip_labels += 1 - - if x+":" == self.code[i]: - jump_size = (i - line_num + skip_labels) * 4 # how many instructions to jump behind - return jump_size - - raise Exception("Address not found!") + raise Exception("Bad Instruction Provided: " + tk + "!") Parser = _Parser() \ No newline at end of file diff --git a/tests/__pycache__/test_class.cpython-36-pytest-6.2.4.pyc b/tests/__pycache__/test_class.cpython-36-pytest-6.2.4.pyc deleted file mode 100644 index d3e9a4a..0000000 Binary files a/tests/__pycache__/test_class.cpython-36-pytest-6.2.4.pyc and /dev/null differ diff --git a/tests/__pycache__/test_class.cpython-37-pytest-6.2.1.pyc b/tests/__pycache__/test_class.cpython-37-pytest-6.2.1.pyc deleted file mode 100644 index 12d155c..0000000 Binary files a/tests/__pycache__/test_class.cpython-37-pytest-6.2.1.pyc and /dev/null differ diff --git a/tests/__pycache__/test_class.cpython-38-pytest-7.1.2.pyc b/tests/__pycache__/test_class.cpython-38-pytest-7.1.2.pyc deleted file mode 100644 index b10b54b..0000000 Binary files a/tests/__pycache__/test_class.cpython-38-pytest-7.1.2.pyc and /dev/null differ diff --git a/tests/__pycache__/test_class.cpython-38-pytest-7.2.2.pyc b/tests/__pycache__/test_class.cpython-38-pytest-7.2.2.pyc index 76680d7..c76ee75 100644 Binary files a/tests/__pycache__/test_class.cpython-38-pytest-7.2.2.pyc and b/tests/__pycache__/test_class.cpython-38-pytest-7.2.2.pyc differ diff --git a/tests/__pycache__/test_class.cpython-38.pyc b/tests/__pycache__/test_class.cpython-38.pyc deleted file mode 100644 index 0127bc9..0000000 Binary files a/tests/__pycache__/test_class.cpython-38.pyc and /dev/null differ diff --git a/tests/__pycache__/test_utils.cpython-36-pytest-6.2.4.pyc b/tests/__pycache__/test_utils.cpython-36-pytest-6.2.4.pyc deleted file mode 100644 index de74647..0000000 Binary files a/tests/__pycache__/test_utils.cpython-36-pytest-6.2.4.pyc and /dev/null differ diff --git a/tests/__pycache__/test_utils.cpython-37-pytest-6.2.1.pyc b/tests/__pycache__/test_utils.cpython-37-pytest-6.2.1.pyc deleted file mode 100644 index 880c80c..0000000 Binary files a/tests/__pycache__/test_utils.cpython-37-pytest-6.2.1.pyc and /dev/null differ diff --git a/tests/__pycache__/test_utils.cpython-38-pytest-7.1.2.pyc b/tests/__pycache__/test_utils.cpython-38-pytest-7.1.2.pyc deleted file mode 100644 index 2050e43..0000000 Binary files a/tests/__pycache__/test_utils.cpython-38-pytest-7.1.2.pyc and /dev/null differ diff --git a/tests/assembly/test4.s b/tests/assembly/test4.s index e69de29..b1427f8 100644 --- a/tests/assembly/test4.s +++ b/tests/assembly/test4.s @@ -0,0 +1,4 @@ +add x1 x0 x0 +addi t0 s0 32 +addi t0 s0 32 +sw s0, 0(sp) \ No newline at end of file diff --git a/tests/pytest.ini b/tests/pytest.ini deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_class.py b/tests/test_class.py index 786b7ac..8829373 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -1,7 +1,7 @@ import pytest from pathlib import Path -from src.riscv_assembler.convert import AssemblyConverter as AC -from src.riscv_assembler.parse import Parser +from riscv_assembler.convert import AssemblyConverter as AC +from riscv_assembler.parse import Parser #TESTS #test simple.s file, writes to txt and bin @@ -109,6 +109,13 @@ def func13(): return cnv(path) +def func14(): + cnv = AC(hex_mode = True, output_mode='a') + + instr = 'add x1 x0 x0\naddi t0 s0 32\naddi t0 s0 32\nsw s0, 0(sp)' + + return cnv(instr) + #----------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------- @@ -152,4 +159,7 @@ def test_11(): # assert func12() == ['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493'] def test_13(): - assert func13() == ['0x00812023'] \ No newline at end of file + assert func13() == ['0x00812023'] + +def test_14(): + assert func14() == ['0x000000b3', '0x02040293', '0x02040293','0x00812023'] \ No newline at end of file