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 pi
!= null and not pi
.tagged
then
328 var tbox
= "struct TBOX_{name}"
329 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
330 v
.add_decl
("val_t BOX_{name}({t} val);")
331 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
335 # Compilation of table and new (or box)
336 fun compile_tables_to_c
(v
: CompilerVisitor)
338 var cc
= v
.program
.compiled_classes
[self.global
]
339 var ctab
= cc
.class_table
340 var clen
= ctab
.length
341 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
342 clen
= v
.program
.table_information
.max_class_table_length
345 v
.add_instr
("const classtable_elt_t VFT_{name}[{clen}] = \{")
349 v
.add_instr
("\{0} /* Class Hole :( */,")
351 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
354 if clen
> ctab
.length
then
355 v
.add_instr
("\{0},"*(clen-ctab
.length
))
359 var itab
= cc
.instance_table
362 v
.add_instr
("/* Instance Hole :( */")
364 v
.add_instr
(e
.compile_to_c
(v
, self))
368 var pi
= primitive_info
369 if name
== "NativeArray".to_symbol
then
370 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
372 v
.add_instr
("Nit_NativeArray array;")
373 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
374 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{name};")
375 v
.add_instr
("array->object_id = object_id_counter;")
376 v
.add_instr
("object_id_counter = object_id_counter + 1;")
377 v
.add_instr
("array->size = length;")
378 v
.add_instr
("return OBJ2VAL(array);")
381 else if pi
== null then
383 # Generate INIT_ATTRIBUTES routine
384 var cname
= "INIT_ATTRIBUTES__{name}"
385 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
386 var decl_writer_old
= v
.decl_writer
387 v
.decl_writer
= v
.writer
.sub
388 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
389 v
.decl_writer
= decl_writer_old
394 # Generate NEW routine
395 v
.add_decl
("val_t NEW_{name}(void);")
396 v
.add_instr
("val_t NEW_{name}(void)")
399 v
.add_instr
("obj_t obj;")
400 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
401 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{name};")
402 v
.add_instr
("obj[1].object_id = object_id_counter;")
403 v
.add_instr
("object_id_counter = object_id_counter + 1;")
404 v
.add_instr
("return OBJ2VAL(obj);")
410 var cname
= "CHECKNEW_{name}"
411 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
412 var decl_writer_old
= v
.decl_writer
413 v
.decl_writer
= v
.writer
.sub
414 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
415 v
.decl_writer
= decl_writer_old
420 var init_table_size
= cshe
.greaters
.length
+ 1
421 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
423 for g
in global_properties
do
425 # FIXME skip invisible constructors
426 if not p
.global
.is_init_for
(self) then continue
427 assert p
isa MMMethod
429 var cname
= "NEW_{self}_{p.global.intro.cname}"
430 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
431 var decl_writer_old
= v
.decl_writer
432 v
.decl_writer
= v
.writer
.sub
433 v
.add_instr
(init_table_decl
)
434 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
435 v
.add_instr
("return {e};")
436 v
.decl_writer
= decl_writer_old
440 else if not pi
.tagged
then
442 var tbox
= "struct TBOX_{name}"
443 v
.add_instr
("val_t BOX_{name}({t} val) \{")
445 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
446 v
.add_instr
("box->vft = VFT_{name};")
447 v
.add_instr
("box->val = val;")
448 v
.add_instr
("box->object_id = object_id_counter;")
449 v
.add_instr
("object_id_counter = object_id_counter + 1;")
450 v
.add_instr
("return OBJ2VAL(box);")
458 fun compile_property_to_c
(v
: CompilerVisitor)
463 var more_params
: nullable String = null
464 if global
.is_init
then more_params
= "int* init_table"
465 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
466 var writer_old
= v
.writer
467 v
.writer
= v
.writer
.sub
468 var decl_writer_old
= v
.decl_writer
469 v
.decl_writer
= v
.writer
.sub
471 var itpos
: nullable String = null
472 if global
.is_init
then
473 itpos
= "itpos{v.new_number}"
474 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
475 v
.add_instr
("if (init_table[{itpos}]) return;")
478 var s
= ir
.compile_to_c
(v
, cname
, args
)
480 if itpos
!= null then
481 v
.add_instr
("init_table[{itpos}] = 1;")
484 v
.add_instr
("return;")
486 v
.add_instr
("return {s};")
491 v
.writer
= writer_old
492 v
.decl_writer
= decl_writer_old