diff --git a/lang/README.md b/lang/README.md index a41877f..fcdc7b2 100644 --- a/lang/README.md +++ b/lang/README.md @@ -324,11 +324,11 @@ note: values of global variables are evaluated at compile time #### constants ```hb -$some_const := 34 - -main := fn(): uint { +main := fn(): u32 { return some_const + 35 } + +$some_const := 34 ``` #### directives diff --git a/lang/src/parser.rs b/lang/src/parser.rs index 161bec7..f65ad9f 100644 --- a/lang/src/parser.rs +++ b/lang/src/parser.rs @@ -399,7 +399,12 @@ impl<'a, 'b> Parser<'a, 'b> { }, T::Ident | T::CtIdent => { let (id, is_first) = self.resolve_ident(token); - E::Ident { pos, is_ct: token.kind == T::CtIdent, id, is_first } + E::Ident { + pos: pos - (token.kind == T::CtIdent) as Pos, + is_ct: token.kind == T::CtIdent, + id, + is_first, + } } T::Under => E::Wildcard { pos }, T::If => E::If { diff --git a/lang/src/son.rs b/lang/src/son.rs index 2d27ba0..a90ab5f 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -2517,7 +2517,7 @@ impl<'a> Codegen<'a> { match decl.expand() { ty::Kind::NEVER => Value::NEVER, ty::Kind::Global(global) => self.gen_global(global), - ty::Kind::Const(cnst) => self.gen_const(cnst), + ty::Kind::Const(cnst) => self.gen_const(cnst, ctx), _ => Some(Value::new(Nid::MAX).ty(decl)), } } @@ -2636,7 +2636,7 @@ impl<'a> Codegen<'a> { { ty::Kind::NEVER => Value::NEVER, ty::Kind::Global(global) => self.gen_global(global), - ty::Kind::Const(cnst) => self.gen_const(cnst), + ty::Kind::Const(cnst) => self.gen_const(cnst, ctx), v => Some(Value::new(Nid::MAX).ty(v.compress())), }; } @@ -3783,14 +3783,14 @@ impl<'a> Codegen<'a> { Some(Value::ptr(value).ty(gl.ty)) } - fn gen_const(&mut self, cnst: ty::Const) -> Option { + fn gen_const(&mut self, cnst: ty::Const, ctx: Ctx) -> Option { let c = &self.tys.ins.consts[cnst]; let f = &self.files[c.file.index()]; let Expr::BinOp { left, right, .. } = c.ast.get(f) else { unreachable!() }; left.find_pattern_path(c.name, right, |expr, is_ct| { debug_assert!(is_ct); - self.expr(expr) + self.expr_ctx(expr, ctx) }) .unwrap_or_else(|_| unreachable!()) } diff --git a/lang/tests/son_tests_constants.txt b/lang/tests/son_tests_constants.txt index 924b2e6..7e20e34 100644 --- a/lang/tests/son_tests_constants.txt +++ b/lang/tests/son_tests_constants.txt @@ -1,6 +1,6 @@ main: - LI64 r1, 69d + LI32 r1, 69w JALA r0, r31, 0a -code size: 29 +code size: 25 ret: 69 status: Ok(()) diff --git a/lang/tests/son_tests_null_check_returning_small_global.txt b/lang/tests/son_tests_null_check_returning_small_global.txt index e69de29..4e6e3ef 100644 --- a/lang/tests/son_tests_null_check_returning_small_global.txt +++ b/lang/tests/son_tests_null_check_returning_small_global.txt @@ -0,0 +1,80 @@ +foo: + ADDI64 r254, r254, -200d + ST r31, r254, 80a, 120h + ADDI64 r32, r254, 64d + LRA r3, r0, :some_file + JAL r31, r0, :get + ST r1, r254, 64a, 16h + LI8 r33, 0b + LD r34, r254, 64a, 1h + ANDI r34, r34, 255d + ANDI r33, r33, 255d + JNE r34, r33, :0 + ADDI64 r35, r254, 48d + ST r33, r254, 48a, 1h + LD r1, r35, 0a, 16h + JMP :1 + 0: LI8 r36, 1b + LI64 r37, 4d + LD r38, r254, 72a, 8h + JNE r38, r37, :2 + ADDI64 r39, r254, 32d + ST r36, r254, 32a, 1h + LI64 r40, 2d + ST r40, r254, 40a, 8h + LD r1, r39, 0a, 16h + JMP :1 + 2: LRA r41, r0, :MAGIC + LD r42, r41, 0a, 8h + JNE r42, r38, :3 + ADDI64 r43, r254, 16d + ST r36, r254, 16a, 1h + LI64 r44, 0d + ST r44, r254, 24a, 8h + LD r1, r43, 0a, 16h + JMP :1 + 3: ADDI64 r45, r254, 0d + ST r33, r254, 0a, 1h + LD r1, r45, 0a, 16h + 1: LD r31, r254, 80a, 120h + ADDI64 r254, r254, 200d + JALA r0, r31, 0a +get: + ADDI64 r254, r254, -32d + LD r3, r3, 0a, 1h + LRA r5, r0, :MAGIC + ANDI r9, r3, 255d + LD r8, r5, 0a, 8h + JNE r8, r9, :0 + LI8 r2, 1b + ADDI64 r1, r254, 16d + ST r2, r254, 16a, 1h + ST r8, r254, 24a, 8h + LD r1, r1, 0a, 16h + JMP :1 + 0: ADDI64 r7, r254, 0d + LI8 r9, 0b + ST r9, r254, 0a, 1h + LD r1, r7, 0a, 16h + 1: ADDI64 r254, r254, 32d + JALA r0, r31, 0a +main: + ADDI64 r254, r254, -48d + ST r31, r254, 16a, 32h + ADDI64 r32, r254, 0d + JAL r31, r0, :foo + ST r1, r254, 0a, 16h + LI8 r33, 0b + LD r34, r254, 0a, 1h + ANDI r34, r34, 255d + ANDI r33, r33, 255d + JNE r34, r33, :0 + LI64 r1, 100d + JMP :1 + 0: LD r1, r254, 8a, 8h + 1: LD r31, r254, 16a, 32h + ADDI64 r254, r254, 48d + JALA r0, r31, 0a +code size: 768 +ret: 0 +status: Ok(()) diff --git a/lang/tests/son_tests_nullable_structure.txt b/lang/tests/son_tests_nullable_structure.txt index cb6c81f..5599901 100644 --- a/lang/tests/son_tests_nullable_structure.txt +++ b/lang/tests/son_tests_nullable_structure.txt @@ -12,18 +12,18 @@ main: LI8 r35, 0b LD r36, r254, 2a, 1h CP r1, r32 - ANDI r37, r37, 255d ANDI r1, r1, 255d + ANDI r34, r34, 255d CMPU r37, r1, r34 CMPUI r37, r37, 0d - ANDI r38, r38, 255d ANDI r36, r36, 255d + ANDI r35, r35, 255d CMPU r38, r36, r35 CMPUI r38, r38, 0d LD r39, r254, 0a, 1h AND r40, r38, r37 - ANDI r41, r41, 255d ANDI r39, r39, 255d + ANDI r35, r35, 255d CMPU r41, r39, r35 CMPUI r41, r41, 0d AND r42, r41, r40