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 {
15931593
15941594 IrInstruction *container_ptr;
15951595 Buf *field_name;
1596 bool is_const;
15961597};
15971598
15981599struct IrInstructionStructFieldPtr {
......@@ -1600,6 +1601,7 @@ struct IrInstructionStructFieldPtr {
16001601
16011602 IrInstruction *struct_ptr;
16021603 TypeStructField *field;
1604 bool is_const;
16031605};
16041606
16051607struct IrInstructionReadField {
......@@ -1614,12 +1616,14 @@ struct IrInstructionElemPtr {
16141616
16151617 IrInstruction *array_ptr;
16161618 IrInstruction *elem_index;
1619 bool is_const;
16171620};
16181621
16191622struct IrInstructionVarPtr {
16201623 IrInstruction base;
16211624
16221625 VariableTableEntry *var;
1626 bool is_const;
16231627};
16241628
16251629struct IrInstructionCall {
......@@ -1716,6 +1720,7 @@ enum LValPurpose {
17161720 LValPurposeNone,
17171721 LValPurposeAssign,
17181722 LValPurposeAddressOf,
1723 LValPurposeConstAddressOf,
17191724};
17201725
17211726#endif
src/ir.cpp+66-15
......@@ -367,6 +367,55 @@ static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node,
367367 return &const_instruction->base;
368368}
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
370419static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
371420 IrInstruction *op1, IrInstruction *op2)
372421{
......@@ -398,9 +447,7 @@ static IrInstruction *ir_build_var_ptr(IrBuilder *irb, AstNode *source_node, Var
398447 return &instruction->base;
399448}
400449
401static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
402 VariableTableEntry *var)
403{
450static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, VariableTableEntry *var) {
404451 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->source_node, var);
405452 ir_link_new_instruction(new_instruction, old_instruction);
406453 return new_instruction;
......@@ -1589,6 +1636,16 @@ static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {
15891636 return ir_build_const_bool(irb, node, node->data.bool_literal.value);
15901637}
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
15921649static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) {
15931650 assert(node->type == NodeTypeArrayType);
15941651
......@@ -1662,6 +1719,8 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
16621719 return ir_gen_bool_literal(irb, node);
16631720 case NodeTypeArrayType:
16641721 return ir_gen_array_type(irb, node);
1722 case NodeTypeStringLiteral:
1723 return ir_gen_string_literal(irb, node);
16651724 case NodeTypeUnwrapErrorExpr:
16661725 case NodeTypeDefer:
16671726 case NodeTypeSliceExpr:
......@@ -1672,7 +1731,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
16721731 case NodeTypeContinue:
16731732 case NodeTypeLabel:
16741733 case NodeTypeSwitchExpr:
1675 case NodeTypeStringLiteral:
16761734 case NodeTypeCharLiteral:
16771735 case NodeTypeNullLiteral:
16781736 case NodeTypeUndefinedLiteral:
......@@ -3331,11 +3389,14 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
33313389 if (var->type->id == TypeTableEntryIdInvalid)
33323390 return var->type;
33333391
3392 zig_panic("TODO if var is a global, this code is wrong");
3393
33343394 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.
33363396 if (var->mem_slot_index != SIZE_MAX) {
33373397 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
33383398 if (mem_slot->ok) {
3399 zig_panic("TODO do we really want to set up this fake pointer to do constant evaluation?");
33393400 ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,
33403401 mem_slot->depends_on_compile_var);
33413402
......@@ -6995,16 +7056,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {
69957056// return g->builtin_types.entry_void;
69967057//}
69977058//
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//
70087059//static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,
70097060// TypeTableEntry *expected_type, AstNode *node)
70107061//{
src/ir_print.cpp+14-1
......@@ -84,9 +84,22 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
8484 fprintf(irp->f, "(scope:%zu:%zu)", node->line + 1, node->column + 1);
8585 break;
8686 }
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 }
87101 case TypeTableEntryIdVar:
88102 case TypeTableEntryIdFloat:
89 case TypeTableEntryIdArray:
90103 case TypeTableEntryIdStruct:
91104 case TypeTableEntryIdUndefLit:
92105 case TypeTableEntryIdNullLit: