summaryrefslogtreecommitdiff
path: root/compile.ml
blob: 039383881136fe8b46f84836737a03702faa3f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
open Format
open Mips
open Ast
open Utils
open Closure
open Optimize
open Primitives

module Smap = Map.Make(String)

type env = { mutable addrs : ident_address Smap.t ;
             mutable fp : int }
let empty_env = { addrs = Smap.empty ; fp = 0 }

let frame_size = ref 0

let next_string_id = ref 1
let next_jump_id = ref 1

let heap_push n =
    Addi (SP, SP, - n)
let heap_pop n =
    Addi (SP, SP, n)

let push reg =
    [Addi (SP, SP, -4) ;
     Sw (reg, (0, SP))]

let pop reg =
    [Lw (reg, (0, SP)) ;
     Addi (SP, SP, 4)]

let malloc n =
    [Li (V0, 9) ;
     Li (A0, n) ;
     Syscall]

(* Compilation des listes :
   allocation d'un bloc de 4 + 4 = 8 octets contenant, en supposant que
   l'adresse soit dans le registre V0, la valeur ou le pointeur de la valeur
   de tête dans 0(V0), et le pointeur vers la queue dans 4(V0) *)

let new_string_label () =
    let label = Printf.sprintf "string___%d" !next_string_id
    in
        incr next_string_id;
        label

let new_jump_label () =
    let label = Printf.sprintf "jump___%d" !next_jump_id
    in
        incr next_jump_id;
        label

let assign_ident env ident =
	if !frame_size = env.fp then frame_size := 4 + !frame_size;
    let new_addrs = Smap.add ident (Areg (env.fp, FP)) env.addrs
    and curr_fp = env.fp
    in
        [Lw (V0, (curr_fp, FP))], { addrs = new_addrs ; fp = env.fp + 4 }

let rec assign_motif env m =
    match m.m with
        | Munderscore -> [], env
        | Mident ident -> assign_ident env ident
        | Mtuple l ->
            let i = ref (-1)
            in
                let instrs, env =
                    List.fold_left (fun (base_instrs, env) m ->
                                        let instrs, env2 = assign_motif env m
                                        in
                                            incr i;
                                            (base_instrs
                                             @
                                             [Lw (V0, (0, SP)) ;
                                              Lw (V0, (4 * !i, V0))]
                                             @
                                             instrs, env2))
                                   ([heap_push 4 ;
                                     Sw (V0, (0, SP))], env)
                                   l
                in
                    instrs @ [heap_pop 4], env

let ident_to_reg env ident reg =
    match Smap.find ident env.addrs with
        | Alab label -> [Lw_label (reg, label)]
        | Areg radd -> [Lw (reg, radd)]

