authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-15 01:48:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-15 01:48:36-05:00
log8d1c6138f950d7ce44d823ce870643b94b19ffb1
treece1542939f07d6d22acd4492dc91323dca5892de
parentaf4d4c882a588f82667ddfaf41a59ff3cbcc5832

IR: implement pointer dereferencing (even at compile time)


1 files changed, 55 insertions(+), 27 deletions(-)

src/ir.cpp+55-27
......@@ -1377,18 +1377,22 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
13771377 return ir_build_phi(irb, node, 2, incoming_blocks, incoming_values);
13781378}
13791379
1380static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, AstNode *node, IrUnOp op_id) {
1380static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, AstNode *node, IrUnOp op_id, LValPurpose lval) {
13811381 assert(node->type == NodeTypePrefixOpExpr);
13821382 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
13831383
1384 IrInstruction *value = ir_gen_node(irb, expr_node, node->block_context);
1384 IrInstruction *value = ir_gen_node_extra(irb, expr_node, node->block_context, lval);
13851385 if (value == irb->codegen->invalid_instruction)
13861386 return value;
13871387
13881388 return ir_build_un_op(irb, node, op_id, value);
13891389}
13901390
1391static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node) {
1391static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, AstNode *node, IrUnOp op_id) {
1392 return ir_gen_prefix_op_id_lval(irb, node, op_id, LValPurposeNone);
1393}
1394
1395static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node, LValPurpose lval) {
13921396 assert(node->type == NodeTypePrefixOpExpr);
13931397
13941398 PrefixOp prefix_op = node->data.prefix_op_expr.prefix_op;
......@@ -1405,11 +1409,11 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node) {
14051409 case PrefixOpNegationWrap:
14061410 return ir_gen_prefix_op_id(irb, node, IrUnOpNegationWrap);
14071411 case PrefixOpAddressOf:
1408 return ir_gen_prefix_op_id(irb, node, IrUnOpAddressOf);
1412 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpAddressOf, LValPurposeAddressOf);
14091413 case PrefixOpConstAddressOf:
1410 return ir_gen_prefix_op_id(irb, node, IrUnOpConstAddressOf);
1414 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpConstAddressOf, LValPurposeConstAddressOf);
14111415 case PrefixOpDereference:
1412 return ir_gen_prefix_op_id(irb, node, IrUnOpDereference);
1416 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpDereference, lval);
14131417 case PrefixOpMaybe:
14141418 return ir_gen_prefix_op_id(irb, node, IrUnOpMaybe);
14151419 case PrefixOpError:
......@@ -1789,7 +1793,7 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
17891793 case NodeTypeIfBoolExpr:
17901794 return ir_gen_if_bool_expr(irb, node);
17911795 case NodeTypePrefixOpExpr:
1792 return ir_gen_prefix_op_expr(irb, node);
1796 return ir_gen_prefix_op_expr(irb, node, lval);
17931797 case NodeTypeContainerInitExpr:
17941798 return ir_gen_container_init_expr(irb, node);
17951799 case NodeTypeVariableDeclaration:
......@@ -3168,24 +3172,18 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
31683172 zig_unreachable();
31693173}
31703174
3171static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
3175static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction,
3176 bool is_const)
3177{
31723178 IrInstruction *value = un_op_instruction->value->other;
31733179 if (value->type_entry->id == TypeTableEntryIdInvalid)
31743180 return ira->codegen->builtin_types.entry_invalid;
31753181
3176 bool is_const;
3177 if (un_op_instruction->op_id == IrUnOpAddressOf) {
3178 is_const = false;
3179 } else if (un_op_instruction->op_id == IrUnOpConstAddressOf) {
3180 is_const = true;
3181 } else {
3182 zig_unreachable();
3183 }
3184
31853182 TypeTableEntry *target_type = value->type_entry;
31863183 TypeTableEntry *canon_target_type = get_underlying_type(target_type);
31873184 switch (canon_target_type->id) {
31883185 case TypeTableEntryIdTypeDecl:
3186 // impossible because we look at the canonicalized type
31893187 zig_unreachable();
31903188 case TypeTableEntryIdInvalid:
31913189 return ira->codegen->builtin_types.entry_invalid;
......@@ -3197,6 +3195,7 @@ static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstruction
31973195 case TypeTableEntryIdBlock:
31983196 case TypeTableEntryIdUnreachable:
31993197 case TypeTableEntryIdVar:
3198 case TypeTableEntryIdGenericFn:
32003199 add_node_error(ira->codegen, un_op_instruction->base.source_node,
32013200 buf_sprintf("unable to get address of type '%s'", buf_ptr(&target_type->name)));
32023201 // TODO if type decl, add note pointing to type decl declaration
......@@ -3210,11 +3209,17 @@ static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstruction
32103209 out_val->data.x_type = get_pointer_to_type(ira->codegen, child_type, is_const);
32113210 return ira->codegen->builtin_types.entry_type;
32123211 }
3212 case TypeTableEntryIdPointer:
3213 {
3214 // this instruction is a noop - we solved this in IR gen by passing
3215 // LValPurposeAddressOf which caused the loadptr to not do the load.
3216 ir_link_new_instruction(value, &un_op_instruction->base);
3217 return ir_finish_anal(ira, target_type);
3218 }
32133219 case TypeTableEntryIdVoid:
32143220 case TypeTableEntryIdBool:
32153221 case TypeTableEntryIdInt:
32163222 case TypeTableEntryIdFloat:
3217 case TypeTableEntryIdPointer:
32183223 case TypeTableEntryIdArray:
32193224 case TypeTableEntryIdStruct:
32203225 case TypeTableEntryIdMaybe:
......@@ -3223,15 +3228,40 @@ static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstruction
32233228 case TypeTableEntryIdEnum:
32243229 case TypeTableEntryIdUnion:
32253230 case TypeTableEntryIdFn:
3226 case TypeTableEntryIdGenericFn:
3227 {
3228 zig_panic("TODO address of operation");
3229 break;
3230 }
3231 zig_unreachable();
32313232 }
32323233 zig_unreachable();
32333234}
32343235
3236static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
3237 IrInstruction *value = un_op_instruction->value->other;
3238
3239 TypeTableEntry *ptr_type = value->type_entry;
3240 TypeTableEntry *child_type;
3241 if (ptr_type->id == TypeTableEntryIdInvalid) {
3242 return ira->codegen->builtin_types.entry_invalid;
3243 } else if (ptr_type->id == TypeTableEntryIdPointer) {
3244 child_type = ptr_type->data.pointer.child_type;
3245 } else {
3246 add_node_error(ira->codegen, un_op_instruction->base.source_node,
3247 buf_sprintf("attempt to dereference non-pointer type '%s'",
3248 buf_ptr(&ptr_type->name)));
3249 return ira->codegen->builtin_types.entry_invalid;
3250 }
3251
3252 // this dereference is always an rvalue because in the IR gen we identify lvalue and emit
3253 // one of the ptr instructions
3254
3255 if (value->static_value.ok) {
3256 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base, false);
3257 *out_val = *value->static_value.data.x_ptr.ptr[0];
3258 return child_type;
3259 }
3260
3261 ir_build_un_op_from(&ira->new_irb, &un_op_instruction->base, IrUnOpDereference, value);
3262 return child_type;
3263}
3264
32353265static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
32363266 IrUnOp op_id = un_op_instruction->op_id;
32373267 switch (op_id) {
......@@ -3304,9 +3334,9 @@ static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructio
33043334 //}
33053335 case IrUnOpAddressOf:
33063336 case IrUnOpConstAddressOf:
3307 return ir_analyze_unary_address_of(ira, un_op_instruction);
3337 return ir_analyze_unary_address_of(ira, un_op_instruction, op_id == IrUnOpConstAddressOf);
33083338 case IrUnOpDereference:
3309 zig_panic("TODO remove this IrUnOp item");
3339 return ir_analyze_dereference(ira, un_op_instruction);
33103340 case IrUnOpMaybe:
33113341 zig_panic("TODO analyze PrefixOpMaybe");
33123342 //{
......@@ -3502,7 +3532,6 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
35023532 mem_slot->depends_on_compile_var);
35033533
35043534 out_val->data.x_ptr.len = 1;
3505 out_val->data.x_ptr.is_c_str = false;
35063535 out_val->data.x_ptr.ptr = allocate<ConstExprValue *>(1);
35073536 out_val->data.x_ptr.ptr[0] = mem_slot;
35083537 return ptr_type;
......@@ -3565,7 +3594,6 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc
35653594 casted_elem_index->static_value.depends_on_compile_var;
35663595 ConstExprValue *out_val = ir_build_const_from(ira, &elem_ptr_instruction->base, depends_on_compile_var);
35673596 out_val->data.x_ptr.len = 1;
3568 out_val->data.x_ptr.is_c_str = false;
35693597 out_val->data.x_ptr.ptr = allocate<ConstExprValue *>(1);
35703598 if (array_type->id == TypeTableEntryIdPointer) {
35713599 uint64_t pointer_len = array_ptr->static_value.data.x_ptr.len;