authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 00:34:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 23:24:40-04:00
log97c9f61db47ddba43a0db562e13773514875fa6a
tree02bfcaf5f43533478ffa0329588dcd182f1dc0a3
parent2d4b95900e7d7273dbd3ce6b7b9a33d2a19779da
signature Commit is signed but in an unrecognized format.

start creating a hash of input parameters

See #1416

5 files changed, 179 insertions(+), 30 deletions(-)

src/all_types.hpp+17-13
...@@ -1543,23 +1543,17 @@ struct LinkLib {...@@ -1543,23 +1543,17 @@ struct LinkLib {
1543 bool provided_explicitly;1543 bool provided_explicitly;
1544};1544};
15451545
1546// When adding fields, check if they should be added to the hash computation in build_with_cache
1546struct CodeGen {1547struct CodeGen {
1548 //////////////////////////// Runtime State
1547 LLVMModuleRef module;1549 LLVMModuleRef module;
1548 ZigList<ErrorMsg*> errors;1550 ZigList<ErrorMsg*> errors;
1549 LLVMBuilderRef builder;1551 LLVMBuilderRef builder;
1550 ZigLLVMDIBuilder *dbuilder;1552 ZigLLVMDIBuilder *dbuilder;
1551 ZigLLVMDICompileUnit *compile_unit;1553 ZigLLVMDICompileUnit *compile_unit;
1552 ZigLLVMDIFile *compile_unit_file;1554 ZigLLVMDIFile *compile_unit_file;
1553
1554 ZigList<LinkLib *> link_libs_list;
1555 LinkLib *libc_link_lib;1555 LinkLib *libc_link_lib;
15561556
1557 // add -framework [name] args to linker
1558 ZigList<Buf *> darwin_frameworks;
1559 // add -rpath [name] args to linker
1560 ZigList<Buf *> rpath_list;
1561
1562
1563 // reminder: hash tables must be initialized before use1557 // reminder: hash tables must be initialized before use
1564 HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;1558 HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
1565 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;1559 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
...@@ -1575,7 +1569,6 @@ struct CodeGen {...@@ -1575,7 +1569,6 @@ struct CodeGen {
1575 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;1569 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
1576 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;1570 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
15771571
1578
1579 ZigList<ImportTableEntry *> import_queue;1572 ZigList<ImportTableEntry *> import_queue;
1580 size_t import_queue_index;1573 size_t import_queue_index;
1581 ZigList<Tld *> resolve_queue;1574 ZigList<Tld *> resolve_queue;
...@@ -1620,7 +1613,20 @@ struct CodeGen {...@@ -1620,7 +1613,20 @@ struct CodeGen {
1620 ZigType *entry_promise;1613 ZigType *entry_promise;
1621 } builtin_types;1614 } builtin_types;
16221615
1616 //////////////////////////// Participates in Input Parameter Cache Hash
1617 ZigList<LinkLib *> link_libs_list;
1618 // add -framework [name] args to linker
1619 ZigList<Buf *> darwin_frameworks;
1620 // add -rpath [name] args to linker
1621 ZigList<Buf *> rpath_list;
1622
1623 EmitFileType emit_file_type;1623 EmitFileType emit_file_type;
1624 BuildMode build_mode;
1625 OutType out_type;
1626
1627
1628 //////////////////////////// Unsorted
1629
1624 ZigTarget zig_target;1630 ZigTarget zig_target;
1625 LLVMTargetDataRef target_data_ref;1631 LLVMTargetDataRef target_data_ref;
1626 unsigned pointer_size_bytes;1632 unsigned pointer_size_bytes;
...@@ -1647,7 +1653,6 @@ struct CodeGen {...@@ -1647,7 +1653,6 @@ struct CodeGen {
1647 Buf *ar_path;1653 Buf *ar_path;
1648 ZigWindowsSDK *win_sdk;1654 ZigWindowsSDK *win_sdk;
1649 Buf triple_str;1655 Buf triple_str;
1650 BuildMode build_mode;
1651 bool is_test_build;1656 bool is_test_build;
1652 bool have_err_ret_tracing;1657 bool have_err_ret_tracing;
1653 uint32_t target_os_index;1658 uint32_t target_os_index;
...@@ -1657,13 +1662,13 @@ struct CodeGen {...@@ -1657,13 +1662,13 @@ struct CodeGen {
1657 LLVMTargetMachineRef target_machine;1662 LLVMTargetMachineRef target_machine;
1658 ZigLLVMDIFile *dummy_di_file;1663 ZigLLVMDIFile *dummy_di_file;
1659 bool is_native_target;1664 bool is_native_target;
1660 PackageTableEntry *root_package;1665 PackageTableEntry *root_package; // participates in cache hash
1661 PackageTableEntry *std_package;1666 PackageTableEntry *std_package;
1662 PackageTableEntry *panic_package;1667 PackageTableEntry *panic_package;
1663 PackageTableEntry *test_runner_package;1668 PackageTableEntry *test_runner_package;
1664 PackageTableEntry *compile_var_package;1669 PackageTableEntry *compile_var_package;
1665 ImportTableEntry *compile_var_import;1670 ImportTableEntry *compile_var_import;
1666 Buf *root_out_name;1671 Buf *root_out_name; // participates in cache hash
1667 bool windows_subsystem_windows;1672 bool windows_subsystem_windows;
1668 bool windows_subsystem_console;1673 bool windows_subsystem_console;
1669 Buf *mmacosx_version_min;1674 Buf *mmacosx_version_min;
...@@ -1676,7 +1681,6 @@ struct CodeGen {...@@ -1676,7 +1681,6 @@ struct CodeGen {
1676 size_t fn_defs_index;1681 size_t fn_defs_index;
1677 ZigList<TldVar *> global_vars;1682 ZigList<TldVar *> global_vars;
16781683
1679 OutType out_type;
1680 ZigFn *cur_fn;1684 ZigFn *cur_fn;
1681 ZigFn *main_fn;1685 ZigFn *main_fn;
1682 ZigFn *panic_fn;1686 ZigFn *panic_fn;
src/codegen.cpp+162-13
...@@ -19,6 +19,7 @@...@@ -19,6 +19,7 @@
19#include "target.hpp"19#include "target.hpp"
20#include "util.hpp"20#include "util.hpp"
21#include "zig_llvm.h"21#include "zig_llvm.h"
22#include "blake2.h"
2223
23#include <stdio.h>24#include <stdio.h>
24#include <errno.h>25#include <errno.h>
...@@ -183,10 +184,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out...@@ -183,10 +184,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
183 return g;184 return g;
184}185}
185186
186void codegen_destroy(CodeGen *codegen) {
187 LLVMDisposeTargetMachine(codegen->target_machine);
188}
189
190void codegen_set_output_h_path(CodeGen *g, Buf *h_path) {187void codegen_set_output_h_path(CodeGen *g, Buf *h_path) {
191 g->out_h_path = h_path;188 g->out_h_path = h_path;
192}189}
...@@ -7112,23 +7109,30 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {...@@ -7112,23 +7109,30 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
7112 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");7109 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
7113}7110}
71147111
7115static void gen_root_source(CodeGen *g) {7112static Buf *get_resolved_root_src_path(CodeGen *g) {
7113 // TODO memoize
7116 if (buf_len(&g->root_package->root_src_path) == 0)7114 if (buf_len(&g->root_package->root_src_path) == 0)
7117 return;7115 return nullptr;
7118
7119 codegen_add_time_event(g, "Semantic Analysis");
71207116
7121 Buf *rel_full_path = buf_alloc();7117 Buf rel_full_path = BUF_INIT;
7122 os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, rel_full_path);7118 os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, &rel_full_path);
71237119
7124 Buf *resolved_path = buf_alloc();7120 Buf *resolved_path = buf_alloc();
7125 Buf *resolve_paths[] = {rel_full_path};7121 Buf *resolve_paths[] = {&rel_full_path};
7126 *resolved_path = os_path_resolve(resolve_paths, 1);7122 *resolved_path = os_path_resolve(resolve_paths, 1);
71277123
7124 return resolved_path;
7125}
7126
7127static void gen_root_source(CodeGen *g) {
7128 Buf *resolved_path = get_resolved_root_src_path(g);
7129 if (resolved_path == nullptr)
7130 return;
7131
7128 Buf *source_code = buf_alloc();7132 Buf *source_code = buf_alloc();
7129 int err;7133 int err;
7130 if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {7134 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
7131 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));7135 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));
7132 exit(1);7136 exit(1);
7133 }7137 }
71347138
...@@ -7671,10 +7675,155 @@ void codegen_add_time_event(CodeGen *g, const char *name) {...@@ -7671,10 +7675,155 @@ void codegen_add_time_event(CodeGen *g, const char *name) {
7671 g->timing_events.append({os_get_time(), name});7675 g->timing_events.append({os_get_time(), name});
7672}7676}
76737677
7678static void add_cache_str(blake2b_state *blake, const char *ptr) {
7679 assert(ptr != nullptr);
7680 // + 1 to include the null byte
7681 blake2b_update(blake, ptr, strlen(ptr) + 1);
7682}
7683
7684static void add_cache_int(blake2b_state *blake, int x) {
7685 // + 1 to include the null byte
7686 uint8_t buf[sizeof(int) + 1];
7687 memcpy(buf, &x, sizeof(int));
7688 buf[sizeof(int)] = 0;
7689 blake2b_update(blake, buf, sizeof(int) + 1);
7690}
7691
7692static void add_cache_buf(blake2b_state *blake, Buf *buf) {
7693 assert(buf != nullptr);
7694 // + 1 to include the null byte
7695 blake2b_update(blake, buf_ptr(buf), buf_len(buf) + 1);
7696}
7697
7698static void add_cache_buf_opt(blake2b_state *blake, Buf *buf) {
7699 if (buf == nullptr) {
7700 add_cache_str(blake, "");
7701 add_cache_str(blake, "");
7702 } else {
7703 add_cache_buf(blake, buf);
7704 }
7705}
7706
7707static void add_cache_list_of_link_lib(blake2b_state *blake, LinkLib **ptr, size_t len) {
7708 for (size_t i = 0; i < len; i += 1) {
7709 LinkLib *lib = ptr[i];
7710 if (lib->provided_explicitly) {
7711 add_cache_buf(blake, lib->name);
7712 }
7713 }
7714 add_cache_str(blake, "");
7715}
7716
7717static void add_cache_list_of_buf(blake2b_state *blake, Buf **ptr, size_t len) {
7718 for (size_t i = 0; i < len; i += 1) {
7719 Buf *buf = ptr[i];
7720 add_cache_buf(blake, buf);
7721 }
7722 add_cache_str(blake, "");
7723}
7724
7725//static void add_cache_file(CodeGen *g, blake2b_state *blake, Buf *resolved_path) {
7726// assert(file_name != nullptr);
7727// g->cache_files.append(resolved_path);
7728//}
7729//
7730//static void add_cache_file_opt(CodeGen *g, blake2b_state *blake, Buf *resolved_path) {
7731// if (resolved_path == nullptr) {
7732// add_cache_str(blake, "");
7733// add_cache_str(blake, "");
7734// } else {
7735// add_cache_file(g, blake, resolved_path);
7736// }
7737//}
7738
7739// Ported from std/base64.zig
7740static uint8_t base64_fs_alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
7741static void base64_encode(Slice<uint8_t> dest, Slice<uint8_t> source) {
7742 size_t dest_len = ((source.len + 2) / 3) * 4;
7743 assert(dest.len == dest_len);
7744
7745 size_t i = 0;
7746 size_t out_index = 0;
7747 for (; i + 2 < source.len; i += 3) {
7748 dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] >> 2) & 0x3f];
7749 out_index += 1;
7750
7751 dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i] & 0x3) << 4) | ((source.ptr[i + 1] & 0xf0) >> 4)];
7752 out_index += 1;
7753
7754 dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i + 1] & 0xf) << 2) | ((source.ptr[i + 2] & 0xc0) >> 6)];
7755 out_index += 1;
7756
7757 dest.ptr[out_index] = base64_fs_alphabet[source.ptr[i + 2] & 0x3f];
7758 out_index += 1;
7759 }
7760
7761 // Assert that we never need pad characters.
7762 assert(i == source.len);
7763 //if (i < source.len) {
7764 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] >> 2) & 0x3f];
7765 // out_index += 1;
7766
7767 // if (i + 1 == source.len) {
7768 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] & 0x3) << 4];
7769 // out_index += 1;
7770
7771 // dest.ptr[out_index] = encoder.pad_char;
7772 // out_index += 1;
7773 // } else {
7774 // dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i] & 0x3) << 4) | ((source.ptr[i + 1] & 0xf0) >> 4)];
7775 // out_index += 1;
7776
7777 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i + 1] & 0xf) << 2];
7778 // out_index += 1;
7779 // }
7780
7781 // dest.ptr[out_index] = encoder.pad_char;
7782 // out_index += 1;
7783 //}
7784}
7785
7786// Called before init()
7787static bool build_with_cache(CodeGen *g) {
7788 blake2b_state blake;
7789 int rc = blake2b_init(&blake, 48);
7790 assert(rc == 0);
7791
7792 // TODO zig exe & dynamic libraries
7793
7794 add_cache_buf(&blake, g->root_out_name);
7795 add_cache_buf_opt(&blake, get_resolved_root_src_path(g)); // Root source file
7796 add_cache_list_of_link_lib(&blake, g->link_libs_list.items, g->link_libs_list.length);
7797 add_cache_list_of_buf(&blake, g->darwin_frameworks.items, g->darwin_frameworks.length);
7798 add_cache_list_of_buf(&blake, g->rpath_list.items, g->rpath_list.length);
7799 add_cache_int(&blake, g->emit_file_type);
7800 add_cache_int(&blake, g->build_mode);
7801 add_cache_int(&blake, g->out_type);
7802 // TODO the rest of the struct CodeGen fields
7803
7804 uint8_t bin_digest[48];
7805 rc = blake2b_final(&blake, bin_digest, 48);
7806 assert(rc == 0);
7807
7808 Buf b64_digest = BUF_INIT;
7809 buf_resize(&b64_digest, 64);
7810 base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});
7811
7812 fprintf(stderr, "input params hash: %s\n", buf_ptr(&b64_digest));
7813 // TODO next look for a manifest file that has all the files from the input parameters
7814 // use that to construct the real hash, which looks up the output directory and another manifest file
7815
7816 return false;
7817}
7818
7674void codegen_build(CodeGen *g) {7819void codegen_build(CodeGen *g) {
7675 assert(g->out_type != OutTypeUnknown);7820 assert(g->out_type != OutTypeUnknown);
7821 if (build_with_cache(g))
7822 return;
7676 init(g);7823 init(g);
76777824
7825 codegen_add_time_event(g, "Semantic Analysis");
7826
7678 gen_global_asm(g);7827 gen_global_asm(g);
7679 gen_root_source(g);7828 gen_root_source(g);
7680 do_code_gen(g);7829 do_code_gen(g);
src/codegen.hpp-1
...@@ -16,7 +16,6 @@...@@ -16,7 +16,6 @@
1616
17CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,17CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
18 Buf *zig_lib_dir);18 Buf *zig_lib_dir);
19void codegen_destroy(CodeGen *codegen);
2019
21void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);20void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
22void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);21void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
src/link.cpp-2
...@@ -69,8 +69,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)...@@ -69,8 +69,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
69 os_path_join(&parent_gen->cache_dir, o_out_name, output_path);69 os_path_join(&parent_gen->cache_dir, o_out_name, output_path);
70 codegen_link(child_gen, buf_ptr(output_path));70 codegen_link(child_gen, buf_ptr(output_path));
7171
72 codegen_destroy(child_gen);
73
74 return output_path;72 return output_path;
75}73}
7674
src/main.cpp-1
...@@ -461,7 +461,6 @@ int main(int argc, char **argv) {...@@ -461,7 +461,6 @@ int main(int argc, char **argv) {
461 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);461 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
462 codegen_build(g);462 codegen_build(g);
463 codegen_link(g, buf_ptr(path_to_build_exe));463 codegen_link(g, buf_ptr(path_to_build_exe));
464 codegen_destroy(g);
465464
466 Termination term;465 Termination term;
467 os_spawn_process(buf_ptr(path_to_build_exe), args, &term);466 os_spawn_process(buf_ptr(path_to_build_exe), args, &term);