From: Jean Privat Date: Mon, 27 Jul 2009 06:59:57 +0000 (-0400) Subject: icode: discard unused pure ICode expressions X-Git-Tag: v0.3~118 X-Git-Url: http://nitlanguage.org icode: discard unused pure ICode expressions Up to now, only unused IMove expressions where discarded. So we also distinguish pure ICode from non pure ICode. Signed-off-by: Jean Privat --- diff --git a/src/compiling/compiling_icode.nit b/src/compiling/compiling_icode.nit index 847280b..97514ac 100644 --- a/src/compiling/compiling_icode.nit +++ b/src/compiling/compiling_icode.nit @@ -358,7 +358,9 @@ redef class ICode if r != null and r.slot_index != null then assert s != null v.add_assignment(v.register(r), s) - else if s != null and not self isa IMove then + else if s != null and not is_pure then + # ICode with side effects must be evaluated + # even if the result is not wanted v.add_instr(s + ";") end end diff --git a/src/icode/icode_base.nit b/src/icode/icode_base.nit index f46ff5b..6a822e3 100644 --- a/src/icode/icode_base.nit +++ b/src/icode/icode_base.nit @@ -88,6 +88,9 @@ abstract class ICode # The location of the icode (if any) readable writable var _location: nullable Location = null + + # Is the icode side effect free? + fun is_pure: Bool do return false end # An icode that uses no registers (no args) @@ -274,6 +277,8 @@ special ICodeN super(e) _code = c end + + redef readable writable var _is_pure: Bool = false end # A register assigment @@ -286,6 +291,8 @@ special ICode1 super(e) _result = r end + + redef fun is_pure do return true end # An attribute read access @@ -300,6 +307,8 @@ special ICode1 super(r) _property = p end + + redef fun is_pure do return true end # An attribute assignment @@ -329,6 +338,8 @@ special ICode1 super(r) _property = p end + + redef fun is_pure do return true end # A type check @@ -343,6 +354,8 @@ special ICode1 super(e) _stype = t end + + redef fun is_pure do return true end # The 'is' operator @@ -353,6 +366,8 @@ special ICode2 do super end + + redef fun is_pure do return true end # The unary 'not' operation @@ -363,6 +378,8 @@ special ICode1 do super end + + redef fun is_pure do return true end # Evaluate body once them return the same value again and again @@ -383,6 +400,8 @@ special ICode0 do _closure_decl = c end + + redef fun is_pure do return true end ################################################# diff --git a/src/icode/icode_builder.nit b/src/icode/icode_builder.nit index dbb26bd..88c3796 100644 --- a/src/icode/icode_builder.nit +++ b/src/icode/icode_builder.nit @@ -169,6 +169,7 @@ class ICodeBuilder fun lit_true_reg: IRegister do var e = new INative("TAG_Bool(true)", null) + e.is_pure = true return expr(e, module.type_bool) end @@ -176,6 +177,7 @@ class ICodeBuilder fun lit_false_reg: IRegister do var e = new INative("TAG_Bool(false)", null) + e.is_pure = true return expr(e, module.type_bool) end diff --git a/src/icode/icode_tools.nit b/src/icode/icode_tools.nit index efdc0b7..5186233 100644 --- a/src/icode/icode_tools.nit +++ b/src/icode/icode_tools.nit @@ -258,7 +258,9 @@ end redef class INative redef fun inner_dup_with(d) do - return new INative(code, d.dup_iregs(exprs)) + var c2 = new INative(code, d.dup_iregs(exprs)) + c2.is_pure = is_pure + return c2 end end