1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Compute and generate tables for classes and modules.
20 import table_computation
22 import icode_generator
23 private import compiling_global
24 private import compiling_icode
27 # The type of code generation to use
28 readable writable var _output_format
: String = "none"
30 # Compile the program depending on the output format
33 if output_format
== "none" then
36 # Optimize all iroutines
37 with_each_iroutines
!action
(i
, m
) do i
.optimize
(m
)
39 if output_format
== "C" then
41 else if output_format
== "icode" then
47 # Compile the program to C
48 # Generate all files (_sep.[ch] or _glob.[ch]), the main file (_table.c) and the build file (_build.sh)
49 # Then execute the build.sh
52 var cprogram
= new CProgram(self)
54 cprogram
.compdir
.mkdir
56 cprogram
.files
.add
("$CLIBDIR/nit_main.c")
57 cprogram
.files
.add
("$CLIBDIR/gc.c")
58 cprogram
.files
.add
("$CLIBDIR/gc_static_objects_list.c")
60 tc
.info
("Generating C code",1)
61 for m
in main_module
.mhe
.greaters_and_self
do m
.compile_separate_module
(cprogram
)
63 tc
.info
("Generating main, tables and makefile ...",1)
64 compile_main
(cprogram
)
66 cprogram
.generate_build_file
68 if not tc
.no_cc
then cprogram
.run_c_compiler
71 # Compile the main file
72 private fun compile_main
(cprogram
: CProgram)
74 var v
= new CompilerVisitor(main_module
, cprogram
)
75 v
.add_decl
("#include <nit_common.h>")
76 compile_tables_to_c
(v
)
78 var filename
= "{cprogram.compdir}/{main_module.name}._tables.c"
79 cprogram
.files
.add
(filename
)
80 var f
= new OFStream.open
(filename
)
81 f
.write
("/* This C file is generated by NIT to compile program {main_module.name}. */\n")
82 for m
in main_module
.mhe
.greaters_and_self
do
83 f
.write
("#include \"{cprogram.module_header_name(m)}\
"\n")
85 v
.header_writer
.write_to_stream
(f
)
86 v
.writer
.write_to_stream
(f
)
92 # Compile the sep or glob files (of the current module only)
93 private fun compile_separate_module
(cprogram
: CProgram)
95 var tc
= cprogram
.program
.tc
96 tc
.info
("Generating C code for module: {name}",2)
97 var v
= new CompilerVisitor(self, cprogram
)
98 v
.add_decl
("#include <nit_common.h>")
100 var native_name
= location
.file
.strip_extension
(".nit")
101 var native_header
= native_name
+ "_nit.h"
102 if native_header
.file_exists
then
103 v
.add_decl
("#include <{native_header.basename("")}>")
104 cprogram
.include_dirs
.add
("-I {native_name.dirname}")
106 var native_body
= native_name
+ "_nit.c"
107 if native_body
.file_exists
then cprogram
.files
.add
(native_body
)
109 declare_class_tables_to_c
(v
)
112 var hfilename
= cprogram
.module_header_name
(self)
113 var f
= new OFStream.open
("{cprogram.compdir}/{hfilename}")
114 f
.write
("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
115 f
.write
("#ifndef {name}{cprogram.get_file_ending}\n")
116 f
.write
("#define {name}{cprogram.get_file_ending}\n")
117 for m
in mhe
.direct_greaters
do f
.write
("#include \"{cprogram.module_header_name(m)}\
"\n")
118 v
.header_writer
.write_to_stream
(f
)
122 var cfilename
= "{cprogram.compdir}/{name}.{cprogram.get_file_ending}.c"
123 cprogram
.files
.add
(cfilename
)
124 f
= new OFStream.open
("{cfilename}")
125 f
.write
("/* This C file is generated by NIT to compile module {name}. */\n")
126 f
.write
("#include \"{hfilename}\
"\n")
127 v
.top_writer
.write_to_stream
(f
)