authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-15 01:05:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-15 01:05:24-07:00
log6ff996f60fa4e26e8dc5a8556986b5caa3eb1021
treed38579da50304852f292f7ccb555bef87562ba43
parent50310cf9df75217cba0edf2420a8af8d560d4b3e

add int_type builtin function


5 files changed, 75 insertions(+), 0 deletions(-)

src/all_types.hpp+1
......@@ -1139,6 +1139,7 @@ enum BuiltinFnId {
11391139 BuiltinFnIdFence,
11401140 BuiltinFnIdDivExact,
11411141 BuiltinFnIdTruncate,
1142 BuiltinFnIdIntType,
11421143};
11431144
11441145struct BuiltinFnEntry {
src/analyze.cpp+48
......@@ -4790,6 +4790,52 @@ static TypeTableEntry *analyze_compile_err(CodeGen *g, ImportTableEntry *import,
47904790 return g->builtin_types.entry_invalid;
47914791}
47924792
4793static TypeTableEntry *analyze_int_type(CodeGen *g, ImportTableEntry *import,
4794 BlockContext *context, AstNode *node)
4795{
4796 AstNode **is_signed_node = &node->data.fn_call_expr.params.at(0);
4797 AstNode **bit_count_node = &node->data.fn_call_expr.params.at(1);
4798 AstNode **is_wrap_node = &node->data.fn_call_expr.params.at(2);
4799
4800 TypeTableEntry *bool_type = g->builtin_types.entry_bool;
4801 TypeTableEntry *usize_type = g->builtin_types.entry_usize;
4802 TypeTableEntry *is_signed_type = analyze_expression(g, import, context, bool_type, *is_signed_node);
4803 TypeTableEntry *bit_count_type = analyze_expression(g, import, context, usize_type, *bit_count_node);
4804 TypeTableEntry *is_wrap_type = analyze_expression(g, import, context, bool_type, *is_wrap_node);
4805
4806 if (is_signed_type->id == TypeTableEntryIdInvalid ||
4807 bit_count_type->id == TypeTableEntryIdInvalid ||
4808 is_wrap_type->id == TypeTableEntryIdInvalid)
4809 {
4810 return g->builtin_types.entry_invalid;
4811 }
4812
4813 ConstExprValue *is_signed_val = &get_resolved_expr(*is_signed_node)->const_val;
4814 ConstExprValue *bit_count_val = &get_resolved_expr(*bit_count_node)->const_val;
4815 ConstExprValue *is_wrap_val = &get_resolved_expr(*is_wrap_node)->const_val;
4816
4817 AstNode *bad_node = nullptr;
4818 if (!is_signed_val->ok) {
4819 bad_node = *is_signed_node;
4820 } else if (!bit_count_val->ok) {
4821 bad_node = *bit_count_node;
4822 } else if (!is_wrap_val->ok) {
4823 bad_node = *is_wrap_node;
4824 }
4825 if (bad_node) {
4826 add_node_error(g, bad_node, buf_sprintf("unable to evaluate constant expression"));
4827 return g->builtin_types.entry_invalid;
4828 }
4829
4830 bool depends_on_compile_var = is_signed_val->depends_on_compile_var ||
4831 bit_count_val->depends_on_compile_var || is_wrap_val->depends_on_compile_var;
4832
4833 TypeTableEntry *int_type = get_int_type(g, is_signed_val->data.x_bool, is_wrap_val->data.x_bool,
4834 bit_count_val->data.x_bignum.data.x_uint);
4835 return resolve_expr_const_val_as_type(g, node, int_type, depends_on_compile_var);
4836
4837}
4838
47934839static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
47944840 TypeTableEntry *expected_type, AstNode *node)
47954841{
......@@ -5136,6 +5182,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
51365182 return analyze_truncate(g, import, context, node);
51375183 case BuiltinFnIdCompileErr:
51385184 return analyze_compile_err(g, import, context, node);
5185 case BuiltinFnIdIntType:
5186 return analyze_int_type(g, import, context, node);
51395187 }
51405188 zig_unreachable();
51415189}
src/codegen.cpp+2
......@@ -540,6 +540,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) {
540540 case BuiltinFnIdImport:
541541 case BuiltinFnIdCImport:
542542 case BuiltinFnIdCompileErr:
543 case BuiltinFnIdIntType:
543544 zig_unreachable();
544545 case BuiltinFnIdCtz:
545546 case BuiltinFnIdClz:
......@@ -4657,6 +4658,7 @@ static void define_builtin_fns(CodeGen *g) {
46574658 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2);
46584659 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);
46594660 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1);
4661 create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "int_type", 3);
46604662}
46614663
46624664static void init(CodeGen *g, Buf *source_path) {
src/eval.cpp+1
......@@ -834,6 +834,7 @@ static bool eval_fn_call_builtin(EvalFn *ef, AstNode *node, ConstExprValue *out_
834834 case BuiltinFnIdFrameAddress:
835835 case BuiltinFnIdReturnAddress:
836836 case BuiltinFnIdCompileErr:
837 case BuiltinFnIdIntType:
837838 zig_unreachable();
838839 }
839840
test/self_hosted.zig+23
......@@ -1689,3 +1689,26 @@ struct DivResult {
16891689 quotient: u64,
16901690 remainder: u64,
16911691}
1692
1693#attribute("test")
1694fn int_type_builtin() {
1695 assert(@int_type(true, 8, false) == i8);
1696 assert(@int_type(true, 16, false) == i16);
1697 assert(@int_type(true, 32, false) == i32);
1698 assert(@int_type(true, 64, false) == i64);
1699
1700 assert(@int_type(false, 8, false) == u8);
1701 assert(@int_type(false, 16, false) == u16);
1702 assert(@int_type(false, 32, false) == u32);
1703 assert(@int_type(false, 64, false) == u64);
1704
1705 assert(@int_type(true, 8, true) == i8w);
1706 assert(@int_type(true, 16, true) == i16w);
1707 assert(@int_type(true, 32, true) == i32w);
1708 assert(@int_type(true, 64, true) == i64w);
1709
1710 assert(@int_type(false, 8, true) == u8w);
1711 assert(@int_type(false, 16, true) == u16w);
1712 assert(@int_type(false, 32, true) == u32w);
1713 assert(@int_type(false, 64, true) == u64w);
1714}