From 55b8472374eede496b59396dbe253b05b16063e1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 30 Nov 2015 19:58:53 -0700 Subject: refactor code to prepare for multiple files verbose compiler output is now behind --verbose flag --- src/analyze.cpp | 105 ++++++++------------------------------------------------ 1 file changed, 15 insertions(+), 90 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 8dae356e81..131bc22e52 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -113,7 +113,7 @@ static void resolve_function_proto(CodeGen *g, AstNode *node) { resolve_type(g, node->data.fn_proto.return_type); } -static void preview_function_declarations(CodeGen *g, AstNode *node) { +static void preview_function_declarations(CodeGen *g, ImportTableEntry *import, AstNode *node) { switch (node->type) { case NodeTypeExternBlock: for (int i = 0; i < node->data.extern_block.directives->length; i += 1) { @@ -139,6 +139,7 @@ static void preview_function_declarations(CodeGen *g, AstNode *node) { fn_table_entry->proto_node = fn_proto; fn_table_entry->is_extern = true; fn_table_entry->calling_convention = LLVMCCallConv; + fn_table_entry->import_entry = import; g->fn_table.put(name, fn_table_entry); } break; @@ -156,6 +157,7 @@ static void preview_function_declarations(CodeGen *g, AstNode *node) { node->codegen_node->data.fn_def_node.skip = true; } else { FnTableEntry *fn_table_entry = allocate(1); + fn_table_entry->import_entry = import; fn_table_entry->proto_node = proto_node; fn_table_entry->fn_def_node = node; fn_table_entry->internal_linkage = proto_node->data.fn_proto.visib_mod != FnProtoVisibModExport; @@ -190,8 +192,8 @@ static void preview_function_declarations(CodeGen *g, AstNode *node) { } else { g->root_export_decl = node; - if (!g->out_name) - g->out_name = &node->data.root_export_decl.name; + if (!g->root_out_name) + g->root_out_name = &node->data.root_export_decl.name; Buf *out_type = &node->data.root_export_decl.type; OutType export_out_type; @@ -209,6 +211,9 @@ static void preview_function_declarations(CodeGen *g, AstNode *node) { g->out_type = export_out_type; } break; + case NodeTypeUse: + zig_panic("TODO use"); + break; case NodeTypeDirective: case NodeTypeParamDecl: case NodeTypeFnProto: @@ -346,6 +351,7 @@ static void analyze_expression(CodeGen *g, AstNode *node) { case NodeTypeRootExportDecl: case NodeTypeExternBlock: case NodeTypeFnDef: + case NodeTypeUse: zig_unreachable(); } } @@ -377,9 +383,9 @@ static void analyze_top_level_declaration(CodeGen *g, AstNode *node) { case NodeTypeRootExportDecl: case NodeTypeExternBlock: + case NodeTypeUse: // already looked at these in the preview pass break; - case NodeTypeDirective: case NodeTypeParamDecl: case NodeTypeFnProto: @@ -400,13 +406,13 @@ static void analyze_top_level_declaration(CodeGen *g, AstNode *node) { } } -static void analyze_root(CodeGen *g, AstNode *node) { +static void analyze_root(CodeGen *g, ImportTableEntry *import, AstNode *node) { assert(node->type == NodeTypeRoot); // find function declarations for (int i = 0; i < node->data.root.top_level_decls.length; i += 1) { AstNode *child = node->data.root.top_level_decls.at(i); - preview_function_declarations(g, child); + preview_function_declarations(g, import, child); } for (int i = 0; i < node->data.root.top_level_decls.length; i += 1) { @@ -414,7 +420,7 @@ static void analyze_root(CodeGen *g, AstNode *node) { analyze_top_level_declaration(g, child); } - if (!g->out_name) { + if (!g->root_out_name) { add_node_error(g, node, buf_sprintf("missing export declaration and output name not provided")); } else if (g->out_type == OutTypeUnknown) { @@ -423,88 +429,7 @@ static void analyze_root(CodeGen *g, AstNode *node) { } } -static void define_primitive_types(CodeGen *g) { - { - TypeTableEntry *entry = allocate(1); - entry->type_ref = LLVMInt8Type(); - buf_init_from_str(&entry->name, "u8"); - entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 8, 8, - LLVMZigEncoding_DW_ATE_unsigned()); - g->type_table.put(&entry->name, entry); - g->builtin_types.entry_u8 = entry; - } - { - TypeTableEntry *entry = allocate(1); - entry->type_ref = LLVMInt32Type(); - buf_init_from_str(&entry->name, "i32"); - entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 32, 32, - LLVMZigEncoding_DW_ATE_signed()); - g->type_table.put(&entry->name, entry); - g->builtin_types.entry_i32 = entry; - } - { - TypeTableEntry *entry = allocate(1); - entry->type_ref = LLVMVoidType(); - buf_init_from_str(&entry->name, "void"); - entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 0, 0, - LLVMZigEncoding_DW_ATE_unsigned()); - g->type_table.put(&entry->name, entry); - g->builtin_types.entry_void = entry; - - // invalid types are void - g->builtin_types.entry_invalid = entry; - } - { - TypeTableEntry *entry = allocate(1); - entry->type_ref = LLVMVoidType(); - buf_init_from_str(&entry->name, "unreachable"); - entry->di_type = g->builtin_types.entry_invalid->di_type; - g->type_table.put(&entry->name, entry); - g->builtin_types.entry_unreachable = entry; - } -} - - -void semantic_analyze(CodeGen *g) { - LLVMInitializeAllTargets(); - LLVMInitializeAllTargetMCs(); - LLVMInitializeAllAsmPrinters(); - LLVMInitializeAllAsmParsers(); - LLVMInitializeNativeTarget(); - - g->is_native_target = true; - char *native_triple = LLVMGetDefaultTargetTriple(); - - LLVMTargetRef target_ref; - char *err_msg = nullptr; - if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) { - zig_panic("unable to get target from triple: %s", err_msg); - } - - char *native_cpu = LLVMZigGetHostCPUName(); - char *native_features = LLVMZigGetNativeFeatures(); - - LLVMCodeGenOptLevel opt_level = (g->build_type == CodeGenBuildTypeDebug) ? - LLVMCodeGenLevelNone : LLVMCodeGenLevelAggressive; - - LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC; - - g->target_machine = LLVMCreateTargetMachine(target_ref, native_triple, - native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault); - - g->target_data_ref = LLVMGetTargetMachineData(g->target_machine); - - - g->module = LLVMModuleCreateWithName("ZigModule"); - - g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref); - - g->builder = LLVMCreateBuilder(); - g->dbuilder = LLVMZigCreateDIBuilder(g->module, true); - - - define_primitive_types(g); - - analyze_root(g, g->root); +void semantic_analyze(CodeGen *g, ImportTableEntry *import_table_entry) { + analyze_root(g, import_table_entry, import_table_entry->root); } -- cgit v1.2.3