فهرست منبع

Started implementing VM changes of extern variables

Taddeus Kroes 11 سال پیش
والد
کامیت
ec7babc046
9فایلهای تغییر یافته به همراه57 افزوده شده و 40 حذف شده
  1. BIN
      bin32/civas
  2. BIN
      bin32/civvm
  3. 0 2
      main.ml
  4. 18 6
      phases/assemble.ml
  5. 11 7
      phases/index.ml
  6. 16 9
      phases/print.ml
  7. 0 6
      phases/typecheck.ml
  8. 0 2
      phases/typecheck.mli
  9. 12 8
      types.mli

BIN
bin32/civas


BIN
bin32/civvm


+ 0 - 2
main.ml

@@ -21,8 +21,6 @@ let phases = [
    "Context analysis");
   ("typecheck", Typecheck.phase, always,
    "Type checking");
-  ("extern", Extern.phase, always,
-   "Create getters and setters for extern variables");
   ("dimreduce", Dimreduce.phase, always,
    "Array dimension reduction");
   ("boolop", Boolop.phase, always,

+ 18 - 6
phases/assemble.ml

@@ -30,20 +30,28 @@ let assemble program =
     | Program (decls, _) ->
       trav_all decls
 
-    | GlobalDef (_, ctype, name, _, _) ->
+    | GlobalDef (false, ctype, name, _, _) ->
       [Comment (sprintf "global var \"%s\" at index %d" name (indexof node));
        Global ctype]
 
+    | GlobalDef (true, ctype, name, _, _) ->
+      [Comment (sprintf "exported var \"%s\" at index %d" name (indexof node));
+       Global ctype;
+       ExportVar (name, indexof node)]
+
+    | GlobalDec (ctype, name, _) ->
+      [Comment (sprintf "imported var \"%s\" at index %d" name (indexof node));
+       ImportVar (name, ctype)]
+
     | FunDec (ret_type, name, params, _) ->
-      [Comment (sprintf "extern fun \"%s\" at index %d" name (indexof node));
-       Import (name, ret_type, List.map typeof params)]
+      [Comment (sprintf "imported fun \"%s\" at index %d" name (indexof node));
+       ImportFun (name, ret_type, List.map typeof params)]
 
     | FunDef (export, ret_type, name, params, body, _) ->
       let label = labelof node in
       begin
-        if export then
-          let param_types = List.map typeof params in
-          [Export (name, ret_type, param_types, label)]
+        if export
+        then [ExportFun (name, ret_type, List.map typeof params, label)]
         else []
       end @
       [Comment (sprintf "function \"%s\" with %d parameters and %d local vars"
@@ -146,6 +154,10 @@ let assemble program =
       let load = LoadConst (typeof node, indexof node) in
       [InlineComment (load, node2str node)]
 
+    | VarUse (GlobalDec (ctype, _, _) as dec, None, _) ->
+      let load = Load (ctype, Extern, indexof dec) in
+      [InlineComment (load, node2str node)]
+
     | VarUse (dec, None, _) ->
       let load = match (depthof dec, depthof node) with
       | (0, _)            -> Load (typeof dec, Glob,        indexof dec)

+ 11 - 7
phases/index.ml

@@ -1,10 +1,13 @@
 open Types
 open Util
 
+let ret_incr r = let cur = !r in incr r; cur
+
 (* Tag declarations with stack frame indices *)
 let tag_index program =
   let nglobs = ref 0 in
-  let nimport = ref 0 in
+  let nimportvar = ref 0 in
+  let nimportfun = ref 0 in
   let consts = Hashtbl.create 32 in
   let rec trav_localfuns trav = function
     | LocalFuns body -> LocalFuns (List.map trav body)
@@ -14,8 +17,7 @@ let tag_index program =
     let trav = tag stacklen callstack in
     match node with
     | GlobalDef _ ->
-      let index = !nglobs in
-      nglobs := !nglobs + 1;
+      let index = ret_incr nglobs in
       annotate (Index index) (traverse_unit trav node)
 
     | FunDef (export, rtype, name, params, body, ann) ->
@@ -40,13 +42,15 @@ let tag_index program =
     | LocalFuns _ -> node
 
     | VarDec _ | Param _ | Dim _ ->
-      let index = !stacklen in
-      stacklen := !stacklen + 1;
+      let index = ret_incr stacklen in
       annotate (Index index) (traverse_unit trav node)
 
+    | GlobalDec (_, name, _) ->
+      let index = ret_incr nimportvar in
+      annotate (Index index) node
+
     | FunDec (_, name, _, _) ->
-      let index = !nimport in
-      nimport := !nimport + 1;
+      let index = ret_incr nimportfun in
       annotate (LabelName name) (annotate (Index index) node)
 
     | Const (value, _) when not (is_immediate_const value) ->

+ 16 - 9
phases/print.ml

@@ -21,20 +21,23 @@ let op2str = function
   | Le  -> "le"
   | Gt  -> "gt"
   | Ge  -> "ge"
-  | _ -> raise (FatalError (Msg "operator unsupported by VM"))
+  | _ -> raise (FatalError (Msg "unsupported operator"))
 
 let prefix = function
+  | Void    -> ""
   | Bool _  -> "b"
   | Int _   -> "i"
   | Float _ -> "f"
-  | Void    -> ""
-  | _       -> "a"
+  | Array _ -> "a"
+  | _ -> raise (FatalError (Msg "invalid type"))
+
 
 let suffix = function
-  | Glob        -> "g"
-  | Current     -> ""
   | Local       -> "l"
   | Rel nesting -> "n " ^ si nesting
+  | Glob        -> "g"
+  | Extern      -> "e"
+  | Current     -> ""
 
 let rtn_suffix = function
   | ExternFun index    -> "e " ^ si index
@@ -51,12 +54,16 @@ let rec instr2str = function
       instr2str instr
   | Label name ->
     name ^ ":"
-  | Export (name, ret_type, arg_types, label) ->
+  | ExportVar (name, index) ->
+    ".exportvar \"" ^ name ^ "\" " ^ si index
+  | ExportFun (name, ret_type, arg_types, label) ->
     let types = List.map type2str (ret_type :: arg_types) in
-    ".export \"" ^ name ^ "\" " ^ (String.concat " " types) ^ " " ^ label
-  | Import (name, ret_type, arg_types) ->
+    ".exportfun \"" ^ name ^ "\" " ^ (String.concat " " types) ^ " " ^ label
+  | ImportVar (name, ctype) ->
+    ".importvar \"" ^ name ^ "\" " ^ type2str ctype
+  | ImportFun (name, ret_type, arg_types) ->
     let types = List.map type2str (ret_type :: arg_types) in
-    ".import \"" ^ name ^ "\" " ^ (String.concat " " types)
+    ".importfun \"" ^ name ^ "\" " ^ (String.concat " " types)
   | Global ctype ->
     ".global " ^ (type2str ctype)
   | ConstDef value ->

+ 0 - 6
phases/typecheck.ml

@@ -228,12 +228,6 @@ let rec typecheck node =
   | Const (FloatVal value, ann) ->
     (Const (FloatVal value, Type Float :: ann), [])
 
-  (* Extern arrays variables are transformed to imported functions, so the
-   * pointer cannot be passed *)
-  | VarUse (GlobalDec (ArrayDims _, _, _), None, _) ->
-    add_error node "imported array pointers may only be dereferenced, not used \
-                    directly"
-
   (* Variables inherit the type of their declaration *)
   | VarUse (dec, None, ann) ->
     (VarUse (dec, None, Type (typeof dec) :: ann), [])

+ 0 - 2
phases/typecheck.mli

@@ -18,8 +18,6 @@
     - The predicate expression of an if, while, or do-while statement must be
       a boolean.
     - Only values of a basic type can be type cast.
-    - Imported array pointers may not be used directly. E.g., [a] in
-      [extern int[n] a] may be used as [a[0]], but not as [foo(a)].
     *)
 
 (** Main phase function, called by {!Main}. *)

+ 12 - 8
types.mli

@@ -144,8 +144,8 @@ and node =
 
 (** {2 Assembly instructions} *)
 
-(** Stack scopes. Correspond to [(load|store|isr)[gln ]] respectively. *)
-type stack_scope = Glob | Local | Rel of int | Current
+(** Stack scopes. Correspond to [(load|store|isr)[lnge ]] respectively. *)
+type stack_scope = Local | Rel of int | Glob | Extern | Current
 
 (** Function scopes. *)
 type rtn_scope = ExternFun of int | LocalFun of int * string
@@ -157,18 +157,22 @@ type instr =
   | Label of string                     (** [<label>:] *)
 
   (* Directives *)
-  | Export of string * ctype * ctype list * string
-    (** [.export "<name>" <ret_type> [<arg_type_1> ...] <label>] *)
-  | Import of string * ctype * ctype list
-    (** [.import "<name>" <ret_type> [<arg_type_1> ...]] *)
+  | ExportVar of string * int
+    (** [.exportvar "<name>" <index>] *)
+  | ExportFun of string * ctype * ctype list * string
+    (** [.exportfun "<name>" <ret_type> [<arg_type_1> ...]] <label> *)
+  | ImportVar of string * ctype
+    (** [.importvar "<name>" <type> *)
+  | ImportFun of string * ctype * ctype list
+    (** [.importfun "<name>" <ret_type> [<arg_type_1> ...]] *)
   | ConstDef of const
     (** [.const <value>] *)
   | Global of ctype
     (** [.global <type>] *)
 
-  | Store of ctype * stack_scope * int  (** [[ifba]store[ gn]] *)
+  | Store of ctype * stack_scope * int  (** [[ifba]store[ nge]] *)
 
-  | Load of ctype * stack_scope * int   (** [[ifb]load[ gn] G] *)
+  | Load of ctype * stack_scope * int   (** [[ifb]load[ nge] G] *)
   | LoadConst of ctype * int            (** [[ifb]loadc C] *)
   | LoadImm of const                    (** [[ifb]loadc_[01tf] | iloadc_m1] *)