Sfoglia il codice sorgente

Desugaring now moves splitted initializations to start of function body

Taddeus Kroes 11 anni fa
parent
commit
1386f8af75
1 ha cambiato i file con 16 aggiunte e 11 eliminazioni
  1. 16 11
      phases/desug.ml

+ 16 - 11
phases/desug.ml

@@ -39,7 +39,7 @@ let rec move_scalar_inits = function
     node
 
   (* Add vardec for scalar value *)
-  | VarDec (ArrayDims (ctype, dims) as atype, name, Some value, ann) as node ->
+  | VarDec (ArrayDims (ctype, dims) as atype, name, Some value, ann) ->
     let scalar_dec = VarDec (ctype, fresh_const "scalar", Some value, ann) in
     let scalar_use = VarUse (scalar_dec, None, annof value) in
     Block [scalar_dec; VarDec (atype, name, Some scalar_use, ann)]
@@ -82,7 +82,6 @@ let rec array_init = function
   (* Transform array constant initialisation into separate assign statements
    * for all entries in the array literal *)
   | VarLet (dec, None, (ArrayConst _ as value), ann) ->
-    let name = nameof dec in
     let intconst i = Const (IntVal (Int32.of_int i), []) in
     let ndims = List.length (dimsof dec) in
     let rec make_assigns depth i indices = function
@@ -105,13 +104,6 @@ let rec array_init = function
 
   (* Scalar initialisation *)
   | VarLet (dec, None, scalar, ann) when is_array dec ->
-    let create_loop dim body =
-      let counter = fresh_id "i" in
-      let start = Const (IntVal 0l, []) in
-      let stop = VarUse (dim, None, ann) in
-      let step = Const (IntVal 1l, []) in
-      For (counter, start, stop, step, body, [])
-    in
     let rec nest_loops indices = function
       | [] -> Block [VarLet (dec, Some (List.rev indices), scalar, [])]
       | dim :: tl ->
@@ -172,6 +164,17 @@ let rec move_vardecs = function
 
   | node -> traverse_unit move_vardecs node
 
+let rec move_inits = function
+  | Block (VarDecs decs :: (LocalFuns _ as funs) :: body)->
+    let rec extract_stats decs stats = function
+      | VarDec _ as dec :: tl -> extract_stats (dec :: decs) stats tl
+      | stat :: tl -> extract_stats decs (stat :: stats) tl
+      | [] -> List.rev decs, List.rev stats
+    in
+    let decs, stats = extract_stats [] [] decs in
+    Block (VarDecs decs :: funs :: stats @ body)
+  | node -> traverse_unit move_inits node
+
 let rec move_global_inits = function
   (* Move global initialisations to __init function *)
   | Program (decls, ann) ->
@@ -219,8 +222,7 @@ let phase = function
        * dimension names are consistent with the array name *)
       move_array_dims node |> move_scalar_inits |>
 
-      (* Split variable initialisations into declarations and assignments, and
-       * move the assignments to the function body *)
+      (* Split variable initialisations into declarations and assignments *)
       split_inits |> add_allocs |>
 
       (* Transform ArrayConst assignment to assignments into for-loops *)
@@ -229,6 +231,9 @@ let phase = function
       (* Transform for-loops to while-loops *)
       for_to_while |> split_inits |>
 
+      (* Move initialization assignments to the function body *)
+      move_inits |>
+
       (* Move variable declarations to the beginning of the function *)
       move_vardecs |>