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 # Nit compiler main module
23 private import compiling
26 # The main class of the nitcompiler program
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_callgraph
: OptionEnum = new OptionEnum(["none", "cha"], "The algorithm to use to build the callgraph", 1, "--global-callgraph")
36 readable var _opt_clibdir
: OptionString = new OptionString("NIT C library directory", "--clibdir")
37 readable var _opt_bindir
: OptionString = new OptionString("NIT tools directory", "--bindir")
38 readable var _opt_compdir
: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
39 readable var _opt_extension_prefix
: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
40 readable var _opt_dump
: OptionBool = new OptionBool("Dump intermediate code", "--dump")
45 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
)
48 redef fun process_options
51 output_file
= opt_output
.value
52 boost
= opt_boost
.value
53 no_cc
= opt_no_cc
.value
54 var ext
= opt_extension_prefix
.value
55 if ext
!= null then ext_prefix
= ext
else ext_prefix
= ""
56 global
= opt_global
.value
57 use_SFT_optimization
= not opt_global_no_STF_opt
.value
58 no_dead_method_removal
= opt_global_no_DMR_opt
.value
59 global_callgraph
= opt_global_callgraph
.value_name
60 compdir
= opt_compdir
.value
61 if compdir
== null then
62 var dir
= once
("NIT_COMPDIR".to_symbol
).environ
63 if not dir
.is_empty
then
66 if compdir
== null then
67 compdir
= ".nit_compile"
72 clibdir
= opt_clibdir
.value
73 if clibdir
== null then
74 var dir
= once
("NIT_DIR".to_symbol
).environ
76 dir
= "{sys.program_name.dirname}/../clib"
77 if dir
.file_exists
then clibdir
= dir
80 if dir
.file_exists
then clibdir
= dir
82 if clibdir
== null then
83 fatal_error
(null, "Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
86 bindir
= opt_bindir
.value
88 if bindir
== null then
89 var dir
= once
("NIT_DIR".to_symbol
).environ
91 dir
= "{sys.program_name.dirname}/../bin"
92 if dir
.file_exists
then bindir
= dir
95 if dir
.file_exists
then bindir
= dir
97 if bindir
== null then
98 fatal_error
(null, "Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
103 fun dump_intermediate_code
(mods
: Collection[MMModule])
106 for c
in mod
.local_classes
do
107 if not c
isa MMConcreteClass then continue
108 for p
in c
.local_local_properties
do
109 var routine
: nullable IRoutine = null
110 if p
isa MMAttribute then
112 else if p
isa MMMethod then
115 if routine
== null then continue
116 print
"**** Property {p.full_name} ****"
117 var icd
= new ICodeDumper
119 print
"**** OPTIMIZE {p.full_name} ****"
120 routine
.optimize
(mod
)
121 icd
= new ICodeDumper
128 redef fun perform_work
(mods
)
130 if opt_dump
.value
then
131 dump_intermediate_code
(mods
)
134 var p
= new Program(mod
, self)
135 p
.compute_main_method
136 p
.generate_allocation_iroutines
139 p
.do_global_optimizations
141 p
.do_table_computation
147 var c
= new NitCompiler