Skip to content

Commit

Permalink
builtins: add Porffor.allocateBytes
Browse files Browse the repository at this point in the history
based off #56

Co-Authored-By: Bob Varioa <[email protected]>
  • Loading branch information
CanadaHonk and BobVarioa committed Jun 7, 2024
1 parent 592cabd commit cb2f296
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions compiler/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -1140,5 +1140,50 @@ export const BuiltinFuncs = function() {
]
};

this.__Porffor_allocateBytes = {
params: [ Valtype.i32 ],
locals: [],
globals: [ Valtype.i32, Valtype.i32 ],
globalNames: [ 'currentPtr', 'bytesWritten' ],
globalInits: [ 0, pageSize ], // init to pageSize so we always allocate on first call
returns: [ Valtype.i32 ],
returnType: TYPES.number,
wasm: [
// bytesWritten += bytesToAllocate
[ Opcodes.local_get, 0 ],
[ Opcodes.global_get, 1 ],
[ Opcodes.i32_add ],
[ Opcodes.global_set, 1 ],

// if bytesWritten >= pageSize:
[ Opcodes.global_get, 1 ],
...number(pageSize, Valtype.i32),
[ Opcodes.i32_ge_s ],
[ Opcodes.if, Valtype.i32 ],
// bytesWritten = 0
[ Opcodes.local_get, 0 ],
[ Opcodes.global_set, 1 ],

// grow memory by 1 page
...number(1, Valtype.i32),
[ Opcodes.memory_grow, 0x00 ],

// currentPtr = old page count * pageSize
...number(pageSize, Valtype.i32),
[ Opcodes.i32_mul ],
[ Opcodes.global_set, 0 ],
[ Opcodes.else ],
// else, currentPtr += bytesToAllocate
[ Opcodes.global_get, 0 ],
[ Opcodes.local_get, 0 ],
[ Opcodes.i32_add ],
[ Opcodes.global_set, 0 ]
[ Opcodes.end ],

// return currentPtr
[ Opcodes.global_get, 0 ]
]
};

GeneratedBuiltins.BuiltinFuncs.call(this);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "porffor",
"description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
"version": "0.18.10+c0227b1a9",
"version": "0.18.11+3744bea72",
"author": "CanadaHonk",
"license": "MIT",
"scripts": {},
Expand Down
2 changes: 1 addition & 1 deletion runner/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'node:fs';
globalThis.version = '0.18.10+c0227b1a9';
globalThis.version = '0.18.11+3744bea72';

// deno compat
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
Expand Down

0 comments on commit cb2f296

Please sign in to comment.