Skip to content

Commit 80a06be

Browse files
authored
fix: make method and action IR call-names FFI compatible (#1465)
1 parent 1e543cd commit 80a06be

File tree

29 files changed

+128
-117
lines changed

29 files changed

+128
-117
lines changed

src/codegen/debug.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'ink> Debug<'ink> for DebugBuilder<'ink> {
762762

763763
let variable_key = VariableKey::new(
764764
variable.get_qualified_name(),
765-
Some(function_scope.linking_context.get_call_name()),
765+
Some(&function_scope.linking_context.get_call_name_for_ir()),
766766
);
767767
self.variables.insert(variable_key, debug_variable);
768768
}
@@ -801,7 +801,7 @@ impl<'ink> Debug<'ink> for DebugBuilder<'ink> {
801801

802802
let variable_key = VariableKey::new(
803803
variable.get_qualified_name(),
804-
Some(function_scope.linking_context.get_call_name()),
804+
Some(&function_scope.linking_context.get_call_name_for_ir()),
805805
);
806806
self.variables.insert(variable_key, debug_variable);
807807
}
@@ -819,7 +819,8 @@ impl<'ink> Debug<'ink> for DebugBuilder<'ink> {
819819
.get_subprogram()
820820
.map(|it| it.as_debug_info_scope())
821821
.unwrap_or_else(|| file.as_debug_info_scope());
822-
let variable_key = VariableKey::new(name, Some(function_scope.linking_context.get_call_name()));
822+
let variable_key =
823+
VariableKey::new(name, Some(&function_scope.linking_context.get_call_name_for_ir()));
823824
if let Some(debug_type) = self.types.get(&name.to_lowercase()) {
824825
let debug_type = *debug_type;
825826
let line = function_scope.linking_context.get_location().get_line_plus_one() as u32;
@@ -865,7 +866,7 @@ impl<'ink> Debug<'ink> for DebugBuilder<'ink> {
865866
scope,
866867
None,
867868
);
868-
let key = VariableKey::new(name, Some(function_scope.linking_context.get_call_name()));
869+
let key = VariableKey::new(name, Some(&function_scope.linking_context.get_call_name_for_ir()));
869870
let variable = self.variables.get(&key);
870871
self.debug_info.insert_declare_at_end(value, variable.copied(), None, location, block);
871872
}

src/codegen/generators/expression_generator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
497497
.ok_or_else(|| Diagnostic::cannot_generate_call_statement(operator))?;
498498

