@finogeeks2026/finshield-opencode 0.0.0-stage → 0.11.50

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.
Files changed (143) hide show
  1. package/DESIGN.md +593 -0
  2. package/LICENSE.rtf +28 -0
  3. package/README.md +132 -2
  4. package/THIRD-PARTY-NOTICES.md +3131 -0
  5. package/bin/node.exe +4 -0
  6. package/dist/gui-experimental.cjs +96 -0
  7. package/dist/updater/server.js +1046 -0
  8. package/install/download-source.json +3 -0
  9. package/install/runtime.js +60 -0
  10. package/node_modules/@finogeeks2026/finsafe-sdk/LICENSE.rtf +28 -0
  11. package/node_modules/@finogeeks2026/finsafe-sdk/README.md +116 -0
  12. package/node_modules/@finogeeks2026/finsafe-sdk/bin/finsafe_native.dll +0 -0
  13. package/node_modules/@finogeeks2026/finsafe-sdk/native-build.json +5 -0
  14. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/README.md +6 -0
  15. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/index.js +12 -0
  16. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/package.json +24 -0
  17. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.lib +0 -0
  18. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/@koromix/koffi-win32-x64/win32_x64/koffi.node +0 -0
  19. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/CHANGELOG.md +1183 -0
  20. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/LICENSE.txt +22 -0
  21. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/README.md +42 -0
  22. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/cnoke.cjs +816 -0
  23. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/benchmarks.md +80 -0
  24. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/callbacks.md +182 -0
  25. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/composites.md +549 -0
  26. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/contribute.md +163 -0
  27. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/index.md +46 -0
  28. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/load.md +191 -0
  29. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/migration.md +256 -0
  30. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/misc.md +76 -0
  31. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/output.md +299 -0
  32. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/pointers.md +385 -0
  33. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/primitives.md +151 -0
  34. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/start.md +110 -0
  35. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/types.md +88 -0
  36. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/unions.md +0 -0
  37. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/doc/values.md +134 -0
  38. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.cjs +1 -0
  39. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.d.ts +389 -0
  40. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/index.js +2 -0
  41. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.cjs +1 -0
  42. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/indirect.js +2 -0
  43. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.cc +11064 -0
  44. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/base.hh +5951 -0
  45. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/lib/native/base/unicode.inc +408 -0
  46. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/package.json +68 -0
  47. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/CMakeLists.txt +249 -0
  48. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.cjs +296 -0
  49. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/index.js +281 -0
  50. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.cjs +295 -0
  51. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/indirect.js +280 -0
  52. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64.cc +1408 -0
  53. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.S +216 -0
  54. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/arm64_asm.asm +181 -0
  55. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64.cc +5 -0
  56. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/loong64_asm.S +200 -0
  57. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64.cc +1345 -0
  58. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/riscv64_asm.S +200 -0
  59. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv.cc +1434 -0
  60. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64sysv_asm.S +224 -0
  61. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win.cc +1086 -0
  62. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.S +162 -0
  63. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x64win_asm.asm +189 -0
  64. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86.cc +1185 -0
  65. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.S +217 -0
  66. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/abi/x86_asm.asm +174 -0
  67. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.cc +2094 -0
  68. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/call.hh +179 -0
  69. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/errno.inc +462 -0
  70. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.cc +3009 -0
  71. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/ffi.hh +461 -0
  72. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.cc +218 -0
  73. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/parser.hh +54 -0
  74. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/primitives.inc +36 -0
  75. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.cjs +130 -0
  76. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/static.js +133 -0
  77. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/trampolines.cjs +77 -0
  78. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.cc +725 -0
  79. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/type.hh +71 -0
  80. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.cc +1040 -0
  81. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/util.hh +493 -0
  82. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.cc +214 -0
  83. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.def +11 -0
  84. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/uv.hh +112 -0
  85. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.cc +198 -0
  86. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/src/koffi/src/win32.hh +148 -0
  87. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/LICENSE.md +9 -0
  88. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/README.md +95 -0
  89. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.deprecated.h +186 -0
  90. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi-inl.h +7211 -0
  91. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-addon-api/napi.h +3421 -0
  92. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/LICENSE +21 -0
  93. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/README.md +95 -0
  94. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/js_native_api.def +125 -0
  95. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/def/node_api.def +157 -0
  96. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api.h +591 -0
  97. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/js_native_api_types.h +210 -0
  98. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api.h +265 -0
  99. package/node_modules/@finogeeks2026/finsafe-sdk/node_modules/koffi/vendor/node-api-headers/include/node_api_types.h +58 -0
  100. package/node_modules/@finogeeks2026/finsafe-sdk/package.json +36 -0
  101. package/node_modules/@finogeeks2026/finsafe-sdk/release.json +4 -0
  102. package/node_modules/@finogeeks2026/finsafe-sdk/src/native.js +85 -0
  103. package/node_modules/@finogeeks2026/finsafe-sdk/src/shell.js +20 -0
  104. package/node_modules/@finogeeks2026/finsafe-sdk/src/status.js +57 -0
  105. package/package.json +47 -4
  106. package/policy/candidate.yaml +347 -0
  107. package/release.json +4 -0
  108. package/src/activation.js +2 -0
  109. package/src/attachment.js +34 -0
  110. package/src/connector.js +91 -0
  111. package/src/contract.js +8 -0
  112. package/src/desktop-contract.js +4 -0
  113. package/src/desktop.js +25 -0
  114. package/src/gui.tsx +58 -0
  115. package/src/host.js +38 -0
  116. package/src/index.js +12 -0
  117. package/src/instance-settings.js +27 -0
  118. package/src/known-folders.js +22 -0
  119. package/src/management.js +17 -0
  120. package/src/preparation.js +18 -0
  121. package/src/presentation.js +153 -0
  122. package/src/settings.js +18 -0
  123. package/ui/feedback.cjs +45 -0
  124. package/ui/page.cjs +225 -0
  125. package/ui/state.cjs +44 -0
  126. package/updater/bootstrap.js +41 -0
  127. package/updater/common/activation.js +81 -0
  128. package/updater/common/coordinator.js +329 -0
  129. package/updater/common/download.js +57 -0
  130. package/updater/common/index.js +12 -0
  131. package/updater/common/package.js +53 -0
  132. package/updater/common/protocol.js +38 -0
  133. package/updater/common/transport.js +65 -0
  134. package/updater/coordinator.js +2 -0
  135. package/updater/launch.js +40 -0
  136. package/updater/management.js +9 -0
  137. package/updater/native.js +18 -0
  138. package/updater/operations.js +45 -0
  139. package/updater/protocol.js +1 -0
  140. package/updater/server.js +100 -0
  141. package/updater/transport.js +10 -0
  142. package/updater/windows-launch-reader.js +40 -0
  143. package/updater/windows-launch.js +96 -0
