jump tests

This commit is contained in:
Peter McGoron 2023-02-11 21:24:48 +00:00
parent 70720b8a16
commit b425e26ea3
5 changed files with 98 additions and 12 deletions

View File

@ -92,9 +92,14 @@ class Instruction(Enum):
MUL = 4, ArgType.REG, ArgType.VAL, ArgType.VAL MUL = 4, ArgType.REG, ArgType.VAL, ArgType.VAL
DIV = 5, ArgType.REG, ArgType.VAL, ArgType.VAL DIV = 5, ArgType.REG, ArgType.VAL, ArgType.VAL
SDIV = 6, ArgType.REG, ArgType.VAL, ArgType.VAL SDIV = 6, ArgType.REG, ArgType.VAL, ArgType.VAL
JL = 7, ArgType.LAB, ArgType.VAL, ArgType.VAL SYS = 7, ArgType.VAL
CLB = 8, ArgType.LAB CLB = 8, ArgType.LAB
SYS = 9, ArgType.VAL JL = 9, ArgType.LAB, ArgType.VAL, ArgType.VAL
JLE = 10, ArgType.LAB, ArgType.VAL, ArgType.VAL
JG = 11, ArgType.LAB, ArgType.VAL, ArgType.VAL
JGE = 12, ArgType.LAB, ArgType.VAL, ArgType.VAL
JE = 13, ArgType.LAB, ArgType.VAL, ArgType.VAL
JNE = 13, ArgType.LAB, ArgType.VAL, ArgType.VAL
def __init__(self, opcode, *args): def __init__(self, opcode, *args):
if opcode > 0x7F or opcode < 0: if opcode > 0x7F or opcode < 0:
@ -214,6 +219,10 @@ class Program:
args_w_type = ins.typecheck(line[1:]) args_w_type = ins.typecheck(line[1:])
self.asm_push_line(ins.opcode, args_w_type) self.asm_push_line(ins.opcode, args_w_type)
def parse_lines(self, lines):
for l in lines:
self.parse_asm_line(l)
def __call__(self): def __call__(self):
b = bytes() b = bytes()
for line in self.asm: for line in self.asm:

View File

@ -120,13 +120,15 @@ class Environment:
def syscall(self, sc): def syscall(self, sc):
raise InvalidSyscallError(sc) raise InvalidSyscallError(sc)
def __call__(self): def __call__(self, debug=False):
sc = c_size_t() sc = c_size_t()
ret = RunRet.CONTINUE ret = RunRet.CONTINUE
while not ret.is_halt(): while not ret.is_halt():
ret = RunRet(dll.creole_step(byref(self.cenv), byref(sc))) ret = RunRet(dll.creole_step(byref(self.cenv), byref(sc)))
if ret == RunRet.SYSCALL: if ret == RunRet.SYSCALL:
self.syscall(sc) self.syscall(sc)
if debug:
print(self.cenv.reg[0])
return ret return ret
class CParseLineException(Exception): class CParseLineException(Exception):

View File

@ -309,6 +309,22 @@ class DivTest(unittest.TestCase):
self.assertEqual(cm.exception.i, 2) self.assertEqual(cm.exception.i, 2)
self.assertEqual(cm.exception.opcode, 5) self.assertEqual(cm.exception.opcode, 5)
class LabelTest(unittest.TestCase):
def test_simple_loop(self):
p = Program()
p.parse_lines([
"add r0 10 0",
"add r1 20 0",
"CLB l0",
"add r0 r0 -1",
"add r1 r1 1",
"jg l0 r0 0"
])
ex = ffi.Environment(p())
self.assertEqual(ex(), ffi.RunRet.STOP)
self.assertEqual(ex.getreg(0), 0)
self.assertEqual(ex.getreg(1), 30)
class ProgramTest(unittest.TestCase): class ProgramTest(unittest.TestCase):
def test_exec_simple_reg(self): def test_exec_simple_reg(self):
p = Program() p = Program()

View File

