Skip to content

Commit

Permalink
Only run/interpret/compile individual files
Browse files Browse the repository at this point in the history
this fixes #404 (issue not found).

The command line still accepts multiple files, but just handles then
independently and in sequence. In `pipeline.ml`, there is always only
ever one input file.

No change to generated WebAssembly output.
  • Loading branch information
nomeata committed May 28, 2019
1 parent d1e086b commit e9ee4db
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 131 deletions.
36 changes: 17 additions & 19 deletions src/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5164,6 +5164,10 @@ and compile_prelude env ae =
let (ae1, code) = compile_prog env ae decs in
(ae1, code)

and compile_main_prog env ae prog =
let (_ae, code) = compile_prog env ae prog in
code

(*
This is a horrible hack
When determining whether an actor is closed, we disregard the prelude, because
Expand All @@ -5181,7 +5185,7 @@ and find_prelude_names env =
ASEnv.in_scope_set env2


and compile_start_func mod_env (progs : Ir.prog list) : E.func_with_names =
and compile_start_func mod_env (prog, _flavor) : E.func_with_names =
let find_last_actor (ds1,e) = match ds1, e.it with
| _, ActorE (i, ds2, fs, _) -> Some (i, ds1 @ ds2, fs)
| [], _ -> None
Expand All @@ -5193,22 +5197,16 @@ and compile_start_func mod_env (progs : Ir.prog list) : E.func_with_names =
in

Func.of_body mod_env [] [] (fun env ->
let rec go ae = function
| [] -> G.nop
(* If the last program ends with an actor, then consider this the current actor *)
| [(prog, _flavor)] ->
begin match find_last_actor prog with
| Some (i, ds, fs) -> main_actor env ae i ds fs
| None ->
let (_ae, code) = compile_prog env ae prog in
code
end
| ((prog, _flavor) :: progs) ->
let (ae1, code1) = compile_prog env ae prog in
let code2 = go ae1 progs in
code1 ^^ code2 in
go ASEnv.empty_ae progs
)
let ae0 = ASEnv.empty_ae in
let (ae1, prelude_code) = compile_prelude env ae0 in
match find_last_actor prog with
| Some (i, ds, fs) ->
prelude_code ^^
main_actor env ae1 i ds fs
| None ->
prelude_code ^^
compile_main_prog env ae1 prog
)

and export_actor_field env ae (f : Ir.field) =
let Name name = f.it.name.it in
Expand Down Expand Up @@ -5400,14 +5398,14 @@ and conclude_module env module_name start_fi_o =
| None -> emodule
| Some rts -> LinkModule.link emodule "rts" rts

let compile mode module_name rts (prelude : Ir.prog) (progs : Ir.prog list) : CustomModule.extended_module =
let compile mode module_name rts (prelude : Ir.prog) (prog : Ir.prog) : CustomModule.extended_module =
let env = E.mk_global mode rts prelude Dfinity.trap_with ClosureTable.table_end in

if E.mode env = DfinityMode then Dfinity.system_imports env;
RTS.system_imports env;
RTS.system_exports env;

let start_fun = compile_start_func env (prelude :: progs) in
let start_fun = compile_start_func env prog in
let start_fi = E.add_fun env "start" start_fun in
let start_fi_o =
if E.mode env = DfinityMode
Expand Down
2 changes: 1 addition & 1 deletion src/compile.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type mode = WasmMode | DfinityMode

val compile : mode -> string -> CustomModule.extended_module option -> Ir.prog -> Ir.prog list -> CustomModule.extended_module
val compile : mode -> string -> CustomModule.extended_module option -> Ir.prog -> Ir.prog -> CustomModule.extended_module
13 changes: 5 additions & 8 deletions src/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,16 @@ let declare_import imp_env (f, (prog:Syntax.prog)) =
; note = typ_note
}

let combine_files imp_env libraries progs : Syntax.prog =
let combine_files imp_env libraries prog : Syntax.prog =
(* This is a hack until the backend has explicit support for libraries *)
let open Source in
{ it = List.map (declare_import imp_env) libraries
@ List.concat (List.map (fun p -> p.it) progs)
{ it = List.map (declare_import imp_env) libraries @ prog.Source.it
; at = no_region
; note = match progs with
| [prog] -> prog.Source.note
| _ -> "all"
; note = prog.Source.note
}

let transform p = prog p

let transform_graph imp_env libraries progs =
prog (combine_files imp_env libraries progs)
let transform_graph imp_env libraries p =
prog (combine_files imp_env libraries p)

2 changes: 1 addition & 1 deletion src/desugar.mli
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val transform : Syntax.prog -> Ir.prog
val transform_graph : Typing.lib_env -> Syntax.libraries -> Syntax.prog list -> Ir.prog
val transform_graph : Typing.lib_env -> Syntax.libraries -> Syntax.prog -> Ir.prog
52 changes: 31 additions & 21 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let argspec = Arg.align
[
"-c", Arg.Unit (set_mode Compile), " compile programs to WebAssembly";
"-r", Arg.Unit (set_mode Run), " interpret programs";
"-i", Arg.Unit (set_mode Interact), " run interactive REPL (implies -r)";
"-i", Arg.Unit (set_mode Interact), " load file and run interactive REPL";
"--check", Arg.Unit (set_mode Check), " type-check only";
"-v", Arg.Set Flags.verbose, " verbose output";
"-p", Arg.Set_int Flags.print_depth, " set print depth";
Expand Down Expand Up @@ -65,30 +65,40 @@ let process_files files : unit =
| Default ->
assert false
| Run ->
if !Flags.interpret_ir
then exit_on_none (Pipeline.interpret_ir_files files)
else exit_on_none (Pipeline.run_files files)
let runner =
if !Flags.interpret_ir
then Pipeline.interpret_ir_file
else Pipeline.run_file in
List.iter (fun file -> exit_on_none (runner file)) files
| Interact ->
printf "%s\n" banner;
exit_on_none (Pipeline.run_files_and_stdin files)
let file = match files with
| [] -> None
| [file] -> Some file
| _ -> eprintf "asc: -i cannot load more than one file"; exit 1 in
exit_on_none (Pipeline.run_file_and_stdin file)
| Check ->
Diag.run (Pipeline.check_files files)
List.iter (fun file -> Diag.run (Pipeline.check_file file)) files
| Compile ->
if !out_file = "" then begin
match files with
| [n] -> out_file := Filename.remove_extension (Filename.basename n) ^ ".wasm"
| ns -> eprintf "asc: no output file specified"; exit 1
end;
let module_ = Diag.run Pipeline.(compile_files !compile_mode !(Flags.link) files) in
let oc = open_out !out_file in
let (source_map, wasm) = CustomModuleEncode.encode module_ in
output_string oc wasm; close_out oc;

if !Flags.source_map then begin
let source_map_file = !out_file ^ ".map" in
let oc_ = open_out source_map_file in
output_string oc_ source_map; close_out oc_
end
if files = [] then begin eprintf "asc: no input files specified"; exit 1 end;
if !out_file != "" && List.length files > 1
then begin eprintf "asc: cannot use -o together with multiple input files"; exit 1 end;
List.iter (fun file ->
let out_filename =
if !out_file != ""
then !out_file
else Filename.remove_extension (Filename.basename file) ^ ".wasm" in
let module_ = Diag.run Pipeline.(compile_file !compile_mode !Flags.link file) in
let oc = open_out out_filename in
let (source_map, wasm) = CustomModuleEncode.encode module_ in
output_string oc wasm; close_out oc;

if !Flags.source_map then begin
let source_map_file = out_filename ^ ".map" in
let oc_ = open_out source_map_file in
output_string oc_ source_map; close_out oc_
end
) files

let () =
(*
Expand Down
Loading

0 comments on commit e9ee4db

Please sign in to comment.