[m-rev.] diff: fix another dummy variables bug in erlang backend

Peter Wang wangp at students.csse.unimelb.edu.au
Fri Jul 20 17:07:18 AEST 2007


Estimated hours taken: 1
Branches: main

Fix yet another bug with dummy variables in the Erlang backend.

compiler/erl_code_util.m:
compiler/erl_unify_gen.m:
	Move var_or_dummy_replacement out of erl_unify_gen.m
	and into erl_code_util.m as erl_var_or_dummy_replacement.

compiler/erl_call_gen.m:
	Replace dummy variables which appear in comparisons using builtin
	operators with `false', since they probably don't exist in the Erlang
	source.

[cvs diff -b]

Index: compiler/erl_call_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_call_gen.m,v
retrieving revision 1.9
diff -u -b -r1.9 erl_call_gen.m
--- compiler/erl_call_gen.m	9 Jul 2007 04:48:43 -0000	1.9
+++ compiler/erl_call_gen.m	20 Jul 2007 07:00:41 -0000
@@ -397,6 +397,7 @@
 erl_gen_builtin(PredId, ProcId, ArgVars, CodeModel, _Context,
         MaybeSuccessExpr, Statement, !Info) :-
     erl_gen_info_get_module_info(!.Info, ModuleInfo),
+    erl_gen_info_get_var_types(!.Info, VarTypes),
     ModuleName = predicate_module(ModuleInfo, PredId),
     PredName = predicate_name(ModuleInfo, PredId),
     (
@@ -414,12 +415,12 @@
             (
                 % We need to avoid generating assignments to dummy variables
                 % introduced for types such as io.state.
-                erl_variable_type(!.Info, Lval, LvalType),
+                map.lookup(VarTypes, Lval, LvalType),
                 is_dummy_argument_type(ModuleInfo, LvalType)
             ->
                 Statement = expr_or_void(MaybeSuccessExpr)
             ;
-                Rval = erl_gen_simple_expr(SimpleExpr),
+                Rval = erl_gen_simple_expr(ModuleInfo, VarTypes, SimpleExpr),
                 Assign = elds.elds_eq(elds.expr_from_var(Lval), Rval),
                 Statement = maybe_join_exprs(Assign, MaybeSuccessExpr)
             )
@@ -437,7 +438,7 @@
         CodeModel = model_semi,
         (
             SimpleCode = test(SimpleTest),
-            TestRval = erl_gen_simple_expr(SimpleTest),
+            TestRval = erl_gen_simple_expr(ModuleInfo, VarTypes, SimpleTest),
             % Unlike Mercury procedures, the builtin tests return true and
             % false instead of {} and fail.
             Statement = elds_case_expr(TestRval, [TrueCase, FalseCase]),
@@ -458,25 +459,39 @@
         unexpected(this_file, "model_non builtin predicate")
     ).
 
-:- func erl_gen_simple_expr(simple_expr(prog_var)) = elds_expr.
+:- func erl_gen_simple_expr(module_info, vartypes, simple_expr(prog_var)) =
+    elds_expr.
 
-erl_gen_simple_expr(leaf(Var)) = elds.expr_from_var(Var).
-erl_gen_simple_expr(int_const(Int)) = elds_term(elds_int(Int)).
-erl_gen_simple_expr(float_const(Float)) = elds_term(elds_float(Float)).
-erl_gen_simple_expr(unary(StdOp, Expr0)) = Expr :-
+erl_gen_simple_expr(ModuleInfo, VarTypes, SimpleExpr) = Expr :-
+    (
+        SimpleExpr = leaf(Var),
+        % Variables of dummy types need to be replaced since they probably
+        % don't exist.
+        Expr = erl_var_or_dummy_replacement(ModuleInfo, VarTypes, elds_false,
+            Var)
+    ;
+        SimpleExpr = int_const(Int),
+        Expr = elds_term(elds_int(Int))
+    ;
+        SimpleExpr = float_const(Float),
+        Expr = elds_term(elds_float(Float))
+    ;
+        SimpleExpr = unary(StdOp, Expr0),
     ( std_unop_to_elds(StdOp, Op) ->
-        SimpleExpr = erl_gen_simple_expr(Expr0),
-        Expr = elds_unop(Op, SimpleExpr)
+            SimpleExpr1 = erl_gen_simple_expr(ModuleInfo, VarTypes, Expr0),
+            Expr = elds_unop(Op, SimpleExpr1)
     ;
         sorry(this_file, "unary builtin not supported on erlang target")
-    ).
-erl_gen_simple_expr(binary(StdOp, Expr1, Expr2)) = Expr :-
+        )
+    ;
+        SimpleExpr = binary(StdOp, Expr1, Expr2),
     ( std_binop_to_elds(StdOp, Op) ->
-        SimpleExpr1 = erl_gen_simple_expr(Expr1),
-        SimpleExpr2 = erl_gen_simple_expr(Expr2),
+            SimpleExpr1 = erl_gen_simple_expr(ModuleInfo, VarTypes, Expr1),
+            SimpleExpr2 = erl_gen_simple_expr(ModuleInfo, VarTypes, Expr2),
         Expr = elds_binop(Op, SimpleExpr1, SimpleExpr2)
     ;
         sorry(this_file, "binary builtin not supported on erlang target")
+        )
     ).
 
 :- pred std_unop_to_elds(unary_op::in, elds_unop::out) is semidet.
Index: compiler/erl_code_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_util.m,v
retrieving revision 1.8
diff -u -b -r1.8 erl_code_util.m
--- compiler/erl_code_util.m	14 Jun 2007 03:52:48 -0000	1.8
+++ compiler/erl_code_util.m	20 Jul 2007 07:00:41 -0000
@@ -149,6 +149,14 @@
 :- pred erl_bind_unbound_vars(erl_gen_info::in, set(prog_var)::in,
     hlds_goal::in, instmap::in, elds_expr::in, elds_expr::out) is det.
 
+    % erl_var_or_dummy_replacement(ModuleInfo, VarTypes, DummyRepl, Var) = Expr
+    %
+    % Return DummyRepl if Var is of a dummy type, otherwise return
+    % Var.
+    %
+:- func erl_var_or_dummy_replacement(module_info, vartypes, elds_term,
+    prog_var) = elds_expr.
+
     % erl_create_renaming(Vars, Subst, !Info):
     %
     % Create a substitution for each variable in Vars to a fresh variable.
@@ -407,6 +415,18 @@
 
 %-----------------------------------------------------------------------------%
 
+erl_var_or_dummy_replacement(ModuleInfo, VarTypes, DummyVarReplacement, Var) =
+    (if
+        map.search(VarTypes, Var, Type),
+        is_dummy_argument_type(ModuleInfo, Type)
+    then
+        elds_term(DummyVarReplacement)
+    else
+        expr_from_var(Var)
+    ).
+
+%-----------------------------------------------------------------------------%
+
 erl_create_renaming(Vars, Subst, !Info) :-
     erl_gen_info_get_varset(!.Info, VarSet0),
     list.foldl2(erl_create_renaming_2, Vars, VarSet0, VarSet, map.init, Subst),
Index: compiler/erl_unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_unify_gen.m,v
retrieving revision 1.8
diff -u -b -r1.8 erl_unify_gen.m
--- compiler/erl_unify_gen.m	9 Jul 2007 04:48:44 -0000	1.8
+++ compiler/erl_unify_gen.m	20 Jul 2007 07:00:41 -0000
@@ -257,8 +257,8 @@
             % unifications we would want to replace them with `false' (what
             % we use for all dummy values).  In deconstructions we replace
             % them by anonymous variables (_).
-            TermArgs = list.map(var_or_dummy_replacement(ModuleInfo, VarTypes,
-                DummyVarReplacement), Args),
+            TermArgs = list.map(erl_var_or_dummy_replacement(ModuleInfo,
+                VarTypes, DummyVarReplacement), Args),
             Term = elds_tuple([Functor | TermArgs])
         )
     ;
@@ -272,19 +272,6 @@
         Term = elds_float(Float)
     ).
 
-:- func var_or_dummy_replacement(module_info, vartypes, elds_term, prog_var) =
-    elds_expr.
-
-var_or_dummy_replacement(ModuleInfo, VarTypes, DummyVarReplacement, Var) =
-    (if
-        map.search(VarTypes, Var, Type),
-        is_dummy_argument_type(ModuleInfo, Type)
-    then
-        elds_term(DummyVarReplacement)
-    else
-        expr_from_var(Var)
-    ).
-
 cons_id_to_expr(ConsId, Args, DummyVarReplacement, Expr, !Info) :-
     (
         ( ConsId = cons(_, _)
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list