| ... | ... | @@ -2960,25 +2960,18 @@ static void define_builtin_types(CodeGen *g) { |
| 2960 | 2960 | } |
| 2961 | 2961 | |
| 2962 | 2962 | |
| 2963 | | static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name) { |
| 2963 | static BuiltinFnEntry *create_builtin_fn(CodeGen *g, BuiltinFnId id, const char *name, size_t count) { |
| 2964 | 2964 | BuiltinFnEntry *builtin_fn = allocate<BuiltinFnEntry>(1); |
| 2965 | 2965 | buf_init_from_str(&builtin_fn->name, name); |
| 2966 | 2966 | builtin_fn->id = id; |
| 2967 | | g->builtin_fn_table.put(&builtin_fn->name, builtin_fn); |
| 2968 | | return builtin_fn; |
| 2969 | | } |
| 2970 | | |
| 2971 | | static BuiltinFnEntry *create_builtin_fn_with_arg_count(CodeGen *g, BuiltinFnId id, const char *name, size_t count) { |
| 2972 | | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, id, name); |
| 2973 | 2967 | builtin_fn->param_count = count; |
| 2974 | | builtin_fn->param_types = allocate<TypeTableEntry *>(count); |
| 2968 | g->builtin_fn_table.put(&builtin_fn->name, builtin_fn); |
| 2975 | 2969 | return builtin_fn; |
| 2976 | 2970 | } |
| 2977 | 2971 | |
| 2978 | 2972 | static void define_builtin_fns(CodeGen *g) { |
| 2979 | 2973 | { |
| 2980 | | BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdBreakpoint, "breakpoint", 0); |
| 2981 | | builtin_fn->return_type = g->builtin_types.entry_void; |
| 2974 | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdBreakpoint, "breakpoint", 0); |
| 2982 | 2975 | builtin_fn->ref_count = 1; |
| 2983 | 2976 | |
| 2984 | 2977 | LLVMTypeRef fn_type = LLVMFunctionType(LLVMVoidType(), nullptr, 0, false); |
| ... | ... | @@ -2988,33 +2981,27 @@ static void define_builtin_fns(CodeGen *g) { |
| 2988 | 2981 | g->trap_fn_val = builtin_fn->fn_val; |
| 2989 | 2982 | } |
| 2990 | 2983 | { |
| 2991 | | BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdReturnAddress, |
| 2984 | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdReturnAddress, |
| 2992 | 2985 | "returnAddress", 0); |
| 2993 | | builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 2986 | TypeTableEntry *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 2994 | 2987 | |
| 2995 | | LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref, |
| 2988 | LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref, |
| 2996 | 2989 | &g->builtin_types.entry_i32->type_ref, 1, false); |
| 2997 | 2990 | builtin_fn->fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type); |
| 2998 | 2991 | assert(LLVMGetIntrinsicID(builtin_fn->fn_val)); |
| 2999 | 2992 | } |
| 3000 | 2993 | { |
| 3001 | | BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdFrameAddress, |
| 2994 | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdFrameAddress, |
| 3002 | 2995 | "frameAddress", 0); |
| 3003 | | builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 2996 | TypeTableEntry *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 3004 | 2997 | |
| 3005 | | LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref, |
| 2998 | LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref, |
| 3006 | 2999 | &g->builtin_types.entry_i32->type_ref, 1, false); |
| 3007 | 3000 | builtin_fn->fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type); |
| 3008 | 3001 | assert(LLVMGetIntrinsicID(builtin_fn->fn_val)); |
| 3009 | 3002 | } |
| 3010 | 3003 | { |
| 3011 | | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdMemcpy, "memcpy"); |
| 3012 | | builtin_fn->return_type = g->builtin_types.entry_void; |
| 3013 | | builtin_fn->param_count = 3; |
| 3014 | | builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count); |
| 3015 | | builtin_fn->param_types[0] = nullptr; // manually checked later |
| 3016 | | builtin_fn->param_types[1] = nullptr; // manually checked later |
| 3017 | | builtin_fn->param_types[2] = g->builtin_types.entry_usize; |
| 3004 | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdMemcpy, "memcpy", 3); |
| 3018 | 3005 | builtin_fn->ref_count = 1; |
| 3019 | 3006 | |
| 3020 | 3007 | LLVMTypeRef param_types[] = { |
| ... | ... | @@ -3032,13 +3019,7 @@ static void define_builtin_fns(CodeGen *g) { |
| 3032 | 3019 | g->memcpy_fn_val = builtin_fn->fn_val; |
| 3033 | 3020 | } |
| 3034 | 3021 | { |
| 3035 | | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdMemset, "memset"); |
| 3036 | | builtin_fn->return_type = g->builtin_types.entry_void; |
| 3037 | | builtin_fn->param_count = 3; |
| 3038 | | builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count); |
| 3039 | | builtin_fn->param_types[0] = nullptr; // manually checked later |
| 3040 | | builtin_fn->param_types[1] = g->builtin_types.entry_u8; |
| 3041 | | builtin_fn->param_types[2] = g->builtin_types.entry_usize; |
| 3022 | BuiltinFnEntry *builtin_fn = create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3); |
| 3042 | 3023 | builtin_fn->ref_count = 1; |
| 3043 | 3024 | |
| 3044 | 3025 | LLVMTypeRef param_types[] = { |
| ... | ... | @@ -3055,38 +3036,38 @@ static void define_builtin_fns(CodeGen *g) { |
| 3055 | 3036 | |
| 3056 | 3037 | g->memset_fn_val = builtin_fn->fn_val; |
| 3057 | 3038 | } |
| 3058 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeOf", 1); |
| 3059 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdAlignof, "alignOf", 1); |
| 3060 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "maxValue", 1); |
| 3061 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "minValue", 1); |
| 3062 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "memberCount", 1); |
| 3063 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeOf", 1); |
| 3064 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4); |
| 3065 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4); |
| 3066 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdMulWithOverflow, "mulWithOverflow", 4); |
| 3067 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdShlWithOverflow, "shlWithOverflow", 4); |
| 3068 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCInclude, "cInclude", 1); |
| 3069 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "cDefine", 2); |
| 3070 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "cUndef", 1); |
| 3071 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compileVar", 1); |
| 3072 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdStaticEval, "staticEval", 1); |
| 3073 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCtz, "ctz", 1); |
| 3074 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdClz, "clz", 1); |
| 3075 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdImport, "import", 1); |
| 3076 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCImport, "cImport", 1); |
| 3077 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdErrName, "errorName", 1); |
| 3078 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdEmbedFile, "embedFile", 1); |
| 3079 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCmpExchange, "cmpxchg", 5); |
| 3080 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1); |
| 3081 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "divExact", 2); |
| 3082 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2); |
| 3083 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compileError", 1); |
| 3084 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "intType", 2); |
| 3085 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdUnreachable, "unreachable", 0); |
| 3086 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSetFnTest, "setFnTest", 1); |
| 3087 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2); |
| 3088 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSetFnNoInline, "setFnNoInline", 2); |
| 3089 | | create_builtin_fn_with_arg_count(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2); |
| 3039 | create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1); |
| 3040 | create_builtin_fn(g, BuiltinFnIdAlignof, "alignOf", 1); |
| 3041 | create_builtin_fn(g, BuiltinFnIdMaxValue, "maxValue", 1); |
| 3042 | create_builtin_fn(g, BuiltinFnIdMinValue, "minValue", 1); |
| 3043 | create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1); |
| 3044 | create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); |
| 3045 | create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4); |
| 3046 | create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4); |
| 3047 | create_builtin_fn(g, BuiltinFnIdMulWithOverflow, "mulWithOverflow", 4); |
| 3048 | create_builtin_fn(g, BuiltinFnIdShlWithOverflow, "shlWithOverflow", 4); |
| 3049 | create_builtin_fn(g, BuiltinFnIdCInclude, "cInclude", 1); |
| 3050 | create_builtin_fn(g, BuiltinFnIdCDefine, "cDefine", 2); |
| 3051 | create_builtin_fn(g, BuiltinFnIdCUndef, "cUndef", 1); |
| 3052 | create_builtin_fn(g, BuiltinFnIdCompileVar, "compileVar", 1); |
| 3053 | create_builtin_fn(g, BuiltinFnIdStaticEval, "staticEval", 1); |
| 3054 | create_builtin_fn(g, BuiltinFnIdCtz, "ctz", 1); |
| 3055 | create_builtin_fn(g, BuiltinFnIdClz, "clz", 1); |
| 3056 | create_builtin_fn(g, BuiltinFnIdImport, "import", 1); |
| 3057 | create_builtin_fn(g, BuiltinFnIdCImport, "cImport", 1); |
| 3058 | create_builtin_fn(g, BuiltinFnIdErrName, "errorName", 1); |
| 3059 | create_builtin_fn(g, BuiltinFnIdEmbedFile, "embedFile", 1); |
| 3060 | create_builtin_fn(g, BuiltinFnIdCmpExchange, "cmpxchg", 5); |
| 3061 | create_builtin_fn(g, BuiltinFnIdFence, "fence", 1); |
| 3062 | create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2); |
| 3063 | create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2); |
| 3064 | create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1); |
| 3065 | create_builtin_fn(g, BuiltinFnIdIntType, "intType", 2); |
| 3066 | create_builtin_fn(g, BuiltinFnIdUnreachable, "unreachable", 0); |
| 3067 | create_builtin_fn(g, BuiltinFnIdSetFnTest, "setFnTest", 1); |
| 3068 | create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2); |
| 3069 | create_builtin_fn(g, BuiltinFnIdSetFnNoInline, "setFnNoInline", 2); |
| 3070 | create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2); |
| 3090 | 3071 | } |
| 3091 | 3072 | |
| 3092 | 3073 | static void init(CodeGen *g, Buf *source_path) { |