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_other
: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
56 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_hardening
)
57 self.option_context
.add_option
(self.opt_no_check_covariance
, self.opt_no_check_initialization
, self.opt_no_check_assert
, self.opt_no_check_other
)
61 redef class ModelBuilder
62 # Entry point to performs a global compilation on the AST of a complete program.
63 # `mainmodule` is the main module of the program
64 # `runtime_type_analysis` is a already computer type analysis.
65 fun run_global_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
68 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
70 var compiler
= new GlobalCompiler(mainmodule
, runtime_type_analysis
, self)
71 var v
= compiler
.header
73 for t
in runtime_type_analysis
.live_types
do
74 compiler
.declare_runtimeclass
(v
, t
)
77 compiler
.compile_class_names
79 # Init instance code (allocate and init-arguments)
80 for t
in runtime_type_analysis
.live_types
do
81 if t
.ctype
== "val*" then
82 compiler
.generate_init_instance
(t
)
83 compiler
.generate_check_init_instance
(t
)
85 compiler
.generate_box_instance
(t
)
89 # Compile until all runtime_functions are visited
91 while not compiler
.todos
.is_empty
do
92 var m
= compiler
.todos
.shift
93 self.toolcontext
.info
("Compile {m} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
94 m
.compile_to_c
(compiler
)
96 self.toolcontext
.info
("Total methods to compile to C: {compiler.visitors.length}", 2)
99 self.toolcontext
.info
("*** END VISITING: {time1-time0} ***", 2)
100 write_and_make
(compiler
)
103 protected fun write_and_make
(compiler
: GlobalCompiler)
105 var mainmodule
= compiler
.mainmodule
107 # Generate the .h and .c files
108 # A single C file regroups many compiled rumtime functions
109 # 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
112 var outname
= self.toolcontext
.opt_output
.value
113 if outname
== null then
114 outname
= "{mainmodule.name}.bin"
117 var hfilename
= ".nit_compile/{mainmodule.name}.1.h"
118 var h
= new OFStream.open
(hfilename
)
119 for l
in compiler
.header
.decl_lines
do
125 var cfiles
= new Array[String]
127 var file
: nullable OFStream = null
132 for vis
in compiler
.visitors
do
133 count
+= vis
.lines
.length
134 if file
== null or count
> 10000 then
136 if file
!= null then file
.close
137 var cfilename
= ".nit_compile/{mainmodule.name}.{i}.c"
138 cfiles
.add
(cfilename
)
139 file
= new OFStream.open
(cfilename
)
140 file
.write
"#include \"{mainmodule.name}.1.h\
"\n"
141 count
= vis
.lines
.length
143 if vis
!= compiler
.header
then
144 for l
in vis
.decl_lines
do
149 for l
in vis
.lines
do
154 if file
!= null then file
.close
156 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
158 # Generate the Makefile
160 var makename
= ".nit_compile/{mainmodule.name}.mk"
161 var makefile
= new OFStream.open
(makename
)
163 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
164 makefile
.write
("all: {outname}\n\n")
166 var ofiles
= new Array[String]
167 # Compile each generated file
169 var o
= f
.strip_extension
(".c") + ".o"
170 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
173 # Compile each required extern body into a specific .o
174 for f
in compiler
.extern_bodies
do
176 var o
= ".nit_compile/{mainmodule.name}.{i}.o"
177 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
181 makefile
.write
("{outname}: {ofiles.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outname} {ofiles.join(" ")} $(LDLIBS)\n\n")
183 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
185 self.toolcontext
.info
("Generated makefile: {makename}", 2)
188 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
190 # Execute the Makefile
192 if self.toolcontext
.opt_no_cc
.value
then return
195 self.toolcontext
.info
("*** COMPILING C ***", 1)
196 self.toolcontext
.info
("make -B -f {makename} -j 4", 2)
199 if self.toolcontext
.verbose_level
>= 3 then
200 res
= sys
.system
("make -B -f {makename} -j 4 2>&1")
202 res
= sys
.system
("make -B -f {makename} -j 4 2>&1 >/dev/null")
205 toolcontext
.error
(null, "make failed! Error code: {res}.")
209 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
213 # Singleton that store the knowledge about the compilation process
215 # The main module of the program
216 var mainmodule
: MModule
218 # The result of the RTA (used to know live types and methods)
219 var runtime_type_analysis
: RapidTypeAnalysis
221 # The modeulbuilder used to know the model and the AST
222 var modelbuilder
: ModelBuilder
224 # Is hardening asked? (see --hardening)
225 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
227 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, modelbuilder
: ModelBuilder)
229 self.header
= new_visitor
230 self.mainmodule
= mainmodule
231 self.runtime_type_analysis
= runtime_type_analysis
232 self.modelbuilder
= modelbuilder
233 self.live_primitive_types
= new Array[MClassType]
234 for t
in runtime_type_analysis
.live_types
do
235 if t
.ctype
!= "val*" then
236 self.live_primitive_types
.add
(t
)
240 # Header declarations
243 # The main function of the C
244 self.compile_main_function
247 protected fun compile_header
do
249 self.header
.add_decl
("#include <stdlib.h>")
250 self.header
.add_decl
("#include <stdio.h>")
251 self.header
.add_decl
("#include <string.h>")
252 # TODO: Better way to activate the GC
253 self.header
.add_decl
("#include <gc/gc.h>")
254 #self.header.add_decl("#define GC_MALLOC(x) calloc(1, (x))")
256 compile_header_structs
258 # Global variable used by the legacy native interface
259 self.header
.add_decl
("extern int glob_argc;")
260 self.header
.add_decl
("extern char **glob_argv;")
261 self.header
.add_decl
("extern val *glob_sys;")
264 # Class names (for the class_name and output_class_name methods)
265 protected fun compile_class_names
do
266 self.header
.add_decl
("extern const char const * class_names[];")
267 self.header
.add
("const char const * class_names[] = \{")
268 for t
in self.runtime_type_analysis
.live_types
do
269 self.header
.add
("\"{t}\
", /* {self.classid(t)} */")
271 self.header
.add
("\};")
274 # Declaration of structures the live Nit types
275 # Each live type is generated as an independent C `struct' type.
276 # They only share a common first field `classid` used to implement the polymorphism.
277 # Usualy, all C variables that refers to a Nit object are typed on the abstract struct `val' that contains only the `classid` field.
278 protected fun compile_header_structs
do
279 self.header
.add_decl
("typedef struct \{int classid;\} val; /* general C type representing a Nit instance. */")
282 # Subset of runtime_type_analysis.live_types that contains only primitive types
283 # Used to implement the equal test
284 var live_primitive_types
: Array[MClassType]
286 # runtime_functions that need to be compiled
287 private var todos
: List[AbstractRuntimeFunction] = new List[AbstractRuntimeFunction]
289 # runtime_functions already seen (todo or done)
290 private var seen
: HashSet[AbstractRuntimeFunction] = new HashSet[AbstractRuntimeFunction]
291 fun todo
(m
: AbstractRuntimeFunction)
293 if seen
.has
(m
) then return
298 # Where global declaration are stored (the main .h)
300 # FIXME: should not be a vistor but just somewhere to store lines
301 # FIXME: should not have a global .h since it does not help recompilations
302 var header
: GlobalCompilerVisitor writable
304 # The list of all associated visitors
305 # Used to generate .c files
306 # FIXME: should not be vistors but just somewhere to store lines
307 private var visitors
: List[GlobalCompilerVisitor] = new List[GlobalCompilerVisitor]
309 # List of additional .c files required to compile (native interface)
310 var extern_bodies
= new ArraySet[String]
312 # Return the C symbol associated to a live type runtime
313 # REQUIRE: self.runtime_type_analysis.live_types.has(mtype)
314 fun classid
(mtype
: MClassType): String
316 if self.classids
.has_key
(mtype
) then
317 return self.classids
[mtype
]
319 print
"No classid for {mtype}"
323 # Cache for classid (computed by declare_runtimeclass)
324 protected var classids
: HashMap[MClassType, String] = new HashMap[MClassType, String]
326 # Declare C structures and identifiers for a runtime class
327 fun declare_runtimeclass
(v
: GlobalCompilerVisitor, mtype
: MClassType)
329 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
330 v
.add_decl
("/* runtime class {mtype} */")
331 var idnum
= classids
.length
332 var idname
= "ID_" + mtype
.c_name
333 self.classids
[mtype
] = idname
334 v
.add_decl
("#define {idname} {idnum} /* {mtype} */")
336 v
.add_decl
("struct {mtype.c_name} \{")
337 v
.add_decl
("int classid; /* must be {idname} */")
339 if mtype
.mclass
.name
== "NativeArray" then
340 # NativeArrays are just a instance header followed by an array of values
341 v
.add_decl
("{mtype.arguments.first.ctype} values[1];")
344 if mtype
.ctype
!= "val*" then
345 # Is the Nit type is native then the struct is a box with two fields:
346 # * the `classid` to be polymorph
347 # * the `value` that contains the native value.
348 v
.add_decl
("{mtype.ctype} value;")
351 # Collect all attributes and associate them a field in the structure.
352 # Note: we do not try to optimize the order and helps CC to optimize the client code.
353 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
354 for p
in cd
.intro_mproperties
do
355 if not p
isa MAttribute then continue
356 var t
= p
.intro
.static_mtype
.as(not null)
357 t
= t
.anchor_to
(self.mainmodule
, mtype
)
358 v
.add_decl
("{t.ctype} {p.intro.c_name}; /* {p}: {t} */")
364 # Generate the init-instance of a live type (allocate + init-instance)
365 fun generate_init_instance
(mtype
: MClassType)
367 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
368 assert mtype
.ctype
== "val*"
369 var v
= self.new_visitor
371 var is_native_array
= mtype
.mclass
.name
== "NativeArray"
374 if is_native_array
then
380 self.header
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}({sig});")
381 v
.add_decl
("/* allocate {mtype} */")
382 v
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}({sig}) \{")
383 var res
= v
.new_var
(mtype
)
385 if is_native_array
then
386 var mtype_elt
= mtype
.arguments
.first
387 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}) + length*sizeof({mtype_elt.ctype}));")
389 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}));")
391 v
.add
("{res}->classid = {self.classid(mtype)};")
393 self.generate_init_attr
(v
, res
, mtype
)
394 v
.add
("return {res};")
398 fun generate_check_init_instance
(mtype
: MClassType)
400 if self.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
402 var v
= self.new_visitor
403 var res
= new RuntimeVariable("self", mtype
, mtype
)
404 self.header
.add_decl
("void CHECK_NEW_{mtype.c_name}({mtype.ctype});")
405 v
.add_decl
("/* allocate {mtype} */")
406 v
.add_decl
("void CHECK_NEW_{mtype.c_name}({mtype.ctype} {res}) \{")
407 self.generate_check_attr
(v
, res
, mtype
)
411 # Generate code that collect initialize the attributes on a new instance
412 fun generate_init_attr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable, mtype
: MClassType)
414 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
416 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
417 for npropdef
in n
.n_propdefs
do
418 if npropdef
isa AAttrPropdef then
419 npropdef
.init_expr
(v
, recv
)
425 # Generate a check-init-instance
426 fun generate_check_attr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable, mtype
: MClassType)
428 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
430 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
431 for npropdef
in n
.n_propdefs
do
432 if npropdef
isa AAttrPropdef then
433 npropdef
.check_expr
(v
, recv
)
439 fun generate_box_instance
(mtype
: MClassType)
441 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
442 assert mtype
.ctype
!= "val*"
443 var v
= self.new_visitor
445 self.header
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype});")
446 v
.add_decl
("/* allocate {mtype} */")
447 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
448 v
.add
("struct {mtype.c_name}*res = GC_MALLOC(sizeof(struct {mtype.c_name}));")
449 v
.add
("res->classid = {self.classid(mtype)};")
450 v
.add
("res->value = value;")
451 v
.add
("return (val*)res;")
456 # look for a needed .h and .c file for a given .nit source-file
457 # FIXME: bad API, parameter should be a MModule, not its source-file
458 fun add_extern
(file
: String)
460 file
= file
.strip_extension
(".nit")
461 var tryfile
= file
+ ".nit.h"
462 if tryfile
.file_exists
then
463 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
465 tryfile
= file
+ "_nit.h"
466 if tryfile
.file_exists
then
467 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
469 tryfile
= file
+ ".nit.c"
470 if tryfile
.file_exists
then
471 self.extern_bodies
.add
(tryfile
)
473 tryfile
= file
+ "_nit.c"
474 if tryfile
.file_exists
then
475 self.extern_bodies
.add
(tryfile
)
479 # Initialize a visitor specific for a compiler engine
480 fun new_visitor
: GlobalCompilerVisitor do return new GlobalCompilerVisitor(self)
482 # Generate the main C function.
484 # allocate the Sys object if it exists
485 # call init if is exists
486 # call main if it exists
487 fun compile_main_function
489 var v
= self.new_visitor
490 v
.add_decl
("int glob_argc;")
491 v
.add_decl
("char **glob_argv;")
492 v
.add_decl
("val *glob_sys;")
493 v
.add_decl
("int main(int argc, char** argv) \{")
494 v
.add
("glob_argc = argc; glob_argv = argv;")
495 var main_type
= mainmodule
.sys_type
496 if main_type
!= null then
497 var mainmodule
= v
.compiler
.mainmodule
498 var glob_sys
= v
.init_instance
(main_type
)
499 v
.add
("glob_sys = {glob_sys};")
500 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
501 if main_init
!= null then
502 v
.send
(main_init
, [glob_sys
])
504 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
505 if main_method
!= null then
506 v
.send
(main_method
, [glob_sys
])
514 private var collect_types_cache
: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
518 # Mangle a string to be a unique valid C identifier
519 fun to_cmangle
: String
522 var underscore
= false
524 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
530 res
.append
('_'.ascii
.to_s
)
533 if c
>= '0' and c
<= '9' then
536 else if c
== '_' then
541 res
.append
(c
.ascii
.to_s
)
549 # Escape " \ ' and non printable characters for literal C strings or characters
550 fun escape_to_c
: String
556 else if c
== '\0' then
558 else if c
== '"' then
560 else if c == '\'' then
562 else if c == '\\
' then
564 else if c.ascii < 32 then
565 b.append("\\{c.ascii.to_base(8, false)}")
575 # Return the C type associated to a given Nit static type
581 # Return the name of the C structure associated to a Nit live type
582 # FIXME: move to GlobalCompiler so we can check that self is a live type
583 fun c_name: String is abstract
584 protected var c_name_cache: nullable String protected writable
587 redef class MClassType
590 var res = self.c_name_cache
591 if res != null then return res
592 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
593 self.c_name_cache = res
597 redef fun ctype: String
599 if mclass.name == "Int" then
601 else if mclass.name == "Bool" then
603 else if mclass.name == "Char" then
605 else if mclass.name == "Float" then
607 else if mclass.name == "NativeString" then
609 else if mclass.name == "NativeArray" then
610 #return "{self.arguments.first.ctype}*"
612 else if mclass.kind == extern_kind then
620 redef class MGenericType
623 var res = self.c_name_cache
624 if res != null then return res
626 for t in self.arguments do
629 self.c_name_cache = res
634 # A C function associated to a Nit method
635 # Because of customization, a given Nit method can be compiler more that once
636 abstract class AbstractRuntimeFunction
637 # The associated Nit method
638 var mmethoddef: MMethodDef
640 # The mangled c name of the runtime_function
641 # Subclasses should redefine `build_c_name` instead
644 var res = self.c_name_cache
645 if res != null then return res
646 res = self.build_c_name
647 self.c_name_cache = res
651 # Non cached version of `c_name`
652 protected fun build_c_name: String is abstract
654 private var c_name_cache: nullable String = null
656 # Implements a call of the runtime_function
657 # May inline the body or generate a C function call
658 fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
660 # Generate the code for the RuntimeFunction
661 # Warning: compile more than once compilation makes CC unhappy
662 fun compile_to_c(compiler: GlobalCompiler) is abstract
665 # A runtime function customized on a specific monomrph receiver type
666 private class CustomizedRuntimeFunction
667 super AbstractRuntimeFunction
669 # The considered reciever
670 # (usually is a live type but no strong guarantee)
673 init(mmethoddef: MMethodDef, recv: MClassType)
679 redef fun build_c_name: String
681 var res = self.c_name_cache
682 if res != null then return res
683 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
684 res = self.mmethoddef.c_name
686 res = "{mmethoddef.c_name}__{recv.c_name}"
688 self.c_name_cache = res
693 # used in the compiler worklist
695 if not o isa CustomizedRuntimeFunction then return false
696 if self.mmethoddef != o.mmethoddef then return false
697 if self.recv != o.recv then return false
702 # used in the compiler work-list
704 var res = self.mmethoddef.hash + self.recv.hash
710 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
711 return self.mmethoddef.to_s
713 return "{self.mmethoddef}@{self.recv}"
717 # compile the code customized for the reciever
718 redef fun compile_to_c(compiler)
721 var mmethoddef = self.mmethoddef
722 if not recv.is_subtype(compiler.mainmodule, null, mmethoddef.mclassdef.bound_mtype) then
723 print("problem: why do we compile {self} for {recv}?")
727 var v = compiler.new_visitor
728 var selfvar = new RuntimeVariable("self", recv, recv)
729 if compiler.runtime_type_analysis.live_types.has(recv) then
730 selfvar.is_exact = true
732 var arguments = new Array[RuntimeVariable]
733 var frame = new Frame(v, mmethoddef, recv, arguments)
737 var comment = new Buffer
738 var ret = mmethoddef.msignature.return_mtype
740 ret = v.resolve_for(ret, selfvar)
741 sig.append("{ret.ctype} ")
742 else if mmethoddef.mproperty.is_new then
744 sig.append("{ret.ctype} ")
748 sig.append(self.c_name)
749 sig.append("({recv.ctype} {selfvar}")
750 comment.append("(self: {recv}")
751 arguments.add(selfvar)
752 for i in [0..mmethoddef.msignature.arity[ do
753 var mtype = mmethoddef.msignature.mparameters[i].mtype
754 if i == mmethoddef.msignature.vararg_rank then
755 mtype = v.get_class("Array").get_mtype([mtype])
757 mtype = v.resolve_for(mtype, selfvar)
758 comment.append(", {mtype}")
759 sig.append(", {mtype.ctype} p{i}")
760 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
761 arguments.add(argvar)
766 comment.append(": {ret}")
768 compiler.header.add_decl("{sig};")
770 v.add_decl("/* method {self} for {comment} */")
771 v.add_decl("{sig} \{")
772 #v.add("printf(\"method {self} for {comment}\\n\");")
774 frame.returnvar = v.new_var(ret)
776 frame.returnlabel = v.get_name("RET_LABEL")
778 mmethoddef.compile_inside_to_c(v, arguments)
780 v.add("{frame.returnlabel.as(not null)}:;")
782 v.add("return {frame.returnvar.as(not null)};")
787 redef fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
789 var ret = self.mmethoddef.msignature.return_mtype
790 if self.mmethoddef.mproperty.is_new then
794 ret = v.resolve_for(ret, arguments.first)
796 if self.mmethoddef.can_inline(v) then
797 var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
798 frame.returnlabel = v.get_name("RET_LABEL")
800 frame.returnvar = v.new_var(ret)
802 var old_frame = v.frame
804 v.add("\{ /* Inline {self} ({arguments.join(",")}) */")
805 self.mmethoddef.compile_inside_to_c(v, arguments)
806 v.add("{frame.returnlabel.as(not null)}:(void)0;")
809 return frame.returnvar
811 v.adapt_signature(self.mmethoddef, arguments)
812 v.compiler.todo(self)
814 v.add("{self.c_name}({arguments.join(",")});")
817 var res = v.new_var(ret)
818 v.add("{res} = {self.c_name}({arguments.join(",")});")
824 # A runtime variable hold a runtime value in C.
825 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
827 # 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.
828 class RuntimeVariable
829 # The name of the variable in the C code
832 # The static type of the variable (as declard in C)
835 # The current casted type of the variable (as known in Nit)
836 var mcasttype: MType writable
838 # If the variable exaclty a mcasttype?
839 # false (usual value) means that the variable is a mcasttype or a subtype.
840 var is_exact: Bool writable = false
842 init(name: String, mtype: MType, mcasttype: MType)
846 self.mcasttype = mcasttype
847 assert not mtype.need_anchor
848 assert not mcasttype.need_anchor
851 redef fun to_s do return name
856 if self.is_exact then
862 if self.mtype == self.mcasttype then
863 type_str = "{mtype}{exact_str}"
865 type_str = "{mtype}({mcasttype}{exact_str})"
867 return "<{name}:{type_str}>"
871 # A visitor on the AST of property definition that generate the C code.
872 # Because of inlining, a visitor can visit more than one property.
873 class GlobalCompilerVisitor
874 # The associated compiler
875 var compiler: GlobalCompiler
877 init(compiler: GlobalCompiler)
879 self.compiler = compiler
880 compiler.visitors.add(self)
883 # Alias for self.compiler.mainmodule.object_type
884 fun object_type: MClassType do return self.compiler.mainmodule.object_type
886 # Alias for self.compiler.mainmodule.bool_type
887 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
889 # Force to get the primitive class named `name' or abort
890 fun get_class
(name
: String): MClass
892 return self.compiler
.mainmodule
.get_primitive_class
(name
)
895 # Force to get the primitive property named `name' in the instance `recv' or abort
896 fun get_property
(name
: String, recv
: MType): MMethod
898 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, recv
, self.compiler
.mainmodule
)
902 var frame
: nullable Frame writable
904 # Anchor a type to the main module and the current receiver
905 fun anchor
(mtype
: MType): MType
907 if not mtype
.need_anchor
then return mtype
908 #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
909 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
912 # Add a line in the main part of the generated C
919 # (used for local or global declaration)
920 fun add_decl
(s
: String)
922 self.decl_lines
.add
(s
)
925 private var lines
: List[String] = new List[String]
926 private var decl_lines
: List[String] = new List[String]
928 # The current visited AST node
929 var current_node
: nullable ANode = null
931 # Compile an expression an return its result
932 # `mtype` is the expected return type, pass null if no specific type is expected.
933 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
935 var old
= self.current_node
936 self.current_node
= nexpr
937 var res
= nexpr
.expr
(self).as(not null)
938 if mtype
!= null then
939 mtype
= self.anchor
(mtype
)
940 res
= self.autobox
(res
, mtype
)
942 self.current_node
= old
946 # Unsafely cast a value to a new type
947 # ie the result share the same C variable but my have a different mcasttype
948 # NOTE: if the adaptation is useless then `value' is returned as it.
949 # ENSURE: return.name == value.name
950 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
952 mtype
= self.anchor
(mtype
)
953 var valmtype
= value
.mcasttype
954 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
958 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
959 var res
= new RuntimeVariable(value
.name
, valmtype
, valmtype
.mtype
)
962 var res
= new RuntimeVariable(value
.name
, valmtype
, mtype
)
967 # Box or unbox a value to another type iff a C type conversion is needed
968 # ENSURE: result.mtype.ctype == mtype.ctype
969 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
971 if value
.mtype
== mtype
then
973 else if value
.mtype
.ctype
== "val*" and mtype
.ctype
== "val*" then
975 else if value
.mtype
.ctype
== "val*" then
976 return self.new_expr
("((struct {mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype
)
977 else if mtype
.ctype
== "val*" then
978 var valtype
= value
.mtype
.as(MClassType)
979 var res
= self.new_var
(mtype
)
980 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
981 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
982 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
985 self.add
("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
988 # Bad things will appen!
989 var res
= self.new_var
(mtype
)
990 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
991 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
996 # Correctly assign a left and a right value
997 # Boxing and unboxing is performed if required
998 fun assign
(left
, right
: RuntimeVariable)
1000 right
= self.autobox
(right
, left
.mtype
)
1001 self.add
("{left} = {right};")
1005 # Alias for `self.expr(nexpr, self.bool_type)'
1006 fun expr_bool
(nexpr
: AExpr): RuntimeVariable
1008 return expr
(nexpr
, bool_type
)
1011 # Compile a statement (if any)
1012 fun stmt
(nexpr
: nullable AExpr)
1014 if nexpr
== null then return
1015 var old
= self.current_node
1016 self.current_node
= nexpr
1018 self.current_node
= old
1021 # Safely show a debug message on the current node and repeat the message in the C code as a comment
1022 fun debug
(message
: String)
1024 var node
= self.current_node
1025 if node
== null then
1026 print
"?: {message}"
1030 self.add
("/* DEBUG: {message} */")
1033 # Return a new uninitialized local runtime_variable
1034 fun new_var
(mtype
: MType): RuntimeVariable
1036 mtype
= self.anchor
(mtype
)
1037 var name
= self.get_name
("var")
1038 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1039 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1043 # Return a new uninitialized named runtime_variable
1044 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
1046 mtype
= self.anchor
(mtype
)
1047 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1048 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
1052 # Return a new local runtime_variable initialized with the C expression `cexpr'.
1053 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
1055 var res
= new_var
(mtype
)
1056 self.add
("{res} = {cexpr};")
1060 # Return the local runtime_variable associated to a Nit local variable
1061 fun variable
(variable
: Variable): RuntimeVariable
1063 if self.variables
.has_key
(variable
) then
1064 return self.variables
[variable
]
1066 var name
= self.get_name
("var_{variable.name}")
1067 var mtype
= variable
.declared_type
.as(not null)
1068 mtype
= self.anchor
(mtype
)
1069 var res
= new RuntimeVariable(name
, mtype
, mtype
)
1070 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
1071 self.variables
[variable
] = res
1076 private var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
1078 # Return an unique and stable identifier associated with an escapemark
1079 fun escapemark_name
(e
: nullable EscapeMark): String
1082 if escapemark_names
.has_key
(e
) then return escapemark_names
[e
]
1084 if name
== null then name
= "label"
1085 name
= get_name
(name
)
1086 escapemark_names
[e
] = name
1090 private var escapemark_names
= new HashMap[EscapeMark, String]
1092 # Return a new name based on `s' and unique in the visitor
1093 fun get_name
(s
: String): String
1095 if not self.names
.has
(s
) then
1099 var i
= self.last
+ 1
1102 if not self.names
.has
(s2
) then
1111 private var last
: Int = 0
1113 private var names
: HashSet[String] = new HashSet[String]
1115 # Generate a return with the value `s'
1116 fun ret
(s
: RuntimeVariable)
1118 self.assign
(self.frame
.returnvar
.as(not null), s
)
1119 self.add
("goto {self.frame.returnlabel.as(not null)};")
1122 # The runtime types that are acceptable for a given receiver.
1123 fun collect_types
(recv
: RuntimeVariable): Array[MClassType]
1125 var mtype
= recv
.mcasttype
1126 if recv
.is_exact
then
1127 assert mtype
isa MClassType
1128 assert self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
)
1132 var cache
= self.compiler
.collect_types_cache
1133 if cache
.has_key
(mtype
) then
1136 var types
= new Array[MClassType]
1137 var mainmodule
= self.compiler
.mainmodule
1138 for t
in self.compiler
.runtime_type_analysis
.live_types
do
1139 if not t
.is_subtype
(mainmodule
, null, mtype
) then continue
1142 cache
[mtype
] = types
1146 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
1148 if not mtype
.need_anchor
then return mtype
1149 #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
1150 var res
= mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
1154 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable])
1156 var elttype
= arguments
.first
.mtype
1157 var recv
= "((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->values"
1158 if pname
== "[]" then
1159 self.ret
(self.new_expr
("{recv}[{arguments[1]}]", ret_type
.as(not null)))
1161 else if pname
== "[]=" then
1162 self.add
("{recv}[{arguments[1]}]={arguments[2]};")
1164 else if pname
== "copy_to" then
1165 var recv1
= "((struct {arguments[1].mcasttype.c_name}*){arguments[1]})->values"
1166 self.add
("memcpy({recv1},{recv},{arguments[2]}*sizeof({elttype.ctype}));")
1171 fun calloc_array
(ret_type
: MType, arguments
: Array[RuntimeVariable])
1173 self.ret
(self.new_expr
("NEW_{ret_type.c_name}({arguments[1]})", ret_type
))
1176 # Add a check and an abort for a null reciever is needed
1177 fun check_recv_notnull
(recv
: RuntimeVariable)
1179 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then return
1181 var maybenull
= recv
.mcasttype
isa MNullableType or recv
.mcasttype
isa MNullType
1183 self.add
("if ({recv} == NULL) \{")
1184 self.add_abort
("Reciever is null")
1189 # Generate a polymorphic send for the method `m' and the arguments `args'
1190 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1192 var types
= self.collect_types
(args
.first
)
1194 var res
: nullable RuntimeVariable
1195 var ret
= m
.intro
.msignature
.return_mtype
1197 ret
= args
.first
.mtype
1198 res
= self.new_var
(ret
)
1199 else if ret
== null then
1202 ret
= self.resolve_for
(ret
, args
.first
)
1203 res
= self.new_var
(ret
)
1206 self.add
("/* send {m} on {args.first.inspect} */")
1207 if args
.first
.mtype
.ctype
!= "val*" then
1208 var mclasstype
= args
.first
.mtype
.as(MClassType)
1209 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mclasstype
) then
1210 self.add
("/* skip, no method {m} */")
1213 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, mclasstype
)
1214 if propdefs
.length
== 0 then
1215 self.add
("/* skip, no method {m} */")
1218 assert propdefs
.length
== 1
1219 var propdef
= propdefs
.first
1220 var res2
= self.call
(propdef
, mclasstype
, args
)
1221 if res
!= null then self.assign
(res
, res2
.as(not null))
1224 var consider_null
= not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
or m
.name
== "==" or m
.name
== "!="
1225 if args
.first
.mcasttype
isa MNullableType or args
.first
.mcasttype
isa MNullType and consider_null
then
1226 # The reciever is potentially null, so we have to 3 cases: ==, != or NullPointerException
1227 self.add
("if ({args.first} == NULL) \{ /* Special null case */")
1228 if m
.name
== "==" then
1230 if args
[1].mcasttype
isa MNullableType then
1231 self.add
("{res} = ({args[1]} == NULL);")
1232 else if args
[1].mcasttype
isa MNullType then
1233 self.add
("{res} = 1; /* is null */")
1235 self.add
("{res} = 0; /* {args[1].inspect} cannot be null */")
1237 else if m
.name
== "!=" then
1239 if args
[1].mcasttype
isa MNullableType then
1240 self.add
("{res} = ({args[1]} != NULL);")
1241 else if args
[1].mcasttype
isa MNullType then
1242 self.add
("{res} = 0; /* is null */")
1244 self.add
("{res} = 1; /* {args[1].inspect} cannot be null */")
1247 self.add_abort
("Reciever is null")
1251 if types
.is_empty
then
1253 self.add
("/*BUG: no live types for {args.first.inspect} . {m}*/")
1254 self.bugtype
(args
.first
)
1259 self.add
("switch({args.first}->classid) \{")
1260 var last
= types
.last
1261 var defaultpropdef
: nullable MMethodDef = null
1263 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
1264 if propdefs
.length
== 0 then
1265 self.add
("/* skip {t}, no method {m} */")
1268 if propdefs
.length
> 1 then
1269 self.debug
("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
1271 var propdef
= propdefs
.first
1272 if propdef
.mclassdef
.mclass
.name
== "Object" and t
.ctype
== "val*" then
1273 defaultpropdef
= propdef
1276 if not self.compiler
.hardening
and t
== last
and defaultpropdef
== null then
1277 self.add
("default: /* test {t} */")
1279 self.add
("case {self.compiler.classid(t)}: /* test {t} */")
1281 var res2
= self.call
(propdef
, t
, args
)
1282 if res
!= null then self.assign
(res
, res2
.as(not null))
1285 if defaultpropdef
!= null then
1286 self.add
("default: /* default is Object */")
1287 var res2
= self.call
(defaultpropdef
, defaultpropdef
.mclassdef
.bound_mtype
, args
)
1288 if res
!= null then self.assign
(res
, res2
.as(not null))
1289 else if self.compiler
.hardening
then
1290 self.add
("default: /* bug */")
1291 self.bugtype
(args
.first
)
1297 # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
1298 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1300 assert t
isa MClassType
1301 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
1302 if propdefs
.length
== 0 then
1305 if propdefs
.length
> 1 then
1306 self.debug
("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
1308 var propdef
= propdefs
.first
1309 return self.call
(propdef
, t
, args
)
1312 fun check_valid_reciever
(recvtype
: MClassType)
1314 if self.compiler
.runtime_type_analysis
.live_types
.has
(recvtype
) or recvtype
.mclass
.name
== "Object" then return
1315 print
"{recvtype} is not a live type"
1319 # Generate a static call on a method definition
1320 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
1322 check_valid_reciever
(recvtype
)
1323 #debug("call {m} on {recvtype} on {args.first}:{args.first.mtype}")
1324 if m
.mclassdef
.mclass
.name
== "Object" and recvtype
.ctype
== "val*" then
1325 recvtype
= m
.mclassdef
.bound_mtype
1327 var recv
= self.autobox
(args
.first
, recvtype
)
1328 recv
= self.autoadapt
(recv
, recvtype
)
1331 self.varargize
(m
.msignature
.as(not null), args
)
1332 if args
.length
!= m
.msignature
.arity
+ 1 then # because of self
1333 add
("printf(\"NOT YET IMPLEMENTED: Invalid arity
for {m}. {args.length} arguments given
.\\n\
"); exit(1);")
1334 debug
("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
1339 var rm
= new CustomizedRuntimeFunction(m
, recvtype
)
1340 return rm
.call
(self, args
)
1343 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
1345 var recv
= args
.first
1346 for i
in [0..m
.msignature
.arity
[ do
1347 var t
= m
.msignature
.mparameters
[i
].mtype
1348 if i
== m
.msignature
.vararg_rank
then
1351 t
= self.resolve_for
(t
, recv
)
1352 args
[i
+1] = self.autobox
(args
[i
+1], t
)
1356 # Transform varargs, in raw arguments, into a single argument of type Array
1357 # Note: this method modify the given `args`
1358 # If there is no vararg, then `args` is not modified.
1359 fun varargize
(msignature
: MSignature, args
: Array[RuntimeVariable])
1361 var recv
= args
.first
1362 var vararg_rank
= msignature
.vararg_rank
1363 if vararg_rank
>= 0 then
1364 assert args
.length
>= msignature
.arity
+ 1 # because of self
1366 args
= new Array[RuntimeVariable]
1368 args
.add
(rawargs
.first
) # recv
1370 for i
in [0..vararg_rank
[ do
1371 args
.add
(rawargs
[i
+1])
1374 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-msignature
.arity
1375 var vararg
= new Array[RuntimeVariable]
1376 for i
in [vararg_rank
..vararg_lastrank
] do
1377 vararg
.add
(rawargs
[i
+1])
1379 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
1380 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
1381 elttype
= self.resolve_for
(elttype
, recv
)
1382 args
.add
(self.array_instance
(vararg
, elttype
))
1384 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
1385 args
.add
(rawargs
[i
+1])
1388 rawargs
.add_all
(args
)
1392 fun bugtype
(recv
: RuntimeVariable)
1394 if recv
.mtype
.ctype
!= "val*" then return
1395 self.add
("fprintf(stderr, \"BTD BUG: Dynamic type is %s
, static
type is %s\\n\
", class_names[{recv}->classid], \"{recv.mcasttype}\
");")
1396 self.add
("exit(1);")
1399 # Generate a polymorphic attribute is_set test
1400 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1402 check_recv_notnull
(recv
)
1404 var types
= self.collect_types
(recv
)
1406 var res
= self.new_var
(bool_type
)
1408 if types
.is_empty
then
1409 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1413 self.add
("/* isset {a} on {recv.inspect} */")
1414 self.add
("switch({recv}->classid) \{")
1415 var last
= types
.last
1417 if not self.compiler
.hardening
and t
== last
then
1418 self.add
("default: /*{self.compiler.classid(t)}*/")
1420 self.add
("case {self.compiler.classid(t)}:")
1422 var recv2
= self.autoadapt
(recv
, t
)
1423 var ta
= a
.intro
.static_mtype
.as(not null)
1424 ta
= self.resolve_for
(ta
, recv2
)
1425 var attr
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1426 if not ta
isa MNullableType then
1427 if ta
.ctype
== "val*" then
1428 self.add
("{res} = ({attr} != NULL);")
1430 self.add
("{res} = 1; /*NOTYET isset on primitive attributes*/")
1435 if self.compiler
.hardening
then
1436 self.add
("default: /* Bug */")
1444 # Generate a polymorphic attribute read
1445 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1447 check_recv_notnull
(recv
)
1449 var types
= self.collect_types
(recv
)
1451 var ret
= a
.intro
.static_mtype
.as(not null)
1452 ret
= self.resolve_for
(ret
, recv
)
1453 var res
= self.new_var
(ret
)
1455 if types
.is_empty
then
1456 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1460 self.add
("/* read {a} on {recv.inspect} */")
1461 self.add
("switch({recv}->classid) \{")
1462 var last
= types
.last
1464 if not self.compiler
.hardening
and t
== last
then
1465 self.add
("default: /*{self.compiler.classid(t)}*/")
1467 self.add
("case {self.compiler.classid(t)}:")
1469 var recv2
= self.autoadapt
(recv
, t
)
1470 var ta
= a
.intro
.static_mtype
.as(not null)
1471 ta
= self.resolve_for
(ta
, recv2
)
1472 var res2
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1473 if not ta
isa MNullableType and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then
1474 if ta
.ctype
== "val*" then
1475 self.add
("if ({res2} == NULL) \{")
1476 self.add_abort
("Uninitialized attribute {a.name}")
1479 self.add
("/*NOTYET isset on primitive attributes*/")
1482 self.assign
(res
, res2
)
1485 if self.compiler
.hardening
then
1486 self.add
("default: /* Bug */")
1494 # Generate a polymorphic attribute write
1495 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable)
1497 check_recv_notnull
(recv
)
1499 var types
= self.collect_types
(recv
)
1501 if types
.is_empty
then
1502 self.add
("/*BUG: no live types for {recv.inspect} . {a}*/")
1506 self.add
("/* write {a} on {recv.inspect} */")
1507 self.add
("switch({recv}->classid) \{")
1508 var last
= types
.last
1510 if not self.compiler
.hardening
and t
== last
then
1511 self.add
("default: /*{self.compiler.classid(t)}*/")
1513 self.add
("case {self.compiler.classid(t)}:")
1515 var recv2
= self.autoadapt
(recv
, t
)
1516 var ta
= a
.intro
.static_mtype
.as(not null)
1517 ta
= self.resolve_for
(ta
, recv2
)
1518 self.add
("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
1521 if self.compiler
.hardening
then
1522 self.add
("default: /* Bug*/")
1528 # Generate a alloc-instance + init-attributes
1529 fun init_instance
(mtype
: MClassType): RuntimeVariable
1531 mtype
= self.anchor
(mtype
).as(MClassType)
1532 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1533 debug
"problem: {mtype} was detected dead"
1535 var res
= self.new_expr
("NEW_{mtype.c_name}()", mtype
)
1540 # Generate a polymorphic subtype test
1541 fun type_test
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1543 mtype
= self.anchor
(mtype
)
1544 var mclasstype
= mtype
1545 if mtype
isa MNullableType then mclasstype
= mtype
.mtype
1546 assert mclasstype
isa MClassType
1547 if not self.compiler
.runtime_type_analysis
.live_cast_types
.has
(mclasstype
) then
1548 debug
"problem: {mtype} was detected cast-dead"
1552 var types
= self.collect_types
(value
)
1554 var res
= self.new_var
(bool_type
)
1556 self.add
("/* isa {mtype} on {value.inspect} */")
1557 if value
.mtype
.ctype
!= "val*" then
1558 if value
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1559 self.add
("{res} = 1;")
1561 self.add
("{res} = 0;")
1565 if value
.mcasttype
isa MNullableType or value
.mcasttype
isa MNullType then
1566 self.add
("if ({value} == NULL) \{")
1567 if mtype
isa MNullableType then
1568 self.add
("{res} = 1; /* isa {mtype} */")
1570 self.add
("{res} = 0; /* not isa {mtype} */")
1572 self.add
("\} else ")
1574 self.add
("switch({value}->classid) \{")
1576 if t
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1577 self.add
("case {self.compiler.classid(t)}: /* {t} */")
1580 self.add
("{res} = 1;")
1582 self.add
("default:")
1583 self.add
("{res} = 0;")
1589 # Generate the code required to dynamically check if 2 objects share the same runtime type
1590 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1592 var res
= self.new_var
(bool_type
)
1593 if value2
.mtype
.ctype
== "val*" then
1594 if value1
.mtype
.ctype
== "val*" then
1595 self.add
"{res} = {value1}->classid == {value2}->classid;"
1597 self.add
"{res} = {self.compiler.classid(value1.mtype.as(MClassType))} == {value2}->classid;"
1600 if value1
.mtype
.ctype
== "val*" then
1601 self.add
"{res} = {value1}->classid == {self.compiler.classid(value2.mtype.as(MClassType))};"
1602 else if value1
.mcasttype
== value2
.mcasttype
then
1603 self.add
"{res} = 1;"
1605 self.add
"{res} = 0;"
1611 # Return a "const char*" variable associated to the classname of the dynamic type of an object
1612 # NOTE: we do not return a RuntimeVariable "NativeString" as the class may not exist in the module/program
1613 fun class_name_string
(value
: RuntimeVariable): String
1615 var res
= self.get_name
("var_class_name")
1616 self.add_decl
("const char* {res};")
1617 if value
.mtype
.ctype
== "val*" then
1618 self.add
"{res} = class_names[{value}->classid];"
1620 self.add
"{res} = class_names[{self.compiler.classid(value.mtype.as(MClassType))}];"
1625 # Generate a Nit "is" for two runtime_variables
1626 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1628 var res
= self.new_var
(bool_type
)
1629 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1634 if value1
.mtype
.ctype
!= "val*" then
1635 if value2
.mtype
== value1
.mtype
then
1636 self.add
("{res} = {value1} == {value2};")
1637 else if value2
.mtype
.ctype
!= "val*" then
1638 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1640 var mtype1
= value1
.mtype
.as(MClassType)
1641 self.add
("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
1642 self.add
("if ({res}) \{")
1643 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1647 var s
= new Array[String]
1648 for t
in self.compiler
.live_primitive_types
do
1649 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value1
.mcasttype
) then continue
1650 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value2
.mcasttype
) then continue
1651 s
.add
"({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
1654 self.add
("{res} = {value1} == {value2};")
1656 self.add
("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")
1662 # Generate a check-init-instance
1663 fun check_init_instance
(recv
: RuntimeVariable, mtype
: MClassType)
1665 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
1667 mtype
= self.anchor
(mtype
).as(MClassType)
1668 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1669 debug
"problem: {mtype} was detected dead"
1672 self.add
("CHECK_NEW_{mtype.c_name}({recv});")
1675 # Generate an integer value
1676 fun int_instance
(value
: Int): RuntimeVariable
1678 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
1679 self.add
("{res} = {value};")
1683 # Generate an array value
1684 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable
1686 elttype
= self.anchor
(elttype
)
1687 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
1688 var res
= self.init_instance
(arraytype
)
1689 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1690 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
1692 self.add
("{nat} = NEW_{nat.mtype.c_name}({array.length});")
1693 for i
in [0..array
.length
[ do
1694 var r
= self.autobox
(array
[i
], elttype
)
1695 self.add
("((struct {nat.mtype.c_name}*) {nat})->values[{i}] = {r};")
1697 var length
= self.int_instance
(array
.length
)
1698 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
1699 self.check_init_instance
(res
, arraytype
)
1704 # Generate a string value
1705 fun string_instance
(string
: String): RuntimeVariable
1707 var mtype
= self.get_class
("String").mclass_type
1708 var name
= self.get_name
("varonce")
1709 self.add_decl
("static {mtype.ctype} {name};")
1710 var res
= self.new_var
(mtype
)
1711 self.add
("if ({name}) \{")
1712 self.add
("{res} = {name};")
1713 self.add
("\} else \{")
1714 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
1715 self.add
("{nat} = \"{string.escape_to_c}\
";")
1716 var res2
= self.init_instance
(mtype
)
1717 self.add
("{res} = {res2};")
1718 var length
= self.int_instance
(string
.length
)
1719 self.send
(self.get_property
("with_native", mtype
), [res
, nat
, length
])
1720 self.check_init_instance
(res
, mtype
)
1721 self.add
("{name} = {res};")
1726 # Generate generic abort
1727 # used by aborts, asserts, casts, etc.
1728 fun add_abort
(message
: String)
1730 if self.current_node
!= null and self.current_node
.location
.file
!= null then
1731 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});")
1733 self.add
("fprintf(stderr, \"Runtime error
: %s\\n\
", \"{message.escape_to_c}\
");")
1735 self.add
("exit(1);")
1739 # A frame correspond to a visited property in a GlobalCompilerVisitor
1741 # The associated visitor
1743 var visitor
: GlobalCompilerVisitor
1745 # The executed property.
1746 # A Method in case of a call, an attribute in case of a default initialization.
1747 var mpropdef
: MPropDef
1749 # The static type of the receiver
1750 var receiver
: MClassType
1752 # Arguments of the method (the first is the receiver)
1753 var arguments
: Array[RuntimeVariable]
1755 # The runtime_variable associated to the return (in a function)
1756 var returnvar
: nullable RuntimeVariable writable = null
1758 # The label at the end of the property
1759 var returnlabel
: nullable String writable = null
1762 redef class MPropDef
1763 private var c_name_cache
: nullable String
1765 # The mangled name associated to the property
1768 var res
= self.c_name_cache
1769 if res
!= null then return res
1770 res
= "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1771 self.c_name_cache
= res
1776 redef class MMethodDef
1777 # Can the body be inlined?
1778 fun can_inline
(v
: GlobalCompilerVisitor): Bool
1780 var modelbuilder
= v
.compiler
.modelbuilder
1781 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1782 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1783 return npropdef
.can_inline
1784 else if self.mproperty
.name
== "init" then
1785 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1792 # Inline the body in another visitor
1793 fun compile_inside_to_c
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1795 var modelbuilder
= v
.compiler
.modelbuilder
1796 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1797 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1798 var oldnode
= v
.current_node
1799 v
.current_node
= npropdef
1800 self.compile_parameter_check
(v
, arguments
)
1801 npropdef
.compile_to_c
(v
, self, arguments
)
1802 v
.current_node
= oldnode
1803 else if self.mproperty
.name
== "init" then
1804 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1805 var oldnode
= v
.current_node
1806 v
.current_node
= nclassdef
1807 self.compile_parameter_check
(v
, arguments
)
1808 nclassdef
.compile_to_c
(v
, self, arguments
)
1809 v
.current_node
= oldnode
1816 # Generate type checks in the C code to check covariant parameters
1817 fun compile_parameter_check
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable])
1819 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_covariance
.value
then return
1821 for i
in [0..msignature
.arity
[ do
1822 # skip test for vararg since the array is instantiated with the correct polymorphic type
1823 if msignature
.vararg_rank
== i
then continue
1825 # skip if the cast is not required
1826 var origmtype
= self.mproperty
.intro
.msignature
.mparameters
[i
].mtype
1827 if not origmtype
.need_anchor
then continue
1829 # get the parameter type
1830 var mtype
= self.msignature
.mparameters
[i
].mtype
1833 # note that v decides if and how to implements the cast
1834 v
.add
("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1835 var cond
= v
.type_test
( arguments
[i
+1], mtype
)
1836 v
.add
("if (!{cond}) \{")
1837 #var x = v.class_name_string(arguments[i+1])
1838 #var y = v.class_name_string(arguments.first)
1839 #v.add("fprintf(stderr, \"expected {mtype} (self is %s), got %s for {arguments[i+1].inspect}\\n\", {y}, {x});")
1840 v
.add_abort
("Cast failed")
1846 redef class APropdef
1847 fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1849 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
1850 debug
("Not yet implemented")
1853 fun can_inline
: Bool do return true
1856 redef class AConcreteMethPropdef
1857 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1859 for i
in [0..mpropdef
.msignature
.arity
[ do
1860 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
1861 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
1863 # Call the implicit super-init
1864 var auto_super_inits
= self.auto_super_inits
1865 if auto_super_inits
!= null then
1866 var selfarg
= [arguments
.first
]
1867 for auto_super_init
in auto_super_inits
do
1868 if auto_super_init
.intro
.msignature
.arity
== 0 then
1869 v
.send
(auto_super_init
, selfarg
)
1871 v
.send
(auto_super_init
, arguments
)
1875 v
.stmt
(self.n_block
)
1878 redef fun can_inline
1880 if self.auto_super_inits
!= null then return false
1881 var nblock
= self.n_block
1882 if nblock
== null then return true
1883 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
1884 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
1889 redef class AInternMethPropdef
1890 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1892 var pname
= mpropdef
.mproperty
.name
1893 var cname
= mpropdef
.mclassdef
.mclass
.name
1894 var ret
= mpropdef
.msignature
.return_mtype
1896 ret
= v
.resolve_for
(ret
, arguments
.first
)
1898 if pname
!= "==" and pname
!= "!=" then
1899 v
.adapt_signature
(mpropdef
, arguments
)
1901 if cname
== "Int" then
1902 if pname
== "output" then
1903 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
1905 else if pname
== "object_id" then
1906 v
.ret
(arguments
.first
)
1908 else if pname
== "+" then
1909 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1911 else if pname
== "-" then
1912 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1914 else if pname
== "unary -" then
1915 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1917 else if pname
== "succ" then
1918 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1920 else if pname
== "prec" then
1921 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
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
== "%" then
1930 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
1932 else if pname
== "lshift" then
1933 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
1935 else if pname
== "rshift" then
1936 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
1938 else if pname
== "==" then
1939 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1941 else if pname
== "!=" then
1942 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1943 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1945 else if pname
== "<" then
1946 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1948 else if pname
== ">" then
1949 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1951 else if pname
== "<=" then
1952 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1954 else if pname
== ">=" then
1955 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1957 else if pname
== "to_f" then
1958 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
1960 else if pname
== "ascii" then
1961 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1964 else if cname
== "Char" then
1965 if pname
== "output" then
1966 v
.add
("printf(\"%c\
", {arguments.first});")
1968 else if pname
== "object_id" then
1969 v
.ret
(arguments
.first
)
1971 else if pname
== "+" then
1972 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1974 else if pname
== "-" then
1975 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1977 else if pname
== "==" then
1978 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1980 else if pname
== "!=" then
1981 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1982 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1984 else if pname
== "succ" then
1985 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1987 else if pname
== "prec" then
1988 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1990 else if pname
== "<" then
1991 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1993 else if pname
== ">" then
1994 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1996 else if pname
== "<=" then
1997 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1999 else if pname
== ">=" then
2000 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2002 else if pname
== "to_i" then
2003 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
2005 else if pname
== "ascii" then
2006 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
2009 else if cname
== "Bool" then
2010 if pname
== "output" then
2011 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
2013 else if pname
== "object_id" then
2014 v
.ret
(arguments
.first
)
2016 else if pname
== "==" then
2017 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2019 else if pname
== "!=" then
2020 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2021 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2024 else if cname
== "Float" then
2025 if pname
== "output" then
2026 v
.add
("printf(\"%f\\n\
", {arguments.first});")
2028 else if pname
== "object_id" then
2029 v
.ret
(v
.new_expr
("(double){arguments.first}", ret
.as(not null)))
2031 else if pname
== "+" then
2032 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
2034 else if pname
== "-" then
2035 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
2037 else if pname
== "unary -" then
2038 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
2040 else if pname
== "succ" then
2041 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
2043 else if pname
== "prec" then
2044 v
.ret
(v
.new_expr
("{arguments[0]}-1", 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
== "==" then
2053 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2055 else if pname
== "!=" then
2056 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2057 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2059 else if pname
== "<" then
2060 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
2062 else if pname
== ">" then
2063 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
2065 else if pname
== "<=" then
2066 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
2068 else if pname
== ">=" then
2069 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
2071 else if pname
== "to_i" then
2072 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
2075 else if cname
== "Char" then
2076 if pname
== "output" then
2077 v
.add
("printf(\"%c\
", {arguments.first});")
2079 else if pname
== "object_id" then
2080 v
.ret
(arguments
.first
)
2082 else if pname
== "==" then
2083 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
2085 else if pname
== "!=" then
2086 var res
= v
.equal_test
(arguments
[0], arguments
[1])
2087 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
2089 else if pname
== "ascii" then
2090 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
2093 else if cname
== "NativeString" then
2094 if pname
== "[]" then
2095 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
2097 else if pname
== "[]=" then
2098 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
2100 else if pname
== "copy_to" then
2101 v
.add
("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
2103 else if pname
== "atoi" then
2104 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
2107 else if cname
== "NativeArray" then
2108 v
.native_array_def
(pname
, ret
, arguments
)
2111 if pname
== "exit" then
2112 v
.add
("exit({arguments[1]});")
2114 else if pname
== "sys" then
2115 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
2117 else if pname
== "calloc_string" then
2118 v
.ret
(v
.new_expr
("(char*)GC_MALLOC({arguments[1]})", ret
.as(not null)))
2120 else if pname
== "calloc_array" then
2121 v
.calloc_array
(ret
.as(not null), arguments
)
2123 else if pname
== "object_id" then
2124 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
2126 else if pname
== "is_same_type" then
2127 v
.ret
(v
.is_same_type_test
(arguments
[0], arguments
[1]))
2129 else if pname
== "output_class_name" then
2130 var nat
= v
.class_name_string
(arguments
.first
)
2131 v
.add
("printf(\"%s\\n\
", {nat});")
2133 else if pname
== "native_class_name" then
2134 var nat
= v
.class_name_string
(arguments
.first
)
2135 v
.ret
(v
.new_expr
("(char*){nat}", ret
.as(not null)))
2137 else if pname
== "force_garbage_collection" then
2138 v
.add
("GC_gcollect();")
2141 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at
{location.to_s}\\n\
");")
2142 debug
("Not implemented {mpropdef}")
2146 redef class AExternMethPropdef
2147 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2150 var nextern
= self.n_extern
2151 if nextern
== null then
2152 v
.add
("fprintf(stderr, \"NOT YET IMPLEMENTED nitni
for {mpropdef} at
{location.to_s}\\n\
");")
2156 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
2157 if location
.file
!= null then
2158 var file
= location
.file
.filename
2159 v
.compiler
.add_extern
(file
)
2161 var res
: nullable RuntimeVariable = null
2162 var ret
= mpropdef
.msignature
.return_mtype
2164 ret
= v
.resolve_for
(ret
, arguments
.first
)
2165 res
= v
.new_var
(ret
)
2167 v
.adapt_signature
(mpropdef
, arguments
)
2170 v
.add
("{externname}({arguments.join(", ")});")
2172 v
.add
("{res} = {externname}({arguments.join(", ")});")
2178 redef class AExternInitPropdef
2179 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2182 var nextern
= self.n_extern
2183 if nextern
== null then
2184 debug
("{mpropdef} need extern name")
2187 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
2188 if location
.file
!= null then
2189 var file
= location
.file
.filename
2190 v
.compiler
.add_extern
(file
)
2192 v
.adapt_signature
(mpropdef
, arguments
)
2193 var ret
= arguments
.first
.mtype
2194 var res
= v
.new_var
(ret
)
2198 v
.add
("{res} = {externname}({arguments.join(", ")});")
2203 redef class AAttrPropdef
2204 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2206 if arguments
.length
== 1 then
2207 var res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
2208 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
2210 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arguments
[1])
2214 fun init_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
2216 var nexpr
= self.n_expr
2217 if nexpr
!= null then
2218 var oldnode
= v
.current_node
2219 v
.current_node
= self
2220 var old_frame
= v
.frame
2221 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
2223 var value
= v
.expr
(nexpr
, self.mpropdef
.static_mtype
)
2224 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
2226 v
.current_node
= oldnode
2230 fun check_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
2232 var nexpr
= self.n_expr
2233 if nexpr
!= null then return
2235 var oldnode
= v
.current_node
2236 v
.current_node
= self
2237 var old_frame
= v
.frame
2238 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
2240 # Force read to check the initialization
2241 v
.read_attribute
(self.mpropdef
.mproperty
, recv
)
2243 v
.current_node
= oldnode
2247 redef class AClassdef
2248 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
2250 if mpropdef
== self.mfree_init
then
2251 var super_inits
= self.super_inits
2252 if super_inits
!= null then
2253 assert arguments
.length
== 1
2254 for su
in super_inits
do
2255 v
.send
(su
, arguments
)
2259 var recv
= arguments
.first
2261 # Collect undefined attributes
2262 for npropdef
in self.n_propdefs
do
2263 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
2264 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, arguments
[i
])
2274 redef class ADeferredMethPropdef
2275 redef fun compile_to_c
(v
, mpropdef
, arguments
)
2277 v
.add_abort
("Deferred method called")
2280 redef fun can_inline
do return true
2284 # Try to compile self as an expression
2285 # Do not call this method directly, use `v.expr' instead
2286 private fun expr
(v
: GlobalCompilerVisitor): nullable RuntimeVariable
2288 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
2289 var mtype
= self.mtype
2290 if mtype
== null then
2293 var res
= v
.new_var
(mtype
)
2294 v
.add
("/* {res} = NOT YET {class_name} */")
2299 # Try to compile self as a statement
2300 # Do not call this method directly, use `v.stmt' instead
2301 private fun stmt
(v
: GlobalCompilerVisitor)
2304 if res
!= null then v
.add
("{res};")
2309 redef class ABlockExpr
2312 for e
in self.n_expr
do
2318 redef class AVardeclExpr
2321 var variable
= self.variable
.as(not null)
2322 var ne
= self.n_expr
2324 var i
= v
.expr
(ne
, variable
.declared_type
)
2325 v
.assign
(v
.variable
(variable
), i
)
2330 redef class AVarExpr
2333 var res
= v
.variable
(self.variable
.as(not null))
2334 var mtype
= self.mtype
.as(not null)
2335 return v
.autoadapt
(res
, mtype
)
2339 redef class AVarAssignExpr
2342 var variable
= self.variable
.as(not null)
2343 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
2344 v
.assign
(v
.variable
(variable
), i
)
2348 redef class AVarReassignExpr
2351 var variable
= self.variable
.as(not null)
2352 var vari
= v
.variable
(variable
)
2353 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
2354 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
2356 v
.assign
(v
.variable
(variable
), res
)
2360 redef class ASelfExpr
2363 return v
.frame
.arguments
.first
2367 redef class AContinueExpr
2370 v
.add
("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
2374 redef class ABreakExpr
2377 v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
2381 redef class AReturnExpr
2384 var nexpr
= self.n_expr
2385 if nexpr
!= null then
2386 var returnvar
= v
.frame
.returnvar
.as(not null)
2387 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
2388 v
.assign
(returnvar
, i
)
2390 v
.add
("goto {v.frame.returnlabel.as(not null)};")
2394 redef class AAbortExpr
2397 v
.add_abort
("Aborted")
2404 var cond
= v
.expr_bool
(self.n_expr
)
2405 v
.add
("if ({cond})\{")
2413 redef class AIfexprExpr
2416 var res
= v
.new_var
(self.mtype
.as(not null))
2417 var cond
= v
.expr_bool
(self.n_expr
)
2418 v
.add
("if ({cond})\{")
2419 v
.assign
(res
, v
.expr
(self.n_then
, null))
2421 v
.assign
(res
, v
.expr
(self.n_else
, null))
2430 v
.stmt
(self.n_block
)
2431 var escapemark
= self.escapemark
2432 if escapemark
!= null then
2433 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2438 redef class AWhileExpr
2442 var cond
= v
.expr_bool
(self.n_expr
)
2443 v
.add
("if (!{cond}) break;")
2444 v
.stmt
(self.n_block
)
2445 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2447 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2451 redef class ALoopExpr
2455 v
.stmt
(self.n_block
)
2456 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2458 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2462 redef class AForExpr
2465 var cl
= v
.expr
(self.n_expr
, null)
2466 var it_meth
= self.method_iterator
2467 assert it_meth
!= null
2468 var it
= v
.send
(it_meth
, [cl
])
2471 var isok_meth
= self.method_is_ok
2472 assert isok_meth
!= null
2473 var ok
= v
.send
(isok_meth
, [it
])
2475 v
.add
("if(!{ok}) break;")
2476 if self.variables
.length
== 1 then
2477 var item_meth
= self.method_item
2478 assert item_meth
!= null
2479 var i
= v
.send
(item_meth
, [it
])
2481 v
.assign
(v
.variable
(variables
.first
), i
)
2482 else if self.variables
.length
== 2 then
2483 var key_meth
= self.method_key
2484 assert key_meth
!= null
2485 var i
= v
.send
(key_meth
, [it
])
2487 v
.assign
(v
.variable
(variables
[0]), i
)
2488 var item_meth
= self.method_item
2489 assert item_meth
!= null
2490 i
= v
.send
(item_meth
, [it
])
2492 v
.assign
(v
.variable
(variables
[1]), i
)
2496 v
.stmt
(self.n_block
)
2497 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2498 var next_meth
= self.method_next
2499 assert next_meth
!= null
2500 v
.send
(next_meth
, [it
])
2502 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2506 redef class AAssertExpr
2509 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
2511 var cond
= v
.expr_bool
(self.n_expr
)
2512 v
.add
("if (!{cond}) \{")
2516 v
.add_abort
("Assert '{nid.text}' failed")
2518 v
.add_abort
("Assert failed")
2527 var res
= v
.new_var
(self.mtype
.as(not null))
2528 var i1
= v
.expr_bool
(self.n_expr
)
2529 v
.add
("if ({i1}) \{")
2532 var i2
= v
.expr_bool
(self.n_expr2
)
2533 v
.add
("{res} = {i2};")
2539 redef class AAndExpr
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 ANotExpr
2557 var cond
= v
.expr_bool
(self.n_expr
)
2558 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2562 redef class AOrElseExpr
2565 var res
= v
.new_var
(self.mtype
.as(not null))
2566 var i1
= v
.expr
(self.n_expr
, null)
2567 v
.add
("if ({i1}!=NULL) \{")
2570 var i2
= v
.expr
(self.n_expr2
, null)
2580 var value1
= v
.expr
(self.n_expr
, null)
2581 var value2
= v
.expr
(self.n_expr2
, null)
2582 return v
.equal_test
(value1
, value2
)
2586 redef class AIntExpr
2589 return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2593 redef class AFloatExpr
2596 return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2600 redef class ACharExpr
2603 return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2607 redef class AArrayExpr
2610 var mtype
= self.mtype
.as(MClassType).arguments
.first
2611 var array
= new Array[RuntimeVariable]
2612 for nexpr
in self.n_exprs
.n_exprs
do
2613 var i
= v
.expr
(nexpr
, mtype
)
2616 return v
.array_instance
(array
, mtype
)
2620 redef class AStringFormExpr
2623 return v
.string_instance
(self.value
.as(not null))
2627 redef class ASuperstringExpr
2630 var array
= new Array[RuntimeVariable]
2631 for ne
in self.n_exprs
do
2632 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
2633 var i
= v
.expr
(ne
, null)
2636 var a
= v
.array_instance
(array
, v
.object_type
)
2637 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2642 redef class ACrangeExpr
2645 var i1
= v
.expr
(self.n_expr
, null)
2646 var i2
= v
.expr
(self.n_expr2
, null)
2647 var mtype
= self.mtype
.as(MClassType)
2648 var res
= v
.init_instance
(mtype
)
2649 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2650 v
.check_init_instance
(res
, mtype
)
2655 redef class AOrangeExpr
2658 var i1
= v
.expr
(self.n_expr
, null)
2659 var i2
= v
.expr
(self.n_expr2
, null)
2660 var mtype
= self.mtype
.as(MClassType)
2661 var res
= v
.init_instance
(mtype
)
2662 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2663 v
.check_init_instance
(res
, mtype
)
2668 redef class ATrueExpr
2671 return v
.new_expr
("1", self.mtype
.as(not null))
2675 redef class AFalseExpr
2678 return v
.new_expr
("0", self.mtype
.as(not null))
2682 redef class ANullExpr
2685 var res
= v
.new_expr
("NULL", self.mtype
.as(not null))
2690 redef class AIsaExpr
2693 var i
= v
.expr
(self.n_expr
, null)
2694 return v
.type_test
(i
, self.cast_type
.as(not null))
2698 redef class AAsCastExpr
2701 var i
= v
.expr
(self.n_expr
, null)
2702 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2704 var cond
= v
.type_test
(i
, self.mtype
.as(not null))
2705 v
.add
("if (!{cond}) \{")
2706 v
.add_abort
("Cast failed")
2712 redef class AAsNotnullExpr
2715 var i
= v
.expr
(self.n_expr
, null)
2716 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2718 v
.add
("if ({i} == NULL) \{")
2719 v
.add_abort
("Cast failed")
2725 redef class AParExpr
2728 return v
.expr
(self.n_expr
, null)
2732 redef class AOnceExpr
2735 var mtype
= self.mtype
.as(not null)
2736 var name
= v
.get_name
("varonce")
2737 var guard
= v
.get_name
(name
+ "_guard")
2738 v
.add_decl
("static {mtype.ctype} {name};")
2739 v
.add_decl
("static int {guard};")
2740 var res
= v
.new_var
(mtype
)
2741 v
.add
("if ({guard}) \{")
2742 v
.add
("{res} = {name};")
2744 var i
= v
.expr
(self.n_expr
, mtype
)
2745 v
.add
("{res} = {i};")
2746 v
.add
("{name} = {res};")
2747 v
.add
("{guard} = 1;")
2753 redef class ASendExpr
2756 var recv
= v
.expr
(self.n_expr
, null)
2758 for a
in self.raw_arguments
.as(not null) do
2759 args
.add
(v
.expr
(a
, null))
2761 var mproperty
= self.mproperty
.as(not null)
2762 return v
.send
(mproperty
, args
)
2766 redef class ASendReassignFormExpr
2769 var recv
= v
.expr
(self.n_expr
, null)
2771 for a
in self.raw_arguments
.as(not null) do
2772 args
.add
(v
.expr
(a
, null))
2774 var value
= v
.expr
(self.n_value
, null)
2776 var mproperty
= self.mproperty
.as(not null)
2777 var left
= v
.send
(mproperty
, args
)
2780 var res
= v
.send
(reassign_property
.mproperty
, [left
, value
])
2784 v
.send
(self.write_mproperty
.as(not null), args
)
2788 redef class ASuperExpr
2791 var recv
= v
.frame
.arguments
.first
2793 for a
in self.n_args
.n_exprs
do
2794 args
.add
(v
.expr
(a
, null))
2796 if args
.length
== 1 then
2797 args
= v
.frame
.arguments
2800 var mproperty
= self.mproperty
2801 if mproperty
!= null then
2802 if mproperty
.intro
.msignature
.arity
== 0 then
2806 var res
= v
.send
(mproperty
, args
)
2810 # stantard call-next-method
2811 var mpropdef
= v
.frame
.mpropdef
2812 # FIXME: we do not want an ugly static call!
2813 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
2814 if mpropdefs
.length
!= 1 then
2815 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at
{location.to_s}\\n\
");")
2816 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
2818 mpropdef
= mpropdefs
.first
2819 assert mpropdef
isa MMethodDef
2820 var res
= v
.call
(mpropdef
, recv
.mtype
.as(MClassType), args
)
2825 redef class ANewExpr
2828 var mproperty
= self.mproperty
.as(not null)
2829 var mtype
= self.mtype
.as(MClassType)
2831 var ctype
= mtype
.ctype
2832 if ctype
== "val*" then
2833 recv
= v
.init_instance
(mtype
)
2834 else if ctype
== "void*" then
2835 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2837 debug
("cannot new {mtype}")
2841 for a
in self.n_args
.n_exprs
do
2842 args
.add
(v
.expr
(a
, null))
2844 var res2
= v
.send
(mproperty
, args
)
2845 if res2
!= null then
2846 #self.debug("got {res2} from {mproperty}. drop {recv}")
2849 v
.check_init_instance
(recv
, mtype
)
2854 redef class AAttrExpr
2857 var recv
= v
.expr
(self.n_expr
, null)
2858 var mproperty
= self.mproperty
.as(not null)
2859 return v
.read_attribute
(mproperty
, recv
)
2863 redef class AAttrAssignExpr
2866 var recv
= v
.expr
(self.n_expr
, null)
2867 var i
= v
.expr
(self.n_value
, null)
2868 var mproperty
= self.mproperty
.as(not null)
2869 v
.write_attribute
(mproperty
, recv
, i
)
2873 redef class AAttrReassignExpr
2876 var recv
= v
.expr
(self.n_expr
, null)
2877 var value
= v
.expr
(self.n_value
, null)
2878 var mproperty
= self.mproperty
.as(not null)
2879 var attr
= v
.read_attribute
(mproperty
, recv
)
2880 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
2882 v
.write_attribute
(mproperty
, recv
, res
)
2886 redef class AIssetAttrExpr
2889 var recv
= v
.expr
(self.n_expr
, null)
2890 var mproperty
= self.mproperty
.as(not null)
2891 return v
.isset_attribute
(mproperty
, recv
)
2895 redef class ADebugTypeExpr