Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate flags from codegen into as_config #692

Merged
merged 2 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/as_config/flags.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
(* common flags for the asc compiler *)

type compile_mode = WasmMode | AncientMode | ICMode

let trace = ref false
let verbose = ref false
let print_depth = ref 2
let release_mode = ref false
let compile_mode = ref ICMode
let multi_value = ref false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was a feature that flags that only affect one module/library should be there. But consistency is the something something hobgoblin. So not a big deal either way.

let await_lowering = ref true
let async_lowering = ref true
let dump_parse = ref false
Expand Down
92 changes: 45 additions & 47 deletions src/codegen/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ this keeps documentation close to the code (a lesson learned from Simon PJ).
open Ir_def
open As_values
open As_types
open As_config

open Wasm.Ast
open Wasm.Types
Expand Down Expand Up @@ -106,9 +107,6 @@ The fields fall into the following categories:

(* Before we can define the environment, we need some auxillary types *)

type mode = WasmMode | AncientMode | ICMode


module E = struct

(* Utilities, internal to E *)
Expand Down Expand Up @@ -136,7 +134,7 @@ module E = struct
type t = {
(* Global fields *)
(* Static *)
mode : mode;
mode : Flags.compile_mode;
prelude : Ir.prog; (* The prelude. Re-used when compiling actors *)
rts : Wasm_exts.CustomModule.extended_module option; (* The rts. Re-used when compiling actors *)
trap_with : t -> string -> G.t;
Expand Down Expand Up @@ -233,7 +231,7 @@ module E = struct
ignore (reg env.exports e)

let add_dfinity_type (env : t) e =
assert (mode env = AncientMode);
assert (mode env = Flags.AncientMode);
ignore (reg env.dfinity_types e)

let add_global (env : t) name g =
Expand Down Expand Up @@ -678,14 +676,14 @@ module RTS = struct
let get_len = G.i (LocalGet (nr 1l)) in
get_str ^^ get_len ^^
match E.mode env with
| AncientMode ->
| Flags.AncientMode ->
E.call_import env "data" "externalize" ^^
E.call_import env "test" "print" ^^
G.i Unreachable
| ICMode ->
| Flags.ICMode ->
E.call_import env "debug" "print" ^^
G.i Unreachable
| WasmMode -> G.i Unreachable
| Flags.WasmMode -> G.i Unreachable
)
) in
E.add_export env (nr {
Expand Down Expand Up @@ -718,7 +716,7 @@ module Heap = struct
(* Page allocation. Ensures that the memory up to the given unskewed pointer is allocated. *)
let grow_memory env =
(* No growing of memory on drun yet *)
if E.mode env = ICMode
if E.mode env = Flags.ICMode
then G.i Drop
else Func.share_code1 env "grow_memory" ("ptr", I32Type) [] (fun env get_ptr ->
let (set_pages_needed, get_pages_needed) = new_local env "pages_needed" in
Expand Down Expand Up @@ -2970,14 +2968,14 @@ module Dfinity = struct

let system_imports env =
match E.mode env with
| ICMode ->
| Flags.ICMode ->
E.add_func_import env "debug" "print" [I32Type; I32Type] [];
E.add_func_import env "msg" "arg_data_size" [I64Type] [I32Type];
E.add_func_import env "msg" "arg_data_copy" [I64Type; I32Type; I32Type; I32Type] [];
E.add_func_import env "msg" "reply" [I64Type; I32Type; I32Type] [];
E.add_func_import env "msg" "reject" [I64Type; I32Type] [];
E.add_func_import env "msg" "error_code" [I64Type] [I32Type]
| AncientMode ->
| Flags.AncientMode ->
E.add_func_import env "test" "print" [I32Type] [];
E.add_func_import env "test" "show_i32" [I32Type] [I32Type];
E.add_func_import env "data" "externalize" [I32Type; I32Type] [I32Type];
Expand Down Expand Up @@ -3016,14 +3014,14 @@ module Dfinity = struct

let prim_print env =
match E.mode env with
| WasmMode -> G.i Drop
| ICMode ->
| Flags.WasmMode -> G.i Drop
| Flags.ICMode ->
Func.share_code1 env "print_text" ("str", I32Type) [] (fun env get_str ->
get_str ^^ Text.payload_ptr_unskewed ^^
get_str ^^ Heap.load_field (Text.len_field) ^^
system_call env "debug" "print"
)
| AncientMode ->
| Flags.AncientMode ->
compile_databuf_of_text env ^^
system_call env "test" "print"

Expand All @@ -3037,7 +3035,7 @@ module Dfinity = struct
compile_static_print env "\n"

let trap_with env s =
if E.mode env = WasmMode
if E.mode env = Flags.WasmMode
then G.i Unreachable
else compile_static_print env (s ^ "\n") ^^ G.i Unreachable

Expand All @@ -3054,7 +3052,7 @@ module Dfinity = struct
})

let export_start_stub env =
assert (E.mode env = AncientMode);
assert (E.mode env = Flags.AncientMode);
let empty_f = Func.of_body env [] [] (fun env1 ->
(* Set up memory *)
G.i (Call (nr (E.built_in env1 "restore_mem"))) ^^
Expand All @@ -3071,7 +3069,7 @@ module Dfinity = struct
E.add_dfinity_type env (fi, [])

let export_start env start_fi =
assert (E.mode env = ICMode);
assert (E.mode env = Flags.ICMode);
(* Create an empty message *)
let empty_f = Func.of_body env ["api_nonce",I64Type] [] (fun env1 ->
G.i (Call (nr start_fi)) ^^
Expand Down Expand Up @@ -3141,7 +3139,7 @@ module OrthogonalPersistence = struct
*)

let register_globals env =
assert (E.mode env = AncientMode);
assert (E.mode env = Flags.AncientMode);
(* We want to put all persistent globals first:
The index in the persist annotation refers to the index in the
list of *exported* globals, not all globals (at least with v8/dvm) *)
Expand All @@ -3153,7 +3151,7 @@ module OrthogonalPersistence = struct
E.persist env (E.get_global env "elemstore") Wasm_exts.CustomModule.ElemBuf

let register env start_funid =
assert (E.mode env = AncientMode);
assert (E.mode env = Flags.AncientMode);
let mem_global = E.get_global env "datastore" in
let elem_global = E.get_global env "elemstore" in

Expand Down Expand Up @@ -3213,11 +3211,11 @@ module OrthogonalPersistence = struct
)

let save_mem env =
assert (E.mode env = AncientMode);
assert (E.mode env = Flags.AncientMode);
G.i (Call (nr (E.built_in env "save_mem")))

let restore_mem env =
assert (E.mode env = AncientMode);
assert (E.mode env = Flags.AncientMode);
G.i (Call (nr (E.built_in env "restore_mem")))

end (* OrthogonalPersistence *)
Expand Down Expand Up @@ -4038,9 +4036,9 @@ module Serialization = struct
let serialize env ts : G.t =
let ts_name = String.concat "," (List.map typ_id ts) in
let name = "@serialize<" ^ ts_name ^ ">" in
if E.mode env = ICMode then assert (List.for_all has_no_references ts);
if E.mode env = Flags.ICMode then assert (List.for_all has_no_references ts);
(* On ancient API returns databuf/elembuf, on new API returns nothing *)
let ret_tys = if E.mode env = ICMode then [] else [I32Type; I32Type] in
let ret_tys = if E.mode env = Flags.ICMode then [] else [I32Type; I32Type] in
Func.share_code1 env name ("x", I32Type) ret_tys (fun env get_x ->
let (set_data_size, get_data_size) = new_local env "data_size" in
let (set_refs_size, get_refs_size) = new_local env "refs_size" in
Expand Down Expand Up @@ -4085,15 +4083,15 @@ module Serialization = struct
E.else_trap_with env "data buffer not filled " ^^

match E.mode env with
| AncientMode ->
| Flags.AncientMode ->
(* Store it all in a databuf and an elembuf *)
get_data_start ^^ get_data_size ^^
Dfinity.system_call env "data" "externalize" ^^

get_refs_start ^^ get_refs_size ^^
Dfinity.system_call env "elem" "externalize"

| ICMode ->
| Flags.ICMode ->
get_refs_size ^^
compile_unboxed_const 0l ^^
G.i (Compare (Wasm.Values.I32 I32Op.Eq)) ^^
Expand All @@ -4102,22 +4100,22 @@ module Serialization = struct
(* Copy out the bytes *)
reply_with_data env get_data_start get_data_size

| WasmMode -> assert false
| Flags.WasmMode -> assert false
)

let deserialize env ts =
if E.mode env = ICMode then assert (List.for_all has_no_references ts);
if E.mode env = Flags.ICMode then assert (List.for_all has_no_references ts);
let ts_name = String.concat "," (List.map typ_id ts) in
let name = "@deserialize<" ^ ts_name ^ ">" in
let args = if E.mode env = ICMode then [] else [("databuf",I32Type);("elembuf", I32Type)] in
let args = if E.mode env = Flags.ICMode then [] else [("databuf",I32Type);("elembuf", I32Type)] in
Func.share_code env name args (List.map (fun _ -> I32Type) ts) (fun env ->
let (set_data_size, get_data_size) = new_local env "data_size" in
let (set_refs_size, get_refs_size) = new_local env "refs_size" in
let (set_data_start, get_data_start) = new_local env "data_start" in
let (set_refs_start, get_refs_start) = new_local env "refs_start" in

begin match E.mode env with
| AncientMode ->
| Flags.AncientMode ->
let get_databuf = G.i (LocalGet (nr 0l)) in
let get_elembuf = G.i (LocalGet (nr 1l)) in

Expand All @@ -4132,7 +4130,7 @@ module Serialization = struct
get_refs_size ^^ compile_mul_const Heap.word_size ^^ Text.dyn_alloc_scratch env ^^ set_refs_start ^^
get_refs_start ^^ get_refs_size ^^ get_elembuf ^^ compile_unboxed_const 0l ^^
Dfinity.system_call env "elem" "internalize"
| ICMode ->
| Flags.ICMode ->
(* Allocate space for the data buffer and copy it *)
argument_data_size env ^^ set_data_size ^^
get_data_size ^^ Text.dyn_alloc_scratch env ^^ set_data_start ^^
Expand Down Expand Up @@ -4762,7 +4760,7 @@ module FuncDec = struct
- Fake orthogonal persistence
*)
let compile_message outer_env outer_ae cc restore_env args mk_body at =
assert (E.mode outer_env = AncientMode);
assert (E.mode outer_env = Flags.AncientMode);
assert (cc.Call_conv.n_res = 0);
let ae0 = ASEnv.mk_fun_ae outer_ae in
Func.of_body outer_env ["clos", I32Type; "databuf", I32Type; "elembuf", I32Type] [] (fun env -> G.with_region at (
Expand Down Expand Up @@ -4800,7 +4798,7 @@ module FuncDec = struct

let compile_static_message outer_env outer_ae cc args mk_body ret_tys at : E.func_with_names =
match E.mode outer_env with
| AncientMode ->
| Flags.AncientMode ->
assert (cc.Call_conv.n_res = 0);
let ae0 = ASEnv.mk_fun_ae outer_ae in
Func.of_body outer_env ["databuf", I32Type; "elembuf", I32Type] [] (fun env -> G.with_region at (
Expand All @@ -4825,7 +4823,7 @@ module FuncDec = struct
(* Save memory *)
OrthogonalPersistence.save_mem env
))
| ICMode ->
| Flags.ICMode ->
let ae0 = ASEnv.mk_fun_ae outer_ae in
Func.of_body outer_env ["api_nonce", I64Type] [] (fun env -> G.with_region at (
G.i (LocalGet (nr 0l)) ^^ Dfinity.set_api_nonce env ^^
Expand All @@ -4841,10 +4839,10 @@ module FuncDec = struct
(* Collect garbage *)
G.i (Call (nr (E.built_in env "collect")))
))
| WasmMode -> assert false
| Flags.WasmMode -> assert false

let declare_dfinity_type env has_closure fi =
if E.mode env = AncientMode then
if E.mode env = Flags.AncientMode then
E.add_dfinity_type env (fi,
(if has_closure then [ Wasm_exts.CustomModule.I32 ] else []) @
[ Wasm_exts.CustomModule.DataBuf; Wasm_exts.CustomModule.ElemBuf ]
Expand Down Expand Up @@ -6167,17 +6165,17 @@ and compile_exp (env : E.t) ae exp =
Error.compile_make_error (compile_exp_vanilla env ae e1) (compile_exp_vanilla env ae e2)

| ICReplyPrim t, [e] ->
assert (E.mode env = ICMode);
assert (E.mode env = Flags.ICMode);
SR.unit,
compile_exp_vanilla env ae e ^^
Serialization.serialize env [t]

| ICRejectPrim, [e] ->
assert (E.mode env = ICMode);
assert (E.mode env = Flags.ICMode);
Dfinity.reject env (compile_exp_vanilla env ae e)

| ICErrorCodePrim, [] ->
assert (E.mode env = ICMode);
assert (E.mode env = Flags.ICMode);
Dfinity.error_code env

(* Unknown prim *)
Expand Down Expand Up @@ -6724,8 +6722,8 @@ and export_actor_field env ae (f : Ir.field) =
FuncDec.declare_dfinity_type env false fi;
E.add_export env (nr {
name = Wasm.Utf8.decode (match E.mode env with
| AncientMode -> f.it.name
| ICMode -> "canister_update " ^ f.it.name
| Flags.AncientMode -> f.it.name
| Flags.ICMode -> "canister_update " ^ f.it.name
| _ -> assert false);
edesc = nr (FuncExport (nr fi))
})
Expand All @@ -6740,7 +6738,7 @@ and actor_lit outer_env this ds fs at =
(E.get_trap_with outer_env)
ClosureTable.table_end in

if E.mode mod_env = AncientMode then OrthogonalPersistence.register_globals mod_env;
if E.mode mod_env = Flags.AncientMode then OrthogonalPersistence.register_globals mod_env;
Heap.register_globals mod_env;
ElemHeap.register_globals mod_env;
Stack.register_globals mod_env;
Expand Down Expand Up @@ -6769,8 +6767,8 @@ and actor_lit outer_env this ds fs at =
prelude_code ^^ decls_code) in
let start_fi = E.add_fun mod_env "start" start_fun in

if E.mode mod_env = ICMode then Dfinity.export_start mod_env start_fi;
if E.mode mod_env = AncientMode then OrthogonalPersistence.register mod_env start_fi;
if E.mode mod_env = Flags.ICMode then Dfinity.export_start mod_env start_fi;
if E.mode mod_env = Flags.AncientMode then OrthogonalPersistence.register mod_env start_fi;

let m = conclude_module mod_env this None in
let (_map, wasm_binary) = Wasm_exts.CustomModuleEncode.encode m in
Expand Down Expand Up @@ -6876,7 +6874,7 @@ and conclude_module env module_name start_fi_o =
let compile mode module_name rts (prelude : Ir.prog) (progs : Ir.prog list) : Wasm_exts.CustomModule.extended_module =
let env = E.mk_global mode rts prelude Dfinity.trap_with ClosureTable.table_end in

if E.mode env = AncientMode then OrthogonalPersistence.register_globals env;
if E.mode env = Flags.AncientMode then OrthogonalPersistence.register_globals env;
Heap.register_globals env;
ElemHeap.register_globals env;
Stack.register_globals env;
Expand All @@ -6889,11 +6887,11 @@ let compile mode module_name rts (prelude : Ir.prog) (progs : Ir.prog list) : Wa
let start_fun = compile_start_func env (prelude :: progs) in
let start_fi = E.add_fun env "start" start_fun in
let start_fi_o = match E.mode env with
| AncientMode ->
| Flags.AncientMode ->
OrthogonalPersistence.register env start_fi;
Dfinity.export_start_stub env;
None
| ICMode -> Dfinity.export_start env start_fi; None
| WasmMode -> Some (nr start_fi) in
| Flags.ICMode -> Dfinity.export_start env start_fi; None
| Flags.WasmMode -> Some (nr start_fi) in

conclude_module env module_name start_fi_o
4 changes: 1 addition & 3 deletions src/codegen/compile.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open Ir_def

type mode = WasmMode | AncientMode | ICMode

val compile : mode -> string -> Wasm_exts.CustomModule.extended_module option -> Ir.prog -> Ir.prog list -> Wasm_exts.CustomModule.extended_module
val compile : As_config.Flags.compile_mode -> string -> Wasm_exts.CustomModule.extended_module option -> Ir.prog -> Ir.prog list -> Wasm_exts.CustomModule.extended_module
2 changes: 1 addition & 1 deletion src/codegen/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name codegen)
(libraries wasm lib lang_utils wasm_exts ir_def linking)
(libraries wasm lib as_config lang_utils wasm_exts ir_def linking)
)
1 change: 0 additions & 1 deletion src/codegen/flags.ml

This file was deleted.

Loading