koffi 0.9.5 → 0.9.6

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 CHANGED
@@ -20,6 +20,7 @@ if(MSVC)
20
20
  endif()
21
21
 
22
22
  set(KOFFI_SRC
23
+ src/call_arm32.cc
23
24
  src/call_arm64.cc
24
25
  src/call_x64_sysv.cc
25
26
  src/call_x64_win.cc
@@ -41,6 +42,8 @@ elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
41
42
  list(APPEND KOFFI_SRC src/call_x86_fwd.asm)
42
43
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i[3456]86|x86|AMD64")
43
44
  list(APPEND KOFFI_SRC src/call_x86_fwd.S)
45
+ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv[678]l")
46
+ list(APPEND KOFFI_SRC src/call_arm32_fwd.S)
44
47
  endif()
45
48
  endif()
46
49
 
package/README.md CHANGED
@@ -8,7 +8,8 @@ The following platforms __are supported__ at the moment:
8
8
  * Windows x86_64
9
9
  * Linux x86
10
10
  * Linux x86_64
11
- * Linux ARM64 *(Raspberry Pi OS 64-bit)*
11
+ * Linux ARM32+VFP Little Endian *(Raspberry Pi OS 32-bit)*
12
+ * Linux ARM64 Little Endian *(Raspberry Pi OS 64-bit)*
12
13
 
13
14
  The following platforms are __not yet supported__ but will be soon:
14
15
 
@@ -17,9 +18,7 @@ The following platforms are __not yet supported__ but will be soon:
17
18
  * FreeBSD/NetBSD/OpenBSD x86_64
18
19
  * FreeBSD/NetBSD/OpenBSD ARM64
19
20
 
20
- Support for ARM32 is not planned, though I don't mind working on it if there is enough demand.
21
-
22
- This is still in development, more tests will come in the near future.
21
+ This is still in development, bugs are to expected. More tests will come in the near future.
23
22
 
24
23
  # Installation
25
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koffi",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "description": "Fast and simple FFI (foreign function interface) for Node.js",
5
5
  "keywords": ["foreign", "function", "interface", "ffi", "binding", "c", "napi"],
6
6
  "repository": {
@@ -0,0 +1,514 @@
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(__arm__)
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 HfaRet {
26
+ double d0;
27
+ double d1;
28
+ double d2;
29
+ double d3;
30
+ };
31
+
32
+ extern "C" uint64_t ForwardCallGG(const void *func, uint8_t *sp);
33
+ extern "C" float ForwardCallF(const void *func, uint8_t *sp);
34
+ extern "C" HfaRet ForwardCallDDDD(const void *func, uint8_t *sp);
35
+
36
+ extern "C" uint64_t ForwardCallXGG(const void *func, uint8_t *sp);
37
+ extern "C" float ForwardCallXF(const void *func, uint8_t *sp);
38
+ extern "C" HfaRet ForwardCallXDDDD(const void *func, uint8_t *sp);
39
+
40
+ static bool IsHFA(const TypeInfo *type)
41
+ {
42
+ if (type->primitive != PrimitiveKind::Record)
43
+ return false;
44
+
45
+ if (type->members.len < 1 || type->members.len > 4)
46
+ return false;
47
+ if (type->members[0].type->primitive != PrimitiveKind::Float32 &&
48
+ type->members[0].type->primitive != PrimitiveKind::Float64)
49
+ return false;
50
+
51
+ for (Size i = 1; i < type->members.len; i++) {
52
+ if (type->members[i].type != type->members[0].type)
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ }
58
+
59
+ bool AnalyseFunction(FunctionInfo *func)
60
+ {
61
+ if (IsHFA(func->ret.type)) {
62
+ func->ret.vec_count = func->ret.type->members.len *
63
+ (func->ret.type->members[0].type->size / 4);
64
+ } else if (func->ret.type->size <= 4) {
65
+ func->ret.gpr_count = 1;
66
+ } else {
67
+ func->ret.use_memory = true;
68
+ }
69
+
70
+ int gpr_avail = 4 - func->ret.use_memory;
71
+ int vec_avail = 16;
72
+ bool started_stack = false;
73
+
74
+ for (ParameterInfo &param: 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::String:
86
+ case PrimitiveKind::Pointer: {
87
+ if (gpr_avail) {
88
+ param.gpr_count = 1;
89
+ gpr_avail--;
90
+ } else {
91
+ started_stack = true;
92
+ }
93
+ } break;
94
+
95
+ case PrimitiveKind::Int64:
96
+ case PrimitiveKind::UInt64: {
97
+ if (gpr_avail >= 2) {
98
+ param.gpr_count = 2;
99
+ gpr_avail -= 2;
100
+ } else {
101
+ started_stack = true;
102
+ }
103
+ } break;
104
+
105
+ case PrimitiveKind::Float32:
106
+ case PrimitiveKind::Float64: {
107
+ Size need = param.type->size / 4;
108
+
109
+ if (need <= vec_avail) {
110
+ param.vec_count = need;
111
+ vec_avail -= need;
112
+ } else {
113
+ started_stack = true;
114
+ }
115
+ } break;
116
+
117
+ case PrimitiveKind::Record: {
118
+ if (IsHFA(param.type)) {
119
+ int vec_count = (int)(param.type->members.len *
120
+ param.type->members[0].type->size / 4);
121
+
122
+ if (vec_count <= vec_avail) {
123
+ param.vec_count = vec_count;
124
+ vec_avail -= vec_count;
125
+ } else {
126
+ vec_avail = 0;
127
+ started_stack = true;
128
+ }
129
+ } else if (param.type->size) {
130
+ int gpr_count = (param.type->size + 3) / 4;
131
+
132
+ if (gpr_count <= gpr_avail) {
133
+ param.gpr_count = gpr_count;
134
+ gpr_avail -= gpr_count;
135
+ } else {
136
+ if (!started_stack) {
137
+ param.gpr_count = gpr_avail;
138
+ gpr_avail = 0;
139
+
140
+ started_stack = true;
141
+ }
142
+ }
143
+ }
144
+ } break;
145
+ }
146
+ }
147
+
148
+ func->forward_fp = (vec_avail < 16);
149
+
150
+ return true;
151
+ }
152
+
153
+ static bool PushHFA(const Napi::Object &obj, const TypeInfo *type, uint8_t *dest)
154
+ {
155
+ Napi::Env env = obj.Env();
156
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
157
+
158
+ RG_ASSERT(obj.IsObject());
159
+ RG_ASSERT(type->primitive == PrimitiveKind::Record);
160
+ RG_ASSERT(AlignUp(dest, type->members[0].type->size) == dest);
161
+
162
+ for (const RecordMember &member: type->members) {
163
+ Napi::Value value = obj.Get(member.name);
164
+
165
+ if (member.type->primitive == PrimitiveKind::Float32) {
166
+ if (!value.IsNumber() && !value.IsBigInt()) {
167
+ ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
168
+ return false;
169
+ }
170
+
171
+ *(float *)dest = CopyNodeNumber<float>(value);
172
+ } else if (member.type->primitive == PrimitiveKind::Float64) {
173
+ if (!value.IsNumber() && !value.IsBigInt()) {
174
+ ThrowError<Napi::TypeError>(env, "Unexpected value %1 for member '%2', expected number", GetValueType(instance, value), member.name);
175
+ return false;
176
+ }
177
+
178
+ *(double *)dest = CopyNodeNumber<double>(value);
179
+ } else {
180
+ RG_UNREACHABLE();
181
+ }
182
+
183
+ dest += type->members[0].type->size;
184
+ }
185
+
186
+ return true;
187
+ }
188
+
189
+ static Napi::Object PopHFA(napi_env env, const uint8_t *ptr, const TypeInfo *type)
190
+ {
191
+ RG_ASSERT(type->primitive == PrimitiveKind::Record);
192
+
193
+ Napi::Object obj = Napi::Object::New(env);
194
+
195
+ for (const RecordMember &member: type->members) {
196
+ if (member.type->primitive == PrimitiveKind::Float32) {
197
+ float f = *(float *)ptr;
198
+ obj.Set(member.name, Napi::Number::New(env, (double)f));
199
+ } else if (member.type->primitive == PrimitiveKind::Float64) {
200
+ double d = *(double *)ptr;
201
+ obj.Set(member.name, Napi::Number::New(env, d));
202
+ } else {
203
+ RG_UNREACHABLE();
204
+ }
205
+
206
+ ptr += member.type->size;
207
+ }
208
+
209
+ return obj;
210
+ }
211
+
212
+ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
213
+ {
214
+ Napi::Env env = info.Env();
215
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
216
+
217
+ FunctionInfo *func = (FunctionInfo *)info.Data();
218
+ LibraryData *lib = func->lib.get();
219
+
220
+ RG_DEFER { lib->tmp_alloc.ReleaseAll(); };
221
+
222
+ // Sanity checks
223
+ if (info.Length() < (uint32_t)func->parameters.len) {
224
+ ThrowError<Napi::TypeError>(env, "Expected %1 arguments, got %2", func->parameters.len, info.Length());
225
+ return env.Null();
226
+ }
227
+
228
+ // Stack pointer and register
229
+ uint8_t *top_ptr = lib->stack.end();
230
+ uint8_t *return_ptr = nullptr;
231
+ uint8_t *args_ptr = nullptr;
232
+ uint32_t *gpr_ptr = nullptr;
233
+ uint32_t *vec_ptr = nullptr;
234
+ uint8_t *sp_ptr = nullptr;
235
+
236
+ // Return through registers unless it's too big
237
+ if (!func->ret.use_memory) {
238
+ args_ptr = top_ptr - func->scratch_size;
239
+ gpr_ptr = (uint32_t *)args_ptr - 4;
240
+ vec_ptr = gpr_ptr - 16;
241
+ sp_ptr = (uint8_t *)vec_ptr;
242
+
243
+ #ifdef RG_DEBUG
244
+ memset(sp_ptr, 0, top_ptr - sp_ptr);
245
+ #endif
246
+ } else {
247
+ return_ptr = top_ptr - AlignLen(func->ret.type->size, 16);
248
+
249
+ args_ptr = return_ptr - func->scratch_size;
250
+ gpr_ptr = (uint32_t *)args_ptr - 4;
251
+ vec_ptr = gpr_ptr - 16;
252
+ sp_ptr = (uint8_t *)vec_ptr;
253
+
254
+ #ifdef RG_DEBUG
255
+ memset(sp_ptr, 0, top_ptr - sp_ptr);
256
+ #endif
257
+
258
+ *(gpr_ptr++) = (uint32_t)return_ptr;
259
+ }
260
+
261
+ RG_ASSERT(AlignUp(lib->stack.ptr, 16) == lib->stack.ptr);
262
+ RG_ASSERT(AlignUp(lib->stack.end(), 16) == lib->stack.end());
263
+ RG_ASSERT(AlignUp(sp_ptr, 16) == sp_ptr);
264
+
265
+ // Push arguments
266
+ for (Size i = 0; i < func->parameters.len; i++) {
267
+ const ParameterInfo &param = func->parameters[i];
268
+ Napi::Value value = info[i];
269
+
270
+ switch (param.type->primitive) {
271
+ case PrimitiveKind::Void: { RG_UNREACHABLE(); } break;
272
+
273
+ case PrimitiveKind::Bool: {
274
+ if (RG_UNLIKELY(!value.IsBoolean())) {
275
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected boolean", GetValueType(instance, value), i + 1);
276
+ return env.Null();
277
+ }
278
+
279
+ bool b = value.As<Napi::Boolean>();
280
+
281
+ if (RG_LIKELY(param.gpr_count)) {
282
+ *(gpr_ptr++) = (uint32_t)b;
283
+ } else {
284
+ *(args_ptr++) = (uint8_t)b;
285
+ }
286
+ } break;
287
+ case PrimitiveKind::Int8:
288
+ case PrimitiveKind::UInt8:
289
+ case PrimitiveKind::Int16:
290
+ case PrimitiveKind::UInt16:
291
+ case PrimitiveKind::Int32:
292
+ case PrimitiveKind::UInt32: {
293
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
294
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
295
+ return env.Null();
296
+ }
297
+
298
+ int64_t v = CopyNodeNumber<int64_t>(value);
299
+
300
+ if (RG_LIKELY(param.gpr_count)) {
301
+ *(gpr_ptr++) = (uint32_t)v;
302
+ } else {
303
+ args_ptr = AlignUp(args_ptr, param.type->align);
304
+ memcpy(args_ptr, &v, param.type->size); // Little Endian
305
+ args_ptr += param.type->size;
306
+ }
307
+ } break;
308
+ case PrimitiveKind::Int64:
309
+ case PrimitiveKind::UInt64: {
310
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
311
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
312
+ return env.Null();
313
+ }
314
+
315
+ int64_t v = CopyNodeNumber<int64_t>(value);
316
+
317
+ if (RG_LIKELY(param.gpr_count)) {
318
+ *(uint64_t *)gpr_ptr = (uint64_t)v;
319
+ gpr_ptr += 2;
320
+ } else {
321
+ args_ptr = AlignUp(args_ptr, 8);
322
+ memcpy(args_ptr, &v, param.type->size); // Little Endian
323
+ args_ptr += 8;
324
+ }
325
+ } break;
326
+ case PrimitiveKind::Float32: {
327
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
328
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
329
+ return env.Null();
330
+ }
331
+
332
+ float f = CopyNodeNumber<float>(value);
333
+
334
+ if (RG_LIKELY(param.vec_count)) {
335
+ memcpy(vec_ptr++, &f, 4);
336
+ } else {
337
+ args_ptr = AlignUp(args_ptr, 4);
338
+ memcpy(args_ptr, &f, 4);
339
+ args_ptr += 4;
340
+ }
341
+ } break;
342
+ case PrimitiveKind::Float64: {
343
+ if (RG_UNLIKELY(!value.IsNumber() && !value.IsBigInt())) {
344
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected number", GetValueType(instance, value), i + 1);
345
+ return env.Null();
346
+ }
347
+
348
+ double d = CopyNodeNumber<double>(value);
349
+
350
+ if (RG_LIKELY(param.vec_count)) {
351
+ memcpy(vec_ptr, &d, 8);
352
+ vec_ptr += 2;
353
+ } else {
354
+ args_ptr = AlignUp(args_ptr, 8);
355
+ memcpy(args_ptr, &d, 8);
356
+ args_ptr += 8;
357
+ }
358
+ } break;
359
+ case PrimitiveKind::String: {
360
+ if (RG_UNLIKELY(!value.IsString())) {
361
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected string", GetValueType(instance, value), i + 1);
362
+ return env.Null();
363
+ }
364
+
365
+ const char *str = CopyNodeString(value, &lib->tmp_alloc);
366
+
367
+ if (RG_LIKELY(param.gpr_count)) {
368
+ *(gpr_ptr++) = (uint64_t)str;
369
+ } else {
370
+ args_ptr = AlignUp(args_ptr, 4);
371
+ *(const char **)args_ptr = str;
372
+ args_ptr += 4;
373
+ }
374
+ } break;
375
+ case PrimitiveKind::Pointer: {
376
+ if (RG_UNLIKELY(!CheckValueTag(instance, value, param.type))) {
377
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected %3", GetValueType(instance, value), i + 1, param.type->name);
378
+ return env.Null();
379
+ }
380
+
381
+ void *ptr = value.As<Napi::External<void>>();
382
+
383
+ if (RG_LIKELY(param.gpr_count)) {
384
+ *(gpr_ptr++) = (uint64_t)ptr;
385
+ } else {
386
+ args_ptr = AlignUp(args_ptr, 4);
387
+ *(void **)args_ptr = ptr;
388
+ args_ptr += 4;
389
+ }
390
+ } break;
391
+
392
+ case PrimitiveKind::Record: {
393
+ if (RG_UNLIKELY(!value.IsObject())) {
394
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for argument %2, expected object", GetValueType(instance, value), i + 1);
395
+ return env.Null();
396
+ }
397
+
398
+ Napi::Object obj = value.As<Napi::Object>();
399
+
400
+ if (param.vec_count) {
401
+ if (!PushHFA(obj, param.type, (uint8_t *)vec_ptr))
402
+ return env.Null();
403
+ vec_ptr += param.vec_count;
404
+ } else {
405
+ if (param.gpr_count) {
406
+ RG_ASSERT(param.type->align <= 8);
407
+
408
+ if (!PushObject(obj, param.type, &lib->tmp_alloc, (uint8_t *)gpr_ptr))
409
+ return env.Null();
410
+
411
+ args_ptr += AlignLen(param.type->size - param.gpr_count * 4, 4);
412
+ gpr_ptr += param.gpr_count;
413
+ } else if (param.type->size) {
414
+ int16_t align = (param.type->align <= 4) ? 4 : 8;
415
+
416
+ args_ptr = AlignUp(args_ptr, align);
417
+ if (!PushObject(obj, param.type, &lib->tmp_alloc, args_ptr))
418
+ return env.Null();
419
+ args_ptr += AlignLen(param.type->size, 4);
420
+ }
421
+ }
422
+ } break;
423
+ }
424
+ }
425
+
426
+ // DumpStack(func, MakeSpan(sp_ptr, top_ptr - sp_ptr));
427
+
428
+ #define PERFORM_CALL(Suffix) \
429
+ (func->forward_fp ? ForwardCallX ## Suffix(func->func, sp_ptr) \
430
+ : ForwardCall ## Suffix(func->func, sp_ptr))
431
+
432
+ // Execute and convert return value
433
+ switch (func->ret.type->primitive) {
434
+ case PrimitiveKind::Float32: {
435
+ float f = PERFORM_CALL(F);
436
+
437
+ return Napi::Number::New(env, (double)f);
438
+ } break;
439
+
440
+ case PrimitiveKind::Float64: {
441
+ HfaRet ret = PERFORM_CALL(DDDD);
442
+
443
+ return Napi::Number::New(env, (double)ret.d0);
444
+ } break;
445
+
446
+ case PrimitiveKind::Record: {
447
+ if (func->ret.gpr_count) {
448
+ uint64_t ret = PERFORM_CALL(GG);
449
+ uint32_t r0 = (uint32_t)ret;
450
+
451
+ Napi::Object obj = PopObject(env, (const uint8_t *)&r0, func->ret.type);
452
+ return obj;
453
+ } else if (func->ret.vec_count) {
454
+ HfaRet ret = PERFORM_CALL(DDDD);
455
+
456
+ Napi::Object obj = PopHFA(env, (const uint8_t *)&ret, func->ret.type);
457
+ return obj;
458
+ } else if (func->ret.type->size) {
459
+ RG_ASSERT(return_ptr);
460
+
461
+ uint64_t ret = PERFORM_CALL(GG);
462
+ uint64_t r0 = (uint32_t)ret;
463
+ RG_ASSERT(r0 == (uint64_t)return_ptr);
464
+
465
+ Napi::Object obj = PopObject(env, return_ptr, func->ret.type);
466
+ return obj;
467
+ } else {
468
+ PERFORM_CALL(GG);
469
+
470
+ Napi::Object obj = Napi::Object::New(env);
471
+ return obj;
472
+ }
473
+ } break;
474
+
475
+ default: {
476
+ uint64_t ret = PERFORM_CALL(GG);
477
+ uint32_t r0 = (uint32_t)ret;
478
+
479
+ switch (func->ret.type->primitive) {
480
+ case PrimitiveKind::Void: return env.Null();
481
+ case PrimitiveKind::Bool: return Napi::Boolean::New(env, r0);
482
+ case PrimitiveKind::Int8: return Napi::Number::New(env, (double)r0);
483
+ case PrimitiveKind::UInt8: return Napi::Number::New(env, (double)r0);
484
+ case PrimitiveKind::Int16: return Napi::Number::New(env, (double)r0);
485
+ case PrimitiveKind::UInt16: return Napi::Number::New(env, (double)r0);
486
+ case PrimitiveKind::Int32: return Napi::Number::New(env, (double)r0);
487
+ case PrimitiveKind::UInt32: return Napi::Number::New(env, (double)r0);
488
+ case PrimitiveKind::Int64: return Napi::BigInt::New(env, (int64_t)ret);
489
+ case PrimitiveKind::UInt64: return Napi::BigInt::New(env, ret);
490
+ case PrimitiveKind::Float32: { RG_UNREACHABLE(); } break;
491
+ case PrimitiveKind::Float64: { RG_UNREACHABLE(); } break;
492
+ case PrimitiveKind::String: return Napi::String::New(env, (const char *)r0);
493
+ case PrimitiveKind::Pointer: {
494
+ void *ptr = (void *)r0;
495
+
496
+ Napi::External<void> external = Napi::External<void>::New(env, ptr);
497
+ SetValueTag(instance, external, func->ret.type);
498
+
499
+ return external;
500
+ } break;
501
+
502
+ case PrimitiveKind::Record: { RG_UNREACHABLE(); } break;
503
+ }
504
+ } break;
505
+ }
506
+
507
+ #undef PERFORM_CALL
508
+
509
+ RG_UNREACHABLE();
510
+ }
511
+
512
+ }
513
+
514
+ #endif
@@ -0,0 +1,108 @@
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
+ .syntax unified
15
+
16
+ // These three are the same, but they differ (in the C side) by their return type.
17
+ // Unlike the three next functions, these ones don't forward XMM argument registers.
18
+ .global ForwardCallGG
19
+ .global ForwardCallF
20
+ .global ForwardCallDDDD
21
+
22
+ // The X variants are slightly slower, and are used when XMM arguments must be forwarded.
23
+ .global ForwardCallXGG
24
+ .global ForwardCallXF
25
+ .global ForwardCallXDDDD
26
+
27
+ // Copy function pointer to r12, in order to save it through argument forwarding.
28
+ // Save RSP in fp (non-volatile), and use carefully assembled stack provided by caller.
29
+ .macro prologue
30
+ .cfi_startproc
31
+ push {fp, lr}
32
+ .cfi_def_cfa sp, 8
33
+ .cfi_offset 11, 4
34
+ .cfi_offset 14, 8
35
+ mov fp, sp
36
+ .cfi_def_cfa fp, 8
37
+ mov r12, r0
38
+ mov sp, r1
39
+ add sp, sp, #80
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, and or FP registers
45
+ .macro epilogue
46
+ blx r12
47
+ mov sp, fp
48
+ .cfi_def_cfa sp, 8
49
+ pop {fp, lr}
50
+ .cfi_def_cfa sp, 0
51
+ .cfi_restore 11
52
+ .cfi_restore 14
53
+ bx lr
54
+ .cfi_endproc
55
+ .endm
56
+
57
+ // Prepare general purpose argument registers from array passed by caller.
58
+ .macro forward_int
59
+ ldr r3, [r1, 76]
60
+ ldr r2, [r1, 72]
61
+ ldr r0, [r1, 64]
62
+ ldr r1, [r1, 68]
63
+ .endm
64
+
65
+ // Prepare vector argument registers from array passed by caller.
66
+ .macro forward_vec
67
+ vldr d7, [r1, 56]
68
+ vldr d6, [r1, 48]
69
+ vldr d5, [r1, 40]
70
+ vldr d4, [r1, 32]
71
+ vldr d3, [r1, 24]
72
+ vldr d2, [r1, 16]
73
+ vldr d1, [r1, 8]
74
+ vldr d0, [r1, 0]
75
+ .endm
76
+
77
+ ForwardCallGG:
78
+ prologue
79
+ forward_int
80
+ epilogue
81
+
82
+ ForwardCallF:
83
+ prologue
84
+ forward_int
85
+ epilogue
86
+
87
+ ForwardCallDDDD:
88
+ prologue
89
+ forward_int
90
+ epilogue
91
+
92
+ ForwardCallXGG:
93
+ prologue
94
+ forward_vec
95
+ forward_int
96
+ epilogue
97
+
98
+ ForwardCallXF:
99
+ prologue
100
+ forward_vec
101
+ forward_int
102
+ epilogue
103
+
104
+ ForwardCallXDDDD:
105
+ prologue
106
+ forward_vec
107
+ forward_int
108
+ epilogue
package/src/call_arm64.cc CHANGED
@@ -66,6 +66,8 @@ bool AnalyseFunction(FunctionInfo *func)
66
66
  func->ret.vec_count = func->ret.type->members.len;
