Skip to content

Compile to IEC 61131-10 XML - Omron - #1896

Draft
DoubleCouponDay wants to merge 22 commits into
PLC-lang:masterfrom
DoubleCouponDay:omron-xml-target
Draft

Compile to IEC 61131-10 XML - Omron#1896
DoubleCouponDay wants to merge 22 commits into
PLC-lang:masterfrom
DoubleCouponDay:omron-xml-target

Conversation

@DoubleCouponDay

@DoubleCouponDay DoubleCouponDay commented Aug 28, 2026

Copy link
Copy Markdown

Based on my discussion post here, I've implemented XML generation as a first class compilation target. For now, the implementation focuses on complying with the specific subset of Structured Text compatible with Sysmac Studio. All that is required is to add the --xml-omron argument to a compilation command. the new plc_xmlgen module should be portable enough to handle other specific subsets in the future, such as CODESYS.

Here is an example of usage:

plc ./source/clampandsaw.st ./source/testallbuiltins.st --xml-omron -i ./externals/stdlib_externals.st -i ./externals/omron_externals.st -l iec61131std -l libNX1P2 -l ws2_32 -l ntdll -l userenv -o ./compiled/lib_structured_text.xml

For this to work, the builtins of a specific PLC device are compiled into a separate library which can be linked to. As a result, VAR_EXTERNAL and VAR_EXTERNAL CONSTANT had to be implemented. Without external references to a stub library, none of the references to built in constants, types, and global functions could compile.

To be clear, Sysmac does not support the following ST syntax:

  • no POINTER TO
  • no REFERENCE TO
  • no WSTRING
  • no CLASS
  • no INTERFACE
  • no ALIAS TO
  • no WSTRING

Unit tests have been added to confirm the feature.

Here is a concise list of changes made in this PR:

New XML export target

  • Added compiler/plc_xmlgen crate: xml_gen.rs generator plus a serializer.rs typed node builder for IEC 61131-10 Ed1 XML.
  • Added --xml-omron CLI flag and a GenerationParameters struct threaded from CompileParameters through CompileOptions into codegen.
  • Added FormatOption::XML with .xml output naming in plc_project and the codegen participant.
  • Added GeneratedModule::persist_to_xml, wired into persist alongside the object/IR/bitcode arms.
  • Added copy_xmlfile_to_output as the XML link step in GeneratedProject.
  • PipelineParticipant::generate now receives &AnnotatedProject and &CompileOptions so the generator can see the AST, not just the LLVM module.
  • Validates generated XML against the bundled Omron schema before writing.

Type rendering for Sysmac

  • Emits only StructTypeSpec and EnumTypeWithNamedValueSpec declarations, the two Sysmac actually accepts.
  • Resolves aliases, arrays and strings to inline TypeName text at every use site instead of declaring them.
  • Renders arrays as ARRAY[0..3] OF X, multi-dimensional as ARRAY[0..1,1..3] OF X with no space after the separator.
  • Renders strings as String[n], defaulting to String[80] to match DEFAULT_STRING_LEN.
  • Recursively resolves nested element types through alias chains, with a hop limit guarding self-referential aliases.

New diagnostic

  • Added E152 (error) for constructs the chosen XML target cannot represent, with a markdown doc page.
  • plc_xmlgen scans user types, globals, POU variable blocks and struct members, skipping external linkage and compiler-generated declarations.

VAR_EXTERNAL behaviour

  • Removed the E106 "VAR_EXTERNAL blocks have no effect" warning and its diagnostic entry, doc file and tests.
  • The initializer lowering now skips external blocks and no longer emits external constructor bodies for standalone declarations.
  • get_struct_member_index now allows external variables as member index.
  • Vtable generation forces internal linkage when constructors are generated internally.

Supporting changes

  • Added Display impls for AstLiteral, Array, Date, Time, TimeOfDay and DateAndTime so literals can be serialised as text.
  • Made SourceLocation.span/file and TextLocation.line/column/offset public.
  • Lowercased VariableBlockType's Display output to match the XML attribute vocabulary.
  • Fixed Windows MSVC linking in the stdlib integration test harness (/MD, explicit exports and system libs).
  • Documented the new target in book/src/using_rusty.md.

@DoubleCouponDay
DoubleCouponDay marked this pull request as draft August 28, 2026 04:37
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.92536% with 437 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.88%. Comparing base (1e70538) to head (9c2990b).
⚠️ Report is 220 commits behind head on master.

Files with missing lines Patch % Lines
compiler/plc_xmlgen/src/serializer.rs 24.30% 218 Missing ⚠️
compiler/plc_xmlgen/src/xml_gen.rs 83.28% 120 Missing ⚠️
compiler/plc_ast/src/literals.rs 9.75% 37 Missing ⚠️
src/codegen.rs 6.45% 29 Missing ⚠️
libs/stdlib/tests/common/mod.rs 0.00% 13 Missing ⚠️
compiler/plc_driver/src/pipelines.rs 65.38% 9 Missing ⚠️
compiler/plc_driver/src/pipelines/participant.rs 58.82% 7 Missing ⚠️
compiler/plc_ast/src/ast.rs 93.75% 1 Missing ⚠️
compiler/plc_driver/src/cli.rs 80.00% 1 Missing ⚠️
compiler/plc_project/src/project.rs 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1896      +/-   ##
==========================================
- Coverage   94.68%   92.88%   -1.80%     
==========================================
  Files         188      197       +9     
  Lines       58726    78862   +20136     
==========================================
+ Hits        55603    73252   +17649     
- Misses       3123     5610    +2487     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant