Skip to content

Commit

Permalink
Get "functions" returning "any" to work in more cases.
Browse files Browse the repository at this point in the history
They can be used in expressions AND called. They must be
non-returning.
  • Loading branch information
skaller committed Dec 29, 2022
1 parent 9a96192 commit afce9c6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/compiler/flx_bind/flx_cal_ret_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ print_endline "$$$$$Flx_cal_ret: about to unify calculated and registered return
print_endline ("$$$$Return type = " ^ Flx_btype.st !ret_type);
print_endline ("$$$$Return expression type = " ^ Flx_btype.st t);
*)
match t with
| BTYP_fix(0,_) -> () (* doesn't contribute to unification *)
| _ ->
if pvtype then
() (* use the declared return type, let the coercion be inserted later *)
else
Expand Down
19 changes: 17 additions & 2 deletions src/compiler/flx_cpp_backend/flx_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ let gen_procedure_methods filename syms bsym_table
);
let cxx_name = cid_of_flxid (Flx_bsym.id bsym) in
match Flx_bsym.bbdcl bsym with
| BBDCL_fun (props,vs,bps,BTYP_fix (0,_),effects,exes)
| BBDCL_fun (props,vs,bps,BTYP_void,effects,exes) ->
if length ts <> length vs then
failwith
Expand Down Expand Up @@ -482,7 +481,6 @@ let gen_execute_methods filename syms bsym_table
let cxx_name = cid_of_flxid (Flx_bsym.id bsym) in

begin match Flx_bsym.bbdcl bsym with
| BBDCL_fun (props,vs,(ps,traint),BTYP_fix (0,_),effects,_)
| BBDCL_fun (props,vs,(ps,traint),BTYP_void,effects,_) ->
bcat s ("//------------------------------\n");
if mem `Cfun props || mem `Pure props && not (mem `Heap_closure props) then
Expand All @@ -498,6 +496,23 @@ let gen_execute_methods filename syms bsym_table
bcat s call;
bcat s2 ctor

| BBDCL_fun (props,vs,(ps,traint),BTYP_fix(0,_),effects,_) ->
(* print_endline ("GENERATING ESCAPE METHODS for " ^ bsym.id); *)
bcat s ("//--------- ESCAPE FUNCTION "^bsym.id ^" ---------------------\n");
if mem `Cfun props || mem `Pure props && not (mem `Heap_closure props) then
bcat s (
Flx_gen_cfunc.gen_C_function_body filename syms bsym_table shapes shape_table
label_info counter index ts (Flx_bsym.sr bsym) instance_no
)
else
let apply,ctor =
gen_function_methods filename syms bsym_table shapes shape_table
label_info counter index ts instance_no
in
bcat s2 ctor;
bcat s apply


| BBDCL_fun (props,vs,(ps,traint),ret,effects,_) ->
bcat s ("//------------------------------\n");
if mem `Cfun props || mem `Pure props && not (mem `Heap_closure props) then
Expand Down
1 change: 0 additions & 1 deletion src/compiler/flx_cpp_backend/flx_gen_cfunc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ let gen_C_procedure_body filename syms bsym_table
)
);
match Flx_bsym.bbdcl bsym with
| BBDCL_fun (props,vs,bps,BTYP_fix (0,_),effects,exes)
| BBDCL_fun (props,vs,bps,BTYP_void,effects,exes) ->
let requires_ptf = mem `Requires_ptf props in
if length ts <> length vs then
Expand Down
24 changes: 19 additions & 5 deletions src/compiler/flx_cpp_backend/flx_gen_exe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
let our_display = get_display_list bsym_table this in
let caller_name = Flx_bsym.id bsym in
let kind = match Flx_bsym.bbdcl bsym with
| BBDCL_fun (_,_,_,BTYP_fix (0,_),_,_) -> Procedure
| BBDCL_fun (_,_,_,BTYP_void,_,_) -> Procedure
| BBDCL_fun (_,_,_,_,_,_) -> Function
| _ -> failwith "Expected executable code to be in function or procedure"
Expand Down Expand Up @@ -157,9 +156,9 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
otherwise pass 'this' as the caller 'return address'
EXCEPT that stack calls don't pass a return address at all
*)
let this = match kind with
let this = if is_ehandler then "" else match kind with
| Function ->
if is_jump && not is_ehandler
if is_jump
then
clierrx "[flx_cpp_backend/flx_gen_exe.ml:167: E300] "
sr ("[gen_exe] can't jump inside function " ^ caller_name ^" to " ^ called_name ^
Expand Down Expand Up @@ -234,6 +233,14 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
" }\n"

| Procedure ->
if is_ehandler then begin
let _,argt = a in
" {\n" ^
subs ^
" (FLX_NEWP(" ^ name ^ ")" ^ Flx_gen_display.strd the_display props ^ ")" ^
"\n ->apply (" ^ args ^ ");\n" ^
" }\n"
end else
let call_string =
" return (FLX_NEWP(" ^ name ^ ")" ^ Flx_gen_display.strd the_display props ^ ")" ^
"\n ->call(" ^ args ^ ");\n"
Expand Down Expand Up @@ -593,7 +600,6 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
| _ -> assert false
in
begin match Flx_bsym.bbdcl bsym with
| BBDCL_fun (props,vs,ps,BTYP_fix (0,_),effects,_)
| BBDCL_fun (props,vs,ps,BTYP_void,effects,_) ->
assert (mem `Stack_closure props);
let a = match a with (a,t) -> a, tsub t in
Expand Down Expand Up @@ -756,6 +762,8 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
begin let _,t = p in match t with
| BTYP_cfunction _ ->
" "^ge sr p ^ "("^ge sr a^");\n"
| BTYP_function (_, BTYP_fix (0,_)) ->
" " ^ ge sr p ^ "->apply(" ^ ge sr a ^ ");//tail call ESCAPE FUN (BEXE_jump)\n"
| _ ->
(if with_comments then
" //"^ src_str ^ "\n"
Expand Down Expand Up @@ -832,6 +840,9 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
| BEXPR_coerce ( (BEXPR_varname _,BTYP_fix (0,_)),_),_ ->
" // elide return of coerced variable of type any\n"

| BEXPR_coerce ( (_,BTYP_fix (0,_)) as x,_),_ ->
" "^ge sr x^"; // return ESCAPE non-returning\n"

| _ ->

let _,t = e in
Expand Down Expand Up @@ -884,6 +895,9 @@ print_endline ("BEXE_INIT, RHS type after tsub = " ^ Flx_btype.st t);
| BEXPR_coerce ( (BEXPR_varname _,BTYP_fix (0,_)),_),_ ->
" // elide assignment of coerced variable of type any to LHS\n"

| BEXPR_coerce ( (_,BTYP_fix(0,_)) as x,_),_ ->
" " ^ ge sr x ^ "; //init or assign expr coerced from type 'any' replaced by evaluation\n"

| _ ->
begin match t with
| BTYP_tuple [] -> ""
Expand All @@ -902,7 +916,7 @@ print_endline ("BEXE_INIT, RHS type after tsub = " ^ Flx_btype.st t);
So we need to treat the assignment as if it were a call to the RHS ..
because it actually is.
*)
ge sr e ^ "; //init or assign type 'any' replaced by evaluation\n"
" " ^ ge sr e ^ "; //init or assign type 'any' replaced by evaluation\n"

| _ ->
let bsym =
Expand Down
1 change: 0 additions & 1 deletion src/compiler/flx_cpp_backend/flx_gen_func.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ let gen_function syms bsym_table props index id sr vs bps ret' ts instance_no =
in
let members = find_members syms bsym_table index ts in
match ret with
| BTYP_fix (0,_)
| BTYP_void ->
let name = cpp_instance_name syms bsym_table index ts in
let ctor = ctor_dcl name in
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/flx_cpp_backend/flx_gen_functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ let gen_functions syms bsym_table (shapes: Flx_set.StringSet.t ref) shape_table
in
match Flx_bsym.bbdcl bsym with
| BBDCL_fun (props,vs,ps,ret,effects,_) ->
let is_proc = match ret with | BTYP_void | BTYP_fix (0,_) -> true | _ -> false in
let name = if is_proc then "PROCEDURE" else "FUNCTION" in
let is_proc = match ret with | BTYP_void -> true | _ -> false in
let is_escape = match ret with | BTYP_fix(0,_) -> true | _ -> false in
let name = (if is_escape then "ESCAPE " else "") ^ (if is_proc then "PROCEDURE" else "FUNCTION") in
bcat s ("\n//------------------------------\n");
let ft = btyp_effector (Flx_bparams.get_btype ps,effects,ret) in
if mem `Cfun props || mem `Pure props && not (mem `Heap_closure props) then begin
Expand Down
24 changes: 22 additions & 2 deletions src/compiler/flx_cpp_backend/flx_tgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,7 @@ let rec gen_type syms bsym_table (index,typ) =
"typedef " ^ string_of_cdecl_type name cdt ^ ";\n"


| BTYP_linearfunction (a,BTYP_fix (0,_))
| BTYP_linearfunction (a,BTYP_void)
| BTYP_function (a,BTYP_fix (0,_))
| BTYP_function (a,BTYP_void) ->
descr ^
let name = cn typ
Expand All @@ -413,6 +411,28 @@ let rec gen_type syms bsym_table (index,typ) =
" virtual ::flx::rtl::con_t *resume()=0;\n" ^
"};\n"

(* ESCAPE FUNCTION *)
| BTYP_linearfunction (a,BTYP_fix (0,_))
| BTYP_function (a,BTYP_fix (0,_)) ->
(* print_endline ("Generating ESCAPE FUNCTION TYPE"); *)
"\n// ------ ESCAPE FUNCTION TYPE ------ "^
descr ^
let name = cn typ
and argtype = tn a
and rettype = "void"
and unitfun = a = btyp_tuple [] || a = btyp_void ()
in
"struct " ^ name ^ " {\n" ^
" typedef " ^ rettype ^ " rettype;\n" ^
" typedef " ^ (if unitfun then "void" else argtype) ^ " argtype;\n" ^
" virtual "^rettype^" apply("^
(if unitfun then "" else argtype^" const &") ^
")=0;\n" ^
" virtual "^name^" *clone()=0;\n" ^
" virtual ~"^name^"(){};\n" ^
"};\n"


(* FUNCTION *)
| BTYP_linearfunction (a,r)
| BTYP_function (a,r) ->
Expand Down

0 comments on commit afce9c6

Please sign in to comment.