Peter Nachtwey
Member
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:
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.
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
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.My parser builds the parse tree.
Those are done by the recursive subroutines.Then I've got different visitors which walk through the parse tree an do things
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.like code optimization like constant folding and the code generation.
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.