@@ -55,7 +55,7 @@ PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_pa
55
55
return entry;
56
56
}
57
57
58
- CodeGen *codegen_create (Buf *root_source_dir , const ZigTarget *target) {
58
+ CodeGen *codegen_create (Buf *root_src_path , const ZigTarget *target) {
59
59
CodeGen *g = allocate<CodeGen>(1 );
60
60
61
61
codegen_add_time_event (g, " Initialize" );
@@ -81,9 +81,17 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
81
81
// reserve index 0 to indicate no error
82
82
g->error_decls .append (nullptr );
83
83
84
- g->root_package = new_package (buf_ptr (root_source_dir), " " );
85
- g->std_package = new_package (ZIG_STD_DIR, " index.zig" );
86
- g->root_package ->package_table .put (buf_create_from_str (" std" ), g->std_package );
84
+ if (root_src_path) {
85
+ Buf *src_basename = buf_alloc ();
86
+ Buf *src_dir = buf_alloc ();
87
+ os_path_split (root_src_path, src_dir, src_basename);
88
+
89
+ g->root_package = new_package (buf_ptr (src_dir), buf_ptr (src_basename));
90
+ g->std_package = new_package (ZIG_STD_DIR, " index.zig" );
91
+ g->root_package ->package_table .put (buf_create_from_str (" std" ), g->std_package );
92
+ } else {
93
+ g->root_package = new_package (" ." , " " );
94
+ }
87
95
g->zig_std_dir = buf_create_from_str (ZIG_STD_DIR);
88
96
89
97
g->zig_std_special_dir = buf_alloc ();
@@ -757,7 +765,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
757
765
758
766
static void gen_debug_safety_crash_for_err (CodeGen *g, LLVMValueRef err_val) {
759
767
LLVMValueRef safety_crash_err_fn = get_safety_crash_err_fn (g);
760
- LLVMBuildCall (g->builder , safety_crash_err_fn, &err_val, 1 , " " );
768
+ ZigLLVMBuildCall (g->builder , safety_crash_err_fn, &err_val, 1 , LLVMFastCallConv, false , " " );
761
769
LLVMBuildUnreachable (g->builder );
762
770
}
763
771
@@ -3655,6 +3663,10 @@ static LLVMValueRef build_alloca(CodeGen *g, TypeTableEntry *type_entry, const c
3655
3663
}
3656
3664
3657
3665
static void do_code_gen (CodeGen *g) {
3666
+ if (g->verbose ) {
3667
+ fprintf (stderr, " \n Code Generation:\n " );
3668
+ fprintf (stderr, " ------------------\n " );
3669
+ }
3658
3670
assert (!g->errors .length );
3659
3671
3660
3672
codegen_add_time_event (g, " Code Generation" );
@@ -4602,8 +4614,9 @@ static void define_builtin_compile_vars(CodeGen *g) {
4602
4614
add_compile_var (g, " panic_implementation_provided" , create_const_bool (g, false ));
4603
4615
}
4604
4616
4605
- static void init (CodeGen *g, Buf *source_path) {
4606
- g->module = LLVMModuleCreateWithName (buf_ptr (source_path));
4617
+ static void init (CodeGen *g) {
4618
+ assert (g->root_out_name );
4619
+ g->module = LLVMModuleCreateWithName (buf_ptr (g->root_out_name ));
4607
4620
4608
4621
get_target_triple (&g->triple_str , &g->zig_target );
4609
4622
@@ -4674,23 +4687,25 @@ static void init(CodeGen *g, Buf *source_path) {
4674
4687
g->const_void_val .type = g->builtin_types .entry_void ;
4675
4688
}
4676
4689
4677
- void codegen_parseh (CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code ) {
4690
+ void codegen_parseh (CodeGen *g, Buf *full_path ) {
4678
4691
find_libc_include_path (g);
4679
- Buf *full_path = buf_alloc ();
4680
- os_path_join (src_dirname, src_basename, full_path);
4692
+
4693
+ Buf *src_basename = buf_alloc ();
4694
+ Buf *src_dirname = buf_alloc ();
4695
+ os_path_split (full_path, src_dirname, src_basename);
4681
4696
4682
4697
ImportTableEntry *import = allocate<ImportTableEntry>(1 );
4683
- import->source_code = source_code ;
4698
+ import->source_code = nullptr ;
4684
4699
import->path = full_path;
4685
4700
g->root_import = import;
4686
4701
import->decls_scope = create_decls_scope (nullptr , nullptr , nullptr , import);
4687
4702
4688
- init (g, full_path );
4703
+ init (g);
4689
4704
4690
4705
import->di_file = ZigLLVMCreateFile (g->dbuilder , buf_ptr (src_basename), buf_ptr (src_dirname));
4691
4706
4692
4707
ZigList<ErrorMsg *> errors = {0 };
4693
- int err = parse_h_buf (import, &errors, source_code , g, nullptr );
4708
+ int err = parse_h_file (import, &errors, buf_ptr (full_path) , g, nullptr );
4694
4709
if (err) {
4695
4710
fprintf (stderr, " unable to parse .h file: %s\n " , err_str (err));
4696
4711
exit (1 );
@@ -4719,7 +4734,7 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
4719
4734
zig_panic (" unable to open '%s': %s" , buf_ptr (&path_to_code_src), err_str (err));
4720
4735
}
4721
4736
4722
- return add_source_file (g, package, abs_full_path, g-> zig_std_special_dir , code_basename, import_code);
4737
+ return add_source_file (g, package, abs_full_path, import_code);
4723
4738
}
4724
4739
4725
4740
static PackageTableEntry *create_bootstrap_pkg (CodeGen *g) {
@@ -4736,23 +4751,27 @@ static PackageTableEntry *create_zigrt_pkg(CodeGen *g) {
4736
4751
return package;
4737
4752
}
4738
4753
4739
- void codegen_add_root_code (CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *source_code) {
4740
- codegen_add_time_event (g, " Semantic Analysis" );
4741
-
4742
- Buf source_path = BUF_INIT;
4743
- os_path_join (src_dir, src_basename, &source_path);
4754
+ static void gen_root_source (CodeGen *g) {
4755
+ if (buf_len (&g->root_package ->root_src_path ) == 0 )
4756
+ return ;
4744
4757
4745
- buf_init_from_buf (&g-> root_package -> root_src_path , src_basename );
4758
+ codegen_add_time_event (g, " Semantic Analysis " );
4746
4759
4747
- init (g, &source_path);
4760
+ Buf *rel_full_path = buf_alloc ();
4761
+ os_path_join (&g->root_package ->root_src_dir , &g->root_package ->root_src_path , rel_full_path);
4748
4762
4749
4763
Buf *abs_full_path = buf_alloc ();
4750
4764
int err;
4751
- if ((err = os_path_real (&source_path, abs_full_path))) {
4752
- zig_panic (" unable to open '%s': %s" , buf_ptr (&source_path), err_str (err));
4765
+ if ((err = os_path_real (rel_full_path, abs_full_path))) {
4766
+ zig_panic (" unable to open '%s': %s" , buf_ptr (rel_full_path), err_str (err));
4767
+ }
4768
+
4769
+ Buf *source_code = buf_alloc ();
4770
+ if ((err = os_fetch_file_path (rel_full_path, source_code))) {
4771
+ zig_panic (" unable to open '%s': %s" , buf_ptr (rel_full_path), err_str (err));
4753
4772
}
4754
4773
4755
- g->root_import = add_source_file (g, g->root_package , abs_full_path, src_dir, src_basename, source_code);
4774
+ g->root_import = add_source_file (g, g->root_package , abs_full_path, source_code);
4756
4775
4757
4776
assert (g->root_out_name );
4758
4777
assert (g->out_type != OutTypeUnknown);
@@ -4787,27 +4806,33 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
4787
4806
exit (1 );
4788
4807
}
4789
4808
4790
- if (g->verbose ) {
4791
- fprintf (stderr, " \n Code Generation:\n " );
4792
- fprintf (stderr, " ------------------\n " );
4793
-
4794
- }
4795
-
4796
- do_code_gen (g);
4797
4809
}
4798
4810
4799
- void codegen_add_root_assembly (CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *source_code ) {
4800
- Buf source_path = BUF_INIT ;
4801
- os_path_join (src_dir, src_basename, &source_path);
4811
+ void codegen_add_assembly (CodeGen *g, Buf *path ) {
4812
+ g-> assembly_files . append (path) ;
4813
+ }
4802
4814
4803
- init (g, &source_path);
4815
+ static void gen_global_asm (CodeGen *g) {
4816
+ Buf contents = BUF_INIT;
4817
+ int err;
4818
+ for (size_t i = 0 ; i < g->assembly_files .length ; i += 1 ) {
4819
+ Buf *asm_file = g->assembly_files .at (i);
4820
+ if ((err = os_fetch_file_path (asm_file, &contents))) {
4821
+ zig_panic (" Unable to read %s: %s" , buf_ptr (asm_file), err_str (err));
4822
+ }
4823
+ if (g->zig_target .arch .arch == ZigLLVM_x86 || g->zig_target .arch .arch == ZigLLVM_x86_64) {
4824
+ buf_append_str (&g->global_asm , " .intel_syntax noprefix\n " );
4825
+ }
4826
+ buf_append_buf (&g->global_asm , &contents);
4827
+ }
4828
+ }
4804
4829
4805
- assert (g-> root_out_name );
4830
+ void codegen_build (CodeGen *g) {
4806
4831
assert (g->out_type != OutTypeUnknown);
4832
+ init (g);
4807
4833
4808
- buf_init_from_str (&g->global_asm , " .intel_syntax noprefix\n " );
4809
- buf_append_buf (&g->global_asm , source_code);
4810
-
4834
+ gen_global_asm (g);
4835
+ gen_root_source (g);
4811
4836
do_code_gen (g);
4812
4837
}
4813
4838
0 commit comments