porffor 0.2.0-fde989a → 0.14.0-032e4ad08
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/CONTRIBUTING.md +262 -0
- package/LICENSE +20 -20
- package/README.md +135 -94
- package/asur/README.md +2 -0
- package/asur/index.js +1262 -0
- package/byg/index.js +216 -0
- package/compiler/2c.js +66 -54
- package/compiler/{sections.js → assemble.js} +109 -21
- package/compiler/builtins/annexb_string.js +72 -0
- package/compiler/builtins/annexb_string.ts +19 -0
- package/compiler/builtins/array.ts +225 -0
- package/compiler/builtins/base64.ts +77 -0
- package/compiler/builtins/boolean.ts +20 -0
- package/compiler/builtins/crypto.ts +121 -0
- package/compiler/builtins/date.ts +2069 -0
- package/compiler/builtins/error.js +22 -0
- package/compiler/builtins/escape.ts +140 -0
- package/compiler/builtins/function.ts +7 -0
- package/compiler/builtins/int.ts +147 -0
- package/compiler/builtins/math.ts +410 -0
- package/compiler/builtins/number.ts +531 -0
- package/compiler/builtins/object.ts +6 -0
- package/compiler/builtins/porffor.d.ts +60 -0
- package/compiler/builtins/set.ts +199 -0
- package/compiler/builtins/string.ts +1081 -0
- package/compiler/builtins/symbol.ts +62 -0
- package/compiler/builtins.js +466 -284
- package/compiler/{codeGen.js → codegen.js} +1573 -656
- package/compiler/decompile.js +3 -4
- package/compiler/embedding.js +22 -22
- package/compiler/encoding.js +94 -10
- package/compiler/expression.js +1 -1
- package/compiler/generated_builtins.js +2110 -0
- package/compiler/index.js +29 -44
- package/compiler/log.js +6 -3
- package/compiler/opt.js +55 -41
- package/compiler/parse.js +38 -30
- package/compiler/precompile.js +121 -0
- package/compiler/prefs.js +31 -0
- package/compiler/prototype.js +209 -201
- package/compiler/types.js +38 -0
- package/compiler/wasmSpec.js +33 -8
- package/compiler/wrap.js +154 -89
- package/package.json +9 -5
- package/porf +2 -0
- package/porffor_tmp.c +202 -0
- package/rhemyn/compile.js +46 -27
- package/rhemyn/parse.js +322 -320
- package/rhemyn/test/parse.js +58 -58
- package/runner/compare.js +33 -34
- package/runner/debug.js +117 -0
- package/runner/index.js +80 -12
- package/runner/profiler.js +75 -0
- package/runner/repl.js +58 -15
- package/runner/sizes.js +37 -37
- package/runner/version.js +10 -8
- package/compiler/builtins/base64.js +0 -92
- package/filesize.cmd +0 -2
- package/runner/info.js +0 -89
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/runner/transform.js +0 -15
- package/tmp.c +0 -661
- package/util/enum.js +0 -20
package/compiler/wasmSpec.js
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
|
1
|
+
const enumify = (...args) => {
|
2
|
+
const obj = {};
|
3
|
+
|
4
|
+
for (let i = 0; i < args.length; i++) {
|
5
|
+
obj[i] = args[i];
|
6
|
+
obj[args[i]] = i;
|
7
|
+
}
|
8
|
+
|
9
|
+
return obj;
|
10
|
+
};
|
2
11
|
|
3
12
|
export const Section = enumify('custom', 'type', 'import', 'func', 'table', 'memory', 'global', 'export', 'start', 'element', 'code', 'data', 'data_count', 'tag');
|
4
13
|
export const ExportDesc = enumify('func', 'table', 'mem', 'global', 'tag');
|
@@ -12,6 +21,11 @@ export const Valtype = {
|
|
12
21
|
v128: 0x7b
|
13
22
|
};
|
14
23
|
|
24
|
+
export const Reftype = {
|
25
|
+
funcref: 0x70,
|
26
|
+
externref: 0x6f
|
27
|
+
};
|
28
|
+
|
15
29
|
export const Blocktype = {
|
16
30
|
void: 0x40,
|
17
31
|
};
|
@@ -32,17 +46,16 @@ export const Opcodes = {
|
|
32
46
|
throw: 0x08,
|
33
47
|
rethrow: 0x09,
|
34
48
|
|
35
|
-
call: 0x10,
|
36
|
-
call_indirect: 0x11,
|
37
|
-
return_call: 0x12,
|
38
|
-
return_call_indirect: 0x13,
|
39
|
-
|
40
49
|
end: 0x0b,
|
41
50
|
br: 0x0c,
|
42
51
|
br_if: 0x0d,
|
43
52
|
br_table: 0x0e,
|
44
53
|
return: 0x0f,
|
54
|
+
|
45
55
|
call: 0x10,
|
56
|
+
call_indirect: 0x11,
|
57
|
+
return_call: 0x12,
|
58
|
+
return_call_indirect: 0x13,
|
46
59
|
|
47
60
|
drop: 0x1a,
|
48
61
|
|
@@ -62,13 +75,22 @@ export const Opcodes = {
|
|
62
75
|
i32_load16_s: 0x2e,
|
63
76
|
i32_load16_u: 0x2f,
|
64
77
|
|
65
|
-
|
66
|
-
|
78
|
+
i64_load8_s: 0x30,
|
79
|
+
i64_load8_u: 0x31,
|
80
|
+
i64_load16_s: 0x32,
|
81
|
+
i64_load16_u: 0x33,
|
67
82
|
|
68
83
|
i32_store: 0x36,
|
69
84
|
i64_store: 0x37,
|
70
85
|
f64_store: 0x39,
|
71
86
|
|
87
|
+
i32_store8: 0x3a,
|
88
|
+
i32_store16: 0x3b,
|
89
|
+
|
90
|
+
i64_store8: 0x3c,
|
91
|
+
i64_store16: 0x3d,
|
92
|
+
|
93
|
+
memory_size: 0x3f,
|
72
94
|
memory_grow: 0x40,
|
73
95
|
|
74
96
|
i32_const: 0x41,
|
@@ -100,6 +122,8 @@ export const Opcodes = {
|
|
100
122
|
i32_shl: 0x74,
|
101
123
|
i32_shr_s: 0x75,
|
102
124
|
i32_shr_u: 0x76,
|
125
|
+
i32_rotl: 0x77,
|
126
|
+
i32_rotr: 0x78,
|
103
127
|
|
104
128
|
i64_eqz: 0x50,
|
105
129
|
i64_eq: 0x51,
|
@@ -123,6 +147,7 @@ export const Opcodes = {
|
|
123
147
|
i64_shr_s: 0x87,
|
124
148
|
i64_shr_u: 0x88,
|
125
149
|
i64_rotl: 0x89,
|
150
|
+
i64_rotr: 0x8a,
|
126
151
|
|
127
152
|
f64_eq: 0x61,
|
128
153
|
f64_ne: 0x62,
|
package/compiler/wrap.js
CHANGED
@@ -1,26 +1,104 @@
|
|
1
|
+
import { encodeVector, encodeLocal } from './encoding.js';
|
2
|
+
import { importedFuncs } from './builtins.js';
|
1
3
|
import compile from './index.js';
|
2
4
|
import decompile from './decompile.js';
|
3
|
-
import {
|
4
|
-
|
5
|
+
import { TYPES } from './types.js';
|
6
|
+
import { log } from './log.js';
|
7
|
+
import Prefs from './prefs.js';
|
5
8
|
|
6
9
|
const bold = x => `\u001b[1m${x}\u001b[0m`;
|
7
10
|
|
8
|
-
const
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
12
|
+
switch (type) {
|
13
|
+
case TYPES.boolean: return Boolean(value);
|
14
|
+
case TYPES.undefined: return undefined;
|
15
|
+
case TYPES.object: return value === 0 ? null : {};
|
16
|
+
|
17
|
+
case TYPES.function: {
|
18
|
+
let func;
|
19
|
+
if (value < 0) {
|
20
|
+
func = importedFuncs[value + importedFuncs.length];
|
21
|
+
} else {
|
22
|
+
func = funcs.find(x => ((x.originalIndex ?? x.index) - importedFuncs.length) === value);
|
23
|
+
}
|
24
|
+
|
25
|
+
if (!func) return function () {};
|
26
|
+
|
27
|
+
// make fake empty func for repl/etc
|
28
|
+
return {[func.name]() {}}[func.name];
|
29
|
+
}
|
30
|
+
|
31
|
+
case TYPES.string: {
|
32
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
33
|
+
return Array.from(new Uint16Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
|
34
|
+
}
|
35
|
+
|
36
|
+
case TYPES.bytestring: {
|
37
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
38
|
+
return Array.from(new Uint8Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
|
39
|
+
}
|
40
|
+
|
41
|
+
case TYPES.array: {
|
42
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
43
|
+
|
44
|
+
const out = [];
|
45
|
+
for (let i = 0; i < length; i++) {
|
46
|
+
const offset = value + 4 + (i * 9);
|
47
|
+
|
48
|
+
// have to slice because of memory alignment (?)
|
49
|
+
const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
|
50
|
+
const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
|
51
|
+
|
52
|
+
// console.log(`reading value at index ${i}...`)
|
53
|
+
// console.log(' memory:', Array.from(new Uint8Array(memory.buffer, offset, 9)).map(x => x.toString(16).padStart(2, '0')).join(' '));
|
54
|
+
// console.log(' read:', { value: v, type: t }, '\n');
|
55
|
+
|
56
|
+
out.push(porfToJSValue({ memory, funcs, pages }, v, t));
|
57
|
+
}
|
58
|
+
|
59
|
+
return out;
|
60
|
+
}
|
61
|
+
|
62
|
+
case TYPES.date: {
|
63
|
+
const t = (new Float64Array(memory.buffer, value, 1))[0];
|
64
|
+
return new Date(t);
|
65
|
+
}
|
66
|
+
|
67
|
+
case TYPES.set: {
|
68
|
+
const size = (new Int32Array(memory.buffer, value, 1))[0];
|
69
|
+
|
70
|
+
const out = new Set();
|
71
|
+
for (let i = 0; i < size; i++) {
|
72
|
+
const offset = value + 4 + (i * 9);
|
73
|
+
|
74
|
+
// have to slice because of memory alignment (?)
|
75
|
+
const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
|
76
|
+
const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
|
77
|
+
|
78
|
+
// console.log(`reading value at index ${i}...`)
|
79
|
+
// console.log(' memory:', Array.from(new Uint8Array(memory.buffer, offset, 9)).map(x => x.toString(16).padStart(2, '0')).join(' '));
|
80
|
+
// console.log(' read:', { value: v, type: t }, '\n');
|
81
|
+
|
82
|
+
out.add(porfToJSValue({ memory, funcs, pages }, v, t));
|
83
|
+
}
|
84
|
+
|
85
|
+
return out;
|
86
|
+
}
|
87
|
+
|
88
|
+
case TYPES.symbol: {
|
89
|
+
const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
|
90
|
+
const offset = descStore + 4 + ((value - 1) * 9);
|
91
|
+
|
92
|
+
const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
|
93
|
+
const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
|
94
|
+
|
95
|
+
const desc = porfToJSValue({ memory, funcs, pages }, v, t);
|
96
|
+
|
97
|
+
return Symbol(desc);
|
98
|
+
}
|
99
|
+
|
100
|
+
default: return value;
|
101
|
+
}
|
24
102
|
};
|
25
103
|
|
26
104
|
export default async (source, flags = [ 'module' ], customImports = {}, print = str => process.stdout.write(str)) => {
|
@@ -29,36 +107,21 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
29
107
|
const t1 = performance.now();
|
30
108
|
const { wasm, funcs, globals, tags, exceptions, pages, c } = compile(source, flags);
|
31
109
|
|
110
|
+
globalThis.porfDebugInfo = { funcs, globals };
|
111
|
+
|
32
112
|
if (source.includes('export function')) flags.push('module');
|
33
113
|
|
34
|
-
// fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
114
|
+
// (await import('node:fs')).writeFileSync('out.wasm', Buffer.from(wasm));
|
35
115
|
|
36
116
|
times.push(performance.now() - t1);
|
37
|
-
if (
|
38
|
-
|
39
|
-
const t2 = performance.now();
|
40
|
-
|
41
|
-
let instance;
|
42
|
-
try {
|
43
|
-
0, { instance } = await WebAssembly.instantiate(wasm, {
|
44
|
-
'': {
|
45
|
-
p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
|
46
|
-
c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
|
47
|
-
t: _ => performance.now(),
|
48
|
-
...customImports
|
49
|
-
}
|
50
|
-
});
|
51
|
-
} catch (e) {
|
52
|
-
// only backtrace for runner, not test262/etc
|
53
|
-
if (!process.argv[1].includes('/runner')) throw e;
|
117
|
+
if (Prefs.profileCompiler) console.log(bold(`compiled in ${times[0].toFixed(2)}ms`));
|
54
118
|
|
55
|
-
|
56
|
-
|
119
|
+
const backtrace = (funcInd, blobOffset) => {
|
120
|
+
if (funcInd == null || blobOffset == null) return false;
|
57
121
|
|
58
122
|
// convert blob offset -> function wasm offset.
|
59
123
|
// this is not good code and is somewhat duplicated
|
60
124
|
// I just want it to work for debugging, I don't care about perf/yes
|
61
|
-
|
62
125
|
const func = funcs.find(x => x.index === funcInd);
|
63
126
|
const locals = Object.values(func.locals).sort((a, b) => a.idx - b.idx).slice(func.params.length).sort((a, b) => a.idx - b.idx);
|
64
127
|
|
@@ -76,7 +139,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
76
139
|
|
77
140
|
if (typeCount !== 0) localDecl.push(encodeLocal(typeCount, lastType));
|
78
141
|
|
79
|
-
const toFind = encodeVector(localDecl).concat(func.wasm.flat().filter(x => x != null && x <= 0xff).slice(0,
|
142
|
+
const toFind = encodeVector(localDecl).concat(func.wasm.flat().filter(x => x != null && x <= 0xff).slice(0, 60));
|
80
143
|
|
81
144
|
let i = 0;
|
82
145
|
for (; i < wasm.length; i++) {
|
@@ -91,7 +154,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
91
154
|
if (!mismatch) break;
|
92
155
|
}
|
93
156
|
|
94
|
-
if (i === wasm.length)
|
157
|
+
if (i === wasm.length) return false;
|
95
158
|
|
96
159
|
const offset = (blobOffset - i) + encodeVector(localDecl).length;
|
97
160
|
|
@@ -102,7 +165,7 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
102
165
|
if (cumLen === offset) break;
|
103
166
|
}
|
104
167
|
|
105
|
-
if (cumLen !== offset)
|
168
|
+
if (cumLen !== offset) return false;
|
106
169
|
|
107
170
|
i -= 1;
|
108
171
|
|
@@ -127,13 +190,46 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
127
190
|
console.log(decomp.join('\n'));
|
128
191
|
console.log('\x1B[90m...\x1B[0m\n');
|
129
192
|
|
193
|
+
return true;
|
194
|
+
};
|
195
|
+
|
196
|
+
const t2 = performance.now();
|
197
|
+
|
198
|
+
let instance;
|
199
|
+
try {
|
200
|
+
let wasmEngine = WebAssembly;
|
201
|
+
if (Prefs.asur) {
|
202
|
+
log.warning('wrap', 'using our !experimental! asur wasm engine instead of host to run');
|
203
|
+
wasmEngine = await import('../asur/index.js');
|
204
|
+
}
|
205
|
+
|
206
|
+
0, { instance } = await wasmEngine.instantiate(wasm, {
|
207
|
+
'': {
|
208
|
+
p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
|
209
|
+
c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
|
210
|
+
t: () => performance.now(),
|
211
|
+
u: () => performance.timeOrigin,
|
212
|
+
y: () => {},
|
213
|
+
z: () => {},
|
214
|
+
...customImports
|
215
|
+
}
|
216
|
+
});
|
217
|
+
} catch (e) {
|
218
|
+
// only backtrace for runner, not test262/etc
|
219
|
+
if (!process.argv[1].includes('/runner')) throw e;
|
220
|
+
|
221
|
+
const funcInd = parseInt(e.message.match(/function #([0-9]+) /)?.[1]);
|
222
|
+
const blobOffset = parseInt(e.message.split('@')?.[1]);
|
223
|
+
|
224
|
+
backtrace(funcInd, blobOffset);
|
130
225
|
throw e;
|
131
226
|
}
|
132
227
|
|
133
228
|
times.push(performance.now() - t2);
|
134
|
-
if (
|
229
|
+
if (Prefs.profileCompiler) console.log(`instantiated in ${times[1].toFixed(2)}ms`);
|
135
230
|
|
136
231
|
const exports = {};
|
232
|
+
const rawValues = process.argv.includes('-i');
|
137
233
|
|
138
234
|
const exceptTag = instance.exports['0'], memory = instance.exports['$'];
|
139
235
|
for (const x in instance.exports) {
|
@@ -151,54 +247,12 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
151
247
|
|
152
248
|
exports[func.name] = function() {
|
153
249
|
try {
|
154
|
-
const
|
155
|
-
|
156
|
-
if (_ret == null) return undefined;
|
157
|
-
|
158
|
-
const [ ret, type ] = _ret;
|
159
|
-
|
160
|
-
// if (ret >= typeBase && ret <= typeBase + 8) return ret > (typeBase + 7) ? 'object' : TYPES[ret];
|
161
|
-
|
162
|
-
switch (TYPES[type]) {
|
163
|
-
case 'boolean': return Boolean(ret);
|
164
|
-
case 'undefined': return undefined;
|
165
|
-
case 'object': return ret === 0 ? null : {};
|
166
|
-
|
167
|
-
case '_array': {
|
168
|
-
const pointer = ret;
|
169
|
-
const length = new Int32Array(memory.buffer, pointer, 1);
|
170
|
-
|
171
|
-
// have to slice because of memory alignment
|
172
|
-
const buf = memory.buffer.slice(pointer + 4, pointer + 4 + 8 * length);
|
173
|
-
|
174
|
-
return Array.from(new Float64Array(buf));
|
175
|
-
}
|
250
|
+
const ret = exp.apply(this, arguments);
|
251
|
+
if (ret == null) return undefined;
|
176
252
|
|
177
|
-
|
178
|
-
const pointer = ret;
|
179
|
-
const length = new Int32Array(memory.buffer, pointer, 1);
|
253
|
+
if (rawValues) return { value: ret[0], type: ret[1], js: porfToJSValue({ memory, funcs, pages }, ret[0], ret[1]) };
|
180
254
|
|
181
|
-
|
182
|
-
}
|
183
|
-
|
184
|
-
case '_bytestring': {
|
185
|
-
const pointer = ret;
|
186
|
-
const length = new Int32Array(memory.buffer, pointer, 1);
|
187
|
-
|
188
|
-
return Array.from(new Uint8Array(memory.buffer, pointer + 4, length)).map(x => String.fromCharCode(x)).join('');
|
189
|
-
}
|
190
|
-
|
191
|
-
case 'function': {
|
192
|
-
// wasm func index, including all imports
|
193
|
-
const func = funcs.find(x => (x.originalIndex ?? x.index) === ret);
|
194
|
-
if (!func) return ret;
|
195
|
-
|
196
|
-
// make fake empty func for repl/etc
|
197
|
-
return {[func.name]() {}}[func.name];
|
198
|
-
}
|
199
|
-
|
200
|
-
default: return ret;
|
201
|
-
}
|
255
|
+
return porfToJSValue({ memory, funcs, pages }, ret[0], ret[1]);
|
202
256
|
} catch (e) {
|
203
257
|
if (e.is && e.is(exceptTag)) {
|
204
258
|
const exceptId = e.getArg(exceptTag, 0);
|
@@ -213,6 +267,17 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
213
267
|
throw new constructor(exception.message);
|
214
268
|
}
|
215
269
|
|
270
|
+
if (e instanceof WebAssembly.RuntimeError) {
|
271
|
+
// only backtrace for runner, not test262/etc
|
272
|
+
if (!process.argv[1].includes('/runner')) throw e;
|
273
|
+
|
274
|
+
const match = e.stack.match(/wasm-function\[([0-9]+)\]:([0-9a-z]+)/) ?? [];
|
275
|
+
const funcInd = parseInt(match[1]);
|
276
|
+
const blobOffset = parseInt(match[2]);
|
277
|
+
|
278
|
+
backtrace(funcInd, blobOffset);
|
279
|
+
}
|
280
|
+
|
216
281
|
throw e;
|
217
282
|
}
|
218
283
|
};
|
package/package.json
CHANGED
@@ -1,21 +1,25 @@
|
|
1
1
|
{
|
2
2
|
"name": "porffor",
|
3
3
|
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.14.0-032e4ad08",
|
5
5
|
"author": "CanadaHonk",
|
6
6
|
"license": "MIT",
|
7
|
+
"scripts": {
|
8
|
+
"precompile": "node ./compiler/precompile.js"
|
9
|
+
},
|
7
10
|
"dependencies": {
|
8
|
-
"acorn": "^8.
|
11
|
+
"acorn": "^8.11.3",
|
12
|
+
"node-repl-polyfill": "^0.1.1"
|
9
13
|
},
|
10
14
|
"optionalDependencies": {
|
11
|
-
"@babel/parser": "^7.
|
15
|
+
"@babel/parser": "^7.24.4",
|
12
16
|
"hermes-parser": "^0.18.2",
|
13
17
|
"meriyah": "^4.3.9"
|
14
18
|
},
|
15
19
|
"bin": {
|
16
20
|
"porf": "./runner/index.js"
|
17
21
|
},
|
18
|
-
"main": "./
|
22
|
+
"main": "./compiler/wrap.js",
|
19
23
|
"type": "module",
|
20
24
|
"repository": {
|
21
25
|
"type": "git",
|
@@ -25,4 +29,4 @@
|
|
25
29
|
"url": "https://github.com/CanadaHonk/porffor/issues"
|
26
30
|
},
|
27
31
|
"homepage": "https://porffor.goose.icu"
|
28
|
-
}
|
32
|
+
}
|
package/porf
CHANGED
package/porffor_tmp.c
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
#include <stdint.h>
|
2
|
+
#include <string.h>
|
3
|
+
#include <stdio.h>
|
4
|
+
#include <stdlib.h>
|
5
|
+
|
6
|
+
typedef uint8_t i8;
|
7
|
+
typedef uint16_t i16;
|
8
|
+
typedef int32_t i32;
|
9
|
+
typedef uint32_t u32;
|
10
|
+
typedef int64_t i64;
|
11
|
+
typedef uint64_t u64;
|
12
|
+
typedef float f32;
|
13
|
+
typedef double f64;
|
14
|
+
|
15
|
+
f64 NAN = 0e+0/0e+0;
|
16
|
+
|
17
|
+
struct ReturnValue {
|
18
|
+
f64 value;
|
19
|
+
i32 type;
|
20
|
+
};
|
21
|
+
|
22
|
+
char _memory[131072];
|
23
|
+
|
24
|
+
int _argc; char** _argv;
|
25
|
+
i32 i32_load(i32 align, i32 offset, i32 pointer) {
|
26
|
+
i32 out;
|
27
|
+
memcpy(&out, _memory + offset + pointer, sizeof(out));
|
28
|
+
return out;
|
29
|
+
}
|
30
|
+
|
31
|
+
i32 i32_load8_u(i32 align, i32 offset, i32 pointer) {
|
32
|
+
i8 out;
|
33
|
+
memcpy(&out, _memory + offset + pointer, sizeof(out));
|
34
|
+
return out;
|
35
|
+
}
|
36
|
+
|
37
|
+
void i32_store(i32 align, i32 offset, i32 pointer, i32 value) {
|
38
|
+
memcpy(_memory + offset + pointer, &value, sizeof(value));
|
39
|
+
}
|
40
|
+
|
41
|
+
void __Porffor_readArgv(u32 index, u32 outPtr) {
|
42
|
+
if (index >= _argc) {
|
43
|
+
printf("expected %d arguments\n", index);
|
44
|
+
exit(1);
|
45
|
+
}
|
46
|
+
|
47
|
+
char* arg = _argv[index];
|
48
|
+
|
49
|
+
u32 read = 0;
|
50
|
+
char* out = _memory + outPtr + 4;
|
51
|
+
char ch;
|
52
|
+
while ((ch = *(arg++)) != 0) {
|
53
|
+
out[read++] = ch;
|
54
|
+
}
|
55
|
+
|
56
|
+
memcpy(_memory + outPtr, &read, sizeof(read));
|
57
|
+
}
|
58
|
+
void __Porffor_readFile(u32 pathPtr, u32 outPtr) {
|
59
|
+
char* path = _memory + pathPtr + 4;
|
60
|
+
FILE* fp = fopen(path, "r");
|
61
|
+
if (fp == NULL) {
|
62
|
+
printf("failed to open file: %s\n", path);
|
63
|
+
exit(1);
|
64
|
+
}
|
65
|
+
|
66
|
+
u32 read = 0;
|
67
|
+
char* out = _memory + outPtr + 4;
|
68
|
+
char ch;
|
69
|
+
while ((ch = fgetc(fp)) != EOF) {
|
70
|
+
out[read++] = ch;
|
71
|
+
}
|
72
|
+
|
73
|
+
fclose(fp);
|
74
|
+
|
75
|
+
memcpy(_memory + outPtr, &read, sizeof(read));
|
76
|
+
}
|
77
|
+
|
78
|
+
f64 out = 0;
|
79
|
+
i32 outdtype = 0;
|
80
|
+
|
81
|
+
void __Porffor_print(f64 x, i32 y) {
|
82
|
+
i32 a = 0;
|
83
|
+
i32 b = 0;
|
84
|
+
i32 dtypeswitch_tmp = 0;
|
85
|
+
|
86
|
+
dtypeswitch_tmp = y;
|
87
|
+
// block
|
88
|
+
// if
|
89
|
+
if (dtypeswitch_tmp == 0) {
|
90
|
+
printf("%g\n", x);
|
91
|
+
goto j0;
|
92
|
+
}
|
93
|
+
// end
|
94
|
+
j1:;
|
95
|
+
// if
|
96
|
+
if (dtypeswitch_tmp == 1) {
|
97
|
+
// if
|
98
|
+
if (((u32)x) != 0) {
|
99
|
+
printf("%c", (int)(1.16e+2));
|
100
|
+
printf("%c", (int)(1.14e+2));
|
101
|
+
printf("%c", (int)(1.17e+2));
|
102
|
+
printf("%c", (int)(1.01e+2));
|
103
|
+
} else {
|
104
|
+
printf("%c", (int)(1.02e+2));
|
105
|
+
printf("%c", (int)(9.7e+1));
|
106
|
+
printf("%c", (int)(1.08e+2));
|
107
|
+
printf("%c", (int)(1.15e+2));
|
108
|
+
printf("%c", (int)(1.01e+2));
|
109
|
+
}
|
110
|
+
// end
|
111
|
+
j3:;
|
112
|
+
goto j0;
|
113
|
+
}
|
114
|
+
// end
|
115
|
+
j2:;
|
116
|
+
// if
|
117
|
+
if (dtypeswitch_tmp == 3) {
|
118
|
+
printf("%c", (int)(1.17e+2));
|
119
|
+
printf("%c", (int)(1.1e+2));
|
120
|
+
printf("%c", (int)(1e+2));
|
121
|
+
printf("%c", (int)(1.01e+2));
|
122
|
+
printf("%c", (int)(1.02e+2));
|
123
|
+
printf("%c", (int)(1.05e+2));
|
124
|
+
printf("%c", (int)(1.1e+2));
|
125
|
+
printf("%c", (int)(1.01e+2));
|
126
|
+
printf("%c", (int)(1e+2));
|
127
|
+
goto j0;
|
128
|
+
}
|
129
|
+
// end
|
130
|
+
j4:;
|
131
|
+
// if
|
132
|
+
if (dtypeswitch_tmp == 4) {
|
133
|
+
// if
|
134
|
+
if (((u32)x) != 0) {
|
135
|
+
printf("%c", (int)(1.23e+2));
|
136
|
+
printf("%c", (int)(1.25e+2));
|
137
|
+
} else {
|
138
|
+
printf("%c", (int)(1.1e+2));
|
139
|
+
printf("%c", (int)(1.17e+2));
|
140
|
+
printf("%c", (int)(1.08e+2));
|
141
|
+
printf("%c", (int)(1.08e+2));
|
142
|
+
}
|
143
|
+
// end
|
144
|
+
j6:;
|
145
|
+
goto j0;
|
146
|
+
}
|
147
|
+
// end
|
148
|
+
j5:;
|
149
|
+
// if
|
150
|
+
if (dtypeswitch_tmp == 5) {
|
151
|
+
printf("%c", (int)(1.02e+2));
|
152
|
+
printf("%c", (int)(1.17e+2));
|
153
|
+
printf("%c", (int)(1.1e+2));
|
154
|
+
printf("%c", (int)(9.9e+1));
|
155
|
+
printf("%c", (int)(1.16e+2));
|
156
|
+
printf("%c", (int)(1.05e+2));
|
157
|
+
printf("%c", (int)(1.11e+2));
|
158
|
+
printf("%c", (int)(1.1e+2));
|
159
|
+
printf("%c", (int)(3.2e+1));
|
160
|
+
printf("%c", (int)(4e+1));
|
161
|
+
printf("%c", (int)(4.1e+1));
|
162
|
+
printf("%c", (int)(3.2e+1));
|
163
|
+
printf("%c", (int)(1.23e+2));
|
164
|
+
printf("%c", (int)(1.25e+2));
|
165
|
+
goto j0;
|
166
|
+
}
|
167
|
+
// end
|
168
|
+
j7:;
|
169
|
+
// if
|
170
|
+
if (dtypeswitch_tmp == 18) {
|
171
|
+
a = (u32)x;
|
172
|
+
b = i32_load(1, 0, a) + a;
|
173
|
+
// loop
|
174
|
+
j9:;
|
175
|
+
printf("%c", (int)((f64)(i32_load8_u(0, 4, a))));
|
176
|
+
a = a + 1;
|
177
|
+
if (a != b) {
|
178
|
+
goto j9;
|
179
|
+
}
|
180
|
+
// end
|
181
|
+
goto j0;
|
182
|
+
}
|
183
|
+
// end
|
184
|
+
j8:;
|
185
|
+
printf("%g\n", x);
|
186
|
+
// end
|
187
|
+
j0:;
|
188
|
+
}
|
189
|
+
|
190
|
+
int main(int argc, char* argv[]) {
|
191
|
+
_argc = argc; _argv = argv;
|
192
|
+
|
193
|
+
out = 1e+0;
|
194
|
+
outdtype = 18;
|
195
|
+
i32_store(1, 0, 65536, 0);
|
196
|
+
__Porffor_readArgv((u32)(0e+0), (u32)(6.5536e+4));
|
197
|
+
__Porffor_readFile((u32)(undefined), (u32)(out));
|
198
|
+
__Porffor_print(out, outdtype);
|
199
|
+
printf("%c", (int)(1e+1));
|
200
|
+
|
201
|
+
return 0;
|
202
|
+
}
|