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 package 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}\
";")
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.module}::{c} for {pc.module}::{pc}::{_property} try {s.module}:{s}"
197 else if found
and c
.che
< s
then
198 if s
.has_global_property
(g
) then
199 #print "found {s.module}::{s}::{p}"
208 redef class TableEltAttr
209 redef fun compile_macros
(v
, value
)
211 var pg
= property
.global
212 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
215 redef fun compile_to_c
(v
, c
)
218 var p
= c
[property
.global
]
219 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
223 redef class AbsTableEltClass
224 # The C macro name refering the value
225 fun symbol
: String is abstract
227 redef fun compile_macros
(v
, value
)
229 v
.add_decl
("#define {symbol} ({value})")
233 redef class TableEltClassId
234 redef fun symbol
do return local_class
.global
.id_id
236 redef fun value
(prog
)
238 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
242 redef class TableEltClassInitTable
243 redef fun symbol
do return local_class
.global
.init_table_pos_id
245 redef fun compile_to_c
(v
, c
)
248 var cc
= prog
.compiled_classes
[local_class
.global
]
249 var linext
= c
.cshe
.reverse_linear_extension
251 while linext
[i
].global
!= local_class
.global
do
254 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
258 redef class TableEltClassColor
259 redef fun symbol
do return local_class
.global
.color_id
261 redef fun value
(prog
)
263 return "{prog.table_information.color(self)} /* Color of {local_class} */"
266 redef fun compile_to_c
(v
, c
)
269 var cc
= prog
.compiled_classes
[local_class
.global
]
270 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
274 redef class TableEltComposite
275 redef fun compile_to_c
(v
, c
) do abort
278 redef class TableEltClassSelfId
279 redef fun compile_to_c
(v
, c
)
282 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
286 redef class TableEltClassObjectSize
287 redef fun compile_to_c
(v
, c
)
291 if c
.name
== "NativeArray".to_symbol
then
294 var cc
= p
.compiled_classes
[c
.global
]
295 var itab
= cc
.instance_table
300 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
304 redef class TableEltObjectId
305 redef fun compile_to_c
(v
, c
)
308 return "/* {p.table_information.color(self)}: Object_id */"
312 redef class TableEltVftPointer
313 redef fun compile_to_c
(v
, c
)
316 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
320 ###############################################################################
322 redef class MMLocalClass
323 # Declaration and macros related to the class table
324 fun declare_tables_to_c
(v
: CompilerVisitor)
327 var pi
= primitive_info
328 v
.add_decl
("extern const classtable_elt_t VFT_{cname}[];")
329 if pi
!= null and not pi
.tagged
then
331 var tbox
= "struct TBOX_{name}"
332 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
333 v
.add_decl
("val_t BOX_{name}({t} val);")
334 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
338 # Compilation of table and new (or box)
339 fun compile_tables_to_c
(v
: CompilerVisitor)
341 var cc
= v
.program
.compiled_classes
[self.global
]
342 var ctab
= cc
.class_table
343 var clen
= ctab
.length
344 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
345 clen
= v
.program
.table_information
.max_class_table_length
348 v
.add_instr
("const classtable_elt_t VFT_{cname}[{clen}] = \{")
352 v
.add_instr
("\{0} /* Class Hole :( */,")
354 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
357 if clen
> ctab
.length
then
358 v
.add_instr
("\{0},"*(clen-ctab
.length
))
362 var itab
= cc
.instance_table
365 v
.add_instr
("/* Instance Hole :( */")
367 v
.add_instr
(e
.compile_to_c
(v
, self))
371 var pi
= primitive_info
372 if name
== "NativeArray".to_symbol
then
373 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
375 v
.add_instr
("Nit_NativeArray array;")
376 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
377 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{cname};")
378 v
.add_instr
("array->object_id = object_id_counter;")
379 v
.add_instr
("object_id_counter = object_id_counter + 1;")
380 v
.add_instr
("array->size = length;")
381 v
.add_instr
("return OBJ2VAL(array);")
384 else if pi
== null then
386 # Generate INIT_ATTRIBUTES routine
387 var cname
= "INIT_ATTRIBUTES__{cname}"
388 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
389 var decl_writer_old
= v
.decl_writer
390 v
.decl_writer
= v
.writer
.sub
391 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
392 v
.decl_writer
= decl_writer_old
397 # Generate NEW routine
398 v
.add_decl
("val_t NEW_{cname}(void);")
399 v
.add_instr
("val_t NEW_{cname}(void)")
402 v
.add_instr
("obj_t obj;")
403 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
404 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{cname};")
405 v
.add_instr
("obj[1].object_id = object_id_counter;")
406 v
.add_instr
("object_id_counter = object_id_counter + 1;")
407 v
.add_instr
("return OBJ2VAL(obj);")
413 var cname
= "CHECKNEW_{cname}"
414 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
415 var decl_writer_old
= v
.decl_writer
416 v
.decl_writer
= v
.writer
.sub
417 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
418 v
.decl_writer
= decl_writer_old
423 var init_table_size
= cshe
.greaters
.length
+ 1
424 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
426 for g
in global_properties
do
428 # FIXME skip invisible constructors
429 if not p
.global
.is_init_for
(self) then continue
430 assert p
isa MMMethod
432 var cname
= "NEW_{self}_{p.global.intro.cname}"
433 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
434 var decl_writer_old
= v
.decl_writer
435 v
.decl_writer
= v
.writer
.sub
436 v
.add_instr
(init_table_decl
)
437 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
438 v
.add_instr
("return {e};")
439 v
.decl_writer
= decl_writer_old
443 else if not pi
.tagged
then
445 var tbox
= "struct TBOX_{name}"
446 v
.add_instr
("val_t BOX_{name}({t} val) \{")
448 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
449 v
.add_instr
("box->vft = VFT_{cname};")
450 v
.add_instr
("box->val = val;")
451 v
.add_instr
("box->object_id = object_id_counter;")
452 v
.add_instr
("object_id_counter = object_id_counter + 1;")
453 v
.add_instr
("return OBJ2VAL(box);")
461 fun compile_property_to_c
(v
: CompilerVisitor)
466 var more_params
: nullable String = null
467 if global
.is_init
then more_params
= "int* init_table"
468 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
469 var writer_old
= v
.writer
470 v
.writer
= v
.writer
.sub
471 var decl_writer_old
= v
.decl_writer
472 v
.decl_writer
= v
.writer
.sub
474 var itpos
: nullable String = null
475 if global
.is_init
then
476 itpos
= "itpos{v.new_number}"
477 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
478 v
.add_instr
("if (init_table[{itpos}]) return;")
481 var s
= ir
.compile_to_c
(v
, cname
, args
)
483 if itpos
!= null then
484 v
.add_instr
("init_table[{itpos}] = 1;")
487 v
.add_instr
("return;")
489 v
.add_instr
("return {s};")
494 v
.writer
= writer_old
495 v
.decl_writer
= decl_writer_old