compile: add a way to choose generated output format
[nit.git] / src / nitc.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 # Nit compiler main module
18 package nitc
19
20 import abstracttool
21 import analysis
22 import program
23 private import compiling
24 private import syntax
25
26 # The main class of the nitcompiler program
27 class NitCompiler
28 special AbstractCompiler
29 readable var _opt_output: OptionString = new OptionString("Output file", "-o", "--output")
30 readable var _opt_boost: OptionBool = new OptionBool("Optimize compilation", "-O", "--boost")
31 readable var _opt_no_cc: OptionBool = new OptionBool("Do not invoke C compiler", "--no_cc")
32 readable var _opt_global: OptionBool = new OptionBool("Use global compilation", "--global")
33 readable var _opt_global_no_STF_opt: OptionBool = new OptionBool("Do not use SFT optimization", "--no-global-SFT-optimization")
34 readable var _opt_global_no_DMR_opt: OptionBool = new OptionBool("Do not use dead method removal optimization", "--no-global-DMR-optimization")
35 readable var _opt_global_no_inline_get_set: OptionBool = new OptionBool("Do not automatically inline getters/setters", "--no-global-get-set-inlining")
36 readable var _opt_global_no_out_of_init_get_test_opt: OptionBool = new OptionBool("Do not remove get tests outside object initialization", "--no-global-OOIT-optimization")
37 readable var _opt_global_no_RFIMA: OptionBool = new OptionBool("Do not use a specialized algorithm to find reachable methods from initializers", "--no-global-RFIM-analysis")
38 readable var _opt_global_callgraph: OptionEnum = new OptionEnum(["none", "cha", "rta"], "The algorithm to use to build the callgraph", 2, "--global-callgraph")
39 readable var _opt_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
40 readable var _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
41 readable var _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
42 readable var _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
43 readable var _opt_dump: OptionBool = new OptionBool("Dump intermediate code", "--dump")
44 readable var _opt_output_format: OptionEnum = new OptionEnum(["none", "C"], "The type of code we want to be generated", 1, "--output-format")
45
46 init
47 do
48 super("nitc")
49 option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix, opt_dump, opt_global_no_STF_opt, opt_global_no_DMR_opt, opt_global_callgraph, opt_global_no_inline_get_set, opt_global_no_RFIMA, opt_global_no_out_of_init_get_test_opt, opt_output_format)
50 end
51
52 redef fun process_options
53 do
54 super
55 output_file = opt_output.value
56 boost = opt_boost.value
57 no_cc = opt_no_cc.value
58 var ext = opt_extension_prefix.value
59 if ext != null then ext_prefix = ext else ext_prefix = ""
60 global = opt_global.value
61 use_SFT_optimization = not opt_global_no_STF_opt.value
62 no_dead_method_removal = opt_global_no_DMR_opt.value
63 no_inline_get_set = opt_global_no_inline_get_set.value
64 no_callgraph_from_init = opt_global_no_RFIMA.value
65 no_out_of_init_get_test_removal = opt_global_no_out_of_init_get_test_opt.value
66 global_callgraph = opt_global_callgraph.value_name
67 compdir = opt_compdir.value
68 if compdir == null then
69 var dir = once ("NIT_COMPDIR".to_symbol).environ
70 if not dir.is_empty then
71 compdir = dir
72 end
73 if compdir == null then
74 compdir = ".nit_compile"
75 end
76 end
77 compdir += ext_prefix
78
79 clibdir = opt_clibdir.value
80 if clibdir == null then
81 var dir = once ("NIT_DIR".to_symbol).environ
82 if dir.is_empty then
83 dir = "{sys.program_name.dirname}/../clib"
84 if dir.file_exists then clibdir = dir
85 else
86 dir = "{dir}/clib"
87 if dir.file_exists then clibdir = dir
88 end
89 if clibdir == null then
90 fatal_error(null, "Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
91 end
92 end
93 bindir = opt_bindir.value
94
95 if bindir == null then
96 var dir = once ("NIT_DIR".to_symbol).environ
97 if dir.is_empty then
98 dir = "{sys.program_name.dirname}/../bin"
99 if dir.file_exists then bindir = dir
100 else
101 dir = "{dir}/bin"
102 if dir.file_exists then bindir = dir
103 end
104 if bindir == null then
105 fatal_error(null, "Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
106 end
107 end
108 end
109
110 fun dump_intermediate_code(mods: Collection[MMModule])
111 do
112 for mod in mods do
113 for c in mod.local_classes do
114 if not c isa MMConcreteClass then continue
115 for p in c.local_local_properties do
116 var routine: nullable IRoutine = null
117 if p isa MMAttribute then
118 routine = p.iroutine
119 else if p isa MMMethod then
120 routine = p.iroutine
121 end
122 if routine == null then continue
123 print "**** Property {p.full_name} ****"
124 var icd = new ICodeDumper
125 routine.dump(icd)
126 print "**** OPTIMIZE {p.full_name} ****"
127 routine.optimize(mod)
128 icd = new ICodeDumper
129 routine.dump(icd)
130 end
131 end
132 end
133 end
134
135 redef fun perform_work(mods)
136 do
137 if opt_dump.value then
138 dump_intermediate_code(mods)
139 end
140 for mod in mods do
141 var p = new Program(mod, self)
142 p.output_format = opt_output_format.value_name
143 p.compute_main_method
144 p.generate_allocation_iroutines
145 if global then
146 p.do_global_pre_analysis_optimizations
147 p.do_global_analysis
148 p.do_global_post_analysis_optimizations
149 if opt_log.value then
150 p.dump_global_optimizations_information(log_directory)
151 p.dump_global_analysis_information(log_directory)
152 end
153 end
154 p.do_table_computation
155 p.compile_prog
156 end
157 end
158 end
159
160 var c = new NitCompiler
161 c.exec_cmd_line