Skip to content

Commit

Permalink
replaced compileFlags with noPervasives and runtimeMode
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko committed Apr 25, 2023
1 parent 0a92558 commit 1e476ff
Show file tree
Hide file tree
Showing 60 changed files with 398 additions and 301 deletions.
4 changes: 0 additions & 4 deletions cli/bin/grain.js
Expand Up @@ -107,10 +107,6 @@ class GrainCommand extends commander.Command {
);
cmd.forwardOption("--import-memory", "import the memory from `env.memory`");
cmd.option("--dir <dir...>", "directory to preopen");
cmd.forwardOption(
"--compilation-mode <mode>",
"compilation mode (advanced use only)"
);
cmd.forwardOption(
"--elide-type-info",
"don't include runtime type information used by toString/print"
Expand Down
33 changes: 5 additions & 28 deletions compiler/src/compile.re
Expand Up @@ -89,32 +89,6 @@ let log_state = state =>
prerr_string("\n\n");
};

let apply_inline_flags = (prog: Parsetree.parsed_program) => {
switch (prog.compile_flags) {
| Some(flags) =>
Grain_utils.Config.apply_inline_flags(
~on_error=
err => {
switch (err) {
| `Help =>
raise(
InlineFlagsError(prog.prog_loc, Cannot_use_help_or_version),
)
| `Message(msg) =>
raise(
InlineFlagsError(
prog.prog_loc,
Cannot_parse_inline_flags(msg),
),
)
}
},
List.map(flag => flag.Location.txt, flags),
)
| None => ()
};
};

