From d3ea334e7ab2897bbc948acc57aa9ba073304215 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 28 Feb 2018 11:20:11 +0000 Subject: `cstring` --- src/ir.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index a1b3ca8ee..6ab3c9ef7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -2890,6 +2890,14 @@ irValue *ir_string_len(irProcedure *proc, irValue *string) { return ir_emit_struct_ev(proc, string, 1); } +irValue *ir_cstring_len(irProcedure *proc, irValue *value) { + GB_ASSERT(is_type_cstring(ir_type(value))); + auto args = array_make(proc->module->allocator, 1); + args[0] = ir_emit_conv(proc, value, t_cstring); + return ir_emit_global_call(proc, "__cstring_len", args); +} + + void ir_fill_slice(irProcedure *proc, irValue *slice_ptr, irValue *data, irValue *len) { Type *t = ir_type(slice_ptr); @@ -3122,6 +3130,18 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) { return ir_emit(proc, ir_instr_conv(proc, irConv_zext, b, t_llvm_bool, t)); } + if (src == t_cstring && is_type_u8_ptr(dst)) { + return ir_emit_bitcast(proc, value, dst); + } + + if (src == t_cstring && dst == t_string) { + irValue *c = ir_emit_conv(proc, value, t_cstring); + auto args = array_make(proc->module->allocator, 1); + args[0] = c; + irValue *s = ir_emit_global_call(proc, "__cstring_to_string", args); + return ir_emit_conv(proc, s, dst); + } + // integer -> boolean if (is_type_integer(src) && is_type_boolean(dst)) { @@ -4171,7 +4191,9 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv v = ir_emit_load(proc, v); t = type_deref(t); } - if (is_type_string(t)) { + if (is_type_cstring(t)) { + return ir_cstring_len(proc, v); + } else if (is_type_string(t)) { return ir_string_len(proc, v); } else if (is_type_array(t)) { GB_PANIC("Array lengths are constant"); @@ -7902,6 +7924,11 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info tag = ir_emit_conv(proc, variant_ptr, t_type_info_string_ptr); break; + case Basic_cstring: + tag = ir_emit_conv(proc, variant_ptr, t_type_info_string_ptr); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), v_true); // is_cstring + break; + case Basic_any: tag = ir_emit_conv(proc, variant_ptr, t_type_info_any_ptr); break; -- cgit v1.2.3