Skip to content

Commit 92654b0

Browse files
committed
default declared variables to uninitialized
This is a special value that should only be used to reserve stack slots for locals. Loading this value is a runtime error.
1 parent c1846d4 commit 92654b0

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

include/lang/vm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ enum compile_time_constant_objects {
8989
/* obj_meson = 2, */
9090
obj_bool_true = 3,
9191
obj_bool_false = 4,
92+
obj_uninitialized = 5,
9293
compile_time_constant_objects_end,
9394
};
9495

src/lang/compiler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static void
424424
vm_comp_reserve_local_stack_slot(struct workspace *wk, obj id)
425425
{
426426
push_code(wk, op_constant);
427-
push_constant(wk, id);
427+
push_constant(wk, obj_uninitialized);
428428
}
429429

430430
static void

src/lang/object.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "compat.h"
99

10+
#include "lang/vm.h"
1011
#include <inttypes.h>
1112
#include <stdlib.h>
1213

@@ -106,6 +107,9 @@ make_default_objects(struct workspace *wk)
106107
assert(id == obj_bool_false);
107108
*(bool *)get_obj_internal(wk, id, obj_bool) = false;
108109

110+
id = make_obj(wk, obj_null);
111+
assert(id == obj_uninitialized);
112+
109113
making_default_objects = false;
110114
}
111115

@@ -1890,6 +1894,11 @@ obj_to_s_opts(struct workspace *wk, obj o, struct tstr *sb, struct obj_to_s_opts
18901894
struct obj_to_s_ctx ctx = { .sb = sb, .opts = opts };
18911895
enum obj_type t = get_obj_type(wk, o);
18921896

1897+
if (o == obj_uninitialized) {
1898+
tstr_pushs(wk, sb, "<uninit>");
1899+
return;
1900+
}
1901+
18931902
switch (t) {
18941903
case obj_include_directory: {
18951904
struct obj_include_directory *inc = get_obj_include_directory(wk, o);

src/lang/vm.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,15 @@ vm_op_add_store_m(struct workspace *wk)
20502050
object_stack_push(wk, *member_target);
20512051
}
20522052

2053+
#define vm_op_store_uninit_check(__val) \
2054+
if (__val == obj_uninitialized) \
2055+
vm_diagnostic(wk, \
2056+
0, \
2057+
wk->vm.in_analyzer ? log_warn : log_error, \
2058+
0, \
2059+
"load of %suninitialized variable", \
2060+
wk->vm.in_analyzer ? "potentially " : "")
2061+
20532062
#define vm_op_store_load_l_common() \
20542063
const struct call_frame *frame = arr_get(&wk->vm.call_stack, wk->vm.call_stack.len - 1); \
20552064
obj slot_idx = vm_get_constant(wk->vm.code.e, &wk->vm.ip) + frame->stack_base; \
@@ -2059,6 +2068,8 @@ static void
20592068
vm_op_load_l(struct workspace *wk)
20602069
{
20612070
vm_op_store_load_l_common();
2071+
vm_op_store_uninit_check(slot->o);
2072+
20622073
object_stack_push(wk, slot->o);
20632074
}
20642075

@@ -2076,6 +2087,7 @@ static void
20762087
vm_op_add_store_l(struct workspace *wk)
20772088
{
20782089
vm_op_store_load_l_common();
2090+
vm_op_store_uninit_check(slot->o);
20792091

20802092
const struct obj_stack_entry *src = object_stack_pop_entry(&wk->vm.stack);
20812093
slot->o = vm_perform_add_store_mutations(wk, src->o, slot->o);
@@ -2086,12 +2098,13 @@ vm_op_add_store_l(struct workspace *wk)
20862098
#define vm_op_store_load_u_common() \
20872099
const struct call_frame *frame = arr_get(&wk->vm.call_stack, wk->vm.call_stack.len - 1); \
20882100
obj slot_idx = vm_get_constant(wk->vm.code.e, &wk->vm.ip); \
2089-
obj *slot = frame->closure->upvalues[slot_idx]->location;
2101+
obj *slot = frame->closure->upvalues[slot_idx]->location \
20902102

20912103
static void
20922104
vm_op_load_u(struct workspace *wk)
20932105
{
20942106
vm_op_store_load_u_common();
2107+
vm_op_store_uninit_check(*slot);
20952108
object_stack_push(wk, *slot);
20962109
}
20972110

@@ -2107,6 +2120,7 @@ static void
21072120
vm_op_add_store_u(struct workspace *wk)
21082121
{
21092122
vm_op_store_load_u_common();
2123+
vm_op_store_uninit_check(*slot);
21102124
obj val = object_stack_pop(&wk->vm.stack);
21112125
*slot = vm_perform_add_store_mutations(wk, val, *slot);
21122126
object_stack_push(wk, *slot);

0 commit comments

Comments
 (0)