Skip to content

Commit

Permalink
fix: declare external pou initializers as external
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Sep 3, 2024
1 parent 41968d3 commit 22bbd64
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ source_filename = "<internal>"
%myFb = type {}

@myPrg_instance = external global %myPrg, section "var-$RUSTY$myPrg_instance:r0", !dbg !0
@__myFb__init = unnamed_addr constant %myFb zeroinitializer, section "var-$RUSTY$__myFb__init:r0", !dbg !5
@__myFb__init = external global %myFb, section "var-$RUSTY$__myFb__init:r0", !dbg !5

declare i32 @myFunc() section "fn-$RUSTY$myFunc:i32"

Expand Down
42 changes: 42 additions & 0 deletions src/codegen/tests/initialization_test/global_initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,45 @@ fn global_constant_without_initializer_gets_declared_initializer() {
//should initialize cmd1 and cmd2 with @__comamnds__init
insta::assert_snapshot!(result);
}

#[test]
fn external_pous_get_external_initializers() {
let result = codegen(
"
{external} FUNCTION_BLOCK ext_fb END_FUNCTION_BLOCK
{external} PROGRAM ext_prog END_PROGRAM
",
);

insta::assert_snapshot!(result, @r###"
; ModuleID = '<internal>'
source_filename = "<internal>"
%ext_fb = type {}
%ext_prog = type {}
@__ext_fb__init = external global %ext_fb, section "var-$RUSTY$__ext_fb__init:r0"
@ext_prog_instance = external global %ext_prog, section "var-$RUSTY$ext_prog_instance:r0"
declare void @ext_fb(%ext_fb*) section "fn-$RUSTY$ext_fb:v"
declare void @ext_prog(%ext_prog*) section "fn-$RUSTY$ext_prog:v"
"###);
}

#[test]
#[ignore = "external struct initializers are not declared external"]
fn external_aggregate_types_get_external_initializers() {
let result = codegen(
"
{external}
VAR_GLOBAL
a: ARRAY[0..10] OF DINT;
b: STRING;
c: STRUCT a: INT; END_STRUCT
END_VAR
",
);

insta::assert_snapshot!(result, @r###""###);
}
6 changes: 4 additions & 2 deletions src/index/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ pub fn visit_pou(index: &mut Index, pou: &Pou) {
&pou.name,
pou.name_location.clone(),
)
.set_constant(true);
.set_constant(true)
.set_linkage(pou.linkage);
index.register_global_initializer(&global_struct_name, variable);
index.register_pou(PouIndexEntry::create_function_block_entry(
&pou.name,
Expand All @@ -165,7 +166,8 @@ pub fn visit_pou(index: &mut Index, pou: &Pou) {
&pou.name,
pou.name_location.clone(),
)
.set_constant(true);
.set_constant(true)
.set_linkage(pou.linkage);
index.register_global_initializer(&global_struct_name, variable);
index.register_pou(PouIndexEntry::create_class_entry(
&pou.name,
Expand Down

0 comments on commit 22bbd64

Please sign in to comment.