Writing a ST compiler

Interesting.

Thomas_v2, you must work for Siemens then.

I tried compiling your example. I don't support user functions yet. I don't to automatic conversion because that is not in the specifications and for some reason I couldn't get my VAR_TEMP..END_VAR to work but with a few modifications to your code I was able to generate this:

Code:
PROGRAM TEST
	VAR_TEMP
        Y,A,B,C,D,E,F,T: REAL;
        I,J : INT;
	END_VAR

	I := 123;
	a := 5.6;
	b := 21;
	y:= ( a + INT_TO_REAL(i) ) * b;

END_PROGRAM


Generating Code

TEST:   LD      ANY_INT# 123
        ST      INT# I  ; offset=32

        LD      ANY_REAL# 5.600000
        ST      REAL# A ; offset=4

        LD      ANY_INT# 21
        ST      REAL# B ; offset=8

        LD      REAL# A ; offset=4
        LD      INT# I  ; offset=32
        INT_TO_REAL
        ADD     ; REAL
        LD      REAL# B ; offset=8
        MUL     ; REAL
        ST      REAL# Y ; offset=0

        RET     ; END_PROGRAM


PROGRAM #OPR = 3
    LABEL NULL# TEST
    VAR_DECLARATIONS #OPR = 2
        INT# 0
        LIST #OPR = 2
            VAR1_INIT_DECL #OPR = 2
                VAR1_LIST #OPR = 2
                    VAR1_LIST #OPR = 2
                        VAR1_LIST #OPR = 2
                            VAR1_LIST #OPR = 2
                                VAR1_LIST #OPR = 2
                                    VAR1_LIST #OPR = 2
                                        VAR1_LIST #OPR = 2
                                            VAR REAL# Y
                                            VAR REAL# A
                                        VAR REAL# B
                                    VAR REAL# C
                                VAR REAL# D
                            VAR REAL# E
                        VAR REAL# F
                    VAR REAL# T
                TYPE = REAL
            VAR1_INIT_DECL #OPR = 2
                VAR1_LIST #OPR = 2
                    VAR INT# I
                    VAR INT# J
                TYPE = INT
    STATEMENT_LIST #OPR = 2
        STATEMENT_LIST #OPR = 2
            STATEMENT_LIST #OPR = 2
                ASSIGN #OPR = 2
                    VAR INT# I
                    ANY_INT# 123
                ASSIGN #OPR = 2
                    VAR REAL# A
                    ANY_REAL# 5.600000
            ASSIGN #OPR = 2
                VAR REAL# B
                ANY_INT# 21
        ASSIGN #OPR = 2
            VAR REAL# Y
            '*' #OPR = 2
                '+' #OPR = 2
                    VAR REAL# A
                    FUNCTION #OPR = 2
                        INT_TO_REAL FC INT_TO_REAL
                        VAR INT# I
                VAR REAL# B

My parser builds the parse tree.
You can see I have a parse tree too. Right now I think of the pseudo assembly as the intermediate code. However, much of the pseudo assembly will translate instruction for instruction. The micro controller I am using only accesses memory from load and store instructions. Other instructions like + and * only work on registers.

Then I've got different visitors which walk through the parse tree an do things
Those are done by the recursive subroutines.

like code optimization like constant folding and the code generation.
I haven't worked on that too hard. I will do so later and I think it will be the fun part. My target micro controller has 32 registers. It is a freescale micro controller. I can easily map 8 most recently used variables into these registers and like I said, the micro controller has a pretty big cache. This gives me a huge advantage in the ability to optimize over writing code for the S7 which has few registers and each is special.

About the optimization. Like I said I haven't thought about it too much but I have thought about cases like

C:=3+A+B+4;

The 3 and 4 should be combined but they are at different points in the tree.
This is the part I haven't got figured out yet.

I bought the dragon book a long time ago. It cost $19.95 and is the second printing from 1978. As I said before, I always wanted to write a compiler. I think I have waited long enough. Most is now obsolete unless one is writing the Flex and Bison code too.
 

Similar Topics

Hi, I'm having an issue in crimson 3.0 when I create a programme using a case statement referencing a fault word that each bit needs to change the...
Replies
5
Views
241
Hello all, I'm currently working on a servo motor linear positioning system (ball screw). I'm all set up regarding communication between my HMI...
Replies
1
Views
92
Hello All: I have a Windows 10 PC running a FTView ME Station 10.00. I have a .mer file version 6.00. It has been running well on it for a long...
Replies
1
Views
171
My R55 Ingersollrand is tripping on motor overload and im falling to see the trip history it is writing Acquarring texts
Replies
0
Views
134
Hi. Not the fist time I'm saying this but just to give a background. 95% of my PLC programming has used Schneider over the last 10 years on...
Replies
66
Views
4,977
Back
Top Bottom