Skip to content

Commit 2dd01f8

Browse files
committed
feat: vtable field in function blocks assigned to global
The `__vtable` field in classes and function blocks is assigned at init time to the correct global variable TODO: missing tests
1 parent 5d8f391 commit 2dd01f8

17 files changed

+699
-204
lines changed

compiler/plc_util/src/convention.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ pub fn internal_type_name<T: AsRef<str> + Display>(prefix: T, original_type_name
1212
format!("__{prefix}{original_type_name}")
1313
}
1414

15+
/// Returns the default vtable global variable name for a function block or class
16+
pub fn generate_vtable_name(name: &str) -> String {
17+
format!("__vtable_{name}")
18+
}
19+
20+
/// Returns the default vtable type name for a function block, class, or interface
21+
pub fn generate_vtable_type_name(name: &str) -> String {
22+
format!("__vtable_{name}_type")
23+
}
24+
1525
#[cfg(test)]
1626
mod tests {
1727
#[test]

src/codegen/tests/debug_tests.rs

Lines changed: 5 additions & 2 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
@@ -389,6 +389,9 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
389389
entry:
390390
%self = alloca %fb*, align 8
391391
store %fb* %0, %fb** %self, align 8
392+
%deref = load %fb*, %fb** %self, align 8
393+
%__vtable = getelementptr inbounds %fb, %fb* %deref, i32 0, i32 0
394+
store i32* bitcast (%__vtable_fb_type* @__vtable_fb to i32*), i32** %__vtable, align 8
392395
ret void
393396
}
394397
@@ -439,7 +442,7 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
439442
!26 = distinct !DISubprogram(name: "fb.foo", linkageName: "fb.foo", scope: !20, file: !2, line: 3, type: !21, scopeLine: 4, flags: DIFlagPublic, spFlags: DISPFlagDefinition, unit: !18, retainedNodes: !23)
440443
!27 = !DILocalVariable(name: "fb", scope: !26, file: !2, line: 4, type: !3)
441444
!28 = !DILocation(line: 4, column: 8, scope: !26)
442-
"#);
445+
"###);
443446
}
444447

445448
#[test]

0 commit comments

Comments
 (0)