From 1e97275cf2b8feded8ee0b3b2dc319eff580beef Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 13 May 2024 20:15:11 +0200 Subject: [PATCH] Remove memory intrinsics --- src/ast.ts | 2 -- src/codegen.ts | 11 ----------- src/typeck/expr.ts | 4 ---- std/rt/alloc.nil | 4 ++-- std/rt/rt.mod.nil | 13 +++++++++++++ 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index eb7777f..3d245d3 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -491,8 +491,6 @@ export const BUILTINS = [ "__i64_store", "__i32_load", "__i64_load", - "__memory_size", - "__memory_grow", "__i32_extend_to_i64_u", "___transmute", "___asm", diff --git a/src/codegen.ts b/src/codegen.ts index 71c01e6..fb9a829 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -851,17 +851,6 @@ function lowerExpr( instrs.push({ kind: "i64.store", imm: {} }); break exprKind; } - case "__memory_size": { - assertArgs(0); - instrs.push({ kind: "memory.size" }); - break exprKind; - } - case "__memory_grow": { - assertArgs(1); - lowerExpr(fcx, instrs, expr.args[0]); - instrs.push({ kind: "memory.grow" }); - break exprKind; - } case "__i32_extend_to_i64_u": { assertArgs(1); lowerExpr(fcx, instrs, expr.args[0]); diff --git a/src/typeck/expr.ts b/src/typeck/expr.ts index 3fbb6cf..105b3f8 100644 --- a/src/typeck/expr.ts +++ b/src/typeck/expr.ts @@ -68,10 +68,6 @@ export function typeOfBuiltinValue( return mkTyFn([TYS.I32], TYS.I32); case "__i64_load": return mkTyFn([TYS.I32], TYS.INT); - case "__memory_size": - return mkTyFn([], TYS.I32); - case "__memory_grow": - return mkTyFn([TYS.I32], TYS.I32); case "__i32_extend_to_i64_u": return mkTyFn([TYS.I32], TYS.INT); default: { diff --git a/std/rt/alloc.nil b/std/rt/alloc.nil index b742475..a5f5235 100644 --- a/std/rt/alloc.nil +++ b/std/rt/alloc.nil @@ -15,9 +15,9 @@ function allocate(size: I32, align: I32): I32 = ( let alignedPtr = std.alignUp(HEAD_PTR, align); let newHeadPtr = alignedPtr + size; - if newHeadPtr > __memory_size() then ( + if newHeadPtr > std.rt.memorySize() then ( // 16 pages, very arbitrary. - let result = __memory_grow(16_I32); + let result = std.rt.memoryGrow(16_I32); // If allocation failed we get -1. We don't have negative numbers yet, lol. if result > 4294967295_I32 then ( std.abort("failed to grow memory"); diff --git a/std/rt/rt.mod.nil b/std/rt/rt.mod.nil index 318b4cf..c0967ea 100644 --- a/std/rt/rt.mod.nil +++ b/std/rt/rt.mod.nil @@ -9,3 +9,16 @@ function memcpy(_dst: I32, _src: I32, _n: I32) = "local.get 0", "memory.copy", ); + +function memoryGrow(_size: I32): I32 = + ___asm( + __locals(), + "local.get 0", + "memory.grow", + ); + +function memorySize(): I32 = + ___asm( + __locals(), + "memory.size", + );