authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-05 03:22:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-05 03:22:00-05:00
log349cd79fe433e165bf6cc7fc614dc5851ac86b6e
tree95d0af046550eeb3c43fb8fee07d5477cf5026e0
parent27a633b32a38bf30e1ccd50d512f5e3498786f1f

containers created during eval get names for parameters


5 files changed, 204 insertions(+), 172 deletions(-)

src/all_types.hpp+1
...@@ -57,6 +57,7 @@ struct IrExecutable {...@@ -57,6 +57,7 @@ struct IrExecutable {
57 Buf *c_import_buf;57 Buf *c_import_buf;
58 AstNode *source_node;58 AstNode *source_node;
59 IrExecutable *parent_exec;59 IrExecutable *parent_exec;
60 Scope *begin_scope;
60};61};
6162
62enum OutType {63enum OutType {
src/analyze.cpp+172
...@@ -3381,3 +3381,175 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *...@@ -3381,3 +3381,175 @@ void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *
3381 zig_unreachable();3381 zig_unreachable();
3382 }3382 }
3383}3383}
3384
3385void render_const_value(Buf *buf, ConstExprValue *const_val) {
3386 switch (const_val->special) {
3387 case ConstValSpecialRuntime:
3388 zig_unreachable();
3389 case ConstValSpecialUndef:
3390 buf_appendf(buf, "undefined");
3391 return;
3392 case ConstValSpecialZeroes:
3393 buf_appendf(buf, "zeroes");
3394 return;
3395 case ConstValSpecialStatic:
3396 break;
3397 }
3398 assert(const_val->type);
3399
3400 TypeTableEntry *canon_type = get_underlying_type(const_val->type);
3401 switch (canon_type->id) {
3402 case TypeTableEntryIdTypeDecl:
3403 zig_unreachable();
3404 case TypeTableEntryIdInvalid:
3405 buf_appendf(buf, "(invalid)");
3406 return;
3407 case TypeTableEntryIdVar:
3408 buf_appendf(buf, "(var)");
3409 return;
3410 case TypeTableEntryIdVoid:
3411 buf_appendf(buf, "{}");
3412 return;
3413 case TypeTableEntryIdNumLitFloat:
3414 buf_appendf(buf, "%f", const_val->data.x_bignum.data.x_float);
3415 return;
3416 case TypeTableEntryIdNumLitInt:
3417 {
3418 BigNum *bignum = &const_val->data.x_bignum;
3419 const char *negative_str = bignum->is_negative ? "-" : "";
3420 buf_appendf(buf, "%s%llu", negative_str, bignum->data.x_uint);
3421 return;
3422 }
3423 case TypeTableEntryIdMetaType:
3424 buf_appendf(buf, "%s", buf_ptr(&const_val->data.x_type->name));
3425 return;
3426 case TypeTableEntryIdInt:
3427 {
3428 BigNum *bignum = &const_val->data.x_bignum;
3429 assert(bignum->kind == BigNumKindInt);
3430 const char *negative_str = bignum->is_negative ? "-" : "";
3431 buf_appendf(buf, "%s%llu", negative_str, bignum->data.x_uint);
3432 }
3433 return;
3434 case TypeTableEntryIdFloat:
3435 {
3436 BigNum *bignum = &const_val->data.x_bignum;
3437 assert(bignum->kind == BigNumKindFloat);
3438 buf_appendf(buf, "%f", bignum->data.x_float);
3439 }
3440 return;
3441 case TypeTableEntryIdUnreachable:
3442 buf_appendf(buf, "@unreachable()");
3443 return;
3444 case TypeTableEntryIdBool:
3445 {
3446 const char *value = const_val->data.x_bool ? "true" : "false";
3447 buf_appendf(buf, "%s", value);
3448 return;
3449 }
3450 case TypeTableEntryIdPointer:
3451 buf_appendf(buf, "&");
3452 if (const_val->data.x_ptr.special == ConstPtrSpecialRuntime) {
3453 buf_appendf(buf, "(runtime pointer value)");
3454 } else if (const_val->data.x_ptr.special == ConstPtrSpecialCStr) {
3455 buf_appendf(buf, "(c str lit)");
3456 } else {
3457 render_const_value(buf, const_ptr_pointee(const_val));
3458 }
3459 return;
3460 case TypeTableEntryIdFn:
3461 {
3462 FnTableEntry *fn_entry = const_val->data.x_fn;
3463 buf_appendf(buf, "%s", buf_ptr(&fn_entry->symbol_name));
3464 return;
3465 }
3466 case TypeTableEntryIdBlock:
3467 {
3468 AstNode *node = const_val->data.x_block->source_node;
3469 buf_appendf(buf, "(scope:%zu:%zu)", node->line + 1, node->column + 1);
3470 return;
3471 }
3472 case TypeTableEntryIdArray:
3473 {
3474 uint64_t len = canon_type->data.array.len;
3475 buf_appendf(buf, "%s{", buf_ptr(&canon_type->name));
3476 for (uint64_t i = 0; i < len; i += 1) {
3477 if (i != 0)
3478 buf_appendf(buf, ",");
3479 ConstExprValue *child_value = &const_val->data.x_array.elements[i];
3480 render_const_value(buf, child_value);
3481 }
3482 buf_appendf(buf, "}");
3483 return;
3484 }
3485 case TypeTableEntryIdNullLit:
3486 {
3487 buf_appendf(buf, "null");
3488 return;
3489 }
3490 case TypeTableEntryIdUndefLit:
3491 {
3492 buf_appendf(buf, "undefined");
3493 return;
3494 }
3495 case TypeTableEntryIdMaybe:
3496 {
3497 if (const_val->data.x_maybe) {
3498 render_const_value(buf, const_val->data.x_maybe);
3499 } else {
3500 buf_appendf(buf, "null");
3501 }
3502 return;
3503 }
3504 case TypeTableEntryIdNamespace:
3505 {
3506 ImportTableEntry *import = const_val->data.x_import;
3507 if (import->c_import_node) {
3508 buf_appendf(buf, "(namespace from C import)");
3509 } else {
3510 buf_appendf(buf, "(namespace: %s)", buf_ptr(import->path));
3511 }
3512 return;
3513 }
3514 case TypeTableEntryIdBoundFn:
3515 {
3516 FnTableEntry *fn_entry = const_val->data.x_bound_fn.fn;
3517 buf_appendf(buf, "(bound fn %s)", buf_ptr(&fn_entry->symbol_name));
3518 return;
3519 }
3520 case TypeTableEntryIdStruct:
3521 {
3522 buf_appendf(buf, "(struct %s constant)", buf_ptr(&canon_type->name));
3523 return;
3524 }
3525 case TypeTableEntryIdEnum:
3526 {
3527 buf_appendf(buf, "(enum %s constant)", buf_ptr(&canon_type->name));
3528 return;
3529 }
3530 case TypeTableEntryIdErrorUnion:
3531 {
3532 buf_appendf(buf, "(error union %s constant)", buf_ptr(&canon_type->name));
3533 return;
3534 }
3535 case TypeTableEntryIdUnion:
3536 {
3537 buf_appendf(buf, "(union %s constant)", buf_ptr(&canon_type->name));
3538 return;
3539 }
3540 case TypeTableEntryIdPureError:
3541 {
3542 buf_appendf(buf, "(pure error constant)");
3543 return;
3544 }
3545 case TypeTableEntryIdEnumTag:
3546 {
3547 TypeTableEntry *enum_type = canon_type->data.enum_tag.enum_type;
3548 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];
3549 buf_appendf(buf, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));
3550 return;
3551 }
3552 }
3553 zig_unreachable();
3554}
3555
src/analyze.hpp+2
...@@ -82,6 +82,8 @@ bool ir_get_var_is_comptime(VariableTableEntry *var);...@@ -82,6 +82,8 @@ bool ir_get_var_is_comptime(VariableTableEntry *var);
82bool const_values_equal(ConstExprValue *a, ConstExprValue *b);82bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
83void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max);83void eval_min_max_value(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val, bool is_max);
8484
85void render_const_value(Buf *buf, ConstExprValue *const_val);
86
85ScopeBlock *create_block_scope(AstNode *node, Scope *parent);87ScopeBlock *create_block_scope(AstNode *node, Scope *parent);
86ScopeDefer *create_defer_scope(AstNode *node, Scope *parent);88ScopeDefer *create_defer_scope(AstNode *node, Scope *parent);
87ScopeDeferExpr *create_defer_expr_scope(AstNode *node, Scope *parent);89ScopeDeferExpr *create_defer_expr_scope(AstNode *node, Scope *parent);
src/ir.cpp+25-3
...@@ -117,6 +117,7 @@ static void ir_ref_bb(IrBasicBlock *bb) {...@@ -117,6 +117,7 @@ static void ir_ref_bb(IrBasicBlock *bb) {
117}117}
118118
119static void ir_ref_instruction(IrInstruction *instruction, IrBasicBlock *cur_bb) {119static void ir_ref_instruction(IrInstruction *instruction, IrBasicBlock *cur_bb) {
120 assert(instruction->id != IrInstructionIdInvalid);
120 instruction->ref_count += 1;121 instruction->ref_count += 1;
121 if (instruction->owner_bb != cur_bb)122 if (instruction->owner_bb != cur_bb)
122 ir_ref_bb(instruction->owner_bb);123 ir_ref_bb(instruction->owner_bb);
...@@ -3095,7 +3096,13 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3095,7 +3096,13 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
30953096
3096static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {3097static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
3097 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);3098 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);
3099 if (op1 == irb->codegen->invalid_instruction)
3100 return op1;
3101
3098 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);3102 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
3103 if (op2 == irb->codegen->invalid_instruction)
3104 return op2;
3105
3099 return ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);3106 return ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);
3100}3107}
31013108
...@@ -3956,6 +3963,8 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node...@@ -3956,6 +3963,8 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
3956 for (size_t i = 0; i < arg_count; i += 1) {3963 for (size_t i = 0; i < arg_count; i += 1) {
3957 AstNode *arg_node = node->data.fn_call_expr.params.at(i);3964 AstNode *arg_node = node->data.fn_call_expr.params.at(i);
3958 args[i] = ir_gen_node(irb, arg_node, scope);3965 args[i] = ir_gen_node(irb, arg_node, scope);
3966 if (args[i] == irb->codegen->invalid_instruction)
3967 return args[i];
3959 }3968 }
39603969
3961 bool is_comptime = node->data.fn_call_expr.is_comptime;3970 bool is_comptime = node->data.fn_call_expr.is_comptime;
...@@ -4946,6 +4955,19 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN...@@ -4946,6 +4955,19 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN
4946 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);4955 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
4947}4956}
49484957
4958static bool render_instance_name_recursive(Buf *name, Scope *outer_scope, Scope *inner_scope) {
4959 if (inner_scope == nullptr || inner_scope == outer_scope) return false;
4960 bool need_comma = render_instance_name_recursive(name, outer_scope, inner_scope->parent);
4961 if (inner_scope->id != ScopeIdVarDecl)
4962 return need_comma;
4963
4964 ScopeVarDecl *var_scope = (ScopeVarDecl *)inner_scope;
4965 if (need_comma)
4966 buf_append_char(name, ',');
4967 render_const_value(name, &var_scope->var->value);
4968 return true;
4969}
4970
4949static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {4971static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
4950 assert(node->type == NodeTypeContainerDecl);4972 assert(node->type == NodeTypeContainerDecl);
49514973
...@@ -4959,9 +4981,7 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,...@@ -4959,9 +4981,7 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
4959 name = buf_alloc();4981 name = buf_alloc();
4960 buf_append_buf(name, &fn_entry->symbol_name);4982 buf_append_buf(name, &fn_entry->symbol_name);
4961 buf_appendf(name, "(");4983 buf_appendf(name, "(");
4962 // TODO render args. note that fn_type_id is likely not complete4984 render_instance_name_recursive(name, &fn_entry->fndef_scope->base, irb->exec->begin_scope);
4963 // at this time.
4964 // probably have to render them from the fn scope
4965 buf_appendf(name, ")");4985 buf_appendf(name, ")");
4966 } else {4986 } else {
4967 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),4987 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),
...@@ -5822,6 +5842,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node...@@ -5822,6 +5842,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
5822 ir_executable.is_inline = true;5842 ir_executable.is_inline = true;
5823 ir_executable.fn_entry = fn_entry;5843 ir_executable.fn_entry = fn_entry;
5824 ir_executable.c_import_buf = c_import_buf;5844 ir_executable.c_import_buf = c_import_buf;
5845 ir_executable.begin_scope = scope;
5825 ir_gen(codegen, node, scope, &ir_executable);5846 ir_gen(codegen, node, scope, &ir_executable);
58265847
5827 if (ir_executable.invalid)5848 if (ir_executable.invalid)
...@@ -5843,6 +5864,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node...@@ -5843,6 +5864,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
5843 analyzed_executable.c_import_buf = c_import_buf;5864 analyzed_executable.c_import_buf = c_import_buf;
5844 analyzed_executable.backward_branch_count = backward_branch_count;5865 analyzed_executable.backward_branch_count = backward_branch_count;
5845 analyzed_executable.backward_branch_quota = backward_branch_quota;5866 analyzed_executable.backward_branch_quota = backward_branch_quota;
5867 analyzed_executable.begin_scope = scope;
5846 TypeTableEntry *result_type = ir_analyze(codegen, &ir_executable, &analyzed_executable, expected_type, node);5868 TypeTableEntry *result_type = ir_analyze(codegen, &ir_executable, &analyzed_executable, expected_type, node);
5847 if (result_type->id == TypeTableEntryIdInvalid)5869 if (result_type->id == TypeTableEntryIdInvalid)
5848 return codegen->invalid_instruction;5870 return codegen->invalid_instruction;
src/ir_print.cpp+4-169
...@@ -32,175 +32,10 @@ static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {...@@ -32,175 +32,10 @@ static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {
32}32}
3333
34static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {34static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {
35 switch (const_val->special) {35 Buf buf = BUF_INIT;
36 case ConstValSpecialRuntime:36 buf_resize(&buf, 0);
37 zig_unreachable();37 render_const_value(&buf, const_val);
38 case ConstValSpecialUndef:38 fprintf(irp->f, "%s", buf_ptr(&buf));
39 fprintf(irp->f, "undefined");
40 return;
41 case ConstValSpecialZeroes:
42 fprintf(irp->f, "zeroes");
43 return;
44 case ConstValSpecialStatic:
45 break;
46 }
47 assert(const_val->type);
48
49 TypeTableEntry *canon_type = get_underlying_type(const_val->type);
50 switch (canon_type->id) {
51 case TypeTableEntryIdTypeDecl:
52 zig_unreachable();
53 case TypeTableEntryIdInvalid:
54 fprintf(irp->f, "(invalid)");
55 return;
56 case TypeTableEntryIdVar:
57 fprintf(irp->f, "(var)");
58 return;
59 case TypeTableEntryIdVoid:
60 fprintf(irp->f, "{}");
61 return;
62 case TypeTableEntryIdNumLitFloat:
63 fprintf(irp->f, "%f", const_val->data.x_bignum.data.x_float);
64 return;
65 case TypeTableEntryIdNumLitInt:
66 {
67 BigNum *bignum = &const_val->data.x_bignum;
68 const char *negative_str = bignum->is_negative ? "-" : "";
69 fprintf(irp->f, "%s%llu", negative_str, bignum->data.x_uint);
70 return;
71 }
72 case TypeTableEntryIdMetaType:
73 fprintf(irp->f, "%s", buf_ptr(&const_val->data.x_type->name));
74 return;
75 case TypeTableEntryIdInt:
76 {
77 BigNum *bignum = &const_val->data.x_bignum;
78 assert(bignum->kind == BigNumKindInt);
79 const char *negative_str = bignum->is_negative ? "-" : "";
80 fprintf(irp->f, "%s%llu", negative_str, bignum->data.x_uint);
81 }
82 return;
83 case TypeTableEntryIdFloat:
84 {
85 BigNum *bignum = &const_val->data.x_bignum;
86 assert(bignum->kind == BigNumKindFloat);
87 fprintf(irp->f, "%f", bignum->data.x_float);
88 }
89 return;
90 case TypeTableEntryIdUnreachable:
91 fprintf(irp->f, "@unreachable()");
92 return;
93 case TypeTableEntryIdBool:
94 {
95 const char *value = const_val->data.x_bool ? "true" : "false";
96 fprintf(irp->f, "%s", value);
97 return;
98 }
99 case TypeTableEntryIdPointer:
100 fprintf(irp->f, "&");
101 if (const_val->data.x_ptr.special == ConstPtrSpecialRuntime) {
102 fprintf(irp->f, "(runtime pointer value)");
103 } else if (const_val->data.x_ptr.special == ConstPtrSpecialCStr) {
104 fprintf(irp->f, "(c str lit)");
105 } else {
106 ir_print_const_value(irp, const_ptr_pointee(const_val));
107 }
108 return;
109 case TypeTableEntryIdFn:
110 {
111 FnTableEntry *fn_entry = const_val->data.x_fn;
112 fprintf(irp->f, "%s", buf_ptr(&fn_entry->symbol_name));
113 return;
114 }
115 case TypeTableEntryIdBlock:
116 {
117 AstNode *node = const_val->data.x_block->source_node;
118 fprintf(irp->f, "(scope:%zu:%zu)", node->line + 1, node->column + 1);
119 return;
120 }
121 case TypeTableEntryIdArray:
122 {
123 uint64_t len = canon_type->data.array.len;
124 fprintf(irp->f, "%s{", buf_ptr(&canon_type->name));
125 for (uint64_t i = 0; i < len; i += 1) {
126 if (i != 0)
127 fprintf(irp->f, ",");
128 ConstExprValue *child_value = &const_val->data.x_array.elements[i];
129 ir_print_const_value(irp, child_value);
130 }
131 fprintf(irp->f, "}");
132 return;
133 }
134 case TypeTableEntryIdNullLit:
135 {
136 fprintf(irp->f, "null");
137 return;
138 }
139 case TypeTableEntryIdUndefLit:
140 {
141 fprintf(irp->f, "undefined");
142 return;
143 }
144 case TypeTableEntryIdMaybe:
145 {
146 if (const_val->data.x_maybe) {
147 ir_print_const_value(irp, const_val->data.x_maybe);
148 } else {
149 fprintf(irp->f, "null");
150 }
151 return;
152 }
153 case TypeTableEntryIdNamespace:
154 {
155 ImportTableEntry *import = const_val->data.x_import;
156 if (import->c_import_node) {
157 fprintf(irp->f, "(namespace from C import)");
158 } else {
159 fprintf(irp->f, "(namespace: %s)", buf_ptr(import->path));
160 }
161 return;
162 }
163 case TypeTableEntryIdBoundFn:
164 {
165 FnTableEntry *fn_entry = const_val->data.x_bound_fn.fn;
166 fprintf(irp->f, "bound %s to ", buf_ptr(&fn_entry->symbol_name));
167 ir_print_other_instruction(irp, const_val->data.x_bound_fn.first_arg);
168 return;
169 }
170 case TypeTableEntryIdStruct:
171 {
172 fprintf(irp->f, "(struct %s constant)", buf_ptr(&canon_type->name));
173 return;
174 }
175 case TypeTableEntryIdEnum:
176 {
177 fprintf(irp->f, "(enum %s constant)", buf_ptr(&canon_type->name));
178 return;
179 }
180 case TypeTableEntryIdErrorUnion:
181 {
182 fprintf(irp->f, "(error union %s constant)", buf_ptr(&canon_type->name));
183 return;
184 }
185 case TypeTableEntryIdUnion:
186 {
187 fprintf(irp->f, "(union %s constant)", buf_ptr(&canon_type->name));
188 return;
189 }
190 case TypeTableEntryIdPureError:
191 {
192 fprintf(irp->f, "(pure error constant)");
193 return;
194 }
195 case TypeTableEntryIdEnumTag:
196 {
197 TypeTableEntry *enum_type = canon_type->data.enum_tag.enum_type;
198 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];
199 fprintf(irp->f, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));
200 return;
201 }
202 }
203 zig_unreachable();
204}39}
20540
206static void ir_print_var_instruction(IrPrint *irp, IrInstruction *instruction) {41static void ir_print_var_instruction(IrPrint *irp, IrInstruction *instruction) {