let next_state = (~is_root_file=false, {cstate_desc, cstate_filename} as cs) => {
let cstate_desc =
switch (cstate_desc) {
Expand Down Expand Up @@ -153,7 +127,10 @@ let next_state = (~is_root_file=false, {cstate_desc, cstate_filename} as cs) =>
cleanup();
Parsed(parsed);
| Parsed(p) =>
apply_inline_flags(p);
Grain_utils.Config.apply_attribute_flags(
~no_pervasives=p.compile_flags.no_pervasives,
~runtime_mode=p.compile_flags.runtime_mode,
);
if (is_root_file) {
Grain_utils.Config.set_root_config();
};
Expand Down Expand Up @@ -274,7 +251,7 @@ let compile_wasi_polyfill = () => {
switch (Grain_utils.Config.wasi_polyfill^) {
| Some(file) =>
Grain_utils.Config.preserve_config(() => {
Grain_utils.Config.compilation_mode := Some("runtime");
Grain_utils.Config.runtime_mode := true;
let cstate = {
cstate_desc: Initial(InputFile(file)),
cstate_filename: Some(file),
Expand Down
22 changes: 18 additions & 4 deletions compiler/src/formatting/format.re
Expand Up @@ -5233,11 +5233,25 @@ let format_ast =
~location=parsed_program.module_name.loc,
parsed_program.comments,
);
let attrs =
(
if (parsed_program.compile_flags.no_pervasives) {
[(Location.mknoloc("noPervasives"), [])];
} else {
[];
}
)
@ (
if (parsed_program.compile_flags.runtime_mode) {
[(Location.mknoloc("runtimeMode"), [])];
} else {
[];
}
);
let compile_flags_attr =
switch (parsed_program.compile_flags) {
| Some(flags) =>
print_attributes([(Location.mknoloc("compileFlags"), flags)])
| None => Doc.nil
switch (attrs) {
| [] => Doc.nil
| attrs => print_attributes(attrs)
};

let module_header =
Expand Down
15 changes: 6 additions & 9 deletions compiler/src/parsing/driver.re
Expand Up @@ -158,15 +158,12 @@ let read_imports = (program: Parsetree.parsed_program) => {
| Grain_utils.Config.Gc_mod => Location.mknoloc("runtime/gc")
}
},
switch (program.compile_flags) {
| Some(flags) =>
Grain_utils.Config.with_inline_flags(
~on_error=_ => (),
List.map(flag => flag.txt, flags),
Grain_utils.Config.get_implicit_opens,
)
| None => Grain_utils.Config.get_implicit_opens()
},
Grain_utils.Config.with_attribute_flags(
~on_error=_ => (),
~no_pervasives=program.compile_flags.no_pervasives,
~runtime_mode=program.compile_flags.runtime_mode,
Grain_utils.Config.get_implicit_opens,
),
);
let found_includes = ref([]);

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/parsing/parser.mly
Expand Up @@ -710,5 +710,5 @@ module_header:
| MODULE UIDENT { mkstr $loc($2) $2 }

program:
| opt_eols attribute? module_header eos toplevel_stmts EOF { make_program $2 $3 $5 }
| opt_eols attribute? module_header eos? EOF { make_program $2 $3 [] }
| opt_eols attributes module_header eos toplevel_stmts EOF { make_program $2 $3 $5 }
| opt_eols attributes module_header eos? EOF { make_program $2 $3 [] }
37 changes: 23 additions & 14 deletions compiler/src/parsing/parser_header.re
Expand Up @@ -107,26 +107,35 @@ let make_module_alias = ident => {
};
};

let make_program = (module_attr, module_name, statements) => {
let make_program = (module_attrs, module_name, statements) => {
let prog_loc = {
loc_start: first_loc^.loc_end,
loc_end: last_loc^.loc_end,
loc_ghost: false,
};
let compile_flags =
switch (module_attr) {
| Some(({txt: "compileFlags"}, flags)) => Some(flags)
| Some(({loc}, _)) =>
raise(
SyntaxError(
loc,
"@compileFlags is the only valid top-level module attribute.",
),
)
| None => None
};
let (no_pervasives, runtime_mode) =
List.fold_left(
((no_pervasives, runtime_mode), attr) => {
switch (attr) {
| ({txt: "noPervasives"}, []) => (true, runtime_mode)
| ({txt: "runtimeMode"}, []) => (no_pervasives, true)
| ({loc}, _) =>
raise(
SyntaxError(
loc,
"@noPervasives and @runtimeMode are the only valid top-level module attributes.",
),
)
}
},
(false, false),
module_attrs,
);
fix_blocks({
compile_flags,
compile_flags: {
no_pervasives,
runtime_mode,
},
module_name,
statements,
comments: [],
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/parsing/parsetree.re
Expand Up @@ -658,9 +658,15 @@ type comment =

/** The type for parsed programs */

[@deriving (sexp, yojson)]
type compile_flags = {
no_pervasives: bool,
runtime_mode: bool,
};

[@deriving (sexp, yojson)]
type parsed_program = {
compile_flags: option(list(loc(string))),
compile_flags,
module_name: loc(string),
statements: list(toplevel_stmt),
comments: list(comment),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/typed/typed_well_formedness.re
Expand Up @@ -299,7 +299,7 @@ module WellFormednessArg: TypedtreeIter.IteratorArgument = {
if (exp_is_wasm_unsafe(exp)
&& !(
Grain_utils.Config.no_gc^
|| Grain_utils.Config.compilation_mode^ == Some("runtime")
|| Grain_utils.Config.runtime_mode^
|| is_unsafe()
)) {
raise(Error(exp_loc, WasmOutsideDisableGc));
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/typed/typemod.re
Expand Up @@ -999,12 +999,12 @@ let initial_env = () => {
);
};

let get_compilation_mode = () => {
switch (Grain_utils.Config.compilation_mode^) {
| Some("runtime") => Env.Runtime
| _ => Env.Normal
let get_compilation_mode = () =>
if (Grain_utils.Config.runtime_mode^) {
Env.Runtime;
} else {
Env.Normal;
};
};

let type_implementation = prog => {
let sourcefile = prog.prog_loc.loc_start.pos_fname;
Expand Down
84 changes: 15 additions & 69 deletions compiler/src/utils/config.re
Expand Up @@ -321,54 +321,6 @@ let with_cli_options = (term: 'a): Cmdliner.Term.t('a) => {
folded;
};

let with_unapplied_cli_options = (term: 'a): Cmdliner.Term.t('a) => {
open Cmdliner;
open Term;
let process_option = acc =>
fun
| Spec(arg, names, box) => const((a, b) => {b}) $ Arg.value(arg) $ acc;
let folded = List.fold_left(process_option, const(term), specs^);
folded;
};

let process_used_cli_options = term => {
open Cmdliner;
open Term;
let process_option = acc =>
fun
| Spec(arg, names, box) => {
const((a, (_, used), b) => {
if (List.fold_left(
(acc, name) =>
acc
|| List.mem("--" ++ name, used)
|| List.mem("-" ++ name, used),
false,
names,
)) {
box := a;
};
b;
})
$ Arg.value(arg)
$ with_used_args(term)
$ acc;
};
let folded = List.fold_left(process_option, term, specs^);
folded;
};

let apply_inline_flags = (~err, flags) => {
open Cmdliner;
let cmd =
Cmd.v(
Cmd.info("grainc"),
process_used_cli_options(with_unapplied_cli_options(Term.const())),
);
let argv = Array.of_list(["", ...flags]);
Cmd.eval_value(~argv, ~err, cmd);
};

let option_conv = ((prsr, prntr)) => (
x =>
switch (prsr(x)) {
Expand Down Expand Up @@ -446,15 +398,11 @@ let import_memory =
false,
);

let compilation_mode =
opt(
let runtime_mode =
toggle_flag(
~names=["compilation-mode"],
~conv=
option_conv(
Cmdliner.Arg.enum([("normal", "normal"), ("runtime", "runtime")]),
),
~doc="Compilation mode (advanced use only)",
None,
false,
);

let statically_link =
Expand Down Expand Up @@ -573,23 +521,21 @@ let module_search_path = () => {
};
};

let apply_inline_flags = (~on_error, flags) => {
let err_buf = Buffer.create(80);
let err = Format.formatter_of_buffer(err_buf);
let result = apply_inline_flags(~err, flags);
switch (result) {
| Ok(`Ok(_)) => ()
| Ok(`Version)
| Ok(`Help) => on_error(`Help)
| Error(_) =>
Format.pp_print_flush(err, ());
on_error(`Message(Buffer.contents(err_buf)));
let apply_attribute_flags = (~no_pervasives as np, ~runtime_mode as rm) => {
// Only apply options if attributes were explicitly given so as to not
// unintentionally override options set previously e.g. compiling a
// wasi-polyfill file in non-runtime-mode if @runtimeMode is not specified
if (np) {
no_pervasives := true;
};
if (rm) {
runtime_mode := true;
};
};

let with_inline_flags = (~on_error, flags, thunk) => {
let with_attribute_flags = (~on_error, ~no_pervasives, ~runtime_mode, thunk) => {
preserve_config(() => {
apply_inline_flags(~on_error, flags);
apply_attribute_flags(~no_pervasives, ~runtime_mode);
thunk();
});
};
Expand All @@ -605,7 +551,7 @@ let get_implicit_opens = () => {
} else {
[Pervasives_mod];
};
if (compilation_mode^ == Some("runtime")) {
if (runtime_mode^) {
[];
} else {
// Pervasives goes first, just for good measure.
Expand Down
14 changes: 7 additions & 7 deletions compiler/src/utils/config.rei
Expand Up @@ -78,9 +78,9 @@ let maximum_memory_pages: ref(option(int));

let import_memory: ref(bool);

/** Compilation mode to use when compiling */
/** Whether this module should be compiled in runtime mode */

let compilation_mode: ref(option(string));
let runtime_mode: ref(bool);

/** Statically link modules after compilation */

Expand Down Expand Up @@ -174,15 +174,15 @@ let preserve_all_configs: (unit => 'a) => 'a;

let with_cli_options: 'a => Cmdliner.Term.t('a);

/** Applies compile flags provided at the start of a file */
/** Applies compile flags provided as module attributes */

let apply_inline_flags:
(~on_error: [> | `Help | `Message(string)] => unit, list(string)) => unit;
let apply_attribute_flags: (~no_pervasives: bool, ~runtime_mode: bool) => unit;

let with_inline_flags:
let with_attribute_flags:
(
~on_error: [> | `Help | `Message(string)] => unit,
list(string),
~no_pervasives: bool,
~runtime_mode: bool,
unit => 'a
) =>
'a;
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/grainfmt/lets.expected.gr
@@ -1,4 +1,4 @@
@compileFlags("--no-pervasives")
@noPervasives @runtimeMode
module Lets

let rec myFun1 = x =>
Expand Down
5 changes: 2 additions & 3 deletions compiler/test/grainfmt/lets.input.gr
@@ -1,6 +1,5 @@
@compileFlags(
"--no-pervasives"
)
@noPervasives @runtimeMode

module Lets

let rec myFun1 = x => x + 1 and myFun2 = x => x + 1
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/input/mallocTight.gr
@@ -1,4 +1,4 @@
@compileFlags("--compilation-mode=runtime")
@runtimeMode
module MallocTight

include "runtime/malloc"
Expand Down
1 change: 0 additions & 1 deletion compiler/test/input/memoryBase/asserts.gr
@@ -1,4 +1,3 @@
@compileFlags("--memory-base", "0x110000")
module Asserts

include "runtime/unsafe/wasmi32"
Expand Down

0 comments on commit 1e476ff

Please sign in to comment.