mirror of
https://github.com/ziglang/zig.git
synced 2026-01-03 20:13:21 +00:00
IR handles global variables correctly
This commit is contained in:
parent
a5c9da0de2
commit
d4f2394dcf
@ -847,7 +847,9 @@ static IrInstruction *analyze_const_value(CodeGen *g, BlockContext *scope, AstNo
|
||||
return g->invalid_instruction;
|
||||
|
||||
if (g->verbose) {
|
||||
fprintf(stderr, "{\n");
|
||||
fprintf(stderr, "\nSource: ");
|
||||
ast_render(stderr, node, 4);
|
||||
fprintf(stderr, "\n{ // (IR)\n");
|
||||
ir_print(stderr, &ir_executable, 4);
|
||||
fprintf(stderr, "}\n");
|
||||
}
|
||||
@ -2573,7 +2575,9 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
|
||||
return;
|
||||
}
|
||||
if (g->verbose) {
|
||||
fprintf(stderr, "fn %s {\n", buf_ptr(&fn_table_entry->symbol_name));
|
||||
fprintf(stderr, "\n");
|
||||
ast_render(stderr, fn_table_entry->fn_def_node, 4);
|
||||
fprintf(stderr, "\n{ // (IR)\n");
|
||||
ir_print(stderr, &fn_table_entry->ir_executable, 4);
|
||||
fprintf(stderr, "}\n");
|
||||
}
|
||||
@ -2583,7 +2587,7 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
|
||||
node->data.fn_def.implicit_return_type = block_return_type;
|
||||
|
||||
if (block_return_type->id != TypeTableEntryIdInvalid && g->verbose) {
|
||||
fprintf(stderr, "fn %s { // (analyzed)\n", buf_ptr(&fn_table_entry->symbol_name));
|
||||
fprintf(stderr, "{ // (analyzed)\n");
|
||||
ir_print(stderr, &fn_table_entry->analyzed_executable, 4);
|
||||
fprintf(stderr, "}\n");
|
||||
}
|
||||
|
||||
@ -655,7 +655,5 @@ void ast_render(FILE *f, AstNode *node, int indent_size) {
|
||||
ar.indent_size = indent_size;
|
||||
ar.indent = 0;
|
||||
|
||||
assert(node->type == NodeTypeRoot);
|
||||
|
||||
render_node(&ar, node);
|
||||
}
|
||||
|
||||
@ -2212,7 +2212,7 @@ static void do_code_gen(CodeGen *g) {
|
||||
if (!type_has_bits(var->type)) {
|
||||
continue;
|
||||
}
|
||||
if (var->ref_count == 0)
|
||||
if (var->is_inline)
|
||||
continue;
|
||||
|
||||
if (var->block_context->node->type == NodeTypeFnDef) {
|
||||
@ -2257,6 +2257,7 @@ static void do_code_gen(CodeGen *g) {
|
||||
|
||||
VariableTableEntry *variable = param_decl->data.param_decl.variable;
|
||||
assert(variable);
|
||||
assert(variable->value_ref);
|
||||
|
||||
if (!handle_is_ptr(variable->type)) {
|
||||
clear_debug_source_node(g);
|
||||
@ -3015,8 +3016,8 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
|
||||
}
|
||||
|
||||
if (g->verbose) {
|
||||
fprintf(stderr, "\nSemantic Analysis:\n");
|
||||
fprintf(stderr, "--------------------\n");
|
||||
fprintf(stderr, "\nIR Generation and Semantic Analysis:\n");
|
||||
fprintf(stderr, "--------------------------------------\n");
|
||||
}
|
||||
if (!g->error_during_imports) {
|
||||
semantic_analyze(g);
|
||||
|
||||
40
src/ir.cpp
40
src/ir.cpp
@ -3389,27 +3389,33 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
|
||||
if (var->type->id == TypeTableEntryIdInvalid)
|
||||
return var->type;
|
||||
|
||||
zig_panic("TODO if var is a global, this code is wrong");
|
||||
|
||||
TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, var->type, false);
|
||||
// TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
|
||||
if (var->mem_slot_index != SIZE_MAX) {
|
||||
ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
|
||||
if (mem_slot->ok) {
|
||||
zig_panic("TODO do we really want to set up this fake pointer to do constant evaluation?");
|
||||
ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,
|
||||
mem_slot->depends_on_compile_var);
|
||||
|
||||
out_val->data.x_ptr.len = 1;
|
||||
out_val->data.x_ptr.is_c_str = false;
|
||||
out_val->data.x_ptr.ptr = allocate<ConstExprValue *>(1);
|
||||
out_val->data.x_ptr.ptr[0] = mem_slot;
|
||||
return ptr_type;
|
||||
}
|
||||
ConstExprValue *mem_slot = nullptr;
|
||||
if (var->block_context->fn_entry) {
|
||||
// TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
|
||||
if (var->mem_slot_index != SIZE_MAX)
|
||||
mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
|
||||
} else if (var->src_is_const) {
|
||||
AstNode *var_decl_node = var->decl_node;
|
||||
assert(var_decl_node->type == NodeTypeVariableDeclaration);
|
||||
mem_slot = &get_resolved_expr(var_decl_node->data.variable_declaration.expr)->const_val;
|
||||
assert(mem_slot->ok);
|
||||
}
|
||||
|
||||
ir_build_var_ptr_from(&ira->new_irb, &var_ptr_instruction->base, var);
|
||||
return ptr_type;
|
||||
if (mem_slot && mem_slot->ok) {
|
||||
ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,
|
||||
mem_slot->depends_on_compile_var);
|
||||
|
||||
out_val->data.x_ptr.len = 1;
|
||||
out_val->data.x_ptr.is_c_str = false;
|
||||
out_val->data.x_ptr.ptr = allocate<ConstExprValue *>(1);
|
||||
out_val->data.x_ptr.ptr[0] = mem_slot;
|
||||
return ptr_type;
|
||||
} else {
|
||||
ir_build_var_ptr_from(&ira->new_irb, &var_ptr_instruction->base, var);
|
||||
return ptr_type;
|
||||
}
|
||||
}
|
||||
|
||||
static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user