Skip to content

Commit eca2aa6

Browse files
authored
Merge pull request #3372 from ziglang/dump-analysis
add -fdump-analysis to dump type information to json
2 parents 7640bec + 39d47b2 commit eca2aa6

10 files changed

+831
-131
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ set(ZIG_SOURCES
443443
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
444444
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
445445
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
446+
"${CMAKE_SOURCE_DIR}/src/dump_analysis.cpp"
446447
"${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
447448
"${CMAKE_SOURCE_DIR}/src/error.cpp"
448449
"${CMAKE_SOURCE_DIR}/src/glibc.cpp"
@@ -453,7 +454,6 @@ set(ZIG_SOURCES
453454
"${CMAKE_SOURCE_DIR}/src/os.cpp"
454455
"${CMAKE_SOURCE_DIR}/src/parser.cpp"
455456
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
456-
"${CMAKE_SOURCE_DIR}/src/stack_report.cpp"
457457
"${CMAKE_SOURCE_DIR}/src/target.cpp"
458458
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
459459
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp"

src/all_types.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,12 @@ struct ZigTypeEnum {
13001300
uint32_t type_ptr_hash(const ZigType *ptr);
13011301
bool type_ptr_eql(const ZigType *a, const ZigType *b);
13021302

1303+
uint32_t pkg_ptr_hash(const ZigPackage *ptr);
1304+
bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b);
1305+
1306+
uint32_t tld_ptr_hash(const Tld *ptr);
1307+
bool tld_ptr_eql(const Tld *a, const Tld *b);
1308+
13031309
struct ZigTypeUnion {
13041310
AstNode *decl_node;
13051311
TypeUnionField *fields;
@@ -2056,6 +2062,7 @@ struct CodeGen {
20562062
bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic
20572063
bool have_stack_probing;
20582064
bool function_sections;
2065+
bool enable_dump_analysis;
20592066

20602067
Buf *mmacosx_version_min;
20612068
Buf *mios_version_min;

src/analyze.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7303,6 +7303,22 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
73037303
return a == b;
73047304
}
73057305

7306+
uint32_t pkg_ptr_hash(const ZigPackage *ptr) {
7307+
return hash_ptr((void*)ptr);
7308+
}
7309+
7310+
bool pkg_ptr_eql(const ZigPackage *a, const ZigPackage *b) {
7311+
return a == b;
7312+
}
7313+
7314+
uint32_t tld_ptr_hash(const Tld *ptr) {
7315+
return hash_ptr((void*)ptr);
7316+
}
7317+
7318+
bool tld_ptr_eql(const Tld *a, const Tld *b) {
7319+
return a == b;
7320+
}
7321+
73067322
ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
73077323
Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
73087324
resolve_top_level_decl(codegen, tld, nullptr, false);

src/codegen.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "util.hpp"
2121
#include "zig_llvm.h"
2222
#include "userland.h"
23+
#include "dump_analysis.hpp"
2324

2425
#include <stdio.h>
2526
#include <errno.h>
@@ -1724,7 +1725,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
17241725
}
17251726

17261727
ATTRIBUTE_NORETURN
1727-
static void report_errors_and_exit(CodeGen *g) {
1728+
void codegen_report_errors_and_exit(CodeGen *g) {
17281729
assert(g->errors.length != 0);
17291730
for (size_t i = 0; i < g->errors.length; i += 1) {
17301731
ErrorMsg *err = g->errors.at(i);
@@ -1735,7 +1736,7 @@ static void report_errors_and_exit(CodeGen *g) {
17351736

17361737
static void report_errors_and_maybe_exit(CodeGen *g) {
17371738
if (g->errors.length != 0) {
1738-
report_errors_and_exit(g);
1739+
codegen_report_errors_and_exit(g);
17391740
}
17401741
}
17411742

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

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

34613462
switch (const_val->special) {
34623463
case ConstValSpecialLazy:
@@ -4253,7 +4254,7 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
42534254
ZigType *struct_type = (struct_ptr_type->id == ZigTypeIdPointer) ?
42544255
struct_ptr_type->data.pointer.child_type : struct_ptr_type;
42554256
if ((err = type_resolve(g, struct_type, ResolveStatusLLVMFull)))
4256-
report_errors_and_exit(g);
4257+
codegen_report_errors_and_exit(g);
42574258

42584259
assert(field->gen_index != SIZE_MAX);
42594260
return LLVMBuildStructGEP(g->builder, struct_ptr, (unsigned)field->gen_index, "");
@@ -6625,7 +6626,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
66256626
check: switch (const_val->special) {
66266627
case ConstValSpecialLazy:
66276628
if ((err = ir_resolve_lazy(g, nullptr, const_val))) {
6628-
report_errors_and_exit(g);
6629+
codegen_report_errors_and_exit(g);
66296630
}
66306631
goto check;
66316632
case ConstValSpecialRuntime:
@@ -10157,6 +10158,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
1015710158
cache_bool(ch, g->have_stack_probing);
1015810159
cache_bool(ch, g->is_dummy_so);
1015910160
cache_bool(ch, g->function_sections);
10161+
cache_bool(ch, g->enable_dump_analysis);
1016010162
cache_buf_opt(ch, g->mmacosx_version_min);
1016110163
cache_buf_opt(ch, g->mios_version_min);
1016210164
cache_usize(ch, g->version_major);
@@ -10338,6 +10340,21 @@ void codegen_build_and_link(CodeGen *g) {
1033810340
gen_h_file(g);
1033910341
}
1034010342
}
10343+
if (g->enable_dump_analysis) {
10344+
const char *analysis_json_filename = buf_ptr(buf_sprintf("%s" OS_SEP "%s-analysis.json",
10345+
buf_ptr(g->output_dir), buf_ptr(g->root_out_name)));
10346+
FILE *f = fopen(analysis_json_filename, "wb");
10347+
if (f == nullptr) {
10348+
fprintf(stderr, "Unable to open '%s': %s\n", analysis_json_filename, strerror(errno));
10349+
exit(1);
10350+
}
10351+
zig_print_analysis_dump(g, f);
10352+
if (fclose(f) != 0) {
10353+
fprintf(stderr, "Unable to write '%s': %s\n", analysis_json_filename, strerror(errno));
10354+
exit(1);
10355+
}
10356+
10357+
}
1034110358

1034210359
// If we're outputting assembly or llvm IR we skip linking.
1034310360
// If we're making a library or executable we must link.

src/codegen.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ void codegen_release_caches(CodeGen *codegen);
6464
bool codegen_fn_has_err_ret_tracing_arg(CodeGen *g, ZigType *return_type);
6565
bool codegen_fn_has_err_ret_tracing_stack(CodeGen *g, ZigFn *fn, bool is_async);
6666

67+
void codegen_report_errors_and_exit(CodeGen *g);
68+
6769
#endif

0 commit comments

Comments
 (0)