Skip to content

Commit

Permalink
Fiddle stuff to support 'any' type better.
Browse files Browse the repository at this point in the history
Some things work and some don't. It's still broken.
  • Loading branch information
skaller committed Dec 29, 2022
1 parent a7ef05a commit 3739b2c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 50 deletions.
35 changes: 6 additions & 29 deletions src/compiler/flx_bind/flx_bind_bexe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,41 +617,17 @@ print_endline ("Flx_bind_bexe: Function return value has MINIMISED type " ^ sbt
if funame="insert_unique'3_mf_3732" then
print_endline ("Flx_bind_bexe: UNIFIYING explicit ret=" ^ sbt bsym_table state.ret_type ^ " and expr ret=" ^ sbt bsym_table t');
*)
begin match t' with
| BTYP_fix (0,_) ->
state.reachable <- false;
[(bexe_fun_return (sr,e))]
| _ ->
let uresult = do_unify state bsym_table state.ret_type t' in
(*
if funame="insert_unique'3_mf_3732" then
print_endline ("Flx_bind_bexe: UNIFICATION DONE, result= " ^ string_of_bool uresult);
*)
let rt = varmap_subst (Flx_lookup_state.get_varmap state.lookup_state) state.ret_type in
let rt = Flx_beta.beta_reduce "flx_bind_bexe: EXE_fun_return" state.counter bsym_table sr rt in
state.ret_type <- rt;
if type_match bsym_table state.counter state.ret_type t' then
(*
if match maybe_matches bsym_table state.counter [state.ret_type, t'] with Some _ -> true | _ -> false then
*)
[(bexe_fun_return (sr,(e',t')))]
(* else if t' = btyp_fix 0 (btyp_type 0) then begin *)
else if t' = btyp_fix 0 (Flx_kind.kind_type) then begin
print_endline "Converting return of 'any' type to procedure call";
state.reachable <- false;
[(bexe_fun_return (sr,(e',state.ret_type)))]
(*
begin match e' with
| BEXPR_apply (f,a) -> [(bexe_jump (sr,f,a))]
| _ ->
clierrx "[flx_bind/flx_bind_bexe.ml:584: E29] " sr
(
"[bind_exe: fun_return ] return expression \n" ^
sbe bsym_table e ^
"\nof type 'any' must be application"
)
end
*)
end
(* coerce type of return value to specified function return type if
they're polymorphic variants, and the return value type
is a subtype of the specified function return type
*)
else begin match unfold "bind_exe_v2" t', unfold "bind_exe_v1" state.ret_type with
| BTYP_variant ts, BTYP_variant rs ->
begin try List.iter (fun (name,t) ->
Expand Down Expand Up @@ -687,6 +663,7 @@ print_endline ("Flx_bind_bexe: UNIFICATION DONE, result= " ^ string_of_bool ures
sbt bsym_table state.ret_type
)
end
end

| EXE_yield e ->
state.return_count <- state.return_count + 1;
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/flx_cpp_backend/flx_cal_type_offsets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ let rec get_offsets' syms bsym_table typ : offset_kind_t list =
let tname = cpp_typename syms bsym_table typ in
let t' = unfold "flx_cal_type_offsets: get_offsets" typ in
match t' with
| BTYP_fix(0,_) -> [] (* type any *)

| BTYP_ellipsis -> assert false
| BBOOL _ -> assert false
| BTYP_typeop _ -> assert false
Expand Down Expand Up @@ -254,7 +256,9 @@ let rec get_offsets' syms bsym_table typ : offset_kind_t list =
| BTYP_type_match _
| BTYP_subtype_match _
| BTYP_type_set_intersection _
| BTYP_type_set_union _ -> assert false
| BTYP_type_set_union _ ->
print_endline ("Attempt to calculate offset of type " ^ Flx_btype.st t');
assert false

let render_offset syms bsym_table new_table s =
match s with
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/flx_cpp_backend/flx_egen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,9 @@ print_endline ("Handling coercion in egen " ^ sbt bsym_table srct ^ " ===> " ^ s


| BTYP_instancetype _,_ -> ce_atom ("reinterpret<"^tn dstt^">("^ge srce^")")

| BTYP_fix(0,_),_ -> ce_atom ( ge srce)

| _ ->
print_endline ("Handling coercion in egen " ^ sbt bsym_table srct ^ " ===> " ^ sbt bsym_table dstt);
print_endline ("Coercion uses reinterpret cast!");
Expand Down
16 changes: 14 additions & 2 deletions src/compiler/flx_cpp_backend/flx_gen_cfunc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ let gen_C_function_body filename syms bsym_table
in
match Flx_bsym.bbdcl bsym with
| BBDCL_val (vs,t,(`Val | `Var | `Once)) when not (List.mem bid params) ->
(bid, rt vs t) :: lst
let t = rt vs t in
begin match t with
| BTYP_void
| BTYP_fix (0,_) -> lst
| _ -> (bid, t) :: lst
end

| BBDCL_val (vs,t,`Ref) when not (List.mem bid params) ->
(bid, btyp_pointer (rt vs t)) :: lst
| _ -> lst
Expand Down Expand Up @@ -301,7 +307,13 @@ let gen_C_procedure_body filename syms bsym_table
in
match Flx_bsym.bbdcl bsym with
| BBDCL_val (vs,t,(`Val | `Var | `Once)) when not (mem bid params) ->
(bid, rt vs t) :: lst
let t = rt vs t in
begin match t with
| BTYP_void
| BTYP_fix (0,_) -> lst
| _ -> (bid, t) :: lst
end

| BBDCL_val (vs,t,`Ref) when not (mem bid params) ->
(bid, btyp_pointer (rt vs t)) :: lst
| _ -> lst
Expand Down
48 changes: 32 additions & 16 deletions src/compiler/flx_cpp_backend/flx_gen_exe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
(* HACK WARNING! *)
begin match t with
| BTYP_fix (0,_) -> " "^ge sr e^"; // non-returning\n"
| _ -> " return "^ge sr e^";\n"
| _ -> " return "^ge sr e^"; // "^tn t^ "\n"
end

| BEXE_nop (_,s) -> " //Nop: " ^ s ^ "\n"
Expand Down Expand Up @@ -861,30 +861,45 @@ print_endline ("BEXE_INIT, RHS type = " ^ Flx_btype.st t);
(*
print_endline ("BEXE_INIT, RHS type after tsub = " ^ Flx_btype.st t);
*)

(* if the RHS is a variable, the side effect has already happend I hope, sp
it is safe to elide the assignment, since the variable carries no
information anyhow
*)
begin match e with
| BEXPR_varname _,BTYP_fix (0,_) ->
" // elide assignment of variable of type any to LHS\n"

| BEXPR_coerce ( (BEXPR_varname _,BTYP_fix (0,_)),_),_ ->
" // elide assignment of coerced variable of type any to LHS\n"

| _ ->
begin match t with
| BTYP_tuple [] -> ""

| BTYP_void -> assert false
| BTYP_fix (0,_) ->
(* NOTE: this MAY NOT WORK.
It WILL work if the RHS is a C binding.
If it's a 'function' or 'procedure' which is reduced to a C style
Felix function it should also work.
If it's a Felix function .. the apply() should work.
If it's a procedure it will NOT work, a procedure
has to be 'called' by a micro scheduler ...
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"

| _ ->
let bsym =
try Flx_bsym_table.find bsym_table v with Not_found ->
failwith ("[gen_exe] can't find index " ^ string_of_bid v)
in
begin match Flx_bsym.bbdcl bsym with
| BBDCL_val (vs,vt,kind) ->
(*
print_endline ("gen_exe: " ^ string_of_bexe bsym_table 0 exe);
print_endline ("init " ^ Flx_bsym.id bsym ^"< instno="^si instance_no^",this="^ si this^ ">:\nLHS type = "^
sbt bsym_table vt^ "\nRHS type = " ^ sbt bsym_table t ^ "\nLHS ts = " ^ catmap "," (sbt bsym_table) ts);
*)

(*
print_endline ("Trying to generate initialiser " ^ sbe bsym_table e ^ " type = " ^ Flx_btype.st t ^ " =? " ^ Flx_btype.st (snd e));
let initialiser = ge sr e in
print_endline ("Got initialiser");
let ref_ref = get_ref_ref syms bsym_table this v ts in
print_endline ("Got ref_ref");
let vtn = get_variable_typename syms bsym_table v [] in
print_endline ("Got variable type name");
*)

(if with_comments then " //"^src_str^"\n" else "") ^
" " ^
Expand All @@ -900,6 +915,7 @@ print_endline ("init " ^ Flx_bsym.id bsym ^"< instno="^si instance_no^",this="^
| _ -> assert false
end
end
end

| BEXE_begin -> " {\n"
| BEXE_end -> " }\n"
Expand Down
11 changes: 9 additions & 2 deletions src/compiler/flx_cpp_backend/flx_gen_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ let find_variable_indices syms bsym_table index =
let children = Flx_bsym_table.find_children bsym_table index in
BidSet.fold begin fun bid bids ->
try match Flx_bsym_table.find_bbdcl bsym_table bid with
| BBDCL_val (_,_,(`Val | `Var | `Ref | `Once)) -> bid :: bids
| BBDCL_val (_,t,(`Val | `Var | `Ref | `Once)) ->
begin match t with
| BTYP_void
| BTYP_fix (0,_) -> bids
| _ -> bid :: bids
end
| _ -> bids
with Not_found -> bids
with Not_found ->
assert false
(* bids *)
end children []

(*
Expand Down

0 comments on commit 3739b2c

Please sign in to comment.