Commit 2ac359fb by Stefan Marr

Added bytecode for multiply and subtract

Signed-off-by: Stefan Marr <git@stefan-marr.de>
1 parent 66acd12d
......@@ -45,8 +45,13 @@ class BytecodeGenerator(object):
self._emit2(mgenc, BC.send, mgenc.find_literal_index(msg))
def emitQUICKSEND(self, mgenc, msg):
if msg.get_string() == "+":
m = msg.get_string()
if m == "+":
self._emit1(mgenc, BC.add)
elif m == "*":
self._emit1(mgenc, BC.multiply)
elif m == "-":
self._emit1(mgenc, BC.subtract)
else:
raise RuntimeError("Unsupported msg: " + str(msg))
......
......@@ -448,7 +448,8 @@ class Parser(object):
@staticmethod
def _is_quick_send(msg):
return msg.get_string() == "+"
m = msg.get_string()
return m == "+" or m == "-" or m == "*"
def _binary_message(self, mgenc, is_super_send):
msg = self._binary_selector()
......
......@@ -24,8 +24,10 @@ class Bytecodes(object):
# quick sends, short cutting well known operations
add = 16
multiply = 17
subtract = 18
_num_bytecodes = 17
_num_bytecodes = 19
_bytecode_length = [ 1, # halt
1, # dup
......@@ -45,6 +47,8 @@ class Bytecodes(object):
1, # return_non_local
1, # add
1, # multiply
1, # subtract
]
_stack_effect_depends_on_message = -1000 # chose a unresonable number to be recognizable
......@@ -63,9 +67,12 @@ class Bytecodes(object):
-1, # pop_field
_stack_effect_depends_on_message, # send
_stack_effect_depends_on_message, # super_send
0, # return_local
0,
-1] # return_non_local
0, # return_local
0, # return_non_local
-1, # add
-1, # multiply
-1, # subtract
]
@jit.elidable
def bytecode_length(bytecode):
......
......@@ -13,10 +13,14 @@ class Interpreter(object):
def __init__(self, universe):
self._universe = universe
self._add_symbol = None
self._add_symbol = None
self._multiply_symbol = None
self._subtract_symbol = None
def initialize_known_quick_sends(self):
self._add_symbol = self._universe.symbol_for("+")
self._add_symbol = self._universe.symbol_for("+")
self._multiply_symbol = self._universe.symbol_for("*")
self._subtract_symbol = self._universe.symbol_for("-")
def get_universe(self):
return self._universe
......@@ -153,6 +157,34 @@ class Interpreter(object):
self._send(method, frame, self._add_symbol,
rcvr.get_class(self._universe), 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)
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)
def _do_send(self, bytecode_index, frame, method):
# Handle the send bytecode
signature = method.get_constant(bytecode_index)
......@@ -225,6 +257,10 @@ class Interpreter(object):
return self._do_return_non_local(frame)
elif bytecode == Bytecodes.add:
self._do_add(current_bc_idx, frame, method)
elif bytecode == Bytecodes.multiply:
self._do_multiply(current_bc_idx, frame, method)
elif bytecode == Bytecodes.subtract:
self._do_subtract(current_bc_idx, frame, method)
current_bc_idx = next_bc_idx
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!