Skip to content

Commit

Permalink
Cleanup: pass visibility info down to scalc
Browse files Browse the repository at this point in the history
  • Loading branch information
AltGr committed Sep 23, 2024
1 parent ea0ab3c commit a20bdc8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
5 changes: 3 additions & 2 deletions compiler/scalc/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ type scope_body = {
scope_body_name : ScopeName.t;
scope_body_var : FuncName.t;
scope_body_func : func;
scope_body_visibility : visibility;
}

type code_item =
| SVar of { var : VarName.t; expr : expr; typ : typ }
| SFunc of { var : FuncName.t; func : func }
| SVar of { var : VarName.t; expr : expr; typ : typ; visibility : visibility }
| SFunc of { var : FuncName.t; func : func; visibility : visibility }
| SScope of scope_body

type ctx = { decl_ctx : decl_ctx; modules : VarName.t ModuleName.Map.t }
Expand Down
12 changes: 9 additions & 3 deletions compiler/scalc/from_lcalc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,10 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
func_return_typ =
TStruct body.scope_body_output_struct, input_pos;
};
scope_body_visibility = body.scope_body_visibility;
}
:: rev_items )
| Topdef (name, topdef_ty, _vis, (EAbs abs, m)) ->
| Topdef (name, topdef_ty, visibility, (EAbs abs, m)) ->
(* Toplevel function def *)
let (block, expr, _ren_ctx_inner), args_id =
let args_a, expr, ctxt_inner = unmbind ctxt abs.binder in
Expand Down Expand Up @@ -657,9 +658,10 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
| TAny, pos_any -> TAny, pos_any
| _ -> failwith "should not happen");
};
visibility;
}
:: rev_items )
| Topdef (name, topdef_ty, _vis, expr) ->
| Topdef (name, topdef_ty, visibility, expr) ->
(* Toplevel constant def *)
let block, expr, _ren_ctx_inner =
let ctxt = { ctxt with context_name = TopdefName.base name } in
Expand All @@ -673,7 +675,9 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
statements, we lift its computation into an auxiliary function *)
let rev_items, ctxt =
if (block :> (A.stmt * Pos.t) list) = [] then
A.SVar { var = var_id; expr; typ = topdef_ty } :: rev_items, ctxt
( A.SVar { var = var_id; expr; typ = topdef_ty; visibility }
:: rev_items,
ctxt )
else
let pos = Mark.get expr in
let func_name, ctxt =
Expand All @@ -687,6 +691,7 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
var = var_id;
expr = A.EApp { f = EFunc func_id, pos; args = [] }, pos;
typ = topdef_ty;
visibility;
}
:: A.SFunc
{
Expand All @@ -699,6 +704,7 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
~tail:[A.SReturn expr, Mark.get expr];
A.func_return_typ = topdef_ty;
};
visibility = Private;
}
:: rev_items,
ctxt )
Expand Down
4 changes: 2 additions & 2 deletions compiler/scalc/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ let format_item decl_ctx ?debug ppf def =
Print.keyword ppf "let ";
let () =
match def with
| SVar { var; expr; typ = _ } ->
| SVar { var; expr; typ = _; visibility = _ } ->
format_var_name ppf var;
Print.punctuation ppf " =";
Format.pp_close_box ppf ();
Format.pp_print_space ppf ();
format_expr decl_ctx ?debug ppf expr
| SScope { scope_body_var = var; scope_body_func = func; _ }
| SFunc { var; func } ->
| SFunc { var; func; visibility = _ } ->
format_func_name ppf var;
Format.pp_print_list
(fun ppf (arg, ty) ->
Expand Down
29 changes: 12 additions & 17 deletions compiler/scalc/to_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ let format_main (fmt : Format.formatter) (p : Ast.program) =
scope_body_func = { func_params = [(_, (TStruct ts, _))]; _ };
scope_body_var = var;
scope_body_name = name;
scope_body_visibility = Public;
} ->
let input_struct =
StructName.Map.find ts p.ctx.decl_ctx.ctx_structs
Expand Down Expand Up @@ -750,18 +751,12 @@ let format_program
List.fold_left
(fun env code_item ->
match code_item with
| SVar { var; expr; typ } ->
| SVar { var; expr; typ; visibility } ->
(* Global variables are turned into inline functions without
parameters that perform lazy evaluation: {[ inline foo_type foo() {
static foo_type foo = NULL; return (foo ? foo : foo = foo_init());
} ]} NOTE: "inline" is not defined in C89 *)
let public =
(* TODO: Ugh! Pass this info into scalc ! *)
Re.(
execp
(compile (seq [str "__"; diff any digit]))
(VarName.to_string var))
in
let public = visibility = Public in
ppboth_if public (fun ppf ->
Format.fprintf ppf "@,@[<v 2>@[<hov 4>%s%a"
(if public then "" else "static")
Expand All @@ -782,20 +777,20 @@ let format_program
expr;
Format.fprintf ppc "@;<1 -2>}@]@,";
{ env with global_vars = VarName.Set.add var env.global_vars }
| SFunc { var; func }
| SScope { scope_body_var = var; scope_body_func = func; _ } ->
| SFunc { var; func; visibility }
| SScope
{
scope_body_var = var;
scope_body_func = func;
scope_body_visibility = visibility;
_;
} ->
let { func_params; func_body; func_return_typ } = func in
let local_vars =
VarName.Set.of_list
(List.map (fun (v, _) -> Mark.remove v) func_params)
in
let public =
(* TODO: Ugh! Pass this info into scalc ! *)
Re.(
execp
(compile (seq [str "__"; diff any digit]))
(FuncName.to_string var))
in
let public = visibility = Public in
ppboth_if public (fun ppf ->
Format.fprintf ppf "@,@[<v 2>@[<hov 4>%s%a@ @[<hv 1>(%a)@]@]"
(if public then "" else "static ")
Expand Down
4 changes: 2 additions & 2 deletions compiler/scalc/to_python.ml
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,10 @@ let format_ctx (type_ordering : TypeIdent.t list) (fmt : Format.formatter) ctx :
(type_ordering @ scope_structs)

let format_code_item ctx fmt = function
| SVar { var; expr; typ = _ } ->
| SVar { var; expr; typ = _; visibility = _ } ->
Format.fprintf fmt "@[<hv 4>%a = (@,%a@;<0 -4>)@]@," VarName.format var
(format_expression ctx) expr
| SFunc { var; func }
| SFunc { var; func; visibility = _ }
| SScope { scope_body_var = var; scope_body_func = func; _ } ->
let { Ast.func_params; Ast.func_body; _ } = func in
Format.fprintf fmt "@[<v 4>@[<hov 2>def %a(@,%a@;<0 -2>):@]@ %a@]@,"
Expand Down

0 comments on commit a20bdc8

Please sign in to comment.