PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
181 stars 47 forks source link

Linking fails on enums with the same variant name #1182

Closed mhasel closed 1 month ago

mhasel commented 1 month ago

Describe the bug Given you have multiple enums with overlapping/same variants across multiple files, when you try to compile them then linking will fail with

error[E071]: An error occurred during linking: lld: error: duplicate symbol: <symbol_name>

regressed commit: 94a74fd

To Reproduce Steps to reproduce the behavior: foo.st:

FUNCTION_BLOCK FOO
VAR
    foo_enum : (start, stop) := stop;
END_VAR
END_FUNCTION_BLOCK

bar.st:

FUNCTION_BLOCK BAR
VAR
    bar_enum : (stop, go) := go;
END_VAR
END_FUNCTION_BLOCK

Compiling with plc foo.st bar.st:

error[E071]: An error occurred during linking: lld: error: duplicate symbol: stop
>>> defined at foo.st
>>>            /tmp/.tmpJFdPQo/target/foo.st.o:(stop)
>>> defined at bar.st
>>>            /tmp/.tmpJFdPQo/target/bar.st.o:(.rodata.cst4+0x4)
riederm commented 1 month ago

this problem came with the refactoring when we started to parallelize codegen into separate objects. In early versions we generated everything into one object and llvm took care of this situation by appending a number to duplicate global variables:

https://github.com/PLC-lang/rusty/blob/2294494aaec1fef2a0b10e6e98308e6dcb7b8bc7/src/tests/adr/enum_adr.rs#L72-L93

We probably need to pick unique llvm-names for the global constants now.

mhasel commented 1 month ago

this problem came with the refactoring when we started to parallelize codegen into separate objects. In early versions we generated everything into one object and llvm took care of this situation by appending a number to duplicate global variables:

https://github.com/PLC-lang/rusty/blob/2294494aaec1fef2a0b10e6e98308e6dcb7b8bc7/src/tests/adr/enum_adr.rs#L72-L93

We probably need to pick unique llvm-names for the global constants now.

@ghaith and I took a look at this last week and came to the same conclusion. This might be an easy fix by mangling the global with the pou qualifier, but I have not tried it yet: https://github.com/PLC-lang/rusty/blob/2294494aaec1fef2a0b10e6e98308e6dcb7b8bc7/src/codegen/generators/variable_generator.rs#L118-L119C102