Add intrinsics.wasm_memory_grow intrinsics.wasm_memory_size

This commit is contained in:
gingerBill
2022-03-30 17:29:37 +01:00
parent 46161f7e19
commit 72ae061769
5 changed files with 118 additions and 2 deletions
+4
View File
@@ -201,6 +201,10 @@ type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, raw
type_hasher_proc :: proc($T: typeid) -> (hasher: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr) where type_is_comparable(T) ---
// WASM targets only
wasm_memory_grow :: proc(index, delta: uintptr) -> int ---
wasm_memory_size :: proc(index: uintptr) -> int ---
// Internal compiler use only
__entry_point :: proc() ---
+68 -1
View File
@@ -304,7 +304,7 @@ bool check_builtin_objc_procedure(CheckerContext *c, Operand *operand, Ast *call
} else if (!is_operand_value(self) || !check_is_assignable_to(c, &self, t_objc_id)) {
gbString e = expr_to_string(self.expr);
gbString t = type_to_string(self.type);
error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s %d", LIT(builtin_name), e, t, self.type->kind);
error(self.expr, "'%.*s' expected a type or value derived from intrinsics.objc_object, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
@@ -4111,6 +4111,73 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
}
case BuiltinProc_wasm_memory_grow:
{
if (!is_arch_wasm()) {
error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
return false;
}
Operand index = {};
Operand delta = {};
check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
check_expr(c, &delta, ce->args[1]); if (delta.mode == Addressing_Invalid) return false;
convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
convert_to_typed(c, &delta, t_uintptr); if (delta.mode == Addressing_Invalid) return false;
if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
gbString e = expr_to_string(index.expr);
gbString t = type_to_string(index.type);
error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
if (!is_operand_value(delta) || !check_is_assignable_to(c, &delta, t_uintptr)) {
gbString e = expr_to_string(delta.expr);
gbString t = type_to_string(delta.type);
error(delta.expr, "'%.*s' expected a uintptr for the memory delta, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
operand->mode = Addressing_Value;
operand->type = t_int;
operand->value = {};
break;
}
break;
case BuiltinProc_wasm_memory_size:
{
if (!is_arch_wasm()) {
error(call, "'%.*s' is only allowed on wasm targets", LIT(builtin_name));
return false;
}
Operand index = {};
check_expr(c, &index, ce->args[0]); if (index.mode == Addressing_Invalid) return false;
convert_to_typed(c, &index, t_uintptr); if (index.mode == Addressing_Invalid) return false;
if (!is_operand_value(index) || !check_is_assignable_to(c, &index, t_uintptr)) {
gbString e = expr_to_string(index.expr);
gbString t = type_to_string(index.type);
error(index.expr, "'%.*s' expected a uintptr for the memory index, got '%s' of type %s", LIT(builtin_name), e, t);
gb_string_free(t);
gb_string_free(e);
return false;
}
operand->mode = Addressing_Value;
operand->type = t_int;
operand->value = {};
break;
}
break;
}
return true;
+3 -1
View File
@@ -1137,7 +1137,9 @@ void check_global_variable_decl(CheckerContext *ctx, Entity *&e, Ast *type_expr,
ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix);
if (is_arch_wasm() && e->Variable.thread_local_model.len != 0) {
error(e->token, "@(thread_local) is not supported for this target platform");
e->Variable.thread_local_model.len = 0;
// NOTE(bill): ignore this message for the time begin
// error(e->token, "@(thread_local) is not supported for this target platform");
}
String context_name = str_lit("variable declaration");
+4
View File
@@ -260,6 +260,8 @@ BuiltinProc__type_end,
BuiltinProc_constant_utf16_cstring,
BuiltinProc_wasm_memory_grow,
BuiltinProc_wasm_memory_size,
BuiltinProc_COUNT,
};
@@ -523,4 +525,6 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
{STR_LIT("constant_utf16_cstring"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("wasm_memory_grow"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
{STR_LIT("wasm_memory_size"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
};
+39
View File
@@ -2207,6 +2207,45 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
return res;
}
case BuiltinProc_wasm_memory_grow:
{
char const *name = "llvm.wasm.memory.grow";
LLVMTypeRef types[1] = {
lb_type(p->module, t_uintptr),
};
unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0]));
LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
LLVMValueRef args[2] = {};
args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value;
args[1] = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_uintptr).value;
lbValue res = {};
res.type = tv.type;
res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
return res;
}
case BuiltinProc_wasm_memory_size:
{
char const *name = "llvm.wasm.memory.size";
LLVMTypeRef types[1] = {
lb_type(p->module, t_uintptr),
};
unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0]));
LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
LLVMValueRef args[1] = {};
args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value;
lbValue res = {};
res.type = tv.type;
res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
return res;
}
}
GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));