Commit f5b18e4d by Stefan Marr

Use polymorphism on AbstractObject for quick bytecodes

Signed-off-by: Stefan Marr <git@stefan-marr.de>
1 parent 402ca7e4
......@@ -145,45 +145,15 @@ class Interpreter(object):
def _do_add(self, bytecode_index, frame, method):
rcvr = frame.get_stack_element(1)
right = frame.get_stack_element(0)
if (isinstance(rcvr, Integer) or
isinstance(rcvr, BigInteger) or
isinstance(rcvr, Double)):
frame.pop()
frame.pop()
frame.push(rcvr.prim_add(right, self._universe))
else:
self._send(method, frame, self._add_symbol,
rcvr.get_class(self._universe), bytecode_index)
rcvr.quick_add(method, frame, self, bytecode_index)
def _do_multiply(self, bytecode_index, frame, method):
rcvr = frame.get_stack_element(1)
right = frame.get_stack_element(0)
if (isinstance(rcvr, Integer) or
isinstance(rcvr, BigInteger) or
isinstance(rcvr, Double)):
frame.pop()
frame.pop()
frame.push(rcvr.prim_multiply(right, self._universe))
else:
self._send(method, frame, self._multiply_symbol,
rcvr.get_class(self._universe), bytecode_index)
rcvr.quick_multiply(method, frame, self, bytecode_index)
def _do_subtract(self, bytecode_index, frame, method):
rcvr = frame.get_stack_element(1)
right = frame.get_stack_element(0)
if (isinstance(rcvr, Integer) or
isinstance(rcvr, BigInteger) or
isinstance(rcvr, Double)):
frame.pop()
frame.pop()
frame.push(rcvr.prim_subtract(right, self._universe))
else:
self._send(method, frame, self._subtract_symbol,
rcvr.get_class(self._universe), bytecode_index)
rcvr.quick_subtract(method, frame, self, bytecode_index)
def _do_send(self, bytecode_index, frame, method):
# Handle the send bytecode
......@@ -272,6 +242,8 @@ class Interpreter(object):
return frame.get_outer_context().get_argument(0, 0)
def _send(self, m, frame, selector, receiver_class, bytecode_index):
# selector.inc_send_count()
# First try the inline cache
cached_class = m.get_inline_cache_class(bytecode_index)
if cached_class == receiver_class:
......
......@@ -50,6 +50,21 @@ class AbstractObject(object):
def get_class(self, universe):
raise NotImplementedError("Subclasses need to implement get_class(universe).")
def quick_add(self, from_method, frame, interpreter, bytecode_index):
interpreter._send(from_method, frame, interpreter._add_symbol,
self.get_class(interpreter.get_universe()),
bytecode_index)
def quick_multiply(self, from_method, frame, interpreter, bytecode_index):
interpreter._send(from_method, frame, interpreter._multiply_symbol,
self.get_class(interpreter.get_universe()),
bytecode_index)
def quick_subtract(self, from_method, frame, interpreter, bytecode_index):
interpreter._send(from_method, frame, interpreter._subtract_symbol,
self.get_class(interpreter.get_universe()),
bytecode_index)
@staticmethod
def is_invokable():
return False
......
......@@ -18,6 +18,24 @@ class BigInteger(AbstractObject):
def get_class(self, universe):
return universe.integerClass
def quick_add(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_add(right, interpreter.get_universe()))
def quick_multiply(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_multiply(right, interpreter.get_universe()))
def quick_subtract(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_subtract(right, interpreter.get_universe()))
def _to_double(self, universe):
return universe.new_double(self._embedded_biginteger.tofloat())
......
......@@ -19,6 +19,24 @@ class Double(AbstractObject):
def get_class(self, universe):
return universe.doubleClass
def quick_add(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_add(right, interpreter.get_universe()))
def quick_multiply(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_multiply(right, interpreter.get_universe()))
def quick_subtract(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_subtract(right, interpreter.get_universe()))
@staticmethod
def _get_float(obj):
from .integer import Integer
......
......@@ -23,6 +23,24 @@ class Integer(AbstractObject):
def get_class(self, universe):
return universe.integerClass
def quick_add(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_add(right, interpreter.get_universe()))
def quick_multiply(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_multiply(right, interpreter.get_universe()))
def quick_subtract(self, from_method, frame, interpreter, bytecode_index):
right = frame.get_stack_element(0)
frame.pop()
frame.pop()
frame.push(self.prim_subtract(right, interpreter.get_universe()))
def _to_double(self, universe):
return universe.new_double(float(self._embedded_integer))
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!