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
= compiler
.new_visitor
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 table_size; 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 # Declare global instances
67 v
.add_decl
("extern int glob_argc;")
68 v
.add_decl
("extern char **glob_argv;")
69 v
.add_decl
("extern val *glob_sys;")
71 # The main function of the C
72 compiler
.compile_main_function
74 # compile class structures
75 for m
in mainmodule
.in_importation
.greaters
do
76 for mclass
in m
.intro_mclasses
do
77 compiler
.compile_class_to_c
(mclass
)
82 for m
in mainmodule
.in_importation
.greaters
do
83 compiler
.compile_module_to_c
(m
)
86 # compile live & cast type structures
87 var mtypes
= compiler
.do_global_type_coloring
89 compiler
.compile_type_to_c
(t
)
92 # compile live generic types selection structures
93 for mclass
in model
.mclasses
do
94 compiler
.compile_live_gentype_to_c
(mclass
)
97 # for the class_name and output_class_name methods
98 compiler
.compile_class_names
100 write_and_make
(compiler
)
104 # Singleton that store the knowledge about the separate compilation process
105 class SeparateCompiler
106 super GlobalCompiler # TODO better separation of concerns
108 private var undead_types
: Set[MClassType] = new HashSet[MClassType]
109 protected var typeids
: HashMap[MClassType, Int] protected writable = new HashMap[MClassType, Int]
111 private var type_colors
: Map[MClassType, Int] = typeids
112 private var type_tables
: nullable Map[MClassType, Array[nullable MClassType]] = null
113 private var livetypes_tables
: nullable Map[MClass, Array[nullable Object]]
114 private var livetypes_tables_sizes
: nullable Map[MClass, Array[Int]]
116 protected var class_colors
: Map[MClass, Int] protected writable
118 protected var method_colors
: Map[MMethod, Int] protected writable
119 protected var method_tables
: Map[MClass, Array[nullable MMethodDef]] protected writable
121 protected var attr_colors
: Map[MAttribute, Int] protected writable
122 protected var attr_tables
: Map[MClass, Array[nullable MAttributeDef]] protected writable
124 private var ft_colors
: Map[MParameterType, Int]
125 private var ft_tables
: Map[MClass, Array[nullable MParameterType]]
127 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
129 var class_coloring
= new ClassColoring(mainmodule
)
130 self.class_colors
= class_coloring
.colorize
(mmbuilder
.model
.mclasses
)
133 var method_coloring
= new MethodColoring(class_coloring
)
134 self.method_colors
= method_coloring
.colorize
135 self.method_tables
= method_coloring
.build_property_tables
137 # attributes coloration
138 var attribute_coloring
= new AttributeColoring(class_coloring
)
139 self.attr_colors
= attribute_coloring
.colorize
140 self.attr_tables
= attribute_coloring
.build_property_tables
143 var ft_coloring
= new FTColoring(class_coloring
)
144 self.ft_colors
= ft_coloring
.colorize
145 self.ft_tables
= ft_coloring
.build_ft_tables
148 protected fun compile_class_names
do
150 # Build type names table
151 var type_array
= new Array[nullable MClassType]
152 for t
, i
in typeids
do
153 if i
>= type_array
.length
then
159 var v
= self.new_visitor
160 self.header
.add_decl
("extern const char const * class_names[];")
161 v
.add
("const char const * class_names[] = \{")
162 for t
in type_array
do
172 # colorize live types of the program
173 private fun do_global_type_coloring
: Set[MClassType] do
174 var mtypes
= new HashSet[MClassType]
175 #print "undead types:"
176 #for t in self.undead_types do
180 #for t in runtime_type_analysis.live_types do
184 #for t in runtime_type_analysis.live_cast_types do
188 mtypes
.add_all
(self.runtime_type_analysis
.live_types
)
189 mtypes
.add_all
(self.runtime_type_analysis
.live_cast_types
)
190 mtypes
.add_all
(self.undead_types
)
192 for mtype
in self.runtime_type_analysis
.live_types
do
193 # add formal types arguments to mtypes
194 if mtype
isa MGenericType then
195 for ft
in mtype
.arguments
do
196 if ft
isa MNullableType then ft
= ft
.mtype
197 if ft
.need_anchor
then
198 print
("Why do we need anchor here ?")
201 mtypes
.add
(ft
.as(MClassType))
207 for mtype
in mtypes
do
208 self.typeids
[mtype
] = self.typeids
.length
211 # build livetypes tables
212 self.livetypes_tables
= new HashMap[MClass, Array[nullable Object]]
213 self.livetypes_tables_sizes
= new HashMap[MClass, Array[Int]]
214 for mtype
in self.runtime_type_analysis
.live_types
do
215 if mtype
isa MGenericType then
216 var table
: Array[nullable Object]
217 var sizes
: Array[Int]
218 if livetypes_tables
.has_key
(mtype
.mclass
) then
219 table
= livetypes_tables
[mtype
.mclass
]
221 table
= new Array[nullable Object]
222 self.livetypes_tables
[mtype
.mclass
] = table
224 if livetypes_tables_sizes
.has_key
(mtype
.mclass
) then
225 sizes
= livetypes_tables_sizes
[mtype
.mclass
]
227 sizes
= new Array[Int]
228 self.livetypes_tables_sizes
[mtype
.mclass
] = sizes
230 build_livetype_table
(mtype
, 0, table
, sizes
)
235 var type_coloring
= new TypeColoring(self.mainmodule
, self.runtime_type_analysis
)
236 self.type_colors
= type_coloring
.colorize
(mtypes
)
237 self.type_tables
= type_coloring
.build_type_tables
(mtypes
, type_colors
)
242 # build live gentype table recursively
243 private fun build_livetype_table
(mtype
: MGenericType, current_rank
: Int, table
: Array[nullable Object], sizes
: Array[Int]) do
244 var ft
= mtype
.arguments
[current_rank
]
245 if ft
isa MNullableType then ft
= ft
.mtype
246 var id
= self.typeids
[ft
.as(MClassType)]
248 if current_rank
>= sizes
.length
then
249 sizes
[current_rank
] = id
+ 1
250 else if id
>= sizes
[current_rank
] then
251 sizes
[current_rank
] = id
+ 1
254 if id
> table
.length
then
255 for i
in [table
.length
.. id
[ do table
[i
] = null
258 if current_rank
== mtype
.arguments
.length
- 1 then
261 var ft_table
: Array[nullable Object]
262 if id
< table
.length
and table
[id
] != null then
263 ft_table
= table
[id
].as(Array[nullable Object])
265 ft_table
= new Array[nullable Object]
268 build_livetype_table
(mtype
, current_rank
+ 1, ft_table
, sizes
)
272 private fun add_to_livetypes_table
(table
: Array[nullable Object], ft
: MClassType) do
273 var id
= self.typeids
[ft
]
274 for i
in [table
.length
.. id
[ do
280 private fun compile_livetype_table
(table
: Array[nullable Object], buffer
: Buffer, depth
: Int, max
: Int) do
284 buffer
.append
("NULL,\n")
286 buffer
.append
("\{\},\n")
288 else if obj
isa MClassType then
289 buffer
.append
("(struct type*) &type_{obj.c_name}, /* {obj} */\n")
290 else if obj
isa Array[nullable Object] then
291 buffer
.append
("\{\n")
292 compile_livetype_table
(obj
, buffer
, depth
+ 1, max
)
293 buffer
.append
("\},\n")
298 # declare live generic types tables selection
299 private fun compile_live_gentype_to_c
(mclass
: MClass) do
300 if mclass
.arity
> 0 then
301 if self.livetypes_tables
.has_key
(mclass
) then
302 var table
= self.livetypes_tables
[mclass
]
303 var sign
= self.livetypes_tables_sizes
[mclass
]
304 var table_buffer
= new Buffer.from
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}] = \{\n")
305 compile_livetype_table
(table
, table_buffer
, 1, mclass
.arity
)
306 table_buffer
.append
("\};")
308 var v
= new SeparateCompilerVisitor(self)
309 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
310 v
.add_decl
(table_buffer
.to_s
)
312 var sign
= new Array[Int].filled_with
(0, mclass
.arity
)
313 var v
= new SeparateCompilerVisitor(self)
314 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
315 v
.add_decl
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
320 # Separately compile all the method definitions of the module
321 fun compile_module_to_c
(mmodule
: MModule)
323 for cd
in mmodule
.mclassdefs
do
324 for pd
in cd
.mpropdefs
do
325 if not pd
isa MMethodDef then continue
326 #print "compile {pd} @ {cd} @ {mmodule}"
327 var r
= new SeparateRuntimeFunction(pd
)
329 if true or cd
.bound_mtype
.ctype
!= "val*" then
330 var r2
= new VirtualRuntimeFunction(pd
)
331 r2
.compile_to_c
(self)
337 # Globaly compile the type structure of a live type
338 fun compile_type_to_c
(mtype
: MClassType)
340 var c_name
= mtype
.c_name
341 var v
= new SeparateCompilerVisitor(self)
342 v
.add_decl
("/* runtime type {mtype} */")
344 # extern const struct type_X
345 self.header
.add_decl
("extern const struct type_{c_name} type_{c_name};")
346 self.header
.add_decl
("struct type_{c_name} \{")
347 self.header
.add_decl
("int id;")
348 self.header
.add_decl
("int color;")
349 self.header
.add_decl
("const struct fts_table_{c_name} *fts_table;")
350 self.header
.add_decl
("int table_size;")
351 self.header
.add_decl
("int type_table[{self.type_tables[mtype].length}];")
352 self.header
.add_decl
("\};")
354 # extern const struct fst_table_X fst_table_X
355 self.header
.add_decl
("extern const struct fts_table_{c_name} fts_table_{c_name};")
356 self.header
.add_decl
("struct fts_table_{c_name} \{")
357 self.header
.add_decl
("struct type *fts[{self.ft_tables[mtype.mclass].length}];")
358 self.header
.add_decl
("\};")
360 # const struct type_X
361 v
.add_decl
("const struct type_{c_name} type_{c_name} = \{")
362 v
.add_decl
("{self.typeids[mtype]},")
363 v
.add_decl
("{self.type_colors[mtype]},")
364 v
.add_decl
("&fts_table_{c_name},")
365 v
.add_decl
("{self.type_tables[mtype].length},")
367 for stype
in self.type_tables
[mtype
] do
368 if stype
== null then
369 v
.add_decl
("-1, /* empty */")
371 v
.add_decl
("{self.typeids[stype]}, /* {stype} */")
377 build_fts_table
(mtype
, v
)
380 # const struct fst_table_X fst_table_X
381 private fun build_fts_table
(mtype
: MClassType, v
: SeparateCompilerVisitor) do
382 v
.add_decl
("const struct fts_table_{mtype.c_name} fts_table_{mtype.c_name} = \{")
384 for ft
in self.ft_tables
[mtype
.mclass
] do
386 v
.add_decl
("NULL, /* empty */")
389 if ft
.mclass
== mtype
.mclass
then
390 ntype
= mtype
.arguments
[ft
.rank
]
392 ntype
= ft
.anchor_to
(self.mainmodule
, mtype
)
394 if ntype
isa MNullableType then ntype
= ntype
.mtype
395 var ftype
= ntype
.as(MClassType)
396 if self.typeids
.has_key
(ftype
) then
397 v
.add_decl
("(struct type*)&type_{ftype.c_name}, /* {ft} ({ftype}) */")
399 v
.add_decl
("NULL, /* empty ({ft} not a live type) */")
407 # Globally compile the table of the class mclass
408 # In a link-time optimisation compiler, tables are globally computed
409 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
410 fun compile_class_to_c
(mclass
: MClass)
412 var mtype
= mclass
.intro
.bound_mtype
413 var c_name
= mclass
.c_name
415 var vft
= self.method_tables
[mclass
]
416 var attrs
= self.attr_tables
[mclass
]
417 var v
= new SeparateCompilerVisitor(self)
419 v
.add_decl
("/* runtime class {c_name} */")
420 var idnum
= classids
.length
421 var idname
= "ID_" + c_name
422 self.classids
[mtype
] = idname
423 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
425 self.header
.add_decl
("struct class_{c_name} \{")
426 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
427 self.header
.add_decl
("\};")
430 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
431 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
433 for i
in [0 .. vft
.length
[ do
434 var mpropdef
= vft
[i
]
435 if mpropdef
== null then
436 v
.add_decl
("NULL, /* empty */")
438 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
439 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
441 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
448 if mtype
.ctype
!= "val*" then
449 #Build instance struct
450 self.header
.add_decl
("struct instance_{c_name} \{")
451 self.header
.add_decl
("const struct type *type;")
452 self.header
.add_decl
("const struct class *class;")
453 self.header
.add_decl
("{mtype.ctype} value;")
454 self.header
.add_decl
("\};")
456 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype}, struct type*);")
457 v
.add_decl
("/* allocate {mtype} */")
458 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value, struct type* type) \{")
459 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
460 v
.add
("res->type = type;")
461 v
.add
("res->class = (struct class*) &class_{c_name};")
462 v
.add
("res->value = value;")
463 v
.add
("return (val*)res;")
468 var is_native_array
= mclass
.name
== "NativeArray"
471 if is_native_array
then
472 sig
= "int length, struct type* type"
474 sig
= "struct type* type"
477 #Build instance struct
478 #extern const struct instance_array__NativeArray instance_array__NativeArray;
479 self.header
.add_decl
("struct instance_{c_name} \{")
480 self.header
.add_decl
("const struct type *type;")
481 self.header
.add_decl
("const struct class *class;")
482 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
483 if is_native_array
then
484 # NativeArrays are just a instance header followed by an array of values
485 self.header
.add_decl
("val* values[0];")
487 self.header
.add_decl
("\};")
490 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
491 v
.add_decl
("/* allocate {mtype} */")
492 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
493 var res
= v
.new_named_var
(mtype
, "self")
495 if is_native_array
then
496 var mtype_elt
= mtype
.arguments
.first
497 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
499 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
501 #v.add("{res} = calloc(sizeof(struct instance_{c_name}), 1);")
502 v
.add
("{res}->type = type;")
503 v
.add
("{res}->class = (struct class*) &class_{c_name};")
505 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
507 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
508 for npropdef
in n
.n_propdefs
do
509 if npropdef
isa AAttrPropdef then
510 npropdef
.init_expr
(v
, res
)
514 v
.add
("return {res};")
518 redef fun new_visitor
do return new SeparateCompilerVisitor(self)
521 # The C function associated to a methoddef separately compiled
522 class SeparateRuntimeFunction
523 super AbstractRuntimeFunction
525 redef fun build_c_name
: String
527 return "{mmethoddef.c_name}"
530 redef fun to_s
do return self.mmethoddef
.to_s
532 redef fun compile_to_c
(compiler
)
534 var mmethoddef
= self.mmethoddef
536 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
537 var v
= compiler
.new_visitor
538 var selfvar
= new RuntimeVariable("self", recv
, recv
)
539 var arguments
= new Array[RuntimeVariable]
540 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
543 var msignature
= mmethoddef
.msignature
.resolve_for
(mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.mmodule
, true)
546 var comment
= new Buffer
547 var ret
= msignature
.return_mtype
549 sig
.append
("{ret.ctype} ")
550 else if mmethoddef
.mproperty
.is_new
then
552 sig
.append
("{ret.ctype} ")
556 sig
.append
(self.c_name
)
557 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
558 comment
.append
("(self: {selfvar}")
559 arguments
.add
(selfvar
)
560 for i
in [0..msignature
.arity
[ do
561 var mtype
= msignature
.mparameters
[i
].mtype
562 if i
== msignature
.vararg_rank
then
563 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
565 comment
.append
(", {mtype}")
566 sig
.append
(", {mtype.ctype} p{i}")
567 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
568 arguments
.add
(argvar
)
573 comment
.append
(": {ret}")
575 compiler
.header
.add_decl
("{sig};")
577 v
.add_decl
("/* method {self} for {comment} */")
578 v
.add_decl
("{sig} \{")
580 frame
.returnvar
= v
.new_var
(ret
)
582 frame
.returnlabel
= v
.get_name
("RET_LABEL")
584 if recv
!= arguments
.first
.mtype
then
585 #print "{self} {recv} {arguments.first}"
587 mmethoddef
.compile_inside_to_c
(v
, arguments
)
589 v
.add
("{frame.returnlabel.as(not null)}:;")
591 v
.add
("return {frame.returnvar.as(not null)};")
597 # The C function associated to a methoddef on a primitive type, stored into a VFT of a class
598 # The first parameter (the reciever) is always typed by val* in order to accept an object value
599 class VirtualRuntimeFunction
600 super AbstractRuntimeFunction
602 redef fun build_c_name
: String
604 return "VIRTUAL_{mmethoddef.c_name}"
607 redef fun to_s
do return self.mmethoddef
.to_s
609 redef fun compile_to_c
(compiler
)
611 var mmethoddef
= self.mmethoddef
613 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
614 var v
= compiler
.new_visitor
615 var selfvar
= new RuntimeVariable("self", v
.object_type
, recv
)
616 var arguments
= new Array[RuntimeVariable]
617 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
621 var comment
= new Buffer
623 # Because the function is virtual, the signature must match the one of the original class
624 var intromclassdef
= self.mmethoddef
.mproperty
.intro
.mclassdef
625 var msignature
= mmethoddef
.mproperty
.intro
.msignature
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
626 var ret
= msignature
.return_mtype
628 sig
.append
("{ret.ctype} ")
629 else if mmethoddef
.mproperty
.is_new
then
631 sig
.append
("{ret.ctype} ")
635 sig
.append
(self.c_name
)
636 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
637 comment
.append
("(self: {selfvar}")
638 arguments
.add
(selfvar
)
639 for i
in [0..msignature
.arity
[ do
640 var mtype
= msignature
.mparameters
[i
].mtype
641 if i
== msignature
.vararg_rank
then
642 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
644 comment
.append
(", {mtype}")
645 sig
.append
(", {mtype.ctype} p{i}")
646 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
647 arguments
.add
(argvar
)
652 comment
.append
(": {ret}")
654 compiler
.header
.add_decl
("{sig};")
656 v
.add_decl
("/* method {self} for {comment} */")
657 v
.add_decl
("{sig} \{")
659 frame
.returnvar
= v
.new_var
(ret
)
661 frame
.returnlabel
= v
.get_name
("RET_LABEL")
663 if recv
!= arguments
.first
.mtype
then
664 #print "{self} {recv} {arguments.first}"
666 mmethoddef
.compile_inside_to_c
(v
, arguments
)
668 v
.add
("{frame.returnlabel.as(not null)}:;")
670 v
.add
("return {frame.returnvar.as(not null)};")
675 redef fun call
(v
, arguments
)
682 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
683 class SeparateCompilerVisitor
684 super GlobalCompilerVisitor # TODO better separation of concerns
686 redef fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
688 var msignature
= m
.msignature
.resolve_for
(m
.mclassdef
.bound_mtype
, m
.mclassdef
.bound_mtype
, m
.mclassdef
.mmodule
, true)
689 var recv
= args
.first
690 if recv
.mtype
.ctype
!= m
.mclassdef
.mclass
.mclass_type
.ctype
then
691 args
.first
= self.autobox
(args
.first
, m
.mclassdef
.mclass
.mclass_type
)
693 for i
in [0..msignature
.arity
[ do
694 var t
= msignature
.mparameters
[i
].mtype
695 if i
== msignature
.vararg_rank
then
698 args
[i
+1] = self.autobox
(args
[i
+1], t
)
702 # Box or unbox a value to another type iff a C type conversion is needed
703 # ENSURE: result.mtype.ctype == mtype.ctype
704 redef fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
706 if value
.mtype
.ctype
== mtype
.ctype
then
708 else if value
.mtype
.ctype
== "val*" then
709 return self.new_expr
("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype
)
710 else if mtype
.ctype
== "val*" then
711 var valtype
= value
.mtype
.as(MClassType)
712 var res
= self.new_var
(mtype
)
713 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
714 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
715 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
718 var totype
= value
.mtype
719 if totype
isa MNullableType then totype
= totype
.mtype
720 self.add
("{res} = BOX_{valtype.c_name}({value}, (struct type*) &type_{totype.c_name}); /* autobox from {value.mtype} to {mtype} */")
723 # Bad things will appen!
724 var res
= self.new_var
(mtype
)
725 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
726 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
731 redef fun send
(mmethod
, arguments
)
733 if arguments
.first
.mtype
.ctype
!= "val*" then
734 assert arguments
.first
.mtype
== arguments
.first
.mcasttype
735 return self.monomorphic_send
(mmethod
, arguments
.first
.mtype
, arguments
)
738 var res
: nullable RuntimeVariable
739 var msignature
= mmethod
.intro
.msignature
.resolve_for
(mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.mmodule
, true)
740 var ret
= msignature
.return_mtype
741 if mmethod
.is_new
then
742 ret
= arguments
.first
.mtype
743 res
= self.new_var
(ret
)
744 else if ret
== null then
747 res
= self.new_var
(ret
)
753 var recv
= arguments
.first
756 self.varargize
(msignature
, arguments
)
757 for i
in [0..msignature
.arity
[ do
758 var a
= arguments
[i
+1]
759 var t
= msignature
.mparameters
[i
].mtype
760 if i
== msignature
.vararg_rank
then
761 t
= arguments
[i
+1].mcasttype
763 s
.append
(", {t.ctype}")
764 a
= self.autobox
(a
, t
)
768 var maybenull
= recv
.mcasttype
isa MNullableType
770 self.add
("if ({recv} == NULL) \{")
771 if mmethod
.name
== "==" then
773 var arg
= arguments
[1]
774 if arg
.mcasttype
isa MNullableType then
775 self.add
("{res} = ({arg} == NULL);")
776 else if arg
.mcasttype
isa MNullType then
777 self.add
("{res} = 1; /* is null */")
779 self.add
("{res} = 0; /* {arg.inspect} cannot be null */")
781 else if mmethod
.name
== "!=" then
783 var arg
= arguments
[1]
784 if arg
.mcasttype
isa MNullableType then
785 self.add
("{res} = ({arg} != NULL);")
786 else if arg
.mcasttype
isa MNullType then
787 self.add
("{res} = 0; /* is null */")
789 self.add
("{res} = 1; /* {arg.inspect} cannot be null */")
792 self.add_abort
("Reciever is null")
794 self.add
("\} else \{")
797 var color
= self.compiler
.as(SeparateCompiler).method_colors
[mmethod
]
799 if ret
== null then r
= "void" else r
= ret
.ctype
800 var call
= "(({r} (*)({s}))({arguments.first}->class->vft[{color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
803 self.add
("{res} = {call};")
815 redef fun call
(mmethoddef
, recvtype
, arguments
)
817 var res
: nullable RuntimeVariable
818 var ret
= mmethoddef
.msignature
.return_mtype
819 if mmethoddef
.mproperty
.is_new
then
820 ret
= arguments
.first
.mtype
821 res
= self.new_var
(ret
)
822 else if ret
== null then
825 ret
= ret
.resolve_for
(mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.mmodule
, true)
826 res
= self.new_var
(ret
)
830 self.adapt_signature
(mmethoddef
, arguments
)
833 self.add
("{mmethoddef.c_name}({arguments.join(", ")});")
836 self.add
("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
842 redef fun isset_attribute
(a
, recv
)
844 # FIXME: Here we inconditionally return boxed primitive attributes
845 self.check_recv_notnull
(recv
)
846 var res
= self.new_var
(bool_type
)
847 self.add
("{res} = {recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}] != NULL; /* {a} on {recv.inspect}*/")
851 redef fun read_attribute
(a
, recv
)
853 # FIXME: Here we inconditionally return boxed primitive attributes
854 var ret
= a
.intro
.static_mtype
.as(not null)
855 ret
= self.resolve_for
(ret
, recv
)
856 var cret
= self.object_type
.as_nullable
857 var res
= self.new_var
(cret
)
860 self.check_recv_notnull
(recv
)
862 self.add
("{res} = {recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}]; /* {a} on {recv.inspect} */")
863 if not ret
isa MNullableType then
864 self.add
("if ({res} == NULL) \{")
865 self.add_abort
("Uninitialized attribute {a.name}")
872 redef fun write_attribute
(a
, recv
, value
)
874 # FIXME: Here we inconditionally box primitive attributes
875 self.check_recv_notnull
(recv
)
876 value
= self.autobox
(value
, self.object_type
.as_nullable
)
877 self.add
("{recv}->attrs[{self.compiler.as(SeparateCompiler).attr_colors[a]}] = {value}; /* {a} on {recv.inspect} */")
880 # Build livetype structure retrieving
881 #ENSURE: mtype.need_anchor
882 fun retrieve_anchored_livetype
(mtype
: MGenericType, buffer
: Buffer) do
883 assert mtype
.need_anchor
885 var recv
= self.frame
.arguments
.first
886 var recv_boxed
= self.autobox
(recv
, self.object_type
)
888 var compiler
= self.compiler
.as(SeparateCompiler)
889 for ft
in mtype
.arguments
do
890 if ft
isa MParameterType then
891 var ftcolor
= compiler
.ft_colors
[ft
]
892 buffer
.append
("[{recv_boxed}->type->fts_table->fts[{ftcolor}]->id]")
893 else if ft
isa MGenericType and ft
.need_anchor
then
894 var bbuff
= new Buffer
895 retrieve_anchored_livetype
(ft
, bbuff
)
896 buffer
.append
("[livetypes_{ft.mclass.c_name}{bbuff.to_s}->id]")
897 else if ft
isa MClassType then
898 var typecolor
= compiler
.type_colors
[ft
]
899 buffer
.append
("[{typecolor}]")
901 self.add
("printf(\"NOT YET IMPLEMENTED: init_instance
(%s
, {mtype}).\\n\
", \"{ft.inspect}\
"); exit(1);")
906 redef fun init_instance
(mtype
)
908 var compiler
= self.compiler
.as(SeparateCompiler)
909 if mtype
isa MGenericType and mtype
.need_anchor
then
910 var buff
= new Buffer
911 retrieve_anchored_livetype
(mtype
, buff
)
912 mtype
= self.anchor
(mtype
).as(MClassType)
913 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) livetypes_{mtype.mclass.c_name}{buff.to_s})", mtype
)
915 compiler
.undead_types
.add
(mtype
)
916 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) &type_{mtype.c_name})", mtype
)
919 redef fun type_test
(value
, mtype
)
921 var compiler
= self.compiler
.as(SeparateCompiler)
923 var recv
= self.frame
.arguments
.first
924 var recv_boxed
= self.autobox
(recv
, self.object_type
)
926 var res
= self.new_var
(bool_type
)
928 var cltype
= self.get_name
("cltype")
929 self.add_decl
("int {cltype};")
930 var idtype
= self.get_name
("idtype")
931 self.add_decl
("int {idtype};")
933 var boxed
= self.autobox
(value
, self.object_type
)
936 if mtype
isa MNullableType then
938 s
= "{boxed} == NULL ||"
940 s
= "{boxed} != NULL &&"
942 if mtype
isa MParameterType then
943 var ftcolor
= compiler
.ft_colors
[mtype
]
944 self.add
("{cltype} = {recv_boxed}->type->fts_table->fts[{ftcolor}]->color;")
945 self.add
("{idtype} = {recv_boxed}->type->fts_table->fts[{ftcolor}]->id;")
946 else if mtype
isa MGenericType and mtype
.need_anchor
then
947 var buff
= new Buffer
948 retrieve_anchored_livetype
(mtype
, buff
)
949 self.add
("{cltype} = livetypes_{mtype.mclass.c_name}{buff.to_s}->color;")
950 self.add
("{idtype} = livetypes_{mtype.mclass.c_name}{buff.to_s}->id;")
951 else if mtype
isa MClassType then
952 compiler
.undead_types
.add
(mtype
)
953 self.add
("{cltype} = type_{mtype.c_name}.color;")
954 self.add
("{idtype} = type_{mtype.c_name}.id;")
956 self.add
("printf(\"NOT YET IMPLEMENTED: type_test
(%s
, {mtype}).\\n\
", \"{boxed.inspect}\
"); exit(1);")
959 # check color is in table
960 self.add
("if({boxed} != NULL && {cltype} >= {boxed}->type->table_size) \{")
961 self.add
("{res} = 0;")
962 self.add
("\} else \{")
963 self.add
("{res} = {s} {boxed}->type->type_table[{cltype}] == {idtype};")
969 redef fun is_same_type_test
(value1
, value2
)
971 var res
= self.new_var
(bool_type
)
972 # Swap values to be symetric
973 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
978 if value1
.mtype
.ctype
!= "val*" then
979 if value2
.mtype
.ctype
== value1
.mtype
.ctype
then
980 self.add
("{res} = 1; /* is_same_type_test: compatible types {value1.mtype} vs. {value2.mtype} */")
981 else if value2
.mtype
.ctype
!= "val*" then
982 self.add
("{res} = 0; /* is_same_type_test: incompatible types {value1.mtype} vs. {value2.mtype}*/")
984 var mtype1
= value1
.mtype
.as(MClassType)
985 self.add
("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name}); /* is_same_type_test */")
988 self.add
("{res} = ({value1} == {value2}) || ({value1} != NULL && {value2} != NULL && {value1}->class == {value2}->class); /* is_same_type_test */")
993 redef fun class_name_string
(value
)
995 var res
= self.get_name
("var_class_name")
996 self.add_decl
("const char *{res};")
997 self.add
("{res} = class_names[{value}->type->id];")
1001 redef fun equal_test
(value1
, value2
)
1003 var res
= self.new_var
(bool_type
)
1004 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1009 if value1
.mtype
.ctype
!= "val*" then
1010 if value2
.mtype
.ctype
== value1
.mtype
.ctype
then
1011 self.add
("{res} = {value1} == {value2};")
1012 else if value2
.mtype
.ctype
!= "val*" then
1013 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1015 var mtype1
= value1
.mtype
.as(MClassType)
1016 self.add
("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name});")
1017 self.add
("if ({res}) \{")
1018 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1022 var s
= new Array[String]
1023 # This is just ugly on so many level. this works but must be rewriten
1024 for t
in self.compiler
.live_primitive_types
do
1025 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value1
.mcasttype
) then continue
1026 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value2
.mcasttype
) then continue
1027 s
.add
"({value1}->class == (struct class*)&class_{t.c_name} && ((struct instance_{t.c_name}*){value1})->value == ((struct instance_{t.c_name}*){value2})->value)"
1030 self.add
("{res} = {value1} == {value2};")
1032 self.add
("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->class == {value2}->class && ({s.join(" || ")}));")
1038 redef fun array_instance
(array
, elttype
)
1040 var compiler
= self.compiler
.as(SeparateCompiler)
1041 var nclass
= self.get_class
("NativeArray")
1042 elttype
= self.anchor
(elttype
)
1043 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
1044 var res
= self.init_instance
(arraytype
)
1045 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1046 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
1048 compiler
.undead_types
.add
(nat
.mtype
.as(MClassType))
1049 self.add
("{nat} = NEW_{nclass.c_name}({array.length}, (struct type *) &type_{nat.mtype.c_name});")
1050 for i
in [0..array
.length
[ do
1051 var r
= self.autobox
(array
[i
], self.object_type
)
1052 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
1054 var length
= self.int_instance
(array
.length
)
1055 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
1056 self.check_init_instance
(res
)
1061 redef fun native_array_def
(pname
, ret_type
, arguments
)
1063 var elttype
= arguments
.first
.mtype
1064 var nclass
= self.get_class
("NativeArray")
1065 var recv
= "((struct instance_{nclass.c_name}*){arguments[0]})->values"
1066 if pname
== "[]" then
1067 self.ret
(self.new_expr
("{recv}[{arguments[1]}]", ret_type
.as(not null)))
1069 else if pname
== "[]=" then
1070 self.add
("{recv}[{arguments[1]}]={arguments[2]};")
1072 else if pname
== "copy_to" then
1073 var recv1
= "((struct instance_{nclass.c_name}*){arguments[1]})->values"
1074 self.add
("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
1079 redef fun calloc_array
(ret_type
, arguments
)
1081 var ret
= ret_type
.as(MClassType)
1082 var compiler
= self.compiler
.as(SeparateCompiler)
1083 compiler
.undead_types
.add
(ret
)
1084 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]}, (struct type*) &type_{ret_type.c_name})", ret_type
))
1089 # Return the name of the C structure associated to a Nit class
1090 fun c_name
: String do
1091 var res
= self.c_name_cache
1092 if res
!= null then return res
1093 res
= "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1094 self.c_name_cache
= res
1097 private var c_name_cache
: nullable String