Skip to content

Commit dbab13d

Browse files
arg processing: refactor, more tests
1 parent fa3e44f commit dbab13d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

esp32_ulp/opcodes.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,15 @@ def arg_qualify(arg):
263263
264264
return result as ARG namedtuple
265265
"""
266-
if len(arg) == 2 and arg[0] in 'rR' and arg[1] in '0123456789':
267-
reg = int(arg[1])
268-
if 0 <= reg <= 3:
269-
return ARG(REG, reg, arg)
270-
raise ValueError('arg_qualify: valid registers are r0, r1, r2, r3. Given: %s' % arg)
271-
if len(arg) == 2 and arg.lower() in ['--', 'eq', 'ov', 'lt', 'gt', 'ge']:
272-
return ARG(COND, arg.lower(), arg)
266+
arg_lower = arg.lower()
267+
if len(arg) == 2:
268+
if arg_lower[0] == 'r' and arg[1] in '0123456789':
269+
reg = int(arg[1])
270+
if 0 <= reg <= 3:
271+
return ARG(REG, reg, arg)
272+
raise ValueError('arg_qualify: valid registers are r0, r1, r2, r3. Given: %s' % arg)
273+
if arg_lower in ['--', 'eq', 'ov', 'lt', 'gt', 'ge']:
274+
return ARG(COND, arg_lower, arg)
273275
try:
274276
return ARG(IMM, int(arg), arg)
275277
except ValueError:

tests/opcodes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from uctypes import UINT32, BFUINT32, BF_POS, BF_LEN
22
from esp32_ulp.opcodes import make_ins, make_ins_struct_def
3-
from esp32_ulp.opcodes import get_reg, get_imm, arg_qualify, ARG, REG, IMM
3+
from esp32_ulp.opcodes import get_reg, get_imm, get_cond, arg_qualify, ARG, REG, IMM, COND
44

55
OPCODE_DELAY = 4
66
LAYOUT_DELAY = """
@@ -39,6 +39,9 @@ def test_arg_qualify():
3939
assert arg_qualify('0x20') == ARG(IMM, 32, '0x20')
4040
assert arg_qualify('0o100') == ARG(IMM, 64, '0o100')
4141
assert arg_qualify('0b1000') == ARG(IMM, 8, '0b1000')
42+
assert arg_qualify('eq') == ARG(COND, 'eq', 'eq')
43+
assert arg_qualify('Eq') == ARG(COND, 'eq', 'Eq')
44+
assert arg_qualify('EQ') == ARG(COND, 'eq', 'EQ')
4245

4346

4447
def test_get_reg():
@@ -50,8 +53,13 @@ def test_get_imm():
5053
assert get_imm('42') == 42
5154

5255

56+
def test_get_cond():
57+
assert get_cond('Eq') == 'eq'
58+
59+
5360
test_make_ins_struct_def()
5461
test_make_ins()
5562
test_arg_qualify()
5663
test_get_reg()
5764
test_get_imm()
65+
test_get_cond()

0 commit comments

Comments
 (0)