compile: cnames for classes and modules
[nit.git] / src / compiling / compiling.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Compute and generate tables for classes and modules.
18 package compiling
19
20 import table_computation
21 import compiling_base
22 import icode_generator
23 private import compiling_global
24 private import compiling_icode
25
26 redef class Program
27 # The type of code generation to use
28 readable writable var _output_format: String = "none"
29
30 # Compile the program depending on the output format
31 fun compile_prog
32 do
33 if output_format == "none" then
34 # Nothing to do
35 else
36 # Optimize all iroutines
37 with_each_iroutines !action(i, m) do i.optimize(m)
38
39 if output_format == "C" then
40 compile_prog_to_c
41 else if output_format == "icode" then
42 generate_icode_files
43 end
44 end
45 end
46
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
50 fun compile_prog_to_c
51 do
52 var cprogram = new CProgram(self)
53
54 cprogram.compdir.mkdir
55
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")
59
60 tc.info("Generating C code",1)
61 for m in main_module.mhe.greaters_and_self do m.compile_separate_module(cprogram)
62
63 tc.info("Generating main, tables and makefile ...",1)
64 compile_main(cprogram)
65
66 cprogram.generate_build_file
67
68 if not tc.no_cc then cprogram.run_c_compiler
69 end
70
71 # Compile the main file
72 private fun compile_main(cprogram: CProgram)
73 do
74 var v = new CompilerVisitor(main_module, cprogram)
75 v.add_decl("#include <nit_common.h>")
76 compile_tables_to_c(v)
77 compile_main_part(v)
78 var filename = "{cprogram.compdir}/{main_module.cname}._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.cname}. */\n")
82 for m in main_module.mhe.greaters_and_self do
83 f.write("#include \"{cprogram.module_header_name(m)}\"\n")
84 end
85 v.header_writer.write_to_stream(f)
86 v.writer.write_to_stream(f)
87 f.close
88 end
89 end
90
91 redef class MMModule
92 # Compile the sep or glob files (of the current module only)
93 private fun compile_separate_module(cprogram: CProgram)
94 do
95 var tc = cprogram.program.tc
96 tc.info("Generating C code for module: {full_name}",2)
97 var v = new CompilerVisitor(self, cprogram)
98 v.add_decl("#include <nit_common.h>")
99
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}")
105 end
106 var native_body = native_name + "_nit.c"
107 if native_body.file_exists then cprogram.files.add(native_body)
108
109 declare_class_tables_to_c(v)
110 compile_mod_to_c(v)
111
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 {full_name}. */\n")
115 f.write("#ifndef {cname}{cprogram.get_file_ending}\n")
116 f.write("#define {cname}{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)
119 f.write("#endif\n")
120 f.close
121
122 var cfilename = "{cprogram.compdir}/{cname}.{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 {cname}. */\n")
126 f.write("#include \"{hfilename}\"\n")
127 v.top_writer.write_to_stream(f)
128 f.close
129 end
130 end
131