tools: add entrypoint information to the program
[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_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
34 readable var _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
35 readable var _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir")
36 readable var _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix")
37 readable var _opt_dump: OptionBool = new OptionBool("Dump intermediate code", "--dump")
38
39 init
40 do
41 super("nitc")
42 option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix, opt_dump)
43 end
44
45 redef fun process_options
46 do
47 super
48 output_file = opt_output.value
49 boost = opt_boost.value
50 no_cc = opt_no_cc.value
51 var ext = opt_extension_prefix.value
52 if ext != null then ext_prefix = ext else ext_prefix = ""
53 global = opt_global.value
54 compdir = opt_compdir.value
55 if compdir == null then
56 var dir = once ("NIT_COMPDIR".to_symbol).environ
57 if not dir.is_empty then
58 compdir = dir
59 end
60 if compdir == null then
61 compdir = ".nit_compile"
62 end
63 end
64 compdir += ext_prefix
65
66 clibdir = opt_clibdir.value
67 if clibdir == null then
68 var dir = once ("NIT_DIR".to_symbol).environ
69 if dir.is_empty then
70 dir = "{sys.program_name.dirname}/../clib"
71 if dir.file_exists then clibdir = dir
72 else
73 dir = "{dir}/clib"
74 if dir.file_exists then clibdir = dir
75 end
76 if clibdir == null then
77 fatal_error(null, "Error: Cannot locate NIT C library directory. Uses --clibdir or envvar NIT_DIR.")
78 end
79 end
80 bindir = opt_bindir.value
81
82 if bindir == null then
83 var dir = once ("NIT_DIR".to_symbol).environ
84 if dir.is_empty then
85 dir = "{sys.program_name.dirname}/../bin"
86 if dir.file_exists then bindir = dir
87 else
88 dir = "{dir}/bin"
89 if dir.file_exists then bindir = dir
90 end
91 if bindir == null then
92 fatal_error(null, "Error: Cannot locate NIT tools directory. Uses --bindir or envvar NIT_DIR.")
93 end
94 end
95 end
96
97 fun dump_intermediate_code(mods: Collection[MMModule])
98 do
99 for mod in mods do
100 for c in mod.local_classes do
101 if not c isa MMConcreteClass then continue
102 for p in c.local_local_properties do
103 var routine: nullable IRoutine = null
104 if p isa MMAttribute then
105 routine = p.iroutine
106 else if p isa MMMethod then
107 routine = p.iroutine
108 end
109 if routine == null then continue
110 print "**** Property {p.full_name} ****"
111 var icd = new ICodeDumper
112 routine.dump(icd)
113 print "**** OPTIMIZE {p.full_name} ****"
114 routine.optimize(mod)
115 icd = new ICodeDumper
116 routine.dump(icd)
117 end
118 end
119 end
120 end
121
122 redef fun perform_work(mods)
123 do
124 if opt_dump.value then
125 dump_intermediate_code(mods)
126 end
127 for mod in mods do
128 var p = new Program(mod)
129 p.compute_main_method
130 p.do_table_computation(self)
131 p.generate_classes_init_to_icode
132 p.compile_prog_to_c(self)
133 end
134 end
135 end
136
137 var c = new NitCompiler
138 c.exec_cmd_line