|
@@ -3,6 +3,14 @@ open Types
|
|
|
open Util
|
|
open Util
|
|
|
open Stringify
|
|
open Stringify
|
|
|
|
|
|
|
|
|
|
+let get_scope node = function
|
|
|
|
|
+ | GlobalDec _ -> Extern
|
|
|
|
|
+ | dec ->
|
|
|
|
|
+ match (depthof dec, depthof node) with
|
|
|
|
|
+ | (0, _) -> Glob
|
|
|
|
|
+ | (a, b) when a = b -> Current
|
|
|
|
|
+ | (a, b) -> Rel (b - a)
|
|
|
|
|
+
|
|
|
let comline comment = InlineComment (EmptyLine, comment)
|
|
let comline comment = InlineComment (EmptyLine, comment)
|
|
|
|
|
|
|
|
let assemble program =
|
|
let assemble program =
|
|
@@ -25,6 +33,7 @@ let assemble program =
|
|
|
| _ -> []
|
|
| _ -> []
|
|
|
in
|
|
in
|
|
|
match node with
|
|
match node with
|
|
|
|
|
+
|
|
|
(* Global *)
|
|
(* Global *)
|
|
|
|
|
|
|
|
| Program (decls, _) ->
|
|
| Program (decls, _) ->
|
|
@@ -77,12 +86,7 @@ let assemble program =
|
|
|
(* Statements *)
|
|
(* Statements *)
|
|
|
|
|
|
|
|
| VarLet (dec, None, value, _) ->
|
|
| VarLet (dec, None, value, _) ->
|
|
|
- let store =
|
|
|
|
|
- match (depthof dec, depthof node) with
|
|
|
|
|
- | (0, _) -> Store (typeof dec, Glob, indexof dec)
|
|
|
|
|
- | (a, b) when a = b -> Store (typeof dec, Current, indexof dec)
|
|
|
|
|
- | (a, b) -> Store (typeof dec, Rel (b - a), indexof dec)
|
|
|
|
|
- in
|
|
|
|
|
|
|
+ let store = Store (typeof dec, get_scope node dec, indexof dec) in
|
|
|
trav value @ [InlineComment (store, node2str node)]
|
|
trav value @ [InlineComment (store, node2str node)]
|
|
|
|
|
|
|
|
| Return (value, _) ->
|
|
| Return (value, _) ->
|
|
@@ -233,6 +237,7 @@ let assemble program =
|
|
|
[InlineComment (Label (endlabel), node2str node)]
|
|
[InlineComment (Label (endlabel), node2str node)]
|
|
|
|
|
|
|
|
(* Arrays *)
|
|
(* Arrays *)
|
|
|
|
|
+
|
|
|
| Allocate (dec, [dim], _) ->
|
|
| Allocate (dec, [dim], _) ->
|
|
|
let store =
|
|
let store =
|
|
|
match (depthof dec, depthof node) with
|
|
match (depthof dec, depthof node) with
|
|
@@ -249,27 +254,14 @@ let assemble program =
|
|
|
(should be one-dimensional)")))
|
|
(should be one-dimensional)")))
|
|
|
|
|
|
|
|
| VarUse (dec, Some dims, _) ->
|
|
| VarUse (dec, Some dims, _) ->
|
|
|
- let load =
|
|
|
|
|
- match dec with
|
|
|
|
|
- | GlobalDec (ctype, name, _) ->
|
|
|
|
|
- Load (ctype, Extern, indexof dec)
|
|
|
|
|
- | _ ->
|
|
|
|
|
- match (depthof dec, depthof node) with
|
|
|
|
|
- | (0, _) -> Load (typeof dec, Glob, indexof dec)
|
|
|
|
|
- | (a, b) when a = b -> Load (typeof dec, Current, indexof dec)
|
|
|
|
|
- | (a, b) -> Load (typeof dec, Rel (b - a), indexof dec)
|
|
|
|
|
- in
|
|
|
|
|
|
|
+ let load = Load (typeof dec, get_scope node dec, indexof dec) in
|
|
|
(trav_all (List.rev dims)) @ (* push dimensions *)
|
|
(trav_all (List.rev dims)) @ (* push dimensions *)
|
|
|
[InlineComment (load, nameof dec)] @ (* push array reference *)
|
|
[InlineComment (load, nameof dec)] @ (* push array reference *)
|
|
|
[InlineComment (LoadArray (basetypeof dec), node2str node)]
|
|
[InlineComment (LoadArray (basetypeof dec), node2str node)]
|
|
|
|
|
|
|
|
| VarLet (dec, Some dims, value, _) ->
|
|
| VarLet (dec, Some dims, value, _) ->
|
|
|
- let load =
|
|
|
|
|
- match (depthof dec, depthof node) with
|
|
|
|
|
- | (0, _) -> Load (typeof dec, Glob, indexof dec)
|
|
|
|
|
- | (a, b) when a = b -> Load (typeof dec, Current, indexof dec)
|
|
|
|
|
- | (a, b) -> Load (typeof dec, Rel (b - a), indexof dec)
|
|
|
|
|
- in
|
|
|
|
|
|
|
+ let load = Load (typeof dec, get_scope node dec, indexof dec) in
|
|
|
|
|
+ (trav_all (List.rev dims)) @ (* push dimensions *)
|
|
|
(trav value) @ (* push value *)
|
|
(trav value) @ (* push value *)
|
|
|
(trav_all dims) @ (* push dimensions *)
|
|
(trav_all dims) @ (* push dimensions *)
|
|
|
[InlineComment (load, nameof dec)] @ (* push array reference *)
|
|
[InlineComment (load, nameof dec)] @ (* push array reference *)
|