diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2020-05-31 21:11:06 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-06-09 00:22:17 -0400 |
| commit | ce3f0077cf3c5ae8edc116177f2f3d33258be9f7 (patch) | |
| tree | a8cc4c5d9721c90b4ccd0304049551952f662dfd /src/ir.cpp | |
| parent | 12051b02f1f455b85d5a519dd1747a67d4bb68d0 (diff) | |
| download | zig-ce3f0077cf3c5ae8edc116177f2f3d33258be9f7.tar.gz zig-ce3f0077cf3c5ae8edc116177f2f3d33258be9f7.zip | |
Add builtin for llvm.wasm.memory.size.i32 instrinsic
This will allow the developer to poll the runtime for currently
allocated memory in the number of Wasm pages. Typical usage:
```zig
var wasm_pages = @wasmMemorySize();
@import("std").debug.assert(wasm_pages > 0);
```
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index a733f282b5..c66b368794 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -556,6 +556,8 @@ static void destroy_instruction_src(IrInstSrc *inst) { return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSpillEnd *>(inst)); case IrInstSrcIdCallArgs: return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcCallArgs *>(inst)); + case IrInstSrcIdWasmMemorySize: + return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcWasmMemorySize *>(inst)); } zig_unreachable(); } @@ -736,6 +738,8 @@ void destroy_instruction_gen(IrInstGen *inst) { return heap::c_allocator.destroy(reinterpret_cast<IrInstGenNegation *>(inst)); case IrInstGenIdNegationWrapping: return heap::c_allocator.destroy(reinterpret_cast<IrInstGenNegationWrapping *>(inst)); + case IrInstGenIdWasmMemorySize: + return heap::c_allocator.destroy(reinterpret_cast<IrInstGenWasmMemorySize *>(inst)); } zig_unreachable(); } @@ -1334,6 +1338,10 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcBoolNot *) { return IrInstSrcIdBoolNot; } +static constexpr IrInstSrcId ir_inst_id(IrInstSrcWasmMemorySize *) { + return IrInstSrcIdWasmMemorySize; +} + static constexpr IrInstSrcId ir_inst_id(IrInstSrcMemset *) { return IrInstSrcIdMemset; } @@ -1955,6 +1963,10 @@ static constexpr IrInstGenId ir_inst_id(IrInstGenConst *) { return IrInstGenIdConst; } +static constexpr IrInstGenId ir_inst_id(IrInstGenWasmMemorySize *) { + return IrInstGenIdWasmMemorySize; +} + template<typename T> static T *ir_create_instruction(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) { T *special_instruction = heap::c_allocator.create<T>(); @@ -3642,6 +3654,20 @@ static IrInstGen *ir_build_bool_not_gen(IrAnalyze *ira, IrInst *source_instr, Ir return &instruction->base; } +static IrInstSrc *ir_build_wasm_memory_size_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node) { + IrInstSrcWasmMemorySize *instruction = ir_build_instruction<IrInstSrcWasmMemorySize>(irb, scope, source_node); + + return &instruction->base; +} + +static IrInstGen *ir_build_wasm_memory_size_gen(IrAnalyze *ira, IrInst *source_instr) { + IrInstGenWasmMemorySize *instruction = ir_build_inst_gen<IrInstGenWasmMemorySize>(&ira->new_irb, + source_instr->scope, source_instr->source_node); + instruction->base.value->type = ira->codegen->builtin_types.entry_i32; + + return &instruction->base; +} + static IrInstSrc *ir_build_memset_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *dest_ptr, IrInstSrc *byte, IrInstSrc *count) { @@ -6754,6 +6780,11 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod IrInstSrc *ir_memset = ir_build_memset_src(irb, scope, node, arg0_value, arg1_value, arg2_value); return ir_lval_wrap(irb, scope, ir_memset, lval, result_loc); } + case BuiltinFnIdWasmMemorySize: + { + IrInstSrc *ir_wasm_memory_size = ir_build_wasm_memory_size_src(irb, scope, node); + return ir_lval_wrap(irb, scope, ir_wasm_memory_size, lval, result_loc); + } case BuiltinFnIdField: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); @@ -27651,6 +27682,16 @@ static IrInstGen *ir_analyze_instruction_has_field(IrAnalyze *ira, IrInstSrcHasF return ir_const_bool(ira, &instruction->base.base, result); } +static IrInstGen *ir_analyze_instruction_wasm_memory_size(IrAnalyze *ira, IrInstSrcWasmMemorySize *instruction) { + if (!target_is_wasm(ira->codegen->zig_target)) { + ir_add_error_node(ira, instruction->base.base.source_node, + buf_sprintf("@wasmMemorySize is a wasm feature only")); + return ira->codegen->invalid_inst_gen; + } + + return ir_build_wasm_memory_size_gen(ira, &instruction->base.base); +} + static IrInstGen *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstSrcBreakpoint *instruction) { return ir_build_breakpoint_gen(ira, &instruction->base.base); } @@ -30892,6 +30933,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc return ir_analyze_instruction_spill_begin(ira, (IrInstSrcSpillBegin *)instruction); case IrInstSrcIdSpillEnd: return ir_analyze_instruction_spill_end(ira, (IrInstSrcSpillEnd *)instruction); + case IrInstSrcIdWasmMemorySize: + return ir_analyze_instruction_wasm_memory_size(ira, (IrInstSrcWasmMemorySize *)instruction); } zig_unreachable(); } @@ -31117,6 +31160,7 @@ bool ir_inst_gen_has_side_effects(IrInstGen *instruction) { case IrInstGenIdBinaryNot: case IrInstGenIdNegation: case IrInstGenIdNegationWrapping: + case IrInstGenIdWasmMemorySize: return false; case IrInstGenIdAsm: @@ -31283,6 +31327,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) { case IrInstSrcIdHasDecl: case IrInstSrcIdAlloca: case IrInstSrcIdSpillEnd: + case IrInstSrcIdWasmMemorySize: return false; case IrInstSrcIdAsm: |
