From 7bb6393b593dcf4c8b929fc6b04b576e55f34607 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 7 Dec 2021 19:35:46 +0100 Subject: stage1: implement @prefetch() builtin --- src/stage1/astgen.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/stage1/astgen.cpp') diff --git a/src/stage1/astgen.cpp b/src/stage1/astgen.cpp index 8fbd02c688..ee59aef04a 100644 --- a/src/stage1/astgen.cpp +++ b/src/stage1/astgen.cpp @@ -349,6 +349,8 @@ void destroy_instruction_src(Stage1ZirInst *inst) { return heap::c_allocator.destroy(reinterpret_cast(inst)); case Stage1ZirInstIdSrc: return heap::c_allocator.destroy(reinterpret_cast(inst)); + case Stage1ZirInstIdPrefetch: + return heap::c_allocator.destroy(reinterpret_cast(inst)); } zig_unreachable(); } @@ -941,6 +943,10 @@ static constexpr Stage1ZirInstId ir_inst_id(Stage1ZirInstSrc *) { return Stage1ZirInstIdSrc; } +static constexpr Stage1ZirInstId ir_inst_id(Stage1ZirInstPrefetch *) { + return Stage1ZirInstIdPrefetch; +} + template static T *ir_create_instruction(Stage1AstGen *ag, Scope *scope, AstNode *source_node) { T *special_instruction = heap::c_allocator.create(); @@ -2870,6 +2876,21 @@ static Stage1ZirInst *ir_build_src(Stage1AstGen *ag, Scope *scope, AstNode *sour return &instruction->base; } +static Stage1ZirInst *ir_build_prefetch(Stage1AstGen *ag, Scope *scope, AstNode *source_node, + Stage1ZirInst *ptr, Stage1ZirInst *options) +{ + Stage1ZirInstPrefetch *prefetch_instruction = ir_build_instruction( + ag, scope, source_node); + prefetch_instruction->ptr = ptr; + prefetch_instruction->options = options; + + ir_ref_instruction(ptr, ag->current_basic_block); + ir_ref_instruction(options, ag->current_basic_block); + + return &prefetch_instruction->base; +} + + static void ir_count_defers(Stage1AstGen *ag, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -5416,6 +5437,29 @@ static Stage1ZirInst *astgen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, Ast Stage1ZirInst *src_inst = ir_build_src(ag, scope, node); return ir_lval_wrap(ag, scope, src_inst, lval, result_loc); } + case BuiltinFnIdPrefetch: + { + ZigType *options_type = get_builtin_type(ag->codegen, "PrefetchOptions"); + Stage1ZirInst *options_type_inst = ir_build_const_type(ag, scope, node, options_type); + ResultLocCast *result_loc_cast = ir_build_cast_result_loc(ag, options_type_inst, no_result_loc()); + + AstNode *ptr_node = node->data.fn_call_expr.params.at(0); + Stage1ZirInst *ptr_value = astgen_node(ag, ptr_node, scope); + if (ptr_value == ag->codegen->invalid_inst_src) + return ptr_value; + + AstNode *options_node = node->data.fn_call_expr.params.at(1); + Stage1ZirInst *options_value = astgen_node_extra(ag, options_node, + scope, LValNone, &result_loc_cast->base); + if (options_value == ag->codegen->invalid_inst_src) + return options_value; + + Stage1ZirInst *casted_options_value = ir_build_implicit_cast( + ag, scope, options_node, options_value, result_loc_cast); + + Stage1ZirInst *ir_extern = ir_build_prefetch(ag, scope, node, ptr_value, casted_options_value); + return ir_lval_wrap(ag, scope, ir_extern, lval, result_loc); + } } zig_unreachable(); } -- cgit v1.2.3