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_ci# Assembler specific extension of ISAPI 15b1994897Sopenharmony_ciInstruction.class_eval do 16b1994897Sopenharmony_ci def asm_token 17b1994897Sopenharmony_ci mnemonic.tr('.', '_').upcase 18b1994897Sopenharmony_ci end 19b1994897Sopenharmony_ci 20b1994897Sopenharmony_ci def call? 21b1994897Sopenharmony_ci properties.include?('call') 22b1994897Sopenharmony_ci end 23b1994897Sopenharmony_ci 24b1994897Sopenharmony_ci def range? 25b1994897Sopenharmony_ci mnemonic.split('.')[-1] == 'range' 26b1994897Sopenharmony_ci end 27b1994897Sopenharmony_ci 28b1994897Sopenharmony_ci def simple_call? 29b1994897Sopenharmony_ci call? && !range? && !properties.include?('dynamic') 30b1994897Sopenharmony_ci end 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_ci def return? 33b1994897Sopenharmony_ci stripped_mnemonic == 'return' 34b1994897Sopenharmony_ci end 35b1994897Sopenharmony_ci 36b1994897Sopenharmony_ci def return_obj? 37b1994897Sopenharmony_ci mnemonic == 'return.obj' 38b1994897Sopenharmony_ci end 39b1994897Sopenharmony_ci 40b1994897Sopenharmony_ci def return64? 41b1994897Sopenharmony_ci mnemonic == 'return.64' 42b1994897Sopenharmony_ci end 43b1994897Sopenharmony_ci 44b1994897Sopenharmony_ci def return32? 45b1994897Sopenharmony_ci mnemonic == 'return' 46b1994897Sopenharmony_ci end 47b1994897Sopenharmony_ci 48b1994897Sopenharmony_ci def return_void? 49b1994897Sopenharmony_ci mnemonic == 'return.void' 50b1994897Sopenharmony_ci end 51b1994897Sopenharmony_ciend 52b1994897Sopenharmony_ci 53b1994897Sopenharmony_cidef bit_cast(what, to_type, from_type) 54b1994897Sopenharmony_ci "bit_cast<#{to_type}, #{from_type}>(static_cast<#{from_type}>(std::get<double>(#{what})))" 55b1994897Sopenharmony_ciend 56b1994897Sopenharmony_ci 57b1994897Sopenharmony_cidef index_of_max(a) 58b1994897Sopenharmony_ci a.each_with_index.max[1] # returns index of `last` max value 59b1994897Sopenharmony_ciend 60b1994897Sopenharmony_ci 61b1994897Sopenharmony_cidef max_number_of_src_regs 62b1994897Sopenharmony_ci Panda::instructions.map do |insn| 63b1994897Sopenharmony_ci insn.operands.select(&:reg?).select(&:src?).size 64b1994897Sopenharmony_ci end.max 65b1994897Sopenharmony_ciend 66b1994897Sopenharmony_ci 67b1994897Sopenharmony_ciIR = Struct.new(:opcode, :flags, :dst_idx, :use_idxs) 68b1994897Sopenharmony_ci 69b1994897Sopenharmony_cimodule Panda 70b1994897Sopenharmony_ci def self.pseudo_instructions 71b1994897Sopenharmony_ci insns = [] 72b1994897Sopenharmony_ci insns << IR.new('MOVX', ['InstFlags::PSEUDO'], 0, [1]) 73b1994897Sopenharmony_ci insns << IR.new('LDAX', ['InstFlags::PSEUDO', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', [0]) 74b1994897Sopenharmony_ci insns << IR.new('STAX', ['InstFlags::PSEUDO', 'InstFlags::ACC_READ'], 0, []) 75b1994897Sopenharmony_ci insns << IR.new('NEWX', ['InstFlags::PSEUDO'], 0, []) 76b1994897Sopenharmony_ci insns << IR.new('INITOBJX', ['InstFlags::PSEUDO', 'InstFlags::CALL', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', []) 77b1994897Sopenharmony_ci insns << IR.new('CALLX', ['InstFlags::PSEUDO', 'InstFlags::CALL', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', []) 78b1994897Sopenharmony_ci insns << IR.new('CALLX_VIRT', ['InstFlags::PSEUDO', 'InstFlags::CALL', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', []) 79b1994897Sopenharmony_ci insns << IR.new('B_P_CALLIX', ['InstFlags::PSEUDO', 'InstFlags::CALL', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', []) 80b1994897Sopenharmony_ci insns << IR.new('B_P_CALLIEX',['InstFlags::PSEUDO', 'InstFlags::CALL', 'InstFlags::ACC_WRITE'], 'INVALID_REG_IDX', []) 81b1994897Sopenharmony_ci insns 82b1994897Sopenharmony_ci end 83b1994897Sopenharmony_ciend 84b1994897Sopenharmony_ci 85b1994897Sopenharmony_ci# returns array of OpenStruct with fields 86b1994897Sopenharmony_ci# name - name of variable in emitter code 87b1994897Sopenharmony_ci# type - type of variable in emitter code 88b1994897Sopenharmony_cidef assembler_signature(group, is_jump) 89b1994897Sopenharmony_ci insn = group.first 90b1994897Sopenharmony_ci format_ops(insn.format).each do |o| 91b1994897Sopenharmony_ci if o.name.start_with?('imm') 92b1994897Sopenharmony_ci if insn.asm_token.start_with?('F') 93b1994897Sopenharmony_ci o.type, o.name = is_jump ? ['const std::string &', 'label'] : ['double', o.name] 94b1994897Sopenharmony_ci else 95b1994897Sopenharmony_ci o.type, o.name = is_jump ? ['const std::string &', 'label'] : ['int64_t', o.name] 96b1994897Sopenharmony_ci end 97b1994897Sopenharmony_ci elsif o.name.start_with?('id') 98b1994897Sopenharmony_ci o.type, o.name = ['const std::string &', o.name] 99b1994897Sopenharmony_ci else 100b1994897Sopenharmony_ci o.type = 'uint16_t' 101b1994897Sopenharmony_ci end 102b1994897Sopenharmony_ci end 103b1994897Sopenharmony_ciend 104