authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-06-02 14:08:58+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-09 00:22:34-04:00
log8ffa8ed9a8c66d2c8cbdffe619b2c5302d17b814
treeeb309d44785d885af6b1684c6dd93e775b5d8539
parent73a3bfd1dd63a160fd3776b8394d9336abd1ae99

Expose full llvm intrinsic


6 files changed, 72 insertions(+), 30 deletions(-)

lib/std/heap.zig+1-1
......@@ -352,7 +352,7 @@ const WasmPageAllocator = struct {
352352 return idx + extendedOffset();
353353 }
354354
355 const prev_page_count = @wasmMemoryGrow(@intCast(u32, page_count));
355 const prev_page_count = @wasmMemoryGrow(0, @intCast(u32, page_count));
356356 if (prev_page_count <= 0) {
357357 return error.OutOfMemory;
358358 }
src/all_types.hpp+6
......@@ -3737,21 +3737,27 @@ struct IrInstGenMemcpy {
37373737
37383738struct IrInstSrcWasmMemorySize {
37393739 IrInstSrc base;
3740
3741 IrInstSrc *index;
37403742};
37413743
37423744struct IrInstGenWasmMemorySize {
37433745 IrInstGen base;
3746
3747 IrInstGen *index;
37443748};
37453749
37463750struct IrInstSrcWasmMemoryGrow {
37473751 IrInstSrc base;
37483752
3753 IrInstSrc *index;
37493754 IrInstSrc *delta;
37503755};
37513756
37523757struct IrInstGenWasmMemoryGrow {
37533758 IrInstGen base;
37543759
3760 IrInstGen *index;
37553761 IrInstGen *delta;
37563762};
37573763
src/codegen.cpp+5-15
......@@ -5620,26 +5620,16 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutableGen *executable, Ir
56205620}
56215621
56225622static LLVMValueRef ir_render_wasm_memory_size(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemorySize *instruction) {
5623 // When Wasm lands multi-memory support, we can relax this to permit the user specify
5624 // memory index to inquire about. For now, we pass in the recommended default of index 0.
5625 //
5626 // More info:
5627 // https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#current-linear-memory-size
56285623 // TODO adjust for wasm64
5629 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
5630 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &zero, 1, "");
5624 LLVMValueRef param = ir_llvm_value(g, instruction->index);
5625 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &param, 1, "");
56315626 return val;
56325627}
56335628
56345629static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemoryGrow *instruction) {
5635 // When Wasm lands multi-memory support, we can relax this to permit the user specify
5636 // memory index to inquire about. For now, we pass in the recommended default of index 0.
5637 //
5638 // More info:
5639 // https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#grow-linear-memory-size
56405630 // TODO adjust for wasm64
56415631 LLVMValueRef params[] = {
5642 LLVMConstNull(g->builtin_types.entry_i32->llvm_type),
5632 ir_llvm_value(g, instruction->index),
56435633 ir_llvm_value(g, instruction->delta),
56445634 };
56455635 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_grow(g), params, 2, "");
......@@ -8722,8 +8712,8 @@ static void define_builtin_fns(CodeGen *g) {
87228712 create_builtin_fn(g, BuiltinFnIdAs, "as", 2);
87238713 create_builtin_fn(g, BuiltinFnIdCall, "call", 3);
87248714 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);
8725 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 0);
8726 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 1);
8715 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 1);
8716 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 2);
87278717}
87288718
87298719static const char *bool_to_str(bool b) {
src/ir.cpp+48-10
......@@ -4985,35 +4985,45 @@ static IrInstGen *ir_build_vector_extract_elem(IrAnalyze *ira, IrInst *source_in
49854985 return &instruction->base;
49864986}
49874987
4988static IrInstSrc *ir_build_wasm_memory_size_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) {
4988static IrInstSrc *ir_build_wasm_memory_size_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *index) {
49894989 IrInstSrcWasmMemorySize *instruction = ir_build_instruction<IrInstSrcWasmMemorySize>(irb, scope, source_node);
4990 instruction->index = index;
4991
4992 ir_ref_instruction(index, irb->current_basic_block);
49904993
49914994 return &instruction->base;
49924995}
49934996
4994static IrInstGen *ir_build_wasm_memory_size_gen(IrAnalyze *ira, IrInst *source_instr) {
4997static IrInstGen *ir_build_wasm_memory_size_gen(IrAnalyze *ira, IrInst *source_instr, IrInstGen *index) {
49954998 IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb,
49964999 source_instr->scope, source_instr->source_node);
49975000 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
5001 instruction->index = index;
5002
5003 ir_ref_inst_gen(index);
49985004
49995005 return &instruction->base;
50005006}
50015007
5002static IrInstSrc *ir_build_wasm_memory_grow_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *delta) {
5008static IrInstSrc *ir_build_wasm_memory_grow_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *index, IrInstSrc *delta) {
50035009 IrInstSrcWasmMemoryGrow *instruction = ir_build_instruction<IrInstSrcWasmMemoryGrow>(irb, scope, source_node);
5010 instruction->index = index;
50045011 instruction->delta = delta;
50055012
5013 ir_ref_instruction(index, irb->current_basic_block);
50065014 ir_ref_instruction(delta, irb->current_basic_block);
50075015
50085016 return &instruction->base;
50095017}
50105018
5011static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_instr, IrInstGen *delta) {
5019static IrInstGen *ir_build_wasm_memory_grow_gen(IrAnalyze *ira, IrInst *source_instr, IrInstGen *index, IrInstGen *delta) {
50125020 IrInstGenWasmMemoryGrow *instruction = ir_build_inst_gen<IrInstGenWasmMemoryGrow>(&ira->new_irb,
50135021 source_instr->scope, source_instr->source_node);
50145022 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
5023 instruction->index = index;
50155024 instruction->delta = delta;
50165025
5026 ir_ref_inst_gen(index);
50175027 ir_ref_inst_gen(delta);
50185028
50195029 return &instruction->base;
......@@ -6815,7 +6825,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
68156825 }
68166826 case BuiltinFnIdWasmMemorySize:
68176827 {
6818 IrInstSrc *ir_wasm_memory_size = ir_build_wasm_memory_size_src(irb, scope, node);
6828 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
6829 IrInstSrc *arg0_value = ir_gen_node(irb, arg0_node, scope);
6830 if (arg0_value == irb->codegen->invalid_inst_src)
6831 return arg0_value;
6832
6833 IrInstSrc *ir_wasm_memory_size = ir_build_wasm_memory_size_src(irb, scope, node, arg0_value);
68196834 return ir_lval_wrap(irb, scope, ir_wasm_memory_size, lval, result_loc);
68206835 }
68216836 case BuiltinFnIdWasmMemoryGrow:
......@@ -6825,7 +6840,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
68256840 if (arg0_value == irb->codegen->invalid_inst_src)
68266841 return arg0_value;
68276842
6828 IrInstSrc *ir_wasm_memory_grow = ir_build_wasm_memory_grow_src(irb, scope, node, arg0_value);
6843 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
6844 IrInstSrc *arg1_value = ir_gen_node(irb, arg1_node, scope);
6845 if (arg1_value == irb->codegen->invalid_inst_src)
6846 return arg1_value;
6847
6848 IrInstSrc *ir_wasm_memory_grow = ir_build_wasm_memory_grow_src(irb, scope, node, arg0_value, arg1_value);
68296849 return ir_lval_wrap(irb, scope, ir_wasm_memory_grow, lval, result_loc);
68306850 }
68316851 case BuiltinFnIdField:
......@@ -27733,7 +27753,17 @@ static IrInstGen *ir_analyze_instruction_wasm_memory_size(IrAnalyze *ira, IrInst
2773327753 return ira->codegen->invalid_inst_gen;
2773427754 }
2773527755
27736 return ir_build_wasm_memory_size_gen(ira, &instruction->base.base);
27756 IrInstGen *index = instruction->index->child;
27757 if (type_is_invalid(index->value->type))
27758 return ira->codegen->invalid_inst_gen;
27759
27760 ZigType *i32_type = ira->codegen->builtin_types.entry_i32;
27761
27762 IrInstGen *casted_index = ir_implicit_cast(ira, index, i32_type);
27763 if (type_is_invalid(casted_index->value->type))
27764 return ira->codegen->invalid_inst_gen;
27765
27766 return ir_build_wasm_memory_size_gen(ira, &instruction->base.base, casted_index);
2773727767}
2773827768
2773927769static IrInstGen *ir_analyze_instruction_wasm_memory_grow(IrAnalyze *ira, IrInstSrcWasmMemoryGrow *instruction) {
......@@ -27744,17 +27774,25 @@ static IrInstGen *ir_analyze_instruction_wasm_memory_grow(IrAnalyze *ira, IrInst
2774427774 return ira->codegen->invalid_inst_gen;
2774527775 }
2774627776
27747 IrInstGen *delta = instruction->delta->child;
27748 if (type_is_invalid(delta->value->type))
27777 IrInstGen *index = instruction->index->child;
27778 if (type_is_invalid(index->value->type))
2774927779 return ira->codegen->invalid_inst_gen;
2775027780
2775127781 ZigType *i32_type = ira->codegen->builtin_types.entry_i32;
2775227782
27783 IrInstGen *casted_index = ir_implicit_cast(ira, index, i32_type);
27784 if (type_is_invalid(casted_index->value->type))
27785 return ira->codegen->invalid_inst_gen;
27786
27787 IrInstGen *delta = instruction->delta->child;
27788 if (type_is_invalid(delta->value->type))
27789 return ira->codegen->invalid_inst_gen;
27790
2775327791 IrInstGen *casted_delta = ir_implicit_cast(ira, delta, i32_type);
2775427792 if (type_is_invalid(casted_delta->value->type))
2775527793 return ira->codegen->invalid_inst_gen;
2775627794
27757 return ir_build_wasm_memory_grow_gen(ira, &instruction->base.base, casted_delta);
27795 return ir_build_wasm_memory_grow_gen(ira, &instruction->base.base, casted_index, casted_delta);
2775827796}
2775927797
2776027798static IrInstGen *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstSrcBreakpoint *instruction) {
src/ir_print.cpp+10-2
......@@ -1717,21 +1717,29 @@ static void ir_print_bool_not(IrPrintGen *irp, IrInstGenBoolNot *instruction) {
17171717}
17181718
17191719static void ir_print_wasm_memory_size(IrPrintSrc *irp, IrInstSrcWasmMemorySize *instruction) {
1720 fprintf(irp->f, "@wasmMemorySize()");
1720 fprintf(irp->f, "@wasmMemorySize(");
1721 ir_print_other_inst_src(irp, instruction->index);
1722 fprintf(irp->f, ")");
17211723}
17221724
17231725static void ir_print_wasm_memory_size(IrPrintGen *irp, IrInstGenWasmMemorySize *instruction) {
1724 fprintf(irp->f, "@wasmMemorySize()");
1726 fprintf(irp->f, "@wasmMemorySize(");
1727 ir_print_other_inst_gen(irp, instruction->index);
1728 fprintf(irp->f, ")");
17251729}
17261730
17271731static void ir_print_wasm_memory_grow(IrPrintSrc *irp, IrInstSrcWasmMemoryGrow *instruction) {
17281732 fprintf(irp->f, "@wasmMemoryGrow(");
1733 ir_print_other_inst_src(irp, instruction->index);
1734 fprintf(irp->f, ", ");
17291735 ir_print_other_inst_src(irp, instruction->delta);
17301736 fprintf(irp->f, ")");
17311737}
17321738
17331739static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *instruction) {
17341740 fprintf(irp->f, "@wasmMemoryGrow(");
1741 ir_print_other_inst_gen(irp, instruction->index);
1742 fprintf(irp->f, ", ");
17351743 ir_print_other_inst_gen(irp, instruction->delta);
17361744 fprintf(irp->f, ")");
17371745}
test/compile_errors.zig+2-2
......@@ -7507,7 +7507,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75077507
75087508 cases.add("wasmMemorySize is a compile error in non-Wasm targets",
75097509 \\export fn foo() void {
7510 \\ _ = @wasmMemorySize();
7510 \\ _ = @wasmMemorySize(0);
75117511 \\ return;
75127512 \\}
75137513 , &[_][]const u8{
......@@ -7516,7 +7516,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75167516
75177517 cases.add("wasmMemoryGrow is a compile error in non-Wasm targets",
75187518 \\export fn foo() void {
7519 \\ _ = @wasmMemoryGrow(1);
7519 \\ _ = @wasmMemoryGrow(0, 1);
75207520 \\ return;
75217521 \\}
75227522 , &[_][]const u8{