Skip to content

Commit

Permalink
fix some code gen issues with void expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed Nov 17, 2023
1 parent c68e5b2 commit 24f70d3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions FrontEnd/emit_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def _GetLValueAddressAsBaseOffset(node, tc: type_corpus.TypeCorpus,
elif isinstance(node, cwast.ExprStmt):
ct = node.x_type
name = id_gen.NewName("expr_stk_var")
assert ct.size > 0
print(f"{TAB}.stk {name} {ct.alignment} {ct.size}")
base = id_gen.NewName("stmt_stk_base")
kind = tc.get_data_address_reg_type()
Expand Down Expand Up @@ -362,6 +363,7 @@ def _EmitExpr1(kind: cwast.UNARY_EXPR_KIND, res, ct: cwast.CanonType, op):


def EmitIRExpr(node, tc: type_corpus.TypeCorpus, id_gen: identifier.IdGenIR) -> Any:
"""Returns None if the type is void"""
ct_dst: cwast.CanonType = node.x_type
assert ct_dst.is_void_or_wrapped_void(
) or ct_dst.fits_in_register(), f"{node} {ct_dst}"
Expand Down Expand Up @@ -432,6 +434,8 @@ def EmitIRExpr(node, tc: type_corpus.TypeCorpus, id_gen: identifier.IdGenIR) ->
f"{TAB}bitcast {res}:{node.type.x_type.get_single_register_type()} = {expr}")
return res
elif isinstance(node, cwast.ExprNarrow):
if ct_dst.is_void_or_wrapped_void():
return None
ct_src: cwast.CanonType = node.expr.x_type
assert ct_src.is_sum() or ct_src.original_type.is_sum()
addr = _GetLValueAddress(node.expr, tc, id_gen)
Expand Down Expand Up @@ -520,8 +524,8 @@ def EmitIRExprToMemory(init_node, dst: BaseOffset,
reg = EmitIRExpr(init_node, tc, id_gen)
print(f"{TAB}st {dst.base} {dst.offset} = {reg}")
elif init_node.x_type.size == 0 or init_node.expr.x_type.size == 0:
# init_node can be union of voids
# init_node.expr can void for widening
# init_node can be union of voids
# init_node.expr can void for widening
pass
elif init_node.x_type.is_untagged_sum() and init_node.expr.x_type.fits_in_register():
reg = EmitIRExpr(init_node.expr, tc, id_gen)
Expand Down Expand Up @@ -613,7 +617,10 @@ def EmitIRStmt(node, result: Optional[ReturnResultLocation], tc: type_corpus.Typ
# This uniquifies names
node.name = id_gen.NewName(node.name)
initial = node.initial_or_undef_or_auto
if _IsDefVarOnStack(node):
if def_type.size == 0 and not isinstance(initial, cwast.ValUndef):
EmitIRExpr(initial, tc, id_gen)
elif _IsDefVarOnStack(node):
assert def_type.size > 0
print(f"{TAB}.stk {node.name} {def_type.alignment} {def_type.size}")
if not isinstance(initial, cwast.ValUndef):
init_base = id_gen.NewName("init_base")
Expand Down Expand Up @@ -667,6 +674,7 @@ def EmitIRStmt(node, result: Optional[ReturnResultLocation], tc: type_corpus.Typ
EmitIRExpr(node.expr, tc, id_gen)
else:
name = id_gen.NewName("stmt_stk_var")
assert ct.size > 0
print(f"{TAB}.stk {name} {ct.alignment} {ct.size}")
base = id_gen.NewName("stmt_stk_base")
kind = tc.get_data_address_reg_type()
Expand Down

0 comments on commit 24f70d3

Please sign in to comment.