Skip to content

Commit 90e2daa

Browse files
authored
Merge pull request #23 from kcelebi/dev
Hexmode, package organization fixes.
2 parents 1e20a0f + 6aa711e commit 90e2daa

20 files changed

+71
-46
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ dist
44
riscv_assembler.egg-info
55
build
66
.pytest_cache/
7-
__pycache__/
87
deprc_setup.py

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
pythonpath = . src
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/riscv_assembler/convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
- Update tests
1313
'''
1414

15-
from src.riscv_assembler.instr_arr import *
16-
from src.riscv_assembler.parse import *
15+
from riscv_assembler.instr_arr import *
16+
from riscv_assembler.parse import *
1717

1818
__all__ = ['AssemblyConverter']
1919

src/riscv_assembler/instr_arr.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ def organize(self, tokens):
292292

293293
return BadInstructionError()
294294

295+
def JUMP(x : str, line_num : int) -> int:
296+
raise NotImplementedError()
297+
298+
# search forward
299+
skip_labels = 0
300+
for i in range(line_num, len(self.code)):
301+
if x+":" == self.code[i]:
302+
jump_size = (i - line_num - skip_labels) * 4 # how many instructions to jump ahead
303+
return jump_size
304+
305+
if self.code[i][-1] == ':':
306+
skip_labels += 1
307+
308+
# search backward
309+
skip_labels = 0
310+
for i in range(line_num, -1, -1):
311+
# substruct correct label itself
312+
if self.code[i][-1] == ':':
313+
skip_labels += 1
314+
315+
if x+":" == self.code[i]:
316+
jump_size = (i - line_num + skip_labels) * 4 # how many instructions to jump behind
317+
return jump_size
318+
319+
raise Exception("Address not found!")
320+
295321
def register_map():
296322
path = Path(__file__).parent / "data/reg_map.dat"
297323
rmap = {}

src/riscv_assembler/parse.py

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from src.riscv_assembler.instr_arr import *
1+
from riscv_assembler.instr_arr import *
22
from types import FunctionType as function
33
__all__ = ['Parser']
44

@@ -19,8 +19,8 @@ class _Parser:
1919

2020
def __call__(self, *args) -> list:
2121
if _Parser.is_file(*args):
22-
return self.read_file(*args)
23-
return [self.interpret(x) for x in args[0].split("\n")]
22+
return _Parser.interpret_file(_Parser.read_file(*args))
23+
return [_Parser.interpret(_Parser.tokenize(x)) for x in args[0].split("\n") if len(_Parser.tokenize(x)) > 0]
2424

2525
@staticmethod
2626
def is_file(x : str) -> bool:
@@ -56,6 +56,8 @@ def handle_specific_instr(x : list) -> list:
5656
if len(x[0]) == 2 and (x[0] in S_instr or x[0] in I_instr):
5757
y = x[-1].split('('); y[1] = y[1].replace(')','')
5858
return x[0:-1] + y
59+
elif 'requires jump' == 5:
60+
...
5961

6062
return x
6163

@@ -64,15 +66,26 @@ def handle_specific_instr(x : list) -> list:
6466
'''
6567
@staticmethod
6668
def read_file(file : str) -> list:
67-
code = []
68-
file = open(file, "r")
69+
'''code = []
70+
file = open(file, "r")
71+
line = file.readline()
72+
while line != "":
73+
tokens = _Parser.tokenize(line)
74+
code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0]
75+
line = file.readline()
76+
file.close()
77+
return code'''
78+
with open(file) as f:
79+
return f.readlines()
6980

70-
line = file.readline()
71-
while line != "":
81+
@staticmethod
82+
def interpret_file(code : list) -> list:
83+
int_code = []
84+
for line in code:
7285
tokens = _Parser.tokenize(line)
73-
code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0]
74-
line = file.readline()
75-
return code
86+
int_code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0]
87+
88+
return int_code
7689

7790
'''
7891
Tokenize a given line
@@ -105,35 +118,6 @@ def determine_type(tk : str) -> function:
105118
for i in range(len(instr_sets)):
106119
if tk in instr_sets[i]:
107120
return parsers[i]
108-
raise Exception("Bad Instruction Provided!")
109-
110-
'''
111-
Calculate jump
112-
'''
113-
def calc_jump(self, x : str, line_num : int) -> int:
114-
raise NotImplementedError()
115-
116-
# search forward
117-
skip_labels = 0
118-
for i in range(line_num, len(self.code)):
119-
if x+":" == self.code[i]:
120-
jump_size = (i - line_num - skip_labels) * 4 # how many instructions to jump ahead
121-
return jump_size
122-
123-
if self.code[i][-1] == ':':
124-
skip_labels += 1
125-
126-
# search backward
127-
skip_labels = 0
128-
for i in range(line_num, -1, -1):
129-
# substruct correct label itself
130-
if self.code[i][-1] == ':':
131-
skip_labels += 1
132-
133-
if x+":" == self.code[i]:
134-
jump_size = (i - line_num + skip_labels) * 4 # how many instructions to jump behind
135-
return jump_size
136-
137-
raise Exception("Address not found!")
121+
raise Exception("Bad Instruction Provided: " + tk + "!")
138122

139123
Parser = _Parser()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-2.47 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/assembly/test4.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add x1 x0 x0
2+
addi t0 s0 32
3+
addi t0 s0 32
4+
sw s0, 0(sp)

tests/pytest.ini

Whitespace-only changes.

tests/test_class.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from pathlib import Path
3-
from src.riscv_assembler.convert import AssemblyConverter as AC
4-
from src.riscv_assembler.parse import Parser
3+
from riscv_assembler.convert import AssemblyConverter as AC
4+
from riscv_assembler.parse import Parser
55
#TESTS
66

77
#test simple.s file, writes to txt and bin
@@ -109,6 +109,13 @@ def func13():
109109

110110
return cnv(path)
111111

112+
def func14():
113+
cnv = AC(hex_mode = True, output_mode='a')
114+
115+
instr = 'add x1 x0 x0\naddi t0 s0 32\naddi t0 s0 32\nsw s0, 0(sp)'
116+
117+
return cnv(instr)
118+
112119
#-----------------------------------------------------------------------------------------
113120
#-----------------------------------------------------------------------------------------
114121
#-----------------------------------------------------------------------------------------
@@ -152,4 +159,7 @@ def test_11():
152159
# assert func12() == ['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493']
153160

154161
def test_13():
155-
assert func13() == ['0x00812023']
162+
assert func13() == ['0x00812023']
163+
164+
def test_14():
165+
assert func14() == ['0x000000b3', '0x02040293', '0x02040293','0x00812023']

0 commit comments

Comments
 (0)