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
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 fun compile_prog_to_c
(tc
: ToolContext)
33 var files
= new Array[String]
34 var includes
= new ArraySet[String]
35 files
.add
("$CLIBDIR/nit_main.c")
36 files
.add
("$CLIBDIR/gc.c")
37 files
.add
("$CLIBDIR/gc_static_objects_list.c")
38 tc
.info
("Generating C code",1)
39 for m
in module.mhe
.greaters_and_self
do
40 files
.add
("{tc.compdir}/{m.name}._sep.c")
41 tc
.info
("Generating C code for module: {m.name}",2)
42 m
.compile_separate_module
(tc
, self)
43 var native_name
= m
.location
.file
.strip_extension
(".nit")
44 if (native_name
+ "_nit.h").file_exists
then
45 includes
.add
("-I {native_name.dirname}")
47 native_name
+= "_nit.c"
48 if native_name
.file_exists
then files
.add
(native_name
)
51 tc
.info
("Generating main, tables and makefile ...",1)
52 files
.add
("{tc.compdir}/{module.name}._tables.c")
55 var fn
= "{tc.compdir}/{module.name}._build.sh"
56 var f
= new OFStream.open
(fn
)
59 if tc
.verbose_level
> 0 then
61 for i
in [1..tc
.verbose_level
] do verbose
= verbose
+ "v"
64 f
.write
("#!/bin/sh\n")
65 f
.write
("# This shell script is generated by NIT to compile the program {module.name}.\n")
66 f
.write
("CLIBDIR=\"{tc.clibdir}\
"\n")
67 f
.write
("{tc.bindir}/gccx {verbose} -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
68 if tc
.output_file
!= null then
69 f
.write
(" -o {tc.output_file}")
70 else if tc
.ext_prefix
.is_empty
then
71 f
.write
(" -o {module.name}")
73 f
.write
(" -o {module.name}_{tc.ext_prefix}")
75 if tc
.boost
then f
.write
(" -O")
76 f
.write
(" \"$@\
" \\\n {files.join("\\\n ")}\n")
85 # Compile the main file
86 private fun compile_main
(tc
: ToolContext)
88 var v
= new GlobalCompilerVisitor(module, tc
, self)
89 v
.add_decl
("#include <nit_common.h>")
90 compile_tables_to_c
(v
)
92 var f
= new OFStream.open
("{tc.compdir}/{module.name}._tables.c")
93 f
.write
("/* This C file is generated by NIT to compile program {module.name}. */\n")
94 for m
in module.mhe
.greaters_and_self
do
95 f
.write
("#include \"{m.name}._sep
.h\
"\n")
103 # Compile the sep files (of the current module only)
104 private fun compile_separate_module
(tc
: ToolContext, program
: Program)
106 var v
= new GlobalCompilerVisitor(self, tc
, program
)
107 v
.add_decl
("#include <nit_common.h>")
108 var native_name
= location
.file
.strip_extension
(".nit")
109 native_name
+= ("_nit.h")
110 if native_name
.file_exists
then v
.add_decl
("#include <{native_name.basename("")}>")
111 declare_class_tables_to_c
(v
)
113 var f
= new OFStream.open
("{tc.compdir}/{name}._sep.h")
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}_sep\n")
116 f
.write
("#define {name}_sep\n")
117 for m
in mhe
.direct_greaters
do f
.write
("#include \"{m.name}._sep
.h\
"\n")
118 for s
in v
.ctx
.decls
do
124 f
= new OFStream.open
("{tc.compdir}/{name}._sep.c")
125 f
.write
("/* This C file is generated by NIT to compile module {name}. */\n")
126 f
.write
("#include \"{name}._sep
.h\
"\n")
127 for s
in v
.ctx
.instrs
do