1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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.
18 module abstract_compiler
24 private import annotation
27 # Add compiling options
28 redef class ToolContext
30 var opt_output
= new OptionString("Output file", "-o", "--output")
32 var opt_dir
= new OptionString("Output directory", "--dir")
34 var opt_no_cc
= new OptionBool("Do not invoke C compiler", "--no-cc")
36 var opt_no_main
= new OptionBool("Do not generate main entry point", "--no-main")
38 var opt_cc_path
= new OptionArray("Set include path for C header files (may be used more than once)", "--cc-path")
40 var opt_make_flags
= new OptionString("Additional options to make", "--make-flags")
42 var opt_compile_dir
= new OptionString("Directory used to generate temporary files", "--compile-dir")
44 var opt_hardening
= new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
45 # --no-check-covariance
46 var opt_no_check_covariance
= new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
47 # --no-check-attr-isset
48 var opt_no_check_attr_isset
= new OptionBool("Disable isset tests before each attribute access (dangerous)", "--no-check-attr-isset")
50 var opt_no_check_assert
= new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
52 var opt_no_check_autocast
= new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
54 var opt_no_check_null
= new OptionBool("Disable tests of null receiver (dangerous)", "--no-check-null")
56 var opt_no_check_all
= new OptionBool("Disable all tests (dangerous)", "--no-check-all")
57 # --typing-test-metrics
58 var opt_typing_test_metrics
= new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
59 # --invocation-metrics
60 var opt_invocation_metrics
= new OptionBool("Enable static and dynamic count of all method invocations", "--invocation-metrics")
61 # --isset-checks-metrics
62 var opt_isset_checks_metrics
= new OptionBool("Enable static and dynamic count of isset checks before attributes access", "--isset-checks-metrics")
64 var opt_stacktrace
= new OptionString("Control the generation of stack traces", "--stacktrace")
66 var opt_no_gcc_directive
= new OptionArray("Disable a advanced gcc directives for optimization", "--no-gcc-directive")
68 var opt_release
= new OptionBool("Compile in release mode and finalize application", "--release")
73 self.option_context
.add_option
(self.opt_output
, self.opt_dir
, self.opt_no_cc
, self.opt_no_main
, self.opt_make_flags
, self.opt_compile_dir
, self.opt_hardening
)
74 self.option_context
.add_option
(self.opt_no_check_covariance
, self.opt_no_check_attr_isset
, self.opt_no_check_assert
, self.opt_no_check_autocast
, self.opt_no_check_null
, self.opt_no_check_all
)
75 self.option_context
.add_option
(self.opt_typing_test_metrics
, self.opt_invocation_metrics
, self.opt_isset_checks_metrics
)
76 self.option_context
.add_option
(self.opt_stacktrace
)
77 self.option_context
.add_option
(self.opt_no_gcc_directive
)
78 self.option_context
.add_option
(self.opt_release
)
81 redef fun process_options
(args
)
85 var st
= opt_stacktrace
.value
86 if st
== "none" or st
== "libunwind" or st
== "nitstack" then
88 else if st
== "auto" or st
== null then
90 opt_stacktrace
.value
= "nitstack"
92 print
"Error: unknown value `{st}` for --stacktrace. Use `none`, `libunwind`, `nitstack` or `auto`."
96 if opt_output
.value
!= null and opt_dir
.value
!= null then
97 print
"Error: cannot use both --dir and --output"
101 if opt_no_check_all
.value
then
102 opt_no_check_covariance
.value
= true
103 opt_no_check_attr_isset
.value
= true
104 opt_no_check_assert
.value
= true
105 opt_no_check_autocast
.value
= true
106 opt_no_check_null
.value
= true
111 redef class ModelBuilder
112 # The compilation directory
113 var compile_dir
: String
115 # Simple indirection to `Toolchain::write_and_make`
116 protected fun write_and_make
(compiler
: AbstractCompiler)
118 var platform
= compiler
.mainmodule
.target_platform
120 if platform
== null then
121 toolchain
= new MakefileToolchain(toolcontext
)
123 toolchain
= platform
.toolchain
(toolcontext
)
125 compile_dir
= toolchain
.compile_dir
126 toolchain
.write_and_make compiler
131 fun toolchain
(toolcontext
: ToolContext): Toolchain is abstract
135 var toolcontext
: ToolContext
137 fun compile_dir
: String
139 var compile_dir
= toolcontext
.opt_compile_dir
.value
140 if compile_dir
== null then compile_dir
= ".nit_compile"
144 fun write_and_make
(compiler
: AbstractCompiler) is abstract
147 class MakefileToolchain
149 # The list of directories to search for included C headers (-I for C compilers)
150 # The list is initially set with :
151 # * the toolcontext --cc-path option
152 # * the NIT_CC_PATH environment variable
153 # * `toolcontext.nit_dir`
154 # Path can be added (or removed) by the client
155 var cc_paths
= new Array[String]
157 protected fun gather_cc_paths
159 # Look for the the Nit clib path
160 var path_env
= toolcontext
.nit_dir
161 if path_env
!= null then
162 var libname
= "{path_env}/clib"
163 if libname
.file_exists
then cc_paths
.add
(libname
)
166 if cc_paths
.is_empty
then
167 toolcontext
.error
(null, "Cannot determine the nit clib path. define envvar NIT_DIR.")
170 # Add user defined cc_paths
171 cc_paths
.append
(toolcontext
.opt_cc_path
.value
)
173 path_env
= "NIT_CC_PATH".environ
174 if not path_env
.is_empty
then
175 cc_paths
.append
(path_env
.split_with
(':'))
179 redef fun write_and_make
(compiler
)
183 var compile_dir
= compile_dir
185 # Generate the .h and .c files
186 # A single C file regroups many compiled rumtime functions
187 # Note that we do not try to be clever an a small change in a Nit source file may change the content of all the generated .c files
189 self.toolcontext
.info
("*** WRITING C ***", 1)
193 var cfiles
= new Array[String]
194 write_files
(compiler
, compile_dir
, cfiles
)
196 # Generate the Makefile
198 write_makefile
(compiler
, compile_dir
, cfiles
)
201 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
203 # Execute the Makefile
205 if self.toolcontext
.opt_no_cc
.value
then return
208 self.toolcontext
.info
("*** COMPILING C ***", 1)
210 compile_c_code
(compiler
, compile_dir
)
213 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
216 fun write_files
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
218 var platform
= compiler
.mainmodule
.target_platform
219 if self.toolcontext
.opt_stacktrace
.value
== "nitstack" and (platform
== null or platform
.supports_libunwind
) then compiler
.build_c_to_nit_bindings
220 var cc_opt_with_libgc
= "-DWITH_LIBGC"
221 if platform
!= null and not platform
.supports_libgc
then cc_opt_with_libgc
= ""
223 # Add gc_choser.h to aditionnal bodies
224 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
225 compiler
.extern_bodies
.add
(gc_chooser
)
226 compiler
.files_to_copy
.add
"{cc_paths.first}/gc_chooser.c"
227 compiler
.files_to_copy
.add
"{cc_paths.first}/gc_chooser.h"
230 for m
in compiler
.mainmodule
.in_importation
.greaters
do
231 compiler
.finalize_ffi_for_module
(m
)
234 # Copy original .[ch] files to compile_dir
235 for src
in compiler
.files_to_copy
do
236 var basename
= src
.basename
("")
237 var dst
= "{compile_dir}/{basename}"
241 var hfilename
= compiler
.header
.file
.name
+ ".h"
242 var hfilepath
= "{compile_dir}/{hfilename}"
243 var h
= new OFStream.open
(hfilepath
)
244 for l
in compiler
.header
.decl_lines
do
248 for l
in compiler
.header
.lines
do
254 for f
in compiler
.files
do
256 var hfile
: nullable OFStream = null
258 var cfilename
= "{f.name}.0.h"
259 var cfilepath
= "{compile_dir}/{cfilename}"
260 hfile
= new OFStream.open
(cfilepath
)
261 hfile
.write
"#include \"{hfilename}\
"\n"
262 for key
in f
.required_declarations
do
263 if not compiler
.provided_declarations
.has_key
(key
) then
264 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
266 node
.debug
"No provided declaration for {key}"
268 print
"No provided declaration for {key}"
272 hfile
.write compiler
.provided_declarations
[key
]
276 var file
: nullable OFStream = null
277 for vis
in f
.writers
do
278 if vis
== compiler
.header
then continue
279 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
280 if total_lines
== 0 then continue
282 if file
== null or count
> 10000 then
284 if file
!= null then file
.close
285 cfilename
= "{f.name}.{i}.c"
286 cfilepath
= "{compile_dir}/{cfilename}"
287 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
288 cfiles
.add
(cfilename
)
289 file
= new OFStream.open
(cfilepath
)
290 file
.write
"#include \"{f.name}.0.h\
"\n"
293 for l
in vis
.decl_lines
do
297 for l
in vis
.lines
do
302 if file
!= null then file
.close
305 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
308 fun makefile_name
(mainmodule
: MModule): String do return "{mainmodule.name}.mk"
310 fun default_outname
(mainmodule
: MModule): String
312 # Search a non fictive module
313 var res
= mainmodule
.name
314 while mainmodule
.is_fictive
do
315 mainmodule
= mainmodule
.in_importation
.direct_greaters
.first
316 res
= mainmodule
.name
321 # Combine options and platform informations to get the final path of the outfile
322 fun outfile
(mainmodule
: MModule): String
324 var res
= self.toolcontext
.opt_output
.value
325 if res
!= null then return res
326 res
= default_outname
(mainmodule
)
327 var dir
= self.toolcontext
.opt_dir
.value
328 if dir
!= null then return dir
.join_path
(res
)
332 fun write_makefile
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
334 var mainmodule
= compiler
.mainmodule
335 var platform
= compiler
.mainmodule
.target_platform
337 var outname
= outfile
(mainmodule
)
339 var orig_dir
= compile_dir
.relpath
(".")
340 var outpath
= orig_dir
.join_path
(outname
).simplify_path
341 var makename
= makefile_name
(mainmodule
)
342 var makepath
= "{compile_dir}/{makename}"
343 var makefile
= new OFStream.open
(makepath
)
347 cc_includes
+= " -I \"" + p + "\
""
350 var linker_options
= new HashSet[String]
351 for m
in mainmodule
.in_importation
.greaters
do
352 var libs
= m
.collect_linker_libs
353 if libs
!= null then linker_options
.add_all
(libs
)
356 makefile
.write
("CC = ccache cc\nCXX = ccache c++\nCFLAGS = -g -O2 -Wno-unused-value -Wno-switch\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc {linker_options.join(" ")}\n\n")
358 var ost
= toolcontext
.opt_stacktrace
.value
359 if (ost
== "libunwind" or ost
== "nitstack") and (platform
== null or platform
.supports_libunwind
) then makefile
.write
("NEED_LIBUNWIND := YesPlease\n")
361 # Dynamic adaptations
362 # While `platform` enable complex toolchains, they are statically applied
363 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
365 # Check and adapt the targeted system
366 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
367 makefile
.write
("ifeq ($(uname_S),Darwin)\n")
368 # remove -lunwind since it is already included on macosx
369 makefile
.write
("\tNEED_LIBUNWIND :=\n")
370 makefile
.write
("endif\n\n")
372 # Check and adapt for the compiler used
373 # clang need an additionnal `-Qunused-arguments`
374 makefile
.write
("clang_check := $(shell sh -c '$(CC) -v 2>&1 | grep -q clang; echo $$?')\nifeq ($(clang_check), 0)\n\tCFLAGS += -Qunused-arguments\nendif\n")
376 makefile
.write
("ifdef NEED_LIBUNWIND\n\tLDLIBS += -lunwind\nendif\n")
378 makefile
.write
("all: {outpath}\n\n")
380 var ofiles
= new Array[String]
381 var dep_rules
= new Array[String]
382 # Compile each generated file
384 var o
= f
.strip_extension
(".c") + ".o"
385 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
390 var java_files
= new Array[ExternFile]
392 # Compile each required extern body into a specific .o
393 for f
in compiler
.extern_bodies
do
394 var o
= f
.makefile_rule_name
395 var ff
= f
.filename
.basename
("")
396 makefile
.write
("{o}: {ff}\n")
397 makefile
.write
("\t{f.makefile_rule_content}\n\n")
398 dep_rules
.add
(f
.makefile_rule_name
)
400 if f
.compiles_to_o_file
then ofiles
.add
(o
)
401 if f
.add_to_jar
then java_files
.add
(f
)
404 if not java_files
.is_empty
then
405 var jar_file
= "{outpath}.jar"
407 var class_files_array
= new Array[String]
408 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
409 var class_files
= class_files_array
.join
(" ")
411 makefile
.write
("{jar_file}: {class_files}\n")
412 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
413 dep_rules
.add jar_file
417 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath} {ofiles.join(" ")} $(LDLIBS)\n\n")
419 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
421 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
424 fun compile_c_code
(compiler
: AbstractCompiler, compile_dir
: String)
426 var makename
= makefile_name
(compiler
.mainmodule
)
428 var makeflags
= self.toolcontext
.opt_make_flags
.value
429 if makeflags
== null then makeflags
= ""
430 self.toolcontext
.info
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags}", 2)
433 if self.toolcontext
.verbose_level
>= 3 then
434 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1")
436 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
439 toolcontext
.error
(null, "make failed! Error code: {res}.")
444 # Singleton that store the knowledge about the compilation process
445 abstract class AbstractCompiler
446 type VISITOR: AbstractCompilerVisitor
448 # Table corresponding c_names to nit names (methods)
449 var names
= new HashMap[String, String]
451 # The main module of the program currently compiled
452 # Is assigned during the separate compilation
453 var mainmodule
: MModule is writable
455 # The real main module of the program
456 var realmainmodule
: MModule
458 # The modelbuilder used to know the model and the AST
459 var modelbuilder
: ModelBuilder is protected writable
461 # Is hardening asked? (see --hardening)
462 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
464 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
466 self.mainmodule
= mainmodule
467 self.realmainmodule
= mainmodule
468 self.modelbuilder
= modelbuilder
471 # Force the creation of a new file
472 # The point is to avoid contamination between must-be-compiled-separately files
473 fun new_file
(name
: String): CodeFile
475 var f
= new CodeFile(name
)
480 # The list of all associated files
481 # Used to generate .c files
482 var files
= new List[CodeFile]
484 # Initialize a visitor specific for a compiler engine
485 fun new_visitor
: VISITOR is abstract
487 # Where global declaration are stored (the main .h)
488 var header
: CodeWriter is writable
490 # Provide a declaration that can be requested (before or latter) by a visitor
491 fun provide_declaration
(key
: String, s
: String)
493 if self.provided_declarations
.has_key
(key
) then
494 assert self.provided_declarations
[key
] == s
496 self.provided_declarations
[key
] = s
499 private var provided_declarations
= new HashMap[String, String]
501 private var requirers_of_declarations
= new HashMap[String, ANode]
503 # Builds the .c and .h files to be used when generating a Stack Trace
504 # Binds the generated C function names to Nit function names
505 fun build_c_to_nit_bindings
507 var compile_dir
= modelbuilder
.compile_dir
509 var stream
= new OFStream.open
("{compile_dir}/c_functions_hash.c")
510 stream
.write
("#include <string.h>\n")
511 stream
.write
("#include <stdlib.h>\n")
512 stream
.write
("#include \"c_functions_hash
.h\
"\n")
513 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
514 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
515 stream
.write
("char* procname = malloc(len+1);")
516 stream
.write
("memcpy(procname, procproc, len);")
517 stream
.write
("procname[len] = '\\0';")
518 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
519 for i
in names
.keys
do
522 stream.write("\
",\"")
523 stream.write(names[i])
524 stream.write("\
"\},\n")
526 stream
.write
("\};\n")
527 stream
.write
("int i;")
528 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
529 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
530 stream
.write
("free(procname);")
531 stream
.write
("return map[i].nit_name;")
534 stream
.write
("free(procname);")
535 stream
.write
("return NULL;")
539 stream
= new OFStream.open
("{compile_dir}/c_functions_hash.h")
540 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
543 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
547 # This method call compile_header_strucs method that has to be refined
548 fun compile_header
do
549 self.header
.add_decl
("#include <stdlib.h>")
550 self.header
.add_decl
("#include <stdio.h>")
551 self.header
.add_decl
("#include <string.h>")
552 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
553 self.header
.add_decl
("#ifdef ANDROID")
554 self.header
.add_decl
(" #include <android/log.h>")
555 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
556 self.header
.add_decl
("#else")
557 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
558 self.header
.add_decl
("#endif")
560 compile_header_structs
561 compile_nitni_structs
563 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
564 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
565 # Signal handler function prototype
566 self.header
.add_decl
("void show_backtrace(int);")
568 self.header
.add_decl
("void show_backtrace(int) __attribute__ ((noreturn));")
571 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
572 self.header
.add_decl
("#define likely(x) (x)")
573 self.header
.add_decl
("#define unlikely(x) (x)")
574 else if gccd_disable
.has
("correct-likely") then
575 # invert the `likely` definition
576 # Used by masochists to bench the worst case
577 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
578 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
580 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
581 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
584 # Global variable used by intern methods
585 self.header
.add_decl
("extern int glob_argc;")
586 self.header
.add_decl
("extern char **glob_argv;")
587 self.header
.add_decl
("extern val *glob_sys;")
590 # Declaration of structures for live Nit types
591 protected fun compile_header_structs
is abstract
593 # Declaration of structures for nitni undelying the FFI
594 protected fun compile_nitni_structs
596 self.header
.add_decl
"""
597 /* Native reference to Nit objects */
598 /* This structure is used to represent every Nit type in extern methods and custom C code. */
600 struct nitni_ref *next,
601 *prev; /* adjacent global references in global list */
602 int count; /* number of time this global reference has been marked */
605 /* List of global references from C code to Nit objects */
606 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
607 struct nitni_global_ref_list_t {
608 struct nitni_ref *head, *tail;
610 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
612 /* Initializer of global reference list */
613 extern void nitni_global_ref_list_init();
615 /* Intern function to add a global reference to the list */
616 extern void nitni_global_ref_add( struct nitni_ref *ref );
618 /* Intern function to remove a global reference from the list */
619 extern void nitni_global_ref_remove( struct nitni_ref *ref );
621 /* Increase count on an existing global reference */
622 extern void nitni_global_ref_incr( struct nitni_ref *ref );
624 /* Decrease count on an existing global reference */
625 extern void nitni_global_ref_decr( struct nitni_ref *ref );
629 fun compile_finalizer_function
631 var finalizable_type
= mainmodule
.finalizable_type
632 if finalizable_type
== null then return
634 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
636 if finalize_meth
== null then
637 modelbuilder
.toolcontext
.error
(null, "The `Finalizable` class doesn't declare the `finalize` method.")
641 var v
= self.new_visitor
642 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
643 var recv
= v
.new_expr
("obj", finalizable_type
)
644 v
.send
(finalize_meth
, [recv
])
648 # Generate the main C function.
652 # * allocate the Sys object if it exists
653 # * call init if is exists
654 # * call main if it exists
655 fun compile_main_function
657 var v
= self.new_visitor
658 v
.add_decl
("#include <signal.h>")
659 var ost
= modelbuilder
.toolcontext
.opt_stacktrace
.value
660 var platform
= mainmodule
.target_platform
662 if platform
!= null and not platform
.supports_libunwind
then ost
= "none"
664 var no_main
= (platform
!= null and platform
.no_main
) or modelbuilder
.toolcontext
.opt_no_main
.value
666 if ost
== "nitstack" or ost
== "libunwind" then
667 v
.add_decl
("#define UNW_LOCAL_ONLY")
668 v
.add_decl
("#include <libunwind.h>")
669 if ost
== "nitstack" then
670 v
.add_decl
("#include \"c_functions_hash
.h\
"")
673 v
.add_decl
("int glob_argc;")
674 v
.add_decl
("char **glob_argv;")
675 v
.add_decl
("val *glob_sys;")
677 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
678 for tag
in count_type_test_tags
do
679 v
.add_decl
("long count_type_test_resolved_{tag};")
680 v
.add_decl
("long count_type_test_unresolved_{tag};")
681 v
.add_decl
("long count_type_test_skipped_{tag};")
682 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
683 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
684 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
688 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
689 v
.add_decl
("long count_invoke_by_tables;")
690 v
.add_decl
("long count_invoke_by_direct;")
691 v
.add_decl
("long count_invoke_by_inline;")
692 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
693 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
694 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
697 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
698 v
.add_decl
("long count_attr_reads = 0;")
699 v
.add_decl
("long count_isset_checks = 0;")
700 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
701 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
704 v
.add_decl
("void sig_handler(int signo)\{")
705 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
706 v
.add_decl
("show_backtrace(signo);")
709 v
.add_decl
("void show_backtrace (int signo) \{")
710 if ost
== "nitstack" or ost
== "libunwind" then
711 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
712 v
.add_decl
("unw_cursor_t cursor;")
713 v
.add_decl
("if(opt==NULL)\{")
714 v
.add_decl
("unw_context_t uc;")
715 v
.add_decl
("unw_word_t ip;")
716 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
717 v
.add_decl
("unw_getcontext(&uc);")
718 v
.add_decl
("unw_init_local(&cursor, &uc);")
719 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
720 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
721 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
722 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
723 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
724 if ost
== "nitstack" then
725 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
726 v
.add_decl
(" if (recv != NULL)\{")
727 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
728 v.add_decl(" \}else\{")
729 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
732 v
.add_decl
(" PRINT_ERROR(\"` %s \\n\",procname);")
735 v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
736 v.add_decl("free(procname);")
739 v.add_decl("exit(signo);")
743 v.add_decl("int nit_main(int argc, char** argv) \{")
745 v.add_decl("int main(int argc, char** argv) \{")
748 v.add("signal(SIGABRT, sig_handler);")
749 v.add("signal(SIGFPE, sig_handler);")
750 v.add("signal(SIGILL, sig_handler);")
751 v.add("signal(SIGINT, sig_handler);")
752 v.add("signal(SIGTERM, sig_handler);")
753 v.add("signal(SIGSEGV, sig_handler);")
754 v.add("signal(SIGPIPE, sig_handler);")
756 v.add("glob_argc = argc; glob_argv = argv;")
757 v.add("initialize_gc_option();")
759 v.add "initialize_nitni_global_refs();"
761 var main_type = mainmodule.sys_type
762 if main_type != null then
763 var mainmodule = v.compiler.mainmodule
764 var glob_sys = v.init_instance(main_type)
765 v.add("glob_sys = {glob_sys};")
766 var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
767 if main_init != null then
768 v.send(main_init, [glob_sys])
770 var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
771 mainmodule.try_get_primitive_method("main", main_type.mclass)
772 if main_method != null then
773 v.send(main_method, [glob_sys])
777 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
778 v.add_decl("long count_type_test_resolved_total = 0;")
779 v.add_decl("long count_type_test_unresolved_total = 0;")
780 v.add_decl("long count_type_test_skipped_total = 0;")
781 v.add_decl("long count_type_test_total_total = 0;")
782 for tag in count_type_test_tags do
783 v.add_decl("long count_type_test_total_{tag};")
784 v.add("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
785 v.add("count_type_test_resolved_total += count_type_test_resolved_{tag};")
786 v.add("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
787 v.add("count_type_test_skipped_total += count_type_test_skipped_{tag};")
788 v.add("count_type_test_total_total += count_type_test_total_{tag};")
790 v.add("printf(\"# dynamic count_type_test: total %l\\n\");")
791 v.add("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\");")
792 var tags = count_type_test_tags.to_a
795 v.add("printf(\"{tag}\");")
796 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
797 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
798 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
799 v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
803 if self.modelbuilder.toolcontext.opt_invocation_metrics.value then
804 v.add_decl("long count_invoke_total;")
805 v.add("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
806 v.add("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
807 v.add("printf(\"by table: %ld (%.2f%%)\\n\", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
808 v.add("printf(\"direct: %ld (%.2f%%)\\n\", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
809 v.add("printf(\"inlined: %ld (%.2f%%)\\n\", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
812 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
813 v.add("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
814 v.add("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
821 # Copile all C functions related to the [incr|decr]_ref features of the FFI
822 fun compile_nitni_global_ref_functions
824 var v = self.new_visitor
826 struct nitni_global_ref_list_t *nitni_global_ref_list;
827 void initialize_nitni_global_refs() {
828 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
829 nitni_global_ref_list->head = NULL;
830 nitni_global_ref_list->tail = NULL;
833 void nitni_global_ref_add( struct nitni_ref *ref ) {
834 if ( nitni_global_ref_list->head == NULL ) {
835 nitni_global_ref_list->head = ref;
838 nitni_global_ref_list->tail->next = ref;
839 ref->prev = nitni_global_ref_list->tail;
841 nitni_global_ref_list->tail = ref;
846 void nitni_global_ref_remove( struct nitni_ref *ref ) {
847 if ( ref->prev == NULL ) {
848 nitni_global_ref_list->head = ref->next;
850 ref->prev->next = ref->next;
853 if ( ref->next == NULL ) {
854 nitni_global_ref_list->tail = ref->prev;
856 ref->next->prev = ref->prev;
860 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
861 if ( ref->count == 0 ) /* not registered */
864 nitni_global_ref_add( ref );
870 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
871 if ( ref->count == 1 ) /* was last reference */
873 /* remove from list */
874 nitni_global_ref_remove( ref );
882 # List of additional files required to compile (FFI)
883 var extern_bodies = new Array[ExternFile]
885 # List of source files to copy over to the compile dir
886 var files_to_copy = new Array[String]
888 # This is used to avoid adding an extern file more than once
889 private var seen_extern = new ArraySet[String]
891 # Generate code that initialize the attributes on a new instance
892 fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
894 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
895 self.mainmodule.linearize_mclassdefs(cds)
897 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
898 var n = self.modelbuilder.mclassdef2nclassdef[cd]
899 for npropdef in n.n_propdefs do
900 if npropdef isa AAttrPropdef then
901 npropdef.init_expr(v, recv)
907 # Generate code that check if an attribute is correctly initialized
908 fun generate_check_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
910 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
911 self.mainmodule.linearize_mclassdefs(cds)
913 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
914 var n = self.modelbuilder.mclassdef2nclassdef[cd]
915 for npropdef in n.n_propdefs do
916 if npropdef isa AAttrPropdef then
917 npropdef.check_expr(v, recv)
925 var count_type_test_tags: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
926 var count_type_test_resolved: HashMap[String, Int] = init_count_type_test_tags
927 var count_type_test_unresolved: HashMap[String, Int] = init_count_type_test_tags
928 var count_type_test_skipped: HashMap[String, Int] = init_count_type_test_tags
930 protected fun init_count_type_test_tags: HashMap[String, Int]
932 var res = new HashMap[String, Int]
933 for tag in count_type_test_tags do
939 # Display stats about compilation process
943 # * type tests against resolved types (`x
isa Collection[Animal]`)
944 # * type tests against unresolved types (`x
isa Collection[E
]`)
945 # * type tests skipped
949 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
950 print "# static count_type_test"
951 print "\tresolved:\tunresolved\tskipped\ttotal"
952 var count_type_test_total = init_count_type_test_tags
953 count_type_test_resolved["total"] = 0
954 count_type_test_unresolved["total"] = 0
955 count_type_test_skipped["total"] = 0
956 count_type_test_total["total"] = 0
957 for tag in count_type_test_tags do
958 count_type_test_total[tag] = count_type_test_resolved[tag] + count_type_test_unresolved[tag] + count_type_test_skipped[tag]
959 count_type_test_resolved["total"] += count_type_test_resolved[tag]
960 count_type_test_unresolved["total"] += count_type_test_unresolved[tag]
961 count_type_test_skipped["total"] += count_type_test_skipped[tag]
962 count_type_test_total["total"] += count_type_test_total[tag]
964 var count_type_test = count_type_test_total["total"]
965 var tags = count_type_test_tags.to_a
969 printn "\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
970 printn "\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
971 printn "\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
972 printn "\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
978 fun finalize_ffi_for_module(mmodule: MModule) do mmodule.finalize_ffi(self)
981 # Avoid division by zero by returning the string "n/a"
982 fun div(a,b:Int):String
984 if b == 0 then return "n/a"
985 return ((a*10000/b).to_f / 100.0).to_precision(2)
989 # A file unit (may be more than one file if
990 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
993 var writers = new Array[CodeWriter]
994 var required_declarations = new HashSet[String]
997 # Where to store generated lines
1000 var lines: List[String] = new List[String]
1001 var decl_lines: List[String] = new List[String]
1003 # Add a line in the main part of the generated C
1004 fun add(s: String) do self.lines.add(s)
1007 # (used for local or global declaration)
1008 fun add_decl(s: String) do self.decl_lines.add(s)
1010 init(file: CodeFile)
1013 file.writers.add(self)
1017 # A visitor on the AST of property definition that generate the C code.
1018 abstract class AbstractCompilerVisitor
1020 type COMPILER: AbstractCompiler
1022 # The associated compiler
1023 var compiler: COMPILER
1025 # The current visited AST node
1026 var current_node: nullable ANode = null is writable
1028 # The current `Frame`
1029 var frame: nullable Frame is writable
1031 # Alias for self.compiler.mainmodule.object_type
1032 fun object_type: MClassType do return self.compiler.mainmodule.object_type
1034 # Alias for self.compiler.mainmodule.bool_type
1035 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
1037 var writer: CodeWriter
1039 init(compiler: COMPILER)
1041 self.compiler = compiler
1042 self.writer = new CodeWriter(compiler.files.last)
1045 # Force to get the primitive class named `name
` or abort
1046 fun get_class(name: String): MClass do return self.compiler.mainmodule.get_primitive_class(name)
1048 # Force to get the primitive property named `name
` in the instance `recv
` or abort
1049 fun get_property(name: String, recv: MType): MMethod
1051 assert recv isa MClassType
1052 return self.compiler.modelbuilder.force_get_primitive_method(self.current_node, name, recv.mclass, self.compiler.mainmodule)
1055 fun compile_callsite(callsite: CallSite, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1057 var initializers = callsite.mpropdef.initializers
1058 if not initializers.is_empty then
1059 var recv = arguments.first
1062 for p in initializers do
1063 if p isa MMethod then
1065 for x in p.intro.msignature.mparameters do
1066 args.add arguments[i]
1070 else if p isa MAttribute then
1071 self.write_attribute(p, recv, arguments[i])
1075 assert i == arguments.length
1077 return self.send(callsite.mproperty, [recv])
1080 return self.send(callsite.mproperty, arguments)
1083 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable is abstract
1085 fun calloc_array(ret_type: MType, arguments: Array[RuntimeVariable]) is abstract
1087 fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract
1089 # Evaluate `args
` as expressions in the call of `mpropdef
` on `recv
`.
1090 # This method is used to manage varargs in signatures and returns the real array
1091 # of runtime variables to use in the call.
1092 fun varargize(mpropdef: MMethodDef, recv: RuntimeVariable, args: SequenceRead[AExpr]): Array[RuntimeVariable]
1094 var msignature = mpropdef.new_msignature or else mpropdef.msignature.as(not null)
1095 var res = new Array[RuntimeVariable]
1098 if args.is_empty then return res
1100 var vararg_rank = msignature.vararg_rank
1101 var vararg_len = args.length - msignature.arity
1102 if vararg_len < 0 then vararg_len = 0
1104 for i in [0..msignature.arity[ do
1105 if i == vararg_rank then
1107 if ne isa AVarargExpr then
1108 var e = self.expr(ne.n_expr, null)
1112 var vararg = new Array[RuntimeVariable]
1113 for j in [vararg_rank..vararg_rank+vararg_len] do
1114 var e = self.expr(args[j], null)
1117 var elttype = msignature.mparameters[vararg_rank].mtype
1118 var arg = self.vararg_instance(mpropdef, recv, vararg, elttype)
1122 if i > vararg_rank then j += vararg_len
1123 var e = self.expr(args[j], null)
1132 # Anchor a type to the main module and the current receiver
1133 fun anchor(mtype: MType): MType
1135 if not mtype.need_anchor then return mtype
1136 return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
1139 fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
1141 if not mtype.need_anchor then return mtype
1142 return mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
1145 # Unsafely cast a value to a new type
1146 # ie the result share the same C variable but my have a different mcasttype
1147 # NOTE: if the adaptation is useless then `value
` is returned as it.
1148 # ENSURE: `result
.name
== value
.name
`
1149 fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
1151 mtype = self.anchor(mtype)
1152 var valmtype = value.mcasttype
1153 if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1157 if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1158 var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
1161 var res = new RuntimeVariable(value.name, valmtype, mtype)
1166 # Generate a super call from a method definition
1167 fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1169 # Adapt the arguments of a method according to targetted `MMethodDef`
1170 fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1172 # Unbox all the arguments of a method when implemented `extern` or `intern`
1173 fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1175 # Box or unbox a value to another type iff a C type conversion is needed
1176 # ENSURE: `result
.mtype
.ctype
== mtype
.ctype
`
1177 fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1179 # Box extern classes to be used in the generated code
1180 fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1182 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1183 fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1185 # Generate a polymorphic subtype test
1186 fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
1188 # Generate the code required to dynamically check if 2 objects share the same runtime type
1189 fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1191 # Generate a Nit "is" for two runtime_variables
1192 fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1196 # Generate a static call on a method definition
1197 fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1199 # Generate a polymorphic send for the method `m
` and the arguments `args
`
1200 fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1202 # Generate a monomorphic send for the method `m
`, the type `t
` and the arguments `args
`
1203 fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1205 assert t isa MClassType
1206 var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
1207 return self.call(propdef, t, args)
1210 # Generate a monomorphic super send from the method `m
`, the type `t
` and the arguments `args
`
1211 fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1213 assert t isa MClassType
1214 m = m.lookup_next_definition(self.compiler.mainmodule, t)
1215 return self.call(m, t, args)
1218 # Attributes handling
1220 # Generate a polymorphic attribute is_set test
1221 fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1223 # Generate a polymorphic attribute read
1224 fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1226 # Generate a polymorphic attribute write
1227 fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable) is abstract
1231 # Add a check and an abort for a null receiver if needed
1232 fun check_recv_notnull(recv: RuntimeVariable)
1234 if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
1236 var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType
1238 self.add("if (unlikely({recv} == NULL)) \{")
1239 self.add_abort("Receiver is null")
1246 private var names = new HashSet[String]
1247 private var last: Int = 0
1249 # Return a new name based on `s
` and unique in the visitor
1250 fun get_name(s: String): String
1252 if not self.names.has(s) then
1256 var i = self.last + 1
1259 if not self.names.has(s2) then
1268 # Return an unique and stable identifier associated with an escapemark
1269 fun escapemark_name(e: nullable EscapeMark): String
1272 if escapemark_names.has_key(e) then return escapemark_names[e]
1274 if name == null then name = "label"
1275 name = get_name(name)
1276 escapemark_names[e] = name
1280 # Insert a C label for associated with an escapemark
1281 fun add_escape_label(e: nullable EscapeMark)
1283 if e == null then return
1284 if e.escapes.is_empty then return
1285 add("BREAK_{escapemark_name(e)}: (void)0;")
1288 private var escapemark_names = new HashMap[EscapeMark, String]
1290 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1291 # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
1292 fun class_name_string(value: RuntimeVariable): String is abstract
1294 # Variables handling
1296 protected var variables = new HashMap[Variable, RuntimeVariable]
1298 # Return the local runtime_variable associated to a Nit local variable
1299 fun variable(variable: Variable): RuntimeVariable
1301 if self.variables.has_key(variable) then
1302 return self.variables[variable]
1304 var name = self.get_name("var_{variable.name}")
1305 var mtype = variable.declared_type.as(not null)
1306 mtype = self.anchor(mtype)
1307 var res = new RuntimeVariable(name, mtype, mtype)
1308 self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1309 self.variables[variable] = res
1314 # Return a new uninitialized local runtime_variable
1315 fun new_var(mtype: MType): RuntimeVariable
1317 mtype = self.anchor(mtype)
1318 var name = self.get_name("var")
1319 var res = new RuntimeVariable(name, mtype, mtype)
1320 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1324 # The difference with `new_var
` is the C static type of the local variable
1325 fun new_var_extern(mtype: MType): RuntimeVariable
1327 mtype = self.anchor(mtype)
1328 var name = self.get_name("var")
1329 var res = new RuntimeVariable(name, mtype, mtype)
1330 self.add_decl("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1334 # Return a new uninitialized named runtime_variable
1335 fun new_named_var(mtype: MType, name: String): RuntimeVariable
1337 mtype = self.anchor(mtype)
1338 var res = new RuntimeVariable(name, mtype, mtype)
1339 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1343 # Correctly assign a left and a right value
1344 # Boxing and unboxing is performed if required
1345 fun assign(left, right: RuntimeVariable)
1347 right = self.autobox(right, left.mtype)
1348 self.add("{left} = {right};")
1351 # Generate instances
1353 # Generate a alloc-instance + init-attributes
1354 fun init_instance(mtype: MClassType): RuntimeVariable is abstract
1356 # Set a GC finalizer on `recv
`, only if `recv
` isa Finalizable
1357 fun set_finalizer(recv: RuntimeVariable)
1359 var mtype = recv.mtype
1360 var finalizable_type = compiler.mainmodule.finalizable_type
1361 if finalizable_type != null and not mtype.need_anchor and
1362 mtype.is_subtype(compiler.mainmodule, null, finalizable_type) then
1363 add "gc_register_finalizer({recv});"
1367 # Generate an integer value
1368 fun int_instance(value: Int): RuntimeVariable
1370 var res = self.new_var(self.get_class("Int").mclass_type)
1371 self.add("{res} = {value};")
1375 # Generate an integer value
1376 fun bool_instance(value: Bool): RuntimeVariable
1378 var res = self.new_var(self.get_class("Bool").mclass_type)
1380 self.add("{res} = 1;")
1382 self.add("{res} = 0;")
1387 # Generate a string value
1388 fun string_instance(string: String): RuntimeVariable
1390 var mtype = self.get_class("String").mclass_type
1391 var name = self.get_name("varonce")
1392 self.add_decl("static {mtype.ctype} {name};")
1393 var res = self.new_var(mtype)
1394 self.add("if ({name}) \{")
1395 self.add("{res} = {name};")
1396 self.add("\} else \{")
1397 var native_mtype = self.get_class("NativeString").mclass_type
1398 var nat = self.new_var(native_mtype)
1399 self.add("{nat} = \"{string.escape_to_c}\";")
1400 var length = self.int_instance(string.length)
1401 self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
1402 self.add("{name} = {res};")
1407 fun value_instance(object: Object): RuntimeVariable
1409 if object isa Int then
1410 return int_instance(object)
1411 else if object isa Bool then
1412 return bool_instance(object)
1413 else if object isa String then
1414 return string_instance(object)
1420 # Generate an array value
1421 fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1423 # Get an instance of a array for a vararg
1424 fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1428 # Add a line in the main part of the generated C
1429 fun add(s: String) do self.writer.lines.add(s)
1432 # (used for local or global declaration)
1433 fun add_decl(s: String) do self.writer.decl_lines.add(s)
1435 # Request the presence of a global declaration
1436 fun require_declaration(key: String)
1438 var reqs = self.writer.file.required_declarations
1439 if reqs.has(key) then return
1441 var node = current_node
1442 if node != null then compiler.requirers_of_declarations[key] = node
1445 # Add a declaration in the local-header
1446 # The declaration is ensured to be present once
1447 fun declare_once(s: String)
1449 self.compiler.provide_declaration(s, s)
1450 self.require_declaration(s)
1453 # look for a needed .h and .c file for a given .nit source-file
1454 # FIXME: bad API, parameter should be a `MModule`, not its source-file
1455 fun add_extern(file: String)
1457 file = file.strip_extension(".nit")
1458 var tryfile = file + ".nit.h"
1459 if tryfile.file_exists then
1460 self.declare_once("#include \"{tryfile.basename("")}\"")
1461 self.compiler.files_to_copy.add(tryfile)
1463 tryfile = file + "_nit.h"
1464 if tryfile.file_exists then
1465 self.declare_once("#include \"{tryfile.basename("")}\"")
1466 self.compiler.files_to_copy.add(tryfile)
1469 if self.compiler.seen_extern.has(file) then return
1470 self.compiler.seen_extern.add(file)
1471 tryfile = file + ".nit.c"
1472 if not tryfile.file_exists then
1473 tryfile = file + "_nit.c"
1474 if not tryfile.file_exists then return
1476 var f = new ExternCFile(tryfile.basename(""), "")
1477 self.compiler.extern_bodies.add(f)
1478 self.compiler.files_to_copy.add(tryfile)
1481 # Return a new local runtime_variable initialized with the C expression `cexpr
`.
1482 fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
1484 var res = new_var(mtype)
1485 self.add("{res} = {cexpr};")
1489 # Generate generic abort
1490 # used by aborts, asserts, casts, etc.
1491 fun add_abort(message: String)
1493 self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
1499 if self.current_node != null and self.current_node.location.file != null then
1500 self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
1502 self.add("PRINT_ERROR(\"\\n\");")
1504 self.add("show_backtrace(1);")
1507 # Add a dynamic cast
1508 fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)
1510 var res = self.type_test(value, mtype, tag)
1511 self.add("if (unlikely(!{res})) \{")
1512 var cn = self.class_name_string(value)
1513 self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s
`, got `%s
`\", \"{mtype.to_s.escape_to_c}\", {cn});")
1518 # Generate a return with the value `s
`
1519 fun ret(s: RuntimeVariable)
1521 self.assign(self.frame.returnvar.as(not null), s)
1522 self.add("goto {self.frame.returnlabel.as(not null)};")
1525 # Compile a statement (if any)
1526 fun stmt(nexpr: nullable AExpr)
1528 if nexpr == null then return
1529 var old = self.current_node
1530 self.current_node = nexpr
1532 self.current_node = old
1535 # Compile an expression an return its result
1536 # `mtype
` is the expected return type, pass null if no specific type is expected.
1537 fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
1539 var old = self.current_node
1540 self.current_node = nexpr
1541 var res = nexpr.expr(self).as(not null)
1542 if mtype != null then
1543 mtype = self.anchor(mtype)
1544 res = self.autobox(res, mtype)
1546 res = autoadapt(res, nexpr.mtype.as(not null))
1547 var implicit_cast_to = nexpr.implicit_cast_to
1548 if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
1549 add_cast(res, implicit_cast_to, "auto")
1550 res = autoadapt(res, implicit_cast_to)
1552 self.current_node = old
1556 # Alias for `self.expr
(nexpr
, self.bool_type
)`
1557 fun expr_bool(nexpr: AExpr): RuntimeVariable do return expr(nexpr, bool_type)
1559 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1560 fun debug(message: String)
1562 var node = self.current_node
1563 if node == null then
1564 print "?: {message}"
1568 self.add("/* DEBUG: {message} */")
1572 # A C function associated to a Nit method
1573 # Because of customization, a given Nit method can be compiler more that once
1574 abstract class AbstractRuntimeFunction
1576 type COMPILER: AbstractCompiler
1577 type VISITOR: AbstractCompilerVisitor
1579 # The associated Nit method
1580 var mmethoddef: MMethodDef
1582 # The mangled c name of the runtime_function
1583 # Subclasses should redefine `build_c_name
` instead
1586 var res = self.c_name_cache
1587 if res != null then return res
1588 res = self.build_c_name
1589 self.c_name_cache = res
1593 # Non cached version of `c_name
`
1594 protected fun build_c_name: String is abstract
1596 protected var c_name_cache: nullable String = null is writable
1598 # Implements a call of the runtime_function
1599 # May inline the body or generate a C function call
1600 fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1602 # Generate the code for the `AbstractRuntimeFunction`
1603 # Warning: compile more than once compilation makes CC unhappy
1604 fun compile_to_c(compiler: COMPILER) is abstract
1607 # A runtime variable hold a runtime value in C.
1608 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
1610 # The tricky point is that a single C variable can be associated to more than one `RuntimeVariable` because the static knowledge of the type of an expression can vary in the C code.
1611 class RuntimeVariable
1612 # The name of the variable in the C code
1615 # The static type of the variable (as declard in C)
1618 # The current casted type of the variable (as known in Nit)
1619 var mcasttype: MType is writable
1621 # If the variable exaclty a mcasttype?
1622 # false (usual value) means that the variable is a mcasttype or a subtype.
1623 var is_exact: Bool = false is writable
1625 init(name: String, mtype: MType, mcasttype: MType)
1629 self.mcasttype = mcasttype
1630 assert not mtype.need_anchor
1631 assert not mcasttype.need_anchor
1634 redef fun to_s do return name
1639 if self.is_exact then
1640 exact_str = " exact"
1645 if self.mtype == self.mcasttype then
1646 type_str = "{mtype}{exact_str}"
1648 type_str = "{mtype}({mcasttype}{exact_str})"
1650 return "<{name}:{type_str}>"
1654 # A frame correspond to a visited property in a `GlobalCompilerVisitor`
1657 type VISITOR: AbstractCompilerVisitor
1659 # The associated visitor
1660 var visitor: VISITOR
1662 # The executed property.
1663 # A Method in case of a call, an attribute in case of a default initialization.
1664 var mpropdef: MPropDef
1666 # The static type of the receiver
1667 var receiver: MClassType
1669 # Arguments of the method (the first is the receiver)
1670 var arguments: Array[RuntimeVariable]
1672 # The runtime_variable associated to the return (in a function)
1673 var returnvar: nullable RuntimeVariable = null is writable
1675 # The label at the end of the property
1676 var returnlabel: nullable String = null is writable
1680 # Return the C type associated to a given Nit static type
1681 fun ctype: String do return "val*"
1683 # C type outside of the compiler code and in boxes
1684 fun ctype_extern: String do return "val*"
1686 # Short name of the `ctype
` to use in unions
1687 fun ctypename: String do return "val"
1689 # Return the name of the C structure associated to a Nit live type
1690 fun c_name: String is abstract
1691 protected var c_name_cache: nullable String is protected writable
1694 redef class MClassType
1697 var res = self.c_name_cache
1698 if res != null then return res
1699 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1700 self.c_name_cache = res
1704 redef fun ctype: String
1706 if mclass.name == "Int" then
1708 else if mclass.name == "Bool" then
1710 else if mclass.name == "Char" then
1712 else if mclass.name == "Float" then
1714 else if mclass.name == "NativeString" then
1716 else if mclass.name == "NativeArray" then
1723 redef fun ctype_extern: String
1725 if mclass.kind == extern_kind then
1732 redef fun ctypename: String
1734 if mclass.name == "Int" then
1736 else if mclass.name == "Bool" then
1738 else if mclass.name == "Char" then
1740 else if mclass.name == "Float" then
1742 else if mclass.name == "NativeString" then
1744 else if mclass.name == "NativeArray" then
1745 #return "{self.arguments.first.ctype}*"
1753 redef class MGenericType
1756 var res = self.c_name_cache
1757 if res != null then return res
1759 for t in self.arguments do
1760 res = res + t.c_name
1762 self.c_name_cache = res
1767 redef class MParameterType
1770 var res = self.c_name_cache
1771 if res != null then return res
1772 res = "{self.mclass.c_name}_FT{self.rank}"
1773 self.c_name_cache = res
1778 redef class MVirtualType
1781 var res = self.c_name_cache
1782 if res != null then return res
1783 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1784 self.c_name_cache = res
1789 redef class MNullableType
1792 var res = self.c_name_cache
1793 if res != null then return res
1794 res = "nullable_{self.mtype.c_name}"
1795 self.c_name_cache = res
1801 # Return the name of the C structure associated to a Nit class
1802 fun c_name: String do
1803 var res = self.c_name_cache
1804 if res != null then return res
1805 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1806 self.c_name_cache = res
1809 private var c_name_cache: nullable String
1812 redef class MProperty
1813 fun c_name: String do
1814 var res = self.c_name_cache
1815 if res != null then return res
1816 res = "{self.intro.c_name}"
1817 self.c_name_cache = res
1820 private var c_name_cache: nullable String
1823 redef class MPropDef
1824 type VISITOR: AbstractCompilerVisitor
1826 private var c_name_cache: nullable String
1828 # The mangled name associated to the property
1831 var res = self.c_name_cache
1832 if res != null then return res
1833 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1834 self.c_name_cache = res
1839 redef class MMethodDef
1840 # Can the body be inlined?
1841 fun can_inline(v: VISITOR): Bool
1843 if is_abstract then return true
1844 var modelbuilder = v.compiler.modelbuilder
1845 if modelbuilder.mpropdef2npropdef.has_key(self) then
1846 var npropdef = modelbuilder.mpropdef2npropdef[self]
1847 return npropdef.can_inline
1848 else if self.mproperty.is_root_init then
1849 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1856 # Inline the body in another visitor
1857 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1859 var modelbuilder = v.compiler.modelbuilder
1860 var val = constant_value
1861 if modelbuilder.mpropdef2npropdef.has_key(self) then
1862 var npropdef = modelbuilder.mpropdef2npropdef[self]
1863 var oldnode = v.current_node
1864 v.current_node = npropdef
1865 self.compile_parameter_check(v, arguments)
1866 npropdef.compile_to_c(v, self, arguments)
1867 v.current_node = oldnode
1868 else if self.mproperty.is_root_init then
1869 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1870 var oldnode = v.current_node
1871 v.current_node = nclassdef
1872 self.compile_parameter_check(v, arguments)
1873 nclassdef.compile_to_c(v, self, arguments)
1874 v.current_node = oldnode
1875 else if val != null then
1876 v.ret(v.value_instance(val))
1883 # Generate type checks in the C code to check covariant parameters
1884 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1886 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1888 for i in [0..msignature.arity[ do
1889 # skip test for vararg since the array is instantiated with the correct polymorphic type
1890 if msignature.vararg_rank == i then continue
1892 # skip if the cast is not required
1893 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1894 if not origmtype.need_anchor then continue
1896 # get the parameter type
1897 var mtype = self.msignature.mparameters[i].mtype
1900 # note that v decides if and how to implements the cast
1901 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1902 v.add_cast(arguments[i+1], mtype, "covariance")
1909 redef class APropdef
1910 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1912 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1913 debug("Not yet implemented")
1916 fun can_inline: Bool do return true
1919 redef class AMethPropdef
1920 redef fun compile_to_c(v, mpropdef, arguments)
1922 if mpropdef.is_abstract then
1923 var cn = v.class_name_string(arguments.first)
1924 v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s
` called on `%s
`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1929 # Call the implicit super-init
1930 var auto_super_inits = self.auto_super_inits
1931 if auto_super_inits != null then
1932 var args = [arguments.first]
1933 for auto_super_init in auto_super_inits do
1934 assert auto_super_init.mproperty != mpropdef.mproperty
1936 for i in [0..auto_super_init.msignature.arity+1[ do
1937 args.add(arguments[i])
1939 assert auto_super_init.mproperty != mpropdef.mproperty
1940 v.compile_callsite(auto_super_init, args)
1943 if auto_super_call then
1944 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
1947 # Try special compilation
1948 if mpropdef.is_intern then
1949 if compile_intern_to_c(v, mpropdef, arguments) then return
1950 else if mpropdef.is_extern then
1951 if mpropdef.mproperty.is_init then
1952 if compile_externinit_to_c(v, mpropdef, arguments) then return
1954 if compile_externmeth_to_c(v, mpropdef, arguments) then return
1958 # Compile block if any
1959 var n_block = n_block
1960 if n_block != null then
1961 for i in [0..mpropdef.msignature.arity[ do
1962 var variable = self.n_signature.n_params[i].variable.as(not null)
1963 v.assign(v.variable(variable), arguments[i+1])
1970 var cn = v.class_name_string(arguments.first)
1971 v.add("PRINT_ERROR(\"Runtime error: uncompiled method `%s
` called on `%s
`. NOT YET IMPLEMENTED\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1975 redef fun can_inline
1977 if self.auto_super_inits != null then return false
1978 var nblock = self.n_block
1979 if nblock == null then return true
1980 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1981 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1985 fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
1987 var pname = mpropdef.mproperty.name
1988 var cname = mpropdef.mclassdef.mclass.name
1989 var ret = mpropdef.msignature.return_mtype
1991 ret = v.resolve_for(ret, arguments.first)
1993 if pname != "==" and pname != "!=" then
1994 v.adapt_signature(mpropdef, arguments)
1995 v.unbox_signature_extern(mpropdef, arguments)
1997 if cname == "Int" then
1998 if pname == "output" then
1999 v.add("printf(\"%ld\\n\", {arguments.first});")
2001 else if pname == "object_id" then
2002 v.ret(arguments.first)
2004 else if pname == "+" then
2005 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2007 else if pname == "-" then
2008 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2010 else if pname == "unary -" then
2011 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2013 else if pname == "*" then
2014 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2016 else if pname == "/" then
2017 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2019 else if pname == "%" then
2020 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
2022 else if pname == "lshift" then
2023 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
2025 else if pname == "rshift" then
2026 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
2028 else if pname == "==" then
2029 v.ret(v.equal_test(arguments[0], arguments[1]))
2031 else if pname == "!=" then
2032 var res = v.equal_test(arguments[0], arguments[1])
2033 v.ret(v.new_expr("!{res}", ret.as(not null)))
2035 else if pname == "<" then
2036 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2038 else if pname == ">" then
2039 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2041 else if pname == "<=" then
2042 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2044 else if pname == ">=" then
2045 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2047 else if pname == "to_f" then
2048 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
2050 else if pname == "ascii" then
2051 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
2054 else if cname == "Char" then
2055 if pname == "output" then
2056 v.add("printf(\"%c\", {arguments.first});")
2058 else if pname == "object_id" then
2059 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2061 else if pname == "successor" then
2062 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2064 else if pname == "predecessor" then
2065 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2067 else if pname == "==" then
2068 v.ret(v.equal_test(arguments[0], arguments[1]))
2070 else if pname == "!=" then
2071 var res = v.equal_test(arguments[0], arguments[1])
2072 v.ret(v.new_expr("!{res}", ret.as(not null)))
2074 else if pname == "<" then
2075 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2077 else if pname == ">" then
2078 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2080 else if pname == "<=" then
2081 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2083 else if pname == ">=" then
2084 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2086 else if pname == "to_i" then
2087 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
2089 else if pname == "ascii" then
2090 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
2093 else if cname == "Bool" then
2094 if pname == "output" then
2095 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
2097 else if pname == "object_id" then
2098 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2100 else if pname == "==" then
2101 v.ret(v.equal_test(arguments[0], arguments[1]))
2103 else if pname == "!=" then
2104 var res = v.equal_test(arguments[0], arguments[1])
2105 v.ret(v.new_expr("!{res}", ret.as(not null)))
2108 else if cname == "Float" then
2109 if pname == "output" then
2110 v.add("printf(\"%f\\n\", {arguments.first});")
2112 else if pname == "object_id" then
2113 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
2115 else if pname == "+" then
2116 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2118 else if pname == "-" then
2119 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2121 else if pname == "unary -" then
2122 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2124 else if pname == "succ" then
2125 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
2127 else if pname == "prec" then
2128 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
2130 else if pname == "*" then
2131 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2133 else if pname == "/" then
2134 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2136 else if pname == "==" then
2137 v.ret(v.equal_test(arguments[0], arguments[1]))
2139 else if pname == "!=" then
2140 var res = v.equal_test(arguments[0], arguments[1])
2141 v.ret(v.new_expr("!{res}", ret.as(not null)))
2143 else if pname == "<" then
2144 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2146 else if pname == ">" then
2147 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2149 else if pname == "<=" then
2150 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2152 else if pname == ">=" then
2153 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2155 else if pname == "to_i" then
2156 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
2159 else if cname == "NativeString" then
2160 if pname == "[]" then
2161 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
2163 else if pname == "[]=" then
2164 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2166 else if pname == "copy_to" then
2167 v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2169 else if pname == "atoi" then
2170 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
2172 else if pname == "new" then
2173 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2176 else if cname == "NativeArray" then
2177 v.native_array_def(pname, ret, arguments)
2180 if pname == "exit" then
2181 v.add("exit({arguments[1]});")
2183 else if pname == "sys" then
2184 v.ret(v.new_expr("glob_sys", ret.as(not null)))
2186 else if pname == "calloc_string" then
2187 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2189 else if pname == "calloc_array" then
2190 v.calloc_array(ret.as(not null), arguments)
2192 else if pname == "object_id" then
2193 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2195 else if pname == "is_same_type" then
2196 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
2198 else if pname == "is_same_instance" then
2199 v.ret(v.equal_test(arguments[0], arguments[1]))
2201 else if pname == "output_class_name" then
2202 var nat = v.class_name_string(arguments.first)
2203 v.add("printf(\"%s\\n\", {nat});")
2205 else if pname == "native_class_name" then
2206 var nat = v.class_name_string(arguments.first)
2207 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
2209 else if pname == "force_garbage_collection" then
2210 v.add("nit_gcollect();")
2212 else if pname == "native_argc" then
2213 v.ret(v.new_expr("glob_argc", ret.as(not null)))
2215 else if pname == "native_argv" then
2216 v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
2222 # Compile an extern method
2223 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2224 fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2227 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2229 externname = at.arg_as_string(v.compiler.modelbuilder)
2230 if externname == null then return false
2234 if location.file != null then
2235 var file = location.file.filename
2238 var res: nullable RuntimeVariable = null
2239 var ret = mpropdef.msignature.return_mtype
2241 ret = v.resolve_for(ret, arguments.first)
2242 res = v.new_var_extern(ret)
2244 v.adapt_signature(mpropdef, arguments)
2245 v.unbox_signature_extern(mpropdef, arguments)
2248 v.add("{externname}({arguments.join(", ")});")
2250 v.add("{res} = {externname}({arguments.join(", ")});")
2251 res = v.box_extern(res, ret.as(not null))
2257 # Compile an extern factory
2258 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2259 fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2262 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2264 externname = at.arg_as_string(v.compiler.modelbuilder)
2265 if externname == null then return false
2269 if location.file != null then
2270 var file = location.file.filename
2273 v.adapt_signature(mpropdef, arguments)
2274 v.unbox_signature_extern(mpropdef, arguments)
2275 var ret = arguments.first.mtype
2276 var res = v.new_var_extern(ret)
2280 v.add("{res} = {externname}({arguments.join(", ")});")
2281 res = v.box_extern(res, ret)
2287 redef class AAttrPropdef
2288 redef fun compile_to_c(v, mpropdef, arguments)
2290 if mpropdef == mreadpropdef then
2291 assert arguments.length == 1
2295 assert nexpr != null
2297 var ret = self.mpropdef.static_mtype
2298 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2299 var guard = self.mlazypropdef.mproperty
2301 set = v.isset_attribute(self.mpropdef.mproperty, arguments.first)
2303 set = v.read_attribute(guard, arguments.first)
2305 v.add("if(likely({set})) \{")
2306 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2308 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2309 v.write_attribute(self.mpropdef.mproperty, arguments.first, value)
2310 v.assign(res, value)
2312 var true_v = v.new_expr("1", v.bool_type)
2313 v.write_attribute(guard, arguments.first, true_v)
2317 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2319 v.assign(v.frame.returnvar.as(not null), res)
2320 else if mpropdef == mwritepropdef then
2321 assert arguments.length == 2
2322 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
2324 var ret = self.mpropdef.static_mtype
2325 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2327 v.write_attribute(self.mlazypropdef.mproperty, arguments.first, v.new_expr("1", v.bool_type))
2335 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2337 var nexpr = self.n_expr
2338 if nexpr != null and not is_lazy then
2339 var oldnode = v.current_node
2340 v.current_node = self
2341 var old_frame = v.frame
2342 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2344 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2345 v.write_attribute(self.mpropdef.mproperty, recv, value)
2347 v.current_node = oldnode
2351 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2353 var nexpr = self.n_expr
2354 if nexpr != null then return
2356 var oldnode = v.current_node
2357 v.current_node = self
2358 var old_frame = v.frame
2359 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2361 # Force read to check the initialization
2362 v.read_attribute(self.mpropdef.mproperty, recv)
2364 v.current_node = oldnode
2368 redef class AClassdef
2369 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2371 if mpropdef == self.mfree_init then
2372 assert mpropdef.mproperty.is_root_init
2373 assert arguments.length == 1
2374 if not mpropdef.is_intro then
2375 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
2385 # Try to compile self as an expression
2386 # Do not call this method directly, use `v
.expr
` instead
2387 private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
2389 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
2390 var mtype = self.mtype
2391 if mtype == null then
2394 var res = v.new_var(mtype)
2395 v.add("/* {res} = NOT YET {class_name} */")
2400 # Try to compile self as a statement
2401 # Do not call this method directly, use `v
.stmt
` instead
2402 private fun stmt(v: AbstractCompilerVisitor)
2408 redef class ABlockExpr
2411 for e in self.n_expr do v.stmt(e)
2415 var last = self.n_expr.last
2416 for e in self.n_expr do
2417 if e == last then break
2420 return v.expr(last, null)
2424 redef class AVardeclExpr
2427 var variable = self.variable.as(not null)
2428 var ne = self.n_expr
2430 var i = v.expr(ne, variable.declared_type)
2431 v.assign(v.variable(variable), i)
2436 redef class AVarExpr
2439 var res = v.variable(self.variable.as(not null))
2440 var mtype = self.mtype.as(not null)
2441 return v.autoadapt(res, mtype)
2445 redef class AVarAssignExpr
2448 var variable = self.variable.as(not null)
2449 var i = v.expr(self.n_value, variable.declared_type)
2450 v.assign(v.variable(variable), i)
2455 redef class AVarReassignExpr
2458 var variable = self.variable.as(not null)
2459 var vari = v.variable(variable)
2460 var value = v.expr(self.n_value, variable.declared_type)
2461 var res = v.compile_callsite(self.reassign_callsite.as(not null), [vari, value])
2463 v.assign(v.variable(variable), res)
2467 redef class ASelfExpr
2468 redef fun expr(v) do return v.frame.arguments.first
2471 redef class AEscapeExpr
2472 redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
2475 redef class AReturnExpr
2478 var nexpr = self.n_expr
2479 if nexpr != null then
2480 var returnvar = v.frame.returnvar.as(not null)
2481 var i = v.expr(nexpr, returnvar.mtype)
2482 v.assign(returnvar, i)
2484 v.add("goto {v.frame.returnlabel.as(not null)};")
2488 redef class AAbortExpr
2489 redef fun stmt(v) do v.add_abort("Aborted")
2495 var cond = v.expr_bool(self.n_expr)
2496 v.add("if ({cond})\{")
2505 var res = v.new_var(self.mtype.as(not null))
2506 var cond = v.expr_bool(self.n_expr)
2507 v.add("if ({cond})\{")
2508 v.assign(res, v.expr(self.n_then.as(not null), null))
2510 v.assign(res, v.expr(self.n_else.as(not null), null))
2516 redef class AIfexprExpr
2519 var res = v.new_var(self.mtype.as(not null))
2520 var cond = v.expr_bool(self.n_expr)
2521 v.add("if ({cond})\{")
2522 v.assign(res, v.expr(self.n_then, null))
2524 v.assign(res, v.expr(self.n_else, null))
2533 v.stmt(self.n_block)
2534 v.add_escape_label(break_mark)
2538 redef class AWhileExpr
2542 var cond = v.expr_bool(self.n_expr)
2543 v.add("if (!{cond}) break;")
2544 v.stmt(self.n_block)
2545 v.add_escape_label(continue_mark)
2547 v.add_escape_label(break_mark)
2551 redef class ALoopExpr
2555 v.stmt(self.n_block)
2556 v.add_escape_label(continue_mark)
2558 v.add_escape_label(break_mark)
2562 redef class AForExpr
2565 var cl = v.expr(self.n_expr, null)
2566 var it_meth = self.method_iterator
2567 assert it_meth != null
2568 var it = v.compile_callsite(it_meth, [cl])
2571 var isok_meth = self.method_is_ok
2572 assert isok_meth != null
2573 var ok = v.compile_callsite(isok_meth, [it])
2575 v.add("if(!{ok}) break;")
2576 if self.variables.length == 1 then
2577 var item_meth = self.method_item
2578 assert item_meth != null
2579 var i = v.compile_callsite(item_meth, [it])
2581 v.assign(v.variable(variables.first), i)
2582 else if self.variables.length == 2 then
2583 var key_meth = self.method_key
2584 assert key_meth != null
2585 var i = v.compile_callsite(key_meth, [it])
2587 v.assign(v.variable(variables[0]), i)
2588 var item_meth = self.method_item
2589 assert item_meth != null
2590 i = v.compile_callsite(item_meth, [it])
2592 v.assign(v.variable(variables[1]), i)
2596 v.stmt(self.n_block)
2597 v.add_escape_label(continue_mark)
2598 var next_meth = self.method_next
2599 assert next_meth != null
2600 v.compile_callsite(next_meth, [it])
2602 v.add_escape_label(break_mark)
2604 var method_finish = self.method_finish
2605 if method_finish != null then
2606 # TODO: Find a way to call this also in long escape (e.g. return)
2607 v.compile_callsite(method_finish, [it])
2612 redef class AAssertExpr
2615 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return
2617 var cond = v.expr_bool(self.n_expr)
2618 v.add("if (unlikely(!{cond})) \{")
2622 v.add_abort("Assert '{nid.text}' failed")
2624 v.add_abort("Assert failed")
2633 var res = v.new_var(self.mtype.as(not null))
2634 var i1 = v.expr_bool(self.n_expr)
2635 v.add("if ({i1}) \{")
2638 var i2 = v.expr_bool(self.n_expr2)
2639 v.add("{res} = {i2};")
2645 redef class AImpliesExpr
2648 var res = v.new_var(self.mtype.as(not null))
2649 var i1 = v.expr_bool(self.n_expr)
2650 v.add("if (!{i1}) \{")
2653 var i2 = v.expr_bool(self.n_expr2)
2654 v.add("{res} = {i2};")
2660 redef class AAndExpr
2663 var res = v.new_var(self.mtype.as(not null))
2664 var i1 = v.expr_bool(self.n_expr)
2665 v.add("if (!{i1}) \{")
2668 var i2 = v.expr_bool(self.n_expr2)
2669 v.add("{res} = {i2};")
2675 redef class ANotExpr
2678 var cond = v.expr_bool(self.n_expr)
2679 return v.new_expr("!{cond}", self.mtype.as(not null))
2683 redef class AOrElseExpr
2686 var res = v.new_var(self.mtype.as(not null))
2687 var i1 = v.expr(self.n_expr, null)
2688 v.add("if ({i1}!=NULL) \{")
2691 var i2 = v.expr(self.n_expr2, null)
2698 redef class AIntExpr
2699 redef fun expr(v) do return v.new_expr("{self.value.to_s}", self.mtype.as(not null))
2702 redef class AFloatExpr
2703 redef fun expr(v) do return v.new_expr("{self.n_float.text}", self.mtype.as(not null)) # FIXME use value, not n_float
2706 redef class ACharExpr
2707 redef fun expr(v) do return v.new_expr("'{self.value.to_s.escape_to_c}'", self.mtype.as(not null))
2710 redef class AArrayExpr
2713 var mtype = self.mtype.as(MClassType).arguments.first
2714 var array = new Array[RuntimeVariable]
2715 for nexpr in self.n_exprs.n_exprs do
2716 var i = v.expr(nexpr, mtype)
2719 return v.array_instance(array, mtype)
2723 redef class AStringFormExpr
2724 redef fun expr(v) do return v.string_instance(self.value.as(not null))
2727 redef class ASuperstringExpr
2730 var array = new Array[RuntimeVariable]
2731 for ne in self.n_exprs do
2732 if ne isa AStringFormExpr and ne.value == "" then continue # skip empty sub-strings
2733 var i = v.expr(ne, null)
2736 var a = v.array_instance(array, v.object_type)
2737 var res = v.send(v.get_property("to_s", a.mtype), [a])
2742 redef class ACrangeExpr
2745 var i1 = v.expr(self.n_expr, null)
2746 var i2 = v.expr(self.n_expr2, null)
2747 var mtype = self.mtype.as(MClassType)
2748 var res = v.init_instance(mtype)
2749 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2754 redef class AOrangeExpr
2757 var i1 = v.expr(self.n_expr, null)
2758 var i2 = v.expr(self.n_expr2, null)
2759 var mtype = self.mtype.as(MClassType)
2760 var res = v.init_instance(mtype)
2761 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2766 redef class ATrueExpr
2767 redef fun expr(v) do return v.new_expr("1", self.mtype.as(not null))
2770 redef class AFalseExpr
2771 redef fun expr(v) do return v.new_expr("0", self.mtype.as(not null))
2774 redef class ANullExpr
2775 redef fun expr(v) do return v.new_expr("NULL", self.mtype.as(not null))
2778 redef class AIsaExpr
2781 var i = v.expr(self.n_expr, null)
2782 return v.type_test(i, self.cast_type.as(not null), "isa")
2786 redef class AAsCastExpr
2789 var i = v.expr(self.n_expr, null)
2790 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2792 v.add_cast(i, self.mtype.as(not null), "as")
2797 redef class AAsNotnullExpr
2800 var i = v.expr(self.n_expr, null)
2801 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2803 if i.mtype.ctype != "val*" then return i
2805 v.add("if (unlikely({i} == NULL)) \{")
2806 v.add_abort("Cast failed")
2812 redef class AParExpr
2813 redef fun expr(v) do return v.expr(self.n_expr, null)
2816 redef class AOnceExpr
2819 var mtype = self.mtype.as(not null)
2820 var name = v.get_name("varonce")
2821 var guard = v.get_name(name + "_guard")
2822 v.add_decl("static {mtype.ctype} {name};")
2823 v.add_decl("static int {guard};")
2824 var res = v.new_var(mtype)
2825 v.add("if ({guard}) \{")
2826 v.add("{res} = {name};")
2828 var i = v.expr(self.n_expr, mtype)
2829 v.add("{res} = {i};")
2830 v.add("{name} = {res};")
2831 v.add("{guard} = 1;")
2837 redef class ASendExpr
2840 var recv = v.expr(self.n_expr, null)
2841 var callsite = self.callsite.as(not null)
2842 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2843 return v.compile_callsite(callsite, args)
2847 redef class ASendReassignFormExpr
2850 var recv = v.expr(self.n_expr, null)
2851 var callsite = self.callsite.as(not null)
2852 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2854 var value = v.expr(self.n_value, null)
2856 var left = v.compile_callsite(callsite, args)
2859 var res = v.compile_callsite(self.reassign_callsite.as(not null), [left, value])
2863 v.compile_callsite(self.write_callsite.as(not null), args)
2867 redef class ASuperExpr
2870 var recv = v.frame.arguments.first
2872 var callsite = self.callsite
2873 if callsite != null then
2874 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2876 # Add additional arguments for the super init call
2877 if args.length == 1 then
2878 for i in [0..callsite.msignature.arity[ do
2879 args.add(v.frame.arguments[i+1])
2883 var res = v.compile_callsite(callsite, args)
2887 var mpropdef = self.mpropdef.as(not null)
2888 var args = v.varargize(mpropdef, recv, self.n_args.n_exprs)
2889 if args.length == 1 then
2890 args = v.frame.arguments
2893 # stantard call-next-method
2894 return v.supercall(mpropdef, recv.mtype.as(MClassType), args)
2898 redef class ANewExpr
2901 var mtype = self.mtype.as(MClassType)
2903 var ctype = mtype.ctype
2904 if mtype.mclass.name == "NativeArray" then
2905 assert self.n_args.n_exprs.length == 1
2906 var l = v.expr(self.n_args.n_exprs.first, null)
2907 assert mtype isa MGenericType
2908 var elttype = mtype.arguments.first
2909 return v.native_array_instance(elttype, l)
2910 else if ctype == "val*" then
2911 recv = v.init_instance(mtype)
2912 else if ctype == "char*" then
2913 recv = v.new_expr("NULL/*special!*/", mtype)
2915 recv = v.new_expr("({ctype})0/*special!*/", mtype)
2918 var callsite = self.callsite.as(not null)
2919 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2920 var res2 = v.compile_callsite(callsite, args)
2921 if res2 != null then
2922 #self.debug("got {res2} from {mproperty}. drop {recv}")
2929 redef class AAttrExpr
2932 var recv = v.expr(self.n_expr, null)
2933 var mproperty = self.mproperty.as(not null)
2934 return v.read_attribute(mproperty, recv)
2938 redef class AAttrAssignExpr
2941 var recv = v.expr(self.n_expr, null)
2942 var i = v.expr(self.n_value, null)
2943 var mproperty = self.mproperty.as(not null)
2944 v.write_attribute(mproperty, recv, i)
2949 redef class AAttrReassignExpr
2952 var recv = v.expr(self.n_expr, null)
2953 var value = v.expr(self.n_value, null)
2954 var mproperty = self.mproperty.as(not null)
2955 var attr = v.read_attribute(mproperty, recv)
2956 var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
2958 v.write_attribute(mproperty, recv, res)
2962 redef class AIssetAttrExpr
2965 var recv = v.expr(self.n_expr, null)
2966 var mproperty = self.mproperty.as(not null)
2967 return v.isset_attribute(mproperty, recv)
2971 redef class ADebugTypeExpr
2980 redef class Array[E]
2981 # Return a new `Array` with the elements only contened in self and not in `o
`
2982 fun -(o: Array[E]): Array[E] do
2983 var res = new Array[E]
2984 for e in self do if not o.has(e) then res.add(e)
2990 # All `MProperty` associated to all `MClassDef` of `mclass
`
2991 fun properties(mclass: MClass): Set[MProperty] do
2992 if not self.properties_cache.has_key(mclass) then
2993 var properties = new HashSet[MProperty]
2994 var parents = new Array[MClass]
2995 if self.flatten_mclass_hierarchy.has(mclass) then
2996 parents.add_all(mclass.in_hierarchy(self).direct_greaters)
2998 for parent in parents do
2999 properties.add_all(self.properties(parent))
3001 for mclassdef in mclass.mclassdefs do
3002 if not self.in_importation <= mclassdef.mmodule then continue
3003 for mprop in mclassdef.intro_mproperties do
3004 properties.add(mprop)
3007 self.properties_cache[mclass] = properties
3009 return properties_cache[mclass]
3011 private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
3013 # Write FFI and nitni results to file
3014 fun finalize_ffi(c: AbstractCompiler) do end
3016 # Give requided addinional system libraries (as given to LD_LIBS)
3017 # Note: can return null instead of an empty set
3018 fun collect_linker_libs: nullable Set[String] do return null
3021 # Create a tool context to handle options and paths
3022 var toolcontext = new ToolContext
3024 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
3026 # We do not add other options, so process them now!
3027 toolcontext.process_options(args)
3029 # We need a model to collect stufs
3030 var model = new Model
3031 # An a model builder to parse files
3032 var modelbuilder = new ModelBuilder(model, toolcontext)
3034 var arguments = toolcontext.option_context.rest
3035 if arguments.length > 1 and toolcontext.opt_output.value != null then
3036 print "Error: --output needs a single source file. Do you prefer --dir?"
3040 # Here we load an process all modules passed on the command line
3041 var mmodules = modelbuilder.parse(arguments)
3043 if mmodules.is_empty then return
3044 modelbuilder.run_phases
3046 for mmodule in mmodules do
3047 toolcontext.info("*** PROCESS {mmodule} ***", 1)
3049 toolcontext.run_global_phases(ms)