Segfault when passing a parameter to a reference to input
Describe the bug A Segfault occurs when passing a parameter to a reference to input.
To Reproduce Steps to reproduce the behavior:
TYPE EnumType : (
red,
green,
blue
);
END_TYPE
FUNCTION fnReferenceTo : EnumType
VAR_INPUT
inVar : Reference To EnumType;
END_VAR
fnReferenceTo := inVar;
END_FUNCTION
FUNCTION main
VAR
outVarReferenceTo : EnumType;
END_VAR
outVarReferenceTo := fnReferenceTo(blue);
END_FUNCTION
Expected behavior There shouldn't be a segfault. A validation error should be present in the case of a invalid scenario, and it should work if the scenario is valid.
This is due to constants being propagated. When trying to emit IR from this example, we get this:
Builder error: /tmp/.tmpkFPSpE/target/demo.st.ll:27:20: error: '@fnReferenceTo' defined with type 'i32 (i32*)*' but expected 'i32 (i32)*'
%call = call i32 @fnReferenceTo(i32 2)
^
.
In order to support passing enum variants (or any constant for that matter) by reference, we need to either reference that constant/variant in .rodata rather than folding it or we need to allocate a temporary variable to store the value into, which we can then pass to the function instead.