From 56c4039e72e569448e8d2fc79a40245e1dc13efa Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 20 Apr 2019 15:05:35 +0100 Subject: [PATCH] `#load` directive (request from #368) (Basic implementation) --- src/check_expr.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++- src/ir.cpp | 49 +++++++++++++++++-------------- src/ir_print.cpp | 15 +++++++++- src/parser.cpp | 35 +++++++++++++--------- 4 files changed, 133 insertions(+), 38 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index d10dd33c7..3ba270845 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -3125,6 +3125,76 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 operand->type = t_source_code_location; operand->mode = Addressing_Value; + } else if (name == "load") { + if (ce->args.count != 1) { + 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(bd->token.pos.file); + String original_string = o.value.value_string; + + + gbMutex *ignore_mutex = nullptr; + String path = {}; + bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path); + + 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 == "assert") { if (ce->args.count != 1) { error(call, "'#assert' expects 1 argument, got %td", ce->args.count); @@ -5318,7 +5388,7 @@ ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call) { ce->proc->kind == Ast_BasicDirective) { ast_node(bd, BasicDirective, ce->proc); String name = bd->name; - if (name == "location" || name == "assert" || name == "defined") { + if (name == "location" || name == "assert" || name == "defined" || name == "load") { operand->mode = Addressing_Builtin; operand->builtin_id = BuiltinProc_DIRECTIVE; operand->expr = ce->proc; diff --git a/src/ir.cpp b/src/ir.cpp index b486a6309..315810d80 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1525,30 +1525,35 @@ irValue *ir_add_module_constant(irModule *m, Type *type, ExactValue value) { gbAllocator a = ir_allocator(); if (is_type_slice(type)) { - ast_node(cl, CompoundLit, value.value_compound); + if (value.kind == ExactValue_String) { + GB_ASSERT(is_type_u8_slice(type)); + return ir_value_constant(type, value); + } else { + ast_node(cl, CompoundLit, value.value_compound); - isize count = cl->elems.count; - if (count == 0) { - return ir_value_nil(type); + isize count = cl->elems.count; + if (count == 0) { + return ir_value_nil(type); + } + Type *elem = base_type(type)->Slice.elem; + Type *t = alloc_type_array(elem, count); + irValue *backing_array = ir_add_module_constant(m, t, value); + + + isize max_len = 7+8+1; + u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len); + isize len = gb_snprintf(cast(char *)str, max_len, "csba$%x", m->global_array_index); + m->global_array_index++; + + String name = make_string(str, len-1); + + Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value); + irValue *g = ir_value_global(e, backing_array); + ir_module_add_value(m, e, g); + map_set(&m->members, hash_string(name), g); + + return ir_value_constant_slice(type, g, count); } - Type *elem = base_type(type)->Slice.elem; - Type *t = alloc_type_array(elem, count); - irValue *backing_array = ir_add_module_constant(m, t, value); - - - isize max_len = 7+8+1; - u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len); - isize len = gb_snprintf(cast(char *)str, max_len, "csba$%x", m->global_array_index); - m->global_array_index++; - - String name = make_string(str, len-1); - - Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value); - irValue *g = ir_value_global(e, backing_array); - ir_module_add_value(m, e, g); - map_set(&m->members, hash_string(name), g); - - return ir_value_constant_slice(type, g, count); } return ir_value_constant(type, value); diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 53c318e47..84a2eddc8 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -673,7 +673,20 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type * ir_write_str_lit(f, "zeroinitializer"); break; } - if (!is_type_string(type)) { + if (is_type_u8_slice(type)) { + irValue *str_array = ir_add_global_string_array(m, str); + ir_write_str_lit(f, "{i8* getelementptr inbounds ("); + ir_print_type(f, m, str_array->Global.entity->type); + ir_write_str_lit(f, ", "); + ir_print_type(f, m, str_array->Global.entity->type); + ir_write_str_lit(f, "* "); + ir_print_encoded_global(f, str_array->Global.entity->token.string, false); + ir_write_str_lit(f, ", "); + ir_print_type(f, m, t_i32); + ir_write_str_lit(f, " 0, i32 0), "); + ir_print_type(f, m, t_int); + ir_fprintf(f, " %lld}", cast(i64)str.len); + } else if (!is_type_string(type)) { GB_ASSERT(is_type_array(type)); ir_write_str_lit(f, "c\""); ir_print_escape_string(f, str, false, false); diff --git a/src/parser.cpp b/src/parser.cpp index 821c699c2..ae346b52f 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1677,6 +1677,9 @@ Ast *parse_operand(AstFile *f, bool lhs) { } else if (name.string == "location") { Ast *tag = ast_basic_directive(f, token, name.string); return parse_call_expr(f, tag); + } else if (name.string == "load") { + Ast *tag = ast_basic_directive(f, token, name.string); + return parse_call_expr(f, tag); } else if (name.string == "assert") { Ast *tag = ast_basic_directive(f, token, name.string); return parse_call_expr(f, tag); @@ -4191,7 +4194,7 @@ bool is_package_name_reserved(String const &name) { } -bool determine_path_from_string(Parser *p, Ast *node, String base_dir, String original_string, String *path) { +bool determine_path_from_string(gbMutex *file_mutex, Ast *node, String base_dir, String original_string, String *path) { GB_ASSERT(path != nullptr); gbAllocator a = heap_allocator(); @@ -4229,8 +4232,8 @@ bool determine_path_from_string(Parser *p, Ast *node, String base_dir, String or return true; } - gb_mutex_lock(&p->file_decl_mutex); - defer (gb_mutex_unlock(&p->file_decl_mutex)); + if (file_mutex) gb_mutex_lock(file_mutex); + defer (if (file_mutex) gb_mutex_unlock(file_mutex)); if (node->kind == Ast_ForeignImportDecl) { @@ -4321,7 +4324,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array String original_string = string_trim_whitespace(id->relpath.string); String import_path = {}; - bool ok = determine_path_from_string(p, node, base_dir, original_string, &import_path); + bool ok = determine_path_from_string(&p->file_decl_mutex, node, base_dir, original_string, &import_path); if (!ok) { decls[i] = ast_bad_decl(f, id->relpath, id->relpath); continue; @@ -4344,7 +4347,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array String fullpath = file_str; String foreign_path = {}; - bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path); + bool ok = determine_path_from_string(&p->file_decl_mutex, node, base_dir, file_str, &foreign_path); if (!ok) { decls[i] = ast_bad_decl(f, fl->filepaths[fp_idx], fl->filepaths[fl->filepaths.count-1]); goto end; @@ -4444,6 +4447,18 @@ bool parse_build_tag(Token token_for_pos, String s) { return true; } +String dir_from_path(String path) { + String base_dir = path; + for (isize i = path.len-1; i >= 0; i--) { + if (base_dir[i] == '\\' || + base_dir[i] == '/') { + break; + } + base_dir.len--; + } + return base_dir; +} + bool parse_file(Parser *p, AstFile *f) { if (f->tokens.count == 0) { return true; @@ -4453,15 +4468,7 @@ bool parse_file(Parser *p, AstFile *f) { } String filepath = f->tokenizer.fullpath; - String base_dir = filepath; - for (isize i = filepath.len-1; i >= 0; i--) { - if (base_dir[i] == '\\' || - base_dir[i] == '/') { - break; - } - base_dir.len--; - } - + String base_dir = dir_from_path(filepath); comsume_comment_groups(f, f->prev_token); CommentGroup *docs = f->lead_comment;