| ... | @@ -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 | } |
| 369 | | 369 | |
| | 370 | static 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 | |
| | 390 | static 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 | |
| 370 | static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id, | 419 | static 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 | } |
| 400 | | 449 | |
| 401 | static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, | 450 | static 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 | } |
| 1591 | | 1638 | |
| | 1639 | static 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 | |
| 1592 | static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) { | 1649 | static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) { |
| 1593 | assert(node->type == NodeTypeArrayType); | 1650 | assert(node->type == NodeTypeArrayType); |
| 1594 | | 1651 | |
| ... | @@ -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; |
| 3333 | | 3391 | |
| | 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); |
| 3341 | | 3402 | |
| ... | @@ -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 | //{ |