Commits

Carl Friedrich Bolz  committed c2743d4

wildly starting to refactor: introduce InStorageIntShape that stores ints by
tagging them.

  • Participants
  • Parent commits fcc9861
  • Branches type-tag-terms

Comments (0)

Files changed (3)

File prolog/interpreter/shape.py

-from pypy.rlib import jit, objectmodel, debug, unroll
+from pypy.rlib import jit, objectmodel, debug, unroll, rerased
 from prolog.interpreter import term
 from prolog.interpreter.graphviz import _dot, view
 # a Callable implementation that tries to save memory
 
+erase, unerase = rerased.new_erasing_pair("pyrolog")
+
 # XXX tune this
 MAX_DEPTH = 6
 MAX_SIZE = 6
         return InStorageShape._singleton
 
     def resolve(self, shaped_callable, index):
-        return shaped_callable.get_storage(index)
+        return unerase(shaped_callable.get_raw_storage(index))
 
     def num_storage_vars(self):
         return 1
 
     def str(self):
         return "InStorageShape()"
+InStorageShape._singleton = InStorageShape()
 
-InStorageShape._singleton = InStorageShape()
+class InStorageIntShape(InStorageShape):
+    @staticmethod
+    def build():
+        return InStorageIntShape._singleton
+
+    def resolve(self, shaped_callable, index):
+        val = rerased.unerase_int(shaped_callable.get_raw_storage(index))
+        return term.Number(val)
+
+    def str(self):
+        return "InStorageIntShape()"
+InStorageIntShape._singleton = InStorageIntShape()
+
 
 def shape_eq((sig1, children1), (sig2, children2)):
     return sig1 is sig2 and children1 == children2

File prolog/interpreter/test/test_shape.py

     def __init__(self, l):
         self.storage = l
 
-    def get_storage(self, i):
+    def get_raw_storage(self, i):
         return self.storage[i]
 
+def erased_storage(l):
+    return FakeShapedCallable([shape.erase(o) for o in l])
+
 def test_instorage_build():
     assert shape.InStorageShape.build() is shape.InStorageShape.build()
 
 
 def test_wrapshape_resolve():
     s = shape.WrapShape(term.Callable.build("a", [term.Number(1)]))
-    w_obj = s.resolve(FakeShapedCallable([1, 2, 3]), 0)
+    w_obj = s.resolve(erased_storage([1, 2, 3]), 0)
     assert w_obj.name() == "a"
     assert w_obj.argument_at(0).num == 1
 
 
 def test_instorage_resolve():
     s = shape.InStorageShape()
-    assert s.resolve(FakeShapedCallable([1, 2, 3]), 0) == 1
-    assert s.resolve(FakeShapedCallable([1, 2, 3]), 1) == 2
-    assert s.resolve(FakeShapedCallable([1, 2, 3]), 2) == 3
+    assert s.resolve(erased_storage([1, 2, 3]), 0) == 1
+    assert s.resolve(erased_storage([1, 2, 3]), 1) == 2
+    assert s.resolve(erased_storage([1, 2, 3]), 2) == 3
 
+def test_instorage_int_resolve():
+    s = shape.InStorageIntShape()
+    assert s.resolve(FakeShapedCallable([shape.rerased.erase_int(1)]), 0).num == 1
 
 def test_sharing_resolve():
     sig = signature.Signature.getsignature("f", 2)
         shape.WrapShape(term.Callable.build("a")),
         shape.InStorageShape()
     ])
-    assert s.resolve_at(0, FakeShapedCallable([1, 2])).name() == "a"
-    assert s.resolve_at(1, FakeShapedCallable([1, 2])) == 1
+    assert s.resolve_at(0, erased_storage([1, 2])).name() == "a"
+    assert s.resolve_at(1, erased_storage([1, 2])) == 1
 
     w_obj = s.resolve(shape.ShapedCallable(s, [2]), 0)
     assert isinstance(w_obj, shape.ShapedCallable)

File prolog/jittest/interpjit.py

         app([], X, X).
         app([H | T1], T2, [H | T3]) :-
             app(T1, T2, T3).
+        infloop([1 | T]) :- infloop(T).
+        infloop2(In, Out) :- infloop2([1 | In], Out).
         loop(0, []).
-        loop(X, [H|T]) :- X > 0, X0 is X - 1, loop(X0, T).
+        loop(X, [X0|T]) :- X > 0, X0 is X - 1, loop(X0, T).
+        loop_crazy(0, []).
+        loop_crazy(X, [Y|T]) :- X > 0, X0 is X - 1, Y is (37 * X0) mod 97, loop_crazy(X0, T).
         loop1(0, []).
         loop1(N, [H|T]) :- N > 0, N1 is N - 1, !, loop1(N1, T).
         loop1(N, [H|T]) :- N > 0, N1 is N - 1, loop1(N1, T).
         )
 
         t1 = parse_query_term("app([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [8, 9], X), X == [1, 2, 3, 4, 5, 6, 8, 9].")
-        #t2 = parse_query_term("loop_when(100).")
-        t2 = parse_query_term("freeze_list(15, T).")
+        t2 = parse_query_term("loop(1000, T), append(T, T, T1).")
+        t2 = parse_query_term("loop(1000, T), map(add1, T, T1).")
+        t2 = parse_query_term("loop(1000, T), reverse(T, [], T1).")
+        #t2 = parse_query_term("infloop(_).")
+        #t2 = parse_query_term("infloop2([], _).")
+        #t2 = parse_query_term("freeze_list(15, T).")
         t3 = parse_query_term("reverse([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [], X), X == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1].")
-        t4 = parse_query_term("run(app([1, 2, 3, 4, 5, 6, 7], [8, 9], X)), X == [1, 2, 3, 4, 5, 6, 7, 8, 9].")
+        t4 = parse_query_term("run(nrev(%s, Y))." % range(100))
+        t4 = parse_query_term("nrev(%s, Y)." % range(100))
         t5 = parse_query_term("map(add1, [1, 2, 3, 4, 5, 6, 7], X), X == [2, 3, 4, 5, 6, 7, 8].")
         t6 = parse_query_term("partition([6, 6, 6, 6, 6, 6, 66, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 106, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10, 3, 6, 1, 2, 6, 8, 9, 0,4, 2, 5, 1, 10], 5, X, Y).")
+        t6 = parse_query_term("loop_crazy(1000, X), partition(X, 50, _, _).")
+        t6 = parse_query_term("loop(1000, X), partition(X, 500, _, _).")
         t7 = parse_query_term("findall(X+Y, app([X|_], [Y|_], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), L).")
         def interp_w(c):
             jitdriver.set_param("inlining", True)
+            jitdriver.set_param("threshold", 10)
             if c == 1:
                 t = t1
             elif c == 2:
         # XXX
         #interp_w(2)
 
-        self.meta_interp(interp_w, [1], listcomp=True, backendopt=True,
+        self.meta_interp(interp_w, [4], listcomp=True, backendopt=True,
                          listops=True)
         #self.meta_interp(interp_w, [3], listcomp=True,
         #                 listops=True)