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.
21 private import compiling_methods
22 private import compiling_global
25 redef class MMSrcModule
27 # Generate all sep files (_sep.[ch]), the main file (_table.c) and the build file (_build.sh)
28 # Then execute the build.sh
29 meth compile_prog_to_c
(tc
: ToolContext)
31 for m
in mhe
.greaters_and_self
do
32 assert m
isa MMSrcModule
36 var ga
= global_analysis
(tc
)
40 var files
= new Array[String]
41 var includes
= new ArraySet[String]
42 files
.add
("$CLIBDIR/nit_main.c")
43 for m
in mhe
.greaters_and_self
do
44 assert m
isa MMSrcModule
45 files
.add
("{tc.base_dir}/{m.name}.{tc.ext_prefix}_sep.c")
46 m
.compile_separate_module
(tc
, ga
)
47 var native_name
= m
.filename
.strip_extension
(".nit")
48 if (native_name
+ "_nit.h").file_exists
then
49 includes
.add
("-I {native_name.dirname}")
51 native_name
.append
("_nit.c")
52 if native_name
.file_exists
then files
.add
(native_name
)
55 files
.add
("{tc.base_dir}/{name}.{tc.ext_prefix}_tables.c")
58 var fn
= "{tc.base_dir}/{name}.{tc.ext_prefix}_build.sh"
59 var f
= new OFStream.open
(fn
)
60 f
.write
("#!/bin/sh\n")
61 f
.write
("CLIBDIR=\"{tc.clibdir}\
"\n")
62 f
.write
("{tc.bindir}/gccx -d {tc.base_dir} -I $CLIBDIR {includes.join(" ")}")
63 if tc
.output_file
!= null then
64 f
.write
(" -o {tc.output_file}")
65 else if tc
.ext_prefix
.is_empty
then
68 f
.write
(" -o {name}_{tc.ext_prefix}")
70 if tc
.boost
then f
.write
(" -O")
71 f
.write
(" \"$@\
" \\\n {files.join("\\\n ")}\n")
79 # Compile the main file
80 private meth compile_main
(tc
: ToolContext, ga
: GlobalAnalysis)
82 var v
= new CompilerVisitor(self)
84 v
.global_analysis
= ga
85 v
.add_decl
("#include <nit_common.h>")
86 compile_tables_to_c
(v
)
88 var f
= new OFStream.open
("{tc.base_dir}/{name}.{tc.ext_prefix}_tables.c")
89 for m
in mhe
.greaters_and_self
do
90 f
.write
("#include \"{m.name}.{tc.ext_prefix}_sep
.h\
"\n")
96 # Compile the sep files (of the current module only)
97 private meth compile_separate_module
(tc
: ToolContext, ga
: GlobalAnalysis)
99 var v
= new CompilerVisitor(self)
101 v
.global_analysis
= ga
102 v
.add_decl
("#include <nit_common.h>")
103 var native_name
= filename
.strip_extension
(".nit")
104 native_name
.append
("_nit.h")
105 if native_name
.file_exists
then v
.add_decl
("#include <{native_name.basename("")}>")
106 declare_class_tables_to_c
(v
)
108 var f
= new OFStream.open
("{tc.base_dir}/{name}.{tc.ext_prefix}_sep.h")
109 f
.write
("#ifndef {name}_{tc.ext_prefix}_sep\n")
110 f
.write
("#define {name}_{tc.ext_prefix}_sep\n")
111 for m
in mhe
.direct_greaters
do f
.write
("#include \"{m.name}.{tc.ext_prefix}_sep
.h\
"\n")
112 f
.write
(v
.ctx
.decls
.join
("\n"))
113 f
.write
("\n#endif\n")
115 var f
= new OFStream.open
("{tc.base_dir}/{name}.{tc.ext_prefix}_sep.c")
116 f
.write
("#include \"{name}.{tc.ext_prefix}_sep
.h\
"\n")
117 f
.write
(v
.ctx
.instrs
.join
("\n"))