67
67
  } else if (func->ret.type->size <= 16) {
68
68
  func->ret.gpr_count = (func->ret.type->size + 7) / 8;
69
+ } else {
70
+ func->ret.use_memory = true;
69
71
  }
70
72
 
71
73
  int gpr_avail = 8;
@@ -104,17 +106,28 @@ bool AnalyseFunction(FunctionInfo *func)
104
106
  if (IsHFA(param.type)) {
105
107
  int vec_count = (int)param.type->members.len;
106
108
 
107
- param.vec_count = std::min(vec_avail, vec_count);
108
- vec_avail -= vec_count;
109
+ if (vec_count <= vec_avail) {
110
+ param.vec_count = vec_count;
111
+ vec_avail -= vec_count;
112
+ } else {
113
+ vec_avail = 0;
114
+ }
109
115
  } else if (param.type->size <= 16) {
110
116
  int gpr_count = (param.type->size + 7) / 8;
111
117
 
112
- param.gpr_count = std::min(gpr_avail, gpr_count);
113
- gpr_avail -= gpr_count;
118
+ if (gpr_count <= gpr_avail) {
119
+ param.gpr_count = gpr_count;
120
+ gpr_avail -= gpr_count;
121
+ } else {
122
+ gpr_avail = 0;
123
+ }
114
124
  } else if (gpr_avail) {
115
125
  // Big types (more than 16 bytes) are replaced by a pointer
116
- param.gpr_count = 1;
117
- gpr_avail -= 1;
126
+ if (gpr_avail) {
127
+ param.gpr_count = 1;
128
+ gpr_avail -= 1;
129
+ }
130
+ param.use_memory = true;
118
131
  }
