porffor 0.37.16 → 0.37.17
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/compiler/2c.js +68 -8
- package/compiler/builtins_precompiled.js +238 -238
- package/compiler/codegen.js +71 -37
- package/package.json +1 -1
- package/runner/index.js +1 -1
package/compiler/codegen.js
CHANGED
@@ -1015,7 +1015,7 @@ const asmFunc = (name, { wasm, params = [], typedParams = false, locals: localTy
|
|
1015
1015
|
const existing = funcByName(name);
|
1016
1016
|
if (existing) return existing;
|
1017
1017
|
|
1018
|
-
const nameParam = i => localNames[i] ??
|
1018
|
+
const nameParam = i => localNames[i] ?? `l${i}`;
|
1019
1019
|
|
1020
1020
|
const allLocals = params.concat(localTypes);
|
1021
1021
|
const locals = {};
|
@@ -1170,15 +1170,7 @@ const setType = (scope, _name, type) => {
|
|
1170
1170
|
const out = typeof type === 'number' ? number(type, Valtype.i32) : type;
|
1171
1171
|
|
1172
1172
|
if (Object.hasOwn(scope.locals, name)) {
|
1173
|
-
if (scope.locals[name]?.metadata?.type != null)
|
1174
|
-
const metadata = scope.locals[name].metadata;
|
1175
|
-
if (metadata.inferredType && knownType(scope, type) !== metadata.type) {
|
1176
|
-
delete metadata.type;
|
1177
|
-
delete metadata.inferredType;
|
1178
|
-
}
|
1179
|
-
|
1180
|
-
return [];
|
1181
|
-
}
|
1173
|
+
if (scope.locals[name]?.metadata?.type != null) return [];
|
1182
1174
|
|
1183
1175
|
const typeLocal = scope.locals[name + '#type'];
|
1184
1176
|
if (typeLocal) return [
|
@@ -1191,15 +1183,7 @@ const setType = (scope, _name, type) => {
|
|
1191
1183
|
}
|
1192
1184
|
|
1193
1185
|
if (Object.hasOwn(globals, name)) {
|
1194
|
-
if (globals[name]?.metadata?.type != null)
|
1195
|
-
const metadata = globals[name].metadata;
|
1196
|
-
if (metadata.inferredType && knownType(scope, type) !== metadata.type) {
|
1197
|
-
delete metadata.type;
|
1198
|
-
delete metadata.inferredType;
|
1199
|
-
}
|
1200
|
-
|
1201
|
-
return [];
|
1202
|
-
}
|
1186
|
+
if (globals[name]?.metadata?.type != null) return [];
|
1203
1187
|
|
1204
1188
|
const typeLocal = globals[name + '#type'];
|
1205
1189
|
if (typeLocal) return [
|
@@ -1414,6 +1398,11 @@ const getNodeType = (scope, node) => {
|
|
1414
1398
|
return TYPES.number;
|
1415
1399
|
}
|
1416
1400
|
|
1401
|
+
if (node.type === 'TemplateLiteral') {
|
1402
|
+
// could be normal string but shrug
|
1403
|
+
return TYPES.bytestring;
|
1404
|
+
}
|
1405
|
+
|
1417
1406
|
if (node.type === 'TaggedTemplateExpression') {
|
1418
1407
|
// hack
|
1419
1408
|
switch (node.tag.name) {
|
@@ -2615,12 +2604,15 @@ const knownType = (scope, type) => {
|
|
2615
2604
|
return read_signedLEB128(type[0].slice(1));
|
2616
2605
|
}
|
2617
2606
|
|
2618
|
-
if (type.length === 1 && type[0][0] === Opcodes.local_get) {
|
2607
|
+
if (typedInput && type.length === 1 && type[0][0] === Opcodes.local_get) {
|
2619
2608
|
const idx = type[0][1];
|
2620
2609
|
|
2621
2610
|
// type idx = var idx + 1
|
2622
2611
|
const name = Object.values(scope.locals).find(x => x.idx === idx)?.name;
|
2623
|
-
if (
|
2612
|
+
if (name) {
|
2613
|
+
const local = scope.locals[name];
|
2614
|
+
if (local.metadata?.type != null) return v.metadata.type;
|
2615
|
+
}
|
2624
2616
|
}
|
2625
2617
|
|
2626
2618
|
return null;
|
@@ -2945,6 +2937,59 @@ const setLocalWithType = (scope, name, isGlobal, decl, tee = false, overrideType
|
|
2945
2937
|
};
|
2946
2938
|
|
2947
2939
|
const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
|
2940
|
+
// statically analyzed ffi dlopen hack to let 2c handle it
|
2941
|
+
if (init && init.type === 'CallExpression' && init.callee.name === '__Porffor_dlopen') {
|
2942
|
+
if (Prefs.target !== 'native' && !Prefs.native) throw new Error('Porffor.dlopen is only supported for native target (use --native)');
|
2943
|
+
|
2944
|
+
// disable pgo if using ffi (lol)
|
2945
|
+
Prefs.pgo = false;
|
2946
|
+
|
2947
|
+
try {
|
2948
|
+
let usedNames = [];
|
2949
|
+
for (const x of pattern.properties) {
|
2950
|
+
const name = x.key.name;
|
2951
|
+
usedNames.push(name);
|
2952
|
+
}
|
2953
|
+
|
2954
|
+
let path = init.arguments[0].value;
|
2955
|
+
let symbols = {};
|
2956
|
+
|
2957
|
+
for (const x of init.arguments[1].properties) {
|
2958
|
+
const name = x.key.name;
|
2959
|
+
if (!usedNames.includes(name)) continue;
|
2960
|
+
|
2961
|
+
let parameters, result;
|
2962
|
+
for (const y of x.value.properties) {
|
2963
|
+
switch (y.key.name) {
|
2964
|
+
case 'parameters':
|
2965
|
+
parameters = y.value.elements.map(z => z.value);
|
2966
|
+
break;
|
2967
|
+
|
2968
|
+
case 'result':
|
2969
|
+
result = y.value.value;
|
2970
|
+
break;
|
2971
|
+
}
|
2972
|
+
}
|
2973
|
+
|
2974
|
+
symbols[name] = { parameters, result };
|
2975
|
+
|
2976
|
+
// mock ffi function
|
2977
|
+
asmFunc(name, {
|
2978
|
+
wasm: [],
|
2979
|
+
params: parameters.map(x => Valtype.i32),
|
2980
|
+
returns: result ? [ Valtype.i32 ] : [],
|
2981
|
+
returnType: TYPES.number
|
2982
|
+
})
|
2983
|
+
}
|
2984
|
+
|
2985
|
+
return [ [ null, 'dlopen', path, symbols ] ];
|
2986
|
+
} catch (e) {
|
2987
|
+
console.error('bad Porffor.dlopen syntax');
|
2988
|
+
throw e;
|
2989
|
+
}
|
2990
|
+
}
|
2991
|
+
|
2992
|
+
|
2948
2993
|
const topLevel = scope.name === 'main';
|
2949
2994
|
|
2950
2995
|
if (typeof pattern === 'string') {
|
@@ -2975,25 +3020,14 @@ const generateVarDstr = (scope, kind, pattern, init, defaultValue, global) => {
|
|
2975
3020
|
// let generated;
|
2976
3021
|
// if (init) generated = generate(scope, init, global, name);
|
2977
3022
|
|
2978
|
-
const typed = typedInput && pattern.typeAnnotation
|
2979
|
-
let idx = allocVar(scope, name, global, !(typed &&
|
3023
|
+
const typed = typedInput && pattern.typeAnnotation;
|
3024
|
+
let idx = allocVar(scope, name, global, !(typed && extractTypeAnnotation(pattern).type != null));
|
3025
|
+
addVarMetadata(scope, name, global, { kind });
|
2980
3026
|
|
2981
|
-
let metadata = { kind };
|
2982
3027
|
if (typed) {
|
2983
|
-
|
2984
|
-
...metadata,
|
2985
|
-
...typed
|
2986
|
-
};
|
2987
|
-
} else if (init) {
|
2988
|
-
const knownInit = knownType(scope, getNodeType(scope, init));
|
2989
|
-
if (knownInit != null) {
|
2990
|
-
metadata.type = knownInit;
|
2991
|
-
metadata.inferredType = true;
|
2992
|
-
}
|
3028
|
+
addVarMetadata(scope, name, global, extractTypeAnnotation(pattern));
|
2993
3029
|
}
|
2994
3030
|
|
2995
|
-
addVarMetadata(scope, name, global, metadata);
|
2996
|
-
|
2997
3031
|
if (init) {
|
2998
3032
|
const alreadyArray = scope.arrays?.get(name) != null;
|
2999
3033
|
|
@@ -5209,7 +5243,7 @@ const countParams = (func, name = undefined) => {
|
|
5209
5243
|
name ??= func.name;
|
5210
5244
|
let params = func.params.length;
|
5211
5245
|
if (func.constr) params -= 4;
|
5212
|
-
if (!
|
5246
|
+
if (!func.internal || builtinFuncs[name]?.typedParams) params = Math.floor(params / 2);
|
5213
5247
|
|
5214
5248
|
return func.argc = params;
|
5215
5249
|
};
|
package/package.json
CHANGED