Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit cef6f33

Browse files
committed
nitj: introduce compiler to Java code
This commit only introduce a stub and a way to test it. Code generation will be added in next commits. Signed-off-by: Alexandre Terrasa <[email protected]>
1 parent 353444d commit cef6f33

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

src/compiler/java_compiler.nit

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This file is part of NIT ( http://www.nitlanguage.org ).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Compile Nit code to Java code
16+
#
17+
# 3 runtime structures are used to represent Nit instance in Java generated code:
18+
# * `RTClass` to represent a class, it's super-type table and its VFT
19+
# * `RTMethod` to reprensent a compiled method definition
20+
# * `RTVal` to reprensent a Nit instance, the null value or a native value
21+
#
22+
# More details are given in the documentation of these 3 classes.
23+
#
24+
# TODO Factorize with `abstract_compiler`
25+
module java_compiler
26+
27+
import rapid_type_analysis
28+
import frontend
29+
30+
redef class ToolContext
31+
32+
# Where to output the generated binary
33+
var opt_output = new OptionString("Output file", "-o", "--output")
34+
35+
# Where to output tmp files
36+
var opt_compile_dir = new OptionString("Directory used to generate temporary files", "--compile-dir")
37+
38+
redef init do
39+
super
40+
option_context.add_option(opt_output, opt_compile_dir)
41+
end
42+
end
43+
44+
redef class ModelBuilder
45+
46+
# Start the Java compiler
47+
fun run_java_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis) do
48+
var time0 = get_time
49+
toolcontext.info("*** GENERATING JAVA ***", 1)
50+
51+
toolcontext.info("NOT YET IMPLEMENTED", 0)
52+
53+
var time1 = get_time
54+
toolcontext.info("*** END GENERATING JAVA: {time1-time0} ***", 2)
55+
end
56+
end

src/nitj.nit

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This file is part of NIT ( http://www.nitlanguage.org ).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Compile Nit into Java code runnable on the Java Virtual Machine.
16+
module nitj
17+
18+
import compiler::java_compiler
19+
20+
# Create a tool context to handle options and paths
21+
var toolcontext = new ToolContext
22+
toolcontext.process_options(args)
23+
24+
# We need a model to collect stufs
25+
var model = new Model
26+
# And a model builder to parse files
27+
var modelbuilder = new ModelBuilder(model, toolcontext)
28+
29+
# Collect arguments
30+
var arguments = toolcontext.option_context.rest
31+
if arguments.is_empty then
32+
toolcontext.option_context.usage
33+
return
34+
end
35+
if arguments.length > 1 then
36+
print "Too much arguments: {arguments.join(" ")}"
37+
toolcontext.option_context.usage
38+
return
39+
end
40+
var progname = arguments.first
41+
42+
# Here we load an process all modules passed on the command line
43+
var mmodules = modelbuilder.parse([progname])
44+
45+
if mmodules.is_empty then return
46+
modelbuilder.run_phases
47+
48+
var mainmodule
49+
if mmodules.length == 1 then
50+
mainmodule = mmodules.first
51+
else
52+
mainmodule = new MModule(model, null, mmodules.first.name, mmodules.first.location)
53+
mainmodule.set_imported_mmodules(mmodules)
54+
end
55+
56+
var analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
57+
58+
# Do compilation
59+
modelbuilder.run_java_compiler(mainmodule, analysis)

tests/tests.sh

+6
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ case $engine in
470470
OPT="--vm $OPT"
471471
savdirs="sav/niti/"
472472
;;
473+
nitj)
474+
engine=nitj;
475+
OPT="--compile-dir $compdir"
476+
enginebinname=nitj;
477+
savdirs="sav/nitc-common/"
478+
;;
473479
emscripten)
474480
enginebinname=nitc
475481
OPT="-m emscripten_nodejs.nit --semi-global $OPT --compile-dir $compdir"

0 commit comments

Comments
 (0)