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")
44 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_hardening
)
48 redef class ModelBuilder
49 fun run_global_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
52 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
54 var compiler
= new GlobalCompiler(mainmodule
, runtime_type_analysis
, self)
55 var v
= new GlobalCompilerVisitor(compiler
)
57 v
.add_decl
("#include <stdlib.h>")
58 v
.add_decl
("#include <stdio.h>")
59 v
.add_decl
("#include <string.h>")
61 # TODO: Better way to activate the GC
62 #v.add_decl("#include <gc/gc.h>")
63 v
.add_decl
("#define GC_MALLOC(x) calloc(1, (x))")
65 # Declare structure for each live type
67 v
.add_decl
("typedef struct \{int classid;\} val;")
68 for t
in runtime_type_analysis
.live_types
do
69 compiler
.declare_runtimeclass
(v
, t
)
72 v
.add_decl
("extern int glob_argc;")
73 v
.add_decl
("extern char **glob_argv;")
74 v
.add_decl
("extern val *glob_sys;")
76 # Class names (for the class_name and output_class_name methods)
78 v
.add_decl
("extern const char const * class_names[];")
79 v
.add
("const char const * class_names[] = \{")
80 for t
in runtime_type_analysis
.live_types
do
81 v
.add
("\"{t}\
", /* {compiler.classid(t)} */")
86 # Init instance code (allocate and init-arguments)
88 for t
in runtime_type_analysis
.live_types
do
89 if t
.ctype
!= "val*" then continue
90 compiler
.generate_init_instance
(t
)
93 # The main function of the C
95 v
= new GlobalCompilerVisitor(compiler
)
96 v
.add_decl
("int glob_argc;")
97 v
.add_decl
("char **glob_argv;")
98 v
.add_decl
("val *glob_sys;")
99 v
.add_decl
("int main(int argc, char** argv) \{")
100 v
.add
("glob_argc = argc; glob_argv = argv;")
101 var main_type
= mainmodule
.sys_type
102 if main_type
== null then return # Nothing to compile
103 var glob_sys
= v
.init_instance
(main_type
)
104 v
.add
("glob_sys = {glob_sys};")
105 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
106 if main_init
!= null then
107 v
.send
(main_init
, [glob_sys
])
109 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
110 if main_method
!= null then
111 v
.send
(main_method
, [glob_sys
])
115 # Compile until all runtime_functions are visited
117 while not compiler
.todos
.is_empty
do
118 var m
= compiler
.todos
.shift
119 self.toolcontext
.info
("Compile {m.mmethoddef} for {m.recv} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
120 m
.mmethoddef
.compile_to_c
(compiler
, self, m
.recv
)
122 self.toolcontext
.info
("Total methods to compile to C: {compiler.visitors.length}", 2)
124 # Generate the .h and .c files
126 var outname
= self.toolcontext
.opt_output
.value
127 if outname
== null then
128 outname
= "{mainmodule.name}.bin"
131 var hfilename
= ".nit_compile/{mainmodule.name}.1.h"
132 var h
= new OFStream.open
(hfilename
)
133 for l
in compiler
.header
.decl_lines
do
139 var cfiles
= new Array[String]
141 var file
: nullable OFStream = null
146 for vis
in compiler
.visitors
do
147 count
+= vis
.lines
.length
148 if file
== null or count
> 10000 then
150 if file
!= null then file
.close
151 var cfilename
= ".nit_compile/{mainmodule.name}.{i}.c"
152 cfiles
.add
(cfilename
)
153 file
= new OFStream.open
(cfilename
)
154 file
.write
"#include \"{mainmodule.name}.1.h\
"\n"
155 count
= vis
.lines
.length
157 if vis
!= compiler
.header
then
158 for l
in vis
.decl_lines
do
163 for l
in vis
.lines
do
168 if file
!= null then file
.close
170 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
172 # Generate the Makefile
174 var makename
= ".nit_compile/{mainmodule.name}.mk"
175 var makefile
= new OFStream.open
(makename
)
177 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
178 makefile
.write
("all: {outname}\n\n")
180 var ofiles
= new Array[String]
182 var o
= f
.strip_extension
(".c") + ".o"
183 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -I .nit_compile -I ../clib -c -o {o} {f}\n\n")
187 makefile
.write
("{outname}: {ofiles.join(" ")} {compiler.extern_bodies.join(" ")}\n\t$(CC) -Wl,--warn-unresolved-symbols $(CFLAGS) $(LDFLAGS) $(LDLIBS) -I .nit_compile -I ../clib -o {outname} {ofiles.join(" ")} {compiler.extern_bodies.join(" ")}\n\n")
189 self.toolcontext
.info
("Generated makefile: {makename}", 2)
192 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
194 # Execute the Makefile
196 if self.toolcontext
.opt_no_cc
.value
then return
199 self.toolcontext
.info
("*** COMPILING C ***", 1)
200 self.toolcontext
.info
("make -f {makename} -j 4", 2)
203 if self.toolcontext
.verbose_level
>= 3 then
204 res
= sys
.system
("make -f {makename} -j 4 2>&1")
206 res
= sys
.system
("make -f {makename} -j 4 2>&1 >/dev/null")
209 toolcontext
.error
(null, "make failed! Error code: {res}.")
213 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
217 # Singleton that store the knowledge about the compilation process
218 private class GlobalCompiler
219 # The main module of the program
220 var mainmodule
: MModule
222 # The result of the RTA (used to know live types and methods)
223 var runtime_type_analysis
: RapidTypeAnalysis
225 # The modeulbuilder used to know the model and the AST
226 var modelbuilder
: ModelBuilder
228 # Is hardening asked (see --hardening)
229 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
231 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, modelbuilder
: ModelBuilder)
233 self.mainmodule
= mainmodule
234 self.runtime_type_analysis
= runtime_type_analysis
235 self.modelbuilder
= modelbuilder
236 self.live_primitive_types
= new Array[MClassType]
237 for t
in runtime_type_analysis
.live_types
do
238 if t
.ctype
!= "val*" then
239 self.live_primitive_types
.add
(t
)
244 # Subset of runtime_type_analysis.live_types that contains only primitive types
245 var live_primitive_types
: Array[MClassType]
247 # runtime_functions that need to be compiled
248 private var todos
: List[RuntimeFunction] = new List[RuntimeFunction]
250 # runtime_functions already seen (todo or done)
251 private var seen
: HashSet[RuntimeFunction] = new HashSet[RuntimeFunction]
252 fun todo
(m
: RuntimeFunction)
254 if seen
.has
(m
) then return
259 # Where global declaration are stored (the main .h)
261 # FIXME: should not be a vistor but just somewhere to store lines
262 # FIXME: should not have a global .h since its does no helps recompilation
263 var header
: nullable GlobalCompilerVisitor = null
265 # The list of all associated visitors
266 # Used to generate .c files
267 private var visitors
: List[GlobalCompilerVisitor] = new List[GlobalCompilerVisitor]
269 # List of additional .c files required to compile (native interface)
270 var extern_bodies
= new ArraySet[String]
272 # Return the C symbol associated to a live type runtime
273 # REQUIRE: self.runtime_type_analysis.live_types.has(mtype)
274 fun classid
(mtype
: MClassType): String
276 if self.classids
.has_key
(mtype
) then
277 return self.classids
[mtype
]
279 print
"No classid for {mtype}"
283 # Cache for classid (computed by declare_runtimeclass)
284 private var classids
: HashMap[MClassType, String] = new HashMap[MClassType, String]
286 # Declare C structures and identifiers for a runtime class
287 fun declare_runtimeclass
(v
: GlobalCompilerVisitor, mtype
: MClassType)
289 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
290 v
.add_decl
("/* runtime class {mtype} */")
291 var idnum
= classids
.length
292 var idname
= "ID_" + mtype
.c_name
293 self.classids
[mtype
] = idname
294 v
.add_decl
("#define {idname} {idnum} /* {mtype} */")
295 v
.add_decl
("struct {mtype.c_name} \{")
296 v
.add_decl
("int classid; /* must be {idname} */")
297 if mtype
.ctype
!= "val*" then
298 v
.add_decl
("{mtype.ctype} value;")
300 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
301 for p
in cd
.intro_mproperties
do
302 if not p
isa MAttribute then continue
303 var t
= p
.intro
.static_mtype
.as(not null)
304 t
= t
.anchor_to
(self.mainmodule
, mtype
)
305 v
.add_decl
("{t.ctype} {p.intro.c_name}; /* {p}: {t} */")
311 # Generate the init-instance of a live type (allocate + init-instance)
312 fun generate_init_instance
(mtype
: MClassType)
314 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
315 assert mtype
.ctype
== "val*"
316 var v
= new GlobalCompilerVisitor(self)
318 self.header
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}(void);")
319 v
.add_decl
("/* allocate {mtype} */")
320 v
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}(void) \{")
321 var res
= v
.new_var
(mtype
)
323 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}));")
324 v
.add
("{res}->classid = {self.classid(mtype)};")
326 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
328 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
329 for npropdef
in n
.n_propdefs
do
330 if npropdef
isa AAttrPropdef then
331 npropdef
.init_expr
(v
, res
)
335 v
.add
("return {res};")
339 # look for a needed .h and .c file for a given .nit source-file
340 # FIXME: bad API, parameter should be a MModule, not its source-file
341 fun add_extern
(file
: String)
343 file
= file
.strip_extension
(".nit")
344 var tryfile
= file
+ ".nit.h"
345 if tryfile
.file_exists
then
346 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
348 tryfile
= file
+ "_nit.h"
349 if tryfile
.file_exists
then
350 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
352 tryfile
= file
+ ".nit.c"
353 if tryfile
.file_exists
then
354 self.extern_bodies
.add
(tryfile
)
356 tryfile
= file
+ "_nit.c"
357 if tryfile
.file_exists
then
358 self.extern_bodies
.add
(tryfile
)
360 #(new OFStream.open("{file.basename("")}._nitni.h")).close
365 # Mangle a string to be a unique valid C identifier
366 fun to_cmangle
: String
369 var underscore
= false
371 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
377 res
.append
('_'.ascii
.to_s
)
380 if c
>= '0' and c
<= '9' then
383 else if c
== '_' then
388 res
.append
(c
.ascii
.to_s
)
396 # Escape " \ ' and non printable characters for literal C strings or characters
397 fun escape_to_c
: String
403 else if c
== '\0' then
405 else if c
== '"' then
407 else if c == '\'' then
409 else if c == '\\
' then
411 else if c.ascii < 32 then
412 b.append("\\{c.ascii.to_base(8, false)}")
422 # Return the C type associated to a given Nit static type
428 # Return the name of the C structure associated to a Nit live type
429 # FIXME: move to GlobalCompiler so we can check that self is a live type
430 fun c_name: String is abstract
431 private var c_name_cache: nullable String
434 redef class MClassType
437 var res = self.c_name_cache
438 if res != null then return res
439 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
440 self.c_name_cache = res
444 redef fun ctype: String
446 if mclass.name == "Int" then
448 else if mclass.name == "Bool" then
450 else if mclass.name == "Char" then
452 else if mclass.name == "Float" then
454 else if mclass.name == "NativeString" then
456 else if mclass.name == "NativeArray" then
457 assert self isa MGenericType
458 return "{self.arguments.first.ctype}*"
459 else if mclass.kind == extern_kind then
467 redef class MGenericType
470 var res = self.c_name_cache
471 if res != null then return res
473 for t in self.arguments do
476 self.c_name_cache = res
481 redef class MNullableType
484 var res = self.c_name_cache
485 if res != null then return res
486 res = "nullable_{self.mtype.c_name}"
487 self.c_name_cache = res
492 # A C function associated to a Nit method
493 # Because of customization, a given Nit method can be compiler more that once
494 private class RuntimeFunction
495 # The associated Nit method
496 var mmethoddef: MMethodDef
498 # The considered reciever
499 # (usually is a live type but no strong guarantee)
502 # The mangled c name of the runtime_function
505 var res = self.c_name_cache
506 if res != null then return res
507 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
508 res = self.mmethoddef.c_name
510 res = "{mmethoddef.c_name}__{recv.c_name}"
512 self.c_name_cache = res
516 private var c_name_cache: nullable String = null
519 # used in the compiler worklist
521 if not o isa RuntimeFunction then return false
522 if self.mmethoddef != o.mmethoddef then return false
523 if self.recv != o.recv then return false
528 # used in the compiler work-list
530 var res = self.mmethoddef.hash + self.recv.hash
536 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
537 return self.mmethoddef.to_s
539 return "{self.mmethoddef}@{self.recv}"
543 # Implements a call of the runtime_function
544 # May inline the body
545 fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
547 var ret = self.mmethoddef.msignature.return_mtype
548 if self.mmethoddef.mproperty.is_new then
549 ret = arguments.first.mtype
552 ret = v.resolve_for(ret, arguments.first)
554 if self.mmethoddef.can_inline(v) then
555 var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
556 frame.returnlabel = v.get_name("RET_LABEL")
558 frame.returnvar = v.new_var(ret)
560 var old_frame = v.frame
562 v.add("\{ /* Inline {self} ({arguments.join(",")}) */")
563 self.mmethoddef.compile_inside_to_c(v, arguments)
564 v.add("{frame.returnlabel.as(not null)}:(void)0;")
567 return frame.returnvar
569 v.adapt_signature(self.mmethoddef, arguments)
570 v.compiler.todo(self)
572 v.add("{self.c_name}({arguments.join(",")});")
575 var res = v.new_var(ret)
576 v.add("{res} = {self.c_name}({arguments.join(",")});")
582 # A runtime variable hold a runtime value in C
583 # Runtime variables are associated to local variables and intermediate results
584 private class RuntimeVariable
585 # The name of the variable in the C code
588 # The static type of the variable (as declard in C)
591 # The current casted type of the variable (as known in Nit)
594 # If the variable exaclty a mcasttype?
595 # false (usual value) means that the variable is a mcasttype or a subtype.
596 var is_exact: Bool = false
598 redef fun to_s do return name
600 redef fun inspect do return "<{inspect_head} {name}:{mtype}({mcasttype})>"
603 # Visit the AST to generate the C code.
604 # Because of inlining, a visitor can visit more than one property.
605 private class GlobalCompilerVisitor
606 # The associated compiler
607 var compiler: GlobalCompiler
609 init(compiler: GlobalCompiler)
611 self.compiler = compiler
612 compiler.visitors.add(self)
615 # Alias for self.compiler.mainmodule.object_type
616 fun object_type: MClassType do return self.compiler.mainmodule.object_type
618 # Alias for self.compiler.mainmodule.bool_type
619 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
621 # Force to get the primitive class named `name' or abort
622 fun get_class
(name
: String): MClass
624 return self.compiler
.mainmodule
.get_primitive_class
(name
)
627 # Force to get the primitive property named `name' in the instance `recv' or abort
628 fun get_property
(name
: String, recv
: MType): MMethod
630 return self.compiler
.mainmodule
.force_get_primitive_method
(name
, recv
)
634 var frame
: nullable Frame
636 # Anchor a type to the main module and the current receiver
637 fun anchor
(mtype
: MType): MType
639 if not mtype
.need_anchor
then return mtype
640 #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
641 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
644 # Add a line in the main part of the generated C
651 # (used for local or global declaration)
652 fun add_decl
(s
: String)
654 self.decl_lines
.add
(s
)
657 private var lines
: List[String] = new List[String]
658 private var decl_lines
: List[String] = new List[String]
660 # The current visited AST node
661 var current_node
: nullable AExpr = null
663 # Compile an expression an return its
664 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
666 var old
= self.current_node
667 self.current_node
= nexpr
668 var res
= nexpr
.expr
(self).as(not null)
669 if mtype
!= null then
670 mtype
= self.anchor
(mtype
)
671 res
= self.autobox
(res
, mtype
)
673 self.current_node
= old
677 # Unsafely cast a value to a new type
678 # ENSURE: return.name == value.name
679 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
681 mtype
= self.anchor
(mtype
)
682 if value
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
686 var valmtype
= value
.mtype
687 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
688 var res
= new RuntimeVariable(value
.name
, value
.mtype
, valmtype
.mtype
)
691 var res
= new RuntimeVariable(value
.name
, value
.mtype
, mtype
)
696 # Box or unbox a value to another type iff a C type conversion is needed
697 # ENSURE: result.mtype.ctype == mtype.ctype
698 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
700 if value
.mtype
.ctype
== mtype
.ctype
then
702 else if value
.mtype
.ctype
== "val*" then
703 return self.new_expr
("((struct {mtype.c_name}*){value})->value /* autounbox from {value.mtype} to {mtype} */", mtype
)
704 else if mtype
.ctype
== "val*" then
705 var valtype
= value
.mtype
.as(MClassType)
706 var res
= self.new_var
(mtype
)
707 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
708 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
709 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
712 self.add
("{res} = GC_MALLOC(sizeof(struct {valtype.c_name})); /* autobox from {value.mtype} to {mtype} */")
713 self.add
("{res}->classid = {self.compiler.classid(valtype)};")
714 self.add
("((struct {valtype.c_name}*){res})->value = {value};")
717 # Bad things will append!
718 var res
= self.new_var
(mtype
)
719 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
720 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
725 # Correctly assign a left and a right value
726 # Boxing and unboxing is performed if required
727 fun assign
(left
, right
: RuntimeVariable)
729 right
= self.autobox
(right
, left
.mtype
)
730 self.add
("{left} = {right};")
734 # Alias for `self.expr(nexpr, self.bool_type)'
735 fun expr_bool
(nexpr
: AExpr): RuntimeVariable
737 return expr
(nexpr
, bool_type
)
741 fun stmt
(nexpr
: nullable AExpr)
743 if nexpr
== null then return
744 var old
= self.current_node
745 self.current_node
= nexpr
747 self.current_node
= old
750 # Safely show a debug message on the current node and repeat the message in the C code
751 fun debug
(message
: String)
753 var node
= self.current_node
759 self.add
("/* DEBUG: {message} */")
762 # Return a new uninitialized local runtime_variable
763 fun new_var
(mtype
: MType): RuntimeVariable
765 if mtype
isa MNullType then
766 mtype
= self.object_type
768 mtype
= self.anchor
(mtype
)
770 var name
= self.get_name
("var")
771 var res
= new RuntimeVariable(name
, mtype
, mtype
)
772 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
776 # Return a new local runtime_variable initialized with the C expression `cexpr'.
777 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
779 var res
= new_var
(mtype
)
780 self.add
("{res} = {cexpr};")
784 # Return the local runtime_variable associated to a Nit local variable
785 fun variable
(variable
: Variable): RuntimeVariable
787 if self.variables
.has_key
(variable
) then
788 return self.variables
[variable
]
790 var name
= self.get_name
("var_{variable.name}")
791 var mtype
= variable
.declared_type
.as(not null)
792 mtype
= self.anchor
(mtype
)
793 var res
= new RuntimeVariable(name
, mtype
, mtype
)
794 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
795 self.variables
[variable
] = res
800 private var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
802 # Return a new name based on `s' and unique in the visitor
803 fun get_name
(s
: String): String
805 if not self.names
.has
(s
) then
809 var i
= self.last
+ 1
812 if not self.names
.has
(s2
) then
821 private var last
: Int = 0
823 private var names
: HashSet[String] = new HashSet[String]
825 # Generate a return with the value `s'
826 fun ret
(s
: RuntimeVariable)
828 self.assign
(self.frame
.returnvar
.as(not null), s
)
829 self.add
("goto {self.frame.returnlabel.as(not null)};")
832 # The runtime types that are acceptable for a given receiver.
833 fun collect_types
(recv
: RuntimeVariable): Array[MClassType]
835 var mtype
= recv
.mcasttype
836 if recv
.is_exact
then
837 assert mtype
isa MClassType
838 assert self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
)
842 var cache
= self.collect_types_cache
843 if cache
.has_key
(mtype
) then
846 var types
= new Array[MClassType]
847 var mainmodule
= self.compiler
.mainmodule
848 for t
in self.compiler
.runtime_type_analysis
.live_types
do
849 if not t
.is_subtype
(mainmodule
, null, mtype
) then continue
856 private var collect_types_cache
: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
858 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
860 if not mtype
.need_anchor
then return mtype
861 #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
862 var res
= mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
866 # Generate a polymorphic send for the method `m' and the arguments `args'
867 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable
869 var types
= self.collect_types
(args
.first
)
871 var res
: nullable RuntimeVariable
872 var ret
= m
.intro
.msignature
.return_mtype
874 ret
= args
.first
.mtype
875 res
= self.new_var
(ret
)
876 else if ret
== null then
879 ret
= self.resolve_for
(ret
, args
.first
)
880 res
= self.new_var
(ret
)
883 if types
.is_empty
then
884 self.add
("/*BUG: no live types for {args.first.mtype} . {m}*/")
887 self.add
("/* send {m} on {args.first}: {args.first.mcasttype} (declared {args.first.mtype}) */")
888 if args
.first
.mtype
.ctype
!= "val*" then
889 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, args
.first
.mtype
)
890 if propdefs
.length
== 0 then
891 self.add
("/* skip {args.first.mcasttype}, no method {m} */")
894 assert propdefs
.length
== 1
895 var propdef
= propdefs
.first
896 var res2
= self.call
(propdef
, args
.first
.mtype
.as(MClassType), args
)
897 if res
!= null then self.assign
(res
, res2
.as(not null))
900 if args
.first
.mcasttype
isa MNullableType then
901 self.add
("if ({args.first} == NULL) \{ /* Special null case */")
902 if m
.name
== "==" then
904 if args
[1].mcasttype
.ctype
== "val*" then
905 self.add
("{res} = ({args[1]} == NULL);")
907 self.add
("{res} = 0; /* {args[1]}: {args[1].mcasttype} cannot be null */")
909 else if m
.name
== "!=" then
911 if args
[1].mcasttype
.ctype
== "val*" then
912 self.add
("{res} = ({args[1]} != NULL);")
914 self.add
("{res} = 1; /* {args[1]}: {args[1].mcasttype} cannot be null */")
917 self.add_abort
("Reciever is null")
921 self.add
("switch({args.first}->classid) \{")
922 var last
= types
.last
923 var defaultpropdef
: nullable MMethodDef = null
925 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
926 if propdefs
.length
== 0 then
927 self.add
("/* skip {t}, no method {m} */")
930 var propdef
= propdefs
.first
931 if propdef
.mclassdef
.mclass
.name
== "Object" and t
.ctype
== "val*" then
932 defaultpropdef
= propdef
935 if not self.compiler
.hardening
and t
== last
and defaultpropdef
== null then
936 self.add
("default: /* test {t} */")
938 self.add
("case {self.compiler.classid(t)}: /* test {t} */")
940 var res2
= self.call
(propdef
, t
, args
)
941 if res
!= null then self.assign
(res
, res2
.as(not null))
944 if defaultpropdef
!= null then
945 self.add
("default: /* default is Object */")
946 var res2
= self.call
(defaultpropdef
, defaultpropdef
.mclassdef
.bound_mtype
, args
)
947 if res
!= null then self.assign
(res
, res2
.as(not null))
948 else if self.compiler
.hardening
then
949 self.add
("default: /* bug */")
950 self.bugtype
(args
.first
)
956 fun check_valid_reciever
(recvtype
: MClassType)
958 if self.compiler
.runtime_type_analysis
.live_types
.has
(recvtype
) or recvtype
.mclass
.name
== "Object" then return
959 print
"{recvtype} is not a live type"
963 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
965 check_valid_reciever
(recvtype
)
966 #debug("call {m} on {recvtype} on {args.first}:{args.first.mtype}")
967 if m
.mclassdef
.mclass
.name
== "Object" and recvtype
.ctype
== "val*" then
968 recvtype
= m
.mclassdef
.bound_mtype
970 var recv
= self.autobox
(args
.first
, recvtype
)
971 recv
= self.autoadapt
(recv
, recvtype
)
973 var vararg_rank
= m
.msignature
.vararg_rank
974 if vararg_rank
>= 0 then
975 assert args
.length
>= m
.msignature
.arity
+ 1 # because of self
977 args
= new Array[RuntimeVariable]
979 args
.add
(rawargs
.first
) # recv
981 for i
in [0..vararg_rank
[ do
982 args
.add
(rawargs
[i
+1])
985 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-m
.msignature
.arity
986 var vararg
= new Array[RuntimeVariable]
987 for i
in [vararg_rank
..vararg_lastrank
] do
988 vararg
.add
(rawargs
[i
+1])
990 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
991 var elttype
= m
.msignature
.parameter_mtypes
[vararg_rank
]
992 elttype
= self.resolve_for
(elttype
, recv
)
993 args
.add
(self.array_instance
(vararg
, elttype
))
995 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
996 args
.add
(rawargs
[i
+1])
1001 assert args
.length
== m
.msignature
.arity
+ 1 # because of self
1004 var rm
= new RuntimeFunction(m
, recvtype
)
1005 return rm
.call
(self, args
)
1008 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
1010 var recv
= args
.first
1011 for i
in [0..m
.msignature
.arity
[ do
1012 var t
= m
.msignature
.parameter_mtypes
[i
]
1013 if i
== m
.msignature
.vararg_rank
then
1016 t
= self.resolve_for
(t
, recv
)
1017 args
[i
+1] = self.autobox
(args
[i
+1], t
)
1021 fun bugtype
(recv
: RuntimeVariable)
1023 if recv
.mtype
.ctype
!= "val*" then return
1024 self.add
("fprintf(stderr, \"BTD BUG: Dynamic type is %s
, static
type is %s\\n\
", class_names[{recv}->classid], \"{recv.mcasttype}\
");")
1027 # Generate a polymorphic attribute read
1028 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1030 var types
= self.collect_types
(recv
)
1032 var ret
= a
.intro
.static_mtype
.as(not null)
1033 ret
= self.resolve_for
(ret
, recv
)
1034 var res
= self.new_var
(ret
)
1036 if types
.is_empty
then
1037 self.add
("/*BUG: no live types for {recv.mtype} . {a}*/")
1040 self.add
("/* read {a} on {recv.mcasttype} */")
1041 self.add
("switch({recv}->classid) \{")
1042 var last
= types
.last
1044 if not self.compiler
.hardening
and t
== last
then
1045 self.add
("default: /*{self.compiler.classid(t)}*/")
1047 self.add
("case {self.compiler.classid(t)}:")
1049 var recv2
= self.autoadapt
(recv
, t
)
1050 var ta
= a
.intro
.static_mtype
.as(not null)
1051 ta
= self.resolve_for
(ta
, recv2
)
1052 var res2
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1053 if not ta
isa MNullableType then
1054 if ta
.ctype
== "val*" then
1055 self.add
("if ({res2} == NULL) \{")
1056 self.add_abort
("Uninitialized attribute {a.name}")
1059 self.add
("/*NOTYET isset on primitive attributes*/")
1062 self.assign
(res
, res2
)
1065 if self.compiler
.hardening
then
1066 self.add
("default: /* Bug */")
1074 # Generate a polymorphic attribute write
1075 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable)
1077 var types
= self.collect_types
(recv
)
1079 if types
.is_empty
then
1080 self.add
("/*BUG: no live types for {recv.mtype} . {a}*/")
1083 self.add
("/* write {a} on {recv.mcasttype} */")
1084 self.add
("switch({recv}->classid) \{")
1085 var last
= types
.last
1087 if not self.compiler
.hardening
and t
== last
then
1088 self.add
("default: /*{self.compiler.classid(t)}*/")
1090 self.add
("case {self.compiler.classid(t)}:")
1092 var recv2
= self.autoadapt
(recv
, t
)
1093 var ta
= a
.intro
.static_mtype
.as(not null)
1094 ta
= self.resolve_for
(ta
, recv2
)
1095 self.add
("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
1098 if self.compiler
.hardening
then
1099 self.add
("default: /* Bug*/")
1105 # Generate a alloc-instance + init-attributes
1106 fun init_instance
(mtype
: MClassType): RuntimeVariable
1108 mtype
= self.anchor
(mtype
).as(MClassType)
1109 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1110 debug
"problem: {mtype} was detected dead"
1112 var res
= self.new_expr
("NEW_{mtype.c_name}()", mtype
)
1117 # Generate a polymorphic subtype test
1118 fun type_test
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1120 mtype
= self.anchor
(mtype
)
1121 var types
= self.collect_types
(value
)
1123 var res
= self.new_var
(bool_type
)
1125 self.add
("/* isa {mtype} on {value.mcasttype} */")
1126 if value
.mcasttype
isa MNullableType then
1127 self.add
("if ({value} == NULL) \{")
1128 if mtype
isa MNullableType then
1129 self.add
("{res} = 1; /* isa {mtype} */")
1131 self.add
("{res} = 0; /* not isa {mtype} */")
1133 self.add
("\} else ")
1135 self.add
("switch({value}->classid) \{")
1137 if t
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1138 self.add
("case {self.compiler.classid(t)}: /* {t} */")
1141 self.add
("{res} = 1;")
1143 self.add
("default:")
1144 self.add
("{res} = 0;")
1150 # Generate a Nit "is" for two runtime_variables
1151 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1153 var res
= self.new_var
(bool_type
)
1154 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1159 if value1
.mtype
.ctype
!= "val*" then
1160 if value2
.mtype
.ctype
== value1
.mtype
.ctype
then
1161 self.add
("{res} = {value1} == {value2};")
1162 else if value2
.mtype
.ctype
!= "val*" then
1163 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1165 var mtype1
= value1
.mtype
.as(MClassType)
1166 self.add
("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
1167 self.add
("if ({res}) \{")
1168 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1172 var s
= new Array[String]
1173 for t
in self.compiler
.live_primitive_types
do
1174 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value1
.mcasttype
) then continue
1175 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value2
.mcasttype
) then continue
1176 s
.add
"({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
1179 self.add
("{res} = {value1} == {value2};")
1181 self.add
("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")
1187 # Generate a check-init-instance
1188 # TODO: is an empty stub currently
1189 fun check_init_instance
(recv
: RuntimeVariable)
1193 # Generate an integer value
1194 fun int_instance
(value
: Int): RuntimeVariable
1196 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
1197 self.add
("{res} = {value};")
1201 # Generate an array value
1202 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable
1204 elttype
= self.anchor
(elttype
)
1205 var res
= self.init_instance
(self.get_class
("Array").get_mtype
([elttype
]))
1206 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1207 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
1209 self.add
("{nat} = GC_MALLOC({array.length} * sizeof({elttype.ctype}));")
1210 for i
in [0..array
.length
[ do
1211 var r
= self.autobox
(array
[i
], elttype
)
1212 self.add
("{nat}[{i}] = {r};")
1214 var length
= self.int_instance
(array
.length
)
1215 self.send
(self.get_property
("with_native", res
.mtype
), [res
, nat
, length
])
1216 self.check_init_instance
(res
)
1221 # Generate a string value
1222 fun string_instance
(string
: String): RuntimeVariable
1224 var mtype
= self.get_class
("String").mclass_type
1225 var name
= self.get_name
("varonce")
1226 self.add_decl
("static {mtype.ctype} {name};")
1227 var res
= self.new_var
(mtype
)
1228 self.add
("if ({name}) \{")
1229 self.add
("{res} = {name};")
1230 self.add
("\} else \{")
1231 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
1232 self.add
("{nat} = \"{string.escape_to_c}\
";")
1233 var res2
= self.init_instance
(mtype
)
1234 self.add
("{res} = {res2};")
1235 var length
= self.int_instance
(string
.length
)
1236 self.send
(self.get_property
("with_native", res
.mtype
), [res
, nat
, length
])
1237 self.check_init_instance
(res
)
1238 self.add
("{name} = {res};")
1243 # Generate generic abort
1244 # used by aborts, asserts, casts, etc.
1245 fun add_abort
(message
: String)
1247 if self.current_node
!= null and self.current_node
.location
.file
!= null then
1248 self.add
("fprintf(stderr, \"%s
(%s
:%d
)\\n\
", \"{message.escape_to_c}\
", \"{self.current_node.location.file.filename.escape_to_c}\
", {current_node.location.line_start});")
1250 self.add
("fprintf(stderr, \"%s\\n\
", \"{message.escape_to_c}\
");")
1252 self.add
("exit(1);")
1256 # A frame correspond to a visited property in a GlobalCompilerVisitor
1258 # The associated visitor
1260 var visitor
: GlobalCompilerVisitor
1262 # The executed property.
1263 # A Method in case of a call, an attribute in case of a default initialization.
1264 var mpropdef
: MPropDef
1266 # The static type of the receiver
1267 var receiver
: MClassType
1269 # Arguments of the method (the first is the receiver)
1270 var arguments
: Array[RuntimeVariable]
1272 # The runtime_variable associated to the return (in a function)
1273 var returnvar
: nullable RuntimeVariable = null
1275 # The label at the end of the property
1276 var returnlabel
: nullable String = null
1279 redef class MPropDef
1280 private var c_name_cache
: nullable String
1282 # The mangled name associated to the property
1285 var res
= self.c_name_cache
1286 if res
!= null then return res
1287 res
= "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1288 self.c_name_cache
= res
1293 redef class MMethodDef
1294 # Can the body be inlined?
1295 private fun can_inline
(v
: GlobalCompilerVisitor): Bool
1297 var modelbuilder
= v
.compiler
.modelbuilder
1298 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1299 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1300 return npropdef
.can_inline
1301 else if self.mproperty
.name
== "init" then
1302 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1309 # Inline the body in another visitor
1310 private fun compile_inside_to_c
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1312 var modelbuilder
= v
.compiler
.modelbuilder
1313 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1314 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1315 npropdef
.compile_to_c
(v
, self, arguments
)
1316 else if self.mproperty
.name
== "init" then
1317 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1318 nclassdef
.compile_to_c
(v
, self, arguments
)
1325 # Compile the body in a new visitor
1326 private fun compile_to_c
(compiler
: GlobalCompiler, modelbuilder
: ModelBuilder, recv
: MClassType)
1328 if not recv
.is_subtype
(compiler
.mainmodule
, null, self.mclassdef
.bound_mtype
) then
1329 print
("problem: why do we compile {self} for {recv}?")
1333 var v
= new GlobalCompilerVisitor(compiler
)
1334 var selfvar
= new RuntimeVariable("self", recv
, recv
)
1335 if compiler
.runtime_type_analysis
.live_types
.has
(recv
) then
1336 selfvar
.is_exact
= true
1338 var arguments
= new Array[RuntimeVariable]
1339 var frame
= new Frame(v
, self, recv
, arguments
)
1342 var sig
= new Buffer
1343 var comment
= new Buffer
1344 var ret
= self.msignature
.return_mtype
1346 ret
= v
.resolve_for
(ret
, selfvar
)
1347 sig
.append
("{ret.ctype} ")
1348 else if self.mproperty
.is_new
then
1350 sig
.append
("{ret.ctype} ")
1354 sig
.append
(self.c_name
)
1355 if recv
!= self.mclassdef
.bound_mtype
then
1356 sig
.append
("__{recv.c_name}")
1358 sig
.append
("({recv.ctype} self")
1359 comment
.append
("(self: {recv}")
1360 arguments
.add
(selfvar
)
1361 for i
in [0..self.msignature
.arity
[ do
1362 var mtype
= self.msignature
.parameter_mtypes
[i
]
1363 if i
== self.msignature
.vararg_rank
then
1364 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
1366 mtype
= v
.resolve_for
(mtype
, selfvar
)
1367 comment
.append
(", {mtype}")
1368 sig
.append
(", {mtype.ctype} p{i}")
1369 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
1370 arguments
.add
(argvar
)
1375 comment
.append
(": {ret}")
1377 compiler
.header
.add_decl
("{sig};")
1379 v
.add_decl
("/* method {self} for {comment} */")
1380 v
.add_decl
("{sig} \{")
1381 #v.add("printf(\"method {self} for {comment}\\n\");")
1383 frame
.returnvar
= v
.new_var
(ret
)
1385 frame
.returnlabel
= v
.get_name
("RET_LABEL")
1387 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1388 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1389 npropdef
.compile_to_c
(v
, self, arguments
)
1390 else if self.mproperty
.name
== "init" then
1391 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1392 nclassdef
.compile_to_c
(v
, self, arguments
)
1397 v
.add
("{frame.returnlabel.as(not null)}:;")
1399 v
.add
("return {frame.returnvar.as(not null)};")
1405 redef class APropdef
1406 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1408 v
.add
("printf(\"Not implemented
{class_name} {mpropdef} at
{location.to_s}\\n\
");")
1409 debug
("Not yet implemented")
1412 private fun can_inline
: Bool do return true
1415 redef class AConcreteMethPropdef
1416 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1418 for i
in [0..mpropdef
.msignature
.arity
[ do
1419 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
1420 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
1422 # Call the implicit super-init
1423 var auto_super_inits
= self.auto_super_inits
1424 if auto_super_inits
!= null then
1425 var selfarg
= [arguments
.first
]
1426 for auto_super_init
in auto_super_inits
do
1427 if auto_super_init
.intro
.msignature
.arity
== 0 then
1428 v
.send
(auto_super_init
, selfarg
)
1430 v
.send
(auto_super_init
, arguments
)
1435 v
.stmt
(self.n_block
)
1438 redef fun can_inline
1440 if self.auto_super_inits
!= null then return false
1441 var nblock
= self.n_block
1442 if nblock
== null then return true
1443 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
1444 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
1449 redef class AInternMethPropdef
1450 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1452 var pname
= mpropdef
.mproperty
.name
1453 var cname
= mpropdef
.mclassdef
.mclass
.name
1454 var ret
= mpropdef
.msignature
.return_mtype
1456 ret
= v
.resolve_for
(ret
, arguments
.first
)
1458 if pname
!= "==" and pname
!= "!=" then
1459 v
.adapt_signature
(mpropdef
, arguments
)
1461 if cname
== "Int" then
1462 if pname
== "output" then
1463 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
1465 else if pname
== "object_id" then
1466 v
.ret
(arguments
.first
)
1468 else if pname
== "+" then
1469 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1471 else if pname
== "-" then
1472 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1474 else if pname
== "unary -" then
1475 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1477 else if pname
== "succ" then
1478 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1480 else if pname
== "prec" then
1481 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1483 else if pname
== "*" then
1484 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
1486 else if pname
== "/" then
1487 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
1489 else if pname
== "%" then
1490 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
1492 else if pname
== "lshift" then
1493 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
1495 else if pname
== "rshift" then
1496 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
1498 else if pname
== "==" then
1499 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1501 else if pname
== "!=" then
1502 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1503 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1505 else if pname
== "<" then
1506 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1508 else if pname
== ">" then
1509 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1511 else if pname
== "<=" then
1512 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1514 else if pname
== ">=" then
1515 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1517 else if pname
== "to_f" then
1518 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
1520 else if pname
== "ascii" then
1521 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1524 else if cname
== "Char" then
1525 if pname
== "output" then
1526 v
.add
("printf(\"%c\
", {arguments.first});")
1528 else if pname
== "object_id" then
1529 v
.ret
(arguments
.first
)
1531 else if pname
== "==" then
1532 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1534 else if pname
== "!=" then
1535 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1536 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1538 else if pname
== "succ" then
1539 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1541 else if pname
== "prec" then
1542 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1544 else if pname
== "<" then
1545 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1547 else if pname
== ">" then
1548 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1550 else if pname
== "<=" then
1551 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1553 else if pname
== ">=" then
1554 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1556 else if pname
== "to_i" then
1557 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
1559 else if pname
== "ascii" then
1560 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
1563 else if cname
== "Bool" then
1564 if pname
== "output" then
1565 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
1567 else if pname
== "object_id" then
1568 v
.ret
(arguments
.first
)
1570 else if pname
== "==" then
1571 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1573 else if pname
== "!=" then
1574 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1575 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1578 else if cname
== "Float" then
1579 if pname
== "output" then
1580 v
.add
("printf(\"%f\\n\
", {arguments.first});")
1582 else if pname
== "object_id" then
1583 v
.ret
(v
.new_expr
("(double){arguments.first}", ret
.as(not null)))
1585 else if pname
== "+" then
1586 v
.ret
(v
.new_expr
("{arguments[0]} + {arguments[1]}", ret
.as(not null)))
1588 else if pname
== "-" then
1589 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1591 else if pname
== "unary -" then
1592 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1594 else if pname
== "succ" then
1595 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1597 else if pname
== "prec" then
1598 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1600 else if pname
== "*" then
1601 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
1603 else if pname
== "/" then
1604 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
1606 else if pname
== "==" then
1607 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1609 else if pname
== "!=" then
1610 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1611 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1613 else if pname
== "<" then
1614 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1616 else if pname
== ">" then
1617 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1619 else if pname
== "<=" then
1620 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1622 else if pname
== ">=" then
1623 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1625 else if pname
== "to_i" then
1626 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
1629 else if cname
== "Char" then
1630 if pname
== "output" then
1631 v
.add
("printf(\"%c\
", {arguments.first});")
1633 else if pname
== "object_id" then
1634 v
.ret
(arguments
.first
)
1636 else if pname
== "==" then
1637 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1639 else if pname
== "!=" then
1640 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1641 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1643 else if pname
== "ascii" then
1644 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1647 else if cname
== "NativeString" then
1648 if pname
== "[]" then
1649 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
1651 else if pname
== "[]=" then
1652 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1654 else if pname
== "copy_to" then
1655 v
.add
("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
1657 else if pname
== "atoi" then
1658 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
1661 else if cname
== "NativeArray" then
1662 var elttype
= arguments
.first
.mtype
1663 if pname
== "[]" then
1664 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
1666 else if pname
== "[]=" then
1667 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1669 else if pname
== "copy_to" then
1670 v
.add
("memcpy({arguments[1]},{arguments[0]},{arguments[2]}*sizeof({elttype.ctype}));")
1674 if pname
== "exit" then
1675 v
.add
("exit({arguments[1]});")
1677 else if pname
== "sys" then
1678 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
1680 else if pname
== "calloc_string" then
1681 v
.ret
(v
.new_expr
("(char*)GC_MALLOC({arguments[1]})", ret
.as(not null)))
1683 else if pname
== "calloc_array" then
1684 var elttype
= arguments
.first
.mtype
.supertype_to
(v
.compiler
.mainmodule
,arguments
.first
.mtype
.as(MClassType),v
.get_class
("ArrayCapable")).as(MGenericType).arguments
.first
1685 v
.ret
(v
.new_expr
("({elttype.ctype}*)GC_MALLOC({arguments[1]} * sizeof({elttype.ctype}))", ret
.as(not null)))
1687 else if pname
== "object_id" then
1688 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
1690 else if pname
== "is_same_type" then
1691 if arguments
[0].mtype
.ctype
== "val*" then
1692 v
.ret
(v
.new_expr
("{arguments[0]}->classid == {arguments[1]}->classid", ret
.as(not null)))
1694 v
.ret
(v
.new_expr
("{v.compiler.classid(arguments[0].mtype.as(MClassType))} == {arguments[1]}->classid", ret
.as(not null)))
1697 else if pname
== "output_class_name" then
1698 if arguments
[0].mtype
.ctype
== "val*" then
1699 v
.add
("printf(\"%s\\n\
", class_names[{arguments.first}->classid]);")
1701 v
.add
("printf(\"%s\\n\
", class_names[{v.compiler.classid(arguments.first.mtype.as(MClassType))}]);")
1704 else if pname
== "native_class_name" then
1705 if arguments
[0].mtype
.ctype
== "val*" then
1706 v
.ret
(v
.new_expr
("(char*)(void*)class_names[{arguments.first}->classid]", ret
.as(not null)))
1708 v
.ret
(v
.new_expr
("(char*)(void*)class_names[{v.compiler.classid(arguments.first.mtype.as(MClassType))}]", ret
.as(not null)))
1712 v
.add
("printf(\"Not implemented
{class_name}:{mpropdef} at
{location.to_s}\\n\
");")
1713 debug
("Not implemented {mpropdef}")
1717 redef class AExternMethPropdef
1718 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1721 var nextern
= self.n_extern
1722 if nextern
== null then
1723 debug
("{mpropdef} need extern name")
1726 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
1727 if location
.file
!= null then
1728 var file
= location
.file
.filename
1729 v
.compiler
.add_extern
(file
)
1731 var res
: nullable RuntimeVariable = null
1732 var ret
= mpropdef
.msignature
.return_mtype
1734 ret
= v
.resolve_for
(ret
, arguments
.first
)
1735 res
= v
.new_var
(ret
)
1739 v
.add
("{externname}({arguments.join(", ")});")
1741 v
.add
("{res} = {externname}({arguments.join(", ")});")
1747 redef class AExternInitPropdef
1748 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1751 var nextern
= self.n_extern
1752 if nextern
== null then
1753 debug
("{mpropdef} need extern name")
1756 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
1757 if location
.file
!= null then
1758 var file
= location
.file
.filename
1759 v
.compiler
.add_extern
(file
)
1761 var ret
= arguments
.first
.mtype
1762 var res
= v
.new_var
(ret
)
1766 v
.add
("{res} = {externname}({arguments.join(", ")});")
1771 redef class AAttrPropdef
1772 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1774 if arguments
.length
== 1 then
1775 var res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
1776 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
1778 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arguments
[1])
1782 private fun init_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
1784 var nexpr
= self.n_expr
1785 if nexpr
!= null then
1786 var old_frame
= v
.frame
1787 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
1789 var value
= v
.expr
(nexpr
, self.mpropdef
.static_mtype
)
1790 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
1796 redef class AClassdef
1797 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1799 if mpropdef
== self.mfree_init
then
1800 var super_inits
= self.super_inits
1801 if super_inits
!= null then
1802 assert arguments
.length
== 1
1803 for su
in super_inits
do
1804 v
.send
(su
, arguments
)
1808 var recv
= arguments
.first
1810 # Collect undefined attributes
1811 for npropdef
in self.n_propdefs
do
1812 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
1813 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, arguments
[i
])
1823 redef class ADeferredMethPropdef
1824 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1826 v
.add
("printf(\"Not implemented
{class_name} {mpropdef} at
{location.to_s}\\n\
");")
1830 redef fun can_inline
do return true
1834 # Try to compile self as an expression
1835 # Do not call this method directly, use `v.expr' instead
1836 private fun expr
(v
: GlobalCompilerVisitor): nullable RuntimeVariable
1838 debug
("Unimplemented expr {class_name}")
1839 v
.add
("printf(\"Not implemented
{class_name}:{location.to_s}\\n\
");")
1840 var mtype
= self.mtype
1841 if mtype
== null then
1844 var res
= v
.new_var
(mtype
)
1845 v
.add
("/* {res} = NOT YET {class_name} */")
1850 # Try to compile self as a statement
1851 # Do not call this method directly, use `v.stmt' instead
1852 private fun stmt
(v
: GlobalCompilerVisitor)
1855 if res
!= null then v
.add
("{res};")
1860 redef class ABlockExpr
1863 for e
in self.n_expr
do
1869 redef class AVardeclExpr
1872 var variable
= self.variable
.as(not null)
1873 var ne
= self.n_expr
1875 var i
= v
.expr
(ne
, variable
.declared_type
)
1876 v
.assign
(v
.variable
(variable
), i
)
1881 redef class AVarExpr
1884 var res
= v
.variable
(self.variable
.as(not null))
1885 var mtype
= self.mtype
.as(not null)
1886 return v
.autoadapt
(res
, mtype
)
1890 redef class AVarAssignExpr
1893 var variable
= self.variable
.as(not null)
1894 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
1895 v
.assign
(v
.variable
(variable
), i
)
1899 redef class AVarReassignExpr
1902 var variable
= self.variable
.as(not null)
1903 var vari
= v
.variable
(variable
)
1904 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
1905 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
1907 v
.assign
(v
.variable
(variable
), res
)
1911 redef class ASelfExpr
1914 return v
.frame
.arguments
.first
1918 redef class AContinueExpr
1921 v
.add
("goto CONTINUE_{self.escapemark.object_id};")
1925 redef class ABreakExpr
1928 v
.add
("goto BREAK_{self.escapemark.object_id};")
1932 redef class AReturnExpr
1935 var nexpr
= self.n_expr
1936 if nexpr
!= null then
1937 var returnvar
= v
.frame
.returnvar
.as(not null)
1938 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
1939 v
.assign
(returnvar
, i
)
1941 v
.add
("goto {v.frame.returnlabel.as(not null)};")
1945 redef class AAbortExpr
1948 v
.add_abort
("Aborted")
1955 var cond
= v
.expr_bool
(self.n_expr
)
1956 v
.add
("if ({cond})\{")
1964 redef class AIfexprExpr
1967 var res
= v
.new_var
(self.mtype
.as(not null))
1968 var cond
= v
.expr_bool
(self.n_expr
)
1969 v
.add
("if ({cond})\{")
1970 v
.assign
(res
, v
.expr
(self.n_then
, null))
1972 v
.assign
(res
, v
.expr
(self.n_else
, null))
1981 v
.stmt
(self.n_block
)
1982 var escapemark
= self.escapemark
1983 if escapemark
!= null then
1984 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
1989 redef class AWhileExpr
1993 var cond
= v
.expr_bool
(self.n_expr
)
1994 v
.add
("if (!{cond}) break;")
1995 v
.stmt
(self.n_block
)
1996 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
1998 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
2002 redef class ALoopExpr
2006 v
.stmt
(self.n_block
)
2007 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
2009 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
2013 redef class AForExpr
2016 var cl
= v
.expr
(self.n_expr
, null)
2017 var it
= v
.send
(v
.get_property
("iterator", cl
.mtype
), [cl
])
2020 var ok
= v
.send
(v
.get_property
("is_ok", it
.mtype
), [it
])
2022 v
.add
("if(!{ok}) break;")
2023 var i
= v
.send
(v
.get_property
("item", it
.mtype
), [it
])
2025 v
.assign
(v
.variable
(variables
.first
), i
)
2026 v
.stmt
(self.n_block
)
2027 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
2028 v
.send
(v
.get_property
("next", it
.mtype
), [it
])
2030 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
2034 redef class AAssertExpr
2037 var cond
= v
.expr_bool
(self.n_expr
)
2038 v
.add
("if (!{cond}) \{")
2042 v
.add_abort
("Assert '{nid.text}' failed")
2044 v
.add_abort
("Assert failed")
2053 var res
= v
.new_var
(self.mtype
.as(not null))
2054 var i1
= v
.expr_bool
(self.n_expr
)
2055 v
.add
("if ({i1}) \{")
2058 var i2
= v
.expr_bool
(self.n_expr2
)
2059 v
.add
("{res} = {i2};")
2065 redef class AAndExpr
2068 var res
= v
.new_var
(self.mtype
.as(not null))
2069 var i1
= v
.expr_bool
(self.n_expr
)
2070 v
.add
("if (!{i1}) \{")
2073 var i2
= v
.expr_bool
(self.n_expr2
)
2074 v
.add
("{res} = {i2};")
2080 redef class ANotExpr
2083 var cond
= v
.expr_bool
(self.n_expr
)
2084 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2088 redef class AOrElseExpr
2091 var res
= v
.new_var
(self.mtype
.as(not null))
2092 var i1
= v
.expr
(self.n_expr
, null)
2093 v
.add
("if ({i1}!=NULL) \{")
2096 var i2
= v
.expr
(self.n_expr2
, null)
2106 var value1
= v
.expr
(self.n_expr
, null)
2107 var value2
= v
.expr
(self.n_expr2
, null)
2108 return v
.equal_test
(value1
, value2
)
2112 redef class AIntExpr
2115 return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2119 redef class AFloatExpr
2122 return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2126 redef class ACharExpr
2129 return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2133 redef class AArrayExpr
2136 var mtype
= self.mtype
.as(MGenericType).arguments
.first
2137 var array
= new Array[RuntimeVariable]
2138 for nexpr
in self.n_exprs
.n_exprs
do
2139 var i
= v
.expr
(nexpr
, mtype
)
2142 return v
.array_instance
(array
, mtype
)
2146 redef class AStringFormExpr
2149 return v
.string_instance
(self.value
.as(not null))
2153 redef class ASuperstringExpr
2156 var array
= new Array[RuntimeVariable]
2157 for ne
in self.n_exprs
do
2158 var i
= v
.expr
(ne
, null)
2161 var a
= v
.array_instance
(array
, v
.object_type
)
2162 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2167 redef class ACrangeExpr
2170 var i1
= v
.expr
(self.n_expr
, null)
2171 var i2
= v
.expr
(self.n_expr2
, null)
2172 var res
= v
.init_instance
(self.mtype
.as(MClassType))
2173 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2174 v
.check_init_instance
(res
)
2179 redef class AOrangeExpr
2182 var i1
= v
.expr
(self.n_expr
, null)
2183 var i2
= v
.expr
(self.n_expr2
, null)
2184 var res
= v
.init_instance
(self.mtype
.as(MClassType))
2185 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2186 v
.check_init_instance
(res
)
2191 redef class ATrueExpr
2194 return v
.new_expr
("1", self.mtype
.as(not null))
2198 redef class AFalseExpr
2201 return v
.new_expr
("0", self.mtype
.as(not null))
2205 redef class ANullExpr
2208 var res
= v
.new_expr
("NULL", self.mtype
.as(not null))
2213 redef class AIsaExpr
2216 var i
= v
.expr
(self.n_expr
, null)
2217 return v
.type_test
(i
, self.cast_type
.as(not null))
2221 redef class AAsCastExpr
2224 var i
= v
.expr
(self.n_expr
, null)
2225 var cond
= v
.type_test
(i
, self.mtype
.as(not null))
2226 v
.add
("if (!{cond}) \{")
2227 v
.add_abort
("Cast failed")
2233 redef class AAsNotnullExpr
2236 var i
= v
.expr
(self.n_expr
, null)
2237 v
.add
("if ({i} == NULL) \{")
2238 v
.add_abort
("Cast failed")
2244 redef class AParExpr
2247 return v
.expr
(self.n_expr
, null)
2251 redef class AOnceExpr
2254 var mtype
= self.mtype
.as(not null)
2255 var name
= v
.get_name
("varonce")
2256 var guard
= v
.get_name
(name
+ "_guard")
2257 v
.add_decl
("static {mtype.ctype} {name};")
2258 v
.add_decl
("static int {guard};")
2259 var res
= v
.new_var
(mtype
)
2260 v
.add
("if ({guard}) \{")
2261 v
.add
("{res} = {name};")
2263 var i
= v
.expr
(self.n_expr
, mtype
)
2264 v
.add
("{res} = {i};")
2265 v
.add
("{name} = {res};")
2266 v
.add
("{guard} = 1;")
2272 redef class ASendExpr
2275 var recv
= v
.expr
(self.n_expr
, null)
2277 for a
in compute_raw_arguments
do
2278 args
.add
(v
.expr
(a
, null))
2280 var mproperty
= self.mproperty
.as(not null)
2281 return v
.send
(mproperty
, args
)
2285 redef class ASendReassignFormExpr
2288 var recv
= v
.expr
(self.n_expr
, null)
2290 for a
in compute_raw_arguments
do
2291 args
.add
(v
.expr
(a
, null))
2293 var value
= v
.expr
(self.n_value
, null)
2295 var mproperty
= self.mproperty
.as(not null)
2296 var left
= v
.send
(mproperty
, args
)
2299 var res
= v
.send
(reassign_property
.mproperty
, [left
, value
])
2303 v
.send
(self.write_mproperty
.as(not null), args
)
2307 redef class ASuperExpr
2310 var recv
= v
.frame
.arguments
.first
2312 for a
in self.n_args
.n_exprs
do
2313 args
.add
(v
.expr
(a
, null))
2315 if args
.length
== 1 then
2316 args
= v
.frame
.arguments
2319 var mproperty
= self.mproperty
2320 if mproperty
!= null then
2321 if mproperty
.intro
.msignature
.arity
== 0 then
2325 var res
= v
.send
(mproperty
, args
)
2329 # stantard call-next-method
2330 var mpropdef
= v
.frame
.mpropdef
2331 # FIXME: we do not want an ugly static call!
2332 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
2333 if mpropdefs
.length
!= 1 then
2334 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
2336 mpropdef
= mpropdefs
.first
2337 assert mpropdef
isa MMethodDef
2338 var res
= v
.call
(mpropdef
, recv
.mtype
.as(MClassType), args
)
2343 redef class ANewExpr
2346 var mproperty
= self.mproperty
.as(not null)
2347 var mtype
= self.mtype
.as(MClassType)
2349 var ctype
= mtype
.ctype
2350 if ctype
== "val*" then
2351 recv
= v
.init_instance
(mtype
)
2352 else if ctype
== "void*" then
2353 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2355 debug
("cannot new {mtype}")
2359 for a
in self.n_args
.n_exprs
do
2360 args
.add
(v
.expr
(a
, null))
2362 var res2
= v
.send
(mproperty
, args
)
2363 if res2
!= null then
2364 #self.debug("got {res2} from {mproperty}. drop {recv}")
2367 v
.check_init_instance
(recv
)
2372 redef class AAttrExpr
2375 var recv
= v
.expr
(self.n_expr
, null)
2376 var mproperty
= self.mproperty
.as(not null)
2377 return v
.read_attribute
(mproperty
, recv
)
2381 redef class AAttrAssignExpr
2384 var recv
= v
.expr
(self.n_expr
, null)
2385 var i
= v
.expr
(self.n_value
, null)
2386 var mproperty
= self.mproperty
.as(not null)
2387 v
.write_attribute
(mproperty
, recv
, i
)
2391 redef class AAttrReassignExpr
2394 var recv
= v
.expr
(self.n_expr
, null)
2395 var value
= v
.expr
(self.n_value
, null)
2396 var mproperty
= self.mproperty
.as(not null)
2397 var attr
= v
.read_attribute
(mproperty
, recv
)
2398 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
2400 v
.write_attribute
(mproperty
, recv
, res
)
2404 redef class AIssetAttrExpr
2407 redef class ADebugTypeExpr