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 'delegate' 15 16def array_type?(type) 17 type[-1] == '[' 18end 19 20def get_object_type(type) 21 if array_type?(type) 22 type = "Array" 23 end 24 t = Runtime::coretypes.find { |t| t.managed_class == type } 25 return '%s *' % t.mirror_class if t 26 d = Runtime::coretypes.find { |d| d.managed_class == "Default" } 27 return '%s *' % d.mirror_class if d 28 'ark::ObjectHeader *' 29end 30 31def get_type(type) 32 @type_map ||= { 33 'void' => ['void'], 34 'u1' => ['uint8_t'], 35 'i8' => ['int8_t'], 36 'u8' => ['uint8_t'], 37 'i16' => ['int16_t'], 38 'u16' => ['uint16_t'], 39 'i32' => ['int32_t'], 40 'u32' => ['uint32_t'], 41 'i64' => ['int64_t'], 42 'u64' => ['uint64_t'], 43 'f32' => ['float'], 44 'f64' => ['double'], 45 'any' => ['uint64_t'], 46 'ptr' => ['void *'], 47 'acc' => ['uint64_t'], 48 'method' => ['ark::Method *'], 49 'string_id' => ['uint32_t'], 50 'method_id' => ['uint32_t'], 51 } 52 @type_map[type] || get_object_type(type) 53end 54 55def get_ret_type(type) 56 @ret_type_map ||= { 57 'void' => 'void', 58 'u1' => 'uint8_t', 59 'i8' => 'int8_t', 60 'u8' => 'uint8_t', 61 'i16' => 'int16_t', 62 'u16' => 'uint16_t', 63 'i32' => 'int32_t', 64 'u32' => 'uint32_t', 65 'i64' => 'int64_t', 66 'u64' => 'uint64_t', 67 'f32' => 'float', 68 'f64' => 'double', 69 'any' => 'uint64_t', 70 'ptr' => 'void *', 71 'acc' => 'coretypes::TaggedValue', 72 'string_id' => 'uint32_t', 73 'method_id' => 'uint32_t', 74 } 75 @ret_type_map[type] || get_object_type(type) 76end 77 78def get_effective_type(type) 79 get_type(type) 80end 81 82def get_ret_effective_type(type) 83 get_ret_type(type) 84end 85 86class Intrinsic < SimpleDelegator 87 def need_abi_wrapper? 88 Object.send(:get_ret_type, signature.ret) != Object.send(:get_ret_effective_type, signature.ret) || 89 signature.args.any? { |arg| Object.send(:get_ret_type, arg) != Object.send(:get_ret_effective_type, arg) } 90 end 91 92 def enum_name 93 res = name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') 94 res.gsub(/([a-z\d])([A-Z])/,'\1_\2').upcase 95 end 96 97 def wrapper_impl 98 return "" unless has_impl? 99 return impl + 'AbiWrapper' if need_abi_wrapper? 100 impl 101 end 102 103 def has_impl? 104 respond_to?(:impl) 105 end 106end 107 108module Runtime 109 module_function 110 111 def intrinsics 112 @data.intrinsics.map do |intrinsic| 113 Intrinsic.new(intrinsic) 114 end 115 end 116 117 def intrinsics_namespace 118 @data.intrinsics_namespace 119 end 120 121 def coretypes 122 @data.coretypes 123 end 124 125 def wrap_data(data) 126 @data = data 127 end 128end 129 130def Gen.on_require(data) 131 Runtime.wrap_data(data) 132end 133