aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-03-24 15:28:58 +0000
committergingerBill <bill@gingerbill.org>2020-03-24 15:28:58 +0000
commitd57fbf48f0cdf4d120679205ef111c3bac4e5545 (patch)
tree4f6c3453e60925cfae2b9942e515a6d554446df3 /src/ir.cpp
parent5cbb266ef5666c1df5d25d3afe23e4a777abd22b (diff)
Support by-reference semantics in `for value_ref, index in &some_array` and `for key, value_ref in &some_map`
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index f51538a2e..4736f2ba1 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -9542,6 +9542,29 @@ void ir_store_type_case_implicit(irProcedure *proc, Ast *clause, irValue *value)
}
}
+irAddr ir_store_range_stmt_val(irProcedure *proc, Ast *stmt_val, irValue *value) {
+ Entity *e = entity_of_node(stmt_val);
+ if (e == nullptr) {
+ return {};
+ }
+
+ if ((e->flags & EntityFlag_Value) == 0) {
+ if (value->kind == irValue_Instr) {
+ if (value->Instr.kind == irInstr_Load) {
+ irValue *ptr = value->Instr.Load.address;
+ ir_module_add_value(proc->module, e, ptr);
+ return ir_addr(ptr);
+ }
+ }
+ }
+
+ // by value
+ irAddr addr = ir_addr(ir_add_local(proc, e, nullptr, false));
+ GB_ASSERT(are_types_identical(ir_type(value), e->type));
+ ir_addr_store(proc, addr, value);
+ return addr;
+}
+
void ir_type_case_body(irProcedure *proc, Ast *label, Ast *clause, irBlock *body, irBlock *done) {
ast_node(cc, CaseClause, clause);
@@ -10076,17 +10099,12 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
}
- irAddr val0_addr = {};
- irAddr val1_addr = {};
- if (val0_type) val0_addr = ir_build_addr(proc, rs->val0);
- if (val1_type) val1_addr = ir_build_addr(proc, rs->val1);
-
if (is_map) {
- if (val0_type) ir_addr_store(proc, val0_addr, key);
- if (val1_type) ir_addr_store(proc, val1_addr, val);
+ if (val0_type) ir_store_range_stmt_val(proc, rs->val0, key);
+ if (val1_type) ir_store_range_stmt_val(proc, rs->val1, val);
} else {
- if (val0_type) ir_addr_store(proc, val0_addr, val);
- if (val1_type) ir_addr_store(proc, val1_addr, key);
+ if (val0_type) ir_store_range_stmt_val(proc, rs->val0, val);
+ if (val1_type) ir_store_range_stmt_val(proc, rs->val1, key);
}
ir_push_target_list(proc, rs->label, done, loop, nullptr);