From 576914aee1565618d8448a2bbc3cbef0c4acc4d1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 5 Aug 2022 11:57:33 +0100 Subject: Make `unreachable()` a built-in compiler-level procedure --- src/check_builtin.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 8108604ba..8f8f7f9e2 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -3569,6 +3569,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 operand->mode = Addressing_NoValue; break; + case BuiltinProc_unreachable: case BuiltinProc_trap: case BuiltinProc_debug_trap: operand->mode = Addressing_NoValue; -- cgit v1.2.3 From 0997df4fcf735830ee6a03867712ce89aa4255e1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 12:39:05 +0100 Subject: Move builtin directives to a separate procedure --- src/check_builtin.cpp | 907 +++++++++++++++++++++++++------------------------- 1 file changed, 455 insertions(+), 452 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 8f8f7f9e2..7a72c574f 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1075,6 +1075,459 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call } +bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) { + ast_node(ce, CallExpr, call); + ast_node(bd, BasicDirective, ce->proc); + String name = bd->name.string; + if (name == "location") { + if (ce->args.count > 1) { + error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count); + } + if (ce->args.count > 0) { + Ast *arg = ce->args[0]; + Entity *e = nullptr; + Operand o = {}; + if (arg->kind == Ast_Ident) { + e = check_ident(c, &o, arg, nullptr, nullptr, true); + } else if (arg->kind == Ast_SelectorExpr) { + e = check_selector(c, &o, arg, nullptr); + } + if (e == nullptr) { + error(ce->args[0], "'#location' expected a valid entity name"); + } + } + + operand->type = t_source_code_location; + operand->mode = Addressing_Value; + } else if (name == "load") { + if (ce->args.count != 1) { + if (ce->args.count == 0) { + error(ce->close, "'#load' expects 1 argument, got 0"); + } else { + error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); + } + + return false; + } + + Ast *arg = ce->args[0]; + Operand o = {}; + check_expr(c, &o, arg); + if (o.mode != Addressing_Constant) { + error(arg, "'#load' expected a constant string argument"); + return false; + } + + if (!is_type_string(o.type)) { + gbString str = type_to_string(o.type); + error(arg, "'#load' expected a constant string, got %s", str); + gb_string_free(str); + return false; + } + + gbAllocator a = heap_allocator(); + + GB_ASSERT(o.value.kind == ExactValue_String); + String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); + String original_string = o.value.value_string; + + + BlockingMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + gb_unused(ok); + + char *c_str = alloc_cstring(a, path); + defer (gb_free(a, c_str)); + + + gbFile f = {}; + gbFileError file_err = gb_file_open(&f, c_str); + defer (gb_file_close(&f)); + + switch (file_err) { + default: + case gbFileError_Invalid: + error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); + return false; + case gbFileError_NotExists: + error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); + return false; + case gbFileError_Permission: + error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); + return false; + case gbFileError_None: + // Okay + break; + } + + String result = {}; + isize file_size = cast(isize)gb_file_size(&f); + if (file_size > 0) { + u8 *data = cast(u8 *)gb_alloc(a, file_size+1); + gb_file_read_at(&f, data, file_size, 0); + data[file_size] = '\0'; + result.text = data; + result.len = file_size; + } + + operand->type = t_u8_slice; + operand->mode = Addressing_Constant; + operand->value = exact_value_string(result); + + } else if (name == "load_hash") { + if (ce->args.count != 2) { + if (ce->args.count == 0) { + error(ce->close, "'#load_hash' expects 2 argument, got 0"); + } else { + error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count); + } + return false; + } + + Ast *arg0 = ce->args[0]; + Ast *arg1 = ce->args[1]; + Operand o = {}; + check_expr(c, &o, arg0); + if (o.mode != Addressing_Constant) { + error(arg0, "'#load_hash' expected a constant string argument"); + return false; + } + + if (!is_type_string(o.type)) { + gbString str = type_to_string(o.type); + error(arg0, "'#load_hash' expected a constant string, got %s", str); + gb_string_free(str); + return false; + } + + Operand o_hash = {}; + check_expr(c, &o_hash, arg1); + if (o_hash.mode != Addressing_Constant) { + error(arg1, "'#load_hash' expected a constant string argument"); + return false; + } + + if (!is_type_string(o_hash.type)) { + gbString str = type_to_string(o.type); + error(arg1, "'#load_hash' expected a constant string, got %s", str); + gb_string_free(str); + return false; + } + + + gbAllocator a = heap_allocator(); + + GB_ASSERT(o.value.kind == ExactValue_String); + GB_ASSERT(o_hash.value.kind == ExactValue_String); + + String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); + String original_string = o.value.value_string; + String hash_kind = o_hash.value.value_string; + + String supported_hashes[] = { + str_lit("adler32"), + str_lit("crc32"), + str_lit("crc64"), + str_lit("fnv32"), + str_lit("fnv64"), + str_lit("fnv32a"), + str_lit("fnv64a"), + str_lit("murmur32"), + str_lit("murmur64"), + }; + + bool hash_found = false; + for (isize i = 0; i < gb_count_of(supported_hashes); i++) { + if (supported_hashes[i] == hash_kind) { + hash_found = true; + break; + } + } + if (!hash_found) { + ERROR_BLOCK(); + error(ce->proc, "Invalid hash kind passed to `#load_hash`, got: %.*s", LIT(hash_kind)); + error_line("\tAvailable hash kinds:\n"); + for (isize i = 0; i < gb_count_of(supported_hashes); i++) { + error_line("\t%.*s\n", LIT(supported_hashes[i])); + } + return false; + } + + + BlockingMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + gb_unused(ok); + + char *c_str = alloc_cstring(a, path); + defer (gb_free(a, c_str)); + + + gbFile f = {}; + gbFileError file_err = gb_file_open(&f, c_str); + defer (gb_file_close(&f)); + + switch (file_err) { + default: + case gbFileError_Invalid: + error(ce->proc, "Failed to `#load_hash` file: %s; invalid file or cannot be found", c_str); + return false; + case gbFileError_NotExists: + error(ce->proc, "Failed to `#load_hash` file: %s; file cannot be found", c_str); + return false; + case gbFileError_Permission: + error(ce->proc, "Failed to `#load_hash` file: %s; file permissions problem", c_str); + return false; + case gbFileError_None: + // Okay + break; + } + + // TODO(bill): make these procedures fast :P + + u64 hash_value = 0; + String result = {}; + isize file_size = cast(isize)gb_file_size(&f); + if (file_size > 0) { + u8 *data = cast(u8 *)gb_alloc(a, file_size); + gb_file_read_at(&f, data, file_size, 0); + if (hash_kind == "adler32") { + hash_value = gb_adler32(data, file_size); + } else if (hash_kind == "crc32") { + hash_value = gb_crc32(data, file_size); + } else if (hash_kind == "crc64") { + hash_value = gb_crc64(data, file_size); + } else if (hash_kind == "fnv32") { + hash_value = gb_fnv32(data, file_size); + } else if (hash_kind == "fnv64") { + hash_value = gb_fnv64(data, file_size); + } else if (hash_kind == "fnv32a") { + hash_value = fnv32a(data, file_size); + } else if (hash_kind == "fnv64a") { + hash_value = fnv64a(data, file_size); + } else if (hash_kind == "murmur32") { + hash_value = gb_murmur32(data, file_size); + } else if (hash_kind == "murmur64") { + hash_value = gb_murmur64(data, file_size); + } else { + compiler_error("unhandled hash kind: %.*s", LIT(hash_kind)); + } + gb_free(a, data); + } + + operand->type = t_untyped_integer; + operand->mode = Addressing_Constant; + operand->value = exact_value_u64(hash_value); + + } else if (name == "load_or") { + if (ce->args.count != 2) { + if (ce->args.count == 0) { + error(ce->close, "'#load_or' expects 2 arguments, got 0"); + } else { + error(ce->args[0], "'#load_or' expects 2 arguments, got %td", ce->args.count); + } + return false; + } + + Ast *arg = ce->args[0]; + Operand o = {}; + check_expr(c, &o, arg); + if (o.mode != Addressing_Constant) { + error(arg, "'#load_or' expected a constant string argument"); + return false; + } + + if (!is_type_string(o.type)) { + gbString str = type_to_string(o.type); + error(arg, "'#load_or' expected a constant string, got %s", str); + gb_string_free(str); + return false; + } + + Ast *default_arg = ce->args[1]; + Operand default_op = {}; + check_expr_with_type_hint(c, &default_op, default_arg, t_u8_slice); + if (default_op.mode != Addressing_Constant) { + error(arg, "'#load_or' expected a constant '[]byte' argument"); + return false; + } + + if (!are_types_identical(base_type(default_op.type), t_u8_slice)) { + gbString str = type_to_string(default_op.type); + error(arg, "'#load_or' expected a constant '[]byte', got %s", str); + gb_string_free(str); + return false; + } + + gbAllocator a = heap_allocator(); + + GB_ASSERT(o.value.kind == ExactValue_String); + String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); + String original_string = o.value.value_string; + + + BlockingMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + gb_unused(ok); + + char *c_str = alloc_cstring(a, path); + defer (gb_free(a, c_str)); + + + gbFile f = {}; + gbFileError file_err = gb_file_open(&f, c_str); + defer (gb_file_close(&f)); + + operand->type = t_u8_slice; + operand->mode = Addressing_Constant; + if (file_err == gbFileError_None) { + String result = {}; + isize file_size = cast(isize)gb_file_size(&f); + if (file_size > 0) { + u8 *data = cast(u8 *)gb_alloc(a, file_size+1); + gb_file_read_at(&f, data, file_size, 0); + data[file_size] = '\0'; + result.text = data; + result.len = file_size; + } + + operand->value = exact_value_string(result); + } else { + operand->value = default_op.value; + } + + } else if (name == "assert") { + if (ce->args.count != 1 && ce->args.count != 2) { + error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count); + return false; + } + if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) { + gbString str = expr_to_string(ce->args[0]); + error(call, "'%s' is not a constant boolean", str); + gb_string_free(str); + return false; + } + if (ce->args.count == 2) { + Ast *arg = unparen_expr(ce->args[1]); + if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) { + gbString str = expr_to_string(arg); + error(call, "'%s' is not a constant string", str); + gb_string_free(str); + return false; + } + } + + if (!operand->value.value_bool) { + gbString arg1 = expr_to_string(ce->args[0]); + gbString arg2 = {}; + + if (ce->args.count == 1) { + error(call, "Compile time assertion: %s", arg1); + } else { + arg2 = expr_to_string(ce->args[1]); + error(call, "Compile time assertion: %s (%s)", arg1, arg2); + } + + if (c->proc_name != "") { + gbString str = type_to_string(c->curr_proc_sig); + error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str); + gb_string_free(str); + } + + gb_string_free(arg1); + if (ce->args.count == 2) { + gb_string_free(arg2); + } + } + + operand->type = t_untyped_bool; + operand->mode = Addressing_Constant; + } else if (name == "panic") { + if (ce->args.count != 1) { + error(call, "'#panic' expects 1 argument, got %td", ce->args.count); + return false; + } + if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) { + gbString str = expr_to_string(ce->args[0]); + error(call, "'%s' is not a constant string", str); + gb_string_free(str); + return false; + } + error(call, "Compile time panic: %.*s", LIT(operand->value.value_string)); + if (c->proc_name != "") { + gbString str = type_to_string(c->curr_proc_sig); + error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str); + gb_string_free(str); + } + operand->type = t_invalid; + operand->mode = Addressing_NoValue; + } else if (name == "defined") { + if (ce->args.count != 1) { + error(call, "'#defined' expects 1 argument, got %td", ce->args.count); + return false; + } + Ast *arg = unparen_expr(ce->args[0]); + if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) { + error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind])); + return false; + } + + if (c->curr_proc_decl == nullptr) { + error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'"); + return false; + } + + bool is_defined = check_identifier_exists(c->scope, arg); + gb_unused(is_defined); + operand->type = t_untyped_bool; + operand->mode = Addressing_Constant; + operand->value = exact_value_bool(false); + + } else if (name == "config") { + if (ce->args.count != 2) { + error(call, "'#config' expects 2 argument, got %td", ce->args.count); + return false; + } + Ast *arg = unparen_expr(ce->args[0]); + if (arg == nullptr || arg->kind != Ast_Ident) { + error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind])); + return false; + } + + Ast *def_arg = unparen_expr(ce->args[1]); + + Operand def = {}; + check_expr(c, &def, def_arg); + if (def.mode != Addressing_Constant) { + error(def_arg, "'#config' default value must be a constant"); + return false; + } + + String name = arg->Ident.token.string; + + + operand->type = def.type; + operand->mode = def.mode; + operand->value = def.value; + + Entity *found = scope_lookup_current(config_pkg->scope, name); + if (found != nullptr) { + if (found->kind != Entity_Constant) { + error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name)); + } else { + operand->type = found->type; + operand->mode = Addressing_Constant; + operand->value = found->Constant.value; + } + } + } else { + error(call, "Unknown directive call: #%.*s", LIT(name)); + } + return true; +} + bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) { ast_node(ce, CallExpr, call); if (ce->inlining != ProcInlining_none) { @@ -1186,458 +1639,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 mpmc_enqueue(&c->info->intrinsics_entry_point_usage, call); break; - case BuiltinProc_DIRECTIVE: { - ast_node(bd, BasicDirective, ce->proc); - String name = bd->name.string; - if (name == "location") { - if (ce->args.count > 1) { - error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count); - } - if (ce->args.count > 0) { - Ast *arg = ce->args[0]; - Entity *e = nullptr; - Operand o = {}; - if (arg->kind == Ast_Ident) { - e = check_ident(c, &o, arg, nullptr, nullptr, true); - } else if (arg->kind == Ast_SelectorExpr) { - e = check_selector(c, &o, arg, nullptr); - } - if (e == nullptr) { - error(ce->args[0], "'#location' expected a valid entity name"); - } - } - - operand->type = t_source_code_location; - operand->mode = Addressing_Value; - } else if (name == "load") { - if (ce->args.count != 1) { - if (ce->args.count == 0) { - error(ce->close, "'#load' expects 1 argument, got 0"); - } else { - error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); - } - - return false; - } - - Ast *arg = ce->args[0]; - Operand o = {}; - check_expr(c, &o, arg); - if (o.mode != Addressing_Constant) { - error(arg, "'#load' expected a constant string argument"); - return false; - } - - if (!is_type_string(o.type)) { - gbString str = type_to_string(o.type); - error(arg, "'#load' expected a constant string, got %s", str); - gb_string_free(str); - return false; - } - - gbAllocator a = heap_allocator(); - - GB_ASSERT(o.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); - String original_string = o.value.value_string; - - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - switch (file_err) { - default: - case gbFileError_Invalid: - error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); - return false; - case gbFileError_NotExists: - error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); - return false; - case gbFileError_Permission: - error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); - return false; - case gbFileError_None: - // Okay - break; - } - - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size+1); - gb_file_read_at(&f, data, file_size, 0); - data[file_size] = '\0'; - result.text = data; - result.len = file_size; - } - - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; - operand->value = exact_value_string(result); - - } else if (name == "load_hash") { - if (ce->args.count != 2) { - if (ce->args.count == 0) { - error(ce->close, "'#load_hash' expects 2 argument, got 0"); - } else { - error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count); - } - return false; - } - - Ast *arg0 = ce->args[0]; - Ast *arg1 = ce->args[1]; - Operand o = {}; - check_expr(c, &o, arg0); - if (o.mode != Addressing_Constant) { - error(arg0, "'#load_hash' expected a constant string argument"); - return false; - } - - if (!is_type_string(o.type)) { - gbString str = type_to_string(o.type); - error(arg0, "'#load_hash' expected a constant string, got %s", str); - gb_string_free(str); - return false; - } - - Operand o_hash = {}; - check_expr(c, &o_hash, arg1); - if (o_hash.mode != Addressing_Constant) { - error(arg1, "'#load_hash' expected a constant string argument"); - return false; - } - - if (!is_type_string(o_hash.type)) { - gbString str = type_to_string(o.type); - error(arg1, "'#load_hash' expected a constant string, got %s", str); - gb_string_free(str); - return false; - } - - - gbAllocator a = heap_allocator(); - - GB_ASSERT(o.value.kind == ExactValue_String); - GB_ASSERT(o_hash.value.kind == ExactValue_String); - - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); - String original_string = o.value.value_string; - String hash_kind = o_hash.value.value_string; - - String supported_hashes[] = { - str_lit("adler32"), - str_lit("crc32"), - str_lit("crc64"), - str_lit("fnv32"), - str_lit("fnv64"), - str_lit("fnv32a"), - str_lit("fnv64a"), - str_lit("murmur32"), - str_lit("murmur64"), - }; - - bool hash_found = false; - for (isize i = 0; i < gb_count_of(supported_hashes); i++) { - if (supported_hashes[i] == hash_kind) { - hash_found = true; - break; - } - } - if (!hash_found) { - ERROR_BLOCK(); - error(ce->proc, "Invalid hash kind passed to `#load_hash`, got: %.*s", LIT(hash_kind)); - error_line("\tAvailable hash kinds:\n"); - for (isize i = 0; i < gb_count_of(supported_hashes); i++) { - error_line("\t%.*s\n", LIT(supported_hashes[i])); - } - return false; - } - - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - switch (file_err) { - default: - case gbFileError_Invalid: - error(ce->proc, "Failed to `#load_hash` file: %s; invalid file or cannot be found", c_str); - return false; - case gbFileError_NotExists: - error(ce->proc, "Failed to `#load_hash` file: %s; file cannot be found", c_str); - return false; - case gbFileError_Permission: - error(ce->proc, "Failed to `#load_hash` file: %s; file permissions problem", c_str); - return false; - case gbFileError_None: - // Okay - break; - } - - // TODO(bill): make these procedures fast :P - - u64 hash_value = 0; - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size); - gb_file_read_at(&f, data, file_size, 0); - if (hash_kind == "adler32") { - hash_value = gb_adler32(data, file_size); - } else if (hash_kind == "crc32") { - hash_value = gb_crc32(data, file_size); - } else if (hash_kind == "crc64") { - hash_value = gb_crc64(data, file_size); - } else if (hash_kind == "fnv32") { - hash_value = gb_fnv32(data, file_size); - } else if (hash_kind == "fnv64") { - hash_value = gb_fnv64(data, file_size); - } else if (hash_kind == "fnv32a") { - hash_value = fnv32a(data, file_size); - } else if (hash_kind == "fnv64a") { - hash_value = fnv64a(data, file_size); - } else if (hash_kind == "murmur32") { - hash_value = gb_murmur32(data, file_size); - } else if (hash_kind == "murmur64") { - hash_value = gb_murmur64(data, file_size); - } else { - compiler_error("unhandled hash kind: %.*s", LIT(hash_kind)); - } - gb_free(a, data); - } - - operand->type = t_untyped_integer; - operand->mode = Addressing_Constant; - operand->value = exact_value_u64(hash_value); - - } else if (name == "load_or") { - if (ce->args.count != 2) { - if (ce->args.count == 0) { - error(ce->close, "'#load_or' expects 2 arguments, got 0"); - } else { - error(ce->args[0], "'#load_or' expects 2 arguments, got %td", ce->args.count); - } - return false; - } - - Ast *arg = ce->args[0]; - Operand o = {}; - check_expr(c, &o, arg); - if (o.mode != Addressing_Constant) { - error(arg, "'#load_or' expected a constant string argument"); - return false; - } - - if (!is_type_string(o.type)) { - gbString str = type_to_string(o.type); - error(arg, "'#load_or' expected a constant string, got %s", str); - gb_string_free(str); - return false; - } - - Ast *default_arg = ce->args[1]; - Operand default_op = {}; - check_expr_with_type_hint(c, &default_op, default_arg, t_u8_slice); - if (default_op.mode != Addressing_Constant) { - error(arg, "'#load_or' expected a constant '[]byte' argument"); - return false; - } - - if (!are_types_identical(base_type(default_op.type), t_u8_slice)) { - gbString str = type_to_string(default_op.type); - error(arg, "'#load_or' expected a constant '[]byte', got %s", str); - gb_string_free(str); - return false; - } - - gbAllocator a = heap_allocator(); - - GB_ASSERT(o.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); - String original_string = o.value.value_string; - - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; - if (file_err == gbFileError_None) { - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size+1); - gb_file_read_at(&f, data, file_size, 0); - data[file_size] = '\0'; - result.text = data; - result.len = file_size; - } - - operand->value = exact_value_string(result); - } else { - operand->value = default_op.value; - } - - } else if (name == "assert") { - if (ce->args.count != 1 && ce->args.count != 2) { - error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count); - return false; - } - if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) { - gbString str = expr_to_string(ce->args[0]); - error(call, "'%s' is not a constant boolean", str); - gb_string_free(str); - return false; - } - if (ce->args.count == 2) { - Ast *arg = unparen_expr(ce->args[1]); - if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) { - gbString str = expr_to_string(arg); - error(call, "'%s' is not a constant string", str); - gb_string_free(str); - return false; - } - } - - if (!operand->value.value_bool) { - gbString arg1 = expr_to_string(ce->args[0]); - gbString arg2 = {}; - - if (ce->args.count == 1) { - error(call, "Compile time assertion: %s", arg1); - } else { - arg2 = expr_to_string(ce->args[1]); - error(call, "Compile time assertion: %s (%s)", arg1, arg2); - } - - if (c->proc_name != "") { - gbString str = type_to_string(c->curr_proc_sig); - error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str); - gb_string_free(str); - } - - gb_string_free(arg1); - if (ce->args.count == 2) { - gb_string_free(arg2); - } - } - - operand->type = t_untyped_bool; - operand->mode = Addressing_Constant; - } else if (name == "panic") { - if (ce->args.count != 1) { - error(call, "'#panic' expects 1 argument, got %td", ce->args.count); - return false; - } - if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) { - gbString str = expr_to_string(ce->args[0]); - error(call, "'%s' is not a constant string", str); - gb_string_free(str); - return false; - } - error(call, "Compile time panic: %.*s", LIT(operand->value.value_string)); - if (c->proc_name != "") { - gbString str = type_to_string(c->curr_proc_sig); - error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str); - gb_string_free(str); - } - operand->type = t_invalid; - operand->mode = Addressing_NoValue; - } else if (name == "defined") { - if (ce->args.count != 1) { - error(call, "'#defined' expects 1 argument, got %td", ce->args.count); - return false; - } - Ast *arg = unparen_expr(ce->args[0]); - if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) { - error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind])); - return false; - } - - if (c->curr_proc_decl == nullptr) { - error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'"); - return false; - } - - bool is_defined = check_identifier_exists(c->scope, arg); - gb_unused(is_defined); - operand->type = t_untyped_bool; - operand->mode = Addressing_Constant; - operand->value = exact_value_bool(false); - - } else if (name == "config") { - if (ce->args.count != 2) { - error(call, "'#config' expects 2 argument, got %td", ce->args.count); - return false; - } - Ast *arg = unparen_expr(ce->args[0]); - if (arg == nullptr || arg->kind != Ast_Ident) { - error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind])); - return false; - } - - Ast *def_arg = unparen_expr(ce->args[1]); - - Operand def = {}; - check_expr(c, &def, def_arg); - if (def.mode != Addressing_Constant) { - error(def_arg, "'#config' default value must be a constant"); - return false; - } - - String name = arg->Ident.token.string; - - - operand->type = def.type; - operand->mode = def.mode; - operand->value = def.value; - - Entity *found = scope_lookup_current(config_pkg->scope, name); - if (found != nullptr) { - if (found->kind != Entity_Constant) { - error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name)); - } else { - operand->type = found->type; - operand->mode = Addressing_Constant; - operand->value = found->Constant.value; - } - } - } else { - error(call, "Unknown directive call: #%.*s", LIT(name)); - } - - break; - } + case BuiltinProc_DIRECTIVE: + return check_builtin_procedure_directive(c, operand, call, id, type_hint); case BuiltinProc_len: check_expr_or_type(c, operand, ce->args[0]); -- cgit v1.2.3 From 38102f14c19f83ef1e0c13a824448bab4e80877e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 13:01:54 +0100 Subject: Add `#load(path) or_else default` in favour of `#load_or(path, default)` --- src/check_builtin.cpp | 175 ++++++++++++++++++++++++------------------- src/check_expr.cpp | 70 ++++++++++++++++- src/llvm_backend_utility.cpp | 4 + src/parser.hpp | 3 +- 4 files changed, 171 insertions(+), 81 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 7a72c574f..4a0863f9d 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1074,8 +1074,100 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call return false; } +LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) { + ast_node(ce, CallExpr, call); + ast_node(bd, BasicDirective, ce->proc); + String name = bd->name.string; + GB_ASSERT(name == "load"); + + if (ce->args.count != 1) { + if (ce->args.count == 0) { + error(ce->close, "'#load' expects 1 argument, got 0"); + } else { + error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); + } + + return LoadDirective_Error; + } + + Ast *arg = ce->args[0]; + Operand o = {}; + check_expr(c, &o, arg); + if (o.mode != Addressing_Constant) { + error(arg, "'#load' expected a constant string argument"); + return LoadDirective_Error; + } + + if (!is_type_string(o.type)) { + gbString str = type_to_string(o.type); + error(arg, "'#load' expected a constant string, got %s", str); + gb_string_free(str); + return LoadDirective_Error; + } + + gbAllocator a = heap_allocator(); + + GB_ASSERT(o.value.kind == ExactValue_String); + String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); + String original_string = o.value.value_string; + + + BlockingMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + gb_unused(ok); + + char *c_str = alloc_cstring(a, path); + defer (gb_free(a, c_str)); + + + gbFile f = {}; + gbFileError file_err = gb_file_open(&f, c_str); + defer (gb_file_close(&f)); + + switch (file_err) { + default: + case gbFileError_Invalid: + if (err_on_not_found) { + error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return LoadDirective_NotFound; + case gbFileError_NotExists: + if (err_on_not_found) { + error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return LoadDirective_NotFound; + case gbFileError_Permission: + if (err_on_not_found) { + error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return LoadDirective_NotFound; + case gbFileError_None: + // Okay + break; + } -bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) { + String result = {}; + isize file_size = cast(isize)gb_file_size(&f); + if (file_size > 0) { + u8 *data = cast(u8 *)gb_alloc(a, file_size+1); + gb_file_read_at(&f, data, file_size, 0); + data[file_size] = '\0'; + result.text = data; + result.len = file_size; + } + + operand->type = t_u8_slice; + operand->mode = Addressing_Constant; + operand->value = exact_value_string(result); + return LoadDirective_Success; +} + + +bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) { ast_node(ce, CallExpr, call); ast_node(bd, BasicDirective, ce->proc); String name = bd->name.string; @@ -1100,81 +1192,7 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast operand->type = t_source_code_location; operand->mode = Addressing_Value; } else if (name == "load") { - if (ce->args.count != 1) { - if (ce->args.count == 0) { - error(ce->close, "'#load' expects 1 argument, got 0"); - } else { - error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); - } - - return false; - } - - Ast *arg = ce->args[0]; - Operand o = {}; - check_expr(c, &o, arg); - if (o.mode != Addressing_Constant) { - error(arg, "'#load' expected a constant string argument"); - return false; - } - - if (!is_type_string(o.type)) { - gbString str = type_to_string(o.type); - error(arg, "'#load' expected a constant string, got %s", str); - gb_string_free(str); - return false; - } - - gbAllocator a = heap_allocator(); - - GB_ASSERT(o.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); - String original_string = o.value.value_string; - - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - switch (file_err) { - default: - case gbFileError_Invalid: - error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); - return false; - case gbFileError_NotExists: - error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); - return false; - case gbFileError_Permission: - error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); - return false; - case gbFileError_None: - // Okay - break; - } - - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size+1); - gb_file_read_at(&f, data, file_size, 0); - data[file_size] = '\0'; - result.text = data; - result.len = file_size; - } - - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; - operand->value = exact_value_string(result); - + return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success; } else if (name == "load_hash") { if (ce->args.count != 2) { if (ce->args.count == 0) { @@ -1263,7 +1281,6 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast char *c_str = alloc_cstring(a, path); defer (gb_free(a, c_str)); - gbFile f = {}; gbFileError file_err = gb_file_open(&f, c_str); defer (gb_file_close(&f)); @@ -1321,6 +1338,8 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast operand->value = exact_value_u64(hash_value); } else if (name == "load_or") { + warning(call, "'#load_or' is deprecated in favour of '#load(path) or_else default'"); + if (ce->args.count != 2) { if (ce->args.count == 0) { error(ce->close, "'#load_or' expects 2 arguments, got 0"); @@ -1640,7 +1659,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 break; case BuiltinProc_DIRECTIVE: - return check_builtin_procedure_directive(c, operand, call, id, type_hint); + return check_builtin_procedure_directive(c, operand, call, type_hint); case BuiltinProc_len: check_expr_or_type(c, operand, ce->args[0]); diff --git a/src/check_expr.cpp b/src/check_expr.cpp index f6c94466b..ae459574c 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -121,6 +121,28 @@ void check_or_return_split_types(CheckerContext *c, Operand *x, String const &na bool is_diverging_expr(Ast *expr); + +enum LoadDirectiveResult { + LoadDirective_Success = 0, + LoadDirective_Error = 1, + LoadDirective_NotFound = 2, +}; + +bool is_load_directive_call(Ast *call) { + call = unparen_expr(call); + if (call->kind != Ast_CallExpr) { + return false; + } + ast_node(ce, CallExpr, call); + if (ce->proc->kind != Ast_BasicDirective) { + return false; + } + ast_node(bd, BasicDirective, ce->proc); + String name = bd->name.string; + return name == "load"; +} +LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found); + void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") { auto results = did_you_mean_results(d); if (results.count != 0) { @@ -7407,9 +7429,54 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type String name = oe->token.string; Ast *arg = oe->x; Ast *default_value = oe->y; - Operand x = {}; Operand y = {}; + + // NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default + if (is_load_directive_call(arg)) { + LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false); + if (res == LoadDirective_Success) { + *o = x; + return Expr_Expr; + } + + bool y_is_diverging = false; + check_expr_base(c, &y, default_value, x.type); + switch (y.mode) { + case Addressing_NoValue: + if (is_diverging_expr(y.expr)) { + // Allow + y.mode = Addressing_Value; + y_is_diverging = true; + } else { + error_operand_no_value(&y); + y.mode = Addressing_Invalid; + } + break; + case Addressing_Type: + error_operand_not_expression(&y); + y.mode = Addressing_Invalid; + break; + } + + if (y.mode == Addressing_Invalid) { + o->mode = Addressing_Value; + o->type = t_invalid; + o->expr = node; + return Expr_Expr; + } + + if (!y_is_diverging) { + check_assignment(c, &y, x.type, name); + } + + o->mode = y.mode; + o->type = y.type; + o->expr = node; + + return Expr_Expr; + } + check_multi_expr_with_type_hint(c, &x, arg, type_hint); if (x.mode == Addressing_Invalid) { o->mode = Addressing_Value; @@ -7417,7 +7484,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type o->expr = node; return Expr_Expr; } - bool y_is_diverging = false; check_expr_base(c, &y, default_value, x.type); switch (y.mode) { diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index ce7b43321..09c45cb7a 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -351,6 +351,10 @@ lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) { lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) { + if (arg->state_flags & StateFlag_DirectiveWasFalse) { + return lb_build_expr(p, else_expr); + } + lbValue lhs = {}; lbValue rhs = {}; lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); diff --git a/src/parser.hpp b/src/parser.hpp index bfdae58a5..7433744e6 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -282,7 +282,8 @@ enum StateFlag : u8 { StateFlag_type_assert = 1<<2, StateFlag_no_type_assert = 1<<3, - StateFlag_SelectorCallExpr = 1<<6, + StateFlag_SelectorCallExpr = 1<<5, + StateFlag_DirectiveWasFalse = 1<<6, StateFlag_BeenHandled = 1<<7, }; -- cgit v1.2.3 From a054c2934eb0caa027a1663cca839cb422621558 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 13:32:45 +0100 Subject: Cache #load data and hashes --- src/check_builtin.cpp | 254 ++++++++++++++++++++++++++------------------------ src/checker.cpp | 4 + src/checker.hpp | 9 ++ 3 files changed, 147 insertions(+), 120 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 4a0863f9d..70b5a0a13 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1074,6 +1074,95 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call return false; } +bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_) { + ast_node(ce, CallExpr, call); + ast_node(bd, BasicDirective, ce->proc); + String builtin_name = bd->name.string; + + String base_dir = dir_from_path(get_file_path_string(call->file_id)); + + BlockingMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + gb_unused(ok); + + + MUTEX_GUARD(&c->info->load_file_mutex); + + gbFileError file_error = gbFileError_None; + String data = {}; + + LoadFileCache **cache_ptr = string_map_get(&c->info->load_file_cache, path); + LoadFileCache *cache = cache_ptr ? *cache_ptr : nullptr; + if (cache) { + file_error = cache->file_error; + data = cache->data; + } + defer ({ + if (cache == nullptr) { + LoadFileCache *new_cache = gb_alloc_item(permanent_allocator(), LoadFileCache); + new_cache->path = path; + new_cache->data = data; + new_cache->file_error = file_error; + string_map_init(&new_cache->hashes, heap_allocator(), 32); + string_map_set(&c->info->load_file_cache, path, new_cache); + if (cache_) *cache_ = new_cache; + } else { + cache->data = data; + cache->file_error = file_error; + if (cache_) *cache_ = cache; + } + }); + + char *c_str = alloc_cstring(heap_allocator(), path); + defer (gb_free(heap_allocator(), c_str)); + + gbFile f = {}; + if (cache == nullptr) { + file_error = gb_file_open(&f, c_str); + } + defer (gb_file_close(&f)); + + switch (file_error) { + default: + case gbFileError_Invalid: + if (err_on_not_found) { + error(ce->proc, "Failed to `#%.*s` file: %s; invalid file or cannot be found", LIT(builtin_name), c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return false; + case gbFileError_NotExists: + if (err_on_not_found) { + error(ce->proc, "Failed to `#%.*s` file: %s; file cannot be found", LIT(builtin_name), c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return false; + case gbFileError_Permission: + if (err_on_not_found) { + error(ce->proc, "Failed to `#%.*s` file: %s; file permissions problem", LIT(builtin_name), c_str); + } + call->state_flags |= StateFlag_DirectiveWasFalse; + return false; + case gbFileError_None: + // Okay + break; + } + + if (cache == nullptr) { + isize file_size = cast(isize)gb_file_size(&f); + if (file_size > 0) { + u8 *ptr = cast(u8 *)gb_alloc(permanent_allocator(), file_size+1); + gb_file_read_at(&f, ptr, file_size, 0); + ptr[file_size] = '\0'; + data.text = ptr; + data.len = file_size; + } + } + + return true; +} + + LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) { ast_node(ce, CallExpr, call); ast_node(bd, BasicDirective, ce->proc); @@ -1105,65 +1194,17 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As return LoadDirective_Error; } - gbAllocator a = heap_allocator(); - GB_ASSERT(o.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); - String original_string = o.value.value_string; - - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - switch (file_err) { - default: - case gbFileError_Invalid: - if (err_on_not_found) { - error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); - } - call->state_flags |= StateFlag_DirectiveWasFalse; - return LoadDirective_NotFound; - case gbFileError_NotExists: - if (err_on_not_found) { - error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); - } - call->state_flags |= StateFlag_DirectiveWasFalse; - return LoadDirective_NotFound; - case gbFileError_Permission: - if (err_on_not_found) { - error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); - } - call->state_flags |= StateFlag_DirectiveWasFalse; - return LoadDirective_NotFound; - case gbFileError_None: - // Okay - break; - } - - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size+1); - gb_file_read_at(&f, data, file_size, 0); - data[file_size] = '\0'; - result.text = data; - result.len = file_size; + LoadFileCache *cache = nullptr; + if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) { + operand->type = t_u8_slice; + operand->mode = Addressing_Constant; + operand->value = exact_value_string(cache->data); + return LoadDirective_Success; } + return LoadDirective_NotFound; - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; - operand->value = exact_value_string(result); - return LoadDirective_Success; } @@ -1232,14 +1273,11 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast gb_string_free(str); return false; } - - gbAllocator a = heap_allocator(); GB_ASSERT(o.value.kind == ExactValue_String); GB_ASSERT(o_hash.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); String original_string = o.value.value_string; String hash_kind = o_hash.value.value_string; @@ -1272,71 +1310,47 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast return false; } - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - - switch (file_err) { - default: - case gbFileError_Invalid: - error(ce->proc, "Failed to `#load_hash` file: %s; invalid file or cannot be found", c_str); - return false; - case gbFileError_NotExists: - error(ce->proc, "Failed to `#load_hash` file: %s; file cannot be found", c_str); - return false; - case gbFileError_Permission: - error(ce->proc, "Failed to `#load_hash` file: %s; file permissions problem", c_str); - return false; - case gbFileError_None: - // Okay - break; - } - - // TODO(bill): make these procedures fast :P - - u64 hash_value = 0; - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size); - gb_file_read_at(&f, data, file_size, 0); - if (hash_kind == "adler32") { - hash_value = gb_adler32(data, file_size); - } else if (hash_kind == "crc32") { - hash_value = gb_crc32(data, file_size); - } else if (hash_kind == "crc64") { - hash_value = gb_crc64(data, file_size); - } else if (hash_kind == "fnv32") { - hash_value = gb_fnv32(data, file_size); - } else if (hash_kind == "fnv64") { - hash_value = gb_fnv64(data, file_size); - } else if (hash_kind == "fnv32a") { - hash_value = fnv32a(data, file_size); - } else if (hash_kind == "fnv64a") { - hash_value = fnv64a(data, file_size); - } else if (hash_kind == "murmur32") { - hash_value = gb_murmur32(data, file_size); - } else if (hash_kind == "murmur64") { - hash_value = gb_murmur64(data, file_size); + LoadFileCache *cache = nullptr; + if (cache_load_file_directive(c, call, original_string, true, &cache)) { + MUTEX_GUARD(&c->info->load_file_mutex); + // TODO(bill): make these procedures fast :P + u64 hash_value = 0; + u64 *hash_value_ptr = string_map_get(&cache->hashes, hash_kind); + if (hash_value_ptr) { + hash_value = *hash_value_ptr; } else { - compiler_error("unhandled hash kind: %.*s", LIT(hash_kind)); + u8 *data = cache->data.text; + isize file_size = cache->data.len; + if (hash_kind == "adler32") { + hash_value = gb_adler32(data, file_size); + } else if (hash_kind == "crc32") { + hash_value = gb_crc32(data, file_size); + } else if (hash_kind == "crc64") { + hash_value = gb_crc64(data, file_size); + } else if (hash_kind == "fnv32") { + hash_value = gb_fnv32(data, file_size); + } else if (hash_kind == "fnv64") { + hash_value = gb_fnv64(data, file_size); + } else if (hash_kind == "fnv32a") { + hash_value = fnv32a(data, file_size); + } else if (hash_kind == "fnv64a") { + hash_value = fnv64a(data, file_size); + } else if (hash_kind == "murmur32") { + hash_value = gb_murmur32(data, file_size); + } else if (hash_kind == "murmur64") { + hash_value = gb_murmur64(data, file_size); + } else { + compiler_error("unhandled hash kind: %.*s", LIT(hash_kind)); + } + string_map_set(&cache->hashes, hash_kind, hash_value); } - gb_free(a, data); - } - - operand->type = t_untyped_integer; - operand->mode = Addressing_Constant; - operand->value = exact_value_u64(hash_value); + operand->type = t_untyped_integer; + operand->mode = Addressing_Constant; + operand->value = exact_value_u64(hash_value); + return true; + } + return false; } else if (name == "load_or") { warning(call, "'#load_or' is deprecated in favour of '#load(path) or_else default'"); diff --git a/src/checker.cpp b/src/checker.cpp index c75fc86af..d01dc5323 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1170,6 +1170,8 @@ void init_checker_info(CheckerInfo *i) { mutex_init(&i->objc_types_mutex); map_init(&i->objc_msgSend_types, a); + mutex_init(&i->load_file_mutex); + string_map_init(&i->load_file_cache, a); } void destroy_checker_info(CheckerInfo *i) { @@ -1205,6 +1207,8 @@ void destroy_checker_info(CheckerInfo *i) { mutex_destroy(&i->objc_types_mutex); map_destroy(&i->objc_msgSend_types); + mutex_init(&i->load_file_mutex); + string_map_destroy(&i->load_file_cache); } CheckerContext make_checker_context(Checker *c) { diff --git a/src/checker.hpp b/src/checker.hpp index f11a00532..badcd93d5 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -287,6 +287,12 @@ struct ObjcMsgData { ObjcMsgKind kind; Type *proc_type; }; +struct LoadFileCache { + String path; + gbFileError file_error; + String data; + StringMap hashes; +}; // CheckerInfo stores all the symbol information for a type-checked program struct CheckerInfo { @@ -363,6 +369,9 @@ struct CheckerInfo { BlockingMutex objc_types_mutex; PtrMap objc_msgSend_types; + + BlockingMutex load_file_mutex; + StringMap load_file_cache; }; struct CheckerContext { -- cgit v1.2.3 From 9eeed9d5bde1348d16d3c3790ba3178f3e0bb09d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 13:35:24 +0100 Subject: Simplify `#load_or` for the time being --- src/check_builtin.cpp | 34 +++------------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 70b5a0a13..b0c7f8d4b 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1392,45 +1392,17 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast gb_string_free(str); return false; } - - gbAllocator a = heap_allocator(); - GB_ASSERT(o.value.kind == ExactValue_String); - String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id)); String original_string = o.value.value_string; - - BlockingMutex *ignore_mutex = nullptr; - String path = {}; - bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); - gb_unused(ok); - - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - - - gbFile f = {}; - gbFileError file_err = gb_file_open(&f, c_str); - defer (gb_file_close(&f)); - operand->type = t_u8_slice; operand->mode = Addressing_Constant; - if (file_err == gbFileError_None) { - String result = {}; - isize file_size = cast(isize)gb_file_size(&f); - if (file_size > 0) { - u8 *data = cast(u8 *)gb_alloc(a, file_size+1); - gb_file_read_at(&f, data, file_size, 0); - data[file_size] = '\0'; - result.text = data; - result.len = file_size; - } - - operand->value = exact_value_string(result); + LoadFileCache *cache = nullptr; + if (cache_load_file_directive(c, call, original_string, false, &cache)) { + operand->value = exact_value_string(cache->data); } else { operand->value = default_op.value; } - } else if (name == "assert") { if (ce->args.count != 1 && ce->args.count != 2) { error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count); -- cgit v1.2.3 From 70dc0c15fd244bdc4a769b0d91b50ecc210bca65 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 13:43:35 +0100 Subject: Improve type hint for #load to allow for string types --- src/check_builtin.cpp | 8 ++++++-- src/check_expr.cpp | 14 ++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index b0c7f8d4b..122d3a461 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1196,10 +1196,14 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As GB_ASSERT(o.value.kind == ExactValue_String); + operand->type = t_u8_slice; + if (type_hint && is_type_string(type_hint)) { + operand->type = type_hint; + } + operand->mode = Addressing_Constant; + LoadFileCache *cache = nullptr; if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) { - operand->type = t_u8_slice; - operand->mode = Addressing_Constant; operand->value = exact_value_string(cache->data); return LoadDirective_Success; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index ae459574c..076eaba73 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7435,10 +7435,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type // NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default if (is_load_directive_call(arg)) { LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false); - if (res == LoadDirective_Success) { - *o = x; - return Expr_Expr; - } bool y_is_diverging = false; check_expr_base(c, &y, default_value, x.type); @@ -7468,10 +7464,16 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type if (!y_is_diverging) { check_assignment(c, &y, x.type, name); + if (y.mode != Addressing_Constant) { + error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'"); + } } - o->mode = y.mode; - o->type = y.type; + if (res == LoadDirective_Success) { + *o = x; + } else { + *o = y; + } o->expr = node; return Expr_Expr; -- cgit v1.2.3 From a7c39060038f63b1840f6eb5c0750bdb47e7d3ea Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 14:30:14 +0100 Subject: `#load(path, type)` where `type` can be `string` or `[]T` where `T` is a simple type --- src/check_builtin.cpp | 49 ++++++++++++++++++++++++++++++++++++------ src/llvm_backend_const.cpp | 4 ++-- src/llvm_backend_general.cpp | 48 +++++++++++++++++++++++++++++++++++++++++ src/types.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 9 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 122d3a461..36c9ea001 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1169,11 +1169,11 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As String name = bd->name.string; GB_ASSERT(name == "load"); - if (ce->args.count != 1) { + if (ce->args.count != 1 && ce->args.count != 2) { if (ce->args.count == 0) { - error(ce->close, "'#load' expects 1 argument, got 0"); + error(ce->close, "'#%.*s' expects 1 or 2 arguments, got 0", LIT(name)); } else { - error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); + error(ce->args[0], "'#%.*s' expects 1 or 2 arguments, got %td", LIT(name), ce->args.count); } return LoadDirective_Error; @@ -1183,13 +1183,13 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As Operand o = {}; check_expr(c, &o, arg); if (o.mode != Addressing_Constant) { - error(arg, "'#load' expected a constant string argument"); + error(arg, "'#%.*s' expected a constant string argument", LIT(name)); return LoadDirective_Error; } if (!is_type_string(o.type)) { gbString str = type_to_string(o.type); - error(arg, "'#load' expected a constant string, got %s", str); + error(arg, "'#%.*s' expected a constant string, got %s", LIT(name), str); gb_string_free(str); return LoadDirective_Error; } @@ -1197,8 +1197,43 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As GB_ASSERT(o.value.kind == ExactValue_String); operand->type = t_u8_slice; - if (type_hint && is_type_string(type_hint)) { - operand->type = type_hint; + if (ce->args.count == 1) { + if (type_hint && is_type_string(type_hint)) { + operand->type = type_hint; + } + } else if (ce->args.count == 2) { + bool failed = false; + Ast *arg_type = ce->args[1]; + Type *type = check_type(c, arg_type); + if (type != nullptr && type != t_invalid) { + if (is_type_string(type)) { + operand->type = type; + } else if (is_type_slice(type) /*|| is_type_array(type) || is_type_enumerated_array(type)*/) { + Type *elem = nullptr; + Type *bt = base_type(type); + if (bt->kind == Type_Slice) { + elem = bt->Slice.elem; + } else if (bt->kind == Type_Array) { + elem = bt->Array.elem; + } else if (bt->kind == Type_EnumeratedArray) { + elem = bt->EnumeratedArray.elem; + } + GB_ASSERT(elem != nullptr); + if (is_type_load_safe(elem)) { + operand->type = type; + } else { + failed = true; + } + } else { + failed = true; + } + } + + if (failed) { + gbString type_str = type_to_string(type); + error(arg_type, "'#%.*s' invalid type, expected a string, or slice of simple types, got %s", LIT(name), type_str); + gb_string_free(type_str); + } } operand->mode = Addressing_Constant; diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index 954778bb6..2d14070e2 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -391,8 +391,8 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc if (is_type_slice(type)) { if (value.kind == ExactValue_String) { - GB_ASSERT(is_type_u8_slice(type)); - res.value = lb_find_or_add_entity_string_byte_slice(m, value.value_string).value; + GB_ASSERT(is_type_slice(type)); + res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value; return res; } else { ast_node(cl, CompoundLit, value.value_compound); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 082163b2f..8704bc7c7 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2523,7 +2523,55 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) res.type = t_u8_slice; return res; } +lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String const &str, Type *slice_type) { + GB_ASSERT(is_type_slice(slice_type)); + LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; + LLVMValueRef data = LLVMConstStringInContext(m->ctx, + cast(char const *)str.text, + cast(unsigned)str.len, + false); + + + char *name = nullptr; + { + isize max_len = 7+8+1; + name = gb_alloc_array(permanent_allocator(), char, max_len); + u32 id = m->gen->global_array_index.fetch_add(1); + isize len = gb_snprintf(name, max_len, "csbs$%x", id); + len -= 1; + } + LLVMTypeRef type = LLVMTypeOf(data); + LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name); + LLVMSetInitializer(global_data, data); + LLVMSetLinkage(global_data, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); + LLVMSetAlignment(global_data, 1); + LLVMSetGlobalConstant(global_data, true); + i64 data_len = str.len; + LLVMValueRef ptr = nullptr; + if (data_len != 0) { + ptr = LLVMConstInBoundsGEP2(type, global_data, indices, 2); + } else { + ptr = LLVMConstNull(lb_type(m, t_u8_ptr)); + } + if (!is_type_u8_slice(slice_type)) { + Type *bt = base_type(slice_type); + Type *elem = bt->Slice.elem; + i64 sz = type_size_of(elem); + GB_ASSERT(sz > 0); + ptr = LLVMConstPointerCast(ptr, lb_type(m, alloc_type_pointer(elem))); + data_len /= sz; + } + + LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), data_len, true); + LLVMValueRef values[2] = {ptr, len}; + + lbValue res = {}; + res.value = llvm_const_named_struct(m, slice_type, values, 2); + res.type = slice_type; + return res; +} diff --git a/src/types.cpp b/src/types.cpp index cba27fd6f..1f4804430 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2398,6 +2398,57 @@ bool is_type_simple_compare(Type *t) { return false; } +bool is_type_load_safe(Type *type) { + GB_ASSERT(type != nullptr); + type = core_type(core_array_type(type)); + switch (type->kind) { + case Type_Basic: + return (type->Basic.flags & (BasicFlag_Boolean|BasicFlag_Numeric|BasicFlag_Rune)) != 0; + + case Type_BitSet: + if (type->BitSet.underlying) { + return is_type_load_safe(type->BitSet.underlying); + } + return true; + + case Type_RelativePointer: + case Type_RelativeSlice: + return true; + + case Type_Pointer: + case Type_MultiPointer: + case Type_Slice: + case Type_DynamicArray: + case Type_Proc: + case Type_SoaPointer: + return false; + + case Type_Enum: + case Type_EnumeratedArray: + case Type_Array: + case Type_SimdVector: + case Type_Matrix: + GB_PANIC("should never be hit"); + return false; + + case Type_Struct: + for_array(i, type->Struct.fields) { + if (!is_type_load_safe(type->Struct.fields[i]->type)) { + return false; + } + } + return type_size_of(type) > 0; + case Type_Union: + for_array(i, type->Union.variants) { + if (!is_type_load_safe(type->Union.variants[i])) { + return false; + } + } + return type_size_of(type) > 0; + } + return false; +} + String lookup_subtype_polymorphic_field(Type *dst, Type *src) { Type *prev_src = src; // Type *prev_dst = dst; -- cgit v1.2.3 From cecadce86d8070ee31d193736d331926efec0fff Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 11 Aug 2022 14:42:29 +0100 Subject: Allow for chaining of '#load(path) or_else #load(path)' --- src/check_builtin.cpp | 56 ++++++++++++++++++++++++++------------------------- src/check_expr.cpp | 55 ++++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 53 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 36c9ea001..687f1694b 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1163,6 +1163,27 @@ bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &origi } +bool is_valid_type_for_load(Type *type) { + if (type == t_invalid) { + return false; + } else if (is_type_string(type)) { + return true; + } else if (is_type_slice(type) /*|| is_type_array(type) || is_type_enumerated_array(type)*/) { + Type *elem = nullptr; + Type *bt = base_type(type); + if (bt->kind == Type_Slice) { + elem = bt->Slice.elem; + } else if (bt->kind == Type_Array) { + elem = bt->Array.elem; + } else if (bt->kind == Type_EnumeratedArray) { + elem = bt->EnumeratedArray.elem; + } + GB_ASSERT(elem != nullptr); + return is_type_load_safe(elem); + } + return false; +} + LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) { ast_node(ce, CallExpr, call); ast_node(bd, BasicDirective, ce->proc); @@ -1198,42 +1219,23 @@ LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, As operand->type = t_u8_slice; if (ce->args.count == 1) { - if (type_hint && is_type_string(type_hint)) { + if (type_hint && is_valid_type_for_load(type_hint)) { operand->type = type_hint; } } else if (ce->args.count == 2) { - bool failed = false; Ast *arg_type = ce->args[1]; Type *type = check_type(c, arg_type); - if (type != nullptr && type != t_invalid) { - if (is_type_string(type)) { + if (type != nullptr) { + if (is_valid_type_for_load(type)) { operand->type = type; - } else if (is_type_slice(type) /*|| is_type_array(type) || is_type_enumerated_array(type)*/) { - Type *elem = nullptr; - Type *bt = base_type(type); - if (bt->kind == Type_Slice) { - elem = bt->Slice.elem; - } else if (bt->kind == Type_Array) { - elem = bt->Array.elem; - } else if (bt->kind == Type_EnumeratedArray) { - elem = bt->EnumeratedArray.elem; - } - GB_ASSERT(elem != nullptr); - if (is_type_load_safe(elem)) { - operand->type = type; - } else { - failed = true; - } } else { - failed = true; + gbString type_str = type_to_string(type); + error(arg_type, "'#%.*s' invalid type, expected a string, or slice of simple types, got %s", LIT(name), type_str); + gb_string_free(type_str); } } - - if (failed) { - gbString type_str = type_to_string(type); - error(arg_type, "'#%.*s' invalid type, expected a string, or slice of simple types, got %s", LIT(name), type_str); - gb_string_free(type_str); - } + } else { + GB_PANIC("unreachable"); } operand->mode = Addressing_Constant; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 076eaba73..310874139 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7436,36 +7436,39 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type if (is_load_directive_call(arg)) { LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false); - bool y_is_diverging = false; - check_expr_base(c, &y, default_value, x.type); - switch (y.mode) { - case Addressing_NoValue: - if (is_diverging_expr(y.expr)) { - // Allow - y.mode = Addressing_Value; - y_is_diverging = true; - } else { - error_operand_no_value(&y); + // Allow for chaining of '#load(path) or_else #load(path)' + if (!(is_load_directive_call(default_value) && res == LoadDirective_Success)) { + bool y_is_diverging = false; + check_expr_base(c, &y, default_value, x.type); + switch (y.mode) { + case Addressing_NoValue: + if (is_diverging_expr(y.expr)) { + // Allow + y.mode = Addressing_Value; + y_is_diverging = true; + } else { + error_operand_no_value(&y); + y.mode = Addressing_Invalid; + } + break; + case Addressing_Type: + error_operand_not_expression(&y); y.mode = Addressing_Invalid; + break; } - break; - case Addressing_Type: - error_operand_not_expression(&y); - y.mode = Addressing_Invalid; - break; - } - if (y.mode == Addressing_Invalid) { - o->mode = Addressing_Value; - o->type = t_invalid; - o->expr = node; - return Expr_Expr; - } + if (y.mode == Addressing_Invalid) { + o->mode = Addressing_Value; + o->type = t_invalid; + o->expr = node; + return Expr_Expr; + } - if (!y_is_diverging) { - check_assignment(c, &y, x.type, name); - if (y.mode != Addressing_Constant) { - error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'"); + if (!y_is_diverging) { + check_assignment(c, &y, x.type, name); + if (y.mode != Addressing_Constant) { + error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'"); + } } } -- cgit v1.2.3 From 82e840a0ca3fb547ac93e680a3dcbbb67958077f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 17 Aug 2022 13:52:13 +0100 Subject: EXPERIMENTAL `intrinsics.valgrind_client_request` --- core/intrinsics/intrinsics.odin | 3 +++ src/build_settings.cpp | 3 +++ src/check_builtin.cpp | 35 +++++++++++++++++++++++++++++ src/checker.cpp | 3 +++ src/checker_builtin_procs.hpp | 4 ++++ src/llvm_backend_proc.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+) (limited to 'src/check_builtin.cpp') diff --git a/core/intrinsics/intrinsics.odin b/core/intrinsics/intrinsics.odin index 22b5d953d..0b682fbc3 100644 --- a/core/intrinsics/intrinsics.odin +++ b/core/intrinsics/intrinsics.odin @@ -295,6 +295,9 @@ objc_register_selector :: proc($name: string) -> objc_SEL --- objc_find_class :: proc($name: string) -> objc_Class --- objc_register_class :: proc($name: string) -> objc_Class --- + +valgrind_client_request :: proc(default: uintptr, request: uintptr, a0, a1, a2, a3, a4: uintptr) -> uintptr --- + // Internal compiler use only __entry_point :: proc() --- \ No newline at end of file diff --git a/src/build_settings.cpp b/src/build_settings.cpp index d49f1cecf..9d5c3e556 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -228,6 +228,7 @@ struct BuildContext { bool ODIN_DISABLE_ASSERT; // Whether the default 'assert' et al is disabled in code or not bool ODIN_DEFAULT_TO_NIL_ALLOCATOR; // Whether the default allocator is a "nil" allocator or not (i.e. it does nothing) bool ODIN_FOREIGN_ERROR_PROCEDURES; + bool ODIN_VALGRIND_SUPPORT; ErrorPosStyle ODIN_ERROR_POS_STYLE; @@ -1190,6 +1191,8 @@ void init_build_context(TargetMetrics *cross_target) { bc->optimization_level = gb_clamp(bc->optimization_level, 0, 3); + bc->ODIN_VALGRIND_SUPPORT = is_arch_x86() && build_context.metrics.os != TargetOs_windows; + #undef LINK_FLAG_X64 #undef LINK_FLAG_386 } diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 687f1694b..83136d576 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -5388,6 +5388,41 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 } break; + case BuiltinProc_valgrind_client_request: + { + if (!is_arch_x86()) { + error(call, "'%.*s' is only allowed on x86 targets (i386, amd64)", LIT(builtin_name)); + return false; + } + + enum {ARG_COUNT = 7}; + GB_ASSERT(builtin_procs[BuiltinProc_valgrind_client_request].arg_count == ARG_COUNT); + + Operand operands[ARG_COUNT] = {}; + for (isize i = 0; i < ARG_COUNT; i++) { + Operand *op = &operands[i]; + check_expr_with_type_hint(c, op, ce->args[i], t_uintptr); + if (op->mode == Addressing_Invalid) { + return false; + } + convert_to_typed(c, op, t_uintptr); + if (op->mode == Addressing_Invalid) { + return false; + } + if (!are_types_identical(op->type, t_uintptr)) { + gbString str = type_to_string(op->type); + error(op->expr, "'%.*s' expected a uintptr, got %s", LIT(builtin_name), str); + gb_string_free(str); + return false; + } + } + + operand->type = t_uintptr; + operand->mode = Addressing_Value; + operand->value = {}; + return true; + } + } return true; diff --git a/src/checker.cpp b/src/checker.cpp index d01dc5323..a7470a4c9 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1037,6 +1037,9 @@ void init_universal(void) { add_global_bool_constant("ODIN_FOREIGN_ERROR_PROCEDURES", bc->ODIN_FOREIGN_ERROR_PROCEDURES); add_global_bool_constant("ODIN_DISALLOW_RTTI", bc->disallow_rtti); + add_global_bool_constant("ODIN_VALGRIND_SUPPORT", bc->ODIN_VALGRIND_SUPPORT); + + // Builtin Procedures for (isize i = 0; i < gb_count_of(builtin_procs); i++) { diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 8dd021255..717422df1 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -291,6 +291,8 @@ BuiltinProc__type_end, BuiltinProc_wasm_memory_atomic_wait32, BuiltinProc_wasm_memory_atomic_notify32, + BuiltinProc_valgrind_client_request, + BuiltinProc_COUNT, }; gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { @@ -582,4 +584,6 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("wasm_memory_size"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("wasm_memory_atomic_wait32"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("wasm_memory_atomic_notify32"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + + {STR_LIT("valgrind_client_request"), 7, false, Expr_Expr, BuiltinProcPkg_intrinsics}, }; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index d7055ea31..f85d8397c 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -2745,6 +2745,55 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, res.value = LLVMBuildCall2(p->builder, func_type, the_asm, args, gb_count_of(args), ""); return res; } + + case BuiltinProc_valgrind_client_request: + { + lbValue args[7] = {}; + for (isize i = 0; i < 7; i++) { + args[i] = lb_emit_conv(p, lb_build_expr(p, ce->args[i]), t_uintptr); + } + if (!build_context.ODIN_VALGRIND_SUPPORT) { + return args[0]; + } + lbValue array = lb_generate_local_array(p, t_uintptr, 6, false); + for (isize i = 0; i < 6; i++) { + lbValue gep = lb_emit_array_epi(p, array, i); + lb_emit_store(p, gep, args[i+1]); + } + + switch (build_context.metrics.arch) { + case TargetArch_amd64: + { + Type *param_types[2] = {}; + param_types[0] = t_uintptr; + param_types[1] = array.type; + + Type *type = alloc_type_proc_from_types(param_types, gb_count_of(param_types), t_uintptr, false, ProcCC_None); + LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type); + LLVMValueRef the_asm = llvm_get_inline_asm( + func_type, + str_lit("rolq $3, %rdi; rolq $13, %rdi\n rolq $61, %rdi; rolq $51, %rdi\n xchgq %rbx, %rbx"), + str_lit("={rdx},{rdx},{rax},cc,memory"), + true + ); + + LLVMValueRef asm_args[2] = {}; + asm_args[0] = args[0].value; + asm_args[1] = array.value; + + lbValue res = {}; + res.type = t_uintptr; + res.value = LLVMBuildCall2(p->builder, func_type, the_asm, asm_args, gb_count_of(asm_args), ""); + return res; + } + break; + default: + GB_PANIC("Unsupported architecture: %.*s", LIT(target_arch_names[build_context.metrics.arch])); + break; + } + + } + } GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name)); -- cgit v1.2.3 From 2908923db9d047185de110991cc3347a112c38a6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 24 Aug 2022 12:18:42 +0100 Subject: Fix #1972 --- core/slice/slice.odin | 4 ++-- core/sys/unix/syscalls_linux.odin | 4 ++-- src/check_builtin.cpp | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/core/slice/slice.odin b/core/slice/slice.odin index 440cf643f..e76a5599d 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -14,14 +14,14 @@ _ :: mem Turn a pointer and a length into a slice. */ from_ptr :: proc "contextless" (ptr: ^$T, count: int) -> []T { - return ([^]T)(ptr)[:count] + return ([^]T)(ptr)[:count] } /* Turn a pointer and a length into a byte slice. */ bytes_from_ptr :: proc "contextless" (ptr: rawptr, byte_count: int) -> []byte { - return ([^]byte)(ptr)[:byte_count] + return ([^]byte)(ptr)[:byte_count] } /* diff --git a/core/sys/unix/syscalls_linux.odin b/core/sys/unix/syscalls_linux.odin index d611e33f0..48e9b49ab 100644 --- a/core/sys/unix/syscalls_linux.odin +++ b/core/sys/unix/syscalls_linux.odin @@ -1568,7 +1568,7 @@ sys_gettid :: proc "contextless" () -> int { } sys_getrandom :: proc "contextless" (buf: [^]byte, buflen: int, flags: uint) -> int { - return cast(int)intrinsics.syscall(SYS_getrandom, buf, cast(uintptr)(buflen), cast(uintptr)(flags)) + return cast(int)intrinsics.syscall(SYS_getrandom, uintptr(buf), uintptr(buflen), uintptr(flags)) } sys_open :: proc "contextless" (path: cstring, flags: int, mode: int = 0o000) -> int { @@ -1748,7 +1748,7 @@ sys_unlink :: proc "contextless" (path: cstring) -> int { } sys_unlinkat :: proc "contextless" (dfd: int, path: cstring, flag: int = 0) -> int { - return int(intrinsics.syscall(SYS_unlinkat, uintptr(dfd), uintptr(rawptr(path)), flag)) + return int(intrinsics.syscall(SYS_unlinkat, uintptr(dfd), uintptr(rawptr(path)), uintptr(flag))) } sys_rmdir :: proc "contextless" (path: cstring) -> int { diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 83136d576..e55a2e024 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4523,7 +4523,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 if (x.mode != Addressing_Invalid) { convert_to_typed(c, &x, t_uintptr); } - if (!is_type_uintptr(operand->type)) { + convert_to_typed(c, &x, t_uintptr); + if (!is_type_uintptr(x.type)) { gbString t = type_to_string(x.type); error(x.expr, "Argument %td must be of type 'uintptr', got %s", i, t); gb_string_free(t); -- cgit v1.2.3