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 module.mhe
.greaters_and_self
do
28 m
.compile_local_table_to_c
(v
)
31 with_each_live_local_classes
!action
(c
) do
32 c
.compile_tables_to_c
(v
)
35 var s
= new Buffer.from
("classtable_t TAG2VFT[4] = \{NULL")
36 for t
in ["Int","Char","Bool"] do
37 if module.has_global_class_named
(t
.to_symbol
) then
38 s
.append
(", (const classtable_t)VFT_{t}")
47 # Compile main part (for _table.c)
48 fun compile_main_part
(v
: CompilerVisitor)
50 v
.add_instr
("int main(int argc, char **argv) \{")
52 v
.add_instr
("prepare_signals();")
53 v
.add_instr
("glob_argc = argc; glob_argv = argv;")
54 if v
.program
.main_method
== null then
57 v
.add_instr
("G_sys = NEW_Sys();")
58 v
.add_instr
("register_static_object(&G_sys);")
59 v
.add_instr
("{v.program.main_method.cname}(G_sys);")
61 v
.add_instr
("return 0;")
68 # Declare class table (for _sep.h or _glob.h)
69 fun declare_class_tables_to_c
(v
: CompilerVisitor)
71 for c
in local_classes
do
72 if c
.global
.module == self then
73 c
.declare_tables_to_c
(v
)
79 fun compile_mod_to_c
(v
: CompilerVisitor)
81 v
.add_decl
("extern const char *LOCATE_{name};")
82 if not v
.program
.tc
.use_SFT_optimization
then
83 v
.add_decl
("extern const int SFT_{name}[];")
86 for e
in local_table
do
88 if v
.program
.tc
.use_SFT_optimization
then
89 value
= "{e.value(v.program)}"
91 value
= "SFT_{name}[{i}]"
94 e
.compile_macros
(v
, value
)
96 for c
in local_classes
do
97 if not c
isa MMConcreteClass then continue
98 for pg
in c
.global_properties
do
100 if p
.local_class
== c
and p
isa MMMethod then
101 p
.compile_property_to_c
(v
)
103 if pg
.is_init_for
(c
) then
104 # Declare constructors
105 var params
= new Array[String]
106 for j
in [0..p
.signature
.arity
[ do
107 params
.add
("val_t p{j}")
109 v
.add_decl
("val_t NEW_{c}_{p.global.intro.cname}({params.join(", ")});")
115 # Compile module file for the current module
116 fun compile_local_table_to_c
(v
: CompilerVisitor)
118 v
.add_instr
("const char *LOCATE_{name} = \"{location.file}\
";")
120 if v
.program
.tc
.use_SFT_optimization
or local_table
.is_empty
then
124 v
.add_instr
("const int SFT_{name}[{local_table.length}] = \{")
126 for e
in local_table
do
127 v
.add_instr
(e
.value
(v
.program
) + ",")
134 ###############################################################################
136 redef class AbsTableElt
137 # Compile the macro needed to use the element and other related elements
138 fun compile_macros
(v
: CompilerVisitor, value
: String) is abstract
142 # Return the value of the element for a given class
143 fun compile_to_c
(v
: CompilerVisitor, c
: MMLocalClass): String is abstract
146 redef class ModuleTableElt
147 # Return the value of the element once the global analisys is performed
148 fun value
(prog
: Program): String is abstract
151 redef class ModuleTableEltGroup
152 redef fun value
(prog
) do return "{prog.table_information.color(elements.first)} /* Group of ? */"
153 redef fun compile_macros
(v
, value
)
157 e
.compile_macros
(v
, "{value} + {i}")
163 redef class TableEltMeth
164 redef fun compile_macros
(v
, value
)
166 var pg
= property
.global
167 v
.add_decl
("#define {pg.meth_call}(recv) (({pg.intro.cname}_t)CALL((recv), ({value})))")
170 redef fun compile_to_c
(v
, c
)
172 var p
= c
[property
.global
]
177 redef class TableEltSuper
178 redef fun compile_macros
(v
, value
)
181 v
.add_decl
("#define {p.super_meth_call}(recv) (({p.cname}_t)CALL((recv), ({value})))")
184 redef fun compile_to_c
(v
, c
)
186 var pc
= property
.local_class
187 var g
= property
.global
188 var lin
= c
.che
.linear_extension
191 #print "{c.module}::{c} for {pc.module}::{pc}::{_property} try {s.module}:{s}"
194 else if found
and c
.che
< s
then
195 if s
.has_global_property
(g
) then
196 #print "found {s.module}::{s}::{p}"
205 redef class TableEltAttr
206 redef fun compile_macros
(v
, value
)
208 var pg
= property
.global
209 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
212 redef fun compile_to_c
(v
, c
)
215 var p
= c
[property
.global
]
216 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
220 redef class AbsTableEltClass
221 # The C macro name refering the value
222 fun symbol
: String is abstract
224 redef fun compile_macros
(v
, value
)
226 v
.add_decl
("#define {symbol} ({value})")
230 redef class TableEltClassId
231 redef fun symbol
do return local_class
.global
.id_id
233 redef fun value
(prog
)
235 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
239 redef class TableEltClassInitTable
240 redef fun symbol
do return local_class
.global
.init_table_pos_id
242 redef fun compile_to_c
(v
, c
)
245 var cc
= prog
.compiled_classes
[local_class
.global
]
246 var linext
= c
.cshe
.reverse_linear_extension
248 while linext
[i
].global
!= local_class
.global
do
251 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
255 redef class TableEltClassColor
256 redef fun symbol
do return local_class
.global
.color_id
258 redef fun value
(prog
)
260 return "{prog.table_information.color(self)} /* Color of {local_class} */"
263 redef fun compile_to_c
(v
, c
)
266 var cc
= prog
.compiled_classes
[local_class
.global
]
267 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
271 redef class TableEltComposite
272 redef fun compile_to_c
(v
, c
) do abort
275 redef class TableEltClassSelfId
276 redef fun compile_to_c
(v
, c
)
279 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
283 redef class TableEltClassObjectSize
284 redef fun compile_to_c
(v
, c
)
288 if c
.name
== "NativeArray".to_symbol
then
291 var cc
= p
.compiled_classes
[c
.global
]
292 var itab
= cc
.instance_table
297 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
301 redef class TableEltObjectId
302 redef fun compile_to_c
(v
, c
)
305 return "/* {p.table_information.color(self)}: Object_id */"
309 redef class TableEltVftPointer
310 redef fun compile_to_c
(v
, c
)
313 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
317 ###############################################################################
319 redef class MMLocalClass
320 # Declaration and macros related to the class table
321 fun declare_tables_to_c
(v
: CompilerVisitor)
324 var pi
= primitive_info
325 v
.add_decl
("extern const classtable_elt_t VFT_{name}[];")
326 if name
== "NativeArray".to_symbol
then
327 v
.add_decl
("val_t NEW_NativeArray(size_t length, size_t size);")
328 else if pi
== null then
329 # v.add_decl("val_t NEW_{name}(void);")
330 else if not pi
.tagged
then
332 var tbox
= "struct TBOX_{name}"
333 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
334 v
.add_decl
("val_t BOX_{name}({t} val);")
335 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
339 # Compilation of table and new (or box)
340 fun compile_tables_to_c
(v
: CompilerVisitor)
342 var cc
= v
.program
.compiled_classes
[self.global
]
343 var ctab
= cc
.class_table
344 var clen
= ctab
.length
345 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
346 clen
= v
.program
.table_information
.max_class_table_length
349 v
.add_instr
("const classtable_elt_t VFT_{name}[{clen}] = \{")
353 v
.add_instr
("\{0} /* Class Hole :( */,")
355 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
358 if clen
> ctab
.length
then
359 v
.add_instr
("\{0},"*(clen-ctab
.length
))
363 var itab
= cc
.instance_table
366 v
.add_instr
("/* Instance Hole :( */")
368 v
.add_instr
(e
.compile_to_c
(v
, self))
372 var pi
= primitive_info
373 if name
== "NativeArray".to_symbol
then
374 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
376 v
.add_instr
("Nit_NativeArray array;")
377 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
378 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{name};")
379 v
.add_instr
("array->object_id = object_id_counter;")
380 v
.add_instr
("object_id_counter = object_id_counter + 1;")
381 v
.add_instr
("array->size = length;")
382 v
.add_instr
("return OBJ2VAL(array);")
385 else if pi
== null then
387 # Generate INIT_ATTRIBUTES routine
388 var cname
= "INIT_ATTRIBUTES__{name}"
389 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
392 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
393 ctx_old
.append
(v
.ctx
)
399 # Generate NEW routine
400 v
.add_decl
("val_t NEW_{name}(void);")
401 v
.add_instr
("val_t NEW_{name}(void)")
404 v
.add_instr
("obj_t obj;")
405 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
406 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{name};")
407 v
.add_instr
("obj[1].object_id = object_id_counter;")
408 v
.add_instr
("object_id_counter = object_id_counter + 1;")
409 v
.add_instr
("return OBJ2VAL(obj);")
415 var cname
= "CHECKNEW_{name}"
416 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
419 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
420 ctx_old
.append
(v
.ctx
)
426 var init_table_size
= cshe
.greaters
.length
+ 1
427 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
429 for g
in global_properties
do
431 # FIXME skip invisible constructors
432 if not p
.global
.is_init_for
(self) then continue
433 assert p
isa MMMethod
435 var cname
= "NEW_{self}_{p.global.intro.cname}"
436 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
439 v
.add_instr
(init_table_decl
)
440 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
441 v
.add_instr
("return {e};")
442 ctx_old
.append
(v
.ctx
)
447 else if not pi
.tagged
then
449 var tbox
= "struct TBOX_{name}"
450 v
.add_instr
("val_t BOX_{name}({t} val) \{")
452 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
453 v
.add_instr
("box->vft = VFT_{name};")
454 v
.add_instr
("box->val = val;")
455 v
.add_instr
("box->object_id = object_id_counter;")
456 v
.add_instr
("object_id_counter = object_id_counter + 1;")
457 v
.add_instr
("return OBJ2VAL(box);")
465 fun compile_property_to_c
(v
: CompilerVisitor)
470 var more_params
: nullable String = null
471 if global
.is_init
then more_params
= "int* init_table"
472 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
478 var itpos
: nullable String = null
479 if global
.is_init
then
480 itpos
= "itpos{v.new_number}"
481 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
482 v
.add_instr
("if (init_table[{itpos}]) return;")
485 var s
= ir
.compile_to_c
(v
, cname
, args
)
487 if itpos
!= null then
488 v
.add_instr
("init_table[{itpos}] = 1;")
491 v
.add_instr
("return;")
493 v
.add_instr
("return ", s
, ";")
496 ctx_old
.append
(v
.ctx
)
501 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)