Skip to content

Commit 19ae752

Browse files
committed
Zero-initialize uninitialized variables in JIT
Added logic to zero-initialize variables when no explicit initializer is provided. Introduced a gen_zero function to handle zeroing memory, and added a test case to verify zero-initialization behavior.
1 parent 5123306 commit 19ae752

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/jit.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ impl<'a> FunctionTranslator<'a> {
394394
if let Some(init_id) = init {
395395
let init_value = self.translate_expr(*init_id, decl, decls);
396396
self.gen_copy(*ty, addr, init_value, decls);
397+
} else {
398+
self.gen_zero(*ty, addr, decls);
397399
}
398400

399401
self.builder.ins().iconst(I32, 0)
@@ -519,6 +521,20 @@ impl<'a> FunctionTranslator<'a> {
519521
}
520522
}
521523

524+
fn gen_zero(
525+
&mut self,
526+
t: crate::TypeID,
527+
dst: Value,
528+
decls: &crate::DeclTable,
529+
) {
530+
let size = t.size(decls) as u32;
531+
let zero = self.builder.ins().iconst(I8, 0);
532+
// Store zero byte-by-byte for the size of the type
533+
for offset in 0..size {
534+
self.builder.ins().store(MemFlags::new(), zero, dst, offset as i32);
535+
}
536+
}
537+
522538
fn gen_eq(
523539
&mut self,
524540
t: crate::TypeID,

tests/cases/zero_init.lyte

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// args: --c
2+
// expected stdout:
3+
// compilation successful
4+
5+
assert(cond: bool) → void
6+
7+
main {
8+
var i: i32
9+
assert(i == 0)
10+
}

0 commit comments

Comments
 (0)