Skip to content

Commit 2572529

Browse files
authored
Merge pull request #1429 from PLC-lang/debug_fixes
fix: correctly handle temp varibles in actions and programs
2 parents ab83531 + 2250ee3 commit 2572529

File tree

6 files changed

+227
-98
lines changed

6 files changed

+227
-98
lines changed

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@
9999
"RUST_LOG": "rusty"
100100
},
101101
"terminal": "integrated"
102+
},
103+
{
104+
"type": "codelldb",
105+
"request": "launch",
106+
"name": "Debug st application demo",
107+
"cwd": "${workspaceFolder}",
108+
"program": "target/demo",
109+
"terminal": "integrated"
110+
102111
}
103112
]
104113
}

src/codegen/debug.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ impl<'ink> DebugBuilder<'ink> {
275275
//Create each type
276276
let index_types = members
277277
.iter()
278+
.filter(|it| !(it.is_temp() || it.is_variadic() || it.is_var_external()))
278279
.map(|it| (it.get_name(), it.get_type_name(), &it.source_location))
279280
.map(|(name, type_name, location)| {
280281
index.get_type(type_name.as_ref()).map(|dt| (name, dt, location))
@@ -522,7 +523,7 @@ impl<'ink> DebugBuilder<'ink> {
522523
///Creates the debug information for function variables
523524
///For a `Function` these will be all VAR_INPUT, VAR_OUTPUT and VAR_IN_OUT in addition to
524525
///entries for VAR and VAR_TEMP
525-
///For other POUs we create enties in VAR_TEMP and an additional single parameter at position 0
526+
///For other POUs we create entries in VAR_TEMP and an additional single parameter at position 0
526527
///(the struct)
527528
fn create_function_variables(
528529
&mut self,
@@ -533,7 +534,7 @@ impl<'ink> DebugBuilder<'ink> {
533534
let mut param_offset = 0;
534535
//Register the return and local variables for debugging
535536
for variable in index
536-
.get_pou_members(pou.get_name())
537+
.get_variables_for_pou(pou)
537538
.iter()
538539
.filter(|it| it.is_local() || it.is_temp() || it.is_return())
539540
{

src/codegen/generators/pou_generator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,6 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
696696
.ok_or_else(|| Diagnostic::missing_function(location))?;
697697
//Generate POU struct declaration for debug
698698
if let Some(block) = self.llvm.builder.get_insert_block() {
699-
// debug.register_struct_parameter(type_name, function_context);
700699
debug.add_variable_declaration(
701700
type_name,
702701
param_pointer,

src/codegen/tests/debug_tests.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,123 @@ fn dbg_declare_has_valid_metadata_references_for_methods() {
406406
!16 = !DILocation(line: 4, column: 8, scope: !14)
407407
"###);
408408
}
409+
410+
#[test]
411+
fn action_with_var_temp() {
412+
let codegen = codegen(
413+
r"
414+
FUNCTION main : DINT
415+
PLC_PRG();
416+
PLC_PRG.act();
417+
END_FUNCTION
418+
419+
PROGRAM PLC_PRG
420+
VAR_TEMP
421+
x : DINT;
422+
END_VAR
423+
424+
x := 0;
425+
END_PROGRAM
426+
427+
ACTIONS
428+
ACTION act
429+
x := x + 1;
430+
END_ACTION
431+
END_ACTIONS
432+
",
433+
);
434+
435+
assert_snapshot!(codegen, @r#"
436+
; ModuleID = '<internal>'
437+
source_filename = "<internal>"
438+
439+
%PLC_PRG = type {}
440+
441+
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 0, void ()* @__init___Test, i8* null }]
442+
@PLC_PRG_instance = global %PLC_PRG zeroinitializer, !dbg !0
443+
444+
define i32 @main() !dbg !9 {
445+
entry:
446+
%main = alloca i32, align 4, !dbg !12
447+
call void @llvm.dbg.declare(metadata i32* %main, metadata !13, metadata !DIExpression()), !dbg !15
448+
store i32 0, i32* %main, align 4, !dbg !12
449+
call void @PLC_PRG(%PLC_PRG* @PLC_PRG_instance), !dbg !12
450+
call void @PLC_PRG.act(%PLC_PRG* @PLC_PRG_instance), !dbg !16
451+
%main_ret = load i32, i32* %main, align 4, !dbg !16
452+
ret i32 %main_ret, !dbg !16
453+
}
454+
455+
define void @PLC_PRG(%PLC_PRG* %0) !dbg !17 {
456+
entry:
457+
call void @llvm.dbg.declare(metadata %PLC_PRG* %0, metadata !20, metadata !DIExpression()), !dbg !21
458+
%x = alloca i32, align 4, !dbg !21
459+
call void @llvm.dbg.declare(metadata i32* %x, metadata !22, metadata !DIExpression()), !dbg !23
460+
store i32 0, i32* %x, align 4, !dbg !21
461+
store i32 0, i32* %x, align 4, !dbg !21
462+
ret void, !dbg !21
463+
}
464+
465+
define void @PLC_PRG.act(%PLC_PRG* %0) !dbg !24 {
466+
entry:
467+
call void @llvm.dbg.declare(metadata %PLC_PRG* %0, metadata !25, metadata !DIExpression()), !dbg !26
468+
%x = alloca i32, align 4, !dbg !26
469+
call void @llvm.dbg.declare(metadata i32* %x, metadata !27, metadata !DIExpression()), !dbg !28
470+
store i32 0, i32* %x, align 4, !dbg !26
471+
%load_x = load i32, i32* %x, align 4, !dbg !26
472+
%tmpVar = add i32 %load_x, 1, !dbg !26
473+
store i32 %tmpVar, i32* %x, align 4, !dbg !26
474+
ret void, !dbg !26
475+
}
476+
477+
; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
478+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
479+
480+
define void @__init_plc_prg(%PLC_PRG* %0) {
481+
entry:
482+
%self = alloca %PLC_PRG*, align 8
483+
store %PLC_PRG* %0, %PLC_PRG** %self, align 8
484+
ret void
485+
}
486+
487+
define void @__init___Test() {
488+
entry:
489+
call void @__init_plc_prg(%PLC_PRG* @PLC_PRG_instance)
490+
ret void
491+
}
492+
493+
attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
494+
495+
!llvm.module.flags = !{!5, !6}
496+
!llvm.dbg.cu = !{!7}
497+
498+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
499+
!1 = distinct !DIGlobalVariable(name: "PLC_PRG", scope: !2, file: !2, line: 7, type: !3, isLocal: false, isDefinition: true)
500+
!2 = !DIFile(filename: "<internal>", directory: "")
501+
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "PLC_PRG", scope: !2, file: !2, line: 7, align: 64, flags: DIFlagPublic, elements: !4, identifier: "PLC_PRG")
502+
!4 = !{}
503+
!5 = !{i32 2, !"Dwarf Version", i32 5}
504+
!6 = !{i32 2, !"Debug Info Version", i32 3}
505+
!7 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, producer: "RuSTy Structured text Compiler", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !8, splitDebugInlining: false)
506+
!8 = !{!0}
507+
!9 = distinct !DISubprogram(name: "main", linkageName: "main", scope: !2, file: !2, line: 2, type: !10, scopeLine: 3, flags: DIFlagPublic, spFlags: DISPFlagDefinition, unit: !7, retainedNodes: !4)
508+
!10 = !DISubroutineType(flags: DIFlagPublic, types: !11)
509+
!11 = !{null}
510+
!12 = !DILocation(line: 3, column: 12, scope: !9)
511+
!13 = !DILocalVariable(name: "main", scope: !9, file: !2, line: 2, type: !14, align: 32)
512+
!14 = !DIBasicType(name: "DINT", size: 32, encoding: DW_ATE_signed, flags: DIFlagPublic)
513+
!15 = !DILocation(line: 2, column: 17, scope: !9)
514+
!16 = !DILocation(line: 4, column: 12, scope: !9)
515+
!17 = distinct !DISubprogram(name: "PLC_PRG", linkageName: "PLC_PRG", scope: !2, file: !2, line: 7, type: !18, scopeLine: 12, flags: DIFlagPublic, spFlags: DISPFlagDefinition, unit: !7, retainedNodes: !4)
516+
!18 = !DISubroutineType(flags: DIFlagPublic, types: !19)
517+
!19 = !{null, !3}
518+
!20 = !DILocalVariable(name: "PLC_PRG", scope: !17, file: !2, line: 12, type: !3)
519+
!21 = !DILocation(line: 12, column: 12, scope: !17)
520+
!22 = !DILocalVariable(name: "x", scope: !17, file: !2, line: 9, type: !14, align: 32)
521+
!23 = !DILocation(line: 9, column: 12, scope: !17)
522+
!24 = distinct !DISubprogram(name: "PLC_PRG.act", linkageName: "PLC_PRG.act", scope: !2, file: !2, line: 16, type: !18, scopeLine: 17, flags: DIFlagPublic, spFlags: DISPFlagDefinition, unit: !7, retainedNodes: !4)
523+
!25 = !DILocalVariable(name: "PLC_PRG", scope: !24, file: !2, line: 17, type: !3)
524+
!26 = !DILocation(line: 17, column: 16, scope: !24)
525+
!27 = !DILocalVariable(name: "x", scope: !24, file: !2, line: 9, type: !14, align: 32)
526+
!28 = !DILocation(line: 9, column: 12, scope: !24)
527+
"#);
528+
}

0 commit comments

Comments
 (0)