119
132
  } break;
120
133
  }
@@ -209,11 +222,11 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
209
222
  uint8_t *sp_ptr = nullptr;
210
223
 
211
224
  // Return through registers unless it's too big
212
- if (!func->ret.type->size || func->ret.vec_count || func->ret.gpr_count) {
225
+ if (!func->ret.use_memory) {
213
226
  args_ptr = scratch_ptr - AlignLen(8 * func->parameters.len, 16);
214
227
  vec_ptr = (uint64_t *)args_ptr - 8;
215
228
  gpr_ptr = vec_ptr - 9;
216
- sp_ptr = (uint8_t *)(gpr_ptr - 7);
229
+ sp_ptr = (uint8_t *)gpr_ptr;
217
230
 
218
231
  #ifdef RG_DEBUG
219
232
  memset(sp_ptr, 0, top_ptr - sp_ptr);
@@ -224,7 +237,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
224
237
  args_ptr = return_ptr - AlignLen(8 * func->parameters.len, 16);
225
238
  vec_ptr = (uint64_t *)args_ptr - 8;
226
239
  gpr_ptr = vec_ptr - 9;
227
- sp_ptr = (uint8_t *)(gpr_ptr - 7);
240
+ sp_ptr = (uint8_t *)gpr_ptr;
228
241
 
229
242
  #ifdef RG_DEBUG
230
243
  memset(sp_ptr, 0, top_ptr - sp_ptr);
@@ -235,7 +248,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
235
248
 
236
249
  RG_ASSERT(AlignUp(lib->stack.ptr, 16) == lib->stack.ptr);
237
250
  RG_ASSERT(AlignUp(lib->stack.end(), 16) == lib->stack.end());
238
- RG_ASSERT(AlignUp(sp_ptr, 16) == sp_ptr);
251
+ RG_ASSERT(AlignUp(sp_ptr - 8, 16) == sp_ptr);
239
252
 
240
253
  // Push arguments
241
254
  for (Size i = 0; i < func->parameters.len; i++) {
@@ -359,7 +372,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
359
372
  if (!PushHFA(obj, param.type, (uint8_t *)vec_ptr))
360
373
  return env.Null();
361
374
  vec_ptr += param.vec_count;
362
- } else if (param.type->size <= 16) {
375
+ } else if (!param.use_memory) {
363
376
  if (param.gpr_count) {
364
377
  RG_ASSERT(param.type->align <= 8);
365
378
 
@@ -367,7 +380,9 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
367
380
  return env.Null();
368
381
  gpr_ptr += param.gpr_count;
369
382
  } else if (param.type->size) {
370
- args_ptr = AlignUp(args_ptr, param.type->align);
383
+ int16_t align = (param.type->align <= 4) ? 4 : 8;
384
+
385
+ args_ptr = AlignUp(args_ptr, align);
371
386
  if (!PushObject(obj, param.type, &lib->tmp_alloc, args_ptr))
372
387
  return env.Null();
373
388
  args_ptr += AlignLen(param.type->size, 8);
@@ -429,7 +444,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
429
444
  RG_ASSERT(return_ptr);
430
445
 
431
446
  X0X1Ret ret = PERFORM_CALL(GG);
432
- RG_ASSERT(ret.x0 = (uint64_t)return_ptr);
447
+ RG_ASSERT(ret.x0 == (uint64_t)return_ptr);
433
448
 
434
449
  Napi::Object obj = PopObject(env, return_ptr, func->ret.type);
435
450
  return obj;
@@ -11,8 +11,8 @@
11
11
  // You should have received a copy of the GNU Affero General Public License
12
12
  // along with this program. If not, see https://www.gnu.org/licenses/.
13
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.
14
+ // These three are the same, but they differ (in the C side) by their return type.
15
+ // Unlike the three next functions, these ones don't forward XMM argument registers.
16
16
  .global ForwardCallGG
17
17
  .global ForwardCallF
18
18
  .global ForwardCallDDDD
@@ -23,20 +23,17 @@
23
23
  .global ForwardCallXDDDD
24
24
 
25
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.
26
+ // Save RSP in r29 (non-volatile), and use carefully assembled stack provided by caller.
27
27
  .macro prologue
28
28
  .cfi_startproc
29
29
  stp x29, x30, [sp, -16]!
30
30
  .cfi_def_cfa sp, 16
31
31
  .cfi_offset 29, 16
32
32
  .cfi_offset 30, 8
33
- str x19, [sp, -16]!
34
- .cfi_def_cfa sp, 32
33
+ mov x29, sp
34
+ .cfi_def_cfa x29, 16
35
35
  mov x9, x0
36
- mov x19, sp
37
- .cfi_def_cfa x19, 32
38
- mov sp, x1
39
- add sp, sp, #192
36
+ add sp, x1, #136
40
37
  .endm
41
38
 
42
39
  // Call native function.
@@ -44,9 +41,7 @@
44
41
  // The return value is passed untouched through r0, r1, v0 and/or v1.
45
42
  .macro epilogue
46
43
  blr x9
47
- mov sp, x19
48
- .cfi_def_cfa sp, 32
49
- ldr x19, [sp], 16
44
+ mov sp, x29
50
45
  .cfi_def_cfa sp, 16
51
46
  ldp x29, x30, [sp], 16
52
47
  .cfi_def_cfa sp, 0
@@ -58,27 +53,27 @@
58
53
 
59
54
  // Prepare general purpose argument registers from array passed by caller.
60
55
  .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]
56
+ ldr x8, [x1, 64]
57
+ ldr x7, [x1, 56]
58
+ ldr x6, [x1, 48]
59
+ ldr x5, [x1, 40]
60
+ ldr x4, [x1, 32]
61
+ ldr x3, [x1, 24]
62
+ ldr x2, [x1, 16]
63
+ ldr x0, [x1, 0]
64
+ ldr x1, [x1, 8]
70
65
  .endm
71
66
 
72
67
  // Prepare vector argument registers from array passed by caller.
73
68
  .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]
69
+ ldr d7, [x1, 128]
70
+ ldr d6, [x1, 120]
71
+ ldr d5, [x1, 112]
72
+ ldr d4, [x1, 104]
73
+ ldr d3, [x1, 96]
74
+ ldr d2, [x1, 88]
75
+ ldr d1, [x1, 80]
76
+ ldr d0, [x1, 72]
82
77
  .endm
83
78
 
84
79
  ForwardCallGG:
@@ -228,7 +228,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
228
228
 
229
229
  RG_ASSERT(AlignUp(lib->stack.ptr, 16) == lib->stack.ptr);
230
230
  RG_ASSERT(AlignUp(lib->stack.end(), 16) == lib->stack.end());
231
- RG_ASSERT(AlignUp(args_ptr, 16) == args_ptr);
231
+ RG_ASSERT(AlignUp(sp_ptr, 16) == sp_ptr);
232
232
 
233
233
  // Push arguments
234
234
  for (Size i = 0; i < func->parameters.len; i++) {
package/src/call_x86.cc CHANGED
@@ -90,7 +90,7 @@ Napi::Value TranslateCall(const Napi::CallbackInfo &info)
90
90
 
91
91
  RG_ASSERT(AlignUp(lib->stack.ptr, 16) == lib->stack.ptr);
92
92
  RG_ASSERT(AlignUp(lib->stack.end(), 16) == lib->stack.end());
93
- RG_ASSERT(AlignUp(args_ptr, 16) == args_ptr);
93
+ RG_ASSERT(AlignUp(sp_ptr, 16) == sp_ptr);
94
94
 
95
95
  // Push arguments
96
96
  for (Size i = 0; i < func->parameters.len; i++) {
package/src/ffi.cc CHANGED
@@ -298,7 +298,7 @@ static Napi::Value LoadSharedLibrary(const Napi::CallbackInfo &info)
298
298
  }
299
299
 
300
300
  // That's enough extra space for pretty much every ABI supported, with 16 bytes of
301
- // bonus for each in case we need to put pointers and exta-align them.
301
+ // bonus for each parameter in case we need to put pointers and exta-align them.
302
302
  extra_size = std::max(extra_size, func->scratch_size +
303
303
  AlignLen(8 * (func->parameters.len + 1), 16));
304
304
 
package/src/ffi.hh CHANGED
@@ -96,7 +96,8 @@ struct ParameterInfo {
96
96
  int8_t gpr_count;
97
97
  int8_t xmm_count;
98
98
  bool gpr_first;
99
- #elif defined(__aarch64__)
99
+ #elif defined(__arm__) || defined(__aarch64__)
100
+ bool use_memory;
100
101
  int8_t gpr_count;
101
102
  int8_t vec_count;
102
103
  #elif defined(__i386__) || defined(_M_IX86)
@@ -118,7 +119,7 @@ struct FunctionInfo {
118
119
 
119
120
  // ABI-specific part
120
121
 
121
- #if defined(__aarch64__) || defined(__x86_64__) || defined(_WIN64)
122
+ #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) || defined(_WIN64)
122
123
  bool forward_fp;
123
124
  #endif
124
125
  };
package/src/util.cc CHANGED
@@ -284,9 +284,10 @@ void DumpStack(const FunctionInfo *func, Span<const uint8_t> sp)
284
284
 
285
285
  PrintLn(stderr, "Stack (%1 bytes) at 0x%2:", sp.len, sp.ptr);
286
286
  for (const uint8_t *ptr = sp.begin(); ptr < sp.end();) {
287
- Print(stderr, " [0x%1 %2 %3] ", FmtArg(ptr).Pad0(-16), FmtArg((ptr - sp.begin()) / 8).Pad(-4),
288
- FmtArg(ptr - sp.begin()).Pad(-4));
289
- for (int i = 0; ptr < sp.end() && i < 8; i++, ptr++) {
287
+ Print(stderr, " [0x%1 %2 %3] ", FmtArg(ptr).Pad0(-16),
288
+ FmtArg((ptr - sp.begin()) / sizeof(void *)).Pad(-4),
289
+ FmtArg(ptr - sp.begin()).Pad(-4));
290
+ for (int i = 0; ptr < sp.end() && i < sizeof(void *); i++, ptr++) {
290
291
  Print(stderr, " %1", FmtHex(*ptr).Pad0(-2));
291
292
  }
292
293
  PrintLn(stderr);
@@ -2055,7 +2055,9 @@ namespace jkj::dragonbox {
2055
2055
  // Using an upper bound on xi, we might be able to optimize the division
2056
2056
  // better than the compiler; we are computing xi / big_divisor here.
2057
2057
  ret_value.significand =
2058
- div::divide_by_pow10<kappa + 1, significand_bits + kappa + 2, kappa + 1>(xi);
2058
+ div::divide_by_pow10<kappa + 1, carrier_uint,
2059
+ (carrier_uint(1) << (significand_bits + 1)) * big_divisor -
2060
+ 1>(xi);
2059
2061
  auto r = std::uint32_t(xi - big_divisor * ret_value.significand);
2060
2062
 
2061
2063
  if (r != 0) {
@@ -2130,7 +2132,9 @@ namespace jkj::dragonbox {
2130
2132
  // Using an upper bound on zi, we might be able to optimize the division better than
2131
2133
  // the compiler; we are computing zi / big_divisor here.
2132
2134
  ret_value.significand =
2133
- div::divide_by_pow10<kappa + 1, significand_bits + kappa + 2, kappa + 1>(zi);
2135
+ div::divide_by_pow10<kappa + 1, carrier_uint,
2136
+ (carrier_uint(1) << (significand_bits + 1)) * big_divisor -
2137
+ 1>(zi);
2134
2138
  auto const r = std::uint32_t(zi - big_divisor * ret_value.significand);
2135
2139
 
2136
2140
  if (r > deltai) {