1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 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 # Compute and generate tables for classes and modules.
18 module compiling_global
20 import table_computation
21 private import compiling_icode
24 # Compile module and class tables
25 fun compile_tables_to_c
(v
: CompilerVisitor)
27 for m
in main_module
.mhe
.greaters_and_self
do
28 m
.compile_local_table_to_c
(v
)
31 with_each_live_local_classes
!action
(c
) do
32 if c
.global
.is_abstract
or c
.global
.is_interface
then continue
33 c
.compile_tables_to_c
(v
)
36 var s
= new Buffer.from
("classtable_t TAG2VFT[4] = \{NULL")
37 for t
in ["Int","Char","Bool"] do
38 if main_module
.has_global_class_named
(t
.to_symbol
) then
39 var c
= main_module
.class_by_name
(t
.to_symbol
)
40 s
.append
(", (const classtable_t)VFT_{c.cname}")
49 # Compile main part (for _table.c)
50 fun compile_main_part
(v
: CompilerVisitor)
52 v
.add_instr
("int main(int argc, char **argv) \{")
54 v
.add_instr
("prepare_signals();")
55 v
.add_instr
("glob_argc = argc; glob_argv = argv;")
56 if v
.program
.main_method
== null then
59 var c
= v
.program
.main_class
60 v
.add_instr
("G_sys = NEW_{c.cname}();")
61 v
.add_instr
("register_static_object(&G_sys);")
62 v
.add_instr
("{v.program.main_method.cname}(G_sys);")
64 v
.add_instr
("return 0;")
71 # Declare class table (for _sep.h or _glob.h)
72 fun declare_class_tables_to_c
(v
: CompilerVisitor)
74 for c
in local_classes
do
75 if c
.global
.mmmodule
== self then
76 c
.declare_tables_to_c
(v
)
82 fun compile_mod_to_c
(v
: CompilerVisitor)
84 v
.add_decl
("extern const char LOCATE_{cname}[];")
85 if not v
.program
.tc
.use_SFT_optimization
then
86 v
.add_decl
("extern const int SFT_{cname}[];")
89 for e
in local_table
do
91 if v
.program
.tc
.use_SFT_optimization
then
92 value
= "{e.value(v.program)}"
94 value
= "SFT_{cname}[{i}]"
97 e
.compile_macros
(v
, value
)
99 for c
in local_classes
do
100 if not c
isa MMConcreteClass then continue
101 for pg
in c
.global_properties
do
103 if p
.local_class
== c
and p
isa MMMethod then
104 p
.compile_property_to_c
(v
)
106 if pg
.is_init_for
(c
) then
107 # Declare constructors
108 var params
= new Array[String]
109 for j
in [0..p
.signature
.arity
[ do
110 params
.add
("val_t p{j}")
112 v
.add_decl
("val_t NEW_{c}_{p.global.intro.cname}({params.join(", ")});")
118 # Compile module file for the current module
119 fun compile_local_table_to_c
(v
: CompilerVisitor)
121 v
.add_instr
("const char LOCATE_{cname}[] = \"{location.file.filename}\
";")
123 if v
.program
.tc
.use_SFT_optimization
or local_table
.is_empty
then
127 v
.add_instr
("const int SFT_{cname}[{local_table.length}] = \{")
129 for e
in local_table
do
130 v
.add_instr
(e
.value
(v
.program
) + ",")
137 ###############################################################################
139 redef class AbsTableElt
140 # Compile the macro needed to use the element and other related elements
141 fun compile_macros
(v
: CompilerVisitor, value
: String) is abstract
145 # Return the value of the element for a given class
146 fun compile_to_c
(v
: CompilerVisitor, c
: MMLocalClass): String is abstract
149 redef class ModuleTableElt
150 # Return the value of the element once the global analisys is performed
151 fun value
(prog
: Program): String is abstract
154 redef class ModuleTableEltGroup
155 redef fun value
(prog
) do return "{prog.table_information.color(elements.first)} /* Group of ? */"
156 redef fun compile_macros
(v
, value
)
160 e
.compile_macros
(v
, "{value} + {i}")
166 redef class TableEltMeth
167 redef fun compile_macros
(v
, value
)
169 var pg
= property
.global
170 v
.add_decl
("#define {pg.meth_call}(recv) (({pg.intro.cname}_t)CALL((recv), ({value})))")
173 redef fun compile_to_c
(v
, c
)
175 var p
= c
[property
.global
]
180 redef class TableEltSuper
181 redef fun compile_macros
(v
, value
)
184 v
.add_decl
("#define {p.super_meth_call}(recv) (({p.cname}_t)CALL((recv), ({value})))")
187 redef fun compile_to_c
(v
, c
)
189 var pc
= property
.local_class
190 var g
= property
.global
191 var lin
= c
.che
.linear_extension
194 #print "{c.mmmodule}::{c} for {pc.mmmodule}::{pc}::{property} try {s.mmmodule}:{s}"
195 if not s
.has_global_property
(g
) then continue
198 if p
.local_class
!= s
then continue
201 else if found
and c
.che
< s
then
202 if s
.has_global_property
(g
) then
203 #print "found {s.mmmodule}::{s} {p.local_class}::{p} {p.cname}"
212 redef class TableEltVTClassColor
213 redef fun compile_macros
(v
, value
)
215 var pg
= property
.global
216 v
.add_decl
("#define {pg.vt_class_color}(recv) (VAL2VFT(recv)[{value}].i)")
219 redef fun compile_to_c
(v
, c
)
222 var p
= c
[property
.global
]
223 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
224 var col
= g
.intro
.as(MMConcreteClass).class_color_pos
225 return "{prog.table_information.color(col)} /* {prog.table_information.color(self)}: VT {c}::{p} : color of {g} */"
229 redef class TableEltVTClassId
230 redef fun compile_macros
(v
, value
)
232 var pg
= property
.global
233 v
.add_decl
("#define {pg.vt_class_id}(recv) (VAL2VFT(recv)[{value}].i)")
236 redef fun compile_to_c
(v
, c
)
239 var p
= c
[property
.global
]
240 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
241 return "{prog.compiled_classes[g].id} /* {prog.table_information.color(self)}: VT {c}::{p} : id of {g} */"
245 redef class TableEltAttr
246 redef fun compile_macros
(v
, value
)
248 var pg
= property
.global
249 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
252 redef fun compile_to_c
(v
, c
)
255 var p
= c
[property
.global
]
256 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
261 redef class AbsTableEltClass
262 # The C macro name refering the value
263 fun symbol
: String is abstract
265 redef fun compile_macros
(v
, value
)
267 v
.add_decl
("#define {symbol} ({value})")
271 redef class TableEltClassId
272 redef fun symbol
do return local_class
.global
.id_id
274 redef fun value
(prog
)
276 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
280 redef class TableEltClassInitTable
281 redef fun symbol
do return local_class
.global
.init_table_pos_id
283 redef fun compile_to_c
(v
, c
)
286 var cc
= prog
.compiled_classes
[local_class
.global
]
287 var linext
= c
.cshe
.reverse_linear_extension
289 while linext
[i
].global
!= local_class
.global
do
292 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
296 redef class TableEltClassColor
297 redef fun symbol
do return local_class
.global
.color_id
299 redef fun value
(prog
)
301 return "{prog.table_information.color(self)} /* Color of {local_class} */"
304 redef fun compile_to_c
(v
, c
)
307 var cc
= prog
.compiled_classes
[local_class
.global
]
308 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
312 redef class TableEltComposite
313 redef fun compile_to_c
(v
, c
) do abort
316 redef class TableEltClassSelfId
317 redef fun compile_to_c
(v
, c
)
320 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
324 redef class TableEltClassSelfName
325 redef fun compile_to_c
(v
, c
)
328 return "\"{c.global.name}\
" /* {prog.table_information.color(self)}: Class Name */"
332 redef class TableEltClassObjectSize
333 redef fun compile_to_c
(v
, c
)
337 if c
.name
== "NativeArray".to_symbol
then
340 var cc
= p
.compiled_classes
[c
.global
]
341 var itab
= cc
.instance_table
346 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
350 redef class TableEltObjectId
351 redef fun compile_to_c
(v
, c
)
354 return "/* {p.table_information.color(self)}: Object_id */"
358 redef class TableEltVftPointer
359 redef fun compile_to_c
(v
, c
)
362 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
366 ###############################################################################
368 redef class MMLocalClass
369 # Declaration and macros related to the class table
370 fun declare_tables_to_c
(v
: CompilerVisitor)
373 var pi
= primitive_info
374 v
.add_decl
("extern const classtable_elt_t VFT_{cname}[];")
375 if pi
!= null and not pi
.tagged
then
377 var tbox
= "struct TBOX_{name}"
378 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
379 v
.add_decl
("val_t BOX_{name}({t} val);")
380 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
384 # Compilation of table and new (or box)
385 fun compile_tables_to_c
(v
: CompilerVisitor)
387 var cc
= v
.program
.compiled_classes
[self.global
]
388 var ctab
= cc
.class_table
389 var clen
= ctab
.length
390 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
391 clen
= v
.program
.table_information
.max_class_table_length
394 v
.add_instr
("const classtable_elt_t VFT_{cname}[{clen}] = \{")
398 v
.add_instr
("\{0} /* Class Hole :( */,")
400 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
403 if clen
> ctab
.length
then
404 v
.add_instr
("\{0},"*(clen-ctab
.length
))
408 var itab
= cc
.instance_table
411 v
.add_instr
("/* Instance Hole :( */")
413 v
.add_instr
(e
.compile_to_c
(v
, self))
417 var pi
= primitive_info
418 if name
== "NativeArray".to_symbol
then
419 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
421 v
.add_instr
("Nit_NativeArray array;")
422 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
423 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{cname};")
424 v
.add_instr
("array->object_id = object_id_counter;")
425 v
.add_instr
("object_id_counter = object_id_counter + 1;")
426 v
.add_instr
("array->size = length;")
427 v
.add_instr
("return OBJ2VAL(array);")
430 else if pi
== null then
432 # Generate INIT_ATTRIBUTES routine
433 var cname
= "INIT_ATTRIBUTES__{cname}"
434 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
435 var decl_writer_old
= v
.decl_writer
436 v
.decl_writer
= v
.writer
.sub
437 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
438 v
.decl_writer
= decl_writer_old
443 # Generate NEW routine
444 v
.add_decl
("val_t NEW_{cname}(void);")
445 v
.add_instr
("val_t NEW_{cname}(void)")
448 v
.add_instr
("obj_t obj;")
449 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
450 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{cname};")
451 v
.add_instr
("obj[1].object_id = object_id_counter;")
452 v
.add_instr
("object_id_counter = object_id_counter + 1;")
453 v
.add_instr
("return OBJ2VAL(obj);")
459 var cname
= "CHECKNEW_{cname}"
460 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
461 var decl_writer_old
= v
.decl_writer
462 v
.decl_writer
= v
.writer
.sub
463 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
464 v
.decl_writer
= decl_writer_old
469 var init_table_size
= cshe
.greaters
.length
+ 1
470 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
472 for g
in global_properties
do
474 # FIXME skip invisible constructors
475 if not p
.global
.is_init_for
(self) then continue
476 assert p
isa MMMethod
478 var cname
= "NEW_{self}_{p.global.intro.cname}"
479 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
480 var decl_writer_old
= v
.decl_writer
481 v
.decl_writer
= v
.writer
.sub
482 v
.add_instr
(init_table_decl
)
483 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
484 v
.add_instr
("return {e};")
485 v
.decl_writer
= decl_writer_old
489 else if not pi
.tagged
then
491 var tbox
= "struct TBOX_{name}"
492 v
.add_instr
("val_t BOX_{name}({t} val) \{")
494 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
495 v
.add_instr
("box->vft = VFT_{cname};")
496 v
.add_instr
("box->val = val;")
497 v
.add_instr
("box->object_id = object_id_counter;")
498 v
.add_instr
("object_id_counter = object_id_counter + 1;")
499 v
.add_instr
("return OBJ2VAL(box);")
507 fun compile_property_to_c
(v
: CompilerVisitor)
512 var more_params
: nullable String = null
513 if global
.is_init
then more_params
= "int* init_table"
514 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
515 var writer_old
= v
.writer
516 v
.writer
= v
.writer
.sub
517 var decl_writer_old
= v
.decl_writer
518 v
.decl_writer
= v
.writer
.sub
520 var itpos
: nullable String = null
521 if global
.is_init
then
522 itpos
= "itpos{v.new_number}"
523 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
524 v
.add_instr
("if (init_table[{itpos}]) return;")
527 var s
= ir
.compile_to_c
(v
, cname
, args
)
529 if itpos
!= null then
530 v
.add_instr
("init_table[{itpos}] = 1;")
533 v
.add_instr
("return;")
535 v
.add_instr
("return {s};")
540 v
.writer
= writer_old
541 v
.decl_writer
= decl_writer_old