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 s
.append
(", (const classtable_t)VFT_{t}")
48 # Compile main part (for _table.c)
49 fun compile_main_part
(v
: CompilerVisitor)
51 v
.add_instr
("int main(int argc, char **argv) \{")
53 v
.add_instr
("prepare_signals();")
54 v
.add_instr
("glob_argc = argc; glob_argv = argv;")
55 if v
.program
.main_method
== null then
58 v
.add_instr
("G_sys = NEW_Sys();")
59 v
.add_instr
("register_static_object(&G_sys);")
60 v
.add_instr
("{v.program.main_method.cname}(G_sys);")
62 v
.add_instr
("return 0;")
69 # Declare class table (for _sep.h or _glob.h)
70 fun declare_class_tables_to_c
(v
: CompilerVisitor)
72 for c
in local_classes
do
73 if c
.global
.mmmodule
== self then
74 c
.declare_tables_to_c
(v
)
80 fun compile_mod_to_c
(v
: CompilerVisitor)
82 v
.add_decl
("extern const char *LOCATE_{name};")
83 if not v
.program
.tc
.use_SFT_optimization
then
84 v
.add_decl
("extern const int SFT_{name}[];")
87 for e
in local_table
do
89 if v
.program
.tc
.use_SFT_optimization
then
90 value
= "{e.value(v.program)}"
92 value
= "SFT_{name}[{i}]"
95 e
.compile_macros
(v
, value
)
97 for c
in local_classes
do
98 if not c
isa MMConcreteClass then continue
99 for pg
in c
.global_properties
do
101 if p
.local_class
== c
and p
isa MMMethod then
102 p
.compile_property_to_c
(v
)
104 if pg
.is_init_for
(c
) then
105 # Declare constructors
106 var params
= new Array[String]
107 for j
in [0..p
.signature
.arity
[ do
108 params
.add
("val_t p{j}")
110 v
.add_decl
("val_t NEW_{c}_{p.global.intro.cname}({params.join(", ")});")
116 # Compile module file for the current module
117 fun compile_local_table_to_c
(v
: CompilerVisitor)
119 v
.add_instr
("const char *LOCATE_{name} = \"{location.file}\
";")
121 if v
.program
.tc
.use_SFT_optimization
or local_table
.is_empty
then
125 v
.add_instr
("const int SFT_{name}[{local_table.length}] = \{")
127 for e
in local_table
do
128 v
.add_instr
(e
.value
(v
.program
) + ",")
135 ###############################################################################
137 redef class AbsTableElt
138 # Compile the macro needed to use the element and other related elements
139 fun compile_macros
(v
: CompilerVisitor, value
: String) is abstract
143 # Return the value of the element for a given class
144 fun compile_to_c
(v
: CompilerVisitor, c
: MMLocalClass): String is abstract
147 redef class ModuleTableElt
148 # Return the value of the element once the global analisys is performed
149 fun value
(prog
: Program): String is abstract
152 redef class ModuleTableEltGroup
153 redef fun value
(prog
) do return "{prog.table_information.color(elements.first)} /* Group of ? */"
154 redef fun compile_macros
(v
, value
)
158 e
.compile_macros
(v
, "{value} + {i}")
164 redef class TableEltMeth
165 redef fun compile_macros
(v
, value
)
167 var pg
= property
.global
168 v
.add_decl
("#define {pg.meth_call}(recv) (({pg.intro.cname}_t)CALL((recv), ({value})))")
171 redef fun compile_to_c
(v
, c
)
173 var p
= c
[property
.global
]
178 redef class TableEltSuper
179 redef fun compile_macros
(v
, value
)
182 v
.add_decl
("#define {p.super_meth_call}(recv) (({p.cname}_t)CALL((recv), ({value})))")
185 redef fun compile_to_c
(v
, c
)
187 var pc
= property
.local_class
188 var g
= property
.global
189 var lin
= c
.che
.linear_extension
192 #print "{c.module}::{c} for {pc.module}::{pc}::{_property} try {s.module}:{s}"
195 else if found
and c
.che
< s
then
196 if s
.has_global_property
(g
) then
197 #print "found {s.module}::{s}::{p}"
206 redef class TableEltVTClassColor
207 redef fun compile_macros
(v
, value
)
209 var pg
= property
.global
210 v
.add_decl
("#define {pg.vt_class_color}(recv) (VAL2VFT(recv)[{value}].i)")
213 redef fun compile_to_c
(v
, c
)
216 var p
= c
[property
.global
]
217 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
218 var col
= g
.intro
.as(MMConcreteClass).class_color_pos
219 return "{prog.table_information.color(col)} /* {prog.table_information.color(self)}: VT {c}::{p} : color of {g} */"
223 redef class TableEltVTClassId
224 redef fun compile_macros
(v
, value
)
226 var pg
= property
.global
227 v
.add_decl
("#define {pg.vt_class_id}(recv) (VAL2VFT(recv)[{value}].i)")
230 redef fun compile_to_c
(v
, c
)
233 var p
= c
[property
.global
]
234 var g
= p
.signature_for
(c
.get_type
).return_type
.local_class
.global
235 return "{prog.compiled_classes[g].id} /* {prog.table_information.color(self)}: VT {c}::{p} : id of {g} */"
239 redef class TableEltAttr
240 redef fun compile_macros
(v
, value
)
242 var pg
= property
.global
243 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
246 redef fun compile_to_c
(v
, c
)
249 var p
= c
[property
.global
]
250 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
255 redef class AbsTableEltClass
256 # The C macro name refering the value
257 fun symbol
: String is abstract
259 redef fun compile_macros
(v
, value
)
261 v
.add_decl
("#define {symbol} ({value})")
265 redef class TableEltClassId
266 redef fun symbol
do return local_class
.global
.id_id
268 redef fun value
(prog
)
270 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
274 redef class TableEltClassInitTable
275 redef fun symbol
do return local_class
.global
.init_table_pos_id
277 redef fun compile_to_c
(v
, c
)
280 var cc
= prog
.compiled_classes
[local_class
.global
]
281 var linext
= c
.cshe
.reverse_linear_extension
283 while linext
[i
].global
!= local_class
.global
do
286 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
290 redef class TableEltClassColor
291 redef fun symbol
do return local_class
.global
.color_id
293 redef fun value
(prog
)
295 return "{prog.table_information.color(self)} /* Color of {local_class} */"
298 redef fun compile_to_c
(v
, c
)
301 var cc
= prog
.compiled_classes
[local_class
.global
]
302 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
306 redef class TableEltComposite
307 redef fun compile_to_c
(v
, c
) do abort
310 redef class TableEltClassSelfId
311 redef fun compile_to_c
(v
, c
)
314 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
318 redef class TableEltClassObjectSize
319 redef fun compile_to_c
(v
, c
)
323 if c
.name
== "NativeArray".to_symbol
then
326 var cc
= p
.compiled_classes
[c
.global
]
327 var itab
= cc
.instance_table
332 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
336 redef class TableEltObjectId
337 redef fun compile_to_c
(v
, c
)
340 return "/* {p.table_information.color(self)}: Object_id */"
344 redef class TableEltVftPointer
345 redef fun compile_to_c
(v
, c
)
348 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
352 ###############################################################################
354 redef class MMLocalClass
355 # Declaration and macros related to the class table
356 fun declare_tables_to_c
(v
: CompilerVisitor)
359 var pi
= primitive_info
360 v
.add_decl
("extern const classtable_elt_t VFT_{name}[];")
361 if pi
!= null and not pi
.tagged
then
363 var tbox
= "struct TBOX_{name}"
364 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
365 v
.add_decl
("val_t BOX_{name}({t} val);")
366 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
370 # Compilation of table and new (or box)
371 fun compile_tables_to_c
(v
: CompilerVisitor)
373 var cc
= v
.program
.compiled_classes
[self.global
]
374 var ctab
= cc
.class_table
375 var clen
= ctab
.length
376 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
377 clen
= v
.program
.table_information
.max_class_table_length
380 v
.add_instr
("const classtable_elt_t VFT_{name}[{clen}] = \{")
384 v
.add_instr
("\{0} /* Class Hole :( */,")
386 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
389 if clen
> ctab
.length
then
390 v
.add_instr
("\{0},"*(clen-ctab
.length
))
394 var itab
= cc
.instance_table
397 v
.add_instr
("/* Instance Hole :( */")
399 v
.add_instr
(e
.compile_to_c
(v
, self))
403 var pi
= primitive_info
404 if name
== "NativeArray".to_symbol
then
405 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
407 v
.add_instr
("Nit_NativeArray array;")
408 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
409 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{name};")
410 v
.add_instr
("array->object_id = object_id_counter;")
411 v
.add_instr
("object_id_counter = object_id_counter + 1;")
412 v
.add_instr
("array->size = length;")
413 v
.add_instr
("return OBJ2VAL(array);")
416 else if pi
== null then
418 # Generate INIT_ATTRIBUTES routine
419 var cname
= "INIT_ATTRIBUTES__{name}"
420 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
421 var decl_writer_old
= v
.decl_writer
422 v
.decl_writer
= v
.writer
.sub
423 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
424 v
.decl_writer
= decl_writer_old
429 # Generate NEW routine
430 v
.add_decl
("val_t NEW_{name}(void);")
431 v
.add_instr
("val_t NEW_{name}(void)")
434 v
.add_instr
("obj_t obj;")
435 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
436 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{name};")
437 v
.add_instr
("obj[1].object_id = object_id_counter;")
438 v
.add_instr
("object_id_counter = object_id_counter + 1;")
439 v
.add_instr
("return OBJ2VAL(obj);")
445 var cname
= "CHECKNEW_{name}"
446 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
447 var decl_writer_old
= v
.decl_writer
448 v
.decl_writer
= v
.writer
.sub
449 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
450 v
.decl_writer
= decl_writer_old
455 var init_table_size
= cshe
.greaters
.length
+ 1
456 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
458 for g
in global_properties
do
460 # FIXME skip invisible constructors
461 if not p
.global
.is_init_for
(self) then continue
462 assert p
isa MMMethod
464 var cname
= "NEW_{self}_{p.global.intro.cname}"
465 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
466 var decl_writer_old
= v
.decl_writer
467 v
.decl_writer
= v
.writer
.sub
468 v
.add_instr
(init_table_decl
)
469 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
470 v
.add_instr
("return {e};")
471 v
.decl_writer
= decl_writer_old
475 else if not pi
.tagged
then
477 var tbox
= "struct TBOX_{name}"
478 v
.add_instr
("val_t BOX_{name}({t} val) \{")
480 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
481 v
.add_instr
("box->vft = VFT_{name};")
482 v
.add_instr
("box->val = val;")
483 v
.add_instr
("box->object_id = object_id_counter;")
484 v
.add_instr
("object_id_counter = object_id_counter + 1;")
485 v
.add_instr
("return OBJ2VAL(box);")
493 fun compile_property_to_c
(v
: CompilerVisitor)
498 var more_params
: nullable String = null
499 if global
.is_init
then more_params
= "int* init_table"
500 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
501 var writer_old
= v
.writer
502 v
.writer
= v
.writer
.sub
503 var decl_writer_old
= v
.decl_writer
504 v
.decl_writer
= v
.writer
.sub
506 var itpos
: nullable String = null
507 if global
.is_init
then
508 itpos
= "itpos{v.new_number}"
509 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
510 v
.add_instr
("if (init_table[{itpos}]) return;")
513 var s
= ir
.compile_to_c
(v
, cname
, args
)
515 if itpos
!= null then
516 v
.add_instr
("init_table[{itpos}] = 1;")
519 v
.add_instr
("return;")
521 v
.add_instr
("return {s};")
526 v
.writer
= writer_old
527 v
.decl_writer
= decl_writer_old