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 compdir
= tc
.compdir
.as(not null)
55 var files
= new Array[String]
56 var includes
= new ArraySet[String]
57 files
.add
("$CLIBDIR/nit_main.c")
58 files
.add
("$CLIBDIR/gc.c")
59 files
.add
("$CLIBDIR/gc_static_objects_list.c")
60 tc
.info
("Generating C code",1)
61 for m
in module.mhe
.greaters_and_self
do
62 files
.add
("{compdir}/{m.name}.{get_file_ending}.c")
63 tc
.info
("Generating C code for module: {m.name}",2)
64 m
.compile_separate_module
(self)
65 var native_name
= m
.location
.file
.strip_extension
(".nit")
66 if (native_name
+ "_nit.h").file_exists
then
67 includes
.add
("-I {native_name.dirname}")
69 native_name
+= "_nit.c"
70 if native_name
.file_exists
then files
.add
(native_name
)
73 tc
.info
("Generating main, tables and makefile ...",1)
74 files
.add
("{compdir}/{module.name}._tables.c")
77 var fn
= "{compdir}/{module.name}._build.sh"
78 var f
= new OFStream.open
(fn
)
81 if tc
.verbose_level
> 0 then
83 for i
in [1..tc
.verbose_level
] do verbose
= verbose
+ "v"
86 f
.write
("#!/bin/sh\n")
87 f
.write
("# This shell script is generated by NIT to compile the program {module.name}.\n")
88 f
.write
("CLIBDIR=\"{tc.clibdir.as(not null)}\
"\n")
89 f
.write
("{tc.bindir.as(not null)}/gccx {verbose} -d {compdir} -I $CLIBDIR {includes.join(" ")}")
90 if tc
.output_file
!= null then
91 f
.write
(" -o {tc.output_file.as(not null)}")
92 else if tc
.ext_prefix
.is_empty
then
93 f
.write
(" -o {module.name}")
95 f
.write
(" -o {module.name}_{tc.ext_prefix}")
97 if tc
.boost
then f
.write
(" -O")
98 if not tc
.cc_link
then f
.write
(" -x \"-c\
"")
99 for l
in tc
.cc_libs
do f
.write
(" -x \"-l
{l}\
"")
100 for lp
in tc
.cc_lib_paths
do f
.write
(" -x \"-L
{lp}\
"")
101 for ip
in tc
.cc_include_paths
do f
.write
(" -x \"-I
{ip}\
"")
102 f
.write
(" \"$@\
" \\\n {files.join("\\\n ")}\n")
106 tc
.info
("Building",1)
107 sys
.system
("sh {fn}")
111 # Compile the main file
112 private fun compile_main
114 var v
= new CompilerVisitor(module, self)
115 v
.add_decl
("#include <nit_common.h>")
116 compile_tables_to_c
(v
)
118 var f
= new OFStream.open
("{tc.compdir.as(not null)}/{module.name}._tables.c")
119 f
.write
("/* This C file is generated by NIT to compile program {module.name}. */\n")
120 for m
in module.mhe
.greaters_and_self
do
121 f
.write
("#include \"{m.name}.{get_file_ending}.h\
"\n")
123 v
.header_writer
.write_to_stream
(f
)
124 v
.writer
.write_to_stream
(f
)
130 # Compile the sep or glob files (of the current module only)
131 private fun compile_separate_module
(program
: Program)
134 var v
= new CompilerVisitor(self, program
)
135 v
.add_decl
("#include <nit_common.h>")
136 var native_name
= location
.file
.strip_extension
(".nit")
137 native_name
+= ("_nit.h")
138 if native_name
.file_exists
then v
.add_decl
("#include <{native_name.basename("")}>")
139 declare_class_tables_to_c
(v
)
141 var f
= new OFStream.open
("{tc.compdir.as(not null)}/{name}.{program.get_file_ending}.h")
142 f
.write
("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
143 f
.write
("#ifndef {name}{program.get_file_ending}\n")
144 f
.write
("#define {name}{program.get_file_ending}\n")
145 for m
in mhe
.direct_greaters
do f
.write
("#include \"{m.name}.{program.get_file_ending}.h\
"\n")
146 v
.header_writer
.write_to_stream
(f
)
150 f
= new OFStream.open
("{tc.compdir.as(not null)}/{name}.{program.get_file_ending}.c")
151 f
.write
("/* This C file is generated by NIT to compile module {name}. */\n")
152 f
.write
("#include \"{name}.{program.get_file_ending}.h\
"\n")
153 v
.top_writer
.write_to_stream
(f
)