authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-03 15:14:18-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-03 15:14:18-05:00
log5a86c0499694d4dafa3af97c6b26860714bc8983
tree36c7b589fa136e75ae8f104009f360fc2ee25431
parent8c9016b6d1a62da847faf95b6124515cdcd9fe0b

add volatileStore() builtin function

See #238 We can revisit how volatile will work later - for now here's a builtin function to do it.

4 files changed, 35 insertions(+), 10 deletions(-)

src/all_types.hpp+2
......@@ -1109,6 +1109,7 @@ enum BuiltinFnId {
11091109 BuiltinFnIdCanImplicitCast,
11101110 BuiltinFnIdSetGlobalAlign,
11111111 BuiltinFnIdSetGlobalSection,
1112 BuiltinFnIdVolatileStore,
11121113};
11131114
11141115struct BuiltinFnEntry {
......@@ -1691,6 +1692,7 @@ struct IrInstructionStorePtr {
16911692
16921693 IrInstruction *ptr;
16931694 IrInstruction *value;
1695 bool is_volatile;
16941696};
16951697
16961698struct IrInstructionFieldPtr {
src/codegen.cpp+5-1
......@@ -1303,7 +1303,10 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
13031303 return gen_struct_memcpy(g, value, ptr, op1_type);
13041304 }
13051305
1306 LLVMBuildStore(g->builder, value, ptr);
1306 LLVMValueRef llvm_instruction = LLVMBuildStore(g->builder, value, ptr);
1307
1308 LLVMSetVolatile(llvm_instruction, instruction->is_volatile);
1309
13071310 return nullptr;
13081311}
13091312
......@@ -3686,6 +3689,7 @@ static void define_builtin_fns(CodeGen *g) {
36863689 create_builtin_fn(g, BuiltinFnIdAlloca, "alloca", 2);
36873690 create_builtin_fn(g, BuiltinFnIdSetGlobalAlign, "setGlobalAlign", 2);
36883691 create_builtin_fn(g, BuiltinFnIdSetGlobalSection, "setGlobalSection", 2);
3692 create_builtin_fn(g, BuiltinFnIdVolatileStore, "volatileStore", 2);
36893693}
36903694
36913695static void init(CodeGen *g, Buf *source_path) {
src/ir.cpp+25-9
......@@ -1042,13 +1042,14 @@ static IrInstruction *ir_build_unreachable_from(IrBuilder *irb, IrInstruction *o
10421042}
10431043
10441044static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1045 IrInstruction *ptr, IrInstruction *value)
1045 IrInstruction *ptr, IrInstruction *value, bool is_volatile)
10461046{
10471047 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);
10481048 instruction->base.value.special = ConstValSpecialStatic;
10491049 instruction->base.value.type = irb->codegen->builtin_types.entry_void;
10501050 instruction->ptr = ptr;
10511051 instruction->value = value;
1052 instruction->is_volatile = is_volatile;
10521053
10531054 ir_ref_instruction(ptr, irb->current_basic_block);
10541055 ir_ref_instruction(value, irb->current_basic_block);
......@@ -1057,10 +1058,10 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
10571058}
10581059
10591060static IrInstruction *ir_build_store_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
1060 IrInstruction *ptr, IrInstruction *value)
1061 IrInstruction *ptr, IrInstruction *value, bool is_volatile)
10611062{
10621063 IrInstruction *new_instruction = ir_build_store_ptr(irb, old_instruction->scope,
1063 old_instruction->source_node, ptr, value);
1064 old_instruction->source_node, ptr, value, is_volatile);
10641065 ir_link_new_instruction(new_instruction, old_instruction);
10651066 return new_instruction;
10661067}
......@@ -3298,7 +3299,7 @@ static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node)
32983299 if (lvalue == irb->codegen->invalid_instruction || rvalue == irb->codegen->invalid_instruction)
32993300 return irb->codegen->invalid_instruction;
33003301
3301 ir_build_store_ptr(irb, scope, node, lvalue, rvalue);
3302 ir_build_store_ptr(irb, scope, node, lvalue, rvalue, false);
33023303 return ir_build_const_void(irb, scope, node);
33033304}
33043305
......@@ -3311,7 +3312,7 @@ static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *no
33113312 if (op2 == irb->codegen->invalid_instruction)
33123313 return op2;
33133314 IrInstruction *result = ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);
3314 ir_build_store_ptr(irb, scope, node, lvalue, result);
3315 ir_build_store_ptr(irb, scope, node, lvalue, result, false);
33153316 return ir_build_const_void(irb, scope, node);
33163317}
33173318
......@@ -4199,6 +4200,20 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
41994200 return ir_build_set_global_section(irb, scope, node, var, arg1_value);
42004201 }
42014202 }
4203 case BuiltinFnIdVolatileStore:
4204 {
4205 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4206 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4207 if (arg0_value == irb->codegen->invalid_instruction)
4208 return arg0_value;
4209
4210 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4211 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4212 if (arg1_value == irb->codegen->invalid_instruction)
4213 return arg1_value;
4214
4215 return ir_build_store_ptr(irb, scope, node, arg0_value, arg1_value, true);
4216 }
42024217 }
42034218 zig_unreachable();
42044219}
......@@ -4613,7 +4628,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
46134628 } else {
46144629 elem_val = ir_build_load_ptr(irb, child_scope, node, elem_ptr);
46154630 }
4616 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val));
4631 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val, false));
46174632
46184633 LoopStackItem *loop_stack_item = irb->loop_stack.add_one();
46194634 loop_stack_item->break_block = end_block;
......@@ -4627,7 +4642,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
46274642
46284643 ir_set_cursor_at_end(irb, continue_block);
46294644 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
4630 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val));
4645 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val, false));
46314646 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
46324647
46334648 ir_set_cursor_at_end(irb, end_block);
......@@ -9274,11 +9289,12 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
92749289 }
92759290 new_ptr_inst->value.type = ptr->value.type;
92769291 ir_build_store_ptr(&ira->new_irb, store_ptr_instruction->base.scope,
9277 store_ptr_instruction->base.source_node, new_ptr_inst, casted_value);
9292 store_ptr_instruction->base.source_node, new_ptr_inst, casted_value, false);
92789293 return ir_analyze_void(ira, &store_ptr_instruction->base);
92799294 }
92809295
9281 ir_build_store_ptr_from(&ira->new_irb, &store_ptr_instruction->base, ptr, casted_value);
9296 ir_build_store_ptr_from(&ira->new_irb, &store_ptr_instruction->base, ptr, casted_value,
9297 store_ptr_instruction->is_volatile);
92829298 return ira->codegen->builtin_types.entry_void;
92839299}
92849300
src/ir_print.cpp+3
......@@ -299,6 +299,9 @@ static void ir_print_store_ptr(IrPrint *irp, IrInstructionStorePtr *instruction)
299299 ir_print_var_instruction(irp, instruction->ptr);
300300 fprintf(irp->f, " = ");
301301 ir_print_other_instruction(irp, instruction->value);
302 if (instruction->is_volatile) {
303 fprintf(irp->f, " // volatile");
304 }
302305}
303306
304307static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {