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.
17 # Global compilation of a Nit program
19 # Techniques used are:
20 # * heterogeneous generics
24 module global_compiler
28 import auto_super_init
29 import rapid_type_analysis
31 redef class ToolContext
33 var opt_output
: OptionString = new OptionString("Output file", "-o", "--output")
36 var opt_no_cc
: OptionBool = new OptionBool("Do not invoke C compiler", "--no-cc")
39 var opt_hardening
: OptionBool = new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
41 # --no-check-covariance
42 var opt_no_check_covariance
: OptionBool = new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
44 # --no-check-initialization
45 var opt_no_check_initialization
: OptionBool = new OptionBool("Disable isset tests at the end of constructors (dangerous)", "--no-check-initialization")
48 var opt_no_check_assert
: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
51 var opt_no_check_autocast
: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
54 var opt_no_check_other
: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
59 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_hardening
)
60 self.option_context
.add_option
(self.opt_no_check_covariance
, self.opt_no_check_initialization
, self.opt_no_check_assert
, self.opt_no_check_autocast
, self.opt_no_check_other
)
64 redef class ModelBuilder
65 # Entry point to performs a global compilation on the AST of a complete program.
66 # `mainmodule` is the main module of the program
67 # `runtime_type_analysis` is a already computer type analysis.
68 fun run_global_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
71 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
73 var compiler
= new GlobalCompiler(mainmodule
, runtime_type_analysis
, self)
74 var v
= compiler
.header
76 for t
in runtime_type_analysis
.live_types
do
77 compiler
.declare_runtimeclass
(v
, t
)
80 compiler
.compile_class_names
82 # Init instance code (allocate and init-arguments)
83 for t
in runtime_type_analysis
.live_types
do
84 if t
.ctype
== "val*" then
85 compiler
.generate_init_instance
(t
)
86 compiler
.generate_check_init_instance
(t
)
88 compiler
.generate_box_instance
(t
)
92 # The main function of the C
93 compiler
.compile_main_function
95 # Compile until all runtime_functions are visited
96 while not compiler
.todos
.is_empty
do
97 var m
= compiler
.todos
.shift
98 self.toolcontext
.info
("Compile {m} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
99 m
.compile_to_c
(compiler
)
101 self.toolcontext
.info
("Total methods to compile to C: {compiler.visitors.length}", 2)
104 self.toolcontext
.info
("*** END VISITING: {time1-time0} ***", 2)
105 write_and_make
(compiler
)
108 protected fun write_and_make
(compiler
: GlobalCompiler)
110 var mainmodule
= compiler
.mainmodule
112 # Generate the .h and .c files
113 # A single C file regroups many compiled rumtime functions
114 # 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
117 var outname
= self.toolcontext
.opt_output
.value
118 if outname
== null then
119 outname
= "{mainmodule.name}.bin"
122 var hfilename
= ".nit_compile/{mainmodule.name}.1.h"
123 var h
= new OFStream.open
(hfilename
)
124 for l
in compiler
.header
.decl_lines
do
130 var cfiles
= new Array[String]
132 var file
: nullable OFStream = null
137 for vis
in compiler
.visitors
do
138 count
+= vis
.lines
.length
139 if file
== null or count
> 10000 then
141 if file
!= null then file
.close
142 var cfilename
= ".nit_compile/{mainmodule.name}.{i}.c"
143 cfiles
.add
(cfilename
)
144 file
= new OFStream.open
(cfilename
)
145 file
.write
"#include \"{mainmodule.name}.1.h\
"\n"
146 count
= vis
.lines
.length
148 if vis
!= compiler
.header
then
149 for l
in vis
.decl_lines
do
154 for l
in vis
.lines
do
159 if file
!= null then file
.close
161 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
163 # Generate the Makefile
165 var makename
= ".nit_compile/{mainmodule.name}.mk"
166 var makefile
= new OFStream.open
(makename
)
168 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
169 makefile
.write
("all: {outname}\n\n")
171 var ofiles
= new Array[String]
172 # Compile each generated file
174 var o
= f
.strip_extension
(".c") + ".o"
175 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
178 # Compile each required extern body into a specific .o
179 for f
in compiler
.extern_bodies
do
181 var o
= ".nit_compile/{mainmodule.name}.{i}.o"
182 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
186 makefile
.write
("{outname}: {ofiles.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outname} {ofiles.join(" ")} $(LDLIBS)\n\n")
188 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
190 self.toolcontext
.info
("Generated makefile: {makename}", 2)
193 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
195 # Execute the Makefile
197 if self.toolcontext
.opt_no_cc
.value
then return
200 self.toolcontext
.info
("*** COMPILING C ***", 1)
201 self.toolcontext
.info
("make -B -f {makename} -j 4", 2)
204 if self.toolcontext
.verbose_level
>= 3 then
205 res
= sys
.system
("make -B -f {makename} -j 4 2>&1")
207 res
= sys
.system
("make -B -f {makename} -j 4 2>&1 >/dev/null")
210 toolcontext
.error
(null, "make failed! Error code: {res}.")
214 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
218 # Singleton that store the knowledge about the compilation process
220 # The main module of the program
221 var mainmodule
: MModule writable
223 # The result of the RTA (used to know live types and methods)
224 var runtime_type_analysis
: RapidTypeAnalysis
226 # The modeulbuilder used to know the model and the AST
227 var modelbuilder
: ModelBuilder
229 # Is hardening asked? (see --hardening)
230 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
232 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, modelbuilder
: ModelBuilder)
234 self.header
= new_visitor
235 self.mainmodule
= mainmodule
236 self.runtime_type_analysis
= runtime_type_analysis
237 self.modelbuilder
= modelbuilder
238 self.live_primitive_types
= new Array[MClassType]
239 for t
in runtime_type_analysis
.live_types
do
240 if t
.ctype
!= "val*" then
241 self.live_primitive_types
.add
(t
)
245 # Header declarations
249 protected fun compile_header
do
251 self.header
.add_decl
("#include <stdlib.h>")
252 self.header
.add_decl
("#include <stdio.h>")
253 self.header
.add_decl
("#include <string.h>")
254 # TODO: Better way to activate the GC
255 self.header
.add_decl
("#include <gc/gc.h>")
256 #self.header.add_decl("#define GC_MALLOC(x) calloc(1, (x))")
258 compile_header_structs
260 # Global variable used by the legacy native interface
261 self.header
.add_decl
("extern int glob_argc;")
262 self.header
.add_decl
("extern char **glob_argv;")
263 self.header
.add_decl
("extern val *glob_sys;")
266 # Class names (for the class_name and output_class_name methods)
267 protected fun compile_class_names
do
268 self.header
.add_decl
("extern const char const * class_names[];")
269 self.header
.add
("const char const * class_names[] = \{")
270 for t
in self.runtime_type_analysis
.live_types
do
271 self.header
.add
("\"{t}\
", /* {self.classid(t)} */")
273 self.header
.add
("\};")
276 # Declaration of structures the live Nit types
277 # Each live type is generated as an independent C `struct' type.
278 # They only share a common first field `classid` used to implement the polymorphism.
279 # Usualy, all C variables that refers to a Nit object are typed on the abstract struct `val' that contains only the `classid` field.
280 protected fun compile_header_structs
do
281 self.header
.add_decl
("typedef struct \{int classid;\} val; /* general C type representing a Nit instance. */")
284 # Subset of runtime_type_analysis.live_types that contains only primitive types
285 # Used to implement the equal test
286 var live_primitive_types
: Array[MClassType]
288 # runtime_functions that need to be compiled
289 private var todos
: List[AbstractRuntimeFunction] = new List[AbstractRuntimeFunction]
291 # runtime_functions already seen (todo or done)
292 private var seen
: HashSet[AbstractRuntimeFunction] = new HashSet[AbstractRuntimeFunction]
293 fun todo
(m
: AbstractRuntimeFunction)
295 if seen
.has
(m
) then return
300 # Where global declaration are stored (the main .h)
302 # FIXME: should not be a vistor but just somewhere to store lines
303 # FIXME: should not have a global .h since it does not help recompilations
304 var header
: GlobalCompilerVisitor writable
306 # The list of all associated visitors
307 # Used to generate .c files
308 # FIXME: should not be vistors but just somewhere to store lines
309 private var visitors
: List[GlobalCompilerVisitor] = new List[GlobalCompilerVisitor]
311 # List of additional .c files required to compile (native interface)
312 var extern_bodies
= new ArraySet[String]
314 # Return the C symbol associated to a live type runtime
315 # REQUIRE: self.runtime_type_analysis.live_types.has(mtype)
316 fun classid
(mtype
: MClassType): String
318 if self.classids
.has_key
(mtype
) then
319 return self.classids
[mtype
]
321 print
"No classid for {mtype}"
325 # Cache for classid (computed by declare_runtimeclass)
326 protected var classids
: HashMap[MClassType, String] = new HashMap[MClassType, String]
328 # Declare C structures and identifiers for a runtime class
329 fun declare_runtimeclass
(v
: GlobalCompilerVisitor, mtype
: MClassType)
331 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
332 v
.add_decl
("/* runtime class {mtype} */")
333 var idnum
= classids
.length
334 var idname
= "ID_" + mtype
.c_name
335 self.classids
[mtype
] = idname
336 v
.add_decl
("#define {idname} {idnum} /* {mtype} */")
338 v
.add_decl
("struct {mtype.c_name} \{")
339 v
.add_decl
("int classid; /* must be {idname} */")
341 if mtype
.mclass
.name
== "NativeArray" then
342 # NativeArrays are just a instance header followed by an array of values
343 v
.add_decl
("{mtype.arguments.first.ctype} values[1];")
346 if mtype
.ctype
!= "val*" then
347 # Is the Nit type is native then the struct is a box with two fields:
348 # * the `classid` to be polymorph
349 # * the `value` that contains the native value.
350 v
.add_decl
("{mtype.ctype} value;")
353 # Collect all attributes and associate them a field in the structure.
354 # Note: we do not try to optimize the order and helps CC to optimize the client code.
355 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
356 for p
in cd
.intro_mproperties
do
357 if not p
isa MAttribute then continue
358 var t
= p
.intro
.static_mtype
.as(not null)
359 t
= t
.anchor_to
(self.mainmodule
, mtype
)
360 v
.add_decl
("{t.ctype} {p.intro.c_name}; /* {p}: {t} */")
366 # Generate the init-instance of a live type (allocate + init-instance)
367 fun generate_init_instance
(mtype
: MClassType)
369 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
370 assert mtype
.ctype
== "val*"
371 var v
= self.new_visitor
373 var is_native_array
= mtype
.mclass
.name
== "NativeArray"
376 if is_native_array
then
382 self.header
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}({sig});")
383 v
.add_decl
("/* allocate {mtype} */")
384 v
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}({sig}) \{")
385 var res
= v
.new_var
(mtype
)
387 if is_native_array
then
388 var mtype_elt
= mtype
.arguments
.first
389 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}) + length*sizeof({mtype_elt.ctype}));")
391 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}));")
393 v
.add
("{res}->classid = {self.classid(mtype)};")
395 self.generate_init_attr
(v
, res
, mtype
)
396 v
.add
("return {res};")
400 fun generate_check_init_instance
(mtype
: MClassType)
402 if self.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
404 var v
= self.new_visitor
405 var res
= new RuntimeVariable("self", mtype
, mtype
)
406 self.header
.add_decl
("void CHECK_NEW_{mtype.c_name}({mtype.ctype});")
407 v
.add_decl
("/* allocate {mtype} */")
408 v
.add_decl
("void CHECK_NEW_{mtype.c_name}({mtype.ctype} {res}) \{")
409 self.generate_check_attr
(v
, res
, mtype
)
413 # Generate code that collect initialize the attributes on a new instance
414 fun generate_init_attr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable, mtype
: MClassType)
416 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
418 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
419 for npropdef
in n
.n_propdefs
do
420 if npropdef
isa AAttrPropdef then
421 npropdef
.init_expr
(v
, recv
)
427 # Generate a check-init-instance
428 fun generate_check_attr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable, mtype
: MClassType)
430 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
432 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
433 for npropdef
in n
.n_propdefs
do
434 if npropdef
isa AAttrPropdef then
435 npropdef
.check_expr
(v
, recv
)
441 fun generate_box_instance
(mtype
: MClassType)
443 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
444 assert mtype
.ctype
!= "val*"
445 var v
= self.new_visitor
447 self.header
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype});")
448 v
.add_decl
("/* allocate {mtype} */")
449 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
450 v
.add
("struct {mtype.c_name}*res = GC_MALLOC(sizeof(struct {mtype.c_name}));")
451 v
.add
("res->classid = {self.classid(mtype)};")
452 v
.add
("res->value = value;")
453 v
.add
("return (val*)res;")
458 # look for a needed .h and .c file for a given .nit source-file
459 # FIXME: bad API, parameter should be a MModule, not its source-file
460 fun add_extern
(file
: String)
462 file
= file
.strip_extension
(".nit")
463 var tryfile
= file
+ ".nit.h"
464 if tryfile
.file_exists
then
465 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
467 tryfile
= file
+ "_nit.h"
468 if tryfile
.file_exists
then
469 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
471 tryfile
= file
+ ".nit.c"
472 if tryfile
.file_exists
then
473 self.extern_bodies
.add
(tryfile
)
475 tryfile
= file
+ "_nit.c"
476 if tryfile
.file_exists
then
477 self.extern_bodies
.add
(tryfile
)
481 # Initialize a visitor specific for a compiler engine
482 fun new_visitor
: GlobalCompilerVisitor do return new GlobalCompilerVisitor(self)
484 # Generate the main C function.
486 # allocate the Sys object if it exists
487 # call init if is exists
488 # call main if it exists
489 fun compile_main_function
491 var v
= self.new_visitor
492 v
.add_decl
("int glob_argc;")
493 v
.add_decl
("char **glob_argv;")
494 v
.add_decl
("val *glob_sys;")
495 v
.add_decl
("int main(int argc, char** argv) \{")
496 v
.add
("glob_argc = argc; glob_argv = argv;")
497 var main_type
= mainmodule
.sys_type
498 if main_type
!= null then
499 var mainmodule
= v
.compiler
.mainmodule
500 var glob_sys
= v
.init_instance
(main_type
)
501 v
.add
("glob_sys = {glob_sys};")
502 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
503 if main_init
!= null then
504 v
.send
(main_init
, [glob_sys
])
506 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
507 if main_method
!= null then
508 v
.send
(main_method
, [glob_sys
])
516 private var collect_types_cache
: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
520 # Mangle a string to be a unique valid C identifier
521 fun to_cmangle
: String
524 var underscore
= false
526 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
532 res
.append
('_'.ascii
.to_s
)
535 if c
>= '0' and c
<= '9' then
538 else if c
== '_' then
543 res
.append
(c
.ascii
.to_s
)
551 # Escape " \ ' and non printable characters for literal C strings or characters
552 fun escape_to_c
: String
558 else if c
== '\0' then
560 else if c
== '"' then
562 else if c == '\'' then
564 else if c == '\\
' then
566 else if c.ascii < 32 then
567 b.append("\\{c.ascii.to_base(8, false)}")
577 # Return the C type associated to a given Nit static type
583 # Return the name of the C structure associated to a Nit live type
584 # FIXME: move to GlobalCompiler so we can check that self is a live type
585 fun c_name: String is abstract
586 protected var c_name_cache: nullable String protected writable
589 redef class MClassType
592 var res = self.c_name_cache
593 if res != null then return res
594 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
595 self.c_name_cache = res
599 redef fun ctype: String
601 if mclass.name == "Int" then
603 else if mclass.name == "Bool" then
605 else if mclass.name == "Char" then
607 else if mclass.name == "Float" then
609 else if mclass.name == "NativeString" then
611 else if mclass.name == "NativeArray" then
612 #return "{self.arguments.first.ctype}*"
614 else if mclass.kind == extern_kind then
622 redef class MGenericType
625 var res = self.c_name_cache
626 if res != null then return res
628 for t in self.arguments do
631 self.c_name_cache = res
636 # A C function associated to a Nit method
637 # Because of customization, a given Nit method can be compiler more that once
638 abstract class AbstractRuntimeFunction
639 # The associated Nit method
640 var mmethoddef: MMethodDef
642 # The mangled c name of the runtime_function
643 # Subclasses should redefine `build_c_name` instead
646 var res = self.c_name_cache
647 if res != null then return res
648 res = self.build_c_name
649 self.c_name_cache = res
653 # Non cached version of `c_name`
654 protected fun build_c_name: String is abstract
656 private var c_name_cache: nullable String = null
658 # Implements a call of the runtime_function
659 # May inline the body or generate a C function call
660 fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
662 # Generate the code for the RuntimeFunction
663 # Warning: compile more than once compilation makes CC unhappy
664 fun compile_to_c(compiler: GlobalCompiler) is abstract
667 # A runtime function customized on a specific monomrph receiver type
668 private class CustomizedRuntimeFunction
669 super AbstractRuntimeFunction
671 # The considered reciever
672 # (usually is a live type but no strong guarantee)
675 init(mmethoddef: MMethodDef, recv: MClassType)
681 redef fun build_c_name: String
683 var res = self.c_name_cache
684 if res != null then return res
685 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
686 res = self.mmethoddef.c_name
688 res = "{mmethoddef.c_name}__{recv.c_name}"
690 self.c_name_cache = res
695 # used in the compiler worklist
697 if not o isa CustomizedRuntimeFunction then return false
698 if self.mmethoddef != o.mmethoddef then return false
699 if self.recv != o.recv then return false
704 # used in the compiler work-list
706 var res = self.mmethoddef.hash + self.recv.hash
712 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
713 return self.mmethoddef.to_s
715 return "{self.mmethoddef}@{self.recv}"
719 # compile the code customized for the reciever
720 redef fun compile_to_c(compiler)
723 var mmethoddef = self.mmethoddef
724 if not recv.is_subtype(compiler.mainmodule, null, mmethoddef.mclassdef.bound_mtype) then
725 print("problem: why do we compile {self} for {recv}?")
729 var v = compiler.new_visitor
730 var selfvar = new RuntimeVariable("self", recv, recv)
731 if compiler.runtime_type_analysis.live_types.has(recv) then
732 selfvar.is_exact = true
734 var arguments = new Array[RuntimeVariable]
735 var frame = new Frame(v, mmethoddef, recv, arguments)
739 var comment = new Buffer
740 var ret = mmethoddef.msignature.return_mtype
742 ret = v.resolve_for(ret, selfvar)
743 sig.append("{ret.ctype} ")
744 else if mmethoddef.mproperty.is_new then
746 sig.append("{ret.ctype} ")
750 sig.append(self.c_name)
751 sig.append("({recv.ctype} {selfvar}")
752 comment.append("(self: {recv}")
753 arguments.add(selfvar)
754 for i in [0..mmethoddef.msignature.arity[ do
755 var mtype = mmethoddef.msignature.mparameters[i].mtype
756 if i == mmethoddef.msignature.vararg_rank then
757 mtype = v.get_class("Array").get_mtype([mtype])
759 mtype = v.resolve_for(mtype, selfvar)
760 comment.append(", {mtype}")
761 sig.append(", {mtype.ctype} p{i}")
762 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
763 arguments.add(argvar)
768 comment.append(": {ret}")
770 compiler.header.add_decl("{sig};")
772 v.add_decl("/* method {self} for {comment} */")
773 v.add_decl("{sig} \{")
774 #v.add("printf(\"method {self} for {comment}\\n\");")
776 frame.returnvar = v.new_var(ret)
778 frame.returnlabel = v.get_name("RET_LABEL")
780 mmethoddef.compile_inside_to_c(v, arguments)
782 v.add("{frame.returnlabel.as(not null)}:;")
784 v.add("return {frame.returnvar.as(not null)};")
789 redef fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
791 var ret = self.mmethoddef.msignature.return_mtype
792 if self.mmethoddef.mproperty.is_new then
796 ret = v.resolve_for(ret, arguments.first)
798 if self.mmethoddef.can_inline(v) then
799 var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
800 frame.returnlabel = v.get_name("RET_LABEL")
802 frame.returnvar = v.new_var(ret)
804 var old_frame = v.frame
806 v.add("\{ /* Inline {self} ({arguments.join(",")}) */")
807 self.mmethoddef.compile_inside_to_c(v, arguments)
808 v.add("{frame.returnlabel.as(not null)}:(void)0;")
811 return frame.returnvar
813 v.adapt_signature(self.mmethoddef, arguments)
814 v.compiler.todo(self)
816 v.add("{self.c_name}({arguments.join(",")});")
819 var res = v.new_var(ret)
820 v.add("{res} = {self.c_name}({arguments.join(",")});")
826 # A runtime variable hold a runtime value in C.
827 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
829 # 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.
830 class RuntimeVariable
831 # The name of the variable in the C code
834 # The static type of the variable (as declard in C)
837 # The current casted type of the variable (as known in Nit)
838 var mcasttype: MType writable
840 # If the variable exaclty a mcasttype?
841 # false (usual value) means that the variable is a mcasttype or a subtype.
842 var is_exact: Bool writable = false
844 init(name: String, mtype: MType, mcasttype: MType)
848 self.mcasttype = mcasttype
849 assert not mtype.need_anchor
850 assert not mcasttype.need_anchor
853 redef fun to_s do return name
858 if self.is_exact then
864 if self.mtype == self.mcasttype then
865 type_str = "{mtype}{exact_str}"
867 type_str = "{mtype}({mcasttype}{exact_str})"
869 return "<{name}:{type_str}>"
873 # A visitor on the AST of property definition that generate the C code.
874 # Because of inlining, a visitor can visit more than one property.
875 class GlobalCompilerVisitor
876 # The associated compiler
877 var compiler: GlobalCompiler
879 init(compiler: GlobalCompiler)
881 self.compiler = compiler
882 compiler.visitors.add(self)
885 # Alias for self.compiler.mainmodule.object_type
886 fun object_type: MClassType do return self.compiler.mainmodule.object_type
888 # Alias for self.compiler.mainmodule.bool_type
889 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
891 # Force to get the primitive class named `name' or abort
892 fun get_class
(name
: String): MClass
894 return self.compiler
.mainmodule
.get_primitive_class
(name
)
897 # Force to get the primitive property named `name' in the instance `recv' or abort
898 fun get_property
(name
: String, recv
: MType): MMethod
900 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, recv
, self.compiler
.mainmodule
)
904 var frame
: nullable Frame writable
906 # Anchor a type to the main module and the current receiver
907 fun anchor
(mtype
: MType): MType
909 if not mtype
.need_anchor
then return mtype
910 #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
911 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
914 # Add a line in the main part of the generated C
921 # (used for local or global declaration)
922 fun add_decl
(s
: String)
924 self.decl_lines
.add
(s
)
927 private var lines
: List[String] = new List[String]
928 private var decl_lines
: List[String] = new List[String]
930 # The current visited AST node
931 var current_node
: nullable ANode = null
933 # Compile an expression an return its result
934 # `mtype` is the expected return type, pass null if no specific type is expected.
935 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
937 var old
= self.current_node
938 self.current_node
= nexpr
939 var res
= nexpr
.expr
(self).as(not null)
940 if mtype
!= null then
941 mtype
= self.anchor
(mtype
)
942 res
= self.autobox
(res
, mtype
)
944 var implicit_cast_to
= nexpr
.implicit_cast_to
945 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
946 var castres
= self.type_test
(res
, implicit_cast_to
)
947 self.add
("if (!{castres}) \{")
948 self.add_abort
("Cast failed")
951 self.current_node
= old
955 # Unsafely cast a value to a new type
956 # ie the result share the same C variable but my have a different mcasttype
957 # NOTE: if the adaptation is useless then `value' is returned as it.
958 # ENSURE: return.name == value.name
959 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
961 mtype
= self.anchor
(mtype
)
962 var valmtype
= value
.mcasttype
963 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
967 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
968 var res
= new RuntimeVariable(value
.name
, valmtype
, valmtype
.mtype
)
971 var res
= new RuntimeVariable(value
.name
, valmtype
, mtype
)
976 # Box or unbox a value to another type iff a C type conversion is needed
977 # ENSURE: result.mtype.ctype == mtype.ctype
978 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
980 if value
.mtype
== mtype
then
982 else if value
.mtype
.ctype
== "val*" and mtype
.ctype
== "val*" then
984 else if value
.mtype
.ctype
== "val*" then
985 return self.new_expr
("((struct {mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype
)
986 else if mtype
.ctype
== "val*" then
987 var valtype
= value
.mtype
.as(MClassType)
988 var res
= self.new_var
(mtype
)
989 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
990 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
991 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
994 self.add
("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
997 # Bad things will appen!
998 var res
= self.new_var
(mtype
)
999 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
1000 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
1005 # Correctly assign a left and a right value
1006 # Boxing and unboxing is performed if required
1007 fun assign
(left
, right
: RuntimeVariable)
1009 right
= self.autobox
(right
, left
.mtype
)
1010 self.add
("{left} = {right};")
1014 # Alias for `self.expr(nexpr, self.bool_type)'
1015 fun expr_bool
(nexpr
: AExpr): RuntimeVariable
1017 return expr
(nexpr
, bool_type
)
1020 # Compile a statement (if any)
1021 fun stmt
(nexpr
: nullable AExpr)
1023 if nexpr
== null then return
1024 var old
= self.current_node
1025 self.current_node
= nexpr
1027 self.current_node
= old
1030 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1031 fun debug
(message
: String)
1033 var node
= self.current_node
1034 if node
== null then
1035 print
"?: {message}"
1039 self.add
("/* DEBUG: {message} */")
1042 # Return a new uninitialized local runtime_variable
1043 fun new_var
(mtype
: MType): RuntimeVariable
1045 mtype
= self.anchor
(mtype
)
1046 var name
= self.get_name
("var")
1047 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1048 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1052 # Return a new uninitialized named runtime_variable
1053 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
1055 mtype
= self.anchor
(mtype
)
1056 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1057 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1061 # Return a new local runtime_variable initialized with the C expression `cexpr'.
1062 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
1064 var res
= new_var
(mtype
)
1065 self.add
("{res} = {cexpr};")
1069 # Return the local runtime_variable associated to a Nit local variable
1070 fun variable
(variable
: Variable): RuntimeVariable
1072 if self.variables
.has_key
(variable
) then
1073 return self.variables
[variable
]
1075 var name
= self.get_name
("var_{variable.name}")
1076 var mtype
= variable
.declared_type
.as(not null)
1077 mtype
= self.anchor
(mtype
)
1078 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1079 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1080 self.variables
[variable
] = res
1085 private var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
1087 # Return an unique and stable identifier associated with an escapemark
1088 fun escapemark_name
(e
: nullable EscapeMark): String
1091 if escapemark_names
.has_key
(e
) then return escapemark_names
[e
]
1093 if name
== null then name
= "label"
1094 name
= get_name
(name
)
1095 escapemark_names
[e
] = name
1099 private var escapemark_names
= new HashMap[EscapeMark, String]
1101 # Return a new name based on `s' and unique in the visitor
1102 fun get_name
(s
: String): String
1104 if not self.names
.has
(s
) then
1108 var i
= self.last
+ 1
1111 if not self.names
.has
(s2
) then
1120 private var last
: Int = 0
1122 private var names
: HashSet[String] = new HashSet[String]
1124 # Generate a return with the value `s'
1125 fun ret
(s
: RuntimeVariable)
1127 self.assign
(self.frame
.returnvar
.as(not null), s
)
1128 self.add
("goto {self.frame.returnlabel.as(not null)};")
1131 # The runtime types that are acceptable for a given receiver.
1132 fun collect_types
(recv
: RuntimeVariable): Array[MClassType]
1134 var mtype
= recv
.mcasttype
1135 if recv
.is_exact
then
1136 assert mtype
isa MClassType
1137 assert self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
)
1141 var cache
= self.compiler
.collect_types_cache
1142 if cache
.has_key
(mtype
) then
1145 var types
= new Array[MClassType]
1146 var mainmodule
= self.compiler
.mainmodule
1147 for t
in self.compiler
.runtime_type_analysis
.live_types
do
1148 if not t
.is_subtype
(mainmodule
, null, mtype
) then continue
1151 cache
[mtype
] = types
1155 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
1157 if not mtype
.need_anchor
then return mtype
1158 #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
1159 var res
= mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
1163 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable])
1165 var elttype
= arguments
.first
.mtype
1166 var recv
= "((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->values"
1167 if pname
== "[]" then
1168 self.ret
(self.new_expr
("{recv}[{arguments[1]}]", ret_type
.as(not null)))
1170 else if pname
== "[]=" then
1171 self.add
("{recv}[{arguments[1]}]={arguments[2]};")
1173 else if pname
== "copy_to" then
1174 var recv1
= "((struct {arguments[1].mcasttype.c_name}*){arguments[1]})->values"
1175 self.add
("memcpy({recv1},{recv},{arguments[2]}*sizeof({elttype.ctype}));")
1180 fun calloc_array
(ret_type
: MType, arguments
: Array[RuntimeVariable])
1182 self.ret
(self.new_expr
("NEW_{ret_type.c_name}({arguments[1]})", ret_type
))
1185 # Add a check and an abort for a null reciever is needed
1186 fun check_recv_notnull
(recv
: RuntimeVariable)
1188 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then return
1190 var maybenull
= recv
.mcasttype
isa MNullableType or recv
.mcasttype
isa MNullType
1192 self.add
("if ({recv} == NULL) \{")
1193 self.add_abort
("Reciever is null")
1198 fun compile_callsite
(callsite
: CallSite, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1200 var ret
= self.send
(callsite
.mproperty
, args
)
1204 # Generate a polymorphic send for the method `m' and the arguments `args'
1205 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1207 var types
= self.collect_types
(args
.first
)
1209 var res
: nullable RuntimeVariable
1210 var ret
= m
.intro
.msignature
.return_mtype
1212 ret
= args
.first
.mtype
1213 res
= self.new_var
(ret
)
1214 else if ret
== null then
1217 ret
= self.resolve_for
(ret
, args
.first
)
1218 res
= self.new_var
(ret
)
1221 self.add
("/* send {m} on {args.first.inspect} */")
1222 if args
.first
.mtype
.ctype
!= "val*" then
1223 var mclasstype
= args
.first
.mtype
.as(MClassType)
1224 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mclasstype
) then
1225 self.add
("/* skip, no method {m} */")
1228 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, mclasstype
)
1229 if propdefs
.length
== 0 then
1230 self.add
("/* skip, no method {m} */")
1233 assert propdefs
.length
== 1
1234 var propdef
= propdefs
.first
1235 var res2
= self.call
(propdef
, mclasstype
, args
)
1236 if res
!= null then self.assign
(res
, res2
.as(not null))
1239 var consider_null
= not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
or m
.name
== "==" or m
.name
== "!="
1240 if args
.first
.mcasttype
isa MNullableType or args
.first
.mcasttype
isa MNullType and consider_null
then
1241 # The reciever is potentially null, so we have to 3 cases: ==, != or NullPointerException
1242 self.add
("if ({args.first} == NULL) \{ /* Special null case */")
1243 if m
.name
== "==" then
1245 if args
[1].mcasttype
isa MNullableType then
1246 self.add
("{res} = ({args[1]} == NULL);")
1247 else if args
[1].mcasttype
isa MNullType then
1248 self.add
("{res} = 1; /* is null */")
1250 self.add
("{res} = 0; /* {args[1].inspect} cannot be null */")
1252 else if m
.name
== "!=" then
1254 if args
[1].mcasttype
isa MNullableType then
1255 self.add
("{res} = ({args[1]} != NULL);")
1256 else if args
[1].mcasttype
isa MNullType then
1257 self.add
("{res} = 0; /* is null */")
1259 self.add
("{res} = 1; /* {args[1].inspect} cannot be null */")
1262 self.add_abort
("Reciever is null")
1266 if types
.is_empty
then
1268 self.add
("/*BUG: no live types for {args.first.inspect} . {m}*/")
1269 self.bugtype
(args
.first
)
1274 self.add
("switch({args.first}->classid) \{")
1275 var last
= types
.last
1276 var defaultpropdef
: nullable MMethodDef = null
1278 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
1279 if propdefs
.length
== 0 then
1280 self.add
("/* skip {t}, no method {m} */")
1283 if propdefs
.length
> 1 then
1284 self.debug
("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
1286 var propdef
= propdefs
.first
1287 if propdef
.mclassdef
.mclass
.name
== "Object" and t
.ctype
== "val*" then
1288 defaultpropdef
= propdef
1291 if not self.compiler
.hardening
and t
== last
and defaultpropdef
== null then
1292 self.add
("default: /* test {t} */")
1294 self.add
("case {self.compiler.classid(t)}: /* test {t} */")
1296 var res2
= self.call
(propdef
, t
, args
)
1297 if res
!= null then self.assign
(res
, res2
.as(not null))
1300 if defaultpropdef
!= null then
1301 self.add
("default: /* default is Object */")
1302 var res2
= self.call
(defaultpropdef
, defaultpropdef
.mclassdef
.bound_mtype
, args
)
1303 if res
!= null then self.assign
(res
, res2
.as(not null))
1304 else if self.compiler
.hardening
then
1305 self.add
("default: /* bug */")
1306 self.bugtype
(args
.first
)
1312 # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
1313 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1315 assert t
isa MClassType
1316 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
1317 if propdefs
.length
== 0 then
1320 if propdefs
.length
> 1 then
1321 self.debug
("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
1323 var propdef
= propdefs
.first
1324 return self.call
(propdef
, t
, args
)
1327 fun check_valid_reciever
(recvtype
: MClassType)
1329 if self.compiler
.runtime_type_analysis
.live_types
.has
(recvtype
) or recvtype
.mclass
.name
== "Object" then return
1330 print
"{recvtype} is not a live type"
1334 # Generate a static call on a method definition
1335 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1337 check_valid_reciever
(recvtype
)
1338 #debug("call {m} on {recvtype} on {args.first}:{args.first.mtype}")
1339 if m
.mclassdef
.mclass
.name
== "Object" and recvtype
.ctype
== "val*" then
1340 recvtype
= m
.mclassdef
.bound_mtype
1342 var recv
= self.autobox
(args
.first
, recvtype
)
1343 recv
= self.autoadapt
(recv
, recvtype
)
1346 self.varargize
(m
.msignature
.as(not null), args
)
1347 if args
.length
!= m
.msignature
.arity
+ 1 then # because of self
1348 add
("printf(\"NOT YET IMPLEMENTED: Invalid arity
for {m}. {args.length} arguments given
.\\n\
"); exit(1);")
1349 debug
("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
1354 var rm
= new CustomizedRuntimeFunction(m
, recvtype
)
1355 return rm
.call
(self, args
)
1358 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
1360 var recv
= args
.first
1361 for i
in [0..m
.msignature
.arity
[ do
1362 var t
= m
.msignature
.mparameters
[i
].mtype
1363 if i
== m
.msignature
.vararg_rank
then
1366 t
= self.resolve_for
(t
, recv
)
1367 args
[i
+1] = self.autobox
(args
[i
+1], t
)
1371 # Transform varargs, in raw arguments, into a single argument of type Array
1372 # Note: this method modify the given `args`
1373 # If there is no vararg, then `args` is not modified.
1374 fun varargize
(msignature
: MSignature, args
: Array[RuntimeVariable])
1376 var recv
= args
.first
1377 var vararg_rank
= msignature
.vararg_rank
1378 if vararg_rank
>= 0 then
1379 assert args
.length
>= msignature
.arity
+ 1 # because of self
1381 args
= new Array[RuntimeVariable]
1383 args
.add
(rawargs
.first
) # recv
1385 for i
in [0..vararg_rank
[ do
1386 args
.add
(rawargs
[i
+1])
1389 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-msignature
.arity
1390 var vararg
= new Array[RuntimeVariable]
1391 for i
in [vararg_rank
..vararg_lastrank
] do
1392 vararg
.add
(rawargs
[i
+1])
1394 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
1395 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
1396 elttype
= self.resolve_for
(elttype
, recv
)
1397 args
.add
(self.array_instance
(vararg
, elttype
))
1399 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
1400 args
.add
(rawargs
[i
+1])
1403 rawargs
.add_all
(args
)
1407 fun bugtype
(recv
: RuntimeVariable)
1409 if recv
.mtype
.ctype
!= "val*" then return
1410 self.add
("fprintf(stderr, \"BTD BUG: Dynamic type is %s
, static
type is %s\\n\
", class_names[{recv}->classid], \"{recv.mcasttype}\
");")
1411 self.add
("exit(1);")
1414 # Generate a polymorphic attribute is_set test
1415 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1417 check_recv_notnull
(recv
)
1419 var types
= self.collect_types
(recv
)
1421 var res
= self.new_var
(bool_type
)
1423 if types
.is_empty
then
1424 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1428 self.add
("/* isset {a} on {recv.inspect} */")
1429 self.add
("switch({recv}->classid) \{")
1430 var last
= types
.last
1432 if not self.compiler
.hardening
and t
== last
then
1433 self.add
("default: /*{self.compiler.classid(t)}*/")
1435 self.add
("case {self.compiler.classid(t)}:")
1437 var recv2
= self.autoadapt
(recv
, t
)
1438 var ta
= a
.intro
.static_mtype
.as(not null)
1439 ta
= self.resolve_for
(ta
, recv2
)
1440 var attr
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1441 if not ta
isa MNullableType then
1442 if ta
.ctype
== "val*" then
1443 self.add
("{res} = ({attr} != NULL);")
1445 self.add
("{res} = 1; /*NOTYET isset on primitive attributes*/")
1450 if self.compiler
.hardening
then
1451 self.add
("default: /* Bug */")
1459 # Generate a polymorphic attribute read
1460 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1462 check_recv_notnull
(recv
)
1464 var types
= self.collect_types
(recv
)
1466 var ret
= a
.intro
.static_mtype
.as(not null)
1467 ret
= self.resolve_for
(ret
, recv
)
1468 var res
= self.new_var
(ret
)
1470 if types
.is_empty
then
1471 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1475 self.add
("/* read {a} on {recv.inspect} */")
1476 self.add
("switch({recv}->classid) \{")
1477 var last
= types
.last
1479 if not self.compiler
.hardening
and t
== last
then
1480 self.add
("default: /*{self.compiler.classid(t)}*/")
1482 self.add
("case {self.compiler.classid(t)}:")
1484 var recv2
= self.autoadapt
(recv
, t
)
1485 var ta
= a
.intro
.static_mtype
.as(not null)
1486 ta
= self.resolve_for
(ta
, recv2
)
1487 var res2
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1488 if not ta
isa MNullableType and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then
1489 if ta
.ctype
== "val*" then
1490 self.add
("if ({res2} == NULL) \{")
1491 self.add_abort
("Uninitialized attribute {a.name}")
1494 self.add
("/*NOTYET isset on primitive attributes*/")
1497 self.assign
(res
, res2
)
1500 if self.compiler
.hardening
then
1501 self.add
("default: /* Bug */")
1509 # Generate a polymorphic attribute write
1510 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable)
1512 check_recv_notnull
(recv
)
1514 var types
= self.collect_types
(recv
)
1516 if types
.is_empty
then
1517 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1521 self.add
("/* write {a} on {recv.inspect} */")
1522 self.add
("switch({recv}->classid) \{")
1523 var last
= types
.last
1525 if not self.compiler
.hardening
and t
== last
then
1526 self.add
("default: /*{self.compiler.classid(t)}*/")
1528 self.add
("case {self.compiler.classid(t)}:")
1530 var recv2
= self.autoadapt
(recv
, t
)
1531 var ta
= a
.intro
.static_mtype
.as(not null)
1532 ta
= self.resolve_for
(ta
, recv2
)
1533 self.add
("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
1536 if self.compiler
.hardening
then
1537 self.add
("default: /* Bug*/")
1543 # Generate a alloc-instance + init-attributes
1544 fun init_instance
(mtype
: MClassType): RuntimeVariable
1546 mtype
= self.anchor
(mtype
).as(MClassType)
1547 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1548 debug
"problem: {mtype} was detected dead"
1550 var res
= self.new_expr
("NEW_{mtype.c_name}()", mtype
)
1555 # Generate a polymorphic subtype test
1556 fun type_test
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1558 mtype
= self.anchor
(mtype
)
1559 var mclasstype
= mtype
1560 if mtype
isa MNullableType then mclasstype
= mtype
.mtype
1561 assert mclasstype
isa MClassType
1562 if not self.compiler
.runtime_type_analysis
.live_cast_types
.has
(mclasstype
) then
1563 debug
"problem: {mtype} was detected cast-dead"
1567 var types
= self.collect_types
(value
)
1569 var res
= self.new_var
(bool_type
)
1571 self.add
("/* isa {mtype} on {value.inspect} */")
1572 if value
.mtype
.ctype
!= "val*" then
1573 if value
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1574 self.add
("{res} = 1;")
1576 self.add
("{res} = 0;")
1580 if value
.mcasttype
isa MNullableType or value
.mcasttype
isa MNullType then
1581 self.add
("if ({value} == NULL) \{")
1582 if mtype
isa MNullableType then
1583 self.add
("{res} = 1; /* isa {mtype} */")
1585 self.add
("{res} = 0; /* not isa {mtype} */")
1587 self.add
("\} else ")
1589 self.add
("switch({value}->classid) \{")
1591 if t
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1592 self.add
("case {self.compiler.classid(t)}: /* {t} */")
1595 self.add
("{res} = 1;")
1597 self.add
("default:")
1598 self.add
("{res} = 0;")
1604 # Generate the code required to dynamically check if 2 objects share the same runtime type
1605 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1607 var res
= self.new_var
(bool_type
)
1608 if value2
.mtype
.ctype
== "val*" then
1609 if value1
.mtype
.ctype
== "val*" then
1610 self.add
"{res} = {value1}->classid == {value2}->classid;"
1612 self.add
"{res} = {self.compiler.classid(value1.mtype.as(MClassType))} == {value2}->classid;"
1615 if value1
.mtype
.ctype
== "val*" then
1616 self.add
"{res} = {value1}->classid == {self.compiler.classid(value2.mtype.as(MClassType))};"
1617 else if value1
.mcasttype
== value2
.mcasttype
then
1618 self.add
"{res} = 1;"
1620 self.add
"{res} = 0;"
1626 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1627 # NOTE: we do not return a RuntimeVariable "NativeString" as the class may not exist in the module/program
1628 fun class_name_string
(value
: RuntimeVariable): String
1630 var res
= self.get_name
("var_class_name")
1631 self.add_decl
("const char* {res};")
1632 if value
.mtype
.ctype
== "val*" then
1633 self.add
"{res} = class_names[{value}->classid];"
1635 self.add
"{res} = class_names[{self.compiler.classid(value.mtype.as(MClassType))}];"
1640 # Generate a Nit "is" for two runtime_variables
1641 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1643 var res
= self.new_var
(bool_type
)
1644 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1649 if value1
.mtype
.ctype
!= "val*" then
1650 if value2
.mtype
== value1
.mtype
then
1651 self.add
("{res} = {value1} == {value2};")
1652 else if value2
.mtype
.ctype
!= "val*" then
1653 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1655 var mtype1
= value1
.mtype
.as(MClassType)
1656 self.add
("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
1657 self.add
("if ({res}) \{")
1658 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1662 var s
= new Array[String]
1663 for t
in self.compiler
.live_primitive_types
do
1664 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value1
.mcasttype
) then continue
1665 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value2
.mcasttype
) then continue
1666 s
.add
"({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
1669 self.add
("{res} = {value1} == {value2};")
1671 self.add
("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")
1677 # Generate a check-init-instance
1678 fun check_init_instance
(recv
: RuntimeVariable, mtype
: MClassType)
1680 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
1682 mtype
= self.anchor
(mtype
).as(MClassType)
1683 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1684 debug
"problem: {mtype} was detected dead"
1687 self.add
("CHECK_NEW_{mtype.c_name}({recv});")
1690 # Generate an integer value
1691 fun int_instance
(value
: Int): RuntimeVariable
1693 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
1694 self.add
("{res} = {value};")
1698 # Generate an array value
1699 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable
1701 elttype
= self.anchor
(elttype
)
1702 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
1703 var res
= self.init_instance
(arraytype
)
1704 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1705 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
1707 self.add
("{nat} = NEW_{nat.mtype.c_name}({array.length});")
1708 for i
in [0..array
.length
[ do
1709 var r
= self.autobox
(array
[i
], elttype
)
1710 self.add
("((struct {nat.mtype.c_name}*) {nat})->values[{i}] = {r};")
1712 var length
= self.int_instance
(array
.length
)
1713 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
1714 self.check_init_instance
(res
, arraytype
)
1719 # Generate a string value
1720 fun string_instance
(string
: String): RuntimeVariable
1722 var mtype
= self.get_class
("String").mclass_type
1723 var name
= self.get_name
("varonce")
1724 self.add_decl
("static {mtype.ctype} {name};")
1725 var res
= self.new_var
(mtype
)
1726 self.add
("if ({name}) \{")
1727 self.add
("{res} = {name};")
1728 self.add
("\} else \{")
1729 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
1730 self.add
("{nat} = \"{string.escape_to_c}\
";")
1731 var res2
= self.init_instance
(mtype
)
1732 self.add
("{res} = {res2};")
1733 var length
= self.int_instance
(string
.length
)
1734 self.send
(self.get_property
("with_native", mtype
), [res
, nat
, length
])
1735 self.check_init_instance
(res
, mtype
)
1736 self.add
("{name} = {res};")
1741 # Generate generic abort
1742 # used by aborts, asserts, casts, etc.
1743 fun add_abort
(message
: String)
1745 if self.current_node
!= null and self.current_node
.location
.file
!= null then
1746 self.add
("fprintf(stderr, \"Runtime error
: %s
(%s
:%d
)\\n\
", \"{message.escape_to_c}\
", \"{self.current_node.location.file.filename.escape_to_c}\
", {current_node.location.line_start});")
1748 self.add
("fprintf(stderr, \"Runtime error
: %s\\n\
", \"{message.escape_to_c}\
");")
1750 self.add
("exit(1);")
1754 # A frame correspond to a visited property in a GlobalCompilerVisitor
1756 # The associated visitor
1758 var visitor
: GlobalCompilerVisitor
1760 # The executed property.
1761 # A Method in case of a call, an attribute in case of a default initialization.
1762 var mpropdef
: MPropDef
1764 # The static type of the receiver
1765 var receiver
: MClassType
1767 # Arguments of the method (the first is the receiver)
1768 var arguments
: Array[RuntimeVariable]
1770 # The runtime_variable associated to the return (in a function)
1771 var returnvar
: nullable RuntimeVariable writable = null
1773 # The label at the end of the property
1774 var returnlabel
: nullable String writable = null
1777 redef class MPropDef
1778 private var c_name_cache
: nullable String
1780 # The mangled name associated to the property
1783 var res
= self.c_name_cache
1784 if res
!= null then return res
1785 res
= "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1786 self.c_name_cache
= res
1791 redef class MMethodDef
1792 # Can the body be inlined?
1793 fun can_inline
(v
: GlobalCompilerVisitor): Bool
1795 var modelbuilder
= v
.compiler
.modelbuilder
1796 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1797 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1798 return npropdef
.can_inline
1799 else if self.mproperty
.name
== "init" then
1800 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1807 # Inline the body in another visitor
1808 fun compile_inside_to_c
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1810 var modelbuilder
= v
.compiler
.modelbuilder
1811 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1812 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1813 var oldnode
= v
.current_node
1814 v
.current_node
= npropdef
1815 self.compile_parameter_check
(v
, arguments
)
1816 npropdef
.compile_to_c
(v
, self, arguments
)
1817 v
.current_node
= oldnode
1818 else if self.mproperty
.name
== "init" then
1819 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1820 var oldnode
= v
.current_node
1821 v
.current_node
= nclassdef
1822 self.compile_parameter_check
(v
, arguments
)
1823 nclassdef
.compile_to_c
(v
, self, arguments
)
1824 v
.current_node
= oldnode
1831 # Generate type checks in the C code to check covariant parameters
1832 fun compile_parameter_check
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable])
1834 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_covariance
.value
then return
1836 for i
in [0..msignature
.arity
[ do
1837 # skip test for vararg since the array is instantiated with the correct polymorphic type
1838 if msignature
.vararg_rank
== i
then continue
1840 # skip if the cast is not required
1841 var origmtype
= self.mproperty
.intro
.msignature
.mparameters
[i
].mtype
1842 if not origmtype
.need_anchor
then continue
1844 # get the parameter type
1845 var mtype
= self.msignature
.mparameters
[i
].mtype
1848 # note that v decides if and how to implements the cast
1849 v
.add
("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1850 var cond
= v
.type_test
( arguments
[i
+1], mtype
)
1851 v
.add
("if (!{cond}) \{")
1852 #var x = v.class_name_string(arguments[i+1])
1853 #var y = v.class_name_string(arguments.first)
1854 #v.add("fprintf(stderr, \"expected {mtype} (self is %s), got %s for {arguments[i+1].inspect}\\n\", {y}, {x});")
1855 v
.add_abort
("Cast failed")
1861 redef class APropdef
1862 fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1864 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
1865 debug
("Not yet implemented")
1868 fun can_inline
: Bool do return true
1871 redef class AConcreteMethPropdef
1872 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1874 for i
in [0..mpropdef
.msignature
.arity
[ do
1875 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
1876 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
1878 # Call the implicit super-init
1879 var auto_super_inits
= self.auto_super_inits
1880 if auto_super_inits
!= null then
1881 var selfarg
= [arguments
.first
]
1882 for auto_super_init
in auto_super_inits
do
1883 if auto_super_init
.intro
.msignature
.arity
== 0 then
1884 v
.send
(auto_super_init
, selfarg
)
1886 v
.send
(auto_super_init
, arguments
)
1890 v
.stmt
(self.n_block
)
1893 redef fun can_inline
1895 if self.auto_super_inits
!= null then return false
1896 var nblock
= self.n_block
1897 if nblock
== null then return true
1898 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
1899 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
1904 redef class AInternMethPropdef
1905 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1907 var pname
= mpropdef
.mproperty
.name
1908 var cname
= mpropdef
.mclassdef
.mclass
.name
1909 var ret
= mpropdef
.msignature
.return_mtype
1911 ret
= v
.resolve_for
(ret
, arguments
.first
)
1913 if pname
!= "==" and pname
!= "!=" then
1914 v
.adapt_signature
(mpropdef
, arguments
)
1916 if cname
== "Int" then
1917 if pname
== "output" then
1918 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
1920 else if pname
== "object_id" then
1921 v
.ret
(arguments
.first
)
1923 else if pname
== "+" then
1924 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1926 else if pname
== "-" then
1927 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1929 else if pname
== "unary -" then
1930 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1932 else if pname
== "succ" then
1933 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1935 else if pname
== "prec" then
1936 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1938 else if pname
== "*" then
1939 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
1941 else if pname
== "/" then
1942 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
1944 else if pname
== "%" then
1945 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
1947 else if pname
== "lshift" then
1948 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
1950 else if pname
== "rshift" then
1951 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
1953 else if pname
== "==" then
1954 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1956 else if pname
== "!=" then
1957 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1958 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1960 else if pname
== "<" then
1961 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1963 else if pname
== ">" then
1964 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1966 else if pname
== "<=" then
1967 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1969 else if pname
== ">=" then
1970 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1972 else if pname
== "to_f" then
1973 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
1975 else if pname
== "ascii" then
1976 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1979 else if cname
== "Char" then
1980 if pname
== "output" then
1981 v
.add
("printf(\"%c\
", {arguments.first});")
1983 else if pname
== "object_id" then
1984 v
.ret
(arguments
.first
)
1986 else if pname
== "+" then
1987 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1989 else if pname
== "-" then
1990 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1992 else if pname
== "==" then
1993 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1995 else if pname
== "!=" then
1996 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1997 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1999 else if pname
== "succ" then
2000 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
2002 else if pname
== "prec" then
2003 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
2005 else if pname
== "<" then
2006 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2008 else if pname
== ">" then
2009 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2011 else if pname
== "<=" then
2012 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2014 else if pname
== ">=" then
2015 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2017 else if pname
== "to_i" then
2018 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
2020 else if pname
== "ascii" then
2021 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2024 else if cname
== "Bool" then
2025 if pname
== "output" then
2026 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
2028 else if pname
== "object_id" then
2029 v
.ret
(arguments
.first
)
2031 else if pname
== "==" then
2032 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2034 else if pname
== "!=" then
2035 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2036 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2039 else if cname
== "Float" then
2040 if pname
== "output" then
2041 v
.add
("printf(\"%f\\n\
", {arguments.first});")
2043 else if pname
== "object_id" then
2044 v
.ret
(v
.new_expr
("(double){arguments.first}", ret
.as(not null)))
2046 else if pname
== "+" then
2047 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2049 else if pname
== "-" then
2050 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2052 else if pname
== "unary -" then
2053 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2055 else if pname
== "succ" then
2056 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
2058 else if pname
== "prec" then
2059 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
2061 else if pname
== "*" then
2062 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
2064 else if pname
== "/" then
2065 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
2067 else if pname
== "==" then
2068 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2070 else if pname
== "!=" then
2071 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2072 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2074 else if pname
== "<" then
2075 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2077 else if pname
== ">" then
2078 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2080 else if pname
== "<=" then
2081 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2083 else if pname
== ">=" then
2084 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2086 else if pname
== "to_i" then
2087 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2090 else if cname
== "Char" then
2091 if pname
== "output" then
2092 v
.add
("printf(\"%c\
", {arguments.first});")
2094 else if pname
== "object_id" then
2095 v
.ret
(arguments
.first
)
2097 else if pname
== "==" then
2098 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2100 else if pname
== "!=" then
2101 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2102 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2104 else if pname
== "ascii" then
2105 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
2108 else if cname
== "NativeString" then
2109 if pname
== "[]" then
2110 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
2112 else if pname
== "[]=" then
2113 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2115 else if pname
== "copy_to" then
2116 v
.add
("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2118 else if pname
== "atoi" then
2119 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
2122 else if cname
== "NativeArray" then
2123 v
.native_array_def
(pname
, ret
, arguments
)
2126 if pname
== "exit" then
2127 v
.add
("exit({arguments[1]});")
2129 else if pname
== "sys" then
2130 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
2132 else if pname
== "calloc_string" then
2133 v
.ret
(v
.new_expr
("(char*)GC_MALLOC({arguments[1]})", ret
.as(not null)))
2135 else if pname
== "calloc_array" then
2136 v
.calloc_array
(ret
.as(not null), arguments
)
2138 else if pname
== "object_id" then
2139 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2141 else if pname
== "is_same_type" then
2142 v
.ret
(v
.is_same_type_test
(arguments
[0], arguments
[1]))
2144 else if pname
== "output_class_name" then
2145 var nat
= v
.class_name_string
(arguments
.first
)
2146 v
.add
("printf(\"%s\\n\
", {nat});")
2148 else if pname
== "native_class_name" then
2149 var nat
= v
.class_name_string
(arguments
.first
)
2150 v
.ret
(v
.new_expr
("(char*){nat}", ret
.as(not null)))
2152 else if pname
== "force_garbage_collection" then
2153 v
.add
("GC_gcollect();")
2156 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at
{location.to_s}\\n\
");")
2157 debug
("Not implemented {mpropdef}")
2161 redef class AExternMethPropdef
2162 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2165 var nextern
= self.n_extern
2166 if nextern
== null then
2167 v
.add
("fprintf(stderr, \"NOT YET IMPLEMENTED nitni
for {mpropdef} at
{location.to_s}\\n\
");")
2171 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
2172 if location
.file
!= null then
2173 var file
= location
.file
.filename
2174 v
.compiler
.add_extern
(file
)
2176 var res
: nullable RuntimeVariable = null
2177 var ret
= mpropdef
.msignature
.return_mtype
2179 ret
= v
.resolve_for
(ret
, arguments
.first
)
2180 res
= v
.new_var
(ret
)
2182 v
.adapt_signature
(mpropdef
, arguments
)
2185 v
.add
("{externname}({arguments.join(", ")});")
2187 v
.add
("{res} = {externname}({arguments.join(", ")});")
2193 redef class AExternInitPropdef
2194 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2197 var nextern
= self.n_extern
2198 if nextern
== null then
2199 debug
("{mpropdef} need extern name")
2202 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
2203 if location
.file
!= null then
2204 var file
= location
.file
.filename
2205 v
.compiler
.add_extern
(file
)
2207 v
.adapt_signature
(mpropdef
, arguments
)
2208 var ret
= arguments
.first
.mtype
2209 var res
= v
.new_var
(ret
)
2213 v
.add
("{res} = {externname}({arguments.join(", ")});")
2218 redef class AAttrPropdef
2219 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2221 if arguments
.length
== 1 then
2222 var res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
2223 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
2225 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arguments
[1])
2229 fun init_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
2231 var nexpr
= self.n_expr
2232 if nexpr
!= null then
2233 var oldnode
= v
.current_node
2234 v
.current_node
= self
2235 var old_frame
= v
.frame
2236 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
2238 var value
= v
.expr
(nexpr
, self.mpropdef
.static_mtype
)
2239 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
2241 v
.current_node
= oldnode
2245 fun check_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
2247 var nexpr
= self.n_expr
2248 if nexpr
!= null then return
2250 var oldnode
= v
.current_node
2251 v
.current_node
= self
2252 var old_frame
= v
.frame
2253 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
2255 # Force read to check the initialization
2256 v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
2258 v
.current_node
= oldnode
2262 redef class AClassdef
2263 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
2265 if mpropdef
== self.mfree_init
then
2266 var super_inits
= self.super_inits
2267 if super_inits
!= null then
2268 assert arguments
.length
== 1
2269 for su
in super_inits
do
2270 v
.send
(su
, arguments
)
2274 var recv
= arguments
.first
2276 # Collect undefined attributes
2277 for npropdef
in self.n_propdefs
do
2278 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
2279 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, arguments
[i
])
2289 redef class ADeferredMethPropdef
2290 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2292 v
.add_abort
("Deferred method called")
2295 redef fun can_inline
do return true
2299 # Try to compile self as an expression
2300 # Do not call this method directly, use `v.expr' instead
2301 private fun expr
(v
: GlobalCompilerVisitor): nullable RuntimeVariable
2303 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
2304 var mtype
= self.mtype
2305 if mtype
== null then
2308 var res
= v
.new_var
(mtype
)
2309 v
.add
("/* {res} = NOT YET {class_name} */")
2314 # Try to compile self as a statement
2315 # Do not call this method directly, use `v.stmt' instead
2316 private fun stmt
(v
: GlobalCompilerVisitor)
2319 if res
!= null then v
.add
("{res};")
2324 redef class ABlockExpr
2327 for e
in self.n_expr
do
2333 redef class AVardeclExpr
2336 var variable
= self.variable
.as(not null)
2337 var ne
= self.n_expr
2339 var i
= v
.expr
(ne
, variable
.declared_type
)
2340 v
.assign
(v
.variable
(variable
), i
)
2345 redef class AVarExpr
2348 var res
= v
.variable
(self.variable
.as(not null))
2349 var mtype
= self.mtype
.as(not null)
2350 return v
.autoadapt
(res
, mtype
)
2354 redef class AVarAssignExpr
2357 var variable
= self.variable
.as(not null)
2358 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
2359 v
.assign
(v
.variable
(variable
), i
)
2363 redef class AVarReassignExpr
2366 var variable
= self.variable
.as(not null)
2367 var vari
= v
.variable
(variable
)
2368 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
2369 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
2371 v
.assign
(v
.variable
(variable
), res
)
2375 redef class ASelfExpr
2378 return v
.frame
.arguments
.first
2382 redef class AContinueExpr
2385 v
.add
("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
2389 redef class ABreakExpr
2392 v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
2396 redef class AReturnExpr
2399 var nexpr
= self.n_expr
2400 if nexpr
!= null then
2401 var returnvar
= v
.frame
.returnvar
.as(not null)
2402 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
2403 v
.assign
(returnvar
, i
)
2405 v
.add
("goto {v.frame.returnlabel.as(not null)};")
2409 redef class AAbortExpr
2412 v
.add_abort
("Aborted")
2419 var cond
= v
.expr_bool
(self.n_expr
)
2420 v
.add
("if ({cond})\{")
2428 redef class AIfexprExpr
2431 var res
= v
.new_var
(self.mtype
.as(not null))
2432 var cond
= v
.expr_bool
(self.n_expr
)
2433 v
.add
("if ({cond})\{")
2434 v
.assign
(res
, v
.expr
(self.n_then
, null))
2436 v
.assign
(res
, v
.expr
(self.n_else
, null))
2445 v
.stmt
(self.n_block
)
2446 var escapemark
= self.escapemark
2447 if escapemark
!= null then
2448 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2453 redef class AWhileExpr
2457 var cond
= v
.expr_bool
(self.n_expr
)
2458 v
.add
("if (!{cond}) break;")
2459 v
.stmt
(self.n_block
)
2460 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2462 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2466 redef class ALoopExpr
2470 v
.stmt
(self.n_block
)
2471 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2473 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2477 redef class AForExpr
2480 var cl
= v
.expr
(self.n_expr
, null)
2481 var it_meth
= self.method_iterator
2482 assert it_meth
!= null
2483 var it
= v
.send
(it_meth
, [cl
])
2486 var isok_meth
= self.method_is_ok
2487 assert isok_meth
!= null
2488 var ok
= v
.send
(isok_meth
, [it
])
2490 v
.add
("if(!{ok}) break;")
2491 if self.variables
.length
== 1 then
2492 var item_meth
= self.method_item
2493 assert item_meth
!= null
2494 var i
= v
.send
(item_meth
, [it
])
2496 v
.assign
(v
.variable
(variables
.first
), i
)
2497 else if self.variables
.length
== 2 then
2498 var key_meth
= self.method_key
2499 assert key_meth
!= null
2500 var i
= v
.send
(key_meth
, [it
])
2502 v
.assign
(v
.variable
(variables
[0]), i
)
2503 var item_meth
= self.method_item
2504 assert item_meth
!= null
2505 i
= v
.send
(item_meth
, [it
])
2507 v
.assign
(v
.variable
(variables
[1]), i
)
2511 v
.stmt
(self.n_block
)
2512 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2513 var next_meth
= self.method_next
2514 assert next_meth
!= null
2515 v
.send
(next_meth
, [it
])
2517 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2521 redef class AAssertExpr
2524 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
2526 var cond
= v
.expr_bool
(self.n_expr
)
2527 v
.add
("if (!{cond}) \{")
2531 v
.add_abort
("Assert '{nid.text}' failed")
2533 v
.add_abort
("Assert failed")
2542 var res
= v
.new_var
(self.mtype
.as(not null))
2543 var i1
= v
.expr_bool
(self.n_expr
)
2544 v
.add
("if ({i1}) \{")
2547 var i2
= v
.expr_bool
(self.n_expr2
)
2548 v
.add
("{res} = {i2};")
2554 redef class AAndExpr
2557 var res
= v
.new_var
(self.mtype
.as(not null))
2558 var i1
= v
.expr_bool
(self.n_expr
)
2559 v
.add
("if (!{i1}) \{")
2562 var i2
= v
.expr_bool
(self.n_expr2
)
2563 v
.add
("{res} = {i2};")
2569 redef class ANotExpr
2572 var cond
= v
.expr_bool
(self.n_expr
)
2573 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2577 redef class AOrElseExpr
2580 var res
= v
.new_var
(self.mtype
.as(not null))
2581 var i1
= v
.expr
(self.n_expr
, null)
2582 v
.add
("if ({i1}!=NULL) \{")
2585 var i2
= v
.expr
(self.n_expr2
, null)
2595 var value1
= v
.expr
(self.n_expr
, null)
2596 var value2
= v
.expr
(self.n_expr2
, null)
2597 return v
.equal_test
(value1
, value2
)
2601 redef class AIntExpr
2604 return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2608 redef class AFloatExpr
2611 return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2615 redef class ACharExpr
2618 return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2622 redef class AArrayExpr
2625 var mtype
= self.mtype
.as(MClassType).arguments
.first
2626 var array
= new Array[RuntimeVariable]
2627 for nexpr
in self.n_exprs
.n_exprs
do
2628 var i
= v
.expr
(nexpr
, mtype
)
2631 return v
.array_instance
(array
, mtype
)
2635 redef class AStringFormExpr
2638 return v
.string_instance
(self.value
.as(not null))
2642 redef class ASuperstringExpr
2645 var array
= new Array[RuntimeVariable]
2646 for ne
in self.n_exprs
do
2647 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
2648 var i
= v
.expr
(ne
, null)
2651 var a
= v
.array_instance
(array
, v
.object_type
)
2652 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2657 redef class ACrangeExpr
2660 var i1
= v
.expr
(self.n_expr
, null)
2661 var i2
= v
.expr
(self.n_expr2
, null)
2662 var mtype
= self.mtype
.as(MClassType)
2663 var res
= v
.init_instance
(mtype
)
2664 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2665 v
.check_init_instance
(res
, mtype
)
2670 redef class AOrangeExpr
2673 var i1
= v
.expr
(self.n_expr
, null)
2674 var i2
= v
.expr
(self.n_expr2
, null)
2675 var mtype
= self.mtype
.as(MClassType)
2676 var res
= v
.init_instance
(mtype
)
2677 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2678 v
.check_init_instance
(res
, mtype
)
2683 redef class ATrueExpr
2686 return v
.new_expr
("1", self.mtype
.as(not null))
2690 redef class AFalseExpr
2693 return v
.new_expr
("0", self.mtype
.as(not null))
2697 redef class ANullExpr
2700 var res
= v
.new_expr
("NULL", self.mtype
.as(not null))
2705 redef class AIsaExpr
2708 var i
= v
.expr
(self.n_expr
, null)
2709 return v
.type_test
(i
, self.cast_type
.as(not null))
2713 redef class AAsCastExpr
2716 var i
= v
.expr
(self.n_expr
, null)
2717 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2719 var cond
= v
.type_test
(i
, self.mtype
.as(not null))
2720 v
.add
("if (!{cond}) \{")
2721 v
.add_abort
("Cast failed")
2727 redef class AAsNotnullExpr
2730 var i
= v
.expr
(self.n_expr
, null)
2731 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2733 v
.add
("if ({i} == NULL) \{")
2734 v
.add_abort
("Cast failed")
2740 redef class AParExpr
2743 return v
.expr
(self.n_expr
, null)
2747 redef class AOnceExpr
2750 var mtype
= self.mtype
.as(not null)
2751 var name
= v
.get_name
("varonce")
2752 var guard
= v
.get_name
(name
+ "_guard")
2753 v
.add_decl
("static {mtype.ctype} {name};")
2754 v
.add_decl
("static int {guard};")
2755 var res
= v
.new_var
(mtype
)
2756 v
.add
("if ({guard}) \{")
2757 v
.add
("{res} = {name};")
2759 var i
= v
.expr
(self.n_expr
, mtype
)
2760 v
.add
("{res} = {i};")
2761 v
.add
("{name} = {res};")
2762 v
.add
("{guard} = 1;")
2768 redef class ASendExpr
2771 var recv
= v
.expr
(self.n_expr
, null)
2773 for a
in self.raw_arguments
.as(not null) do
2774 args
.add
(v
.expr
(a
, null))
2776 return v
.compile_callsite
(self.callsite
.as(not null), args
)
2780 redef class ASendReassignFormExpr
2783 var recv
= v
.expr
(self.n_expr
, null)
2785 for a
in self.raw_arguments
.as(not null) do
2786 args
.add
(v
.expr
(a
, null))
2788 var value
= v
.expr
(self.n_value
, null)
2790 var left
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2793 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
2797 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
2801 redef class ASuperExpr
2804 var recv
= v
.frame
.arguments
.first
2806 for a
in self.n_args
.n_exprs
do
2807 args
.add
(v
.expr
(a
, null))
2809 if args
.length
== 1 then
2810 args
= v
.frame
.arguments
2813 var mproperty
= self.mproperty
2814 if mproperty
!= null then
2815 if mproperty
.intro
.msignature
.arity
== 0 then
2819 var res
= v
.send
(mproperty
, args
)
2823 # stantard call-next-method
2824 var mpropdef
= v
.frame
.mpropdef
2825 # FIXME: we do not want an ugly static call!
2826 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
2827 if mpropdefs
.length
!= 1 then
2828 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
2829 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
2831 mpropdef
= mpropdefs
.first
2832 assert mpropdef
isa MMethodDef
2833 var res
= v
.call
(mpropdef
, recv
.mtype
.as(MClassType), args
)
2838 redef class ANewExpr
2841 var mtype
= self.mtype
.as(MClassType)
2843 var ctype
= mtype
.ctype
2844 if ctype
== "val*" then
2845 recv
= v
.init_instance
(mtype
)
2846 else if ctype
== "void*" then
2847 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2849 debug
("cannot new {mtype}")
2853 for a
in self.n_args
.n_exprs
do
2854 args
.add
(v
.expr
(a
, null))
2856 var res2
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2857 if res2
!= null then
2858 #self.debug("got {res2} from {mproperty}. drop {recv}")
2861 v
.check_init_instance
(recv
, mtype
)
2866 redef class AAttrExpr
2869 var recv
= v
.expr
(self.n_expr
, null)
2870 var mproperty
= self.mproperty
.as(not null)
2871 return v
.read_attribute
(mproperty
, recv
)
2875 redef class AAttrAssignExpr
2878 var recv
= v
.expr
(self.n_expr
, null)
2879 var i
= v
.expr
(self.n_value
, null)
2880 var mproperty
= self.mproperty
.as(not null)
2881 v
.write_attribute
(mproperty
, recv
, i
)
2885 redef class AAttrReassignExpr
2888 var recv
= v
.expr
(self.n_expr
, null)
2889 var value
= v
.expr
(self.n_value
, null)
2890 var mproperty
= self.mproperty
.as(not null)
2891 var attr
= v
.read_attribute
(mproperty
, recv
)
2892 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
2894 v
.write_attribute
(mproperty
, recv
, res
)
2898 redef class AIssetAttrExpr
2901 var recv
= v
.expr
(self.n_expr
, null)
2902 var mproperty
= self.mproperty
.as(not null)
2903 return v
.isset_attribute
(mproperty
, recv
)
2907 redef class ADebugTypeExpr