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 TableEltVTClassColor
209 redef fun compile_macros
(v
, value
)
211 var pg
= property
.global
212 v
.add_decl
("#define {pg.vt_class_color}(recv) (VAL2VFT(recv)[{value}].i)")
215 redef fun compile_to_c
(v
, c
)
218 var p
= c
[property
.global
]
219 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
220 var col
= g
.intro
.as(MMConcreteClass).class_color_pos
221 return "{prog.table_information.color(col)} /* {prog.table_information.color(self)}: VT {c}::{p} : color of {g} */"
225 redef class TableEltVTClassId
226 redef fun compile_macros
(v
, value
)
228 var pg
= property
.global
229 v
.add_decl
("#define {pg.vt_class_id}(recv) (VAL2VFT(recv)[{value}].i)")
232 redef fun compile_to_c
(v
, c
)
235 var p
= c
[property
.global
]
236 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
237 return "{prog.compiled_classes[g].id} /* {prog.table_information.color(self)}: VT {c}::{p} : id of {g} */"
241 redef class TableEltAttr
242 redef fun compile_macros
(v
, value
)
244 var pg
= property
.global
245 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
248 redef fun compile_to_c
(v
, c
)
251 var p
= c
[property
.global
]
252 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
257 redef class AbsTableEltClass
258 # The C macro name refering the value
259 fun symbol
: String is abstract
261 redef fun compile_macros
(v
, value
)
263 v
.add_decl
("#define {symbol} ({value})")
267 redef class TableEltClassId
268 redef fun symbol
do return local_class
.global
.id_id
270 redef fun value
(prog
)
272 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
276 redef class TableEltClassInitTable
277 redef fun symbol
do return local_class
.global
.init_table_pos_id
279 redef fun compile_to_c
(v
, c
)
282 var cc
= prog
.compiled_classes
[local_class
.global
]
283 var linext
= c
.cshe
.reverse_linear_extension
285 while linext
[i
].global
!= local_class
.global
do
288 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
292 redef class TableEltClassColor
293 redef fun symbol
do return local_class
.global
.color_id
295 redef fun value
(prog
)
297 return "{prog.table_information.color(self)} /* Color of {local_class} */"
300 redef fun compile_to_c
(v
, c
)
303 var cc
= prog
.compiled_classes
[local_class
.global
]
304 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
308 redef class TableEltComposite
309 redef fun compile_to_c
(v
, c
) do abort
312 redef class TableEltClassSelfId
313 redef fun compile_to_c
(v
, c
)
316 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
320 redef class TableEltClassSelfName
321 redef fun compile_to_c
(v
, c
)
324 return "\"{c.global.name}\
" /* {prog.table_information.color(self)}: Class Name */"
328 redef class TableEltClassObjectSize
329 redef fun compile_to_c
(v
, c
)
333 if c
.name
== "NativeArray".to_symbol
then
336 var cc
= p
.compiled_classes
[c
.global
]
337 var itab
= cc
.instance_table
342 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
346 redef class TableEltObjectId
347 redef fun compile_to_c
(v
, c
)
350 return "/* {p.table_information.color(self)}: Object_id */"
354 redef class TableEltVftPointer
355 redef fun compile_to_c
(v
, c
)
358 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
362 ###############################################################################
364 redef class MMLocalClass
365 # Declaration and macros related to the class table
366 fun declare_tables_to_c
(v
: CompilerVisitor)
369 var pi
= primitive_info
370 v
.add_decl
("extern const classtable_elt_t VFT_{cname}[];")
371 if pi
!= null and not pi
.tagged
then
373 var tbox
= "struct TBOX_{name}"
374 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
375 v
.add_decl
("val_t BOX_{name}({t} val);")
376 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
380 # Compilation of table and new (or box)
381 fun compile_tables_to_c
(v
: CompilerVisitor)
383 var cc
= v
.program
.compiled_classes
[self.global
]
384 var ctab
= cc
.class_table
385 var clen
= ctab
.length
386 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
387 clen
= v
.program
.table_information
.max_class_table_length
390 v
.add_instr
("const classtable_elt_t VFT_{cname}[{clen}] = \{")
394 v
.add_instr
("\{0} /* Class Hole :( */,")
396 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
399 if clen
> ctab
.length
then
400 v
.add_instr
("\{0},"*(clen-ctab
.length
))
404 var itab
= cc
.instance_table
407 v
.add_instr
("/* Instance Hole :( */")
409 v
.add_instr
(e
.compile_to_c
(v
, self))
413 var pi
= primitive_info
414 if name
== "NativeArray".to_symbol
then
415 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
417 v
.add_instr
("Nit_NativeArray array;")
418 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
419 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{cname};")
420 v
.add_instr
("array->object_id = object_id_counter;")
421 v
.add_instr
("object_id_counter = object_id_counter + 1;")
422 v
.add_instr
("array->size = length;")
423 v
.add_instr
("return OBJ2VAL(array);")
426 else if pi
== null then
428 # Generate INIT_ATTRIBUTES routine
429 var cname
= "INIT_ATTRIBUTES__{cname}"
430 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
431 var decl_writer_old
= v
.decl_writer
432 v
.decl_writer
= v
.writer
.sub
433 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
434 v
.decl_writer
= decl_writer_old
439 # Generate NEW routine
440 v
.add_decl
("val_t NEW_{cname}(void);")
441 v
.add_instr
("val_t NEW_{cname}(void)")
444 v
.add_instr
("obj_t obj;")
445 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
446 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{cname};")
447 v
.add_instr
("obj[1].object_id = object_id_counter;")
448 v
.add_instr
("object_id_counter = object_id_counter + 1;")
449 v
.add_instr
("return OBJ2VAL(obj);")
455 var cname
= "CHECKNEW_{cname}"
456 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
457 var decl_writer_old
= v
.decl_writer
458 v
.decl_writer
= v
.writer
.sub
459 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
460 v
.decl_writer
= decl_writer_old
465 var init_table_size
= cshe
.greaters
.length
+ 1
466 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
468 for g
in global_properties
do
470 # FIXME skip invisible constructors
471 if not p
.global
.is_init_for
(self) then continue
472 assert p
isa MMMethod
474 var cname
= "NEW_{self}_{p.global.intro.cname}"
475 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
476 var decl_writer_old
= v
.decl_writer
477 v
.decl_writer
= v
.writer
.sub
478 v
.add_instr
(init_table_decl
)
479 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
480 v
.add_instr
("return {e};")
481 v
.decl_writer
= decl_writer_old
485 else if not pi
.tagged
then
487 var tbox
= "struct TBOX_{name}"
488 v
.add_instr
("val_t BOX_{name}({t} val) \{")
490 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
491 v
.add_instr
("box->vft = VFT_{cname};")
492 v
.add_instr
("box->val = val;")
493 v
.add_instr
("box->object_id = object_id_counter;")
494 v
.add_instr
("object_id_counter = object_id_counter + 1;")
495 v
.add_instr
("return OBJ2VAL(box);")
503 fun compile_property_to_c
(v
: CompilerVisitor)
508 var more_params
: nullable String = null
509 if global
.is_init
then more_params
= "int* init_table"
510 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
511 var writer_old
= v
.writer
512 v
.writer
= v
.writer
.sub
513 var decl_writer_old
= v
.decl_writer
514 v
.decl_writer
= v
.writer
.sub
516 var itpos
: nullable String = null
517 if global
.is_init
then
518 itpos
= "itpos{v.new_number}"
519 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
520 v
.add_instr
("if (init_table[{itpos}]) return;")
523 var s
= ir
.compile_to_c
(v
, cname
, args
)
525 if itpos
!= null then
526 v
.add_instr
("init_table[{itpos}] = 1;")
529 v
.add_instr
("return;")
531 v
.add_instr
("return {s};")
536 v
.writer
= writer_old
537 v
.decl_writer
= decl_writer_old