Skip to content

Commit df31533

Browse files
committed
add test for user_init with stack-variables
1 parent a08c212 commit df31533

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-15
lines changed

src/codegen/tests/initialization_test/complex_initializers.rs

+71-15
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,10 @@ fn init_functions_generated_for_programs() {
148148
ret void
149149
}
150150
151-
define void @__user_init_PLC_PRG(%PLC_PRG* %0) {
152-
entry:
153-
%self = alloca %PLC_PRG*, align 8
154-
store %PLC_PRG* %0, %PLC_PRG** %self, align 8
155-
ret void
156-
}
157-
158151
define void @__init___Test() {
159152
entry:
160153
call void @__init_plc_prg(%PLC_PRG* @PLC_PRG_instance)
161154
call void @__user_init_PLC_PRG(%PLC_PRG* @PLC_PRG_instance)
162-
call void @__user_init_PLC_PRG(%PLC_PRG* @PLC_PRG_instance)
163155
ret void
164156
}
165157
"#);
@@ -567,7 +559,6 @@ fn nested_initializer_pous() {
567559
}
568560

569561
#[test]
570-
#[ignore = "initializing references in same POU not yet supported"]
571562
fn local_address() {
572563
let res = generate_to_string(
573564
"Test",
@@ -588,8 +579,7 @@ fn local_address() {
588579
}
589580

590581
#[test]
591-
#[ignore = "initializing references in same POU not yet supported"]
592-
fn tmpo() {
582+
fn user_init_called_for_variables_on_stack() {
593583
let result = generate_to_string(
594584
"Test",
595585
vec![SourceCode::from(
@@ -599,17 +589,83 @@ fn tmpo() {
599589
i : INT;
600590
pi: REF_TO INT;
601591
END_VAR
592+
METHOD FB_INIT
593+
pi := ADR(i);
594+
END_METHOD
602595
END_FUNCTION_BLOCK
603596
604-
ACTION foo.init
605-
pi := REF(i);
606-
END_ACTION
597+
FUNCTION main
598+
VAR
599+
fb: foo;
600+
END_VAR
601+
fb();
602+
END_FUNCTION
607603
"#,
608604
)],
609605
)
610606
.unwrap();
611607

612-
insta::assert_snapshot!(result, @r###""###);
608+
insta::assert_snapshot!(result, @r#"
609+
; ModuleID = '<internal>'
610+
source_filename = "<internal>"
611+
612+
%foo = type { i16, i16* }
613+
614+
@__foo__init = constant %foo zeroinitializer
615+
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @__init___Test, i8* null }]
616+
617+
define void @foo(%foo* %0) {
618+
entry:
619+
%i = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
620+
%pi = getelementptr inbounds %foo, %foo* %0, i32 0, i32 1
621+
ret void
622+
}
623+
624+
define void @foo__FB_INIT(%foo* %0) {
625+
entry:
626+
%i = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
627+
%pi = getelementptr inbounds %foo, %foo* %0, i32 0, i32 1
628+
store i16* %i, i16** %pi, align 8
629+
ret void
630+
}
631+
632+
define void @main() {
633+
entry:
634+
%fb = alloca %foo, align 8
635+
%0 = bitcast %foo* %fb to i8*
636+
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %0, i8* align 1 bitcast (%foo* @__foo__init to i8*), i64 ptrtoint (%foo* getelementptr (%foo, %foo* null, i32 1) to i64), i1 false)
637+
call void @__init_foo(%foo* %fb)
638+
call void @__user_init_foo(%foo* %fb)
639+
call void @foo(%foo* %fb)
640+
ret void
641+
}
642+
643+
; Function Attrs: argmemonly nofree nounwind willreturn
644+
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #0
645+
646+
define void @__init_foo(%foo* %0) {
647+
entry:
648+
%self = alloca %foo*, align 8
649+
store %foo* %0, %foo** %self, align 8
650+
ret void
651+
}
652+
653+
define void @__user_init_foo(%foo* %0) {
654+
entry:
655+
%self = alloca %foo*, align 8
656+
store %foo* %0, %foo** %self, align 8
657+
%deref = load %foo*, %foo** %self, align 8
658+
call void @foo__FB_INIT(%foo* %deref)
659+
ret void
660+
}
661+
662+
define void @__init___Test() {
663+
entry:
664+
ret void
665+
}
666+
667+
attributes #0 = { argmemonly nofree nounwind willreturn }
668+
"#);
613669
}
614670

615671
#[test]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: (%COMPILE %s && %RUN) | %CHECK %s
2+
FUNCTION_BLOCK foo
3+
METHOD FB_INIT
4+
printf('FB_INIT called$N');
5+
END_METHOD
6+
END_FUNCTION_BLOCK
7+
8+
FUNCTION main
9+
VAR
10+
fb: foo;
11+
END_VAR
12+
// CHECK: FB_INIT called
13+
END_FUNCTION

0 commit comments

Comments
 (0)