Skip to content

Commit

Permalink
Merge pull request #3372 from ziglang/dump-analysis
Browse files Browse the repository at this point in the history
add -fdump-analysis to dump type information to json
  • Loading branch information
andrewrk authored Oct 3, 2019
2 parents 7640bec + 39d47b2 commit eca2aa6
Show file tree
Hide file tree
Showing 10 changed files with 831 additions and 131 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
"${CMAKE_SOURCE_DIR}/src/dump_analysis.cpp"
"${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
"${CMAKE_SOURCE_DIR}/src/error.cpp"
"${CMAKE_SOURCE_DIR}/src/glibc.cpp"
Expand All @@ -453,7 +454,6 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/os.cpp"
"${CMAKE_SOURCE_DIR}/src/parser.cpp"
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
"${CMAKE_SOURCE_DIR}/src/stack_report.cpp"
"${CMAKE_SOURCE_DIR}/src/target.cpp"
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
Expand Down
7 changes: 7 additions & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,12 @@ struct ZigTypeEnum {
uint32_t type_ptr_hash(const ZigType *ptr);
bool type_ptr_eql(const ZigType *a, const ZigType *b);

uint32_t pkg_ptr_hash(const ZigPackage *ptr);
bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b);

uint32_t tld_ptr_hash(const Tld *ptr);
bool tld_ptr_eql(const Tld *a, const Tld *b);

struct ZigTypeUnion {
AstNode *decl_node;
TypeUnionField *fields;
Expand Down Expand Up @@ -2056,6 +2062,7 @@ struct CodeGen {
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
bool have_stack_probing;
bool function_sections;
bool enable_dump_analysis;

Buf *mmacosx_version_min;
Buf *mios_version_min;
Expand Down
16 changes: 16 additions & 0 deletions src/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7303,6 +7303,22 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
return a == b;
}

uint32_t pkg_ptr_hash(const ZigPackage *ptr) {
return hash_ptr((void*)ptr);
}

bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) {
return a == b;
}

uint32_t tld_ptr_hash(const Tld *ptr) {
return hash_ptr((void*)ptr);
}

bool tld_ptr_eql(const Tld *a, const Tld *b) {
return a == b;
}

ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
resolve_top_level_decl(codegen, tld, nullptr, false);
Expand Down
29 changes: 23 additions & 6 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "util.hpp"
#include "zig_llvm.h"
#include "userland.h"
#include "dump_analysis.hpp"

#include <stdio.h>
#include <errno.h>
Expand Down Expand Up @@ -1724,7 +1725,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
}

ATTRIBUTE_NORETURN
static void report_errors_and_exit(CodeGen *g) {
void codegen_report_errors_and_exit(CodeGen *g) {
assert(g->errors.length != 0);
for (size_t i = 0; i < g->errors.length; i += 1) {
ErrorMsg *err = g->errors.at(i);
Expand All @@ -1735,7 +1736,7 @@ static void report_errors_and_exit(CodeGen *g) {

static void report_errors_and_maybe_exit(CodeGen *g) {
if (g->errors.length != 0) {
report_errors_and_exit(g);
codegen_report_errors_and_exit(g);
}
}

Expand All @@ -1745,7 +1746,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {
buf_sprintf("TODO: support C ABI for more targets. https://github.com/ziglang/zig/issues/1481"));
add_error_note(g, msg, source_node,
buf_sprintf("pointers, integers, floats, bools, and enums work on all targets"));
report_errors_and_exit(g);
codegen_report_errors_and_exit(g);
}

static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment) {
Expand Down Expand Up @@ -3456,7 +3457,7 @@ static bool value_is_all_undef(CodeGen *g, ConstExprValue *const_val) {
Error err;
if (const_val->special == ConstValSpecialLazy &&
(err = ir_resolve_lazy(g, nullptr, const_val)))
report_errors_and_exit(g);
codegen_report_errors_and_exit(g);

switch (const_val->special) {
case ConstValSpecialLazy:
Expand Down Expand Up @@ -4253,7 +4254,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
ZigType *struct_type = (struct_ptr_type->id == ZigTypeIdPointer) ?
struct_ptr_type->data.pointer.child_type : struct_ptr_type;
if ((err = type_resolve(g, struct_type, ResolveStatusLLVMFull)))
report_errors_and_exit(g);
codegen_report_errors_and_exit(g);

assert(field->gen_index != SIZE_MAX);
return LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, "");
Expand Down Expand Up @@ -6625,7 +6626,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
check: switch (const_val->special) {
case ConstValSpecialLazy:
if ((err = ir_resolve_lazy(g, nullptr, const_val))) {
report_errors_and_exit(g);
codegen_report_errors_and_exit(g);
}
goto check;
case ConstValSpecialRuntime:
Expand Down Expand Up @@ -10157,6 +10158,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
cache_bool(ch, g->have_stack_probing);
cache_bool(ch, g->is_dummy_so);
cache_bool(ch, g->function_sections);
cache_bool(ch, g->enable_dump_analysis);
cache_buf_opt(ch, g->mmacosx_version_min);
cache_buf_opt(ch, g->mios_version_min);
cache_usize(ch, g->version_major);
Expand Down Expand Up @@ -10338,6 +10340,21 @@ void codegen_build_and_link(CodeGen *g) {
gen_h_file(g);
}
}
if (g->enable_dump_analysis) {
const char *analysis_json_filename = buf_ptr(buf_sprintf("%s" OS_SEP "%s-analysis.json",
buf_ptr(g->output_dir), buf_ptr(g->root_out_name)));
FILE *f = fopen(analysis_json_filename, "wb");
if (f == nullptr) {
fprintf(stderr, "Unable to open '%s': %s\n", analysis_json_filename, strerror(errno));
exit(1);
}
zig_print_analysis_dump(g, f);
if (fclose(f) != 0) {
fprintf(stderr, "Unable to write '%s': %s\n", analysis_json_filename, strerror(errno));
exit(1);
}

}

// If we're outputting assembly or llvm IR we skip linking.
// If we're making a library or executable we must link.
Expand Down
2 changes: 2 additions & 0 deletions src/codegen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ void codegen_release_caches(CodeGen *codegen);
bool codegen_fn_has_err_ret_tracing_arg(CodeGen *g, ZigType *return_type);
bool codegen_fn_has_err_ret_tracing_stack(CodeGen *g, ZigFn *fn, bool is_async);

void codegen_report_errors_and_exit(CodeGen *g);

#endif
Loading

0 comments on commit eca2aa6

Please # to comment.