|
| 1 | +//===-- APIDefs.td - Base definitions for Offload tablegen -*- tablegen -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file contains the class definitions used to implement the Offload API, |
| 10 | +// as well as helper functions used to help populate relevant records. |
| 11 | +// See offload/API/README.md for more detailed documentation. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | + |
| 16 | +// Parameter flags |
| 17 | +defvar PARAM_IN = 0x1; |
| 18 | +defvar PARAM_OUT = 0x2; |
| 19 | +defvar PARAM_OPTIONAL = 0x4; |
| 20 | + |
| 21 | +// Prefix for API naming. This could be hard-coded in the future when a value |
| 22 | +// is agreed upon. |
| 23 | +defvar PREFIX = "OL"; |
| 24 | +defvar prefix = !tolower(PREFIX); |
| 25 | + |
| 26 | +// Does the type end with '_handle_t'? |
| 27 | +class IsHandleType<string Type> { |
| 28 | + // size("_handle_t") == 9 |
| 29 | + bit ret = !if(!lt(!size(Type), 9), 0, |
| 30 | + !ne(!find(Type, "_handle_t", !sub(!size(Type), 9)), -1)); |
| 31 | +} |
| 32 | + |
| 33 | +// Does the type end with '*'? |
| 34 | +class IsPointerType<string Type> { |
| 35 | + bit ret = !ne(!find(Type, "*", !sub(!size(Type), 1)), -1); |
| 36 | +} |
| 37 | + |
| 38 | +class Param<string Type, string Name, string Desc, bits<3> Flags = 0> { |
| 39 | + string type = Type; |
| 40 | + string name = Name; |
| 41 | + string desc = Desc; |
| 42 | + bits<3> flags = Flags; |
| 43 | + bit IsHandle = IsHandleType<type>.ret; |
| 44 | + bit IsPointer = IsPointerType<type>.ret; |
| 45 | +} |
| 46 | + |
| 47 | +class Return<string Value, list<string> Conditions = []> { |
| 48 | + string value = Value; |
| 49 | + list<string> conditions = Conditions; |
| 50 | +} |
| 51 | + |
| 52 | +class ShouldCheckHandle<Param P> { |
| 53 | + bit ret = !and(P.IsHandle, !eq(!and(PARAM_OPTIONAL, P.flags), 0)); |
| 54 | +} |
| 55 | + |
| 56 | +class ShouldCheckPointer<Param P> { |
| 57 | + bit ret = !and(P.IsPointer, !eq(!and(PARAM_OPTIONAL, P.flags), 0)); |
| 58 | +} |
| 59 | + |
| 60 | +// For a list of returns that contains a specific return code, find and append |
| 61 | +// new conditions to that return |
| 62 | +class AppendConditionsToReturn<list<Return> Returns, string ReturnValue, |
| 63 | + list<string> Conditions> { |
| 64 | + list<Return> ret = |
| 65 | + !foreach(Ret, Returns, |
| 66 | + !if(!eq(Ret.value, ReturnValue), |
| 67 | + Return<Ret.value, Ret.conditions#Conditions>, Ret)); |
| 68 | +} |
| 69 | + |
| 70 | +// Add null handle checks to a function's return value descriptions |
| 71 | +class AddHandleChecksToReturns<list<Param> Params, list<Return> Returns> { |
| 72 | + list<string> handle_params = |
| 73 | + !foreach(P, Params, !if(ShouldCheckHandle<P>.ret, P.name, "")); |
| 74 | + list<string> handle_params_filt = |
| 75 | + !filter(param, handle_params, !ne(param, "")); |
| 76 | + list<string> handle_param_conds = |
| 77 | + !foreach(handle, handle_params_filt, "`NULL == "#handle#"`"); |
| 78 | + |
| 79 | + // Does the list of returns already contain ERROR_INVALID_NULL_HANDLE? |
| 80 | + bit returns_has_inv_handle = !foldl( |
| 81 | + 0, Returns, HasErr, Ret, |
| 82 | + !or(HasErr, !eq(Ret.value, PREFIX#"_RESULT_ERROR_INVALID_NULL_HANDLE"))); |
| 83 | + |
| 84 | + list<Return> returns_out = !if(returns_has_inv_handle, |
| 85 | + AppendConditionsToReturn<Returns, PREFIX # "_RESULT_ERROR_INVALID_NULL_HANDLE", handle_param_conds>.ret, |
| 86 | + !listconcat(Returns, [Return<PREFIX # "_RESULT_ERROR_INVALID_NULL_HANDLE", handle_param_conds>]) |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +// Add null pointer checks to a function's return value descriptions |
| 91 | +class AddPointerChecksToReturns<list<Param> Params, list<Return> Returns> { |
| 92 | + list<string> ptr_params = |
| 93 | + !foreach(P, Params, !if(ShouldCheckPointer<P>.ret, P.name, "")); |
| 94 | + list<string> ptr_params_filt = !filter(param, ptr_params, !ne(param, "")); |
| 95 | + list<string> ptr_param_conds = |
| 96 | + !foreach(ptr, ptr_params_filt, "`NULL == "#ptr#"`"); |
| 97 | + |
| 98 | + // Does the list of returns already contain ERROR_INVALID_NULL_POINTER? |
| 99 | + bit returns_has_inv_ptr = !foldl( |
| 100 | + 0, Returns, HasErr, Ret, |
| 101 | + !or(HasErr, !eq(Ret.value, PREFIX#"_RESULT_ERROR_INVALID_NULL_POINTER"))); |
| 102 | + list<Return> returns_out = !if(returns_has_inv_ptr, |
| 103 | + AppendConditionsToReturn<Returns, PREFIX # "_RESULT_ERROR_INVALID_NULL_POINTER", ptr_param_conds>.ret, |
| 104 | + !listconcat(Returns, [Return<PREFIX # "_RESULT_ERROR_INVALID_NULL_POINTER", ptr_param_conds>]) |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +defvar DefaultReturns = [Return<PREFIX#"_RESULT_SUCCESS">, |
| 109 | + Return<PREFIX#"_RESULT_ERROR_UNINITIALIZED">, |
| 110 | + Return<PREFIX#"_RESULT_ERROR_DEVICE_LOST">, |
| 111 | + Return<PREFIX#"_RESULT_ERROR_ADAPTER_SPECIFIC">]; |
| 112 | + |
| 113 | +class APIObject { |
| 114 | + string name; |
| 115 | + string desc; |
| 116 | +} |
| 117 | + |
| 118 | +class Function<string Class> : APIObject { |
| 119 | + string api_class = Class; |
| 120 | + list<Param> params; |
| 121 | + list<Return> returns; |
| 122 | + list<string> details = []; |
| 123 | + list<string> analogues = []; |
| 124 | + |
| 125 | + list<Return> returns_with_def = !listconcat(DefaultReturns, returns); |
| 126 | + list<Return> all_returns = AddPointerChecksToReturns<params, |
| 127 | + AddHandleChecksToReturns<params, returns_with_def>.returns_out>.returns_out; |
| 128 | +} |
| 129 | + |
| 130 | +class Etor<string Name, string Desc> { |
| 131 | + string name = Name; |
| 132 | + string desc = Desc; |
| 133 | +} |
| 134 | + |
| 135 | +class Enum : APIObject { |
| 136 | + // This refers to whether the enumerator descriptions specify a return |
| 137 | + // type for functions where this enum may be used as an input type. |
| 138 | + // The format is "[$x_some_return_t] Description text" |
| 139 | + // (TODO: This is lifted from UR, is it relevant?) |
| 140 | + bit is_typed = 0; |
| 141 | + |
| 142 | + list<Etor> etors = []; |
| 143 | +} |
| 144 | + |
| 145 | +class StructMember<string Type, string Name, string Desc> { |
| 146 | + string type = Type; |
| 147 | + string name = Name; |
| 148 | + string desc = Desc; |
| 149 | +} |
| 150 | + |
| 151 | +defvar DefaultPropStructMembers = |
| 152 | + [StructMember<prefix#"_structure_type_t", "stype", |
| 153 | + "type of this structure">, |
| 154 | + StructMember<"void*", "pNext", "pointer to extension-specific structure">]; |
| 155 | + |
| 156 | +class StructHasInheritedMembers<string BaseClass> { |
| 157 | + bit ret = !or(!eq(BaseClass, prefix#"_base_properties_t"), |
| 158 | + !eq(BaseClass, prefix#"_base_desc_t")); |
| 159 | +} |
| 160 | + |
| 161 | +class Struct : APIObject { |
| 162 | + string base_class = ""; |
| 163 | + list<StructMember> members; |
| 164 | + list<StructMember> all_members = |
| 165 | + !if(StructHasInheritedMembers<base_class>.ret, |
| 166 | + DefaultPropStructMembers, [])#members; |
| 167 | +} |
| 168 | + |
| 169 | +class Typedef : APIObject { string value; } |
| 170 | + |
| 171 | +class FptrTypedef : APIObject { |
| 172 | + list<Param> params; |
| 173 | + list<Return> returns; |
| 174 | +} |
| 175 | + |
| 176 | +class Macro : APIObject { |
| 177 | + string value; |
| 178 | + |
| 179 | + string condition; |
| 180 | + string alt_value; |
| 181 | +} |
| 182 | + |
| 183 | +class Handle : APIObject; |
0 commit comments