Skip to content

Assemble the defined instruction #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions lib/syntax_tree/yarv/assembler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,43 @@ def visit_string_literal(node)
end
end

CALLDATA_FLAGS = {
"ARGS_SPLAT" => CallData::CALL_ARGS_SPLAT,
"ARGS_BLOCKARG" => CallData::CALL_ARGS_BLOCKARG,
"FCALL" => CallData::CALL_FCALL,
"VCALL" => CallData::CALL_VCALL,
"ARGS_SIMPLE" => CallData::CALL_ARGS_SIMPLE,
"BLOCKISEQ" => CallData::CALL_BLOCKISEQ,
"KWARG" => CallData::CALL_KWARG,
"KW_SPLAT" => CallData::CALL_KW_SPLAT,
"TAILCALL" => CallData::CALL_TAILCALL,
"SUPER" => CallData::CALL_SUPER,
"ZSUPER" => CallData::CALL_ZSUPER,
"OPT_SEND" => CallData::CALL_OPT_SEND,
"KW_SPLAT_MUT" => CallData::CALL_KW_SPLAT_MUT
}.freeze

DEFINED_TYPES = [
nil,
"nil",
"instance-variable",
"local-variable",
"global-variable",
"class variable",
"constant",
"method",
"yield",
"super",
"self",
"true",
"false",
"assignment",
"expression",
"ref",
"func",
"constant-from"
].freeze

attr_reader :filepath

def initialize(filepath)
Expand Down Expand Up @@ -105,7 +142,12 @@ def assemble_iseq(iseq, lines)
assemble_iseq(class_iseq, body)
iseq.defineclass(name, class_iseq, flags)
when "defined"
raise NotImplementedError
type, object, message = operands.split(/,\s*/)
iseq.defined(
DEFINED_TYPES.index(type),
parse_symbol(object),
parse_string(message)
)
when "definemethod"
body = parse_nested(lines[line_index..])
line_index += body.length
Expand Down Expand Up @@ -158,12 +200,13 @@ def assemble_iseq(iseq, lines)
when "intern"
iseq.intern
when "invokeblock"
cdata = operands ? calldata(operands) : YARV.calldata(nil, 0)
iseq.invokeblock(cdata)
iseq.invokeblock(
operands ? parse_calldata(operands) : YARV.calldata(nil, 0)
)
when "invokesuper"
cdata =
calldata =
if operands
calldata(operands)
parse_calldata(operands)
else
YARV.calldata(
nil,
Expand All @@ -183,7 +226,7 @@ def assemble_iseq(iseq, lines)
block_iseq
end

iseq.invokesuper(cdata, block_iseq)
iseq.invokesuper(calldata, block_iseq)
when "jump"
iseq.jump(labels[operands])
when "leave"
Expand Down Expand Up @@ -282,7 +325,7 @@ def assemble_iseq(iseq, lines)
when "opt_reverse"
iseq.send(YARV.calldata(:reverse))
when "opt_send_without_block"
iseq.send(calldata(operands))
iseq.send(parse_calldata(operands))
when "opt_size"
iseq.send(YARV.calldata(:size))
when "opt_str_freeze"
Expand Down Expand Up @@ -316,7 +359,7 @@ def assemble_iseq(iseq, lines)
block_iseq
end

iseq.send(calldata(operands), block_iseq)
iseq.send(parse_calldata(operands), block_iseq)
when "setblockparam"
lookup = find_local(iseq, operands)
iseq.setblockparam(lookup.index, lookup.level)
Expand Down Expand Up @@ -400,23 +443,7 @@ def parse_nested(lines)
body.map! { |line| line.delete_prefix!(" ") || +"" }
end

CALLDATA_FLAGS = {
"ARGS_SPLAT" => CallData::CALL_ARGS_SPLAT,
"ARGS_BLOCKARG" => CallData::CALL_ARGS_BLOCKARG,
"FCALL" => CallData::CALL_FCALL,
"VCALL" => CallData::CALL_VCALL,
"ARGS_SIMPLE" => CallData::CALL_ARGS_SIMPLE,
"BLOCKISEQ" => CallData::CALL_BLOCKISEQ,
"KWARG" => CallData::CALL_KWARG,
"KW_SPLAT" => CallData::CALL_KW_SPLAT,
"TAILCALL" => CallData::CALL_TAILCALL,
"SUPER" => CallData::CALL_SUPER,
"ZSUPER" => CallData::CALL_ZSUPER,
"OPT_SEND" => CallData::CALL_OPT_SEND,
"KW_SPLAT_MUT" => CallData::CALL_KW_SPLAT_MUT
}.freeze

def calldata(value)
def parse_calldata(value)
message, argc_value, flags_value = value.split
flags =
if flags_value
Expand Down