499499
let parameters_list = parameters.map(flatten_expression_list).unwrap_or_default();
500-
let implementation_name = implementation.get_call_name();
500+
let implementation_name = &implementation.get_call_name();
501501
// if the function is builtin, generate a basic value enum for it
502502
if let Some(builtin) = self.index.get_builtin_function(implementation_name) {
503503
// adr, ref, etc.
@@ -865,7 +865,7 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
865865
.ok_or_else(|| Diagnostic::cannot_generate_call_statement(operator))?;
866866

867867
self.generate_stateful_pou_arguments(
868-
implementation.get_call_name(),
868+
&implementation.get_call_name_for_ir(),
869869
None,
870870
call_ptr,
871871
passed_parameters,
@@ -874,7 +874,7 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> {
874874
_ => {
875875
let call_ptr = self.generate_lvalue(operator)?;
876876
self.generate_stateful_pou_arguments(
877-
implementation.get_call_name(),
877+
&implementation.get_call_name_for_ir(),
878878
None,
879879
call_ptr,
880880
passed_parameters,

src/codegen/generators/pou_generator.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
162162
}
163163

164164
fn mangle_function(&self, implementation: &ImplementationIndexEntry) -> Result<String, Diagnostic> {
165-
let ctx = SectionMangler::function(implementation.get_call_name().to_lowercase());
165+
let ctx = SectionMangler::function(implementation.get_call_name_for_ir().to_lowercase());
166166

167167
let params = self.index.get_declared_parameters(implementation.get_call_name());
168168

@@ -279,7 +279,8 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
279279

280280
let function_declaration = self.create_llvm_function_type(parameters, variadic, return_type_llvm)?;
281281

282-
let curr_f = module.add_function(implementation.get_call_name(), function_declaration, None);
282+
let curr_f: FunctionValue<'_> =
283+
module.add_function(&implementation.get_call_name_for_ir(), function_declaration, None);
283284

284285
let section_name = self.get_section(implementation)?;
285286
curr_f.set_section(section_name.as_deref());

src/codegen/generators/statement_generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
805805
//generate return void
806806
self.llvm.builder.build_return(None);
807807
} else {
808-
// renerate return statement
809-
let call_name = self.function_context.linking_context.get_call_name();
808+
// generate return statement
809+
let call_name = &self.function_context.linking_context.get_call_name_for_ir();
810810
let var_name = format!("{call_name}_ret"); // TODO: Naming convention (see plc_util/src/convention.rs)
811811
let ret_name = ret_v.get_qualified_name();
812812
let value_ptr =

src/codegen/tests/code_gen_tests.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,11 @@ fn fb_method_called_locally() {
11151115
define void @foo(%foo* %0) {
11161116
entry:
11171117
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
1118-
%call = call i32 @foo.addToBar(%foo* %0, i16 42)
1118+
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
11191119
ret void
11201120
}
11211121
1122-
define i32 @foo.addToBar(%foo* %0, i16 %1) {
1122+
define i32 @foo_addToBar(%foo* %0, i16 %1) {
11231123
entry:
11241124
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
11251125
%foo.addToBar = alloca i32, align 4
@@ -1133,8 +1133,8 @@ fn fb_method_called_locally() {
11331133
store i32 %tmpVar, i32* %bar, align 4
11341134
%load_bar1 = load i32, i32* %bar, align 4
11351135
store i32 %load_bar1, i32* %foo.addToBar, align 4
1136-
%foo.addToBar_ret = load i32, i32* %foo.addToBar, align 4
1137-
ret i32 %foo.addToBar_ret
1136+
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
1137+
ret i32 %foo_addToBar_ret
11381138
}
11391139
11401140
define void @main() {
@@ -1144,7 +1144,7 @@ fn fb_method_called_locally() {
11441144
%0 = bitcast %foo* %fb to i8*
11451145
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)
11461146
store i32 0, i32* %x, align 4
1147-
%call = call i32 @foo.addToBar(%foo* %fb, i16 3)
1147+
%call = call i32 @foo_addToBar(%foo* %fb, i16 3)
11481148
store i32 %call, i32* %x, align 4
11491149
ret void
11501150
}
@@ -1198,11 +1198,11 @@ fn fb_local_method_var_shadows_parent_var() {
11981198
define void @foo(%foo* %0) {
11991199
entry:
12001200
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
1201-
%call = call i32 @foo.addToBar(%foo* %0, i16 42)
1201+
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
12021202
ret void
12031203
}
12041204
1205-
define i32 @foo.addToBar(%foo* %0, i16 %1) {
1205+
define i32 @foo_addToBar(%foo* %0, i16 %1) {
12061206
entry:
12071207
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
12081208
%foo.addToBar = alloca i32, align 4
@@ -1218,8 +1218,8 @@ fn fb_local_method_var_shadows_parent_var() {
12181218
store i32 %tmpVar, i32* %bar1, align 4
12191219
%load_bar2 = load i32, i32* %bar1, align 4
12201220
store i32 %load_bar2, i32* %foo.addToBar, align 4
1221-
%foo.addToBar_ret = load i32, i32* %foo.addToBar, align 4
1222-
ret i32 %foo.addToBar_ret
1221+
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
1222+
ret i32 %foo_addToBar_ret
12231223
}
12241224
12251225
define void @main() {
@@ -1229,7 +1229,7 @@ fn fb_local_method_var_shadows_parent_var() {
12291229
%0 = bitcast %foo* %fb to i8*
12301230
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)
12311231
store i32 0, i32* %x, align 4
1232-
%call = call i32 @foo.addToBar(%foo* %fb, i16 3)
1232+
%call = call i32 @foo_addToBar(%foo* %fb, i16 3)
12331233
store i32 %call, i32* %x, align 4
12341234
ret void
12351235
}
@@ -1280,11 +1280,11 @@ fn prog_method_called_locally() {
12801280
define void @foo(%foo* %0) {
12811281
entry:
12821282
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
1283-
%call = call i32 @foo.addToBar(%foo* %0, i16 42)
1283+
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
12841284
ret void
12851285
}
12861286
1287-
define i32 @foo.addToBar(%foo* %0, i16 %1) {
1287+
define i32 @foo_addToBar(%foo* %0, i16 %1) {
12881288
entry:
12891289
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
12901290
%foo.addToBar = alloca i32, align 4
@@ -1298,15 +1298,15 @@ fn prog_method_called_locally() {
12981298
store i32 %tmpVar, i32* %bar, align 4
12991299
%load_bar1 = load i32, i32* %bar, align 4
13001300
store i32 %load_bar1, i32* %foo.addToBar, align 4
1301-
%foo.addToBar_ret = load i32, i32* %foo.addToBar, align 4
1302-
ret i32 %foo.addToBar_ret
1301+
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
1302+
ret i32 %foo_addToBar_ret
13031303
}
13041304
13051305
define void @main() {
13061306
entry:
13071307
%x = alloca i32, align 4
13081308
store i32 0, i32* %x, align 4
1309-
%call = call i32 @foo.addToBar(%foo* @foo_instance, i16 3)
1309+
%call = call i32 @foo_addToBar(%foo* @foo_instance, i16 3)
13101310
store i32 %call, i32* %x, align 4
13111311
ret void
13121312
}
@@ -1354,11 +1354,11 @@ fn prog_local_method_var_shadows_parent_var() {
13541354
define void @foo(%foo* %0) {
13551355
entry:
13561356
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
1357-
%call = call i32 @foo.addToBar(%foo* %0, i16 42)
1357+
%call = call i32 @foo_addToBar(%foo* %0, i16 42)
13581358
ret void
13591359
}
13601360
1361-
define i32 @foo.addToBar(%foo* %0, i16 %1) {
1361+
define i32 @foo_addToBar(%foo* %0, i16 %1) {
13621362
entry:
13631363
%bar = getelementptr inbounds %foo, %foo* %0, i32 0, i32 0
13641364
%foo.addToBar = alloca i32, align 4
@@ -1374,15 +1374,15 @@ fn prog_local_method_var_shadows_parent_var() {
13741374
store i32 %tmpVar, i32* %bar1, align 4
13751375
%load_bar2 = load i32, i32* %bar1, align 4
13761376
store i32 %load_bar2, i32* %foo.addToBar, align 4
1377-
%foo.addToBar_ret = load i32, i32* %foo.addToBar, align 4
1378-
ret i32 %foo.addToBar_ret
1377+
%foo_addToBar_ret = load i32, i32* %foo.addToBar, align 4
1378+
ret i32 %foo_addToBar_ret
13791379
}
13801380
13811381
define void @main() {
13821382
entry:
13831383
%x = alloca i32, align 4
13841384
store i32 0, i32* %x, align 4
1385-
%call = call i32 @foo.addToBar(%foo* @foo_instance, i16 3)
1385+
%call = call i32 @foo_addToBar(%foo* @foo_instance, i16 3)
13861386
store i32 %call, i32* %x, align 4
13871387
ret void
13881388
}

src/codegen/tests/debug_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
349349
",
350350
);
351351

352-
assert_snapshot!(codegen, @r###"
352+
assert_snapshot!(codegen, @r#"
353353
; ModuleID = '<internal>'
354354
source_filename = "<internal>"
355355
@@ -364,7 +364,7 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
364364
ret void, !dbg !13
365365
}
366366
367-
define void @fb.foo(%fb* %0) !dbg !14 {
367+
define void @fb_foo(%fb* %0) !dbg !14 {
368368
entry:
369369
call void @llvm.dbg.declare(metadata %fb* %0, metadata !15, metadata !DIExpression()), !dbg !16
370370
ret void, !dbg !16
@@ -407,7 +407,7 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
407407
!14 = distinct !DISubprogram(name: "fb.foo", linkageName: "fb.foo", scope: !9, file: !2, line: 3, type: !10, scopeLine: 4, flags: DIFlagPublic, spFlags: DISPFlagDefinition, unit: !7, retainedNodes: !4)
408408
!15 = !DILocalVariable(name: "fb", scope: !14, file: !2, line: 4, type: !3)
409409
!16 = !DILocation(line: 4, column: 8, scope: !14)
410-
"###);
410+
"#);
411411
}
412412

413413
#[test]
@@ -450,7 +450,7 @@ fn action_with_var_temp() {
450450
call void @llvm.dbg.declare(metadata i32* %main, metadata !12, metadata !DIExpression()), !dbg !14
451451
store i32 0, i32* %main, align 4
452452
call void @PLC_PRG(%PLC_PRG* @PLC_PRG_instance), !dbg !15
453-
call void @PLC_PRG.act(%PLC_PRG* @PLC_PRG_instance), !dbg !16
453+
call void @PLC_PRG_act(%PLC_PRG* @PLC_PRG_instance), !dbg !16
454454
%main_ret = load i32, i32* %main, align 4, !dbg !17
455455
ret i32 %main_ret, !dbg !17
456456
}
@@ -465,7 +465,7 @@ fn action_with_var_temp() {
465465
ret void, !dbg !25
466466
}
467467
468-
define void @PLC_PRG.act(%PLC_PRG* %0) !dbg !26 {
468+
define void @PLC_PRG_act(%PLC_PRG* %0) !dbg !26 {
469469
entry:
470470
call void @llvm.dbg.declare(metadata %PLC_PRG* %0, metadata !27, metadata !DIExpression()), !dbg !28
471471
%x = alloca i32, align 4

0 commit comments

Comments
 (0)