1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Separate compilation of a Nit program
16 module separate_compiler
19 import global_compiler
# TODO better separation of concerns
20 intrude import coloring
21 redef class ToolContext
23 var opt_separate
: OptionBool = new OptionBool("Use separate compilation", "--separate")
28 self.option_context
.add_option
(self.opt_separate
)
32 redef class ModelBuilder
33 redef fun run_global_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
35 # Hijack the run_global_compiler to run the separate one if requested.
36 if self.toolcontext
.opt_separate
.value
then
37 run_separate_compiler
(mainmodule
, runtime_type_analysis
)
43 fun run_separate_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
46 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
48 var compiler
= new SeparateCompiler(mainmodule
, runtime_type_analysis
, self)
49 var v
= new SeparateCompilerVisitor(compiler
)
51 v
.add_decl
("#include <stdlib.h>")
52 v
.add_decl
("#include <stdio.h>")
53 v
.add_decl
("#include <string.h>")
54 v
.add_decl
("#include <gc/gc.h>")
55 v
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
56 v
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
58 # Class abstract representation
59 v
.add_decl
("struct class \{ nitmethod_t vft[1]; \}; /* general C type representing a Nit class. */")
60 # Type abstract representation
61 v
.add_decl
("struct type \{ int id; int color; struct fts_table *fts_table; int type_table[1]; \}; /* general C type representing a Nit type. */")
62 v
.add_decl
("struct fts_table \{ struct type *fts[1]; \}; /* fts list of a C type representation. */")
63 # Instance abstract representation
64 v
.add_decl
("typedef struct \{ struct type *type; struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
66 # Class names (for the class_name and output_class_name methods)
67 v
.add_decl
("extern const char const * class_names[];")
68 v
.add
("const char const * class_names[] = \{")
69 for t
in runtime_type_analysis
.live_types
do
74 # The main function of the C
76 v
= new SeparateCompilerVisitor(compiler
)
77 v
.add_decl
("int glob_argc;")
78 v
.add_decl
("char **glob_argv;")
79 v
.add_decl
("val *glob_sys;")
80 v
.add_decl
("int main(int argc, char** argv) \{")
81 v
.add
("glob_argc = argc; glob_argv = argv;")
82 var main_type
= mainmodule
.sys_type
83 if main_type
== null then return # Nothing to compile
84 var glob_sys
= v
.init_instance
(main_type
)
85 v
.add
("glob_sys = {glob_sys};")
86 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
87 if main_init
!= null then
88 v
.send
(main_init
, [glob_sys
])
90 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
91 if main_method
!= null then
92 v
.send
(main_method
, [glob_sys
])
96 for m
in mainmodule
.in_importation
.greaters
do
97 compiler
.compile_module_to_c
(m
)
98 for mclass
in m
.intro_mclasses
do
99 compiler
.compile_class_to_c
(mclass
)
103 # compile live & cast type structures
104 var mtypes
= compiler
.do_global_type_coloring
106 compiler
.compile_type_to_c
(t
)
109 for mclass
in model
.mclasses
do
110 compiler
.compile_live_gentype_to_c
(mclass
)
113 write_and_make
(compiler
)
117 # Singleton that store the knowledge about the separate compilation process
118 class SeparateCompiler
119 super GlobalCompiler # TODO better separation of concerns
121 private var undead_types
: Set[MClassType] = new HashSet[MClassType]
122 protected var typeids
: HashMap[MClassType, Int] = new HashMap[MClassType, Int]
124 private var type_colors
: Map[MClassType, Int] = typeids
125 private var type_tables
: nullable Map[MClassType, Array[nullable MClassType]] = null
126 private var livetypes_tables
: nullable Map[MClass, Array[nullable Object]]
127 private var livetypes_tables_sizes
: nullable Map[MClass, Array[Int]]
129 private var class_colors
: Map[MClass, Int]
131 private var method_colors
: Map[MMethod, Int]
132 private var method_tables
: Map[MClass, Array[nullable MMethodDef]]
134 private var attr_colors
: Map[MAttribute, Int]
135 private var attr_tables
: Map[MClass, Array[nullable MAttributeDef]]
137 private var ft_colors
: Map[MParameterType, Int]
138 private var ft_tables
: Map[MClass, Array[nullable MParameterType]]
140 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
142 var class_coloring
= new ClassColoring(mainmodule
)
143 self.class_colors
= class_coloring
.colorize
(mmbuilder
.model
.mclasses
)
146 var method_coloring
= new MethodColoring(class_coloring
)
147 self.method_colors
= method_coloring
.colorize
148 self.method_tables
= method_coloring
.build_property_tables
150 # attributes coloration
151 var attribute_coloring
= new AttributeColoring(class_coloring
)
152 self.attr_colors
= attribute_coloring
.colorize
153 self.attr_tables
= attribute_coloring
.build_property_tables
156 var ft_coloring
= new FTColoring(class_coloring
)
157 self.ft_colors
= ft_coloring
.colorize
158 self.ft_tables
= ft_coloring
.build_ft_tables
161 # colorize live types of the program
162 private fun do_global_type_coloring
: Set[MClassType] do
163 var mtypes
= new HashSet[MClassType]
164 #print "undead types:"
165 #for t in self.undead_types do
169 #for t in runtime_type_analysis.live_types do
173 #for t in runtime_type_analysis.live_cast_types do
177 mtypes
.add_all
(self.runtime_type_analysis
.live_types
)
178 mtypes
.add_all
(self.runtime_type_analysis
.live_cast_types
)
179 mtypes
.add_all
(self.undead_types
)
181 # add formal types arguments to mtypes
182 for mtype
in mtypes
do
183 if mtype
isa MGenericType then
184 #TODO do it recursive
185 for ft
in mtype
.arguments
do
186 if ft
isa MNullableType then ft
= ft
.mtype
187 mtypes
.add
(ft
.as(MClassType))
193 for mtype
in mtypes
do
194 self.typeids
[mtype
] = self.typeids
.length
197 # build livetypes tables
198 self.livetypes_tables
= new HashMap[MClass, Array[nullable Object]]
199 self.livetypes_tables_sizes
= new HashMap[MClass, Array[Int]]
200 for mtype
in mtypes
do
201 if mtype
isa MGenericType then
202 var table
: Array[nullable Object]
203 var sizes
: Array[Int]
204 if livetypes_tables
.has_key
(mtype
.mclass
) then
205 table
= livetypes_tables
[mtype
.mclass
]
207 table
= new Array[nullable Object]
208 self.livetypes_tables
[mtype
.mclass
] = table
210 if livetypes_tables_sizes
.has_key
(mtype
.mclass
) then
211 sizes
= livetypes_tables_sizes
[mtype
.mclass
]
213 sizes
= new Array[Int]
214 self.livetypes_tables_sizes
[mtype
.mclass
] = sizes
216 build_livetype_table
(mtype
, 0, table
, sizes
)
221 var type_coloring
= new TypeColoring(self.mainmodule
, self.runtime_type_analysis
)
222 self.type_colors
= type_coloring
.colorize
(mtypes
)
223 self.type_tables
= type_coloring
.build_type_tables
(mtypes
, type_colors
)
228 # build live gentype table recursively
229 private fun build_livetype_table
(mtype
: MGenericType, current_rank
: Int, table
: Array[nullable Object], sizes
: Array[Int]) do
230 var ft
= mtype
.arguments
[current_rank
]
231 if ft
isa MNullableType then ft
= ft
.mtype
232 var id
= self.typeids
[ft
.as(MClassType)]
234 if current_rank
>= sizes
.length
then
235 sizes
[current_rank
] = id
+ 1
236 else if id
>= sizes
[current_rank
] then
237 sizes
[current_rank
] = id
+ 1
240 if id
> table
.length
then
241 for i
in [table
.length
.. id
[ do table
[i
] = null
244 if current_rank
== mtype
.arguments
.length
- 1 then
247 var ft_table
= new Array[nullable Object]
249 build_livetype_table
(mtype
, current_rank
+ 1, ft_table
, sizes
)
253 private fun add_to_livetypes_table
(table
: Array[nullable Object], ft
: MClassType) do
254 var id
= self.typeids
[ft
]
255 for i
in [table
.length
.. id
[ do
261 private fun compile_livetype_table
(table
: Array[nullable Object], buffer
: Buffer, depth
: Int, max
: Int) do
265 buffer
.append
("NULL,\n")
267 buffer
.append
("\{\},\n")
269 else if obj
isa MClassType then
270 buffer
.append
("(struct type*) &type_{obj.c_name}, /* {obj} */\n")
271 else if obj
isa Array[nullable Object] then
272 buffer
.append
("\{\n")
273 compile_livetype_table
(obj
, buffer
, depth
+ 1, max
)
274 buffer
.append
("\},\n")
279 # declare live generic types tables selection
280 private fun compile_live_gentype_to_c
(mclass
: MClass) do
281 if mclass
.arity
> 0 then
282 if self.livetypes_tables
.has_key
(mclass
) then
283 var table
= self.livetypes_tables
[mclass
]
284 var sign
= self.livetypes_tables_sizes
[mclass
]
285 var table_buffer
= new Buffer.from
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}] = \{\n")
286 compile_livetype_table
(table
, table_buffer
, 1, mclass
.arity
)
287 table_buffer
.append
("\};")
289 var v
= new SeparateCompilerVisitor(self)
290 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
291 v
.add_decl
(table_buffer
.to_s
)
293 var sign
= new Array[Int].filled_with
(0, mclass
.arity
)
294 var v
= new SeparateCompilerVisitor(self)
295 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
296 v
.add_decl
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
301 # Separately compile all the method definitions of the module
302 fun compile_module_to_c
(mmodule
: MModule)
304 for cd
in mmodule
.mclassdefs
do
305 for pd
in cd
.mpropdefs
do
306 if not pd
isa MMethodDef then continue
307 #print "compile {pd} @ {cd} @ {mmodule}"
308 var r
= new SeparateRuntimeFunction(pd
)
310 if cd
.bound_mtype
.ctype
!= "val*" then
311 var r2
= new VirtualRuntimeFunction(pd
)
312 r2
.compile_to_c
(self)
318 # Globaly compile the type structure of a live type
319 fun compile_type_to_c
(mtype
: MClassType)
321 var c_name
= mtype
.c_name
322 var v
= new SeparateCompilerVisitor(self)
323 v
.add_decl
("/* runtime type {mtype} */")
325 # extern const struct type_X
326 self.header
.add_decl
("extern const struct type_{c_name} type_{c_name};")
327 self.header
.add_decl
("struct type_{c_name} \{")
328 self.header
.add_decl
("int id;")
329 self.header
.add_decl
("int color;")
330 self.header
.add_decl
("const struct fts_table_{c_name} *fts_table;")
331 self.header
.add_decl
("int type_table[{self.type_tables[mtype].length}];")
332 self.header
.add_decl
("\};")
334 # extern const struct fst_table_X fst_table_X
335 self.header
.add_decl
("extern const struct fts_table_{c_name} fts_table_{c_name};")
336 self.header
.add_decl
("struct fts_table_{c_name} \{")
337 self.header
.add_decl
("struct type *fts[{self.ft_tables[mtype.mclass].length}];")
338 self.header
.add_decl
("\};")
340 # const struct type_X
341 v
.add_decl
("const struct type_{c_name} type_{c_name} = \{")
342 v
.add_decl
("{self.typeids[mtype]},")
343 v
.add_decl
("{self.type_colors[mtype]},")
344 v
.add_decl
("&fts_table_{c_name},")
346 for stype
in self.type_tables
[mtype
] do
347 if stype
== null then
348 v
.add_decl
("-1, /* empty */")
350 v
.add_decl
("{self.typeids[stype]}, /* {stype} */")
356 # const struct fst_table_X fst_table_X
357 v
.add_decl
("const struct fts_table_{c_name} fts_table_{c_name} = \{")
360 if mtype
isa MGenericType then
361 for ft
in self.ft_tables
[mtype
.mclass
] do
363 v
.add_decl
("NULL, /* empty */")
367 if ft
.mclass
== mtype
.mclass
then
368 ntype
= mtype
.arguments
[ft
.rank
]
370 ntype
= ft
.anchor_to
(self.mainmodule
, mtype
)
372 if ntype
isa MNullableType then ntype
= ntype
.mtype
373 var ftype
= ntype
.as(MClassType)
374 if self.typeids
.has_key
(ftype
) then
375 v
.add_decl
("(struct type*)&type_{ftype.c_name}, /* {ft} ({ftype}) */")
377 v
.add_decl
("NULL, /* empty ({ft} not a live type) */")
387 # Globally compile the table of the class mclass
388 # In a link-time optimisation compiler, tables are globally computed
389 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
390 fun compile_class_to_c
(mclass
: MClass)
392 var mtype
= mclass
.intro
.bound_mtype
393 var c_name
= mclass
.c_name
395 var vft
= self.method_tables
[mclass
]
396 var attrs
= self.attr_tables
[mclass
]
397 var v
= new SeparateCompilerVisitor(self)
399 v
.add_decl
("/* runtime class {c_name} */")
400 var idnum
= classids
.length
401 var idname
= "ID_" + c_name
402 self.classids
[mtype
] = idname
403 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
405 self.header
.add_decl
("struct class_{c_name} \{")
406 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
408 if mtype
.ctype
!= "val*" then
409 # Is the Nit type is native then the struct is a box with two fields:
410 # * the `vft` to be polymorph
411 # * the `value` that contains the native value.
412 self.header
.add_decl
("{mtype.ctype} value;")
415 # Collect all attributes and associate them a field in the structure.
416 # Note: we do not try to optimize the order and helps CC to optimize the client code.
417 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
418 for p
in cd
.intro_mproperties
do
419 if not p
isa MAttribute then continue
420 var t
= p
.intro
.static_mtype
.as(not null)
421 t
= t
.anchor_to
(self.mainmodule
, mtype
)
422 self.header
.add_decl
("{t.ctype} {p.intro.c_name}; /* {p}: {t} */")
425 self.header
.add_decl
("\};")
428 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
429 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
431 for i
in [0 .. vft
.length
[ do
432 var mpropdef
= vft
[i
]
433 if mpropdef
== null then
434 v
.add_decl
("NULL, /* empty */")
436 if mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
437 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
439 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
446 if mtype
.ctype
!= "val*" then
447 #Build instance struct
448 self.header
.add_decl
("struct instance_{c_name} \{")
449 self.header
.add_decl
("const struct type *type;")
450 self.header
.add_decl
("const struct class *class;")
451 self.header
.add_decl
("{mtype.ctype} value;")
452 self.header
.add_decl
("\};")
454 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype}, struct type*);")
455 v
.add_decl
("/* allocate {mtype} */")
456 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value, struct type* type) \{")
457 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
458 v
.add
("res->type = type;")
459 v
.add
("res->class = (struct class*) &class_{c_name};")
460 v
.add
("res->value = value;")
461 v
.add
("return (val*)res;")
466 #Build instance struct
467 v
.add_decl
("struct instance_{c_name} \{")
468 v
.add_decl
("const struct type *type;")
469 v
.add_decl
("const struct class *class;")
470 v
.add_decl
("nitattribute_t attrs[{attrs.length}];")
474 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}(struct type* type);")
475 v
.add_decl
("/* allocate {mtype} */")
476 v
.add_decl
("{mtype.ctype} NEW_{c_name}(struct type* type) \{")
477 var res
= v
.new_named_var
(mtype
, "self")
479 v
.add
("{res} = calloc(sizeof(struct instance_{c_name}), 1);")
480 v
.add
("{res}->type = type;")
481 v
.add
("{res}->class = (struct class*) &class_{c_name};")
483 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
485 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
486 for npropdef
in n
.n_propdefs
do
487 if npropdef
isa AAttrPropdef then
488 npropdef
.init_expr
(v
, res
)
492 v
.add
("return {res};")
497 # The C function associated to a methoddef separately compiled
498 class SeparateRuntimeFunction
499 super AbstractRuntimeFunction
501 redef fun build_c_name
: String
503 return "{mmethoddef.c_name}"
506 redef fun to_s
do return self.mmethoddef
.to_s
508 redef fun compile_to_c
(compiler
)
510 var mmethoddef
= self.mmethoddef
512 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
513 var v
= new SeparateCompilerVisitor(compiler
)
514 var selfvar
= new RuntimeVariable("self", recv
, recv
)
515 var arguments
= new Array[RuntimeVariable]
516 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
520 var comment
= new Buffer
521 var ret
= mmethoddef
.msignature
.return_mtype
523 ret
= v
.resolve_for
(ret
, selfvar
)
524 sig
.append
("{ret.ctype} ")
525 else if mmethoddef
.mproperty
.is_new
then
527 sig
.append
("{ret.ctype} ")
531 sig
.append
(self.c_name
)
532 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
533 comment
.append
("(self: {selfvar}")
534 arguments
.add
(selfvar
)
535 for i
in [0..mmethoddef
.msignature
.arity
[ do
536 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
537 if i
== mmethoddef
.msignature
.vararg_rank
then
538 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
540 mtype
= v
.resolve_for
(mtype
, selfvar
)
541 comment
.append
(", {mtype}")
542 sig
.append
(", {mtype.ctype} p{i}")
543 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
544 arguments
.add
(argvar
)
549 comment
.append
(": {ret}")
551 compiler
.header
.add_decl
("{sig};")
553 v
.add_decl
("/* method {self} for {comment} */")
554 v
.add_decl
("{sig} \{")
556 frame
.returnvar
= v
.new_var
(ret
)
558 frame
.returnlabel
= v
.get_name
("RET_LABEL")
560 if recv
!= arguments
.first
.mtype
then
561 #print "{self} {recv} {arguments.first}"
563 mmethoddef
.compile_inside_to_c
(v
, arguments
)
565 v
.add
("{frame.returnlabel.as(not null)}:;")
567 v
.add
("return {frame.returnvar.as(not null)};")
573 # The C function associated to a methoddef on a primitive type, stored into a VFT of a class
574 # The first parameter (the reciever) is always typed by val* in order to accept an object value
575 class VirtualRuntimeFunction
576 super AbstractRuntimeFunction
578 redef fun build_c_name
: String
580 return "VIRTUAL_{mmethoddef.c_name}"
583 redef fun to_s
do return self.mmethoddef
.to_s
585 redef fun compile_to_c
(compiler
)
587 var mmethoddef
= self.mmethoddef
589 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
590 var v
= new SeparateCompilerVisitor(compiler
)
591 var selfvar
= new RuntimeVariable("self", v
.object_type
, recv
)
592 var arguments
= new Array[RuntimeVariable]
593 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
597 var comment
= new Buffer
599 # Because the function is virtual, the signature must match the one of the original class
600 var intromclassdef
= self.mmethoddef
.mproperty
.intro
.mclassdef
601 var msignature
= mmethoddef
.mproperty
.intro
.msignature
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
602 var ret
= msignature
.return_mtype
604 sig
.append
("{ret.ctype} ")
605 else if mmethoddef
.mproperty
.is_new
then
607 sig
.append
("{ret.ctype} ")
611 sig
.append
(self.c_name
)
612 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
613 comment
.append
("(self: {selfvar}")
614 arguments
.add
(selfvar
)
615 for i
in [0..msignature
.arity
[ do
616 var mtype
= msignature
.mparameters
[i
].mtype
617 if i
== msignature
.vararg_rank
then
618 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
620 comment
.append
(", {mtype}")
621 sig
.append
(", {mtype.ctype} p{i}")
622 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
623 arguments
.add
(argvar
)
628 comment
.append
(": {ret}")
630 compiler
.header
.add_decl
("{sig};")
632 v
.add_decl
("/* method {self} for {comment} */")
633 v
.add_decl
("{sig} \{")
635 frame
.returnvar
= v
.new_var
(ret
)
637 frame
.returnlabel
= v
.get_name
("RET_LABEL")
639 if recv
!= arguments
.first
.mtype
then
640 #print "{self} {recv} {arguments.first}"
642 mmethoddef
.compile_inside_to_c
(v
, arguments
)
644 v
.add
("{frame.returnlabel.as(not null)}:;")
646 v
.add
("return {frame.returnvar.as(not null)};")
651 redef fun call
(v
, arguments
)
658 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
659 class SeparateCompilerVisitor
660 super GlobalCompilerVisitor # TODO better separation of concerns
662 redef fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
664 var recv
= args
.first
665 if recv
.mtype
.ctype
!= m
.mclassdef
.mclass
.mclass_type
.ctype
then
666 args
.first
= self.autobox
(args
.first
, m
.mclassdef
.mclass
.mclass_type
)
668 for i
in [0..m
.msignature
.arity
[ do
669 var t
= m
.msignature
.mparameters
[i
].mtype
670 if i
== m
.msignature
.vararg_rank
then
673 t
= self.resolve_for
(t
, recv
)
674 args
[i
+1] = self.autobox
(args
[i
+1], t
)
678 # Box or unbox a value to another type iff a C type conversion is needed
679 # ENSURE: result.mtype.ctype == mtype.ctype
680 redef fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
682 if value
.mtype
.ctype
== mtype
.ctype
then
684 else if value
.mtype
.ctype
== "val*" then
685 return self.new_expr
("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype
)
686 else if mtype
.ctype
== "val*" then
687 var valtype
= value
.mtype
.as(MClassType)
688 var res
= self.new_var
(mtype
)
689 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
690 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
691 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
694 var totype
= value
.mtype
695 if totype
isa MNullableType then totype
= totype
.mtype
696 self.add
("{res} = BOX_{valtype.c_name}({value}, (struct type*) &type_{totype.c_name}); /* autobox from {value.mtype} to {mtype} */")
699 # Bad things will appen!
700 var res
= self.new_var
(mtype
)
701 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
702 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
707 redef fun send
(mmethod
, arguments
)
709 if arguments
.first
.mtype
.ctype
!= "val*" then
710 assert arguments
.first
.mtype
== arguments
.first
.mcasttype
711 return self.monomorphic_send
(mmethod
, arguments
.first
.mtype
, arguments
)
714 var res
: nullable RuntimeVariable
715 var msignature
= mmethod
.intro
.msignature
.resolve_for
(mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.mmodule
, true)
716 var ret
= msignature
.return_mtype
717 if mmethod
.is_new
then
718 ret
= arguments
.first
.mtype
719 res
= self.new_var
(ret
)
720 else if ret
== null then
723 ret
= self.resolve_for
(ret
, arguments
.first
)
724 res
= self.new_var
(ret
)
730 var recv
= arguments
.first
733 for i
in [0..msignature
.arity
[ do
734 var a
= arguments
[i
+1]
735 var t
= msignature
.mparameters
[i
].mtype
736 s
.append
(", {t.ctype}")
737 a
= self.autobox
(a
, t
)
741 var maybenull
= recv
.mcasttype
isa MNullableType
743 self.add
("if ({recv} == NULL) \{")
744 if mmethod
.name
== "==" then
746 var arg
= arguments
[1]
747 if arg
.mcasttype
isa MNullableType then
748 self.add
("{res} = ({arg} == NULL);")
749 else if arg
.mcasttype
isa MNullType then
750 self.add
("{res} = 1; /* is null */")
752 self.add
("{res} = 0; /* {arg.inspect} cannot be null */")
754 else if mmethod
.name
== "!=" then
756 var arg
= arguments
[1]
757 if arg
.mcasttype
isa MNullableType then
758 self.add
("{res} = ({arg} != NULL);")
759 else if arg
.mcasttype
isa MNullType then
760 self.add
("{res} = 0; /* is null */")
762 self.add
("{res} = 1; /* {arg.inspect} cannot be null */")
765 self.add_abort
("Reciever is null")
767 self.add
("\} else \{")
770 var color
= self.compiler
.as(SeparateCompiler).method_colors
[mmethod
]
772 if ret
== null then r
= "void" else r
= ret
.ctype
773 var call
= "(({r} (*)({s}))({arguments.first}->class->vft[{color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
776 self.add
("{res} = {call};")
788 redef fun call
(mmethoddef
, recvtype
, arguments
)
790 var res
: nullable RuntimeVariable
791 var ret
= mmethoddef
.msignature
.return_mtype
792 if mmethoddef
.mproperty
.is_new
then
793 ret
= arguments
.first
.mtype
794 res
= self.new_var
(ret
)
795 else if ret
== null then
798 ret
= self.resolve_for
(ret
, arguments
.first
)
799 res
= self.new_var
(ret
)
803 self.adapt_signature
(mmethoddef
, arguments
)
806 self.add
("{mmethoddef.c_name}({arguments.join(", ")});")
809 self.add
("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
815 redef fun isset_attribute
(a
, recv
)
817 # FIXME: Here we inconditionally return boxed primitive attributes
818 var res
= self.new_var
(bool_type
)
819 self.add
("{res} = {recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}] != NULL; /* {a} on {recv.inspect}*/")
823 redef fun read_attribute
(a
, recv
)
825 # FIXME: Here we inconditionally return boxed primitive attributes
826 var ret
= a
.intro
.static_mtype
.as(not null)
827 ret
= self.resolve_for
(ret
, recv
)
828 var cret
= self.object_type
.as_nullable
829 var res
= self.new_var
(cret
)
831 self.add
("{res} = {recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}]; /* {a} on {recv.inspect} */")
832 if not ret
isa MNullableType then
833 self.add
("if ({res} == NULL) \{")
834 self.add_abort
("Uninitialized attribute {a.name}")
841 redef fun write_attribute
(a
, recv
, value
)
843 # FIXME: Here we inconditionally box primitive attributes
844 value
= self.autobox
(value
, self.object_type
.as_nullable
)
845 self.add
("{recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}] = {value}; /* {a} on {recv.inspect} */")
848 redef fun init_instance
(mtype
)
850 var compiler
= self.compiler
.as(SeparateCompiler)
851 if mtype
isa MGenericType and mtype
.need_anchor
then
852 var buff
= new Buffer
853 for ft
in mtype
.mclass
.mclass_type
.arguments
do
854 var ftcolor
= compiler
.ft_colors
[ft
.as(MParameterType)]
855 buff
.append
("[self->type->fts_table->fts[{ftcolor}]->id]")
857 mtype
= self.anchor
(mtype
).as(MClassType)
858 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) livetypes_{mtype.mclass.c_name}{buff.to_s})", mtype
)
860 compiler
.undead_types
.add
(mtype
)
861 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) &type_{mtype.c_name})", mtype
)
864 redef fun type_test
(value
, mtype
)
866 var compiler
= self.compiler
.as(SeparateCompiler)
867 var res
= self.new_var
(bool_type
)
868 var buff
= new Buffer
870 if mtype
isa MNullableType then mtype
= mtype
.mtype
871 if mtype
isa MGenericType and mtype
.need_anchor
then
872 for ft
in mtype
.mclass
.mclass_type
.arguments
do
873 var ftcolor
= compiler
.ft_colors
[ft
.as(MParameterType)]
874 buff
.append
("[self->type->fts_table->fts[{ftcolor}]->id]")
876 self.add
("{res} = {value}->type->type_table[livetypes_{mtype.mclass.c_name}{buff.to_s}->color] == livetypes_{mtype.mclass.c_name}{buff.to_s}->id;")
877 else if mtype
isa MClassType then
878 compiler
.undead_types
.add
(mtype
)
879 self.add
("{res} = {value}->type->type_table[type_{mtype.c_name}.color] == type_{mtype.c_name}.id;")
880 else if mtype
isa MParameterType then
881 var ftcolor
= compiler
.ft_colors
[mtype
]
882 self.add
("{res} = {value}->type->type_table[self->type->fts_table->fts[{ftcolor}]->color] == self->type->fts_table->fts[{ftcolor}]->id;")
888 redef fun is_same_type_test
(value1
, value2
)
890 var res
= self.new_var
(bool_type
)
892 add
("printf(\"NOT YET IMPLEMENTED: is_same_type
(%s
,%s
).\\n\
", \"{value1.inspect}\
", \"{value2.inspect}\
"); exit(1);")
896 redef fun class_name_string
(value1
)
898 var res
= self.get_name
("var_class_name")
899 self.add_decl
("const char* {res};")
901 add
("printf(\"NOT YET IMPLEMENTED: class_name_string
(%s
).\\n\
", \"{value1.inspect}\
"); exit(1);")
905 redef fun equal_test
(value1
, value2
)
907 var res
= self.new_var
(bool_type
)
908 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
913 if value1
.mtype
.ctype
!= "val*" then
914 if value2
.mtype
.ctype
== value1
.mtype
.ctype
then
915 self.add
("{res} = {value1} == {value2};")
916 else if value2
.mtype
.ctype
!= "val*" then
917 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
919 var mtype1
= value1
.mtype
.as(MClassType)
920 self.add
("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name});")
921 self.add
("if ({res}) \{")
922 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
926 self.add
("{res} = {value1} == {value2};")
933 # Return the name of the C structure associated to a Nit class
934 fun c_name
: String do
935 var res
= self.c_name_cache
936 if res
!= null then return res
937 res
= "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
938 self.c_name_cache
= res
941 private var c_name_cache
: nullable String