1# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'ostruct' 15require 'set' 16require 'delegate' 17 18def get_shorty_type(type) 19 @shorty_type_map ||= { 20 'void' => 'VOID', 21 'u1' => 'U1', 22 'i8' => 'I8', 23 'u8' => 'U8', 24 'i16' => 'I16', 25 'u16' => 'U16', 26 'i32' => 'I32', 27 'u32' => 'U32', 28 'i64' => 'I64', 29 'u64' => 'U64', 30 'f32' => 'F32', 31 'f64' => 'F64', 32 'any' => 'TAGGED', 33 'acc' => 'TAGGED', 34 'string_id' => 'U32', 35 'method_id' => 'U32', 36 } 37 @shorty_type_map[type] || 'REFERENCE' 38end 39 40def primitive_type?(type) 41 @primitive_types ||= Set[ 42 'void', 43 'u1', 44 'i8', 45 'u8', 46 'i16', 47 'u16', 48 'i32', 49 'u32', 50 'i64', 51 'u64', 52 'f32', 53 'f64', 54 'any', 55 'acc', 56 'string_id', 57 'method_id', 58 ] 59 @primitive_types.include?(type) 60end 61 62def get_primitive_descriptor(type) 63 @primitive_type_map ||= { 64 'u1' => 'Z', 65 'i8' => 'B', 66 'u8' => 'H', 67 'i16' => 'S', 68 'u16' => 'C', 69 'i32' => 'I', 70 'u32' => 'U', 71 'i64' => 'J', 72 'u64' => 'Q', 73 'f32' => 'F', 74 'f64' => 'D', 75 'any' => 'A', 76 'acc' => 'A', 77 'string_id' => 'S', 78 'method_id' => 'M', 79 } 80 @primitive_type_map[type] 81end 82 83def object_type?(type) 84 !primitive_type?(type) 85end 86 87def get_object_descriptor(type) 88 rank = type.count('[') 89 type = type[0...-(rank * 2)] if rank > 0 90 return '[' * rank + get_primitive_descriptor(type) if primitive_type?(type) 91 '[' * rank + 'L' + type.gsub('.', '/') + ';' 92end 93 94def floating_point_type?(type) 95 @fp_types ||= Set[ 96 'f32', 97 'f64', 98 ] 99 @fp_types.include?(type) 100end 101 102class Intrinsic < SimpleDelegator 103 def enum_name 104 res = name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') 105 res.gsub(/([a-z\d])([A-Z])/,'\1_\2').upcase 106 end 107 108 def need_abi_wrapper? 109 defined? orig_impl 110 end 111 112 def has_impl? 113 respond_to?(:impl) 114 end 115 116 def is_irtoc? 117 class_name == 'Irtoc' 118 end 119end 120 121module Runtime 122 module_function 123 124 def intrinsics 125 @data.intrinsics.map do |intrinsic| 126 Intrinsic.new(intrinsic) 127 end 128 end 129 130 def include_headers 131 @data.include_headers 132 end 133 134 def wrap_data(data) 135 @data = data 136 end 137end 138 139def Gen.on_require(data) 140 Runtime.wrap_data(data) 141end 142