Skip to content

Commit

Permalink
Remove specialized bytecodes
Browse files Browse the repository at this point in the history
This also disables optimizations for trivial methods, which I won’t adapt to work, because we probably want to remove those optimizations too from this branch.

Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Dec 5, 2023
1 parent 228f028 commit 32ee2e2
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 1,144 deletions.
34 changes: 2 additions & 32 deletions src/som/compiler/ast/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,14 @@ def get_initialized_write_node(self, context_level, value_expr, source_section):
return LocalInnerVarWriteNode(self.access_idx, value_expr, source_section)
return LocalFrameVarWriteNode(self.access_idx, value_expr, source_section)

def get_push_bytecode(self, ctx_level):
def get_push_bytecode(self, ctx_level): # pylint: disable=unused-argument
if self.is_accessed_out_of_context():
if ctx_level == 0:
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 0:
return Bytecodes.push_inner_0
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 1:
return Bytecodes.push_inner_1
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 2:
return Bytecodes.push_inner_2
return Bytecodes.push_inner

if ctx_level == 0:
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 0:
return Bytecodes.push_frame_0
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 1:
return Bytecodes.push_frame_1
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 2:
return Bytecodes.push_frame_2
return Bytecodes.push_frame

def get_pop_bytecode(self, ctx_level):
def get_pop_bytecode(self, ctx_level): # pylint: disable=unused-argument
if self.is_accessed_out_of_context():
if ctx_level == 0:
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 0:
return Bytecodes.pop_inner_0
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 1:
return Bytecodes.pop_inner_1
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 2:
return Bytecodes.pop_inner_2
return Bytecodes.pop_inner

if ctx_level == 0:
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 0:
return Bytecodes.pop_frame_0
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 1:
return Bytecodes.pop_frame_1
if self.access_idx == FRAME_AND_INNER_RCVR_IDX + 2:
return Bytecodes.pop_frame_2
return Bytecodes.pop_frame

def get_qualified_name(self):
Expand Down
102 changes: 5 additions & 97 deletions src/som/compiler/bc/bytecode_generator.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
from som.interpreter.bc.bytecodes import Bytecodes as BC
from som.vm.globals import nilObject, trueObject, falseObject
from som.vm.symbols import sym_nil, sym_true, sym_false


def emit_inc(mgenc):
emit1(mgenc, BC.inc, 0)


def emit_dec(mgenc):
emit1(mgenc, BC.dec, 0)


def emit_inc_field_push(mgenc, field_idx, ctx_level):
emit3(mgenc, BC.inc_field_push, field_idx, ctx_level, 1)


def emit_pop(mgenc):
Expand All @@ -24,35 +10,14 @@ def emit_push_argument(mgenc, idx, ctx):
emit3(mgenc, BC.push_argument, idx, ctx, 1)


def emit_return_self(mgenc):
mgenc.optimize_dup_pop_pop_sequence()
emit1(mgenc, BC.return_self, 0)


def emit_return_local(mgenc):
if not mgenc.optimize_return_field():
emit1(mgenc, BC.return_local, 0)
emit1(mgenc, BC.return_local, 0)


def emit_return_non_local(mgenc):
emit2(mgenc, BC.return_non_local, mgenc.get_max_context_level(), 0)


def emit_return_field(mgenc, field_idx):
if field_idx == 0:
emit1(mgenc, BC.return_field_0, 0)
return
if field_idx == 1:
emit1(mgenc, BC.return_field_1, 0)
return
if field_idx == 2:
emit1(mgenc, BC.return_field_2, 0)
return
raise NotImplementedError(
"Don't support fields with index > 2, but got " + str(field_idx)
)


def emit_dup(mgenc):
emit1(mgenc, BC.dup, 1)

Expand All @@ -73,29 +38,13 @@ def emit_push_field(mgenc, field_name):
emit_push_field_with_index(mgenc, field_idx, ctx_level)


def emit_push_field_with_index(mgenc, field_idx, ctx_level):
if ctx_level == 0:
if field_idx == 0:
emit1(mgenc, BC.push_field_0, 1)
return
if field_idx == 1:
emit1(mgenc, BC.push_field_1, 1)
return

def emit_push_field_with_index(
mgenc, field_idx, ctx_level # pylint: disable=unused-argument
):
emit3(mgenc, BC.push_field, field_idx, mgenc.get_max_context_level(), 1)


