summaryrefslogtreecommitdiff
path: root/mips.ml
blob: abc0d0ae1b1da49f4144719e3b771a1c3ef46f7f (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
(****************************************
  mips.ml - opérations mips & formatage
 ****************************************)

open Format

type register = 
    | ZERO
    | V0 | V1
    | A0 | A1 | A2 | A3
    | T0 | T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9
    | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9
    | RA | GP | SP | FP

type label = string
type regaddress = int * register

type ident_address =
    | Alab of label
    | Areg of register
    | Amem of regaddress
    | Aclosure of int * regaddress

type instruction =
    | Move of register * register
    | Li of register * int
    | La of register * label
    | Lw of register * regaddress
    | Sw of register * regaddress
    | Lw_label of register * label
    | Sw_label of register * label
    | Add of register * register * register
    | Addi of register * register * int
    | Sub of register * register * register
    | Mul of register * register * register
    | Div of register * register * register
    | Neg of register * register
    | Not of register * register
    | And of register * register * register
    | Or of register * register * register
    | Seq of register * register * register
    | Sne of register * register * register
    | Slt of register * register * register
    | Sle of register * register * register
    | Sgt of register * register * register
    | Sge of register * register * register
    | Bgtz of register * label
    | Blez of register * label
    | J of label
    | Jal of label
    | Jalr of register
    | Jr of register
    | Syscall
    | Comment of string
    | Label of label
    | Asciiz of label * string
    | Word of label * int

(* Formateurs divers et variés *)
let print_register fmt = function
    | ZERO -> pp_print_string fmt "$zero"
    | V0 -> pp_print_string fmt "$v0"
    | V1 -> pp_print_string fmt "$v1"
    | A0 -> pp_print_string fmt "$a0"
    | A1 -> pp_print_string fmt "$a1"
    | A2 -> pp_print_string fmt "$a2"
    | A3 -> pp_print_string fmt "$a3"
    | T0 -> pp_print_string fmt "$t0"
    | T1 -> pp_print_string fmt "$t1"
    | T2 -> pp_print_string fmt "$t2"
    | T3 -> pp_print_string fmt "$t3"
    | T4 -> pp_print_string fmt "$t4"
    | T5 -> pp_print_string fmt "$t5"
    | T6 -> pp_print_string fmt "$t6"
    | T7 -> pp_print_string fmt "$t7"
    | T8 -> pp_print_string fmt "$t8"
    | T9 -> pp_print_string fmt "$t9"
    | S0 -> pp_print_string fmt "$s0"
    | S1 -> pp_print_string fmt "$s1"
    | S2 -> pp_print_string fmt "$s2"
    | S3 -> pp_print_string fmt "$s3"
    | S4 -> pp_print_string fmt "$s4"
    | S5 -> pp_print_string fmt "$s5"
    | S6 -> pp_print_string fmt "$s6"
    | S7 -> pp_print_string fmt "$s7"
    | S8 -> pp_print_string fmt "$s8"
    | S9 -> pp_print_string fmt "$s9"
    | RA -> pp_print_string fmt "$ra"
    | GP -> pp_print_string fmt "$gp"
    | SP -> pp_print_string fmt "$sp"
    | FP -> pp_print_string fmt "$fp"

let print_regaddress fmt (offset, r) =
    fprintf fmt "%d(%a)" offset print_register r

let print_instruction fmt = function
    | Move (dst, src) ->
        fprintf fmt "\tmove %a, %a\n" print_register dst print_register src
    | Li (r, i) ->
        fprintf fmt "\tli   %a, %d\n" print_register r i
    | La (r, s) ->
        fprintf fmt "\tla   %a, %s\n" print_register r s
    | Lw (r, a) ->
        fprintf fmt "\tlw   %a, %a\n" print_register r print_regaddress a
    | Sw (r, a) ->
        fprintf fmt "\tsw   %a, %a\n" print_register r print_regaddress a
    | Lw_label (r, l) ->
        fprintf fmt "\tlw   %a, %s\n" print_register r l
    | Sw_label (r, l) ->
        fprintf fmt "\tsw   %a, %s\n" print_register r l
    | Add (dst, src1, src2) ->
        fprintf fmt "\tadd %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Addi (dst, src1, i) ->
        fprintf fmt "\taddi %a, %a, %d\n" print_register dst
                                          print_register src1
                                          i
    | Sub (dst, src1, src2) ->
        fprintf fmt "\tsub %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Mul (dst, src1, src2) ->
        fprintf fmt "\tmul %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Div (dst, src1, src2) ->
        fprintf fmt "\tdiv %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | And (dst, src1, src2) ->
        fprintf fmt "\tand %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Or (dst, src1, src2) ->
        fprintf fmt "\tor %a, %a, %a\n" print_register dst
                                        print_register src1
                                        print_register src2
    | Neg (dst, src) ->
        fprintf fmt "\tneg %a, %a\n" print_register dst
                                     print_register src
    | Not (dst, src) ->
        fprintf fmt "\tnot %a, %a\n" print_register dst
                                     print_register src
    | Seq (dst, src1, src2) ->
        fprintf fmt "\tseq %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Sne (dst, src1, src2) ->
        fprintf fmt "\tsne %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Slt (dst, src1, src2) ->
        fprintf fmt "\tslt %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Sle (dst, src1, src2) ->
        fprintf fmt "\tsle %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Sgt (dst, src1, src2) ->
        fprintf fmt "\tsgt %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Sge (dst, src1, src2) ->
        fprintf fmt "\tsge %a, %a, %a\n" print_register dst
                                         print_register src1
                                         print_register src2
    | Bgtz (src, label) ->
        fprintf fmt "\tbgtz %a, %s\n" print_register src label
    | Blez (src, label) ->
        fprintf fmt "\tblez %a, %s\n" print_register src label
    | J s ->
        fprintf fmt "\tj  %s\n" s
    | Jal s ->
        fprintf fmt "\tjal  %s\n" s
    | Jalr radd ->
        fprintf fmt "\tjalr %a\n" print_register radd
    | Jr r ->
        fprintf fmt "\tjr   %a\n" print_register r
    | Syscall ->
        fprintf fmt "\tsyscall\n"
    | Label s ->
        fprintf fmt "%s:\n" s
    | Comment s ->
        fprintf fmt "\t# %s\n" s
    | Asciiz _
    | Word _ -> failwith "print_instruction : Unreachable matching"

let rec output_byte = function
    | "" -> "0"
    | s ->
        let c = s.[0] and s2 = String.sub s 1 ((String.length s) - 1)
        in
            (Printf.sprintf "%d, " (int_of_char c)) ^ (output_byte s2)

let print_data fmt = function
    | Asciiz (l, s) ->
        fprintf fmt "%s:\t.byte %s\n" l (output_byte s)
    | Word (l, n) ->
        fprintf fmt "%s:\t.word %d\n" l n
    | _ -> failwith "print_data : Unreachable matching"

type program =
    { text : instruction list ;
      data : instruction list }

(* Supprime les commentaires du code assembleur *)
let rec filter_comments = function
    | [] -> []
    | (Comment c)::l -> filter_comments l
    | x::l -> x::(filter_comments l)

(* Sépare les data des instructions text *)
let rec filter_program = function
    | [] -> { text = [] ; data = [] }
    | (Asciiz (label, s))::l ->
        let p = filter_program l
        in
            {p with data = (Asciiz (label, s))::p.data}
    | (Word (label, i))::l ->
        let p = filter_program l
        in
            {p with data = (Word (label, i))::p.data}
    | instr::l ->
        let p = filter_program l
        in
            {p with text = instr::p.text}

let format_program fmt p =
    fprintf fmt "\t.text\n";
    List.iter (print_instruction fmt) p.text;
    fprintf fmt "\t.data\n";
    List.iter (print_data fmt) p.data