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 for c
in module.local_classes
do
32 c
.compile_tables_to_c
(v
)
34 var s
= new Buffer.from
("classtable_t TAG2VFT[4] = \{NULL")
35 for t
in ["Int","Char","Bool"] do
36 if module.has_global_class_named
(t
.to_symbol
) then
37 s
.append
(", (const classtable_t)VFT_{t}")
46 # Compile main part (for _table.c)
47 fun compile_main_part
(v
: CompilerVisitor)
49 v
.add_instr
("int main(int argc, char **argv) \{")
51 v
.add_instr
("prepare_signals();")
52 v
.add_instr
("glob_argc = argc; glob_argv = argv;")
53 if v
.program
.main_method
== null then
56 v
.add_instr
("G_sys = NEW_Sys();")
57 v
.add_instr
("register_static_object(&G_sys);")
58 v
.add_instr
("{v.program.main_method.cname}(G_sys);")
60 v
.add_instr
("return 0;")
67 # Declare class table (for _sep.h or _glob.h)
68 fun declare_class_tables_to_c
(v
: CompilerVisitor)
70 for c
in local_classes
do
71 if c
.global
.module == self then
72 c
.declare_tables_to_c
(v
)
78 fun compile_mod_to_c
(v
: CompilerVisitor)
80 v
.add_decl
("extern const char *LOCATE_{name};")
81 if not v
.program
.tc
.use_SFT_optimization
then
82 v
.add_decl
("extern const int SFT_{name}[];")
85 for e
in local_table
do
87 if v
.program
.tc
.use_SFT_optimization
then
88 value
= "{e.value(v.program)}"
90 value
= "SFT_{name}[{i}]"
93 e
.compile_macros
(v
, value
)
95 for c
in local_classes
do
96 if not c
isa MMConcreteClass then continue
97 for pg
in c
.global_properties
do
99 if p
.local_class
== c
and p
isa MMMethod then
100 p
.compile_property_to_c
(v
)
102 if pg
.is_init_for
(c
) then
103 # Declare constructors
104 var params
= new Array[String]
105 for j
in [0..p
.signature
.arity
[ do
106 params
.add
("val_t p{j}")
108 v
.add_decl
("val_t NEW_{c}_{p.global.intro.cname}({params.join(", ")});")
114 # Compile module file for the current module
115 fun compile_local_table_to_c
(v
: CompilerVisitor)
117 v
.add_instr
("const char *LOCATE_{name} = \"{location.file}\
";")
119 if v
.program
.tc
.use_SFT_optimization
or local_table
.is_empty
then
123 v
.add_instr
("const int SFT_{name}[{local_table.length}] = \{")
125 for e
in local_table
do
126 v
.add_instr
(e
.value
(v
.program
) + ",")
133 ###############################################################################
135 redef class AbsTableElt
136 # Compile the macro needed to use the element and other related elements
137 fun compile_macros
(v
: CompilerVisitor, value
: String) is abstract
141 # Return the value of the element for a given class
142 fun compile_to_c
(v
: CompilerVisitor, c
: MMLocalClass): String is abstract
145 redef class ModuleTableElt
146 # Return the value of the element once the global analisys is performed
147 fun value
(prog
: Program): String is abstract
150 redef class ModuleTableEltGroup
151 redef fun value
(prog
) do return "{prog.table_information.color(elements.first)} /* Group of ? */"
152 redef fun compile_macros
(v
, value
)
156 e
.compile_macros
(v
, "{value} + {i}")
162 redef class TableEltMeth
163 redef fun compile_macros
(v
, value
)
165 var pg
= property
.global
166 v
.add_decl
("#define {pg.meth_call}(recv) (({pg.intro.cname}_t)CALL((recv), ({value})))")
169 redef fun compile_to_c
(v
, c
)
171 var p
= c
[property
.global
]
176 redef class TableEltSuper
177 redef fun compile_macros
(v
, value
)
180 v
.add_decl
("#define {p.super_meth_call}(recv) (({p.cname}_t)CALL((recv), ({value})))")
183 redef fun compile_to_c
(v
, c
)
185 var pc
= property
.local_class
186 var g
= property
.global
187 var lin
= c
.che
.linear_extension
190 #print "{c.module}::{c} for {pc.module}::{pc}::{_property} try {s.module}:{s}"
193 else if found
and c
.che
< s
then
194 if s
.has_global_property
(g
) then
195 #print "found {s.module}::{s}::{p}"
204 redef class TableEltAttr
205 redef fun compile_macros
(v
, value
)
207 var pg
= property
.global
208 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
211 redef fun compile_to_c
(v
, c
)
214 var p
= c
[property
.global
]
215 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
219 redef class AbsTableEltClass
220 # The C macro name refering the value
221 fun symbol
: String is abstract
223 redef fun compile_macros
(v
, value
)
225 v
.add_decl
("#define {symbol} ({value})")
229 redef class TableEltClassId
230 redef fun symbol
do return local_class
.global
.id_id
232 redef fun value
(prog
)
234 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
238 redef class TableEltClassInitTable
239 redef fun symbol
do return local_class
.global
.init_table_pos_id
241 redef fun compile_to_c
(v
, c
)
244 var cc
= prog
.compiled_classes
[local_class
.global
]
245 var linext
= c
.cshe
.reverse_linear_extension
247 while linext
[i
].global
!= local_class
.global
do
250 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
254 redef class TableEltClassColor
255 redef fun symbol
do return local_class
.global
.color_id
257 redef fun value
(prog
)
259 return "{prog.table_information.color(self)} /* Color of {local_class} */"
262 redef fun compile_to_c
(v
, c
)
265 var cc
= prog
.compiled_classes
[local_class
.global
]
266 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
270 redef class TableEltComposite
271 redef fun compile_to_c
(v
, c
) do abort
274 redef class TableEltClassSelfId
275 redef fun compile_to_c
(v
, c
)
278 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
282 redef class TableEltClassObjectSize
283 redef fun compile_to_c
(v
, c
)
287 if c
.name
== "NativeArray".to_symbol
then
290 var cc
= p
.compiled_classes
[c
.global
]
291 var itab
= cc
.instance_table
296 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
300 redef class TableEltObjectId
301 redef fun compile_to_c
(v
, c
)
304 return "/* {p.table_information.color(self)}: Object_id */"
308 redef class TableEltVftPointer
309 redef fun compile_to_c
(v
, c
)
312 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
316 ###############################################################################
318 redef class MMLocalClass
319 # Declaration and macros related to the class table
320 fun declare_tables_to_c
(v
: CompilerVisitor)
323 var pi
= primitive_info
324 v
.add_decl
("extern const classtable_elt_t VFT_{name}[];")
325 if name
== "NativeArray".to_symbol
then
326 v
.add_decl
("val_t NEW_NativeArray(size_t length, size_t size);")
327 else if pi
== null then
328 # v.add_decl("val_t NEW_{name}(void);")
329 else if 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_{name}[{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_{name};")
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__{name}"
388 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
391 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
392 ctx_old
.append
(v
.ctx
)
398 # Generate NEW routine
399 v
.add_decl
("val_t NEW_{name}(void);")
400 v
.add_instr
("val_t NEW_{name}(void)")
403 v
.add_instr
("obj_t obj;")
404 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
405 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{name};")
406 v
.add_instr
("obj[1].object_id = object_id_counter;")
407 v
.add_instr
("object_id_counter = object_id_counter + 1;")
408 v
.add_instr
("return OBJ2VAL(obj);")
414 var cname
= "CHECKNEW_{name}"
415 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
418 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
419 ctx_old
.append
(v
.ctx
)
425 var init_table_size
= cshe
.greaters
.length
+ 1
426 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
428 for g
in global_properties
do
430 # FIXME skip invisible constructors
431 if not p
.global
.is_init_for
(self) then continue
432 assert p
isa MMMethod
434 var cname
= "NEW_{self}_{p.global.intro.cname}"
435 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
438 v
.add_instr
(init_table_decl
)
439 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
440 v
.add_instr
("return {e};")
441 ctx_old
.append
(v
.ctx
)
446 else if not pi
.tagged
then
448 var tbox
= "struct TBOX_{name}"
449 v
.add_instr
("val_t BOX_{name}({t} val) \{")
451 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
452 v
.add_instr
("box->vft = VFT_{name};")
453 v
.add_instr
("box->val = val;")
454 v
.add_instr
("box->object_id = object_id_counter;")
455 v
.add_instr
("object_id_counter = object_id_counter + 1;")
456 v
.add_instr
("return OBJ2VAL(box);")
464 fun compile_property_to_c
(v
: CompilerVisitor)
469 var more_params
: nullable String = null
470 if global
.is_init
then more_params
= "int* init_table"
471 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
477 var itpos
: nullable String = null
478 if global
.is_init
then
479 itpos
= "itpos{v.new_number}"
480 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
481 v
.add_instr
("if (init_table[{itpos}]) return;")
484 var s
= ir
.compile_to_c
(v
, cname
, args
)
486 if itpos
!= null then
487 v
.add_instr
("init_table[{itpos}] = 1;")
490 v
.add_instr
("return;")
492 v
.add_instr
("return ", s
, ";")
495 ctx_old
.append
(v
.ctx
)
500 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)