diff --git a/compiler/obc/c/c.mli b/compiler/obc/c/c.mli index cee66f0..5e9a817 100644 --- a/compiler/obc/c/c.mli +++ b/compiler/obc/c/c.mli @@ -25,6 +25,7 @@ type cty = | Cty_ptr of cty (** C points-to-other-type type. *) | Cty_arr of int * cty (** A static array of the specified size. *) | Cty_void (** Well, [void] is not really a C type. *) + | Cty_future of cty (** async result as a future *) (** A C block: declarations and statements. In source code form, it begins with variable declarations before a list of semicolon-separated statements, the @@ -48,6 +49,7 @@ and cexpr = | Cstructlit of string * cexpr list (** Structure literal " \{f1, f2, ... \}". *) | Carraylit of cexpr list (** Array literal [e1, e2, ...]. *) + | Cmethod_call of cexpr * string * cexpr list (** Object member function call with parameters. *) and cconst = | Ccint of int (** Integer constant. *) | Ccfloat of float (** Floating-point number constant. *) diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index 20ede8c..9c68c4c 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -103,7 +103,7 @@ let rec ctype_of_otype oty = ctype_of_otype ty) | Tprod _ -> assert false | Tunit -> assert false - | Tasync _ -> assert false (* TODO async *) + | Tasync (a,ty) -> Cty_future (ctype_of_otype ty) let cvarlist_of_ovarlist vl = let cvar_of_ovar vd = @@ -289,8 +289,8 @@ let rec cexpr_of_exp var_env exp = Cstructlit (ctyn, cexps) | Earray e_list -> Carraylit (cexprs_of_exps var_env e_list) - | Ebang _ -> - (* TODO async *) assert false + | Ebang e -> + Cmethod_call (cexpr_of_exp var_env e, "get", []) and cexprs_of_exps var_env exps = List.map (cexpr_of_exp var_env) exps @@ -309,7 +309,7 @@ and cop_of_op_aux op_name cexps = match op_name with Cbop (copname op, el, er) | _ -> Cfun_call(op, cexps) end - | {qual = m; name = op} -> Cfun_call(op,cexps) (*TODO m should be used?*) + | {qual = m; name = op} -> Cfun_call(op,cexps) and cop_of_op var_env op_name exps = let cexps = cexprs_of_exps var_env exps in @@ -474,6 +474,19 @@ let rec cstm_of_act var_env obj_env act = [Cfor(name x, int_of_static_exp i1, int_of_static_exp i2, cstm_of_act_list var_env obj_env act)] + (** Special case for x = 0^n^n...*) + | Aassgn (vn, { e_desc = Econst c }) -> + let vn = clhs_of_lhs var_env vn in + create_affect_const var_env vn c + + (** Purely syntactic translation from an Obc local variable to a C + local one, with recursive translation of the rhs expression. *) + | Aassgn (vn, e) -> + let vn = clhs_of_lhs var_env vn in + let ty = assoc_type_lhs vn var_env in + let ce = cexpr_of_exp var_env e in + create_affect_stm vn ce ty + (** Reinitialization of an object variable, extracting the reset function's name from our environment [obj_env]. *) | Acall (name_list, o, Mreset, args) -> @@ -494,20 +507,17 @@ let rec cstm_of_act var_env obj_env act = [Csexpr (Cfun_call (classn ^ "_reset", elt ))] )] ) - | Aasync_call _ -> assert false (* TODO async *) - - (** Special case for x = 0^n^n...*) - | Aassgn (vn, { e_desc = Econst c }) -> - let vn = clhs_of_lhs var_env vn in - create_affect_const var_env vn c - - (** Purely syntactic translation from an Obc local variable to a C - local one, with recursive translation of the rhs expression. *) - | Aassgn (vn, e) -> - let vn = clhs_of_lhs var_env vn in - let ty = assoc_type_lhs vn var_env in - let ce = cexpr_of_exp var_env e in - create_affect_stm vn ce ty + | Aasync_call (a, name_list, o, Mreset, args) -> + assert_empty name_list; + assert_empty args; + let on = obj_ref_name o in + let obj = assoc_obj on obj_env in + let classn = cname_of_qn obj.o_class in + (match obj.o_size with + | None -> + [Csexpr (Cfun_call (classn ^ "_reset", [Caddrof (Cfield (Cderef (Cvar "self"), local_qn on))] ))] + | _ -> assert false (* TODO array async *) + ) (** Step functions applications can return multiple values, so we use a local structure to hold the results, before allocating to our @@ -517,6 +527,8 @@ let rec cstm_of_act var_env obj_env act = let outvl = clhss_of_lhss var_env outvl in generate_function_call var_env obj_env outvl objn args + | Aasync_call _ -> assert false (* TODO async *) + and cstm_of_act_list var_env obj_env b = let l = List.map cvar_of_vd b.b_locals in diff --git a/compiler/obc/c/csubst.ml b/compiler/obc/c/csubst.ml index 71829ab..4ac69af 100644 --- a/compiler/obc/c/csubst.ml +++ b/compiler/obc/c/csubst.ml @@ -41,6 +41,7 @@ and subst_exp map = function | Caddrof lhs -> Caddrof (subst_lhs map lhs) | Cstructlit (s, el) -> Cstructlit (s, subst_exp_list map el) | Carraylit el -> Carraylit (subst_exp_list map el) + | Cmethod_call _ -> (*TODO async*) assert false and subst_exp_list map = List.map (subst_exp map)