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_make_flags
= new OptionString("Additional options to the `make` command", "--make-flags")
45 var opt_max_c_lines
= new OptionInt("Maximum number of lines in generated C files. Use 0 for unlimited", 10000, "--max-c-lines")
47 var opt_group_c_files
= new OptionBool("Group all generated code in the same series of files", "--group-c-files")
49 var opt_compile_dir
= new OptionString("Directory used to generate temporary files", "--compile-dir")
51 var opt_hardening
= new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
52 # --no-check-covariance
53 var opt_no_check_covariance
= new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
54 # --no-check-attr-isset
55 var opt_no_check_attr_isset
= new OptionBool("Disable isset tests before each attribute access (dangerous)", "--no-check-attr-isset")
57 var opt_no_check_assert
= new OptionBool("Disable the evaluation of explicit `assert` and `as` (dangerous)", "--no-check-assert")
59 var opt_no_check_autocast
= new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
61 var opt_no_check_null
= new OptionBool("Disable tests of null receiver (dangerous)", "--no-check-null")
63 var opt_no_check_all
= new OptionBool("Disable all tests (dangerous)", "--no-check-all")
64 # --typing-test-metrics
65 var opt_typing_test_metrics
= new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
66 # --invocation-metrics
67 var opt_invocation_metrics
= new OptionBool("Enable static and dynamic count of all method invocations", "--invocation-metrics")
68 # --isset-checks-metrics
69 var opt_isset_checks_metrics
= new OptionBool("Enable static and dynamic count of isset checks before attributes access", "--isset-checks-metrics")
71 var opt_no_stacktrace
= new OptionBool("Disable the generation of stack traces", "--no-stacktrace")
73 var opt_no_gcc_directive
= new OptionArray("Disable advanced gcc directives for optimization", "--no-gcc-directive")
75 var opt_release
= new OptionBool("Compile in release mode and finalize application", "--release")
77 var opt_debug
= new OptionBool("Compile in debug mode (no C-side optimization)", "-g", "--debug")
79 var opt_trace
= new OptionBool("Compile with lttng's instrumentation", "--trace")
84 self.option_context
.add_option
(self.opt_output
, self.opt_dir
, self.opt_run
, self.opt_no_cc
, self.opt_no_main
, self.opt_make_flags
, self.opt_compile_dir
, self.opt_hardening
)
85 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
)
86 self.option_context
.add_option
(self.opt_typing_test_metrics
, self.opt_invocation_metrics
, self.opt_isset_checks_metrics
)
87 self.option_context
.add_option
(self.opt_no_stacktrace
)
88 self.option_context
.add_option
(self.opt_no_gcc_directive
)
89 self.option_context
.add_option
(self.opt_release
)
90 self.option_context
.add_option
(self.opt_max_c_lines
, self.opt_group_c_files
)
91 self.option_context
.add_option
(self.opt_debug
)
92 self.option_context
.add_option
(self.opt_trace
)
94 opt_no_main
.hidden
= true
97 redef fun process_options
(args
)
101 if opt_output
.value
!= null and opt_dir
.value
!= null then
102 print
"Option Error: cannot use both --dir and --output"
106 if opt_no_check_all
.value
then
107 opt_no_check_covariance
.value
= true
108 opt_no_check_attr_isset
.value
= true
109 opt_no_check_assert
.value
= true
110 opt_no_check_autocast
.value
= true
111 opt_no_check_null
.value
= true
116 redef class ModelBuilder
117 # Simple indirection to `Toolchain::write_and_make`
118 protected fun write_and_make
(compiler
: AbstractCompiler)
120 var platform
= compiler
.target_platform
121 var toolchain
= platform
.toolchain
(toolcontext
, compiler
)
122 compiler
.toolchain
= toolchain
123 toolchain
.write_and_make
128 # The specific tool-chain associated to the platform
129 fun toolchain
(toolcontext
: ToolContext, compiler
: AbstractCompiler): Toolchain
131 return new MakefileToolchain(toolcontext
, compiler
)
135 # Build toolchain for a specific target program, varies per `Platform`
139 var toolcontext
: ToolContext
141 # Compiler of the target program
142 var compiler
: AbstractCompiler
144 # Directory where to generate all files
146 # The option `--compile_dir` change this directory.
147 fun root_compile_dir
: String
149 var compile_dir
= toolcontext
.opt_compile_dir
.value
150 if compile_dir
== null then compile_dir
= "nit_compile"
154 # Directory where to generate all C files
156 # By default it is `root_compile_dir` but some platform may require that it is a subdirectory.
157 fun compile_dir
: String do return root_compile_dir
159 # Write all C files and compile them
160 fun write_and_make
is abstract
163 # Default toolchain using a Makefile
164 class MakefileToolchain
167 redef fun write_and_make
169 var debug
= toolcontext
.opt_debug
.value
170 var compile_dir
= compile_dir
172 # Remove the compilation directory unless explicitly set
173 var auto_remove
= toolcontext
.opt_compile_dir
.value
== null
174 # If debug flag is set, do not remove sources
175 if debug
then auto_remove
= false
177 # Generate the .h and .c files
178 # A single C file regroups many compiled rumtime functions
179 # 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
181 self.toolcontext
.info
("*** WRITING C ***", 1)
183 root_compile_dir
.mkdir
186 var cfiles
= new Array[String]
187 write_files
(compile_dir
, cfiles
)
189 # Generate the Makefile
191 write_makefile
(compile_dir
, cfiles
)
194 self.toolcontext
.info
("*** END WRITING C: {time1-time0} ***", 2)
196 if not toolcontext
.check_errors
then return
198 # Execute the Makefile
200 if self.toolcontext
.opt_no_cc
.value
then return
203 self.toolcontext
.info
("*** COMPILING C ***", 1)
205 compile_c_code
(compile_dir
)
208 sys
.system
("rm -r -- '{root_compile_dir.escape_to_sh}/'")
211 if toolcontext
.opt_run
.value
then
212 var mainmodule
= compiler
.mainmodule
213 var out
= outfile
(mainmodule
)
215 cmd
.append toolcontext
.option_context
.rest
216 toolcontext
.exec_and_check
(cmd
, "--run")
220 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
223 # Write all source files to the `compile_dir`
224 fun write_files
(compile_dir
: String, cfiles
: Array[String])
226 var platform
= compiler
.target_platform
227 if platform
.supports_libunwind
then compiler
.build_c_to_nit_bindings
228 var cc_opt_with_libgc
= "-DWITH_LIBGC"
229 if not platform
.supports_libgc
then cc_opt_with_libgc
= ""
231 # Add gc_choser.h to aditionnal bodies
232 var gc_chooser
= new ExternCFile("gc_chooser.c", cc_opt_with_libgc
)
233 if cc_opt_with_libgc
!= "" then gc_chooser
.pkgconfigs
.add
"bdw-gc"
234 compiler
.extern_bodies
.add
(gc_chooser
)
235 var clib
= toolcontext
.nit_dir
/ "clib"
236 compiler
.files_to_copy
.add
"{clib}/gc_chooser.c"
237 compiler
.files_to_copy
.add
"{clib}/gc_chooser.h"
239 # Add lttng traces provider to external bodies
240 if toolcontext
.opt_trace
.value
then
241 #-I. is there in order to make the TRACEPOINT_INCLUDE directive in clib/traces.h refer to the directory in which gcc was invoked.
242 var traces
= new ExternCFile("traces.c", "-I.")
243 traces
.pkgconfigs
.add
"lttng-ust"
244 compiler
.extern_bodies
.add
(traces
)
245 compiler
.files_to_copy
.add
"{clib}/traces.c"
246 compiler
.files_to_copy
.add
"{clib}/traces.h"
250 for m
in compiler
.mainmodule
.in_importation
.greaters
do
251 compiler
.finalize_ffi_for_module
(m
)
254 # Copy original .[ch] files to compile_dir
255 for src
in compiler
.files_to_copy
do
256 var basename
= src
.basename
257 var dst
= "{compile_dir}/{basename}"
261 var hfilename
= compiler
.header
.file
.name
+ ".h"
262 var hfilepath
= "{compile_dir}/{hfilename}"
263 var h
= new FileWriter.open
(hfilepath
)
264 for l
in compiler
.header
.decl_lines
do
268 for l
in compiler
.header
.lines
do
274 var max_c_lines
= toolcontext
.opt_max_c_lines
.value
275 for f
in compiler
.files
do
278 var file
: nullable FileWriter = null
279 for vis
in f
.writers
do
280 if vis
== compiler
.header
then continue
281 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
282 if total_lines
== 0 then continue
284 if file
== null or (count
> max_c_lines
and max_c_lines
> 0) then
286 if file
!= null then file
.close
287 var cfilename
= "{f.name}.{i}.c"
288 var cfilepath
= "{compile_dir}/{cfilename}"
289 self.toolcontext
.info
("new C source files to compile: {cfilepath}", 3)
290 cfiles
.add
(cfilename
)
291 file
= new FileWriter.open
(cfilepath
)
292 file
.write
"#include \"{f.name}.0.h\
"\n"
295 for l
in vis
.decl_lines
do
299 for l
in vis
.lines
do
304 if file
== null then continue
307 var cfilename
= "{f.name}.0.h"
308 var cfilepath
= "{compile_dir}/{cfilename}"
309 var hfile
: nullable FileWriter = null
310 hfile
= new FileWriter.open
(cfilepath
)
311 hfile
.write
"#include \"{hfilename}\
"\n"
312 for key
in f
.required_declarations
do
313 if not compiler
.provided_declarations
.has_key
(key
) then
314 var node
= compiler
.requirers_of_declarations
.get_or_null
(key
)
316 node
.debug
"No provided declaration for {key}"
318 print
"No provided declaration for {key}"
322 hfile
.write compiler
.provided_declarations
[key
]
328 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
331 # Get the name of the Makefile to use
332 fun makefile_name
: String do return "{compiler.mainmodule.c_name}.mk"
334 # Get the default name of the executable to produce
335 fun default_outname
: String
337 var mainmodule
= compiler
.mainmodule
.first_real_mmodule
338 return mainmodule
.name
341 # Combine options and platform informations to get the final path of the outfile
342 fun outfile
(mainmodule
: MModule): String
344 var res
= self.toolcontext
.opt_output
.value
345 if res
!= null then return res
346 res
= default_outname
347 var dir
= self.toolcontext
.opt_dir
.value
348 if dir
!= null then return dir
.join_path
(res
)
353 fun write_makefile
(compile_dir
: String, cfiles
: Array[String])
355 var mainmodule
= compiler
.mainmodule
356 var platform
= compiler
.target_platform
358 var outname
= outfile
(mainmodule
)
360 var real_outpath
= compile_dir
.relpath
(outname
)
361 var outpath
= real_outpath
.escape_to_mk
362 if outpath
!= real_outpath
then
363 # If the name is crazy and need escaping, we will do an indirection
364 # 1. generate the binary in the nit_compile dir under an escaped name
365 # 2. copy the binary at the right place in the `all` goal.
366 outpath
= mainmodule
.c_name
368 var makename
= makefile_name
369 var makepath
= "{compile_dir}/{makename}"
370 var makefile
= new FileWriter.open
(makepath
)
372 var linker_options
= new HashSet[String]
373 for m
in mainmodule
.in_importation
.greaters
do
374 var libs
= m
.collect_linker_libs
375 if libs
!= null then linker_options
.add_all
(libs
)
377 var debug
= toolcontext
.opt_debug
.value
380 ifeq ($(origin CC), default)
383 ifeq ($(origin CXX), default)
386 CFLAGS ?= -g {{{if not debug then "-O2" else ""}}} -Wno-unused-value -Wno-switch -Wno-attributes -Wno-trigraphs
389 LDLIBS ?= -lm {{{linker_options.join(" ")}}}
392 if self.toolcontext
.opt_trace
.value
then makefile
.write
"LDLIBS += -llttng-ust -ldl\n"
394 makefile
.write
"\n# SPECIAL CONFIGURATION FLAGS\n"
395 if platform
.supports_libunwind
then
396 if toolcontext
.opt_no_stacktrace
.value
then
397 makefile
.write
"NO_STACKTRACE ?= True"
399 makefile
.write
"NO_STACKTRACE ?= # Set to `True` to enable"
403 # Dynamic adaptations
404 # While `platform` enable complex toolchains, they are statically applied
405 # For a dynamic adaptsation of the compilation, the generated Makefile should check and adapt things itself
406 makefile
.write
"\n\n"
408 # Check and adapt the targeted system
409 makefile
.write
("uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n")
411 # Check and adapt for the compiler used
412 # clang need an additionnal `-Qunused-arguments`
413 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")
415 if platform
.supports_libunwind
then
417 ifneq ($(NO_STACKTRACE), True)
418 # Check and include lib-unwind in a portable way
419 ifneq ($(uname_S),Darwin)
420 # already included on macosx, but need to get the correct flags in other supported platforms.
421 ifeq ($(shell pkg-config --exists 'libunwind'; echo $$?), 0)
422 LDLIBS += `pkg-config --libs libunwind`
423 CFLAGS += `pkg-config --cflags libunwind`
425 $(warning "[_] stack-traces disabled. Please install libunwind-dev.")
426 CFLAGS += -D NO_STACKTRACE
430 # Stacktraces disabled
431 CFLAGS += -D NO_STACKTRACE
436 makefile
.write
("CFLAGS += -D NO_STACKTRACE\n\n")
440 # Special configuration for Darwin
441 ifeq ($(uname_S),Darwin)
442 # Remove POSIX flag -lrt
443 LDLIBS := $(filter-out -lrt,$(LDLIBS))
446 # Special configuration for Windows under mingw64
447 ifneq ($(findstring MINGW64,$(uname_S)),)
448 # Use the pcreposix regex library
449 LDLIBS += -lpcreposix
451 # Remove POSIX flag -lrt
452 LDLIBS := $(filter-out -lrt,$(LDLIBS))
454 # Silence warnings when storing Int, Char and Bool as pointer address
455 CFLAGS += -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast
460 makefile
.write
("all: {outpath}\n")
461 if outpath
!= real_outpath
then
462 makefile
.write
("\tcp -- {outpath.escape_to_sh} {real_outpath.escape_to_sh.replace("$","$$")}")
466 var ofiles
= new Array[String]
467 var dep_rules
= new Array[String]
468 # Compile each generated file
470 var o
= f
.strip_extension
(".c") + ".o"
471 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -c -o {o} {f}\n\n")
476 # Generate linker script, if any
477 if not compiler
.linker_script
.is_empty
then
478 var linker_script_path
= "{compile_dir}/linker_script"
479 ofiles
.add
"linker_script"
480 var f
= new FileWriter.open
(linker_script_path
)
481 for l
in compiler
.linker_script
do
488 # pkg-config annotation support
489 var pkgconfigs
= new Array[String]
490 for f
in compiler
.extern_bodies
do
491 pkgconfigs
.add_all f
.pkgconfigs
494 # Only test if pkg-config is used
495 if not pkgconfigs
.is_empty
then
497 # Check availability of pkg-config, silence the proc output
498 toolcontext
.check_pkgconfig_packages pkgconfigs
500 # Double the check in the Makefile in case it's distributed
502 # does pkg-config exists?
503 ifneq ($(shell which pkg-config >/dev/null; echo $$?), 0)
504 $(error "Command `pkg-config` not found. Please install it")
507 for p
in pkgconfigs
do
509 # Check for library {{{p}}}
510 ifneq ($(shell pkg-config --exists '{{{p}}}'; echo $$?), 0)
511 $(error "pkg-config: package {{{p}}} is not found.")
517 # Compile each required extern body into a specific .o
518 var java_files
= new Array[ExternFile]
519 for f
in compiler
.extern_bodies
do
520 var o
= f
.makefile_rule_name
521 var ff
= f
.filename
.basename
522 makefile
.write
("{o}: {ff}\n")
523 makefile
.write
("\t{f.makefile_rule_content}\n\n")
524 dep_rules
.add
(f
.makefile_rule_name
)
526 if f
.compiles_to_o_file
then ofiles
.add
(o
)
527 if f
.add_to_jar
then java_files
.add
(f
)
530 if not java_files
.is_empty
then
531 var jar_file
= "{outpath}.jar"
533 var class_files_array
= new Array[String]
534 for f
in java_files
do class_files_array
.add
(f
.makefile_rule_name
)
535 var class_files
= class_files_array
.join
(" ")
537 makefile
.write
("{jar_file}: {class_files}\n")
538 makefile
.write
("\tjar cf {jar_file} {class_files}\n\n")
539 dep_rules
.add jar_file
544 if not pkgconfigs
.is_empty
then
545 pkg
= "`pkg-config --libs {pkgconfigs.join(" ")}`"
547 makefile
.write
("{outpath}: {dep_rules.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outpath.escape_to_sh} {ofiles.join(" ")} $(LDLIBS) {pkg}\n\n")
549 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n")
550 if outpath
!= real_outpath
then
551 makefile
.write
("\trm -- {outpath.escape_to_sh} 2>/dev/null\n")
554 self.toolcontext
.info
("Generated makefile: {makepath}", 2)
556 makepath
.file_copy_to
"{compile_dir}/Makefile"
559 # The C code is generated, compile it to an executable
560 fun compile_c_code
(compile_dir
: String)
562 var makename
= makefile_name
564 var makeflags
= self.toolcontext
.opt_make_flags
.value
565 if makeflags
== null then makeflags
= ""
567 var command
= "make -B -C {compile_dir} -f {makename} -j 4 {makeflags}"
568 self.toolcontext
.info
(command
, 2)
571 if self.toolcontext
.verbose_level
>= 3 then
572 res
= sys
.system
("{command} 2>&1")
573 else if is_windows
then
574 res
= sys
.system
("{command} 2>&1 >nul")
576 res
= sys
.system
("{command} 2>&1 >/dev/null")
579 toolcontext
.error
(null, "Compilation Error: `make` failed with error code: {res}. The command was `{command}`.")
584 # Singleton that store the knowledge about the compilation process
585 abstract class AbstractCompiler
586 type VISITOR: AbstractCompilerVisitor
588 # Table corresponding c_names to nit names (methods)
589 var names
= new HashMap[String, String]
591 # The main module of the program currently compiled
592 # Is assigned during the separate compilation
593 var mainmodule
: MModule is writable
595 # The real main module of the program
596 var realmainmodule
: MModule is noinit
598 # The modelbuilder used to know the model and the AST
599 var modelbuilder
: ModelBuilder is protected writable
601 # The associated toolchain
603 # Set by `modelbuilder.write_and_make` and permit sub-routines to access the current toolchain if required.
604 var toolchain
: Toolchain is noinit
606 # Is hardening asked? (see --hardening)
607 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
609 # The targeted specific platform
610 var target_platform
: Platform is noinit
614 self.realmainmodule
= mainmodule
615 target_platform
= mainmodule
.target_platform
or else new Platform
618 # Do the full code generation of the program `mainmodule`
619 # It is the main method usually called after the instantiation
620 fun do_compilation
is abstract
622 # Force the creation of a new file
623 # The point is to avoid contamination between must-be-compiled-separately files
624 fun new_file
(name
: String): CodeFile
626 if modelbuilder
.toolcontext
.opt_group_c_files
.value
then
627 if self.files
.is_empty
then
628 var f
= new CodeFile(mainmodule
.c_name
)
631 return self.files
.first
633 var f
= new CodeFile(name
)
638 # The list of all associated files
639 # Used to generate .c files
640 var files
= new Array[CodeFile]
642 # Initialize a visitor specific for a compiler engine
643 fun new_visitor
: VISITOR is abstract
645 # Where global declaration are stored (the main .h)
646 var header
: CodeWriter is writable, noinit
648 # Additionnal linker script for `ld`.
649 # Mainly used to do specific link-time symbol resolution
650 var linker_script
= new Array[String]
652 # Provide a declaration that can be requested (before or latter) by a visitor
653 fun provide_declaration
(key
: String, s
: String)
655 if self.provided_declarations
.has_key
(key
) then
656 assert self.provided_declarations
[key
] == s
658 self.provided_declarations
[key
] = s
661 private var provided_declarations
= new HashMap[String, String]
663 private var requirers_of_declarations
= new HashMap[String, ANode]
665 # Builds the .c and .h files to be used when generating a Stack Trace
666 # Binds the generated C function names to Nit function names
667 fun build_c_to_nit_bindings
669 var compile_dir
= toolchain
.compile_dir
671 var stream
= new FileWriter.open
("{compile_dir}/c_functions_hash.c")
672 stream
.write
("#include <string.h>\n")
673 stream
.write
("#include <stdlib.h>\n")
674 stream
.write
("#include \"c_functions_hash
.h\
"\n")
675 stream
.write
("typedef struct C_Nit_Names\{char* name; char* nit_name;\}C_Nit_Names;\n")
676 stream
.write
("const char* get_nit_name(register const char* procproc, register unsigned int len)\{\n")
677 stream
.write
("char* procname = malloc(len+1);")
678 stream
.write
("memcpy(procname, procproc, len);")
679 stream
.write
("procname[len] = '\\0';")
680 stream
.write
("static const C_Nit_Names map[{names.length}] = \{\n")
681 for i
in names
.keys
do
683 stream.write(i.escape_to_c)
684 stream.write("\
",\"")
685 stream.write(names[i].escape_to_c)
686 stream.write("\
"\},\n")
688 stream
.write
("\};\n")
689 stream
.write
("int i;")
690 stream
.write
("for(i = 0; i < {names.length}; i++)\{")
691 stream
.write
("if(strcmp(procname,map[i].name) == 0)\{")
692 stream
.write
("free(procname);")
693 stream
.write
("return map[i].nit_name;")
696 stream
.write
("free(procname);")
697 stream
.write
("return NULL;")
701 stream
= new FileWriter.open
("{compile_dir}/c_functions_hash.h")
702 stream
.write
("const char* get_nit_name(register const char* procname, register unsigned int len);\n")
705 extern_bodies
.add
(new ExternCFile("{compile_dir}/c_functions_hash.c", ""))
709 # This method call compile_header_strucs method that has to be refined
710 fun compile_header
do
711 self.header
.add_decl
("#include <stdlib.h>")
712 self.header
.add_decl
("#include <stdio.h>")
713 self.header
.add_decl
("#include <string.h>")
715 self.header
.add_decl
("#include <setjmp.h>\n")
716 self.header
.add_decl
("#include <sys/types.h>\n")
717 self.header
.add_decl
("#include <unistd.h>\n")
718 self.header
.add_decl
("#include <stdint.h>\n")
719 self.header
.add_decl
("#ifdef __linux__")
720 self.header
.add_decl
(" #include <endian.h>")
721 self.header
.add_decl
("#endif")
722 self.header
.add_decl
("#include <inttypes.h>\n")
723 self.header
.add_decl
("#include \"gc_chooser
.h\
"")
724 if modelbuilder
.toolcontext
.opt_trace
.value
then self.header
.add_decl
("#include \"traces
.h\
"")
725 self.header
.add_decl
("#ifdef __APPLE__")
726 self.header
.add_decl
(" #include <TargetConditionals.h>")
727 self.header
.add_decl
(" #include <syslog.h>")
728 self.header
.add_decl
(" #include <libkern/OSByteOrder.h>")
729 self.header
.add_decl
(" #define be32toh(x) OSSwapBigToHostInt32(x)")
730 self.header
.add_decl
("#endif")
731 self.header
.add_decl
("#ifdef _WIN32")
732 self.header
.add_decl
(" #define be32toh(val) _byteswap_ulong(val)")
733 self.header
.add_decl
("#endif")
734 self.header
.add_decl
("#ifdef ANDROID")
735 self.header
.add_decl
(" #ifndef be32toh")
736 self.header
.add_decl
(" #define be32toh(val) betoh32(val)")
737 self.header
.add_decl
(" #endif")
738 self.header
.add_decl
(" #include <android/log.h>")
739 self.header
.add_decl
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)")
740 self.header
.add_decl
("#elif TARGET_OS_IPHONE")
741 self.header
.add_decl
(" #define PRINT_ERROR(...) syslog(LOG_ERR, __VA_ARGS__)")
742 self.header
.add_decl
("#else")
743 self.header
.add_decl
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)")
744 self.header
.add_decl
("#endif")
746 compile_header_structs
747 compile_nitni_structs
750 var gccd_disable
= modelbuilder
.toolcontext
.opt_no_gcc_directive
.value
751 if gccd_disable
.has
("noreturn") or gccd_disable
.has
("all") then
752 # Signal handler function prototype
753 self.header
.add_decl
("void fatal_exit(int);")
755 self.header
.add_decl
("void fatal_exit(int) __attribute__ ((noreturn));")
758 if gccd_disable
.has
("likely") or gccd_disable
.has
("all") then
759 self.header
.add_decl
("#define likely(x) (x)")
760 self.header
.add_decl
("#define unlikely(x) (x)")
761 else if gccd_disable
.has
("correct-likely") then
762 # invert the `likely` definition
763 # Used by masochists to bench the worst case
764 self.header
.add_decl
("#define likely(x) __builtin_expect((x),0)")
765 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),1)")
767 self.header
.add_decl
("#define likely(x) __builtin_expect((x),1)")
768 self.header
.add_decl
("#define unlikely(x) __builtin_expect((x),0)")
771 # Global variable used by intern methods
772 self.header
.add_decl
("extern int glob_argc;")
773 self.header
.add_decl
("extern char **glob_argv;")
774 self.header
.add_decl
("extern val *glob_sys;")
777 # Stack stocking environment for longjumps
778 protected fun compile_catch_stack
do
779 self.header
.add_decl
"""
780 struct catch_stack_t {
785 extern __thread struct catch_stack_t catchStack;
789 # Declaration of structures for live Nit types
790 protected fun compile_header_structs
is abstract
792 # Declaration of structures for nitni undelying the FFI
793 protected fun compile_nitni_structs
795 self.header
.add_decl
"""
796 /* Native reference to Nit objects */
797 /* This structure is used to represent every Nit type in extern methods and custom C code. */
799 struct nitni_ref *next,
800 *prev; /* adjacent global references in global list */
801 int count; /* number of time this global reference has been marked */
804 /* List of global references from C code to Nit objects */
805 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
806 struct nitni_global_ref_list_t {
807 struct nitni_ref *head, *tail;
809 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
811 /* Initializer of global reference list */
812 extern void nitni_global_ref_list_init();
814 /* Intern function to add a global reference to the list */
815 extern void nitni_global_ref_add( struct nitni_ref *ref );
817 /* Intern function to remove a global reference from the list */
818 extern void nitni_global_ref_remove( struct nitni_ref *ref );
820 /* Increase count on an existing global reference */
821 extern void nitni_global_ref_incr( struct nitni_ref *ref );
823 /* Decrease count on an existing global reference */
824 extern void nitni_global_ref_decr( struct nitni_ref *ref );
828 fun compile_finalizer_function
830 var finalizable_type
= mainmodule
.finalizable_type
831 if finalizable_type
== null then return
833 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
835 if finalize_meth
== null then
836 modelbuilder
.toolcontext
.error
(null, "Error: the `Finalizable` class does not declare the `finalize` method.")
840 var v
= self.new_visitor
841 v
.add_decl
"void gc_finalize (void *obj, void *client_data) \{"
842 var recv
= v
.new_expr
("obj", finalizable_type
)
843 v
.send
(finalize_meth
, [recv
])
847 # Hook to add specif piece of code before the the main C function.
849 # Is called by `compile_main_function`
850 fun compile_before_main
(v
: VISITOR)
854 # Hook to add specif piece of code at the begin on the main C function.
856 # Is called by `compile_main_function`
857 fun compile_begin_main
(v
: VISITOR)
861 # Generate the main C function.
865 # * allocate the Sys object if it exists
866 # * call init if is exists
867 # * call main if it exists
868 fun compile_main_function
870 var v
= self.new_visitor
871 v
.add_decl
("#include <signal.h>")
872 var platform
= target_platform
874 var no_main
= platform
.no_main
or modelbuilder
.toolcontext
.opt_no_main
.value
876 if platform
.supports_libunwind
then
877 v
.add_decl
("#ifndef NO_STACKTRACE")
878 v
.add_decl
("#define UNW_LOCAL_ONLY")
879 v
.add_decl
("#include <libunwind.h>")
880 v
.add_decl
("#include \"c_functions_hash
.h\
"")
883 v
.add_decl
("int glob_argc;")
884 v
.add_decl
("char **glob_argv;")
885 v
.add_decl
("val *glob_sys;")
886 v
.add_decl
("__thread struct catch_stack_t catchStack = \{-1, 0, NULL\};")
888 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
889 for tag
in count_type_test_tags
do
890 v
.add_decl
("long count_type_test_resolved_{tag};")
891 v
.add_decl
("long count_type_test_unresolved_{tag};")
892 v
.add_decl
("long count_type_test_skipped_{tag};")
893 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
894 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
895 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
899 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
900 v
.add_decl
("long count_invoke_by_tables;")
901 v
.add_decl
("long count_invoke_by_direct;")
902 v
.add_decl
("long count_invoke_by_inline;")
903 v
.compiler
.header
.add_decl
("extern long count_invoke_by_tables;")
904 v
.compiler
.header
.add_decl
("extern long count_invoke_by_direct;")
905 v
.compiler
.header
.add_decl
("extern long count_invoke_by_inline;")
908 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
909 v
.add_decl
("long count_attr_reads = 0;")
910 v
.add_decl
("long count_isset_checks = 0;")
911 v
.compiler
.header
.add_decl
("extern long count_attr_reads;")
912 v
.compiler
.header
.add_decl
("extern long count_isset_checks;")
915 v
.add_decl
("static void show_backtrace(void) \{")
916 if platform
.supports_libunwind
then
917 v
.add_decl
("#ifndef NO_STACKTRACE")
918 v
.add_decl
("char* opt = getenv(\"NIT_NO_STACK\
");")
919 v
.add_decl
("unw_cursor_t cursor;")
920 v
.add_decl
("if(opt==NULL)\{")
921 v
.add_decl
("unw_context_t uc;")
922 v
.add_decl
("unw_word_t ip;")
923 v
.add_decl
("char* procname = malloc(sizeof(char) * 100);")
924 v
.add_decl
("unw_getcontext(&uc);")
925 v
.add_decl
("unw_init_local(&cursor, &uc);")
926 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
927 v
.add_decl
("PRINT_ERROR(\"-- Stack Trace ------------------------------\\n\
");")
928 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
929 v
.add_decl
("while (unw_step(&cursor) > 0) \{")
930 v
.add_decl
(" unw_get_proc_name(&cursor, procname, 100, &ip);")
931 v
.add_decl
(" const char* recv = get_nit_name(procname, strlen(procname));")
932 v
.add_decl
(" if (recv != NULL)\{")
933 v
.add_decl
(" PRINT_ERROR(\"` %s\\n\", recv);")
934 v.add_decl(" \}else\{")
935 v.add_decl(" PRINT_ERROR(\"` %s\\n\
", procname);")
938 v
.add_decl
("PRINT_ERROR(\"-------------------------------------------------\\n\
");")
939 v
.add_decl
("free(procname);")
941 v
.add_decl
("#endif /* NO_STACKTRACE */")
945 v
.add_decl
("void sig_handler(int signo)\{")
946 v
.add_decl
"#ifdef _WIN32"
947 v
.add_decl
"PRINT_ERROR(\"Caught signal
: %s\\n\
", signo);"
949 v
.add_decl
("PRINT_ERROR(\"Caught signal
: %s\\n\
", strsignal(signo));")
951 v
.add_decl
("show_backtrace();")
953 v
.add_decl
("signal(signo, SIG_DFL);")
954 v
.add_decl
"#ifndef _WIN32"
955 v
.add_decl
("kill(getpid(), signo);")
959 v
.add_decl
("void fatal_exit(int status) \{")
960 v
.add_decl
("show_backtrace();")
961 v
.add_decl
("exit(status);")
964 compile_before_main
(v
)
967 v
.add_decl
("int nit_main(int argc, char** argv) \{")
969 v
.add_decl
("int main(int argc, char** argv) \{")
972 compile_begin_main
(v
)
974 v
.add
"#if !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE)"
975 v
.add
("signal(SIGABRT, sig_handler);")
976 v
.add
("signal(SIGFPE, sig_handler);")
977 v
.add
("signal(SIGILL, sig_handler);")
978 v
.add
("signal(SIGINT, sig_handler);")
979 v
.add
("signal(SIGTERM, sig_handler);")
980 v
.add
("signal(SIGSEGV, sig_handler);")
982 v
.add
"#ifndef _WIN32"
983 v
.add
("signal(SIGPIPE, SIG_IGN);")
986 v
.add
("glob_argc = argc; glob_argv = argv;")
987 v
.add
("initialize_gc_option();")
989 v
.add
"initialize_nitni_global_refs();"
991 var main_type
= mainmodule
.sys_type
992 if main_type
!= null then
993 var mainmodule
= v
.compiler
.mainmodule
994 var glob_sys
= v
.init_instance
(main_type
)
995 v
.add
("glob_sys = {glob_sys};")
996 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
.mclass
)
997 if main_init
!= null then
998 v
.send
(main_init
, [glob_sys
])
1000 var main_method
= mainmodule
.try_get_primitive_method
("run", main_type
.mclass
) or else
1001 mainmodule
.try_get_primitive_method
("main", main_type
.mclass
)
1002 if main_method
!= null then
1003 v
.send
(main_method
, [glob_sys
])
1007 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1008 v
.add_decl
("long count_type_test_resolved_total = 0;")
1009 v
.add_decl
("long count_type_test_unresolved_total = 0;")
1010 v
.add_decl
("long count_type_test_skipped_total = 0;")
1011 v
.add_decl
("long count_type_test_total_total = 0;")
1012 for tag
in count_type_test_tags
do
1013 v
.add_decl
("long count_type_test_total_{tag};")
1014 v
.add
("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
1015 v
.add
("count_type_test_resolved_total += count_type_test_resolved_{tag};")
1016 v
.add
("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
1017 v
.add
("count_type_test_skipped_total += count_type_test_skipped_{tag};")
1018 v
.add
("count_type_test_total_total += count_type_test_total_{tag};")
1020 v
.add
("printf(\"# dynamic count_type_test: total %l\\n\");")
1021 v
.add
("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\
");")
1022 var tags
= count_type_test_tags
.to_a
1025 v
.add
("printf(\"{tag}\
");")
1026 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
1027 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
1028 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
1029 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\\n\
", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
1033 if self.modelbuilder
.toolcontext
.opt_invocation_metrics
.value
then
1034 v
.add_decl
("long count_invoke_total;")
1035 v
.add
("count_invoke_total = count_invoke_by_tables + count_invoke_by_direct + count_invoke_by_inline;")
1036 v
.add
("printf(\"# dynamic count_invocation: total %ld\\n\", count_invoke_total);")
1037 v
.add
("printf(\"by table
: %ld
(%.2f
%%)\\n\
", count_invoke_by_tables, 100.0*count_invoke_by_tables/count_invoke_total);")
1038 v
.add
("printf(\"direct
: %ld
(%.2f
%%)\\n\
", count_invoke_by_direct, 100.0*count_invoke_by_direct/count_invoke_total);")
1039 v
.add
("printf(\"inlined
: %ld
(%.2f
%%)\\n\
", count_invoke_by_inline, 100.0*count_invoke_by_inline/count_invoke_total);")
1042 if self.modelbuilder
.toolcontext
.opt_isset_checks_metrics
.value
then
1043 v
.add
("printf(\"# dynamic attribute reads: %ld\\n\", count_attr_reads);")
1044 v
.add
("printf(\"# dynamic isset checks: %ld\\n\", count_isset_checks);")
1050 for m
in mainmodule
.in_importation
.greaters
do
1051 var f
= "FILE_"+m
.c_name
1052 v
.add
"const char {f}[] = \"{m.location.file.filename.escape_to_c}\
";"
1053 provide_declaration
(f
, "extern const char {f}[];")
1057 # Copile all C functions related to the [incr|decr]_ref features of the FFI
1058 fun compile_nitni_global_ref_functions
1060 var v
= self.new_visitor
1062 struct nitni_global_ref_list_t *nitni_global_ref_list;
1063 void initialize_nitni_global_refs() {
1064 nitni_global_ref_list = (struct nitni_global_ref_list_t*)nit_alloc(sizeof(struct nitni_global_ref_list_t));
1065 nitni_global_ref_list->head = NULL;
1066 nitni_global_ref_list->tail = NULL;
1069 void nitni_global_ref_add( struct nitni_ref *ref ) {
1070 if ( nitni_global_ref_list->head == NULL ) {
1071 nitni_global_ref_list->head = ref;
1074 nitni_global_ref_list->tail->next = ref;
1075 ref->prev = nitni_global_ref_list->tail;
1077 nitni_global_ref_list->tail = ref;
1082 void nitni_global_ref_remove( struct nitni_ref *ref ) {
1083 if ( ref->prev == NULL ) {
1084 nitni_global_ref_list->head = ref->next;
1086 ref->prev->next = ref->next;
1089 if ( ref->next == NULL ) {
1090 nitni_global_ref_list->tail = ref->prev;
1092 ref->next->prev = ref->prev;
1096 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
1097 if ( ref->count == 0 ) /* not registered */
1100 nitni_global_ref_add( ref );
1106 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
1107 if ( ref->count == 1 ) /* was last reference */
1109 /* remove from list */
1110 nitni_global_ref_remove( ref );
1118 # List of additional files required to compile (FFI)
1119 var extern_bodies
= new Array[ExternFile]
1121 # List of source files to copy over to the compile dir
1122 var files_to_copy
= new Array[String]
1124 # This is used to avoid adding an extern file more than once
1125 private var seen_extern
= new ArraySet[String]
1127 # Generate code that initialize the attributes on a new instance
1128 fun generate_init_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
1130 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
1131 self.mainmodule
.linearize_mclassdefs
(cds
)
1133 for npropdef
in modelbuilder
.collect_attr_propdef
(cd
) do
1134 npropdef
.init_expr
(v
, recv
)
1139 # Generate code that check if an attribute is correctly initialized
1140 fun generate_check_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
1142 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
1143 self.mainmodule
.linearize_mclassdefs
(cds
)
1145 for npropdef
in modelbuilder
.collect_attr_propdef
(cd
) do
1146 npropdef
.check_expr
(v
, recv
)
1153 var count_type_test_tags
: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
1154 var count_type_test_resolved
: HashMap[String, Int] = init_count_type_test_tags
1155 var count_type_test_unresolved
: HashMap[String, Int] = init_count_type_test_tags
1156 var count_type_test_skipped
: HashMap[String, Int] = init_count_type_test_tags
1158 protected fun init_count_type_test_tags
: HashMap[String, Int]
1160 var res
= new HashMap[String, Int]
1161 for tag
in count_type_test_tags
do
1167 # Display stats about compilation process
1171 # * type tests against resolved types (`x isa Collection[Animal]`)
1172 # * type tests against unresolved types (`x isa Collection[E]`)
1173 # * type tests skipped
1174 # * type tests total
1177 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1178 print
"# static count_type_test"
1179 print
"\tresolved:\tunresolved\tskipped\ttotal"
1180 var count_type_test_total
= init_count_type_test_tags
1181 count_type_test_resolved
["total"] = 0
1182 count_type_test_unresolved
["total"] = 0
1183 count_type_test_skipped
["total"] = 0
1184 count_type_test_total
["total"] = 0
1185 for tag
in count_type_test_tags
do
1186 count_type_test_total
[tag
] = count_type_test_resolved
[tag
] + count_type_test_unresolved
[tag
] + count_type_test_skipped
[tag
]
1187 count_type_test_resolved
["total"] += count_type_test_resolved
[tag
]
1188 count_type_test_unresolved
["total"] += count_type_test_unresolved
[tag
]
1189 count_type_test_skipped
["total"] += count_type_test_skipped
[tag
]
1190 count_type_test_total
["total"] += count_type_test_total
[tag
]
1192 var count_type_test
= count_type_test_total
["total"]
1193 var tags
= count_type_test_tags
.to_a
1197 printn
"\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
1198 printn
"\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
1199 printn
"\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
1200 printn
"\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
1206 fun finalize_ffi_for_module
(mmodule
: MModule) do mmodule
.finalize_ffi
(self)
1209 # C code file generated from the `writers` and the `required_declarations`
1211 # A file unit aims to be autonomous and is made or one or more `CodeWriter`s.
1213 # Basename of the file, will be appended by `.h` and `.c`
1216 # `CodeWriter` used in sequence to fill the top of the body, then the bottom
1217 var writers
= new Array[CodeWriter]
1219 # Required declarations keys
1221 # See: `provide_declaration`
1222 var required_declarations
= new HashSet[String]
1225 # Store generated lines
1227 # Instances are added to `file.writers` at construction.
1232 # Main lines of code (written at the bottom of the body)
1233 var lines
= new Array[String]
1235 # Lines of code for declarations (written at the top of the body)
1236 var decl_lines
= new Array[String]
1238 # Add a line in the main lines of code (to `lines`)
1239 fun add
(s
: String) do self.lines
.add
(s
)
1241 # Add a declaration line (to `decl_lines`)
1243 # Used for local and global declaration.
1244 fun add_decl
(s
: String) do self.decl_lines
.add
(s
)
1248 file
.writers
.add
(self)
1252 # A visitor on the AST of property definition that generate the C code.
1253 abstract class AbstractCompilerVisitor
1255 type COMPILER: AbstractCompiler
1257 # The associated compiler
1258 var compiler
: COMPILER
1260 # The current visited AST node
1261 var current_node
: nullable ANode = null is writable
1263 # The current `StaticFrame`
1264 var frame
: nullable StaticFrame = null is writable
1266 # Alias for self.compiler.mainmodule.object_type
1267 fun object_type
: MClassType do return self.compiler
.mainmodule
.object_type
1269 # Alias for self.compiler.mainmodule.bool_type
1270 fun bool_type
: MClassType do return self.compiler
.mainmodule
.bool_type
1272 var writer
: CodeWriter is noinit
1276 self.writer
= new CodeWriter(compiler
.files
.last
)
1279 # Force to get the primitive property named `name` in the instance `recv` or abort
1280 fun get_property
(name
: String, recv
: MType): MMethod
1282 assert recv
isa MClassType
1283 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
, name
, recv
.mclass
, self.compiler
.mainmodule
)
1286 fun compile_callsite
(callsite
: CallSite, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1288 if callsite
.is_broken
then return null
1289 var initializers
= callsite
.mpropdef
.initializers
1290 if not initializers
.is_empty
then
1291 var recv
= arguments
.first
1294 for p
in initializers
do
1295 if p
isa MMethod then
1297 for x
in p
.intro
.msignature
.mparameters
do
1298 args
.add arguments
[i
]
1302 else if p
isa MAttribute then
1303 self.write_attribute
(p
, recv
, arguments
[i
])
1307 assert i
== arguments
.length
1309 return self.send
(callsite
.mproperty
, [recv
])
1312 return self.send
(callsite
.mproperty
, arguments
)
1315 fun native_array_instance
(elttype
: MType, length
: RuntimeVariable): RuntimeVariable is abstract
1317 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable]): Bool do return false
1319 # Return an element of a native array.
1320 # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays
1321 fun native_array_get
(native_array
: RuntimeVariable, index
: Int): RuntimeVariable is abstract
1323 # Store an element in a native array.
1324 # The method is unsafe and is just a direct wrapper for the specific implementation of native arrays
1325 fun native_array_set
(native_array
: RuntimeVariable, index
: Int, value
: RuntimeVariable) is abstract
1327 # Allocate `size` bytes with the low_level `nit_alloc` C function
1329 # This method can be redefined to inject statistic or tracing code.
1331 # `tag` if any, is used to mark the class of the allocated object.
1332 fun nit_alloc
(size
: String, tag
: nullable String): String
1334 return "nit_alloc({size})"
1337 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
1338 # This method is used to manage varargs in signatures and returns the real array
1339 # of runtime variables to use in the call.
1340 fun varargize
(mpropdef
: MMethodDef, map
: nullable SignatureMap, recv
: RuntimeVariable, args
: SequenceRead[AExpr]): Array[RuntimeVariable]
1342 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
1343 var res
= new Array[RuntimeVariable]
1346 if msignature
.arity
== 0 then return res
1349 assert args
.length
== msignature
.arity
1351 res
.add
self.expr
(ne
, null)
1356 # Eval in order of arguments, not parameters
1357 var exprs
= new Array[RuntimeVariable].with_capacity
(args
.length
)
1359 exprs
.add
self.expr
(ne
, null)
1362 # Fill `res` with the result of the evaluation according to the mapping
1363 for i
in [0..msignature
.arity
[ do
1364 var param
= msignature
.mparameters
[i
]
1365 var j
= map
.map
.get_or_null
(i
)
1368 res
.add
(null_instance
)
1371 if param
.is_vararg
and args
[i
].vararg_decl
> 0 then
1372 var vararg
= exprs
.sub
(j
, args
[i
].vararg_decl
)
1373 var elttype
= param
.mtype
1374 var arg
= self.vararg_instance
(mpropdef
, recv
, vararg
, elttype
)
1385 # Anchor a type to the main module and the current receiver
1386 fun anchor
(mtype
: MType): MType
1388 if not mtype
.need_anchor
then return mtype
1389 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
1392 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
1394 if not mtype
.need_anchor
then return mtype
1395 return mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
1398 # Unsafely cast a value to a new type
1399 # ie the result share the same C variable but my have a different mcasttype
1400 # NOTE: if the adaptation is useless then `value` is returned as it.
1401 # ENSURE: `result.name == value.name`
1402 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1404 mtype
= self.anchor
(mtype
)
1405 var valmtype
= value
.mcasttype
1407 # CPrimitive is the best you can do
1408 if valmtype
.is_c_primitive
then
1412 # Do nothing if useless autocast
1413 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1417 # Just as_not_null if the target is not nullable.
1419 # eg `nullable PreciseType` adapted to `Object` gives precisetype.
1420 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1421 mtype
= valmtype
.mtype
1424 var res
= new RuntimeVariable(value
.name
, value
.mtype
, mtype
)
1428 # Generate a super call from a method definition
1429 fun supercall
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1431 # Adapt the arguments of a method according to targetted `MMethodDef`
1432 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
1434 # Unbox all the arguments of a method when implemented `extern` or `intern`
1435 fun unbox_signature_extern
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
1437 # Box or unbox a value to another type iff a C type conversion is needed
1438 # ENSURE: `result.mtype.ctype == mtype.ctype`
1439 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1441 # Box extern classes to be used in the generated code
1442 fun box_extern
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1444 # Unbox extern classes to be used in extern code (legacy NI and FFI)
1445 fun unbox_extern
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
1447 # Generate a polymorphic subtype test
1448 fun type_test
(value
: RuntimeVariable, mtype
: MType, tag
: String): RuntimeVariable is abstract
1450 # Generate the code required to dynamically check if 2 objects share the same runtime type
1451 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
1453 # Generate a Nit "is" for two runtime_variables
1454 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
1458 # Generate a static call on a method definition
1459 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1461 # Generate a polymorphic send for the method `m` and the arguments `args`
1462 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
1464 # Generate a monomorphic send for the method `m`, the type `t` and the arguments `args`
1465 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1467 assert t
isa MClassType
1468 var propdef
= m
.lookup_first_definition
(self.compiler
.mainmodule
, t
)
1469 return self.call
(propdef
, t
, args
)
1472 # Generate a monomorphic super send from the method `m`, the type `t` and the arguments `args`
1473 fun monomorphic_super_send
(m
: MMethodDef, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1475 assert t
isa MClassType
1476 m
= m
.lookup_next_definition
(self.compiler
.mainmodule
, t
)
1477 return self.call
(m
, t
, args
)
1480 # Attributes handling
1482 # Generate a polymorphic attribute is_set test
1483 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
1485 # Generate a polymorphic attribute read
1486 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
1488 # Generate a polymorphic attribute write
1489 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable) is abstract
1493 # Can value be null? (according to current knowledge)
1494 fun maybe_null
(value
: RuntimeVariable): Bool
1496 return value
.mcasttype
isa MNullableType or value
.mcasttype
isa MNullType
1499 # Add a check and an abort for a null receiver if needed
1500 fun check_recv_notnull
(recv
: RuntimeVariable)
1502 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_null
.value
then return
1504 if maybe_null
(recv
) then
1505 self.add
("if (unlikely({recv} == NULL)) \{")
1506 self.add_abort
("Receiver is null")
1513 private var names
= new HashSet[String]
1514 private var last
: Int = 0
1516 # Return a new name based on `s` and unique in the visitor
1517 fun get_name
(s
: String): String
1519 if not self.names
.has
(s
) then
1523 var i
= self.last
+ 1
1526 if not self.names
.has
(s2
) then
1535 # Return an unique and stable identifier associated with an escapemark
1536 fun escapemark_name
(e
: nullable EscapeMark): String
1539 if frame
.escapemark_names
.has_key
(e
) then return frame
.escapemark_names
[e
]
1541 if name
== null then name
= "label"
1542 name
= get_name
(name
)
1543 frame
.escapemark_names
[e
] = name
1547 # Insert a C label for associated with an escapemark
1548 fun add_escape_label
(e
: nullable EscapeMark)
1550 if e
== null then return
1551 if e
.escapes
.is_empty
then return
1552 add
("BREAK_{escapemark_name(e)}: (void)0;")
1555 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1556 # NOTE: we do not return a `RuntimeVariable` "CString" as the class may not exist in the module/program
1557 fun class_name_string
(value
: RuntimeVariable): String is abstract
1559 # Variables handling
1561 protected var variables
= new HashMap[Variable, RuntimeVariable]
1563 # Return the local runtime_variable associated to a Nit local variable
1564 fun variable
(variable
: Variable): RuntimeVariable
1566 if self.variables
.has_key
(variable
) then
1567 return self.variables
[variable
]
1569 var name
= self.get_name
("var_{variable.name}")
1570 var mtype
= variable
.declared_type
.as(not null)
1571 mtype
= self.anchor
(mtype
)
1572 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1573 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1574 self.variables
[variable
] = res
1579 # Return a new uninitialized local runtime_variable
1580 fun new_var
(mtype
: MType): RuntimeVariable
1582 mtype
= self.anchor
(mtype
)
1583 var name
= self.get_name
("var")
1584 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1585 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1589 # The difference with `new_var` is the C static type of the local variable
1590 fun new_var_extern
(mtype
: MType): RuntimeVariable
1592 mtype
= self.anchor
(mtype
)
1593 var name
= self.get_name
("var")
1594 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1595 self.add_decl
("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
1599 # Return a new uninitialized named runtime_variable
1600 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
1602 mtype
= self.anchor
(mtype
)
1603 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1604 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1608 # Correctly assign a left and a right value
1609 # Boxing and unboxing is performed if required
1610 fun assign
(left
, right
: RuntimeVariable)
1612 right
= self.autobox
(right
, left
.mtype
)
1613 self.add
("{left} = {right};")
1616 # Generate instances
1618 # Generate a alloc-instance + init-attributes
1619 fun init_instance
(mtype
: MClassType): RuntimeVariable is abstract
1621 # Allocate and init attributes of an instance of a standard or extern class
1623 # Does not support universals and the pseudo-internal `NativeArray` class.
1624 fun init_instance_or_extern
(mtype
: MClassType): RuntimeVariable
1627 var ctype
= mtype
.ctype
1628 assert mtype
.mclass
.name
!= "NativeArray"
1629 if not mtype
.is_c_primitive
then
1630 recv
= init_instance
(mtype
)
1631 else if ctype
== "char*" then
1632 recv
= new_expr
("NULL/*special!*/", mtype
)
1634 recv
= new_expr
("({ctype})0/*special!*/", mtype
)
1639 # Set a GC finalizer on `recv`, only if `recv` isa Finalizable
1640 fun set_finalizer
(recv
: RuntimeVariable)
1642 var mtype
= recv
.mtype
1643 var finalizable_type
= compiler
.mainmodule
.finalizable_type
1644 if finalizable_type
!= null and not mtype
.need_anchor
and
1645 mtype
.is_subtype
(compiler
.mainmodule
, null, finalizable_type
) then
1646 add
"gc_register_finalizer({recv});"
1650 # The currently processed module
1652 # alias for `compiler.mainmodule`
1653 fun mmodule
: MModule do return compiler
.mainmodule
1655 # Generate an integer value
1656 fun int_instance
(value
: Int): RuntimeVariable
1658 var t
= mmodule
.int_type
1659 var res
= new RuntimeVariable("{value.to_s}l", t
, t
)
1663 # Generate a byte value
1664 fun byte_instance
(value
: Byte): RuntimeVariable
1666 var t
= mmodule
.byte_type
1667 var res
= new RuntimeVariable("((unsigned char){value.to_s})", t
, t
)
1671 # Generate an int8 value
1672 fun int8_instance
(value
: Int8): RuntimeVariable
1674 var t
= mmodule
.int8_type
1675 var res
= new RuntimeVariable("INT8_C({value.to_s})", t
, t
)
1679 # Generate an int16 value
1680 fun int16_instance
(value
: Int16): RuntimeVariable
1682 var t
= mmodule
.int16_type
1683 var res
= new RuntimeVariable("INT16_C({value.to_s})", t
, t
)
1687 # Generate a uint16 value
1688 fun uint16_instance
(value
: UInt16): RuntimeVariable
1690 var t
= mmodule
.uint16_type
1691 var res
= new RuntimeVariable("UINT16_C({value.to_s})", t
, t
)
1695 # Generate an int32 value
1696 fun int32_instance
(value
: Int32): RuntimeVariable
1698 var t
= mmodule
.int32_type
1699 var res
= new RuntimeVariable("INT32_C({value.to_s})", t
, t
)
1703 # Generate a uint32 value
1704 fun uint32_instance
(value
: UInt32): RuntimeVariable
1706 var t
= mmodule
.uint32_type
1707 var res
= new RuntimeVariable("UINT32_C({value.to_s})", t
, t
)
1711 # Generate a char value
1712 fun char_instance
(value
: Char): RuntimeVariable
1714 var t
= mmodule
.char_type
1716 if value
.code_point
< 128 then
1717 return new RuntimeVariable("'{value.to_s.escape_to_c}'", t
, t
)
1719 return new RuntimeVariable("{value.code_point}", t
, t
)
1723 # Generate a float value
1725 # FIXME pass a Float, not a string
1726 fun float_instance
(value
: String): RuntimeVariable
1728 var t
= mmodule
.float_type
1729 var res
= new RuntimeVariable("{value}", t
, t
)
1733 # Generate an integer value
1734 fun bool_instance
(value
: Bool): RuntimeVariable
1736 var s
= if value
then "1" else "0"
1737 var res
= new RuntimeVariable(s
, bool_type
, bool_type
)
1741 # Generate the `null` value
1742 fun null_instance
: RuntimeVariable
1744 var t
= compiler
.mainmodule
.model
.null_type
1745 var res
= new RuntimeVariable("((val*)NULL)", t
, t
)
1749 # Generates a CString instance fully escaped in C-style \xHH fashion
1750 fun c_string_instance
(ns
: CString, len
: Int): RuntimeVariable do
1751 var mtype
= mmodule
.c_string_type
1752 var nat
= new_var
(mtype
)
1753 var byte_esc
= new Buffer.with_cap
(len
* 4)
1754 for i
in [0 .. len
[ do
1755 byte_esc
.append
("\\x{ns[i].to_s.substring_from(2)}")
1757 self.add
("{nat} = \"{byte_esc}\
";")
1761 # Generate a string value
1762 fun string_instance
(string
: String): RuntimeVariable
1764 var mtype
= mmodule
.string_type
1765 var name
= self.get_name
("varonce")
1766 self.add_decl
("static {mtype.ctype} {name};")
1767 var res
= self.new_var
(mtype
)
1768 self.add
("if (likely({name}!=NULL)) \{")
1769 self.add
("{res} = {name};")
1770 self.add
("\} else \{")
1771 var native_mtype
= mmodule
.c_string_type
1772 var nat
= self.new_var
(native_mtype
)
1773 self.add
("{nat} = \"{string.escape_to_c}\
";")
1774 var byte_length
= self.int_instance
(string
.byte_length
)
1775 var unilen
= self.int_instance
(string
.length
)
1776 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)};")
1777 self.add
("{name} = {res};")
1782 fun value_instance
(object
: Object): RuntimeVariable
1784 if object
isa Int then
1785 return int_instance
(object
)
1786 else if object
isa Bool then
1787 return bool_instance
(object
)
1788 else if object
isa String then
1789 return string_instance
(object
)
1795 # Generate an array value
1796 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
1798 # Get an instance of a array for a vararg
1799 fun vararg_instance
(mpropdef
: MPropDef, recv
: RuntimeVariable, varargs
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
1803 # Add a line in the main part of the generated C
1804 fun add
(s
: String) do self.writer
.lines
.add
(s
)
1807 # (used for local or global declaration)
1808 fun add_decl
(s
: String) do self.writer
.decl_lines
.add
(s
)
1810 # Request the presence of a global declaration
1811 fun require_declaration
(key
: String)
1813 var reqs
= self.writer
.file
.required_declarations
1814 if reqs
.has
(key
) then return
1816 var node
= current_node
1817 if node
!= null then compiler
.requirers_of_declarations
[key
] = node
1820 # Add a declaration in the local-header
1821 # The declaration is ensured to be present once
1822 fun declare_once
(s
: String)
1824 self.compiler
.provide_declaration
(s
, s
)
1825 self.require_declaration
(s
)
1828 # Look for a needed .h and .c file for a given module
1829 # This is used for the legacy FFI
1830 fun add_extern
(mmodule
: MModule)
1832 var file
= mmodule
.filepath
1833 file
= file
.strip_extension
(".nit")
1834 var tryfile
= file
+ ".nit.h"
1835 if tryfile
.file_exists
then
1836 self.declare_once
("#include \"{tryfile.basename}\
"")
1837 self.compiler
.files_to_copy
.add
(tryfile
)
1839 tryfile
= file
+ "_nit.h"
1840 if tryfile
.file_exists
then
1841 self.declare_once
("#include \"{tryfile.basename}\
"")
1842 self.compiler
.files_to_copy
.add
(tryfile
)
1845 if self.compiler
.seen_extern
.has
(file
) then return
1846 self.compiler
.seen_extern
.add
(file
)
1847 tryfile
= file
+ ".nit.c"
1848 if not tryfile
.file_exists
then
1849 tryfile
= file
+ "_nit.c"
1850 if not tryfile
.file_exists
then return
1852 var f
= new ExternCFile(tryfile
.basename
, "")
1853 self.compiler
.extern_bodies
.add
(f
)
1854 self.compiler
.files_to_copy
.add
(tryfile
)
1857 # Return a new local runtime_variable initialized with the C expression `cexpr`.
1858 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
1860 var res
= new_var
(mtype
)
1861 self.add
("{res} = {cexpr};")
1865 # Generate generic abort
1866 # used by aborts, asserts, casts, etc.
1867 fun add_abort
(message
: String)
1870 self.add
("PRINT_ERROR(\"Runtime error
: %s\
", \"{message.escape_to_c}\
");")
1874 # Generate a long jump if there is a catch block.
1876 # This method should be called before the error messages and before a `add_raw_abort`.
1879 self.add
("if(catchStack.cursor >= 0)\{")
1880 self.add
("longjmp(catchStack.envs[catchStack.cursor], 1);")
1884 # Generate abort without a message.
1886 # Used when one need a more complex message.
1887 # Do not forget to call `add_raw_abort` before the display of a custom user message.
1890 var current_node
= self.current_node
1891 if current_node
!= null and current_node
.location
.file
!= null and
1892 current_node
.location
.file
.mmodule
!= null then
1893 var f
= "FILE_{self.current_node.location.file.mmodule.c_name}"
1894 self.require_declaration
(f
)
1895 self.add
("PRINT_ERROR(\" (%s
:%d
)\\n\
", {f}, {current_node.location.line_start});")
1897 self.add
("PRINT_ERROR(\"\\n\
");")
1899 self.add
("fatal_exit(1);")
1902 # Add a dynamic cast
1903 fun add_cast
(value
: RuntimeVariable, mtype
: MType, tag
: String)
1905 var res
= self.type_test
(value
, mtype
, tag
)
1906 self.add
("if (unlikely(!{res})) \{")
1908 var cn
= self.class_name_string
(value
)
1909 self.add
("PRINT_ERROR(\"Runtime error
: Cast failed
. Expected `%s`, got
`%s`\
", \"{mtype.to_s.escape_to_c}\
", {cn});")
1914 # Generate a return with the value `s`
1915 fun ret
(s
: RuntimeVariable)
1917 self.assign
(self.frame
.returnvar
.as(not null), s
)
1918 self.add
("goto {self.frame.returnlabel.as(not null)};")
1921 # Compile a statement (if any)
1922 fun stmt
(nexpr
: nullable AExpr)
1924 if nexpr
== null then return
1925 if nexpr
.is_broken
then
1926 # Untyped expression.
1927 # Might mean dead code or invalid code
1929 add_abort
("FATAL: bad statement executed.")
1933 var narray
= nexpr
.comprehension
1934 if narray
!= null then
1935 var recv
= frame
.comprehension
.as(not null)
1936 var val
= expr
(nexpr
, narray
.element_mtype
)
1937 compile_callsite
(narray
.push_callsite
.as(not null), [recv
, val
])
1941 var old
= self.current_node
1942 self.current_node
= nexpr
1944 self.current_node
= old
1947 # Compile an expression an return its result
1948 # `mtype` is the expected return type, pass null if no specific type is expected.
1949 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
1951 var old
= self.current_node
1952 self.current_node
= nexpr
1955 if nexpr
.mtype
!= null then
1956 res
= nexpr
.expr
(self)
1960 # Untyped expression.
1961 # Might mean dead code or invalid code.
1963 add_abort
("FATAL: bad expression executed.")
1964 # and return a placebo result to please the C compiler
1965 if mtype
== null then mtype
= compiler
.mainmodule
.object_type
1966 res
= new_var
(mtype
)
1968 self.current_node
= old
1972 if mtype
!= null then
1973 mtype
= self.anchor
(mtype
)
1974 res
= self.autobox
(res
, mtype
)
1976 res
= autoadapt
(res
, nexpr
.mtype
.as(not null))
1977 var implicit_cast_to
= nexpr
.implicit_cast_to
1978 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
1979 add_cast
(res
, implicit_cast_to
, "auto")
1980 res
= autoadapt
(res
, implicit_cast_to
)
1982 self.current_node
= old
1986 # Alias for `self.expr(nexpr, self.bool_type)`
1987 fun expr_bool
(nexpr
: AExpr): RuntimeVariable do return expr
(nexpr
, bool_type
)
1989 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1990 fun debug
(message
: String)
1992 var node
= self.current_node
1993 if node
== null then
1994 print
"?: {message}"
1998 self.add
("/* DEBUG: {message} */")
2002 # A C function associated to a Nit method
2003 # Because of customization, a given Nit method can be compiler more that once
2004 abstract class AbstractRuntimeFunction
2006 type COMPILER: AbstractCompiler
2007 type VISITOR: AbstractCompilerVisitor
2009 # The associated Nit method
2010 var mmethoddef
: MMethodDef
2012 # The mangled c name of the runtime_function
2013 # Subclasses should redefine `build_c_name` instead
2016 var res
= self.c_name_cache
2017 if res
!= null then return res
2018 res
= self.build_c_name
2019 self.c_name_cache
= res
2023 # Non cached version of `c_name`
2024 protected fun build_c_name
: String is abstract
2026 protected var c_name_cache
: nullable String = null is writable
2028 # Implements a call of the runtime_function
2029 # May inline the body or generate a C function call
2030 fun call
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
2032 # Generate the code for the `AbstractRuntimeFunction`
2033 # Warning: compile more than once compilation makes CC unhappy
2034 fun compile_to_c
(compiler
: COMPILER) is abstract
2037 # A runtime variable hold a runtime value in C.
2038 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
2040 # 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.
2041 class RuntimeVariable
2042 # The name of the variable in the C code
2045 # The static type of the variable (as declard in C)
2048 # The current casted type of the variable (as known in Nit)
2049 var mcasttype
: MType is writable
2051 # If the variable exaclty a mcasttype?
2052 # false (usual value) means that the variable is a mcasttype or a subtype.
2053 var is_exact
: Bool = false is writable
2057 assert not mtype
.need_anchor
2058 assert not mcasttype
.need_anchor
2061 redef fun to_s
do return name
2066 if self.is_exact
then
2067 exact_str
= " exact"
2072 if self.mtype
== self.mcasttype
then
2073 type_str
= "{mtype}{exact_str}"
2075 type_str
= "{mtype}({mcasttype}{exact_str})"
2077 return "<{name}:{type_str}>"
2081 # The static context of a visited property in a `AbstractCompilerVisitor`
2084 type VISITOR: AbstractCompilerVisitor
2086 # The associated visitor
2087 var visitor
: VISITOR
2089 # The executed property.
2090 # A Method in case of a call, an attribute in case of a default initialization.
2091 var mpropdef
: MPropDef
2093 # The static type of the receiver
2094 var receiver
: MClassType
2096 # Arguments of the method (the first is the receiver)
2097 var arguments
: Array[RuntimeVariable]
2099 # The runtime_variable associated to the return (in a function)
2100 var returnvar
: nullable RuntimeVariable = null is writable
2102 # The label at the end of the property
2103 var returnlabel
: nullable String = null is writable
2105 # Labels associated to a each escapemarks.
2106 # Because of inlinings, escape-marks must be associated to their context (the frame)
2107 private var escapemark_names
= new HashMap[EscapeMark, String]
2109 # The array comprehension currently filled, if any
2110 private var comprehension
: nullable RuntimeVariable = null
2114 # Return the C type associated to a given Nit static type
2115 fun ctype
: String do return "val*"
2117 # C type outside of the compiler code and in boxes
2118 fun ctype_extern
: String do return "val*"
2120 # Short name of the `ctype` to use in unions
2121 fun ctypename
: String do return "val"
2123 # Is the associated C type a primitive one?
2125 # ENSURE `result == (ctype != "val*")`
2126 fun is_c_primitive
: Bool do return false
2129 redef class MClassType
2131 redef var ctype
is lazy
do
2132 if mclass
.name
== "Int" then
2134 else if mclass
.name
== "Bool" then
2136 else if mclass
.name
== "Char" then
2138 else if mclass
.name
== "Float" then
2140 else if mclass
.name
== "Int8" then
2142 else if mclass
.name
== "Byte" then
2143 return "unsigned char"
2144 else if mclass
.name
== "Int16" then
2146 else if mclass
.name
== "UInt16" then
2148 else if mclass
.name
== "Int32" then
2150 else if mclass
.name
== "UInt32" then
2152 else if mclass
.name
== "CString" then
2154 else if mclass
.name
== "NativeArray" then
2161 redef var is_c_primitive
is lazy
do return ctype
!= "val*"
2163 redef fun ctype_extern
: String
2165 if mclass
.kind
== extern_kind
then
2172 redef fun ctypename
: String
2174 if mclass
.name
== "Int" then
2176 else if mclass
.name
== "Bool" then
2178 else if mclass
.name
== "Char" then
2180 else if mclass
.name
== "Float" then
2182 else if mclass
.name
== "Int8" then
2184 else if mclass
.name
== "Byte" then
2186 else if mclass
.name
== "Int16" then
2188 else if mclass
.name
== "UInt16" then
2190 else if mclass
.name
== "Int32" then
2192 else if mclass
.name
== "UInt32" then
2194 else if mclass
.name
== "CString" then
2196 else if mclass
.name
== "NativeArray" then
2197 #return "{self.arguments.first.ctype}*"
2205 redef class MPropDef
2206 type VISITOR: AbstractCompilerVisitor
2209 redef class MMethodDef
2210 # Can the body be inlined?
2211 fun can_inline
(v
: VISITOR): Bool
2213 if is_abstract
then return true
2214 if constant_value
!= null then return true
2215 var modelbuilder
= v
.compiler
.modelbuilder
2216 var node
= modelbuilder
.mpropdef2node
(self)
2217 if node
isa APropdef then
2218 return node
.can_inline
2219 else if node
isa AClassdef then
2220 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
2222 else if node
== null then
2229 # Inline the body in another visitor
2230 fun compile_inside_to_c
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
2232 var modelbuilder
= v
.compiler
.modelbuilder
2233 var val
= constant_value
2234 var node
= modelbuilder
.mpropdef2node
(self)
2238 var cn
= v
.class_name_string
(arguments
.first
)
2239 v
.current_node
= node
2240 v
.add
("PRINT_ERROR(\"Runtime error
: Abstract method
`%s` called on
`%s`\
", \"{mproperty.name.escape_to_c}\
", {cn});")
2245 if node
isa APropdef then
2246 var oldnode
= v
.current_node
2247 v
.current_node
= node
2248 self.compile_parameter_check
(v
, arguments
)
2249 node
.compile_to_c
(v
, self, arguments
)
2250 v
.current_node
= oldnode
2251 else if node
isa AClassdef then
2252 var oldnode
= v
.current_node
2253 v
.current_node
= node
2254 self.compile_parameter_check
(v
, arguments
)
2255 node
.compile_to_c
(v
, self, arguments
)
2256 v
.current_node
= oldnode
2257 else if val
!= null then
2258 v
.ret
(v
.value_instance
(val
))
2265 # Generate type checks in the C code to check covariant parameters
2266 fun compile_parameter_check
(v
: VISITOR, arguments
: Array[RuntimeVariable])
2268 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_covariance
.value
then return
2270 var msignature
= self.msignature
.as(not null)
2272 for i
in [0..msignature
.arity
[ do
2273 var mp
= msignature
.mparameters
[i
]
2274 # skip test for vararg since the array is instantiated with the correct polymorphic type
2275 if mp
.is_vararg
then continue
2277 # skip if the cast is not required
2278 var origmtype
= self.mproperty
.intro
.msignature
.mparameters
[i
].mtype
2279 if not origmtype
.need_anchor
then continue
2281 # get the parameter type
2282 var mtype
= mp
.mtype
2285 # note that v decides if and how to implements the cast
2286 v
.add
("/* Covariant cast for argument {i} ({mp.name}) {arguments[i+1].inspect} isa {mtype} */")
2287 v
.add_cast
(arguments
[i
+1], mtype
, "covariance")
2294 redef class APropdef
2295 fun compile_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
2297 v
.add
("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
2298 debug
("Not yet implemented")
2301 fun can_inline
: Bool do return true
2304 redef class AMethPropdef
2305 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2307 # Call the implicit super-init
2308 var auto_super_inits
= self.auto_super_inits
2309 if auto_super_inits
!= null then
2310 var args
= [arguments
.first
]
2311 for auto_super_init
in auto_super_inits
do
2312 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
2314 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
2315 args
.add
(arguments
[i
])
2317 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
2318 v
.compile_callsite
(auto_super_init
, args
)
2321 if auto_super_call
then
2322 v
.supercall
(mpropdef
, arguments
.first
.mtype
.as(MClassType), arguments
)
2325 # Try special compilation
2326 if mpropdef
.is_intern
then
2327 if compile_intern_to_c
(v
, mpropdef
, arguments
) then return
2329 if mpropdef
.is_extern
then
2330 if mpropdef
.mproperty
.is_init
then
2331 if compile_externinit_to_c
(v
, mpropdef
, arguments
) then return
2333 if compile_externmeth_to_c
(v
, mpropdef
, arguments
) then return
2337 # Compile block if any
2338 var n_block
= n_block
2339 if n_block
!= null then
2340 for i
in [0..mpropdef
.msignature
.arity
[ do
2341 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
2342 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
2350 var cn
= v
.class_name_string
(arguments
.first
)
2351 v
.add
("PRINT_ERROR(\"Runtime error
: uncompiled method
`%s` called on
`%s`. NOT YET IMPLEMENTED\
", \"{mpropdef.mproperty.name.escape_to_c}\
", {cn});")
2355 redef fun can_inline
2357 if self.auto_super_inits
!= null then return false
2358 var nblock
= self.n_block
2359 if nblock
== null then return true
2360 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
2361 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
2365 fun compile_intern_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
2367 var pname
= mpropdef
.mproperty
.name
2368 var cname
= mpropdef
.mclassdef
.mclass
.name
2369 var ret
= mpropdef
.msignature
.return_mtype
2371 ret
= v
.resolve_for
(ret
, arguments
.first
)
2373 if pname
!= "==" and pname
!= "!=" then
2374 v
.adapt_signature
(mpropdef
, arguments
)
2375 v
.unbox_signature_extern
(mpropdef
, arguments
)
2377 if cname
== "Int" then
2378 if pname
== "output" then
2379 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
2381 else if pname
== "object_id" then
2382 v
.ret
(arguments
.first
)
2384 else if pname
== "+" then
2385 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2387 else if pname
== "-" then
2388 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2390 else if pname
== "unary -" then
2391 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2393 else if pname
== "unary +" then
2396 else if pname
== "*" then
2397 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2399 else if pname
== "/" then
2400 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2402 else if pname
== "%" then
2403 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2405 else if pname
== "==" then
2406 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2408 else if pname
== "!=" then
2409 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2410 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2412 else if pname
== "<" then
2413 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2415 else if pname
== ">" then
2416 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2418 else if pname
== "<=" then
2419 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2421 else if pname
== ">=" then
2422 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2424 else if pname
== "to_i8" then
2425 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2427 else if pname
== "to_i16" then
2428 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2430 else if pname
== "to_u16" then
2431 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2433 else if pname
== "to_i32" then
2434 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2436 else if pname
== "to_u32" then
2437 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2439 else if pname
== "to_f" then
2440 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2442 else if pname
== "to_b" then
2443 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2445 else if pname
== "code_point" then
2446 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2448 else if pname
== "&" then
2449 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2451 else if pname
== "|" then
2452 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
2454 else if pname
== ">>" then
2455 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2457 else if pname
== "<<" then
2458 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2461 else if cname
== "Char" then
2462 if pname
== "object_id" then
2463 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2465 else if pname
== "successor" then
2466 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2468 else if pname
== "predecessor" then
2469 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2471 else if pname
== "==" then
2472 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2474 else if pname
== "!=" then
2475 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2476 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2478 else if pname
== "<" then
2479 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2481 else if pname
== ">" then
2482 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2484 else if pname
== "<=" then
2485 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2487 else if pname
== ">=" then
2488 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2490 else if pname
== "to_i" then
2491 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
2493 else if pname
== "code_point" then
2494 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2497 else if cname
== "Byte" then
2498 if pname
== "output" then
2499 v
.add
("printf(\"%x\\n\
", {arguments.first});")
2501 else if pname
== "object_id" then
2502 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2504 else if pname
== "+" then
2505 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2507 else if pname
== "-" then
2508 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2510 else if pname
== "unary -" then
2511 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2513 else if pname
== "unary +" then
2516 else if pname
== "*" then
2517 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2519 else if pname
== "/" then
2520 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2522 else if pname
== "%" then
2523 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2525 else if pname
== "==" then
2526 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2528 else if pname
== "!=" then
2529 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2530 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2532 else if pname
== "<" then
2533 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2535 else if pname
== ">" then
2536 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2538 else if pname
== "<=" then
2539 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2541 else if pname
== ">=" then
2542 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2544 else if pname
== ">>" then
2545 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2547 else if pname
== "<<" then
2548 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2550 else if pname
== "&" then
2551 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2553 else if pname
== "to_i" then
2554 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2556 else if pname
== "to_f" then
2557 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2559 else if pname
== "to_i8" then
2560 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2562 else if pname
== "to_i16" then
2563 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2565 else if pname
== "to_u16" then
2566 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2568 else if pname
== "to_i32" then
2569 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2571 else if pname
== "to_u32" then
2572 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2574 else if pname
== "ascii" then
2575 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2578 else if cname
== "Bool" then
2579 if pname
== "output" then
2580 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
2582 else if pname
== "object_id" then
2583 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2585 else if pname
== "==" then
2586 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2588 else if pname
== "!=" then
2589 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2590 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2593 else if cname
== "Float" then
2594 if pname
== "output" then
2595 v
.add
("printf(\"%f\\n\
", {arguments.first});")
2597 else if pname
== "object_id" then
2598 v
.ret
(v
.new_expr
("(double){arguments.first}", ret
.as(not null)))
2600 else if pname
== "+" then
2601 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2603 else if pname
== "-" then
2604 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2606 else if pname
== "unary -" then
2607 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2609 else if pname
== "unary +" then
2612 else if pname
== "succ" then
2613 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
2615 else if pname
== "prec" then
2616 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
2618 else if pname
== "*" then
2619 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2621 else if pname
== "/" then
2622 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2624 else if pname
== "==" then
2625 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2627 else if pname
== "!=" then
2628 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2629 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2631 else if pname
== "<" then
2632 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2634 else if pname
== ">" then
2635 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2637 else if pname
== "<=" then
2638 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2640 else if pname
== ">=" then
2641 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2643 else if pname
== "to_i" then
2644 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2646 else if pname
== "to_b" then
2647 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2649 else if pname
== "to_i8" then
2650 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2652 else if pname
== "to_i16" then
2653 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2655 else if pname
== "to_u16" then
2656 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2658 else if pname
== "to_i32" then
2659 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2661 else if pname
== "to_u32" then
2662 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2665 else if cname
== "CString" then
2666 if pname
== "[]" then
2667 v
.ret
(v
.new_expr
("(unsigned char)((int){arguments[0]}[{arguments[1]}])", ret
.as(not null)))
2669 else if pname
== "[]=" then
2670 v
.add
("{arguments[0]}[{arguments[1]}]=(unsigned char){arguments[2]};")
2672 else if pname
== "copy_to" then
2673 v
.add
("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2675 else if pname
== "atoi" then
2676 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
2678 else if pname
== "fast_cstring" then
2679 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2681 else if pname
== "==" then
2682 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2684 else if pname
== "!=" then
2685 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2686 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2688 else if pname
== "new" then
2689 var alloc
= v
.nit_alloc
(arguments
[1].to_s
, "CString")
2690 v
.ret
(v
.new_expr
("(char*){alloc}", ret
.as(not null)))
2692 else if pname
== "fetch_4_chars" then
2693 v
.ret
(v
.new_expr
("*((uint32_t*)({arguments[0]} + {arguments[1]}))", ret
.as(not null)))
2695 else if pname
== "fetch_4_hchars" then
2696 v
.ret
(v
.new_expr
("(uint32_t)be32toh(*((uint32_t*)({arguments[0]} + {arguments[1]})))", ret
.as(not null)))
2699 else if cname
== "NativeArray" then
2700 return v
.native_array_def
(pname
, ret
, arguments
)
2701 else if cname
== "Int8" then
2702 if pname
== "output" then
2703 v
.add
("printf(\"%\
"PRIi8 \"\\n\
", {arguments.first});")
2705 else if pname
== "object_id" then
2706 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2708 else if pname
== "+" then
2709 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2711 else if pname
== "-" then
2712 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2714 else if pname
== "unary -" then
2715 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2717 else if pname
== "unary +" then
2720 else if pname
== "*" then
2721 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2723 else if pname
== "/" then
2724 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2726 else if pname
== "%" then
2727 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2729 else if pname
== "<<" then
2730 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2732 else if pname
== ">>" then
2733 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2735 else if pname
== "==" then
2736 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2738 else if pname
== "!=" then
2739 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2740 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2742 else if pname
== "<" then
2743 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2745 else if pname
== ">" then
2746 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2748 else if pname
== "<=" then
2749 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2751 else if pname
== ">=" then
2752 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2754 else if pname
== "to_i" then
2755 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2757 else if pname
== "to_b" then
2758 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2760 else if pname
== "to_i16" then
2761 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2763 else if pname
== "to_u16" then
2764 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2766 else if pname
== "to_i32" then
2767 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2769 else if pname
== "to_u32" then
2770 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2772 else if pname
== "to_f" then
2773 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2775 else if pname
== "&" then
2776 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2778 else if pname
== "|" then
2779 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
2781 else if pname
== "^" then
2782 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
2784 else if pname
== "unary ~" then
2785 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
2788 else if cname
== "Int16" then
2789 if pname
== "output" then
2790 v
.add
("printf(\"%\
"PRIi16 \"\\n\
", {arguments.first});")
2792 else if pname
== "object_id" then
2793 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2795 else if pname
== "+" then
2796 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2798 else if pname
== "-" then
2799 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2801 else if pname
== "unary -" then
2802 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2804 else if pname
== "unary +" then
2807 else if pname
== "*" then
2808 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2810 else if pname
== "/" then
2811 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2813 else if pname
== "%" then
2814 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2816 else if pname
== "<<" then
2817 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2819 else if pname
== ">>" then
2820 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2822 else if pname
== "==" then
2823 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2825 else if pname
== "!=" then
2826 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2827 v
.ret
(v
.new_expr
("!{res}", 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
== ">" then
2833 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2835 else if pname
== "<=" then
2836 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2838 else if pname
== ">=" then
2839 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2841 else if pname
== "to_i" then
2842 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2844 else if pname
== "to_b" then
2845 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2847 else if pname
== "to_i8" then
2848 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2850 else if pname
== "to_u16" then
2851 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
2853 else if pname
== "to_i32" then
2854 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2856 else if pname
== "to_u32" then
2857 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2859 else if pname
== "to_f" then
2860 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2862 else if pname
== "&" then
2863 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2865 else if pname
== "|" then
2866 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
2868 else if pname
== "^" then
2869 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
2871 else if pname
== "unary ~" then
2872 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
2875 else if cname
== "UInt16" then
2876 if pname
== "output" then
2877 v
.add
("printf(\"%\
"PRIu16 \"\\n\
", {arguments.first});")
2879 else if pname
== "object_id" then
2880 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2882 else if pname
== "+" then
2883 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2885 else if pname
== "-" then
2886 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2888 else if pname
== "unary -" then
2889 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2891 else if pname
== "unary +" then
2894 else if pname
== "*" then
2895 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2897 else if pname
== "/" then
2898 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2900 else if pname
== "%" then
2901 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2903 else if pname
== "<<" then
2904 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2906 else if pname
== ">>" then
2907 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2909 else if pname
== "==" then
2910 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2912 else if pname
== "!=" then
2913 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2914 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2916 else if pname
== "<" then
2917 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2919 else if pname
== ">" then
2920 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", 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
== "to_i" then
2929 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2931 else if pname
== "to_b" then
2932 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2934 else if pname
== "to_i8" then
2935 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
2937 else if pname
== "to_i16" then
2938 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
2940 else if pname
== "to_i32" then
2941 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
2943 else if pname
== "to_u32" then
2944 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
2946 else if pname
== "to_f" then
2947 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
2949 else if pname
== "&" then
2950 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
2952 else if pname
== "|" then
2953 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
2955 else if pname
== "^" then
2956 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
2958 else if pname
== "unary ~" then
2959 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
2962 else if cname
== "Int32" then
2963 if pname
== "output" then
2964 v
.add
("printf(\"%\
"PRIi32 \"\\n\
", {arguments.first});")
2966 else if pname
== "object_id" then
2967 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2969 else if pname
== "+" then
2970 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2972 else if pname
== "-" then
2973 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2975 else if pname
== "unary -" then
2976 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2978 else if pname
== "unary +" then
2981 else if pname
== "*" then
2982 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2984 else if pname
== "/" then
2985 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2987 else if pname
== "%" then
2988 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
2990 else if pname
== "<<" then
2991 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
2993 else if pname
== ">>" then
2994 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
2996 else if pname
== "==" then
2997 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2999 else if pname
== "!=" then
3000 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3001 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3003 else if pname
== "<" then
3004 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3006 else if pname
== ">" then
3007 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3009 else if pname
== "<=" then
3010 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3012 else if pname
== ">=" then
3013 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3015 else if pname
== "to_i" then
3016 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3018 else if pname
== "to_b" then
3019 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3021 else if pname
== "to_i8" then
3022 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3024 else if pname
== "to_i16" then
3025 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3027 else if pname
== "to_u16" then
3028 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3030 else if pname
== "to_u32" then
3031 v
.ret
(v
.new_expr
("(uint32_t){arguments[0]}", ret
.as(not null)))
3033 else if pname
== "to_f" then
3034 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3036 else if pname
== "&" then
3037 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3039 else if pname
== "|" then
3040 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3042 else if pname
== "^" then
3043 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3045 else if pname
== "unary ~" then
3046 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3049 else if cname
== "UInt32" then
3050 if pname
== "output" then
3051 v
.add
("printf(\"%\
"PRIu32 \"\\n\
", {arguments.first});")
3053 else if pname
== "object_id" then
3054 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3056 else if pname
== "+" then
3057 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
3059 else if pname
== "-" then
3060 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
3062 else if pname
== "unary -" then
3063 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
3065 else if pname
== "unary +" then
3068 else if pname
== "*" then
3069 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
3071 else if pname
== "/" then
3072 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
3074 else if pname
== "%" then
3075 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
3077 else if pname
== "<<" then
3078 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
3080 else if pname
== ">>" then
3081 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
3083 else if pname
== "==" then
3084 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3086 else if pname
== "!=" then
3087 var res
= v
.equal_test
(arguments
[0], arguments
[1])
3088 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
3090 else if pname
== "<" then
3091 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
3093 else if pname
== ">" then
3094 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
3096 else if pname
== "<=" then
3097 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
3099 else if pname
== ">=" then
3100 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
3102 else if pname
== "to_i" then
3103 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
3105 else if pname
== "to_b" then
3106 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
3108 else if pname
== "to_i8" then
3109 v
.ret
(v
.new_expr
("(int8_t){arguments[0]}", ret
.as(not null)))
3111 else if pname
== "to_i16" then
3112 v
.ret
(v
.new_expr
("(int16_t){arguments[0]}", ret
.as(not null)))
3114 else if pname
== "to_u16" then
3115 v
.ret
(v
.new_expr
("(uint16_t){arguments[0]}", ret
.as(not null)))
3117 else if pname
== "to_i32" then
3118 v
.ret
(v
.new_expr
("(int32_t){arguments[0]}", ret
.as(not null)))
3120 else if pname
== "to_f" then
3121 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
3123 else if pname
== "&" then
3124 v
.ret
(v
.new_expr
("{arguments[0]} & {arguments[1]}", ret
.as(not null)))
3126 else if pname
== "|" then
3127 v
.ret
(v
.new_expr
("{arguments[0]} | {arguments[1]}", ret
.as(not null)))
3129 else if pname
== "^" then
3130 v
.ret
(v
.new_expr
("{arguments[0]} ^ {arguments[1]}", ret
.as(not null)))
3132 else if pname
== "unary ~" then
3133 v
.ret
(v
.new_expr
("~{arguments[0]}", ret
.as(not null)))
3137 if pname
== "exit" then
3138 v
.add
("exit((int){arguments[1]});")
3140 else if pname
== "sys" then
3141 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
3143 else if pname
== "object_id" then
3144 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
3146 else if pname
== "is_same_type" then
3147 v
.ret
(v
.is_same_type_test
(arguments
[0], arguments
[1]))
3149 else if pname
== "is_same_instance" then
3150 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
3152 else if pname
== "output_class_name" then
3153 var nat
= v
.class_name_string
(arguments
.first
)
3154 v
.add
("printf(\"%s\\n\
", {nat});")
3156 else if pname
== "native_class_name" then
3157 var nat
= v
.class_name_string
(arguments
.first
)
3158 v
.ret
(v
.new_expr
("(char*){nat}", ret
.as(not null)))
3160 else if pname
== "force_garbage_collection" then
3161 v
.add
("nit_gcollect();")
3163 else if pname
== "native_argc" then
3164 v
.ret
(v
.new_expr
("glob_argc", ret
.as(not null)))
3166 else if pname
== "native_argv" then
3167 v
.ret
(v
.new_expr
("glob_argv[{arguments[1]}]", ret
.as(not null)))
3173 # Compile an extern method
3174 # Return `true` if the compilation was successful, `false` if a fall-back is needed
3175 fun compile_externmeth_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
3178 var at
= self.get_single_annotation
("extern", v
.compiler
.modelbuilder
)
3179 if at
!= null and at
.n_args
.length
== 1 then
3180 externname
= at
.arg_as_string
(v
.compiler
.modelbuilder
)
3181 if externname
== null then return false
3185 v
.add_extern
(mpropdef
.mclassdef
.mmodule
)
3186 var res
: nullable RuntimeVariable = null
3187 var ret
= mpropdef
.msignature
.return_mtype
3189 ret
= v
.resolve_for
(ret
, arguments
.first
)
3190 res
= v
.new_var_extern
(ret
)
3192 v
.adapt_signature
(mpropdef
, arguments
)
3193 v
.unbox_signature_extern
(mpropdef
, arguments
)
3196 v
.add
("{externname}({arguments.join(", ")});")
3198 v
.add
("{res} = {externname}({arguments.join(", ")});")
3199 res
= v
.box_extern
(res
, ret
.as(not null))
3205 # Compile an extern factory
3206 # Return `true` if the compilation was successful, `false` if a fall-back is needed
3207 fun compile_externinit_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable]): Bool
3210 var at
= self.get_single_annotation
("extern", v
.compiler
.modelbuilder
)
3212 externname
= at
.arg_as_string
(v
.compiler
.modelbuilder
)
3213 if externname
== null then return false
3217 v
.add_extern
(mpropdef
.mclassdef
.mmodule
)
3218 v
.adapt_signature
(mpropdef
, arguments
)
3219 v
.unbox_signature_extern
(mpropdef
, arguments
)
3220 var ret
= arguments
.first
.mtype
3221 var res
= v
.new_var_extern
(ret
)
3225 v
.add
("{res} = {externname}({arguments.join(", ")});")
3226 res
= v
.box_extern
(res
, ret
)
3232 redef class AAttrPropdef
3233 redef fun can_inline
: Bool do return not is_lazy
3235 redef fun compile_to_c
(v
, mpropdef
, arguments
)
3237 if mpropdef
== mreadpropdef
then
3238 assert arguments
.length
== 1
3239 var recv
= arguments
.first
3243 var ret
= self.mtype
3244 var useiset
= not ret
.is_c_primitive
and not ret
isa MNullableType
3245 var guard
= self.mlazypropdef
.mproperty
3247 set
= v
.isset_attribute
(self.mpropdef
.mproperty
, recv
)
3249 set
= v
.read_attribute
(guard
, recv
)
3251 v
.add
("if(likely({set})) \{")
3252 res
= v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
3255 var value
= evaluate_expr
(v
, recv
)
3257 v
.assign
(res
, value
)
3259 var true_v
= v
.bool_instance
(true)
3260 v
.write_attribute
(guard
, arguments
.first
, true_v
)
3264 res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
3266 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
3267 else if mpropdef
== mwritepropdef
then
3268 assert arguments
.length
== 2
3269 var recv
= arguments
.first
3270 var arg
= arguments
[1]
3271 if is_optional
and v
.maybe_null
(arg
) then
3272 var value
= v
.new_var
(self.mpropdef
.static_mtype
.as(not null))
3273 v
.add
("if ({arg} == NULL) \{")
3274 v
.assign
(value
, evaluate_expr
(v
, recv
))
3276 v
.assign
(value
, arg
)
3280 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arg
)
3282 var ret
= self.mtype
3283 var useiset
= not ret
.is_c_primitive
and not ret
isa MNullableType
3285 v
.write_attribute
(self.mlazypropdef
.mproperty
, arguments
.first
, v
.bool_instance
(true))
3293 fun init_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable)
3295 if has_value
and not is_lazy
and not is_optional
and not n_expr
isa ANullExpr then evaluate_expr
(v
, recv
)
3298 # Evaluate, store and return the default value of the attribute
3299 private fun evaluate_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable): RuntimeVariable
3301 var oldnode
= v
.current_node
3302 v
.current_node
= self
3303 var old_frame
= v
.frame
3304 var frame
= new StaticFrame(v
, self.mreadpropdef
.as(not null), recv
.mcasttype
.undecorate
.as(MClassType), [recv
])
3308 var mtype
= self.mtype
3309 assert mtype
!= null
3311 var nexpr
= self.n_expr
3312 var nblock
= self.n_block
3313 if nexpr
!= null then
3314 value
= v
.expr
(nexpr
, mtype
)
3315 else if nblock
!= null then
3316 value
= v
.new_var
(mtype
)
3317 frame
.returnvar
= value
3318 frame
.returnlabel
= v
.get_name
("RET_LABEL")
3321 v
.add
("{frame.returnlabel.as(not null)}:(void)0;")
3327 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
3330 v
.current_node
= oldnode
3335 fun check_expr
(v
: AbstractCompilerVisitor, recv
: RuntimeVariable)
3337 var nexpr
= self.n_expr
3338 if nexpr
!= null then return
3340 var oldnode
= v
.current_node
3341 v
.current_node
= self
3342 var old_frame
= v
.frame
3343 var frame
= new StaticFrame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
3345 # Force read to check the initialization
3346 v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
3348 v
.current_node
= oldnode
3352 redef class AClassdef
3353 private fun compile_to_c
(v
: AbstractCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
3355 if mpropdef
.mproperty
.is_root_init
then
3356 assert arguments
.length
== 1
3357 if not mpropdef
.is_intro
then
3358 v
.supercall
(mpropdef
, arguments
.first
.mtype
.as(MClassType), arguments
)
3368 # Try to compile self as an expression
3369 # Do not call this method directly, use `v.expr` instead
3370 private fun expr
(v
: AbstractCompilerVisitor): nullable RuntimeVariable
3372 v
.add
("PRINT_ERROR(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
3373 var mtype
= self.mtype
3374 if mtype
== null then
3377 var res
= v
.new_var
(mtype
)
3378 v
.add
("/* {res} = NOT YET {class_name} */")
3383 # Try to compile self as a statement
3384 # Do not call this method directly, use `v.stmt` instead
3385 private fun stmt
(v
: AbstractCompilerVisitor)
3391 redef class ABlockExpr
3394 for e
in self.n_expr
do v
.stmt
(e
)
3398 var last
= self.n_expr
.last
3399 for e
in self.n_expr
do
3400 if e
== last
then break
3403 return v
.expr
(last
, null)
3407 redef class AVardeclExpr
3410 var variable
= self.variable
.as(not null)
3411 var ne
= self.n_expr
3413 var i
= v
.expr
(ne
, variable
.declared_type
)
3414 v
.assign
(v
.variable
(variable
), i
)
3419 redef class AVarExpr
3422 var res
= v
.variable
(self.variable
.as(not null))
3423 var mtype
= self.mtype
.as(not null)
3424 return v
.autoadapt
(res
, mtype
)
3428 redef class AVarAssignExpr
3431 var variable
= self.variable
.as(not null)
3432 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
3433 v
.assign
(v
.variable
(variable
), i
)
3438 redef class AVarReassignExpr
3441 var variable
= self.variable
.as(not null)
3442 var vari
= v
.variable
(variable
)
3443 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
3444 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
3446 v
.assign
(v
.variable
(variable
), res
)
3450 redef class ASelfExpr
3451 redef fun expr
(v
) do return v
.frame
.arguments
.first
3454 redef class AImplicitSelfExpr
3455 redef fun expr
(v
) do
3456 if not is_sys
then return super
3457 return v
.new_expr
("glob_sys", mtype
.as(not null))
3461 redef class AEscapeExpr
3462 redef fun stmt
(v
) do v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
3465 redef class AReturnExpr
3468 var nexpr
= self.n_expr
3469 if nexpr
!= null then
3470 var returnvar
= v
.frame
.returnvar
.as(not null)
3471 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
3472 v
.assign
(returnvar
, i
)
3474 v
.add
("goto {v.frame.returnlabel.as(not null)};")
3478 redef class AAbortExpr
3479 redef fun stmt
(v
) do v
.add_abort
("Aborted")
3485 var cond
= v
.expr_bool
(self.n_expr
)
3486 v
.add
("if ({cond})\{")
3495 var res
= v
.new_var
(self.mtype
.as(not null))
3496 var cond
= v
.expr_bool
(self.n_expr
)
3497 v
.add
("if ({cond})\{")
3498 v
.assign
(res
, v
.expr
(self.n_then
.as(not null), null))
3500 v
.assign
(res
, v
.expr
(self.n_else
.as(not null), null))
3506 redef class AIfexprExpr
3509 var res
= v
.new_var
(self.mtype
.as(not null))
3510 var cond
= v
.expr_bool
(self.n_expr
)
3511 v
.add
("if ({cond})\{")
3512 v
.assign
(res
, v
.expr
(self.n_then
, null))
3514 v
.assign
(res
, v
.expr
(self.n_else
, null))
3523 if self.n_catch
!= null then
3524 v
.add
("if(catchStack.currentSize == 0) \{")
3525 v
.add
(" catchStack.cursor = -1;")
3526 v
.add
(" catchStack.currentSize = 100;")
3527 v
.add
(" catchStack.envs = malloc(sizeof(jmp_buf)*100);")
3528 v
.add
("\} else if(catchStack.cursor == catchStack.currentSize - 1) \{")
3529 v
.add
(" catchStack.currentSize *= 2;")
3530 v
.add
(" catchStack.envs = realloc(catchStack.envs, sizeof(jmp_buf)*catchStack.currentSize);")
3532 v
.add
("catchStack.cursor += 1;")
3533 v
.add
("if(!setjmp(catchStack.envs[catchStack.cursor]))\{")
3534 v
.stmt
(self.n_block
)
3535 v
.add
("catchStack.cursor -= 1;")
3537 v
.add
("catchStack.cursor -= 1;")
3538 v
.stmt
(self.n_catch
)
3541 v
.stmt
(self.n_block
)
3543 v
.add_escape_label
(break_mark
)
3547 redef class AWhileExpr
3551 var cond
= v
.expr_bool
(self.n_expr
)
3552 v
.add
("if (!{cond}) break;")
3553 v
.stmt
(self.n_block
)
3554 v
.add_escape_label
(continue_mark
)
3556 v
.add_escape_label
(break_mark
)
3560 redef class ALoopExpr
3564 v
.stmt
(self.n_block
)
3565 v
.add_escape_label
(continue_mark
)
3567 v
.add_escape_label
(break_mark
)
3571 redef class AForExpr
3574 for g
in n_groups
do
3575 var cl
= v
.expr
(g
.n_expr
, null)
3576 var it_meth
= g
.method_iterator
3577 assert it_meth
!= null
3578 var it
= v
.compile_callsite
(it_meth
, [cl
])
3583 for g
in n_groups
do
3585 var isok_meth
= g
.method_is_ok
3586 assert isok_meth
!= null
3587 var ok
= v
.compile_callsite
(isok_meth
, [it
])
3589 v
.add
("if(!{ok}) break;")
3590 if g
.variables
.length
== 1 then
3591 var item_meth
= g
.method_item
3592 assert item_meth
!= null
3593 var i
= v
.compile_callsite
(item_meth
, [it
])
3595 v
.assign
(v
.variable
(g
.variables
.first
), i
)
3596 else if g
.variables
.length
== 2 then
3597 var key_meth
= g
.method_key
3598 assert key_meth
!= null
3599 var i
= v
.compile_callsite
(key_meth
, [it
])
3601 v
.assign
(v
.variable
(g
.variables
[0]), i
)
3602 var item_meth
= g
.method_item
3603 assert item_meth
!= null
3604 i
= v
.compile_callsite
(item_meth
, [it
])
3606 v
.assign
(v
.variable
(g
.variables
[1]), i
)
3611 v
.stmt
(self.n_block
)
3612 v
.add_escape_label
(continue_mark
)
3613 for g
in n_groups
do
3614 var next_meth
= g
.method_next
3615 assert next_meth
!= null
3616 v
.compile_callsite
(next_meth
, [g
.it
])
3619 v
.add_escape_label
(break_mark
)
3621 for g
in n_groups
do
3622 var method_finish
= g
.method_finish
3623 if method_finish
!= null then
3624 # TODO: Find a way to call this also in long escape (e.g. return)
3625 v
.compile_callsite
(method_finish
, [g
.it
])
3631 redef class AForGroup
3632 # C variable representing the iterator
3633 private var it
: RuntimeVariable is noinit
3636 redef class AAssertExpr
3639 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
3641 var cond
= v
.expr_bool
(self.n_expr
)
3642 v
.add
("if (unlikely(!{cond})) \{")
3649 v
.add_abort
("Assert '{nid.text}' failed")
3651 v
.add_abort
("Assert failed")
3656 # Explain assert if it fails
3657 private fun explain_assert
(v
: AbstractCompilerVisitor)
3659 var explain_assert_str
= explain_assert_str
3660 if explain_assert_str
== null then return
3662 var nas
= v
.compiler
.modelbuilder
.model
.get_mclasses_by_name
("NativeArray")
3663 if nas
== null then return
3665 nas
= v
.compiler
.modelbuilder
.model
.get_mclasses_by_name
("Array")
3666 if nas
== null or nas
.is_empty
then return
3668 var expr
= explain_assert_str
.expr
(v
)
3669 if expr
== null then return
3671 var cstr
= v
.send
(v
.get_property
("to_cstring", expr
.mtype
), [expr
])
3672 if cstr
== null then return
3674 v
.add
"PRINT_ERROR(\"Runtime assert: %s\\n\
", {cstr});"
3681 var res
= v
.new_var
(self.mtype
.as(not null))
3682 var i1
= v
.expr_bool
(self.n_expr
)
3683 v
.add
("if ({i1}) \{")
3686 var i2
= v
.expr_bool
(self.n_expr2
)
3687 v
.add
("{res} = {i2};")
3693 redef class AImpliesExpr
3696 var res
= v
.new_var
(self.mtype
.as(not null))
3697 var i1
= v
.expr_bool
(self.n_expr
)
3698 v
.add
("if (!{i1}) \{")
3701 var i2
= v
.expr_bool
(self.n_expr2
)
3702 v
.add
("{res} = {i2};")
3708 redef class AAndExpr
3711 var res
= v
.new_var
(self.mtype
.as(not null))
3712 var i1
= v
.expr_bool
(self.n_expr
)
3713 v
.add
("if (!{i1}) \{")
3716 var i2
= v
.expr_bool
(self.n_expr2
)
3717 v
.add
("{res} = {i2};")
3723 redef class ANotExpr
3726 var cond
= v
.expr_bool
(self.n_expr
)
3727 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
3731 redef class AOrElseExpr
3734 var res
= v
.new_var
(self.mtype
.as(not null))
3735 var i1
= v
.expr
(self.n_expr
, null)
3737 if not v
.maybe_null
(i1
) then return i1
3739 v
.add
("if ({i1}!=NULL) \{")
3742 var i2
= v
.expr
(self.n_expr2
, null)
3749 redef class AIntegerExpr
3750 redef fun expr
(v
) do
3751 if value
isa Int then return v
.int_instance
(value
.as(Int))
3752 if value
isa Byte then return v
.byte_instance
(value
.as(Byte))
3753 if value
isa Int8 then return v
.int8_instance
(value
.as(Int8))
3754 if value
isa Int16 then return v
.int16_instance
(value
.as(Int16))
3755 if value
isa UInt16 then return v
.uint16_instance
(value
.as(UInt16))
3756 if value
isa Int32 then return v
.int32_instance
(value
.as(Int32))
3757 if value
isa UInt32 then return v
.uint32_instance
(value
.as(UInt32))
3758 # Should never happen
3763 redef class AFloatExpr
3764 redef fun expr
(v
) do return v
.float_instance
("{self.n_float.text}") # FIXME use value, not n_float
3767 redef class ACharExpr
3768 redef fun expr
(v
) do
3769 if is_ascii
then return v
.byte_instance
(value
.as(not null).ascii
)
3770 if is_code_point
then return v
.int_instance
(value
.as(not null).code_point
)
3771 return v
.char_instance
(self.value
.as(not null))
3775 redef class AArrayExpr
3778 var mtype
= self.element_mtype
.as(not null)
3779 var array
= new Array[RuntimeVariable]
3780 var res
= v
.array_instance
(array
, mtype
)
3782 var old_comprehension
= v
.frame
.comprehension
3783 v
.frame
.comprehension
= res
3784 for nexpr
in self.n_exprs
do
3787 v
.frame
.comprehension
= old_comprehension
3793 redef class AugmentedStringFormExpr
3794 # Factorize the making of a `Regex` object from a literal prefixed string
3795 protected fun make_re
(v
: AbstractCompilerVisitor, rs
: RuntimeVariable): nullable RuntimeVariable do
3798 var res
= v
.compile_callsite
(re
, [rs
])
3800 print
"Cannot call property `to_re` on {self}"
3803 for i
in suffix
.chars
do
3805 var ign
= ignore_case
3807 v
.compile_callsite
(ign
, [res
, v
.bool_instance
(true)])
3813 v
.compile_callsite
(nl
, [res
, v
.bool_instance
(true)])
3819 v
.compile_callsite
(ext
, [res
, v
.bool_instance
(false)])
3822 # Should not happen, this needs to be updated
3823 # along with the addition of new suffixes
3830 redef class AStringFormExpr
3831 redef fun expr
(v
) do return v
.string_instance
(value
)
3834 redef class AStringExpr
3835 redef fun expr
(v
) do
3836 var s
= v
.string_instance
(value
)
3837 if is_string
then return s
3838 if is_bytestring
then
3839 var ns
= v
.c_string_instance
(bytes
.items
, bytes
.length
)
3840 var ln
= v
.int_instance
(bytes
.length
)
3841 var cs
= to_bytes_with_copy
3843 var res
= v
.compile_callsite
(cs
, [ns
, ln
])
3847 var res
= make_re
(v
, s
)
3851 print
"Unimplemented prefix or suffix for {self}"
3858 redef class ASuperstringExpr
3861 var type_string
= v
.mmodule
.string_type
3863 # Collect elements of the superstring
3864 var array
= new Array[AExpr]
3865 for ne
in self.n_exprs
do
3866 # Drop literal empty string.
3867 # They appears in things like "{a}" that is ["", a, ""]
3868 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
3872 # Store the allocated native array in a static variable
3874 var varonce
= v
.get_name
("varonce")
3875 v
.add
("if (unlikely({varonce}==NULL)) \{")
3877 # The native array that will contains the elements to_s-ized.
3878 # For fast concatenation.
3879 var a
= v
.native_array_instance
(type_string
, v
.int_instance
(array
.length
))
3881 v
.add_decl
("static {a.mtype.ctype} {varonce};")
3883 # Pre-fill the array with the literal string parts.
3884 # So they do not need to be filled again when reused
3885 for i
in [0..array
.length
[ do
3887 if not ne
isa AStringFormExpr then continue
3888 var e
= v
.expr
(ne
, null)
3889 v
.native_array_set
(a
, i
, e
)
3893 # Take the native-array from the store.
3894 # The point is to prevent that some recursive execution use (and corrupt) the same native array
3895 # WARNING: not thread safe! (FIXME?)
3896 v
.add
("{a} = {varonce};")
3897 v
.add
("{varonce} = NULL;")
3900 # Stringify the elements and put them in the native array
3901 var to_s_method
= v
.get_property
("to_s", v
.object_type
)
3902 for i
in [0..array
.length
[ do
3904 if ne
isa AStringFormExpr then continue
3905 var e
= v
.expr
(ne
, null)
3906 # Skip the `to_s` if the element is already a String
3907 if not e
.mcasttype
.is_subtype
(v
.compiler
.mainmodule
, null, type_string
) then
3908 e
= v
.send
(to_s_method
, [e
]).as(not null)
3910 v
.native_array_set
(a
, i
, e
)
3913 # Fast join the C string to get the result
3914 var res
= v
.send
(v
.get_property
("native_to_s", a
.mtype
), [a
])
3917 if is_re
then res
= make_re
(v
, res
)
3919 # We finish to work with the native array,
3920 # so store it so that it can be reused
3921 v
.add
("{varonce} = {a};")
3927 redef class ACrangeExpr
3930 var i1
= v
.expr
(self.n_expr
, null)
3931 var i2
= v
.expr
(self.n_expr2
, null)
3932 var mtype
= self.mtype
.as(MClassType)
3933 var res
= v
.init_instance
(mtype
)
3934 v
.compile_callsite
(init_callsite
.as(not null), [res
, i1
, i2
])
3939 redef class AOrangeExpr
3942 var i1
= v
.expr
(self.n_expr
, null)
3943 var i2
= v
.expr
(self.n_expr2
, null)
3944 var mtype
= self.mtype
.as(MClassType)
3945 var res
= v
.init_instance
(mtype
)
3946 v
.compile_callsite
(init_callsite
.as(not null), [res
, i1
, i2
])
3951 redef class ATrueExpr
3952 redef fun expr
(v
) do return v
.bool_instance
(true)
3955 redef class AFalseExpr
3956 redef fun expr
(v
) do return v
.bool_instance
(false)
3959 redef class ANullExpr
3960 redef fun expr
(v
) do return v
.null_instance
3963 redef class AIsaExpr
3966 var i
= v
.expr
(self.n_expr
, null)
3967 var cast_type
= self.cast_type
3968 if cast_type
== null then return null # no-no on broken node
3969 return v
.type_test
(i
, cast_type
, "isa")
3973 redef class AAsCastExpr
3976 var i
= v
.expr
(self.n_expr
, null)
3977 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
3979 v
.add_cast
(i
, self.mtype
.as(not null), "as")
3984 redef class AAsNotnullExpr
3987 var i
= v
.expr
(self.n_expr
, null)
3988 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
3990 if not v
.maybe_null
(i
) then return i
3992 v
.add
("if (unlikely({i} == NULL)) \{")
3993 v
.add_abort
("Cast failed")
3999 redef class AParExpr
4000 redef fun expr
(v
) do return v
.expr
(self.n_expr
, null)
4003 redef class AOnceExpr
4006 var mtype
= self.mtype
.as(not null)
4007 var name
= v
.get_name
("varonce")
4008 var guard
= v
.get_name
(name
+ "_guard")
4009 v
.add_decl
("static {mtype.ctype} {name};")
4010 v
.add_decl
("static int {guard};")
4011 var res
= v
.new_var
(mtype
)
4012 v
.add
("if (likely({guard})) \{")
4013 v
.add
("{res} = {name};")
4015 var i
= v
.expr
(self.n_expr
, mtype
)
4016 v
.add
("{res} = {i};")
4017 v
.add
("{name} = {res};")
4018 v
.add
("{guard} = 1;")
4024 redef class ASendExpr
4027 var recv
= v
.expr
(self.n_expr
, null)
4028 var callsite
= self.callsite
.as(not null)
4029 if callsite
.is_broken
then return null
4030 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
4031 return v
.compile_callsite
(callsite
, args
)
4035 redef class ASendReassignFormExpr
4038 var recv
= v
.expr
(self.n_expr
, null)
4039 var callsite
= self.callsite
.as(not null)
4040 if callsite
.is_broken
then return
4041 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
4043 var value
= v
.expr
(self.n_value
, null)
4045 var left
= v
.compile_callsite
(callsite
, args
)
4048 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
4052 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
4056 redef class ASuperExpr
4059 var frame
= v
.frame
.as(not null)
4060 var recv
= frame
.arguments
.first
4062 var callsite
= self.callsite
4063 if callsite
!= null then
4064 if callsite
.is_broken
then return null
4067 if self.n_args
.n_exprs
.is_empty
then
4068 # Add automatic arguments for the super init call
4070 for i
in [0..callsite
.msignature
.arity
[ do
4071 args
.add
(frame
.arguments
[i
+1])
4074 args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
4078 var res
= v
.compile_callsite
(callsite
, args
)
4082 var mpropdef
= self.mpropdef
.as(not null)
4085 if self.n_args
.n_exprs
.is_empty
then
4086 args
= frame
.arguments
4088 args
= v
.varargize
(mpropdef
, signaturemap
, recv
, self.n_args
.n_exprs
)
4091 # Standard call-next-method
4092 return v
.supercall
(mpropdef
, recv
.mtype
.as(MClassType), args
)
4096 redef class ANewExpr
4099 var mtype
= self.recvtype
4100 assert mtype
!= null
4102 if mtype
.mclass
.name
== "NativeArray" then
4103 assert self.n_args
.n_exprs
.length
== 1
4104 var l
= v
.expr
(self.n_args
.n_exprs
.first
, null)
4105 assert mtype
isa MGenericType
4106 var elttype
= mtype
.arguments
.first
4107 return v
.native_array_instance
(elttype
, l
)
4110 var callsite
= self.callsite
4111 if callsite
== null then return v
.init_instance_or_extern
(mtype
)
4112 if callsite
.is_broken
then return null
4115 # new factories are badly implemented.
4116 # They assume a stub temporary receiver exists.
4117 # This temporary receiver is required because it
4118 # currently holds the method and the formal types.
4120 # However, this object could be reused if the formal types are the same.
4121 # Therefore, the following code will `once` it in these case
4122 if callsite
.mproperty
.is_new
and not mtype
.need_anchor
then
4123 var name
= v
.get_name
("varoncenew")
4124 var guard
= v
.get_name
(name
+ "_guard")
4125 v
.add_decl
("static {mtype.ctype} {name};")
4126 v
.add_decl
("static int {guard};")
4127 recv
= v
.new_var
(mtype
)
4128 v
.add
("if (likely({guard})) \{")
4129 v
.add
("{recv} = {name};")
4131 var i
= v
.init_instance_or_extern
(mtype
)
4132 v
.add
("{recv} = {i};")
4133 v
.add
("{name} = {recv};")
4134 v
.add
("{guard} = 1;")
4137 recv
= v
.init_instance_or_extern
(mtype
)
4140 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
4141 var res2
= v
.compile_callsite
(callsite
, args
)
4142 if res2
!= null then
4143 #self.debug("got {res2} from {mproperty}. drop {recv}")
4150 redef class AAttrExpr
4153 var recv
= v
.expr
(self.n_expr
, null)
4154 var mproperty
= self.mproperty
.as(not null)
4155 return v
.read_attribute
(mproperty
, recv
)
4159 redef class AAttrAssignExpr
4162 var recv
= v
.expr
(self.n_expr
, null)
4163 var i
= v
.expr
(self.n_value
, null)
4164 var mproperty
= self.mproperty
.as(not null)
4165 v
.write_attribute
(mproperty
, recv
, i
)
4170 redef class AAttrReassignExpr
4173 var recv
= v
.expr
(self.n_expr
, null)
4174 var value
= v
.expr
(self.n_value
, null)
4175 var mproperty
= self.mproperty
.as(not null)
4176 var attr
= v
.read_attribute
(mproperty
, recv
)
4177 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
4179 v
.write_attribute
(mproperty
, recv
, res
)
4183 redef class AIssetAttrExpr
4186 var recv
= v
.expr
(self.n_expr
, null)
4187 var mproperty
= self.mproperty
.as(not null)
4188 return v
.isset_attribute
(mproperty
, recv
)
4192 redef class AVarargExpr
4195 return v
.expr
(self.n_expr
, null)
4199 redef class ANamedargExpr
4202 return v
.expr
(self.n_expr
, null)
4206 redef class ADebugTypeExpr
4215 redef class Array[E
]
4216 # Return a new `Array` with the elements only contened in self and not in `o`
4217 fun -(o
: Array[E
]): Array[E
] do
4218 var res
= new Array[E
]
4219 for e
in self do if not o
.has
(e
) then res
.add
(e
)
4225 # All `MProperty` associated to all `MClassDef` of `mclass`
4226 fun properties
(mclass
: MClass): Set[MProperty] do
4227 if not self.properties_cache
.has_key
(mclass
) then
4228 var properties
= new HashSet[MProperty]
4229 var parents
= new Array[MClass]
4230 if self.flatten_mclass_hierarchy
.has
(mclass
) then
4231 parents
.add_all
(mclass
.in_hierarchy
(self).direct_greaters
)
4233 for parent
in parents
do
4234 properties
.add_all
(self.properties
(parent
))
4236 for mclassdef
in mclass
.mclassdefs
do
4237 if not self.in_importation
<= mclassdef
.mmodule
then continue
4238 for mprop
in mclassdef
.intro_mproperties
do
4239 properties
.add
(mprop
)
4242 self.properties_cache
[mclass
] = properties
4244 return properties_cache
[mclass
]
4246 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
4248 # Write FFI and nitni results to file
4249 fun finalize_ffi
(c
: AbstractCompiler) do end
4251 # Give requided addinional system libraries (as given to LD_LIBS)
4252 # Note: can return null instead of an empty set
4253 fun collect_linker_libs
: nullable Array[String] do return null
4256 # Create a tool context to handle options and paths
4257 var toolcontext
= new ToolContext
4259 toolcontext
.tooldescription
= "Usage: nitc [OPTION]... file.nit...\nCompiles Nit programs."
4261 # We do not add other options, so process them now!
4262 toolcontext
.process_options
(args
)
4264 # We need a model to collect stufs
4265 var model
= new Model
4266 # An a model builder to parse files
4267 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
4269 var arguments
= toolcontext
.option_context
.rest
4270 if toolcontext
.opt_run
.value
then
4271 # When --run, only the first is the program, the rest is the run arguments
4272 arguments
= [toolcontext
.option_context
.rest
.shift
]
4274 if arguments
.length
> 1 and toolcontext
.opt_output
.value
!= null then
4275 print
"Option Error: --output needs a single source file. Do you prefer --dir?"
4279 # Here we load an process all modules passed on the command line
4280 var mmodules
= modelbuilder
.parse
(arguments
)
4282 if mmodules
.is_empty
then toolcontext
.quit
4284 modelbuilder
.run_phases
4286 for mmodule
in mmodules
do
4287 toolcontext
.info
("*** PROCESS {mmodule} ***", 1)
4289 toolcontext
.run_global_phases
(ms
)