Skip to content

fix: method and action IR names are now separated by __ instead of _ #1471

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions book/src/libraries/api_lib_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ This global instance follows the naming scheme of `__<FunctionBlockName>__init`,
myFunctionBlock __myFunctionBlock__init = { 0 };
```

3. Optionally create an initialization function following the naming pattern `<FunctionBlockName>_FB_INIT`:
3. Optionally create an initialization function following the naming pattern `<FunctionBlockName>__FB_INIT`:

```c
void myFunctionBlock_FB_INIT(myFunctionBlock* fb_instance) {
void myFunctionBlock__FB_INIT(myFunctionBlock* fb_instance) {
// Initialize members here
fb_instance->a = 1;
fb_instance->b = 2;
Expand Down
2 changes: 1 addition & 1 deletion book/src/using_rusty.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ When your code is compiled, the compiler creates a special initialization functi

`<projectname>` is either taken directly from the `plc.json`'s `name` field or derived from the first input file (replacing `.`/`-` with `_`) when compiling without a `plc.json` (e.g. `plc prog.st ...` would yield `__init___prog_st`).

This function is added to the global constructor list, therefore loading the binary will automatically call the `__init___<projectname>` function (and therefore your `<FunctionBlockName>_FB_INIT` function) when an instance of your function block is created, before any other methods are called. This allows you to set default values or perform required setup for your function block.
This function is added to the global constructor list, therefore loading the binary will automatically call the `__init___<projectname>` function (and therefore your `<FunctionBlockName>__FB_INIT` function) when an instance of your function block is created, before any other methods are called. This allows you to set default values or perform required setup for your function block.

> **IMPORTANT:** The global constructor initialization is currently only supported for `x86` ISAs. To make sure initialization code runs reliably regardless of target-architecture, ensure your runtime calls this function before starting main task execution.
If you're using the executable without a runtime, you **must** ensure that `__init___<projectname>` is called before any other code runs. Failure to do so will result in uninitialized function blocks and pointers, which can lead to undefined behavior and/or crashes.
Expand Down
40 changes: 20 additions & 20 deletions src/codegen/tests/code_gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,11 @@ fn fb_method_called_locally() {
define void @foo(%foo* %0) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
%call = call i32 @foo__addToBar(%foo* %0, i16 42)
ret void
}

define i32 @foo_addToBar(%foo* %0, i16 %1) {
define i32 @foo__addToBar(%foo* %0, i16 %1) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%foo.addToBar = alloca i32, align 4
Expand All @@ -1133,8 +1133,8 @@ fn fb_method_called_locally() {
store i32 %tmpVar, i32* %bar, align 4
%load_bar1 = load i32, i32* %bar, align 4
store i32 %load_bar1, i32* %foo.addToBar, align 4
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo_addToBar_ret
%foo__addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo__addToBar_ret
}

define void @main() {
Expand All @@ -1144,7 +1144,7 @@ fn fb_method_called_locally() {
%0 = bitcast %foo* %fb to i8*
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)
store i32 0, i32* %x, align 4
%call = call i32 @foo_addToBar(%foo* %fb, i16 3)
%call = call i32 @foo__addToBar(%foo* %fb, i16 3)
store i32 %call, i32* %x, align 4
ret void
}
Expand Down Expand Up @@ -1198,11 +1198,11 @@ fn fb_local_method_var_shadows_parent_var() {
define void @foo(%foo* %0) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
%call = call i32 @foo__addToBar(%foo* %0, i16 42)
ret void
}

define i32 @foo_addToBar(%foo* %0, i16 %1) {
define i32 @foo__addToBar(%foo* %0, i16 %1) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%foo.addToBar = alloca i32, align 4
Expand All @@ -1218,8 +1218,8 @@ fn fb_local_method_var_shadows_parent_var() {
store i32 %tmpVar, i32* %bar1, align 4
%load_bar2 = load i32, i32* %bar1, align 4
store i32 %load_bar2, i32* %foo.addToBar, align 4
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo_addToBar_ret
%foo__addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo__addToBar_ret
}

define void @main() {
Expand All @@ -1229,7 +1229,7 @@ fn fb_local_method_var_shadows_parent_var() {
%0 = bitcast %foo* %fb to i8*
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)
store i32 0, i32* %x, align 4
%call = call i32 @foo_addToBar(%foo* %fb, i16 3)
%call = call i32 @foo__addToBar(%foo* %fb, i16 3)
store i32 %call, i32* %x, align 4
ret void
}
Expand Down Expand Up @@ -1280,11 +1280,11 @@ fn prog_method_called_locally() {
define void @foo(%foo* %0) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
%call = call i32 @foo__addToBar(%foo* %0, i16 42)
ret void
}

define i32 @foo_addToBar(%foo* %0, i16 %1) {
define i32 @foo__addToBar(%foo* %0, i16 %1) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%foo.addToBar = alloca i32, align 4
Expand All @@ -1298,15 +1298,15 @@ fn prog_method_called_locally() {
store i32 %tmpVar, i32* %bar, align 4
%load_bar1 = load i32, i32* %bar, align 4
store i32 %load_bar1, i32* %foo.addToBar, align 4
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo_addToBar_ret
%foo__addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo__addToBar_ret
}

define void @main() {
entry:
%x = alloca i32, align 4
store i32 0, i32* %x, align 4
%call = call i32 @foo_addToBar(%foo* @foo_instance, i16 3)
%call = call i32 @foo__addToBar(%foo* @foo_instance, i16 3)
store i32 %call, i32* %x, align 4
ret void
}
Expand Down Expand Up @@ -1354,11 +1354,11 @@ fn prog_local_method_var_shadows_parent_var() {
define void @foo(%foo* %0) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
%call = call i32 @foo__addToBar(%foo* %0, i16 42)
ret void
}

define i32 @foo_addToBar(%foo* %0, i16 %1) {
define i32 @foo__addToBar(%foo* %0, i16 %1) {
entry:
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
%foo.addToBar = alloca i32, align 4
Expand All @@ -1374,15 +1374,15 @@ fn prog_local_method_var_shadows_parent_var() {
store i32 %tmpVar, i32* %bar1, align 4
%load_bar2 = load i32, i32* %bar1, align 4
store i32 %load_bar2, i32* %foo.addToBar, align 4
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo_addToBar_ret
%foo__addToBar_ret = load i32, i32* %foo.addToBar, align 4
ret i32 %foo__addToBar_ret
}

define void @main() {
entry:
%x = alloca i32, align 4
store i32 0, i32* %x, align 4
%call = call i32 @foo_addToBar(%foo* @foo_instance, i16 3)
%call = call i32 @foo__addToBar(%foo* @foo_instance, i16 3)
store i32 %call, i32* %x, align 4
ret void
}
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/tests/debug_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
ret void, !dbg !13
}

define void @fb_foo(%fb* %0) !dbg !14 {
define void @fb__foo(%fb* %0) !dbg !14 {
entry:
call void @llvm.dbg.declare(metadata %fb* %0, metadata !15, metadata !DIExpression()), !dbg !16
ret void, !dbg !16
Expand Down Expand Up @@ -457,7 +457,7 @@ fn action_with_var_temp() {
call void @llvm.dbg.declare(metadata i32* %main, metadata !12, metadata !DIExpression()), !dbg !14
store i32 0, i32* %main, align 4
call void @PLC_PRG(%PLC_PRG* @PLC_PRG_instance), !dbg !15
call void @PLC_PRG_act(%PLC_PRG* @PLC_PRG_instance), !dbg !16
call void @PLC_PRG__act(%PLC_PRG* @PLC_PRG_instance), !dbg !16
%main_ret = load i32, i32* %main, align 4, !dbg !17
ret i32 %main_ret, !dbg !17
}
Expand All @@ -472,7 +472,7 @@ fn action_with_var_temp() {
ret void, !dbg !25
}

define void @PLC_PRG_act(%PLC_PRG* %0) !dbg !26 {
define void @PLC_PRG__act(%PLC_PRG* %0) !dbg !26 {
entry:
call void @llvm.dbg.declare(metadata %PLC_PRG* %0, metadata !27, metadata !DIExpression()), !dbg !28
%x = alloca i32, align 4
Expand Down
Loading