def emit_push_global(mgenc, glob):
if glob is sym_nil:
emit_push_constant(mgenc, nilObject)
return
if glob is sym_true:
emit_push_constant(mgenc, trueObject)
return
if glob is sym_false:
emit_push_constant(mgenc, falseObject)
return

idx = mgenc.add_literal_if_absent(glob)
# the block needs to be able to send #unknownGlobal: to self
if not mgenc.is_global_known(glob):
Expand All @@ -115,20 +64,10 @@ def emit_pop_field(mgenc, field_name):
ctx_level = mgenc.get_max_context_level()
field_idx = mgenc.get_field_index(field_name)

if mgenc.optimize_inc_field(field_idx, ctx_level):
return

emit_pop_field_with_index(mgenc, field_idx, ctx_level)


def emit_pop_field_with_index(mgenc, field_idx, ctx_level):
if ctx_level == 0:
if field_idx == 0:
emit1(mgenc, BC.pop_field_0, -1)
return
if field_idx == 1:
emit1(mgenc, BC.pop_field_1, -1)
return
emit3(mgenc, BC.pop_field, field_idx, ctx_level, -1)


Expand All @@ -143,42 +82,11 @@ def emit_send(mgenc, msg):
num_args = msg.get_number_of_signature_arguments()
stack_effect = -num_args + 1 # +1 for the result

if num_args == 1:
emit2(mgenc, BC.send_1, idx, stack_effect)
elif num_args == 2:
emit2(mgenc, BC.send_2, idx, stack_effect)
elif num_args == 3:
emit2(mgenc, BC.send_3, idx, stack_effect)
else:
emit2(mgenc, BC.send_n, idx, stack_effect)
emit2(mgenc, BC.send_n, idx, stack_effect)


def emit_push_constant(mgenc, lit):
from som.vmobjects.integer import Integer

if isinstance(lit, Integer):
if lit.get_embedded_integer() == 0:
emit1(mgenc, BC.push_0, 1)
return
if lit.get_embedded_integer() == 1:
emit1(mgenc, BC.push_1, 1)
return

if lit is nilObject:
emit1(mgenc, BC.push_nil, 1)
return

idx = mgenc.add_literal_if_absent(lit)
if idx == 0:
emit1(mgenc, BC.push_constant_0, 1)
return
if idx == 1:
emit1(mgenc, BC.push_constant_1, 1)
return
if idx == 2:
emit1(mgenc, BC.push_constant_2, 1)
return

emit2(mgenc, BC.push_constant, idx, 1)


Expand Down
30 changes: 1 addition & 29 deletions src/som/compiler/bc/disassembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,7 @@ def dump_bytecode(m, b, indent=""):
+ ", context: "
+ str(m.get_bytecode(b + 2))
)
elif (
bytecode == Bytecodes.push_frame_0
or bytecode == Bytecodes.push_frame_1
or bytecode == Bytecodes.push_frame_2
or bytecode == Bytecodes.pop_frame_0
or bytecode == Bytecodes.pop_frame_1
or bytecode == Bytecodes.pop_frame_2
or bytecode == Bytecodes.push_inner_0
or bytecode == Bytecodes.push_inner_1
or bytecode == Bytecodes.push_inner_2
or bytecode == Bytecodes.pop_inner_0
or bytecode == Bytecodes.pop_inner_1
or bytecode == Bytecodes.pop_inner_2
):
# don't need any other arguments
error_println("")
elif (
bytecode == Bytecodes.push_field
or bytecode == Bytecodes.pop_field
or bytecode == Bytecodes.inc_field_push
or bytecode == Bytecodes.inc_field
):
elif bytecode == Bytecodes.push_field or bytecode == Bytecodes.pop_field:
if m.get_holder():
field_name = str(
m.get_holder().get_instance_field_name(m.get_bytecode(b + 1))
Expand Down Expand Up @@ -151,15 +130,8 @@ def dump_bytecode(m, b, indent=""):
+ str(m.get_constant(b))
)
elif bytecode in (
Bytecodes.send_1,
Bytecodes.send_2,
Bytecodes.send_3,
Bytecodes.send_n,
Bytecodes.super_send,
Bytecodes.q_super_send_1,
Bytecodes.q_super_send_2,
Bytecodes.q_super_send_3,
Bytecodes.q_super_send_n,
):
error_println(
"(index: "
Expand Down
Loading

0 comments on commit 32ee2e2

Please sign in to comment.