(* Compile une expression, et assure que le résultat sera dans V0 *)
let rec compile_expr env expr =
    match expr.e with
        | Econst (Cint c) ->
            [Li (V0, c)]
        | Econst (Cstring s) ->
            let label = new_string_label ()
            in
                [Asciiz (label, s);
                 La (V0, label)]
        | Econst (Cbool true) ->
            [Li (V0, 1)]
        | Econst (Cbool false)
        | Econst Cunit
        | Econst Cemptylist ->
            [Li (V0, 0)]
        | Eident ident ->
            ident_to_reg env ident V0
        | Etuple l ->
            let len = List.length l
            and i = ref (-1)
            in
                malloc (4 * len)
                @
                [heap_push 4 ;
                 Sw (V0, (0, SP))]
                @
                (List.fold_left (fun base e ->
                                    incr i;
                                    base
                                    @
                                    (compile_expr env e)
                                    @
                                    [Lw (T0, (0, SP)) ;
                                     Sw (V0, (4 * !i, T0))])
                                [] l)
                @
                [heap_pop 4]
        | Ebinop (e1, o, e2) ->
            (compile_expr env e1)
            @
            [heap_push 4 ;
             Sw (V0, (0, SP))]
            @
            (compile_expr env e2)
            @
            [Lw (A0, (0, SP)) ;
             begin
                match o with
                    | OAdd -> Add (V0, V0, A0)
                    | OSub -> Sub (V0, V0, A0)
                    | OMul -> Mul (V0, V0, A0)
                    | ODiv -> Div (V0, V0, A0)
                    | OAnd -> And (V0, V0, A0)
                    | OOr -> Or (V0, V0, A0)
                    | OEq -> Seq (V0, V0, A0)
                    | ONeq -> Sne (V0, V0, A0)
                    | OLt -> Slt (V0, V0, A0)
                    | OLe -> Sle (V0, V0, A0)
                    | OGt -> Sgt (V0, V0, A0)
                    | OGe -> Sge (V0, V0, A0)
             end ;
             heap_pop 4]
        | Eunop (o, e) ->
            (compile_expr env e)
            @
            [begin
                match o with
                    | ONot -> Not (V0, V0)
                    | ONeg -> Neg (V0, V0)
             end]
        | Eletin (m, val_expr, body_expr) ->
            let val_instrs = compile_expr env val_expr
            and set_instrs, env2 = assign_motif env m
            in
                val_instrs
                @
                set_instrs
                @
                (compile_expr env2 body_expr)
        | Eif (cond_expr, then_expr, else_expr) ->
            let else_label = new_jump_label ()
            and end_label = new_jump_label ()
            in
                (compile_expr env cond_expr)
                @
                [Blez (V0, else_label)]
                @
                (compile_expr env then_expr)
                @
                [J end_label ;
                 Label else_label]
                @
                (compile_expr env else_expr)
                @
                [Label end_label]
        | Elistcons (head_expr, tail_expr) ->
            (compile_expr env head_expr)
            @
            [heap_push 8 ; (* On réserve direct les deux blocs pour backup
                              de la valeur de tête de de queue *)
             Sw (V0, (4, SP))]
            @
            (compile_expr env tail_expr)
            @
            [Sw (V0, (0, SP))]
            @
            (malloc 8)
            @
            [Lw (A1, (0, SP)) ;
             Lw (A0, (4, SP)) ;
             Sw (A0, (0, V0)) ;
             Sw (A1, (4, V0)) ;
             heap_pop 8]
        | Ecall (func_expr, arg_expr) ->
            (compile_expr env func_expr)
            @
            [heap_push 4 ;
             Sw (V0, (0, SP))]
            @
            (compile_expr env arg_expr)
            @
            [Move (A1, V0) ; (* Cloture dans A0, argument dans A1,
                                adresse du bloc de code dans T0 *)
             Lw (A0, (0, SP)) ;
             Lw (T0, (0, A0)) ;
             Jalr (T0, RA)]
        | Ematch (match_expr, empty_expr, head_m, tail_m, result_expr) ->
            (* Petit trick : a::b est représenté comme (a, b) dans la
               mémoire, abusons donc le processus pour faciliter l'assignation
               Note : on pourrait (en plus) remplacer le match par un if
               bien placé, mais ça serait vraiment coquin *)
            let empty_label = new_jump_label ()
            and end_label = new_jump_label ()
            and set_instrs, env2 = assign_motif env
                                                (raw_motif (Mtuple [head_m ;
                                                                    tail_m]))
            in
                (compile_expr env match_expr)
                @
                [Blez (V0, empty_label)]
                @
                set_instrs
                @
                (compile_expr env2 result_expr)
                @
                [J end_label ;
                 Label empty_label]
                @
                (compile_expr env empty_expr)
                @
                [Label end_label]
        | Efunc _ -> failwith "compile_expr : Unreachable matching"
        | Eclos (fname, vars) ->
            let i = ref 0
            in
                (malloc ((List.length vars) + 1))
                @
                (List.fold_left (fun base ident ->
                                    incr i;
                                    base
                                    @
                                    (ident_to_reg env ident A0)
                                    @
                                    [Lw (A0, (4 * !i, V0))])
                                [La (A0, fname) ;
                                 Lw (A0, (0, V0))]
                                vars)
and compile_letfuns env = function
    | [] -> [], env
    | (fname, fvars, farg, fbody)::l ->
        let letfuns, env = (compile_letfuns env l)
        in
            [Label fname ;
             Addi (SP, SP, -8) ;
             Sw (RA, (4, SP)) ;
             Sw (FP, (0, SP))]
            @
            (compile_expr env fbody)
            @
            [Lw (RA, (4, SP)) ;
             Lw (FP, (0, SP)) ;
             Jr RA]
            @
            letfuns, env

let header_code =
    print_int_code
    @
    [Label "main" ;
     Addi (SP, SP, -(!frame_size)) ;
     Addi (FP, SP, !frame_size - 4) ;
     Move (T9, RA)]
and footer_code =
    [Move (RA, T9) ;
     Addi (SP, SP, !frame_size) ;
     Jr (RA)]

let base_env =
    { empty_env with addrs = Smap.add "print_int"
                                      (Alab "print_int")
                                      Smap.empty }

let compile a ofile =
    let a, letfuns = replace_funcs (optimize_ast a)
    in
        let compiled_letfuns, env = compile_letfuns base_env letfuns
        in
            let instructions = header_code
                             @ (compile_expr env a)
                             @ footer_code
            and f = open_out ofile
            in
                let program = optimize_program (filter_program instructions)
                and fmt = formatter_of_out_channel f
                in
                        format_program fmt program;
                        fprintf fmt "@?";
                        close_out f