remove data duplicata

This commit is contained in:
Mylloon 2022-12-13 14:50:18 +01:00
parent f9ef6bc20c
commit fd6ca52efc
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,15 +1,21 @@
open Ast open Ast
open Baselib
let collect_constant_strings code = let collect_constant_strings code =
let counter = ref 0 in let counter = ref 0 in
let env = ref Env.empty in
let ccs_value = function let ccs_value = function
| V1.Void -> V2.Void, [] | V1.Void -> V2.Void, []
| V1.Bool b -> V2.Bool b, [] | V1.Bool b -> V2.Bool b, []
| V1.Int n -> V2.Int n, [] | V1.Int n -> V2.Int n, []
| V1.Str s -> | V1.Str s ->
incr counter; (match Env.find_opt s !env with
let lbl = "str" ^ string_of_int !counter in | Some lbl -> V2.Data lbl, [ lbl, Mips.Asciiz s ]
V2.Data lbl, [ lbl, Mips.Asciiz s ] | None ->
incr counter;
let lbl = "str" ^ string_of_int !counter in
env := Env.add s lbl !env;
V2.Data lbl, [ lbl, Mips.Asciiz s ])
in in
let rec ccs_expr = function let rec ccs_expr = function
| IR1.Val v -> | IR1.Val v ->
@ -17,8 +23,8 @@ let collect_constant_strings code =
IR2.Val v2, cs IR2.Val v2, cs
| IR1.Var v -> IR2.Var v, [] | IR1.Var v -> IR2.Var v, []
| IR1.Call (fn, args) -> | IR1.Call (fn, args) ->
let a2 = List.map ccs_expr args in let args2 = List.map ccs_expr args in
IR2.Call (fn, List.map fst a2), List.flatten (List.map snd a2) IR2.Call (fn, List.map fst args2), List.flatten (List.map snd args2)
in in
let ccs_instr = function let ccs_instr = function
| IR1.Decl v -> IR2.Decl v, [] | IR1.Decl v -> IR2.Decl v, []
@ -43,7 +49,11 @@ let collect_constant_strings code =
IR2.Func (name, args, body2), cs IR2.Func (name, args, body2), cs
in in
let code2 = List.map ccs_def code in let code2 = List.map ccs_def code in
List.map fst code2, List.flatten (List.map snd code2) ( List.map fst code2
, List.fold_left
(fun list el -> if List.mem el list then list else el :: list)
[]
(List.flatten (List.map snd code2)) )
;; ;;
let simplify ir = collect_constant_strings ir let simplify ir = collect_constant_strings ir