making many tests work

This commit is contained in:
mlokr 2024-09-15 20:14:56 +02:00
parent 6968e7d769
commit 79e4cead2d
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
14 changed files with 1148 additions and 640 deletions

View file

@ -461,23 +461,22 @@ main := fn(): int {
height := 30
x := 0
y := 0
t := 0
//t := 0
i := 0
loop {
if x < width {
t += set_pixel(x, y, width)
if x < height {
//t += set_pixel(x, y, height)
x += 1
i += 1
} else {
x = 0
y += 1
if set_pixel(x, y, width) != i return 0
if set_pixel(x, y, height) != i return 0
if y == width break
}
i += 1
if y == width break
}
return t
return i
}
```
@ -682,6 +681,8 @@ line := fn(buffer: Buffer, p0: Point, p1: Point, color: ColorBGRA, thickness: in
}
} else {
if p0.y > p1.y {
// blah, test leading new line on directives
@inline(line_high, buffer, p1, p0, color)
} else {
@inline(line_high, buffer, p0, p1, color)
@ -791,21 +792,21 @@ main := fn(): int {
return 2
}
//if state_change_in_break(0) != 0 {
// return 3
//}
if state_change_in_break(0) != 0 {
return 3
}
//if state_change_in_break(4) != 10 {
// return 4
//}
if state_change_in_break(4) != 10 {
return 4
}
//if continue_and_state_change(0) != 10 {
// return 5
//}
if continue_and_state_change(10) != 10 {
return 5
}
//if continue_and_state_change(3) != 0 {
// return 6
//}
if continue_and_state_change(3) != 0 {
return 6
}
return 0
}
@ -818,31 +819,31 @@ multiple_breaks := fn(arg: int): int {
return arg
}
//state_change_in_break := fn(arg: int): int {
// loop if arg < 10 {
// if arg == 3 {
// arg = 0
// break
// }
// arg += 1
// } else break
// return arg
//}
state_change_in_break := fn(arg: int): int {
loop if arg < 10 {
if arg == 3 {
arg = 0
break
}
arg += 1
} else break
return arg
}
//continue_and_state_change := fn(arg: int): int {
// loop if arg < 10 {
// if arg == 2 {
// arg = 4
// continue
// }
// if arg == 3 {
// arg = 0
// break
// }
// arg += 1
// } else break
// return arg
//}
continue_and_state_change := fn(arg: int): int {
loop if arg < 10 {
if arg == 2 {
arg = 4
continue
}
if arg == 3 {
arg = 0
break
}
arg += 1
} else break
return arg
}
```
#### writing_into_string

View file

@ -1581,7 +1581,7 @@ impl Codegen {
let signed = ty.is_signed();
if let Loc::Ct { value: CtValue(mut imm), derefed } = right.loc
&& let Some(oper) = op.imm_math_op(signed, size)
&& let Some(oper) = op.imm_binop(signed, size)
{
if derefed {
let mut dst = [0u8; 8];
@ -1634,7 +1634,7 @@ impl Codegen {
}
}
if let Some(op) = op.math_op(signed, size) {
if let Some(op) = op.binop(signed, size) {
self.ci.emit(op(dst.get(), lhs.get(), rhs.get()));
self.ci.regs.free(lhs);
self.ci.regs.free(rhs);
@ -1963,7 +1963,7 @@ impl Codegen {
let lhs = self.loc_to_reg(left, size);
if let Loc::Ct { value, derefed: false } = right
&& let Some(op) = op.imm_math_op(signed, size)
&& let Some(op) = op.imm_binop(signed, size)
{
self.ci.emit(op(lhs.get(), lhs.get(), value.0));
return Some(if let Some(value) = ctx.into_value() {
@ -1976,7 +1976,7 @@ impl Codegen {
let rhs = self.loc_to_reg(right, size);
if let Some(op) = op.math_op(signed, size) {
if let Some(op) = op.binop(signed, size) {
self.ci.emit(op(lhs.get(), lhs.get(), rhs.get()));
self.ci.regs.free(rhs);
return if let Some(value) = ctx.into_value() {

View file

@ -1,4 +1,4 @@
use crate::instrs;
use crate::{instrs, EncodedInstr};
const fn ascii_mask(chars: &[u8]) -> u128 {
let mut eq = 0;
@ -171,10 +171,7 @@ pub enum TokenKind {
impl TokenKind {
#[allow(clippy::type_complexity)]
pub fn cond_op(
self,
signed: bool,
) -> Option<(fn(u8, u8, i16) -> (usize, [u8; instrs::MAX_SIZE]), bool)> {
pub fn cond_op(self, signed: bool) -> Option<(fn(u8, u8, i16) -> EncodedInstr, bool)> {
Some((
match self {
Self::Le if signed => instrs::jgts,
@ -193,12 +190,7 @@ impl TokenKind {
))
}
#[allow(clippy::type_complexity)]
pub fn math_op(
self,
signed: bool,
size: u32,
) -> Option<fn(u8, u8, u8) -> (usize, [u8; instrs::MAX_SIZE])> {
pub fn binop(self, signed: bool, size: u32) -> Option<fn(u8, u8, u8) -> EncodedInstr> {
use instrs::*;
macro_rules! div { ($($op:ident),*) => {[$(|a, b, c| $op(a, 0, b, c)),*]}; }
@ -225,11 +217,7 @@ impl TokenKind {
}
#[allow(clippy::type_complexity)]
pub fn imm_math_op(
self,
signed: bool,
size: u32,
) -> Option<fn(u8, u8, u64) -> (usize, [u8; instrs::MAX_SIZE])> {
pub fn imm_binop(self, signed: bool, size: u32) -> Option<fn(u8, u8, u64) -> EncodedInstr> {
use instrs::*;
macro_rules! def_op {
($name:ident |$a:ident, $b:ident, $c:ident| $($tt:tt)*) => {
@ -275,15 +263,15 @@ impl TokenKind {
matches!(self, S::Eq | S::Ne | S::Bor | S::Xor | S::Band | S::Add | S::Mul)
}
pub fn apply(self, a: i64, b: i64) -> i64 {
pub fn apply_binop(self, a: i64, b: i64) -> i64 {
match self {
TokenKind::Add => a.wrapping_add(b),
TokenKind::Sub => a.wrapping_sub(b),
TokenKind::Mul => a.wrapping_mul(b),
TokenKind::Div => a.wrapping_div(b),
TokenKind::Shl => a.wrapping_shl(b as _),
TokenKind::Eq => (a == b) as i64,
TokenKind::Band => a & b,
Self::Add => a.wrapping_add(b),
Self::Sub => a.wrapping_sub(b),
Self::Mul => a.wrapping_mul(b),
Self::Div => a.wrapping_div(b),
Self::Shl => a.wrapping_shl(b as _),
Self::Eq => (a == b) as i64,
Self::Band => a & b,
s => todo!("{s}"),
}
}
@ -293,6 +281,20 @@ impl TokenKind {
&& self.precedence() != Self::Gt.precedence()
&& self.precedence() != Self::Eof.precedence()
}
pub fn unop(&self) -> Option<fn(u8, u8) -> EncodedInstr> {
Some(match self {
Self::Sub => instrs::neg,
_ => return None,
})
}
pub fn apply_unop(&self, value: i64) -> i64 {
match self {
Self::Sub => value.wrapping_neg(),
s => todo!("{s}"),
}
}
}
gen_token_kind! {

View file

@ -251,6 +251,8 @@ mod ty {
let (oa, ob) = (Self(self.0.min(ob.0)), Self(self.0.max(ob.0)));
let (a, b) = (oa.strip_pointer(), ob.strip_pointer());
Some(match () {
_ if oa == Self::from(NEVER) => ob,
_ if ob == Self::from(NEVER) => oa,
_ if oa == ob => oa,
_ if oa.is_pointer() && ob.is_pointer() => return None,
_ if a.is_signed() && b.is_signed() || a.is_unsigned() && b.is_unsigned() => ob,
@ -467,9 +469,14 @@ mod ty {
}
}
type EncodedInstr = (usize, [u8; instrs::MAX_SIZE]);
type Offset = u32;
type Size = u32;
fn emit(out: &mut Vec<u8>, (len, instr): EncodedInstr) {
out.extend_from_slice(&instr[..len]);
}
#[derive(PartialEq, Eq, Hash)]
struct SymKey {
file: u32,
@ -606,10 +613,6 @@ struct Types {
arrays: Vec<Array>,
}
fn emit(out: &mut Vec<u8>, (len, instr): (usize, [u8; instrs::MAX_SIZE])) {
out.extend_from_slice(&instr[..len]);
}
impl Types {
fn assemble(&mut self, to: &mut Vec<u8>) {
emit(to, instrs::jal(reg::RET_ADDR, reg::ZERO, 0));
@ -1462,7 +1465,10 @@ fn test_run_vm(out: &[u8], output: &mut String) {
let mut stack = [0_u64; 1024 * 20];
let mut vm = unsafe {
hbvm::Vm::<_, 0>::new(LoggedMem::default(), hbvm::mem::Address::new(out.as_ptr() as u64))
hbvm::Vm::<_, { 1024 * 100 }>::new(
LoggedMem::default(),
hbvm::mem::Address::new(out.as_ptr() as u64),
)
};
vm.write_reg(codegen::STACK_PTR, unsafe { stack.as_mut_ptr().add(stack.len()) } as u64);

View file

@ -281,7 +281,7 @@ impl<'a, 'b> Parser<'a, 'b> {
}
}
T::Directive => E::Directive {
pos,
pos: pos - 1, // need to undo the directive shift
name: self.move_str(token),
args: {
self.expect_advance(T::LParen);

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,10 @@
main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 1d
LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
code size: 77
ret: 1
ret: 0
status: Ok(())

View file

@ -1,18 +1,17 @@
main:
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
CP r32, r2
ADDI64 r254, r254, -16d
ST r31, r254, 0a, 16h
LI64 r1, 1d
JNE r32, r1, :0
JNE r2, r1, :0
JMP :1
0: LI64 r33, 0d
JNE r32, r33, :2
0: LI64 r32, 0d
JNE r2, r32, :2
LI64 r1, 2d
JMP :1
2: LI64 r1, 3d
1: LD r31, r254, 0a, 24h
ADDI64 r254, r254, 24d
1: LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a
code size: 130
code size: 127
ret: 2
status: Ok(())

View file

@ -1,10 +1,11 @@
main:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
ADDI64 r254, r254, -16d
ST r31, r254, 0a, 16h
JAL r31, r0, :foo
CP r32, r1
LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a
foo:
ADDI64 r254, r254, -8d
@ -12,6 +13,6 @@ foo:
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
code size: 143
code size: 146
ret: 0
status: Ok(())

View file

@ -1,37 +1,103 @@
main:
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
LI64 r2, 0d
ADDI64 r254, r254, -88d
ST r31, r254, 0a, 88h
LI64 r32, 0d
CP r2, r32
JAL r31, r0, :multiple_breaks
CP r32, r1
LI64 r32, 3d
JEQ r32, r32, :0
CP r33, r1
LI64 r34, 3d
JEQ r33, r34, :0
LI64 r1, 1d
JMP :1
0: LI64 r2, 4d
0: LI64 r35, 4d
CP r2, r35
JAL r31, r0, :multiple_breaks
LI64 r33, 10d
JEQ r1, r33, :2
CP r36, r1
LI64 r37, 10d
JEQ r36, r37, :2
LI64 r1, 2d
JMP :1
2: LI64 r1, 0d
1: LD r31, r254, 0a, 24h
2: CP r2, r32
JAL r31, r0, :state_change_in_break
CP r38, r1
JEQ r38, r32, :3
CP r1, r34
JMP :1
3: CP r2, r35
JAL r31, r0, :state_change_in_break
CP r39, r1
JEQ r39, r37, :4
CP r1, r35
JMP :1
4: CP r2, r37
JAL r31, r0, :continue_and_state_change
CP r40, r1
JEQ r40, r37, :5
LI64 r1, 5d
JMP :1
5: CP r2, r34
JAL r31, r0, :continue_and_state_change
CP r41, r1
JEQ r41, r32, :6
LI64 r1, 6d
JMP :1
6: CP r1, r32
1: LD r31, r254, 0a, 88h
ADDI64 r254, r254, 88d
JALA r0, r31, 0a
continue_and_state_change:
ADDI64 r254, r254, -40d
ST r31, r254, 0a, 40h
CP r1, r2
LI64 r32, 3d
LI64 r33, 4d
LI64 r34, 2d
LI64 r35, 10d
6: JLTU r1, r35, :0
JMP :1
0: JNE r1, r34, :2
CP r1, r33
JMP :3
2: JNE r1, r32, :4
LI64 r1, 0d
1: JMP :5
4: ADDI64 r33, r1, 1d
CP r1, r33
3: JMP :6
5: LD r31, r254, 0a, 40h
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
state_change_in_break:
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
CP r1, r2
LI64 r32, 3d
LI64 r33, 10d
4: JLTU r1, r33, :0
JMP :1
0: JNE r1, r32, :2
LI64 r1, 0d
1: JMP :3
2: ADDI64 r1, r1, 1d
JMP :4
3: LD r31, r254, 0a, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
multiple_breaks:
ADDI64 r254, r254, -32d
ST r31, r254, 0a, 32h
CP r32, r2
CP r1, r32
2: LI64 r33, 10d
JLTS r1, r33, :0
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
CP r1, r2
LI64 r32, 3d
LI64 r33, 10d
4: JLTU r1, r33, :0
JMP :1
0: ADDI64 r34, r1, 1d
CP r1, r34
JMP :2
1: LD r31, r254, 0a, 32h
ADDI64 r254, r254, 32d
0: ADDI64 r1, r1, 1d
JNE r1, r32, :2
1: JMP :3
2: JMP :4
3: LD r31, r254, 0a, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
code size: 278
code size: 704
ret: 0
status: Ok(())

View file

@ -1,48 +1,45 @@
main:
ADDI64 r254, r254, -96d
ST r31, r254, 0a, 96h
ADDI64 r254, r254, -88d
ST r31, r254, 0a, 88h
JAL r31, r0, :check_platform
LI64 r32, 0d
CP r33, r32
CP r32, r1
LI64 r33, 0d
LI64 r34, 30d
CP r35, r34
LI64 r36, 100d
CP r37, r36
LI64 r35, 100d
CP r36, r33
CP r37, r33
CP r38, r33
CP r39, r37
4: ADDI64 r40, r35, 1d
JGTS r33, r40, :0
5: JLTU r37, r34, :0
ADDI64 r36, r36, 1d
CP r2, r33
CP r3, r33
CP r4, r37
CP r3, r36
CP r4, r34
JAL r31, r0, :set_pixel
CP r41, r37
ADDI64 r33, r33, 1d
CP r42, r33
JMP :1
0: CP r2, r33
CP r3, r38
CP r4, r39
JAL r31, r0, :set_pixel
CP r41, r39
CP r33, r32
ADDI64 r42, r38, 1d
1: JNE r42, r41, :2
JMP :3
2: CP r35, r34
CP r37, r36
CP r38, r42
CP r39, r41
CP r39, r1
JEQ r39, r38, :1
CP r1, r33
JMP :2
1: JNE r36, r35, :3
CP r1, r38
JMP :2
3: CP r37, r33
CP r40, r38
JMP :4
3: LD r31, r254, 0a, 96h
ADDI64 r254, r254, 96d
0: ADDI64 r40, r38, 1d
ADDI64 r41, r37, 1d
CP r37, r41
4: CP r38, r40
JMP :5
2: LD r31, r254, 0a, 88h
ADDI64 r254, r254, 88d
JALA r0, r31, 0a
set_pixel:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r1, 0d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
ADDI64 r254, r254, -16d
ST r31, r254, 0a, 16h
MUL64 r32, r4, r3
ADD64 r1, r32, r2
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
JALA r0, r31, 0a
check_platform:
ADDI64 r254, r254, -8d
@ -58,7 +55,6 @@ x86_fb_ptr:
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
timed out
code size: 437
ret: 0
code size: 423
ret: 3000
status: Ok(())

View file

@ -1,33 +1,30 @@
main:
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
LI64 r32, 10d
CP r2, r32
LI64 r2, 10d
JAL r31, r0, :add_one
CP r32, r1
LI64 r33, 20d
CP r2, r33
LI64 r2, 20d
JAL r31, r0, :add_two
ADD64 r1, r1, r32
CP r33, r1
ADD64 r1, r33, r32
LD r31, r254, 0a, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
add_two:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
CP r1, r2
ADDI64 r1, r1, 2d
ADDI64 r1, r2, 2d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
add_one:
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
CP r1, r2
ADDI64 r1, r1, 1d
ADDI64 r1, r2, 1d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
code size: 260
code size: 251
ret: 33
status: Ok(())

View file

@ -1,31 +1,29 @@
main:
ADDI64 r254, r254, -16d
ST r31, r254, 0a, 16h
LI64 r32, 10d
CP r2, r32
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r2, 10d
JAL r31, r0, :fib
LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
fib:
ADDI64 r254, r254, -40d
ST r31, r254, 0a, 40h
CP r32, r2
LI64 r33, 2d
JGTS r32, r33, :0
LI64 r1, 1d
LI64 r33, 2d
JGTU r32, r33, :0
JMP :1
0: ADDI64 r34, r32, -1d
CP r2, r34
0: SUB64 r2, r32, r1
JAL r31, r0, :fib
CP r34, r1
SUB64 r35, r32, r33
CP r2, r35
SUB64 r2, r32, r33
JAL r31, r0, :fib
ADD64 r1, r1, r34
CP r35, r1
ADD64 r1, r35, r34
1: LD r31, r254, 0a, 40h
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
code size: 221
code size: 208
ret: 55
status: Ok(())

View file

@ -1,4 +1,28 @@
main:
code size: 8
ret: 0
status: Err(Unreachable)
ADDI64 r254, r254, -8d
ST r31, r254, 0a, 8h
LI64 r2, 10d
JAL r31, r0, :fib
LD r31, r254, 0a, 8h
ADDI64 r254, r254, 8d
JALA r0, r31, 0a
fib:
ADDI64 r254, r254, -40d
ST r31, r254, 0a, 40h
LI64 r32, 1d
LI64 r33, 0d
CP r1, r33
CP r34, r32
2: JNE r2, r33, :0
JMP :1
0: ADD64 r35, r34, r1
SUB64 r2, r2, r32
CP r1, r34
CP r34, r35
JMP :2
1: LD r31, r254, 0a, 40h
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
code size: 198
ret: 55
status: Ok(())