adding better binaro operator errors positions

This commit is contained in:
Jakub Doka 2024-10-25 15:40:23 +02:00
parent 423361a80e
commit e62aab9b4b
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
16 changed files with 77 additions and 70 deletions

View file

@ -1352,7 +1352,7 @@ impl Codegen {
), ),
} }
} }
E::BinOp { left, op: T::Decl, right } if self.has_ct(left) => { E::BinOp { left, op: T::Decl, right, .. } if self.has_ct(left) => {
let slot_base = self.ct.vm.read_reg(reg::STACK_PTR).0; let slot_base = self.ct.vm.read_reg(reg::STACK_PTR).0;
let (cnt, ty) = self.eval_const_low(self.ci.file, right, None); let (cnt, ty) = self.eval_const_low(self.ci.file, right, None);
if self.assign_ct_pattern(left, ty, cnt as _) { if self.assign_ct_pattern(left, ty, cnt as _) {
@ -1360,7 +1360,7 @@ impl Codegen {
} }
Some(Value::void()) Some(Value::void())
} }
E::BinOp { left, op: T::Decl, right } => { E::BinOp { left, op: T::Decl, right, .. } => {
let value = self.expr(right)?; let value = self.expr(right)?;
self.assign_pattern(left, value) self.assign_pattern(left, value)
} }
@ -1506,7 +1506,7 @@ impl Codegen {
E::If { cond, then, mut else_, .. } => { E::If { cond, then, mut else_, .. } => {
let mut then = Some(then); let mut then = Some(then);
let jump_offset; let jump_offset;
if let &E::BinOp { left, op, right } = cond if let &E::BinOp { left, op, right, .. } = cond
&& let ty = self.infer_type(left) && let ty = self.infer_type(left)
&& let Some((op, swapped)) = op.cond_op(ty.is_signed()) && let Some((op, swapped)) = op.cond_op(ty.is_signed())
{ {
@ -1605,7 +1605,7 @@ impl Codegen {
self.ci.emit(jmp(loop_.offset as i32 - offset as i32)); self.ci.emit(jmp(loop_.offset as i32 - offset as i32));
None None
} }
E::BinOp { left, op: op @ (T::And | T::Or), right } => { E::BinOp { left, op: op @ (T::And | T::Or), right, .. } => {
let lhs = self.expr_ctx(left, Ctx::default().with_ty(ty::BOOL))?; let lhs = self.expr_ctx(left, Ctx::default().with_ty(ty::BOOL))?;
let lhs = self.loc_to_reg(lhs.loc, 1); let lhs = self.loc_to_reg(lhs.loc, 1);
let jump_offset = self.ci.code.len() + 3; let jump_offset = self.ci.code.len() + 3;
@ -1622,7 +1622,7 @@ impl Codegen {
Some(Value { ty: ty::Id::BOOL, loc: Loc::reg(lhs) }) Some(Value { ty: ty::Id::BOOL, loc: Loc::reg(lhs) })
} }
E::BinOp { left, op, right } if op != T::Decl => 'ops: { E::BinOp { left, op, right, .. } if op != T::Decl => 'ops: {
let left = self.expr_ctx(left, Ctx { let left = self.expr_ctx(left, Ctx {
ty: ctx.ty.filter(|_| op.is_homogenous()), ty: ctx.ty.filter(|_| op.is_homogenous()),
check: ctx.check, check: ctx.check,

View file

@ -358,7 +358,8 @@ impl<'a> Formatter<'a> {
Expr::BinOp { Expr::BinOp {
left, left,
op: TokenKind::Assign, op: TokenKind::Assign,
right: &Expr::BinOp { left: lleft, op, right }, right: &Expr::BinOp { left: lleft, op, right, .. },
..
} if left.pos() == lleft.pos() => { } if left.pos() == lleft.pos() => {
self.fmt(left, f)?; self.fmt(left, f)?;
f.write_str(" ")?; f.write_str(" ")?;
@ -366,7 +367,7 @@ impl<'a> Formatter<'a> {
f.write_str("= ")?; f.write_str("= ")?;
self.fmt(right, f) self.fmt(right, f)
} }
Expr::BinOp { right, op, left } => { Expr::BinOp { right, op, left, .. } => {
let prec_miss_left = |e: &Expr| { let prec_miss_left = |e: &Expr| {
matches!( matches!(
e, Expr::BinOp { op: lop, .. } if op.precedence() > lop.precedence() e, Expr::BinOp { op: lop, .. } if op.precedence() > lop.precedence()

View file

@ -159,7 +159,7 @@ impl<'a, 'b> Parser<'a, 'b> {
break; break;
} }
let op = self.next().kind; let Token { kind: op, start: pos, .. } = self.next();
if op == TokenKind::Decl { if op == TokenKind::Decl {
self.declare_rec(&fold, top_level); self.declare_rec(&fold, top_level);
@ -172,10 +172,15 @@ impl<'a, 'b> Parser<'a, 'b> {
if let Some(op) = op.ass_op() { if let Some(op) = op.ass_op() {
self.flag_idents(*left, idfl::MUTABLE); self.flag_idents(*left, idfl::MUTABLE);
let right = Expr::BinOp { left: self.arena.alloc(fold), op, right }; let right = Expr::BinOp { left: self.arena.alloc(fold), pos, op, right };
fold = Expr::BinOp { left, op: TokenKind::Assign, right: self.arena.alloc(right) }; fold = Expr::BinOp {
left,
pos,
op: TokenKind::Assign,
right: self.arena.alloc(right),
};
} else { } else {
fold = Expr::BinOp { left, right, op }; fold = Expr::BinOp { left, right, pos, op };
if op == TokenKind::Assign { if op == TokenKind::Assign {
self.flag_idents(*left, idfl::MUTABLE); self.flag_idents(*left, idfl::MUTABLE);
} }
@ -762,6 +767,7 @@ generate_expr! {
/// `Expr OP Expr` /// `Expr OP Expr`
BinOp { BinOp {
left: &'a Self, left: &'a Self,
pos: Pos,
op: TokenKind, op: TokenKind,
right: &'a Self, right: &'a Self,
}, },

View file

@ -2265,7 +2265,7 @@ impl<'a> Codegen<'a> {
} }
Some(self.ci.nodes.new_node_lit(val.ty, Kind::UnOp { op }, [VOID, val.id])) Some(self.ci.nodes.new_node_lit(val.ty, Kind::UnOp { op }, [VOID, val.id]))
} }
Expr::BinOp { left, op: TokenKind::Decl, right } => { Expr::BinOp { left, op: TokenKind::Decl, right, .. } => {
let mut right = self.expr(right)?; let mut right = self.expr(right)?;
if right.ty.loc(&self.tys) == Loc::Stack { if right.ty.loc(&self.tys) == Loc::Stack {
let stck = self.ci.nodes.new_node_nop(right.ty, Kind::Stck, [VOID, MEM]); let stck = self.ci.nodes.new_node_nop(right.ty, Kind::Stck, [VOID, MEM]);
@ -2276,11 +2276,11 @@ impl<'a> Codegen<'a> {
self.assign_pattern(left, right); self.assign_pattern(left, right);
Some(Value::VOID) Some(Value::VOID)
} }
Expr::BinOp { left, op: TokenKind::Assign, right } => { Expr::BinOp { left, pos, op: TokenKind::Assign, right } => {
let dest = self.raw_expr(left)?; let dest = self.raw_expr(left)?;
let mut value = self.expr_ctx(right, Ctx::default().with_ty(dest.ty))?; let mut value = self.expr_ctx(right, Ctx::default().with_ty(dest.ty))?;
self.assert_ty(left.pos(), &mut value, dest.ty, "assignment source"); self.assert_ty(pos, &mut value, dest.ty, "assignment source");
if dest.var { if dest.var {
let var = &mut self.ci.scope.vars[(u16::MAX - dest.id) as usize]; let var = &mut self.ci.scope.vars[(u16::MAX - dest.id) as usize];
@ -2295,12 +2295,12 @@ impl<'a> Codegen<'a> {
} else if dest.ptr { } else if dest.ptr {
self.store_mem(dest.id, dest.ty, value.id); self.store_mem(dest.id, dest.ty, value.id);
} else { } else {
self.report(left.pos(), "cannot assign to this expression"); self.report(pos, "cannot assign to this expression");
} }
Some(Value::VOID) Some(Value::VOID)
} }
Expr::BinOp { left, op, right } Expr::BinOp { left, pos, op, right }
if !matches!(op, TokenKind::Assign | TokenKind::Decl) => if !matches!(op, TokenKind::Assign | TokenKind::Decl) =>
{ {
let mut lhs = self.raw_expr_ctx(left, ctx)?; let mut lhs = self.raw_expr_ctx(left, ctx)?;
@ -2316,7 +2316,7 @@ impl<'a> Codegen<'a> {
self.ci.nodes.unlock(lhs.id); self.ci.nodes.unlock(lhs.id);
let mut rhs = rhs?; let mut rhs = rhs?;
self.strip_var(&mut rhs); self.strip_var(&mut rhs);
let ty = self.binop_ty(right.pos(), &mut lhs, &mut rhs, op); let ty = self.binop_ty(pos, &mut lhs, &mut rhs, op);
let inps = [VOID, lhs.id, rhs.id]; let inps = [VOID, lhs.id, rhs.id];
Some(self.ci.nodes.new_node_lit( Some(self.ci.nodes.new_node_lit(
ty::bin_ret(ty, op), ty::bin_ret(ty, op),
@ -2330,15 +2330,15 @@ impl<'a> Codegen<'a> {
self.ci.nodes.unlock(lhs.id); self.ci.nodes.unlock(lhs.id);
let mut rhs = rhs?; let mut rhs = rhs?;
self.strip_var(&mut rhs); self.strip_var(&mut rhs);
self.assert_ty(right.pos(), &mut rhs, lhs.ty, "struct operand"); self.assert_ty(pos, &mut rhs, lhs.ty, "struct operand");
let dst = self.ci.nodes.new_node(lhs.ty, Kind::Stck, [VOID, MEM]); let dst = self.ci.nodes.new_node(lhs.ty, Kind::Stck, [VOID, MEM]);
self.struct_op(left.pos(), op, s, dst, lhs.id, rhs.id); self.struct_op(left.pos(), op, s, dst, lhs.id, rhs.id);
Some(Value::ptr(dst).ty(lhs.ty)) Some(Value::ptr(dst).ty(lhs.ty))
} }
_ => { _ => {
self.report( self.report(
left.pos(), pos,
fa!("'{0} {op} {0}' is not supported", self.ty_display(lhs.ty),), fa!("'{} {op} _' is not supported", self.ty_display(lhs.ty)),
); );
Value::NEVER Value::NEVER
} }

View file

@ -4,9 +4,9 @@ main:
LI64 r7, 4d LI64 r7, 4d
LI64 r8, 2d LI64 r8, 2d
LI64 r9, 1d LI64 r9, 1d
LI64 r6, 1d LI8 r6, 1b
LI64 r10, 255d LI8 r10, 255b
LI64 r11, 0d LI8 r11, 0b
ADDI64 r12, r254, 0d ADDI64 r12, r254, 0d
ST r11, r254, 0a, 1h ST r11, r254, 0a, 1h
ST r11, r254, 1a, 1h ST r11, r254, 1a, 1h
@ -24,14 +24,14 @@ main:
ADDI64 r254, r254, 36d ADDI64 r254, r254, 36d
JALA r0, r31, 0a JALA r0, r31, 0a
pass: pass:
LD r3, r2, 8a, 8h LD r4, r2, 8a, 8h
MULI64 r8, r3, 8d MULI64 r8, r4, 8d
ADD64 r10, r8, r2 ADD64 r10, r8, r2
LD r8, r10, 0a, 8h LD r8, r10, 0a, 8h
LD r9, r2, 0a, 8h LD r7, r2, 0a, 8h
ADD64 r11, r3, r9 ADD64 r11, r4, r7
ADD64 r1, r8, r11 ADD64 r1, r8, r11
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 348 code size: 327
ret: 8 ret: 8
status: Ok(()) status: Ok(())

View file

@ -12,7 +12,7 @@ main:
ADDI64 r254, r254, 16d ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
str_len: str_len:
LI64 r6, 0d LI8 r6, 0b
LI64 r1, 0d LI64 r1, 0d
2: LD r8, r2, 0a, 1h 2: LD r8, r2, 0a, 1h
ANDI r8, r8, 255d ANDI r8, r8, 255d
@ -23,6 +23,6 @@ str_len:
ADDI64 r1, r1, 1d ADDI64 r1, r1, 1d
JMP :2 JMP :2
1: JALA r0, r31, 0a 1: JALA r0, r31, 0a
code size: 226 code size: 219
ret: 16 ret: 16
status: Ok(()) status: Ok(())

View file

@ -1,9 +1,9 @@
main: main:
ADDI64 r254, r254, -24d ADDI64 r254, r254, -24d
LI64 r6, 2d LI32 r6, 2w
LI64 r5, 0d LI32 r5, 0w
LI64 r8, 0d LI8 r8, 0b
LI64 r9, 255d LI8 r9, 255b
ADDI64 r10, r254, 0d ADDI64 r10, r254, 0d
ADDI64 r7, r254, 12d ADDI64 r7, r254, 12d
ST r9, r254, 12a, 1h ST r9, r254, 12a, 1h
@ -43,6 +43,6 @@ main:
ANDI r1, r12, 4294967295d ANDI r1, r12, 4294967295d
1: ADDI64 r254, r254, 24d 1: ADDI64 r254, r254, 24d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 449 code size: 427
ret: 512 ret: 512
status: Ok(()) status: Ok(())

View file

@ -7,7 +7,7 @@ add:
main: main:
ADDI64 r254, r254, -16d ADDI64 r254, r254, -16d
ST r31, r254, 0a, 16h ST r31, r254, 0a, 16h
LI64 r3, 2d LI32 r3, 2w
CP r2, r3 CP r2, r3
JAL r31, r0, :add JAL r31, r0, :add
CP r32, r1 CP r32, r1
@ -20,6 +20,6 @@ main:
LD r31, r254, 0a, 16h LD r31, r254, 0a, 16h
ADDI64 r254, r254, 16d ADDI64 r254, r254, 16d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 165 code size: 161
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,20 +1,20 @@
main: main:
ADDI64 r254, r254, -128d ADDI64 r254, r254, -128d
LI64 r6, 69d LI8 r7, 69b
LI64 r5, 128d LI64 r5, 128d
LI64 r7, 0d LI64 r6, 0d
ADDI64 r4, r254, 0d ADDI64 r4, r254, 0d
2: JLTU r7, r5, :0 2: JLTU r6, r5, :0
LD r2, r254, 42a, 1h LD r3, r254, 42a, 1h
ANDI r1, r2, 255d ANDI r1, r3, 255d
JMP :1 JMP :1
0: ADDI64 r8, r7, 1d 0: ADDI64 r8, r6, 1d
ADD64 r3, r7, r4 ADD64 r6, r6, r4
ST r6, r3, 0a, 1h ST r7, r6, 0a, 1h
CP r7, r8 CP r6, r8
JMP :2 JMP :2
1: ADDI64 r254, r254, 128d 1: ADDI64 r254, r254, 128d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 152 code size: 145
ret: 69 ret: 69
status: Ok(()) status: Ok(())

View file

@ -15,8 +15,8 @@ main:
JALA r0, r31, 0a JALA r0, r31, 0a
set: set:
ADDI64 r254, r254, -25d ADDI64 r254, r254, -25d
LI64 r9, 8d LI32 r9, 8w
LI64 r8, 5d LI8 r8, 5b
ANDI r10, r4, 4294967295d ANDI r10, r4, 4294967295d
ADDI64 r1, r254, 0d ADDI64 r1, r254, 0d
ST r8, r254, 0a, 1h ST r8, r254, 0a, 1h
@ -27,6 +27,6 @@ set:
LD r1, r254, 1a, 4h LD r1, r254, 1a, 4h
ADDI64 r254, r254, 25d ADDI64 r254, r254, 25d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 309 code size: 298
ret: 1024 ret: 1024
status: Ok(()) status: Ok(())

View file

@ -7,7 +7,7 @@ create_back_buffer:
JAL r31, r0, :request_page JAL r31, r0, :request_page
JMP :1 JMP :1
0: CP r33, r2 0: CP r33, r2
LI64 r34, 255d LI8 r34, 255b
CP r2, r34 CP r2, r34
JAL r31, r0, :request_page JAL r31, r0, :request_page
CP r35, r1 CP r35, r1
@ -45,6 +45,6 @@ request_page:
ST r12, r4, 1a, 1h ST r12, r4, 1a, 1h
ECA ECA
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 346 code size: 339
ret: 42 ret: 42
status: Ok(()) status: Ok(())

View file

@ -1,8 +1,8 @@
main: main:
ADDI64 r254, r254, -168d ADDI64 r254, r254, -168d
LI64 r11, 4d LI8 r11, 4b
LI64 r5, 1d LI8 r5, 1b
LI64 r3, 0d LI8 r3, 0b
ADDI64 r6, r254, 144d ADDI64 r6, r254, 144d
ADDI64 r6, r254, 148d ADDI64 r6, r254, 148d
ST r3, r254, 148a, 1h ST r3, r254, 148a, 1h
@ -115,6 +115,6 @@ main:
ADD64 r1, r5, r3 ADD64 r1, r5, r3
1: ADDI64 r254, r254, 168d 1: ADDI64 r254, r254, 168d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 1247 code size: 1226
ret: 10 ret: 10
status: Ok(()) status: Ok(())

View file

@ -34,7 +34,7 @@ fib_iter:
main: main:
ADDI64 r254, r254, -18d ADDI64 r254, r254, -18d
ST r31, r254, 2a, 16h ST r31, r254, 2a, 16h
LI64 r1, 10d LI8 r1, 10b
ADDI64 r3, r254, 0d ADDI64 r3, r254, 0d
ST r1, r254, 0a, 1h ST r1, r254, 0a, 1h
ST r1, r254, 1a, 1h ST r1, r254, 1a, 1h
@ -50,6 +50,6 @@ main:
LD r31, r254, 2a, 16h LD r31, r254, 2a, 16h
ADDI64 r254, r254, 18d ADDI64 r254, r254, 18d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 382 code size: 375
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,6 +1,6 @@
foo: foo:
ADDI64 r254, r254, -16d ADDI64 r254, r254, -16d
LI64 r3, 2d LI32 r3, 2w
LI64 r5, 3d LI64 r5, 3d
ADDI64 r4, r254, 0d ADDI64 r4, r254, 0d
ST r5, r254, 0a, 8h ST r5, r254, 0a, 8h
@ -33,6 +33,6 @@ main:
LD r31, r254, 48a, 8h LD r31, r254, 48a, 8h
ADDI64 r254, r254, 56d ADDI64 r254, r254, 56d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 359 code size: 355
ret: 0 ret: 0
status: Ok(()) status: Ok(())

View file

@ -1,6 +1,6 @@
main: main:
ADDI64 r254, r254, -10240d ADDI64 r254, r254, -10240d
LI64 r6, 64d LI8 r6, 64b
LI64 r7, 1024d LI64 r7, 1024d
LI64 r9, 1d LI64 r9, 1d
LI64 r8, 0d LI64 r8, 0d
@ -25,6 +25,6 @@ main:
JMP :4 JMP :4
2: ADDI64 r254, r254, 10240d 2: ADDI64 r254, r254, 10240d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 205 code size: 198
ret: 64 ret: 64
status: Ok(()) status: Ok(())

View file

@ -8,10 +8,10 @@ main:
ST r1, r254, 16a, 16h ST r1, r254, 16a, 16h
ADDI64 r7, r254, 0d ADDI64 r7, r254, 0d
BMC r32, r7, 16h BMC r32, r7, 16h
LD r1, r254, 12a, 1h LD r12, r254, 12a, 1h
LD r11, r254, 3a, 1h LD r11, r254, 3a, 1h
SUB8 r2, r11, r1 SUB8 r3, r11, r12
ANDI r1, r2, 255d ANDI r1, r3, 255d
LD r31, r254, 32a, 16h LD r31, r254, 32a, 16h
ADDI64 r254, r254, 48d ADDI64 r254, r254, 48d
JALA r0, r31, 0a JALA r0, r31, 0a
@ -21,9 +21,9 @@ maina:
ADDI64 r32, r254, 36d ADDI64 r32, r254, 36d
JAL r31, r0, :small_struct JAL r31, r0, :small_struct
ST r1, r254, 36a, 4h ST r1, r254, 36a, 4h
LI64 r2, 1d LI8 r2, 1b
LI64 r3, 3d LI8 r3, 3b
LI64 r1, 0d LI8 r1, 0b
ADDI64 r6, r254, 0d ADDI64 r6, r254, 0d
ADDI64 r8, r6, 8d ADDI64 r8, r6, 8d
ADDI64 r7, r254, 16d ADDI64 r7, r254, 16d
@ -47,13 +47,13 @@ maina:
JALA r0, r31, 0a JALA r0, r31, 0a
small_struct: small_struct:
ADDI64 r254, r254, -4d ADDI64 r254, r254, -4d
LI64 r2, 0d LI16 r2, 0h
ADDI64 r3, r254, 0d ADDI64 r3, r254, 0d
ST r2, r254, 0a, 2h ST r2, r254, 0a, 2h
ST r2, r254, 2a, 2h ST r2, r254, 2a, 2h
LD r1, r3, 0a, 4h LD r1, r3, 0a, 4h
ADDI64 r254, r254, 4d ADDI64 r254, r254, 4d
JALA r0, r31, 0a JALA r0, r31, 0a
code size: 573 code size: 546
ret: 2 ret: 2
status: Ok(()) status: Ok(())