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
257 var file
: nullable OFStream = null
258 for vis
in f
.writers
do
259 if vis
== compiler
.header
then continue
260 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
261 if total_lines
== 0 then continue
263 if file
== null or count
> 10000 then
265 if file
!= null then file
.close
266 var cfilename
= "{f.name}.{i}.c"
267 var cfilepath
= "{compile_dir}/{cfilename}"
268 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
269 cfiles
.add
(cfilename
)
270 file
= new OFStream.open
(cfilepath
)
271 file
.write
"#include \"{f.name}.0.h\
"\n"
274 for l
in vis
.decl_lines
do
278 for l
in vis
.lines
do
283 if file
== null then continue
286 var cfilename
= "{f.name}.0.h"
287 var cfilepath
= "{compile_dir}/{cfilename}"
288 var hfile
: nullable OFStream = null
289 hfile
= new OFStream.open
(cfilepath
)
290 hfile
.write
"#include \"{hfilename}\
"\n"
291 for key
in f
.required_declarations
do
292 if not compiler
.provided_declarations
.has_key
(key
) then
293 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
295 node
.debug
"No provided declaration for {key}"
297 print
"No provided declaration for {key}"
301 hfile
.write compiler
.provided_declarations
[key
]
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
314 # Search a non fictive module
315 var res
= mainmodule
.name
316 while mainmodule
.is_fictive
do
317 mainmodule
= mainmodule
.in_importation
.direct_greaters
.first
318 res
= mainmodule
.name
323 # Combine options and platform informations to get the final path of the outfile
324 fun outfile
(mainmodule
: MModule): String
326 var res
= self.toolcontext
.opt_output
.value
327 if res
!= null then return res
328 res
= default_outname
(mainmodule
)
329 var dir
= self.toolcontext
.opt_dir
.value
330 if dir
!= null then return dir
.join_path
(res
)
334 fun write_makefile
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
336 var mainmodule
= compiler
.mainmodule
337 var platform
= compiler
.mainmodule
.target_platform
339 var outname
= outfile
(mainmodule
)
341 var orig_dir
= compile_dir
.relpath
(".")
342 var outpath
= orig_dir
.join_path
(outname
).simplify_path
343 var makename
= makefile_name
(mainmodule
)
344 var makepath
= "{compile_dir}/{makename}"
345 var makefile
= new OFStream.open
(makepath
)
349 cc_includes
+= " -I \"" + p + "\
""
352 var linker_options
= new HashSet[String]
353 for m
in mainmodule
.in_importation
.greaters
do
354 var libs
= m
.collect_linker_libs
355 if libs
!= null then linker_options
.add_all
(libs
)
358 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")
360 var ost
= toolcontext
.opt_stacktrace
.value
361 if (ost
== "libunwind" or ost
== "nitstack") and (platform
== null or platform
.supports_libunwind
) then makefile
.write
("NEED_LIBUNWIND := YesPlease\n")
363 # Dynamic adaptations
364 # While `platform` enable complex toolchains, they are statically applied
365 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
367 # Check and adapt the targeted system
368 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
369 makefile
.write
("ifeq ($(uname_S),Darwin)\n")
370 # remove -lunwind since it is already included on macosx
371 makefile
.write
("\tNEED_LIBUNWIND :=\n")
372 makefile
.write
("endif\n\n")
374 # Check and adapt for the compiler used
375 # clang need an additionnal `-Qunused-arguments`
376 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")
378 makefile
.write
("ifdef NEED_LIBUNWIND\n\tLDLIBS += -lunwind\nendif\n")
380 makefile
.write
("all: {outpath}\n\n")
382 var ofiles
= new Array[String]
383 var dep_rules
= new Array[String]
384 # Compile each generated file
386 var o
= f
.strip_extension
(".c") + ".o"
387 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
392 var java_files
= new Array[ExternFile]
394 # Compile each required extern body into a specific .o
395 for f
in compiler
.extern_bodies
do
396 var o
= f
.makefile_rule_name
397 var ff
= f
.filename
.basename
("")
398 makefile
.write
("{o}: {ff}\n")
399 makefile
.write
("\t{f.makefile_rule_content}\n\n")
400 dep_rules
.add
(f
.makefile_rule_name
)
402 if f
.compiles_to_o_file
then ofiles
.add
(o
)
403 if f
.add_to_jar
then java_files
.add
(f
)
406 if not java_files
.is_empty
then
407 var jar_file
= "{outpath}.jar"
409 var class_files_array
= new Array[String]
410 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
411 var class_files
= class_files_array
.join
(" ")
413 makefile
.write
("{jar_file}: {class_files}\n")
414 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
415 dep_rules
.add jar_file
419 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath} {ofiles.join(" ")} $(LDLIBS)\n\n")
421 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
423 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
426 fun compile_c_code
(compiler
: AbstractCompiler, compile_dir
: String)
428 var makename
= makefile_name
(compiler
.mainmodule
)
430 var makeflags
= self.toolcontext
.opt_make_flags
.value
431 if makeflags
== null then makeflags
= ""
432 self.toolcontext
.info
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags}", 2)
435 if self.toolcontext
.verbose_level
>= 3 then
436 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1")
438 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
441 toolcontext
.error
(null, "make failed! Error code: {res}.")
446 # Singleton that store the knowledge about the compilation process
447 abstract class AbstractCompiler
448 type VISITOR: AbstractCompilerVisitor
450 # Table corresponding c_names to nit names (methods)
451 var names
= new HashMap[String, String]
453 # The main module of the program currently compiled
454 # Is assigned during the separate compilation
455 var mainmodule
: MModule is writable
457 # The real main module of the program
458 var realmainmodule
: MModule
460 # The modelbuilder used to know the model and the AST
461 var modelbuilder
: ModelBuilder is protected writable
463 # Is hardening asked? (see --hardening)
464 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
466 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
468 self.mainmodule
= mainmodule
469 self.realmainmodule
= mainmodule
470 self.modelbuilder
= modelbuilder
473 # Force the creation of a new file
474 # The point is to avoid contamination between must-be-compiled-separately files
475 fun new_file
(name
: String): CodeFile
477 var f
= new CodeFile(name
)
482 # The list of all associated files
483 # Used to generate .c files
484 var files
= new List[CodeFile]
486 # Initialize a visitor specific for a compiler engine
487 fun new_visitor
: VISITOR is abstract
489 # Where global declaration are stored (the main .h)
490 var header
: CodeWriter is writable
492 # Provide a declaration that can be requested (before or latter) by a visitor
493 fun provide_declaration
(key
: String, s
: String)
495 if self.provided_declarations
.has_key
(key
) then
496 assert self.provided_declarations
[key
] == s
498 self.provided_declarations
[key
] = s
501 private var provided_declarations
= new HashMap[String, String]
503 private var requirers_of_declarations
= new HashMap[String, ANode]
505 # Builds the .c and .h files to be used when generating a Stack Trace
506 # Binds the generated C function names to Nit function names
507 fun build_c_to_nit_bindings
509 var compile_dir
= modelbuilder
.compile_dir
511 var stream
= new OFStream.open
("{compile_dir}/c_functions_hash.c")
512 stream
.write
("#include <string.h>\n")
513 stream
.write
("#include <stdlib.h>\n")
514 stream
.write
("#include \"c_functions_hash
.h\
"\n")
515 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
516 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
517 stream
.write
("char* procname = malloc(len+1);")
518 stream
.write
("memcpy(procname, procproc, len);")
519 stream
.write
("procname[len] = '\\0';")
520 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
521 for i
in names
.keys
do
524 stream.write("\
",\"")
525 stream.write(names[i])
526 stream.write("\
"\},\n")
528 stream
.write
("\};\n")
529 stream
.write
("int i;")
530 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
531 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
532 stream
.write
("free(procname);")
533 stream
.write
("return map[i].nit_name;")
536 stream
.write
("free(procname);")
537 stream
.write
("return NULL;")
541 stream
= new OFStream.open
("{compile_dir}/c_functions_hash.h")
542 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
545 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
549 # This method call compile_header_strucs method that has to be refined
550 fun compile_header
do
551 self.header
.add_decl
("#include <stdlib.h>")
552 self.header
.add_decl
("#include <stdio.h>")
553 self.header
.add_decl
("#include <string.h>")
554 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
555 self.header
.add_decl
("#ifdef ANDROID")
556 self.header
.add_decl
(" #include <android/log.h>")
557 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
558 self.header
.add_decl
("#else")
559 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
560 self.header
.add_decl
("#endif")
562 compile_header_structs
563 compile_nitni_structs
565 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
566 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
567 # Signal handler function prototype
568 self.header
.add_decl
("void show_backtrace(int);")
570 self.header
.add_decl
("void show_backtrace(int) __attribute__ ((noreturn));")
573 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
574 self.header
.add_decl
("#define likely(x) (x)")
575 self.header
.add_decl
("#define unlikely(x) (x)")
576 else if gccd_disable
.has
("correct-likely") then
577 # invert the `likely` definition
578 # Used by masochists to bench the worst case
579 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
580 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
582 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
583 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
586 # Global variable used by intern methods
587 self.header
.add_decl
("extern int glob_argc;")
588 self.header
.add_decl
("extern char **glob_argv;")
589 self.header
.add_decl
("extern val *glob_sys;")
592 # Declaration of structures for live Nit types
593 protected fun compile_header_structs
is abstract
595 # Declaration of structures for nitni undelying the FFI
596 protected fun compile_nitni_structs
598 self.header
.add_decl
"""
599 /* Native reference to Nit objects */
600 /* This structure is used to represent every Nit type in extern methods and custom C code. */
602 struct nitni_ref *next,
603 *prev; /* adjacent global references in global list */
604 int count; /* number of time this global reference has been marked */
607 /* List of global references from C code to Nit objects */
608 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
609 struct nitni_global_ref_list_t {
610 struct nitni_ref *head, *tail;
612 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
614 /* Initializer of global reference list */
615 extern void nitni_global_ref_list_init();
617 /* Intern function to add a global reference to the list */
618 extern void nitni_global_ref_add( struct nitni_ref *ref );
620 /* Intern function to remove a global reference from the list */
621 extern void nitni_global_ref_remove( struct nitni_ref *ref );
623 /* Increase count on an existing global reference */
624 extern void nitni_global_ref_incr( struct nitni_ref *ref );
626 /* Decrease count on an existing global reference */
627 extern void nitni_global_ref_decr( struct nitni_ref *ref );
631 fun compile_finalizer_function
633 var finalizable_type
= mainmodule
.finalizable_type
634 if finalizable_type
== null then return
636 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
638 if finalize_meth
== null then
639 modelbuilder
.toolcontext
.error
(null, "The `Finalizable` class doesn't declare the `finalize` method.")
643 var v
= self.new_visitor
644 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
645 var recv
= v
.new_expr
("obj", finalizable_type
)
646 v
.send
(finalize_meth
, [recv
])
650 # Generate the main C function.
654 # * allocate the Sys object if it exists
655 # * call init if is exists
656 # * call main if it exists
657 fun compile_main_function
659 var v
= self.new_visitor
660 v
.add_decl
("#include <signal.h>")
661 var ost
= modelbuilder
.toolcontext
.opt_stacktrace
.value
662 var platform
= mainmodule
.target_platform
664 if platform
!= null and not platform
.supports_libunwind
then ost
= "none"
666 var no_main
= (platform
!= null and platform
.no_main
) or modelbuilder
.toolcontext
.opt_no_main
.value
668 if ost
== "nitstack" or ost
== "libunwind" then
669 v
.add_decl
("#define UNW_LOCAL_ONLY")
670 v
.add_decl
("#include <libunwind.h>")
671 if ost
== "nitstack" then
672 v
.add_decl
("#include \"c_functions_hash
.h\
"")
675 v
.add_decl
("int glob_argc;")
676 v
.add_decl
("char **glob_argv;")
677 v
.add_decl
("val *glob_sys;")
679 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
680 for tag
in count_type_test_tags
do
681 v
.add_decl
("long count_type_test_resolved_{tag};")
682 v
.add_decl
("long count_type_test_unresolved_{tag};")
683 v
.add_decl
("long count_type_test_skipped_{tag};")
684 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
685 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
686 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
690 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
691 v
.add_decl
("long count_invoke_by_tables;")
692 v
.add_decl
("long count_invoke_by_direct;")
693 v
.add_decl
("long count_invoke_by_inline;")
694 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
695 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
696 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
699 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
700 v
.add_decl
("long count_attr_reads = 0;")
701 v
.add_decl
("long count_isset_checks = 0;")
702 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
703 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
706 v
.add_decl
("void sig_handler(int signo)\{")
707 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
708 v
.add_decl
("show_backtrace(signo);")
711 v
.add_decl
("void show_backtrace (int signo) \{")
712 if ost
== "nitstack" or ost
== "libunwind" then
713 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
714 v
.add_decl
("unw_cursor_t cursor;")
715 v
.add_decl
("if(opt==NULL)\{")
716 v
.add_decl
("unw_context_t uc;")
717 v
.add_decl
("unw_word_t ip;")
718 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
719 v
.add_decl
("unw_getcontext(&uc);")
720 v
.add_decl
("unw_init_local(&cursor, &uc);")
721 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
722 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
723 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
724 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
725 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
726 if ost
== "nitstack" then
727 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
728 v
.add_decl
(" if (recv != NULL)\{")
729 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
730 v.add_decl(" \}else\{")
731 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
734 v
.add_decl
(" PRINT_ERROR(\"` %s \\n\",procname);")
737 v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
738 v.add_decl("free(procname);")
741 v.add_decl("exit(signo);")
745 v.add_decl("int nit_main(int argc, char** argv) \{")
747 v.add_decl("int main(int argc, char** argv) \{")
750 v.add("signal(SIGABRT, sig_handler);")
751 v.add("signal(SIGFPE, sig_handler);")
752 v.add("signal(SIGILL, sig_handler);")
753 v.add("signal(SIGINT, sig_handler);")
754 v.add("signal(SIGTERM, sig_handler);")
755 v.add("signal(SIGSEGV, sig_handler);")
756 v.add("signal(SIGPIPE, sig_handler);")
758 v.add("glob_argc = argc; glob_argv = argv;")
759 v.add("initialize_gc_option();")
761 v.add "initialize_nitni_global_refs();"
763 var main_type = mainmodule.sys_type
764 if main_type != null then
765 var mainmodule = v.compiler.mainmodule
766 var glob_sys = v.init_instance(main_type)
767 v.add("glob_sys = {glob_sys};")
768 var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
769 if main_init != null then
770 v.send(main_init, [glob_sys])
772 var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
773 mainmodule.try_get_primitive_method("main", main_type.mclass)
774 if main_method != null then
775 v.send(main_method, [glob_sys])
779 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
780 v.add_decl("long count_type_test_resolved_total = 0;")
781 v.add_decl("long count_type_test_unresolved_total = 0;")
782 v.add_decl("long count_type_test_skipped_total = 0;")
783 v.add_decl("long count_type_test_total_total = 0;")
784 for tag in count_type_test_tags do
785 v.add_decl("long count_type_test_total_{tag};")
786 v.add("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
787 v.add("count_type_test_resolved_total += count_type_test_resolved_{tag};")
788 v.add("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
789 v.add("count_type_test_skipped_total += count_type_test_skipped_{tag};")
790 v.add("count_type_test_total_total += count_type_test_total_{tag};")
792 v.add("printf(\"# dynamic count_type_test: total %l\\n\");")
793 v.add("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\");")
794 var tags = count_type_test_tags.to_a
797 v.add("printf(\"{tag}\");")
798 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
799 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
800 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
801 v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
805 if self.modelbuilder.toolcontext.opt_invocation_metrics.value then
806 v.add_decl("long count_invoke_total;")
807 v.add("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
808 v.add("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
809 v.add("printf(\"by table: %ld (%.2f%%)\\n\", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
810 v.add("printf(\"direct: %ld (%.2f%%)\\n\", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
811 v.add("printf(\"inlined: %ld (%.2f%%)\\n\", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
814 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
815 v.add("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
816 v.add("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
823 # Copile all C functions related to the [incr|decr]_ref features of the FFI
824 fun compile_nitni_global_ref_functions
826 var v = self.new_visitor
828 struct nitni_global_ref_list_t *nitni_global_ref_list;
829 void initialize_nitni_global_refs() {
830 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
831 nitni_global_ref_list->head = NULL;
832 nitni_global_ref_list->tail = NULL;
835 void nitni_global_ref_add( struct nitni_ref *ref ) {
836 if ( nitni_global_ref_list->head == NULL ) {
837 nitni_global_ref_list->head = ref;
840 nitni_global_ref_list->tail->next = ref;
841 ref->prev = nitni_global_ref_list->tail;
843 nitni_global_ref_list->tail = ref;
848 void nitni_global_ref_remove( struct nitni_ref *ref ) {
849 if ( ref->prev == NULL ) {
850 nitni_global_ref_list->head = ref->next;
852 ref->prev->next = ref->next;
855 if ( ref->next == NULL ) {
856 nitni_global_ref_list->tail = ref->prev;
858 ref->next->prev = ref->prev;
862 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
863 if ( ref->count == 0 ) /* not registered */
866 nitni_global_ref_add( ref );
872 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
873 if ( ref->count == 1 ) /* was last reference */
875 /* remove from list */
876 nitni_global_ref_remove( ref );
884 # List of additional files required to compile (FFI)
885 var extern_bodies = new Array[ExternFile]
887 # List of source files to copy over to the compile dir
888 var files_to_copy = new Array[String]
890 # This is used to avoid adding an extern file more than once
891 private var seen_extern = new ArraySet[String]
893 # Generate code that initialize the attributes on a new instance
894 fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
896 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
897 self.mainmodule.linearize_mclassdefs(cds)
899 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
900 var n = self.modelbuilder.mclassdef2nclassdef[cd]
901 for npropdef in n.n_propdefs do
902 if npropdef isa AAttrPropdef then
903 npropdef.init_expr(v, recv)
909 # Generate code that check if an attribute is correctly initialized
910 fun generate_check_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
912 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
913 self.mainmodule.linearize_mclassdefs(cds)
915 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
916 var n = self.modelbuilder.mclassdef2nclassdef[cd]
917 for npropdef in n.n_propdefs do
918 if npropdef isa AAttrPropdef then
919 npropdef.check_expr(v, recv)
927 var count_type_test_tags: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
928 var count_type_test_resolved: HashMap[String, Int] = init_count_type_test_tags
929 var count_type_test_unresolved: HashMap[String, Int] = init_count_type_test_tags
930 var count_type_test_skipped: HashMap[String, Int] = init_count_type_test_tags
932 protected fun init_count_type_test_tags: HashMap[String, Int]
934 var res = new HashMap[String, Int]
935 for tag in count_type_test_tags do
941 # Display stats about compilation process
945 # * type tests against resolved types (`x
isa Collection[Animal]`)
946 # * type tests against unresolved types (`x
isa Collection[E
]`)
947 # * type tests skipped
951 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
952 print "# static count_type_test"
953 print "\tresolved:\tunresolved\tskipped\ttotal"
954 var count_type_test_total = init_count_type_test_tags
955 count_type_test_resolved["total"] = 0
956 count_type_test_unresolved["total"] = 0
957 count_type_test_skipped["total"] = 0
958 count_type_test_total["total"] = 0
959 for tag in count_type_test_tags do
960 count_type_test_total[tag] = count_type_test_resolved[tag] + count_type_test_unresolved[tag] + count_type_test_skipped[tag]
961 count_type_test_resolved["total"] += count_type_test_resolved[tag]
962 count_type_test_unresolved["total"] += count_type_test_unresolved[tag]
963 count_type_test_skipped["total"] += count_type_test_skipped[tag]
964 count_type_test_total["total"] += count_type_test_total[tag]
966 var count_type_test = count_type_test_total["total"]
967 var tags = count_type_test_tags.to_a
971 printn "\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
972 printn "\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
973 printn "\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
974 printn "\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
980 fun finalize_ffi_for_module(mmodule: MModule) do mmodule.finalize_ffi(self)
983 # Avoid division by zero by returning the string "n/a"
984 fun div(a,b:Int):String
986 if b == 0 then return "n/a"
987 return ((a*10000/b).to_f / 100.0).to_precision(2)
991 # A file unit (may be more than one file if
992 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
995 var writers = new Array[CodeWriter]
996 var required_declarations = new HashSet[String]
999 # Where to store generated lines
1002 var lines: List[String] = new List[String]
1003 var decl_lines: List[String] = new List[String]
1005 # Add a line in the main part of the generated C
1006 fun add(s: String) do self.lines.add(s)
1009 # (used for local or global declaration)
1010 fun add_decl(s: String) do self.decl_lines.add(s)
1012 init(file: CodeFile)
1015 file.writers.add(self)
1019 # A visitor on the AST of property definition that generate the C code.
1020 abstract class AbstractCompilerVisitor
1022 type COMPILER: AbstractCompiler
1024 # The associated compiler
1025 var compiler: COMPILER
1027 # The current visited AST node
1028 var current_node: nullable ANode = null is writable
1030 # The current `Frame`
1031 var frame: nullable Frame is writable
1033 # Alias for self.compiler.mainmodule.object_type
1034 fun object_type: MClassType do return self.compiler.mainmodule.object_type
1036 # Alias for self.compiler.mainmodule.bool_type
1037 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
1039 var writer: CodeWriter
1041 init(compiler: COMPILER)
1043 self.compiler = compiler
1044 self.writer = new CodeWriter(compiler.files.last)
1047 # Force to get the primitive class named `name
` or abort
1048 fun get_class(name: String): MClass do return self.compiler.mainmodule.get_primitive_class(name)
1050 # Force to get the primitive property named `name
` in the instance `recv
` or abort
1051 fun get_property(name: String, recv: MType): MMethod
1053 assert recv isa MClassType
1054 return self.compiler.modelbuilder.force_get_primitive_method(self.current_node, name, recv.mclass, self.compiler.mainmodule)
1057 fun compile_callsite(callsite: CallSite, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1059 var initializers = callsite.mpropdef.initializers
1060 if not initializers.is_empty then
1061 var recv = arguments.first
1064 for p in initializers do
1065 if p isa MMethod then
1067 for x in p.intro.msignature.mparameters do
1068 args.add arguments[i]
1072 else if p isa MAttribute then
1073 self.write_attribute(p, recv, arguments[i])
1077 assert i == arguments.length
1079 return self.send(callsite.mproperty, [recv])
1082 return self.send(callsite.mproperty, arguments)
1085 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable is abstract
1087 fun calloc_array(ret_type: MType, arguments: Array[RuntimeVariable]) is abstract
1089 fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract
1091 # Evaluate `args
` as expressions in the call of `mpropdef
` on `recv
`.
1092 # This method is used to manage varargs in signatures and returns the real array
1093 # of runtime variables to use in the call.
1094 fun varargize(mpropdef: MMethodDef, recv: RuntimeVariable, args: SequenceRead[AExpr]): Array[RuntimeVariable]
1096 var msignature = mpropdef.new_msignature or else mpropdef.msignature.as(not null)
1097 var res = new Array[RuntimeVariable]
1100 if args.is_empty then return res
1102 var vararg_rank = msignature.vararg_rank
1103 var vararg_len = args.length - msignature.arity
1104 if vararg_len < 0 then vararg_len = 0
1106 for i in [0..msignature.arity[ do
1107 if i == vararg_rank then
1109 if ne isa AVarargExpr then
1110 var e = self.expr(ne.n_expr, null)
1114 var vararg = new Array[RuntimeVariable]
1115 for j in [vararg_rank..vararg_rank+vararg_len] do
1116 var e = self.expr(args[j], null)
1119 var elttype = msignature.mparameters[vararg_rank].mtype
1120 var arg = self.vararg_instance(mpropdef, recv, vararg, elttype)
1124 if i > vararg_rank then j += vararg_len
1125 var e = self.expr(args[j], null)
1134 # Anchor a type to the main module and the current receiver
1135 fun anchor(mtype: MType): MType
1137 if not mtype.need_anchor then return mtype
1138 return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
1141 fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
1143 if not mtype.need_anchor then return mtype
1144 return mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
1147 # Unsafely cast a value to a new type
1148 # ie the result share the same C variable but my have a different mcasttype
1149 # NOTE: if the adaptation is useless then `value
` is returned as it.
1150 # ENSURE: `result
.name
== value
.name
`
1151 fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
1153 mtype = self.anchor(mtype)
1154 var valmtype = value.mcasttype
1155 if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1159 if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1160 var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
1163 var res = new RuntimeVariable(value.name, valmtype, mtype)
1168 # Generate a super call from a method definition
1169 fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1171 # Adapt the arguments of a method according to targetted `MMethodDef`
1172 fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1174 # Unbox all the arguments of a method when implemented `extern` or `intern`
1175 fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1177 # Box or unbox a value to another type iff a C type conversion is needed
1178 # ENSURE: `result
.mtype
.ctype
== mtype
.ctype
`
1179 fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1181 # Box extern classes to be used in the generated code
1182 fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1184 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1185 fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1187 # Generate a polymorphic subtype test
1188 fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
1190 # Generate the code required to dynamically check if 2 objects share the same runtime type
1191 fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1193 # Generate a Nit "is" for two runtime_variables
1194 fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1198 # Generate a static call on a method definition
1199 fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1201 # Generate a polymorphic send for the method `m
` and the arguments `args
`
1202 fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1204 # Generate a monomorphic send for the method `m
`, the type `t
` and the arguments `args
`
1205 fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1207 assert t isa MClassType
1208 var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
1209 return self.call(propdef, t, args)
1212 # Generate a monomorphic super send from the method `m
`, the type `t
` and the arguments `args
`
1213 fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1215 assert t isa MClassType
1216 m = m.lookup_next_definition(self.compiler.mainmodule, t)
1217 return self.call(m, t, args)
1220 # Attributes handling
1222 # Generate a polymorphic attribute is_set test
1223 fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1225 # Generate a polymorphic attribute read
1226 fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1228 # Generate a polymorphic attribute write
1229 fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable) is abstract
1233 # Add a check and an abort for a null receiver if needed
1234 fun check_recv_notnull(recv: RuntimeVariable)
1236 if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
1238 var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType
1240 self.add("if (unlikely({recv} == NULL)) \{")
1241 self.add_abort("Receiver is null")
1248 private var names = new HashSet[String]
1249 private var last: Int = 0
1251 # Return a new name based on `s
` and unique in the visitor
1252 fun get_name(s: String): String
1254 if not self.names.has(s) then
1258 var i = self.last + 1
1261 if not self.names.has(s2) then
1270 # Return an unique and stable identifier associated with an escapemark
1271 fun escapemark_name(e: nullable EscapeMark): String
1274 if escapemark_names.has_key(e) then return escapemark_names[e]
1276 if name == null then name = "label"
1277 name = get_name(name)
1278 escapemark_names[e] = name
1282 # Insert a C label for associated with an escapemark
1283 fun add_escape_label(e: nullable EscapeMark)
1285 if e == null then return
1286 if e.escapes.is_empty then return
1287 add("BREAK_{escapemark_name(e)}: (void)0;")
1290 private var escapemark_names = new HashMap[EscapeMark, String]
1292 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1293 # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
1294 fun class_name_string(value: RuntimeVariable): String is abstract
1296 # Variables handling
1298 protected var variables = new HashMap[Variable, RuntimeVariable]
1300 # Return the local runtime_variable associated to a Nit local variable
1301 fun variable(variable: Variable): RuntimeVariable
1303 if self.variables.has_key(variable) then
1304 return self.variables[variable]
1306 var name = self.get_name("var_{variable.name}")
1307 var mtype = variable.declared_type.as(not null)
1308 mtype = self.anchor(mtype)
1309 var res = new RuntimeVariable(name, mtype, mtype)
1310 self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1311 self.variables[variable] = res
1316 # Return a new uninitialized local runtime_variable
1317 fun new_var(mtype: MType): RuntimeVariable
1319 mtype = self.anchor(mtype)
1320 var name = self.get_name("var")
1321 var res = new RuntimeVariable(name, mtype, mtype)
1322 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1326 # The difference with `new_var
` is the C static type of the local variable
1327 fun new_var_extern(mtype: MType): RuntimeVariable
1329 mtype = self.anchor(mtype)
1330 var name = self.get_name("var")
1331 var res = new RuntimeVariable(name, mtype, mtype)
1332 self.add_decl("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1336 # Return a new uninitialized named runtime_variable
1337 fun new_named_var(mtype: MType, name: String): RuntimeVariable
1339 mtype = self.anchor(mtype)
1340 var res = new RuntimeVariable(name, mtype, mtype)
1341 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1345 # Correctly assign a left and a right value
1346 # Boxing and unboxing is performed if required
1347 fun assign(left, right: RuntimeVariable)
1349 right = self.autobox(right, left.mtype)
1350 self.add("{left} = {right};")
1353 # Generate instances
1355 # Generate a alloc-instance + init-attributes
1356 fun init_instance(mtype: MClassType): RuntimeVariable is abstract
1358 # Set a GC finalizer on `recv
`, only if `recv
` isa Finalizable
1359 fun set_finalizer(recv: RuntimeVariable)
1361 var mtype = recv.mtype
1362 var finalizable_type = compiler.mainmodule.finalizable_type
1363 if finalizable_type != null and not mtype.need_anchor and
1364 mtype.is_subtype(compiler.mainmodule, null, finalizable_type) then
1365 add "gc_register_finalizer({recv});"
1369 # Generate an integer value
1370 fun int_instance(value: Int): RuntimeVariable
1372 var res = self.new_var(self.get_class("Int").mclass_type)
1373 self.add("{res} = {value};")
1377 # Generate an integer value
1378 fun bool_instance(value: Bool): RuntimeVariable
1380 var res = self.new_var(self.get_class("Bool").mclass_type)
1382 self.add("{res} = 1;")
1384 self.add("{res} = 0;")
1389 # Generate a string value
1390 fun string_instance(string: String): RuntimeVariable
1392 var mtype = self.get_class("String").mclass_type
1393 var name = self.get_name("varonce")
1394 self.add_decl("static {mtype.ctype} {name};")
1395 var res = self.new_var(mtype)
1396 self.add("if ({name}) \{")
1397 self.add("{res} = {name};")
1398 self.add("\} else \{")
1399 var native_mtype = self.get_class("NativeString").mclass_type
1400 var nat = self.new_var(native_mtype)
1401 self.add("{nat} = \"{string.escape_to_c}\";")
1402 var length = self.int_instance(string.length)
1403 self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
1404 self.add("{name} = {res};")
1409 fun value_instance(object: Object): RuntimeVariable
1411 if object isa Int then
1412 return int_instance(object)
1413 else if object isa Bool then
1414 return bool_instance(object)
1415 else if object isa String then
1416 return string_instance(object)
1422 # Generate an array value
1423 fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1425 # Get an instance of a array for a vararg
1426 fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1430 # Add a line in the main part of the generated C
1431 fun add(s: String) do self.writer.lines.add(s)
1434 # (used for local or global declaration)
1435 fun add_decl(s: String) do self.writer.decl_lines.add(s)
1437 # Request the presence of a global declaration
1438 fun require_declaration(key: String)
1440 var reqs = self.writer.file.required_declarations
1441 if reqs.has(key) then return
1443 var node = current_node
1444 if node != null then compiler.requirers_of_declarations[key] = node
1447 # Add a declaration in the local-header
1448 # The declaration is ensured to be present once
1449 fun declare_once(s: String)
1451 self.compiler.provide_declaration(s, s)
1452 self.require_declaration(s)
1455 # look for a needed .h and .c file for a given .nit source-file
1456 # FIXME: bad API, parameter should be a `MModule`, not its source-file
1457 fun add_extern(file: String)
1459 file = file.strip_extension(".nit")
1460 var tryfile = file + ".nit.h"
1461 if tryfile.file_exists then
1462 self.declare_once("#include \"{tryfile.basename("")}\"")
1463 self.compiler.files_to_copy.add(tryfile)
1465 tryfile = file + "_nit.h"
1466 if tryfile.file_exists then
1467 self.declare_once("#include \"{tryfile.basename("")}\"")
1468 self.compiler.files_to_copy.add(tryfile)
1471 if self.compiler.seen_extern.has(file) then return
1472 self.compiler.seen_extern.add(file)
1473 tryfile = file + ".nit.c"
1474 if not tryfile.file_exists then
1475 tryfile = file + "_nit.c"
1476 if not tryfile.file_exists then return
1478 var f = new ExternCFile(tryfile.basename(""), "")
1479 self.compiler.extern_bodies.add(f)
1480 self.compiler.files_to_copy.add(tryfile)
1483 # Return a new local runtime_variable initialized with the C expression `cexpr
`.
1484 fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
1486 var res = new_var(mtype)
1487 self.add("{res} = {cexpr};")
1491 # Generate generic abort
1492 # used by aborts, asserts, casts, etc.
1493 fun add_abort(message: String)
1495 self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
1501 if self.current_node != null and self.current_node.location.file != null then
1502 self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
1504 self.add("PRINT_ERROR(\"\\n\");")
1506 self.add("show_backtrace(1);")
1509 # Add a dynamic cast
1510 fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)
1512 var res = self.type_test(value, mtype, tag)
1513 self.add("if (unlikely(!{res})) \{")
1514 var cn = self.class_name_string(value)
1515 self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s
`, got `%s
`\", \"{mtype.to_s.escape_to_c}\", {cn});")
1520 # Generate a return with the value `s
`
1521 fun ret(s: RuntimeVariable)
1523 self.assign(self.frame.returnvar.as(not null), s)
1524 self.add("goto {self.frame.returnlabel.as(not null)};")
1527 # Compile a statement (if any)
1528 fun stmt(nexpr: nullable AExpr)
1530 if nexpr == null then return
1531 var old = self.current_node
1532 self.current_node = nexpr
1534 self.current_node = old
1537 # Compile an expression an return its result
1538 # `mtype
` is the expected return type, pass null if no specific type is expected.
1539 fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
1541 var old = self.current_node
1542 self.current_node = nexpr
1543 var res = nexpr.expr(self).as(not null)
1544 if mtype != null then
1545 mtype = self.anchor(mtype)
1546 res = self.autobox(res, mtype)
1548 res = autoadapt(res, nexpr.mtype.as(not null))
1549 var implicit_cast_to = nexpr.implicit_cast_to
1550 if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
1551 add_cast(res, implicit_cast_to, "auto")
1552 res = autoadapt(res, implicit_cast_to)
1554 self.current_node = old
1558 # Alias for `self.expr
(nexpr
, self.bool_type
)`
1559 fun expr_bool(nexpr: AExpr): RuntimeVariable do return expr(nexpr, bool_type)
1561 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1562 fun debug(message: String)
1564 var node = self.current_node
1565 if node == null then
1566 print "?: {message}"
1570 self.add("/* DEBUG: {message} */")
1574 # A C function associated to a Nit method
1575 # Because of customization, a given Nit method can be compiler more that once
1576 abstract class AbstractRuntimeFunction
1578 type COMPILER: AbstractCompiler
1579 type VISITOR: AbstractCompilerVisitor
1581 # The associated Nit method
1582 var mmethoddef: MMethodDef
1584 # The mangled c name of the runtime_function
1585 # Subclasses should redefine `build_c_name
` instead
1588 var res = self.c_name_cache
1589 if res != null then return res
1590 res = self.build_c_name
1591 self.c_name_cache = res
1595 # Non cached version of `c_name
`
1596 protected fun build_c_name: String is abstract
1598 protected var c_name_cache: nullable String = null is writable
1600 # Implements a call of the runtime_function
1601 # May inline the body or generate a C function call
1602 fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1604 # Generate the code for the `AbstractRuntimeFunction`
1605 # Warning: compile more than once compilation makes CC unhappy
1606 fun compile_to_c(compiler: COMPILER) is abstract
1609 # A runtime variable hold a runtime value in C.
1610 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
1612 # 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.
1613 class RuntimeVariable
1614 # The name of the variable in the C code
1617 # The static type of the variable (as declard in C)
1620 # The current casted type of the variable (as known in Nit)
1621 var mcasttype: MType is writable
1623 # If the variable exaclty a mcasttype?
1624 # false (usual value) means that the variable is a mcasttype or a subtype.
1625 var is_exact: Bool = false is writable
1627 init(name: String, mtype: MType, mcasttype: MType)
1631 self.mcasttype = mcasttype
1632 assert not mtype.need_anchor
1633 assert not mcasttype.need_anchor
1636 redef fun to_s do return name
1641 if self.is_exact then
1642 exact_str = " exact"
1647 if self.mtype == self.mcasttype then
1648 type_str = "{mtype}{exact_str}"
1650 type_str = "{mtype}({mcasttype}{exact_str})"
1652 return "<{name}:{type_str}>"
1656 # A frame correspond to a visited property in a `GlobalCompilerVisitor`
1659 type VISITOR: AbstractCompilerVisitor
1661 # The associated visitor
1662 var visitor: VISITOR
1664 # The executed property.
1665 # A Method in case of a call, an attribute in case of a default initialization.
1666 var mpropdef: MPropDef
1668 # The static type of the receiver
1669 var receiver: MClassType
1671 # Arguments of the method (the first is the receiver)
1672 var arguments: Array[RuntimeVariable]
1674 # The runtime_variable associated to the return (in a function)
1675 var returnvar: nullable RuntimeVariable = null is writable
1677 # The label at the end of the property
1678 var returnlabel: nullable String = null is writable
1682 # Return the C type associated to a given Nit static type
1683 fun ctype: String do return "val*"
1685 # C type outside of the compiler code and in boxes
1686 fun ctype_extern: String do return "val*"
1688 # Short name of the `ctype
` to use in unions
1689 fun ctypename: String do return "val"
1691 # Return the name of the C structure associated to a Nit live type
1692 fun c_name: String is abstract
1693 protected var c_name_cache: nullable String is protected writable
1696 redef class MClassType
1699 var res = self.c_name_cache
1700 if res != null then return res
1701 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1702 self.c_name_cache = res
1706 redef fun ctype: String
1708 if mclass.name == "Int" then
1710 else if mclass.name == "Bool" then
1712 else if mclass.name == "Char" then
1714 else if mclass.name == "Float" then
1716 else if mclass.name == "NativeString" then
1718 else if mclass.name == "NativeArray" then
1725 redef fun ctype_extern: String
1727 if mclass.kind == extern_kind then
1734 redef fun ctypename: String
1736 if mclass.name == "Int" then
1738 else if mclass.name == "Bool" then
1740 else if mclass.name == "Char" then
1742 else if mclass.name == "Float" then
1744 else if mclass.name == "NativeString" then
1746 else if mclass.name == "NativeArray" then
1747 #return "{self.arguments.first.ctype}*"
1755 redef class MGenericType
1758 var res = self.c_name_cache
1759 if res != null then return res
1761 for t in self.arguments do
1762 res = res + t.c_name
1764 self.c_name_cache = res
1769 redef class MParameterType
1772 var res = self.c_name_cache
1773 if res != null then return res
1774 res = "{self.mclass.c_name}_FT{self.rank}"
1775 self.c_name_cache = res
1780 redef class MVirtualType
1783 var res = self.c_name_cache
1784 if res != null then return res
1785 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1786 self.c_name_cache = res
1791 redef class MNullableType
1794 var res = self.c_name_cache
1795 if res != null then return res
1796 res = "nullable_{self.mtype.c_name}"
1797 self.c_name_cache = res
1803 # Return the name of the C structure associated to a Nit class
1804 fun c_name: String do
1805 var res = self.c_name_cache
1806 if res != null then return res
1807 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1808 self.c_name_cache = res
1811 private var c_name_cache: nullable String
1814 redef class MProperty
1815 fun c_name: String do
1816 var res = self.c_name_cache
1817 if res != null then return res
1818 res = "{self.intro.c_name}"
1819 self.c_name_cache = res
1822 private var c_name_cache: nullable String
1825 redef class MPropDef
1826 type VISITOR: AbstractCompilerVisitor
1828 private var c_name_cache: nullable String
1830 # The mangled name associated to the property
1833 var res = self.c_name_cache
1834 if res != null then return res
1835 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1836 self.c_name_cache = res
1841 redef class MMethodDef
1842 # Can the body be inlined?
1843 fun can_inline(v: VISITOR): Bool
1845 if is_abstract then return true
1846 var modelbuilder = v.compiler.modelbuilder
1847 if modelbuilder.mpropdef2npropdef.has_key(self) then
1848 var npropdef = modelbuilder.mpropdef2npropdef[self]
1849 return npropdef.can_inline
1850 else if self.mproperty.is_root_init then
1851 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1858 # Inline the body in another visitor
1859 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1861 var modelbuilder = v.compiler.modelbuilder
1862 var val = constant_value
1863 if modelbuilder.mpropdef2npropdef.has_key(self) then
1864 var npropdef = modelbuilder.mpropdef2npropdef[self]
1865 var oldnode = v.current_node
1866 v.current_node = npropdef
1867 self.compile_parameter_check(v, arguments)
1868 npropdef.compile_to_c(v, self, arguments)
1869 v.current_node = oldnode
1870 else if self.mproperty.is_root_init then
1871 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1872 var oldnode = v.current_node
1873 v.current_node = nclassdef
1874 self.compile_parameter_check(v, arguments)
1875 nclassdef.compile_to_c(v, self, arguments)
1876 v.current_node = oldnode
1877 else if val != null then
1878 v.ret(v.value_instance(val))
1885 # Generate type checks in the C code to check covariant parameters
1886 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1888 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1890 for i in [0..msignature.arity[ do
1891 # skip test for vararg since the array is instantiated with the correct polymorphic type
1892 if msignature.vararg_rank == i then continue
1894 # skip if the cast is not required
1895 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1896 if not origmtype.need_anchor then continue
1898 # get the parameter type
1899 var mtype = self.msignature.mparameters[i].mtype
1902 # note that v decides if and how to implements the cast
1903 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1904 v.add_cast(arguments[i+1], mtype, "covariance")
1911 redef class APropdef
1912 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1914 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1915 debug("Not yet implemented")
1918 fun can_inline: Bool do return true
1921 redef class AMethPropdef
1922 redef fun compile_to_c(v, mpropdef, arguments)
1924 if mpropdef.is_abstract then
1925 var cn = v.class_name_string(arguments.first)
1926 v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s
` called on `%s
`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1931 # Call the implicit super-init
1932 var auto_super_inits = self.auto_super_inits
1933 if auto_super_inits != null then
1934 var args = [arguments.first]
1935 for auto_super_init in auto_super_inits do
1936 assert auto_super_init.mproperty != mpropdef.mproperty
1938 for i in [0..auto_super_init.msignature.arity+1[ do
1939 args.add(arguments[i])
1941 assert auto_super_init.mproperty != mpropdef.mproperty
1942 v.compile_callsite(auto_super_init, args)
1945 if auto_super_call then
1946 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
1949 # Try special compilation
1950 if mpropdef.is_intern then
1951 if compile_intern_to_c(v, mpropdef, arguments) then return
1952 else if mpropdef.is_extern then
1953 if mpropdef.mproperty.is_init then
1954 if compile_externinit_to_c(v, mpropdef, arguments) then return
1956 if compile_externmeth_to_c(v, mpropdef, arguments) then return
1960 # Compile block if any
1961 var n_block = n_block
1962 if n_block != null then
1963 for i in [0..mpropdef.msignature.arity[ do
1964 var variable = self.n_signature.n_params[i].variable.as(not null)
1965 v.assign(v.variable(variable), arguments[i+1])
1972 var cn = v.class_name_string(arguments.first)
1973 v.add("PRINT_ERROR(\"Runtime error: uncompiled method `%s
` called on `%s
`. NOT YET IMPLEMENTED\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1977 redef fun can_inline
1979 if self.auto_super_inits != null then return false
1980 var nblock = self.n_block
1981 if nblock == null then return true
1982 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1983 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1987 fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
1989 var pname = mpropdef.mproperty.name
1990 var cname = mpropdef.mclassdef.mclass.name
1991 var ret = mpropdef.msignature.return_mtype
1993 ret = v.resolve_for(ret, arguments.first)
1995 if pname != "==" and pname != "!=" then
1996 v.adapt_signature(mpropdef, arguments)
1997 v.unbox_signature_extern(mpropdef, arguments)
1999 if cname == "Int" then
2000 if pname == "output" then
2001 v.add("printf(\"%ld\\n\", {arguments.first});")
2003 else if pname == "object_id" then
2004 v.ret(arguments.first)
2006 else if pname == "+" then
2007 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2009 else if pname == "-" then
2010 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2012 else if pname == "unary -" then
2013 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2015 else if pname == "*" then
2016 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2018 else if pname == "/" then
2019 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2021 else if pname == "%" then
2022 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
2024 else if pname == "lshift" then
2025 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
2027 else if pname == "rshift" then
2028 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
2030 else if pname == "==" then
2031 v.ret(v.equal_test(arguments[0], arguments[1]))
2033 else if pname == "!=" then
2034 var res = v.equal_test(arguments[0], arguments[1])
2035 v.ret(v.new_expr("!{res}", ret.as(not null)))
2037 else if pname == "<" then
2038 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2040 else if pname == ">" then
2041 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2043 else if pname == "<=" then
2044 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2046 else if pname == ">=" then
2047 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2049 else if pname == "to_f" then
2050 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
2052 else if pname == "ascii" then
2053 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
2056 else if cname == "Char" then
2057 if pname == "output" then
2058 v.add("printf(\"%c\", {arguments.first});")
2060 else if pname == "object_id" then
2061 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2063 else if pname == "successor" then
2064 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2066 else if pname == "predecessor" then
2067 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2069 else if pname == "==" then
2070 v.ret(v.equal_test(arguments[0], arguments[1]))
2072 else if pname == "!=" then
2073 var res = v.equal_test(arguments[0], arguments[1])
2074 v.ret(v.new_expr("!{res}", ret.as(not null)))
2076 else if pname == "<" then
2077 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2079 else if pname == ">" then
2080 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2082 else if pname == "<=" then
2083 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2085 else if pname == ">=" then
2086 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2088 else if pname == "to_i" then
2089 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
2091 else if pname == "ascii" then
2092 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
2095 else if cname == "Bool" then
2096 if pname == "output" then
2097 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
2099 else if pname == "object_id" then
2100 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2102 else if pname == "==" then
2103 v.ret(v.equal_test(arguments[0], arguments[1]))
2105 else if pname == "!=" then
2106 var res = v.equal_test(arguments[0], arguments[1])
2107 v.ret(v.new_expr("!{res}", ret.as(not null)))
2110 else if cname == "Float" then
2111 if pname == "output" then
2112 v.add("printf(\"%f\\n\", {arguments.first});")
2114 else if pname == "object_id" then
2115 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
2117 else if pname == "+" then
2118 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2120 else if pname == "-" then
2121 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2123 else if pname == "unary -" then
2124 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2126 else if pname == "succ" then
2127 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
2129 else if pname == "prec" then
2130 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
2132 else if pname == "*" then
2133 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2135 else if pname == "/" then
2136 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2138 else if pname == "==" then
2139 v.ret(v.equal_test(arguments[0], arguments[1]))
2141 else if pname == "!=" then
2142 var res = v.equal_test(arguments[0], arguments[1])
2143 v.ret(v.new_expr("!{res}", ret.as(not null)))
2145 else if pname == "<" then
2146 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2148 else if pname == ">" then
2149 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2151 else if pname == "<=" then
2152 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2154 else if pname == ">=" then
2155 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2157 else if pname == "to_i" then
2158 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
2161 else if cname == "NativeString" then
2162 if pname == "[]" then
2163 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
2165 else if pname == "[]=" then
2166 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2168 else if pname == "copy_to" then
2169 v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2171 else if pname == "atoi" then
2172 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
2174 else if pname == "new" then
2175 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2178 else if cname == "NativeArray" then
2179 v.native_array_def(pname, ret, arguments)
2182 if pname == "exit" then
2183 v.add("exit({arguments[1]});")
2185 else if pname == "sys" then
2186 v.ret(v.new_expr("glob_sys", ret.as(not null)))
2188 else if pname == "calloc_string" then
2189 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2191 else if pname == "calloc_array" then
2192 v.calloc_array(ret.as(not null), arguments)
2194 else if pname == "object_id" then
2195 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2197 else if pname == "is_same_type" then
2198 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
2200 else if pname == "is_same_instance" then
2201 v.ret(v.equal_test(arguments[0], arguments[1]))
2203 else if pname == "output_class_name" then
2204 var nat = v.class_name_string(arguments.first)
2205 v.add("printf(\"%s\\n\", {nat});")
2207 else if pname == "native_class_name" then
2208 var nat = v.class_name_string(arguments.first)
2209 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
2211 else if pname == "force_garbage_collection" then
2212 v.add("nit_gcollect();")
2214 else if pname == "native_argc" then
2215 v.ret(v.new_expr("glob_argc", ret.as(not null)))
2217 else if pname == "native_argv" then
2218 v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
2224 # Compile an extern method
2225 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2226 fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2229 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2231 externname = at.arg_as_string(v.compiler.modelbuilder)
2232 if externname == null then return false
2236 if location.file != null then
2237 var file = location.file.filename
2240 var res: nullable RuntimeVariable = null
2241 var ret = mpropdef.msignature.return_mtype
2243 ret = v.resolve_for(ret, arguments.first)
2244 res = v.new_var_extern(ret)
2246 v.adapt_signature(mpropdef, arguments)
2247 v.unbox_signature_extern(mpropdef, arguments)
2250 v.add("{externname}({arguments.join(", ")});")
2252 v.add("{res} = {externname}({arguments.join(", ")});")
2253 res = v.box_extern(res, ret.as(not null))
2259 # Compile an extern factory
2260 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2261 fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2264 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2266 externname = at.arg_as_string(v.compiler.modelbuilder)
2267 if externname == null then return false
2271 if location.file != null then
2272 var file = location.file.filename
2275 v.adapt_signature(mpropdef, arguments)
2276 v.unbox_signature_extern(mpropdef, arguments)
2277 var ret = arguments.first.mtype
2278 var res = v.new_var_extern(ret)
2282 v.add("{res} = {externname}({arguments.join(", ")});")
2283 res = v.box_extern(res, ret)
2289 redef class AAttrPropdef
2290 redef fun compile_to_c(v, mpropdef, arguments)
2292 if mpropdef == mreadpropdef then
2293 assert arguments.length == 1
2297 assert nexpr != null
2299 var ret = self.mpropdef.static_mtype
2300 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2301 var guard = self.mlazypropdef.mproperty
2303 set = v.isset_attribute(self.mpropdef.mproperty, arguments.first)
2305 set = v.read_attribute(guard, arguments.first)
2307 v.add("if(likely({set})) \{")
2308 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2310 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2311 v.write_attribute(self.mpropdef.mproperty, arguments.first, value)
2312 v.assign(res, value)
2314 var true_v = v.new_expr("1", v.bool_type)
2315 v.write_attribute(guard, arguments.first, true_v)
2319 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2321 v.assign(v.frame.returnvar.as(not null), res)
2322 else if mpropdef == mwritepropdef then
2323 assert arguments.length == 2
2324 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
2326 var ret = self.mpropdef.static_mtype
2327 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2329 v.write_attribute(self.mlazypropdef.mproperty, arguments.first, v.new_expr("1", v.bool_type))
2337 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2339 var nexpr = self.n_expr
2340 if nexpr != null and not is_lazy then
2341 var oldnode = v.current_node
2342 v.current_node = self
2343 var old_frame = v.frame
2344 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2346 var value = v.expr(nexpr, self.mpropdef.static_mtype)
2347 v.write_attribute(self.mpropdef.mproperty, recv, value)
2349 v.current_node = oldnode
2353 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2355 var nexpr = self.n_expr
2356 if nexpr != null then return
2358 var oldnode = v.current_node
2359 v.current_node = self
2360 var old_frame = v.frame
2361 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2363 # Force read to check the initialization
2364 v.read_attribute(self.mpropdef.mproperty, recv)
2366 v.current_node = oldnode
2370 redef class AClassdef
2371 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2373 if mpropdef == self.mfree_init then
2374 assert mpropdef.mproperty.is_root_init
2375 assert arguments.length == 1
2376 if not mpropdef.is_intro then
2377 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
2387 # Try to compile self as an expression
2388 # Do not call this method directly, use `v
.expr
` instead
2389 private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
2391 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
2392 var mtype = self.mtype
2393 if mtype == null then
2396 var res = v.new_var(mtype)
2397 v.add("/* {res} = NOT YET {class_name} */")
2402 # Try to compile self as a statement
2403 # Do not call this method directly, use `v
.stmt
` instead
2404 private fun stmt(v: AbstractCompilerVisitor)
2410 redef class ABlockExpr
2413 for e in self.n_expr do v.stmt(e)
2417 var last = self.n_expr.last
2418 for e in self.n_expr do
2419 if e == last then break
2422 return v.expr(last, null)
2426 redef class AVardeclExpr
2429 var variable = self.variable.as(not null)
2430 var ne = self.n_expr
2432 var i = v.expr(ne, variable.declared_type)
2433 v.assign(v.variable(variable), i)
2438 redef class AVarExpr
2441 var res = v.variable(self.variable.as(not null))
2442 var mtype = self.mtype.as(not null)
2443 return v.autoadapt(res, mtype)
2447 redef class AVarAssignExpr
2450 var variable = self.variable.as(not null)
2451 var i = v.expr(self.n_value, variable.declared_type)
2452 v.assign(v.variable(variable), i)
2457 redef class AVarReassignExpr
2460 var variable = self.variable.as(not null)
2461 var vari = v.variable(variable)
2462 var value = v.expr(self.n_value, variable.declared_type)
2463 var res = v.compile_callsite(self.reassign_callsite.as(not null), [vari, value])
2465 v.assign(v.variable(variable), res)
2469 redef class ASelfExpr
2470 redef fun expr(v) do return v.frame.arguments.first
2473 redef class AEscapeExpr
2474 redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
2477 redef class AReturnExpr
2480 var nexpr = self.n_expr
2481 if nexpr != null then
2482 var returnvar = v.frame.returnvar.as(not null)
2483 var i = v.expr(nexpr, returnvar.mtype)
2484 v.assign(returnvar, i)
2486 v.add("goto {v.frame.returnlabel.as(not null)};")
2490 redef class AAbortExpr
2491 redef fun stmt(v) do v.add_abort("Aborted")
2497 var cond = v.expr_bool(self.n_expr)
2498 v.add("if ({cond})\{")
2507 var res = v.new_var(self.mtype.as(not null))
2508 var cond = v.expr_bool(self.n_expr)
2509 v.add("if ({cond})\{")
2510 v.assign(res, v.expr(self.n_then.as(not null), null))
2512 v.assign(res, v.expr(self.n_else.as(not null), null))
2518 redef class AIfexprExpr
2521 var res = v.new_var(self.mtype.as(not null))
2522 var cond = v.expr_bool(self.n_expr)
2523 v.add("if ({cond})\{")
2524 v.assign(res, v.expr(self.n_then, null))
2526 v.assign(res, v.expr(self.n_else, null))
2535 v.stmt(self.n_block)
2536 v.add_escape_label(break_mark)
2540 redef class AWhileExpr
2544 var cond = v.expr_bool(self.n_expr)
2545 v.add("if (!{cond}) break;")
2546 v.stmt(self.n_block)
2547 v.add_escape_label(continue_mark)
2549 v.add_escape_label(break_mark)
2553 redef class ALoopExpr
2557 v.stmt(self.n_block)
2558 v.add_escape_label(continue_mark)
2560 v.add_escape_label(break_mark)
2564 redef class AForExpr
2567 var cl = v.expr(self.n_expr, null)
2568 var it_meth = self.method_iterator
2569 assert it_meth != null
2570 var it = v.compile_callsite(it_meth, [cl])
2573 var isok_meth = self.method_is_ok
2574 assert isok_meth != null
2575 var ok = v.compile_callsite(isok_meth, [it])
2577 v.add("if(!{ok}) break;")
2578 if self.variables.length == 1 then
2579 var item_meth = self.method_item
2580 assert item_meth != null
2581 var i = v.compile_callsite(item_meth, [it])
2583 v.assign(v.variable(variables.first), i)
2584 else if self.variables.length == 2 then
2585 var key_meth = self.method_key
2586 assert key_meth != null
2587 var i = v.compile_callsite(key_meth, [it])
2589 v.assign(v.variable(variables[0]), i)
2590 var item_meth = self.method_item
2591 assert item_meth != null
2592 i = v.compile_callsite(item_meth, [it])
2594 v.assign(v.variable(variables[1]), i)
2598 v.stmt(self.n_block)
2599 v.add_escape_label(continue_mark)
2600 var next_meth = self.method_next
2601 assert next_meth != null
2602 v.compile_callsite(next_meth, [it])
2604 v.add_escape_label(break_mark)
2606 var method_finish = self.method_finish
2607 if method_finish != null then
2608 # TODO: Find a way to call this also in long escape (e.g. return)
2609 v.compile_callsite(method_finish, [it])
2614 redef class AAssertExpr
2617 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return
2619 var cond = v.expr_bool(self.n_expr)
2620 v.add("if (unlikely(!{cond})) \{")
2624 v.add_abort("Assert '{nid.text}' failed")
2626 v.add_abort("Assert failed")
2635 var res = v.new_var(self.mtype.as(not null))
2636 var i1 = v.expr_bool(self.n_expr)
2637 v.add("if ({i1}) \{")
2640 var i2 = v.expr_bool(self.n_expr2)
2641 v.add("{res} = {i2};")
2647 redef class AImpliesExpr
2650 var res = v.new_var(self.mtype.as(not null))
2651 var i1 = v.expr_bool(self.n_expr)
2652 v.add("if (!{i1}) \{")
2655 var i2 = v.expr_bool(self.n_expr2)
2656 v.add("{res} = {i2};")
2662 redef class AAndExpr
2665 var res = v.new_var(self.mtype.as(not null))
2666 var i1 = v.expr_bool(self.n_expr)
2667 v.add("if (!{i1}) \{")
2670 var i2 = v.expr_bool(self.n_expr2)
2671 v.add("{res} = {i2};")
2677 redef class ANotExpr
2680 var cond = v.expr_bool(self.n_expr)
2681 return v.new_expr("!{cond}", self.mtype.as(not null))
2685 redef class AOrElseExpr
2688 var res = v.new_var(self.mtype.as(not null))
2689 var i1 = v.expr(self.n_expr, null)
2690 v.add("if ({i1}!=NULL) \{")
2693 var i2 = v.expr(self.n_expr2, null)
2700 redef class AIntExpr
2701 redef fun expr(v) do return v.new_expr("{self.value.to_s}", self.mtype.as(not null))
2704 redef class AFloatExpr
2705 redef fun expr(v) do return v.new_expr("{self.n_float.text}", self.mtype.as(not null)) # FIXME use value, not n_float
2708 redef class ACharExpr
2709 redef fun expr(v) do return v.new_expr("'{self.value.to_s.escape_to_c}'", self.mtype.as(not null))
2712 redef class AArrayExpr
2715 var mtype = self.mtype.as(MClassType).arguments.first
2716 var array = new Array[RuntimeVariable]
2717 for nexpr in self.n_exprs.n_exprs do
2718 var i = v.expr(nexpr, mtype)
2721 return v.array_instance(array, mtype)
2725 redef class AStringFormExpr
2726 redef fun expr(v) do return v.string_instance(self.value.as(not null))
2729 redef class ASuperstringExpr
2732 var array = new Array[RuntimeVariable]
2733 for ne in self.n_exprs do
2734 if ne isa AStringFormExpr and ne.value == "" then continue # skip empty sub-strings
2735 var i = v.expr(ne, null)
2738 var a = v.array_instance(array, v.object_type)
2739 var res = v.send(v.get_property("to_s", a.mtype), [a])
2744 redef class ACrangeExpr
2747 var i1 = v.expr(self.n_expr, null)
2748 var i2 = v.expr(self.n_expr2, null)
2749 var mtype = self.mtype.as(MClassType)
2750 var res = v.init_instance(mtype)
2751 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2756 redef class AOrangeExpr
2759 var i1 = v.expr(self.n_expr, null)
2760 var i2 = v.expr(self.n_expr2, null)
2761 var mtype = self.mtype.as(MClassType)
2762 var res = v.init_instance(mtype)
2763 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2768 redef class ATrueExpr
2769 redef fun expr(v) do return v.new_expr("1", self.mtype.as(not null))
2772 redef class AFalseExpr
2773 redef fun expr(v) do return v.new_expr("0", self.mtype.as(not null))
2776 redef class ANullExpr
2777 redef fun expr(v) do return v.new_expr("NULL", self.mtype.as(not null))
2780 redef class AIsaExpr
2783 var i = v.expr(self.n_expr, null)
2784 return v.type_test(i, self.cast_type.as(not null), "isa")
2788 redef class AAsCastExpr
2791 var i = v.expr(self.n_expr, null)
2792 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2794 v.add_cast(i, self.mtype.as(not null), "as")
2799 redef class AAsNotnullExpr
2802 var i = v.expr(self.n_expr, null)
2803 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2805 if i.mtype.ctype != "val*" then return i
2807 v.add("if (unlikely({i} == NULL)) \{")
2808 v.add_abort("Cast failed")
2814 redef class AParExpr
2815 redef fun expr(v) do return v.expr(self.n_expr, null)
2818 redef class AOnceExpr
2821 var mtype = self.mtype.as(not null)
2822 var name = v.get_name("varonce")
2823 var guard = v.get_name(name + "_guard")
2824 v.add_decl("static {mtype.ctype} {name};")
2825 v.add_decl("static int {guard};")
2826 var res = v.new_var(mtype)
2827 v.add("if ({guard}) \{")
2828 v.add("{res} = {name};")
2830 var i = v.expr(self.n_expr, mtype)
2831 v.add("{res} = {i};")
2832 v.add("{name} = {res};")
2833 v.add("{guard} = 1;")
2839 redef class ASendExpr
2842 var recv = v.expr(self.n_expr, null)
2843 var callsite = self.callsite.as(not null)
2844 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2845 return v.compile_callsite(callsite, args)
2849 redef class ASendReassignFormExpr
2852 var recv = v.expr(self.n_expr, null)
2853 var callsite = self.callsite.as(not null)
2854 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2856 var value = v.expr(self.n_value, null)
2858 var left = v.compile_callsite(callsite, args)
2861 var res = v.compile_callsite(self.reassign_callsite.as(not null), [left, value])
2865 v.compile_callsite(self.write_callsite.as(not null), args)
2869 redef class ASuperExpr
2872 var recv = v.frame.arguments.first
2874 var callsite = self.callsite
2875 if callsite != null then
2876 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2878 # Add additional arguments for the super init call
2879 if args.length == 1 then
2880 for i in [0..callsite.msignature.arity[ do
2881 args.add(v.frame.arguments[i+1])
2885 var res = v.compile_callsite(callsite, args)
2889 var mpropdef = self.mpropdef.as(not null)
2890 var args = v.varargize(mpropdef, recv, self.n_args.n_exprs)
2891 if args.length == 1 then
2892 args = v.frame.arguments
2895 # stantard call-next-method
2896 return v.supercall(mpropdef, recv.mtype.as(MClassType), args)
2900 redef class ANewExpr
2903 var mtype = self.recvtype
2904 assert mtype != null
2906 var ctype = mtype.ctype
2907 if mtype.mclass.name == "NativeArray" then
2908 assert self.n_args.n_exprs.length == 1
2909 var l = v.expr(self.n_args.n_exprs.first, null)
2910 assert mtype isa MGenericType
2911 var elttype = mtype.arguments.first
2912 return v.native_array_instance(elttype, l)
2913 else if ctype == "val*" then
2914 recv = v.init_instance(mtype)
2915 else if ctype == "char*" then
2916 recv = v.new_expr("NULL/*special!*/", mtype)
2918 recv = v.new_expr("({ctype})0/*special!*/", mtype)
2921 var callsite = self.callsite.as(not null)
2922 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2923 var res2 = v.compile_callsite(callsite, args)
2924 if res2 != null then
2925 #self.debug("got {res2} from {mproperty}. drop {recv}")
2932 redef class AAttrExpr
2935 var recv = v.expr(self.n_expr, null)
2936 var mproperty = self.mproperty.as(not null)
2937 return v.read_attribute(mproperty, recv)
2941 redef class AAttrAssignExpr
2944 var recv = v.expr(self.n_expr, null)
2945 var i = v.expr(self.n_value, null)
2946 var mproperty = self.mproperty.as(not null)
2947 v.write_attribute(mproperty, recv, i)
2952 redef class AAttrReassignExpr
2955 var recv = v.expr(self.n_expr, null)
2956 var value = v.expr(self.n_value, null)
2957 var mproperty = self.mproperty.as(not null)
2958 var attr = v.read_attribute(mproperty, recv)
2959 var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
2961 v.write_attribute(mproperty, recv, res)
2965 redef class AIssetAttrExpr
2968 var recv = v.expr(self.n_expr, null)
2969 var mproperty = self.mproperty.as(not null)
2970 return v.isset_attribute(mproperty, recv)
2974 redef class ADebugTypeExpr
2983 redef class Array[E]
2984 # Return a new `Array` with the elements only contened in self and not in `o
`
2985 fun -(o: Array[E]): Array[E] do
2986 var res = new Array[E]
2987 for e in self do if not o.has(e) then res.add(e)
2993 # All `MProperty` associated to all `MClassDef` of `mclass
`
2994 fun properties(mclass: MClass): Set[MProperty] do
2995 if not self.properties_cache.has_key(mclass) then
2996 var properties = new HashSet[MProperty]
2997 var parents = new Array[MClass]
2998 if self.flatten_mclass_hierarchy.has(mclass) then
2999 parents.add_all(mclass.in_hierarchy(self).direct_greaters)
3001 for parent in parents do
3002 properties.add_all(self.properties(parent))
3004 for mclassdef in mclass.mclassdefs do
3005 if not self.in_importation <= mclassdef.mmodule then continue
3006 for mprop in mclassdef.intro_mproperties do
3007 properties.add(mprop)
3010 self.properties_cache[mclass] = properties
3012 return properties_cache[mclass]
3014 private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
3016 # Write FFI and nitni results to file
3017 fun finalize_ffi(c: AbstractCompiler) do end
3019 # Give requided addinional system libraries (as given to LD_LIBS)
3020 # Note: can return null instead of an empty set
3021 fun collect_linker_libs: nullable Set[String] do return null
3024 # Create a tool context to handle options and paths
3025 var toolcontext = new ToolContext
3027 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
3029 # We do not add other options, so process them now!
3030 toolcontext.process_options(args)
3032 # We need a model to collect stufs
3033 var model = new Model
3034 # An a model builder to parse files
3035 var modelbuilder = new ModelBuilder(model, toolcontext)
3037 var arguments = toolcontext.option_context.rest
3038 if arguments.length > 1 and toolcontext.opt_output.value != null then
3039 print "Error: --output needs a single source file. Do you prefer --dir?"
3043 # Here we load an process all modules passed on the command line
3044 var mmodules = modelbuilder.parse(arguments)
3046 if mmodules.is_empty then return
3047 modelbuilder.run_phases
3049 for mmodule in mmodules do
3050 toolcontext.info("*** PROCESS {mmodule} ***", 1)
3052 toolcontext.run_global_phases(ms)