1b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
3b1994897Sopenharmony_ci# you may not use this file except in compliance with the License.
4b1994897Sopenharmony_ci# You may obtain a copy of the License at
5b1994897Sopenharmony_ci#
6b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
7b1994897Sopenharmony_ci#
8b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
9b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
10b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11b1994897Sopenharmony_ci# See the License for the specific language governing permissions and
12b1994897Sopenharmony_ci# limitations under the License.
13b1994897Sopenharmony_ci
14b1994897Sopenharmony_cirequire 'ostruct'
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci# PandaFile specific extension of ISAPI
17b1994897Sopenharmony_ci
18b1994897Sopenharmony_ciInstruction.class_eval do
19b1994897Sopenharmony_ci  def emitter_name
20b1994897Sopenharmony_ci    mnemonic.split('.').map { |p| p == '64' ? 'Wide' : p.capitalize }.join
21b1994897Sopenharmony_ci  end
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_ci  def each_operand
24b1994897Sopenharmony_ci    getters = {:reg? => 0, :imm? => 0, :id? => 0}
25b1994897Sopenharmony_ci    operands.each do |op|
26b1994897Sopenharmony_ci      key = getters.keys.find { |x| op.send(x) }
27b1994897Sopenharmony_ci      yield op, getters[key]
28b1994897Sopenharmony_ci      getters[key] += 1
29b1994897Sopenharmony_ci    end
30b1994897Sopenharmony_ci  end
31b1994897Sopenharmony_ci
32b1994897Sopenharmony_ci  def jcmp?
33b1994897Sopenharmony_ci    jump? && conditional? && stripped_mnemonic[-1] != 'z'
34b1994897Sopenharmony_ci  end
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_ci  def jcmpz?
37b1994897Sopenharmony_ci    jump? && conditional? && stripped_mnemonic[-1] == 'z'
38b1994897Sopenharmony_ci  end
39b1994897Sopenharmony_ciend
40b1994897Sopenharmony_ci
41b1994897Sopenharmony_ciOperand.class_eval do
42b1994897Sopenharmony_ci  def to_h
43b1994897Sopenharmony_ci    instance_variables.map { |v| [v.to_s[1..-1], instance_variable_get(v)] }.to_h
44b1994897Sopenharmony_ci  end
45b1994897Sopenharmony_ciend
46b1994897Sopenharmony_ci
47b1994897Sopenharmony_cidef storage_width(bits)
48b1994897Sopenharmony_ci  (bits + 7) / 8 * 8
49b1994897Sopenharmony_ciend
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_cidef format_ops(format)
52b1994897Sopenharmony_ci  format.encoding.values.map(&:dup)
53b1994897Sopenharmony_ciend
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_ci# returns array of OpenStruct with fields
56b1994897Sopenharmony_ci# name - name of variable in emitter code
57b1994897Sopenharmony_ci# type - type of variable in emitter code
58b1994897Sopenharmony_ci# width - bit width
59b1994897Sopenharmony_ci# tag - the same as in Operand isapi class
60b1994897Sopenharmony_cidef emitter_signature(group, is_jump)
61b1994897Sopenharmony_ci  sig = format_ops(group.first.format).each { |o| o.width = storage_width(o.width) }
62b1994897Sopenharmony_ci  group.each do |insn|
63b1994897Sopenharmony_ci    insn.operands.each_with_index do |o, i|
64b1994897Sopenharmony_ci      sig[i].width = [o.width, sig[i].width].max
65b1994897Sopenharmony_ci      if o.is_signed_imm? || o.is_float_imm?
66b1994897Sopenharmony_ci        if is_jump
67b1994897Sopenharmony_ci          sig[i].type, sig[i].name = ['const Label &', 'label']
68b1994897Sopenharmony_ci        else
69b1994897Sopenharmony_ci          sig[i].type = "int#{sig[i].width}_t"
70b1994897Sopenharmony_ci        end
71b1994897Sopenharmony_ci      else
72b1994897Sopenharmony_ci        sig[i].type = "uint#{sig[i].width}_t"
73b1994897Sopenharmony_ci      end
74b1994897Sopenharmony_ci    end
75b1994897Sopenharmony_ci  end
76b1994897Sopenharmony_ci  return sig
77b1994897Sopenharmony_ciend
78b1994897Sopenharmony_ci
79b1994897Sopenharmony_cidef insns_uniq_sort_fmts
80b1994897Sopenharmony_ci  Panda.instructions.uniq { |i| i.format.pretty }.sort_by { |insn| insn.format.pretty }
81b1994897Sopenharmony_ciend
82