diff options
author | sjakie | 1999-11-23 12:03:07 +0000 |
---|---|---|
committer | sjakie | 1999-11-23 12:03:07 +0000 |
commit | 05b79443ca056cc97053521b327c522ac7e046d9 (patch) | |
tree | 6d11d8c451601423185b5ee5360aced06ff4c149 | |
parent | added code for dealing with dynamics (diff) |
change: dynamics are now converted before 'fusion'
git-svn-id: https://svn.cs.ru.nl/repos/clean-compiler/trunk@58 1f8540f1-abd5-4d5b-9d24-4c5ce8603e2d
-rw-r--r-- | frontend/convertDynamics.dcl | 8 | ||||
-rw-r--r-- | frontend/convertDynamics.icl | 234 | ||||
-rw-r--r-- | frontend/convertcases.dcl | 10 | ||||
-rw-r--r-- | frontend/convertcases.icl | 173 | ||||
-rw-r--r-- | frontend/syntax.dcl | 2 | ||||
-rw-r--r-- | frontend/syntax.icl | 6 | ||||
-rw-r--r-- | frontend/trans.dcl | 5 | ||||
-rw-r--r-- | frontend/trans.icl | 44 |
8 files changed, 293 insertions, 189 deletions
diff --git a/frontend/convertDynamics.dcl b/frontend/convertDynamics.dcl index 45c8d35..0a0e38d 100644 --- a/frontend/convertDynamics.dcl +++ b/frontend/convertDynamics.dcl @@ -2,6 +2,12 @@ definition module convertDynamics import syntax, transform, convertcases + +convertDynamicPatternsIntoUnifyAppls :: {! GlobalTCType} !{# CommonDefs} !*{! Group} !*{#FunDef} !*PredefinedSymbols !*VarHeap !*TypeHeaps !*ExpressionHeap + -> (!*{! Group}, !*{#FunDef}, !*PredefinedSymbols, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) + +/* convertDynamicPatternsIntoUnifyAppls :: {! GlobalTCType} !{# CommonDefs} !*{! Group} !*{#FunDef} !*PredefinedSymbols - !*{#{# CheckedTypeDef}} !ImportedFunctions !*VarHeap !*TypeHeaps !*ExpressionHeap + !*{#{# CheckedTypeDef}} !ImportedConstructors !*VarHeap !*TypeHeaps !*ExpressionHeap -> (!*{! Group}, !*{#FunDef}, !*PredefinedSymbols, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) +*/
\ No newline at end of file diff --git a/frontend/convertDynamics.icl b/frontend/convertDynamics.icl index d046d72..32ef5ec 100644 --- a/frontend/convertDynamics.icl +++ b/frontend/convertDynamics.icl @@ -6,6 +6,7 @@ import syntax, transform, utilities, convertcases { ci_predef_symb :: !*PredefinedSymbols , ci_var_heap :: !*VarHeap , ci_expr_heap :: !*ExpressionHeap + , ci_new_variables :: ![FreeVar] , ci_new_functions :: ![FunctionInfoPtr] , ci_fun_heap :: !*FunctionHeap , ci_next_fun_nr :: !Index @@ -22,25 +23,22 @@ import syntax, transform, utilities, convertcases } :: DefaultExpression :== Optional (BoundVar, [IndirectionVar]) //DefaultRecord -:: DefaultRecord = - { c_i :: BoundVar - , indirections :: [IndirectionVar] - } -:: BoundVariables :== [(FreeVar, AType)] + +:: BoundVariables :== [TypedVariable] + :: IndirectionVar :== BoundVar -convertDynamicPatternsIntoUnifyAppls :: {! GlobalTCType} !{# CommonDefs} !*{! Group} !*{#FunDef} !*PredefinedSymbols - !*{#{# CheckedTypeDef}} !ImportedFunctions !*VarHeap !*TypeHeaps !*ExpressionHeap +convertDynamicPatternsIntoUnifyAppls :: {! GlobalTCType} !{# CommonDefs} !*{! Group} !*{#FunDef} !*PredefinedSymbols !*VarHeap !*TypeHeaps !*ExpressionHeap -> (!*{! Group}, !*{#FunDef}, !*PredefinedSymbols, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) -convertDynamicPatternsIntoUnifyAppls global_type_instances common_defs groups fun_defs predefined_symbols - imported_types imported_conses var_heap type_heaps expr_heap +convertDynamicPatternsIntoUnifyAppls global_type_instances common_defs groups fun_defs predefined_symbols var_heap type_heaps expr_heap #! nr_of_funs = size fun_defs + # imported_types = {com_type_defs \\ {com_type_defs} <-: common_defs } # (groups, (fun_defs, {ci_predef_symb, ci_var_heap, ci_expr_heap, ci_fun_heap, ci_new_functions})) = convert_groups 0 groups global_type_instances (fun_defs, { ci_predef_symb = predefined_symbols, ci_var_heap = var_heap, ci_expr_heap = expr_heap, - ci_new_functions = [], ci_fun_heap = newHeap, ci_next_fun_nr = nr_of_funs }) + ci_new_functions = [], ci_new_variables = [], ci_fun_heap = newHeap, ci_next_fun_nr = nr_of_funs }) (groups, new_fun_defs, imported_types, imported_conses, type_heaps, ci_var_heap) - = addNewFunctionsToGroups common_defs ci_fun_heap ci_new_functions groups imported_types imported_conses type_heaps ci_var_heap + = addNewFunctionsToGroups common_defs ci_fun_heap ci_new_functions groups imported_types [] type_heaps ci_var_heap = (groups, { fundef \\ fundef <- [ fundef \\ fundef <-: fun_defs ] ++ new_fun_defs }, ci_predef_symb, imported_types, imported_conses, ci_var_heap, type_heaps, ci_expr_heap) where convert_groups group_nr groups global_type_instances fun_defs_and_ci @@ -51,18 +49,24 @@ where convert_function group_nr global_type_instances fun (fun_defs, ci) #! fun_def = fun_defs.[fun] - # {fun_body,fun_type} = fun_def + # {fun_body, fun_type, fun_info} = fun_def (fun_body, ci) = convert_dynamics_in_body {cinp_glob_type_inst = global_type_instances, cinp_group_index = group_nr} fun_body fun_type ci - = ({fun_defs & [fun] = { fun_def & fun_body = fun_body }}, ci) + = ({fun_defs & [fun] = { fun_def & fun_body = fun_body, fun_info = { fun_info & fi_local_vars = ci.ci_new_variables ++ fun_info.fi_local_vars }}}, + { ci & ci_new_variables = [] }) ---> ("convert_function", ci.ci_new_variables ++ fun_info.fi_local_vars) convert_dynamics_in_body global_type_instances (TransformedBody {tb_args,tb_rhs}) (Yes {st_args}) ci - # vars_with_types = zip2 tb_args st_args + # vars_with_types = bindVarsToTypes tb_args st_args [] (tb_rhs, ci) = convertDynamics global_type_instances vars_with_types No tb_rhs ci = (TransformedBody {tb_args = tb_args,tb_rhs = tb_rhs}, ci) convert_dynamics_in_body global_type_instances other fun_type ci = abort "unexpected value in 'convert dynamics.convert_dynamics_in_body'" +bindVarsToTypes vars types typed_vars + = fold2St bind_var_to_type vars types typed_vars +where + bind_var_to_type var type typed_vars + = [{tv_free_var = var, tv_type = type } : typed_vars] class convertDynamics a :: !ConversionInput !BoundVariables !DefaultExpression !a !*ConversionInfo -> (!a, !*ConversionInfo) @@ -89,7 +93,7 @@ where convertDynamicsOfAlgebraicPattern :: !ConversionInput !BoundVariables !DefaultExpression !(!AlgebraicPattern,[AType]) !*ConversionInfo -> (!AlgebraicPattern,!*ConversionInfo) convertDynamicsOfAlgebraicPattern cinp bound_vars default_expr (algebraic_pattern=:{ap_vars, ap_expr}, arg_types_of_conses) ci - # (ap_expr, ci) = convertDynamics cinp (zipAppend2 ap_vars arg_types_of_conses bound_vars) default_expr ap_expr ci + # (ap_expr, ci) = convertDynamics cinp (bindVarsToTypes ap_vars arg_types_of_conses bound_vars) default_expr ap_expr ci = ({algebraic_pattern & ap_expr = ap_expr}, ci) instance convertDynamics BasicPattern @@ -114,7 +118,7 @@ where = (expr @ exprs, ci) convertDynamics cinp bound_vars default_expr (Let letje=:{let_binds, let_expr,let_info_ptr}) ci # (let_types, ci) = determine_let_types let_info_ptr ci - bound_vars = zipAppend2 [ bind.bind_dst \\ bind <- let_binds ] let_types bound_vars + bound_vars = bindVarsToTypes [ bind.bind_dst \\ bind <- let_binds ] let_types bound_vars (let_binds, ci) = convertDynamics cinp bound_vars default_expr let_binds ci (let_expr, ci) = convertDynamics cinp bound_vars default_expr let_expr ci = (Let { letje & let_binds = let_binds, let_expr = let_expr}, ci) @@ -245,19 +249,20 @@ determine_defaults case_default _ ci add_dynamic_bound_vars :: ![DynamicPattern] BoundVariables -> BoundVariables add_dynamic_bound_vars [] bound_vars = bound_vars add_dynamic_bound_vars [{dp_var, dp_type_patterns_vars} : patterns] bound_vars - = add_dynamic_bound_vars patterns [(dp_var, empty_attributed_type) : mapAppend bind_info_ptr dp_type_patterns_vars bound_vars] + = add_dynamic_bound_vars patterns (foldSt bind_info_ptr dp_type_patterns_vars [ {tv_free_var = dp_var, tv_type = empty_attributed_type } : bound_vars ]) where - bind_info_ptr var_info_ptr - = ({fv_def_level = NotALevel, fv_name = a_ij_var_name, fv_info_ptr = var_info_ptr, fv_count = 0}, empty_attributed_type) + bind_info_ptr var_info_ptr bound_vars + = [{ tv_free_var = {fv_def_level = NotALevel, fv_name = a_ij_var_name, fv_info_ptr = var_info_ptr, fv_count = 0}, tv_type = empty_attributed_type } : bound_vars] open_dynamic :: Expression !*ConversionInfo -> (OpenedDynamic, Bind Expression FreeVar, !*ConversionInfo) open_dynamic dynamic_expr ci # (twotuple, ci) = getTupleSymbol 2 ci (dynamicType_var, ci) = newVariable "dt" VI_Empty ci - = ( { opened_dynamic_expr = TupleSelect twotuple 0 dynamic_expr, opened_dynamic_type = Var dynamicType_var }, - { bind_src = TupleSelect twotuple 1 dynamic_expr, bind_dst = varToFreeVar dynamicType_var 0 }, - ci) + dynamicType_fv = varToFreeVar dynamicType_var 1 + = ( { opened_dynamic_expr = TupleSelect twotuple 0 dynamic_expr, opened_dynamic_type = Var dynamicType_var }, + { bind_src = TupleSelect twotuple 1 dynamic_expr, bind_dst = dynamicType_fv }, + { ci & ci_new_variables = [ dynamicType_fv : ci.ci_new_variables ]}) /**************************************************************************************************/ @@ -268,91 +273,113 @@ convertDynamicPatterns cinp bound_vars {case_guards = DynamicPatterns [], case_d No -> abort "unexpected value in convertDynamics: 'convertDynamicPatterns'" convertDynamicPatterns cinp bound_vars {case_expr, case_guards = DynamicPatterns patterns, case_default, case_info_ptr} ci # (opened_dynamic, dt_bind, ci) = open_dynamic case_expr ci - (ind_0, ci) = newVariable "ind_0" VI_Empty ci + (ind_0, ci) = newVariable "ind_0" (VI_Indirection 0) ci (c_1, ci) = newVariable "c_1" (VI_Default 0) ci new_default = newDefault c_1 ind_0 (result_type, ci) = getResultType case_info_ptr ci bound_vars = addToBoundVars (freeVarToVar dt_bind.bind_dst) empty_attributed_type (addToBoundVars ind_0 empty_attributed_type (addToBoundVars c_1 result_type (add_dynamic_bound_vars patterns bound_vars))) - (binds, expr, ci) = convertDynamicPattern cinp bound_vars new_default 1 opened_dynamic result_type case_default patterns ci + (binds, expr, ci) = convert_dynamic_pattern cinp bound_vars new_default 1 opened_dynamic result_type case_default patterns ci (let_info_ptr, ci) = let_ptr ci = (Let {let_strict = False, let_binds = [ dt_bind : binds ], let_expr = expr, let_info_ptr = let_info_ptr}, ci) - -convertDynamicPattern :: !ConversionInput !BoundVariables DefaultExpression Int OpenedDynamic AType (Optional Expression) ![DynamicPattern] *ConversionInfo -> (Env Expression FreeVar, Expression, *ConversionInfo) -convertDynamicPattern cinp bound_vars this_default pattern_number opened_dynamic result_type last_default - [{ dp_var, dp_type_patterns_vars, dp_type_code, dp_rhs } : patterns] ci - # /*** The last case may noy have a default ***/ - ind_var = getIndirectionVar this_default - this_default = if (isEmpty patterns && (isNo last_default)) No this_default - - /*** convert the elements of this pattern ***/ - x_i_bind = { bind_src = opened_dynamic.opened_dynamic_expr, bind_dst = dp_var } - (a_ij_binds, ci) = createVariables dp_type_patterns_vars [] ci - (type_code, ci) = convertTypecode cinp dp_type_code ci - (dp_rhs, ci) = convertDynamics cinp bound_vars this_default dp_rhs ci - - /*** recursively convert the other patterns ***/ - (binds, ci) = convertOtherPatterns cinp bound_vars this_default pattern_number opened_dynamic result_type last_default patterns ci - - /*** generate the expression ***/ - (unify_symb, ci) = getSymbol PD_unify SK_Function 2 ci - (twotuple, ci) = getTupleSymbol 2 ci - (let_info_ptr, ci) = let_ptr ci - (case_info_ptr, ci) = case_ptr ci - (default_expr, ci) = toExpression this_default ci - (unify_result, ci) = newVariable "result" VI_Empty ci - (unify_bool, ci) = newVariable "unify_bool" VI_Empty ci - let_expr = Let { let_strict = False, - let_binds = [{ bind_src = App { app_symb = unify_symb, app_args = [opened_dynamic.opened_dynamic_type, type_code], app_info_ptr = nilPtr }, - bind_dst = varToFreeVar unify_result 0 }, - { bind_src = TupleSelect twotuple 0 (Var unify_result), - bind_dst = varToFreeVar unify_bool 0 }, - { bind_src = TupleSelect twotuple 1 (Var unify_result), - bind_dst = varToFreeVar ind_var 0 } - ], - let_expr = Case { case_expr = Var unify_bool, - case_guards = BasicPatterns BT_Bool [{bp_value = BVB True, bp_expr = dp_rhs}], - case_default = default_expr, - case_ident = No, - case_info_ptr = case_info_ptr }, - let_info_ptr = let_info_ptr } - = ([x_i_bind : a_ij_binds ++ binds], let_expr, ci) - -convertOtherPatterns :: ConversionInput BoundVariables DefaultExpression Int OpenedDynamic AType !(Optional Expression) ![DynamicPattern] !*ConversionInfo -> (Env Expression FreeVar, *ConversionInfo) -convertOtherPatterns _ _ _ _ _ _ No [] ci - = ([], ci) -convertOtherPatterns cinp bound_vars this_default _ _ result_type (Yes last_default_expr) [] ci - # c_i = getVariable this_default - (c_bind, ci) = generateBinding cinp bound_vars c_i last_default_expr result_type ci - = ([c_bind], ci) -convertOtherPatterns cinp bound_vars this_default pattern_number opened_dynamic result_type last_default patterns ci - # (ind_i, ci) = newVariable ("ind_"+++toString (pattern_number)) VI_Empty ci - (c_inc_i, ci) = newVariable ("c_"+++toString (inc pattern_number)) (VI_Default 0) ci - new_default = newDefault c_inc_i ind_i - bound_vars = addToBoundVars ind_i empty_attributed_type (addToBoundVars c_inc_i result_type bound_vars) - (binds, expr, ci) = convertDynamicPattern cinp bound_vars new_default (inc pattern_number) opened_dynamic result_type last_default patterns ci - c_i = getVariable this_default - (c_bind, ci) = generateBinding cinp bound_vars c_i expr result_type ci - = ([c_bind : binds], ci) +where + convert_dynamic_pattern :: !ConversionInput !BoundVariables DefaultExpression Int OpenedDynamic AType (Optional Expression) ![DynamicPattern] *ConversionInfo + -> (Env Expression FreeVar, Expression, *ConversionInfo) + convert_dynamic_pattern cinp bound_vars this_default pattern_number opened_dynamic result_type last_default + [{ dp_var, dp_type_patterns_vars, dp_type_code, dp_rhs } : patterns] ci + # /*** The last case may noy have a default ***/ + + ind_var = getIndirectionVar this_default + + this_default = if (isEmpty patterns && (isNo last_default)) No this_default + + /*** convert the elements of this pattern ***/ + + x_i_bind = { bind_src = opened_dynamic.opened_dynamic_expr, bind_dst = dp_var } + (a_ij_binds, ci) = createVariables dp_type_patterns_vars [] ci + (type_code, ci) = convertTypecode cinp dp_type_code ci + (dp_rhs, ci) = convertDynamics cinp bound_vars this_default dp_rhs ci + + /*** recursively convert the other patterns ***/ + + (binds, ci) = convert_other_patterns cinp bound_vars this_default pattern_number opened_dynamic result_type last_default patterns ci + + + /*** generate the expression ***/ + (unify_symb, ci) = getSymbol PD_unify SK_Function 2 ci + (twotuple, ci) = getTupleSymbol 2 ci + (let_info_ptr, ci) = let_ptr ci + (case_info_ptr, ci) = case_ptr ci + (default_expr, ci) = toExpression this_default ci + (unify_result_var, ci) = newVariable "result" VI_Empty ci + unify_result_fv = varToFreeVar unify_result_var 1 + (unify_bool_var, ci) = newVariable "unify_bool" VI_Empty ci + unify_bool_fv = varToFreeVar unify_bool_var 1 + + (let_binds, ci) = bind_indirection_var ind_var unify_result_var twotuple ci + + let_expr = Let { let_strict = False, + let_binds = [{ bind_src = App { app_symb = unify_symb, app_args = [opened_dynamic.opened_dynamic_type, type_code], app_info_ptr = nilPtr }, + bind_dst = unify_result_fv }, + { bind_src = TupleSelect twotuple 0 (Var unify_result_var), + bind_dst = unify_bool_fv } : let_binds + ], + let_expr = Case { case_expr = Var unify_bool_var, + case_guards = BasicPatterns BT_Bool [{bp_value = BVB True, bp_expr = dp_rhs}], + case_default = default_expr, + case_ident = No, + case_info_ptr = case_info_ptr }, + let_info_ptr = let_info_ptr } + = ([x_i_bind : a_ij_binds ++ binds], let_expr, { ci & ci_new_variables = [unify_result_fv, unify_bool_fv : ci.ci_new_variables]}) + where + bind_indirection_var var=:{var_info_ptr} unify_result_var twotuple ci=:{ci_var_heap,ci_new_variables} + # (VI_Indirection ref_count, ci_var_heap) = readPtr var_info_ptr ci_var_heap + | ref_count > 0 + # ind_fv = varToFreeVar var ref_count + = ([{ bind_src = TupleSelect twotuple 1 (Var unify_result_var), bind_dst = ind_fv }], + { ci & ci_var_heap = ci_var_heap, ci_new_variables = [ ind_fv : ci_new_variables ]}) + = ([], {ci & ci_var_heap = ci_var_heap}) + + convert_other_patterns :: ConversionInput BoundVariables DefaultExpression Int OpenedDynamic AType !(Optional Expression) ![DynamicPattern] !*ConversionInfo + -> (Env Expression FreeVar, *ConversionInfo) + convert_other_patterns _ _ _ _ _ _ No [] ci + = ([], ci) + convert_other_patterns cinp bound_vars this_default _ _ result_type (Yes last_default_expr) [] ci + # c_i = getVariable this_default + (c_bind, ci) = generateBinding cinp bound_vars c_i last_default_expr result_type ci + = ([c_bind], ci) + convert_other_patterns cinp bound_vars this_default pattern_number opened_dynamic result_type last_default patterns ci + # (ind_i, ci) = newVariable ("ind_"+++toString (pattern_number)) (VI_Indirection 0) ci + (c_inc_i, ci) = newVariable ("c_"+++toString (inc pattern_number)) (VI_Default 0) ci + new_default = newDefault c_inc_i ind_i + bound_vars = addToBoundVars ind_i empty_attributed_type (addToBoundVars c_inc_i result_type bound_vars) + (binds, expr, ci) = convert_dynamic_pattern cinp bound_vars new_default (inc pattern_number) opened_dynamic result_type last_default patterns ci + c_i = getVariable this_default + (c_bind, ci) = generateBinding cinp bound_vars c_i expr result_type ci + = ([c_bind : binds], ci) generateBinding :: !ConversionInput BoundVariables BoundVar Expression AType !*ConversionInfo -> *(Bind Expression FreeVar, *ConversionInfo) generateBinding cinp bound_vars var bind_expr result_type ci # (ref_count, ci) = get_reference_count var ci | ref_count == 0 - = ({ bind_src = bind_expr, bind_dst = varToFreeVar var 1}, ci) + # free_var = varToFreeVar var 1 + = ({ bind_src = bind_expr, bind_dst = free_var }, { ci & ci_new_variables = [ free_var : ci.ci_new_variables ]}) # (saved_defaults, ci_var_heap) = foldSt save_default bound_vars ([], ci.ci_var_heap) - (act_args, free_typed_vars, tb_rhs, ci_var_heap) = copyExpression bound_vars bind_expr ci_var_heap + (act_args, free_typed_vars, local_free_vars, tb_rhs, ci_var_heap) = copyExpression bound_vars bind_expr ci_var_heap + (ci_new_variables, ci_var_heap) = foldSt remove_local_var ci.ci_new_variables ([], ci_var_heap) ci_var_heap = foldSt restore_default saved_defaults ci_var_heap - tb_args = [ arg \\ (arg, _) <- free_typed_vars ] - arg_types = [ type \\ (_, type) <- free_typed_vars ] + tb_args = [ ftv.tv_free_var \\ ftv <- free_typed_vars ] + arg_types = [ ftv.tv_type \\ ftv <- free_typed_vars ] (fun_symb, (ci_next_fun_nr, ci_new_functions, ci_fun_heap)) - = newFunction No (TransformedBody {tb_args = tb_args, tb_rhs = tb_rhs}) arg_types result_type cinp.cinp_group_index + = newFunction No (TransformedBody {tb_args = tb_args, tb_rhs = tb_rhs}) local_free_vars arg_types result_type cinp.cinp_group_index (ci.ci_next_fun_nr, ci.ci_new_functions, ci.ci_fun_heap) + free_var = varToFreeVar var (inc ref_count) = ({ bind_src = App { app_symb = fun_symb, app_args = act_args, app_info_ptr = nilPtr }, - bind_dst = varToFreeVar var (inc ref_count) }, - { ci & ci_var_heap = ci_var_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions, ci_fun_heap = ci_fun_heap }) + bind_dst = free_var }, + { ci & ci_var_heap = ci_var_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions, ci_fun_heap = ci_fun_heap, + ci_new_variables = [ free_var : ci_new_variables ] }) where get_reference_count {var_name,var_info_ptr} ci=:{ci_var_heap} # (info, ci_var_heap) = readPtr var_info_ptr ci_var_heap @@ -361,14 +388,25 @@ generateBinding cinp bound_vars var bind_expr result_type ci VI_Default ref_count -> (ref_count, ci) // _ -> (0, ci) ---> ("get_reference_count", var_name) /* A predicted variable always has a ref_count */ - save_default ({fv_info_ptr},_) (saved_defaults, ci_var_heap) + save_default {tv_free_var={fv_info_ptr}} (saved_defaults, ci_var_heap) # (info, ci_var_heap) = readPtr fv_info_ptr ci_var_heap = case info of - VI_Default ref_count -> ([(fv_info_ptr, ref_count) : saved_defaults] , ci_var_heap) + VI_Default ref_count + -> ([(fv_info_ptr, info) : saved_defaults] , ci_var_heap) + VI_Indirection ref_count + -> ([(fv_info_ptr, info) : saved_defaults] , ci_var_heap) _ -> (saved_defaults, ci_var_heap) - restore_default (var_info_ptr,ref_count) ci_var_heap - = ci_var_heap <:= (var_info_ptr, VI_Default ref_count) + restore_default (var_info_ptr,info) ci_var_heap + = ci_var_heap <:= (var_info_ptr, info) + + remove_local_var fv=:{fv_info_ptr} (local_vars, var_heap) + # (info, var_heap) = readPtr fv_info_ptr var_heap + = case info of + VI_LocalVar + -> (local_vars, var_heap) + _ + -> ([fv : local_vars], var_heap) /**************************************************************************************************/ @@ -381,12 +419,13 @@ where create_variable var_info_ptr ci # (placeholder_symb, ci) = getSymbol PD_variablePlaceholder SK_Constructor 3 ci cyclic_var = {var_name = a_ij_var_name, var_info_ptr = var_info_ptr, var_expr_ptr = nilPtr} + cyclic_fv = varToFreeVar cyclic_var 1 = ({ bind_src = App { app_symb = placeholder_symb, app_args = [Var cyclic_var, Var cyclic_var], app_info_ptr = nilPtr }, bind_dst = varToFreeVar cyclic_var 1 }, - ci) + { ci & ci_new_variables = [ cyclic_fv : ci.ci_new_variables ]}) /**************************************************************************************************/ @@ -417,9 +456,14 @@ where toExpression2 variable [indirection_var : indirection_vars] ci # (expression, ci) = toExpression2 variable indirection_vars ci (undo_symb, ci) = getSymbol PD_undo_indirections SK_Function 2 ci + ci_var_heap = adjust_ref_count indirection_var ci.ci_var_heap = (App { app_symb = undo_symb, app_args = [expression, Var indirection_var], - app_info_ptr = nilPtr }, ci) + app_info_ptr = nilPtr }, { ci & ci_var_heap = ci_var_heap }) + + adjust_ref_count {var_info_ptr} var_heap + # (VI_Indirection ref_count, var_heap) = readPtr var_info_ptr var_heap + = var_heap <:= (var_info_ptr, VI_Indirection (inc ref_count)) varToFreeVar :: BoundVar Int -> FreeVar varToFreeVar {var_name, var_info_ptr} count @@ -432,7 +476,7 @@ freeVarToVar {fv_name, fv_info_ptr} addToBoundVars :: BoundVar AType BoundVariables -> BoundVariables addToBoundVars var type bound_vars - = [ (varToFreeVar var 0, type) : bound_vars ] + = [ { tv_free_var = varToFreeVar var 0, tv_type = type } : bound_vars ] get_constructor :: !{!GlobalTCType} Index -> Expression diff --git a/frontend/convertcases.dcl b/frontend/convertcases.dcl index d7b2cf2..7f1e9ff 100644 --- a/frontend/convertcases.dcl +++ b/frontend/convertcases.dcl @@ -18,10 +18,16 @@ convertIclModule :: !{# CommonDefs} !*{#{# CheckedTypeDef}} !ImportedConstructor -> (!*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps) -newFunction :: !(Optional Ident) !FunctionBody ![AType] !AType !Int !(!Int, ![FunctionInfoPtr],!*FunctionHeap) +newFunction :: !(Optional Ident) !FunctionBody ![FreeVar] ![AType] !AType !Int !(!Int, ![FunctionInfoPtr],!*FunctionHeap) -> (! SymbIdent, !(!Int, ![FunctionInfoPtr],!*FunctionHeap)) -copyExpression :: ![(FreeVar,AType)] !Expression !*VarHeap -> (![Expression], ![.(FreeVar,AType)], !Expression, !*VarHeap) + +:: TypedVariable = + { tv_free_var :: !FreeVar + , tv_type :: !AType + } + +copyExpression :: ![TypedVariable] !Expression !*VarHeap -> (![Expression], ![TypedVariable], ![FreeVar], !Expression, !*VarHeap) addNewFunctionsToGroups :: !{#.CommonDefs} FunctionHeap ![FunctionInfoPtr] !*{! Group} !*{#{# CheckedTypeDef}} !ImportedFunctions !*TypeHeaps !*VarHeap -> (!*{! Group}, ![FunDef], !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*TypeHeaps, !*VarHeap) diff --git a/frontend/convertcases.icl b/frontend/convertcases.icl index 67518eb..377420f 100644 --- a/frontend/convertcases.icl +++ b/frontend/convertcases.icl @@ -89,9 +89,11 @@ where = (TupleSelect tuple_symbol arg_nr expr, ci) convertCases bound_vars group_index common_defs (Case case_expr) ci = convertCasesInCaseExpression bound_vars group_index common_defs cHasNoDefault case_expr ci +/* convertCases bound_vars group_index common_defs (DynamicExpr dynamik) ci # (dynamik, ci) = convertCases bound_vars group_index common_defs dynamik ci = (DynamicExpr dynamik, ci) +*/ convertCases bound_vars group_index common_defs expr ci = (expr, ci) @@ -110,10 +112,25 @@ where cHasNoDefault :== nilPtr convertDefaultToExpression default_ptr (EI_Default expr type prev_default) bound_vars group_index common_defs ci=:{ci_var_heap} - # (act_args, free_typed_vars, expression, ci_var_heap) = copyExpression bound_vars expr ci_var_heap - (fun_symb, ci) = newDefaultFunction free_typed_vars expression type prev_default group_index common_defs { ci & ci_var_heap = ci_var_heap } + # ci_var_heap = foldSt (\({fv_info_ptr}, type) -> writePtr fv_info_ptr (VI_BoundVar type)) bound_vars ci_var_heap + (expression, {cp_free_vars, cp_var_heap, cp_local_vars}) = copy expr { cp_free_vars = [], cp_var_heap = ci_var_heap, cp_local_vars = [] } + (act_args, free_typed_vars, ci_var_heap) = foldSt retrieveVariable cp_free_vars ([], [], cp_var_heap) + (fun_symb, ci) = new_default_function free_typed_vars cp_local_vars expression type prev_default group_index common_defs { ci & ci_var_heap = ci_var_heap } = (App { app_symb = fun_symb, app_args = act_args, app_info_ptr = nilPtr }, { ci & ci_expr_heap = ci.ci_expr_heap <:= (default_ptr, EI_DefaultFunction fun_symb act_args)}) +where + new_default_function free_vars local_vars rhs_expr result_type prev_default group_index common_defs ci + # (guarded_exprs, ci) = convertPatternExpression [] [free_vars] group_index common_defs prev_default rhs_expr ci + fun_bodies = map build_pattern guarded_exprs + arg_types = map (\(_,type) -> type) free_vars + (fun_symb, (ci_next_fun_nr, ci_new_functions, ci_fun_heap)) + = newFunction No (BackendBody fun_bodies) local_vars arg_types result_type group_index + (ci.ci_next_fun_nr, ci.ci_new_functions, ci.ci_fun_heap) + = (fun_symb, { ci & ci_fun_heap = ci_fun_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions }) + + build_pattern ([ right_patterns : _ ], bb_rhs) + = { bb_args = right_patterns, bb_rhs = bb_rhs } + convertDefaultToExpression default_ptr (EI_DefaultFunction fun_symb act_args) bound_vars group_index common_defs ci = (App { app_symb = fun_symb, app_args = act_args, app_info_ptr = nilPtr }, ci) @@ -144,40 +161,35 @@ combineDefaults default_ptr this_default bound_vars guards group_index common_de = (this_default, ci) +:: TypedVariable = + { tv_free_var :: !FreeVar + , tv_type :: !AType + } + +copyExpression :: ![TypedVariable] !Expression !*VarHeap -> (![Expression], ![TypedVariable], ![FreeVar], !Expression, !*VarHeap) +copyExpression bound_vars expression var_heap + # var_heap = foldSt (\{tv_free_var={fv_info_ptr},tv_type} -> writePtr fv_info_ptr (VI_BoundVar tv_type)) bound_vars var_heap + (expression, {cp_free_vars, cp_var_heap, cp_local_vars}) = copy expression { cp_free_vars = [], cp_var_heap = var_heap, cp_local_vars = [] } + (bound_vars, free_typed_vars, var_heap) = foldSt retrieve_variable cp_free_vars ([], [], cp_var_heap) + = (bound_vars, free_typed_vars, cp_local_vars, expression, var_heap) +where + retrieve_variable (var_info_ptr, type) (bound_vars, free_typed_vars, var_heap) + # (VI_FreeVar name new_ptr count type, var_heap) = readPtr var_info_ptr var_heap + = ( [Var { var_name = name, var_info_ptr = var_info_ptr, var_expr_ptr = nilPtr} : bound_vars], + [{tv_free_var = { fv_def_level = NotALevel, fv_name = name, fv_info_ptr = new_ptr, fv_count = count }, tv_type = type} : free_typed_vars], var_heap) + retrieveVariable (var_info_ptr, type) (bound_vars, free_typed_vars, var_heap) # (VI_FreeVar name new_ptr count type, var_heap) = readPtr var_info_ptr var_heap = ( [Var { var_name = name, var_info_ptr = var_info_ptr, var_expr_ptr = nilPtr} : bound_vars], - [({ fv_def_level = NotALevel, fv_name = name, fv_info_ptr = new_ptr, fv_count = count }, type) : free_typed_vars], var_heap) - -copyCaseExpression bound_vars opt_variable guards_and_default var_heap - # var_heap = foldSt (\({fv_name,fv_info_ptr},type) -> writePtr fv_info_ptr (VI_BoundVar type)) bound_vars var_heap - (opt_copied_var, var_heap) = copy_variable opt_variable var_heap - (expression, {cp_free_vars, cp_var_heap}) = copy guards_and_default ({ cp_free_vars = [], cp_var_heap = var_heap } - ==> ("copyCaseExpression", bound_vars, guards_and_default)) - (bound_vars, free_typed_vars, var_heap) = foldSt retrieveVariable cp_free_vars ([], [], cp_var_heap) - (opt_free_var, var_heap) = toOptionalFreeVar opt_copied_var var_heap - = (bound_vars, free_typed_vars, opt_free_var, expression, var_heap) -where - copy_variable (Yes (var=:{var_name,var_info_ptr}, var_type)) var_heap - # (new_info, var_heap) = newPtr VI_Empty var_heap - = (Yes (var_info_ptr, var_type), var_heap <:= (var_info_ptr, VI_FreeVar var_name new_info 0 var_type)) - copy_variable No var_heap - = (No, var_heap) - -copyExpression :: ![(FreeVar,AType)] !Expression !*VarHeap -> (![Expression], ![.(FreeVar,AType)], !Expression, !*VarHeap) -copyExpression bound_vars expression var_heap - # var_heap = foldSt (\({fv_name,fv_info_ptr},type) -> writePtr fv_info_ptr (VI_BoundVar type)) bound_vars var_heap - (expression, {cp_free_vars, cp_var_heap}) = copy expression { cp_free_vars = [], cp_var_heap = var_heap } - (bound_vars, free_typed_vars, var_heap) = foldSt retrieveVariable cp_free_vars ([], [], cp_var_heap) - = (bound_vars, free_typed_vars, expression, var_heap) + [({ fv_def_level = NotALevel, fv_name = name, fv_info_ptr = new_ptr, fv_count = count }, type) : free_typed_vars], var_heap) convertCasesInCaseExpression bound_vars group_index common_defs default_ptr { case_expr, case_guards, case_default, case_ident, case_info_ptr} ci # (case_default, ci) = combineDefaults default_ptr case_default bound_vars case_guards group_index common_defs ci (case_expr, ci) = convertCases bound_vars group_index common_defs case_expr ci (EI_CaseTypeAndRefCounts case_type ref_counts, ci_expr_heap) = readPtr case_info_ptr ci.ci_expr_heap - (act_vars, form_vars, opt_free_var, (case_guards, case_default), ci_var_heap) - = copyCaseExpression bound_vars (get_variable case_expr case_type.ct_pattern_type) (case_guards,case_default) ci.ci_var_heap - (fun_symb, ci) = newCaseFunction case_ident case_guards case_default case_type opt_free_var form_vars + (act_vars, form_vars, opt_free_var, local_vars, (case_guards, case_default), ci_var_heap) + = copy_case_expression bound_vars (get_variable case_expr case_type.ct_pattern_type) (case_guards,case_default) ci.ci_var_heap + (fun_symb, ci) = new_case_function case_ident case_guards case_default case_type opt_free_var form_vars local_vars group_index common_defs default_ptr { ci & ci_var_heap = ci_var_heap, ci_expr_heap = ci_expr_heap } = (App { app_symb = fun_symb, app_args = [ case_expr : act_vars ], app_info_ptr = nilPtr }, ci) where @@ -185,6 +197,31 @@ where = Yes (var, pattern_type) get_variable _ _ = No + + copy_case_expression bound_vars opt_variable guards_and_default var_heap + # var_heap = foldSt (\({fv_name,fv_info_ptr},type) -> writePtr fv_info_ptr (VI_BoundVar type)) bound_vars var_heap + (opt_copied_var, var_heap) = copy_variable opt_variable var_heap + (expression, {cp_free_vars, cp_var_heap, cp_local_vars}) = copy guards_and_default { cp_free_vars = [], cp_var_heap = var_heap, cp_local_vars = [] } + (bound_vars, free_typed_vars, var_heap) = foldSt retrieveVariable cp_free_vars ([], [], cp_var_heap) + (opt_free_var, var_heap) = toOptionalFreeVar opt_copied_var var_heap + = (bound_vars, free_typed_vars, opt_free_var, cp_local_vars, expression, var_heap) + + copy_variable (Yes (var=:{var_name,var_info_ptr}, var_type)) var_heap + # (new_info, var_heap) = newPtr VI_Empty var_heap + = (Yes (var_info_ptr, var_type), var_heap <:= (var_info_ptr, VI_FreeVar var_name new_info 0 var_type)) + copy_variable No var_heap + = (No, var_heap) + + new_case_function opt_id patterns case_default {ct_result_type,ct_pattern_type,ct_cons_types} opt_var free_vars local_vars + group_index common_defs prev_default ci=:{ci_expr_heap} + # (default_ptr, ci_expr_heap) = makePtrToDefault case_default ct_result_type prev_default ci_expr_heap + (fun_bodies, ci) = convertPatterns patterns ct_cons_types opt_var [] free_vars default_ptr group_index common_defs { ci & ci_expr_heap = ci_expr_heap } + (fun_bodies, ci) = convertDefault default_ptr opt_var [] free_vars group_index common_defs (fun_bodies, ci) + (fun_symb, (ci_next_fun_nr, ci_new_functions, ci_fun_heap)) + = newFunction opt_id (BackendBody fun_bodies) local_vars [ct_pattern_type : [ type \\ (_, type) <- free_vars]] ct_result_type group_index + (ci.ci_next_fun_nr, ci.ci_new_functions, ci.ci_fun_heap) + = (fun_symb, { ci & ci_fun_heap = ci_fun_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions }) + makePtrToDefault (Yes default_expr) type prev_default_ptr expr_heap @@ -215,31 +252,10 @@ where typed_free_var_to_pattern (free_var, type) = FP_Variable free_var -newDefaultFunction free_vars rhs_expr result_type prev_default group_index common_defs ci - # (guarded_exprs, ci) = convertPatternExpression [] [free_vars] group_index common_defs prev_default rhs_expr ci - fun_bodies = map build_pattern guarded_exprs - arg_types = map (\(_,type) -> type) free_vars - (fun_symb, (ci_next_fun_nr, ci_new_functions, ci_fun_heap)) - = newFunction No (BackendBody fun_bodies) arg_types result_type group_index - (ci.ci_next_fun_nr, ci.ci_new_functions, ci.ci_fun_heap) - = (fun_symb, { ci & ci_fun_heap = ci_fun_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions }) -where - build_pattern ([ right_patterns : _ ], bb_rhs) - = { bb_args = right_patterns, bb_rhs = bb_rhs } -newCaseFunction opt_id patterns case_default {ct_result_type,ct_pattern_type,ct_cons_types} opt_var free_vars - group_index common_defs prev_default ci=:{ci_expr_heap} - # (default_ptr, ci_expr_heap) = makePtrToDefault case_default ct_result_type prev_default ci_expr_heap - (fun_bodies, ci) = convertPatterns patterns ct_cons_types opt_var [] free_vars default_ptr group_index common_defs { ci & ci_expr_heap = ci_expr_heap } - (fun_bodies, ci) = convertDefault default_ptr opt_var [] free_vars group_index common_defs (fun_bodies, ci) - (fun_symb, (ci_next_fun_nr, ci_new_functions, ci_fun_heap)) - = newFunction opt_id (BackendBody fun_bodies) [ct_pattern_type : map (\(_,type) -> type) free_vars] ct_result_type group_index - (ci.ci_next_fun_nr, ci.ci_new_functions, ci.ci_fun_heap) - = (fun_symb, { ci & ci_fun_heap = ci_fun_heap, ci_next_fun_nr = ci_next_fun_nr, ci_new_functions = ci_new_functions }) - -newFunction :: !(Optional Ident) !FunctionBody ![AType] !AType !Int !(!Int, ![FunctionInfoPtr],!*FunctionHeap) +newFunction :: !(Optional Ident) !FunctionBody ![FreeVar] ![AType] !AType !Int !(!Int, ![FunctionInfoPtr],!*FunctionHeap) -> (! SymbIdent, !(!Int, ![FunctionInfoPtr],!*FunctionHeap)) -newFunction opt_id fun_bodies arg_types result_type group_index (ci_next_fun_nr, ci_new_functions, ci_fun_heap) +newFunction opt_id fun_bodies local_vars arg_types result_type group_index (ci_next_fun_nr, ci_new_functions, ci_fun_heap) # (fun_def_ptr, ci_fun_heap) = newPtr FI_Empty ci_fun_heap fun_id = getIdent opt_id ci_next_fun_nr arity = length arg_types @@ -263,7 +279,7 @@ newFunction opt_id fun_bodies arg_types result_type group_index (ci_next_fun_nr, , fun_index = NoIndex , fun_kind = FK_Function , fun_lifted = 0 - , fun_info = { EmptyFunInfo & fi_group_index = group_index } + , fun_info = { EmptyFunInfo & fi_group_index = group_index, fi_local_vars = local_vars } } = ({ symb_name = fun_id, symb_kind = SK_GeneratedFunction fun_def_ptr ci_next_fun_nr, symb_arity = arity }, (inc ci_next_fun_nr, [fun_def_ptr : ci_new_functions], @@ -721,25 +737,27 @@ convertRootExpression bound_vars group_index common_defs _ expr ci :: CopyInfo = { cp_free_vars :: ![(VarInfoPtr,AType)] + , cp_local_vars :: ![FreeVar] , cp_var_heap :: !.VarHeap } - class copy e :: !e !*CopyInfo -> (!e, !*CopyInfo) instance copy BoundVar where - copy var=:{var_name,var_info_ptr} cp_info=:{cp_free_vars, cp_var_heap} - #! var_info = sreadPtr var_info_ptr cp_var_heap + copy var=:{var_name,var_info_ptr} cp_info=:{cp_var_heap} + # (var_info, cp_var_heap) = readPtr var_info_ptr cp_var_heap + cp_info = { cp_info & cp_var_heap = cp_var_heap } = case var_info of VI_FreeVar name new_info_ptr count type - -> ({ var & var_info_ptr = new_info_ptr }, { cp_free_vars = cp_free_vars, - cp_var_heap = cp_var_heap <:= (var_info_ptr, VI_FreeVar name new_info_ptr (inc count) type)}) + -> ({ var & var_info_ptr = new_info_ptr }, + { cp_info & cp_var_heap = cp_info.cp_var_heap <:= (var_info_ptr, VI_FreeVar name new_info_ptr (inc count) type)}) VI_LocalVar - -> (var, {cp_free_vars = cp_free_vars, cp_var_heap = cp_var_heap}) + -> (var, cp_info) VI_BoundVar type - # (new_info_ptr, cp_var_heap) = newPtr VI_Empty cp_var_heap - -> ({ var & var_info_ptr = new_info_ptr }, { cp_free_vars = [ (var_info_ptr, type) : cp_free_vars ], + # (new_info_ptr, cp_var_heap) = newPtr VI_Empty cp_info.cp_var_heap + -> ({ var & var_info_ptr = new_info_ptr }, + { cp_info & cp_free_vars = [ (var_info_ptr, type) : cp_info.cp_free_vars ], cp_var_heap = cp_var_heap <:= (var_info_ptr, VI_FreeVar var_name new_info_ptr 1 type) }) _ -> abort "copy [BoundVar] (convertcases, 612)" <<- (var_info ---> (var_name, ptrToInt var_info_ptr)) @@ -755,10 +773,13 @@ where copy (fun_expr @ exprs) cp_info # ((fun_expr, exprs), cp_info) = copy (fun_expr, exprs) cp_info = (fun_expr @ exprs, cp_info) - copy (Let lad=:{let_binds,let_expr}) cp_info=:{cp_var_heap} - # ((let_binds,let_expr), cp_info) = copy (let_binds,let_expr) - { cp_info & cp_var_heap = foldSt (\{bind_dst={fv_info_ptr}} -> writePtr fv_info_ptr VI_LocalVar) let_binds cp_var_heap } + copy (Let lad=:{let_binds,let_expr}) cp_info=:{cp_var_heap, cp_local_vars} + # (cp_local_vars, cp_var_heap) = foldSt bind_let_var let_binds (cp_local_vars, cp_var_heap) + # ((let_binds,let_expr), cp_info) = copy (let_binds,let_expr) {cp_info & cp_var_heap = cp_var_heap, cp_local_vars = cp_local_vars } = (Let {lad & let_expr = let_expr, let_binds = let_binds }, cp_info) + where + bind_let_var {bind_dst} (local_vars, var_heap) + = ([bind_dst : local_vars], var_heap <:= (bind_dst.fv_info_ptr, VI_LocalVar)) copy (Case case_expr) cp_info # (case_expr, cp_info) = copy case_expr cp_info = (Case case_expr, cp_info) @@ -783,9 +804,11 @@ where copy (TupleSelect tuple_symbol arg_nr expr) cp_info # (expr, cp_info) = copy expr cp_info = (TupleSelect tuple_symbol arg_nr expr, cp_info) +/* copy (DynamicExpr dynamik) cp_info # (dynamik, cp_info) = copy dynamik cp_info = (DynamicExpr dynamik, cp_info) +*/ copy EE cp_info = (EE, cp_info) copy expr cp_info @@ -811,7 +834,7 @@ where copy selector cp_info = (selector, cp_info) - +/* instance copy DynamicExpr where copy dynamik=:{dyn_expr,dyn_uni_vars,dyn_type_code} cp_info=:{cp_var_heap} @@ -842,6 +865,9 @@ copyVarInfo var_info_ptr cp_info=:{cp_free_vars, cp_var_heap} # (new_info_ptr, cp_var_heap) = newPtr VI_Empty cp_var_heap -> (new_info_ptr, { cp_free_vars = [ (var_info_ptr, type) : cp_free_vars ], cp_var_heap = cp_var_heap <:= (var_info_ptr, VI_FreeVar { id_name = "_t", id_info = nilPtr } new_info_ptr 1 type) }) + +*/ + instance copy Case where copy this_case=:{case_expr, case_guards, case_default} cp_info @@ -856,9 +882,11 @@ where copy (BasicPatterns type patterns) cp_info # (patterns, cp_info) = copy patterns cp_info = (BasicPatterns type patterns, cp_info) +/* copy (DynamicPatterns patterns) cp_info # (patterns, cp_info) = copy patterns cp_info = (DynamicPatterns patterns, cp_info) +*/ instance copy AlgebraicPattern where @@ -871,7 +899,7 @@ where copy pattern=:{bp_expr} cp_info # (bp_expr, cp_info) = copy bp_expr cp_info = ({ pattern & bp_expr = bp_expr }, cp_info) - +/* instance copy DynamicPattern where copy pattern=:{dp_var={fv_info_ptr},dp_rhs,dp_type_patterns_vars, dp_type_code} cp_info=:{cp_var_heap} @@ -880,7 +908,7 @@ where <:= (fv_info_ptr, VI_LocalVar) } (dp_type_code, cp_info) = copy dp_type_code cp_info = ({ pattern & dp_rhs = dp_rhs, dp_type_code = dp_type_code }, cp_info) - +*/ instance copy [a] | copy a where copy l cp_info = mapSt copy l cp_info @@ -998,8 +1026,10 @@ where = weightedRefCount dcl_functions common_defs depth (expression, expressions) rc_info weightedRefCount dcl_functions common_defs depth (TupleSelect tuple_symbol arg_nr expr) rc_info = weightedRefCount dcl_functions common_defs depth expr rc_info +/* weightedRefCount dcl_functions common_defs depth (DynamicExpr {dyn_expr}) rc_info = weightedRefCount dcl_functions common_defs depth dyn_expr rc_info +*/ weightedRefCount dcl_functions common_defs depth (AnyCodeExpr _ _ _) rc_info = rc_info weightedRefCount dcl_functions common_defs depth (ABCCodeExpr _ _) rc_info @@ -1132,17 +1162,17 @@ instance weightedRefCount App where weightedRefCount dcl_functions common_defs depth {app_symb,app_args} rc_info # rc_info = weightedRefCount dcl_functions common_defs depth app_args rc_info - = check_import dcl_functions common_defs app_symb.symb_kind rc_info + = check_import dcl_functions common_defs app_symb rc_info where - check_import dcl_functions common_defs symb_kind=:(SK_Function {glob_module,glob_object}) rc_info=:{rc_imports, rc_var_heap} + check_import dcl_functions common_defs {symb_kind=SK_Function {glob_module,glob_object}} rc_info=:{rc_imports, rc_var_heap} = checkImportOfDclFunction dcl_functions common_defs glob_module glob_object rc_info - check_import dcl_functions common_defs symb_kind=:(SK_Constructor {glob_module,glob_object}) rc_info=:{rc_imports, rc_var_heap} + check_import dcl_functions common_defs {symb_name,symb_kind=symb_kind=:(SK_Constructor {glob_module,glob_object})} rc_info=:{rc_imports, rc_var_heap} | glob_module <> cIclModIndex # {cons_type_ptr} = common_defs.[glob_module].com_cons_defs.[glob_object] (rc_imports, rc_var_heap) = checkImportedSymbol symb_kind cons_type_ptr (rc_imports, rc_var_heap) = { rc_info & rc_imports = rc_imports, rc_var_heap = rc_var_heap } = rc_info - check_import dcl_functions common_defs symb_kind rc_info + check_import dcl_functions common_defs _ rc_info = rc_info @@ -1272,9 +1302,10 @@ where is_moved LES_Moved = True is_moved _ = False - distributeLets depth (DynamicExpr dynamik=:{dyn_expr}) dl_info +/* distributeLets depth (DynamicExpr dynamik=:{dyn_expr}) dl_info # (dyn_expr, dl_info) = distributeLets depth dyn_expr dl_info = (DynamicExpr { dynamik & dyn_expr = dyn_expr }, dl_info) +*/ distributeLets depth expr=:(TypeCodeExpression _) dl_info = (expr, dl_info) distributeLets depth (AnyCodeExpr in_params out_params code_expr) dl_info=:{di_var_heap} diff --git a/frontend/syntax.dcl b/frontend/syntax.dcl index 4339b75..944a0a2 100644 --- a/frontend/syntax.dcl +++ b/frontend/syntax.dcl @@ -478,7 +478,7 @@ cIsALocalVar :== False VI_ExpandedType !SymbolType | /* for storing the (expanded) type of an imported function */ VI_Record ![AuxiliaryPattern] | VI_Pattern !AuxiliaryPattern | - VI_Default !Int | /* used during conversion of dynamics; the Int indiacted the refenrence count */ + VI_Default !Int | VI_Indirection !Int | /* used during conversion of dynamics; the Int indiacted the refenrence count */ VI_Body !SymbIdent !TransformedBody ![FreeVar] | /* used during fusion */ VI_Dictionary !SymbIdent ![Expression] ![Type] | /* used during fusion */ VI_Extended !ExtendedVarInfo !VarInfo diff --git a/frontend/syntax.icl b/frontend/syntax.icl index 444c3b0..b25b345 100644 --- a/frontend/syntax.icl +++ b/frontend/syntax.icl @@ -434,7 +434,7 @@ cIsALocalVar :== False VI_ExpandedType !SymbolType | /* for storing the (expanded) type of an imported function */ VI_Record ![AuxiliaryPattern] | VI_Pattern !AuxiliaryPattern | - VI_Default !Int | /* used during conversion of dynamics; the Int indiacted the refenrence count */ + VI_Default !Int | VI_Indirection !Int | /* used during conversion of dynamics; the Int indiacted the refenrence count */ VI_Body !SymbIdent !TransformedBody ![FreeVar] | /* used during fusion */ VI_Dictionary !SymbIdent ![Expression] ![Type] | /* used during fusion */ VI_Extended !ExtendedVarInfo !VarInfo @@ -1332,7 +1332,7 @@ where // was (<<<) file (App {app_symb, app_args}) // = file <<< app_symb <<< ' ' <<< app_args (<<<) file (f_exp @ a_exp) = file <<< '(' <<< f_exp <<< " @ " <<< a_exp <<< ')' - (<<<) file (Let {let_binds, let_expr}) = write_binds (file <<< "let " <<< '\n') let_binds <<< "in\n" <<< let_expr + (<<<) file (Let {let_info_ptr, let_binds, let_expr}) = write_binds (file <<< "let " <<< ptrToInt let_info_ptr <<< '\n') let_binds <<< "in\n" <<< let_expr where write_binds file [] = file @@ -1516,7 +1516,7 @@ where instance <<< FreeVar where - (<<<) file {fv_name,fv_info_ptr} = file <<< fv_name <<< '<' <<< ptrToInt fv_info_ptr <<< '>' + (<<<) file {fv_name,fv_info_ptr,fv_count} = file <<< fv_name <<< '.' <<< fv_count <<< '<' <<< ptrToInt fv_info_ptr <<< '>' instance <<< DynamicType where diff --git a/frontend/trans.dcl b/frontend/trans.dcl index ac0dda9..1bed708 100644 --- a/frontend/trans.dcl +++ b/frontend/trans.dcl @@ -13,8 +13,9 @@ cAccumulating :== -3 analyseGroups :: !{# CommonDefs} !*{! Group} !*{#FunDef} !*VarHeap !*ExpressionHeap -> (!CleanupInfo, !*{! ConsClasses}, !*{! Group}, !*{#FunDef}, !*VarHeap, !*ExpressionHeap) -transformGroups :: !CleanupInfo !*{! Group} !*{#FunDef} !{!.ConsClasses} !{# CommonDefs} !{# {# FunType} } !*VarHeap !*TypeHeaps !*ExpressionHeap - -> (!*{! Group}, !*{#FunDef}, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) +transformGroups :: !CleanupInfo !*{! Group} !*{#FunDef} !{!.ConsClasses} !{# CommonDefs} !{# {# FunType} } + !*{#{# CheckedTypeDef}} !ImportedConstructors !*VarHeap !*TypeHeaps !*ExpressionHeap + -> (!*{! Group}, !*{#FunDef}, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) partitionateFunctions :: !*{# FunDef} ![IndexRange] -> (!*{! Group}, !*{# FunDef}) diff --git a/frontend/trans.icl b/frontend/trans.icl index 1212756..9c2dc76 100644 --- a/frontend/trans.icl +++ b/frontend/trans.icl @@ -224,16 +224,24 @@ instance consumerRequirements Expression where { ai & ai_next_var = new_next_var, ai_next_var_of_fun = new_ai_next_var_of_fun, ai_var_heap = ai_var_heap } = consumerRequirements let_expr common_defs ai // XXX why not not_an_unsafe_pattern where - init_variables [{bind_dst={fv_info_ptr}} : binds] ai_next_var ai_next_var_of_fun ai_var_heap - = init_variables binds (inc ai_next_var) (inc ai_next_var_of_fun) - (writePtr fv_info_ptr (VI_AccVar ai_next_var ai_next_var_of_fun) ai_var_heap) + init_variables [{bind_dst={fv_count, fv_info_ptr}} : binds] ai_next_var ai_next_var_of_fun ai_var_heap +/* Sjaak ... */ + | fv_count > 0 + = init_variables binds (inc ai_next_var) (inc ai_next_var_of_fun) + (writePtr fv_info_ptr (VI_AccVar ai_next_var ai_next_var_of_fun) ai_var_heap) + = init_variables binds ai_next_var ai_next_var_of_fun ai_var_heap +/* ... Sjaak */ init_variables [] ai_next_var ai_next_var_of_fun ai_var_heap = (ai_next_var, ai_next_var_of_fun, ai_var_heap) - acc_requirements_of_let_binds [ {bind_src, bind_dst={fv_info_ptr}} : binds ] ai_next_var common_defs ai - # (bind_var, _, ai) = consumerRequirements bind_src common_defs ai - ai_class_subst = unifyClassifications ai_next_var bind_var ai.ai_class_subst - = acc_requirements_of_let_binds binds (inc ai_next_var) common_defs { ai & ai_class_subst = ai_class_subst } + acc_requirements_of_let_binds [ {bind_src, bind_dst} : binds ] ai_next_var common_defs ai +/* Sjaak ... */ + | bind_dst.fv_count > 0 + # (bind_var, _, ai) = consumerRequirements bind_src common_defs ai + ai_class_subst = unifyClassifications ai_next_var bind_var ai.ai_class_subst + = acc_requirements_of_let_binds binds (inc ai_next_var) common_defs { ai & ai_class_subst = ai_class_subst } + = acc_requirements_of_let_binds binds ai_next_var common_defs ai +/* ... Sjaak */ acc_requirements_of_let_binds [] ai_next_var _ ai = ai @@ -412,9 +420,9 @@ instance consumerRequirements DynamicPattern where = consumerRequirements dp_rhs common_defs ai bindPatternVars [fv=:{fv_info_ptr,fv_count} : vars] next_var next_var_of_fun var_heap -// | fv_count > 0 + | fv_count > 0 = bindPatternVars vars (inc next_var) (inc next_var_of_fun) (writePtr fv_info_ptr (VI_AccVar next_var next_var_of_fun) var_heap) -// = bindPatternVars vars next_var next_var_of_fun (writePtr fv_info_ptr (VI_Count 0 False) var_heap) + = bindPatternVars vars next_var next_var_of_fun (writePtr fv_info_ptr (VI_Count 0 False) var_heap) bindPatternVars [] next_var next_var_of_fun var_heap = (next_var, next_var_of_fun, var_heap) @@ -1743,13 +1751,21 @@ where :: ImportedConstructors :== [Global Index] -transformGroups :: !CleanupInfo !*{! Group} !*{#FunDef} !{!.ConsClasses} !{# CommonDefs} !{# {# FunType} } !*VarHeap !*TypeHeaps !*ExpressionHeap - -> (!*{! Group}, !*{#FunDef}, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) -transformGroups cleanup_info groups fun_defs cons_args common_defs imported_funs var_heap type_heaps symbol_heap +/* Sjaak ... */ +// transformGroups :: !CleanupInfo !*{! Group} !*{#FunDef} !{!.ConsClasses} !{# CommonDefs} !{# {# FunType} } !*VarHeap !*TypeHeaps !*ExpressionHeap +// -> (!*{! Group}, !*{#FunDef}, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) + +transformGroups :: !CleanupInfo !*{! Group} !*{#FunDef} !{!.ConsClasses} !{# CommonDefs} !{# {# FunType} } + !*{#{# CheckedTypeDef}} !ImportedConstructors !*VarHeap !*TypeHeaps !*ExpressionHeap + -> (!*{! Group}, !*{#FunDef}, !*{#{# CheckedTypeDef}}, !ImportedConstructors, !*VarHeap, !*TypeHeaps, !*ExpressionHeap) +/* ... Sjaak */ + +// transformGroups cleanup_info groups fun_defs cons_args common_defs imported_funs var_heap type_heaps symbol_heap +transformGroups cleanup_info groups fun_defs cons_args common_defs imported_funs imported_types collected_imports var_heap type_heaps symbol_heap #! (nr_of_funs, fun_defs) = usize fun_defs - # imported_types = {com_type_defs \\ {com_type_defs} <-: common_defs } +// # imported_types = {com_type_defs \\ {com_type_defs} <-: common_defs } # (groups, imported_types, collected_imports, ti) - = transform_groups 0 groups common_defs imported_funs imported_types [] + = transform_groups 0 groups common_defs imported_funs imported_types collected_imports {ti_fun_defs = fun_defs, ti_instances = createArray nr_of_funs II_Empty, ti_cons_args = cons_args, ti_new_functions = [], ti_fun_heap = newHeap, ti_var_heap = var_heap, ti_symbol_heap = symbol_heap, ti_type_heaps = type_heaps, ti_next_fun_nr = nr_of_funs, ti_cleanup_info = cleanup_info, |