authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-05 18:43:16-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-05 18:43:16-05:00
log24048b2af62f1b36678aa08a20d0754cb712e485
tree09519ba8509ba40a3c1d9c85eee7d35f181ca37b
parent0541532ed6cf5a32b57a4a3f74e6d3a1699223a7

IR: implement break and continue


7 files changed, 228 insertions(+), 193 deletions(-)

src/all_types.hpp+16-3
......@@ -642,9 +642,11 @@ struct AstNodeBoolLiteral {
642642};
643643
644644struct AstNodeBreakExpr {
645 bool is_inline; // TODO
645646};
646647
647648struct AstNodeContinueExpr {
649 bool is_inline; // TODO
648650};
649651
650652struct AstNodeArrayType {
......@@ -1233,11 +1235,21 @@ struct LabelTableEntry {
12331235 bool used;
12341236};
12351237
1238enum ScopeId {
1239 ScopeIdDecls,
1240 ScopeIdBlock,
1241 ScopeIdDefer,
1242 ScopeIdVarDecl,
1243 ScopeIdCImport,
1244 ScopeIdLoop,
1245 ScopeIdFnDef,
1246};
1247
12361248struct Scope {
1237 AstNode *node;
1249 ScopeId id;
1250 AstNode *source_node;
12381251
1239 // if the scope has a parent, this is it. Every scope has a parent except
1240 // ScopeIdGlobal
1252 // if the scope has a parent, this is it
12411253 Scope *parent;
12421254
12431255 ZigLLVMDIScope *di_scope;
......@@ -1293,6 +1305,7 @@ struct ScopeCImport {
12931305// This scope is created for a loop such as for or while in order to
12941306// make break and continue statements work.
12951307// NodeTypeForExpr or NodeTypeWhileExpr
1308// TODO I think we can get rid of this
12961309struct ScopeLoop {
12971310 Scope base;
12981311};
src/analyze.cpp+15-21
......@@ -130,15 +130,16 @@ ScopeDecls *get_container_scope(TypeTableEntry *type_entry) {
130130 return *get_container_scope_ptr(type_entry);
131131}
132132
133void init_scope(Scope *dest, AstNode *node, Scope *parent) {
134 dest->node = node;
133void init_scope(Scope *dest, ScopeId id, AstNode *source_node, Scope *parent) {
134 dest->id = id;
135 dest->source_node = source_node;
135136 dest->parent = parent;
136137}
137138
138139static ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import) {
139140 assert(node->type == NodeTypeRoot || node->type == NodeTypeContainerDecl);
140141 ScopeDecls *scope = allocate<ScopeDecls>(1);
141 init_scope(&scope->base, node, parent);
142 init_scope(&scope->base, ScopeIdDecls, node, parent);
142143 scope->decl_table.init(4);
143144 scope->container_type = container_type;
144145 scope->import = import;
......@@ -148,7 +149,7 @@ static ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEnt
148149Scope *create_block_scope(AstNode *node, Scope *parent) {
149150 assert(node->type == NodeTypeBlock);
150151 ScopeBlock *scope = allocate<ScopeBlock>(1);
151 init_scope(&scope->base, node, parent);
152 init_scope(&scope->base, ScopeIdBlock, node, parent);
152153 scope->label_table.init(1);
153154 return &scope->base;
154155}
......@@ -156,14 +157,13 @@ Scope *create_block_scope(AstNode *node, Scope *parent) {
156157Scope *create_defer_scope(AstNode *node, Scope *parent) {
157158 assert(node->type == NodeTypeDefer);
158159 ScopeDefer *scope = allocate<ScopeDefer>(1);
159 init_scope(&scope->base, node, parent);
160 init_scope(&scope->base, ScopeIdDefer, node, parent);
160161 return &scope->base;
161162}
162163
163164Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var) {
164 assert(node->type == NodeTypeVariableDeclaration || node->type == NodeTypeParamDecl);
165165 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);
166 init_scope(&scope->base, node, parent);
166 init_scope(&scope->base, ScopeIdVarDecl, node, parent);
167167 scope->var = var;
168168 return &scope->base;
169169}
......@@ -171,7 +171,7 @@ Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var) {
171171Scope *create_cimport_scope(AstNode *node, Scope *parent) {
172172 assert(node->type == NodeTypeFnCallExpr);
173173 ScopeCImport *scope = allocate<ScopeCImport>(1);
174 init_scope(&scope->base, node, parent);
174 init_scope(&scope->base, ScopeIdCImport, node, parent);
175175 buf_resize(&scope->c_import_buf, 0);
176176 return &scope->base;
177177}
......@@ -179,21 +179,21 @@ Scope *create_cimport_scope(AstNode *node, Scope *parent) {
179179Scope *create_loop_scope(AstNode *node, Scope *parent) {
180180 assert(node->type == NodeTypeWhileExpr || node->type == NodeTypeForExpr);
181181 ScopeLoop *scope = allocate<ScopeLoop>(1);
182 init_scope(&scope->base, node, parent);
182 init_scope(&scope->base, ScopeIdLoop, node, parent);
183183 return &scope->base;
184184}
185185
186186ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) {
187187 assert(node->type == NodeTypeFnDef);
188188 ScopeFnDef *scope = allocate<ScopeFnDef>(1);
189 init_scope(&scope->base, node, parent);
189 init_scope(&scope->base, ScopeIdFnDef, node, parent);
190190 scope->fn_entry = fn_entry;
191191 return scope;
192192}
193193
194194ImportTableEntry *get_scope_import(Scope *scope) {
195195 while (scope) {
196 if (scope->node->type == NodeTypeRoot || scope->node->type == NodeTypeContainerDecl) {
196 if (scope->id == ScopeIdDecls) {
197197 ScopeDecls *decls_scope = (ScopeDecls *)scope;
198198 assert(decls_scope->import);
199199 return decls_scope->import;
......@@ -1991,9 +1991,7 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *
19911991
19921992Tld *find_decl(Scope *scope, Buf *name) {
19931993 while (scope) {
1994 if (scope->node->type == NodeTypeRoot ||
1995 scope->node->type == NodeTypeContainerDecl)
1996 {
1994 if (scope->id == ScopeIdDecls) {
19971995 ScopeDecls *decls_scope = (ScopeDecls *)scope;
19981996 auto entry = decls_scope->decl_table.maybe_get(name);
19991997 if (entry)
......@@ -2006,15 +2004,11 @@ Tld *find_decl(Scope *scope, Buf *name) {
20062004
20072005VariableTableEntry *find_variable(CodeGen *g, Scope *scope, Buf *name) {
20082006 while (scope) {
2009 if (scope->node->type == NodeTypeVariableDeclaration ||
2010 scope->node->type == NodeTypeParamDecl)
2011 {
2007 if (scope->id == ScopeIdVarDecl) {
20122008 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
20132009 if (buf_eql_buf(name, &var_scope->var->name))
20142010 return var_scope->var;
2015 } else if (scope->node->type == NodeTypeRoot ||
2016 scope->node->type == NodeTypeContainerDecl)
2017 {
2011 } else if (scope->id == ScopeIdDecls) {
20182012 ScopeDecls *decls_scope = (ScopeDecls *)scope;
20192013 auto entry = decls_scope->decl_table.maybe_get(name);
20202014 if (entry) {
......@@ -2034,7 +2028,7 @@ VariableTableEntry *find_variable(CodeGen *g, Scope *scope, Buf *name) {
20342028
20352029FnTableEntry *scope_fn_entry(Scope *scope) {
20362030 while (scope) {
2037 if (scope->node->type == NodeTypeFnDef) {
2031 if (scope->id == ScopeIdFnDef) {
20382032 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
20392033 return fn_scope->fn_entry;
20402034 }
src/ast_render.cpp+58-7
......@@ -364,6 +364,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
364364 case NodeTypeSwitchProng:
365365 case NodeTypeSwitchRange:
366366 case NodeTypeLabel:
367 case NodeTypeStructValueField:
367368 zig_unreachable();
368369 case NodeTypeRoot:
369370 for (size_t i = 0; i < node->data.root.top_level_decls.length; i += 1) {
......@@ -602,9 +603,30 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
602603 }
603604 case NodeTypeContainerInitExpr:
604605 render_node_ungrouped(ar, node->data.container_init_expr.type);
605 fprintf(ar->f, "{");
606 assert(node->data.container_init_expr.entries.length == 0);
606 if (node->data.container_init_expr.kind == ContainerInitKindStruct) {
607 fprintf(ar->f, "{\n");
608 ar->indent += ar->indent_size;
609 } else {
610 fprintf(ar->f, "{");
611 }
612 for (size_t i = 0; i < node->data.container_init_expr.entries.length; i += 1) {
613 AstNode *entry = node->data.container_init_expr.entries.at(i);
614 if (entry->type == NodeTypeStructValueField) {
615 Buf *name = entry->data.struct_val_field.name;
616 AstNode *expr = entry->data.struct_val_field.expr;
617 fprintf(ar->f, ".%s = ", buf_ptr(name));
618 render_node_grouped(ar, expr);
619 fprintf(ar->f, ",\n");
620 } else {
621 if (i != 0)
622 fprintf(ar->f, ", ");
623 render_node_grouped(ar, entry);
624 }
625 }
607626 fprintf(ar->f, "}");
627 if (node->data.container_init_expr.kind == ContainerInitKindStruct) {
628 ar->indent -= ar->indent_size;
629 }
608630 break;
609631 case NodeTypeArrayType:
610632 {
......@@ -788,7 +810,40 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
788810 }
789811 case NodeTypeGoto:
790812 {
791 fprintf(ar->f, "goto %s", buf_ptr(node->data.goto_expr.name));
813 const char *inline_str = node->data.goto_expr.is_inline ? "inline " : "";
814 fprintf(ar->f, "%sgoto %s", inline_str, buf_ptr(node->data.goto_expr.name));
815 break;
816 }
817 case NodeTypeForExpr:
818 {
819 const char *inline_str = node->data.for_expr.is_inline ? "inline " : "";
820 fprintf(ar->f, "%sfor (", inline_str);
821 render_node_grouped(ar, node->data.for_expr.array_expr);
822 fprintf(ar->f, ") ");
823 if (node->data.for_expr.elem_node) {
824 fprintf(ar->f, "|");
825 if (node->data.for_expr.elem_is_ptr)
826 fprintf(ar->f, "*");
827 render_node_grouped(ar, node->data.for_expr.elem_node);
828 if (node->data.for_expr.index_node) {
829 fprintf(ar->f, ", ");
830 render_node_grouped(ar, node->data.for_expr.index_node);
831 }
832 fprintf(ar->f, "| ");
833 }
834 render_node_grouped(ar, node->data.for_expr.body);
835 break;
836 }
837 case NodeTypeBreak:
838 {
839 const char *inline_str = node->data.break_expr.is_inline ? "inline " : "";
840 fprintf(ar->f, "%sbreak", inline_str);
841 break;
842 }
843 case NodeTypeContinue:
844 {
845 const char *inline_str = node->data.continue_expr.is_inline ? "inline " : "";
846 fprintf(ar->f, "%scontinue", inline_str);
792847 break;
793848 }
794849 case NodeTypeFnDecl:
......@@ -797,12 +852,8 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
797852 case NodeTypeUnwrapErrorExpr:
798853 case NodeTypeSliceExpr:
799854 case NodeTypeStructField:
800 case NodeTypeStructValueField:
801855 case NodeTypeUse:
802856 case NodeTypeZeroesLiteral:
803 case NodeTypeForExpr:
804 case NodeTypeBreak:
805 case NodeTypeContinue:
806857 zig_panic("TODO more ast rendering");
807858 }
808859}
src/codegen.cpp+50-35
......@@ -277,40 +277,55 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
277277 if (scope->di_scope)
278278 return scope->di_scope;
279279
280 if (scope->node->type == NodeTypeFnDef) {
281 assert(scope->parent);
282 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
283 FnTableEntry *fn_table_entry = fn_scope->fn_entry;
284 unsigned line_number = fn_table_entry->proto_node->line + 1;
285 unsigned scope_line = line_number;
286 bool is_definition = fn_table_entry->fn_def_node != nullptr;
287 unsigned flags = 0;
288 bool is_optimized = g->is_release_build;
289 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
290 get_di_scope(g, scope->parent), buf_ptr(&fn_table_entry->symbol_name), "",
291 scope->node->owner->di_file, line_number,
292 fn_table_entry->type_entry->di_type, fn_table_entry->internal_linkage,
293 is_definition, scope_line, flags, is_optimized, nullptr);
294
295 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
296 ZigLLVMFnSetSubprogram(fn_llvm_value(g, fn_table_entry), subprogram);
297 } else if (scope->node->type == NodeTypeRoot) {
298 scope->di_scope = ZigLLVMFileToScope(scope->node->owner->di_file);
299 } else if (scope->node->type == NodeTypeContainerDecl) {
300 ScopeDecls *decls_scope = (ScopeDecls *)scope;
301 assert(decls_scope->container_type);
302 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
303 } else {
304 assert(scope->parent);
305 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
306 get_di_scope(g, scope->parent),
307 scope->node->owner->di_file,
308 scope->node->line + 1,
309 scope->node->column + 1);
310 scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
280 ImportTableEntry *import = get_scope_import(scope);
281 switch (scope->id) {
282 case ScopeIdCImport:
283 zig_unreachable();
284 case ScopeIdFnDef:
285 {
286 assert(scope->parent);
287 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
288 FnTableEntry *fn_table_entry = fn_scope->fn_entry;
289 unsigned line_number = fn_table_entry->proto_node->line + 1;
290 unsigned scope_line = line_number;
291 bool is_definition = fn_table_entry->fn_def_node != nullptr;
292 unsigned flags = 0;
293 bool is_optimized = g->is_release_build;
294 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
295 get_di_scope(g, scope->parent), buf_ptr(&fn_table_entry->symbol_name), "",
296 import->di_file, line_number,
297 fn_table_entry->type_entry->di_type, fn_table_entry->internal_linkage,
298 is_definition, scope_line, flags, is_optimized, nullptr);
299
300 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
301 ZigLLVMFnSetSubprogram(fn_llvm_value(g, fn_table_entry), subprogram);
302 return scope->di_scope;
303 }
304 case ScopeIdDecls:
305 if (scope->parent) {
306 ScopeDecls *decls_scope = (ScopeDecls *)scope;
307 assert(decls_scope->container_type);
308 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
309 } else {
310 scope->di_scope = ZigLLVMFileToScope(import->di_file);
311 }
312 return scope->di_scope;
313 case ScopeIdBlock:
314 case ScopeIdDefer:
315 case ScopeIdVarDecl:
316 case ScopeIdLoop:
317 {
318 assert(scope->parent);
319 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
320 get_di_scope(g, scope->parent),
321 import->di_file,
322 scope->source_node->line + 1,
323 scope->source_node->column + 1);
324 scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
325 return scope->di_scope;
326 }
311327 }
312
313 return scope->di_scope;
328 zig_unreachable();
314329}
315330
316331static void clear_debug_source_node(CodeGen *g) {
......@@ -400,11 +415,11 @@ static bool ir_want_debug_safety(CodeGen *g, IrInstruction *instruction) {
400415 // TODO memoize
401416 Scope *scope = instruction->scope;
402417 while (scope) {
403 if (scope->node->type == NodeTypeBlock) {
418 if (scope->id == ScopeIdBlock) {
404419 ScopeBlock *block_scope = (ScopeBlock *)scope;
405420 if (block_scope->safety_set_node)
406421 return !block_scope->safety_off;
407 } else if (scope->node->type == NodeTypeRoot || scope->node->type == NodeTypeContainerDecl) {
422 } else if (scope->id == ScopeIdDecls) {
408423 ScopeDecls *decls_scope = (ScopeDecls *)scope;
409424 if (decls_scope->safety_set_node)
410425 return !decls_scope->safety_off;
src/ir.cpp+73-126
......@@ -18,12 +18,17 @@ struct IrExecContext {
1818 size_t mem_slot_count;
1919};
2020
21struct LoopStackItem {
22 IrBasicBlock *break_block;
23 IrBasicBlock *continue_block;
24 bool is_inline;
25};
26
2127struct IrBuilder {
2228 CodeGen *codegen;
2329 IrExecutable *exec;
2430 IrBasicBlock *current_basic_block;
25 ZigList<IrBasicBlock *> break_block_stack;
26 ZigList<IrBasicBlock *> continue_block_stack;
31 ZigList<LoopStackItem> loop_stack;
2732};
2833
2934struct IrAnalyze {
......@@ -1241,13 +1246,17 @@ static void ir_gen_defers_for_block(IrBuilder *irb, Scope *parent_scope, Scope *
12411246 bool gen_error_defers, bool gen_maybe_defers)
12421247{
12431248 while (inner_scope != outer_scope) {
1244 if (inner_scope->node->type == NodeTypeDefer &&
1245 ((inner_scope->node->data.defer.kind == ReturnKindUnconditional) ||
1246 (gen_error_defers && inner_scope->node->data.defer.kind == ReturnKindError) ||
1247 (gen_maybe_defers && inner_scope->node->data.defer.kind == ReturnKindMaybe)))
1248 {
1249 AstNode *defer_expr_node = inner_scope->node->data.defer.expr;
1250 ir_gen_node(irb, defer_expr_node, parent_scope);
1249 if (inner_scope->id == ScopeIdDefer) {
1250 assert(inner_scope->source_node->type == NodeTypeDefer);
1251 ReturnKind defer_kind = inner_scope->source_node->data.defer.kind;
1252 if (defer_kind == ReturnKindUnconditional ||
1253 (gen_error_defers && defer_kind == ReturnKindError) ||
1254 (gen_maybe_defers && defer_kind == ReturnKindMaybe))
1255 {
1256 AstNode *defer_expr_node = inner_scope->source_node->data.defer.expr;
1257 ir_gen_node(irb, defer_expr_node, parent_scope);
1258 }
1259
12511260 }
12521261 inner_scope = inner_scope->parent;
12531262 }
......@@ -2070,11 +2079,12 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
20702079
20712080 ir_set_cursor_at_end(irb, body_block);
20722081
2073 irb->break_block_stack.append(end_block);
2074 irb->continue_block_stack.append(continue_block);
2082 LoopStackItem *loop_stack_item = irb->loop_stack.add_one();
2083 loop_stack_item->break_block = end_block;
2084 loop_stack_item->continue_block = continue_block;
2085 loop_stack_item->is_inline = is_inline;
20752086 ir_gen_node(irb, node->data.while_expr.body, scope);
2076 irb->break_block_stack.pop();
2077 irb->continue_block_stack.pop();
2087 irb->loop_stack.pop();
20782088
20792089 ir_build_br(irb, scope, node, continue_block, is_inline);
20802090 ir_set_cursor_at_end(irb, end_block);
......@@ -2096,9 +2106,11 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
20962106 }
20972107 assert(elem_node->type == NodeTypeSymbol);
20982108
2099 IrInstruction *array_val = ir_gen_node(irb, array_node, parent_scope);
2100 if (array_val == irb->codegen->invalid_instruction)
2101 return array_val;
2109 IrInstruction *array_val_ptr = ir_gen_node_extra(irb, array_node, parent_scope, LValPurposeAddressOf);
2110 if (array_val_ptr == irb->codegen->invalid_instruction)
2111 return array_val_ptr;
2112
2113 IrInstruction *array_val = ir_build_load_ptr(irb, parent_scope, array_node, array_val_ptr);
21022114
21032115 IrInstruction *array_type = ir_build_typeof(irb, parent_scope, array_node, array_val);
21042116 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_type);
......@@ -2155,7 +2167,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
21552167 ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_inline);
21562168
21572169 ir_set_cursor_at_end(irb, body_block);
2158 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val, index_val, true);
2170 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val_ptr, index_val, true);
21592171 IrInstruction *elem_val;
21602172 if (node->data.for_expr.elem_is_ptr) {
21612173 elem_val = elem_ptr;
......@@ -2164,11 +2176,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
21642176 }
21652177 ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val);
21662178
2167 irb->break_block_stack.append(end_block);
2168 irb->continue_block_stack.append(continue_block);
2179 LoopStackItem *loop_stack_item = irb->loop_stack.add_one();
2180 loop_stack_item->break_block = end_block;
2181 loop_stack_item->continue_block = continue_block;
2182 loop_stack_item->is_inline = is_inline;
21692183 ir_gen_node(irb, body_node, child_scope);
2170 irb->break_block_stack.pop();
2171 irb->continue_block_stack.pop();
2184 irb->loop_stack.pop();
21722185
21732186 ir_build_br(irb, child_scope, node, continue_block, is_inline);
21742187
......@@ -2195,14 +2208,14 @@ static IrInstruction *ir_gen_this_literal(IrBuilder *irb, Scope *scope, AstNode
21952208 return ir_build_const_fn(irb, scope, node, fn_entry);
21962209 }
21972210
2198 if (scope->node->type == NodeTypeContainerDecl) {
2211 if (scope->id == ScopeIdDecls) {
21992212 ScopeDecls *decls_scope = (ScopeDecls *)scope;
22002213 TypeTableEntry *container_type = decls_scope->container_type;
22012214 assert(container_type);
22022215 return ir_build_const_type(irb, scope, node, container_type);
22032216 }
22042217
2205 if (scope->node->type == NodeTypeBlock)
2218 if (scope->id == ScopeIdBlock)
22062219 return ir_build_const_scope(irb, scope, node, scope);
22072220
22082221 zig_unreachable();
......@@ -2246,8 +2259,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
22462259
22472260 return ir_build_array_type(irb, scope, node, size_value, child_type);
22482261 } else {
2249 IrInstruction *child_type = ir_gen_node_extra(irb, child_type_node,
2250 scope, LValPurposeAddressOf);
2262 IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope);
22512263 if (child_type == irb->codegen->invalid_instruction)
22522264 return child_type;
22532265
......@@ -2575,7 +2587,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
25752587
25762588static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name) {
25772589 while (scope) {
2578 if (scope->node->type == NodeTypeBlock) {
2590 if (scope->id == ScopeIdBlock) {
25792591 ScopeBlock *block_scope = (ScopeBlock *)scope;
25802592 auto entry = block_scope->label_table.maybe_get(name);
25812593 if (entry)
......@@ -2589,7 +2601,7 @@ static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name)
25892601
25902602static ScopeBlock *find_block_scope(IrExecutable *exec, Scope *scope) {
25912603 while (scope) {
2592 if (scope->node->type == NodeTypeBlock)
2604 if (scope->id == ScopeIdBlock)
25932605 return (ScopeBlock *)scope;
25942606 scope = scope->parent;
25952607 }
......@@ -2637,6 +2649,36 @@ static IrInstruction *ir_gen_goto(IrBuilder *irb, Scope *scope, AstNode *node) {
26372649 return ir_build_unreachable(irb, scope, node);
26382650}
26392651
2652static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *scope, AstNode *node) {
2653 assert(node->type == NodeTypeBreak);
2654
2655 if (irb->loop_stack.length == 0) {
2656 add_node_error(irb->codegen, node,
2657 buf_sprintf("'break' expression outside loop"));
2658 return irb->codegen->invalid_instruction;
2659 }
2660
2661 bool is_inline = ir_should_inline(irb) || node->data.break_expr.is_inline;
2662 LoopStackItem *loop_stack_item = &irb->loop_stack.last();
2663 IrBasicBlock *dest_block = loop_stack_item->break_block;
2664 return ir_build_br(irb, scope, node, dest_block, is_inline);
2665}
2666
2667static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *node) {
2668 assert(node->type == NodeTypeContinue);
2669
2670 if (irb->loop_stack.length == 0) {
2671 add_node_error(irb->codegen, node,
2672 buf_sprintf("'continue' expression outside loop"));
2673 return irb->codegen->invalid_instruction;
2674 }
2675
2676 bool is_inline = ir_should_inline(irb) || node->data.continue_expr.is_inline;
2677 LoopStackItem *loop_stack_item = &irb->loop_stack.last();
2678 IrBasicBlock *dest_block = loop_stack_item->continue_block;
2679 return ir_build_br(irb, scope, node, dest_block, is_inline);
2680}
2681
26402682static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LValPurpose lval) {
26412683 if (lval == LValPurposeNone)
26422684 return value;
......@@ -2712,11 +2754,13 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
27122754 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
27132755 case NodeTypeTypeLiteral:
27142756 return ir_lval_wrap(irb, scope, ir_gen_type_literal(irb, scope, node), lval);
2757 case NodeTypeBreak:
2758 return ir_lval_wrap(irb, scope, ir_gen_break(irb, scope, node), lval);
2759 case NodeTypeContinue:
2760 return ir_lval_wrap(irb, scope, ir_gen_continue(irb, scope, node), lval);
27152761 case NodeTypeUnwrapErrorExpr:
27162762 case NodeTypeDefer:
27172763 case NodeTypeSliceExpr:
2718 case NodeTypeBreak:
2719 case NodeTypeContinue:
27202764 case NodeTypeCharLiteral:
27212765 case NodeTypeZeroesLiteral:
27222766 case NodeTypeErrorType:
......@@ -7704,87 +7748,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
77047748// }
77057749//}
77067750//
7707//static TypeTableEntry *analyze_while_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
7708// TypeTableEntry *expected_type, AstNode *node)
7709//{
7710// assert(node->type == NodeTypeWhileExpr);
7711//
7712// AstNode **condition_node = &node->data.while_expr.condition;
7713// AstNode *while_body_node = node->data.while_expr.body;
7714// AstNode **continue_expr_node = &node->data.while_expr.continue_expr;
7715//
7716// TypeTableEntry *condition_type = analyze_expression(g, import, context,
7717// g->builtin_types.entry_bool, *condition_node);
7718//
7719// if (*continue_expr_node) {
7720// analyze_expression(g, import, context, g->builtin_types.entry_void, *continue_expr_node);
7721// }
7722//
7723// BlockContext *child_context = new_block_context(node, context);
7724// child_context->parent_loop_node = node;
7725//
7726// analyze_expression(g, import, child_context, g->builtin_types.entry_void, while_body_node);
7727//
7728//
7729// TypeTableEntry *expr_return_type = g->builtin_types.entry_void;
7730//
7731// if (condition_type->id == TypeTableEntryIdInvalid) {
7732// expr_return_type = g->builtin_types.entry_invalid;
7733// } else {
7734// // if the condition is a simple constant expression and there are no break statements
7735// // then the return type is unreachable
7736// ConstExprValue *const_val = &get_resolved_expr(*condition_node)->const_val;
7737// if (const_val->ok) {
7738// if (const_val->data.x_bool) {
7739// node->data.while_expr.condition_always_true = true;
7740// if (!node->data.while_expr.contains_break) {
7741// expr_return_type = g->builtin_types.entry_unreachable;
7742// }
7743// }
7744// }
7745// }
7746//
7747// return expr_return_type;
7748//}
7749//
7750//static TypeTableEntry *analyze_break_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
7751// TypeTableEntry *expected_type, AstNode *node)
7752//{
7753// assert(node->type == NodeTypeBreak);
7754//
7755// AstNode *loop_node = context->parent_loop_node;
7756// if (loop_node) {
7757// if (loop_node->type == NodeTypeWhileExpr) {
7758// loop_node->data.while_expr.contains_break = true;
7759// } else if (loop_node->type == NodeTypeForExpr) {
7760// loop_node->data.for_expr.contains_break = true;
7761// } else {
7762// zig_unreachable();
7763// }
7764// } else {
7765// add_node_error(g, node, buf_sprintf("'break' expression outside loop"));
7766// }
7767// return g->builtin_types.entry_unreachable;
7768//}
7769//
7770//static TypeTableEntry *analyze_continue_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
7771// TypeTableEntry *expected_type, AstNode *node)
7772//{
7773// AstNode *loop_node = context->parent_loop_node;
7774// if (loop_node) {
7775// if (loop_node->type == NodeTypeWhileExpr) {
7776// loop_node->data.while_expr.contains_continue = true;
7777// } else if (loop_node->type == NodeTypeForExpr) {
7778// loop_node->data.for_expr.contains_continue = true;
7779// } else {
7780// zig_unreachable();
7781// }
7782// } else {
7783// add_node_error(g, node, buf_sprintf("'continue' expression outside loop"));
7784// }
7785// return g->builtin_types.entry_unreachable;
7786//}
7787//
77887751//static TypeTableEntry *analyze_defer(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,
77897752// TypeTableEntry *expected_type, AstNode *node)
77907753//{
......@@ -8592,22 +8555,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
85928555// }
85938556//}
85948557//
8595//static LLVMValueRef gen_break(CodeGen *g, AstNode *node) {
8596// assert(node->type == NodeTypeBreak);
8597// LLVMBasicBlockRef dest_block = g->break_block_stack.last();
8598//
8599// set_debug_source_node(g, node);
8600// return LLVMBuildBr(g->builder, dest_block);
8601//}
8602
8603//static LLVMValueRef gen_continue(CodeGen *g, AstNode *node) {
8604// assert(node->type == NodeTypeContinue);
8605// LLVMBasicBlockRef dest_block = g->continue_block_stack.last();
8606//
8607// set_debug_source_node(g, node);
8608// return LLVMBuildBr(g->builder, dest_block);
8609//}
8610//
86118558//static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,
86128559// bool unwrap_maybe, LLVMValueRef *init_value, TypeTableEntry **expr_type, bool var_is_ptr)
86138560//{
src/ir_print.cpp+1-1
......@@ -104,7 +104,7 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
104104 }
105105 case TypeTableEntryIdBlock:
106106 {
107 AstNode *node = const_val->data.x_block->node;
107 AstNode *node = const_val->data.x_block->source_node;
108108 fprintf(irp->f, "(scope:%zu:%zu)", node->line + 1, node->column + 1);
109109 return;
110110 }
test/self_hosted2.zig+15
......@@ -139,6 +139,20 @@ fn testFnWithInlineArgs() {
139139}
140140
141141
142fn testContinueInForLoop() {
143 const array = []i32 {1, 2, 3, 4, 5};
144 var sum : i32 = 0;
145 for (array) |x| {
146 sum += x;
147 if (x < 3) {
148 continue;
149 }
150 break;
151 }
152 assert(sum == 6);
153}
154
155
142156fn assert(ok: bool) {
143157 if (!ok)
144158 @unreachable();
......@@ -158,6 +172,7 @@ fn runAllTests() {
158172 testCompileTimeFib();
159173 testCompileTimeGenericEval();
160174 testFnWithInlineArgs();
175 testContinueInForLoop();
161176}
162177
163178export nakedcc fn _start() -> unreachable {