Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constant propagation in ranged type limits #505

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/codegen/tests/code_gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2923,3 +2923,37 @@ fn optional_output_assignment() {
// codegen should be successful
insta::assert_snapshot!(result);
}

#[test]
fn constant_expressions_in_ranged_type_declaration_are_propagated() {
//GIVEN a ranged type from 0 .. MIN+1 where MIN is a global constant
//WHEN the code is generated
let result = codegen(
"
VAR_GLOBAL CONSTANT
MIN : INT := 7;
END_VAR

FUNCTION CheckRangeSigned: INT
VAR_INPUT
value : INT;
lower : INT;
upper : INT;
END_VAR
CheckRangeSigned := value;
END_FUNCTION

PROGRAM prg
VAR
x: INT(0 .. MIN+1);
END_VAR
x := 5;
END_PROGRAM",
);

// THEN we expect that the assignment to the range-typed variable (x := 5) will result
// in a call to CheckRangedSigned where the upper bound is a literal i16 8 - NOT an
// add-expression that really calculates the upper bound at runtime

insta::assert_snapshot!(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: src/codegen/tests/code_gen_tests.rs
expression: result
---
; ModuleID = 'main'
source_filename = "main"

%prg_interface = type { i16 }

@MIN = unnamed_addr constant i16 7
@prg_instance = global %prg_interface zeroinitializer

define i16 @CheckRangeSigned(i16 %0, i16 %1, i16 %2) {
entry:
%value = alloca i16, align 2
store i16 %0, i16* %value, align 2
%lower = alloca i16, align 2
store i16 %1, i16* %lower, align 2
%upper = alloca i16, align 2
store i16 %2, i16* %upper, align 2
%CheckRangeSigned = alloca i16, align 2
store i16 0, i16* %CheckRangeSigned, align 2
%load_value = load i16, i16* %value, align 2
store i16 %load_value, i16* %CheckRangeSigned, align 2
%CheckRangeSigned_ret = load i16, i16* %CheckRangeSigned, align 2
ret i16 %CheckRangeSigned_ret
}

define void @prg(%prg_interface* %0) {
entry:
%x = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 0
%call = call i16 @CheckRangeSigned(i16 5, i16 0, i16 8)
store i16 %call, i16* %x, align 2
ret void
}