# Copyright (C) 2018-2019 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
require_relative 'Argument'
require_relative 'Fits'
require_relative 'Metadata'
class Opcode
attr_reader :id
attr_reader :args
attr_reader :metadata
attr_reader :extras
attr_reader :checkpoints
module Size
Narrow = "OpcodeSize::Narrow"
Wide16 = "OpcodeSize::Wide16"
Wide32 = "OpcodeSize::Wide32"
end
@@id = 0
def self.id
tid = @@id
@@id = @@id + 1
tid
end
def initialize(section, name, extras, args, metadata, metadata_initializers, tmps, checkpoints)
@section = section
@name = name
@extras = extras || {}
@metadata = Metadata.new metadata, metadata_initializers
@args = args.map.with_index { |(arg_name, type), index| Argument.new arg_name, type, index } unless args.nil?
@tmps = tmps
@checkpoints = checkpoints.map { |(checkpoint, _)| checkpoint } unless checkpoints.nil?
end
def create_id!
@id = self.class.id
end
def print_args(&block)
return if @args.nil?
@args.map(&block).join "\n"
end
def print_members(prefix, &block)
return if @args.nil?
@args.map(&block).map { |arg| "#{prefix}#{arg}" }.join "\n"
end
def capitalized_name
(name.split('_').map do |s|
s = s.capitalize
if s.match(/^\d/)
i = s.match(/^\d+/)[0].size
s = s[0..i-1] + s[i..-1].capitalize
end
s
end).join
end
def typed_args
return if @args.nil?
@args.map(&:create_param).unshift("").join(", ")
end
def typed_reference_args
return if @args.nil?
@args.map(&:create_reference_param).unshift("").join(", ")
end
def untyped_args
return if @args.nil?
@args.map(&:name).unshift("").join(", ")
end
def opcodeIDType
@section.is_wasm? ? :WasmOpcodeID : :OpcodeID
end
def wide16
@section.is_wasm? ? :wasm_wide16 : :op_wide16
end
def wide32
@section.is_wasm? ? :wasm_wide32 : :op_wide32
end
def traits
@section.is_wasm? ? "WasmOpcodeTraits" : "JSOpcodeTraits"
end
def type_prefix
@section.is_wasm? ? "Wasm" : "JS"
end
def map_fields_with_size(prefix, size, &block)
args = [Argument.new("opcodeID", opcodeIDType, 0)]
args += @args.dup if @args
unless @metadata.empty?
args