@ -36,9 +36,14 @@ static const struct {
defop(MUL, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(MUL, 3, TYPE_REG, TYPE_VAL, TYPE_VAL),
defop(DIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(DIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL),
defop(SDIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL), defop(SDIV, 3, TYPE_REG, TYPE_VAL, TYPE_VAL),
defop(JL, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL), defop(SYS, 1, TYPE_VAL, TYPE_NONE, TYPE_NONE),
defop(CLB, 1, TYPE_LAB, TYPE_NONE, TYPE_NONE), defop(CLB, 1, TYPE_LAB, TYPE_NONE, TYPE_NONE),
defop(SYS, 1, TYPE_VAL, TYPE_NONE, TYPE_NONE) defop(JL, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL),
defop(JLE, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL),
defop(JG, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL),
defop(JGE, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL),
defop(JE, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL),
defop(JNE, 3, TYPE_LAB, TYPE_VAL, TYPE_VAL)
}; };
/************************************************************************* /*************************************************************************
@ -518,6 +523,7 @@ enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc)
struct creole_ins *ins = env->prg + env->prgptr; struct creole_ins *ins = env->prg + env->prgptr;
creole_word a1, a2; creole_word a1, a2;
int rcode = CREOLE_STEP_CONTINUE; int rcode = CREOLE_STEP_CONTINUE;
int increase_pointer = 1;
if (env->prgptr == env->prgend) if (env->prgptr == env->prgend)
return CREOLE_STEP_STOP; return CREOLE_STEP_STOP;
@ -556,21 +562,69 @@ enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc)
check(creole_reg_write(env, ins->w[0], check(creole_reg_write(env, ins->w[0],
(creole_signed)a1 / (creole_signed)a2)); (creole_signed)a1 / (creole_signed)a2));
break; break;
case CREOLE_SYS:
check(read_val(env, ins, 0, sc));
rcode = CREOLE_STEP_SYSCALL;
break;
case CREOLE_JL: case CREOLE_JL:
check(read_val(env, ins, 1, &a1)); check(read_val(env, ins, 1, &a1));
check(read_val(env, ins, 2, &a2)); check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0])); check(check_label(env, ins->w[0]));
if (a1 < a2) if (a1 < a2) {
env->prgptr = env->lab[ins->w[0]]; env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break; break;
case CREOLE_SYS: case CREOLE_JLE:
check(read_val(env, ins, 0, sc)); check(read_val(env, ins, 1, &a1));
rcode = CREOLE_STEP_SYSCALL; check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0]));
if (a1 <= a2) {
env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break;
case CREOLE_JG:
check(read_val(env, ins, 1, &a1));
check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0]));
if (a1 > a2) {
env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break;
case CREOLE_JGE:
check(read_val(env, ins, 1, &a1));
check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0]));
if (a1 >= a2) {
env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break;
case CREOLE_JE:
check(read_val(env, ins, 1, &a1));
check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0]));
if (a1 == a2) {
env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break;
case CREOLE_JNE:
check(read_val(env, ins, 1, &a1));
check(read_val(env, ins, 2, &a2));
check(check_label(env, ins->w[0]));
if (a1 != a2) {
env->prgptr = env->lab[ins->w[0]];
increase_pointer = 0;
}
break; break;
default: default:
rcode = CREOLE_STEP_UNKNOWN_OPCODE; rcode = CREOLE_STEP_UNKNOWN_OPCODE;
} }
if (increase_pointer)
env->prgptr++; env->prgptr++;
return rcode; return rcode;
} }

View File

@ -24,9 +24,14 @@ enum creole_opcode {
CREOLE_MUL = 4, CREOLE_MUL = 4,
CREOLE_DIV = 5, CREOLE_DIV = 5,
CREOLE_SDIV = 6, CREOLE_SDIV = 6,
CREOLE_JL = 7, CREOLE_SYS = 7,
CREOLE_CLB = 8, CREOLE_CLB = 8,
CREOLE_SYS = 9, CREOLE_JL = 9,
CREOLE_JLE = 10,
CREOLE_JG = 11,
CREOLE_JGE = 12,
CREOLE_JE = 13,
CREOLE_JNE = 14,
CREOLE_OPCODE_LEN CREOLE_OPCODE_LEN
}; };