1# Copyright (c) 2023-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 llvm_type_getter(type, gc_space)
17  @llvm_type_map ||= {
18    'void' => 'Void',
19    'int8_t' => 'Int8',
20    'uint8_t' => 'Int8',
21    'int16_t' => 'Int16',
22    'uint16_t' => 'Int16',
23    'int32_t' => 'Int32',
24    'uint32_t' => 'Int32',
25    'int64_t' => 'Int64',
26    'uint64_t' => 'Int64',
27    'float' => 'Float',
28    'double' => 'Double',
29  }
30  if @llvm_type_map.key? type
31    return 'llvm::Type::get' + @llvm_type_map[type] + 'Ty(ctx)'
32  elsif type == 'void *' || type == 'ark::Method *'
33    return 'llvm::PointerType::get(ctx, 0)'
34  elsif type.include? '*'
35    return 'llvm::PointerType::get(ctx, '+ gc_space + ')'
36  else
37    raise "Unexpected type required to lower into LLVM IR: #{type}"
38  end
39end
40
41class Intrinsic < SimpleDelegator
42  def need_abi_wrapper?
43    false
44  end
45
46  def llvm_internal_name
47    '__panda_intrinsic_' + name
48  end
49
50  def enum_name
51    res = name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
52    res = res.gsub(/([a-z\d])([A-Z])/,'\1_\2').upcase
53    is_stub ? res : 'INTRINSIC_' + res
54  end
55
56  def wrapper_impl
57    return impl + 'AbiWrapper' if need_abi_wrapper?
58    impl
59  end
60end
61
62module Runtime
63  module_function
64
65  def intrinsics
66    @exclude_list = [
67    ]
68
69    @data.intrinsics.select { |i| !@exclude_list.include?(i.name) }.map do |intrinsic|
70      Intrinsic.new(intrinsic)
71    end
72  end
73
74  def intrinsics_namespace
75    @data.intrinsics_namespace
76  end
77
78  def coretypes
79    @data.coretypes
80  end
81
82  def wrap_data(data)
83    @data = data
84  end
85end
86
87def Gen.on_require(data)
88  Runtime.wrap_data(data)
89end
90