koffi 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +94 -0
- package/README.md +153 -0
- package/build/ALL_BUILD.vcxproj +190 -0
- package/build/ALL_BUILD.vcxproj.filters +8 -0
- package/build/CMakeCache.txt +429 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeASMCompiler.cmake +20 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeASM_MASMCompiler.cmake +20 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeCCompiler.cmake +72 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeCXXCompiler.cmake +83 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_C.bin +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_CXX.bin +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeRCCompiler.cmake +6 -0
- package/build/CMakeFiles/3.23.0-rc1/CMakeSystem.cmake +15 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CMakeCCompilerId.c +828 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.exe +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.vcxproj +71 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CMakeCCompilerId.obj +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.exe.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CompilerIdC.lastbuildstate +2 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CMakeCXXCompilerId.cpp +816 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.exe +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.vcxproj +71 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CMakeCXXCompilerId.obj +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.exe.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CompilerIdCXX.lastbuildstate +2 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.command.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.read.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.write.1.tlog +0 -0
- package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.txt +1 -0
- package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.vcxproj +31 -0
- package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.recipe +11 -0
- package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.tlog/VCTargetsPath.lastbuildstate +2 -0
- package/build/CMakeFiles/41bcd16856091d4a38fd1f71fbe2f202/generate.stamp.rule +1 -0
- package/build/CMakeFiles/CMakeError.log +108 -0
- package/build/CMakeFiles/CMakeOutput.log +413 -0
- package/build/CMakeFiles/TargetDirectories.txt +4 -0
- package/build/CMakeFiles/cmake.check_cache +1 -0
- package/build/CMakeFiles/generate.stamp +1 -0
- package/build/CMakeFiles/generate.stamp.depend +28 -0
- package/build/CMakeFiles/generate.stamp.list +1 -0
- package/build/Raylib.dir/Release/Raylib.dll.recipe +14 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.command.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.read.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CL.write.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/Raylib.dir/Release/Raylib.tlog/Raylib.lastbuildstate +2 -0
- package/build/Raylib.dir/Release/Raylib.tlog/Raylib.write.1u.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.command.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.read.1.tlog +0 -0
- package/build/Raylib.dir/Release/Raylib.tlog/link.write.1.tlog +0 -0
- package/build/Raylib.dir/Release/raudio.obj +0 -0
- package/build/Raylib.dir/Release/rcore.obj +0 -0
- package/build/Raylib.dir/Release/rglfw.obj +0 -0
- package/build/Raylib.dir/Release/rmodels.obj +0 -0
- package/build/Raylib.dir/Release/rshapes.obj +0 -0
- package/build/Raylib.dir/Release/rtext.obj +0 -0
- package/build/Raylib.dir/Release/rtextures.obj +0 -0
- package/build/Raylib.dir/Release/utils.obj +0 -0
- package/build/Raylib.vcxproj +358 -0
- package/build/Raylib.vcxproj.filters +37 -0
- package/build/Release/Raylib.dll +0 -0
- package/build/Release/Raylib.exp +0 -0
- package/build/Release/Raylib.lib +0 -0
- package/build/Release/koffi.exp +0 -0
- package/build/Release/koffi.lib +0 -0
- package/build/Release/koffi.node +0 -0
- package/build/ZERO_CHECK.vcxproj +176 -0
- package/build/ZERO_CHECK.vcxproj.filters +13 -0
- package/build/cmake_install.cmake +44 -0
- package/build/koffi.dir/Release/call_arm64.obj +0 -0
- package/build/koffi.dir/Release/call_x64_sysv.obj +0 -0
- package/build/koffi.dir/Release/call_x64_win.obj +0 -0
- package/build/koffi.dir/Release/call_x64_win_fwd.obj +0 -0
- package/build/koffi.dir/Release/call_x86.obj +0 -0
- package/build/koffi.dir/Release/ffi.obj +0 -0
- package/build/koffi.dir/Release/koffi.node.recipe +14 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.command.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.read.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CL.write.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/koffi.dir/Release/koffi.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/koffi.dir/Release/koffi.tlog/Masm.read.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/Masm.write.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/koffi.lastbuildstate +2 -0
- package/build/koffi.dir/Release/koffi.tlog/koffi.write.1u.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.command.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.read.1.tlog +0 -0
- package/build/koffi.dir/Release/koffi.tlog/link.write.1.tlog +0 -0
- package/build/koffi.dir/Release/libcc.obj +0 -0
- package/build/koffi.dir/Release/util.obj +0 -0
- package/build/koffi.dir/Release/win_delay_load_hook.obj +0 -0
- package/build/koffi.sln +67 -0
- package/build/koffi.vcxproj +363 -0
- package/build/koffi.vcxproj.filters +40 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.recipe +20 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/ALL_BUILD.lastbuildstate +2 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.read.1.tlog +27 -0
- package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.recipe +11 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.command.1.tlog +10 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.read.1.tlog +28 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.write.1.tlog +2 -0
- package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/ZERO_CHECK.lastbuildstate +2 -0
- package/package.json +14 -0
- package/src/call.hh +27 -0
- package/src/call_arm64.cc +482 -0
- package/src/call_arm64_fwd.S +115 -0
- package/src/call_x64_sysv.cc +477 -0
- package/src/call_x64_sysv_fwd.S +131 -0
- package/src/call_x64_win.cc +243 -0
- package/src/call_x64_win_fwd.asm +105 -0
- package/src/call_x86.cc +259 -0
- package/src/call_x86_fwd.S +48 -0
- package/src/call_x86_fwd.asm +50 -0
- package/src/ffi.cc +504 -0
- package/src/ffi.hh +135 -0
- package/src/util.cc +296 -0
- package/src/util.hh +80 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Affero General Public License for more details.
|
|
10
|
+
//
|
|
11
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
#if defined(__aarch64__)
|
|
15
|
+
|
|
16
|
+
#include "vendor/libcc/libcc.hh"
|
|
17
|
+
#include "ffi.hh"
|
|
18
|
+
#include "call.hh"
|
|
19
|
+
#include "util.hh"
|
|
20
|
+
|
|
21
|
+
#include <napi.h>
|
|
22
|
+
|
|
23
|
+
namespace RG {
|
|
24
|
+
|
|
25
|
+
struct X0X1Ret {
|
|
26
|
+
uint64_t x0;
|
|
27
|
+
uint64_t x1;
|
|
28
|
+
};
|
|
29
|
+
struct HfaRet {
|
|
30
|
+
double d0;
|
|
31
|
+
double d1;
|
|
32
|
+
double d2;
|
|
33
|
+
double d3;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
extern "C" X0X1Ret ForwardCallGG(const void *func, uint8_t *sp);
|
|
37
|
+
extern "C" float ForwardCallF(const void *func, uint8_t *sp);
|
|
38
|
+
extern "C" HfaRet ForwardCallDDDD(const void *func, uint8_t *sp);
|
|
39
|
+
|
|
40
|
+
extern "C" X0X1Ret ForwardCallXGG(const void *func, uint8_t *sp);
|
|
41
|
+
extern "C" float ForwardCallXF(const void *func, uint8_t *sp);
|
|
42
|
+
extern "C" HfaRet ForwardCallXDDDD(const void *func, uint8_t *sp);
|
|
43
|
+
|
|
44
|
+
static bool IsHFA(const TypeInfo *type)
|
|
45
|
+
{
|
|
46
|
+
if (type->primitive != PrimitiveKind::Record)
|
|
47
|
+
return false;
|
|
48
|
+
|
|
49
|
+
if (type->members.len < 1 || type->members.len > 4)
|
|
50
|
+
return false;
|
|
51
|
+
if (type->members[0].type->primitive != PrimitiveKind::Float32 &&
|
|
52
|
+
type->members[0].type->primitive != PrimitiveKind::Float64)
|
|
53
|
+
return false;
|
|
54
|
+
|
|
55
|
+
for (Size i = 1; i < type->members.len; i++) {
|
|
56
|
+
if (type->members[i].type != type->members[0].type)
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
bool AnalyseFunction(FunctionInfo *func)
|
|
64
|
+
{
|
|
65
|
+
if (IsHFA(func->ret.type)) {
|
|
66
|
+
func->ret.vec_count = func->ret.type->members.len;
|
|
67
|
+
} else if (func->ret.type->size <= 16) {
|
|
68
|
+
func->ret.gpr_count = (func->ret.type->size + 7) / 8;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
int gpr_avail = 8;
|
|
72
|
+
int vec_avail = 8;
|
|
73
|
+
|
|
74
|
+
for (ParameterInfo ¶m: func->parameters) {
|
|
75
|
+
switch (param.type->primitive) {
|
|
76
|
+
case PrimitiveKind::Void: { RG_UNREACHABLE(); } break;
|
|
77
|
+
|
|
78
|
+
case PrimitiveKind::Bool:
|
|
79
|
+
case PrimitiveKind::Int8:
|
|
80
|
+
case PrimitiveKind::UInt8:
|
|
81
|
+
case PrimitiveKind::Int16:
|
|
82
|
+
case PrimitiveKind::UInt16:
|
|
83
|
+
case PrimitiveKind::Int32:
|
|
84
|
+
case PrimitiveKind::UInt32:
|
|
85
|
+
case PrimitiveKind::Int64:
|
|
86
|
+
case PrimitiveKind::UInt64:
|
|
87
|
+
case PrimitiveKind::String:
|
|
88
|
+
case PrimitiveKind::Pointer: {
|
|
89
|
+
if (gpr_avail) {
|
|
90
|
+
param.gpr_count = 1;
|
|
91
|
+
gpr_avail--;
|
|
92
|
+
}
|
|
93
|
+
} break;
|
|
94
|
+
|
|
95
|
+
case PrimitiveKind::Float32:
|
|
96
|
+
case PrimitiveKind::Float64: {
|
|
97
|
+
if (vec_avail) {
|
|
98
|
+
param.vec_count = 1;
|
|
99
|
+
vec_avail--;
|
|
100
|
+
}
|
|
101
|
+
} break;
|
|
102
|
+
|
|
103
|
+
case PrimitiveKind::Record: {
|
|
104
|
+
if (IsHFA(param.type)) {
|
|
105
|
+
int vec_count = (int)param.type->members.len;
|
|
106
|
+
|
|
107
|
+
param.vec_count = std::min(vec_avail, vec_count);
|
|
108
|
+
vec_avail -= vec_count;
|
|
109
|
+
} else if (param.type->size <= 16) {
|
|
110
|
+
int gpr_count = (param.type->size + 7) / 8;
|
|
111
|
+
|
|
112
|
+
param.gpr_count = std::min(gpr_avail, gpr_count);
|
|
113
|
+
gpr_avail -= gpr_count;
|
|
114
|
+
} else if (gpr_avail) {
|
|
115
|
+
// Big types (more than 16 bytes) are replaced by a pointer
|
|
116
|
+
param.gpr_count = 1;
|
|
117
|
+
gpr_avail -= 1;
|
|
118
|
+
}
|
|
119
|
+
} break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
func->forward_fp = (vec_avail < 8);
|
|
124
|
+
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static bool PushHFA(const Napi::Object &obj, const TypeInfo *type, uint8_t *dest)
|
|
129
|
+
{
|
|
130
|
+
Napi::Env env = obj.Env();
|
|
131
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
132
|
+
|
|
133
|
+
RG_ASSERT(obj.IsObject());
|
|
134
|
+
RG_ASSERT(type->primitive == PrimitiveKind::Record);
|
|
135
|
+
RG_ASSERT(AlignUp(dest, 8) == dest);
|
|
136
|
+
|
|
137
|
+
for (const RecordMember &member: type->members) {
|
|
138
|
+
Napi::Value value = obj.Get(member.name);
|
|
139
|
+
|
|
140
|
+
if (member.type->primitive == PrimitiveKind::Float32) {
|
|
141
|
+
if (!value.IsNumber() && !value.IsBigInt()) {
|
|
142
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
*(float *)dest = CopyNodeNumber<float>(value);
|
|
147
|
+
} else if (member.type->primitive == PrimitiveKind::Float64) {
|
|
148
|
+
if (!value.IsNumber() && !value.IsBigInt()) {
|
|
149
|
+
ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
*(double *)dest = CopyNodeNumber<double>(value);
|
|
154
|
+
} else {
|
|
155
|
+
RG_UNREACHABLE();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
dest += 8;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static Napi::Object PopHFA(napi_env env, const uint8_t *ptr, const TypeInfo *type)
|
|
165
|
+
{
|
|
166
|
+
RG_ASSERT(type->primitive == PrimitiveKind::Record);
|
|
167
|
+
|
|
168
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
169
|
+
|
|
170
|
+
for (const RecordMember &member: type->members) {
|
|
171
|
+
if (member.type->primitive == PrimitiveKind::Float32) {
|
|
172
|
+
float f = *(float *)ptr;
|
|
173
|
+
obj.Set(member.name, Napi::Number::New(env, (double)f));
|
|
174
|
+
} else if (member.type->primitive == PrimitiveKind::Float64) {
|
|
175
|
+
double d = *(double *)ptr;
|
|
176
|
+
obj.Set(member.name, Napi::Number::New(env, d));
|
|
177
|
+
} else {
|
|
178
|
+
RG_UNREACHABLE();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
ptr += 8;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return obj;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
Napi::Value TranslateCall(const Napi::CallbackInfo &info)
|
|
188
|
+
{
|
|
189
|
+
Napi::Env env = info.Env();
|
|
190
|
+
InstanceData *instance = env.GetInstanceData<InstanceData>();
|
|
191
|
+
|
|
192
|
+
FunctionInfo *func = (FunctionInfo *)info.Data();
|
|
193
|
+
LibraryData *lib = func->lib.get();
|
|
194
|
+
|
|
195
|
+
RG_DEFER { lib->tmp_alloc.ReleaseAll(); };
|
|
196
|
+
|
|
197
|
+
// Sanity checks
|
|
198
|
+
if (info.Length() < (uint32_t)func->parameters.len) {
|
|
199
|
+
ThrowError<Napi::TypeError>(env, "Expected %1 arguments, got %2", func->parameters.len, info.Length());
|
|
200
|
+
return env.Null();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Stack pointer and register
|
|
204
|
+
uint8_t *top_ptr = lib->stack.end();
|
|
205
|
+
uint8_t *scratch_ptr = top_ptr - func->scratch_size;
|
|
206
|
+
uint8_t *return_ptr = nullptr;
|
|
207
|
+
uint8_t *args_ptr = nullptr;
|
|
208
|
+
uint64_t *gpr_ptr = nullptr, *vec_ptr = nullptr;
|
|
209
|
+
uint8_t *sp_ptr = nullptr;
|
|
210
|
+
|
|
211
|
+
// Return through registers unless it's too big
|
|
212
|
+
if (!func->ret.type->size || func->ret.vec_count || func->ret.gpr_count) {
|
|
213
|
+
args_ptr = scratch_ptr - AlignLen(8 * func->parameters.len, 16);
|
|
214
|
+
vec_ptr = (uint64_t *)args_ptr - 8;
|
|
215
|
+
gpr_ptr = vec_ptr - 9;
|
|
216
|
+
sp_ptr = (uint8_t *)(gpr_ptr - 7);
|
|
217
|
+
|
|
218
|
+
#ifdef RG_DEBUG
|
|
219
|
+
memset(sp_ptr, 0, top_ptr - sp_ptr);
|
|
220
|
+
#endif
|
|
221
|
+
} else {
|
|
222
|
+
return_ptr = scratch_ptr - AlignLen(func->ret.type->size, 16);
|
|
223
|
+
|
|
224
|
+
args_ptr = return_ptr - AlignLen(8 * func->parameters.len, 16);
|
|
225
|
+
vec_ptr = (uint64_t *)args_ptr - 8;
|
|
226
|
+
gpr_ptr = vec_ptr - 9;
|
|
227
|
+
sp_ptr = (uint8_t *)(gpr_ptr - 7);
|
|
228
|
+
|
|
229
|
+
#ifdef RG_DEBUG
|
|
230
|
+
memset(sp_ptr, 0, top_ptr - sp_ptr);
|
|
231
|
+
#endif
|
|
232
|
+
|
|
233
|
+
gpr_ptr[8] = (uint64_t)return_ptr;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
RG_ASSERT(AlignUp(lib->stack.ptr, 16) == lib->stack.ptr);
|
|
237
|
+
RG_ASSERT(AlignUp(lib->stack.end(), 16) == lib->stack.end());
|
|
238
|
+
RG_ASSERT(AlignUp(sp_ptr, 16) == sp_ptr);
|
|
239
|
+
|
|
240
|
+
// Push arguments
|
|
241
|
+
for (Size i = 0; i < func->parameters.len; i++) {
|
|
242
|
+
const ParameterInfo ¶m = func->parameters[i];
|
|
243
|
+
Napi::Value value = info[i];
|
|
244
|
+
|
|
245
|
+
switch (param.type->primitive) {
|
|
246
|
+
case PrimitiveKind::Void: { RG_UNREACHABLE(); } break;
|
|
247
|
+
|
|
248
|
+
case PrimitiveKind::Bool: {
|
|
249
|
+
if (RG_UNLIKELY(!value.IsBoolean())) {
|
|
250
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected boolean", GetValueType(instance, value), i + 1);
|
|
251
|
+
return env.Null();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
bool b = value.As<Napi::Boolean>();
|
|
255
|
+
|
|
256
|
+
if (RG_LIKELY(param.gpr_count)) {
|
|
257
|
+
*(gpr_ptr++) = (uint64_t)b;
|
|
258
|
+
} else {
|
|
259
|
+
*(args_ptr++) = (uint8_t)b;
|
|
260
|
+
}
|
|
261
|
+
} break;
|
|
262
|
+
case PrimitiveKind::Int8:
|
|
263
|
+
case PrimitiveKind::UInt8:
|
|
264
|
+
case PrimitiveKind::Int16:
|
|
265
|
+
case PrimitiveKind::UInt16:
|
|
266
|
+
case PrimitiveKind::Int32:
|
|
267
|
+
case PrimitiveKind::UInt32:
|
|
268
|
+
case PrimitiveKind::Int64:
|
|
269
|
+
case PrimitiveKind::UInt64: {
|
|
270
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
271
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
|
|
272
|
+
return env.Null();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
int64_t v = CopyNodeNumber<int64_t>(value);
|
|
276
|
+
|
|
277
|
+
if (RG_LIKELY(param.gpr_count)) {
|
|
278
|
+
*(gpr_ptr++) = (uint64_t)v;
|
|
279
|
+
} else {
|
|
280
|
+
args_ptr = AlignUp(args_ptr, param.type->align);
|
|
281
|
+
memcpy(args_ptr, &v, param.type->size); // Little Endian
|
|
282
|
+
args_ptr += param.type->size;
|
|
283
|
+
}
|
|
284
|
+
} break;
|
|
285
|
+
case PrimitiveKind::Float32: {
|
|
286
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
287
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
|
|
288
|
+
return env.Null();
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
float f = CopyNodeNumber<float>(value);
|
|
292
|
+
|
|
293
|
+
if (RG_LIKELY(param.vec_count)) {
|
|
294
|
+
memcpy(vec_ptr++, &f, 4);
|
|
295
|
+
} else {
|
|
296
|
+
args_ptr = AlignUp(args_ptr, 4);
|
|
297
|
+
memcpy(args_ptr, &f, 4);
|
|
298
|
+
args_ptr += 4;
|
|
299
|
+
}
|
|
300
|
+
} break;
|
|
301
|
+
case PrimitiveKind::Float64: {
|
|
302
|
+
if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
|
|
303
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
|
|
304
|
+
return env.Null();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
double d = CopyNodeNumber<double>(value);
|
|
308
|
+
|
|
309
|
+
if (RG_LIKELY(param.vec_count)) {
|
|
310
|
+
memcpy(vec_ptr++, &d, 8);
|
|
311
|
+
} else {
|
|
312
|
+
args_ptr = AlignUp(args_ptr, 8);
|
|
313
|
+
memcpy(args_ptr, &d, 8);
|
|
314
|
+
args_ptr += 8;
|
|
315
|
+
}
|
|
316
|
+
} break;
|
|
317
|
+
case PrimitiveKind::String: {
|
|
318
|
+
if (RG_UNLIKELY(!value.IsString())) {
|
|
319
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected string", GetValueType(instance, value), i + 1);
|
|
320
|
+
return env.Null();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const char *str = CopyNodeString(value, &lib->tmp_alloc);
|
|
324
|
+
|
|
325
|
+
if (RG_LIKELY(param.gpr_count)) {
|
|
326
|
+
*(gpr_ptr++) = (uint64_t)str;
|
|
327
|
+
} else {
|
|
328
|
+
args_ptr = AlignUp(args_ptr, 8);
|
|
329
|
+
*(uint64_t *)args_ptr = (uint64_t)str;
|
|
330
|
+
args_ptr += 8;
|
|
331
|
+
}
|
|
332
|
+
} break;
|
|
333
|
+
case PrimitiveKind::Pointer: {
|
|
334
|
+
if (RG_UNLIKELY(!CheckValueTag(instance, value, param.type))) {
|
|
335
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected %3", GetValueType(instance, value), i + 1, param.type->name);
|
|
336
|
+
return env.Null();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
void *ptr = value.As<Napi::External<void>>();
|
|
340
|
+
|
|
341
|
+
if (RG_LIKELY(param.gpr_count)) {
|
|
342
|
+
*(gpr_ptr++) = (uint64_t)ptr;
|
|
343
|
+
} else {
|
|
344
|
+
args_ptr = AlignUp(args_ptr, 8);
|
|
345
|
+
*(uint64_t *)args_ptr = (uint64_t)ptr;
|
|
346
|
+
args_ptr += 8;
|
|
347
|
+
}
|
|
348
|
+
} break;
|
|
349
|
+
|
|
350
|
+
case PrimitiveKind::Record: {
|
|
351
|
+
if (RG_UNLIKELY(!value.IsObject())) {
|
|
352
|
+
ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected object", GetValueType(instance, value), i + 1);
|
|
353
|
+
return env.Null();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
Napi::Object obj = value.As<Napi::Object>();
|
|
357
|
+
|
|
358
|
+
if (param.vec_count) {
|
|
359
|
+
if (!PushHFA(obj, param.type, (uint8_t *)vec_ptr))
|
|
360
|
+
return env.Null();
|
|
361
|
+
vec_ptr += param.vec_count;
|
|
362
|
+
} else if (param.type->size <= 16) {
|
|
363
|
+
if (param.gpr_count) {
|
|
364
|
+
RG_ASSERT(param.type->align <= 8);
|
|
365
|
+
|
|
366
|
+
if (!PushObject(obj, param.type, &lib->tmp_alloc, (uint8_t *)gpr_ptr))
|
|
367
|
+
return env.Null();
|
|
368
|
+
gpr_ptr += param.gpr_count;
|
|
369
|
+
} else if (param.type->size) {
|
|
370
|
+
args_ptr = AlignUp(args_ptr, param.type->align);
|
|
371
|
+
if (!PushObject(obj, param.type, &lib->tmp_alloc, args_ptr))
|
|
372
|
+
return env.Null();
|
|
373
|
+
args_ptr += AlignLen(param.type->size, 8);
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
uint8_t *ptr = scratch_ptr;
|
|
377
|
+
scratch_ptr += AlignLen(param.type->size, 16);
|
|
378
|
+
|
|
379
|
+
if (param.gpr_count) {
|
|
380
|
+
RG_ASSERT(param.gpr_count == 1);
|
|
381
|
+
RG_ASSERT(param.vec_count == 0);
|
|
382
|
+
|
|
383
|
+
*(gpr_ptr++) = (uint64_t)ptr;
|
|
384
|
+
} else {
|
|
385
|
+
args_ptr = AlignUp(args_ptr, 8);
|
|
386
|
+
*(uint8_t **)args_ptr = ptr;
|
|
387
|
+
args_ptr += 8;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!PushObject(obj, param.type, &lib->tmp_alloc, ptr))
|
|
391
|
+
return env.Null();
|
|
392
|
+
}
|
|
393
|
+
} break;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// DumpStack(func, MakeSpan(sp_ptr, top_ptr - sp_ptr));
|
|
398
|
+
|
|
399
|
+
#define PERFORM_CALL(Suffix) \
|
|
400
|
+
(func->forward_fp ? ForwardCallX ## Suffix(func->func, sp_ptr) \
|
|
401
|
+
: ForwardCall ## Suffix(func->func, sp_ptr))
|
|
402
|
+
|
|
403
|
+
// Execute and convert return value
|
|
404
|
+
switch (func->ret.type->primitive) {
|
|
405
|
+
case PrimitiveKind::Float32: {
|
|
406
|
+
float f = PERFORM_CALL(F);
|
|
407
|
+
|
|
408
|
+
return Napi::Number::New(env, (double)f);
|
|
409
|
+
} break;
|
|
410
|
+
|
|
411
|
+
case PrimitiveKind::Float64: {
|
|
412
|
+
HfaRet ret = PERFORM_CALL(DDDD);
|
|
413
|
+
|
|
414
|
+
return Napi::Number::New(env, (double)ret.d0);
|
|
415
|
+
} break;
|
|
416
|
+
|
|
417
|
+
case PrimitiveKind::Record: {
|
|
418
|
+
if (func->ret.gpr_count) {
|
|
419
|
+
X0X1Ret ret = PERFORM_CALL(GG);
|
|
420
|
+
|
|
421
|
+
Napi::Object obj = PopObject(env, (const uint8_t *)&ret, func->ret.type);
|
|
422
|
+
return obj;
|
|
423
|
+
} else if (func->ret.vec_count) {
|
|
424
|
+
HfaRet ret = PERFORM_CALL(DDDD);
|
|
425
|
+
|
|
426
|
+
Napi::Object obj = PopHFA(env, (const uint8_t *)&ret, func->ret.type);
|
|
427
|
+
return obj;
|
|
428
|
+
} else if (func->ret.type->size) {
|
|
429
|
+
RG_ASSERT(return_ptr);
|
|
430
|
+
|
|
431
|
+
X0X1Ret ret = PERFORM_CALL(GG);
|
|
432
|
+
RG_ASSERT(ret.x0 = (uint64_t)return_ptr);
|
|
433
|
+
|
|
434
|
+
Napi::Object obj = PopObject(env, return_ptr, func->ret.type);
|
|
435
|
+
return obj;
|
|
436
|
+
} else {
|
|
437
|
+
PERFORM_CALL(GG);
|
|
438
|
+
|
|
439
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
440
|
+
return obj;
|
|
441
|
+
}
|
|
442
|
+
} break;
|
|
443
|
+
|
|
444
|
+
default: {
|
|
445
|
+
X0X1Ret ret = PERFORM_CALL(GG);
|
|
446
|
+
|
|
447
|
+
switch (func->ret.type->primitive) {
|
|
448
|
+
case PrimitiveKind::Void: return env.Null();
|
|
449
|
+
case PrimitiveKind::Bool: return Napi::Boolean::New(env, ret.x0);
|
|
450
|
+
case PrimitiveKind::Int8: return Napi::Number::New(env, (double)ret.x0);
|
|
451
|
+
case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)ret.x0);
|
|
452
|
+
case PrimitiveKind::Int16: return Napi::Number::New(env, (double)ret.x0);
|
|
453
|
+
case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)ret.x0);
|
|
454
|
+
case PrimitiveKind::Int32: return Napi::Number::New(env, (double)ret.x0);
|
|
455
|
+
case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)ret.x0);
|
|
456
|
+
case PrimitiveKind::Int64: return Napi::BigInt::New(env, (int64_t)ret.x0);
|
|
457
|
+
case PrimitiveKind::UInt64: return Napi::BigInt::New(env, ret.x0);
|
|
458
|
+
case PrimitiveKind::Float32: { RG_UNREACHABLE(); } break;
|
|
459
|
+
case PrimitiveKind::Float64: { RG_UNREACHABLE(); } break;
|
|
460
|
+
case PrimitiveKind::String: return Napi::String::New(env, (const char *)ret.x0);
|
|
461
|
+
case PrimitiveKind::Pointer: {
|
|
462
|
+
void *ptr = (void *)ret.x0;
|
|
463
|
+
|
|
464
|
+
Napi::External<void> external = Napi::External<void>::New(env, ptr);
|
|
465
|
+
SetValueTag(instance, external, func->ret.type);
|
|
466
|
+
|
|
467
|
+
return external;
|
|
468
|
+
} break;
|
|
469
|
+
|
|
470
|
+
case PrimitiveKind::Record: { RG_UNREACHABLE(); } break;
|
|
471
|
+
}
|
|
472
|
+
} break;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#undef PERFORM_CALL
|
|
476
|
+
|
|
477
|
+
RG_UNREACHABLE();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
#endif
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// This program is free software: you can redistribute it and/or modify
|
|
2
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
3
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
4
|
+
// (at your option) any later version.
|
|
5
|
+
//
|
|
6
|
+
// This program is distributed in the hope that it will be useful,
|
|
7
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9
|
+
// GNU Affero General Public License for more details.
|
|
10
|
+
|
|
11
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
12
|
+
// along with this program. If not, see https://www.gnu.org/licenses/.
|
|
13
|
+
|
|
14
|
+
// These five are the same, but they differ (in the C side) by their return type.
|
|
15
|
+
// Unlike the five next functions, these ones don't forward XMM argument registers.
|
|
16
|
+
.global ForwardCallGG
|
|
17
|
+
.global ForwardCallF
|
|
18
|
+
.global ForwardCallDDDD
|
|
19
|
+
|
|
20
|
+
// The X variants are slightly slower, and are used when XMM arguments must be forwarded.
|
|
21
|
+
.global ForwardCallXGG
|
|
22
|
+
.global ForwardCallXF
|
|
23
|
+
.global ForwardCallXDDDD
|
|
24
|
+
|
|
25
|
+
// Copy function pointer to r9, in order to save it through argument forwarding.
|
|
26
|
+
// Save RSP in r19 (non-volatile), and use carefully assembled stack provided by caller.
|
|
27
|
+
.macro prologue
|
|
28
|
+
.cfi_startproc
|
|
29
|
+
stp x29, x30, [sp, -16]!
|
|
30
|
+
.cfi_def_cfa sp, 16
|
|
31
|
+
.cfi_offset 29, 16
|
|
32
|
+
.cfi_offset 30, 8
|
|
33
|
+
str x19, [sp, -16]!
|
|
34
|
+
.cfi_def_cfa sp, 32
|
|
35
|
+
mov x9, x0
|
|
36
|
+
mov x19, sp
|
|
37
|
+
.cfi_def_cfa x19, 32
|
|
38
|
+
mov sp, x1
|
|
39
|
+
add sp, sp, #192
|
|
40
|
+
.endm
|
|
41
|
+
|
|
42
|
+
// Call native function.
|
|
43
|
+
// Once done, restore normal stack pointer and return.
|
|
44
|
+
// The return value is passed untouched through r0, r1, v0 and/or v1.
|
|
45
|
+
.macro epilogue
|
|
46
|
+
blr x9
|
|
47
|
+
mov sp, x19
|
|
48
|
+
.cfi_def_cfa sp, 32
|
|
49
|
+
ldr x19, [sp], 16
|
|
50
|
+
.cfi_def_cfa sp, 16
|
|
51
|
+
ldp x29, x30, [sp], 16
|
|
52
|
+
.cfi_def_cfa sp, 0
|
|
53
|
+
.cfi_restore 30
|
|
54
|
+
.cfi_restore 29
|
|
55
|
+
ret
|
|
56
|
+
.cfi_endproc
|
|
57
|
+
.endm
|
|
58
|
+
|
|
59
|
+
// Prepare general purpose argument registers from array passed by caller.
|
|
60
|
+
.macro forward_int
|
|
61
|
+
ldr x8, [x1, 120]
|
|
62
|
+
ldr x7, [x1, 112]
|
|
63
|
+
ldr x6, [x1, 104]
|
|
64
|
+
ldr x5, [x1, 96]
|
|
65
|
+
ldr x4, [x1, 88]
|
|
66
|
+
ldr x3, [x1, 80]
|
|
67
|
+
ldr x2, [x1, 72]
|
|
68
|
+
ldr x0, [x1, 56]
|
|
69
|
+
ldr x1, [x1, 64]
|
|
70
|
+
.endm
|
|
71
|
+
|
|
72
|
+
// Prepare vector argument registers from array passed by caller.
|
|
73
|
+
.macro forward_vec
|
|
74
|
+
ldr d7, [x1, 184]
|
|
75
|
+
ldr d6, [x1, 176]
|
|
76
|
+
ldr d5, [x1, 168]
|
|
77
|
+
ldr d4, [x1, 160]
|
|
78
|
+
ldr d3, [x1, 152]
|
|
79
|
+
ldr d2, [x1, 144]
|
|
80
|
+
ldr d1, [x1, 136]
|
|
81
|
+
ldr d0, [x1, 128]
|
|
82
|
+
.endm
|
|
83
|
+
|
|
84
|
+
ForwardCallGG:
|
|
85
|
+
prologue
|
|
86
|
+
forward_int
|
|
87
|
+
epilogue
|
|
88
|
+
|
|
89
|
+
ForwardCallF:
|
|
90
|
+
prologue
|
|
91
|
+
forward_int
|
|
92
|
+
epilogue
|
|
93
|
+
|
|
94
|
+
ForwardCallDDDD:
|
|
95
|
+
prologue
|
|
96
|
+
forward_int
|
|
97
|
+
epilogue
|
|
98
|
+
|
|
99
|
+
ForwardCallXGG:
|
|
100
|
+
prologue
|
|
101
|
+
forward_vec
|
|
102
|
+
forward_int
|
|
103
|
+
epilogue
|
|
104
|
+
|
|
105
|
+
ForwardCallXF:
|
|
106
|
+
prologue
|
|
107
|
+
forward_vec
|
|
108
|
+
forward_int
|
|
109
|
+
epilogue
|
|
110
|
+
|
|
111
|
+
ForwardCallXDDDD:
|
|
112
|
+
prologue
|
|
113
|
+
forward_vec
|
|
114
|
+
forward_int
|
|
115
|
+
epilogue
|