analysis: inline getters/setters before analysis
[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_callgraph: OptionEnum = new OptionEnum(["none", "cha", "rta"], "The algorithm to use to build the callgraph", 2, "--global-callgraph")
37 readable var _opt_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
38 readable var _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
39 readable var _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
40 readable var _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
41 readable var _opt_dump: OptionBool = new OptionBool("Dump intermediate code", "--dump")
42
43 init
44 do
45 super("nitc")
46 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)
47 end
48
49 redef fun process_options
50 do
51 super
52 output_file = opt_output.value
53 boost = opt_boost.value
54 no_cc = opt_no_cc.value
55 var ext = opt_extension_prefix.value
56 if ext != null then ext_prefix = ext else ext_prefix = ""
57 global = opt_global.value
58 use_SFT_optimization = not opt_global_no_STF_opt.value
59 no_dead_method_removal = opt_global_no_DMR_opt.value
60 no_inline_get_set = opt_global_no_inline_get_set.value
61 global_callgraph = opt_global_callgraph.value_name
62 compdir = opt_compdir.value
63 if compdir == null then
64 var dir = once ("NIT_COMPDIR".to_symbol).environ
65 if not dir.is_empty then
66 compdir = dir
67 end
68 if compdir == null then
69 compdir = ".nit_compile"
70 end
71 end
72 compdir += ext_prefix
73
74 clibdir = opt_clibdir.value
75 if clibdir == null then
76 var dir = once ("NIT_DIR".to_symbol).environ
77 if dir.is_empty then
78 dir = "{sys.program_name.dirname}/../clib"
79 if dir.file_exists then clibdir = dir
80 else
81 dir = "{dir}/clib"
82 if dir.file_exists then clibdir = dir
83 end
84 if clibdir == null then
85 fatal_error(null, "Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
86 end
87 end
88 bindir = opt_bindir.value
89
90 if bindir == null then
91 var dir = once ("NIT_DIR".to_symbol).environ
92 if dir.is_empty then
93 dir = "{sys.program_name.dirname}/../bin"
94 if dir.file_exists then bindir = dir
95 else
96 dir = "{dir}/bin"
97 if dir.file_exists then bindir = dir
98 end
99 if bindir == null then
100 fatal_error(null, "Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
101 end
102 end
103 end
104
105 fun dump_intermediate_code(mods: Collection[MMModule])
106 do
107 for mod in mods do
108 for c in mod.local_classes do
109 if not c isa MMConcreteClass then continue
110 for p in c.local_local_properties do
111 var routine: nullable IRoutine = null
112 if p isa MMAttribute then
113 routine = p.iroutine
114 else if p isa MMMethod then
115 routine = p.iroutine
116 end
117 if routine == null then continue
118 print "**** Property {p.full_name} ****"
119 var icd = new ICodeDumper
120 routine.dump(icd)
121 print "**** OPTIMIZE {p.full_name} ****"
122 routine.optimize(mod)
123 icd = new ICodeDumper
124 routine.dump(icd)
125 end
126 end
127 end
128 end
129
130 redef fun perform_work(mods)
131 do
132 if opt_dump.value then
133 dump_intermediate_code(mods)
134 end
135 for mod in mods do
136 var p = new Program(mod, self)
137 p.compute_main_method
138 p.generate_allocation_iroutines
139 if global then
140 p.do_global_pre_analysis_optimizations
141 p.do_global_analysis
142 p.do_global_post_analysis_optimizations
143 if opt_log.value then
144 p.dump_global_analysis_information(log_directory)
145 end
146 end
147 p.do_table_computation
148 p.compile_prog_to_c
149 end
150 end
151 end
152
153 var c = new NitCompiler
154 c.exec_cmd_line