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 private import compiling_global
23 private import compiling_icode
26 # The type of code generation to use
27 readable writable var _output_format
: String = "none"
29 # Compile the program depending on the output format
32 if output_format
== "none" then
34 else if output_format
== "C" then
39 # Compile the program to C
40 # Generate all files (_sep.[ch] or _glob.[ch]), the main file (_table.c) and the build file (_build.sh)
41 # Then execute the build.sh
46 var files
= new Array[String]
47 var includes
= new ArraySet[String]
48 files
.add
("$CLIBDIR/nit_main.c")
49 files
.add
("$CLIBDIR/gc.c")
50 files
.add
("$CLIBDIR/gc_static_objects_list.c")
51 tc
.info
("Generating C code",1)
52 for m
in module.mhe
.greaters_and_self
do
53 files
.add
("{tc.compdir}/{m.name}.{get_file_ending}.c")
54 tc
.info
("Generating C code for module: {m.name}",2)
55 m
.compile_separate_module
(self)
56 var native_name
= m
.location
.file
.strip_extension
(".nit")
57 if (native_name
+ "_nit.h").file_exists
then
58 includes
.add
("-I {native_name.dirname}")
60 native_name
+= "_nit.c"
61 if native_name
.file_exists
then files
.add
(native_name
)
64 tc
.info
("Generating main, tables and makefile ...",1)
65 files
.add
("{tc.compdir}/{module.name}._tables.c")
68 var fn
= "{tc.compdir}/{module.name}._build.sh"
69 var f
= new OFStream.open
(fn
)
72 if tc
.verbose_level
> 0 then
74 for i
in [1..tc
.verbose_level
] do verbose
= verbose
+ "v"
77 f
.write
("#!/bin/sh\n")
78 f
.write
("# This shell script is generated by NIT to compile the program {module.name}.\n")
79 f
.write
("CLIBDIR=\"{tc.clibdir}\
"\n")
80 f
.write
("{tc.bindir}/gccx {verbose} -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
81 if tc
.output_file
!= null then
82 f
.write
(" -o {tc.output_file}")
83 else if tc
.ext_prefix
.is_empty
then
84 f
.write
(" -o {module.name}")
86 f
.write
(" -o {module.name}_{tc.ext_prefix}")
88 if tc
.boost
then f
.write
(" -O")
89 f
.write
(" \"$@\
" \\\n {files.join("\\\n ")}\n")
98 # Compile the main file
99 private fun compile_main
101 var v
= new CompilerVisitor(module, self)
102 v
.add_decl
("#include <nit_common.h>")
103 compile_tables_to_c
(v
)
105 var f
= new OFStream.open
("{tc.compdir}/{module.name}._tables.c")
106 f
.write
("/* This C file is generated by NIT to compile program {module.name}. */\n")
107 for m
in module.mhe
.greaters_and_self
do
108 f
.write
("#include \"{m.name}.{get_file_ending}.h\
"\n")
116 # Compile the sep or glob files (of the current module only)
117 private fun compile_separate_module
(program
: Program)
120 var v
= new CompilerVisitor(self, program
)
121 v
.add_decl
("#include <nit_common.h>")
122 var native_name
= location
.file
.strip_extension
(".nit")
123 native_name
+= ("_nit.h")
124 if native_name
.file_exists
then v
.add_decl
("#include <{native_name.basename("")}>")
125 declare_class_tables_to_c
(v
)
127 var f
= new OFStream.open
("{tc.compdir}/{name}.{program.get_file_ending}.h")
128 f
.write
("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
129 f
.write
("#ifndef {name}{program.get_file_ending}\n")
130 f
.write
("#define {name}{program.get_file_ending}\n")
131 for m
in mhe
.direct_greaters
do f
.write
("#include \"{m.name}.{program.get_file_ending}.h\
"\n")
132 for s
in v
.ctx
.decls
do
138 f
= new OFStream.open
("{tc.compdir}/{name}.{program.get_file_ending}.c")
139 f
.write
("/* This C file is generated by NIT to compile module {name}. */\n")
140 f
.write
("#include \"{name}.{program.get_file_ending}.h\
"\n")
141 for s
in v
.ctx
.instrs
do