Skip to content

Commit

Permalink
CORE: Zero initialize variables without an initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 26, 2024
1 parent 6754eef commit 122a7be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 12 additions & 8 deletions core/codegen/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ func (c *codegen) VisitVar(stmt *ast.Var) {
c.scopes.addVariable(stmt.Name, stmt.ActualType, pointer, 0)

// Initializer
var initializer ir.Value

if stmt.Value != nil {
initializer := c.implicitCastLoadExpr(stmt.ActualType, stmt.Value)
initializer = c.implicitCastLoadExpr(stmt.ActualType, stmt.Value).v
} else {
initializer = &ir.ZeroInitConst{Typ: c.types.get(stmt.ActualType)}
}

store := c.block.Add(&ir.StoreInst{
Pointer: pointer.v,
Value: initializer.v,
Align: stmt.ActualType.Align() * 8,
})
store := c.block.Add(&ir.StoreInst{
Pointer: pointer.v,
Value: initializer,
Align: stmt.ActualType.Align() * 8,
})

c.setLocationMeta(store, stmt)
}
c.setLocationMeta(store, stmt)
}

func (c *codegen) VisitIf(stmt *ast.If) {
Expand Down
8 changes: 8 additions & 0 deletions tests/src/statements.fb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
namespace Tests.Statements;

#[Test]
func variableZeroInit() bool {
var n i32;
var p *void;

return n == 0 && p == nil;
}

#[Test]
func variableShadowing() bool {
var a = 3;
Expand Down

0 comments on commit 122a7be

Please sign in to comment.