1 | |
---|
2 | (** This module defines the abstract syntax tree of [RTL]. *) |
---|
3 | |
---|
4 | (* Adapted from Pottiers's Pseudo-Pascal pedagogical compiler *) |
---|
5 | |
---|
6 | (* This is the definition of the abstract syntax for the Explicit |
---|
7 | Register Transfer Language (ERTL). |
---|
8 | |
---|
9 | This language is very much like RTL, except the calling convention |
---|
10 | has been made explicit. That is, functions and procedures no longer |
---|
11 | accept parameters and return results via a high-level mechanism; |
---|
12 | instead, they do so via either hardware registers or stack |
---|
13 | slots. |
---|
14 | |
---|
15 | Functions and procedures no longer magically return to their caller: instead, |
---|
16 | a new [St_return] instruction appears, whose semantics is to transfer control |
---|
17 | to the address stored in the return address registers. |
---|
18 | |
---|
19 | Functions and procedures are no longer explicitly distinguished: functions |
---|
20 | are simply procedures that happen to write the hardware register. There only |
---|
21 | remains a distinction at [St_return] instructions (see below). |
---|
22 | |
---|
23 | Two new instructions, [St_newframe] and [St_delframe], appear in order to |
---|
24 | allocate and release stack frames. They will be translated, in the final |
---|
25 | assembly code, to arithmetic on the stack pointer registers. *) |
---|
26 | |
---|
27 | (* Stack organization. |
---|
28 | |
---|
29 | The stack is going from top to bottom. Below is a schema of the stack |
---|
30 | organization viewed by the function being executed. |
---|
31 | |
---|
32 | formal parameters (the first parameter is at the top) |
---|
33 | spilled variables (the first spilled variable is a the top) |
---|
34 | local variables (the first local variable is at the bottom) |
---|
35 | stack pointer -> |
---|
36 | *) |
---|
37 | |
---|
38 | type registers = Register.t list |
---|
39 | |
---|
40 | type statement = |
---|
41 | |
---|
42 | (* The empty statement. *) |
---|
43 | | St_skip of Label.t |
---|
44 | |
---|
45 | (* Comment. *) |
---|
46 | | St_comment of string * Label.t |
---|
47 | |
---|
48 | (* Emit a cost label. *) |
---|
49 | | St_cost of CostLabel.t * Label.t |
---|
50 | |
---|
51 | (* Assign the content of a hardware register to a pseudo register. Parameters |
---|
52 | are the destination pseudo register, the source hardware register, and the |
---|
53 | label of the next statement. *) |
---|
54 | | St_get_hdw of Register.t * I8051.register * Label.t |
---|
55 | |
---|
56 | (* Assign the content of a pseudo register to a hardware register. Parameters |
---|
57 | are the destination hardware register, the source pseudo register, and the |
---|
58 | label of the next statement. *) |
---|
59 | | St_set_hdw of I8051.register * Register.t * Label.t |
---|
60 | |
---|
61 | (* Assign the content of a hardware register to a hardware |
---|
62 | register. Parameters are the destination register, the source register, and |
---|
63 | the label of the next statement. Only used to save the return value before |
---|
64 | the epilogue and restore it right before leaving the function. *) |
---|
65 | | St_hdw_to_hdw of I8051.register * I8051.register * Label.t |
---|
66 | |
---|
67 | (* Allocate required space on the stack for the function. Parameter is the |
---|
68 | label of the next statement. *) |
---|
69 | | St_newframe of Label.t |
---|
70 | |
---|
71 | (* Deallocate required space on the stack for the function. Parameter is the |
---|
72 | label of the next statement. *) |
---|
73 | | St_delframe of Label.t |
---|
74 | |
---|
75 | (* Assign the frame size to a register. Parameters are the destination |
---|
76 | register, and the label of the next statement. *) |
---|
77 | | St_framesize of Register.t * Label.t |
---|
78 | |
---|
79 | (* Pop a value from the IRAM to a register. Parameter are the destination |
---|
80 | register, and the label of the next statement. *) |
---|
81 | | St_pop of Register.t * Label.t |
---|
82 | |
---|
83 | (* Push a value from a register to the IRAM. Parameter are the source |
---|
84 | register, and the label of the next statement. *) |
---|
85 | | St_push of Register.t * Label.t |
---|
86 | |
---|
87 | (* Assign the most significant bits of the address of a symbol to a |
---|
88 | register. Parameters are the destination register, the symbol and the label |
---|
89 | of the next statement. *) |
---|
90 | | St_addrH of Register.t * AST.ident * Label.t |
---|
91 | |
---|
92 | (* Assign the least significant bits of the address of a symbol to a |
---|
93 | register. Parameters are the destination register, the symbol and the label |
---|
94 | of the next statement. *) |
---|
95 | | St_addrL of Register.t * AST.ident * Label.t |
---|
96 | |
---|
97 | (* Assign an integer constant to a register. Parameters are the destination |
---|
98 | register, the integer and the label of the next statement. *) |
---|
99 | | St_int of Register.t * int * Label.t |
---|
100 | |
---|
101 | (* Move the content of a register to another. Parameters are the destination |
---|
102 | register, the source register, and the label of the next statement. *) |
---|
103 | | St_move of Register.t * Register.t * Label.t |
---|
104 | |
---|
105 | (* Apply a binary operation that will later be translated in an operation on |
---|
106 | the accumulators. Parameters are the operation, the destination register, |
---|
107 | the source registers, and the label of the next statement. *) |
---|
108 | | St_opaccs of I8051.opaccs * Register.t * Register.t * Register.t * Label.t |
---|
109 | |
---|
110 | (* Apply an unary operation. Parameters are the operation, the destination |
---|
111 | register, the source register, and the label of the next statement. *) |
---|
112 | | St_op1 of I8051.op1 * Register.t * Register.t * Label.t |
---|
113 | |
---|
114 | (* Apply a binary operation. Parameters are the operation, the destination |
---|
115 | register, the source registers, and the label of the next statement. *) |
---|
116 | | St_op2 of I8051.op2 * Register.t * Register.t * Register.t * Label.t |
---|
117 | |
---|
118 | (* Set the carry flag to zero. Parameter is the label of the next |
---|
119 | statement. *) |
---|
120 | | St_clear_carry of Label.t |
---|
121 | |
---|
122 | (* Load from external memory. Parameters are the destination register, the |
---|
123 | address registers, and the label of the next statement. *) |
---|
124 | | St_load of Register.t * Register.t * Register.t * Label.t |
---|
125 | |
---|
126 | (* Store to external memory. Parameters are the address registers, the source |
---|
127 | register, and the label of the next statement. *) |
---|
128 | | St_store of Register.t * Register.t * Register.t * Label.t |
---|
129 | |
---|
130 | (* Call to a function given its name. Parameters are the name of the function, |
---|
131 | the number of arguments of the function, and the label of the next |
---|
132 | statement. *) |
---|
133 | | St_call_id of AST.ident * int * Label.t |
---|
134 | |
---|
135 | (* |
---|
136 | (* Call to a function given its address. Parameters are registers holding the |
---|
137 | address of the function, the arguments of the function, the destination |
---|
138 | registers, and the label of the next statement. *) |
---|
139 | | St_call_ptr of registers * register list * registers * Label.t |
---|
140 | |
---|
141 | (* Tail call to a function given its name. Parameters are the name of the |
---|
142 | function, and the number of arguments of the function. *) |
---|
143 | | St_tailcall_id of AST.ident * int |
---|
144 | |
---|
145 | (* Tail call to a function given its address. Parameters are registers holding |
---|
146 | the address of the function, and the arguments of the function. *) |
---|
147 | | St_tailcall_ptr of registers * register list |
---|
148 | *) |
---|
149 | |
---|
150 | (* Branch. Parameters are the register holding the value for the branching, |
---|
151 | the label to go to when the value is not 0, and the label to go to when the |
---|
152 | value is 0. *) |
---|
153 | | St_condacc of Register.t * Label.t * Label.t |
---|
154 | |
---|
155 | (* Transfer control to the address stored in the return address registers. *) |
---|
156 | | St_return of registers |
---|
157 | |
---|
158 | type graph = statement Label.Map.t |
---|
159 | |
---|
160 | type internal_function = |
---|
161 | { f_luniverse : Label.Gen.universe ; |
---|
162 | f_runiverse : Register.universe ; |
---|
163 | f_params : int ; |
---|
164 | f_locals : Register.Set.t ; |
---|
165 | f_stacksize : int ; |
---|
166 | f_graph : graph ; |
---|
167 | f_entry : Label.t ; |
---|
168 | f_exit : Label.t } |
---|
169 | |
---|
170 | type function_def = |
---|
171 | | F_int of internal_function |
---|
172 | | F_ext of AST.external_function |
---|
173 | |
---|
174 | (* A program is a list of global variables and their reserved space, a list of |
---|
175 | function names and their definition, and the name of the main function. *) |
---|
176 | |
---|
177 | type program = |
---|
178 | { vars : (AST.ident * int (* size *)) list ; |
---|
179 | functs : (AST.ident * function_def) list ; |
---|
180 | main : AST.ident option } |
---|