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
)
83 opt_no_main
.hidden
= true
86 redef fun process_options
(args
)
90 var st
= opt_stacktrace
.value
91 if st
== "none" or st
== "libunwind" or st
== "nitstack" then
93 else if st
== "auto" or st
== null then
95 opt_stacktrace
.value
= "nitstack"
97 print
"Error: unknown value `{st}` for --stacktrace. Use `none`, `libunwind`, `nitstack` or `auto`."
101 if opt_output
.value
!= null and opt_dir
.value
!= null then
102 print
"Error: cannot use both --dir and --output"
106 if opt_no_check_all
.value
then
107 opt_no_check_covariance
.value
= true
108 opt_no_check_attr_isset
.value
= true
109 opt_no_check_assert
.value
= true
110 opt_no_check_autocast
.value
= true
111 opt_no_check_null
.value
= true
116 redef class ModelBuilder
117 # The compilation directory
118 var compile_dir
: String
120 # Simple indirection to `Toolchain::write_and_make`
121 protected fun write_and_make
(compiler
: AbstractCompiler)
123 var platform
= compiler
.mainmodule
.target_platform
125 if platform
== null then
126 toolchain
= new MakefileToolchain(toolcontext
)
128 toolchain
= platform
.toolchain
(toolcontext
)
130 compile_dir
= toolchain
.compile_dir
131 toolchain
.write_and_make compiler
136 fun toolchain
(toolcontext
: ToolContext): Toolchain is abstract
140 var toolcontext
: ToolContext
142 fun compile_dir
: String
144 var compile_dir
= toolcontext
.opt_compile_dir
.value
145 if compile_dir
== null then compile_dir
= ".nit_compile"
149 fun write_and_make
(compiler
: AbstractCompiler) is abstract
152 class MakefileToolchain
155 redef fun write_and_make
(compiler
)
157 var compile_dir
= compile_dir
159 # Generate the .h and .c files
160 # A single C file regroups many compiled rumtime functions
161 # 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
163 self.toolcontext
.info
("*** WRITING C ***", 1)
167 var cfiles
= new Array[String]
168 write_files
(compiler
, compile_dir
, cfiles
)
170 # Generate the Makefile
172 write_makefile
(compiler
, compile_dir
, cfiles
)
175 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
177 # Execute the Makefile
179 if self.toolcontext
.opt_no_cc
.value
then return
182 self.toolcontext
.info
("*** COMPILING C ***", 1)
184 compile_c_code
(compiler
, compile_dir
)
187 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
190 fun write_files
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
192 var platform
= compiler
.mainmodule
.target_platform
193 if self.toolcontext
.opt_stacktrace
.value
== "nitstack" and (platform
== null or platform
.supports_libunwind
) then compiler
.build_c_to_nit_bindings
194 var cc_opt_with_libgc
= "-DWITH_LIBGC `pkg-config --cflags bdw-gc`"
195 if platform
!= null and not platform
.supports_libgc
then cc_opt_with_libgc
= ""
197 # Add gc_choser.h to aditionnal bodies
198 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
199 compiler
.extern_bodies
.add
(gc_chooser
)
200 var clib
= toolcontext
.nit_dir
/ "clib"
201 compiler
.files_to_copy
.add
"{clib}/gc_chooser.c"
202 compiler
.files_to_copy
.add
"{clib}/gc_chooser.h"
205 for m
in compiler
.mainmodule
.in_importation
.greaters
do
206 compiler
.finalize_ffi_for_module
(m
)
209 # Copy original .[ch] files to compile_dir
210 for src
in compiler
.files_to_copy
do
211 var basename
= src
.basename
("")
212 var dst
= "{compile_dir}/{basename}"
216 var hfilename
= compiler
.header
.file
.name
+ ".h"
217 var hfilepath
= "{compile_dir}/{hfilename}"
218 var h
= new OFStream.open
(hfilepath
)
219 for l
in compiler
.header
.decl_lines
do
223 for l
in compiler
.header
.lines
do
229 var max_c_lines
= toolcontext
.opt_max_c_lines
.value
230 for f
in compiler
.files
do
233 var file
: nullable OFStream = null
234 for vis
in f
.writers
do
235 if vis
== compiler
.header
then continue
236 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
237 if total_lines
== 0 then continue
239 if file
== null or (count
> max_c_lines
and max_c_lines
> 0) then
241 if file
!= null then file
.close
242 var cfilename
= "{f.name}.{i}.c"
243 var cfilepath
= "{compile_dir}/{cfilename}"
244 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
245 cfiles
.add
(cfilename
)
246 file
= new OFStream.open
(cfilepath
)
247 file
.write
"#include \"{f.name}.0.h\
"\n"
250 for l
in vis
.decl_lines
do
254 for l
in vis
.lines
do
259 if file
== null then continue
262 var cfilename
= "{f.name}.0.h"
263 var cfilepath
= "{compile_dir}/{cfilename}"
264 var hfile
: nullable OFStream = null
265 hfile
= new OFStream.open
(cfilepath
)
266 hfile
.write
"#include \"{hfilename}\
"\n"
267 for key
in f
.required_declarations
do
268 if not compiler
.provided_declarations
.has_key
(key
) then
269 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
271 node
.debug
"No provided declaration for {key}"
273 print
"No provided declaration for {key}"
277 hfile
.write compiler
.provided_declarations
[key
]
283 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
286 fun makefile_name
(mainmodule
: MModule): String do return "{mainmodule.name}.mk"
288 fun default_outname
(mainmodule
: MModule): String
290 # Search a non fictive module
291 var res
= mainmodule
.name
292 while mainmodule
.is_fictive
do
293 mainmodule
= mainmodule
.in_importation
.direct_greaters
.first
294 res
= mainmodule
.name
299 # Combine options and platform informations to get the final path of the outfile
300 fun outfile
(mainmodule
: MModule): String
302 var res
= self.toolcontext
.opt_output
.value
303 if res
!= null then return res
304 res
= default_outname
(mainmodule
)
305 var dir
= self.toolcontext
.opt_dir
.value
306 if dir
!= null then return dir
.join_path
(res
)
310 fun write_makefile
(compiler
: AbstractCompiler, compile_dir
: String, cfiles
: Array[String])
312 var mainmodule
= compiler
.mainmodule
313 var platform
= compiler
.mainmodule
.target_platform
315 var outname
= outfile
(mainmodule
)
317 var outpath
= compile_dir
.relpath
(outname
)
318 var makename
= makefile_name
(mainmodule
)
319 var makepath
= "{compile_dir}/{makename}"
320 var makefile
= new OFStream.open
(makepath
)
322 var linker_options
= new HashSet[String]
323 for m
in mainmodule
.in_importation
.greaters
do
324 var libs
= m
.collect_linker_libs
325 if libs
!= null then linker_options
.add_all
(libs
)
328 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")
330 var ost
= toolcontext
.opt_stacktrace
.value
331 if (ost
== "libunwind" or ost
== "nitstack") and (platform
== null or platform
.supports_libunwind
) then makefile
.write
("NEED_LIBUNWIND := YesPlease\n")
333 # Dynamic adaptations
334 # While `platform` enable complex toolchains, they are statically applied
335 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
337 # Check and adapt the targeted system
338 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
339 makefile
.write
("ifeq ($(uname_S),Darwin)\n")
340 # remove -lunwind since it is already included on macosx
341 makefile
.write
("\tNEED_LIBUNWIND :=\n")
342 makefile
.write
("endif\n\n")
344 # Check and adapt for the compiler used
345 # clang need an additionnal `-Qunused-arguments`
346 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")
348 makefile
.write
("ifdef NEED_LIBUNWIND\n\tLDLIBS += -lunwind\nendif\n")
350 makefile
.write
("all: {outpath}\n\n")
352 var ofiles
= new Array[String]
353 var dep_rules
= new Array[String]
354 # Compile each generated file
356 var o
= f
.strip_extension
(".c") + ".o"
357 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
362 var java_files
= new Array[ExternFile]
364 # Compile each required extern body into a specific .o
365 for f
in compiler
.extern_bodies
do
366 var o
= f
.makefile_rule_name
367 var ff
= f
.filename
.basename
("")
368 makefile
.write
("{o}: {ff}\n")
369 makefile
.write
("\t{f.makefile_rule_content}\n\n")
370 dep_rules
.add
(f
.makefile_rule_name
)
372 if f
.compiles_to_o_file
then ofiles
.add
(o
)
373 if f
.add_to_jar
then java_files
.add
(f
)
376 if not java_files
.is_empty
then
377 var jar_file
= "{outpath}.jar"
379 var class_files_array
= new Array[String]
380 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
381 var class_files
= class_files_array
.join
(" ")
383 makefile
.write
("{jar_file}: {class_files}\n")
384 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
385 dep_rules
.add jar_file
389 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath} {ofiles.join(" ")} $(LDLIBS)\n\n")
391 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
393 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
395 makepath
.file_copy_to
"{compile_dir}/Makefile"
398 fun compile_c_code
(compiler
: AbstractCompiler, compile_dir
: String)
400 var makename
= makefile_name
(compiler
.mainmodule
)
402 var makeflags
= self.toolcontext
.opt_make_flags
.value
403 if makeflags
== null then makeflags
= ""
404 self.toolcontext
.info
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags}", 2)
407 if self.toolcontext
.verbose_level
>= 3 then
408 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1")
410 res
= sys
.system
("make -B -C {compile_dir} -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
413 toolcontext
.error
(null, "make failed! Error code: {res}.")
418 # Singleton that store the knowledge about the compilation process
419 abstract class AbstractCompiler
420 type VISITOR: AbstractCompilerVisitor
422 # Table corresponding c_names to nit names (methods)
423 var names
= new HashMap[String, String]
425 # The main module of the program currently compiled
426 # Is assigned during the separate compilation
427 var mainmodule
: MModule is writable
429 # The real main module of the program
430 var realmainmodule
: MModule
432 # The modelbuilder used to know the model and the AST
433 var modelbuilder
: ModelBuilder is protected writable
435 # Is hardening asked? (see --hardening)
436 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
438 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
440 self.mainmodule
= mainmodule
441 self.realmainmodule
= mainmodule
442 self.modelbuilder
= modelbuilder
445 # Force the creation of a new file
446 # The point is to avoid contamination between must-be-compiled-separately files
447 fun new_file
(name
: String): CodeFile
449 if modelbuilder
.toolcontext
.opt_group_c_files
.value
then
450 if self.files
.is_empty
then
451 var f
= new CodeFile(mainmodule
.name
)
454 return self.files
.first
456 var f
= new CodeFile(name
)
461 # The list of all associated files
462 # Used to generate .c files
463 var files
= new List[CodeFile]
465 # Initialize a visitor specific for a compiler engine
466 fun new_visitor
: VISITOR is abstract
468 # Where global declaration are stored (the main .h)
469 var header
: CodeWriter is writable
471 # Provide a declaration that can be requested (before or latter) by a visitor
472 fun provide_declaration
(key
: String, s
: String)
474 if self.provided_declarations
.has_key
(key
) then
475 assert self.provided_declarations
[key
] == s
477 self.provided_declarations
[key
] = s
480 private var provided_declarations
= new HashMap[String, String]
482 private var requirers_of_declarations
= new HashMap[String, ANode]
484 # Builds the .c and .h files to be used when generating a Stack Trace
485 # Binds the generated C function names to Nit function names
486 fun build_c_to_nit_bindings
488 var compile_dir
= modelbuilder
.compile_dir
490 var stream
= new OFStream.open
("{compile_dir}/c_functions_hash.c")
491 stream
.write
("#include <string.h>\n")
492 stream
.write
("#include <stdlib.h>\n")
493 stream
.write
("#include \"c_functions_hash
.h\
"\n")
494 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
495 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
496 stream
.write
("char* procname = malloc(len+1);")
497 stream
.write
("memcpy(procname, procproc, len);")
498 stream
.write
("procname[len] = '\\0';")
499 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
500 for i
in names
.keys
do
503 stream.write("\
",\"")
504 stream.write(names[i])
505 stream.write("\
"\},\n")
507 stream
.write
("\};\n")
508 stream
.write
("int i;")
509 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
510 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
511 stream
.write
("free(procname);")
512 stream
.write
("return map[i].nit_name;")
515 stream
.write
("free(procname);")
516 stream
.write
("return NULL;")
520 stream
= new OFStream.open
("{compile_dir}/c_functions_hash.h")
521 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
524 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
528 # This method call compile_header_strucs method that has to be refined
529 fun compile_header
do
530 self.header
.add_decl
("#include <stdlib.h>")
531 self.header
.add_decl
("#include <stdio.h>")
532 self.header
.add_decl
("#include <string.h>")
533 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
534 self.header
.add_decl
("#ifdef ANDROID")
535 self.header
.add_decl
(" #include <android/log.h>")
536 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
537 self.header
.add_decl
("#else")
538 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
539 self.header
.add_decl
("#endif")
541 compile_header_structs
542 compile_nitni_structs
544 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
545 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
546 # Signal handler function prototype
547 self.header
.add_decl
("void show_backtrace(int);")
549 self.header
.add_decl
("void show_backtrace(int) __attribute__ ((noreturn));")
552 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
553 self.header
.add_decl
("#define likely(x) (x)")
554 self.header
.add_decl
("#define unlikely(x) (x)")
555 else if gccd_disable
.has
("correct-likely") then
556 # invert the `likely` definition
557 # Used by masochists to bench the worst case
558 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
559 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
561 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
562 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
565 # Global variable used by intern methods
566 self.header
.add_decl
("extern int glob_argc;")
567 self.header
.add_decl
("extern char **glob_argv;")
568 self.header
.add_decl
("extern val *glob_sys;")
571 # Declaration of structures for live Nit types
572 protected fun compile_header_structs
is abstract
574 # Declaration of structures for nitni undelying the FFI
575 protected fun compile_nitni_structs
577 self.header
.add_decl
"""
578 /* Native reference to Nit objects */
579 /* This structure is used to represent every Nit type in extern methods and custom C code. */
581 struct nitni_ref *next,
582 *prev; /* adjacent global references in global list */
583 int count; /* number of time this global reference has been marked */
586 /* List of global references from C code to Nit objects */
587 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
588 struct nitni_global_ref_list_t {
589 struct nitni_ref *head, *tail;
591 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
593 /* Initializer of global reference list */
594 extern void nitni_global_ref_list_init();
596 /* Intern function to add a global reference to the list */
597 extern void nitni_global_ref_add( struct nitni_ref *ref );
599 /* Intern function to remove a global reference from the list */
600 extern void nitni_global_ref_remove( struct nitni_ref *ref );
602 /* Increase count on an existing global reference */
603 extern void nitni_global_ref_incr( struct nitni_ref *ref );
605 /* Decrease count on an existing global reference */
606 extern void nitni_global_ref_decr( struct nitni_ref *ref );
610 fun compile_finalizer_function
612 var finalizable_type
= mainmodule
.finalizable_type
613 if finalizable_type
== null then return
615 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
617 if finalize_meth
== null then
618 modelbuilder
.toolcontext
.error
(null, "The `Finalizable` class doesn't declare the `finalize` method.")
622 var v
= self.new_visitor
623 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
624 var recv
= v
.new_expr
("obj", finalizable_type
)
625 v
.send
(finalize_meth
, [recv
])
629 # Generate the main C function.
633 # * allocate the Sys object if it exists
634 # * call init if is exists
635 # * call main if it exists
636 fun compile_main_function
638 var v
= self.new_visitor
639 v
.add_decl
("#include <signal.h>")
640 var ost
= modelbuilder
.toolcontext
.opt_stacktrace
.value
641 var platform
= mainmodule
.target_platform
643 if platform
!= null and not platform
.supports_libunwind
then ost
= "none"
645 var no_main
= (platform
!= null and platform
.no_main
) or modelbuilder
.toolcontext
.opt_no_main
.value
647 if ost
== "nitstack" or ost
== "libunwind" then
648 v
.add_decl
("#define UNW_LOCAL_ONLY")
649 v
.add_decl
("#include <libunwind.h>")
650 if ost
== "nitstack" then
651 v
.add_decl
("#include \"c_functions_hash
.h\
"")
654 v
.add_decl
("int glob_argc;")
655 v
.add_decl
("char **glob_argv;")
656 v
.add_decl
("val *glob_sys;")
658 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
659 for tag
in count_type_test_tags
do
660 v
.add_decl
("long count_type_test_resolved_{tag};")
661 v
.add_decl
("long count_type_test_unresolved_{tag};")
662 v
.add_decl
("long count_type_test_skipped_{tag};")
663 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
664 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
665 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
669 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
670 v
.add_decl
("long count_invoke_by_tables;")
671 v
.add_decl
("long count_invoke_by_direct;")
672 v
.add_decl
("long count_invoke_by_inline;")
673 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
674 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
675 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
678 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
679 v
.add_decl
("long count_attr_reads = 0;")
680 v
.add_decl
("long count_isset_checks = 0;")
681 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
682 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
685 v
.add_decl
("void sig_handler(int signo)\{")
686 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
687 v
.add_decl
("show_backtrace(signo);")
690 v
.add_decl
("void show_backtrace (int signo) \{")
691 if ost
== "nitstack" or ost
== "libunwind" then
692 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
693 v
.add_decl
("unw_cursor_t cursor;")
694 v
.add_decl
("if(opt==NULL)\{")
695 v
.add_decl
("unw_context_t uc;")
696 v
.add_decl
("unw_word_t ip;")
697 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
698 v
.add_decl
("unw_getcontext(&uc);")
699 v
.add_decl
("unw_init_local(&cursor, &uc);")
700 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
701 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
702 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
703 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
704 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
705 if ost
== "nitstack" then
706 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
707 v
.add_decl
(" if (recv != NULL)\{")
708 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
709 v.add_decl(" \}else\{")
710 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
713 v
.add_decl
(" PRINT_ERROR(\"` %s \\n\",procname);")
716 v.add_decl("PRINT_ERROR(\"-------------------------------------------------\\n\");")
717 v.add_decl("free(procname);")
720 v.add_decl("exit(signo);")
724 v.add_decl("int nit_main(int argc, char** argv) \{")
726 v.add_decl("int main(int argc, char** argv) \{")
729 v.add("signal(SIGABRT, sig_handler);")
730 v.add("signal(SIGFPE, sig_handler);")
731 v.add("signal(SIGILL, sig_handler);")
732 v.add("signal(SIGINT, sig_handler);")
733 v.add("signal(SIGTERM, sig_handler);")
734 v.add("signal(SIGSEGV, sig_handler);")
735 v.add("signal(SIGPIPE, sig_handler);")
737 v.add("glob_argc = argc; glob_argv = argv;")
738 v.add("initialize_gc_option();")
740 v.add "initialize_nitni_global_refs();"
742 var main_type = mainmodule.sys_type
743 if main_type != null then
744 var mainmodule = v.compiler.mainmodule
745 var glob_sys = v.init_instance(main_type)
746 v.add("glob_sys = {glob_sys};")
747 var main_init = mainmodule.try_get_primitive_method("init", main_type.mclass)
748 if main_init != null then
749 v.send(main_init, [glob_sys])
751 var main_method = mainmodule.try_get_primitive_method("run", main_type.mclass) or else
752 mainmodule.try_get_primitive_method("main", main_type.mclass)
753 if main_method != null then
754 v.send(main_method, [glob_sys])
758 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
759 v.add_decl("long count_type_test_resolved_total = 0;")
760 v.add_decl("long count_type_test_unresolved_total = 0;")
761 v.add_decl("long count_type_test_skipped_total = 0;")
762 v.add_decl("long count_type_test_total_total = 0;")
763 for tag in count_type_test_tags do
764 v.add_decl("long count_type_test_total_{tag};")
765 v.add("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
766 v.add("count_type_test_resolved_total += count_type_test_resolved_{tag};")
767 v.add("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
768 v.add("count_type_test_skipped_total += count_type_test_skipped_{tag};")
769 v.add("count_type_test_total_total += count_type_test_total_{tag};")
771 v.add("printf(\"# dynamic count_type_test: total %l\\n\");")
772 v.add("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\");")
773 var tags = count_type_test_tags.to_a
776 v.add("printf(\"{tag}\");")
777 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
778 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
779 v.add("printf(\"\\t%ld (%.2f%%)\", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
780 v.add("printf(\"\\t%ld (%.2f%%)\\n\", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
784 if self.modelbuilder.toolcontext.opt_invocation_metrics.value then
785 v.add_decl("long count_invoke_total;")
786 v.add("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
787 v.add("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
788 v.add("printf(\"by table: %ld (%.2f%%)\\n\", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
789 v.add("printf(\"direct: %ld (%.2f%%)\\n\", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
790 v.add("printf(\"inlined: %ld (%.2f%%)\\n\", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
793 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
794 v.add("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
795 v.add("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
802 # Copile all C functions related to the [incr|decr]_ref features of the FFI
803 fun compile_nitni_global_ref_functions
805 var v = self.new_visitor
807 struct nitni_global_ref_list_t *nitni_global_ref_list;
808 void initialize_nitni_global_refs() {
809 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
810 nitni_global_ref_list->head = NULL;
811 nitni_global_ref_list->tail = NULL;
814 void nitni_global_ref_add( struct nitni_ref *ref ) {
815 if ( nitni_global_ref_list->head == NULL ) {
816 nitni_global_ref_list->head = ref;
819 nitni_global_ref_list->tail->next = ref;
820 ref->prev = nitni_global_ref_list->tail;
822 nitni_global_ref_list->tail = ref;
827 void nitni_global_ref_remove( struct nitni_ref *ref ) {
828 if ( ref->prev == NULL ) {
829 nitni_global_ref_list->head = ref->next;
831 ref->prev->next = ref->next;
834 if ( ref->next == NULL ) {
835 nitni_global_ref_list->tail = ref->prev;
837 ref->next->prev = ref->prev;
841 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
842 if ( ref->count == 0 ) /* not registered */
845 nitni_global_ref_add( ref );
851 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
852 if ( ref->count == 1 ) /* was last reference */
854 /* remove from list */
855 nitni_global_ref_remove( ref );
863 # List of additional files required to compile (FFI)
864 var extern_bodies = new Array[ExternFile]
866 # List of source files to copy over to the compile dir
867 var files_to_copy = new Array[String]
869 # This is used to avoid adding an extern file more than once
870 private var seen_extern = new ArraySet[String]
872 # Generate code that initialize the attributes on a new instance
873 fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
875 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
876 self.mainmodule.linearize_mclassdefs(cds)
878 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
879 var n = self.modelbuilder.mclassdef2nclassdef[cd]
880 for npropdef in n.n_propdefs do
881 if npropdef isa AAttrPropdef then
882 npropdef.init_expr(v, recv)
888 # Generate code that check if an attribute is correctly initialized
889 fun generate_check_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType)
891 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
892 self.mainmodule.linearize_mclassdefs(cds)
894 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
895 var n = self.modelbuilder.mclassdef2nclassdef[cd]
896 for npropdef in n.n_propdefs do
897 if npropdef isa AAttrPropdef then
898 npropdef.check_expr(v, recv)
906 var count_type_test_tags: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
907 var count_type_test_resolved: HashMap[String, Int] = init_count_type_test_tags
908 var count_type_test_unresolved: HashMap[String, Int] = init_count_type_test_tags
909 var count_type_test_skipped: HashMap[String, Int] = init_count_type_test_tags
911 protected fun init_count_type_test_tags: HashMap[String, Int]
913 var res = new HashMap[String, Int]
914 for tag in count_type_test_tags do
920 # Display stats about compilation process
924 # * type tests against resolved types (`x
isa Collection[Animal]`)
925 # * type tests against unresolved types (`x
isa Collection[E
]`)
926 # * type tests skipped
930 if self.modelbuilder.toolcontext.opt_typing_test_metrics.value then
931 print "# static count_type_test"
932 print "\tresolved:\tunresolved\tskipped\ttotal"
933 var count_type_test_total = init_count_type_test_tags
934 count_type_test_resolved["total"] = 0
935 count_type_test_unresolved["total"] = 0
936 count_type_test_skipped["total"] = 0
937 count_type_test_total["total"] = 0
938 for tag in count_type_test_tags do
939 count_type_test_total[tag] = count_type_test_resolved[tag] + count_type_test_unresolved[tag] + count_type_test_skipped[tag]
940 count_type_test_resolved["total"] += count_type_test_resolved[tag]
941 count_type_test_unresolved["total"] += count_type_test_unresolved[tag]
942 count_type_test_skipped["total"] += count_type_test_skipped[tag]
943 count_type_test_total["total"] += count_type_test_total[tag]
945 var count_type_test = count_type_test_total["total"]
946 var tags = count_type_test_tags.to_a
950 printn "\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
951 printn "\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
952 printn "\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
953 printn "\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
959 fun finalize_ffi_for_module(mmodule: MModule) do mmodule.finalize_ffi(self)
962 # Avoid division by zero by returning the string "n/a"
963 fun div(a,b:Int):String
965 if b == 0 then return "n/a"
966 return ((a*10000/b).to_f / 100.0).to_precision(2)
970 # A file unit (may be more than one file if
971 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
974 var writers = new Array[CodeWriter]
975 var required_declarations = new HashSet[String]
978 # Where to store generated lines
981 var lines: List[String] = new List[String]
982 var decl_lines: List[String] = new List[String]
984 # Add a line in the main part of the generated C
985 fun add(s: String) do self.lines.add(s)
988 # (used for local or global declaration)
989 fun add_decl(s: String) do self.decl_lines.add(s)
994 file.writers.add(self)
998 # A visitor on the AST of property definition that generate the C code.
999 abstract class AbstractCompilerVisitor
1001 type COMPILER: AbstractCompiler
1003 # The associated compiler
1004 var compiler: COMPILER
1006 # The current visited AST node
1007 var current_node: nullable ANode = null is writable
1009 # The current `Frame`
1010 var frame: nullable Frame is writable
1012 # Alias for self.compiler.mainmodule.object_type
1013 fun object_type: MClassType do return self.compiler.mainmodule.object_type
1015 # Alias for self.compiler.mainmodule.bool_type
1016 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
1018 var writer: CodeWriter
1020 init(compiler: COMPILER)
1022 self.compiler = compiler
1023 self.writer = new CodeWriter(compiler.files.last)
1026 # Force to get the primitive class named `name
` or abort
1027 fun get_class(name: String): MClass do return self.compiler.mainmodule.get_primitive_class(name)
1029 # Force to get the primitive property named `name
` in the instance `recv
` or abort
1030 fun get_property(name: String, recv: MType): MMethod
1032 assert recv isa MClassType
1033 return self.compiler.modelbuilder.force_get_primitive_method(self.current_node, name, recv.mclass, self.compiler.mainmodule)
1036 fun compile_callsite(callsite: CallSite, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1038 var initializers = callsite.mpropdef.initializers
1039 if not initializers.is_empty then
1040 var recv = arguments.first
1043 for p in initializers do
1044 if p isa MMethod then
1046 for x in p.intro.msignature.mparameters do
1047 args.add arguments[i]
1051 else if p isa MAttribute then
1052 self.write_attribute(p, recv, arguments[i])
1056 assert i == arguments.length
1058 return self.send(callsite.mproperty, [recv])
1061 return self.send(callsite.mproperty, arguments)
1064 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable is abstract
1066 fun calloc_array(ret_type: MType, arguments: Array[RuntimeVariable]) is abstract
1068 fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]) is abstract
1070 # Evaluate `args
` as expressions in the call of `mpropdef
` on `recv
`.
1071 # This method is used to manage varargs in signatures and returns the real array
1072 # of runtime variables to use in the call.
1073 fun varargize(mpropdef: MMethodDef, recv: RuntimeVariable, args: SequenceRead[AExpr]): Array[RuntimeVariable]
1075 var msignature = mpropdef.new_msignature or else mpropdef.msignature.as(not null)
1076 var res = new Array[RuntimeVariable]
1079 if args.is_empty then return res
1081 var vararg_rank = msignature.vararg_rank
1082 var vararg_len = args.length - msignature.arity
1083 if vararg_len < 0 then vararg_len = 0
1085 for i in [0..msignature.arity[ do
1086 if i == vararg_rank then
1088 if ne isa AVarargExpr then
1089 var e = self.expr(ne.n_expr, null)
1093 var vararg = new Array[RuntimeVariable]
1094 for j in [vararg_rank..vararg_rank+vararg_len] do
1095 var e = self.expr(args[j], null)
1098 var elttype = msignature.mparameters[vararg_rank].mtype
1099 var arg = self.vararg_instance(mpropdef, recv, vararg, elttype)
1103 if i > vararg_rank then j += vararg_len
1104 var e = self.expr(args[j], null)
1113 # Anchor a type to the main module and the current receiver
1114 fun anchor(mtype: MType): MType
1116 if not mtype.need_anchor then return mtype
1117 return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
1120 fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
1122 if not mtype.need_anchor then return mtype
1123 return mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
1126 # Unsafely cast a value to a new type
1127 # ie the result share the same C variable but my have a different mcasttype
1128 # NOTE: if the adaptation is useless then `value
` is returned as it.
1129 # ENSURE: `result
.name
== value
.name
`
1130 fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
1132 mtype = self.anchor(mtype)
1133 var valmtype = value.mcasttype
1134 if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1138 if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
1139 var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
1142 var res = new RuntimeVariable(value.name, valmtype, mtype)
1147 # Generate a super call from a method definition
1148 fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1150 # Adapt the arguments of a method according to targetted `MMethodDef`
1151 fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1153 # Unbox all the arguments of a method when implemented `extern` or `intern`
1154 fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
1156 # Box or unbox a value to another type iff a C type conversion is needed
1157 # ENSURE: `result
.mtype
.ctype
== mtype
.ctype
`
1158 fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1160 # Box extern classes to be used in the generated code
1161 fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1163 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1164 fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
1166 # Generate a polymorphic subtype test
1167 fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
1169 # Generate the code required to dynamically check if 2 objects share the same runtime type
1170 fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1172 # Generate a Nit "is" for two runtime_variables
1173 fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable is abstract
1177 # Generate a static call on a method definition
1178 fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1180 # Generate a polymorphic send for the method `m
` and the arguments `args
`
1181 fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1183 # Generate a monomorphic send for the method `m
`, the type `t
` and the arguments `args
`
1184 fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1186 assert t isa MClassType
1187 var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
1188 return self.call(propdef, t, args)
1191 # Generate a monomorphic super send from the method `m
`, the type `t
` and the arguments `args
`
1192 fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
1194 assert t isa MClassType
1195 m = m.lookup_next_definition(self.compiler.mainmodule, t)
1196 return self.call(m, t, args)
1199 # Attributes handling
1201 # Generate a polymorphic attribute is_set test
1202 fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1204 # Generate a polymorphic attribute read
1205 fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable is abstract
1207 # Generate a polymorphic attribute write
1208 fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable) is abstract
1212 # Add a check and an abort for a null receiver if needed
1213 fun check_recv_notnull(recv: RuntimeVariable)
1215 if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return
1217 var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType
1219 self.add("if (unlikely({recv} == NULL)) \{")
1220 self.add_abort("Receiver is null")
1227 private var names = new HashSet[String]
1228 private var last: Int = 0
1230 # Return a new name based on `s
` and unique in the visitor
1231 fun get_name(s: String): String
1233 if not self.names.has(s) then
1237 var i = self.last + 1
1240 if not self.names.has(s2) then
1249 # Return an unique and stable identifier associated with an escapemark
1250 fun escapemark_name(e: nullable EscapeMark): String
1253 if escapemark_names.has_key(e) then return escapemark_names[e]
1255 if name == null then name = "label"
1256 name = get_name(name)
1257 escapemark_names[e] = name
1261 # Insert a C label for associated with an escapemark
1262 fun add_escape_label(e: nullable EscapeMark)
1264 if e == null then return
1265 if e.escapes.is_empty then return
1266 add("BREAK_{escapemark_name(e)}: (void)0;")
1269 private var escapemark_names = new HashMap[EscapeMark, String]
1271 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1272 # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
1273 fun class_name_string(value: RuntimeVariable): String is abstract
1275 # Variables handling
1277 protected var variables = new HashMap[Variable, RuntimeVariable]
1279 # Return the local runtime_variable associated to a Nit local variable
1280 fun variable(variable: Variable): RuntimeVariable
1282 if self.variables.has_key(variable) then
1283 return self.variables[variable]
1285 var name = self.get_name("var_{variable.name}")
1286 var mtype = variable.declared_type.as(not null)
1287 mtype = self.anchor(mtype)
1288 var res = new RuntimeVariable(name, mtype, mtype)
1289 self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1290 self.variables[variable] = res
1295 # Return a new uninitialized local runtime_variable
1296 fun new_var(mtype: MType): RuntimeVariable
1298 mtype = self.anchor(mtype)
1299 var name = self.get_name("var")
1300 var res = new RuntimeVariable(name, mtype, mtype)
1301 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1305 # The difference with `new_var
` is the C static type of the local variable
1306 fun new_var_extern(mtype: MType): RuntimeVariable
1308 mtype = self.anchor(mtype)
1309 var name = self.get_name("var")
1310 var res = new RuntimeVariable(name, mtype, mtype)
1311 self.add_decl("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1315 # Return a new uninitialized named runtime_variable
1316 fun new_named_var(mtype: MType, name: String): RuntimeVariable
1318 mtype = self.anchor(mtype)
1319 var res = new RuntimeVariable(name, mtype, mtype)
1320 self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
1324 # Correctly assign a left and a right value
1325 # Boxing and unboxing is performed if required
1326 fun assign(left, right: RuntimeVariable)
1328 right = self.autobox(right, left.mtype)
1329 self.add("{left} = {right};")
1332 # Generate instances
1334 # Generate a alloc-instance + init-attributes
1335 fun init_instance(mtype: MClassType): RuntimeVariable is abstract
1337 # Set a GC finalizer on `recv
`, only if `recv
` isa Finalizable
1338 fun set_finalizer(recv: RuntimeVariable)
1340 var mtype = recv.mtype
1341 var finalizable_type = compiler.mainmodule.finalizable_type
1342 if finalizable_type != null and not mtype.need_anchor and
1343 mtype.is_subtype(compiler.mainmodule, null, finalizable_type) then
1344 add "gc_register_finalizer({recv});"
1348 # Generate an integer value
1349 fun int_instance(value: Int): RuntimeVariable
1351 var res = self.new_var(self.get_class("Int").mclass_type)
1352 self.add("{res} = {value};")
1356 # Generate an integer value
1357 fun bool_instance(value: Bool): RuntimeVariable
1359 var res = self.new_var(self.get_class("Bool").mclass_type)
1361 self.add("{res} = 1;")
1363 self.add("{res} = 0;")
1368 # Generate a string value
1369 fun string_instance(string: String): RuntimeVariable
1371 var mtype = self.get_class("String").mclass_type
1372 var name = self.get_name("varonce")
1373 self.add_decl("static {mtype.ctype} {name};")
1374 var res = self.new_var(mtype)
1375 self.add("if ({name}) \{")
1376 self.add("{res} = {name};")
1377 self.add("\} else \{")
1378 var native_mtype = self.get_class("NativeString").mclass_type
1379 var nat = self.new_var(native_mtype)
1380 self.add("{nat} = \"{string.escape_to_c}\";")
1381 var length = self.int_instance(string.length)
1382 self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
1383 self.add("{name} = {res};")
1388 fun value_instance(object: Object): RuntimeVariable
1390 if object isa Int then
1391 return int_instance(object)
1392 else if object isa Bool then
1393 return bool_instance(object)
1394 else if object isa String then
1395 return string_instance(object)
1401 # Generate an array value
1402 fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1404 # Get an instance of a array for a vararg
1405 fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable is abstract
1409 # Add a line in the main part of the generated C
1410 fun add(s: String) do self.writer.lines.add(s)
1413 # (used for local or global declaration)
1414 fun add_decl(s: String) do self.writer.decl_lines.add(s)
1416 # Request the presence of a global declaration
1417 fun require_declaration(key: String)
1419 var reqs = self.writer.file.required_declarations
1420 if reqs.has(key) then return
1422 var node = current_node
1423 if node != null then compiler.requirers_of_declarations[key] = node
1426 # Add a declaration in the local-header
1427 # The declaration is ensured to be present once
1428 fun declare_once(s: String)
1430 self.compiler.provide_declaration(s, s)
1431 self.require_declaration(s)
1434 # look for a needed .h and .c file for a given .nit source-file
1435 # FIXME: bad API, parameter should be a `MModule`, not its source-file
1436 fun add_extern(file: String)
1438 file = file.strip_extension(".nit")
1439 var tryfile = file + ".nit.h"
1440 if tryfile.file_exists then
1441 self.declare_once("#include \"{tryfile.basename("")}\"")
1442 self.compiler.files_to_copy.add(tryfile)
1444 tryfile = file + "_nit.h"
1445 if tryfile.file_exists then
1446 self.declare_once("#include \"{tryfile.basename("")}\"")
1447 self.compiler.files_to_copy.add(tryfile)
1450 if self.compiler.seen_extern.has(file) then return
1451 self.compiler.seen_extern.add(file)
1452 tryfile = file + ".nit.c"
1453 if not tryfile.file_exists then
1454 tryfile = file + "_nit.c"
1455 if not tryfile.file_exists then return
1457 var f = new ExternCFile(tryfile.basename(""), "")
1458 self.compiler.extern_bodies.add(f)
1459 self.compiler.files_to_copy.add(tryfile)
1462 # Return a new local runtime_variable initialized with the C expression `cexpr
`.
1463 fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
1465 var res = new_var(mtype)
1466 self.add("{res} = {cexpr};")
1470 # Generate generic abort
1471 # used by aborts, asserts, casts, etc.
1472 fun add_abort(message: String)
1474 self.add("PRINT_ERROR(\"Runtime error: %s\", \"{message.escape_to_c}\");")
1480 if self.current_node != null and self.current_node.location.file != null then
1481 self.add("PRINT_ERROR(\" (%s:%d)\\n\", \"{self.current_node.location.file.filename.escape_to_c}\", {current_node.location.line_start});")
1483 self.add("PRINT_ERROR(\"\\n\");")
1485 self.add("show_backtrace(1);")
1488 # Add a dynamic cast
1489 fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)
1491 var res = self.type_test(value, mtype, tag)
1492 self.add("if (unlikely(!{res})) \{")
1493 var cn = self.class_name_string(value)
1494 self.add("PRINT_ERROR(\"Runtime error: Cast failed. Expected `%s
`, got `%s
`\", \"{mtype.to_s.escape_to_c}\", {cn});")
1499 # Generate a return with the value `s
`
1500 fun ret(s: RuntimeVariable)
1502 self.assign(self.frame.returnvar.as(not null), s)
1503 self.add("goto {self.frame.returnlabel.as(not null)};")
1506 # Compile a statement (if any)
1507 fun stmt(nexpr: nullable AExpr)
1509 if nexpr == null then return
1510 var old = self.current_node
1511 self.current_node = nexpr
1513 self.current_node = old
1516 # Compile an expression an return its result
1517 # `mtype
` is the expected return type, pass null if no specific type is expected.
1518 fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
1520 var old = self.current_node
1521 self.current_node = nexpr
1522 var res = nexpr.expr(self).as(not null)
1523 if mtype != null then
1524 mtype = self.anchor(mtype)
1525 res = self.autobox(res, mtype)
1527 res = autoadapt(res, nexpr.mtype.as(not null))
1528 var implicit_cast_to = nexpr.implicit_cast_to
1529 if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
1530 add_cast(res, implicit_cast_to, "auto")
1531 res = autoadapt(res, implicit_cast_to)
1533 self.current_node = old
1537 # Alias for `self.expr
(nexpr
, self.bool_type
)`
1538 fun expr_bool(nexpr: AExpr): RuntimeVariable do return expr(nexpr, bool_type)
1540 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1541 fun debug(message: String)
1543 var node = self.current_node
1544 if node == null then
1545 print "?: {message}"
1549 self.add("/* DEBUG: {message} */")
1553 # A C function associated to a Nit method
1554 # Because of customization, a given Nit method can be compiler more that once
1555 abstract class AbstractRuntimeFunction
1557 type COMPILER: AbstractCompiler
1558 type VISITOR: AbstractCompilerVisitor
1560 # The associated Nit method
1561 var mmethoddef: MMethodDef
1563 # The mangled c name of the runtime_function
1564 # Subclasses should redefine `build_c_name
` instead
1567 var res = self.c_name_cache
1568 if res != null then return res
1569 res = self.build_c_name
1570 self.c_name_cache = res
1574 # Non cached version of `c_name
`
1575 protected fun build_c_name: String is abstract
1577 protected var c_name_cache: nullable String = null is writable
1579 # Implements a call of the runtime_function
1580 # May inline the body or generate a C function call
1581 fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1583 # Generate the code for the `AbstractRuntimeFunction`
1584 # Warning: compile more than once compilation makes CC unhappy
1585 fun compile_to_c(compiler: COMPILER) is abstract
1588 # A runtime variable hold a runtime value in C.
1589 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
1591 # 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.
1592 class RuntimeVariable
1593 # The name of the variable in the C code
1596 # The static type of the variable (as declard in C)
1599 # The current casted type of the variable (as known in Nit)
1600 var mcasttype: MType is writable
1602 # If the variable exaclty a mcasttype?
1603 # false (usual value) means that the variable is a mcasttype or a subtype.
1604 var is_exact: Bool = false is writable
1606 init(name: String, mtype: MType, mcasttype: MType)
1610 self.mcasttype = mcasttype
1611 assert not mtype.need_anchor
1612 assert not mcasttype.need_anchor
1615 redef fun to_s do return name
1620 if self.is_exact then
1621 exact_str = " exact"
1626 if self.mtype == self.mcasttype then
1627 type_str = "{mtype}{exact_str}"
1629 type_str = "{mtype}({mcasttype}{exact_str})"
1631 return "<{name}:{type_str}>"
1635 # A frame correspond to a visited property in a `GlobalCompilerVisitor`
1638 type VISITOR: AbstractCompilerVisitor
1640 # The associated visitor
1641 var visitor: VISITOR
1643 # The executed property.
1644 # A Method in case of a call, an attribute in case of a default initialization.
1645 var mpropdef: MPropDef
1647 # The static type of the receiver
1648 var receiver: MClassType
1650 # Arguments of the method (the first is the receiver)
1651 var arguments: Array[RuntimeVariable]
1653 # The runtime_variable associated to the return (in a function)
1654 var returnvar: nullable RuntimeVariable = null is writable
1656 # The label at the end of the property
1657 var returnlabel: nullable String = null is writable
1661 # Return the C type associated to a given Nit static type
1662 fun ctype: String do return "val*"
1664 # C type outside of the compiler code and in boxes
1665 fun ctype_extern: String do return "val*"
1667 # Short name of the `ctype
` to use in unions
1668 fun ctypename: String do return "val"
1670 # Return the name of the C structure associated to a Nit live type
1671 fun c_name: String is abstract
1672 protected var c_name_cache: nullable String is protected writable
1675 redef class MClassType
1678 var res = self.c_name_cache
1679 if res != null then return res
1680 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1681 self.c_name_cache = res
1685 redef fun ctype: String
1687 if mclass.name == "Int" then
1689 else if mclass.name == "Bool" then
1691 else if mclass.name == "Char" then
1693 else if mclass.name == "Float" then
1695 else if mclass.name == "NativeString" then
1697 else if mclass.name == "NativeArray" then
1704 redef fun ctype_extern: String
1706 if mclass.kind == extern_kind then
1713 redef fun ctypename: String
1715 if mclass.name == "Int" then
1717 else if mclass.name == "Bool" then
1719 else if mclass.name == "Char" then
1721 else if mclass.name == "Float" then
1723 else if mclass.name == "NativeString" then
1725 else if mclass.name == "NativeArray" then
1726 #return "{self.arguments.first.ctype}*"
1734 redef class MGenericType
1737 var res = self.c_name_cache
1738 if res != null then return res
1740 for t in self.arguments do
1741 res = res + t.c_name
1743 self.c_name_cache = res
1748 redef class MParameterType
1751 var res = self.c_name_cache
1752 if res != null then return res
1753 res = "{self.mclass.c_name}_FT{self.rank}"
1754 self.c_name_cache = res
1759 redef class MVirtualType
1762 var res = self.c_name_cache
1763 if res != null then return res
1764 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1765 self.c_name_cache = res
1770 redef class MNullableType
1773 var res = self.c_name_cache
1774 if res != null then return res
1775 res = "nullable_{self.mtype.c_name}"
1776 self.c_name_cache = res
1782 # Return the name of the C structure associated to a Nit class
1783 fun c_name: String do
1784 var res = self.c_name_cache
1785 if res != null then return res
1786 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1787 self.c_name_cache = res
1790 private var c_name_cache: nullable String
1793 redef class MProperty
1794 fun c_name: String do
1795 var res = self.c_name_cache
1796 if res != null then return res
1797 res = "{self.intro.c_name}"
1798 self.c_name_cache = res
1801 private var c_name_cache: nullable String
1804 redef class MPropDef
1805 type VISITOR: AbstractCompilerVisitor
1807 private var c_name_cache: nullable String
1809 # The mangled name associated to the property
1812 var res = self.c_name_cache
1813 if res != null then return res
1814 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1815 self.c_name_cache = res
1820 redef class MMethodDef
1821 # Can the body be inlined?
1822 fun can_inline(v: VISITOR): Bool
1824 if is_abstract then return true
1825 var modelbuilder = v.compiler.modelbuilder
1826 if modelbuilder.mpropdef2npropdef.has_key(self) then
1827 var npropdef = modelbuilder.mpropdef2npropdef[self]
1828 return npropdef.can_inline
1829 else if self.mproperty.is_root_init then
1830 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1837 # Inline the body in another visitor
1838 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1840 var modelbuilder = v.compiler.modelbuilder
1841 var val = constant_value
1842 if modelbuilder.mpropdef2npropdef.has_key(self) then
1843 var npropdef = modelbuilder.mpropdef2npropdef[self]
1844 var oldnode = v.current_node
1845 v.current_node = npropdef
1846 self.compile_parameter_check(v, arguments)
1847 npropdef.compile_to_c(v, self, arguments)
1848 v.current_node = oldnode
1849 else if self.mproperty.is_root_init then
1850 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1851 var oldnode = v.current_node
1852 v.current_node = nclassdef
1853 self.compile_parameter_check(v, arguments)
1854 nclassdef.compile_to_c(v, self, arguments)
1855 v.current_node = oldnode
1856 else if val != null then
1857 v.ret(v.value_instance(val))
1864 # Generate type checks in the C code to check covariant parameters
1865 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1867 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1869 for i in [0..msignature.arity[ do
1870 # skip test for vararg since the array is instantiated with the correct polymorphic type
1871 if msignature.vararg_rank == i then continue
1873 # skip if the cast is not required
1874 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1875 if not origmtype.need_anchor then continue
1877 # get the parameter type
1878 var mtype = self.msignature.mparameters[i].mtype
1881 # note that v decides if and how to implements the cast
1882 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1883 v.add_cast(arguments[i+1], mtype, "covariance")
1890 redef class APropdef
1891 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1893 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1894 debug("Not yet implemented")
1897 fun can_inline: Bool do return true
1900 redef class AMethPropdef
1901 redef fun compile_to_c(v, mpropdef, arguments)
1903 if mpropdef.is_abstract then
1904 var cn = v.class_name_string(arguments.first)
1905 v.add("PRINT_ERROR(\"Runtime error: Abstract method `%s
` called on `%s
`\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1910 # Call the implicit super-init
1911 var auto_super_inits = self.auto_super_inits
1912 if auto_super_inits != null then
1913 var args = [arguments.first]
1914 for auto_super_init in auto_super_inits do
1915 assert auto_super_init.mproperty != mpropdef.mproperty
1917 for i in [0..auto_super_init.msignature.arity+1[ do
1918 args.add(arguments[i])
1920 assert auto_super_init.mproperty != mpropdef.mproperty
1921 v.compile_callsite(auto_super_init, args)
1924 if auto_super_call then
1925 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
1928 # Try special compilation
1929 if mpropdef.is_intern then
1930 if compile_intern_to_c(v, mpropdef, arguments) then return
1931 else if mpropdef.is_extern then
1932 if mpropdef.mproperty.is_init then
1933 if compile_externinit_to_c(v, mpropdef, arguments) then return
1935 if compile_externmeth_to_c(v, mpropdef, arguments) then return
1939 # Compile block if any
1940 var n_block = n_block
1941 if n_block != null then
1942 for i in [0..mpropdef.msignature.arity[ do
1943 var variable = self.n_signature.n_params[i].variable.as(not null)
1944 v.assign(v.variable(variable), arguments[i+1])
1951 var cn = v.class_name_string(arguments.first)
1952 v.add("PRINT_ERROR(\"Runtime error: uncompiled method `%s
` called on `%s
`. NOT YET IMPLEMENTED\", \"{mpropdef.mproperty.name.escape_to_c}\", {cn});")
1956 redef fun can_inline
1958 if self.auto_super_inits != null then return false
1959 var nblock = self.n_block
1960 if nblock == null then return true
1961 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1962 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1966 fun compile_intern_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
1968 var pname = mpropdef.mproperty.name
1969 var cname = mpropdef.mclassdef.mclass.name
1970 var ret = mpropdef.msignature.return_mtype
1972 ret = v.resolve_for(ret, arguments.first)
1974 if pname != "==" and pname != "!=" then
1975 v.adapt_signature(mpropdef, arguments)
1976 v.unbox_signature_extern(mpropdef, arguments)
1978 if cname == "Int" then
1979 if pname == "output" then
1980 v.add("printf(\"%ld\\n\", {arguments.first});")
1982 else if pname == "object_id" then
1983 v.ret(arguments.first)
1985 else if pname == "+" then
1986 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1988 else if pname == "-" then
1989 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1991 else if pname == "unary -" then
1992 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1994 else if pname == "*" then
1995 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1997 else if pname == "/" then
1998 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2000 else if pname == "%" then
2001 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
2003 else if pname == "lshift" then
2004 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
2006 else if pname == "rshift" then
2007 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
2009 else if pname == "==" then
2010 v.ret(v.equal_test(arguments[0], arguments[1]))
2012 else if pname == "!=" then
2013 var res = v.equal_test(arguments[0], arguments[1])
2014 v.ret(v.new_expr("!{res}", ret.as(not null)))
2016 else if pname == "<" then
2017 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2019 else if pname == ">" then
2020 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2022 else if pname == "<=" then
2023 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2025 else if pname == ">=" then
2026 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2028 else if pname == "to_f" then
2029 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
2031 else if pname == "ascii" then
2032 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
2035 else if cname == "Char" then
2036 if pname == "output" then
2037 v.add("printf(\"%c\", {arguments.first});")
2039 else if pname == "object_id" then
2040 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2042 else if pname == "successor" then
2043 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2045 else if pname == "predecessor" then
2046 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2048 else if pname == "==" then
2049 v.ret(v.equal_test(arguments[0], arguments[1]))
2051 else if pname == "!=" then
2052 var res = v.equal_test(arguments[0], arguments[1])
2053 v.ret(v.new_expr("!{res}", ret.as(not null)))
2055 else if pname == "<" then
2056 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2058 else if pname == ">" then
2059 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2061 else if pname == "<=" then
2062 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2064 else if pname == ">=" then
2065 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2067 else if pname == "to_i" then
2068 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
2070 else if pname == "ascii" then
2071 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
2074 else if cname == "Bool" then
2075 if pname == "output" then
2076 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
2078 else if pname == "object_id" then
2079 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2081 else if pname == "==" then
2082 v.ret(v.equal_test(arguments[0], arguments[1]))
2084 else if pname == "!=" then
2085 var res = v.equal_test(arguments[0], arguments[1])
2086 v.ret(v.new_expr("!{res}", ret.as(not null)))
2089 else if cname == "Float" then
2090 if pname == "output" then
2091 v.add("printf(\"%f\\n\", {arguments.first});")
2093 else if pname == "object_id" then
2094 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
2096 else if pname == "+" then
2097 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
2099 else if pname == "-" then
2100 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
2102 else if pname == "unary -" then
2103 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
2105 else if pname == "succ" then
2106 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
2108 else if pname == "prec" then
2109 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
2111 else if pname == "*" then
2112 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
2114 else if pname == "/" then
2115 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
2117 else if pname == "==" then
2118 v.ret(v.equal_test(arguments[0], arguments[1]))
2120 else if pname == "!=" then
2121 var res = v.equal_test(arguments[0], arguments[1])
2122 v.ret(v.new_expr("!{res}", ret.as(not null)))
2124 else if pname == "<" then
2125 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
2127 else if pname == ">" then
2128 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
2130 else if pname == "<=" then
2131 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
2133 else if pname == ">=" then
2134 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
2136 else if pname == "to_i" then
2137 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
2140 else if cname == "NativeString" then
2141 if pname == "[]" then
2142 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
2144 else if pname == "[]=" then
2145 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2147 else if pname == "copy_to" then
2148 v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2150 else if pname == "atoi" then
2151 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
2153 else if pname == "new" then
2154 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2157 else if cname == "NativeArray" then
2158 v.native_array_def(pname, ret, arguments)
2161 if pname == "exit" then
2162 v.add("exit({arguments[1]});")
2164 else if pname == "sys" then
2165 v.ret(v.new_expr("glob_sys", ret.as(not null)))
2167 else if pname == "calloc_string" then
2168 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
2170 else if pname == "calloc_array" then
2171 v.calloc_array(ret.as(not null), arguments)
2173 else if pname == "object_id" then
2174 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
2176 else if pname == "is_same_type" then
2177 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
2179 else if pname == "is_same_instance" then
2180 v.ret(v.equal_test(arguments[0], arguments[1]))
2182 else if pname == "output_class_name" then
2183 var nat = v.class_name_string(arguments.first)
2184 v.add("printf(\"%s\\n\", {nat});")
2186 else if pname == "native_class_name" then
2187 var nat = v.class_name_string(arguments.first)
2188 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
2190 else if pname == "force_garbage_collection" then
2191 v.add("nit_gcollect();")
2193 else if pname == "native_argc" then
2194 v.ret(v.new_expr("glob_argc", ret.as(not null)))
2196 else if pname == "native_argv" then
2197 v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
2203 # Compile an extern method
2204 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2205 fun compile_externmeth_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2208 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2210 externname = at.arg_as_string(v.compiler.modelbuilder)
2211 if externname == null then return false
2215 if location.file != null then
2216 var file = location.file.filename
2219 var res: nullable RuntimeVariable = null
2220 var ret = mpropdef.msignature.return_mtype
2222 ret = v.resolve_for(ret, arguments.first)
2223 res = v.new_var_extern(ret)
2225 v.adapt_signature(mpropdef, arguments)
2226 v.unbox_signature_extern(mpropdef, arguments)
2229 v.add("{externname}({arguments.join(", ")});")
2231 v.add("{res} = {externname}({arguments.join(", ")});")
2232 res = v.box_extern(res, ret.as(not null))
2238 # Compile an extern factory
2239 # Return `true` if the compilation was successful, `false` if a fall-back is needed
2240 fun compile_externinit_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool
2243 var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
2245 externname = at.arg_as_string(v.compiler.modelbuilder)
2246 if externname == null then return false
2250 if location.file != null then
2251 var file = location.file.filename
2254 v.adapt_signature(mpropdef, arguments)
2255 v.unbox_signature_extern(mpropdef, arguments)
2256 var ret = arguments.first.mtype
2257 var res = v.new_var_extern(ret)
2261 v.add("{res} = {externname}({arguments.join(", ")});")
2262 res = v.box_extern(res, ret)
2268 redef class AAttrPropdef
2269 redef fun compile_to_c(v, mpropdef, arguments)
2271 if mpropdef == mreadpropdef then
2272 assert arguments.length == 1
2273 var recv = arguments.first
2277 var ret = self.mpropdef.static_mtype
2278 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2279 var guard = self.mlazypropdef.mproperty
2281 set = v.isset_attribute(self.mpropdef.mproperty, recv)
2283 set = v.read_attribute(guard, recv)
2285 v.add("if(likely({set})) \{")
2286 res = v.read_attribute(self.mpropdef.mproperty, recv)
2289 var value = evaluate_expr(v, recv)
2291 v.assign(res, value)
2293 var true_v = v.new_expr("1", v.bool_type)
2294 v.write_attribute(guard, arguments.first, true_v)
2298 res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
2300 v.assign(v.frame.returnvar.as(not null), res)
2301 else if mpropdef == mwritepropdef then
2302 assert arguments.length == 2
2303 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
2305 var ret = self.mpropdef.static_mtype
2306 var useiset = ret.ctype == "val*" and not ret isa MNullableType
2308 v.write_attribute(self.mlazypropdef.mproperty, arguments.first, v.new_expr("1", v.bool_type))
2316 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2318 if has_value and not is_lazy then evaluate_expr(v, recv)
2321 # Evaluate, store and return the default value of the attribute
2322 private fun evaluate_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable): RuntimeVariable
2324 var oldnode = v.current_node
2325 v.current_node = self
2326 var old_frame = v.frame
2327 var frame = new Frame(v, self.mpropdef.as(not null), recv.mcasttype.as(MClassType), [recv])
2331 var mtype = self.mpropdef.static_mtype
2332 assert mtype != null
2334 var nexpr = self.n_expr
2335 var nblock = self.n_block
2336 if nexpr != null then
2337 value = v.expr(nexpr, mtype)
2338 else if nblock != null then
2339 value = v.new_var(mtype)
2340 frame.returnvar = value
2341 frame.returnlabel = v.get_name("RET_LABEL")
2344 v.add("{frame.returnlabel.as(not null)}:(void)0;")
2350 v.write_attribute(self.mpropdef.mproperty, recv, value)
2353 v.current_node = oldnode
2358 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
2360 var nexpr = self.n_expr
2361 if nexpr != null then return
2363 var oldnode = v.current_node
2364 v.current_node = self
2365 var old_frame = v.frame
2366 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
2368 # Force read to check the initialization
2369 v.read_attribute(self.mpropdef.mproperty, recv)
2371 v.current_node = oldnode
2375 redef class AClassdef
2376 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
2378 if mpropdef == self.mfree_init then
2379 assert mpropdef.mproperty.is_root_init
2380 assert arguments.length == 1
2381 if not mpropdef.is_intro then
2382 v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
2392 # Try to compile self as an expression
2393 # Do not call this method directly, use `v
.expr
` instead
2394 private fun expr(v: AbstractCompilerVisitor): nullable RuntimeVariable
2396 v.add("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\");")
2397 var mtype = self.mtype
2398 if mtype == null then
2401 var res = v.new_var(mtype)
2402 v.add("/* {res} = NOT YET {class_name} */")
2407 # Try to compile self as a statement
2408 # Do not call this method directly, use `v
.stmt
` instead
2409 private fun stmt(v: AbstractCompilerVisitor)
2415 redef class ABlockExpr
2418 for e in self.n_expr do v.stmt(e)
2422 var last = self.n_expr.last
2423 for e in self.n_expr do
2424 if e == last then break
2427 return v.expr(last, null)
2431 redef class AVardeclExpr
2434 var variable = self.variable.as(not null)
2435 var ne = self.n_expr
2437 var i = v.expr(ne, variable.declared_type)
2438 v.assign(v.variable(variable), i)
2443 redef class AVarExpr
2446 var res = v.variable(self.variable.as(not null))
2447 var mtype = self.mtype.as(not null)
2448 return v.autoadapt(res, mtype)
2452 redef class AVarAssignExpr
2455 var variable = self.variable.as(not null)
2456 var i = v.expr(self.n_value, variable.declared_type)
2457 v.assign(v.variable(variable), i)
2462 redef class AVarReassignExpr
2465 var variable = self.variable.as(not null)
2466 var vari = v.variable(variable)
2467 var value = v.expr(self.n_value, variable.declared_type)
2468 var res = v.compile_callsite(self.reassign_callsite.as(not null), [vari, value])
2470 v.assign(v.variable(variable), res)
2474 redef class ASelfExpr
2475 redef fun expr(v) do return v.frame.arguments.first
2478 redef class AEscapeExpr
2479 redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
2482 redef class AReturnExpr
2485 var nexpr = self.n_expr
2486 if nexpr != null then
2487 var returnvar = v.frame.returnvar.as(not null)
2488 var i = v.expr(nexpr, returnvar.mtype)
2489 v.assign(returnvar, i)
2491 v.add("goto {v.frame.returnlabel.as(not null)};")
2495 redef class AAbortExpr
2496 redef fun stmt(v) do v.add_abort("Aborted")
2502 var cond = v.expr_bool(self.n_expr)
2503 v.add("if ({cond})\{")
2512 var res = v.new_var(self.mtype.as(not null))
2513 var cond = v.expr_bool(self.n_expr)
2514 v.add("if ({cond})\{")
2515 v.assign(res, v.expr(self.n_then.as(not null), null))
2517 v.assign(res, v.expr(self.n_else.as(not null), null))
2523 redef class AIfexprExpr
2526 var res = v.new_var(self.mtype.as(not null))
2527 var cond = v.expr_bool(self.n_expr)
2528 v.add("if ({cond})\{")
2529 v.assign(res, v.expr(self.n_then, null))
2531 v.assign(res, v.expr(self.n_else, null))
2540 v.stmt(self.n_block)
2541 v.add_escape_label(break_mark)
2545 redef class AWhileExpr
2549 var cond = v.expr_bool(self.n_expr)
2550 v.add("if (!{cond}) break;")
2551 v.stmt(self.n_block)
2552 v.add_escape_label(continue_mark)
2554 v.add_escape_label(break_mark)
2558 redef class ALoopExpr
2562 v.stmt(self.n_block)
2563 v.add_escape_label(continue_mark)
2565 v.add_escape_label(break_mark)
2569 redef class AForExpr
2572 var cl = v.expr(self.n_expr, null)
2573 var it_meth = self.method_iterator
2574 assert it_meth != null
2575 var it = v.compile_callsite(it_meth, [cl])
2578 var isok_meth = self.method_is_ok
2579 assert isok_meth != null
2580 var ok = v.compile_callsite(isok_meth, [it])
2582 v.add("if(!{ok}) break;")
2583 if self.variables.length == 1 then
2584 var item_meth = self.method_item
2585 assert item_meth != null
2586 var i = v.compile_callsite(item_meth, [it])
2588 v.assign(v.variable(variables.first), i)
2589 else if self.variables.length == 2 then
2590 var key_meth = self.method_key
2591 assert key_meth != null
2592 var i = v.compile_callsite(key_meth, [it])
2594 v.assign(v.variable(variables[0]), i)
2595 var item_meth = self.method_item
2596 assert item_meth != null
2597 i = v.compile_callsite(item_meth, [it])
2599 v.assign(v.variable(variables[1]), i)
2603 v.stmt(self.n_block)
2604 v.add_escape_label(continue_mark)
2605 var next_meth = self.method_next
2606 assert next_meth != null
2607 v.compile_callsite(next_meth, [it])
2609 v.add_escape_label(break_mark)
2611 var method_finish = self.method_finish
2612 if method_finish != null then
2613 # TODO: Find a way to call this also in long escape (e.g. return)
2614 v.compile_callsite(method_finish, [it])
2619 redef class AAssertExpr
2622 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return
2624 var cond = v.expr_bool(self.n_expr)
2625 v.add("if (unlikely(!{cond})) \{")
2629 v.add_abort("Assert '{nid.text}' failed")
2631 v.add_abort("Assert failed")
2640 var res = v.new_var(self.mtype.as(not null))
2641 var i1 = v.expr_bool(self.n_expr)
2642 v.add("if ({i1}) \{")
2645 var i2 = v.expr_bool(self.n_expr2)
2646 v.add("{res} = {i2};")
2652 redef class AImpliesExpr
2655 var res = v.new_var(self.mtype.as(not null))
2656 var i1 = v.expr_bool(self.n_expr)
2657 v.add("if (!{i1}) \{")
2660 var i2 = v.expr_bool(self.n_expr2)
2661 v.add("{res} = {i2};")
2667 redef class AAndExpr
2670 var res = v.new_var(self.mtype.as(not null))
2671 var i1 = v.expr_bool(self.n_expr)
2672 v.add("if (!{i1}) \{")
2675 var i2 = v.expr_bool(self.n_expr2)
2676 v.add("{res} = {i2};")
2682 redef class ANotExpr
2685 var cond = v.expr_bool(self.n_expr)
2686 return v.new_expr("!{cond}", self.mtype.as(not null))
2690 redef class AOrElseExpr
2693 var res = v.new_var(self.mtype.as(not null))
2694 var i1 = v.expr(self.n_expr, null)
2695 v.add("if ({i1}!=NULL) \{")
2698 var i2 = v.expr(self.n_expr2, null)
2705 redef class AIntExpr
2706 redef fun expr(v) do return v.new_expr("{self.value.to_s}", self.mtype.as(not null))
2709 redef class AFloatExpr
2710 redef fun expr(v) do return v.new_expr("{self.n_float.text}", self.mtype.as(not null)) # FIXME use value, not n_float
2713 redef class ACharExpr
2714 redef fun expr(v) do return v.new_expr("'{self.value.to_s.escape_to_c}'", self.mtype.as(not null))
2717 redef class AArrayExpr
2720 var mtype = self.mtype.as(MClassType).arguments.first
2721 var array = new Array[RuntimeVariable]
2722 for nexpr in self.n_exprs.n_exprs do
2723 var i = v.expr(nexpr, mtype)
2726 return v.array_instance(array, mtype)
2730 redef class AStringFormExpr
2731 redef fun expr(v) do return v.string_instance(self.value.as(not null))
2734 redef class ASuperstringExpr
2737 var array = new Array[RuntimeVariable]
2738 for ne in self.n_exprs do
2739 if ne isa AStringFormExpr and ne.value == "" then continue # skip empty sub-strings
2740 var i = v.expr(ne, null)
2743 var a = v.array_instance(array, v.object_type)
2744 var res = v.send(v.get_property("to_s", a.mtype), [a])
2749 redef class ACrangeExpr
2752 var i1 = v.expr(self.n_expr, null)
2753 var i2 = v.expr(self.n_expr2, null)
2754 var mtype = self.mtype.as(MClassType)
2755 var res = v.init_instance(mtype)
2756 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2761 redef class AOrangeExpr
2764 var i1 = v.expr(self.n_expr, null)
2765 var i2 = v.expr(self.n_expr2, null)
2766 var mtype = self.mtype.as(MClassType)
2767 var res = v.init_instance(mtype)
2768 v.compile_callsite(init_callsite.as(not null), [res, i1, i2])
2773 redef class ATrueExpr
2774 redef fun expr(v) do return v.new_expr("1", self.mtype.as(not null))
2777 redef class AFalseExpr
2778 redef fun expr(v) do return v.new_expr("0", self.mtype.as(not null))
2781 redef class ANullExpr
2782 redef fun expr(v) do return v.new_expr("NULL", self.mtype.as(not null))
2785 redef class AIsaExpr
2788 var i = v.expr(self.n_expr, null)
2789 return v.type_test(i, self.cast_type.as(not null), "isa")
2793 redef class AAsCastExpr
2796 var i = v.expr(self.n_expr, null)
2797 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2799 v.add_cast(i, self.mtype.as(not null), "as")
2804 redef class AAsNotnullExpr
2807 var i = v.expr(self.n_expr, null)
2808 if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
2810 if i.mtype.ctype != "val*" then return i
2812 v.add("if (unlikely({i} == NULL)) \{")
2813 v.add_abort("Cast failed")
2819 redef class AParExpr
2820 redef fun expr(v) do return v.expr(self.n_expr, null)
2823 redef class AOnceExpr
2826 var mtype = self.mtype.as(not null)
2827 var name = v.get_name("varonce")
2828 var guard = v.get_name(name + "_guard")
2829 v.add_decl("static {mtype.ctype} {name};")
2830 v.add_decl("static int {guard};")
2831 var res = v.new_var(mtype)
2832 v.add("if ({guard}) \{")
2833 v.add("{res} = {name};")
2835 var i = v.expr(self.n_expr, mtype)
2836 v.add("{res} = {i};")
2837 v.add("{name} = {res};")
2838 v.add("{guard} = 1;")
2844 redef class ASendExpr
2847 var recv = v.expr(self.n_expr, null)
2848 var callsite = self.callsite.as(not null)
2849 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2850 return v.compile_callsite(callsite, args)
2854 redef class ASendReassignFormExpr
2857 var recv = v.expr(self.n_expr, null)
2858 var callsite = self.callsite.as(not null)
2859 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
2861 var value = v.expr(self.n_value, null)
2863 var left = v.compile_callsite(callsite, args)
2866 var res = v.compile_callsite(self.reassign_callsite.as(not null), [left, value])
2870 v.compile_callsite(self.write_callsite.as(not null), args)
2874 redef class ASuperExpr
2877 var recv = v.frame.arguments.first
2879 var callsite = self.callsite
2880 if callsite != null then
2881 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2883 # Add additional arguments for the super init call
2884 if args.length == 1 then
2885 for i in [0..callsite.msignature.arity[ do
2886 args.add(v.frame.arguments[i+1])
2890 var res = v.compile_callsite(callsite, args)
2894 var mpropdef = self.mpropdef.as(not null)
2895 var args = v.varargize(mpropdef, recv, self.n_args.n_exprs)
2896 if args.length == 1 then
2897 args = v.frame.arguments
2900 # stantard call-next-method
2901 return v.supercall(mpropdef, recv.mtype.as(MClassType), args)
2905 redef class ANewExpr
2908 var mtype = self.recvtype
2909 assert mtype != null
2911 var ctype = mtype.ctype
2912 if mtype.mclass.name == "NativeArray" then
2913 assert self.n_args.n_exprs.length == 1
2914 var l = v.expr(self.n_args.n_exprs.first, null)
2915 assert mtype isa MGenericType
2916 var elttype = mtype.arguments.first
2917 return v.native_array_instance(elttype, l)
2918 else if ctype == "val*" then
2919 recv = v.init_instance(mtype)
2920 else if ctype == "char*" then
2921 recv = v.new_expr("NULL/*special!*/", mtype)
2923 recv = v.new_expr("({ctype})0/*special!*/", mtype)
2926 var callsite = self.callsite.as(not null)
2927 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
2928 var res2 = v.compile_callsite(callsite, args)
2929 if res2 != null then
2930 #self.debug("got {res2} from {mproperty}. drop {recv}")
2937 redef class AAttrExpr
2940 var recv = v.expr(self.n_expr, null)
2941 var mproperty = self.mproperty.as(not null)
2942 return v.read_attribute(mproperty, recv)
2946 redef class AAttrAssignExpr
2949 var recv = v.expr(self.n_expr, null)
2950 var i = v.expr(self.n_value, null)
2951 var mproperty = self.mproperty.as(not null)
2952 v.write_attribute(mproperty, recv, i)
2957 redef class AAttrReassignExpr
2960 var recv = v.expr(self.n_expr, null)
2961 var value = v.expr(self.n_value, null)
2962 var mproperty = self.mproperty.as(not null)
2963 var attr = v.read_attribute(mproperty, recv)
2964 var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
2966 v.write_attribute(mproperty, recv, res)
2970 redef class AIssetAttrExpr
2973 var recv = v.expr(self.n_expr, null)
2974 var mproperty = self.mproperty.as(not null)
2975 return v.isset_attribute(mproperty, recv)
2979 redef class ADebugTypeExpr
2988 redef class Array[E]
2989 # Return a new `Array` with the elements only contened in self and not in `o
`
2990 fun -(o: Array[E]): Array[E] do
2991 var res = new Array[E]
2992 for e in self do if not o.has(e) then res.add(e)
2998 # All `MProperty` associated to all `MClassDef` of `mclass
`
2999 fun properties(mclass: MClass): Set[MProperty] do
3000 if not self.properties_cache.has_key(mclass) then
3001 var properties = new HashSet[MProperty]
3002 var parents = new Array[MClass]
3003 if self.flatten_mclass_hierarchy.has(mclass) then
3004 parents.add_all(mclass.in_hierarchy(self).direct_greaters)
3006 for parent in parents do
3007 properties.add_all(self.properties(parent))
3009 for mclassdef in mclass.mclassdefs do
3010 if not self.in_importation <= mclassdef.mmodule then continue
3011 for mprop in mclassdef.intro_mproperties do
3012 properties.add(mprop)
3015 self.properties_cache[mclass] = properties
3017 return properties_cache[mclass]
3019 private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
3021 # Write FFI and nitni results to file
3022 fun finalize_ffi(c: AbstractCompiler) do end
3024 # Give requided addinional system libraries (as given to LD_LIBS)
3025 # Note: can return null instead of an empty set
3026 fun collect_linker_libs: nullable Set[String] do return null
3029 # Create a tool context to handle options and paths
3030 var toolcontext = new ToolContext
3032 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
3034 # We do not add other options, so process them now!
3035 toolcontext.process_options(args)
3037 # We need a model to collect stufs
3038 var model = new Model
3039 # An a model builder to parse files
3040 var modelbuilder = new ModelBuilder(model, toolcontext)
3042 var arguments = toolcontext.option_context.rest
3043 if arguments.length > 1 and toolcontext.opt_output.value != null then
3044 print "Error: --output needs a single source file. Do you prefer --dir?"
3048 # Here we load an process all modules passed on the command line
3049 var mmodules = modelbuilder.parse(arguments)
3051 if mmodules.is_empty then return
3052 modelbuilder.run_phases
3054 for mmodule in mmodules do
3055 toolcontext.info("*** PROCESS {mmodule} ***", 1)
3057 toolcontext.run_global_phases(ms)