Skip to content

Commit

Permalink
clean codegen of old logic of using is_mut as pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jan 2, 2024
1 parent ef2e8d6 commit 073e6db
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 59 deletions.
45 changes: 9 additions & 36 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ def gen_stmt(self, stmt):
)
self.cur_func.add_label(body_label)
value_t_ir = self.ir_type(iterable_sym.info.elem_typ)
value_is_ref_or_is_mut = stmt.value.is_ref or stmt.value.is_mut
value_t_is_boxed = isinstance(
value_t_ir, ir.Pointer
) and value_t_ir.is_managed
Expand All @@ -436,11 +435,11 @@ def gen_stmt(self, stmt):
), idx
]
)
if value_is_ref_or_is_mut and not isinstance(
if stmt.value.is_ref and not isinstance(
value_t_ir, ir.Pointer
):
value_t_ir = ir.Pointer(value_t_ir)
if not value_is_ref_or_is_mut or (
if not stmt.value.is_ref or (
isinstance(value_t_ir, ir.Pointer) and value_t_ir.is_managed
):
value = ir.Inst(ir.InstKind.LoadPtr, [value])
Expand Down Expand Up @@ -1211,7 +1210,7 @@ def gen_expr(self, expr, custom_tmp = None):
left_sym = expr.sym.self_typ.symbol()
if left_sym.kind == TypeKind.DynArray:
expr.sym = self.comp.dyn_array_sym[expr.sym.name]
sym_rec_is_ref = expr.sym.self_is_ptr or expr.sym.self_is_mut
sym_rec_is_ref = expr.sym.self_is_ptr or expr.sym.self_is_boxed
receiver = expr.left.left
if left_sym.kind == TypeKind.Trait and expr.sym.self_typ != receiver.typ:
self_expr = self.gen_expr_with_cast(
Expand All @@ -1235,12 +1234,6 @@ def gen_expr(self, expr, custom_tmp = None):
fn_arg = expr.sym.get_arg(i)
fn_arg_typ = fn_arg.typ
fn_arg_typ_sym = fn_arg_typ.symbol()
if fn_arg.is_mut and not isinstance(
fn_arg.typ, type.Ptr
) and not (
fn_arg_typ_sym.is_boxed() or fn_arg_typ_sym.is_primitive()
):
fn_arg_typ = type.Ptr(fn_arg_typ)
arg_value = self.gen_expr_with_cast(fn_arg_typ, arg.expr)
if expr.sym.is_method:
left_sym = expr.left.left_typ.symbol()
Expand Down Expand Up @@ -1867,9 +1860,7 @@ def gen_expr(self, expr, custom_tmp = None):
), expr_var_exit_label
)
var_t = self.ir_type(expr.var.typ)
var_t2 = var_t if isinstance(var_t, ir.Pointer) or (
expr.var.is_mut or expr.var.is_ref
) else var_t.ptr()
var_t2 = var_t if isinstance(var_t, ir.Pointer) else var_t.ptr()
if left_sym.kind == TypeKind.Enum:
union_name = f"{cg_utils.mangle_symbol(left_sym)}6_Union"
union_type = ir.Type(union_name)
Expand All @@ -1880,11 +1871,6 @@ def gen_expr(self, expr, custom_tmp = None):
)
if expr.var.is_ref:
val = ir.Inst(ir.InstKind.GetPtr, [val], var_t2)
elif expr.var.is_mut and not isinstance(
var_t, ir.Pointer
):
val = ir.Inst(ir.InstKind.GetPtr, [val], var_t2)
var_t = var_t.ptr()
else:
val = ir.Inst(
ir.InstKind.Cast, [
Expand All @@ -1894,7 +1880,7 @@ def gen_expr(self, expr, custom_tmp = None):
)
if not ((
isinstance(var_t2, ir.Pointer) and var_t2.is_managed
) or expr.var.is_mut or expr.var.is_ref):
) or expr.var.is_ref):
val = ir.Inst(ir.InstKind.LoadPtr, [val], var_t2)
unique_name = self.cur_func.unique_name(expr.var.name)
expr.scope.update_ir_name(expr.var.name, unique_name)
Expand Down Expand Up @@ -2158,10 +2144,6 @@ def gen_expr(self, expr, custom_tmp = None):
)
if b.var_is_ref:
val = ir.Inst(ir.InstKind.GetPtr, [val])
elif b.var_is_mut and not isinstance(
var_t, ir.Pointer
):
val = ir.Inst(ir.InstKind.GetPtr, [val])
else:
val = ir.Inst(
ir.InstKind.Cast, [
Expand All @@ -2172,16 +2154,10 @@ def gen_expr(self, expr, custom_tmp = None):
]
)
if not (
b.var_is_mut or (
isinstance(var_t, ir.Pointer)
and var_t.is_managed
)
isinstance(var_t, ir.Pointer)
and var_t.is_managed
):
val = ir.Inst(ir.InstKind.LoadPtr, [val])
if b.var_is_mut and not isinstance(
var_t, ir.Pointer
):
var_t = var_t.ptr(True)
unique_name = self.cur_func.unique_name(b.var_name)
b.scope.update_ir_name(b.var_name, unique_name)
self.cur_func.inline_alloca(var_t, unique_name, val)
Expand Down Expand Up @@ -2919,10 +2895,7 @@ def ir_type(self, typ, gen_self_arg = False):
if gen_self_arg:
args.append(ir.RAWPTR_T)
for arg in typ.args:
arg_t = self.ir_type(arg.typ)
if arg.is_mut and not isinstance(arg_t, ir.Pointer):
arg_t = ir.Pointer(arg_t)
args.append(arg_t)
args.append(self.ir_type(arg.typ))
return ir.Function(args, self.ir_type(typ.ret_typ))
elif isinstance(typ, type.Slice):
return ir.SLICE_T
Expand Down Expand Up @@ -3007,7 +2980,7 @@ def gen_types(self):
fields = [
ir.Field("_idx_", ir.UINT_T),
ir.Field("_id_", ir.UINT_T),
ir.Field("obj", ir.RAWPTR_T),
ir.Field("obj", ir.RAWPTR_T)
]
for f in ts.fields:
f_typ = self.ir_type(f.typ)
Expand Down
14 changes: 7 additions & 7 deletions tests/valid/src/defer_stmt.ri
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test "`defer` from a call" {
@assert(test_defer_call() == 5);
}

