Skip to content

Commit

Permalink
Merge pull request #23 from kcelebi/dev
Browse files Browse the repository at this point in the history
Hexmode, package organization fixes.
  • Loading branch information
kcelebi authored Apr 10, 2023
2 parents 1e20a0f + 6aa711e commit 90e2daa
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 46 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ dist
riscv_assembler.egg-info
build
.pytest_cache/
__pycache__/
deprc_setup.py
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = . src
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions src/riscv_assembler/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
26 changes: 26 additions & 0 deletions src/riscv_assembler/instr_arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
64 changes: 24 additions & 40 deletions src/riscv_assembler/parse.py
Original file line number Diff line number Diff line change
@@ -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']

Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tests/__pycache__/test_class.cpython-38-pytest-7.2.2.pyc
Binary file not shown.
Binary file removed tests/__pycache__/test_class.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/assembly/test4.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add x1 x0 x0
addi t0 s0 32
addi t0 s0 32
sw s0, 0(sp)
Empty file removed tests/pytest.ini
Empty file.
16 changes: 13 additions & 3 deletions tests/test_class.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

#-----------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -152,4 +159,7 @@ def test_11():
# assert func12() == ['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493']

def test_13():
assert func13() == ['0x00812023']
assert func13() == ['0x00812023']

def test_14():
assert func14() == ['0x000000b3', '0x02040293', '0x02040293','0x00812023']

0 comments on commit 90e2daa

Please sign in to comment.