|
22 | 22 | #include "vm/compiler/assembler/assembler.h" |
23 | 23 | #include "vm/compiler/ffi/callback.h" |
24 | 24 | #include "vm/compiler/ffi/marshaller.h" |
| 25 | +#include "vm/compiler/ffi/native_type.h" |
25 | 26 | #include "vm/compiler/jit/compiler.h" |
26 | 27 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
27 | 28 |
|
@@ -67,6 +68,142 @@ DEFINE_NATIVE_ENTRY(Ffi_updateNativeCallableKeepIsolateAliveCounter, 1, 1) { |
67 | 68 | return Object::null(); |
68 | 69 | } |
69 | 70 |
|
| 71 | +static ObjectPtr LoadStoreAbiSpecificInt(Zone* zone, |
| 72 | + NativeArguments* arguments, |
| 73 | + bool is_load, |
| 74 | + bool at_index) { |
| 75 | +#if defined(DART_DYNAMIC_MODULES) && !defined(DART_PRECOMPILED_RUNTIME) |
| 76 | + const auto& type_args = |
| 77 | + TypeArguments::Handle(zone, arguments->NativeTypeArgs()); |
| 78 | + const auto& base = Instance::CheckedHandle(zone, arguments->NativeArgAt(0)); |
| 79 | + int64_t offset_in_bytes = |
| 80 | + Integer::CheckedHandle(zone, arguments->NativeArgAt(1)).Value(); |
| 81 | + |
| 82 | + const AbstractType& type_argument = |
| 83 | + AbstractType::Handle(zone, type_args.TypeAt(0)); |
| 84 | + |
| 85 | + // AbiSpecificTypes can have an incomplete mapping. |
| 86 | + const char* error = nullptr; |
| 87 | + const auto* native_type = |
| 88 | + compiler::ffi::NativeType::FromAbstractType(zone, type_argument, &error); |
| 89 | + if (error != nullptr) { |
| 90 | + const auto& language_error = Error::Handle( |
| 91 | + LanguageError::New(String::Handle(String::New(error, Heap::kOld)), |
| 92 | + Report::kError, Heap::kOld)); |
| 93 | + Report::LongJump(language_error); |
| 94 | + } |
| 95 | + |
| 96 | + if (at_index) { |
| 97 | + const int64_t index = |
| 98 | + Integer::CheckedHandle(zone, arguments->NativeArgAt(2)).Value(); |
| 99 | + offset_in_bytes += index * native_type->SizeInBytes(); |
| 100 | + } |
| 101 | + |
| 102 | + int64_t value = 0; |
| 103 | + if (!is_load) { |
| 104 | + value = |
| 105 | + Integer::CheckedHandle(zone, arguments->NativeArgAt(at_index ? 3 : 2)) |
| 106 | + .Value(); |
| 107 | + } |
| 108 | + { |
| 109 | + NoSafepointScope no_safepoint; |
| 110 | + void* addr; |
| 111 | + if (base.IsPointer()) { |
| 112 | + addr = reinterpret_cast<void*>(Pointer::Cast(base).NativeAddress() + |
| 113 | + offset_in_bytes); |
| 114 | + } else if (base.IsTypedDataBase()) { |
| 115 | + addr = TypedDataBase::Cast(base).DataAddr(offset_in_bytes); |
| 116 | + } else { |
| 117 | + UNREACHABLE(); |
| 118 | + } |
| 119 | + if (is_load) { |
| 120 | + ASSERT(native_type->IsPrimitive()); |
| 121 | + switch (native_type->AsPrimitive().representation()) { |
| 122 | + case compiler::ffi::kInt8: |
| 123 | + value = *reinterpret_cast<int8_t*>(addr); |
| 124 | + break; |
| 125 | + case compiler::ffi::kInt16: |
| 126 | + value = *reinterpret_cast<int16_t*>(addr); |
| 127 | + break; |
| 128 | + case compiler::ffi::kInt32: |
| 129 | + value = *reinterpret_cast<int32_t*>(addr); |
| 130 | + break; |
| 131 | + case compiler::ffi::kInt64: |
| 132 | + value = *reinterpret_cast<int64_t*>(addr); |
| 133 | + break; |
| 134 | + case compiler::ffi::kUint8: |
| 135 | + value = *reinterpret_cast<uint8_t*>(addr); |
| 136 | + break; |
| 137 | + case compiler::ffi::kUint16: |
| 138 | + value = *reinterpret_cast<uint16_t*>(addr); |
| 139 | + break; |
| 140 | + case compiler::ffi::kUint32: |
| 141 | + value = *reinterpret_cast<uint32_t*>(addr); |
| 142 | + break; |
| 143 | + case compiler::ffi::kUint64: |
| 144 | + value = *reinterpret_cast<uint64_t*>(addr); |
| 145 | + break; |
| 146 | + default: |
| 147 | + UNREACHABLE(); |
| 148 | + } |
| 149 | + } else { |
| 150 | + ASSERT(native_type->IsPrimitive()); |
| 151 | + switch (native_type->AsPrimitive().representation()) { |
| 152 | + case compiler::ffi::kInt8: |
| 153 | + *reinterpret_cast<int8_t*>(addr) = static_cast<int8_t>(value); |
| 154 | + break; |
| 155 | + case compiler::ffi::kInt16: |
| 156 | + *reinterpret_cast<int16_t*>(addr) = static_cast<int16_t>(value); |
| 157 | + break; |
| 158 | + case compiler::ffi::kInt32: |
| 159 | + *reinterpret_cast<int32_t*>(addr) = static_cast<int32_t>(value); |
| 160 | + break; |
| 161 | + case compiler::ffi::kInt64: |
| 162 | + *reinterpret_cast<int64_t*>(addr) = value; |
| 163 | + break; |
| 164 | + case compiler::ffi::kUint8: |
| 165 | + *reinterpret_cast<uint8_t*>(addr) = static_cast<uint8_t>(value); |
| 166 | + break; |
| 167 | + case compiler::ffi::kUint16: |
| 168 | + *reinterpret_cast<uint16_t*>(addr) = static_cast<uint16_t>(value); |
| 169 | + break; |
| 170 | + case compiler::ffi::kUint32: |
| 171 | + *reinterpret_cast<uint32_t*>(addr) = static_cast<uint32_t>(value); |
| 172 | + break; |
| 173 | + case compiler::ffi::kUint64: |
| 174 | + *reinterpret_cast<uint64_t*>(addr) = static_cast<uint64_t>(value); |
| 175 | + break; |
| 176 | + default: |
| 177 | + UNREACHABLE(); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return is_load ? Integer::New(value) : Object::null(); |
| 182 | +#else |
| 183 | + UNIMPLEMENTED(); |
| 184 | +#endif // defined(DART_DYNAMIC_MODULES) && !defined(DART_PRECOMPILED_RUNTIME) |
| 185 | +} |
| 186 | + |
| 187 | +DEFINE_NATIVE_ENTRY(Ffi_loadAbiSpecificInt, 1, 2) { |
| 188 | + return LoadStoreAbiSpecificInt(zone, arguments, /*is_load=*/true, |
| 189 | + /*at_index=*/false); |
| 190 | +} |
| 191 | + |
| 192 | +DEFINE_NATIVE_ENTRY(Ffi_loadAbiSpecificIntAtIndex, 1, 3) { |
| 193 | + return LoadStoreAbiSpecificInt(zone, arguments, /*is_load=*/true, |
| 194 | + /*at_index=*/true); |
| 195 | +} |
| 196 | + |
| 197 | +DEFINE_NATIVE_ENTRY(Ffi_storeAbiSpecificInt, 1, 3) { |
| 198 | + return LoadStoreAbiSpecificInt(zone, arguments, /*is_load=*/false, |
| 199 | + /*at_index=*/false); |
| 200 | +} |
| 201 | + |
| 202 | +DEFINE_NATIVE_ENTRY(Ffi_storeAbiSpecificIntAtIndex, 1, 4) { |
| 203 | + return LoadStoreAbiSpecificInt(zone, arguments, /*is_load=*/false, |
| 204 | + /*at_index=*/true); |
| 205 | +} |
| 206 | + |
70 | 207 | DEFINE_NATIVE_ENTRY(DartNativeApiFunctionPointer, 0, 1) { |
71 | 208 | GET_NON_NULL_NATIVE_ARGUMENT(String, name_dart, arguments->NativeArgAt(0)); |
72 | 209 | const char* name = name_dart.ToCString(); |
|
0 commit comments