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
28 private import explain_assert_api
30 # Add compiling options
31 redef class ToolContext
33 var opt_output
= new OptionString("Filename of the generated executable", "-o", "--output")
35 var opt_dir
= new OptionString("Output directory", "--dir")
37 var opt_run
= new OptionBool("Execute the binary after the compilation", "--run")
39 var opt_no_cc
= new OptionBool("Do not invoke the C compiler", "--no-cc")
41 var opt_no_main
= new OptionBool("Do not generate main entry point", "--no-main")
43 var opt_shared_lib
= new OptionBool("Compile to a native shared library", "--shared-lib")
45 var opt_make_flags
= new OptionString("Additional options to the `make` command", "--make-flags")
47 var opt_max_c_lines
= new OptionInt("Maximum number of lines in generated C files. Use 0 for unlimited", 10000, "--max-c-lines")
49 var opt_group_c_files
= new OptionBool("Group all generated code in the same series of files", "--group-c-files")
51 var opt_compile_dir
= new OptionString("Directory used to generate temporary files", "--compile-dir")
53 var opt_hardening
= new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
54 # --no-check-covariance
55 var opt_no_check_covariance
= new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
56 # --no-check-attr-isset
57 var opt_no_check_attr_isset
= new OptionBool("Disable isset tests before each attribute access (dangerous)", "--no-check-attr-isset")
59 var opt_no_check_assert
= new OptionBool("Disable the evaluation of explicit `assert` and `as` (dangerous)", "--no-check-assert")
61 var opt_no_check_autocast
= new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
63 var opt_no_check_null
= new OptionBool("Disable tests of null receiver (dangerous)", "--no-check-null")
65 var opt_no_check_all
= new OptionBool("Disable all tests (dangerous)", "--no-check-all")
66 # --typing-test-metrics
67 var opt_typing_test_metrics
= new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
68 # --invocation-metrics
69 var opt_invocation_metrics
= new OptionBool("Enable static and dynamic count of all method invocations", "--invocation-metrics")
70 # --isset-checks-metrics
71 var opt_isset_checks_metrics
= new OptionBool("Enable static and dynamic count of isset checks before attributes access", "--isset-checks-metrics")
73 var opt_no_stacktrace
= new OptionBool("Disable the generation of stack traces", "--no-stacktrace")
75 var opt_no_gcc_directive
= new OptionArray("Disable advanced gcc directives for optimization", "--no-gcc-directive")
77 var opt_release
= new OptionBool("Compile in release mode and finalize application", "--release")
79 var opt_debug
= new OptionBool("Compile in debug mode (no C-side optimization)", "-g", "--debug")
81 var opt_trace
= new OptionBool("Compile with lttng's instrumentation", "--trace")
86 self.option_context
.add_option
(self.opt_output
, self.opt_dir
, self.opt_run
, self.opt_no_cc
, self.opt_no_main
, self.opt_shared_lib
, self.opt_make_flags
, self.opt_compile_dir
, self.opt_hardening
)
87 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
)
88 self.option_context
.add_option
(self.opt_typing_test_metrics
, self.opt_invocation_metrics
, self.opt_isset_checks_metrics
)
89 self.option_context
.add_option
(self.opt_no_stacktrace
)
90 self.option_context
.add_option
(self.opt_no_gcc_directive
)
91 self.option_context
.add_option
(self.opt_release
)
92 self.option_context
.add_option
(self.opt_max_c_lines
, self.opt_group_c_files
)
93 self.option_context
.add_option
(self.opt_debug
)
94 self.option_context
.add_option
(self.opt_trace
)
96 opt_no_main
.hidden
= true
97 opt_shared_lib
.hidden
= true
100 redef fun process_options
(args
)
104 if opt_output
.value
!= null and opt_dir
.value
!= null then
105 print
"Option Error: cannot use both --dir and --output"
109 if opt_no_check_all
.value
then
110 opt_no_check_covariance
.value
= true
111 opt_no_check_attr_isset
.value
= true
112 opt_no_check_assert
.value
= true
113 opt_no_check_autocast
.value
= true
114 opt_no_check_null
.value
= true
119 redef class ModelBuilder
120 # Simple indirection to `Toolchain::write_and_make`
121 protected fun write_and_make
(compiler
: AbstractCompiler)
123 var platform
= compiler
.target_platform
124 var toolchain
= platform
.toolchain
(toolcontext
, compiler
)
125 compiler
.toolchain
= toolchain
126 toolchain
.write_and_make
131 # The specific tool-chain associated to the platform
132 fun toolchain
(toolcontext
: ToolContext, compiler
: AbstractCompiler): Toolchain
134 return new MakefileToolchain(toolcontext
, compiler
)
138 # Build toolchain for a specific target program, varies per `Platform`
142 var toolcontext
: ToolContext
144 # Compiler of the target program
145 var compiler
: AbstractCompiler
147 # Directory where to generate all files
149 # The option `--compile_dir` change this directory.
150 fun root_compile_dir
: String
152 var compile_dir
= toolcontext
.opt_compile_dir
.value
153 if compile_dir
== null then compile_dir
= "nit_compile"
157 # Directory where to generate all C files
159 # By default it is `root_compile_dir` but some platform may require that it is a subdirectory.
160 fun compile_dir
: String do return root_compile_dir
162 # Write all C files and compile them
163 fun write_and_make
is abstract
166 # Default toolchain using a Makefile
167 class MakefileToolchain
170 redef fun write_and_make
172 var debug
= toolcontext
.opt_debug
.value
173 var compile_dir
= compile_dir
175 # Remove the compilation directory unless explicitly set
176 var auto_remove
= toolcontext
.opt_compile_dir
.value
== null
177 # If debug flag is set, do not remove sources
178 if debug
then auto_remove
= false
180 # Generate the .h and .c files
181 # A single C file regroups many compiled rumtime functions
182 # 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
184 self.toolcontext
.info
("*** WRITING C ***", 1)
186 root_compile_dir
.mkdir
189 var cfiles
= new Array[String]
190 write_files
(compile_dir
, cfiles
)
192 # Generate the Makefile
194 write_makefile
(compile_dir
, cfiles
)
197 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
199 toolcontext
.check_errors
201 # Execute the Makefile
203 if self.toolcontext
.opt_no_cc
.value
then return
206 self.toolcontext
.info
("*** COMPILING C ***", 1)
208 compile_c_code
(compile_dir
)
211 sys
.system
("rm -r -- '{root_compile_dir.escape_to_sh}/'")
214 if toolcontext
.opt_run
.value
then
215 var mainmodule
= compiler
.mainmodule
216 var out
= outfile
(mainmodule
)
218 cmd
.append toolcontext
.option_context
.rest
219 toolcontext
.exec_and_check
(cmd
, "--run")
223 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
226 # Write all source files to the `compile_dir`
227 fun write_files
(compile_dir
: String, cfiles
: Array[String])
229 var platform
= compiler
.target_platform
230 if platform
.supports_libunwind
then compiler
.build_c_to_nit_bindings
231 var cc_opt_with_libgc
= "-DWITH_LIBGC"
232 if not platform
.supports_libgc
then cc_opt_with_libgc
= ""
234 # Add gc_choser.h to aditionnal bodies
235 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
236 if cc_opt_with_libgc
!= "" then gc_chooser
.pkgconfigs
.add
"bdw-gc"
237 compiler
.extern_bodies
.add
(gc_chooser
)
238 var clib
= toolcontext
.nit_dir
/ "clib"
239 compiler
.files_to_copy
.add
"{clib}/gc_chooser.c"
240 compiler
.files_to_copy
.add
"{clib}/gc_chooser.h"
242 # Add lttng traces provider to external bodies
243 if toolcontext
.opt_trace
.value
then
244 #-I. is there in order to make the TRACEPOINT_INCLUDE directive in clib/traces.h refer to the directory in which gcc was invoked.
245 var traces
= new ExternCFile("traces.c", "-I.")
246 traces
.pkgconfigs
.add
"lttng-ust"
247 compiler
.extern_bodies
.add
(traces
)
248 compiler
.files_to_copy
.add
"{clib}/traces.c"
249 compiler
.files_to_copy
.add
"{clib}/traces.h"
253 for m
in compiler
.mainmodule
.in_importation
.greaters
do
254 compiler
.finalize_ffi_for_module
(m
)
257 # Copy original .[ch] files to compile_dir
258 for src
in compiler
.files_to_copy
do
259 var basename
= src
.basename
260 var dst
= "{compile_dir}/{basename}"
264 var hfilename
= compiler
.header
.file
.name
+ ".h"
265 var hfilepath
= "{compile_dir}/{hfilename}"
266 var h
= new FileWriter.open
(hfilepath
)
267 for l
in compiler
.header
.decl_lines
do
271 for l
in compiler
.header
.lines
do
277 var max_c_lines
= toolcontext
.opt_max_c_lines
.value
278 for f
in compiler
.files
do
281 var file
: nullable FileWriter = null
282 for vis
in f
.writers
do
283 if vis
== compiler
.header
then continue
284 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
285 if total_lines
== 0 then continue
287 if file
== null or (count
> max_c_lines
and max_c_lines
> 0) then
289 if file
!= null then file
.close
290 var cfilename
= "{f.name}.{i}.c"
291 var cfilepath
= "{compile_dir}/{cfilename}"
292 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
293 cfiles
.add
(cfilename
)
294 file
= new FileWriter.open
(cfilepath
)
295 file
.write
"#include \"{f.name}.0.h\
"\n"
298 for l
in vis
.decl_lines
do
302 for l
in vis
.lines
do
307 if file
== null then continue
310 var cfilename
= "{f.name}.0.h"
311 var cfilepath
= "{compile_dir}/{cfilename}"
312 var hfile
: nullable FileWriter = null
313 hfile
= new FileWriter.open
(cfilepath
)
314 hfile
.write
"#include \"{hfilename}\
"\n"
315 for key
in f
.required_declarations
do
316 if not compiler
.provided_declarations
.has_key
(key
) then
317 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
319 node
.debug
"No provided declaration for {key}"
321 print
"No provided declaration for {key}"
325 hfile
.write compiler
.provided_declarations
[key
]
331 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
334 # Get the name of the Makefile to use
335 fun makefile_name
: String do return "{compiler.mainmodule.c_name}.mk"
337 # Get the default name of the executable to produce
338 fun default_outname
: String
340 var mainmodule
= compiler
.mainmodule
.first_real_mmodule
341 return mainmodule
.name
344 # Combine options and platform informations to get the final path of the outfile
345 fun outfile
(mainmodule
: MModule): String
347 var res
= self.toolcontext
.opt_output
.value
348 if res
!= null then return res
349 res
= default_outname
350 var dir
= self.toolcontext
.opt_dir
.value
351 if dir
!= null then return dir
.join_path
(res
)
356 fun write_makefile
(compile_dir
: String, cfiles
: Array[String])
358 var mainmodule
= compiler
.mainmodule
359 var platform
= compiler
.target_platform
361 var outname
= outfile
(mainmodule
)
363 var real_outpath
= compile_dir
.relpath
(outname
)
364 var outpath
= real_outpath
.escape_to_mk
365 if outpath
!= real_outpath
then
366 # If the name is crazy and need escaping, we will do an indirection
367 # 1. generate the binary in the nit_compile dir under an escaped name
368 # 2. copy the binary at the right place in the `all` goal.
369 outpath
= mainmodule
.c_name
371 var makename
= makefile_name
372 var makepath
= "{compile_dir}/{makename}"
373 var makefile
= new FileWriter.open
(makepath
)
375 var linker_options
= new HashSet[String]
376 for m
in mainmodule
.in_importation
.greaters
do
377 var libs
= m
.collect_linker_libs
378 if libs
!= null then linker_options
.add_all
(libs
)
380 var debug
= toolcontext
.opt_debug
.value
383 ifeq ($(origin CC), default)
386 ifeq ($(origin CXX), default)
389 CFLAGS ?= -g {{{if not debug then "-O2" else ""}}} -Wno-unused-value -Wno-switch -Wno-attributes -Wno-trigraphs
392 LDLIBS ?= -lm {{{linker_options.join(" ")}}}
395 if self.toolcontext
.opt_trace
.value
then makefile
.write
"LDLIBS += -llttng-ust -ldl\n"
397 if toolcontext
.opt_shared_lib
.value
then
400 LDFLAGS += -shared -Wl,-soname,{{{outname}}}
404 makefile
.write
"\n# SPECIAL CONFIGURATION FLAGS\n"
405 if platform
.supports_libunwind
then
406 if toolcontext
.opt_no_stacktrace
.value
then
407 makefile
.write
"NO_STACKTRACE ?= True"
409 makefile
.write
"NO_STACKTRACE ?= # Set to `True` to enable"
413 # Dynamic adaptations
414 # While `platform` enable complex toolchains, they are statically applied
415 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
416 makefile
.write
"\n\n"
418 # Check and adapt the targeted system
419 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
421 # Check and adapt for the compiler used
422 # clang need an additionnal `-Qunused-arguments`
423 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")
425 if platform
.supports_libunwind
then
427 ifneq ($(NO_STACKTRACE), True)
428 # Check and include lib-unwind in a portable way
429 ifneq ($(uname_S),Darwin)
430 # already included on macosx, but need to get the correct flags in other supported platforms.
431 ifeq ($(shell pkg-config --exists 'libunwind'; echo $$?), 0)
432 LDLIBS += `pkg-config --libs libunwind`
433 CFLAGS += `pkg-config --cflags libunwind`
435 $(warning "[_] stack-traces disabled. Please install libunwind-dev.")
436 CFLAGS += -D NO_STACKTRACE
440 # Stacktraces disabled
441 CFLAGS += -D NO_STACKTRACE
446 makefile
.write
("CFLAGS += -D NO_STACKTRACE\n\n")
450 # Special configuration for Darwin
451 ifeq ($(uname_S),Darwin)
452 # Remove POSIX flag -lrt
453 LDLIBS := $(filter-out -lrt,$(LDLIBS))
456 # Special configuration for Windows under mingw64
457 ifneq ($(findstring MINGW64,$(uname_S)),)
458 # Use the pcreposix regex library
459 LDLIBS += -lpcreposix
461 # Remove POSIX flag -lrt
462 LDLIBS := $(filter-out -lrt,$(LDLIBS))
464 # Silence warnings when storing Int, Char and Bool as pointer address
465 CFLAGS += -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast
468 # Add the compilation dir to the Java CLASSPATH
472 CLASSPATH := $(CLASSPATH):.
477 makefile
.write
("all: {outpath}\n")
478 if outpath
!= real_outpath
then
479 makefile
.write
("\tcp -- {outpath.escape_to_sh} {real_outpath.escape_to_sh.replace("$","$$")}")
483 var ofiles
= new Array[String]
484 var dep_rules
= new Array[String]
485 # Compile each generated file
487 var o
= f
.strip_extension
(".c") + ".o"
488 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
493 # Generate linker script, if any
494 if not compiler
.linker_script
.is_empty
then
495 var linker_script_path
= "{compile_dir}/linker_script"
496 ofiles
.add
"linker_script"
497 var f
= new FileWriter.open
(linker_script_path
)
498 for l
in compiler
.linker_script
do
505 # pkg-config annotation support
506 var pkgconfigs
= new Array[String]
507 for f
in compiler
.extern_bodies
do
508 pkgconfigs
.add_all f
.pkgconfigs
511 # Only test if pkg-config is used
512 if not pkgconfigs
.is_empty
then
514 # Check availability of pkg-config, silence the proc output
515 toolcontext
.check_pkgconfig_packages pkgconfigs
517 # Double the check in the Makefile in case it's distributed
519 # does pkg-config exists?
520 ifneq ($(shell which pkg-config >/dev/null; echo $$?), 0)
521 $(error "Command `pkg-config` not found. Please install it")
524 for p
in pkgconfigs
do
526 # Check for library {{{p}}}
527 ifneq ($(shell pkg-config --exists '{{{p}}}'; echo $$?), 0)
528 $(error "pkg-config: package {{{p}}} is not found.")
534 # Compile each required extern body into a specific .o
535 var java_files
= new Array[ExternFile]
536 for f
in compiler
.extern_bodies
do
537 var o
= f
.makefile_rule_name
538 makefile
.write
("{o}: {f.filename}\n")
539 makefile
.write
("\t{f.makefile_rule_content}\n\n")
540 dep_rules
.add
(f
.makefile_rule_name
)
542 if f
.compiles_to_o_file
then ofiles
.add
(o
)
543 if f
.add_to_jar
then java_files
.add
(f
)
546 if not java_files
.is_empty
then
547 var jar_file
= "{outpath}.jar"
549 var class_files_array
= new Array[String]
550 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
551 var class_files
= class_files_array
.join
(" ")
553 makefile
.write
("{jar_file}: {class_files}\n")
554 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
555 dep_rules
.add jar_file
560 if not pkgconfigs
.is_empty
then
561 pkg
= "`pkg-config --libs {pkgconfigs.join(" ")}`"
563 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath.escape_to_sh} {ofiles.join(" ")} $(LDLIBS) {pkg}\n\n")
565 makefile
.write
("clean:\n\trm -f {ofiles.join(" ")} 2>/dev/null\n")
566 if outpath
!= real_outpath
then
567 makefile
.write
("\trm -f -- {outpath.escape_to_sh} 2>/dev/null\n")
570 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
572 makepath
.file_copy_to
"{compile_dir}/Makefile"
575 # The C code is generated, compile it to an executable
576 fun compile_c_code
(compile_dir
: String)
578 var makename
= makefile_name
580 var makeflags
= self.toolcontext
.opt_make_flags
.value
581 if makeflags
== null then makeflags
= ""
583 var command
= "make -B -C {compile_dir} -f {makename} -j 4 {makeflags}"
584 self.toolcontext
.info
(command
, 2)
587 if self.toolcontext
.verbose_level
>= 3 then
588 res
= sys
.system
("{command} 2>&1")
589 else if is_windows
then
590 res
= sys
.system
("{command} 2>&1 >nul")
592 res
= sys
.system
("{command} 2>&1 >/dev/null")
595 toolcontext
.error
(null, "Compilation Error: `make` failed with error code: {res}. The command was `{command}`.")
600 # Singleton that store the knowledge about the compilation process
601 abstract class AbstractCompiler
602 type VISITOR: AbstractCompilerVisitor
604 # Table corresponding c_names to nit names (methods)
605 var names
= new HashMap[String, String]
607 # The main module of the program currently compiled
608 # Is assigned during the separate compilation
609 var mainmodule
: MModule is writable
611 # The real main module of the program
612 var realmainmodule
: MModule is noinit
614 # The modelbuilder used to know the model and the AST
615 var modelbuilder
: ModelBuilder is protected writable
617 # The associated toolchain
619 # Set by `modelbuilder.write_and_make` and permit sub-routines to access the current toolchain if required.
620 var toolchain
: Toolchain is noinit
622 # Is hardening asked? (see --hardening)
623 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
625 # The targeted specific platform
626 var target_platform
: Platform is noinit
630 self.realmainmodule
= mainmodule
631 target_platform
= mainmodule
.target_platform
or else new Platform
634 # Do the full code generation of the program `mainmodule`
635 # It is the main method usually called after the instantiation
636 fun do_compilation
is abstract
638 # Force the creation of a new file
639 # The point is to avoid contamination between must-be-compiled-separately files
640 fun new_file
(name
: String): CodeFile
642 if modelbuilder
.toolcontext
.opt_group_c_files
.value
then
643 if self.files
.is_empty
then
644 var f
= new CodeFile(mainmodule
.c_name
)
647 return self.files
.first
649 var f
= new CodeFile(name
)
654 # The list of all associated files
655 # Used to generate .c files
656 var files
= new Array[CodeFile]
658 # Initialize a visitor specific for a compiler engine
659 fun new_visitor
: VISITOR is abstract
661 # Where global declaration are stored (the main .h)
662 var header
: CodeWriter is writable, noinit
664 # Additionnal linker script for `ld`.
665 # Mainly used to do specific link-time symbol resolution
666 var linker_script
= new Array[String]
668 # Provide a declaration that can be requested (before or latter) by a visitor
669 fun provide_declaration
(key
: String, s
: String)
671 if self.provided_declarations
.has_key
(key
) then
672 assert self.provided_declarations
[key
] == s
674 self.provided_declarations
[key
] = s
677 private var provided_declarations
= new HashMap[String, String]
679 private var requirers_of_declarations
= new HashMap[String, ANode]
681 # Builds the .c and .h files to be used when generating a Stack Trace
682 # Binds the generated C function names to Nit function names
683 fun build_c_to_nit_bindings
685 var compile_dir
= toolchain
.compile_dir
687 var stream
= new FileWriter.open
("{compile_dir}/c_functions_hash.c")
688 stream
.write
("#include <string.h>\n")
689 stream
.write
("#include <stdlib.h>\n")
690 stream
.write
("#include \"c_functions_hash
.h\
"\n")
691 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
692 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
693 stream
.write
("char* procname = malloc(len+1);")
694 stream
.write
("memcpy(procname, procproc, len);")
695 stream
.write
("procname[len] = '\\0';")
696 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
697 for i
in names
.keys
do
699 stream.write(i.escape_to_c)
700 stream.write("\
",\"")
701 stream.write(names[i].escape_to_c)
702 stream.write("\
"\},\n")
704 stream
.write
("\};\n")
705 stream
.write
("int i;")
706 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
707 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
708 stream
.write
("free(procname);")
709 stream
.write
("return map[i].nit_name;")
712 stream
.write
("free(procname);")
713 stream
.write
("return NULL;")
717 stream
= new FileWriter.open
("{compile_dir}/c_functions_hash.h")
718 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
721 extern_bodies
.add
(new ExternCFile("c_functions_hash.c", ""))
725 # This method call compile_header_strucs method that has to be refined
726 fun compile_header
do
727 self.header
.add_decl
("#include <stdlib.h>")
728 self.header
.add_decl
("#include <stdio.h>")
729 self.header
.add_decl
("#include <string.h>")
731 self.header
.add_decl
("#include <setjmp.h>\n")
732 self.header
.add_decl
("#include <sys/types.h>\n")
733 self.header
.add_decl
("#include <unistd.h>\n")
734 self.header
.add_decl
("#include <stdint.h>\n")
735 self.header
.add_decl
("#ifdef __linux__")
736 self.header
.add_decl
(" #include <endian.h>")
737 self.header
.add_decl
("#endif")
738 self.header
.add_decl
("#include <inttypes.h>\n")
739 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
740 if modelbuilder
.toolcontext
.opt_trace
.value
then self.header
.add_decl
("#include \"traces
.h\
"")
741 self.header
.add_decl
("#ifdef __APPLE__")
742 self.header
.add_decl
(" #include <TargetConditionals.h>")
743 self.header
.add_decl
(" #include <syslog.h>")
744 self.header
.add_decl
(" #include <libkern/OSByteOrder.h>")
745 self.header
.add_decl
(" #define be32toh(x) OSSwapBigToHostInt32(x)")
746 self.header
.add_decl
("#endif")
747 self.header
.add_decl
("#ifdef _WIN32")
748 self.header
.add_decl
(" #define be32toh(val) _byteswap_ulong(val)")
749 self.header
.add_decl
("#endif")
750 self.header
.add_decl
("#ifdef ANDROID")
751 self.header
.add_decl
(" #ifndef be32toh")
752 self.header
.add_decl
(" #define be32toh(val) betoh32(val)")
753 self.header
.add_decl
(" #endif")
754 self.header
.add_decl
(" #include <android/log.h>")
755 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
756 self.header
.add_decl
("#elif TARGET_OS_IPHONE")
757 self.header
.add_decl
(" #define PRINT_ERROR(...) syslog(LOG_ERR, __VA_ARGS__)")
758 self.header
.add_decl
("#else")
759 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
760 self.header
.add_decl
("#endif")
762 compile_header_structs
763 compile_nitni_structs
766 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
767 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
768 # Signal handler function prototype
769 self.header
.add_decl
("void fatal_exit(int);")
771 self.header
.add_decl
("void fatal_exit(int) __attribute__ ((noreturn));")
774 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
775 self.header
.add_decl
("#define likely(x) (x)")
776 self.header
.add_decl
("#define unlikely(x) (x)")
777 else if gccd_disable
.has
("correct-likely") then
778 # invert the `likely` definition
779 # Used by masochists to bench the worst case
780 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
781 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
783 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
784 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
787 # Global variable used by intern methods
788 self.header
.add_decl
("extern int glob_argc;")
789 self.header
.add_decl
("extern char **glob_argv;")
790 self.header
.add_decl
("extern val *glob_sys;")
793 # Stack stocking environment for longjumps
794 protected fun compile_catch_stack
do
795 self.header
.add_decl
"""
796 struct catch_stack_t {
801 extern struct catch_stack_t *getCatchStack();
805 # Declaration of structures for live Nit types
806 protected fun compile_header_structs
is abstract
808 # Declaration of structures for nitni undelying the FFI
809 protected fun compile_nitni_structs
811 self.header
.add_decl
"""
812 /* Native reference to Nit objects */
813 /* This structure is used to represent every Nit type in extern methods and custom C code. */
815 struct nitni_ref *next,
816 *prev; /* adjacent global references in global list */
817 int count; /* number of time this global reference has been marked */
820 /* List of global references from C code to Nit objects */
821 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
822 struct nitni_global_ref_list_t {
823 struct nitni_ref *head, *tail;
825 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
827 /* Initializer of global reference list */
828 extern void nitni_global_ref_list_init();
830 /* Intern function to add a global reference to the list */
831 extern void nitni_global_ref_add( struct nitni_ref *ref );
833 /* Intern function to remove a global reference from the list */
834 extern void nitni_global_ref_remove( struct nitni_ref *ref );
836 /* Increase count on an existing global reference */
837 extern void nitni_global_ref_incr( struct nitni_ref *ref );
839 /* Decrease count on an existing global reference */
840 extern void nitni_global_ref_decr( struct nitni_ref *ref );
844 fun compile_finalizer_function
846 var finalizable_type
= mainmodule
.finalizable_type
847 if finalizable_type
== null then return
849 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
851 if finalize_meth
== null then
852 modelbuilder
.toolcontext
.error
(null, "Error: the `Finalizable` class does not declare the `finalize` method.")
856 var v
= self.new_visitor
857 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
858 var recv
= v
.new_expr
("obj", finalizable_type
)
859 v
.send
(finalize_meth
, [recv
])
863 # Hook to add specif piece of code before the the main C function.
865 # Is called by `compile_main_function`
866 fun compile_before_main
(v
: VISITOR)
870 # Hook to add specif piece of code at the begin on the main C function.
872 # Is called by `compile_main_function`
873 fun compile_begin_main
(v
: VISITOR)
877 # Generate the main C function.
881 # * allocate the Sys object if it exists
882 # * call init if is exists
883 # * call main if it exists
884 fun compile_main_function
886 var v
= self.new_visitor
887 v
.add_decl
("#include <signal.h>")
888 var platform
= target_platform
890 var no_main
= platform
.no_main
or modelbuilder
.toolcontext
.opt_no_main
.value
892 if platform
.supports_libunwind
then
893 v
.add_decl
("#ifndef NO_STACKTRACE")
894 v
.add_decl
("#define UNW_LOCAL_ONLY")
895 v
.add_decl
("#include <libunwind.h>")
896 v
.add_decl
("#include \"c_functions_hash
.h\
"")
899 v
.add_decl
("int glob_argc;")
900 v
.add_decl
("char **glob_argv;")
901 v
.add_decl
("val *glob_sys;")
903 # Store catch stack in thread local storage
905 #if defined(TARGET_OS_IPHONE)
906 // Use pthread_key_create and others for iOS
909 static pthread_key_t catch_stack_key;
910 static pthread_once_t catch_stack_key_created = PTHREAD_ONCE_INIT;
912 static void create_catch_stack()
914 pthread_key_create(&catch_stack_key, NULL);
917 struct catch_stack_t *getCatchStack()
919 pthread_once(&catch_stack_key_created, &create_catch_stack);
920 struct catch_stack_t *data = pthread_getspecific(catch_stack_key);
922 data = malloc(sizeof(struct catch_stack_t));
924 data->currentSize = 0;
926 pthread_setspecific(catch_stack_key, data);
931 // Use __thread when available
932 __thread struct catch_stack_t catchStack = {-1, 0, NULL};
934 struct catch_stack_t *getCatchStack()
941 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
942 for tag
in count_type_test_tags
do
943 v
.add_decl
("long count_type_test_resolved_{tag};")
944 v
.add_decl
("long count_type_test_unresolved_{tag};")
945 v
.add_decl
("long count_type_test_skipped_{tag};")
946 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
947 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
948 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
952 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
953 v
.add_decl
("long count_invoke_by_tables;")
954 v
.add_decl
("long count_invoke_by_direct;")
955 v
.add_decl
("long count_invoke_by_inline;")
956 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
957 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
958 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
961 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
962 v
.add_decl
("long count_attr_reads = 0;")
963 v
.add_decl
("long count_isset_checks = 0;")
964 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
965 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
968 v
.add_decl
("static void show_backtrace(void) \{")
969 if platform
.supports_libunwind
then
970 v
.add_decl
("#ifndef NO_STACKTRACE")
971 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
972 v
.add_decl
("unw_cursor_t cursor;")
973 v
.add_decl
("if(opt==NULL)\{")
974 v
.add_decl
("unw_context_t uc;")
975 v
.add_decl
("unw_word_t ip;")
976 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
977 v
.add_decl
("unw_getcontext(&uc);")
978 v
.add_decl
("unw_init_local(&cursor, &uc);")
979 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
980 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
981 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
982 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
983 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
984 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
985 v
.add_decl
(" if (recv != NULL)\{")
986 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
987 v.add_decl(" \}else\{")
988 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
991 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
992 v
.add_decl
("free(procname);")
994 v
.add_decl
("#endif /* NO_STACKTRACE */")
998 v
.add_decl
("void sig_handler(int signo)\{")
999 v
.add_decl
"#ifdef _WIN32"
1000 v
.add_decl
"PRINT_ERROR(\"Caught signal
: %s\\n\
", signo);"
1002 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
1004 v
.add_decl
("show_backtrace();")
1006 v
.add_decl
("signal(signo, SIG_DFL);")
1007 v
.add_decl
"#ifndef _WIN32"
1008 v
.add_decl
("kill(getpid(), signo);")
1012 v
.add_decl
("void fatal_exit(int status) \{")
1013 v
.add_decl
("show_backtrace();")
1014 v
.add_decl
("exit(status);")
1017 compile_before_main
(v
)
1020 v
.add_decl
("int nit_main(int argc, char** argv) \{")
1022 v
.add_decl
("int main(int argc, char** argv) \{")
1025 compile_begin_main
(v
)
1027 v
.add
"#if !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE)"
1028 v
.add
("signal(SIGABRT, sig_handler);")
1029 v
.add
("signal(SIGFPE, sig_handler);")
1030 v
.add
("signal(SIGILL, sig_handler);")
1031 v
.add
("signal(SIGINT, sig_handler);")
1032 v
.add
("signal(SIGTERM, sig_handler);")
1033 v
.add
("signal(SIGSEGV, sig_handler);")
1035 v
.add
"#ifndef _WIN32"
1036 v
.add
("signal(SIGPIPE, SIG_IGN);")
1039 v
.add
("glob_argc = argc; glob_argv = argv;")
1040 v
.add
("initialize_gc_option();")
1042 v
.add
"initialize_nitni_global_refs();"
1044 var main_type
= mainmodule
.sys_type
1045 if main_type
!= null then
1046 var mainmodule
= v
.compiler
.mainmodule
1047 var glob_sys
= v
.init_instance
(main_type
)
1048 v
.add
("glob_sys = {glob_sys};")
1049 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
.mclass
)
1050 if main_init
!= null then
1051 v
.send
(main_init
, [glob_sys
])
1053 var main_method
= mainmodule
.try_get_primitive_method
("run", main_type
.mclass
) or else
1054 mainmodule
.try_get_primitive_method
("main", main_type
.mclass
)
1055 if main_method
!= null then
1056 v
.send
(main_method
, [glob_sys
])
1060 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1061 v
.add_decl
("long count_type_test_resolved_total = 0;")
1062 v
.add_decl
("long count_type_test_unresolved_total = 0;")
1063 v
.add_decl
("long count_type_test_skipped_total = 0;")
1064 v
.add_decl
("long count_type_test_total_total = 0;")
1065 for tag
in count_type_test_tags
do
1066 v
.add_decl
("long count_type_test_total_{tag};")
1067 v
.add
("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
1068 v
.add
("count_type_test_resolved_total += count_type_test_resolved_{tag};")
1069 v
.add
("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
1070 v
.add
("count_type_test_skipped_total += count_type_test_skipped_{tag};")
1071 v
.add
("count_type_test_total_total += count_type_test_total_{tag};")
1073 v
.add
("printf(\"# dynamic count_type_test: total %l\\n\");")
1074 v
.add
("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\
");")
1075 var tags
= count_type_test_tags
.to_a
1078 v
.add
("printf(\"{tag}\
");")
1079 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
1080 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
1081 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
1082 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\\n\
", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
1086 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
1087 v
.add_decl
("long count_invoke_total;")
1088 v
.add
("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
1089 v
.add
("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
1090 v
.add
("printf(\"by table
: %ld
(%.2f
%%)\\n\
", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
1091 v
.add
("printf(\"direct
: %ld
(%.2f
%%)\\n\
", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
1092 v
.add
("printf(\"inlined
: %ld
(%.2f
%%)\\n\
", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
1095 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
1096 v
.add
("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
1097 v
.add
("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
1103 for m
in mainmodule
.in_importation
.greaters
do
1104 var f
= "FILE_"+m
.c_name
1105 v
.add
"const char {f}[] = \"{m.location.file.filename.escape_to_c}\
";"
1106 provide_declaration
(f
, "extern const char {f}[];")
1110 # Copile all C functions related to the [incr|decr]_ref features of the FFI
1111 fun compile_nitni_global_ref_functions
1113 var v
= self.new_visitor
1115 struct nitni_global_ref_list_t *nitni_global_ref_list;
1116 void initialize_nitni_global_refs() {
1117 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
1118 nitni_global_ref_list->head = NULL;
1119 nitni_global_ref_list->tail = NULL;
1122 void nitni_global_ref_add( struct nitni_ref *ref ) {
1123 if ( nitni_global_ref_list->head == NULL ) {
1124 nitni_global_ref_list->head = ref;
1127 nitni_global_ref_list->tail->next = ref;
1128 ref->prev = nitni_global_ref_list->tail;
1130 nitni_global_ref_list->tail = ref;
1135 void nitni_global_ref_remove( struct nitni_ref *ref ) {
1136 if ( ref->prev == NULL ) {
1137 nitni_global_ref_list->head = ref->next;
1139 ref->prev->next = ref->next;
1142 if ( ref->next == NULL ) {
1143 nitni_global_ref_list->tail = ref->prev;
1145 ref->next->prev = ref->prev;
1149 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
1150 if ( ref->count == 0 ) /* not registered */
1153 nitni_global_ref_add( ref );
1159 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
1160 if ( ref->count == 1 ) /* was last reference */
1162 /* remove from list */
1163 nitni_global_ref_remove( ref );
1171 # List of additional files required to compile (FFI)
1172 var extern_bodies
= new Array[ExternFile]
1174 # List of source files to copy over to the compile dir
1175 var files_to_copy
= new Array[String]
1177 # This is used to avoid adding an extern file more than once
1178 private var seen_extern
= new ArraySet[String]
1180 # Generate code that initialize the attributes on a new instance
1181 fun generate_init_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
1183 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
1184 self.mainmodule
.linearize_mclassdefs
(cds
)
1186 for npropdef
in modelbuilder
.collect_attr_propdef
(cd
) do
1187 npropdef
.init_expr
(v
, recv
)
1192 # Generate code that check if an attribute is correctly initialized
1193 fun generate_check_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
1195 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
1196 self.mainmodule
.linearize_mclassdefs
(cds
)
1198 for npropdef
in modelbuilder
.collect_attr_propdef
(cd
) do
1199 npropdef
.check_expr
(v
, recv
)
1206 var count_type_test_tags
: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
1207 var count_type_test_resolved
: HashMap[String, Int] = init_count_type_test_tags
1208 var count_type_test_unresolved
: HashMap[String, Int] = init_count_type_test_tags
1209 var count_type_test_skipped
: HashMap[String, Int] = init_count_type_test_tags
1211 protected fun init_count_type_test_tags
: HashMap[String, Int]
1213 var res
= new HashMap[String, Int]
1214 for tag
in count_type_test_tags
do
1220 # Display stats about compilation process
1224 # * type tests against resolved types (`x isa Collection[Animal]`)
1225 # * type tests against unresolved types (`x isa Collection[E]`)
1226 # * type tests skipped
1227 # * type tests total
1230 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1231 print
"# static count_type_test"
1232 print
"\tresolved:\tunresolved\tskipped\ttotal"
1233 var count_type_test_total
= init_count_type_test_tags
1234 count_type_test_resolved
["total"] = 0
1235 count_type_test_unresolved
["total"] = 0
1236 count_type_test_skipped
["total"] = 0
1237 count_type_test_total
["total"] = 0
1238 for tag
in count_type_test_tags
do
1239 count_type_test_total
[tag
] = count_type_test_resolved
[tag
] + count_type_test_unresolved
[tag
] + count_type_test_skipped
[tag
]
1240 count_type_test_resolved
["total"] += count_type_test_resolved
[tag
]
1241 count_type_test_unresolved
["total"] += count_type_test_unresolved
[tag
]
1242 count_type_test_skipped
["total"] += count_type_test_skipped
[tag
]
1243 count_type_test_total
["total"] += count_type_test_total
[tag
]
1245 var count_type_test
= count_type_test_total
["total"]
1246 var tags
= count_type_test_tags
.to_a
1250 printn
"\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
1251 printn
"\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
1252 printn
"\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
1253 printn
"\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
1259 fun finalize_ffi_for_module
(mmodule
: MModule) do mmodule
.finalize_ffi
(self)
1262 # C code file generated from the `writers` and the `required_declarations`
1264 # A file unit aims to be autonomous and is made or one or more `CodeWriter`s.
1266 # Basename of the file, will be appended by `.h` and `.c`
1269 # `CodeWriter` used in sequence to fill the top of the body, then the bottom
1270 var writers
= new Array[CodeWriter]
1272 # Required declarations keys
1274 # See: `provide_declaration`
1275 var required_declarations
= new HashSet[String]
1278 # Store generated lines
1280 # Instances are added to `file.writers` at construction.
1285 # Main lines of code (written at the bottom of the body)
1286 var lines
= new Array[String]
1288 # Lines of code for declarations (written at the top of the body)
1289 var decl_lines
= new Array[String]
1291 # Add a line in the main lines of code (to `lines`)
1292 fun add
(s
: String) do self.lines
.add
(s
)
1294 # Add a declaration line (to `decl_lines`)
1296 # Used for local and global declaration.
1297 fun add_decl
(s
: String) do self.decl_lines
.add
(s
)
1301 file
.writers
.add
(self)
1305 # A visitor on the AST of property definition that generate the C code.
1306 abstract class AbstractCompilerVisitor
1308 type COMPILER: AbstractCompiler
1310 # The associated compiler
1311 var compiler
: COMPILER
1313 # The current visited AST node
1314 var current_node
: nullable ANode = null is writable
1316 # The current `StaticFrame`
1317 var frame
: nullable StaticFrame = null is writable
1319 # Alias for self.compiler.mainmodule.object_type
1320 fun object_type
: MClassType do return self.compiler
.mainmodule
.object_type
1322 # Alias for self.compiler.mainmodule.bool_type
1323 fun bool_type
: MClassType do return self.compiler
.mainmodule
.bool_type
1325 var writer
: CodeWriter is noinit
1329 self.writer
= new CodeWriter(compiler
.files
.last
)
1332 # Force to get the primitive property named `name` in the instance `recv` or abort
1333 fun get_property
(name
: String, recv
: MType): MMethod
1335 assert recv
isa MClassType
1336 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
, name
, recv
.mclass
, self.compiler
.mainmodule
)
1339 fun compile_callsite
(callsite
: CallSite, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1341 if callsite
.is_broken
then return null
1342 var initializers
= callsite
.mpropdef
.initializers
1343 if not initializers
.is_empty
then
1344 var recv
= arguments
.first
1347 for p
in initializers
do
1348 if p
isa MMethod then
1350 for x
in p
.intro
.msignature
.mparameters
do
1351 args
.add arguments
[i
]
1355 else if p
isa MAttribute then
1356 self.write_attribute
(p
, recv
, arguments
[i
])
1360 assert i
== arguments
.length
1362 return self.send
(callsite
.mproperty
, [recv
])
1365 return self.send
(callsite
.mproperty
, arguments
)
1368 fun native_array_instance
(elttype
: MType, length
: RuntimeVariable): RuntimeVariable is abstract
1370 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable]): Bool do return false
1372 # Return an element of a native array.
1373 # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays
1374 fun native_array_get
(native_array
: RuntimeVariable, index
: Int): RuntimeVariable is abstract
1376 # Store an element in a native array.
1377 # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays
1378 fun native_array_set
(native_array
: RuntimeVariable, index
: Int, value
: RuntimeVariable) is abstract
1380 # Allocate `size` bytes with the low_level `nit_alloc` C function
1382 # This method can be redefined to inject statistic or tracing code.
1384 # `tag` if any, is used to mark the class of the allocated object.
1385 fun nit_alloc
(size
: String, tag
: nullable String): String
1387 return "nit_alloc({size})"
1390 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
1391 # This method is used to manage varargs in signatures and returns the real array
1392 # of runtime variables to use in the call.
1393 fun varargize
(mpropdef
: MMethodDef, map
: nullable SignatureMap, recv
: RuntimeVariable, args
: SequenceRead[AExpr]): Array[RuntimeVariable]
1395 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
1396 var res
= new Array[RuntimeVariable]
1399 if msignature
.arity
== 0 then return res
1402 assert args
.length
== msignature
.arity
1404 res
.add
self.expr
(ne
, null)
1409 # Eval in order of arguments, not parameters
1410 var exprs
= new Array[RuntimeVariable].with_capacity
(args
.length
)
1412 exprs
.add
self.expr
(ne
, null)
1415 # Fill `res` with the result of the evaluation according to the mapping
1416 for i
in [0..msignature
.arity
[ do
1417 var param
= msignature
.mparameters
[i
]
1418 var j
= map
.map
.get_or_null
(i
)
1421 res
.add
(null_instance
)
1424 if param
.is_vararg
and args
[i
].vararg_decl
> 0 then
1425 var vararg
= exprs
.sub
(j
, args
[i
].vararg_decl
)
1426 var elttype
= param
.mtype
1427 var arg
= self.vararg_instance
(mpropdef
, recv
, vararg
, elttype
)
1438 # Anchor a type to the main module and the current receiver
1439 fun anchor
(mtype
: MType): MType
1441 if not mtype
.need_anchor
then return mtype
1442 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
1445 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
1447 if not mtype
.need_anchor
then return mtype
1448 return mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
1451 # Unsafely cast a value to a new type
1452 # ie the result share the same C variable but my have a different mcasttype
1453 # NOTE: if the adaptation is useless then `value` is returned as it.
1454 # ENSURE: `result.name == value.name`
1455 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1457 mtype
= self.anchor
(mtype
)
1458 var valmtype
= value
.mcasttype
1460 # CPrimitive is the best you can do
1461 if valmtype
.is_c_primitive
then
1465 # Do nothing if useless autocast
1466 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1470 # Just as_not_null if the target is not nullable.
1472 # eg `nullable PreciseType` adapted to `Object` gives precisetype.
1473 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1474 mtype
= valmtype
.mtype
1477 var res
= new RuntimeVariable(value
.name
, value
.mtype
, mtype
)
1481 # Generate a super call from a method definition
1482 fun supercall
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1484 # Adapt the arguments of a method according to targetted `MMethodDef`
1485 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
1487 # Unbox all the arguments of a method when implemented `extern` or `intern`
1488 fun unbox_signature_extern
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
1490 # Box or unbox a value to another type iff a C type conversion is needed
1491 # ENSURE: `result.mtype.ctype == mtype.ctype`
1492 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1494 # Box extern classes to be used in the generated code
1495 fun box_extern
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1497 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1498 fun unbox_extern
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1500 # Generate a polymorphic subtype test
1501 fun type_test
(value
: RuntimeVariable, mtype
: MType, tag
: String): RuntimeVariable is abstract
1503 # Generate the code required to dynamically check if 2 objects share the same runtime type
1504 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
1506 # Generate a Nit "is" for two runtime_variables
1507 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
1511 # Generate a static call on a method definition
1512 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1514 # Generate a polymorphic send for the method `m` and the arguments `args`
1515 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1517 # Generate a monomorphic send for the method `m`, the type `t` and the arguments `args`
1518 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1520 assert t
isa MClassType
1521 var propdef
= m
.lookup_first_definition
(self.compiler
.mainmodule
, t
)
1522 return self.call
(propdef
, t
, args
)
1525 # Generate a monomorphic super send from the method `m`, the type `t` and the arguments `args`
1526 fun monomorphic_super_send
(m
: MMethodDef, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1528 assert t
isa MClassType
1529 m
= m
.lookup_next_definition
(self.compiler
.mainmodule
, t
)
1530 return self.call
(m
, t
, args
)
1533 # Attributes handling
1535 # Generate a polymorphic attribute is_set test
1536 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
1538 # Generate a polymorphic attribute read
1539 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
1541 # Generate a polymorphic attribute write
1542 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable) is abstract
1546 # Can value be null? (according to current knowledge)
1547 fun maybe_null
(value
: RuntimeVariable): Bool
1549 return value
.mcasttype
isa MNullableType or value
.mcasttype
isa MNullType
1552 # Add a check and an abort for a null receiver if needed
1553 fun check_recv_notnull
(recv
: RuntimeVariable)
1555 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_null
.value
then return
1557 if maybe_null
(recv
) then
1558 self.add
("if (unlikely({recv} == NULL)) \{")
1559 self.add_abort
("Receiver is null")
1566 private var names
= new HashSet[String]
1567 private var last
: Int = 0
1569 # Return a new name based on `s` and unique in the visitor
1570 fun get_name
(s
: String): String
1572 if not self.names
.has
(s
) then
1576 var i
= self.last
+ 1
1579 if not self.names
.has
(s2
) then
1588 # Return an unique and stable identifier associated with an escapemark
1589 fun escapemark_name
(e
: nullable EscapeMark): String
1592 if frame
.escapemark_names
.has_key
(e
) then return frame
.escapemark_names
[e
]
1594 if name
== null then name
= "label"
1595 name
= get_name
(name
)
1596 frame
.escapemark_names
[e
] = name
1600 # Insert a C label for associated with an escapemark
1601 fun add_escape_label
(e
: nullable EscapeMark)
1603 if e
== null then return
1604 if e
.escapes
.is_empty
then return
1605 add
("BREAK_{escapemark_name(e)}: (void)0;")
1608 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1609 # NOTE: we do not return a `RuntimeVariable` "CString" as the class may not exist in the module/program
1610 fun class_name_string
(value
: RuntimeVariable): String is abstract
1612 # Variables handling
1614 protected var variables
= new HashMap[Variable, RuntimeVariable]
1616 # Return the local runtime_variable associated to a Nit local variable
1617 fun variable
(variable
: Variable): RuntimeVariable
1619 if self.variables
.has_key
(variable
) then
1620 return self.variables
[variable
]
1622 var name
= self.get_name
("var_{variable.name}")
1623 var mtype
= variable
.declared_type
.as(not null)
1624 mtype
= self.anchor
(mtype
)
1625 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1626 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1627 self.variables
[variable
] = res
1632 # Return a new uninitialized local runtime_variable
1633 fun new_var
(mtype
: MType): RuntimeVariable
1635 mtype
= self.anchor
(mtype
)
1636 var name
= self.get_name
("var")
1637 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1638 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1642 # The difference with `new_var` is the C static type of the local variable
1643 fun new_var_extern
(mtype
: MType): RuntimeVariable
1645 mtype
= self.anchor
(mtype
)
1646 var name
= self.get_name
("var")
1647 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1648 self.add_decl
("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1652 # Return a new uninitialized named runtime_variable
1653 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
1655 mtype
= self.anchor
(mtype
)
1656 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1657 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1661 # Correctly assign a left and a right value
1662 # Boxing and unboxing is performed if required
1663 fun assign
(left
, right
: RuntimeVariable)
1665 right
= self.autobox
(right
, left
.mtype
)
1666 self.add
("{left} = {right};")
1669 # Generate instances
1671 # Generate a alloc-instance + init-attributes
1672 fun init_instance
(mtype
: MClassType): RuntimeVariable is abstract
1674 # Allocate and init attributes of an instance of a standard or extern class
1676 # Does not support universals and the pseudo-internal `NativeArray` class.
1677 fun init_instance_or_extern
(mtype
: MClassType): RuntimeVariable
1680 var ctype
= mtype
.ctype
1681 assert mtype
.mclass
.name
!= "NativeArray"
1682 if not mtype
.is_c_primitive
then
1683 recv
= init_instance
(mtype
)
1684 else if ctype
== "char*" then
1685 recv
= new_expr
("NULL/*special!*/", mtype
)
1687 recv
= new_expr
("({ctype})0/*special!*/", mtype
)
1692 # Set a GC finalizer on `recv`, only if `recv` isa Finalizable
1693 fun set_finalizer
(recv
: RuntimeVariable)
1695 var mtype
= recv
.mtype
1696 var finalizable_type
= compiler
.mainmodule
.finalizable_type
1697 if finalizable_type
!= null and not mtype
.need_anchor
and
1698 mtype
.is_subtype
(compiler
.mainmodule
, null, finalizable_type
) then
1699 add
"gc_register_finalizer({recv});"
1703 # The currently processed module
1705 # alias for `compiler.mainmodule`
1706 fun mmodule
: MModule do return compiler
.mainmodule
1708 # Generate an integer value
1709 fun int_instance
(value
: Int): RuntimeVariable
1711 var t
= mmodule
.int_type
1712 var res
= new RuntimeVariable("{value.to_s}l", t
, t
)
1716 # Generate a byte value
1717 fun byte_instance
(value
: Byte): RuntimeVariable
1719 var t
= mmodule
.byte_type
1720 var res
= new RuntimeVariable("((unsigned char){value.to_s})", t
, t
)
1724 # Generate an int8 value
1725 fun int8_instance
(value
: Int8): RuntimeVariable
1727 var t
= mmodule
.int8_type
1728 var res
= new RuntimeVariable("INT8_C({value.to_s})", t
, t
)
1732 # Generate an int16 value
1733 fun int16_instance
(value
: Int16): RuntimeVariable
1735 var t
= mmodule
.int16_type
1736 var res
= new RuntimeVariable("INT16_C({value.to_s})", t
, t
)
1740 # Generate a uint16 value
1741 fun uint16_instance
(value
: UInt16): RuntimeVariable
1743 var t
= mmodule
.uint16_type
1744 var res
= new RuntimeVariable("UINT16_C({value.to_s})", t
, t
)
1748 # Generate an int32 value
1749 fun int32_instance
(value
: Int32): RuntimeVariable
1751 var t
= mmodule
.int32_type
1752 var res
= new RuntimeVariable("INT32_C({value.to_s})", t
, t
)
1756 # Generate a uint32 value
1757 fun uint32_instance
(value
: UInt32): RuntimeVariable
1759 var t
= mmodule
.uint32_type
1760 var res
= new RuntimeVariable("UINT32_C({value.to_s})", t
, t
)
1764 # Generate a char value
1765 fun char_instance
(value
: Char): RuntimeVariable
1767 var t
= mmodule
.char_type
1769 if value
.code_point
< 128 then
1770 return new RuntimeVariable("'{value.to_s.escape_to_c}'", t
, t
)
1772 return new RuntimeVariable("{value.code_point}", t
, t
)
1776 # Generate a float value
1778 # FIXME pass a Float, not a string
1779 fun float_instance
(value
: Float): RuntimeVariable
1781 var t
= mmodule
.float_type
1782 var res
= new RuntimeVariable("{value.to_hexa_exponential_notation}", t
, t
)
1786 # Generate an integer value
1787 fun bool_instance
(value
: Bool): RuntimeVariable
1789 var s
= if value
then "1" else "0"
1790 var res
= new RuntimeVariable(s
, bool_type
, bool_type
)
1794 # Generate the `null` value
1795 fun null_instance
: RuntimeVariable
1797 var t
= compiler
.mainmodule
.model
.null_type
1798 var res
= new RuntimeVariable("((val*)NULL)", t
, t
)
1802 # Generates a CString instance fully escaped in C-style \xHH fashion
1803 fun c_string_instance
(ns
: CString, len
: Int): RuntimeVariable do
1804 var mtype
= mmodule
.c_string_type
1805 var nat
= new_var
(mtype
)
1806 var byte_esc
= new Buffer.with_cap
(len
* 4)
1807 for i
in [0 .. len
[ do
1808 byte_esc
.append
("\\x{ns[i].to_hex}")
1810 self.add
("{nat} = \"{byte_esc}\
";")
1814 # Generate a string value
1815 fun string_instance
(string
: String): RuntimeVariable
1817 var mtype
= mmodule
.string_type
1818 var name
= self.get_name
("varonce")
1819 self.add_decl
("static {mtype.ctype} {name};")
1820 var res
= self.new_var
(mtype
)
1821 self.add
("if (likely({name}!=NULL)) \{")
1822 self.add
("{res} = {name};")
1823 self.add
("\} else \{")
1824 var native_mtype
= mmodule
.c_string_type
1825 var nat
= self.new_var
(native_mtype
)
1826 self.add
("{nat} = \"{string.escape_to_c}\
";")
1827 var byte_length
= self.int_instance
(string
.byte_length
)
1828 var unilen
= self.int_instance
(string
.length
)
1829 self.add
("{res} = {self.send(self.get_property("to_s_unsafe", native_mtype), [nat, byte_length, unilen, value_instance(false), value_instance(false)]).as(not null)};")
1830 self.add
("{name} = {res};")
1835 fun value_instance
(object
: Object): RuntimeVariable
1837 if object
isa Int then
1838 return int_instance
(object
)
1839 else if object
isa Bool then
1840 return bool_instance
(object
)
1841 else if object
isa String then
1842 return string_instance
(object
)
1848 # Generate an array value
1849 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
1851 # Get an instance of a array for a vararg
1852 fun vararg_instance
(mpropdef
: MPropDef, recv
: RuntimeVariable, varargs
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
1856 # Add a line in the main part of the generated C
1857 fun add
(s
: String) do self.writer
.lines
.add
(s
)
1860 # (used for local or global declaration)
1861 fun add_decl
(s
: String) do self.writer
.decl_lines
.add
(s
)
1863 # Request the presence of a global declaration
1864 fun require_declaration
(key
: String)
1866 var reqs
= self.writer
.file
.required_declarations
1867 if reqs
.has
(key
) then return
1869 var node
= current_node
1870 if node
!= null then compiler
.requirers_of_declarations
[key
] = node
1873 # Add a declaration in the local-header
1874 # The declaration is ensured to be present once
1875 fun declare_once
(s
: String)
1877 self.compiler
.provide_declaration
(s
, s
)
1878 self.require_declaration
(s
)
1881 # Look for a needed .h and .c file for a given module
1882 # This is used for the legacy FFI
1883 fun add_extern
(mmodule
: MModule)
1885 var file
= mmodule
.filepath
1886 file
= file
.strip_extension
(".nit")
1887 var tryfile
= file
+ ".nit.h"
1888 if tryfile
.file_exists
then
1889 self.declare_once
("#include \"{tryfile.basename}\
"")
1890 self.compiler
.files_to_copy
.add
(tryfile
)
1892 tryfile
= file
+ "_nit.h"
1893 if tryfile
.file_exists
then
1894 self.declare_once
("#include \"{tryfile.basename}\
"")
1895 self.compiler
.files_to_copy
.add
(tryfile
)
1898 if self.compiler
.seen_extern
.has
(file
) then return
1899 self.compiler
.seen_extern
.add
(file
)
1900 tryfile
= file
+ ".nit.c"
1901 if not tryfile
.file_exists
then
1902 tryfile
= file
+ "_nit.c"
1903 if not tryfile
.file_exists
then return
1905 var f
= new ExternCFile(tryfile
.basename
, "")
1906 self.compiler
.extern_bodies
.add
(f
)
1907 self.compiler
.files_to_copy
.add
(tryfile
)
1910 # Return a new local runtime_variable initialized with the C expression `cexpr`.
1911 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
1913 var res
= new_var
(mtype
)
1914 self.add
("{res} = {cexpr};")
1918 # Generate generic abort
1919 # used by aborts, asserts, casts, etc.
1920 fun add_abort
(message
: String)
1923 self.add
("PRINT_ERROR(\"Runtime error
: %s\
", \"{message.escape_to_c}\
");")
1927 # Generate a long jump if there is a catch block.
1929 # This method should be called before the error messages and before a `add_raw_abort`.
1933 self.add
("struct catch_stack_t *catchStack = getCatchStack();")
1934 self.add
("if(catchStack->cursor >= 0)\{")
1935 self.add
(" longjmp(catchStack->envs[catchStack->cursor], 1);")
1940 # Generate abort without a message.
1942 # Used when one need a more complex message.
1943 # Do not forget to call `add_raw_abort` before the display of a custom user message.
1946 var current_node
= self.current_node
1947 if current_node
!= null and current_node
.location
.file
!= null and
1948 current_node
.location
.file
.mmodule
!= null then
1949 var f
= "FILE_{self.current_node.location.file.mmodule.c_name}"
1950 self.require_declaration
(f
)
1951 self.add
("PRINT_ERROR(\" (%s
:%d
)\\n\
", {f}, {current_node.location.line_start});")
1953 self.add
("PRINT_ERROR(\"\\n\
");")
1955 self.add
("fatal_exit(1);")
1958 # Add a dynamic cast
1959 fun add_cast
(value
: RuntimeVariable, mtype
: MType, tag
: String)
1961 var res
= self.type_test
(value
, mtype
, tag
)
1962 self.add
("if (unlikely(!{res})) \{")
1964 var cn
= self.class_name_string
(value
)
1965 self.add
("PRINT_ERROR(\"Runtime error
: Cast failed
. Expected `%s`, got
`%s`\
", \"{mtype.to_s.escape_to_c}\
", {cn});")
1970 # Generate a return with the value `s`
1971 fun ret
(s
: RuntimeVariable)
1973 self.assign
(self.frame
.returnvar
.as(not null), s
)
1974 self.add
("goto {self.frame.returnlabel.as(not null)};")
1977 # Compile a statement (if any)
1978 fun stmt
(nexpr
: nullable AExpr)
1980 if nexpr
== null then return
1981 if nexpr
.is_broken
then
1982 # Untyped expression.
1983 # Might mean dead code or invalid code
1985 add_abort
("FATAL: bad statement executed.")
1989 var narray
= nexpr
.comprehension
1990 if narray
!= null then
1991 var recv
= frame
.comprehension
.as(not null)
1992 var val
= expr
(nexpr
, narray
.element_mtype
)
1993 compile_callsite
(narray
.push_callsite
.as(not null), [recv
, val
])
1997 var old
= self.current_node
1998 self.current_node
= nexpr
2000 self.current_node
= old
2003 # Compile an expression an return its result
2004 # `mtype` is the expected return type, pass null if no specific type is expected.
2005 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
2007 var old
= self.current_node
2008 self.current_node
= nexpr
2011 if nexpr
.mtype
!= null then
2012 res
= nexpr
.expr
(self)
2016 # Untyped expression.
2017 # Might mean dead code or invalid code.
2019 add_abort
("FATAL: bad expression executed.")
2020 # and return a placebo result to please the C compiler
2021 if mtype
== null then mtype
= compiler
.mainmodule
.object_type
2022 res
= new_var
(mtype
)
2024 self.current_node
= old
2028 if mtype
!= null then
2029 mtype
= self.anchor
(mtype
)
2030 res
= self.autobox
(res
, mtype
)
2032 res
= autoadapt
(res
, nexpr
.mtype
.as(not null))
2033 var implicit_cast_to
= nexpr
.implicit_cast_to
2034 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
2035 add_cast
(res
, implicit_cast_to
, "auto")
2036 res
= autoadapt
(res
, implicit_cast_to
)
2038 self.current_node
= old
2042 # Alias for `self.expr(nexpr, self.bool_type)`
2043 fun expr_bool
(nexpr
: AExpr): RuntimeVariable do return expr
(nexpr
, bool_type
)
2045 # Safely show a debug message on the current node and repeat the message in the C code as a comment
2046 fun debug
(message
: String)
2048 var node
= self.current_node
2049 if node
== null then
2050 print
"?: {message}"
2054 self.add
("/* DEBUG: {message} */")
2058 # A C function associated to a Nit method
2059 # This is the base class for all runtime representation of a nit method.
2060 # It implements the Template Design Pattern whose steps are :
2061 # 1. create the receiver `RuntimeVariable` (selfvar)
2062 # 2. create a `StaticFrame`
2063 # 3. resolve the return type.
2064 # 4. write the function signature
2065 # 5. Declare the function signature (for C header files)
2066 # 6. writer the function body
2067 # 7. post-compiler hook (optional)
2068 # Each step is called in `AbstractRuntimeFunction::compile_to_c`. A default
2069 # body is provided foreach step except for compilation hooks. Subclasses may
2070 # redefine any of the above mentioned steps. However, it's not recommanded
2071 # to override `compile_to_c` since there's an ordering of the compilation steps.
2072 # Any information between steps must be saved in the visitor. Currently most
2073 # of the future runtime info are stored in the `StaticFrame` of the visitor,
2074 # e.g. the receiver (selfvar), the arguments, etc.
2076 # Moreover, any subclass may redefine : the receiver type, the return type and
2077 # the signature. This allow for a better customization for the final implementation.
2078 # Because of customization, a given Nit method can be compile more that once.
2079 abstract class AbstractRuntimeFunction
2081 type COMPILER: AbstractCompiler
2082 type VISITOR: AbstractCompilerVisitor
2084 # The associated Nit method
2085 var mmethoddef
: MMethodDef
2087 protected var return_mtype
: nullable MType = null
2089 # The mangled c name of the runtime_function
2090 # Subclasses should redefine `build_c_name` instead
2093 var res
= self.c_name_cache
2094 if res
!= null then return res
2095 res
= self.build_c_name
2096 self.c_name_cache
= res
2100 fun c_ref
: String do return "&{c_name}"
2102 # Non cached version of `c_name`
2103 protected fun build_c_name
: String is abstract
2105 protected var c_name_cache
: nullable String = null is writable
2107 # Implements a call of the runtime_function
2108 # May inline the body or generate a C function call
2109 fun call
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
2111 # Returns `true` if the associated `mmethoddef`'s return type isn't null,
2112 # otherwise `false`.
2113 fun has_return
: Bool
2115 return mmethoddef
.msignature
.return_mtype
!= null
2118 # The current msignature to use when compiling : `signature_to_c` and `body_to_c`.
2119 # This method is useful since most concrete implementation doesn't use the
2120 # mmethoddef's signature. By providing a definition in the abstract class,
2121 # subclasses can use any msignature.
2122 fun msignature
: MSignature
2124 return mmethoddef
.msignature
.as(not null)
2127 # The current receiver type to compile : `signature_to_c` and `body_to_c`.
2128 # See `msignature` method for more information.
2129 protected fun recv_mtype
: MType
2131 return mmethoddef
.mclassdef
.bound_mtype
2134 # Prepare the `self` runtime variable to be used by the rest of
2135 # compilation steps.
2137 protected fun resolve_receiver
(v
: VISITOR): RuntimeVariable
2139 var casttype
= mmethoddef
.mclassdef
.bound_mtype
2140 return new RuntimeVariable("self", recv_mtype
, casttype
)
2143 # Builds the static frame for current runtime method
2145 protected fun build_frame
(v
: VISITOR, arguments
: Array[RuntimeVariable]): StaticFrame
2147 return new StaticFrame(v
, mmethoddef
, recv_mtype
.as(MClassType), arguments
)
2150 # Step 3 : Returns the return type used by the runtime function.
2151 protected fun resolve_return_mtype
(v
: VISITOR)
2153 return_mtype
= msignature
.return_mtype
2156 # Fills the argument array inside v.frame.arguments, calling `resolve_ith_parameter`
2157 # for each parameter.
2158 private fun fill_parameters
(v
: VISITOR)
2160 assert v
.frame
!= null
2161 for i
in [0..msignature
.arity
[ do
2162 var arg
= resolve_ith_parameter
(v
, i
)
2163 v
.frame
.arguments
.add
(arg
)
2167 # Step 4 : Creates `RuntimeVariable` for each method argument.
2168 protected fun resolve_ith_parameter
(v
: VISITOR, i
: Int): RuntimeVariable
2170 var mp
= msignature
.mparameters
[i
]
2171 var mtype
= mp
.mtype
2172 if mp
.is_vararg
then
2173 mtype
= v
.mmodule
.array_type
(mtype
)
2175 return new RuntimeVariable("p{i}", mtype
, mtype
)
2178 # Generate the code for the signature with an open curly brace
2180 # Returns the generated signature without a semicolon and curly brace,
2181 # e.g `RES f(T0 p0, T1 p1, ..., TN pN)`
2183 protected fun signature_to_c
(v
: VISITOR): String
2185 assert v
.frame
!= null
2186 var arguments
= v
.frame
.arguments
2187 var comment
= new FlatBuffer
2188 var selfvar
= v
.frame
.selfvar
2191 c_ret
= "{return_mtype.ctype}"
2193 var sig
= new FlatBuffer
2194 sig
.append
("{c_ret} {c_name}({recv_mtype.ctype} self")
2195 comment
.append
("({selfvar}: {selfvar.mtype}")
2197 for i
in [0..arguments
.length-1
[ do
2199 var arg
= arguments
[i
+1]
2200 comment
.append
(", {arg.mtype}")
2201 sig
.append
(", {arg.mtype.ctype} p{i}")
2206 comment
.append
(": {return_mtype.as(not null)}")
2208 v
.add_decl
("/* method {self} for {comment} */")
2209 v
.add_decl
("{sig} \{")
2213 # How the concrete compiler will declare the method, e.g inside a global header file,
2214 # extern signature, etc.
2216 protected fun declare_signature
(v
: VISITOR, signature
: String) is abstract
2218 # Generate the code for the body without return statement at the end and
2221 protected fun body_to_c
(v
: VISITOR)
2223 mmethoddef
.compile_inside_to_c
(v
, v
.frame
.arguments
)
2226 # Hook called at the end of `compile_to_c` function. This function
2227 # is useful if you need to register any function compiled to c.
2228 # Step 8 (optional).
2229 protected fun end_compile_to_c
(v
: VISITOR)
2231 # Nothing to do by default
2235 fun compile_to_c
(compiler
: COMPILER)
2237 var v
= compiler
.new_visitor
2238 var selfvar
= resolve_receiver
(v
)
2239 var arguments
= [selfvar
]
2240 var frame
= build_frame
(v
, arguments
)
2243 resolve_return_mtype
(v
)
2246 # WARNING: the signature must be declared before creating
2247 # any returnlabel and returnvar (`StaticFrame`). Otherwise,
2248 # you could end up with variable outside the function.
2249 var sig
= signature_to_c
(v
)
2250 declare_signature
(v
, sig
)
2252 frame
.returnlabel
= v
.get_name
("RET_LABEL")
2254 var ret_mtype
= return_mtype
2255 assert ret_mtype
!= null
2256 frame
.returnvar
= v
.new_var
(ret_mtype
)
2260 v
.add
("{frame.returnlabel.as(not null)}:;")
2262 v
.add
("return {frame.returnvar.as(not null)};")
2269 # Base class for all thunk-like function. A thunk is a function whose purpose
2270 # is to call another function. Like a class wrapper or decorator, a thunk is used
2271 # to computer things (conversions, log, etc) before or after a function call. It's
2272 # an intermediary between the caller and the callee.
2274 # The most basic use of a thunk is to unbox its argument before invoking the real callee.
2275 # Virtual functions are a use case of thunk function:
2278 # redef class Object
2279 # fun toto(x: Int): Int do return 1 + x
2282 # redef fun toto(x) do return x + self
2285 # ```generated C code
2286 # long Object__toto(val* self, long x) { return 1 + x }
2287 # long Int__toto(long self, long x) { return x + self }
2288 # long THUNK_Int__toto(val* self, long x) {
2289 # long self2 = (long)(self)>>2 // Unboxing from Object to Int
2290 # return Int_toto(self2, x)
2296 # A thunk has its OWN SIGNATURE and is considered like any other `AbstractRuntimeFunction`.
2297 # Thus, one might need to be careful when overriding parent's method. Since most usage of
2298 # thunks in Nit is to unbox and box things between a caller and a callee, the default
2299 # implementation is doing just that. In other words, a thunk takes a signature and a method
2300 # and tries to cast its signature to the underlying method's signature.
2302 # A virtual function has the same signature as its `mmethoddef` field, except for
2303 # its receiver is of type `Object`.
2304 # In the same vibe, a call reference has all of its argument boxed as `Object`.
2305 abstract class ThunkFunction
2306 super AbstractRuntimeFunction
2308 # Determines if the callsite should be polymorphic or static.
2309 var polymorph_call_flag
= false is writable
2311 # The type expected by the callee. Used to resolve `mmethoddef`'s formal
2312 # parameters and virtual type. This type must NOT need anchor.
2313 fun target_recv
: MType is abstract
2315 redef fun body_to_c
(v
)
2317 assert not target_recv
.need_anchor
2319 assert frame
!= null
2320 var selfvar
= frame
.selfvar
2321 var arguments
= frame
.arguments
2322 var arguments2
= new Array[RuntimeVariable]
2323 arguments2
.push
(v
.autobox
(selfvar
, target_recv
))
2324 var resolved_sig
= msignature
.resolve_for
(target_recv
, target_recv
.as(MClassType), v
.mmodule
, true)
2325 for i
in [0..resolved_sig
.arity
[ do
2326 var param
= resolved_sig
.mparameters
[i
]
2327 var mtype
= param
.mtype
2328 if param
.is_vararg
then
2329 mtype
= v
.mmodule
.array_type
(mtype
)
2331 var temp
= v
.autobox
(arguments
[i
+1], mtype
)
2332 arguments2
.push
(temp
)
2334 v
.add
("/* {mmethoddef}, {recv_mtype.ctype} */")
2335 var subret
: nullable RuntimeVariable = null
2336 if polymorph_call_flag
then
2337 subret
= v
.send
(mmethoddef
.mproperty
, arguments2
)
2339 subret
= v
.call
(mmethoddef
, arguments2
[0].mcasttype
.as(MClassType), arguments2
)
2342 assert subret
!= null
2343 var subret2
= v
.autobox
(subret
, return_mtype
.as(not null))
2344 v
.assign
(frame
.returnvar
.as(not null), subret2
)
2351 # A runtime variable hold a runtime value in C.
2352 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
2354 # 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.
2355 class RuntimeVariable
2356 # The name of the variable in the C code
2359 # The static type of the variable (as declard in C)
2362 # The current casted type of the variable (as known in Nit)
2363 var mcasttype
: MType is writable
2365 # If the variable exaclty a mcasttype?
2366 # false (usual value) means that the variable is a mcasttype or a subtype.
2367 var is_exact
: Bool = false is writable
2371 assert not mtype
.need_anchor
2372 assert not mcasttype
.need_anchor
2375 redef fun to_s
do return name
2380 if self.is_exact
then
2381 exact_str
= " exact"
2386 if self.mtype
== self.mcasttype
then
2387 type_str
= "{mtype}{exact_str}"
2389 type_str
= "{mtype}({mcasttype}{exact_str})"
2391 return "<{name}:{type_str}>"
2395 # The static context of a visited property in a `AbstractCompilerVisitor`
2398 type VISITOR: AbstractCompilerVisitor
2400 # The associated visitor
2401 var visitor
: VISITOR
2403 # The executed property.
2404 # A Method in case of a call, an attribute in case of a default initialization.
2405 var mpropdef
: MPropDef
2407 # The static type of the receiver
2408 var receiver
: MClassType
2410 # Arguments of the method (the first is the receiver)
2411 var arguments
: Array[RuntimeVariable]
2413 # The runtime_variable associated to the return (in a function)
2414 var returnvar
: nullable RuntimeVariable = null is writable
2416 # The label at the end of the property
2417 var returnlabel
: nullable String = null is writable
2419 # Labels associated to a each escapemarks.
2420 # Because of inlinings, escape-marks must be associated to their context (the frame)
2421 private var escapemark_names
= new HashMap[EscapeMark, String]
2423 # The array comprehension currently filled, if any
2424 private var comprehension
: nullable RuntimeVariable = null
2426 # Returns the first argument (the receiver) of a frame.
2427 # REQUIRE: arguments.length >= 1
2428 fun selfvar
: RuntimeVariable
2430 assert arguments
.length
>= 1
2431 return arguments
.first
2436 # Return the C type associated to a given Nit static type
2437 fun ctype
: String do return "val*"
2439 # C type outside of the compiler code and in boxes
2440 fun ctype_extern
: String do return "val*"
2442 # Short name of the `ctype` to use in unions
2443 fun ctypename
: String do return "val"
2445 # Is the associated C type a primitive one?
2447 # ENSURE `result == (ctype != "val*")`
2448 fun is_c_primitive
: Bool do return false
2451 redef class MClassType
2453 redef var ctype
is lazy
do
2454 if mclass
.name
== "Int" then
2456 else if mclass
.name
== "Bool" then
2458 else if mclass
.name
== "Char" then
2460 else if mclass
.name
== "Float" then
2462 else if mclass
.name
== "Int8" then
2464 else if mclass
.name
== "Byte" then
2465 return "unsigned char"
2466 else if mclass
.name
== "Int16" then
2468 else if mclass
.name
== "UInt16" then
2470 else if mclass
.name
== "Int32" then
2472 else if mclass
.name
== "UInt32" then
2474 else if mclass
.name
== "CString" then
2476 else if mclass
.name
== "NativeArray" then
2483 redef var is_c_primitive
is lazy
do return ctype
!= "val*"
2485 redef fun ctype_extern
: String
2487 if mclass
.kind
== extern_kind
then
2494 redef fun ctypename
: String
2496 if mclass
.name
== "Int" then
2498 else if mclass
.name
== "Bool" then
2500 else if mclass
.name
== "Char" then
2502 else if mclass
.name
== "Float" then
2504 else if mclass
.name
== "Int8" then
2506 else if mclass
.name
== "Byte" then
2508 else if mclass
.name
== "Int16" then
2510 else if mclass
.name
== "UInt16" then
2512 else if mclass
.name
== "Int32" then
2514 else if mclass
.name
== "UInt32" then
2516 else if mclass
.name
== "CString" then
2518 else if mclass
.name
== "NativeArray" then
2519 #return "{self.arguments.first.ctype}*"
2527 redef class MPropDef
2528 type VISITOR: AbstractCompilerVisitor
2531 redef class MMethodDef
2532 # Can the body be inlined?
2533 fun can_inline
(v
: VISITOR): Bool
2535 if is_abstract
then return true
2536 if constant_value
!= null then return true
2537 var modelbuilder
= v
.compiler
.modelbuilder
2538 var node
= modelbuilder
.mpropdef2node
(self)
2539 if node
isa APropdef then
2540 return node
.can_inline
2541 else if node
isa AClassdef then
2542 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
2544 else if node
== null then
2551 # Inline the body in another visitor
2552 fun compile_inside_to_c
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
2554 var modelbuilder
= v
.compiler
.modelbuilder
2555 var val
= constant_value
2556 var node
= modelbuilder
.mpropdef2node
(self)
2560 var cn
= v
.class_name_string
(arguments
.first
)
2561 v
.current_node
= node
2562 v
.add
("PRINT_ERROR(\"Runtime error
: Abstract method
`%s` called on
`%s`\
", \"{mproperty.name.escape_to_c}\
", {cn});")
2567 if node
isa APropdef then
2568 var oldnode
= v
.current_node
2569 v
.current_node
= node
2570 self.compile_parameter_check
(v
, arguments
)
2571 node
.compile_to_c
(v
, self, arguments
)
2572 v
.current_node
= oldnode
2573 else if node
isa AClassdef then
2574 var oldnode
= v
.current_node
2575 v
.current_node
= node
2576 self.compile_parameter_check
(v
, arguments
)
2577 node
.compile_to_c
(v
, self, arguments
)
2578 v
.current_node
= oldnode
2579 else if val
!= null then
2580 v
.ret
(v
.value_instance
(val
))
2587 # Generate type checks in the C code to check covariant parameters
2588 fun compile_parameter_check
(v
: VISITOR, arguments
: Array[RuntimeVariable])
2590 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_covariance
.value
then return
2592 var msignature
= self.msignature
.as(not null)
2594 for i
in [0..msignature
.arity
[ do
2595 var mp
= msignature
.mparameters
[i
]
2596 # skip test for vararg since the array is instantiated with the correct polymorphic type
2597 if mp
.is_vararg
then continue
2599 # skip if the cast is not required
2600 var origmtype
= self.mproperty
.intro
.msignature
.mparameters
[i
].mtype
2601 if not origmtype
.need_anchor
then continue
2603 # get the parameter type
2604 var mtype
= mp
.mtype
2607 # note that v decides if and how to implements the cast
2608 v
.add
("/* Covariant cast for argument {i} ({mp.name}) {arguments[i+1].inspect} isa {mtype} */")
2609 v
.add_cast
(arguments
[i
+1], mtype
, "covariance")
2616 redef class APropdef
2617 fun compile_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
2619 v
.add
("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
2620 debug
("Not yet implemented")
2623 fun can_inline
: Bool do return true
2626 redef class AMethPropdef
2627 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2629 # Call the implicit super-init
2630 var auto_super_inits
= self.auto_super_inits
2631 if auto_super_inits
!= null then
2632 var args
= [arguments
.first
]
2633 for auto_super_init
in auto_super_inits
do
2634 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
2636 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
2637 args
.add
(arguments
[i
])
2639 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
2640 v
.compile_callsite
(auto_super_init
, args
)
2643 if auto_super_call
then
2644 v
.supercall
(mpropdef
, arguments
.first
.mtype
.as(MClassType), arguments
)
2647 # Try special compilation
2648 if mpropdef
.is_intern
then
2649 if compile_intern_to_c
(v
, mpropdef
, arguments
) then return
2651 if mpropdef
.is_extern
then
2652 if mpropdef
.mproperty
.is_init
then
2653 if compile_externinit_to_c
(v
, mpropdef
, arguments
) then return
2655 if compile_externmeth_to_c
(v
, mpropdef
, arguments
) then return
2659 # Compile block if any
2660 var n_block
= n_block
2661 if n_block
!= null then
2662 for i
in [0..mpropdef
.msignature
.arity
[ do
2663 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
2664 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
2672 var cn
= v
.class_name_string
(arguments
.first
)
2673 v
.add
("PRINT_ERROR(\"Runtime error
: uncompiled method
`%s` called on
`%s`. NOT YET IMPLEMENTED\
", \"{mpropdef.mproperty.name.escape_to_c}\
", {cn});")
2677 redef fun can_inline
2679 if self.auto_super_inits
!= null then return false
2680 var nblock
= self.n_block
2681 if nblock
== null then return true
2682 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
2683 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
2687 fun compile_intern_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
2689 var pname
= mpropdef
.mproperty
.name
2690 var cname
= mpropdef
.mclassdef
.mclass
.name
2691 var ret
= mpropdef
.msignature
.return_mtype
2693 ret
= v
.resolve_for
(ret
, arguments
.first
)
2695 if pname
!= "==" and pname
!= "!=" then
2696 v
.adapt_signature
(mpropdef
, arguments
)
2697 v
.unbox_signature_extern
(mpropdef
, arguments
)
2699 if cname
== "Int" then
2700 if pname
== "output" then
2701 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
2703 else if pname
== "object_id" then
2704 v
.ret
(arguments
.first
)
2706 else if pname
== "+" then
2707 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2709 else if pname
== "-" then
2710 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2712 else if pname
== "unary -" then
2713 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2715 else if pname
== "unary +" then
2718 else if pname
== "*" then
2719 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2721 else if pname
== "/" then
2722 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2724 else if pname
== "%" then
2725 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2727 else if pname
== "==" then
2728 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2730 else if pname
== "!=" then
2731 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2732 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2734 else if pname
== "<" then
2735 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2737 else if pname
== ">" then
2738 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2740 else if pname
== "<=" then
2741 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2743 else if pname
== ">=" then
2744 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2746 else if pname
== "to_i8" then
2747 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2749 else if pname
== "to_i16" then
2750 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2752 else if pname
== "to_u16" then
2753 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2755 else if pname
== "to_i32" then
2756 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2758 else if pname
== "to_u32" then
2759 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2761 else if pname
== "to_f" then
2762 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2764 else if pname
== "to_b" then
2765 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2767 else if pname
== "code_point" then
2768 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2770 else if pname
== "&" then
2771 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2773 else if pname
== "|" then
2774 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
2776 else if pname
== ">>" then
2777 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2779 else if pname
== "<<" then
2780 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2783 else if cname
== "Char" then
2784 if pname
== "object_id" then
2785 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2787 else if pname
== "successor" then
2788 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2790 else if pname
== "predecessor" then
2791 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2793 else if pname
== "==" then
2794 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2796 else if pname
== "!=" then
2797 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2798 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2800 else if pname
== "<" then
2801 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2803 else if pname
== ">" then
2804 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2806 else if pname
== "<=" then
2807 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2809 else if pname
== ">=" then
2810 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2812 else if pname
== "to_i" then
2813 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
2815 else if pname
== "code_point" then
2816 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2819 else if cname
== "Byte" then
2820 if pname
== "output" then
2821 v
.add
("printf(\"%x\\n\
", {arguments.first});")
2823 else if pname
== "object_id" then
2824 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2826 else if pname
== "+" then
2827 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2829 else if pname
== "-" then
2830 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2832 else if pname
== "unary -" then
2833 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2835 else if pname
== "unary +" then
2838 else if pname
== "*" then
2839 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2841 else if pname
== "/" then
2842 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2844 else if pname
== "%" then
2845 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2847 else if pname
== "==" then
2848 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2850 else if pname
== "!=" then
2851 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2852 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2854 else if pname
== "<" then
2855 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2857 else if pname
== ">" then
2858 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2860 else if pname
== "<=" then
2861 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2863 else if pname
== ">=" then
2864 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2866 else if pname
== ">>" then
2867 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2869 else if pname
== "<<" then
2870 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2872 else if pname
== "&" then
2873 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2875 else if pname
== "to_i" then
2876 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2878 else if pname
== "to_f" then
2879 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2881 else if pname
== "to_i8" then
2882 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2884 else if pname
== "to_i16" then
2885 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2887 else if pname
== "to_u16" then
2888 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2890 else if pname
== "to_i32" then
2891 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2893 else if pname
== "to_u32" then
2894 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2896 else if pname
== "ascii" then
2897 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2900 else if cname
== "Bool" then
2901 if pname
== "output" then
2902 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
2904 else if pname
== "object_id" then
2905 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2907 else if pname
== "==" then
2908 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2910 else if pname
== "!=" then
2911 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2912 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2915 else if cname
== "Float" then
2916 if pname
== "output" then
2917 v
.add
("printf(\"%f\\n\
", {arguments.first});")
2919 else if pname
== "object_id" then
2920 v
.ret
(v
.new_expr
("(double){arguments.first}", ret
.as(not null)))
2922 else if pname
== "+" then
2923 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2925 else if pname
== "-" then
2926 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2928 else if pname
== "unary -" then
2929 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2931 else if pname
== "unary +" then
2934 else if pname
== "succ" then
2935 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
2937 else if pname
== "prec" then
2938 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
2940 else if pname
== "*" then
2941 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2943 else if pname
== "/" then
2944 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2946 else if pname
== "==" then
2947 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2949 else if pname
== "!=" then
2950 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2951 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2953 else if pname
== "<" then
2954 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2956 else if pname
== ">" then
2957 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2959 else if pname
== "<=" then
2960 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2962 else if pname
== ">=" then
2963 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2965 else if pname
== "to_i" then
2966 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2968 else if pname
== "to_b" then
2969 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2971 else if pname
== "to_i8" then
2972 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2974 else if pname
== "to_i16" then
2975 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2977 else if pname
== "to_u16" then
2978 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2980 else if pname
== "to_i32" then
2981 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2983 else if pname
== "to_u32" then
2984 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2987 else if cname
== "CString" then
2988 if pname
== "[]" then
2989 v
.ret
(v
.new_expr
("(unsigned char)((int){arguments[0]}[{arguments[1]}])", ret
.as(not null)))
2991 else if pname
== "[]=" then
2992 v
.add
("{arguments[0]}[{arguments[1]}]=(unsigned char){arguments[2]};")
2994 else if pname
== "copy_to" then
2995 v
.add
("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2997 else if pname
== "atoi" then
2998 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
3000 else if pname
== "fast_cstring" then
3001 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3003 else if pname
== "==" then
3004 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3006 else if pname
== "!=" then
3007 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3008 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3010 else if pname
== "new" then
3011 var alloc
= v
.nit_alloc
(arguments
[1].to_s
, "CString")
3012 v
.ret
(v
.new_expr
("(char*){alloc}", ret
.as(not null)))
3014 else if pname
== "fetch_4_chars" then
3015 v
.ret
(v
.new_expr
("*((uint32_t*)({arguments[0]} + {arguments[1]}))", ret
.as(not null)))
3017 else if pname
== "fetch_4_hchars" then
3018 v
.ret
(v
.new_expr
("(uint32_t)be32toh(*((uint32_t*)({arguments[0]} + {arguments[1]})))", ret
.as(not null)))
3021 else if cname
== "NativeArray" then
3022 return v
.native_array_def
(pname
, ret
, arguments
)
3023 else if cname
== "Int8" then
3024 if pname
== "output" then
3025 v
.add
("printf(\"%\
"PRIi8 \"\\n\
", {arguments.first});")
3027 else if pname
== "object_id" then
3028 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3030 else if pname
== "+" then
3031 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3033 else if pname
== "-" then
3034 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3036 else if pname
== "unary -" then
3037 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3039 else if pname
== "unary +" then
3042 else if pname
== "*" then
3043 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3045 else if pname
== "/" then
3046 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3048 else if pname
== "%" then
3049 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3051 else if pname
== "<<" then
3052 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3054 else if pname
== ">>" then
3055 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3057 else if pname
== "==" then
3058 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3060 else if pname
== "!=" then
3061 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3062 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3064 else if pname
== "<" then
3065 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3067 else if pname
== ">" then
3068 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3070 else if pname
== "<=" then
3071 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3073 else if pname
== ">=" then
3074 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3076 else if pname
== "to_i" then
3077 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3079 else if pname
== "to_b" then
3080 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3082 else if pname
== "to_i16" then
3083 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3085 else if pname
== "to_u16" then
3086 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3088 else if pname
== "to_i32" then
3089 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
3091 else if pname
== "to_u32" then
3092 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
3094 else if pname
== "to_f" then
3095 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3097 else if pname
== "&" then
3098 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3100 else if pname
== "|" then
3101 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3103 else if pname
== "^" then
3104 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3106 else if pname
== "unary ~" then
3107 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3110 else if cname
== "Int16" then
3111 if pname
== "output" then
3112 v
.add
("printf(\"%\
"PRIi16 \"\\n\
", {arguments.first});")
3114 else if pname
== "object_id" then
3115 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3117 else if pname
== "+" then
3118 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3120 else if pname
== "-" then
3121 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3123 else if pname
== "unary -" then
3124 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3126 else if pname
== "unary +" then
3129 else if pname
== "*" then
3130 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3132 else if pname
== "/" then
3133 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3135 else if pname
== "%" then
3136 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3138 else if pname
== "<<" then
3139 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3141 else if pname
== ">>" then
3142 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3144 else if pname
== "==" then
3145 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3147 else if pname
== "!=" then
3148 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3149 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3151 else if pname
== "<" then
3152 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3154 else if pname
== ">" then
3155 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3157 else if pname
== "<=" then
3158 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3160 else if pname
== ">=" then
3161 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3163 else if pname
== "to_i" then
3164 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3166 else if pname
== "to_b" then
3167 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3169 else if pname
== "to_i8" then
3170 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3172 else if pname
== "to_u16" then
3173 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3175 else if pname
== "to_i32" then
3176 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
3178 else if pname
== "to_u32" then
3179 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
3181 else if pname
== "to_f" then
3182 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3184 else if pname
== "&" then
3185 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3187 else if pname
== "|" then
3188 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3190 else if pname
== "^" then
3191 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3193 else if pname
== "unary ~" then
3194 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3197 else if cname
== "UInt16" then
3198 if pname
== "output" then
3199 v
.add
("printf(\"%\
"PRIu16 \"\\n\
", {arguments.first});")
3201 else if pname
== "object_id" then
3202 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3204 else if pname
== "+" then
3205 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3207 else if pname
== "-" then
3208 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3210 else if pname
== "unary -" then
3211 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3213 else if pname
== "unary +" then
3216 else if pname
== "*" then
3217 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3219 else if pname
== "/" then
3220 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3222 else if pname
== "%" then
3223 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3225 else if pname
== "<<" then
3226 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3228 else if pname
== ">>" then
3229 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3231 else if pname
== "==" then
3232 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3234 else if pname
== "!=" then
3235 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3236 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3238 else if pname
== "<" then
3239 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3241 else if pname
== ">" then
3242 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3244 else if pname
== "<=" then
3245 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3247 else if pname
== ">=" then
3248 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3250 else if pname
== "to_i" then
3251 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3253 else if pname
== "to_b" then
3254 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3256 else if pname
== "to_i8" then
3257 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3259 else if pname
== "to_i16" then
3260 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3262 else if pname
== "to_i32" then
3263 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
3265 else if pname
== "to_u32" then
3266 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
3268 else if pname
== "to_f" then
3269 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3271 else if pname
== "&" then
3272 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3274 else if pname
== "|" then
3275 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3277 else if pname
== "^" then
3278 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3280 else if pname
== "unary ~" then
3281 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3284 else if cname
== "Int32" then
3285 if pname
== "output" then
3286 v
.add
("printf(\"%\
"PRIi32 \"\\n\
", {arguments.first});")
3288 else if pname
== "object_id" then
3289 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3291 else if pname
== "+" then
3292 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3294 else if pname
== "-" then
3295 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3297 else if pname
== "unary -" then
3298 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3300 else if pname
== "unary +" then
3303 else if pname
== "*" then
3304 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3306 else if pname
== "/" then
3307 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3309 else if pname
== "%" then
3310 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3312 else if pname
== "<<" then
3313 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3315 else if pname
== ">>" then
3316 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3318 else if pname
== "==" then
3319 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3321 else if pname
== "!=" then
3322 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3323 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3325 else if pname
== "<" then
3326 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3328 else if pname
== ">" then
3329 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3331 else if pname
== "<=" then
3332 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3334 else if pname
== ">=" then
3335 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3337 else if pname
== "to_i" then
3338 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3340 else if pname
== "to_b" then
3341 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3343 else if pname
== "to_i8" then
3344 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3346 else if pname
== "to_i16" then
3347 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3349 else if pname
== "to_u16" then
3350 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3352 else if pname
== "to_u32" then
3353 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
3355 else if pname
== "to_f" then
3356 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3358 else if pname
== "&" then
3359 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3361 else if pname
== "|" then
3362 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3364 else if pname
== "^" then
3365 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3367 else if pname
== "unary ~" then
3368 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3371 else if cname
== "UInt32" then
3372 if pname
== "output" then
3373 v
.add
("printf(\"%\
"PRIu32 \"\\n\
", {arguments.first});")
3375 else if pname
== "object_id" then
3376 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3378 else if pname
== "+" then
3379 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3381 else if pname
== "-" then
3382 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3384 else if pname
== "unary -" then
3385 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3387 else if pname
== "unary +" then
3390 else if pname
== "*" then
3391 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3393 else if pname
== "/" then
3394 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3396 else if pname
== "%" then
3397 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3399 else if pname
== "<<" then
3400 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3402 else if pname
== ">>" then
3403 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3405 else if pname
== "==" then
3406 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3408 else if pname
== "!=" then
3409 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3410 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3412 else if pname
== "<" then
3413 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3415 else if pname
== ">" then
3416 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3418 else if pname
== "<=" then
3419 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3421 else if pname
== ">=" then
3422 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3424 else if pname
== "to_i" then
3425 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3427 else if pname
== "to_b" then
3428 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3430 else if pname
== "to_i8" then
3431 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3433 else if pname
== "to_i16" then
3434 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3436 else if pname
== "to_u16" then
3437 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3439 else if pname
== "to_i32" then
3440 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
3442 else if pname
== "to_f" then
3443 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3445 else if pname
== "&" then
3446 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3448 else if pname
== "|" then
3449 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3451 else if pname
== "^" then
3452 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3454 else if pname
== "unary ~" then
3455 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3459 if pname
== "exit" then
3460 v
.add
("exit((int){arguments[1]});")
3462 else if pname
== "sys" then
3463 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
3465 else if pname
== "object_id" then
3466 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3468 else if pname
== "is_same_type" then
3469 v
.ret
(v
.is_same_type_test
(arguments
[0], arguments
[1]))
3471 else if pname
== "is_same_instance" then
3472 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3474 else if pname
== "output_class_name" then
3475 var nat
= v
.class_name_string
(arguments
.first
)
3476 v
.add
("printf(\"%s\\n\
", {nat});")
3478 else if pname
== "native_class_name" then
3479 var nat
= v
.class_name_string
(arguments
.first
)
3480 v
.ret
(v
.new_expr
("(char*){nat}", ret
.as(not null)))
3482 else if pname
== "force_garbage_collection" then
3483 v
.add
("nit_gcollect();")
3485 else if pname
== "native_argc" then
3486 v
.ret
(v
.new_expr
("glob_argc", ret
.as(not null)))
3488 else if pname
== "native_argv" then
3489 v
.ret
(v
.new_expr
("glob_argv[{arguments[1]}]", ret
.as(not null)))
3495 # Compile an extern method
3496 # Return `true` if the compilation was successful, `false` if a fall-back is needed
3497 fun compile_externmeth_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
3500 var at
= self.get_single_annotation
("extern", v
.compiler
.modelbuilder
)
3501 if at
!= null and at
.n_args
.length
== 1 then
3502 externname
= at
.arg_as_string
(v
.compiler
.modelbuilder
)
3503 if externname
== null then return false
3507 v
.add_extern
(mpropdef
.mclassdef
.mmodule
)
3508 var res
: nullable RuntimeVariable = null
3509 var ret
= mpropdef
.msignature
.return_mtype
3511 ret
= v
.resolve_for
(ret
, arguments
.first
)
3512 res
= v
.new_var_extern
(ret
)
3514 v
.adapt_signature
(mpropdef
, arguments
)
3515 v
.unbox_signature_extern
(mpropdef
, arguments
)
3518 v
.add
("{externname}({arguments.join(", ")});")
3520 v
.add
("{res} = {externname}({arguments.join(", ")});")
3521 res
= v
.box_extern
(res
, ret
.as(not null))
3527 # Compile an extern factory
3528 # Return `true` if the compilation was successful, `false` if a fall-back is needed
3529 fun compile_externinit_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
3532 var at
= self.get_single_annotation
("extern", v
.compiler
.modelbuilder
)
3534 externname
= at
.arg_as_string
(v
.compiler
.modelbuilder
)
3535 if externname
== null then return false
3539 v
.add_extern
(mpropdef
.mclassdef
.mmodule
)
3540 v
.adapt_signature
(mpropdef
, arguments
)
3541 v
.unbox_signature_extern
(mpropdef
, arguments
)
3542 var ret
= arguments
.first
.mtype
3543 var res
= v
.new_var_extern
(ret
)
3547 v
.add
("{res} = {externname}({arguments.join(", ")});")
3548 res
= v
.box_extern
(res
, ret
)
3554 redef class AAttrPropdef
3555 redef fun can_inline
: Bool do return not is_lazy
3557 redef fun compile_to_c
(v
, mpropdef
, arguments
)
3559 if mpropdef
== mreadpropdef
then
3560 assert arguments
.length
== 1
3561 var recv
= arguments
.first
3565 var ret
= self.mtype
3566 var useiset
= not ret
.is_c_primitive
and not ret
isa MNullableType
3567 var guard
= self.mlazypropdef
.mproperty
3569 set
= v
.isset_attribute
(self.mpropdef
.mproperty
, recv
)
3571 set
= v
.read_attribute
(guard
, recv
)
3573 v
.add
("if(likely({set})) \{")
3574 res
= v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
3577 var value
= evaluate_expr
(v
, recv
)
3579 v
.assign
(res
, value
)
3581 var true_v
= v
.bool_instance
(true)
3582 v
.write_attribute
(guard
, arguments
.first
, true_v
)
3586 res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
3588 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
3589 else if mpropdef
== mwritepropdef
then
3590 assert arguments
.length
== 2
3591 var recv
= arguments
.first
3592 var arg
= arguments
[1]
3593 if is_optional
and v
.maybe_null
(arg
) then
3594 var value
= v
.new_var
(self.mpropdef
.static_mtype
.as(not null))
3595 v
.add
("if ({arg} == NULL) \{")
3596 v
.assign
(value
, evaluate_expr
(v
, recv
))
3598 v
.assign
(value
, arg
)
3602 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arg
)
3604 var ret
= self.mtype
3605 var useiset
= not ret
.is_c_primitive
and not ret
isa MNullableType
3607 v
.write_attribute
(self.mlazypropdef
.mproperty
, arguments
.first
, v
.bool_instance
(true))
3615 fun init_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable)
3617 if has_value
and not is_lazy
and not is_optional
and not n_expr
isa ANullExpr then evaluate_expr
(v
, recv
)
3620 # Evaluate, store and return the default value of the attribute
3621 private fun evaluate_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable): RuntimeVariable
3623 var oldnode
= v
.current_node
3624 v
.current_node
= self
3625 var old_frame
= v
.frame
3626 var frame
= new StaticFrame(v
, self.mreadpropdef
.as(not null), recv
.mcasttype
.undecorate
.as(MClassType), [recv
])
3630 var mtype
= self.mtype
3631 assert mtype
!= null
3633 var nexpr
= self.n_expr
3634 var nblock
= self.n_block
3635 if nexpr
!= null then
3636 value
= v
.expr
(nexpr
, mtype
)
3637 else if nblock
!= null then
3638 value
= v
.new_var
(mtype
)
3639 frame
.returnvar
= value
3640 frame
.returnlabel
= v
.get_name
("RET_LABEL")
3643 v
.add
("{frame.returnlabel.as(not null)}:(void)0;")
3649 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
3652 v
.current_node
= oldnode
3657 fun check_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable)
3659 var nexpr
= self.n_expr
3660 if nexpr
!= null then return
3662 var oldnode
= v
.current_node
3663 v
.current_node
= self
3664 var old_frame
= v
.frame
3665 var frame
= new StaticFrame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
3667 # Force read to check the initialization
3668 v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
3670 v
.current_node
= oldnode
3674 redef class AClassdef
3675 private fun compile_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
3677 if mpropdef
.mproperty
.is_root_init
then
3678 assert arguments
.length
== 1
3679 if not mpropdef
.is_intro
then
3680 v
.supercall
(mpropdef
, arguments
.first
.mtype
.as(MClassType), arguments
)
3690 # Try to compile self as an expression
3691 # Do not call this method directly, use `v.expr` instead
3692 private fun expr
(v
: AbstractCompilerVisitor): nullable RuntimeVariable
3694 v
.add
("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
3695 var mtype
= self.mtype
3696 if mtype
== null then
3699 var res
= v
.new_var
(mtype
)
3700 v
.add
("/* {res} = NOT YET {class_name} */")
3705 # Try to compile self as a statement
3706 # Do not call this method directly, use `v.stmt` instead
3707 private fun stmt
(v
: AbstractCompilerVisitor)
3713 redef class ABlockExpr
3716 for e
in self.n_expr
do v
.stmt
(e
)
3720 var last
= self.n_expr
.last
3721 for e
in self.n_expr
do
3722 if e
== last
then break
3725 return v
.expr
(last
, null)
3729 redef class AVardeclExpr
3732 var variable
= self.variable
.as(not null)
3733 var ne
= self.n_expr
3735 var i
= v
.expr
(ne
, variable
.declared_type
)
3736 v
.assign
(v
.variable
(variable
), i
)
3741 redef class AVarExpr
3744 var res
= v
.variable
(self.variable
.as(not null))
3745 var mtype
= self.mtype
.as(not null)
3746 return v
.autoadapt
(res
, mtype
)
3750 redef class AVarAssignExpr
3753 var variable
= self.variable
.as(not null)
3754 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
3755 v
.assign
(v
.variable
(variable
), i
)
3760 redef class AVarReassignExpr
3763 var variable
= self.variable
.as(not null)
3764 var vari
= v
.variable
(variable
)
3765 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
3766 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
3768 v
.assign
(v
.variable
(variable
), res
)
3772 redef class ASelfExpr
3773 redef fun expr
(v
) do return v
.frame
.arguments
.first
3776 redef class AImplicitSelfExpr
3777 redef fun expr
(v
) do
3778 if not is_sys
then return super
3779 return v
.new_expr
("glob_sys", mtype
.as(not null))
3783 redef class AEscapeExpr
3784 redef fun stmt
(v
) do v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
3787 redef class AReturnExpr
3790 var nexpr
= self.n_expr
3791 if nexpr
!= null then
3792 var returnvar
= v
.frame
.returnvar
.as(not null)
3793 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
3794 v
.assign
(returnvar
, i
)
3796 v
.add
("goto {v.frame.returnlabel.as(not null)};")
3800 redef class AAbortExpr
3801 redef fun stmt
(v
) do v
.add_abort
("Aborted")
3807 var cond
= v
.expr_bool
(self.n_expr
)
3808 v
.add
("if ({cond})\{")
3817 var res
= v
.new_var
(self.mtype
.as(not null))
3818 var cond
= v
.expr_bool
(self.n_expr
)
3819 v
.add
("if ({cond})\{")
3820 v
.assign
(res
, v
.expr
(self.n_then
.as(not null), null))
3822 v
.assign
(res
, v
.expr
(self.n_else
.as(not null), null))
3828 redef class AIfexprExpr
3831 var res
= v
.new_var
(self.mtype
.as(not null))
3832 var cond
= v
.expr_bool
(self.n_expr
)
3833 v
.add
("if ({cond})\{")
3834 v
.assign
(res
, v
.expr
(self.n_then
, null))
3836 v
.assign
(res
, v
.expr
(self.n_else
, null))
3845 if self.n_catch
!= null then
3847 v
.add
("struct catch_stack_t *catchStack = getCatchStack();")
3848 v
.add
("if(catchStack->currentSize == 0) \{")
3849 v
.add
(" catchStack->cursor = -1;")
3850 v
.add
(" catchStack->currentSize = 100;")
3851 v
.add
(" catchStack->envs = malloc(sizeof(jmp_buf)*100);")
3852 v
.add
("\} else if(catchStack->cursor == catchStack->currentSize - 1) \{")
3853 v
.add
(" catchStack->currentSize *= 2;")
3854 v
.add
(" catchStack->envs = realloc(catchStack->envs, sizeof(jmp_buf)*catchStack->currentSize);")
3856 v
.add
("catchStack->cursor += 1;")
3857 v
.add
("if(!setjmp(catchStack->envs[catchStack->cursor]))\{")
3858 v
.stmt
(self.n_block
)
3859 v
.add
("catchStack->cursor -= 1;")
3861 v
.add
("catchStack->cursor -= 1;")
3862 v
.stmt
(self.n_catch
)
3866 v
.stmt
(self.n_block
)
3868 v
.add_escape_label
(break_mark
)
3872 redef class AWhileExpr
3876 var cond
= v
.expr_bool
(self.n_expr
)
3877 v
.add
("if (!{cond}) break;")
3878 v
.stmt
(self.n_block
)
3879 v
.add_escape_label
(continue_mark
)
3881 v
.add_escape_label
(break_mark
)
3885 redef class ALoopExpr
3889 v
.stmt
(self.n_block
)
3890 v
.add_escape_label
(continue_mark
)
3892 v
.add_escape_label
(break_mark
)
3896 redef class AForExpr
3899 for g
in n_groups
do
3900 var cl
= v
.expr
(g
.n_expr
, null)
3901 var it_meth
= g
.method_iterator
3902 assert it_meth
!= null
3903 var it
= v
.compile_callsite
(it_meth
, [cl
])
3908 for g
in n_groups
do
3910 var isok_meth
= g
.method_is_ok
3911 assert isok_meth
!= null
3912 var ok
= v
.compile_callsite
(isok_meth
, [it
])
3914 v
.add
("if(!{ok}) break;")
3915 if g
.variables
.length
== 1 then
3916 var item_meth
= g
.method_item
3917 assert item_meth
!= null
3918 var i
= v
.compile_callsite
(item_meth
, [it
])
3920 v
.assign
(v
.variable
(g
.variables
.first
), i
)
3921 else if g
.variables
.length
== 2 then
3922 var key_meth
= g
.method_key
3923 assert key_meth
!= null
3924 var i
= v
.compile_callsite
(key_meth
, [it
])
3926 v
.assign
(v
.variable
(g
.variables
[0]), i
)
3927 var item_meth
= g
.method_item
3928 assert item_meth
!= null
3929 i
= v
.compile_callsite
(item_meth
, [it
])
3931 v
.assign
(v
.variable
(g
.variables
[1]), i
)
3936 v
.stmt
(self.n_block
)
3937 v
.add_escape_label
(continue_mark
)
3938 for g
in n_groups
do
3939 var next_meth
= g
.method_next
3940 assert next_meth
!= null
3941 v
.compile_callsite
(next_meth
, [g
.it
])
3944 v
.add_escape_label
(break_mark
)
3946 for g
in n_groups
do
3947 var method_finish
= g
.method_finish
3948 if method_finish
!= null then
3949 # TODO: Find a way to call this also in long escape (e.g. return)
3950 v
.compile_callsite
(method_finish
, [g
.it
])
3956 redef class AForGroup
3957 # C variable representing the iterator
3958 private var it
: RuntimeVariable is noinit
3961 redef class AAssertExpr
3964 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
3966 var cond
= v
.expr_bool
(self.n_expr
)
3967 v
.add
("if (unlikely(!{cond})) \{")
3974 v
.add_abort
("Assert '{nid.text}' failed")
3976 v
.add_abort
("Assert failed")
3981 # Explain assert if it fails
3982 private fun explain_assert
(v
: AbstractCompilerVisitor)
3984 var explain_assert_str
= explain_assert_str
3985 if explain_assert_str
== null then return
3987 var nas
= v
.compiler
.modelbuilder
.model
.get_mclasses_by_name
("NativeArray")
3988 if nas
== null then return
3990 nas
= v
.compiler
.modelbuilder
.model
.get_mclasses_by_name
("Array")
3991 if nas
== null or nas
.is_empty
then return
3993 var expr
= explain_assert_str
.expr
(v
)
3994 if expr
== null then return
3996 var cstr
= v
.send
(v
.get_property
("to_cstring", expr
.mtype
), [expr
])
3997 if cstr
== null then return
3999 v
.add
"PRINT_ERROR(\"Runtime assert: %s\\n\
", {cstr});"
4006 var res
= v
.new_var
(self.mtype
.as(not null))
4007 var i1
= v
.expr_bool
(self.n_expr
)
4008 v
.add
("if ({i1}) \{")
4011 var i2
= v
.expr_bool
(self.n_expr2
)
4012 v
.add
("{res} = {i2};")
4018 redef class AImpliesExpr
4021 var res
= v
.new_var
(self.mtype
.as(not null))
4022 var i1
= v
.expr_bool
(self.n_expr
)
4023 v
.add
("if (!{i1}) \{")
4026 var i2
= v
.expr_bool
(self.n_expr2
)
4027 v
.add
("{res} = {i2};")
4033 redef class AAndExpr
4036 var res
= v
.new_var
(self.mtype
.as(not null))
4037 var i1
= v
.expr_bool
(self.n_expr
)
4038 v
.add
("if (!{i1}) \{")
4041 var i2
= v
.expr_bool
(self.n_expr2
)
4042 v
.add
("{res} = {i2};")
4048 redef class ANotExpr
4051 var cond
= v
.expr_bool
(self.n_expr
)
4052 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
4056 redef class AOrElseExpr
4059 var res
= v
.new_var
(self.mtype
.as(not null))
4060 var i1
= v
.expr
(self.n_expr
, null)
4062 if not v
.maybe_null
(i1
) then return i1
4064 v
.add
("if ({i1}!=NULL) \{")
4067 var i2
= v
.expr
(self.n_expr2
, null)
4074 redef class AIntegerExpr
4075 redef fun expr
(v
) do
4076 if value
isa Int then return v
.int_instance
(value
.as(Int))
4077 if value
isa Byte then return v
.byte_instance
(value
.as(Byte))
4078 if value
isa Int8 then return v
.int8_instance
(value
.as(Int8))
4079 if value
isa Int16 then return v
.int16_instance
(value
.as(Int16))
4080 if value
isa UInt16 then return v
.uint16_instance
(value
.as(UInt16))
4081 if value
isa Int32 then return v
.int32_instance
(value
.as(Int32))
4082 if value
isa UInt32 then return v
.uint32_instance
(value
.as(UInt32))
4083 # Should never happen
4088 redef class AFloatExpr
4089 redef fun expr
(v
) do return v
.float_instance
(self.value
.as(Float))
4092 redef class ACharExpr
4093 redef fun expr
(v
) do
4094 if is_code_point
then
4095 return v
.int_instance
(value
.as(not null).code_point
)
4097 return v
.char_instance
(self.value
.as(not null))
4101 redef class AArrayExpr
4104 var mtype
= self.element_mtype
.as(not null)
4105 var array
= new Array[RuntimeVariable]
4106 var res
= v
.array_instance
(array
, mtype
)
4108 var old_comprehension
= v
.frame
.comprehension
4109 v
.frame
.comprehension
= res
4110 for nexpr
in self.n_exprs
do
4113 v
.frame
.comprehension
= old_comprehension
4119 redef class AugmentedStringFormExpr
4120 # Factorize the making of a `Regex` object from a literal prefixed string
4121 protected fun make_re
(v
: AbstractCompilerVisitor, rs
: RuntimeVariable): nullable RuntimeVariable do
4124 var res
= v
.compile_callsite
(re
, [rs
])
4126 print
"Cannot call property `to_re` on {self}"
4129 for i
in suffix
.chars
do
4131 var ign
= ignore_case
4133 v
.compile_callsite
(ign
, [res
, v
.bool_instance
(true)])
4139 v
.compile_callsite
(nl
, [res
, v
.bool_instance
(true)])
4145 v
.compile_callsite
(ext
, [res
, v
.bool_instance
(false)])
4148 # Should not happen, this needs to be updated
4149 # along with the addition of new suffixes
4156 redef class AStringFormExpr
4157 redef fun expr
(v
) do return v
.string_instance
(value
)
4160 redef class AStringExpr
4161 redef fun expr
(v
) do
4162 var s
= v
.string_instance
(value
)
4163 if is_string
then return s
4164 if is_bytestring
then
4165 var ns
= v
.c_string_instance
(bytes
.items
, bytes
.length
)
4166 var ln
= v
.int_instance
(bytes
.length
)
4167 var cs
= to_bytes_with_copy
4169 var res
= v
.compile_callsite
(cs
, [ns
, ln
])
4173 var res
= make_re
(v
, s
)
4177 print
"Unimplemented prefix or suffix for {self}"
4184 redef class ASuperstringExpr
4187 var type_string
= v
.mmodule
.string_type
4189 # Collect elements of the superstring
4190 var array
= new Array[AExpr]
4191 for ne
in self.n_exprs
do
4192 # Drop literal empty string.
4193 # They appears in things like "{a}" that is ["", a, ""]
4194 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
4198 # Store the allocated native array in a static variable
4200 var varonce
= v
.get_name
("varonce")
4201 v
.add
("if (unlikely({varonce}==NULL)) \{")
4203 # The native array that will contains the elements to_s-ized.
4204 # For fast concatenation.
4205 var a
= v
.native_array_instance
(type_string
, v
.int_instance
(array
.length
))
4207 v
.add_decl
("static {a.mtype.ctype} {varonce};")
4209 # Pre-fill the array with the literal string parts.
4210 # So they do not need to be filled again when reused
4211 for i
in [0..array
.length
[ do
4213 if not ne
isa AStringFormExpr then continue
4214 var e
= v
.expr
(ne
, null)
4215 v
.native_array_set
(a
, i
, e
)
4219 # Take the native-array from the store.
4220 # The point is to prevent that some recursive execution use (and corrupt) the same native array
4221 # WARNING: not thread safe! (FIXME?)
4222 v
.add
("{a} = {varonce};")
4223 v
.add
("{varonce} = NULL;")
4226 # Stringify the elements and put them in the native array
4227 var to_s_method
= v
.get_property
("to_s", v
.object_type
)
4228 for i
in [0..array
.length
[ do
4230 if ne
isa AStringFormExpr then continue
4231 var e
= v
.expr
(ne
, null)
4232 # Skip the `to_s` if the element is already a String
4233 if not e
.mcasttype
.is_subtype
(v
.compiler
.mainmodule
, null, type_string
) then
4234 e
= v
.send
(to_s_method
, [e
]).as(not null)
4236 v
.native_array_set
(a
, i
, e
)
4239 # Fast join the C string to get the result
4240 var res
= v
.send
(v
.get_property
("native_to_s", a
.mtype
), [a
])
4243 if is_re
then res
= make_re
(v
, res
)
4245 # We finish to work with the native array,
4246 # so store it so that it can be reused
4247 v
.add
("{varonce} = {a};")
4253 redef class ACrangeExpr
4256 var i1
= v
.expr
(self.n_expr
, null)
4257 var i2
= v
.expr
(self.n_expr2
, null)
4258 var mtype
= self.mtype
.as(MClassType)
4259 var res
= v
.init_instance
(mtype
)
4260 v
.compile_callsite
(init_callsite
.as(not null), [res
, i1
, i2
])
4265 redef class AOrangeExpr
4268 var i1
= v
.expr
(self.n_expr
, null)
4269 var i2
= v
.expr
(self.n_expr2
, null)
4270 var mtype
= self.mtype
.as(MClassType)
4271 var res
= v
.init_instance
(mtype
)
4272 v
.compile_callsite
(init_callsite
.as(not null), [res
, i1
, i2
])
4277 redef class ATrueExpr
4278 redef fun expr
(v
) do return v
.bool_instance
(true)
4281 redef class AFalseExpr
4282 redef fun expr
(v
) do return v
.bool_instance
(false)
4285 redef class ANullExpr
4286 redef fun expr
(v
) do return v
.null_instance
4289 redef class AIsaExpr
4292 var i
= v
.expr
(self.n_expr
, null)
4293 var cast_type
= self.cast_type
4294 if cast_type
== null then return null # no-no on broken node
4295 return v
.type_test
(i
, cast_type
, "isa")
4299 redef class AAsCastExpr
4302 var i
= v
.expr
(self.n_expr
, null)
4303 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
4305 v
.add_cast
(i
, self.mtype
.as(not null), "as")
4310 redef class AAsNotnullExpr
4313 var i
= v
.expr
(self.n_expr
, null)
4314 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
4316 if not v
.maybe_null
(i
) then return i
4318 v
.add
("if (unlikely({i} == NULL)) \{")
4319 v
.add_abort
("Cast failed")
4325 redef class AParExpr
4326 redef fun expr
(v
) do return v
.expr
(self.n_expr
, null)
4329 redef class AOnceExpr
4332 var mtype
= self.mtype
.as(not null)
4333 var name
= v
.get_name
("varonce")
4334 var guard
= v
.get_name
(name
+ "_guard")
4335 v
.add_decl
("static {mtype.ctype} {name};")
4336 v
.add_decl
("static int {guard};")
4337 var res
= v
.new_var
(mtype
)
4338 v
.add
("if (likely({guard})) \{")
4339 v
.add
("{res} = {name};")
4341 var i
= v
.expr
(self.n_expr
, mtype
)
4342 v
.add
("{res} = {i};")
4343 v
.add
("{name} = {res};")
4344 v
.add
("{guard} = 1;")
4350 redef class ASendExpr
4353 var recv
= v
.expr
(self.n_expr
, null)
4355 v
.add
"if ({recv}!=NULL) \{"
4357 var callsite
= self.callsite
.as(not null)
4358 if callsite
.is_broken
then return null
4359 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
4360 var res
= v
.compile_callsite
(callsite
, args
)
4364 res
= v
.new_var
(self.mtype
.as(not null))
4365 v
.add
("{res} = {orig_res};")
4367 v
.add
("{res} = NULL;")
4375 redef class ACallrefExpr
4378 v
.add_abort
("NOT YET IMPLEMENTED callref expressions.")
4383 redef class ASendReassignFormExpr
4386 var recv
= v
.expr
(self.n_expr
, null)
4387 var callsite
= self.callsite
.as(not null)
4388 if callsite
.is_broken
then return
4389 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
4391 var value
= v
.expr
(self.n_value
, null)
4393 var left
= v
.compile_callsite
(callsite
, args
)
4396 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
4400 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
4404 redef class ASuperExpr
4407 var frame
= v
.frame
.as(not null)
4408 var recv
= frame
.arguments
.first
4410 var callsite
= self.callsite
4411 if callsite
!= null then
4412 if callsite
.is_broken
then return null
4415 if self.n_args
.n_exprs
.is_empty
then
4416 # Add automatic arguments for the super init call
4418 for i
in [0..callsite
.msignature
.arity
[ do
4419 args
.add
(frame
.arguments
[i
+1])
4422 args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
4426 var res
= v
.compile_callsite
(callsite
, args
)
4430 var mpropdef
= self.mpropdef
.as(not null)
4433 if self.n_args
.n_exprs
.is_empty
then
4434 args
= frame
.arguments
4436 args
= v
.varargize
(mpropdef
, signaturemap
, recv
, self.n_args
.n_exprs
)
4439 # Standard call-next-method
4440 return v
.supercall
(mpropdef
, recv
.mtype
.as(MClassType), args
)
4444 redef class ANewExpr
4447 var mtype
= self.recvtype
4448 assert mtype
!= null
4450 if mtype
.mclass
.name
== "NativeArray" then
4451 assert self.n_args
.n_exprs
.length
== 1
4452 var l
= v
.expr
(self.n_args
.n_exprs
.first
, null)
4453 assert mtype
isa MGenericType
4454 var elttype
= mtype
.arguments
.first
4455 return v
.native_array_instance
(elttype
, l
)
4458 var callsite
= self.callsite
4459 if callsite
== null then return v
.init_instance_or_extern
(mtype
)
4460 if callsite
.is_broken
then return null
4463 # new factories are badly implemented.
4464 # They assume a stub temporary receiver exists.
4465 # This temporary receiver is required because it
4466 # currently holds the method and the formal types.
4468 # However, this object could be reused if the formal types are the same.
4469 # Therefore, the following code will `once` it in these case
4470 if callsite
.mproperty
.is_new
and not mtype
.need_anchor
then
4471 var name
= v
.get_name
("varoncenew")
4472 var guard
= v
.get_name
(name
+ "_guard")
4473 v
.add_decl
("static {mtype.ctype} {name};")
4474 v
.add_decl
("static int {guard};")
4475 recv
= v
.new_var
(mtype
)
4476 v
.add
("if (likely({guard})) \{")
4477 v
.add
("{recv} = {name};")
4479 var i
= v
.init_instance_or_extern
(mtype
)
4480 v
.add
("{recv} = {i};")
4481 v
.add
("{name} = {recv};")
4482 v
.add
("{guard} = 1;")
4485 recv
= v
.init_instance_or_extern
(mtype
)
4488 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
4489 var res2
= v
.compile_callsite
(callsite
, args
)
4490 if res2
!= null then
4491 #self.debug("got {res2} from {mproperty}. drop {recv}")
4498 redef class AAttrExpr
4501 var recv
= v
.expr
(self.n_expr
, null)
4502 var mproperty
= self.mproperty
.as(not null)
4503 return v
.read_attribute
(mproperty
, recv
)
4507 redef class AAttrAssignExpr
4510 var recv
= v
.expr
(self.n_expr
, null)
4511 var i
= v
.expr
(self.n_value
, null)
4512 var mproperty
= self.mproperty
.as(not null)
4513 v
.write_attribute
(mproperty
, recv
, i
)
4518 redef class AAttrReassignExpr
4521 var recv
= v
.expr
(self.n_expr
, null)
4522 var value
= v
.expr
(self.n_value
, null)
4523 var mproperty
= self.mproperty
.as(not null)
4524 var attr
= v
.read_attribute
(mproperty
, recv
)
4525 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
4527 v
.write_attribute
(mproperty
, recv
, res
)
4531 redef class AIssetAttrExpr
4534 var recv
= v
.expr
(self.n_expr
, null)
4535 var mproperty
= self.mproperty
.as(not null)
4536 return v
.isset_attribute
(mproperty
, recv
)
4540 redef class AVarargExpr
4543 return v
.expr
(self.n_expr
, null)
4547 redef class ASafeExpr
4550 return v
.expr
(self.n_expr
, null)
4554 redef class ANamedargExpr
4557 return v
.expr
(self.n_expr
, null)
4561 redef class ADebugTypeExpr
4570 redef class Array[E
]
4571 # Return a new `Array` with the elements only contened in self and not in `o`
4572 fun -(o
: Array[E
]): Array[E
] do
4573 var res
= new Array[E
]
4574 for e
in self do if not o
.has
(e
) then res
.add
(e
)
4580 # All `MProperty` associated to all `MClassDef` of `mclass`
4581 fun properties
(mclass
: MClass): Set[MProperty] do
4582 if not self.properties_cache
.has_key
(mclass
) then
4583 var properties
= new HashSet[MProperty]
4584 var parents
= new Array[MClass]
4585 if self.flatten_mclass_hierarchy
.has
(mclass
) then
4586 parents
.add_all
(mclass
.in_hierarchy
(self).direct_greaters
)
4588 for parent
in parents
do
4589 properties
.add_all
(self.properties
(parent
))
4591 for mclassdef
in mclass
.mclassdefs
do
4592 if not self.in_importation
<= mclassdef
.mmodule
then continue
4593 for mprop
in mclassdef
.intro_mproperties
do
4594 properties
.add
(mprop
)
4597 self.properties_cache
[mclass
] = properties
4599 return properties_cache
[mclass
]
4601 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
4603 # Write FFI and nitni results to file
4604 fun finalize_ffi
(c
: AbstractCompiler) do end
4606 # Give requided addinional system libraries (as given to LD_LIBS)
4607 # Note: can return null instead of an empty set
4608 fun collect_linker_libs
: nullable Array[String] do return null
4611 # Create a tool context to handle options and paths
4612 var toolcontext
= new ToolContext
4614 toolcontext
.tooldescription
= "Usage: nitc [OPTION]... file.nit...\nCompiles Nit programs."
4616 # We do not add other options, so process them now!
4617 toolcontext
.process_options
(args
)
4619 # We need a model to collect stufs
4620 var model
= new Model
4621 # An a model builder to parse files
4622 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
4624 var arguments
= toolcontext
.option_context
.rest
4625 if toolcontext
.opt_run
.value
then
4626 # When --run, only the first is the program, the rest is the run arguments
4627 arguments
= [toolcontext
.option_context
.rest
.shift
]
4629 if arguments
.length
> 1 and toolcontext
.opt_output
.value
!= null then
4630 print
"Option Error: --output needs a single source file. Do you prefer --dir?"
4634 # Here we load an process all modules passed on the command line
4635 var mmodules
= modelbuilder
.parse
(arguments
)
4637 if mmodules
.is_empty
then toolcontext
.quit
4639 modelbuilder
.run_phases
4641 for mmodule
in mmodules
do
4642 toolcontext
.info
("*** PROCESS {mmodule} ***", 1)
4644 toolcontext
.run_global_phases
(ms
)