func test_defer_error(mut err_defer: ErrDefer, err: bool) -> ! {
func test_defer_error(err_defer: &mut ErrDefer, err: bool) -> ! {
defer err_defer.i += 1;
defer(error) err_defer.i += 2;

Expand All @@ -63,26 +63,26 @@ func test_defer_error(mut err_defer: ErrDefer, err: bool) -> ! {
test "`defer(error)` statement" {
mut err_defer := ErrDefer(i: 0);

test_defer_error(err_defer, false) catch { };
test_defer_error(&mut err_defer, false) catch { };
@assert(err_defer.i == 1);

err_defer.i = 0;
test_defer_error(err_defer, true) catch { };
test_defer_error(&mut err_defer, true) catch { };
@assert(err_defer.i == 3);
}

func throw_error() -> ! {
throw MyError();
}

func throw_and_defer_error(mut err_defer: ErrDefer) -> ! {
func throw_and_defer_error(err_defer: &mut ErrDefer) -> ! {
defer(error) err_defer.i = 4;
throw_error()!;
}

test "`defer(error)` with error propagation" {
mut err_defer := ErrDefer(i: 0);
throw_and_defer_error(err_defer) catch {};
throw_and_defer_error(&mut err_defer) catch {};
@assert(err_defer.i == 4);
}

Expand All @@ -95,7 +95,7 @@ test "`defer(success)`" {
@assert(sum == 0);
}

func throw_and_defer_success(mut err_defer: ErrDefer) -> ! {
func throw_and_defer_success(err_defer: &mut ErrDefer) -> ! {
// should not run, an error is thrown
defer(success) {
err_defer.i += 2;
Expand All @@ -108,7 +108,7 @@ func throw_and_defer_success(mut err_defer: ErrDefer) -> ! {

test "`defer(success)` with throwed error" {
mut err_defer := ErrDefer(i: 0);
throw_and_defer_error(err_defer) catch {};
throw_and_defer_error(&mut err_defer) catch {};
@assert(err_defer.i == 4);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/valid/src/dynamic_array.ri
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test "dynamic array from fixed array" {
arr := [1, 2, 3];
mut dyn_array := arr[:].to_dynamic_array();
dyn_array := arr[:].to_dynamic_array();
dyn_array.push(2);
@assert(dyn_array.len == 4);

Expand All @@ -11,12 +11,12 @@ test "dynamic array from fixed array" {

test "dynamic array constructor" {
{
mut da := []int32();
da := []int32();
da.push(5);
@assert(da[0] == 5);
}
{
mut da := []int32(cap: 2);
da := []int32(cap: 2);
da.push(10);
@assert(da.cap == 2);
@assert(da[0] == 10);
Expand Down
8 changes: 4 additions & 4 deletions tests/valid/src/enums.ri
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ test "enums: tagged enum" {
else -> @assert(false)
}
match e {
.Abc(mut e_abc2) -> {
e_abc2 += 2;
@assert(e_abc2 == 8);
.Abc(&mut e_abc2) -> {
e_abc2.* += 2;
@assert(e_abc2.* == 8);
},
else -> @assert(false)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ test "enums: tagged enum with fields" {
}

user_os = .Linux(kernel_version: 5_16_0, distro_name: "CentOS");
if user_os is .Linux(mut linux) {
if user_os is .Linux(&mut linux) {
@assert(linux.kernel_version == 5_16_0);
@assert(linux.distro_name == "CentOS");
linux.kernel_version = 5_12_0;
Expand Down
6 changes: 3 additions & 3 deletions tests/valid/src/for_stmt.ri
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ test "`for` statement with referenced value" {
}

test "`for` statement with mutable value" {
mut arr: []mut int32 := +[1, 2, 3];
for mut elem in arr {
elem = elem * 2;
arr: []mut int32 := +[1, 2, 3];
for &mut elem in arr {
elem.* = elem.* * 2;
}
@assert(arr == +[2, 4, 6]);
}
12 changes: 6 additions & 6 deletions tests/valid/src/traits.ri
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
trait Reader {
func read(self) -> uint8;
func read(+self) -> uint8;
}

trait Writer {
func write(self, b: uint8) -> bool;
func write(+self, b: uint8) -> bool;
}

trait ReaderWriter < Reader, Writer {
func write_and_read(self, b: uint8) -> uint8;
func write_and_read(+self, b: uint8) -> uint8;
}

struct SomeStream < ReaderWriter {
Expand Down Expand Up @@ -49,16 +49,16 @@ test "traits: inheritance" {
trait Eq {
x: string;

func equal(self, x: Self) -> bool;
func equal(+self, x: Self) -> bool;

func nequal(self, x: Self) -> bool {
func nequal(+self, x: Self) -> bool {
return !self.equal(x);
}
}

#[boxed]
struct Poketrait < Eq {
pub func equal(self, x: Eq) -> bool {
pub func equal(+self, x: Eq) -> bool {
return self.x == x.x;
}
}
Expand Down

0 comments on commit 073e6db

Please sign in to comment.