[2] | 1 | = INTRODUCTION =
|
---|
| 2 |
|
---|
| 3 | Welcome reader,
|
---|
| 4 |
|
---|
| 5 | You will find our solution on assignment 5 of Compiler Constructions 2008
|
---|
| 6 | giving at LIACS University. First we will you a grand overview of the files
|
---|
| 7 | altered, next we will bullet point the most important design decisions.
|
---|
| 8 |
|
---|
| 9 | And the grand finale will show the usage of the program.
|
---|
| 10 |
|
---|
| 11 | Enjoy!
|
---|
| 12 | Johan and Rick <hvdzwet@liacs.nl>
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | = FILES EDITED/ADDED =
|
---|
| 16 | E ICGenerator.cc = Intermidiate code generator C++ source
|
---|
| 17 | E ICGenerator.h = Intermidiate code generator C++ source
|
---|
| 18 | E main.cc = Main program function, argument parsing
|
---|
| 19 |
|
---|
| 20 | = DESIGN DECISIONS =
|
---|
| 21 | * Constants are evaluated upfront, also nested ones. Optimalisations are
|
---|
| 22 | included into the tree and generated on the fly
|
---|
| 23 |
|
---|
| 24 | * In order to keep the code readable human-readable tests are defined like
|
---|
| 25 | IsZero, IsConstant.
|
---|
| 26 |
|
---|
| 27 | * Made icode, symtab global to avoid calling the Treewalk with a whole lot of
|
---|
| 28 | arguments every time.
|
---|
| 29 |
|
---|
| 30 | * Recursive walking of Tree using Treewalk allowing us to generate code on the
|
---|
| 31 | fly on three different 'location' e.g. first encounter, midle pass, last visit.
|
---|
| 32 |
|
---|
| 33 | * Tried various optimatisations on PostProcess, by changing variable names,
|
---|
| 34 | removing assignments, but yet all led to race conditions.
|
---|
| 35 |
|
---|
| 36 | * Wrote some theoritical tests PostProcess which could be implemented.
|
---|
| 37 |
|
---|
| 38 | * Constants in multiple additions (e.g. 10 + a + 20 + b) will be simplified 30 + a + b
|
---|
| 39 |
|
---|
| 40 | * Constants in multiple combinations of additions and substractions will be
|
---|
| 41 | simplyfied (e.g. 10 + a - 20 + b) will be simplified -10 + a + b
|
---|
| 42 |
|
---|
| 43 | * In order to allow the full folding, NODE_ADD and NODE_SUB are evaluated
|
---|
| 44 | differently, Like first right child, then left child.
|
---|
| 45 |
|
---|
| 46 | * Full constant folding on NODE_MUL has not been implemented, but will goes
|
---|
| 47 | more and less the same as the code at NODE_ADD
|
---|
| 48 |
|
---|
| 49 | = SAMPLE GENERATED CODE =
|
---|
| 50 | C01) program example;
|
---|
| 51 | C02)
|
---|
| 52 | C03) var x, y, z: integer;
|
---|
| 53 | C04) function foo(a,b: integer): integer;
|
---|
| 54 | C05) begin
|
---|
| 55 | C06) foo := 1
|
---|
| 56 | C07) end;
|
---|
| 57 | C08)
|
---|
| 58 | C09) begin
|
---|
| 59 | C10) x := readinteger;
|
---|
| 60 | C11) y := 20;
|
---|
| 61 | C12) z := x + 20;
|
---|
| 62 | C13) z := z + foo(0,0)
|
---|
| 63 | C14) end.
|
---|
| 64 |
|
---|
| 65 | Program: example
|
---|
| 66 | # operator | operand1 | operand2 | result
|
---|
| 67 | ------------------+-------------------+-------------------+-------------------
|
---|
| 68 | 0: CALL FUNC | sym: readinteger | | sym: t1
|
---|
| 69 | 1: ASSIGN_I | sym: t1 | | sym: x
|
---|
| 70 | 2: ASSIGN_I | int: 20 | | sym: y
|
---|
| 71 | 3: ADD_I | sym: x | int: 20 | sym: t2
|
---|
| 72 | 4: ASSIGN_I | sym: t2 | | sym: z
|
---|
| 73 | 5: PARAM_I | int: 0 | |
|
---|
| 74 | 6: PARAM_I | int: 0 | |
|
---|
| 75 | 7: CALL FUNC | sym: foo | | sym: t3
|
---|
| 76 | 8: ADD_I | sym: z | sym: t3 | sym: t4
|
---|
| 77 | 9: ASSIGN_I | sym: t4 | | sym: z
|
---|
| 78 | 10: RETURN | | |
|
---|
| 79 |
|
---|
| 80 | 11: SUBPROG | sym: foo | |
|
---|
| 81 | 12: ASSIGN_I | int: 1 | | sym: foo
|
---|
| 82 | 13: RETURN_I | sym: foo | |
|
---|
| 83 |
|
---|
| 84 | ------------------+-------------------+-------------------+-------------------
|
---|
| 85 |
|
---|
| 86 | Line 0,1 will be line C10. Direct assignments like line C11 will only use one
|
---|
| 87 | line 3. When calling functions (C13), first the parameters will be set on
|
---|
| 88 | place at line 5,6 then the function itself will be caled at line 7.
|
---|
| 89 |
|
---|
| 90 | Some book keeping could be found at line 13 (return)
|
---|
| 91 |
|
---|
| 92 | = USAGE =
|
---|
| 93 | # Compile
|
---|
| 94 | $ make dirs
|
---|
| 95 | $ make
|
---|
| 96 | # Call without optimalisation
|
---|
| 97 | $ ./comp -w -00 <testfile
|
---|
| 98 | # Call with optimalisation
|
---|
| 99 | $ ./comp -w -01 <testfile
|
---|