r2k-in-the-vortex
Matiec of course implements IEC 61131-3:2003, but what about IEC 61131-3:2013? 

Well, there is new ST compiler RuSTy in the works, while it's very much work in progress it is in active development and aiming to go the full way. Which to me in long term sounds more promising than Matiec. Of course long term is not today, but there needs to be some sort of starting point somewhere. 

So, I'm going to try and integrate RuSTy as an alternative compiler for OpenPLC over next few weeks - as sort of a pathfinding trial in nothing else. 

Ideas, questions and suggestions are welcome.
thiagoralves
Pretty cool! Changing the compiler is not a straightforward task, as the runtime was coded based on the interface MatIEC generates. A lot of the runtime will have to be rewritten or adapted if one is to use a different compiler. But this is progress!
Reply 0 0
r2k-in-the-vortex
It's definitely a challange, but with rusty it should be easier in some ways because it's a true compiler, not a iec2c translator, it doesn't impose any structure, it's just a llvm front end.

Exersise for me is just to get the right things linked up and automating the compilation. Gluevars is probably where all the work is. Retain variables.. rusty doesn't provide a mechanism for yet, so that's no work at all. Hardware access and calling runtime, that's all really as first, minimal implementation.
Reply 0 0
thiagoralves
I would still keep the ST to C mechanism because that ensures portability among so many different platforms. I’m not familiar with LLVM but I guess it is generic enough so that you can define a C target, right? If you use LLVM to compile directly to bare metal, you’ll have too many platforms to worry about
Reply 0 0
r2k-in-the-vortex
There is no compile to C option, the way LLVM works is that it consists of front ends and back ends. Front ends support the various languages, in this case ST. They all compile whatever language they support to LLVM intermediate representation language, which is a sort of generic assembly language. Back ends turn that to whatever architecture you need. Because of the IR in between the front end doesn't need to worry about architecture and back end doesn't need to worry about the original language. Clang is a LLVM front end for C, so those compiling Matiec generated C with that already use LLVM.

There probably are some portability issues on embedded platforms, I don't imagine absolutely everything out there having a LLVM back end ready to go. But the point of the exercise is not to replace existing working pipeline, but to provide a alternative to it. If that alternative has some limitations.... then it has some limitations.

Rusty itself definitely has its limitations, it is a work in progress, not a mature and tested compiler. But.. you can look at the repo yourself, it's developing at a rapid pace and the devs are doing it right, things are well arranged, test coverage is high, good practices are kept to, lots of things going for it that matiec doesn't have and likely will never have.
Reply 0 0
r2k-in-the-vortex
Last time I got stuck on this because of some compatibility issues with compiling for arm, which is my main platform of interest.
 
But now some things are fixed and it's not an issue anymore
So I gobbled together a testcase of cpp code calling a program compiled from ST using rusty compiler. It's possible to debug it, remotely! This is VS code running on desktop and compiled structure text object running on pi, called from c++ main program (not openplc main program, but just from a tescase).
 
I think variable linking is also doable, in this case the c++ main program is assigning 7 to udint_input which is declared in a GVL and reading 
udint_output is also no problem. 
 
Of course there are still bunch of things to figure out to make it even remotely usable, but I think ability to debug ST code alone should be massive motivation. Can't do that with matiec.
 
[203128%2F8605093%2Fmceclip0]
Reply 0 0
thiagoralves
This is mind blowing! So this thing can generate machine code from ST? How does it work on the hardware side of things? Like how can I trigger a GPIO pin from ST code?
Reply 0 0
r2k-in-the-vortex
Yeah, rusty is a full compiler, not a translator like MatIEC. But it's just a compiler, it's not a runtime, you can write a program or library in IEC but all the IO access and such you have to handle externally.
 
From the PLC side it has to work the same way, you declare your IOs like normal and the runtime puts things together.
 
[203128%2F8606603%2Fmceclip0]
 
The part about how to mate it with OpenPLC is what I'm trying to figure out right now. I think I can pretty much just slot it in and rework the variable gluing.
 
But there are nuances, I can't just have OpenPLC directly accessing something like this boolean_input, guaranteed to run into variable name conflicts in no time depending on how the PLC user decides to name their variables. Luckily OpenPLC itself is compiled as c++ so variable name mangling prevents most of the issues, but I'm not so sure about all the utilities, lots of things on global scope and it all has to link together in the end.
 
You can have a look at what I have cooked up so far, just testing out ideas so far, but it looks reasonably promising.  https://github.com/r2k-in-the-vortex/ExamplePrjForRustyIntegration
Reply 0 0
Reply