dimreduce.ml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. open Types
  2. open Util
  3. open Globals
  4. let rec expand_dims = function
  5. (* Flatten Block nodes returned by transformations below *)
  6. | FunDef (export, ret_type, name, params, body, ann) ->
  7. let params = flatten_blocks (List.map expand_dims params) in
  8. FunDef (export, ret_type, name, params, expand_dims body, ann)
  9. | FunDec (ret_type, name, params, ann) ->
  10. let params = flatten_blocks (List.map expand_dims params) in
  11. FunDec (ret_type, name, params, ann)
  12. | FunUse (dec, params, ann) ->
  13. FunUse (dec, flatten_blocks (List.map expand_dims params), ann)
  14. (* Add additional parameters for array dimensions *)
  15. | Param (ArrayDims (ctype, dims), name, ann) ->
  16. let rec do_expand = function
  17. | [] -> [Param (Array ctype, name, ann)]
  18. | Dim (name, ann) :: tail ->
  19. Param (Int, name, ann) :: (do_expand tail)
  20. | _ -> raise InvalidNode
  21. in
  22. Block (do_expand dims)
  23. (* Add additional function arguments for array dimensions *)
  24. | Arg (VarUse (VarDec (ArrayDims (ctype, dims), name, None, decann), None, ann)) ->
  25. let rec do_expand = function
  26. | [] ->
  27. (* Remove the (now obsolete dimensions fromt the type) *)
  28. let dec = VarDec (Array ctype, name, None, decann) in
  29. [VarUse (dec, None, ann)]
  30. | hd :: tl ->
  31. (* A VarDec node has been added for each dimension during
  32. * desugaring, so we can safely reconstruct it here (we need no
  33. * refrence because the type is immutable, yay!) *)
  34. let dimdec = VarDec (Int, nameof hd, None, annof hd) in
  35. Arg (VarUse (dimdec, None, [])) :: (do_expand tl)
  36. in
  37. Block (do_expand dims)
  38. (* Simplify array types in declarations *)
  39. | VarDec (ArrayDims (ctype, _), name, None, ann) ->
  40. VarDec (Array ctype, name, None, ann)
  41. | node -> transform_children expand_dims node
  42. let rec multiply = function
  43. | [] -> raise InvalidNode
  44. | [node] -> node
  45. | hd :: tl -> Binop (Mul, hd, multiply tl, [Type Int])
  46. let rec multiply_all = function
  47. | [] -> raise InvalidNode
  48. | [node] -> node
  49. | hd :: tl -> Binop (Mul, hd, multiply_all tl, [])
  50. let rec expand depth dims =
  51. let rec do_expand dims = function
  52. | [] -> raise InvalidNode
  53. | [node] -> dim_reduce depth node
  54. | i :: j :: tl ->
  55. let parent_width = List.hd dims in
  56. let mul = Binop (Mul, dim_reduce depth i, parent_width, [Type Int]) in
  57. do_expand (List.tl dims) (Binop (Add, mul, j, [Type Int]) :: tl)
  58. in
  59. let use_dim = function
  60. | Dim _ as dim -> VarUse (dim, None, [Type Int; Depth depth])
  61. | node -> node
  62. in
  63. do_expand (List.map use_dim (List.tl dims))
  64. and dim_reduce depth = function
  65. | Allocate (dec, dims, ann) ->
  66. Allocate (dec, [multiply dims], ann)
  67. (* Increase nesting depth when goiing into function *)
  68. | FunDef (export, ret_type, name, params, body, ann) ->
  69. let trav = dim_reduce (depth + 1) in
  70. FunDef (export, ret_type, name, List.map trav params, trav body, ann)
  71. (* Expand indices when dereferencing *)
  72. | VarUse (dec, Some values, ann) as node ->
  73. (match typeof dec with
  74. | ArrayDims (_, dims) ->
  75. VarUse (dec, Some [expand depth dims values], ann)
  76. | _ -> node
  77. )
  78. (* Expand indices when assigning to array index *)
  79. | VarLet (dec, Some values, value, ann) as node ->
  80. (match typeof dec with
  81. | ArrayDims (_, dims) ->
  82. VarLet (dec, Some [expand depth dims values], value, ann)
  83. | _ -> node
  84. )
  85. | node -> transform_children (dim_reduce depth) node
  86. let phase = function
  87. | Ast node -> Ast (dim_reduce 0 (expand_dims node))
  88. | _ -> raise (InvalidInput "dimension reduction")