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 {...@@ -352,7 +352,7 @@ const WasmPageAllocator = struct {
352 return idx + extendedOffset();352 return idx + extendedOffset();
353 }353 }
354354
355 const prev_page_count = @wasmMemoryGrow(@intCast(u32, page_count));355 const prev_page_count = @wasmMemoryGrow(0, @intCast(u32, page_count));
356 if (prev_page_count <= 0) {356 if (prev_page_count <= 0) {
357 return error.OutOfMemory;357 return error.OutOfMemory;
358 }358 }
src/all_types.hpp+6
...@@ -3737,21 +3737,27 @@ struct IrInstGenMemcpy {...@@ -3737,21 +3737,27 @@ struct IrInstGenMemcpy {
37373737
3738struct IrInstSrcWasmMemorySize {3738struct IrInstSrcWasmMemorySize {
3739 IrInstSrc base;3739 IrInstSrc base;
3740
3741 IrInstSrc *index;
3740};3742};
37413743
3742struct IrInstGenWasmMemorySize {3744struct IrInstGenWasmMemorySize {
3743 IrInstGen base;3745 IrInstGen base;
3746
3747 IrInstGen *index;
3744};3748};
37453749
3746struct IrInstSrcWasmMemoryGrow {3750struct IrInstSrcWasmMemoryGrow {
3747 IrInstSrc base;3751 IrInstSrc base;
37483752
3753 IrInstSrc *index;
3749 IrInstSrc *delta;3754 IrInstSrc *delta;
3750};3755};
37513756
3752struct IrInstGenWasmMemoryGrow {3757struct IrInstGenWasmMemoryGrow {
3753 IrInstGen base;3758 IrInstGen base;
37543759
3760 IrInstGen *index;
3755 IrInstGen *delta;3761 IrInstGen *delta;
3756};3762};
37573763
src/codegen.cpp+5-15
...@@ -5620,26 +5620,16 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutableGen *executable, Ir...@@ -5620,26 +5620,16 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutableGen *executable, Ir
5620}5620}
56215621
5622static LLVMValueRef ir_render_wasm_memory_size(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemorySize *instruction) {5622static 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
5628 // TODO adjust for wasm645623 // TODO adjust for wasm64
5629 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);5624 LLVMValueRef param = ir_llvm_value(g, instruction->index);
5630 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &zero, 1, "");5625 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_size(g), &param, 1, "");
5631 return val;5626 return val;
5632}5627}
56335628
5634static LLVMValueRef ir_render_wasm_memory_grow(CodeGen *g, IrExecutableGen *executable, IrInstGenWasmMemoryGrow *instruction) {5629static 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
5640 // TODO adjust for wasm645630 // TODO adjust for wasm64
5641 LLVMValueRef params[] = {5631 LLVMValueRef params[] = {
5642 LLVMConstNull(g->builtin_types.entry_i32->llvm_type),5632 ir_llvm_value(g, instruction->index),
5643 ir_llvm_value(g, instruction->delta),5633 ir_llvm_value(g, instruction->delta),
5644 };5634 };
5645 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_grow(g), params, 2, "");5635 LLVMValueRef val = LLVMBuildCall(g->builder, gen_wasm_memory_grow(g), params, 2, "");
...@@ -8722,8 +8712,8 @@ static void define_builtin_fns(CodeGen *g) {...@@ -8722,8 +8712,8 @@ static void define_builtin_fns(CodeGen *g) {
8722 create_builtin_fn(g, BuiltinFnIdAs, "as", 2);8712 create_builtin_fn(g, BuiltinFnIdAs, "as", 2);
8723 create_builtin_fn(g, BuiltinFnIdCall, "call", 3);8713 create_builtin_fn(g, BuiltinFnIdCall, "call", 3);
8724 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);8714 create_builtin_fn(g, BuiltinFnIdBitSizeof, "bitSizeOf", 1);
8725 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 0);8715 create_builtin_fn(g, BuiltinFnIdWasmMemorySize, "wasmMemorySize", 1);
8726 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 1);8716 create_builtin_fn(g, BuiltinFnIdWasmMemoryGrow, "wasmMemoryGrow", 2);
8727}8717}
87288718
8729static const char *bool_to_str(bool b) {8719static 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...@@ -4985,35 +4985,45 @@ static IrInstGen *ir_build_vector_extract_elem(IrAnalyze *ira, IrInst *source_in
4985 return &instruction->base;4985 return &instruction->base;
4986}4986}
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) {
4989 IrInstSrcWasmMemorySize *instruction = ir_build_instruction<IrInstSrcWasmMemorySize>(irb, scope, source_node);4989 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
4991 return &instruction->base;4994 return &instruction->base;
4992}4995}
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) {
4995 IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb,4998 IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb,
4996 source_instr->scope, source_instr->source_node);4999 source_instr->scope, source_instr->source_node);
4997 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;5000 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
5001 instruction->index = index;
5002
5003 ir_ref_inst_gen(index);
49985004
4999 return &instruction->base;5005 return &instruction->base;
5000}5006}
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) {
5003 IrInstSrcWasmMemoryGrow *instruction = ir_build_instruction<IrInstSrcWasmMemoryGrow>(irb, scope, source_node);5009 IrInstSrcWasmMemoryGrow *instruction = ir_build_instruction<IrInstSrcWasmMemoryGrow>(irb, scope, source_node);
5010 instruction->index = index;
5004 instruction->delta = delta;5011 instruction->delta = delta;
50055012
5013 ir_ref_instruction(index, irb->current_basic_block);
5006 ir_ref_instruction(delta, irb->current_basic_block);5014 ir_ref_instruction(delta, irb->current_basic_block);
50075015
5008 return &instruction->base;5016 return &instruction->base;
5009}5017}
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) {
5012 IrInstGenWasmMemoryGrow *instruction = ir_build_inst_gen<IrInstGenWasmMemoryGrow>(&ira->new_irb,5020 IrInstGenWasmMemoryGrow *instruction = ir_build_inst_gen<IrInstGenWasmMemoryGrow>(&ira->new_irb,
5013 source_instr->scope, source_instr->source_node);5021 source_instr->scope, source_instr->source_node);
5014 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;5022 instruction->base.value->type = ira->codegen->builtin_types.entry_i32;
5023 instruction->index = index;
5015 instruction->delta = delta;5024 instruction->delta = delta;
50165025
5026 ir_ref_inst_gen(index);
5017 ir_ref_inst_gen(delta);5027 ir_ref_inst_gen(delta);
50185028
5019 return &instruction->base;5029 return &instruction->base;
...@@ -6815,7 +6825,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -6815,7 +6825,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
6815 }6825 }
6816 case BuiltinFnIdWasmMemorySize:6826 case BuiltinFnIdWasmMemorySize:
6817 {6827 {
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);
6819 return ir_lval_wrap(irb, scope, ir_wasm_memory_size, lval, result_loc);6834 return ir_lval_wrap(irb, scope, ir_wasm_memory_size, lval, result_loc);
6820 }6835 }
6821 case BuiltinFnIdWasmMemoryGrow:6836 case BuiltinFnIdWasmMemoryGrow:
...@@ -6825,7 +6840,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -6825,7 +6840,12 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
6825 if (arg0_value == irb->codegen->invalid_inst_src)6840 if (arg0_value == irb->codegen->invalid_inst_src)
6826 return arg0_value;6841 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);
6829 return ir_lval_wrap(irb, scope, ir_wasm_memory_grow, lval, result_loc);6849 return ir_lval_wrap(irb, scope, ir_wasm_memory_grow, lval, result_loc);
6830 }6850 }
6831 case BuiltinFnIdField:6851 case BuiltinFnIdField:
...@@ -27733,7 +27753,17 @@ static IrInstGen *ir_analyze_instruction_wasm_memory_size(IrAnalyze *ira, IrInst...@@ -27733,7 +27753,17 @@ static IrInstGen *ir_analyze_instruction_wasm_memory_size(IrAnalyze *ira, IrInst
27733 return ira->codegen->invalid_inst_gen;27753 return ira->codegen->invalid_inst_gen;
27734 }27754 }
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);
27737}27767}
2773827768
27739static IrInstGen *ir_analyze_instruction_wasm_memory_grow(IrAnalyze *ira, IrInstSrcWasmMemoryGrow *instruction) {27769static 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...@@ -27744,17 +27774,25 @@ static IrInstGen *ir_analyze_instruction_wasm_memory_grow(IrAnalyze *ira, IrInst
27744 return ira->codegen->invalid_inst_gen;27774 return ira->codegen->invalid_inst_gen;
27745 }27775 }
2774627776
27747 IrInstGen *delta = instruction->delta->child;27777 IrInstGen *index = instruction->index->child;
27748 if (type_is_invalid(delta->value->type))27778 if (type_is_invalid(index->value->type))
27749 return ira->codegen->invalid_inst_gen;27779 return ira->codegen->invalid_inst_gen;
2775027780
27751 ZigType *i32_type = ira->codegen->builtin_types.entry_i32;27781 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
27753 IrInstGen *casted_delta = ir_implicit_cast(ira, delta, i32_type);27791 IrInstGen *casted_delta = ir_implicit_cast(ira, delta, i32_type);
27754 if (type_is_invalid(casted_delta->value->type))27792 if (type_is_invalid(casted_delta->value->type))
27755 return ira->codegen->invalid_inst_gen;27793 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);
27758}27796}
2775927797
27760static IrInstGen *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstSrcBreakpoint *instruction) {27798static 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) {...@@ -1717,21 +1717,29 @@ static void ir_print_bool_not(IrPrintGen *irp, IrInstGenBoolNot *instruction) {
1717}1717}
17181718
1719static void ir_print_wasm_memory_size(IrPrintSrc *irp, IrInstSrcWasmMemorySize *instruction) {1719static 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, ")");
1721}1723}
17221724
1723static void ir_print_wasm_memory_size(IrPrintGen *irp, IrInstGenWasmMemorySize *instruction) {1725static 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, ")");
1725}1729}
17261730
1727static void ir_print_wasm_memory_grow(IrPrintSrc *irp, IrInstSrcWasmMemoryGrow *instruction) {1731static void ir_print_wasm_memory_grow(IrPrintSrc *irp, IrInstSrcWasmMemoryGrow *instruction) {
1728 fprintf(irp->f, "@wasmMemoryGrow(");1732 fprintf(irp->f, "@wasmMemoryGrow(");
1733 ir_print_other_inst_src(irp, instruction->index);
1734 fprintf(irp->f, ", ");
1729 ir_print_other_inst_src(irp, instruction->delta);1735 ir_print_other_inst_src(irp, instruction->delta);
1730 fprintf(irp->f, ")");1736 fprintf(irp->f, ")");
1731}1737}
17321738
1733static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *instruction) {1739static void ir_print_wasm_memory_grow(IrPrintGen *irp, IrInstGenWasmMemoryGrow *instruction) {
1734 fprintf(irp->f, "@wasmMemoryGrow(");1740 fprintf(irp->f, "@wasmMemoryGrow(");
1741 ir_print_other_inst_gen(irp, instruction->index);
1742 fprintf(irp->f, ", ");
1735 ir_print_other_inst_gen(irp, instruction->delta);1743 ir_print_other_inst_gen(irp, instruction->delta);
1736 fprintf(irp->f, ")");1744 fprintf(irp->f, ")");
1737}1745}
test/compile_errors.zig+2-2
...@@ -7507,7 +7507,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7507,7 +7507,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75077507
7508 cases.add("wasmMemorySize is a compile error in non-Wasm targets",7508 cases.add("wasmMemorySize is a compile error in non-Wasm targets",
7509 \\export fn foo() void {7509 \\export fn foo() void {
7510 \\ _ = @wasmMemorySize();7510 \\ _ = @wasmMemorySize(0);
7511 \\ return;7511 \\ return;
7512 \\}7512 \\}
7513 , &[_][]const u8{7513 , &[_][]const u8{
...@@ -7516,7 +7516,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7516,7 +7516,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75167516
7517 cases.add("wasmMemoryGrow is a compile error in non-Wasm targets",7517 cases.add("wasmMemoryGrow is a compile error in non-Wasm targets",
7518 \\export fn foo() void {7518 \\export fn foo() void {
7519 \\ _ = @wasmMemoryGrow(1);7519 \\ _ = @wasmMemoryGrow(0, 1);
7520 \\ return;7520 \\ return;
7521 \\}7521 \\}
7522 , &[_][]const u8{7522 , &[_][]const u8{