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
25 # Add compiling options
26 redef class ToolContext
28 var opt_output
: OptionString = new OptionString("Output file", "-o", "--output")
30 var opt_dir
: OptionString = new OptionString("Output directory", "--dir")
32 var opt_no_cc
: OptionBool = new OptionBool("Do not invoke C compiler", "--no-cc")
34 var opt_no_main
: OptionBool = new OptionBool("Do not generate main entry point", "--no-main")
36 var opt_cc_path
: OptionArray = new OptionArray("Set include path for C header files (may be used more than once)", "--cc-path")
38 var opt_make_flags
: OptionString = new OptionString("Additional options to make", "--make-flags")
40 var opt_compile_dir
: OptionString = new OptionString("Directory used to generate temporary files", "--compile-dir")
42 var opt_hardening
: OptionBool = new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
44 var opt_no_shortcut_range
: OptionBool = new OptionBool("Always insantiate a range and its iterator on 'for' loops", "--no-shortcut-range")
45 # --no-check-covariance
46 var opt_no_check_covariance
: OptionBool = new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
47 # --no-check-attr-isset
48 var opt_no_check_attr_isset
: OptionBool = new OptionBool("Disable isset tests before each attribute access (dangerous)", "--no-check-attr-isset")
50 var opt_no_check_assert
: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
52 var opt_no_check_autocast
: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
54 var opt_no_check_null
: OptionBool = new OptionBool("Disable tests of null receiver (dangerous)", "--no-check-null")
56 var opt_no_check_all
: OptionBool = new OptionBool("Disable all tests (dangerous)", "--no-check-all")
57 # --typing-test-metrics
58 var opt_typing_test_metrics
: OptionBool = new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
59 # --invocation-metrics
60 var opt_invocation_metrics
: OptionBool = new OptionBool("Enable static and dynamic count of all method invocations", "--invocation-metrics")
61 # --isset-checks-metrics
62 var opt_isset_checks_metrics
: OptionBool = new OptionBool("Enable static and dynamic count of isset checks before attributes access", "--isset-checks-metrics")
64 var opt_stacktrace
: OptionString = 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
, self.opt_no_shortcut_range
)
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 mainmodule
= compiler
.mainmodule
184 var compile_dir
= compile_dir
186 # Generate the .h and .c files
187 # A single C file regroups many compiled rumtime functions
188 # 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
190 self.toolcontext
.info
("*** WRITING C ***", 1)
194 var cfiles
= new Array[String]
195 write_files
(compiler
, compile_dir
, cfiles
)
197 # Generate the Makefile
199 write_makefile
(compiler
, compile_dir
, cfiles
)
202 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
204 # Execute the Makefile
206 if self.toolcontext
.opt_no_cc
.value
then return
209 self.toolcontext
.info
("*** COMPILING C ***", 1)
211 compile_c_code
(compiler
, compile_dir
)
214 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
217 fun write_files
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
219 var platform
= compiler
.mainmodule
.target_platform
220 if self.toolcontext
.opt_stacktrace
.value
== "nitstack" and (platform
== null or platform
.supports_libunwind
) then compiler
.build_c_to_nit_bindings
221 var cc_opt_with_libgc
= "-DWITH_LIBGC"
222 if platform
!= null and not platform
.supports_libgc
then cc_opt_with_libgc
= ""
224 # Add gc_choser.h to aditionnal bodies
225 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
226 compiler
.extern_bodies
.add
(gc_chooser
)
227 compiler
.files_to_copy
.add
"{cc_paths.first}/gc_chooser.c"
228 compiler
.files_to_copy
.add
"{cc_paths.first}/gc_chooser.h"
231 var m2m
= toolcontext
.modelbuilder
.mmodule2nmodule
232 for m
in compiler
.mainmodule
.in_importation
.greaters
do
233 compiler
.finalize_ffi_for_module
(m
)
236 # Copy original .[ch] files to compile_dir
237 for src
in compiler
.files_to_copy
do
238 var basename
= src
.basename
("")
239 var dst
= "{compile_dir}/{basename}"
243 var hfilename
= compiler
.header
.file
.name
+ ".h"
244 var hfilepath
= "{compile_dir}/{hfilename}"
245 var h
= new OFStream.open
(hfilepath
)
246 for l
in compiler
.header
.decl_lines
do
250 for l
in compiler
.header
.lines
do
256 for f
in compiler
.files
do
258 var hfile
: nullable OFStream = null
260 var cfilename
= "{f.name}.0.h"
261 var cfilepath
= "{compile_dir}/{cfilename}"
262 hfile
= new OFStream.open
(cfilepath
)
263 hfile
.write
"#include \"{hfilename}\
"\n"
264 for key
in f
.required_declarations
do
265 if not compiler
.provided_declarations
.has_key
(key
) then
266 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
268 node
.debug
"No provided declaration for {key}"
270 print
"No provided declaration for {key}"
274 hfile
.write compiler
.provided_declarations
[key
]
278 var file
: nullable OFStream = null
279 for vis
in f
.writers
do
280 if vis
== compiler
.header
then continue
281 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
282 if total_lines
== 0 then continue
284 if file
== null or count
> 10000 then
286 if file
!= null then file
.close
287 cfilename
= "{f.name}.{i}.c"
288 cfilepath
= "{compile_dir}/{cfilename}"
289 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
290 cfiles
.add
(cfilename
)
291 file
= new OFStream.open
(cfilepath
)
292 file
.write
"#include \"{f.name}.0.h\
"\n"
295 for l
in vis
.decl_lines
do
299 for l
in vis
.lines
do
304 if file
!= null then file
.close
307 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
310 fun makefile_name
(mainmodule
: MModule): String do return "{mainmodule.name}.mk"
312 fun default_outname
(mainmodule
: MModule): String do return mainmodule
.name
314 # Combine options and platform informations to get the final path of the outfile
315 fun outfile
(mainmodule
: MModule): String
317 var res
= self.toolcontext
.opt_output
.value
318 if res
!= null then return res
319 res
= default_outname
(mainmodule
)
320 var dir
= self.toolcontext
.opt_dir
.value
321 if dir
!= null then return dir
.join_path
(res
)
325 fun write_makefile
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
327 var mainmodule
= compiler
.mainmodule
328 var platform
= compiler
.mainmodule
.target_platform
330 var outname
= outfile
(mainmodule
)
332 var orig_dir
=".." # FIXME only works if `compile_dir` is a subdirectory of cwd
333 var outpath
= orig_dir
.join_path
(outname
).simplify_path
334 var makename
= makefile_name
(mainmodule
)
335 var makepath
= "{compile_dir}/{makename}"
336 var makefile
= new OFStream.open
(makepath
)
340 cc_includes
+= " -I \"" + p + "\
""
343 var linker_options
= new HashSet[String]
344 var m2m
= toolcontext
.modelbuilder
.mmodule2nmodule
345 for m
in mainmodule
.in_importation
.greaters
do
346 var libs
= m
.collect_linker_libs
347 if libs
!= null then linker_options
.add_all
(libs
)
350 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")
352 var ost
= toolcontext
.opt_stacktrace
.value
353 if (ost
== "libunwind" or ost
== "nitstack") and (platform
== null or platform
.supports_libunwind
) then makefile
.write
("NEED_LIBUNWIND := YesPlease\n")
355 # Dynamic adaptations
356 # While `platform` enable complex toolchains, they are statically applied
357 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
359 # Check and adapt the targeted system
360 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
361 makefile
.write
("ifeq ($(uname_S),Darwin)\n")
362 # remove -lunwind since it is already included on macosx
363 makefile
.write
("\tNEED_LIBUNWIND :=\n")
364 makefile
.write
("endif\n\n")
366 # Check and adapt for the compiler used
367 # clang need an additionnal `-Qunused-arguments`
368 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")
370 makefile
.write
("ifdef NEED_LIBUNWIND\n\tLDLIBS += -lunwind\nendif\n")
372 makefile
.write
("all: {outpath}\n\n")
374 var ofiles
= new Array[String]
375 var dep_rules
= new Array[String]
376 # Compile each generated file
378 var o
= f
.strip_extension
(".c") + ".o"
379 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
384 var java_files
= new Array[ExternFile]
386 # Compile each required extern body into a specific .o
387 for f
in compiler
.extern_bodies
do
388 var o
= f
.makefile_rule_name
389 var ff
= f
.filename
.basename
("")
390 makefile
.write
("{o}: {ff}\n")
391 makefile
.write
("\t{f.makefile_rule_content}\n\n")
392 dep_rules
.add
(f
.makefile_rule_name
)
394 if f
.compiles_to_o_file
then ofiles
.add
(o
)
395 if f
.add_to_jar
then java_files
.add
(f
)
398 if not java_files
.is_empty
then
399 var jar_file
= "{outpath}.jar"
401 var class_files_array
= new Array[String]
402 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
403 var class_files
= class_files_array
.join
(" ")
405 makefile
.write
("{jar_file}: {class_files}\n")
406 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
407 dep_rules
.add jar_file
411 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath} {ofiles.join(" ")} $(LDLIBS)\n\n")
413 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
415 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
418 fun compile_c_code
(compiler
: AbstractCompiler, compile_dir
: String)
420 var makename
= makefile_name
(compiler
.mainmodule
)
422 var makeflags
= self.toolcontext
.opt_make_flags
.value
423 if makeflags
== null then makeflags
= ""
424 self.toolcontext
.info
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags}", 2)
427 if self.toolcontext
.verbose_level
>= 3 then
428 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1")
430 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
433 toolcontext
.error
(null, "make failed! Error code: {res}.")
438 # Singleton that store the knowledge about the compilation process
439 abstract class AbstractCompiler
440 type VISITOR: AbstractCompilerVisitor
442 # Table corresponding c_names to nit names (methods)
443 var names
= new HashMap[String, String]
445 # The main module of the program currently compiled
446 # Is assigned during the separate compilation
447 var mainmodule
: MModule writable
449 # The real main module of the program
450 var realmainmodule
: MModule
452 # The modeulbuilder used to know the model and the AST
453 var modelbuilder
: ModelBuilder protected writable
455 # Is hardening asked? (see --hardening)
456 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
458 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
460 self.mainmodule
= mainmodule
461 self.realmainmodule
= mainmodule
462 self.modelbuilder
= modelbuilder
465 # Force the creation of a new file
466 # The point is to avoid contamination between must-be-compiled-separately files
467 fun new_file
(name
: String): CodeFile
469 var f
= new CodeFile(name
)
474 # The list of all associated files
475 # Used to generate .c files
476 var files
: List[CodeFile] = new List[CodeFile]
478 # Initialize a visitor specific for a compiler engine
479 fun new_visitor
: VISITOR is abstract
481 # Where global declaration are stored (the main .h)
482 var header
: CodeWriter writable
484 # Provide a declaration that can be requested (before or latter) by a visitor
485 fun provide_declaration
(key
: String, s
: String)
487 if self.provided_declarations
.has_key
(key
) then
488 assert self.provided_declarations
[key
] == s
490 self.provided_declarations
[key
] = s
493 private var provided_declarations
= new HashMap[String, String]
495 private var requirers_of_declarations
= new HashMap[String, ANode]
497 # Builds the .c and .h files to be used when generating a Stack Trace
498 # Binds the generated C function names to Nit function names
499 fun build_c_to_nit_bindings
501 var compile_dir
= modelbuilder
.compile_dir
503 var stream
= new OFStream.open
("{compile_dir}/c_functions_hash.c")
504 stream
.write
("#include <string.h>\n")
505 stream
.write
("#include <stdlib.h>\n")
506 stream
.write
("#include \"c_functions_hash
.h\
"\n")
507 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
508 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
509 stream
.write
("char* procname = malloc(len+1);")
510 stream
.write
("memcpy(procname, procproc, len);")
511 stream
.write
("procname[len] = '\\0';")
512 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
513 for i
in names
.keys
do
516 stream.write("\
",\"")
517 stream.write(names[i])
518 stream.write("\
"\},\n")
520 stream
.write
("\};\n")
521 stream
.write
("int i;")
522 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
523 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
524 stream
.write
("free(procname);")
525 stream
.write
("return map[i].nit_name;")
528 stream
.write
("free(procname);")
529 stream
.write
("return NULL;")
533 stream
= new OFStream.open
("{compile_dir}/c_functions_hash.h")
534 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
537 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
541 # This method call compile_header_strucs method that has to be refined
542 fun compile_header
do
544 var toolctx
= modelbuilder
.toolcontext
545 self.header
.add_decl
("#include <stdlib.h>")
546 self.header
.add_decl
("#include <stdio.h>")
547 self.header
.add_decl
("#include <string.h>")
548 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
549 self.header
.add_decl
("#ifdef ANDROID")
550 self.header
.add_decl
(" #include <android/log.h>")
551 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
552 self.header
.add_decl
("#else")
553 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
554 self.header
.add_decl
("#endif")
556 compile_header_structs
557 compile_nitni_structs
559 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
560 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
561 # Signal handler function prototype
562 self.header
.add_decl
("void show_backtrace(int);")
564 self.header
.add_decl
("void show_backtrace(int) __attribute__ ((noreturn));")
567 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
568 self.header
.add_decl
("#define likely(x) (x)")
569 self.header
.add_decl
("#define unlikely(x) (x)")
570 else if gccd_disable
.has
("correct-likely") then
571 # invert the `likely` definition
572 # Used by masochists to bench the worst case
573 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
574 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
576 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
577 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
580 # Global variable used by intern methods
581 self.header
.add_decl
("extern int glob_argc;")
582 self.header
.add_decl
("extern char **glob_argv;")
583 self.header
.add_decl
("extern val *glob_sys;")
586 # Declaration of structures for live Nit types
587 protected fun compile_header_structs
is abstract
589 # Declaration of structures for nitni undelying the FFI
590 protected fun compile_nitni_structs
592 self.header
.add_decl
"""
593 /* Native reference to Nit objects */
594 /* This structure is used to represent every Nit type in extern methods and custom C code. */
596 struct nitni_ref *next,
597 *prev; /* adjacent global references in global list */
598 int count; /* number of time this global reference has been marked */
601 /* List of global references from C code to Nit objects */
602 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
603 struct nitni_global_ref_list_t {
604 struct nitni_ref *head, *tail;
606 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
608 /* Initializer of global reference list */
609 extern void nitni_global_ref_list_init();
611 /* Intern function to add a global reference to the list */
612 extern void nitni_global_ref_add( struct nitni_ref *ref );
614 /* Intern function to remove a global reference from the list */
615 extern void nitni_global_ref_remove( struct nitni_ref *ref );
617 /* Increase count on an existing global reference */
618 extern void nitni_global_ref_incr( struct nitni_ref *ref );
620 /* Decrease count on an existing global reference */
621 extern void nitni_global_ref_decr( struct nitni_ref *ref );
625 fun compile_finalizer_function
627 var finalizable_type
= mainmodule
.finalizable_type
628 if finalizable_type
== null then return
630 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
632 if finalize_meth
== null then
633 modelbuilder
.toolcontext
.error
(null, "The `Finalizable` class doesn't declare the `finalize` method.")
637 var v
= self.new_visitor
638 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
639 var recv
= v
.new_expr
("obj", finalizable_type
)
640 v
.send
(finalize_meth
, [recv
])
644 # Generate the main C function.
646 # * allocate the Sys object if it exists
647 # * call init if is exists
648 # * call main if it exists
649 fun compile_main_function
651 var v
= self.new_visitor
652 v
.add_decl
("#include <signal.h>")
653 var ost
= modelbuilder
.toolcontext
.opt_stacktrace
.value
654 var platform
= mainmodule
.target_platform
656 if platform
!= null and not platform
.supports_libunwind
then ost
= "none"
658 var no_main
= (platform
!= null and platform
.no_main
) or modelbuilder
.toolcontext
.opt_no_main
.value
660 if ost
== "nitstack" or ost
== "libunwind" then
661 v
.add_decl
("#define UNW_LOCAL_ONLY")
662 v
.add_decl
("#include <libunwind.h>")
663 if ost
== "nitstack" then
664 v
.add_decl
("#include \"c_functions_hash
.h\
"")
667 v
.add_decl
("int glob_argc;")
668 v
.add_decl
("char **glob_argv;")
669 v
.add_decl
("val *glob_sys;")
671 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
672 for tag
in count_type_test_tags
do
673 v
.add_decl
("long count_type_test_resolved_{tag};")
674 v
.add_decl
("long count_type_test_unresolved_{tag};")
675 v
.add_decl
("long count_type_test_skipped_{tag};")
676 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
677 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
678 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
682 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
683 v
.add_decl
("long count_invoke_by_tables;")
684 v
.add_decl
("long count_invoke_by_direct;")
685 v
.add_decl
("long count_invoke_by_inline;")
686 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
687 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
688 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
691 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
692 v
.add_decl
("long count_attr_reads = 0;")
693 v
.add_decl
("long count_isset_checks = 0;")
694 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
695 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
698 v
.add_decl
("void sig_handler(int signo)\{")
699 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
700 v
.add_decl
("show_backtrace(signo);")
703 v
.add_decl
("void show_backtrace (int signo) \{")
704 if ost
== "nitstack" or ost
== "libunwind" then
705 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
706 v
.add_decl
("unw_cursor_t cursor;")
707 v
.add_decl
("if(opt==NULL)\{")
708 v
.add_decl
("unw_context_t uc;")
709 v
.add_decl
("unw_word_t ip;")
710 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
711 v
.add_decl
("unw_getcontext(&uc);")
712 v
.add_decl
("unw_init_local(&cursor, &uc);")
713 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
714 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
715 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
716 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
717 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
718 if ost
== "nitstack" then
719 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
720 v
.add_decl
(" if (recv != NULL)\{")
721 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
722 v.add_decl(" \}else\{")
723 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
726 v
.add_decl
(" PRINT_ERROR(\"` %s \\n\",procname);")
729 v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
730 v.add_decl("free(procname);")
733 v.add_decl("exit(signo);")
737 v.add_decl("int nit_main(int argc, char** argv) \{")
739 v.add_decl("int main(int argc, char** argv) \{")
742 v.add("signal(SIGABRT, sig_handler);")
743 v.add("signal(SIGFPE, sig_handler);")
744 v.add("signal(SIGILL, sig_handler);")
745 v.add("signal(SIGINT, sig_handler);")
746 v.add("signal(SIGTERM, sig_handler);")
747 v.add("signal(SIGSEGV, sig_handler);")
748 v.add("signal(SIGPIPE, sig_handler);")
750 v.add("glob_argc = argc; glob_argv = argv;")
751 v.add("initialize_gc_option();")
753 v.add "initialize_nitni_global_refs();"
755 var main_type = mainmodule.sys_type
756 if main_type != null then
757 var mainmodule = v.compiler.mainmodule
758 var glob_sys = v.init_instance(main_type)
759 v.add("glob_sys = {glob_sys};")
760 var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
761 if main_init != null then
762 v.send(main_init, [glob_sys])
764 var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
765 mainmodule.try_get_primitive_method("main", main_type.mclass)
766 if main_method != null then
767 v.send(main_method, [glob_sys])
771 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
772 v.add_decl("long count_type_test_resolved_total = 0;")
773 v.add_decl("long count_type_test_unresolved_total = 0;")
774 v.add_decl("long count_type_test_skipped_total = 0;")
775 v.add_decl("long count_type_test_total_total = 0;")
776 for tag in count_type_test_tags do
777 v.add_decl("long count_type_test_total_{tag};")
778 v.add("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
779 v.add("count_type_test_resolved_total += count_type_test_resolved_{tag};")
780 v.add("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
781 v.add("count_type_test_skipped_total += count_type_test_skipped_{tag};")
782 v.add("count_type_test_total_total += count_type_test_total_{tag};")
784 v.add("printf(\"# dynamic count_type_test: total %l\\n\");")
785 v.add("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\");")
786 var tags = count_type_test_tags.to_a
789 v.add("printf(\"{tag}\");")
790 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
791 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
792 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
793 v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
797 if self.modelbuilder.toolcontext.opt_invocation_metrics.value then
798 v.add_decl("long count_invoke_total;")
799 v.add("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
800 v.add("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
801 v.add("printf(\"by table: %ld (%.2f%%)\\n\", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
802 v.add("printf(\"direct: %ld (%.2f%%)\\n\", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
803 v.add("printf(\"inlined: %ld (%.2f%%)\\n\", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
806 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
807 v.add("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
808 v.add("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
815 # Copile all C functions related to the [incr|decr]_ref features of the FFI
816 fun compile_nitni_global_ref_functions
818 var v = self.new_visitor
820 struct nitni_global_ref_list_t *nitni_global_ref_list;
821 void initialize_nitni_global_refs() {
822 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
823 nitni_global_ref_list->head = NULL;
824 nitni_global_ref_list->tail = NULL;
827 void nitni_global_ref_add( struct nitni_ref *ref ) {
828 if ( nitni_global_ref_list->head == NULL ) {
829 nitni_global_ref_list->head = ref;
832 nitni_global_ref_list->tail->next = ref;
833 ref->prev = nitni_global_ref_list->tail;
835 nitni_global_ref_list->tail = ref;
840 void nitni_global_ref_remove( struct nitni_ref *ref ) {
841 if ( ref->prev == NULL ) {
842 nitni_global_ref_list->head = ref->next;
844 ref->prev->next = ref->next;
847 if ( ref->next == NULL ) {
848 nitni_global_ref_list->tail = ref->prev;
850 ref->next->prev = ref->prev;
854 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
855 if ( ref->count == 0 ) /* not registered */
858 nitni_global_ref_add( ref );
864 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
865 if ( ref->count == 1 ) /* was last reference */
867 /* remove from list */
868 nitni_global_ref_remove( ref );
876 # List of additional files required to compile (FFI)
877 var extern_bodies = new Array[ExternFile]
879 # List of source files to copy over to the compile dir
880 var files_to_copy = new Array[String]
882 # This is used to avoid adding an extern file more than once
883 private var seen_extern = new ArraySet[String]
885 # Generate code that initialize the attributes on a new instance
886 fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
888 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
889 self.mainmodule.linearize_mclassdefs(cds)
891 var n = self.modelbuilder.mclassdef2nclassdef[cd]
892 for npropdef in n.n_propdefs do
893 if npropdef isa AAttrPropdef then
894 npropdef.init_expr(v, recv)
900 # Generate code that check if an attribute is correctly initialized
901 fun generate_check_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
903 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
904 self.mainmodule.linearize_mclassdefs(cds)
906 var n = self.modelbuilder.mclassdef2nclassdef[cd]
907 for npropdef in n.n_propdefs do
908 if npropdef isa AAttrPropdef then
909 npropdef.check_expr(v, recv)
917 var count_type_test_tags: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
918 var count_type_test_resolved: HashMap[String, Int] = init_count_type_test_tags
919 var count_type_test_unresolved: HashMap[String, Int] = init_count_type_test_tags
920 var count_type_test_skipped: HashMap[String, Int] = init_count_type_test_tags
922 protected fun init_count_type_test_tags: HashMap[String, Int]
924 var res = new HashMap[String, Int]
925 for tag in count_type_test_tags do
931 # Display stats about compilation process
933 # * type tests against resolved types (`x
isa Collection[Animal]`)
934 # * type tests against unresolved types (`x
isa Collection[E
]`)
935 # * type tests skipped
940 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
941 print "# static count_type_test"
942 print "\tresolved:\tunresolved\tskipped\ttotal"
943 var count_type_test_total = init_count_type_test_tags
944 count_type_test_resolved["total"] = 0
945 count_type_test_unresolved["total"] = 0
946 count_type_test_skipped["total"] = 0
947 count_type_test_total["total"] = 0
948 for tag in count_type_test_tags do
949 count_type_test_total[tag] = count_type_test_resolved[tag] + count_type_test_unresolved[tag] + count_type_test_skipped[tag]
950 count_type_test_resolved["total"] += count_type_test_resolved[tag]
951 count_type_test_unresolved["total"] += count_type_test_unresolved[tag]
952 count_type_test_skipped["total"] += count_type_test_skipped[tag]
953 count_type_test_total["total"] += count_type_test_total[tag]
955 var count_type_test = count_type_test_total["total"]
956 var tags = count_type_test_tags.to_a
960 printn "\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
961 printn "\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
962 printn "\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
963 printn "\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
969 fun finalize_ffi_for_module(mmodule: MModule) do mmodule.finalize_ffi(self)
972 # Avoid division by zero by returning the string "n/a"
973 fun div(a,b:Int):String
975 if b == 0 then return "n/a"
976 return ((a*10000/b).to_f / 100.0).to_precision(2)
980 # A file unit (may be more than one file if
981 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
984 var writers = new Array[CodeWriter]
985 var required_declarations = new HashSet[String]
988 # Where to store generated lines
991 var lines: List[String] = new List[String]
992 var decl_lines: List[String] = new List[String]
994 # Add a line in the main part of the generated C
995 fun add(s: String) do self.lines.add(s)
998 # (used for local or global declaration)
999 fun add_decl(s: String) do self.decl_lines.add(s)
1001 init(file: CodeFile)
1004 file.writers.add(self)
1008 # A visitor on the AST of property definition that generate the C code.
1009 abstract class AbstractCompilerVisitor
1011 type COMPILER: AbstractCompiler
1013 # The associated compiler
1014 var compiler: COMPILER
1016 # The current visited AST node
1017 var current_node: nullable ANode writable = null
1019 # The current `Frame`
1020 var frame: nullable Frame writable
1022 # Alias for self.compiler.mainmodule.object_type
1023 fun object_type: MClassType do return self.compiler.mainmodule.object_type
1025 # Alias for self.compiler.mainmodule.bool_type
1026 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
1028 var writer: CodeWriter
1030 init(compiler: COMPILER)
1032 self.compiler = compiler
1033 self.writer = new CodeWriter(compiler.files.last)
1036 # Force to get the primitive class named `name
` or abort
1037 fun get_class(name: String): MClass do return self.compiler.mainmodule.get_primitive_class(name)
1039 # Force to get the primitive property named `name
` in the instance `recv
` or abort
1040 fun get_property(name: String, recv: MType): MMethod
1042 assert recv isa MClassType
1043 return self.compiler.modelbuilder.force_get_primitive_method(self.current_node.as(not null), name, recv.mclass, self.compiler.mainmodule)
1046 fun compile_callsite(callsite: CallSite, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1048 var initializers = callsite.mpropdef.initializers
1049 if not initializers.is_empty then
1050 var recv = arguments.first
1052 assert initializers.length == arguments.length - 1 else debug("expected {initializers.length}, got {arguments.length - 1}")
1054 for p in initializers do
1055 if p isa MMethod then
1056 self.send(p, [recv, arguments[i]])
1057 else if p isa MAttribute then
1058 self.write_attribute(p, recv, arguments[i])
1063 return self.send(callsite.mproperty, [recv])
1066 return self.send(callsite.mproperty, arguments)
1069 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable is abstract
1071 fun calloc_array(ret_type: MType, arguments: Array[RuntimeVariable]) is abstract
1073 fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract
1075 # Transform varargs, in raw arguments, into a single argument of type `Array`
1076 # Note: this method modify the given `args
`
1077 # If there is no vararg, then `args
` is not modified.
1078 fun varargize(mpropdef: MPropDef, msignature: MSignature, args: Array[RuntimeVariable])
1080 var recv = args.first
1081 var vararg_rank = msignature.vararg_rank
1082 if vararg_rank >= 0 then
1083 assert args.length >= msignature.arity + 1 # because of self
1085 args = new Array[RuntimeVariable]
1087 args.add(rawargs.first) # recv
1089 for i in [0..vararg_rank[ do
1090 args.add(rawargs[i+1])
1093 var vararg_lastrank = vararg_rank + rawargs.length-1-msignature.arity
1094 var vararg = new Array[RuntimeVariable]
1095 for i in [vararg_rank..vararg_lastrank] do
1096 vararg.add(rawargs[i+1])
1099 var elttype = msignature.mparameters[vararg_rank].mtype
1100 args.add(self.vararg_instance(mpropdef, recv, vararg, elttype))
1102 for i in [vararg_lastrank+1..rawargs.length-1[ do
1103 args.add(rawargs[i+1])
1106 rawargs.add_all(args)
1112 # Anchor a type to the main module and the current receiver
1113 fun anchor(mtype: MType): MType
1115 if not mtype.need_anchor then return mtype
1116 return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
1119 fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
1121 if not mtype.need_anchor then return mtype
1122 return mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
1125 # Unsafely cast a value to a new type
1126 # ie the result share the same C variable but my have a different mcasttype
1127 # NOTE: if the adaptation is useless then `value
` is returned as it.
1128 # ENSURE: `result
.name
== value
.name
`
1129 fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
1131 mtype = self.anchor(mtype)
1132 var valmtype = value.mcasttype
1133 if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1137 if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1138 var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
1141 var res = new RuntimeVariable(value.name, valmtype, mtype)
1146 # Generate a super call from a method definition
1147 fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1149 fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1151 # Box or unbox a value to another type iff a C type conversion is needed
1152 # ENSURE: `result
.mtype
.ctype
== mtype
.ctype
`
1153 fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1155 # Generate a polymorphic subtype test
1156 fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
1158 # Generate the code required to dynamically check if 2 objects share the same runtime type
1159 fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1161 # Generate a Nit "is" for two runtime_variables
1162 fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1166 # Generate a static call on a method definition
1167 fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1169 # Generate a polymorphic send for the method `m
` and the arguments `args
`
1170 fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1172 # Generate a monomorphic send for the method `m
`, the type `t
` and the arguments `args
`
1173 fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1175 assert t isa MClassType
1176 var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
1177 return self.call(propdef, t, args)
1180 # Generate a monomorphic super send from the method `m
`, the type `t
` and the arguments `args
`
1181 fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1183 assert t isa MClassType
1184 m = m.lookup_next_definition(self.compiler.mainmodule, t)
1185 return self.call(m, t, args)
1188 # Attributes handling
1190 # Generate a polymorphic attribute is_set test
1191 fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1193 # Generate a polymorphic attribute read
1194 fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1196 # Generate a polymorphic attribute write
1197 fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable) is abstract
1201 # Add a check and an abort for a null reciever if needed
1202 fun check_recv_notnull(recv: RuntimeVariable)
1204 if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
1206 var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType
1208 self.add("if (unlikely({recv} == NULL)) \{")
1209 self.add_abort("Receiver is null")
1216 private var names: HashSet[String] = new HashSet[String]
1217 private var last: Int = 0
1219 # Return a new name based on `s
` and unique in the visitor
1220 fun get_name(s: String): String
1222 if not self.names.has(s) then
1226 var i = self.last + 1
1229 if not self.names.has(s2) then
1238 # Return an unique and stable identifier associated with an escapemark
1239 fun escapemark_name(e: nullable EscapeMark): String
1242 if escapemark_names.has_key(e) then return escapemark_names[e]
1244 if name == null then name = "label"
1245 name = get_name(name)
1246 escapemark_names[e] = name
1250 private var escapemark_names = new HashMap[EscapeMark, String]
1252 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1253 # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
1254 fun class_name_string(value: RuntimeVariable): String is abstract
1256 # Variables handling
1258 protected var variables: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
1260 # Return the local runtime_variable associated to a Nit local variable
1261 fun variable(variable: Variable): RuntimeVariable
1263 if self.variables.has_key(variable) then
1264 return self.variables[variable]
1266 var name = self.get_name("var_{variable.name}")
1267 var mtype = variable.declared_type.as(not null)
1268 mtype = self.anchor(mtype)
1269 var res = new RuntimeVariable(name, mtype, mtype)
1270 self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1271 self.variables[variable] = res
1276 # Return a new uninitialized local runtime_variable
1277 fun new_var(mtype: MType): RuntimeVariable
1279 mtype = self.anchor(mtype)
1280 var name = self.get_name("var")
1281 var res = new RuntimeVariable(name, mtype, mtype)
1282 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1286 # Return a new uninitialized named runtime_variable
1287 fun new_named_var(mtype: MType, name: String): RuntimeVariable
1289 mtype = self.anchor(mtype)
1290 var res = new RuntimeVariable(name, mtype, mtype)
1291 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1295 # Correctly assign a left and a right value
1296 # Boxing and unboxing is performed if required
1297 fun assign(left, right: RuntimeVariable)
1299 right = self.autobox(right, left.mtype)
1300 self.add("{left} = {right};")
1303 # Generate instances
1305 # Generate a alloc-instance + init-attributes
1306 fun init_instance(mtype: MClassType): RuntimeVariable is abstract
1308 # Set a GC finalizer on `recv
`, only if `recv
` isa Finalizable
1309 fun set_finalizer(recv: RuntimeVariable)
1311 var mtype = recv.mtype
1312 var finalizable_type = compiler.mainmodule.finalizable_type
1313 if finalizable_type != null and not mtype.need_anchor and
1314 mtype.is_subtype(compiler.mainmodule, null, finalizable_type) then
1315 add "gc_register_finalizer({recv});"
1319 # Generate an integer value
1320 fun int_instance(value: Int): RuntimeVariable
1322 var res = self.new_var(self.get_class("Int").mclass_type)
1323 self.add("{res} = {value};")
1327 # Generate a string value
1328 fun string_instance(string: String): RuntimeVariable
1330 var mtype = self.get_class("String").mclass_type
1331 var name = self.get_name("varonce")
1332 self.add_decl("static {mtype.ctype} {name};")
1333 var res = self.new_var(mtype)
1334 self.add("if ({name}) \{")
1335 self.add("{res} = {name};")
1336 self.add("\} else \{")
1337 var native_mtype = self.get_class("NativeString").mclass_type
1338 var nat = self.new_var(native_mtype)
1339 self.add("{nat} = \"{string.escape_to_c}\";")
1340 var length = self.int_instance(string.length)
1341 self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
1342 self.add("{name} = {res};")
1347 # Generate an array value
1348 fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1350 # Get an instance of a array for a vararg
1351 fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1355 # Add a line in the main part of the generated C
1356 fun add(s: String) do self.writer.lines.add(s)
1359 # (used for local or global declaration)
1360 fun add_decl(s: String) do self.writer.decl_lines.add(s)
1362 # Request the presence of a global declaration
1363 fun require_declaration(key: String)
1365 var reqs = self.writer.file.required_declarations
1366 if reqs.has(key) then return
1368 var node = current_node
1369 if node != null then compiler.requirers_of_declarations[key] = node
1372 # Add a declaration in the local-header
1373 # The declaration is ensured to be present once
1374 fun declare_once(s: String)
1376 self.compiler.provide_declaration(s, s)
1377 self.require_declaration(s)
1380 # look for a needed .h and .c file for a given .nit source-file
1381 # FIXME: bad API, parameter should be a `MModule`, not its source-file
1382 fun add_extern(file: String)
1384 file = file.strip_extension(".nit")
1385 var tryfile = file + ".nit.h"
1386 if tryfile.file_exists then
1387 self.declare_once("#include \"{tryfile.basename("")}\"")
1388 self.compiler.files_to_copy.add(tryfile)
1390 tryfile = file + "_nit.h"
1391 if tryfile.file_exists then
1392 self.declare_once("#include \"{tryfile.basename("")}\"")
1393 self.compiler.files_to_copy.add(tryfile)
1396 if self.compiler.seen_extern.has(file) then return
1397 self.compiler.seen_extern.add(file)
1398 tryfile = file + ".nit.c"
1399 if not tryfile.file_exists then
1400 tryfile = file + "_nit.c"
1401 if not tryfile.file_exists then return
1403 var f = new ExternCFile(tryfile.basename(""), "")
1404 self.compiler.extern_bodies.add(f)
1405 self.compiler.files_to_copy.add(tryfile)
1408 # Return a new local runtime_variable initialized with the C expression `cexpr
`.
1409 fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
1411 var res = new_var(mtype)
1412 self.add("{res} = {cexpr};")
1416 # Generate generic abort
1417 # used by aborts, asserts, casts, etc.
1418 fun add_abort(message: String)
1420 self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
1426 if self.current_node != null and self.current_node.location.file != null then
1427 self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
1429 self.add("PRINT_ERROR(\"\\n\");")
1431 self.add("show_backtrace(1);")
1434 # Add a dynamic cast
1435 fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)
1437 var res = self.type_test(value, mtype, tag)
1438 self.add("if (unlikely(!{res})) \{")
1439 var cn = self.class_name_string(value)
1440 self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s
`, got `%s
`\", \"{mtype.to_s.escape_to_c}\", {cn});")
1445 # Generate a return with the value `s
`
1446 fun ret(s: RuntimeVariable)
1448 self.assign(self.frame.returnvar.as(not null), s)
1449 self.add("goto {self.frame.returnlabel.as(not null)};")
1452 # Compile a statement (if any)
1453 fun stmt(nexpr: nullable AExpr)
1455 if nexpr == null then return
1456 var old = self.current_node
1457 self.current_node = nexpr
1459 self.current_node = old
1462 # Compile an expression an return its result
1463 # `mtype
` is the expected return type, pass null if no specific type is expected.
1464 fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
1466 var old = self.current_node
1467 self.current_node = nexpr
1468 var res = nexpr.expr(self).as(not null)
1469 if mtype != null then
1470 mtype = self.anchor(mtype)
1471 res = self.autobox(res, mtype)
1473 res = autoadapt(res, nexpr.mtype.as(not null))
1474 var implicit_cast_to = nexpr.implicit_cast_to
1475 if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
1476 add_cast(res, implicit_cast_to, "auto")
1477 res = autoadapt(res, implicit_cast_to)
1479 self.current_node = old
1483 # Alias for `self.expr
(nexpr
, self.bool_type
)`
1484 fun expr_bool(nexpr: AExpr): RuntimeVariable do return expr(nexpr, bool_type)
1486 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1487 fun debug(message: String)
1489 var node = self.current_node
1490 if node == null then
1491 print "?: {message}"
1495 self.add("/* DEBUG: {message} */")
1499 # A C function associated to a Nit method
1500 # Because of customization, a given Nit method can be compiler more that once
1501 abstract class AbstractRuntimeFunction
1503 type COMPILER: AbstractCompiler
1504 type VISITOR: AbstractCompilerVisitor
1506 # The associated Nit method
1507 var mmethoddef: MMethodDef
1509 # The mangled c name of the runtime_function
1510 # Subclasses should redefine `build_c_name
` instead
1513 var res = self.c_name_cache
1514 if res != null then return res
1515 res = self.build_c_name
1516 self.c_name_cache = res
1520 # Non cached version of `c_name
`
1521 protected fun build_c_name: String is abstract
1523 protected var c_name_cache: nullable String writable = null
1525 # Implements a call of the runtime_function
1526 # May inline the body or generate a C function call
1527 fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1529 # Generate the code for the `AbstractRuntimeFunction`
1530 # Warning: compile more than once compilation makes CC unhappy
1531 fun compile_to_c(compiler: COMPILER) is abstract
1534 # A runtime variable hold a runtime value in C.
1535 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
1537 # 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.
1538 class RuntimeVariable
1539 # The name of the variable in the C code
1542 # The static type of the variable (as declard in C)
1545 # The current casted type of the variable (as known in Nit)
1546 var mcasttype: MType writable
1548 # If the variable exaclty a mcasttype?
1549 # false (usual value) means that the variable is a mcasttype or a subtype.
1550 var is_exact: Bool writable = false
1552 init(name: String, mtype: MType, mcasttype: MType)
1556 self.mcasttype = mcasttype
1557 assert not mtype.need_anchor
1558 assert not mcasttype.need_anchor
1561 redef fun to_s do return name
1566 if self.is_exact then
1567 exact_str = " exact"
1572 if self.mtype == self.mcasttype then
1573 type_str = "{mtype}{exact_str}"
1575 type_str = "{mtype}({mcasttype}{exact_str})"
1577 return "<{name}:{type_str}>"
1581 # A frame correspond to a visited property in a `GlobalCompilerVisitor`
1584 type VISITOR: AbstractCompilerVisitor
1586 # The associated visitor
1587 var visitor: VISITOR
1589 # The executed property.
1590 # A Method in case of a call, an attribute in case of a default initialization.
1591 var mpropdef: MPropDef
1593 # The static type of the receiver
1594 var receiver: MClassType
1596 # Arguments of the method (the first is the receiver)
1597 var arguments: Array[RuntimeVariable]
1599 # The runtime_variable associated to the return (in a function)
1600 var returnvar: nullable RuntimeVariable writable = null
1602 # The label at the end of the property
1603 var returnlabel: nullable String writable = null
1607 # Return the C type associated to a given Nit static type
1608 fun ctype: String do return "val*"
1610 fun ctypename: String do return "val"
1612 # Return the name of the C structure associated to a Nit live type
1613 fun c_name: String is abstract
1614 protected var c_name_cache: nullable String protected writable
1617 redef class MClassType
1620 var res = self.c_name_cache
1621 if res != null then return res
1622 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1623 self.c_name_cache = res
1627 redef fun ctype: String
1629 if mclass.name == "Int" then
1631 else if mclass.name == "Bool" then
1633 else if mclass.name == "Char" then
1635 else if mclass.name == "Float" then
1637 else if mclass.name == "NativeString" then
1639 else if mclass.name == "NativeArray" then
1641 else if mclass.kind == extern_kind then
1648 redef fun ctypename: String
1650 if mclass.name == "Int" then
1652 else if mclass.name == "Bool" then
1654 else if mclass.name == "Char" then
1656 else if mclass.name == "Float" then
1658 else if mclass.name == "NativeString" then
1660 else if mclass.name == "NativeArray" then
1661 #return "{self.arguments.first.ctype}*"
1663 else if mclass.kind == extern_kind then
1671 redef class MGenericType
1674 var res = self.c_name_cache
1675 if res != null then return res
1677 for t in self.arguments do
1678 res = res + t.c_name
1680 self.c_name_cache = res
1685 redef class MParameterType
1688 var res = self.c_name_cache
1689 if res != null then return res
1690 res = "{self.mclass.c_name}_FT{self.rank}"
1691 self.c_name_cache = res
1696 redef class MVirtualType
1699 var res = self.c_name_cache
1700 if res != null then return res
1701 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1702 self.c_name_cache = res
1707 redef class MNullableType
1710 var res = self.c_name_cache
1711 if res != null then return res
1712 res = "nullable_{self.mtype.c_name}"
1713 self.c_name_cache = res
1719 # Return the name of the C structure associated to a Nit class
1720 fun c_name: String do
1721 var res = self.c_name_cache
1722 if res != null then return res
1723 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1724 self.c_name_cache = res
1727 private var c_name_cache: nullable String
1730 redef class MProperty
1731 fun c_name: String do
1732 var res = self.c_name_cache
1733 if res != null then return res
1734 res = "{self.intro.c_name}"
1735 self.c_name_cache = res
1738 private var c_name_cache: nullable String
1741 redef class MPropDef
1742 type VISITOR: AbstractCompilerVisitor
1744 private var c_name_cache: nullable String
1746 # The mangled name associated to the property
1749 var res = self.c_name_cache
1750 if res != null then return res
1751 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1752 self.c_name_cache = res
1757 redef class MMethodDef
1758 # Can the body be inlined?
1759 fun can_inline(v: VISITOR): Bool
1761 if is_abstract then return true
1762 var modelbuilder = v.compiler.modelbuilder
1763 if modelbuilder.mpropdef2npropdef.has_key(self) then
1764 var npropdef = modelbuilder.mpropdef2npropdef[self]
1765 return npropdef.can_inline
1766 else if self.mproperty.name == "init" then
1767 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1774 # Inline the body in another visitor
1775 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1777 var modelbuilder = v.compiler.modelbuilder
1778 if modelbuilder.mpropdef2npropdef.has_key(self) then
1779 var npropdef = modelbuilder.mpropdef2npropdef[self]
1780 var oldnode = v.current_node
1781 v.current_node = npropdef
1782 self.compile_parameter_check(v, arguments)
1783 npropdef.compile_to_c(v, self, arguments)
1784 v.current_node = oldnode
1785 else if self.mproperty.name == "init" then
1786 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1787 var oldnode = v.current_node
1788 v.current_node = nclassdef
1789 self.compile_parameter_check(v, arguments)
1790 nclassdef.compile_to_c(v, self, arguments)
1791 v.current_node = oldnode
1798 # Generate type checks in the C code to check covariant parameters
1799 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1801 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1803 for i in [0..msignature.arity[ do
1804 # skip test for vararg since the array is instantiated with the correct polymorphic type
1805 if msignature.vararg_rank == i then continue
1807 # skip if the cast is not required
1808 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1809 if not origmtype.need_anchor then continue
1811 # get the parameter type
1812 var mtype = self.msignature.mparameters[i].mtype
1815 # note that v decides if and how to implements the cast
1816 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1817 v.add_cast(arguments[i+1], mtype, "covariance")
1824 redef class APropdef
1825 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1827 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1828 debug("Not yet implemented")
1831 fun can_inline: Bool do return true
1834 redef class AMethPropdef
1835 redef fun compile_to_c(v, mpropdef, arguments)
1837 if mpropdef.is_abstract then
1838 var cn = v.class_name_string(arguments.first)
1839 v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s
` called on `%s
`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1844 # Call the implicit super-init
1845 var auto_super_inits = self.auto_super_inits
1846 if auto_super_inits != null then
1847 var args = [arguments.first]
1848 for auto_super_init in auto_super_inits do
1849 assert auto_super_init.mproperty != mpropdef.mproperty
1851 for i in [0..auto_super_init.msignature.arity+1[ do
1852 args.add(arguments[i])
1854 assert auto_super_init.mproperty != mpropdef.mproperty
1855 v.compile_callsite(auto_super_init, args)
1858 if auto_super_call then
1859 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
1862 var n_block = n_block
1863 if n_block != null then
1864 for i in [0..mpropdef.msignature.arity[ do
1865 var variable = self.n_signature.n_params[i].variable.as(not null)
1866 v.assign(v.variable(variable), arguments[i+1])
1869 else if mpropdef.is_intern then
1870 compile_intern_to_c(v, mpropdef, arguments)
1871 else if mpropdef.is_extern then
1872 if mpropdef.mproperty.is_init then
1873 compile_externinit_to_c(v, mpropdef, arguments)
1875 compile_externmeth_to_c(v, mpropdef, arguments)
1882 redef fun can_inline
1884 if self.auto_super_inits != null then return false
1885 var nblock = self.n_block
1886 if nblock == null then return true
1887 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1888 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1892 fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1894 var pname = mpropdef.mproperty.name
1895 var cname = mpropdef.mclassdef.mclass.name
1896 var ret = mpropdef.msignature.return_mtype
1898 ret = v.resolve_for(ret, arguments.first)
1899 else if mpropdef.mproperty.is_new then
1900 ret = arguments.first.mcasttype
1902 if pname != "==" and pname != "!=" then
1903 v.adapt_signature(mpropdef, arguments)
1905 if cname == "Int" then
1906 if pname == "output" then
1907 v.add("printf(\"%ld\\n\", {arguments.first});")
1909 else if pname == "object_id" then
1910 v.ret(arguments.first)
1912 else if pname == "+" then
1913 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1915 else if pname == "-" then
1916 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1918 else if pname == "unary -" then
1919 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1921 else if pname == "*" then
1922 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1924 else if pname == "/" then
1925 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1927 else if pname == "%" then
1928 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
1930 else if pname == "lshift" then
1931 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
1933 else if pname == "rshift" then
1934 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
1936 else if pname == "==" then
1937 v.ret(v.equal_test(arguments[0], arguments[1]))
1939 else if pname == "!=" then
1940 var res = v.equal_test(arguments[0], arguments[1])
1941 v.ret(v.new_expr("!{res}", ret.as(not null)))
1943 else if pname == "<" then
1944 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1946 else if pname == ">" then
1947 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1949 else if pname == "<=" then
1950 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1952 else if pname == ">=" then
1953 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1955 else if pname == "to_f" then
1956 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
1958 else if pname == "ascii" then
1959 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1962 else if cname == "Char" then
1963 if pname == "output" then
1964 v.add("printf(\"%c\", {arguments.first});")
1966 else if pname == "object_id" then
1967 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
1969 else if pname == "successor" then
1970 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1972 else if pname == "predecessor" then
1973 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1975 else if pname == "==" then
1976 v.ret(v.equal_test(arguments[0], arguments[1]))
1978 else if pname == "!=" then
1979 var res = v.equal_test(arguments[0], arguments[1])
1980 v.ret(v.new_expr("!{res}", ret.as(not null)))
1982 else if pname == "<" then
1983 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1985 else if pname == ">" then
1986 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1988 else if pname == "<=" then
1989 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1991 else if pname == ">=" then
1992 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1994 else if pname == "to_i" then
1995 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
1997 else if pname == "ascii" then
1998 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
2001 else if cname == "Bool" then
2002 if pname == "output" then
2003 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
2005 else if pname == "object_id" then
2006 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2008 else if pname == "==" then
2009 v.ret(v.equal_test(arguments[0], arguments[1]))
2011 else if pname == "!=" then
2012 var res = v.equal_test(arguments[0], arguments[1])
2013 v.ret(v.new_expr("!{res}", ret.as(not null)))
2016 else if cname == "Float" then
2017 if pname == "output" then
2018 v.add("printf(\"%f\\n\", {arguments.first});")
2020 else if pname == "object_id" then
2021 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
2023 else if pname == "+" then
2024 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2026 else if pname == "-" then
2027 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2029 else if pname == "unary -" then
2030 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2032 else if pname == "succ" then
2033 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
2035 else if pname == "prec" then
2036 v.ret(v.new_expr("{arguments[0]}-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.equal_test(arguments[0], arguments[1]))
2047 else if pname == "!=" then
2048 var res = v.equal_test(arguments[0], arguments[1])
2049 v.ret(v.new_expr("!{res}", ret.as(not null)))
2051 else if pname == "<" then
2052 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2054 else if pname == ">" then
2055 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2057 else if pname == "<=" then
2058 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2060 else if pname == ">=" then
2061 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2063 else if pname == "to_i" then
2064 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
2067 else if cname == "NativeString" then
2068 if pname == "[]" then
2069 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
2071 else if pname == "[]=" then
2072 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2074 else if pname == "copy_to" then
2075 v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2077 else if pname == "atoi" then
2078 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
2080 else if pname == "init" then
2081 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2084 else if cname == "NativeArray" then
2085 v.native_array_def(pname, ret, arguments)
2088 if pname == "exit" then
2089 v.add("exit({arguments[1]});")
2091 else if pname == "sys" then
2092 v.ret(v.new_expr("glob_sys", ret.as(not null)))
2094 else if pname == "calloc_string" then
2095 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2097 else if pname == "calloc_array" then
2098 v.calloc_array(ret.as(not null), arguments)
2100 else if pname == "object_id" then
2101 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2103 else if pname == "is_same_type" then
2104 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
2106 else if pname == "is_same_instance" then
2107 v.ret(v.equal_test(arguments[0], arguments[1]))
2109 else if pname == "output_class_name" then
2110 var nat = v.class_name_string(arguments.first)
2111 v.add("printf(\"%s\\n\", {nat});")
2113 else if pname == "native_class_name" then
2114 var nat = v.class_name_string(arguments.first)
2115 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
2117 else if pname == "force_garbage_collection" then
2118 v.add("nit_gcollect();")
2120 else if pname == "native_argc" then
2121 v.ret(v.new_expr("glob_argc", ret.as(not null)))
2123 else if pname == "native_argv" then
2124 v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
2127 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
2128 debug("Not implemented {mpropdef}")
2131 fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2134 var nextern = self.n_extern
2135 if nextern == null then
2136 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
2137 v.add("show_backtrace(1);")
2140 externname = nextern.text.substring(1, nextern.text.length-2)
2141 if location.file != null then
2142 var file = location.file.filename
2145 var res: nullable RuntimeVariable = null
2146 var ret = mpropdef.msignature.return_mtype
2148 ret = v.resolve_for(ret, arguments.first)
2149 res = v.new_var(ret)
2151 v.adapt_signature(mpropdef, arguments)
2154 v.add("{externname}({arguments.join(", ")});")
2156 v.add("{res} = {externname}({arguments.join(", ")});")
2161 fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2164 var nextern = self.n_extern
2165 if nextern == null then
2166 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
2167 v.add("show_backtrace(1);")
2170 externname = nextern.text.substring(1, nextern.text.length-2)
2171 if location.file != null then
2172 var file = location.file.filename
2175 v.adapt_signature(mpropdef, arguments)
2176 var ret = arguments.first.mtype
2177 var res = v.new_var(ret)
2181 v.add("{res} = {externname}({arguments.join(", ")});")
2186 redef class AAttrPropdef
2187 redef fun compile_to_c(v, mpropdef, arguments)
2189 if mpropdef == mreadpropdef then
2190 assert arguments.length == 1
2194 assert nexpr != null
2196 var ret = self.mpropdef.static_mtype
2197 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2198 var guard = self.mlazypropdef.mproperty
2200 set = v.isset_attribute(self.mpropdef.mproperty, arguments.first)
2202 set = v.read_attribute(guard, arguments.first)
2204 v.add("if(likely({set})) \{")
2205 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2207 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2208 v.write_attribute(self.mpropdef.mproperty, arguments.first, value)
2209 v.assign(res, value)
2211 var true_v = v.new_expr("1", v.bool_type)
2212 v.write_attribute(guard, arguments.first, true_v)
2216 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2218 v.assign(v.frame.returnvar.as(not null), res)
2219 else if mpropdef == mwritepropdef then
2220 assert arguments.length == 2
2221 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
2223 var ret = self.mpropdef.static_mtype
2224 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2226 v.write_attribute(self.mlazypropdef.mproperty, arguments.first, v.new_expr("1", v.bool_type))
2234 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2236 var nexpr = self.n_expr
2237 if nexpr != null and not is_lazy then
2238 var oldnode = v.current_node
2239 v.current_node = self
2240 var old_frame = v.frame
2241 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2243 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2244 v.write_attribute(self.mpropdef.mproperty, recv, value)
2246 v.current_node = oldnode
2250 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2252 var nexpr = self.n_expr
2253 if nexpr != null then return
2255 var oldnode = v.current_node
2256 v.current_node = self
2257 var old_frame = v.frame
2258 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2260 # Force read to check the initialization
2261 v.read_attribute(self.mpropdef.mproperty, recv)
2263 v.current_node = oldnode
2267 redef class AClassdef
2268 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2270 if mpropdef == self.mfree_init then
2271 if mpropdef.mproperty.is_root_init then
2272 assert self.super_inits == null
2273 assert arguments.length == 1
2274 if not mpropdef.is_intro then
2275 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
2280 var super_inits = self.super_inits
2281 if super_inits != null then
2282 var args_of_super = arguments
2283 if arguments.length > 1 then args_of_super = [arguments.first]
2284 for su in super_inits do
2285 v.send(su, args_of_super)
2289 var recv = arguments.first
2291 # Collect undefined attributes
2292 for npropdef in self.n_propdefs do
2293 if npropdef isa AAttrPropdef and npropdef.n_expr == null and not npropdef.noinit then
2294 v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
2305 # Try to compile self as an expression
2306 # Do not call this method directly, use `v
.expr
` instead
2307 private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
2309 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
2310 var mtype = self.mtype
2311 if mtype == null then
2314 var res = v.new_var(mtype)
2315 v.add("/* {res} = NOT YET {class_name} */")
2320 # Try to compile self as a statement
2321 # Do not call this method directly, use `v
.stmt
` instead
2322 private fun stmt(v: AbstractCompilerVisitor)
2325 if res != null then v.add("{res};")
2329 redef class ABlockExpr
2332 for e in self.n_expr do v.stmt(e)
2336 var last = self.n_expr.last
2337 for e in self.n_expr do
2338 if e == last then break
2341 return v.expr(last, null)
2345 redef class AVardeclExpr
2348 var variable = self.variable.as(not null)
2349 var ne = self.n_expr
2351 var i = v.expr(ne, variable.declared_type)
2352 v.assign(v.variable(variable), i)
2357 redef class AVarExpr
2360 var res = v.variable(self.variable.as(not null))
2361 var mtype = self.mtype.as(not null)
2362 return v.autoadapt(res, mtype)
2366 redef class AVarAssignExpr
2369 var variable = self.variable.as(not null)
2370 var i = v.expr(self.n_value, variable.declared_type)
2371 v.assign(v.variable(variable), i)
2375 var variable = self.variable.as(not null)
2376 var i = v.expr(self.n_value, variable.declared_type)
2377 v.assign(v.variable(variable), i)
2382 redef class AVarReassignExpr
2385 var variable = self.variable.as(not null)
2386 var vari = v.variable(variable)
2387 var value = v.expr(self.n_value, variable.declared_type)
2388 var res = v.compile_callsite(self.reassign_callsite.as(not null), [vari, value])
2390 v.assign(v.variable(variable), res)
2394 redef class ASelfExpr
2395 redef fun expr(v) do return v.frame.arguments.first
2398 redef class AContinueExpr
2399 redef fun stmt(v) do v.add("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
2402 redef class ABreakExpr
2403 redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
2406 redef class AReturnExpr
2409 var nexpr = self.n_expr
2410 if nexpr != null then
2411 var returnvar = v.frame.returnvar.as(not null)
2412 var i = v.expr(nexpr, returnvar.mtype)
2413 v.assign(returnvar, i)
2415 v.add("goto {v.frame.returnlabel.as(not null)};")
2419 redef class AAbortExpr
2420 redef fun stmt(v) do v.add_abort("Aborted")
2426 var cond = v.expr_bool(self.n_expr)
2427 v.add("if ({cond})\{")
2436 var res = v.new_var(self.mtype.as(not null))
2437 var cond = v.expr_bool(self.n_expr)
2438 v.add("if ({cond})\{")
2439 v.assign(res, v.expr(self.n_then.as(not null), null))
2441 v.assign(res, v.expr(self.n_else.as(not null), null))
2447 redef class AIfexprExpr
2450 var res = v.new_var(self.mtype.as(not null))
2451 var cond = v.expr_bool(self.n_expr)
2452 v.add("if ({cond})\{")
2453 v.assign(res, v.expr(self.n_then, null))
2455 v.assign(res, v.expr(self.n_else, null))
2464 v.stmt(self.n_block)
2465 var escapemark = self.escapemark
2466 if escapemark != null then
2467 v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2472 redef class AWhileExpr
2476 var cond = v.expr_bool(self.n_expr)
2477 v.add("if (!{cond}) break;")
2478 v.stmt(self.n_block)
2479 v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2481 v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2485 redef class ALoopExpr
2489 v.stmt(self.n_block)
2490 v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2492 v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2496 redef class AForExpr
2499 # Shortcut on explicit range
2500 # Avoid the instantiation of the range and the iterator
2501 var nexpr = self.n_expr
2502 if self.variables.length == 1 and nexpr isa ARangeExpr and not v.compiler.modelbuilder.toolcontext.opt_no_shortcut_range.value then
2503 var from = v.expr(nexpr.n_expr, null)
2504 var to = v.expr(nexpr.n_expr2, null)
2505 var variable = v.variable(variables.first)
2506 var one = v.new_expr("1", v.get_class("Int").mclass_type)
2508 v.assign(variable, from)
2509 v.add("for(;;) \{ /* shortcut range */")
2512 if nexpr isa AOrangeExpr then
2513 ok = v.send(v.get_property("<", variable.mtype), [variable, to])
2515 ok = v.send(v.get_property("<=", variable.mtype), [variable, to])
2518 v.add("if(!{ok}) break;")
2520 v.stmt(self.n_block)
2522 v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2523 var succ = v.send(v.get_property("successor", variable.mtype), [variable, one])
2525 v.assign(variable, succ)
2527 v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2531 var cl = v.expr(self.n_expr, null)
2532 var it_meth = self.method_iterator
2533 assert it_meth != null
2534 var it = v.compile_callsite(it_meth, [cl])
2537 var isok_meth = self.method_is_ok
2538 assert isok_meth != null
2539 var ok = v.compile_callsite(isok_meth, [it])
2541 v.add("if(!{ok}) break;")
2542 if self.variables.length == 1 then
2543 var item_meth = self.method_item
2544 assert item_meth != null
2545 var i = v.compile_callsite(item_meth, [it])
2547 v.assign(v.variable(variables.first), i)
2548 else if self.variables.length == 2 then
2549 var key_meth = self.method_key
2550 assert key_meth != null
2551 var i = v.compile_callsite(key_meth, [it])
2553 v.assign(v.variable(variables[0]), i)
2554 var item_meth = self.method_item
2555 assert item_meth != null
2556 i = v.compile_callsite(item_meth, [it])
2558 v.assign(v.variable(variables[1]), i)
2562 v.stmt(self.n_block)
2563 v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2564 var next_meth = self.method_next
2565 assert next_meth != null
2566 v.compile_callsite(next_meth, [it])
2568 v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2572 redef class AAssertExpr
2575 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return
2577 var cond = v.expr_bool(self.n_expr)
2578 v.add("if (unlikely(!{cond})) \{")
2582 v.add_abort("Assert '{nid.text}' failed")
2584 v.add_abort("Assert failed")
2593 var res = v.new_var(self.mtype.as(not null))
2594 var i1 = v.expr_bool(self.n_expr)
2595 v.add("if ({i1}) \{")
2598 var i2 = v.expr_bool(self.n_expr2)
2599 v.add("{res} = {i2};")
2605 redef class AImpliesExpr
2608 var res = v.new_var(self.mtype.as(not null))
2609 var i1 = v.expr_bool(self.n_expr)
2610 v.add("if (!{i1}) \{")
2613 var i2 = v.expr_bool(self.n_expr2)
2614 v.add("{res} = {i2};")
2620 redef class AAndExpr
2623 var res = v.new_var(self.mtype.as(not null))
2624 var i1 = v.expr_bool(self.n_expr)
2625 v.add("if (!{i1}) \{")
2628 var i2 = v.expr_bool(self.n_expr2)
2629 v.add("{res} = {i2};")
2635 redef class ANotExpr
2638 var cond = v.expr_bool(self.n_expr)
2639 return v.new_expr("!{cond}", self.mtype.as(not null))
2643 redef class AOrElseExpr
2646 var res = v.new_var(self.mtype.as(not null))
2647 var i1 = v.expr(self.n_expr, null)
2648 v.add("if ({i1}!=NULL) \{")
2651 var i2 = v.expr(self.n_expr2, null)
2658 redef class AIntExpr
2659 redef fun expr(v) do return v.new_expr("{self.value.to_s}", self.mtype.as(not null))
2662 redef class AFloatExpr
2663 redef fun expr(v) do return v.new_expr("{self.n_float.text}", self.mtype.as(not null)) # FIXME use value, not n_float
2666 redef class ACharExpr
2667 redef fun expr(v) do return v.new_expr("'{self.value.to_s.escape_to_c}'", self.mtype.as(not null))
2670 redef class AArrayExpr
2673 var mtype = self.mtype.as(MClassType).arguments.first
2674 var array = new Array[RuntimeVariable]
2675 for nexpr in self.n_exprs.n_exprs do
2676 var i = v.expr(nexpr, mtype)
2679 return v.array_instance(array, mtype)
2683 redef class AStringFormExpr
2684 redef fun expr(v) do return v.string_instance(self.value.as(not null))
2687 redef class ASuperstringExpr
2690 var array = new Array[RuntimeVariable]
2691 for ne in self.n_exprs do
2692 if ne isa AStringFormExpr and ne.value == "" then continue # skip empty sub-strings
2693 var i = v.expr(ne, null)
2696 var a = v.array_instance(array, v.object_type)
2697 var res = v.send(v.get_property("to_s", a.mtype), [a])
2702 redef class ACrangeExpr
2705 var i1 = v.expr(self.n_expr, null)
2706 var i2 = v.expr(self.n_expr2, null)
2707 var mtype = self.mtype.as(MClassType)
2708 var res = v.init_instance(mtype)
2709 var it = v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2714 redef class AOrangeExpr
2717 var i1 = v.expr(self.n_expr, null)
2718 var i2 = v.expr(self.n_expr2, null)
2719 var mtype = self.mtype.as(MClassType)
2720 var res = v.init_instance(mtype)
2721 var it = v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2726 redef class ATrueExpr
2727 redef fun expr(v) do return v.new_expr("1", self.mtype.as(not null))
2730 redef class AFalseExpr
2731 redef fun expr(v) do return v.new_expr("0", self.mtype.as(not null))
2734 redef class ANullExpr
2735 redef fun expr(v) do return v.new_expr("NULL", self.mtype.as(not null))
2738 redef class AIsaExpr
2741 var i = v.expr(self.n_expr, null)
2742 return v.type_test(i, self.cast_type.as(not null), "isa")
2746 redef class AAsCastExpr
2749 var i = v.expr(self.n_expr, null)
2750 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2752 v.add_cast(i, self.mtype.as(not null), "as")
2757 redef class AAsNotnullExpr
2760 var i = v.expr(self.n_expr, null)
2761 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2763 if i.mtype.ctype != "val*" then return i
2765 v.add("if (unlikely({i} == NULL)) \{")
2766 v.add_abort("Cast failed")
2772 redef class AParExpr
2773 redef fun expr(v) do return v.expr(self.n_expr, null)
2776 redef class AOnceExpr
2779 var mtype = self.mtype.as(not null)
2780 var name = v.get_name("varonce")
2781 var guard = v.get_name(name + "_guard")
2782 v.add_decl("static {mtype.ctype} {name};")
2783 v.add_decl("static int {guard};")
2784 var res = v.new_var(mtype)
2785 v.add("if ({guard}) \{")
2786 v.add("{res} = {name};")
2788 var i = v.expr(self.n_expr, mtype)
2789 v.add("{res} = {i};")
2790 v.add("{name} = {res};")
2791 v.add("{guard} = 1;")
2797 redef class ASendExpr
2800 var recv = v.expr(self.n_expr, null)
2802 for a in self.raw_arguments do
2803 args.add(v.expr(a, null))
2805 return v.compile_callsite(self.callsite.as(not null), args)
2809 redef class ASendReassignFormExpr
2812 var recv = v.expr(self.n_expr, null)
2814 for a in self.raw_arguments do
2815 args.add(v.expr(a, null))
2817 var value = v.expr(self.n_value, null)
2819 var left = v.compile_callsite(self.callsite.as(not null), args)
2822 var res = v.compile_callsite(self.reassign_callsite.as(not null), [left, value])
2826 v.compile_callsite(self.write_callsite.as(not null), args)
2830 redef class ASuperExpr
2833 var recv = v.frame.arguments.first
2835 for a in self.n_args.n_exprs do
2836 args.add(v.expr(a, null))
2839 var callsite = self.callsite
2840 if callsite != null then
2841 # Add additionnals arguments for the super init call
2842 if args.length == 1 then
2843 for i in [0..callsite.msignature.arity[ do
2844 args.add(v.frame.arguments[i+1])
2848 var res = v.compile_callsite(callsite, args)
2852 if args.length == 1 then
2853 args = v.frame.arguments
2856 # stantard call-next-method
2857 return v.supercall(mpropdef.as(not null), recv.mtype.as(MClassType), args)
2861 redef class ANewExpr
2864 var mtype = self.mtype.as(MClassType)
2866 var ctype = mtype.ctype
2867 if mtype.mclass.name == "NativeArray" then
2868 assert self.n_args.n_exprs.length == 1
2869 var l = v.expr(self.n_args.n_exprs.first, null)
2870 assert mtype isa MGenericType
2871 var elttype = mtype.arguments.first
2872 return v.native_array_instance(elttype, l)
2873 else if ctype == "val*" then
2874 recv = v.init_instance(mtype)
2875 else if ctype == "void*" then
2876 recv = v.new_expr("NULL/*special!*/", mtype)
2878 recv = v.new_expr("({ctype})0/*special!*/", mtype)
2881 for a in self.n_args.n_exprs do
2882 args.add(v.expr(a, null))
2884 var res2 = v.compile_callsite(self.callsite.as(not null), args)
2885 if res2 != null then
2886 #self.debug("got {res2} from {mproperty}. drop {recv}")
2893 redef class AAttrExpr
2896 var recv = v.expr(self.n_expr, null)
2897 var mproperty = self.mproperty.as(not null)
2898 return v.read_attribute(mproperty, recv)
2902 redef class AAttrAssignExpr
2905 var recv = v.expr(self.n_expr, null)
2906 var i = v.expr(self.n_value, null)
2907 var mproperty = self.mproperty.as(not null)
2908 v.write_attribute(mproperty, recv, i)
2912 redef class AAttrReassignExpr
2915 var recv = v.expr(self.n_expr, null)
2916 var value = v.expr(self.n_value, null)
2917 var mproperty = self.mproperty.as(not null)
2918 var attr = v.read_attribute(mproperty, recv)
2919 var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
2921 v.write_attribute(mproperty, recv, res)
2925 redef class AIssetAttrExpr
2928 var recv = v.expr(self.n_expr, null)
2929 var mproperty = self.mproperty.as(not null)
2930 return v.isset_attribute(mproperty, recv)
2934 redef class ADebugTypeExpr
2943 redef class Array[E]
2944 # Return a new `Array` with the elements only contened in self and not in `o
`
2945 fun -(o: Array[E]): Array[E] do
2946 var res = new Array[E]
2947 for e in self do if not o.has(e) then res.add(e)
2953 # All `MProperty` associated to all `MClassDef` of `mclass
`
2954 fun properties(mclass: MClass): Set[MProperty] do
2955 if not self.properties_cache.has_key(mclass) then
2956 var properties = new HashSet[MProperty]
2957 var parents = new Array[MClass]
2958 if self.flatten_mclass_hierarchy.has(mclass) then
2959 parents.add_all(mclass.in_hierarchy(self).direct_greaters)
2961 for parent in parents do
2962 properties.add_all(self.properties(parent))
2964 for mclassdef in mclass.mclassdefs do
2965 if not self.in_importation <= mclassdef.mmodule then continue
2966 for mprop in mclassdef.intro_mproperties do
2967 properties.add(mprop)
2970 self.properties_cache[mclass] = properties
2972 return properties_cache[mclass]
2974 private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
2976 # Write FFI and nitni results to file
2977 fun finalize_ffi(c: AbstractCompiler) do end
2979 # Give requided addinional system libraries (as given to LD_LIBS)
2980 # Note: can return null instead of an empty set
2981 fun collect_linker_libs: nullable Set[String] do return null
2984 # Create a tool context to handle options and paths
2985 var toolcontext = new ToolContext
2987 var opt_mixins = new OptionArray("Additionals module to min-in", "-m")
2988 toolcontext.option_context.add_option(opt_mixins)
2990 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
2992 # We do not add other options, so process them now!
2993 toolcontext.process_options(args)
2995 # We need a model to collect stufs
2996 var model = new Model
2997 # An a model builder to parse files
2998 var modelbuilder = new ModelBuilder(model, toolcontext)
3000 var arguments = toolcontext.option_context.rest
3001 if arguments.length > 1 and toolcontext.opt_output.value != null then
3002 print "Error: --output needs a single source file. Do you prefer --dir?"
3006 # Here we load an process all modules passed on the command line
3007 var mmodules = modelbuilder.parse(arguments)
3008 var mixins = modelbuilder.parse(opt_mixins.value)
3010 if mmodules.is_empty then return
3011 modelbuilder.run_phases
3013 for mmodule in mmodules do
3014 toolcontext.info("*** PROCESS {mmodule} ***", 1)
3016 if not mixins.is_empty then
3019 toolcontext.run_global_phases(ms)