@@ -0,0 +1,1434 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // SPDX-FileCopyrightText: 2026 Niels Martignène <niels.martignene@protonmail.com>
3
+
4
+ #if defined(__x86_64__) && !defined(_WIN32)
5
+
6
+ #include "lib/native/base/base.hh"
7
+ #include "../ffi.hh"
8
+ #include "../call.hh"
9
+ #include "../type.hh"
10
+ #include "../util.hh"
11
+
12
+ #include <napi.h>
13
+
14
+ namespace K {
15
+
16
+ struct RaxRdxRet {
17
+ uint64_t rax;
18
+ uint64_t rdx;
19
+ };
20
+ struct RaxXmm0Ret {
21
+ uint64_t rax;
22
+ double xmm0;
23
+ };
24
+ struct Xmm0RaxRet {
25
+ double xmm0;
26
+ uint64_t rax;
27
+ };
28
+ struct Xmm0Xmm1Ret {
29
+ double xmm0;
30
+ double xmm1;
31
+ };
32
+
33
+ struct BackRegisters {
34
+ uint64_t rax;
35
+ uint64_t rdx;
36
+ double xmm0;
37
+ double xmm1;
38
+ };
39
+
40
+ extern "C" RaxRdxRet ForwardCallGG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
41
+ extern "C" float ForwardCallF(const void *func, uint8_t *sp, uint8_t **out_old_sp);
42
+ extern "C" Xmm0RaxRet ForwardCallDG(const void *func, uint8_t *sp, uint8_t **out_old_sp);
43
+ extern "C" RaxXmm0Ret ForwardCallGD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
44
+ extern "C" Xmm0Xmm1Ret ForwardCallDD(const void *func, uint8_t *sp, uint8_t **out_old_sp);
45
+
46
+ extern "C" RaxRdxRet ForwardCallGGX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
47
+ extern "C" float ForwardCallFX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
48
+ extern "C" Xmm0RaxRet ForwardCallDGX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
49
+ extern "C" RaxXmm0Ret ForwardCallGDX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
50
+ extern "C" Xmm0Xmm1Ret ForwardCallDDX(const void *func, uint8_t *sp, uint8_t **out_old_sp);
51
+
52
+ enum class AbiOpcode {
53
+ #define PRIMITIVE(Name) Push ## Name,
54
+ #include "../primitives.inc"
55
+ PushAggregateReg,
56
+ PushAggregateStack,
57
+ #define PRIMITIVE(Name) Run ## Name,
58
+ #include "../primitives.inc"
59
+ RunAggregateGG,
60
+ RunAggregateDD,
61
+ RunAggregateGD,
62
+ RunAggregateDG,
63
+ RunAggregateMem,
64
+ #define PRIMITIVE(Name) Run ## Name ## X,
65
+ #include "../primitives.inc"
66
+ RunAggregateGGX,
67
+ RunAggregateDDX,
68
+ RunAggregateGDX,
69
+ RunAggregateDGX,
70
+ RunAggregateMemX,
71
+ Yield,
72
+ CallGG,
73
+ CallF,
74
+ CallDG,
75
+ CallGD,
76
+ CallDD,
77
+ CallStack,
78
+ CallGGX,
79
+ CallFX,
80
+ CallDGX,
81
+ CallGDX,
82
+ CallDDX,
83
+ CallStackX,
84
+ #define PRIMITIVE(Name) Return ## Name,
85
+ #include "../primitives.inc"
86
+ ReturnAggregateReg,
87
+ ReturnAggregateMem
88
+ };
89
+
90
+ enum class AbiMethod {
91
+ Stack,
92
+ Gpr,
93
+ GprGpr,
94
+ Xmm,
95
+ XmmXmm,
96
+ GprXmm,
97
+ XmmGpr
98
+ };
99
+
100
+ struct ClassResult {
101
+ AbiMethod method;
102
+
103
+ int gpr_index;
104
+ int xmm_index;
105
+ Size stack_offset;
106
+ };
107
+
108
+ namespace {
109
+ #if defined(MUST_TAIL)
110
+ PRESERVE_NONE typedef napi_value ForwardFunc(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst);
111
+
112
+ extern ForwardFunc *const ForwardDispatch[256];
113
+
114
+ inline void *Code2Op(AbiOpcode code)
115
+ {
116
+ return (void *)ForwardDispatch[(int)code];
117
+ }
118
+ #else
119
+ inline void *Code2Op(AbiOpcode code)
120
+ {
121
+ return (void *)code;
122
+ }
123
+ #endif
124
+ }
125
+
126
+ class ClassAnalyser {
127
+ enum class RegisterClass {
128
+ NoClass = 0, // Explicitly 0
129
+ Integer,
130
+ SSE,
131
+ Memory
132
+ };
133
+
134
+ int gpr_max;
135
+ int xmm_max;
136
+ int gpr_avail;
137
+ int xmm_avail;
138
+
139
+ Size stack_offset = 0;
140
+
141
+ public:
142
+ ClassAnalyser(int gpr_index, int gpr_max, int xmm_index, int xmm_max)
143
+ : gpr_max(gpr_max), xmm_max(xmm_max), gpr_avail(gpr_max - gpr_index), xmm_avail(xmm_max - xmm_index) {}
144
+
145
+ ClassResult Analyse(const TypeInfo *type);
146
+
147
+ Size Classify(Span<RegisterClass> classes, const TypeInfo *type, Size offset);
148
+ RegisterClass MergeClasses(RegisterClass cls1, RegisterClass cls2);
149
+
150
+ int GprCount() const { return gpr_max - gpr_avail; }
151
+ int XmmCount() const { return xmm_max - xmm_avail; }
152
+ Size StackOffset() const { return stack_offset; }
153
+ };
154
+
155
+ ClassResult ClassAnalyser::Analyse(const TypeInfo *type)
156
+ {
157
+ ClassResult ret = {};
158
+
159
+ LocalArray<RegisterClass, 8> classes = {};
160
+ classes.len = Classify(classes.data, type, 0);
161
+
162
+ if (classes.len <= 2) {
163
+ int gpr_count = 0;
164
+ int xmm_count = 0;
165
+
166
+ for (RegisterClass cls: classes) {
167
+ switch (cls) {
168
+ case RegisterClass::NoClass: { K_UNREACHABLE(); } break;
169
+ case RegisterClass::Integer: { gpr_count++; } break;
170
+ case RegisterClass::SSE: { xmm_count++; } break;
171
+ case RegisterClass::Memory: goto stack;
172
+ }
173
+ }
174
+
175
+ if (gpr_count > gpr_avail || xmm_count > xmm_avail)
176
+ goto stack;
177
+
178
+ if (gpr_count && xmm_count) {
179
+ bool gpr_xmm = (classes.len && classes[0] == RegisterClass::Integer);
180
+ ret.method = gpr_xmm ? AbiMethod::GprXmm : AbiMethod::XmmGpr;
181
+ } else if (xmm_count == 2) {
182
+ ret.method = AbiMethod::XmmXmm;
183
+ } else if (xmm_count == 1) {
184
+ ret.method = AbiMethod::Xmm;
185
+ } else if (gpr_count == 2) {
186
+ ret.method = AbiMethod::GprGpr;
187
+ } else {
188
+ K_ASSERT(gpr_count <= 1 && !xmm_count);
189
+ ret.method = AbiMethod::Gpr;
190
+ }
191
+
192
+ ret.gpr_index = (gpr_max - gpr_avail);
193
+ ret.xmm_index = (xmm_max - xmm_avail);
194
+
195
+ gpr_avail -= gpr_count;
196
+ xmm_avail -= xmm_count;
197
+
198
+ return ret;
199
+ }
200
+
201
+ stack:
202
+ {
203
+ ret.method = AbiMethod::Stack;
204
+ ret.stack_offset = stack_offset;
205
+
206
+ stack_offset += AlignLen(type->size, 8);
207
+ }
208
+
209
+ return ret;
210
+ }
211
+
212
+ Size ClassAnalyser::Classify(Span<RegisterClass> classes, const TypeInfo *type, Size offset)
213
+ {
214
+ K_ASSERT(classes.len > 0);
215
+
216
+ switch (type->primitive) {
217
+ case PrimitiveKind::Void: { return 0; } break;
218
+
219
+ case PrimitiveKind::Bool:
220
+ case PrimitiveKind::Int8:
221
+ case PrimitiveKind::UInt8:
222
+ case PrimitiveKind::Int16:
223
+ case PrimitiveKind::Int16S:
224
+ case PrimitiveKind::UInt16:
225
+ case PrimitiveKind::UInt16S:
226
+ case PrimitiveKind::Int32:
227
+ case PrimitiveKind::Int32S:
228
+ case PrimitiveKind::UInt32:
229
+ case PrimitiveKind::UInt32S:
230
+ case PrimitiveKind::Int64:
231
+ case PrimitiveKind::Int64S:
232
+ case PrimitiveKind::UInt64:
233
+ case PrimitiveKind::UInt64S:
234
+ case PrimitiveKind::String:
235
+ case PrimitiveKind::String16:
236
+ case PrimitiveKind::String32:
237
+ case PrimitiveKind::Pointer:
238
+ case PrimitiveKind::Callback: {
239
+ classes[0] = MergeClasses(classes[0], RegisterClass::Integer);
240
+ return 1;
241
+ } break;
242
+ case PrimitiveKind::Record: {
243
+ if (type->size > 64) {
244
+ classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
245
+ return 1;
246
+ }
247
+
248
+ for (const RecordMember &member: type->members) {
249
+ Size member_offset = offset + member.offset;
250
+ Size start = member_offset / 8;
251
+ Classify(classes.Take(start, classes.len - start), member.type, member_offset % 8);
252
+ }
253
+ offset += type->size;
254
+
255
+ return (offset + 7) / 8;
256
+ } break;
257
+ case PrimitiveKind::Union: {
258
+ if (type->size > 64) {
259
+ classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
260
+ return 1;
261
+ }
262
+
263
+ for (const RecordMember &member: type->members) {
264
+ Size start = offset / 8;
265
+ Classify(classes.Take(start, classes.len - start), member.type, offset % 8);
266
+ }
267
+ offset += type->size;
268
+
269
+ return (offset + 7) / 8;
270
+ } break;
271
+ case PrimitiveKind::Array: {
272
+ if (type->size > 64) {
273
+ classes[0] = MergeClasses(classes[0], RegisterClass::Memory);
274
+ return 1;
275
+ }
276
+
277
+ Size len = type->size / type->ref.type->size;
278
+
279
+ for (Size i = 0; i < len; i++) {
280
+ Size start = offset / 8;
281
+ Classify(classes.Take(start, classes.len - start), type->ref.type, offset % 8);
282
+ offset += type->ref.type->size;
283
+ }
284
+
285
+ return (offset + 7) / 8;
286
+ } break;
287
+ case PrimitiveKind::Float32:
288
+ case PrimitiveKind::Float64: {
289
+ classes[0] = MergeClasses(classes[0], RegisterClass::SSE);
290
+ return 1;
291
+ } break;
292
+
293
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
294
+ }
295
+
296
+ K_UNREACHABLE();
297
+ }
298
+
299
+ ClassAnalyser::RegisterClass ClassAnalyser::MergeClasses(RegisterClass cls1, RegisterClass cls2)
300
+ {
301
+ if (cls1 == cls2)
302
+ return cls1;
303
+
304
+ if (cls1 == RegisterClass::NoClass)
305
+ return cls2;
306
+ if (cls2 == RegisterClass::NoClass)
307
+ return cls1;
308
+
309
+ if (cls1 == RegisterClass::Memory || cls2 == RegisterClass::Memory)
310
+ return RegisterClass::Memory;
311
+ if (cls1 == RegisterClass::Integer || cls2 == RegisterClass::Integer)
312
+ return RegisterClass::Integer;
313
+
314
+ return RegisterClass::SSE;
315
+ }
316
+
317
+ bool AnalyseFunction(Napi::Env, InstanceData *, FunctionInfo *func)
318
+ {
319
+ // Handle return value
320
+ {
321
+ ClassAnalyser analyser(0, 2, 0, 2);
322
+ ClassResult ret = analyser.Analyse(func->ret.type);
323
+
324
+ func->ret.abi.method = ret.method;
325
+ }
326
+
327
+ // Handle parameters
328
+ {
329
+ int gpr_result = (func->ret.abi.method == AbiMethod::Stack);
330
+ ClassAnalyser analyser(gpr_result, 6, 0, 8);
331
+
332
+ for (ParameterInfo &param: func->parameters) {
333
+ ClassResult ret = analyser.Analyse(param.type);
334
+
335
+ switch (ret.method) {
336
+ case AbiMethod::Stack: {
337
+ param.abi.regular = false;
338
+ param.abi.offsets[0] = 16 * 8 + ret.stack_offset;
339
+ } break;
340
+ case AbiMethod::Gpr: {
341
+ param.abi.regular = true;
342
+ param.abi.offsets[0] = (0 + ret.gpr_index) * 8;
343
+ param.abi.offsets[1] = param.abi.offsets[0];
344
+ } break;
345
+ case AbiMethod::GprGpr: {
346
+ param.abi.regular = true;
347
+ param.abi.offsets[0] = (0 + ret.gpr_index) * 8;
348
+ param.abi.offsets[1] = param.abi.offsets[0] + 8;
349
+ } break;
350
+ case AbiMethod::Xmm: {
351
+ param.abi.regular = true;
352
+ param.abi.offsets[0] = (6 + ret.xmm_index) * 8;
353
+ param.abi.offsets[1] = param.abi.offsets[0];
354
+ } break;
355
+ case AbiMethod::XmmXmm: {
356
+ param.abi.regular = true;
357
+ param.abi.offsets[0] = (6 + ret.xmm_index) * 8;
358
+ param.abi.offsets[1] = param.abi.offsets[0] + 8;
359
+ } break;
360
+ case AbiMethod::GprXmm: {
361
+ param.abi.regular = true;
362
+ param.abi.offsets[0] = (0 + ret.gpr_index) * 8;
363
+ param.abi.offsets[1] = (6 + ret.xmm_index) * 8;
364
+ } break;
365
+ case AbiMethod::XmmGpr: {
366
+ param.abi.regular = true;
367
+ param.abi.offsets[0] = (6 + ret.xmm_index) * 8;
368
+ param.abi.offsets[1] = (0 + ret.gpr_index) * 8;
369
+ } break;
370
+ }
371
+
372
+ if (param.type->primitive == PrimitiveKind::Record || param.type->primitive == PrimitiveKind::Union) {
373
+ AbiOpcode code = param.abi.regular ? AbiOpcode::PushAggregateReg : AbiOpcode::PushAggregateStack;
374
+
375
+ func->sync.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.abi.offsets[1], .type = param.type });
376
+ func->async.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.abi.offsets[1], .type = param.type });
377
+ } else {
378
+ int delta = (int)AbiOpcode::PushVoid - (int)PrimitiveKind::Void;
379
+ AbiOpcode code = (AbiOpcode)((int)param.type->primitive + delta);
380
+
381
+ func->sync.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.directions, .type = param.type });
382
+ func->async.Append({ .op = Code2Op(code), .a = param.offset, .b1 = (int16_t)param.abi.offsets[0], .b2 = (int16_t)param.directions, .type = param.type });
383
+ }
384
+ }
385
+
386
+ func->stk_size = AlignLen(16 * 8 + analyser.StackOffset(), 16);
387
+ func->forward_fp = analyser.XmmCount();
388
+ }
389
+
390
+ func->async.Append({ .op = Code2Op(AbiOpcode::Yield) });
391
+
392
+ switch (func->ret.type->primitive) {
393
+ case PrimitiveKind::Void:
394
+ case PrimitiveKind::Bool:
395
+ case PrimitiveKind::Int8:
396
+ case PrimitiveKind::UInt8:
397
+ case PrimitiveKind::Int16:
398
+ case PrimitiveKind::Int16S:
399
+ case PrimitiveKind::UInt16:
400
+ case PrimitiveKind::UInt16S:
401
+ case PrimitiveKind::Int32:
402
+ case PrimitiveKind::Int32S:
403
+ case PrimitiveKind::UInt32:
404
+ case PrimitiveKind::UInt32S:
405
+ case PrimitiveKind::Int64:
406
+ case PrimitiveKind::Int64S:
407
+ case PrimitiveKind::UInt64:
408
+ case PrimitiveKind::UInt64S:
409
+ case PrimitiveKind::String:
410
+ case PrimitiveKind::String16:
411
+ case PrimitiveKind::String32: {
412
+ if (func->forward_fp) {
413
+ int delta = (int)AbiOpcode::RunVoidX - (int)PrimitiveKind::Void;
414
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
415
+
416
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
417
+ } else {
418
+ int delta = (int)AbiOpcode::RunVoid - (int)PrimitiveKind::Void;
419
+ AbiOpcode run = (AbiOpcode)((int)func->ret.type->primitive + delta);
420
+
421
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
422
+ }
423
+
424
+ // Async
425
+ {
426
+ int delta = (int)AbiOpcode::ReturnVoid - (int)PrimitiveKind::Void;
427
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
428
+ AbiOpcode ret = (AbiOpcode)((int)func->ret.type->primitive + delta);
429
+
430
+ func->async.Append({ .op = Code2Op(call) });
431
+ func->async.Append({ .op = Code2Op(ret), .type = func->ret.type });
432
+ }
433
+ } break;
434
+
435
+ case PrimitiveKind::Pointer: {
436
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunPointerX : AbiOpcode::RunPointer;
437
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
438
+
439
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type->ref.type });
440
+ func->async.Append({ .op = Code2Op(call) });
441
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnPointer), .type = func->ret.type->ref.type });
442
+ } break;
443
+ case PrimitiveKind::Callback: {
444
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunCallbackX : AbiOpcode::RunCallback;
445
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
446
+
447
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
448
+ func->async.Append({ .op = Code2Op(call) });
449
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnCallback), .type = func->ret.type });
450
+ } break;
451
+
452
+ case PrimitiveKind::Record:
453
+ case PrimitiveKind::Union: {
454
+ switch (func->ret.abi.method) {
455
+ case AbiMethod::Stack: {
456
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateMemX : AbiOpcode::RunAggregateMem;
457
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallStackX : AbiOpcode::CallStack;
458
+
459
+ func->sync.Append({ .op = Code2Op(run), .a = (int32_t)func->ret.type->size, .type = func->ret.type });
460
+ func->async.Append({ .op = Code2Op(call), .a = (int32_t)func->ret.type->size });
461
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateMem), .type = func->ret.type });
462
+
463
+ // Allocate stack space for return value
464
+ func->stk_size += AlignLen(func->ret.type->size, 16);
465
+ } break;
466
+ case AbiMethod::Gpr: {
467
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateGGX : AbiOpcode::RunAggregateGG;
468
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
469
+
470
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
471
+ func->async.Append({ .op = Code2Op(call) });
472
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
473
+ } break;
474
+ case AbiMethod::GprGpr: {
475
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateGGX : AbiOpcode::RunAggregateGG;
476
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGGX : AbiOpcode::CallGG;
477
+
478
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
479
+ func->async.Append({ .op = Code2Op(call) });
480
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
481
+ } break;
482
+ case AbiMethod::Xmm: {
483
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateDDX : AbiOpcode::RunAggregateDD;
484
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDDX : AbiOpcode::CallDD;
485
+
486
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
487
+ func->async.Append({ .op = Code2Op(call) });
488
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
489
+ } break;
490
+ case AbiMethod::XmmXmm: {
491
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateDDX : AbiOpcode::RunAggregateDD;
492
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDDX : AbiOpcode::CallDD;
493
+
494
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
495
+ func->async.Append({ .op = Code2Op(call) });
496
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
497
+ } break;
498
+ case AbiMethod::GprXmm: {
499
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateGDX : AbiOpcode::RunAggregateGD;
500
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallGDX : AbiOpcode::CallGD;
501
+
502
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
503
+ func->async.Append({ .op = Code2Op(call) });
504
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
505
+ } break;
506
+ case AbiMethod::XmmGpr: {
507
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunAggregateDGX : AbiOpcode::RunAggregateDG;
508
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDGX : AbiOpcode::CallDG;
509
+
510
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
511
+ func->async.Append({ .op = Code2Op(call) });
512
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnAggregateReg), .type = func->ret.type });
513
+ } break;
514
+ }
515
+ } break;
516
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
517
+
518
+ case PrimitiveKind::Float32: {
519
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunFloat32X : AbiOpcode::RunFloat32;
520
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallFX : AbiOpcode::CallF;
521
+
522
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
523
+ func->async.Append({ .op = Code2Op(call) });
524
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat32), .type = func->ret.type });
525
+ } break;
526
+ case PrimitiveKind::Float64: {
527
+ AbiOpcode run = func->forward_fp ? AbiOpcode::RunFloat64X : AbiOpcode::RunFloat64;
528
+ AbiOpcode call = func->forward_fp ? AbiOpcode::CallDDX : AbiOpcode::CallDD;
529
+
530
+ func->sync.Append({ .op = Code2Op(run), .type = func->ret.type });
531
+ func->async.Append({ .op = Code2Op(call) });
532
+ func->async.Append({ .op = Code2Op(AbiOpcode::ReturnFloat64), .type = func->ret.type });
533
+ } break;
534
+
535
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
536
+ }
537
+
538
+ return true;
539
+ }
540
+
541
+ #pragma GCC diagnostic push
542
+ #pragma GCC diagnostic ignored "-Wunused-parameter"
543
+
544
+ namespace {
545
+ #if defined(MUST_TAIL)
546
+ #define OP(Code) \
547
+ PRESERVE_NONE napi_value Handle ## Code(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
548
+ #define NEXT() \
549
+ do { \
550
+ const AbiInstruction *next = inst + 1; \
551
+ MUST_TAIL return ((ForwardFunc *)next->op)(call, args, base, next); \
552
+ } while (false)
553
+ #else
554
+ #define OP(Code) \
555
+ case (int)AbiOpcode::Code:
556
+ #define NEXT() \
557
+ break
558
+
559
+ napi_value RunLoop(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
560
+ {
561
+ for (;; ++inst) {
562
+ switch ((intptr_t)inst->op) {
563
+ #endif
564
+
565
+ #define INTEGER(CType) \
566
+ do { \
567
+ CType v; \
568
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
569
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
570
+ return call->env.Null(); \
571
+ } \
572
+ \
573
+ *(uint64_t *)(base + inst->b1) = (uint64_t)v; \
574
+ } while (false)
575
+ #define INTEGER_SWAP(CType) \
576
+ do { \
577
+ CType v; \
578
+ if (!TryNumber(call->env, args[inst->a], &v)) [[unlikely]] { \
579
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a])); \
580
+ return call->env.Null(); \
581
+ } \
582
+ \
583
+ *(uint64_t *)(base + inst->b1) = (uint64_t)ReverseBytes(v); \
584
+ } while (false)
585
+
586
+ OP(PushVoid) { K_UNREACHABLE(); return call->env.Null(); }
587
+ OP(PushBool) {
588
+ bool b;
589
+ if (napi_get_value_bool(call->env, args[inst->a], &b) != napi_ok) [[unlikely]] {
590
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected boolean", GetValueType(call->instance, args[inst->a]));
591
+ return call->env.Null();
592
+ }
593
+
594
+ *(uint64_t *)(base + inst->b1) = (uint64_t)b;
595
+
596
+ NEXT();
597
+ }
598
+ OP(PushInt8) { INTEGER(int8_t); NEXT(); }
599
+ OP(PushUInt8) { INTEGER(uint8_t); NEXT(); }
600
+ OP(PushInt16) { INTEGER(int16_t); NEXT(); }
601
+ OP(PushInt16S) { INTEGER_SWAP(int16_t); NEXT(); }
602
+ OP(PushUInt16) { INTEGER(uint16_t); NEXT(); }
603
+ OP(PushUInt16S) { INTEGER_SWAP(uint16_t); NEXT(); }
604
+ OP(PushInt32) { INTEGER(int32_t); NEXT(); }
605
+ OP(PushInt32S) { INTEGER_SWAP(int32_t); NEXT(); }
606
+ OP(PushUInt32) { INTEGER(uint32_t); NEXT(); }
607
+ OP(PushUInt32S) { INTEGER_SWAP(uint32_t); NEXT(); }
608
+ OP(PushInt64) { INTEGER(int64_t); NEXT(); }
609
+ OP(PushInt64S) { INTEGER_SWAP(int64_t); NEXT(); }
610
+ OP(PushUInt64) { INTEGER(int64_t); NEXT(); }
611
+ OP(PushUInt64S) { INTEGER_SWAP(int64_t); NEXT(); }
612
+ OP(PushString) {
613
+ const char *str;
614
+ if (!call->PushString(args[inst->a], inst->b2, &str)) [[unlikely]]
615
+ return call->env.Null();
616
+
617
+ *(const char **)(base + inst->b1) = str;
618
+
619
+ NEXT();
620
+ }
621
+ OP(PushString16) {
622
+ const char16_t *str16;
623
+ if (!call->PushString16(args[inst->a], inst->b2, &str16)) [[unlikely]]
624
+ return call->env.Null();
625
+
626
+ *(const char16_t **)(base + inst->b1) = str16;
627
+
628
+ NEXT();
629
+ }
630
+ OP(PushString32) {
631
+ const char32_t *str32;
632
+ if (!call->PushString32(args[inst->a], inst->b2, &str32)) [[unlikely]]
633
+ return call->env.Null();
634
+
635
+ *(const char32_t **)(base + inst->b1) = str32;
636
+
637
+ NEXT();
638
+ }
639
+ OP(PushPointer) {
640
+ void *ptr;
641
+ if (!call->PushPointer(args[inst->a], inst->type, inst->b2, &ptr)) [[unlikely]]
642
+ return call->env.Null();
643
+
644
+ *(void **)(base + inst->b1) = ptr;
645
+
646
+ NEXT();
647
+ }
648
+ OP(PushRecord) { K_UNREACHABLE(); return call->env.Null(); }
649
+ OP(PushUnion) { K_UNREACHABLE(); return call->env.Null(); }
650
+ OP(PushArray) { K_UNREACHABLE(); return call->env.Null(); }
651
+ OP(PushFloat32) {
652
+ float f;
653
+ if (!TryNumber(call->env, args[inst->a], &f)) [[unlikely]] {
654
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
655
+ return call->env.Null();
656
+ }
657
+
658
+ memset(base + inst->b1, 0, 8);
659
+ memcpy(base + inst->b1, &f, 4);
660
+
661
+ NEXT();
662
+ }
663
+ OP(PushFloat64) {
664
+ double d;
665
+ if (!TryNumber(call->env, args[inst->a], &d)) [[unlikely]] {
666
+ ThrowError<Napi::TypeError>(call->env, "Unexpected %1 value, expected number", GetValueType(call->instance, args[inst->a]));
667
+ return call->env.Null();
668
+ }
669
+
670
+ *(double *)(base + inst->b1) = d;
671
+
672
+ NEXT();
673
+ }
674
+ OP(PushCallback) {
675
+ void *ptr;
676
+ if (!call->PushCallback(args[inst->a], inst->type, &ptr)) [[unlikely]]
677
+ return call->env.Null();
678
+
679
+ *(void **)(base + inst->b1) = ptr;
680
+
681
+ NEXT();
682
+ }
683
+ OP(PushPrototype) { K_UNREACHABLE(); return call->env.Null(); }
684
+ OP(PushAggregateReg) {
685
+ napi_value arg = args[inst->a];
686
+
687
+ uint64_t buf[2];
688
+ if (!call->PushObject(arg, inst->type, (uint8_t *)buf)) [[unlikely]]
689
+ return call->env.Null();
690
+
691
+ // The second part might be useless (if object fits in one register), in
692
+ // which case the analysis code will put the same value in both offsets to
693
+ // make sure we don't overwrite something else. Well, if we copy the second
694
+ // part first, that is, as we do below.
695
+ *(uint64_t *)(base + inst->b2) = buf[1];
696
+ *(uint64_t *)(base + inst->b1) = buf[0];
697
+
698
+ NEXT();
699
+ }
700
+ OP(PushAggregateStack) {
701
+ napi_value arg = args[inst->a];
702
+
703
+ if (!call->PushObject(arg, inst->type, base + inst->b1)) [[unlikely]]
704
+ return call->env.Null();
705
+
706
+ NEXT();
707
+ }
708
+
709
+ #undef INTEGER_SWAP
710
+ #undef INTEGER
711
+
712
+ #define WRAP(Expr) (call->DebugForward(), (Expr))
713
+
714
+ #define INTEGER(Suffix, CType) \
715
+ do { \
716
+ uint64_t rax = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp).rax); \
717
+ return NewInt(call->env, (CType)rax); \
718
+ } while (false)
719
+ #define INTEGER_SWAP(Suffix, CType) \
720
+ do { \
721
+ uint64_t rax = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp).rax); \
722
+ return NewInt(call->env, ReverseBytes((CType)rax)); \
723
+ } while (false)
724
+ #define DISPOSE(Ptr) \
725
+ do { \
726
+ if (inst->type->dispose) { \
727
+ inst->type->dispose(call->instance, inst->type, (Ptr)); \
728
+ } \
729
+ } while (false)
730
+
731
+ OP(RunVoid) {
732
+ WRAP(ForwardCallGG(call->native, base, &call->saved_sp));
733
+ return nullptr;
734
+ }
735
+ OP(RunBool) {
736
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
737
+ return Napi::Boolean::New(call->env, rax & 0x1);
738
+ }
739
+ OP(RunInt8) { INTEGER(GG, int8_t); }
740
+ OP(RunUInt8) { INTEGER(GG, uint8_t); }
741
+ OP(RunInt16) { INTEGER(GG, int16_t); }
742
+ OP(RunInt16S) { INTEGER_SWAP(GG, int16_t); }
743
+ OP(RunUInt16) { INTEGER(GG, uint16_t); }
744
+ OP(RunUInt16S) { INTEGER_SWAP(GG, uint16_t); }
745
+ OP(RunInt32) { INTEGER(GG, int32_t); }
746
+ OP(RunInt32S) { INTEGER_SWAP(GG, int32_t); }
747
+ OP(RunUInt32) { INTEGER(GG, uint32_t); }
748
+ OP(RunUInt32S) { INTEGER_SWAP(GG, uint32_t); }
749
+ OP(RunInt64) { INTEGER(GG, int64_t); }
750
+ OP(RunInt64S) { INTEGER_SWAP(GG, int64_t); }
751
+ OP(RunUInt64) { INTEGER(GG, uint64_t); }
752
+ OP(RunUInt64S) { INTEGER_SWAP(GG, uint64_t); }
753
+ OP(RunString) {
754
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
755
+ napi_value value = NewString(call->env, (const char *)rax);
756
+ DISPOSE((void *)rax);
757
+ return value;
758
+ }
759
+ OP(RunString16) {
760
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
761
+ napi_value value = NewString(call->env, (const char16_t *)rax);
762
+ DISPOSE((void *)rax);
763
+ return value;
764
+ }
765
+ OP(RunString32) {
766
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
767
+ napi_value value = NewString(call->env, (const char32_t *)rax);
768
+ DISPOSE((void *)rax);
769
+ return value;
770
+ }
771
+ OP(RunPointer) {
772
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
773
+ napi_value value = WrapPointer(call->env, inst->type, (void *)rax);
774
+ DISPOSE((void *)rax);
775
+ return value;
776
+ }
777
+ OP(RunCallback) {
778
+ uint64_t rax = WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
779
+ return WrapPointer(call->env, inst->type, (void *)rax);
780
+ }
781
+ OP(RunRecord) { K_UNREACHABLE(); return call->env.Null(); }
782
+ OP(RunUnion) { K_UNREACHABLE(); return call->env.Null(); }
783
+ OP(RunArray) { K_UNREACHABLE(); return call->env.Null(); }
784
+ OP(RunFloat32) {
785
+ float f = WRAP(ForwardCallF(call->native, base, &call->saved_sp));
786
+ return NewFloat(call->env, f);
787
+ }
788
+ OP(RunFloat64) {
789
+ double d = WRAP(ForwardCallDD(call->native, base, &call->saved_sp).xmm0);
790
+ return NewFloat(call->env, d);
791
+ }
792
+ OP(RunPrototype) { K_UNREACHABLE(); return call->env.Null(); }
793
+ OP(RunAggregateGG) {
794
+ auto ret = WRAP(ForwardCallGG(call->native, base, &call->saved_sp));
795
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
796
+ }
797
+ OP(RunAggregateDD) {
798
+ auto ret = WRAP(ForwardCallDD(call->native, base, &call->saved_sp));
799
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
800
+ }
801
+ OP(RunAggregateGD) {
802
+ auto ret = WRAP(ForwardCallGD(call->native, base, &call->saved_sp));
803
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
804
+ }
805
+ OP(RunAggregateDG) {
806
+ auto ret = WRAP(ForwardCallDG(call->native, base, &call->saved_sp));
807
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
808
+ }
809
+ OP(RunAggregateMem) {
810
+ uint8_t *ptr = call->AllocHeap(inst->a);
811
+ *(uint8_t **)base = ptr;
812
+ WRAP(ForwardCallGG(call->native, base, &call->saved_sp).rax);
813
+ return DecodeObject(call->instance, ptr, inst->type);
814
+ }
815
+ OP(RunVoidX) {
816
+ WRAP(ForwardCallGGX(call->native, base, &call->saved_sp));
817
+ return nullptr;
818
+ }
819
+ OP(RunBoolX) {
820
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
821
+ return Napi::Boolean::New(call->env, rax & 0x1);
822
+ }
823
+ OP(RunInt8X) { INTEGER(GGX, int8_t); }
824
+ OP(RunUInt8X) { INTEGER(GGX, uint8_t); }
825
+ OP(RunInt16X) { INTEGER(GGX, int16_t); }
826
+ OP(RunInt16SX) { INTEGER_SWAP(GGX, int16_t); }
827
+ OP(RunUInt16X) { INTEGER(GGX, uint16_t); }
828
+ OP(RunUInt16SX) { INTEGER_SWAP(GGX, uint16_t); }
829
+ OP(RunInt32X) { INTEGER(GGX, int32_t); }
830
+ OP(RunInt32SX) { INTEGER_SWAP(GGX, int32_t); }
831
+ OP(RunUInt32X) { INTEGER(GGX, uint32_t); }
832
+ OP(RunUInt32SX) { INTEGER_SWAP(GGX, uint32_t); }
833
+ OP(RunInt64X) { INTEGER(GGX, int64_t); }
834
+ OP(RunInt64SX) { INTEGER_SWAP(GGX, int64_t); }
835
+ OP(RunUInt64X) { INTEGER(GGX, uint64_t); }
836
+ OP(RunUInt64SX) { INTEGER_SWAP(GGX, uint64_t); }
837
+ OP(RunStringX) {
838
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
839
+ napi_value value = NewString(call->env, (const char *)rax);
840
+ DISPOSE((void *)rax);
841
+ return value;
842
+ }
843
+ OP(RunString16X) {
844
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
845
+ napi_value value = NewString(call->env, (const char16_t *)rax);
846
+ DISPOSE((void *)rax);
847
+ return value;
848
+ }
849
+ OP(RunString32X) {
850
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
851
+ napi_value value = NewString(call->env, (const char32_t *)rax);
852
+ DISPOSE((void *)rax);
853
+ return value;
854
+ }
855
+ OP(RunPointerX) {
856
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
857
+ napi_value value = WrapPointer(call->env, inst->type, (void *)rax);
858
+ DISPOSE((void *)rax);
859
+ return value;
860
+ }
861
+ OP(RunCallbackX) {
862
+ uint64_t rax = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
863
+ return WrapPointer(call->env, inst->type, (void *)rax);
864
+ }
865
+ OP(RunRecordX) { K_UNREACHABLE(); return call->env.Null(); }
866
+ OP(RunUnionX) { K_UNREACHABLE(); return call->env.Null(); }
867
+ OP(RunArrayX) { K_UNREACHABLE(); return call->env.Null(); }
868
+ OP(RunFloat32X) {
869
+ float f = WRAP(ForwardCallFX(call->native, base, &call->saved_sp));
870
+ return NewFloat(call->env, f);
871
+ }
872
+ OP(RunFloat64X) {
873
+ double d = WRAP(ForwardCallDDX(call->native, base, &call->saved_sp).xmm0);
874
+ return NewFloat(call->env, d);
875
+ }
876
+ OP(RunPrototypeX) { K_UNREACHABLE(); return call->env.Null(); }
877
+ OP(RunAggregateGGX) {
878
+ auto ret = WRAP(ForwardCallGGX(call->native, base, &call->saved_sp));
879
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
880
+ }
881
+ OP(RunAggregateDDX) {
882
+ auto ret = WRAP(ForwardCallDDX(call->native, base, &call->saved_sp));
883
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
884
+ }
885
+ OP(RunAggregateGDX) {
886
+ auto ret = WRAP(ForwardCallGDX(call->native, base, &call->saved_sp));
887
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
888
+ }
889
+ OP(RunAggregateDGX) {
890
+ auto ret = WRAP(ForwardCallDGX(call->native, base, &call->saved_sp));
891
+ return DecodeObject(call->instance, (const uint8_t *)&ret, inst->type);
892
+ }
893
+ OP(RunAggregateMemX) {
894
+ uint8_t *ptr = call->AllocHeap(inst->a);
895
+ *(uint8_t **)base = ptr;
896
+ WRAP(ForwardCallGGX(call->native, base, &call->saved_sp).rax);
897
+ return DecodeObject(call->instance, ptr, inst->type);
898
+ }
899
+
900
+ #undef DISPOSE
901
+ #undef INTEGER_SWAP
902
+ #undef INTEGER
903
+
904
+ #define CALL(Suffix) \
905
+ do { \
906
+ auto ret = WRAP(ForwardCall ## Suffix(call->native, base, &call->saved_sp)); \
907
+ memcpy(base, &ret, K_SIZE(ret)); \
908
+ } while (false)
909
+ #define DISPOSE() \
910
+ do { \
911
+ if (inst->type->dispose) { \
912
+ void *ptr = *(void **)base; \
913
+ inst->type->dispose(call->instance, inst->type, ptr); \
914
+ } \
915
+ } while (false)
916
+ #define INTEGER(CType) \
917
+ do { \
918
+ uint64_t rax = *(uint64_t *)base; \
919
+ return NewInt(call->env, (CType)rax); \
920
+ } while (false)
921
+ #define INTEGER_SWAP(CType) \
922
+ do { \
923
+ uint64_t rax = *(uint64_t *)base; \
924
+ return NewInt(call->env, ReverseBytes((CType)rax)); \
925
+ } while (false)
926
+
927
+ OP(Yield) {
928
+ call->async_ip = inst + 1;
929
+ return nullptr;
930
+ }
931
+
932
+ OP(CallGG) { CALL(GG); return nullptr; }
933
+ OP(CallF) { CALL(F); return nullptr; }
934
+ OP(CallDG) { CALL(DG); return nullptr; }
935
+ OP(CallGD) { CALL(GD); return nullptr; }
936
+ OP(CallDD) { CALL(DD); return nullptr; }
937
+ OP(CallStack) {
938
+ uint8_t *ptr = call->AllocHeap(inst->a);
939
+ *(uint8_t **)base = ptr;
940
+ CALL(GG);
941
+ return nullptr;
942
+ }
943
+ OP(CallGGX) { CALL(GGX); return nullptr; }
944
+ OP(CallFX) { CALL(FX); return nullptr; }
945
+ OP(CallDGX) { CALL(DGX); return nullptr; }
946
+ OP(CallGDX) { CALL(GDX); return nullptr; }
947
+ OP(CallDDX) { CALL(DDX); return nullptr; }
948
+ OP(CallStackX) {
949
+ uint8_t *ptr = call->AllocHeap(inst->a);
950
+ *(uint8_t **)base = ptr;
951
+ CALL(GGX);
952
+ return nullptr;
953
+ }
954
+
955
+ OP(ReturnVoid) { return nullptr; }
956
+ OP(ReturnBool) {
957
+ uint64_t rax = *(uint64_t *)base;
958
+ return Napi::Boolean::New(call->env, rax & 0x1);
959
+ }
960
+ OP(ReturnInt8) { INTEGER(int8_t); }
961
+ OP(ReturnUInt8) { INTEGER(uint8_t); }
962
+ OP(ReturnInt16) { INTEGER(int16_t); }
963
+ OP(ReturnInt16S) { INTEGER_SWAP(int16_t); }
964
+ OP(ReturnUInt16) { INTEGER(uint16_t); }
965
+ OP(ReturnUInt16S) { INTEGER_SWAP(uint16_t); }
966
+ OP(ReturnInt32) { INTEGER(int32_t); }
967
+ OP(ReturnInt32S) { INTEGER_SWAP(int32_t); }
968
+ OP(ReturnUInt32) { INTEGER(uint32_t); }
969
+ OP(ReturnUInt32S) { INTEGER_SWAP(uint32_t); }
970
+ OP(ReturnInt64) { INTEGER(int64_t); }
971
+ OP(ReturnInt64S) { INTEGER_SWAP(int64_t); }
972
+ OP(ReturnUInt64) { INTEGER(uint64_t); }
973
+ OP(ReturnUInt64S) { INTEGER_SWAP(uint64_t); }
974
+ OP(ReturnString) {
975
+ uint64_t rax = *(uint64_t *)base;
976
+ napi_value value = NewString(call->env, (const char *)rax);
977
+ DISPOSE();
978
+ return value;
979
+ }
980
+ OP(ReturnString16) {
981
+ uint64_t rax = *(uint64_t *)base;
982
+ napi_value value = NewString(call->env, (const char16_t *)rax);
983
+ DISPOSE();
984
+ return value;
985
+ }
986
+ OP(ReturnString32) {
987
+ uint64_t rax = *(uint64_t *)base;
988
+ napi_value value = NewString(call->env, (const char32_t *)rax);
989
+ DISPOSE();
990
+ return value;
991
+ }
992
+ OP(ReturnPointer) {
993
+ uint64_t rax = *(uint64_t *)base;
994
+ napi_value value = WrapPointer(call->env, inst->type, (void *)rax);
995
+ DISPOSE();
996
+ return value;
997
+ }
998
+ OP(ReturnCallback) {
999
+ uint64_t rax = *(uint64_t *)base;
1000
+ return WrapPointer(call->env, inst->type, (void *)rax);
1001
+ }
1002
+ OP(ReturnRecord) { K_UNREACHABLE(); return call->env.Null(); }
1003
+ OP(ReturnUnion) { K_UNREACHABLE(); return call->env.Null(); }
1004
+ OP(ReturnArray) { K_UNREACHABLE(); return call->env.Null(); }
1005
+ OP(ReturnFloat32) {
1006
+ float f = *(float *)base;
1007
+ return NewFloat(call->env, f);
1008
+ }
1009
+ OP(ReturnFloat64) {
1010
+ double d = *(double *)base;
1011
+ return NewFloat(call->env, d);
1012
+ }
1013
+ OP(ReturnPrototype) { K_UNREACHABLE(); return call->env.Null(); }
1014
+ OP(ReturnAggregateReg) { return DecodeObject(call->instance, base, inst->type); }
1015
+ OP(ReturnAggregateMem) {
1016
+ uint64_t rax = *(uint64_t *)base;
1017
+ return DecodeObject(call->instance, (const uint8_t *)rax, inst->type);
1018
+ }
1019
+
1020
+ #undef INTEGER_SWAP
1021
+ #undef INTEGER
1022
+ #undef DISPOSE
1023
+ #undef CALL
1024
+
1025
+ #if defined(MUST_TAIL)
1026
+ ForwardFunc *const ForwardDispatch[256] = {
1027
+ #define PRIMITIVE(Name) HandlePush ## Name,
1028
+ #include "../primitives.inc"
1029
+ HandlePushAggregateReg,
1030
+ HandlePushAggregateStack,
1031
+ #define PRIMITIVE(Name) HandleRun ## Name,
1032
+ #include "../primitives.inc"
1033
+ HandleRunAggregateGG,
1034
+ HandleRunAggregateDD,
1035
+ HandleRunAggregateGD,
1036
+ HandleRunAggregateDG,
1037
+ HandleRunAggregateMem,
1038
+ #define PRIMITIVE(Name) HandleRun ## Name ## X,
1039
+ #include "../primitives.inc"
1040
+ HandleRunAggregateGGX,
1041
+ HandleRunAggregateDDX,
1042
+ HandleRunAggregateGDX,
1043
+ HandleRunAggregateDGX,
1044
+ HandleRunAggregateMemX,
1045
+ HandleYield,
1046
+ HandleCallGG,
1047
+ HandleCallF,
1048
+ HandleCallDG,
1049
+ HandleCallGD,
1050
+ HandleCallDD,
1051
+ HandleCallStack,
1052
+ HandleCallGGX,
1053
+ HandleCallFX,
1054
+ HandleCallDGX,
1055
+ HandleCallGDX,
1056
+ HandleCallDDX,
1057
+ HandleCallStackX,
1058
+ #define PRIMITIVE(Name) HandleReturn ## Name,
1059
+ #include "../primitives.inc"
1060
+ HandleReturnAggregateReg,
1061
+ HandleReturnAggregateMem
1062
+ };
1063
+
1064
+ FORCE_INLINE napi_value RunLoop(CallData *call, napi_value *args, uint8_t *base, const AbiInstruction *inst)
1065
+ {
1066
+ return ((ForwardFunc *)inst->op)(call, args, base, inst);
1067
+ }
1068
+ #else
1069
+ }
1070
+ }
1071
+
1072
+ K_UNREACHABLE();
1073
+ }
1074
+ #endif
1075
+
1076
+ #undef WRAP
1077
+
1078
+ #undef NEXT
1079
+ #undef OP
1080
+ }
1081
+
1082
+ #pragma GCC diagnostic pop
1083
+
1084
+ napi_value CallData::Run(const FunctionInfo *func, napi_value *args)
1085
+ {
1086
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
1087
+ if (!base) [[unlikely]]
1088
+ return env.Null();
1089
+
1090
+ const AbiInstruction *first = func->sync.ptr;
1091
+ return RunLoop(this, args, base, first);
1092
+ }
1093
+
1094
+ bool CallData::PrepareAsync(const FunctionInfo *func, napi_value *args)
1095
+ {
1096
+ uint8_t *base = AllocStack<uint8_t>(func->stk_size);
1097
+ if (!base) [[unlikely]]
1098
+ return env.Null();
1099
+ async_base = base;
1100
+
1101
+ const AbiInstruction *first = func->async.ptr;
1102
+ return !RunLoop(this, args, base, first); // Yield returns nullptr
1103
+ }
1104
+
1105
+ void CallData::ExecuteAsync()
1106
+ {
1107
+ const AbiInstruction *next = async_ip++;
1108
+ RunLoop(this, nullptr, async_base, next);
1109
+ }
1110
+
1111
+ napi_value CallData::EndAsync()
1112
+ {
1113
+ const AbiInstruction *next = async_ip++;
1114
+ return RunLoop(this, nullptr, async_base, next);
1115
+ }
1116
+
1117
+ void CallData::Relay(Size idx, uint8_t *sp)
1118
+ {
1119
+ TrampolineInfo *trampoline = &shared.trampolines[idx];
1120
+ const FunctionInfo *proto = trampoline->proto;
1121
+
1122
+ uint8_t *in_ptr = sp + 32;
1123
+ BackRegisters *out_reg = (BackRegisters *)sp;
1124
+
1125
+ K_DEFER_N(err_guard) {
1126
+ trampoline->state = -1;
1127
+ memset(out_reg, 0, K_SIZE(*out_reg));
1128
+ };
1129
+
1130
+ napi_value arguments[MaxParameters];
1131
+
1132
+ #define POP_INTEGER(CType) \
1133
+ do { \
1134
+ const uint8_t *src = in_ptr + param.abi.offsets[0]; \
1135
+ CType v = *(const CType *)src; \
1136
+ \
1137
+ arguments[i] = NewInt(env, v); \
1138
+ } while (false)
1139
+ #define POP_INTEGER_SWAP(CType) \
1140
+ do { \
1141
+ const uint8_t *src = in_ptr + param.abi.offsets[0]; \
1142
+ CType v = *(const CType *)src; \
1143
+ \
1144
+ arguments[i] = NewInt(env, ReverseBytes(v)); \
1145
+ } while (false)
1146
+
1147
+ // Convert to JS arguments
1148
+ for (Size i = 0; i < proto->parameters.len; i++) {
1149
+ const ParameterInfo &param = proto->parameters[i];
1150
+ K_ASSERT(param.directions >= 1 && param.directions <= 3);
1151
+
1152
+ switch (param.type->primitive) {
1153
+ case PrimitiveKind::Void: { K_UNREACHABLE(); } break;
1154
+
1155
+ case PrimitiveKind::Bool: {
1156
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1157
+ bool b = *(bool *)src;
1158
+
1159
+ arguments[i] = Napi::Boolean::New(env, b);
1160
+ } break;
1161
+
1162
+ case PrimitiveKind::Int8: { POP_INTEGER(int8_t); } break;
1163
+ case PrimitiveKind::UInt8: { POP_INTEGER(uint8_t); } break;
1164
+ case PrimitiveKind::Int16: { POP_INTEGER(int16_t); } break;
1165
+ case PrimitiveKind::Int16S: { POP_INTEGER_SWAP(int16_t); } break;
1166
+ case PrimitiveKind::UInt16: { POP_INTEGER(uint16_t); } break;
1167
+ case PrimitiveKind::UInt16S: { POP_INTEGER_SWAP(uint16_t); } break;
1168
+ case PrimitiveKind::Int32: { POP_INTEGER(int32_t); } break;
1169
+ case PrimitiveKind::Int32S: { POP_INTEGER_SWAP(int32_t); } break;
1170
+ case PrimitiveKind::UInt32: { POP_INTEGER(uint32_t); } break;
1171
+ case PrimitiveKind::UInt32S: { POP_INTEGER_SWAP(uint32_t); } break;
1172
+ case PrimitiveKind::Int64: { POP_INTEGER(int64_t); } break;
1173
+ case PrimitiveKind::Int64S: { POP_INTEGER_SWAP(int64_t); } break;
1174
+ case PrimitiveKind::UInt64: { POP_INTEGER(uint64_t); } break;
1175
+ case PrimitiveKind::UInt64S: { POP_INTEGER_SWAP(uint64_t); } break;
1176
+
1177
+ case PrimitiveKind::String: {
1178
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1179
+ const char *str = *(const char **)src;
1180
+
1181
+ arguments[i] = NewString(env, str);
1182
+
1183
+ if (param.type->dispose) {
1184
+ param.type->dispose(instance, param.type, str);
1185
+ }
1186
+ } break;
1187
+ case PrimitiveKind::String16: {
1188
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1189
+ const char16_t *str16 = *(const char16_t **)src;
1190
+
1191
+ arguments[i] = NewString(env, str16);
1192
+
1193
+ if (param.type->dispose) {
1194
+ param.type->dispose(instance, param.type, str16);
1195
+ }
1196
+ } break;
1197
+ case PrimitiveKind::String32: {
1198
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1199
+ const char32_t *str32 = *(const char32_t **)src;
1200
+
1201
+ arguments[i] = NewString(env, str32);
1202
+
1203
+ if (param.type->dispose) {
1204
+ param.type->dispose(instance, param.type, str32);
1205
+ }
1206
+ } break;
1207
+
1208
+ case PrimitiveKind::Pointer: {
1209
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1210
+ void *ptr2 = *(void **)src;
1211
+
1212
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
1213
+
1214
+ if (param.type->dispose) {
1215
+ param.type->dispose(instance, param.type, ptr2);
1216
+ }
1217
+ } break;
1218
+
1219
+ case PrimitiveKind::Record:
1220
+ case PrimitiveKind::Union: {
1221
+ if (param.abi.regular) {
1222
+ uint64_t buf[2];
1223
+ buf[0] = *(uint64_t *)(in_ptr + param.abi.offsets[0]);
1224
+ buf[1] = *(uint64_t *)(in_ptr + param.abi.offsets[1]);
1225
+
1226
+ arguments[i] = DecodeObject(instance, (const uint8_t *)buf, param.type);
1227
+ } else {
1228
+ arguments[i] = DecodeObject(instance, in_ptr + param.abi.offsets[0], param.type);
1229
+ }
1230
+ } break;
1231
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1232
+
1233
+ case PrimitiveKind::Float32: {
1234
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1235
+ float f = *(float *)src;
1236
+
1237
+ arguments[i] = NewFloat(env, f);
1238
+ } break;
1239
+ case PrimitiveKind::Float64: {
1240
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1241
+ double d = *(double *)src;
1242
+
1243
+ arguments[i] = NewFloat(env, d);
1244
+ } break;
1245
+
1246
+ case PrimitiveKind::Callback: {
1247
+ const uint8_t *src = in_ptr + param.abi.offsets[0];
1248
+ void *ptr2 = *(void **)src;
1249
+
1250
+ arguments[i] = WrapPointer(env, param.type->ref.type, ptr2);
1251
+ } break;
1252
+
1253
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1254
+ }
1255
+ }
1256
+
1257
+ #undef POP_INTEGER_SWAP
1258
+ #undef POP_INTEGER
1259
+
1260
+ const TypeInfo *type = proto->ret.type;
1261
+
1262
+ // We're ready, make the call!
1263
+ napi_value value = CallCallback(trampoline, arguments, proto->parameters.len);
1264
+
1265
+ if (!value) [[unlikely]]
1266
+ return;
1267
+
1268
+ #define RETURN_INTEGER(CType) \
1269
+ do { \
1270
+ CType v; \
1271
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1272
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1273
+ return; \
1274
+ } \
1275
+ \
1276
+ out_reg->rax = (uint64_t)v; \
1277
+ } while (false)
1278
+ #define RETURN_INTEGER_SWAP(CType) \
1279
+ do { \
1280
+ CType v; \
1281
+ if (!TryNumber(env, value, &v)) [[unlikely]] { \
1282
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value)); \
1283
+ return; \
1284
+ } \
1285
+ \
1286
+ out_reg->rax = (uint64_t)ReverseBytes(v); \
1287
+ } while (false)
1288
+
1289
+ // Convert the result
1290
+ switch (type->primitive) {
1291
+ case PrimitiveKind::Void: {} break;
1292
+
1293
+ case PrimitiveKind::Bool: {
1294
+ bool b;
1295
+ if (napi_get_value_bool(env, value, &b) != napi_ok) [[unlikely]] {
1296
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected boolean", GetValueType(instance, value));
1297
+ return;
1298
+ }
1299
+
1300
+ out_reg->rax = (uint64_t)b;
1301
+ } break;
1302
+
1303
+ case PrimitiveKind::Int8: { RETURN_INTEGER(int8_t); } break;
1304
+ case PrimitiveKind::UInt8: { RETURN_INTEGER(uint8_t); } break;
1305
+ case PrimitiveKind::Int16: { RETURN_INTEGER(int16_t); } break;
1306
+ case PrimitiveKind::Int16S: { RETURN_INTEGER_SWAP(int16_t); } break;
1307
+ case PrimitiveKind::UInt16: { RETURN_INTEGER(uint16_t); } break;
1308
+ case PrimitiveKind::UInt16S: { RETURN_INTEGER_SWAP(uint16_t); } break;
1309
+ case PrimitiveKind::Int32: { RETURN_INTEGER(int32_t); } break;
1310
+ case PrimitiveKind::Int32S: { RETURN_INTEGER_SWAP(int32_t); } break;
1311
+ case PrimitiveKind::UInt32: { RETURN_INTEGER(uint32_t); } break;
1312
+ case PrimitiveKind::UInt32S: { RETURN_INTEGER_SWAP(uint32_t); } break;
1313
+ case PrimitiveKind::Int64: { RETURN_INTEGER(int64_t); } break;
1314
+ case PrimitiveKind::Int64S: { RETURN_INTEGER_SWAP(int64_t); } break;
1315
+ case PrimitiveKind::UInt64: { RETURN_INTEGER(uint64_t); } break;
1316
+ case PrimitiveKind::UInt64S: { RETURN_INTEGER_SWAP(uint64_t); } break;
1317
+
1318
+ case PrimitiveKind::String: {
1319
+ const char *str;
1320
+ if (!PushString(value, 1, &str)) [[unlikely]]
1321
+ return;
1322
+
1323
+ out_reg->rax = (uint64_t)str;
1324
+ } break;
1325
+ case PrimitiveKind::String16: {
1326
+ const char16_t *str16;
1327
+ if (!PushString16(value, 1, &str16)) [[unlikely]]
1328
+ return;
1329
+
1330
+ out_reg->rax = (uint64_t)str16;
1331
+ } break;
1332
+ case PrimitiveKind::String32: {
1333
+ const char32_t *str32;
1334
+ if (!PushString32(value, 1, &str32)) [[unlikely]]
1335
+ return;
1336
+
1337
+ out_reg->rax = (uint64_t)str32;
1338
+ } break;
1339
+
1340
+ case PrimitiveKind::Pointer: {
1341
+ void *ptr;
1342
+ if (!PushPointer(value, type, 1, &ptr)) [[unlikely]]
1343
+ return;
1344
+
1345
+ out_reg->rax = (uint64_t)ptr;
1346
+ } break;
1347
+
1348
+ case PrimitiveKind::Record:
1349
+ case PrimitiveKind::Union: {
1350
+ if (proto->ret.abi.method == AbiMethod::Stack) {
1351
+ uint64_t *gpr_ptr = (uint64_t *)in_ptr;
1352
+ uint8_t *dest = (uint8_t *)gpr_ptr[0];
1353
+
1354
+ if (!PushObject(value, type, dest)) [[unlikely]]
1355
+ return;
1356
+
1357
+ out_reg->rax = (uint64_t)dest;
1358
+ } else {
1359
+ K_ASSERT(type->size <= 16);
1360
+
1361
+ uint8_t buf[16] = {};
1362
+ if (!PushObject(value, type, buf)) [[unlikely]]
1363
+ return;
1364
+
1365
+ switch (proto->ret.abi.method) {
1366
+ case AbiMethod::Stack: { K_UNREACHABLE(); } break;
1367
+
1368
+ case AbiMethod::Gpr: {
1369
+ memcpy(&out_reg->rax, buf + 0, 8);
1370
+ } break;
1371
+ case AbiMethod::GprGpr: {
1372
+ memcpy(&out_reg->rax, buf + 0, 8);
1373
+ memcpy(&out_reg->rdx, buf + 8, 8);
1374
+ } break;
1375
+ case AbiMethod::Xmm: {
1376
+ memcpy(&out_reg->xmm0, buf + 0, 8);
1377
+ } break;
1378
+ case AbiMethod::XmmXmm: {
1379
+ memcpy(&out_reg->xmm0, buf + 0, 8);
1380
+ memcpy(&out_reg->xmm1, buf + 8, 8);
1381
+ } break;
1382
+ case AbiMethod::GprXmm: {
1383
+ memcpy(&out_reg->rax, buf + 0, 8);
1384
+ memcpy(&out_reg->xmm0, buf + 8, 8);
1385
+ } break;
1386
+ case AbiMethod::XmmGpr: {
1387
+ memcpy(&out_reg->xmm0, buf + 0, 8);
1388
+ memcpy(&out_reg->rax, buf + 8, 8);
1389
+ } break;
1390
+ }
1391
+ }
1392
+ } break;
1393
+ case PrimitiveKind::Array: { K_UNREACHABLE(); } break;
1394
+
1395
+ case PrimitiveKind::Float32: {
1396
+ float f;
1397
+ if (!TryNumber(env, value, &f)) [[unlikely]] {
1398
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1399
+ return;
1400
+ }
1401
+
1402
+ memset(&out_reg->xmm0, 0, 8);
1403
+ memcpy(&out_reg->xmm0, &f, 4);
1404
+ } break;
1405
+ case PrimitiveKind::Float64: {
1406
+ double d;
1407
+ if (!TryNumber(env, value, &d)) [[unlikely]] {
1408
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value, expected number", GetValueType(instance, value));
1409
+ return;
1410
+ }
1411
+
1412
+ out_reg->xmm0 = d;
1413
+ } break;
1414
+
1415
+ case PrimitiveKind::Callback: {
1416
+ void *ptr;
1417
+ if (!PushCallback(value, type, &ptr)) [[unlikely]]
1418
+ return;
1419
+
1420
+ out_reg->rax = (uint64_t)ptr;
1421
+ } break;
1422
+
1423
+ case PrimitiveKind::Prototype: { K_UNREACHABLE(); } break;
1424
+ }
1425
+
1426
+ #undef RETURN_INTEGER_SWAP
1427
+ #undef RETURN_INTEGER
1428
+
1429
+ err_guard.Disable();
1430
+ }
1431
+
1432
+ }
1433
+
1434
+ #endif