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.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.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 TableEltClassSelfName
287 redef fun compile_to_c
(v
, c
)
290 return "\"{c.global.name}\
" /* {prog.table_information.color(self)}: Class Name */"
294 redef class TableEltClassObjectSize
295 redef fun compile_to_c
(v
, c
)
299 if c
.name
== "NativeArray".to_symbol
then
302 var cc
= p
.compiled_classes
[c
.global
]
303 var itab
= cc
.instance_table
308 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
312 redef class TableEltObjectId
313 redef fun compile_to_c
(v
, c
)
316 return "/* {p.table_information.color(self)}: Object_id */"
320 redef class TableEltVftPointer
321 redef fun compile_to_c
(v
, c
)
324 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
328 ###############################################################################
330 redef class MMLocalClass
331 # Declaration and macros related to the class table
332 fun declare_tables_to_c
(v
: CompilerVisitor)
335 var pi
= primitive_info
336 v
.add_decl
("extern const classtable_elt_t VFT_{cname}[];")
337 if pi
!= null and not pi
.tagged
then
339 var tbox
= "struct TBOX_{name}"
340 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
341 v
.add_decl
("val_t BOX_{name}({t} val);")
342 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
346 # Compilation of table and new (or box)
347 fun compile_tables_to_c
(v
: CompilerVisitor)
349 var cc
= v
.program
.compiled_classes
[self.global
]
350 var ctab
= cc
.class_table
351 var clen
= ctab
.length
352 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
353 clen
= v
.program
.table_information
.max_class_table_length
356 v
.add_instr
("const classtable_elt_t VFT_{cname}[{clen}] = \{")
360 v
.add_instr
("\{0} /* Class Hole :( */,")
362 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
365 if clen
> ctab
.length
then
366 v
.add_instr
("\{0},"*(clen-ctab
.length
))
370 var itab
= cc
.instance_table
373 v
.add_instr
("/* Instance Hole :( */")
375 v
.add_instr
(e
.compile_to_c
(v
, self))
379 var pi
= primitive_info
380 if name
== "NativeArray".to_symbol
then
381 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
383 v
.add_instr
("Nit_NativeArray array;")
384 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
385 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{cname};")
386 v
.add_instr
("array->object_id = object_id_counter;")
387 v
.add_instr
("object_id_counter = object_id_counter + 1;")
388 v
.add_instr
("array->size = length;")
389 v
.add_instr
("return OBJ2VAL(array);")
392 else if pi
== null then
394 # Generate INIT_ATTRIBUTES routine
395 var cname
= "INIT_ATTRIBUTES__{cname}"
396 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
397 var decl_writer_old
= v
.decl_writer
398 v
.decl_writer
= v
.writer
.sub
399 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
400 v
.decl_writer
= decl_writer_old
405 # Generate NEW routine
406 v
.add_decl
("val_t NEW_{cname}(void);")
407 v
.add_instr
("val_t NEW_{cname}(void)")
410 v
.add_instr
("obj_t obj;")
411 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
412 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{cname};")
413 v
.add_instr
("obj[1].object_id = object_id_counter;")
414 v
.add_instr
("object_id_counter = object_id_counter + 1;")
415 v
.add_instr
("return OBJ2VAL(obj);")
421 var cname
= "CHECKNEW_{cname}"
422 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
423 var decl_writer_old
= v
.decl_writer
424 v
.decl_writer
= v
.writer
.sub
425 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
426 v
.decl_writer
= decl_writer_old
431 var init_table_size
= cshe
.greaters
.length
+ 1
432 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
434 for g
in global_properties
do
436 # FIXME skip invisible constructors
437 if not p
.global
.is_init_for
(self) then continue
438 assert p
isa MMMethod
440 var cname
= "NEW_{self}_{p.global.intro.cname}"
441 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
442 var decl_writer_old
= v
.decl_writer
443 v
.decl_writer
= v
.writer
.sub
444 v
.add_instr
(init_table_decl
)
445 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
446 v
.add_instr
("return {e};")
447 v
.decl_writer
= decl_writer_old
451 else if not pi
.tagged
then
453 var tbox
= "struct TBOX_{name}"
454 v
.add_instr
("val_t BOX_{name}({t} val) \{")
456 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
457 v
.add_instr
("box->vft = VFT_{cname};")
458 v
.add_instr
("box->val = val;")
459 v
.add_instr
("box->object_id = object_id_counter;")
460 v
.add_instr
("object_id_counter = object_id_counter + 1;")
461 v
.add_instr
("return OBJ2VAL(box);")
469 fun compile_property_to_c
(v
: CompilerVisitor)
474 var more_params
: nullable String = null
475 if global
.is_init
then more_params
= "int* init_table"
476 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
477 var writer_old
= v
.writer
478 v
.writer
= v
.writer
.sub
479 var decl_writer_old
= v
.decl_writer
480 v
.decl_writer
= v
.writer
.sub
482 var itpos
: nullable String = null
483 if global
.is_init
then
484 itpos
= "itpos{v.new_number}"
485 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
486 v
.add_instr
("if (init_table[{itpos}]) return;")
489 var s
= ir
.compile_to_c
(v
, cname
, args
)
491 if itpos
!= null then
492 v
.add_instr
("init_table[{itpos}] = 1;")
495 v
.add_instr
("return;")
497 v
.add_instr
("return {s};")
502 v
.writer
= writer_old
503 v
.decl_writer
= decl_writer_old