authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-10 00:41:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-10 00:41:17-05:00
loga5c9da0de2d0e2bace3127ba47bd43f5333724c4
treef650389e6c59f85261d7b55f1a450c0ec1b9a10a
parent9d19b8d66e5eb26ec739ce74b0635270cebf6343

IR: gen string literal


3 files changed, 85 insertions(+), 16 deletions(-)

src/all_types.hpp+5
...@@ -1593,6 +1593,7 @@ struct IrInstructionFieldPtr {...@@ -1593,6 +1593,7 @@ struct IrInstructionFieldPtr {
15931593
1594 IrInstruction *container_ptr;1594 IrInstruction *container_ptr;
1595 Buf *field_name;1595 Buf *field_name;
1596 bool is_const;
1596};1597};
15971598
1598struct IrInstructionStructFieldPtr {1599struct IrInstructionStructFieldPtr {
...@@ -1600,6 +1601,7 @@ struct IrInstructionStructFieldPtr {...@@ -1600,6 +1601,7 @@ struct IrInstructionStructFieldPtr {
16001601
1601 IrInstruction *struct_ptr;1602 IrInstruction *struct_ptr;
1602 TypeStructField *field;1603 TypeStructField *field;
1604 bool is_const;
1603};1605};
16041606
1605struct IrInstructionReadField {1607struct IrInstructionReadField {
...@@ -1614,12 +1616,14 @@ struct IrInstructionElemPtr {...@@ -1614,12 +1616,14 @@ struct IrInstructionElemPtr {
16141616
1615 IrInstruction *array_ptr;1617 IrInstruction *array_ptr;
1616 IrInstruction *elem_index;1618 IrInstruction *elem_index;
1619 bool is_const;
1617};1620};
16181621
1619struct IrInstructionVarPtr {1622struct IrInstructionVarPtr {
1620 IrInstruction base;1623 IrInstruction base;
16211624
1622 VariableTableEntry *var;1625 VariableTableEntry *var;
1626 bool is_const;
1623};1627};
16241628
1625struct IrInstructionCall {1629struct IrInstructionCall {
...@@ -1716,6 +1720,7 @@ enum LValPurpose {...@@ -1716,6 +1720,7 @@ enum LValPurpose {
1716 LValPurposeNone,1720 LValPurposeNone,
1717 LValPurposeAssign,1721 LValPurposeAssign,
1718 LValPurposeAddressOf,1722 LValPurposeAddressOf,
1723 LValPurposeConstAddressOf,
1719};1724};
17201725
1721#endif1726#endif
src/ir.cpp+66-15
...@@ -367,6 +367,55 @@ static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node,...@@ -367,6 +367,55 @@ static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node,
367 return &const_instruction->base;367 return &const_instruction->base;
368}368}
369369
370static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, AstNode *source_node, Buf *str) {
371 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
372 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
373 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
374 const_instruction->base.type_entry = type_entry;
375 ConstExprValue *const_val = &const_instruction->base.static_value;
376 const_val->ok = true;
377 const_val->data.x_array.fields = allocate<ConstExprValue*>(buf_len(str));
378
379 ConstExprValue *all_chars = allocate<ConstExprValue>(buf_len(str));
380 for (size_t i = 0; i < buf_len(str); i += 1) {
381 ConstExprValue *this_char = &all_chars[i];
382 this_char->ok = true;
383 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
384 const_val->data.x_array.fields[i] = this_char;
385 }
386
387 return &const_instruction->base;
388}
389
390static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, AstNode *source_node, Buf *str) {
391 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
392 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
393 TypeTableEntry *type_entry = get_pointer_to_type(irb->codegen, u8_type, true);
394 const_instruction->base.type_entry = type_entry;
395 ConstExprValue *const_val = &const_instruction->base.static_value;
396 const_val->ok = true;
397
398 size_t len_with_null = buf_len(str) + 1;
399 const_val->data.x_ptr.ptr = allocate<ConstExprValue*>(len_with_null);
400 const_val->data.x_ptr.len = len_with_null;
401 const_val->data.x_ptr.is_c_str = true;
402
403 ConstExprValue *all_chars = allocate<ConstExprValue>(len_with_null);
404 for (size_t i = 0; i < buf_len(str); i += 1) {
405 ConstExprValue *this_char = &all_chars[i];
406 this_char->ok = true;
407 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
408 const_val->data.x_ptr.ptr[i] = this_char;
409 }
410
411 ConstExprValue *null_char = &all_chars[len_with_null - 1];
412 null_char->ok = true;
413 bignum_init_unsigned(&null_char->data.x_bignum, 0);
414 const_val->data.x_ptr.ptr[len_with_null - 1] = null_char;
415
416 return &const_instruction->base;
417}
418
370static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,419static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
371 IrInstruction *op1, IrInstruction *op2)420 IrInstruction *op1, IrInstruction *op2)
372{421{
...@@ -398,9 +447,7 @@ static IrInstruction *ir_build_var_ptr(IrBuilder *irb, AstNode *source_node, Var...@@ -398,9 +447,7 @@ static IrInstruction *ir_build_var_ptr(IrBuilder *irb, AstNode *source_node, Var
398 return &instruction->base;447 return &instruction->base;
399}448}
400449
401static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,450static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, VariableTableEntry *var) {
402 VariableTableEntry *var)
403{
404 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->source_node, var);451 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->source_node, var);
405 ir_link_new_instruction(new_instruction, old_instruction);452 ir_link_new_instruction(new_instruction, old_instruction);
406 return new_instruction;453 return new_instruction;
...@@ -1589,6 +1636,16 @@ static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {...@@ -1589,6 +1636,16 @@ static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {
1589 return ir_build_const_bool(irb, node, node->data.bool_literal.value);1636 return ir_build_const_bool(irb, node, node->data.bool_literal.value);
1590}1637}
15911638
1639static IrInstruction *ir_gen_string_literal(IrBuilder *irb, AstNode *node) {
1640 assert(node->type == NodeTypeStringLiteral);
1641
1642 if (node->data.string_literal.c) {
1643 return ir_build_const_c_str_lit(irb, node, node->data.string_literal.buf);
1644 } else {
1645 return ir_build_const_str_lit(irb, node, node->data.string_literal.buf);
1646 }
1647}
1648
1592static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) {1649static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) {
1593 assert(node->type == NodeTypeArrayType);1650 assert(node->type == NodeTypeArrayType);
15941651
...@@ -1662,6 +1719,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1662,6 +1719,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1662 return ir_gen_bool_literal(irb, node);1719 return ir_gen_bool_literal(irb, node);
1663 case NodeTypeArrayType:1720 case NodeTypeArrayType:
1664 return ir_gen_array_type(irb, node);1721 return ir_gen_array_type(irb, node);
1722 case NodeTypeStringLiteral:
1723 return ir_gen_string_literal(irb, node);
1665 case NodeTypeUnwrapErrorExpr:1724 case NodeTypeUnwrapErrorExpr:
1666 case NodeTypeDefer:1725 case NodeTypeDefer:
1667 case NodeTypeSliceExpr:1726 case NodeTypeSliceExpr:
...@@ -1672,7 +1731,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1672,7 +1731,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1672 case NodeTypeContinue:1731 case NodeTypeContinue:
1673 case NodeTypeLabel:1732 case NodeTypeLabel:
1674 case NodeTypeSwitchExpr:1733 case NodeTypeSwitchExpr:
1675 case NodeTypeStringLiteral:
1676 case NodeTypeCharLiteral:1734 case NodeTypeCharLiteral:
1677 case NodeTypeNullLiteral:1735 case NodeTypeNullLiteral:
1678 case NodeTypeUndefinedLiteral:1736 case NodeTypeUndefinedLiteral:
...@@ -3331,11 +3389,14 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct...@@ -3331,11 +3389,14 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
3331 if (var->type->id == TypeTableEntryIdInvalid)3389 if (var->type->id == TypeTableEntryIdInvalid)
3332 return var->type;3390 return var->type;
33333391
3392 zig_panic("TODO if var is a global, this code is wrong");
3393
3334 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, var->type, false);3394 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, var->type, false);
3335 // TODO once the anlayze code is fully ported over to IR we won't need this SIZE_MAX thing.3395 // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
3336 if (var->mem_slot_index != SIZE_MAX) {3396 if (var->mem_slot_index != SIZE_MAX) {
3337 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];3397 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
3338 if (mem_slot->ok) {3398 if (mem_slot->ok) {
3399 zig_panic("TODO do we really want to set up this fake pointer to do constant evaluation?");
3339 ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,3400 ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,
3340 mem_slot->depends_on_compile_var);3401 mem_slot->depends_on_compile_var);
33413402
...@@ -6995,16 +7056,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {...@@ -6995,16 +7056,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {
6995// return g->builtin_types.entry_void;7056// return g->builtin_types.entry_void;
6996//}7057//}
6997//7058//
6998//static TypeTableEntry *analyze_string_literal_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
6999// TypeTableEntry *expected_type, AstNode *node)
7000//{
7001// if (node->data.string_literal.c) {
7002// return resolve_expr_const_val_as_c_string_lit(g, node, node->data.string_literal.buf);
7003// } else {
7004// return resolve_expr_const_val_as_string_lit(g, node, node->data.string_literal.buf);
7005// }
7006//}
7007//
7008//static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,7059//static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,
7009// TypeTableEntry *expected_type, AstNode *node)7060// TypeTableEntry *expected_type, AstNode *node)
7010//{7061//{
src/ir_print.cpp+14-1
...@@ -84,9 +84,22 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const...@@ -84,9 +84,22 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
84 fprintf(irp->f, "(scope:%zu:%zu)", node->line + 1, node->column + 1);84 fprintf(irp->f, "(scope:%zu:%zu)", node->line + 1, node->column + 1);
85 break;85 break;
86 }86 }
87 case TypeTableEntryIdArray:
88 {
89 uint64_t len = type_entry->data.array.len;
90 fprintf(irp->f, "%s{", buf_ptr(&type_entry->name));
91 for (uint64_t i = 0; i < len; i += 1) {
92 if (i != 0)
93 fprintf(irp->f, ",");
94 ConstExprValue *child_value = const_val->data.x_array.fields[i];
95 TypeTableEntry *child_type = type_entry->data.array.child_type;
96 ir_print_const_value(irp, child_type, child_value);
97 }
98 fprintf(irp->f, "}");
99 break;
100 }
87 case TypeTableEntryIdVar:101 case TypeTableEntryIdVar:
88 case TypeTableEntryIdFloat:102 case TypeTableEntryIdFloat:
89 case TypeTableEntryIdArray:
90 case TypeTableEntryIdStruct:103 case TypeTableEntryIdStruct:
91 case TypeTableEntryIdUndefLit:104 case TypeTableEntryIdUndefLit:
92 case TypeTableEntryIdNullLit:105 case TypeTableEntryIdNullLit: