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_make_flags
= new OptionString("Additional options to make", "--make-flags")
40 var opt_max_c_lines
= new OptionInt("Maximum number of lines in generated C files. Use 0 for unlimited", 10000, "--max-c-lines")
42 var opt_group_c_files
= new OptionBool("Group all generated code in the same series of files", "--group-c-files")
44 var opt_compile_dir
= new OptionString("Directory used to generate temporary files", "--compile-dir")
46 var opt_hardening
= new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
47 # --no-check-covariance
48 var opt_no_check_covariance
= new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
49 # --no-check-attr-isset
50 var opt_no_check_attr_isset
= new OptionBool("Disable isset tests before each attribute access (dangerous)", "--no-check-attr-isset")
52 var opt_no_check_assert
= new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
54 var opt_no_check_autocast
= new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
56 var opt_no_check_null
= new OptionBool("Disable tests of null receiver (dangerous)", "--no-check-null")
58 var opt_no_check_all
= new OptionBool("Disable all tests (dangerous)", "--no-check-all")
59 # --typing-test-metrics
60 var opt_typing_test_metrics
= new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
61 # --invocation-metrics
62 var opt_invocation_metrics
= new OptionBool("Enable static and dynamic count of all method invocations", "--invocation-metrics")
63 # --isset-checks-metrics
64 var opt_isset_checks_metrics
= new OptionBool("Enable static and dynamic count of isset checks before attributes access", "--isset-checks-metrics")
66 var opt_stacktrace
= new OptionString("Control the generation of stack traces", "--stacktrace")
68 var opt_no_gcc_directive
= new OptionArray("Disable a advanced gcc directives for optimization", "--no-gcc-directive")
70 var opt_release
= new OptionBool("Compile in release mode and finalize application", "--release")
75 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
)
76 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
)
77 self.option_context
.add_option
(self.opt_typing_test_metrics
, self.opt_invocation_metrics
, self.opt_isset_checks_metrics
)
78 self.option_context
.add_option
(self.opt_stacktrace
)
79 self.option_context
.add_option
(self.opt_no_gcc_directive
)
80 self.option_context
.add_option
(self.opt_release
)
81 self.option_context
.add_option
(self.opt_max_c_lines
, self.opt_group_c_files
)
84 redef fun process_options
(args
)
88 var st
= opt_stacktrace
.value
89 if st
== "none" or st
== "libunwind" or st
== "nitstack" then
91 else if st
== "auto" or st
== null then
93 opt_stacktrace
.value
= "nitstack"
95 print
"Error: unknown value `{st}` for --stacktrace. Use `none`, `libunwind`, `nitstack` or `auto`."
99 if opt_output
.value
!= null and opt_dir
.value
!= null then
100 print
"Error: cannot use both --dir and --output"
104 if opt_no_check_all
.value
then
105 opt_no_check_covariance
.value
= true
106 opt_no_check_attr_isset
.value
= true
107 opt_no_check_assert
.value
= true
108 opt_no_check_autocast
.value
= true
109 opt_no_check_null
.value
= true
114 redef class ModelBuilder
115 # The compilation directory
116 var compile_dir
: String
118 # Simple indirection to `Toolchain::write_and_make`
119 protected fun write_and_make
(compiler
: AbstractCompiler)
121 var platform
= compiler
.mainmodule
.target_platform
123 if platform
== null then
124 toolchain
= new MakefileToolchain(toolcontext
)
126 toolchain
= platform
.toolchain
(toolcontext
)
128 compile_dir
= toolchain
.compile_dir
129 toolchain
.write_and_make compiler
134 fun toolchain
(toolcontext
: ToolContext): Toolchain is abstract
138 var toolcontext
: ToolContext
140 fun compile_dir
: String
142 var compile_dir
= toolcontext
.opt_compile_dir
.value
143 if compile_dir
== null then compile_dir
= ".nit_compile"
147 fun write_and_make
(compiler
: AbstractCompiler) is abstract
150 class MakefileToolchain
153 redef fun write_and_make
(compiler
)
155 var compile_dir
= compile_dir
157 # Generate the .h and .c files
158 # A single C file regroups many compiled rumtime functions
159 # 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
161 self.toolcontext
.info
("*** WRITING C ***", 1)
165 var cfiles
= new Array[String]
166 write_files
(compiler
, compile_dir
, cfiles
)
168 # Generate the Makefile
170 write_makefile
(compiler
, compile_dir
, cfiles
)
173 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
175 # Execute the Makefile
177 if self.toolcontext
.opt_no_cc
.value
then return
180 self.toolcontext
.info
("*** COMPILING C ***", 1)
182 compile_c_code
(compiler
, compile_dir
)
185 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
188 fun write_files
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
190 var platform
= compiler
.mainmodule
.target_platform
191 if self.toolcontext
.opt_stacktrace
.value
== "nitstack" and (platform
== null or platform
.supports_libunwind
) then compiler
.build_c_to_nit_bindings
192 var cc_opt_with_libgc
= "-DWITH_LIBGC `pkg-config --cflags bdw-gc`"
193 if platform
!= null and not platform
.supports_libgc
then cc_opt_with_libgc
= ""
195 # Add gc_choser.h to aditionnal bodies
196 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
197 compiler
.extern_bodies
.add
(gc_chooser
)
198 var clib
= toolcontext
.nit_dir
/ "clib"
199 compiler
.files_to_copy
.add
"{clib}/gc_chooser.c"
200 compiler
.files_to_copy
.add
"{clib}/gc_chooser.h"
203 for m
in compiler
.mainmodule
.in_importation
.greaters
do
204 compiler
.finalize_ffi_for_module
(m
)
207 # Copy original .[ch] files to compile_dir
208 for src
in compiler
.files_to_copy
do
209 var basename
= src
.basename
("")
210 var dst
= "{compile_dir}/{basename}"
214 var hfilename
= compiler
.header
.file
.name
+ ".h"
215 var hfilepath
= "{compile_dir}/{hfilename}"
216 var h
= new OFStream.open
(hfilepath
)
217 for l
in compiler
.header
.decl_lines
do
221 for l
in compiler
.header
.lines
do
227 var max_c_lines
= toolcontext
.opt_max_c_lines
.value
228 for f
in compiler
.files
do
231 var file
: nullable OFStream = null
232 for vis
in f
.writers
do
233 if vis
== compiler
.header
then continue
234 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
235 if total_lines
== 0 then continue
237 if file
== null or (count
> max_c_lines
and max_c_lines
> 0) then
239 if file
!= null then file
.close
240 var cfilename
= "{f.name}.{i}.c"
241 var cfilepath
= "{compile_dir}/{cfilename}"
242 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
243 cfiles
.add
(cfilename
)
244 file
= new OFStream.open
(cfilepath
)
245 file
.write
"#include \"{f.name}.0.h\
"\n"
248 for l
in vis
.decl_lines
do
252 for l
in vis
.lines
do
257 if file
== null then continue
260 var cfilename
= "{f.name}.0.h"
261 var cfilepath
= "{compile_dir}/{cfilename}"
262 var hfile
: nullable OFStream = null
263 hfile
= new OFStream.open
(cfilepath
)
264 hfile
.write
"#include \"{hfilename}\
"\n"
265 for key
in f
.required_declarations
do
266 if not compiler
.provided_declarations
.has_key
(key
) then
267 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
269 node
.debug
"No provided declaration for {key}"
271 print
"No provided declaration for {key}"
275 hfile
.write compiler
.provided_declarations
[key
]
281 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
284 fun makefile_name
(mainmodule
: MModule): String do return "{mainmodule.name}.mk"
286 fun default_outname
(mainmodule
: MModule): String
288 # Search a non fictive module
289 var res
= mainmodule
.name
290 while mainmodule
.is_fictive
do
291 mainmodule
= mainmodule
.in_importation
.direct_greaters
.first
292 res
= mainmodule
.name
297 # Combine options and platform informations to get the final path of the outfile
298 fun outfile
(mainmodule
: MModule): String
300 var res
= self.toolcontext
.opt_output
.value
301 if res
!= null then return res
302 res
= default_outname
(mainmodule
)
303 var dir
= self.toolcontext
.opt_dir
.value
304 if dir
!= null then return dir
.join_path
(res
)
308 fun write_makefile
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
310 var mainmodule
= compiler
.mainmodule
311 var platform
= compiler
.mainmodule
.target_platform
313 var outname
= outfile
(mainmodule
)
315 var outpath
= compile_dir
.relpath
(outname
)
316 var makename
= makefile_name
(mainmodule
)
317 var makepath
= "{compile_dir}/{makename}"
318 var makefile
= new OFStream.open
(makepath
)
320 var linker_options
= new HashSet[String]
321 for m
in mainmodule
.in_importation
.greaters
do
322 var libs
= m
.collect_linker_libs
323 if libs
!= null then linker_options
.add_all
(libs
)
326 makefile
.write
("CC = ccache cc\nCXX = ccache c++\nCFLAGS = -g -O2 -Wno-unused-value -Wno-switch\nCINCL =\nLDFLAGS ?= \nLDLIBS ?= -lm `pkg-config --libs bdw-gc` {linker_options.join(" ")}\n\n")
328 var ost
= toolcontext
.opt_stacktrace
.value
329 if (ost
== "libunwind" or ost
== "nitstack") and (platform
== null or platform
.supports_libunwind
) then makefile
.write
("NEED_LIBUNWIND := YesPlease\n")
331 # Dynamic adaptations
332 # While `platform` enable complex toolchains, they are statically applied
333 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
335 # Check and adapt the targeted system
336 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
337 makefile
.write
("ifeq ($(uname_S),Darwin)\n")
338 # remove -lunwind since it is already included on macosx
339 makefile
.write
("\tNEED_LIBUNWIND :=\n")
340 makefile
.write
("endif\n\n")
342 # Check and adapt for the compiler used
343 # clang need an additionnal `-Qunused-arguments`
344 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")
346 makefile
.write
("ifdef NEED_LIBUNWIND\n\tLDLIBS += -lunwind\nendif\n")
348 makefile
.write
("all: {outpath}\n\n")
350 var ofiles
= new Array[String]
351 var dep_rules
= new Array[String]
352 # Compile each generated file
354 var o
= f
.strip_extension
(".c") + ".o"
355 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
360 var java_files
= new Array[ExternFile]
362 # Compile each required extern body into a specific .o
363 for f
in compiler
.extern_bodies
do
364 var o
= f
.makefile_rule_name
365 var ff
= f
.filename
.basename
("")
366 makefile
.write
("{o}: {ff}\n")
367 makefile
.write
("\t{f.makefile_rule_content}\n\n")
368 dep_rules
.add
(f
.makefile_rule_name
)
370 if f
.compiles_to_o_file
then ofiles
.add
(o
)
371 if f
.add_to_jar
then java_files
.add
(f
)
374 if not java_files
.is_empty
then
375 var jar_file
= "{outpath}.jar"
377 var class_files_array
= new Array[String]
378 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
379 var class_files
= class_files_array
.join
(" ")
381 makefile
.write
("{jar_file}: {class_files}\n")
382 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
383 dep_rules
.add jar_file
387 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath} {ofiles.join(" ")} $(LDLIBS)\n\n")
389 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
391 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
393 makepath
.file_copy_to
"{compile_dir}/Makefile"
396 fun compile_c_code
(compiler
: AbstractCompiler, compile_dir
: String)
398 var makename
= makefile_name
(compiler
.mainmodule
)
400 var makeflags
= self.toolcontext
.opt_make_flags
.value
401 if makeflags
== null then makeflags
= ""
402 self.toolcontext
.info
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags}", 2)
405 if self.toolcontext
.verbose_level
>= 3 then
406 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1")
408 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
411 toolcontext
.error
(null, "make failed! Error code: {res}.")
416 # Singleton that store the knowledge about the compilation process
417 abstract class AbstractCompiler
418 type VISITOR: AbstractCompilerVisitor
420 # Table corresponding c_names to nit names (methods)
421 var names
= new HashMap[String, String]
423 # The main module of the program currently compiled
424 # Is assigned during the separate compilation
425 var mainmodule
: MModule is writable
427 # The real main module of the program
428 var realmainmodule
: MModule
430 # The modelbuilder used to know the model and the AST
431 var modelbuilder
: ModelBuilder is protected writable
433 # Is hardening asked? (see --hardening)
434 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
436 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
438 self.mainmodule
= mainmodule
439 self.realmainmodule
= mainmodule
440 self.modelbuilder
= modelbuilder
443 # Force the creation of a new file
444 # The point is to avoid contamination between must-be-compiled-separately files
445 fun new_file
(name
: String): CodeFile
447 if modelbuilder
.toolcontext
.opt_group_c_files
.value
then
448 if self.files
.is_empty
then
449 var f
= new CodeFile(mainmodule
.name
)
452 return self.files
.first
454 var f
= new CodeFile(name
)
459 # The list of all associated files
460 # Used to generate .c files
461 var files
= new List[CodeFile]
463 # Initialize a visitor specific for a compiler engine
464 fun new_visitor
: VISITOR is abstract
466 # Where global declaration are stored (the main .h)
467 var header
: CodeWriter is writable
469 # Provide a declaration that can be requested (before or latter) by a visitor
470 fun provide_declaration
(key
: String, s
: String)
472 if self.provided_declarations
.has_key
(key
) then
473 assert self.provided_declarations
[key
] == s
475 self.provided_declarations
[key
] = s
478 private var provided_declarations
= new HashMap[String, String]
480 private var requirers_of_declarations
= new HashMap[String, ANode]
482 # Builds the .c and .h files to be used when generating a Stack Trace
483 # Binds the generated C function names to Nit function names
484 fun build_c_to_nit_bindings
486 var compile_dir
= modelbuilder
.compile_dir
488 var stream
= new OFStream.open
("{compile_dir}/c_functions_hash.c")
489 stream
.write
("#include <string.h>\n")
490 stream
.write
("#include <stdlib.h>\n")
491 stream
.write
("#include \"c_functions_hash
.h\
"\n")
492 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
493 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
494 stream
.write
("char* procname = malloc(len+1);")
495 stream
.write
("memcpy(procname, procproc, len);")
496 stream
.write
("procname[len] = '\\0';")
497 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
498 for i
in names
.keys
do
501 stream.write("\
",\"")
502 stream.write(names[i])
503 stream.write("\
"\},\n")
505 stream
.write
("\};\n")
506 stream
.write
("int i;")
507 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
508 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
509 stream
.write
("free(procname);")
510 stream
.write
("return map[i].nit_name;")
513 stream
.write
("free(procname);")
514 stream
.write
("return NULL;")
518 stream
= new OFStream.open
("{compile_dir}/c_functions_hash.h")
519 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
522 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
526 # This method call compile_header_strucs method that has to be refined
527 fun compile_header
do
528 self.header
.add_decl
("#include <stdlib.h>")
529 self.header
.add_decl
("#include <stdio.h>")
530 self.header
.add_decl
("#include <string.h>")
531 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
532 self.header
.add_decl
("#ifdef ANDROID")
533 self.header
.add_decl
(" #include <android/log.h>")
534 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
535 self.header
.add_decl
("#else")
536 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
537 self.header
.add_decl
("#endif")
539 compile_header_structs
540 compile_nitni_structs
542 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
543 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
544 # Signal handler function prototype
545 self.header
.add_decl
("void show_backtrace(int);")
547 self.header
.add_decl
("void show_backtrace(int) __attribute__ ((noreturn));")
550 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
551 self.header
.add_decl
("#define likely(x) (x)")
552 self.header
.add_decl
("#define unlikely(x) (x)")
553 else if gccd_disable
.has
("correct-likely") then
554 # invert the `likely` definition
555 # Used by masochists to bench the worst case
556 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
557 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
559 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
560 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
563 # Global variable used by intern methods
564 self.header
.add_decl
("extern int glob_argc;")
565 self.header
.add_decl
("extern char **glob_argv;")
566 self.header
.add_decl
("extern val *glob_sys;")
569 # Declaration of structures for live Nit types
570 protected fun compile_header_structs
is abstract
572 # Declaration of structures for nitni undelying the FFI
573 protected fun compile_nitni_structs
575 self.header
.add_decl
"""
576 /* Native reference to Nit objects */
577 /* This structure is used to represent every Nit type in extern methods and custom C code. */
579 struct nitni_ref *next,
580 *prev; /* adjacent global references in global list */
581 int count; /* number of time this global reference has been marked */
584 /* List of global references from C code to Nit objects */
585 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
586 struct nitni_global_ref_list_t {
587 struct nitni_ref *head, *tail;
589 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
591 /* Initializer of global reference list */
592 extern void nitni_global_ref_list_init();
594 /* Intern function to add a global reference to the list */
595 extern void nitni_global_ref_add( struct nitni_ref *ref );
597 /* Intern function to remove a global reference from the list */
598 extern void nitni_global_ref_remove( struct nitni_ref *ref );
600 /* Increase count on an existing global reference */
601 extern void nitni_global_ref_incr( struct nitni_ref *ref );
603 /* Decrease count on an existing global reference */
604 extern void nitni_global_ref_decr( struct nitni_ref *ref );
608 fun compile_finalizer_function
610 var finalizable_type
= mainmodule
.finalizable_type
611 if finalizable_type
== null then return
613 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
615 if finalize_meth
== null then
616 modelbuilder
.toolcontext
.error
(null, "The `Finalizable` class doesn't declare the `finalize` method.")
620 var v
= self.new_visitor
621 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
622 var recv
= v
.new_expr
("obj", finalizable_type
)
623 v
.send
(finalize_meth
, [recv
])
627 # Generate the main C function.
631 # * allocate the Sys object if it exists
632 # * call init if is exists
633 # * call main if it exists
634 fun compile_main_function
636 var v
= self.new_visitor
637 v
.add_decl
("#include <signal.h>")
638 var ost
= modelbuilder
.toolcontext
.opt_stacktrace
.value
639 var platform
= mainmodule
.target_platform
641 if platform
!= null and not platform
.supports_libunwind
then ost
= "none"
643 var no_main
= (platform
!= null and platform
.no_main
) or modelbuilder
.toolcontext
.opt_no_main
.value
645 if ost
== "nitstack" or ost
== "libunwind" then
646 v
.add_decl
("#define UNW_LOCAL_ONLY")
647 v
.add_decl
("#include <libunwind.h>")
648 if ost
== "nitstack" then
649 v
.add_decl
("#include \"c_functions_hash
.h\
"")
652 v
.add_decl
("int glob_argc;")
653 v
.add_decl
("char **glob_argv;")
654 v
.add_decl
("val *glob_sys;")
656 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
657 for tag
in count_type_test_tags
do
658 v
.add_decl
("long count_type_test_resolved_{tag};")
659 v
.add_decl
("long count_type_test_unresolved_{tag};")
660 v
.add_decl
("long count_type_test_skipped_{tag};")
661 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
662 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
663 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
667 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
668 v
.add_decl
("long count_invoke_by_tables;")
669 v
.add_decl
("long count_invoke_by_direct;")
670 v
.add_decl
("long count_invoke_by_inline;")
671 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
672 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
673 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
676 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
677 v
.add_decl
("long count_attr_reads = 0;")
678 v
.add_decl
("long count_isset_checks = 0;")
679 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
680 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
683 v
.add_decl
("void sig_handler(int signo)\{")
684 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
685 v
.add_decl
("show_backtrace(signo);")
688 v
.add_decl
("void show_backtrace (int signo) \{")
689 if ost
== "nitstack" or ost
== "libunwind" then
690 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
691 v
.add_decl
("unw_cursor_t cursor;")
692 v
.add_decl
("if(opt==NULL)\{")
693 v
.add_decl
("unw_context_t uc;")
694 v
.add_decl
("unw_word_t ip;")
695 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
696 v
.add_decl
("unw_getcontext(&uc);")
697 v
.add_decl
("unw_init_local(&cursor, &uc);")
698 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
699 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
700 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
701 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
702 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
703 if ost
== "nitstack" then
704 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
705 v
.add_decl
(" if (recv != NULL)\{")
706 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
707 v.add_decl(" \}else\{")
708 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
711 v
.add_decl
(" PRINT_ERROR(\"` %s \\n\",procname);")
714 v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
715 v.add_decl("free(procname);")
718 v.add_decl("exit(signo);")
722 v.add_decl("int nit_main(int argc, char** argv) \{")
724 v.add_decl("int main(int argc, char** argv) \{")
727 v.add("signal(SIGABRT, sig_handler);")
728 v.add("signal(SIGFPE, sig_handler);")
729 v.add("signal(SIGILL, sig_handler);")
730 v.add("signal(SIGINT, sig_handler);")
731 v.add("signal(SIGTERM, sig_handler);")
732 v.add("signal(SIGSEGV, sig_handler);")
733 v.add("signal(SIGPIPE, sig_handler);")
735 v.add("glob_argc = argc; glob_argv = argv;")
736 v.add("initialize_gc_option();")
738 v.add "initialize_nitni_global_refs();"
740 var main_type = mainmodule.sys_type
741 if main_type != null then
742 var mainmodule = v.compiler.mainmodule
743 var glob_sys = v.init_instance(main_type)
744 v.add("glob_sys = {glob_sys};")
745 var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
746 if main_init != null then
747 v.send(main_init, [glob_sys])
749 var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
750 mainmodule.try_get_primitive_method("main", main_type.mclass)
751 if main_method != null then
752 v.send(main_method, [glob_sys])
756 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
757 v.add_decl("long count_type_test_resolved_total = 0;")
758 v.add_decl("long count_type_test_unresolved_total = 0;")
759 v.add_decl("long count_type_test_skipped_total = 0;")
760 v.add_decl("long count_type_test_total_total = 0;")
761 for tag in count_type_test_tags do
762 v.add_decl("long count_type_test_total_{tag};")
763 v.add("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
764 v.add("count_type_test_resolved_total += count_type_test_resolved_{tag};")
765 v.add("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
766 v.add("count_type_test_skipped_total += count_type_test_skipped_{tag};")
767 v.add("count_type_test_total_total += count_type_test_total_{tag};")
769 v.add("printf(\"# dynamic count_type_test: total %l\\n\");")
770 v.add("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\");")
771 var tags = count_type_test_tags.to_a
774 v.add("printf(\"{tag}\");")
775 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
776 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
777 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
778 v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
782 if self.modelbuilder.toolcontext.opt_invocation_metrics.value then
783 v.add_decl("long count_invoke_total;")
784 v.add("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
785 v.add("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
786 v.add("printf(\"by table: %ld (%.2f%%)\\n\", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
787 v.add("printf(\"direct: %ld (%.2f%%)\\n\", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
788 v.add("printf(\"inlined: %ld (%.2f%%)\\n\", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
791 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
792 v.add("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
793 v.add("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
800 # Copile all C functions related to the [incr|decr]_ref features of the FFI
801 fun compile_nitni_global_ref_functions
803 var v = self.new_visitor
805 struct nitni_global_ref_list_t *nitni_global_ref_list;
806 void initialize_nitni_global_refs() {
807 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
808 nitni_global_ref_list->head = NULL;
809 nitni_global_ref_list->tail = NULL;
812 void nitni_global_ref_add( struct nitni_ref *ref ) {
813 if ( nitni_global_ref_list->head == NULL ) {
814 nitni_global_ref_list->head = ref;
817 nitni_global_ref_list->tail->next = ref;
818 ref->prev = nitni_global_ref_list->tail;
820 nitni_global_ref_list->tail = ref;
825 void nitni_global_ref_remove( struct nitni_ref *ref ) {
826 if ( ref->prev == NULL ) {
827 nitni_global_ref_list->head = ref->next;
829 ref->prev->next = ref->next;
832 if ( ref->next == NULL ) {
833 nitni_global_ref_list->tail = ref->prev;
835 ref->next->prev = ref->prev;
839 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
840 if ( ref->count == 0 ) /* not registered */
843 nitni_global_ref_add( ref );
849 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
850 if ( ref->count == 1 ) /* was last reference */
852 /* remove from list */
853 nitni_global_ref_remove( ref );
861 # List of additional files required to compile (FFI)
862 var extern_bodies = new Array[ExternFile]
864 # List of source files to copy over to the compile dir
865 var files_to_copy = new Array[String]
867 # This is used to avoid adding an extern file more than once
868 private var seen_extern = new ArraySet[String]
870 # Generate code that initialize the attributes on a new instance
871 fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
873 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
874 self.mainmodule.linearize_mclassdefs(cds)
876 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
877 var n = self.modelbuilder.mclassdef2nclassdef[cd]
878 for npropdef in n.n_propdefs do
879 if npropdef isa AAttrPropdef then
880 npropdef.init_expr(v, recv)
886 # Generate code that check if an attribute is correctly initialized
887 fun generate_check_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
889 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
890 self.mainmodule.linearize_mclassdefs(cds)
892 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
893 var n = self.modelbuilder.mclassdef2nclassdef[cd]
894 for npropdef in n.n_propdefs do
895 if npropdef isa AAttrPropdef then
896 npropdef.check_expr(v, recv)
904 var count_type_test_tags: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
905 var count_type_test_resolved: HashMap[String, Int] = init_count_type_test_tags
906 var count_type_test_unresolved: HashMap[String, Int] = init_count_type_test_tags
907 var count_type_test_skipped: HashMap[String, Int] = init_count_type_test_tags
909 protected fun init_count_type_test_tags: HashMap[String, Int]
911 var res = new HashMap[String, Int]
912 for tag in count_type_test_tags do
918 # Display stats about compilation process
922 # * type tests against resolved types (`x
isa Collection[Animal]`)
923 # * type tests against unresolved types (`x
isa Collection[E
]`)
924 # * type tests skipped
928 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
929 print "# static count_type_test"
930 print "\tresolved:\tunresolved\tskipped\ttotal"
931 var count_type_test_total = init_count_type_test_tags
932 count_type_test_resolved["total"] = 0
933 count_type_test_unresolved["total"] = 0
934 count_type_test_skipped["total"] = 0
935 count_type_test_total["total"] = 0
936 for tag in count_type_test_tags do
937 count_type_test_total[tag] = count_type_test_resolved[tag] + count_type_test_unresolved[tag] + count_type_test_skipped[tag]
938 count_type_test_resolved["total"] += count_type_test_resolved[tag]
939 count_type_test_unresolved["total"] += count_type_test_unresolved[tag]
940 count_type_test_skipped["total"] += count_type_test_skipped[tag]
941 count_type_test_total["total"] += count_type_test_total[tag]
943 var count_type_test = count_type_test_total["total"]
944 var tags = count_type_test_tags.to_a
948 printn "\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
949 printn "\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
950 printn "\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
951 printn "\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
957 fun finalize_ffi_for_module(mmodule: MModule) do mmodule.finalize_ffi(self)
960 # Avoid division by zero by returning the string "n/a"
961 fun div(a,b:Int):String
963 if b == 0 then return "n/a"
964 return ((a*10000/b).to_f / 100.0).to_precision(2)
968 # A file unit (may be more than one file if
969 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
972 var writers = new Array[CodeWriter]
973 var required_declarations = new HashSet[String]
976 # Where to store generated lines
979 var lines: List[String] = new List[String]
980 var decl_lines: List[String] = new List[String]
982 # Add a line in the main part of the generated C
983 fun add(s: String) do self.lines.add(s)
986 # (used for local or global declaration)
987 fun add_decl(s: String) do self.decl_lines.add(s)
992 file.writers.add(self)
996 # A visitor on the AST of property definition that generate the C code.
997 abstract class AbstractCompilerVisitor
999 type COMPILER: AbstractCompiler
1001 # The associated compiler
1002 var compiler: COMPILER
1004 # The current visited AST node
1005 var current_node: nullable ANode = null is writable
1007 # The current `Frame`
1008 var frame: nullable Frame is writable
1010 # Alias for self.compiler.mainmodule.object_type
1011 fun object_type: MClassType do return self.compiler.mainmodule.object_type
1013 # Alias for self.compiler.mainmodule.bool_type
1014 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
1016 var writer: CodeWriter
1018 init(compiler: COMPILER)
1020 self.compiler = compiler
1021 self.writer = new CodeWriter(compiler.files.last)
1024 # Force to get the primitive class named `name
` or abort
1025 fun get_class(name: String): MClass do return self.compiler.mainmodule.get_primitive_class(name)
1027 # Force to get the primitive property named `name
` in the instance `recv
` or abort
1028 fun get_property(name: String, recv: MType): MMethod
1030 assert recv isa MClassType
1031 return self.compiler.modelbuilder.force_get_primitive_method(self.current_node, name, recv.mclass, self.compiler.mainmodule)
1034 fun compile_callsite(callsite: CallSite, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1036 var initializers = callsite.mpropdef.initializers
1037 if not initializers.is_empty then
1038 var recv = arguments.first
1041 for p in initializers do
1042 if p isa MMethod then
1044 for x in p.intro.msignature.mparameters do
1045 args.add arguments[i]
1049 else if p isa MAttribute then
1050 self.write_attribute(p, recv, arguments[i])
1054 assert i == arguments.length
1056 return self.send(callsite.mproperty, [recv])
1059 return self.send(callsite.mproperty, arguments)
1062 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable is abstract
1064 fun calloc_array(ret_type: MType, arguments: Array[RuntimeVariable]) is abstract
1066 fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract
1068 # Evaluate `args
` as expressions in the call of `mpropdef
` on `recv
`.
1069 # This method is used to manage varargs in signatures and returns the real array
1070 # of runtime variables to use in the call.
1071 fun varargize(mpropdef: MMethodDef, recv: RuntimeVariable, args: SequenceRead[AExpr]): Array[RuntimeVariable]
1073 var msignature = mpropdef.new_msignature or else mpropdef.msignature.as(not null)
1074 var res = new Array[RuntimeVariable]
1077 if args.is_empty then return res
1079 var vararg_rank = msignature.vararg_rank
1080 var vararg_len = args.length - msignature.arity
1081 if vararg_len < 0 then vararg_len = 0
1083 for i in [0..msignature.arity[ do
1084 if i == vararg_rank then
1086 if ne isa AVarargExpr then
1087 var e = self.expr(ne.n_expr, null)
1091 var vararg = new Array[RuntimeVariable]
1092 for j in [vararg_rank..vararg_rank+vararg_len] do
1093 var e = self.expr(args[j], null)
1096 var elttype = msignature.mparameters[vararg_rank].mtype
1097 var arg = self.vararg_instance(mpropdef, recv, vararg, elttype)
1101 if i > vararg_rank then j += vararg_len
1102 var e = self.expr(args[j], null)
1111 # Anchor a type to the main module and the current receiver
1112 fun anchor(mtype: MType): MType
1114 if not mtype.need_anchor then return mtype
1115 return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
1118 fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
1120 if not mtype.need_anchor then return mtype
1121 return mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
1124 # Unsafely cast a value to a new type
1125 # ie the result share the same C variable but my have a different mcasttype
1126 # NOTE: if the adaptation is useless then `value
` is returned as it.
1127 # ENSURE: `result
.name
== value
.name
`
1128 fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
1130 mtype = self.anchor(mtype)
1131 var valmtype = value.mcasttype
1132 if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1136 if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1137 var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
1140 var res = new RuntimeVariable(value.name, valmtype, mtype)
1145 # Generate a super call from a method definition
1146 fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1148 # Adapt the arguments of a method according to targetted `MMethodDef`
1149 fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1151 # Unbox all the arguments of a method when implemented `extern` or `intern`
1152 fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1154 # Box or unbox a value to another type iff a C type conversion is needed
1155 # ENSURE: `result
.mtype
.ctype
== mtype
.ctype
`
1156 fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1158 # Box extern classes to be used in the generated code
1159 fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1161 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1162 fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1164 # Generate a polymorphic subtype test
1165 fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
1167 # Generate the code required to dynamically check if 2 objects share the same runtime type
1168 fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1170 # Generate a Nit "is" for two runtime_variables
1171 fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1175 # Generate a static call on a method definition
1176 fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1178 # Generate a polymorphic send for the method `m
` and the arguments `args
`
1179 fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1181 # Generate a monomorphic send for the method `m
`, the type `t
` and the arguments `args
`
1182 fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1184 assert t isa MClassType
1185 var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
1186 return self.call(propdef, t, args)
1189 # Generate a monomorphic super send from the method `m
`, the type `t
` and the arguments `args
`
1190 fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1192 assert t isa MClassType
1193 m = m.lookup_next_definition(self.compiler.mainmodule, t)
1194 return self.call(m, t, args)
1197 # Attributes handling
1199 # Generate a polymorphic attribute is_set test
1200 fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1202 # Generate a polymorphic attribute read
1203 fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1205 # Generate a polymorphic attribute write
1206 fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable) is abstract
1210 # Add a check and an abort for a null receiver if needed
1211 fun check_recv_notnull(recv: RuntimeVariable)
1213 if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
1215 var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType
1217 self.add("if (unlikely({recv} == NULL)) \{")
1218 self.add_abort("Receiver is null")
1225 private var names = new HashSet[String]
1226 private var last: Int = 0
1228 # Return a new name based on `s
` and unique in the visitor
1229 fun get_name(s: String): String
1231 if not self.names.has(s) then
1235 var i = self.last + 1
1238 if not self.names.has(s2) then
1247 # Return an unique and stable identifier associated with an escapemark
1248 fun escapemark_name(e: nullable EscapeMark): String
1251 if escapemark_names.has_key(e) then return escapemark_names[e]
1253 if name == null then name = "label"
1254 name = get_name(name)
1255 escapemark_names[e] = name
1259 # Insert a C label for associated with an escapemark
1260 fun add_escape_label(e: nullable EscapeMark)
1262 if e == null then return
1263 if e.escapes.is_empty then return
1264 add("BREAK_{escapemark_name(e)}: (void)0;")
1267 private var escapemark_names = new HashMap[EscapeMark, String]
1269 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1270 # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
1271 fun class_name_string(value: RuntimeVariable): String is abstract
1273 # Variables handling
1275 protected var variables = new HashMap[Variable, RuntimeVariable]
1277 # Return the local runtime_variable associated to a Nit local variable
1278 fun variable(variable: Variable): RuntimeVariable
1280 if self.variables.has_key(variable) then
1281 return self.variables[variable]
1283 var name = self.get_name("var_{variable.name}")
1284 var mtype = variable.declared_type.as(not null)
1285 mtype = self.anchor(mtype)
1286 var res = new RuntimeVariable(name, mtype, mtype)
1287 self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1288 self.variables[variable] = res
1293 # Return a new uninitialized local runtime_variable
1294 fun new_var(mtype: MType): RuntimeVariable
1296 mtype = self.anchor(mtype)
1297 var name = self.get_name("var")
1298 var res = new RuntimeVariable(name, mtype, mtype)
1299 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1303 # The difference with `new_var
` is the C static type of the local variable
1304 fun new_var_extern(mtype: MType): RuntimeVariable
1306 mtype = self.anchor(mtype)
1307 var name = self.get_name("var")
1308 var res = new RuntimeVariable(name, mtype, mtype)
1309 self.add_decl("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1313 # Return a new uninitialized named runtime_variable
1314 fun new_named_var(mtype: MType, name: String): RuntimeVariable
1316 mtype = self.anchor(mtype)
1317 var res = new RuntimeVariable(name, mtype, mtype)
1318 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1322 # Correctly assign a left and a right value
1323 # Boxing and unboxing is performed if required
1324 fun assign(left, right: RuntimeVariable)
1326 right = self.autobox(right, left.mtype)
1327 self.add("{left} = {right};")
1330 # Generate instances
1332 # Generate a alloc-instance + init-attributes
1333 fun init_instance(mtype: MClassType): RuntimeVariable is abstract
1335 # Set a GC finalizer on `recv
`, only if `recv
` isa Finalizable
1336 fun set_finalizer(recv: RuntimeVariable)
1338 var mtype = recv.mtype
1339 var finalizable_type = compiler.mainmodule.finalizable_type
1340 if finalizable_type != null and not mtype.need_anchor and
1341 mtype.is_subtype(compiler.mainmodule, null, finalizable_type) then
1342 add "gc_register_finalizer({recv});"
1346 # Generate an integer value
1347 fun int_instance(value: Int): RuntimeVariable
1349 var res = self.new_var(self.get_class("Int").mclass_type)
1350 self.add("{res} = {value};")
1354 # Generate an integer value
1355 fun bool_instance(value: Bool): RuntimeVariable
1357 var res = self.new_var(self.get_class("Bool").mclass_type)
1359 self.add("{res} = 1;")
1361 self.add("{res} = 0;")
1366 # Generate a string value
1367 fun string_instance(string: String): RuntimeVariable
1369 var mtype = self.get_class("String").mclass_type
1370 var name = self.get_name("varonce")
1371 self.add_decl("static {mtype.ctype} {name};")
1372 var res = self.new_var(mtype)
1373 self.add("if ({name}) \{")
1374 self.add("{res} = {name};")
1375 self.add("\} else \{")
1376 var native_mtype = self.get_class("NativeString").mclass_type
1377 var nat = self.new_var(native_mtype)
1378 self.add("{nat} = \"{string.escape_to_c}\";")
1379 var length = self.int_instance(string.length)
1380 self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
1381 self.add("{name} = {res};")
1386 fun value_instance(object: Object): RuntimeVariable
1388 if object isa Int then
1389 return int_instance(object)
1390 else if object isa Bool then
1391 return bool_instance(object)
1392 else if object isa String then
1393 return string_instance(object)
1399 # Generate an array value
1400 fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1402 # Get an instance of a array for a vararg
1403 fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1407 # Add a line in the main part of the generated C
1408 fun add(s: String) do self.writer.lines.add(s)
1411 # (used for local or global declaration)
1412 fun add_decl(s: String) do self.writer.decl_lines.add(s)
1414 # Request the presence of a global declaration
1415 fun require_declaration(key: String)
1417 var reqs = self.writer.file.required_declarations
1418 if reqs.has(key) then return
1420 var node = current_node
1421 if node != null then compiler.requirers_of_declarations[key] = node
1424 # Add a declaration in the local-header
1425 # The declaration is ensured to be present once
1426 fun declare_once(s: String)
1428 self.compiler.provide_declaration(s, s)
1429 self.require_declaration(s)
1432 # look for a needed .h and .c file for a given .nit source-file
1433 # FIXME: bad API, parameter should be a `MModule`, not its source-file
1434 fun add_extern(file: String)
1436 file = file.strip_extension(".nit")
1437 var tryfile = file + ".nit.h"
1438 if tryfile.file_exists then
1439 self.declare_once("#include \"{tryfile.basename("")}\"")
1440 self.compiler.files_to_copy.add(tryfile)
1442 tryfile = file + "_nit.h"
1443 if tryfile.file_exists then
1444 self.declare_once("#include \"{tryfile.basename("")}\"")
1445 self.compiler.files_to_copy.add(tryfile)
1448 if self.compiler.seen_extern.has(file) then return
1449 self.compiler.seen_extern.add(file)
1450 tryfile = file + ".nit.c"
1451 if not tryfile.file_exists then
1452 tryfile = file + "_nit.c"
1453 if not tryfile.file_exists then return
1455 var f = new ExternCFile(tryfile.basename(""), "")
1456 self.compiler.extern_bodies.add(f)
1457 self.compiler.files_to_copy.add(tryfile)
1460 # Return a new local runtime_variable initialized with the C expression `cexpr
`.
1461 fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
1463 var res = new_var(mtype)
1464 self.add("{res} = {cexpr};")
1468 # Generate generic abort
1469 # used by aborts, asserts, casts, etc.
1470 fun add_abort(message: String)
1472 self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
1478 if self.current_node != null and self.current_node.location.file != null then
1479 self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
1481 self.add("PRINT_ERROR(\"\\n\");")
1483 self.add("show_backtrace(1);")
1486 # Add a dynamic cast
1487 fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)
1489 var res = self.type_test(value, mtype, tag)
1490 self.add("if (unlikely(!{res})) \{")
1491 var cn = self.class_name_string(value)
1492 self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s
`, got `%s
`\", \"{mtype.to_s.escape_to_c}\", {cn});")
1497 # Generate a return with the value `s
`
1498 fun ret(s: RuntimeVariable)
1500 self.assign(self.frame.returnvar.as(not null), s)
1501 self.add("goto {self.frame.returnlabel.as(not null)};")
1504 # Compile a statement (if any)
1505 fun stmt(nexpr: nullable AExpr)
1507 if nexpr == null then return
1508 var old = self.current_node
1509 self.current_node = nexpr
1511 self.current_node = old
1514 # Compile an expression an return its result
1515 # `mtype
` is the expected return type, pass null if no specific type is expected.
1516 fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
1518 var old = self.current_node
1519 self.current_node = nexpr
1520 var res = nexpr.expr(self).as(not null)
1521 if mtype != null then
1522 mtype = self.anchor(mtype)
1523 res = self.autobox(res, mtype)
1525 res = autoadapt(res, nexpr.mtype.as(not null))
1526 var implicit_cast_to = nexpr.implicit_cast_to
1527 if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
1528 add_cast(res, implicit_cast_to, "auto")
1529 res = autoadapt(res, implicit_cast_to)
1531 self.current_node = old
1535 # Alias for `self.expr
(nexpr
, self.bool_type
)`
1536 fun expr_bool(nexpr: AExpr): RuntimeVariable do return expr(nexpr, bool_type)
1538 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1539 fun debug(message: String)
1541 var node = self.current_node
1542 if node == null then
1543 print "?: {message}"
1547 self.add("/* DEBUG: {message} */")
1551 # A C function associated to a Nit method
1552 # Because of customization, a given Nit method can be compiler more that once
1553 abstract class AbstractRuntimeFunction
1555 type COMPILER: AbstractCompiler
1556 type VISITOR: AbstractCompilerVisitor
1558 # The associated Nit method
1559 var mmethoddef: MMethodDef
1561 # The mangled c name of the runtime_function
1562 # Subclasses should redefine `build_c_name
` instead
1565 var res = self.c_name_cache
1566 if res != null then return res
1567 res = self.build_c_name
1568 self.c_name_cache = res
1572 # Non cached version of `c_name
`
1573 protected fun build_c_name: String is abstract
1575 protected var c_name_cache: nullable String = null is writable
1577 # Implements a call of the runtime_function
1578 # May inline the body or generate a C function call
1579 fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1581 # Generate the code for the `AbstractRuntimeFunction`
1582 # Warning: compile more than once compilation makes CC unhappy
1583 fun compile_to_c(compiler: COMPILER) is abstract
1586 # A runtime variable hold a runtime value in C.
1587 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
1589 # 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.
1590 class RuntimeVariable
1591 # The name of the variable in the C code
1594 # The static type of the variable (as declard in C)
1597 # The current casted type of the variable (as known in Nit)
1598 var mcasttype: MType is writable
1600 # If the variable exaclty a mcasttype?
1601 # false (usual value) means that the variable is a mcasttype or a subtype.
1602 var is_exact: Bool = false is writable
1604 init(name: String, mtype: MType, mcasttype: MType)
1608 self.mcasttype = mcasttype
1609 assert not mtype.need_anchor
1610 assert not mcasttype.need_anchor
1613 redef fun to_s do return name
1618 if self.is_exact then
1619 exact_str = " exact"
1624 if self.mtype == self.mcasttype then
1625 type_str = "{mtype}{exact_str}"
1627 type_str = "{mtype}({mcasttype}{exact_str})"
1629 return "<{name}:{type_str}>"
1633 # A frame correspond to a visited property in a `GlobalCompilerVisitor`
1636 type VISITOR: AbstractCompilerVisitor
1638 # The associated visitor
1639 var visitor: VISITOR
1641 # The executed property.
1642 # A Method in case of a call, an attribute in case of a default initialization.
1643 var mpropdef: MPropDef
1645 # The static type of the receiver
1646 var receiver: MClassType
1648 # Arguments of the method (the first is the receiver)
1649 var arguments: Array[RuntimeVariable]
1651 # The runtime_variable associated to the return (in a function)
1652 var returnvar: nullable RuntimeVariable = null is writable
1654 # The label at the end of the property
1655 var returnlabel: nullable String = null is writable
1659 # Return the C type associated to a given Nit static type
1660 fun ctype: String do return "val*"
1662 # C type outside of the compiler code and in boxes
1663 fun ctype_extern: String do return "val*"
1665 # Short name of the `ctype
` to use in unions
1666 fun ctypename: String do return "val"
1668 # Return the name of the C structure associated to a Nit live type
1669 fun c_name: String is abstract
1670 protected var c_name_cache: nullable String is protected writable
1673 redef class MClassType
1676 var res = self.c_name_cache
1677 if res != null then return res
1678 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1679 self.c_name_cache = res
1683 redef fun ctype: String
1685 if mclass.name == "Int" then
1687 else if mclass.name == "Bool" then
1689 else if mclass.name == "Char" then
1691 else if mclass.name == "Float" then
1693 else if mclass.name == "NativeString" then
1695 else if mclass.name == "NativeArray" then
1702 redef fun ctype_extern: String
1704 if mclass.kind == extern_kind then
1711 redef fun ctypename: String
1713 if mclass.name == "Int" then
1715 else if mclass.name == "Bool" then
1717 else if mclass.name == "Char" then
1719 else if mclass.name == "Float" then
1721 else if mclass.name == "NativeString" then
1723 else if mclass.name == "NativeArray" then
1724 #return "{self.arguments.first.ctype}*"
1732 redef class MGenericType
1735 var res = self.c_name_cache
1736 if res != null then return res
1738 for t in self.arguments do
1739 res = res + t.c_name
1741 self.c_name_cache = res
1746 redef class MParameterType
1749 var res = self.c_name_cache
1750 if res != null then return res
1751 res = "{self.mclass.c_name}_FT{self.rank}"
1752 self.c_name_cache = res
1757 redef class MVirtualType
1760 var res = self.c_name_cache
1761 if res != null then return res
1762 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1763 self.c_name_cache = res
1768 redef class MNullableType
1771 var res = self.c_name_cache
1772 if res != null then return res
1773 res = "nullable_{self.mtype.c_name}"
1774 self.c_name_cache = res
1780 # Return the name of the C structure associated to a Nit class
1781 fun c_name: String do
1782 var res = self.c_name_cache
1783 if res != null then return res
1784 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1785 self.c_name_cache = res
1788 private var c_name_cache: nullable String
1791 redef class MProperty
1792 fun c_name: String do
1793 var res = self.c_name_cache
1794 if res != null then return res
1795 res = "{self.intro.c_name}"
1796 self.c_name_cache = res
1799 private var c_name_cache: nullable String
1802 redef class MPropDef
1803 type VISITOR: AbstractCompilerVisitor
1805 private var c_name_cache: nullable String
1807 # The mangled name associated to the property
1810 var res = self.c_name_cache
1811 if res != null then return res
1812 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1813 self.c_name_cache = res
1818 redef class MMethodDef
1819 # Can the body be inlined?
1820 fun can_inline(v: VISITOR): Bool
1822 if is_abstract then return true
1823 var modelbuilder = v.compiler.modelbuilder
1824 if modelbuilder.mpropdef2npropdef.has_key(self) then
1825 var npropdef = modelbuilder.mpropdef2npropdef[self]
1826 return npropdef.can_inline
1827 else if self.mproperty.is_root_init then
1828 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1835 # Inline the body in another visitor
1836 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1838 var modelbuilder = v.compiler.modelbuilder
1839 var val = constant_value
1840 if modelbuilder.mpropdef2npropdef.has_key(self) then
1841 var npropdef = modelbuilder.mpropdef2npropdef[self]
1842 var oldnode = v.current_node
1843 v.current_node = npropdef
1844 self.compile_parameter_check(v, arguments)
1845 npropdef.compile_to_c(v, self, arguments)
1846 v.current_node = oldnode
1847 else if self.mproperty.is_root_init then
1848 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1849 var oldnode = v.current_node
1850 v.current_node = nclassdef
1851 self.compile_parameter_check(v, arguments)
1852 nclassdef.compile_to_c(v, self, arguments)
1853 v.current_node = oldnode
1854 else if val != null then
1855 v.ret(v.value_instance(val))
1862 # Generate type checks in the C code to check covariant parameters
1863 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1865 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1867 for i in [0..msignature.arity[ do
1868 # skip test for vararg since the array is instantiated with the correct polymorphic type
1869 if msignature.vararg_rank == i then continue
1871 # skip if the cast is not required
1872 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1873 if not origmtype.need_anchor then continue
1875 # get the parameter type
1876 var mtype = self.msignature.mparameters[i].mtype
1879 # note that v decides if and how to implements the cast
1880 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1881 v.add_cast(arguments[i+1], mtype, "covariance")
1888 redef class APropdef
1889 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1891 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1892 debug("Not yet implemented")
1895 fun can_inline: Bool do return true
1898 redef class AMethPropdef
1899 redef fun compile_to_c(v, mpropdef, arguments)
1901 if mpropdef.is_abstract then
1902 var cn = v.class_name_string(arguments.first)
1903 v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s
` called on `%s
`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1908 # Call the implicit super-init
1909 var auto_super_inits = self.auto_super_inits
1910 if auto_super_inits != null then
1911 var args = [arguments.first]
1912 for auto_super_init in auto_super_inits do
1913 assert auto_super_init.mproperty != mpropdef.mproperty
1915 for i in [0..auto_super_init.msignature.arity+1[ do
1916 args.add(arguments[i])
1918 assert auto_super_init.mproperty != mpropdef.mproperty
1919 v.compile_callsite(auto_super_init, args)
1922 if auto_super_call then
1923 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
1926 # Try special compilation
1927 if mpropdef.is_intern then
1928 if compile_intern_to_c(v, mpropdef, arguments) then return
1929 else if mpropdef.is_extern then
1930 if mpropdef.mproperty.is_init then
1931 if compile_externinit_to_c(v, mpropdef, arguments) then return
1933 if compile_externmeth_to_c(v, mpropdef, arguments) then return
1937 # Compile block if any
1938 var n_block = n_block
1939 if n_block != null then
1940 for i in [0..mpropdef.msignature.arity[ do
1941 var variable = self.n_signature.n_params[i].variable.as(not null)
1942 v.assign(v.variable(variable), arguments[i+1])
1949 var cn = v.class_name_string(arguments.first)
1950 v.add("PRINT_ERROR(\"Runtime error: uncompiled method `%s
` called on `%s
`. NOT YET IMPLEMENTED\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1954 redef fun can_inline
1956 if self.auto_super_inits != null then return false
1957 var nblock = self.n_block
1958 if nblock == null then return true
1959 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1960 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1964 fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
1966 var pname = mpropdef.mproperty.name
1967 var cname = mpropdef.mclassdef.mclass.name
1968 var ret = mpropdef.msignature.return_mtype
1970 ret = v.resolve_for(ret, arguments.first)
1972 if pname != "==" and pname != "!=" then
1973 v.adapt_signature(mpropdef, arguments)
1974 v.unbox_signature_extern(mpropdef, arguments)
1976 if cname == "Int" then
1977 if pname == "output" then
1978 v.add("printf(\"%ld\\n\", {arguments.first});")
1980 else if pname == "object_id" then
1981 v.ret(arguments.first)
1983 else if pname == "+" then
1984 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1986 else if pname == "-" then
1987 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1989 else if pname == "unary -" then
1990 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1992 else if pname == "*" then
1993 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1995 else if pname == "/" then
1996 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1998 else if pname == "%" then
1999 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
2001 else if pname == "lshift" then
2002 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
2004 else if pname == "rshift" then
2005 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
2007 else if pname == "==" then
2008 v.ret(v.equal_test(arguments[0], arguments[1]))
2010 else if pname == "!=" then
2011 var res = v.equal_test(arguments[0], arguments[1])
2012 v.ret(v.new_expr("!{res}", ret.as(not null)))
2014 else if pname == "<" then
2015 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2017 else if pname == ">" then
2018 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2020 else if pname == "<=" then
2021 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", 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 == "to_f" then
2027 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
2029 else if pname == "ascii" then
2030 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
2033 else if cname == "Char" then
2034 if pname == "output" then
2035 v.add("printf(\"%c\", {arguments.first});")
2037 else if pname == "object_id" then
2038 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2040 else if pname == "successor" then
2041 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2043 else if pname == "predecessor" then
2044 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2046 else if pname == "==" then
2047 v.ret(v.equal_test(arguments[0], arguments[1]))
2049 else if pname == "!=" then
2050 var res = v.equal_test(arguments[0], arguments[1])
2051 v.ret(v.new_expr("!{res}", ret.as(not null)))
2053 else if pname == "<" then
2054 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2056 else if pname == ">" then
2057 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2059 else if pname == "<=" then
2060 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2062 else if pname == ">=" then
2063 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2065 else if pname == "to_i" then
2066 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
2068 else if pname == "ascii" then
2069 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
2072 else if cname == "Bool" then
2073 if pname == "output" then
2074 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
2076 else if pname == "object_id" then
2077 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2079 else if pname == "==" then
2080 v.ret(v.equal_test(arguments[0], arguments[1]))
2082 else if pname == "!=" then
2083 var res = v.equal_test(arguments[0], arguments[1])
2084 v.ret(v.new_expr("!{res}", ret.as(not null)))
2087 else if cname == "Float" then
2088 if pname == "output" then
2089 v.add("printf(\"%f\\n\", {arguments.first});")
2091 else if pname == "object_id" then
2092 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
2094 else if pname == "+" then
2095 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2097 else if pname == "-" then
2098 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2100 else if pname == "unary -" then
2101 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2103 else if pname == "succ" then
2104 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
2106 else if pname == "prec" then
2107 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
2109 else if pname == "*" then
2110 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2112 else if pname == "/" then
2113 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2115 else if pname == "==" then
2116 v.ret(v.equal_test(arguments[0], arguments[1]))
2118 else if pname == "!=" then
2119 var res = v.equal_test(arguments[0], arguments[1])
2120 v.ret(v.new_expr("!{res}", ret.as(not null)))
2122 else if pname == "<" then
2123 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2125 else if pname == ">" then
2126 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2128 else if pname == "<=" then
2129 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2131 else if pname == ">=" then
2132 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2134 else if pname == "to_i" then
2135 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
2138 else if cname == "NativeString" then
2139 if pname == "[]" then
2140 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
2142 else if pname == "[]=" then
2143 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2145 else if pname == "copy_to" then
2146 v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2148 else if pname == "atoi" then
2149 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
2151 else if pname == "new" then
2152 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2155 else if cname == "NativeArray" then
2156 v.native_array_def(pname, ret, arguments)
2159 if pname == "exit" then
2160 v.add("exit({arguments[1]});")
2162 else if pname == "sys" then
2163 v.ret(v.new_expr("glob_sys", ret.as(not null)))
2165 else if pname == "calloc_string" then
2166 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2168 else if pname == "calloc_array" then
2169 v.calloc_array(ret.as(not null), arguments)
2171 else if pname == "object_id" then
2172 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2174 else if pname == "is_same_type" then
2175 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
2177 else if pname == "is_same_instance" then
2178 v.ret(v.equal_test(arguments[0], arguments[1]))
2180 else if pname == "output_class_name" then
2181 var nat = v.class_name_string(arguments.first)
2182 v.add("printf(\"%s\\n\", {nat});")
2184 else if pname == "native_class_name" then
2185 var nat = v.class_name_string(arguments.first)
2186 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
2188 else if pname == "force_garbage_collection" then
2189 v.add("nit_gcollect();")
2191 else if pname == "native_argc" then
2192 v.ret(v.new_expr("glob_argc", ret.as(not null)))
2194 else if pname == "native_argv" then
2195 v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
2201 # Compile an extern method
2202 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2203 fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2206 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2208 externname = at.arg_as_string(v.compiler.modelbuilder)
2209 if externname == null then return false
2213 if location.file != null then
2214 var file = location.file.filename
2217 var res: nullable RuntimeVariable = null
2218 var ret = mpropdef.msignature.return_mtype
2220 ret = v.resolve_for(ret, arguments.first)
2221 res = v.new_var_extern(ret)
2223 v.adapt_signature(mpropdef, arguments)
2224 v.unbox_signature_extern(mpropdef, arguments)
2227 v.add("{externname}({arguments.join(", ")});")
2229 v.add("{res} = {externname}({arguments.join(", ")});")
2230 res = v.box_extern(res, ret.as(not null))
2236 # Compile an extern factory
2237 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2238 fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2241 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2243 externname = at.arg_as_string(v.compiler.modelbuilder)
2244 if externname == null then return false
2248 if location.file != null then
2249 var file = location.file.filename
2252 v.adapt_signature(mpropdef, arguments)
2253 v.unbox_signature_extern(mpropdef, arguments)
2254 var ret = arguments.first.mtype
2255 var res = v.new_var_extern(ret)
2259 v.add("{res} = {externname}({arguments.join(", ")});")
2260 res = v.box_extern(res, ret)
2266 redef class AAttrPropdef
2267 redef fun compile_to_c(v, mpropdef, arguments)
2269 if mpropdef == mreadpropdef then
2270 assert arguments.length == 1
2271 var recv = arguments.first
2275 var ret = self.mpropdef.static_mtype
2276 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2277 var guard = self.mlazypropdef.mproperty
2279 set = v.isset_attribute(self.mpropdef.mproperty, recv)
2281 set = v.read_attribute(guard, recv)
2283 v.add("if(likely({set})) \{")
2284 res = v.read_attribute(self.mpropdef.mproperty, recv)
2287 var value = evaluate_expr(v, recv)
2289 v.assign(res, value)
2291 var true_v = v.new_expr("1", v.bool_type)
2292 v.write_attribute(guard, arguments.first, true_v)
2296 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2298 v.assign(v.frame.returnvar.as(not null), res)
2299 else if mpropdef == mwritepropdef then
2300 assert arguments.length == 2
2301 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
2303 var ret = self.mpropdef.static_mtype
2304 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2306 v.write_attribute(self.mlazypropdef.mproperty, arguments.first, v.new_expr("1", v.bool_type))
2314 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2316 if has_value and not is_lazy then evaluate_expr(v, recv)
2319 # Evaluate, store and return the default value of the attribute
2320 private fun evaluate_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable): RuntimeVariable
2322 var oldnode = v.current_node
2323 v.current_node = self
2324 var old_frame = v.frame
2325 var frame = new Frame(v, self.mpropdef.as(not null), recv.mcasttype.as(MClassType), [recv])
2329 var mtype = self.mpropdef.static_mtype
2330 assert mtype != null
2332 var nexpr = self.n_expr
2333 var nblock = self.n_block
2334 if nexpr != null then
2335 value = v.expr(nexpr, mtype)
2336 else if nblock != null then
2337 value = v.new_var(mtype)
2338 frame.returnvar = value
2339 frame.returnlabel = v.get_name("RET_LABEL")
2342 v.add("{frame.returnlabel.as(not null)}:(void)0;")
2348 v.write_attribute(self.mpropdef.mproperty, recv, value)
2351 v.current_node = oldnode
2356 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2358 var nexpr = self.n_expr
2359 if nexpr != null then return
2361 var oldnode = v.current_node
2362 v.current_node = self
2363 var old_frame = v.frame
2364 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2366 # Force read to check the initialization
2367 v.read_attribute(self.mpropdef.mproperty, recv)
2369 v.current_node = oldnode
2373 redef class AClassdef
2374 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2376 if mpropdef == self.mfree_init then
2377 assert mpropdef.mproperty.is_root_init
2378 assert arguments.length == 1
2379 if not mpropdef.is_intro then
2380 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
2390 # Try to compile self as an expression
2391 # Do not call this method directly, use `v
.expr
` instead
2392 private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
2394 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
2395 var mtype = self.mtype
2396 if mtype == null then
2399 var res = v.new_var(mtype)
2400 v.add("/* {res} = NOT YET {class_name} */")
2405 # Try to compile self as a statement
2406 # Do not call this method directly, use `v
.stmt
` instead
2407 private fun stmt(v: AbstractCompilerVisitor)
2413 redef class ABlockExpr
2416 for e in self.n_expr do v.stmt(e)
2420 var last = self.n_expr.last
2421 for e in self.n_expr do
2422 if e == last then break
2425 return v.expr(last, null)
2429 redef class AVardeclExpr
2432 var variable = self.variable.as(not null)
2433 var ne = self.n_expr
2435 var i = v.expr(ne, variable.declared_type)
2436 v.assign(v.variable(variable), i)
2441 redef class AVarExpr
2444 var res = v.variable(self.variable.as(not null))
2445 var mtype = self.mtype.as(not null)
2446 return v.autoadapt(res, mtype)
2450 redef class AVarAssignExpr
2453 var variable = self.variable.as(not null)
2454 var i = v.expr(self.n_value, variable.declared_type)
2455 v.assign(v.variable(variable), i)
2460 redef class AVarReassignExpr
2463 var variable = self.variable.as(not null)
2464 var vari = v.variable(variable)
2465 var value = v.expr(self.n_value, variable.declared_type)
2466 var res = v.compile_callsite(self.reassign_callsite.as(not null), [vari, value])
2468 v.assign(v.variable(variable), res)
2472 redef class ASelfExpr
2473 redef fun expr(v) do return v.frame.arguments.first
2476 redef class AEscapeExpr
2477 redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
2480 redef class AReturnExpr
2483 var nexpr = self.n_expr
2484 if nexpr != null then
2485 var returnvar = v.frame.returnvar.as(not null)
2486 var i = v.expr(nexpr, returnvar.mtype)
2487 v.assign(returnvar, i)
2489 v.add("goto {v.frame.returnlabel.as(not null)};")
2493 redef class AAbortExpr
2494 redef fun stmt(v) do v.add_abort("Aborted")
2500 var cond = v.expr_bool(self.n_expr)
2501 v.add("if ({cond})\{")
2510 var res = v.new_var(self.mtype.as(not null))
2511 var cond = v.expr_bool(self.n_expr)
2512 v.add("if ({cond})\{")
2513 v.assign(res, v.expr(self.n_then.as(not null), null))
2515 v.assign(res, v.expr(self.n_else.as(not null), null))
2521 redef class AIfexprExpr
2524 var res = v.new_var(self.mtype.as(not null))
2525 var cond = v.expr_bool(self.n_expr)
2526 v.add("if ({cond})\{")
2527 v.assign(res, v.expr(self.n_then, null))
2529 v.assign(res, v.expr(self.n_else, null))
2538 v.stmt(self.n_block)
2539 v.add_escape_label(break_mark)
2543 redef class AWhileExpr
2547 var cond = v.expr_bool(self.n_expr)
2548 v.add("if (!{cond}) break;")
2549 v.stmt(self.n_block)
2550 v.add_escape_label(continue_mark)
2552 v.add_escape_label(break_mark)
2556 redef class ALoopExpr
2560 v.stmt(self.n_block)
2561 v.add_escape_label(continue_mark)
2563 v.add_escape_label(break_mark)
2567 redef class AForExpr
2570 var cl = v.expr(self.n_expr, null)
2571 var it_meth = self.method_iterator
2572 assert it_meth != null
2573 var it = v.compile_callsite(it_meth, [cl])
2576 var isok_meth = self.method_is_ok
2577 assert isok_meth != null
2578 var ok = v.compile_callsite(isok_meth, [it])
2580 v.add("if(!{ok}) break;")
2581 if self.variables.length == 1 then
2582 var item_meth = self.method_item
2583 assert item_meth != null
2584 var i = v.compile_callsite(item_meth, [it])
2586 v.assign(v.variable(variables.first), i)
2587 else if self.variables.length == 2 then
2588 var key_meth = self.method_key
2589 assert key_meth != null
2590 var i = v.compile_callsite(key_meth, [it])
2592 v.assign(v.variable(variables[0]), i)
2593 var item_meth = self.method_item
2594 assert item_meth != null
2595 i = v.compile_callsite(item_meth, [it])
2597 v.assign(v.variable(variables[1]), i)
2601 v.stmt(self.n_block)
2602 v.add_escape_label(continue_mark)
2603 var next_meth = self.method_next
2604 assert next_meth != null
2605 v.compile_callsite(next_meth, [it])
2607 v.add_escape_label(break_mark)
2609 var method_finish = self.method_finish
2610 if method_finish != null then
2611 # TODO: Find a way to call this also in long escape (e.g. return)
2612 v.compile_callsite(method_finish, [it])
2617 redef class AAssertExpr
2620 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return
2622 var cond = v.expr_bool(self.n_expr)
2623 v.add("if (unlikely(!{cond})) \{")
2627 v.add_abort("Assert '{nid.text}' failed")
2629 v.add_abort("Assert failed")
2638 var res = v.new_var(self.mtype.as(not null))
2639 var i1 = v.expr_bool(self.n_expr)
2640 v.add("if ({i1}) \{")
2643 var i2 = v.expr_bool(self.n_expr2)
2644 v.add("{res} = {i2};")
2650 redef class AImpliesExpr
2653 var res = v.new_var(self.mtype.as(not null))
2654 var i1 = v.expr_bool(self.n_expr)
2655 v.add("if (!{i1}) \{")
2658 var i2 = v.expr_bool(self.n_expr2)
2659 v.add("{res} = {i2};")
2665 redef class AAndExpr
2668 var res = v.new_var(self.mtype.as(not null))
2669 var i1 = v.expr_bool(self.n_expr)
2670 v.add("if (!{i1}) \{")
2673 var i2 = v.expr_bool(self.n_expr2)
2674 v.add("{res} = {i2};")
2680 redef class ANotExpr
2683 var cond = v.expr_bool(self.n_expr)
2684 return v.new_expr("!{cond}", self.mtype.as(not null))
2688 redef class AOrElseExpr
2691 var res = v.new_var(self.mtype.as(not null))
2692 var i1 = v.expr(self.n_expr, null)
2693 v.add("if ({i1}!=NULL) \{")
2696 var i2 = v.expr(self.n_expr2, null)
2703 redef class AIntExpr
2704 redef fun expr(v) do return v.new_expr("{self.value.to_s}", self.mtype.as(not null))
2707 redef class AFloatExpr
2708 redef fun expr(v) do return v.new_expr("{self.n_float.text}", self.mtype.as(not null)) # FIXME use value, not n_float
2711 redef class ACharExpr
2712 redef fun expr(v) do return v.new_expr("'{self.value.to_s.escape_to_c}'", self.mtype.as(not null))
2715 redef class AArrayExpr
2718 var mtype = self.mtype.as(MClassType).arguments.first
2719 var array = new Array[RuntimeVariable]
2720 for nexpr in self.n_exprs.n_exprs do
2721 var i = v.expr(nexpr, mtype)
2724 return v.array_instance(array, mtype)
2728 redef class AStringFormExpr
2729 redef fun expr(v) do return v.string_instance(self.value.as(not null))
2732 redef class ASuperstringExpr
2735 var array = new Array[RuntimeVariable]
2736 for ne in self.n_exprs do
2737 if ne isa AStringFormExpr and ne.value == "" then continue # skip empty sub-strings
2738 var i = v.expr(ne, null)
2741 var a = v.array_instance(array, v.object_type)
2742 var res = v.send(v.get_property("to_s", a.mtype), [a])
2747 redef class ACrangeExpr
2750 var i1 = v.expr(self.n_expr, null)
2751 var i2 = v.expr(self.n_expr2, null)
2752 var mtype = self.mtype.as(MClassType)
2753 var res = v.init_instance(mtype)
2754 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2759 redef class AOrangeExpr
2762 var i1 = v.expr(self.n_expr, null)
2763 var i2 = v.expr(self.n_expr2, null)
2764 var mtype = self.mtype.as(MClassType)
2765 var res = v.init_instance(mtype)
2766 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2771 redef class ATrueExpr
2772 redef fun expr(v) do return v.new_expr("1", self.mtype.as(not null))
2775 redef class AFalseExpr
2776 redef fun expr(v) do return v.new_expr("0", self.mtype.as(not null))
2779 redef class ANullExpr
2780 redef fun expr(v) do return v.new_expr("NULL", self.mtype.as(not null))
2783 redef class AIsaExpr
2786 var i = v.expr(self.n_expr, null)
2787 return v.type_test(i, self.cast_type.as(not null), "isa")
2791 redef class AAsCastExpr
2794 var i = v.expr(self.n_expr, null)
2795 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2797 v.add_cast(i, self.mtype.as(not null), "as")
2802 redef class AAsNotnullExpr
2805 var i = v.expr(self.n_expr, null)
2806 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2808 if i.mtype.ctype != "val*" then return i
2810 v.add("if (unlikely({i} == NULL)) \{")
2811 v.add_abort("Cast failed")
2817 redef class AParExpr
2818 redef fun expr(v) do return v.expr(self.n_expr, null)
2821 redef class AOnceExpr
2824 var mtype = self.mtype.as(not null)
2825 var name = v.get_name("varonce")
2826 var guard = v.get_name(name + "_guard")
2827 v.add_decl("static {mtype.ctype} {name};")
2828 v.add_decl("static int {guard};")
2829 var res = v.new_var(mtype)
2830 v.add("if ({guard}) \{")
2831 v.add("{res} = {name};")
2833 var i = v.expr(self.n_expr, mtype)
2834 v.add("{res} = {i};")
2835 v.add("{name} = {res};")
2836 v.add("{guard} = 1;")
2842 redef class ASendExpr
2845 var recv = v.expr(self.n_expr, null)
2846 var callsite = self.callsite.as(not null)
2847 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2848 return v.compile_callsite(callsite, args)
2852 redef class ASendReassignFormExpr
2855 var recv = v.expr(self.n_expr, null)
2856 var callsite = self.callsite.as(not null)
2857 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2859 var value = v.expr(self.n_value, null)
2861 var left = v.compile_callsite(callsite, args)
2864 var res = v.compile_callsite(self.reassign_callsite.as(not null), [left, value])
2868 v.compile_callsite(self.write_callsite.as(not null), args)
2872 redef class ASuperExpr
2875 var recv = v.frame.arguments.first
2877 var callsite = self.callsite
2878 if callsite != null then
2879 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2881 # Add additional arguments for the super init call
2882 if args.length == 1 then
2883 for i in [0..callsite.msignature.arity[ do
2884 args.add(v.frame.arguments[i+1])
2888 var res = v.compile_callsite(callsite, args)
2892 var mpropdef = self.mpropdef.as(not null)
2893 var args = v.varargize(mpropdef, recv, self.n_args.n_exprs)
2894 if args.length == 1 then
2895 args = v.frame.arguments
2898 # stantard call-next-method
2899 return v.supercall(mpropdef, recv.mtype.as(MClassType), args)
2903 redef class ANewExpr
2906 var mtype = self.recvtype
2907 assert mtype != null
2909 var ctype = mtype.ctype
2910 if mtype.mclass.name == "NativeArray" then
2911 assert self.n_args.n_exprs.length == 1
2912 var l = v.expr(self.n_args.n_exprs.first, null)
2913 assert mtype isa MGenericType
2914 var elttype = mtype.arguments.first
2915 return v.native_array_instance(elttype, l)
2916 else if ctype == "val*" then
2917 recv = v.init_instance(mtype)
2918 else if ctype == "char*" then
2919 recv = v.new_expr("NULL/*special!*/", mtype)
2921 recv = v.new_expr("({ctype})0/*special!*/", mtype)
2924 var callsite = self.callsite.as(not null)
2925 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2926 var res2 = v.compile_callsite(callsite, args)
2927 if res2 != null then
2928 #self.debug("got {res2} from {mproperty}. drop {recv}")
2935 redef class AAttrExpr
2938 var recv = v.expr(self.n_expr, null)
2939 var mproperty = self.mproperty.as(not null)
2940 return v.read_attribute(mproperty, recv)
2944 redef class AAttrAssignExpr
2947 var recv = v.expr(self.n_expr, null)
2948 var i = v.expr(self.n_value, null)
2949 var mproperty = self.mproperty.as(not null)
2950 v.write_attribute(mproperty, recv, i)
2955 redef class AAttrReassignExpr
2958 var recv = v.expr(self.n_expr, null)
2959 var value = v.expr(self.n_value, null)
2960 var mproperty = self.mproperty.as(not null)
2961 var attr = v.read_attribute(mproperty, recv)
2962 var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
2964 v.write_attribute(mproperty, recv, res)
2968 redef class AIssetAttrExpr
2971 var recv = v.expr(self.n_expr, null)
2972 var mproperty = self.mproperty.as(not null)
2973 return v.isset_attribute(mproperty, recv)
2977 redef class ADebugTypeExpr
2986 redef class Array[E]
2987 # Return a new `Array` with the elements only contened in self and not in `o
`
2988 fun -(o: Array[E]): Array[E] do
2989 var res = new Array[E]
2990 for e in self do if not o.has(e) then res.add(e)
2996 # All `MProperty` associated to all `MClassDef` of `mclass
`
2997 fun properties(mclass: MClass): Set[MProperty] do
2998 if not self.properties_cache.has_key(mclass) then
2999 var properties = new HashSet[MProperty]
3000 var parents = new Array[MClass]
3001 if self.flatten_mclass_hierarchy.has(mclass) then
3002 parents.add_all(mclass.in_hierarchy(self).direct_greaters)
3004 for parent in parents do
3005 properties.add_all(self.properties(parent))
3007 for mclassdef in mclass.mclassdefs do
3008 if not self.in_importation <= mclassdef.mmodule then continue
3009 for mprop in mclassdef.intro_mproperties do
3010 properties.add(mprop)
3013 self.properties_cache[mclass] = properties
3015 return properties_cache[mclass]
3017 private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
3019 # Write FFI and nitni results to file
3020 fun finalize_ffi(c: AbstractCompiler) do end
3022 # Give requided addinional system libraries (as given to LD_LIBS)
3023 # Note: can return null instead of an empty set
3024 fun collect_linker_libs: nullable Set[String] do return null
3027 # Create a tool context to handle options and paths
3028 var toolcontext = new ToolContext
3030 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
3032 # We do not add other options, so process them now!
3033 toolcontext.process_options(args)
3035 # We need a model to collect stufs
3036 var model = new Model
3037 # An a model builder to parse files
3038 var modelbuilder = new ModelBuilder(model, toolcontext)
3040 var arguments = toolcontext.option_context.rest
3041 if arguments.length > 1 and toolcontext.opt_output.value != null then
3042 print "Error: --output needs a single source file. Do you prefer --dir?"
3046 # Here we load an process all modules passed on the command line
3047 var mmodules = modelbuilder.parse(arguments)
3049 if mmodules.is_empty then return
3050 modelbuilder.run_phases
3052 for mmodule in mmodules do
3053 toolcontext.info("*** PROCESS {mmodule} ***", 1)
3055 toolcontext.run_global_phases(ms)