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.filename}\
";")
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 TableEltAttr
207 redef fun compile_macros
(v
, value
)
209 var pg
= property
.global
210 v
.add_decl
("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
213 redef fun compile_to_c
(v
, c
)
216 var p
= c
[property
.global
]
217 return "/* {prog.table_information.color(self)}: Attribute {c}::{p} */"
221 redef class AbsTableEltClass
222 # The C macro name refering the value
223 fun symbol
: String is abstract
225 redef fun compile_macros
(v
, value
)
227 v
.add_decl
("#define {symbol} ({value})")
231 redef class TableEltClassId
232 redef fun symbol
do return local_class
.global
.id_id
234 redef fun value
(prog
)
236 return "{prog.compiled_classes[local_class.global].id} /* Id of {local_class} */"
240 redef class TableEltClassInitTable
241 redef fun symbol
do return local_class
.global
.init_table_pos_id
243 redef fun compile_to_c
(v
, c
)
246 var cc
= prog
.compiled_classes
[local_class
.global
]
247 var linext
= c
.cshe
.reverse_linear_extension
249 while linext
[i
].global
!= local_class
.global
do
252 return "{i} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
256 redef class TableEltClassColor
257 redef fun symbol
do return local_class
.global
.color_id
259 redef fun value
(prog
)
261 return "{prog.table_information.color(self)} /* Color of {local_class} */"
264 redef fun compile_to_c
(v
, c
)
267 var cc
= prog
.compiled_classes
[local_class
.global
]
268 return "{cc.id} /* {prog.table_information.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
272 redef class TableEltComposite
273 redef fun compile_to_c
(v
, c
) do abort
276 redef class TableEltClassSelfId
277 redef fun compile_to_c
(v
, c
)
280 return "{prog.compiled_classes[c.global].id} /* {prog.table_information.color(self)}: Identity */"
284 redef class TableEltClassSelfName
285 redef fun compile_to_c
(v
, c
)
288 return "\"{c.global.name}\
" /* {prog.table_information.color(self)}: Class Name */"
292 redef class TableEltClassObjectSize
293 redef fun compile_to_c
(v
, c
)
297 if c
.name
== "NativeArray".to_symbol
then
300 var cc
= p
.compiled_classes
[c
.global
]
301 var itab
= cc
.instance_table
306 return "{nb} /* {p.table_information.color(self)}: Object size (-1 if a NativeArray)*/"
310 redef class TableEltObjectId
311 redef fun compile_to_c
(v
, c
)
314 return "/* {p.table_information.color(self)}: Object_id */"
318 redef class TableEltVftPointer
319 redef fun compile_to_c
(v
, c
)
322 return "/* {prog.table_information.color(self)}: Pointer to the classtable */"
326 ###############################################################################
328 redef class MMLocalClass
329 # Declaration and macros related to the class table
330 fun declare_tables_to_c
(v
: CompilerVisitor)
333 var pi
= primitive_info
334 v
.add_decl
("extern const classtable_elt_t VFT_{name}[];")
335 if pi
!= null and not pi
.tagged
then
337 var tbox
= "struct TBOX_{name}"
338 v
.add_decl
("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
339 v
.add_decl
("val_t BOX_{name}({t} val);")
340 v
.add_decl
("#define UNBOX_{name}(x) ((({tbox} *)(VAL2OBJ(x)))->val)")
344 # Compilation of table and new (or box)
345 fun compile_tables_to_c
(v
: CompilerVisitor)
347 var cc
= v
.program
.compiled_classes
[self.global
]
348 var ctab
= cc
.class_table
349 var clen
= ctab
.length
350 if v
.program
.table_information
.max_class_table_length
> ctab
.length
then
351 clen
= v
.program
.table_information
.max_class_table_length
354 v
.add_instr
("const classtable_elt_t VFT_{name}[{clen}] = \{")
358 v
.add_instr
("\{0} /* Class Hole :( */,")
360 v
.add_instr
("\{(bigint) {e.compile_to_c(v, self)}},")
363 if clen
> ctab
.length
then
364 v
.add_instr
("\{0},"*(clen-ctab
.length
))
368 var itab
= cc
.instance_table
371 v
.add_instr
("/* Instance Hole :( */")
373 v
.add_instr
(e
.compile_to_c
(v
, self))
377 var pi
= primitive_info
378 if name
== "NativeArray".to_symbol
then
379 v
.add_instr
("val_t NEW_NativeArray(size_t length, size_t size) \{")
381 v
.add_instr
("Nit_NativeArray array;")
382 v
.add_instr
("array = (Nit_NativeArray)alloc(sizeof(struct Nit_NativeArray) + ((length - 1) * size));")
383 v
.add_instr
("array->vft = (classtable_elt_t*)VFT_{name};")
384 v
.add_instr
("array->object_id = object_id_counter;")
385 v
.add_instr
("object_id_counter = object_id_counter + 1;")
386 v
.add_instr
("array->size = length;")
387 v
.add_instr
("return OBJ2VAL(array);")
390 else if pi
== null then
392 # Generate INIT_ATTRIBUTES routine
393 var cname
= "INIT_ATTRIBUTES__{name}"
394 var args
= init_var_iroutine
.compile_signature_to_c
(v
, cname
, "init var of {name}", null, null)
395 var decl_writer_old
= v
.decl_writer
396 v
.decl_writer
= v
.writer
.sub
397 init_var_iroutine
.compile_to_c
(v
, cname
, args
)
398 v
.decl_writer
= decl_writer_old
403 # Generate NEW routine
404 v
.add_decl
("val_t NEW_{name}(void);")
405 v
.add_instr
("val_t NEW_{name}(void)")
408 v
.add_instr
("obj_t obj;")
409 v
.add_instr
("obj = alloc(sizeof(val_t) * {itab.length});")
410 v
.add_instr
("obj->vft = (classtable_elt_t*)VFT_{name};")
411 v
.add_instr
("obj[1].object_id = object_id_counter;")
412 v
.add_instr
("object_id_counter = object_id_counter + 1;")
413 v
.add_instr
("return OBJ2VAL(obj);")
419 var cname
= "CHECKNEW_{name}"
420 var args
= checknew_iroutine
.compile_signature_to_c
(v
, cname
, "check new {name}", null, null)
421 var decl_writer_old
= v
.decl_writer
422 v
.decl_writer
= v
.writer
.sub
423 checknew_iroutine
.compile_to_c
(v
, cname
, args
)
424 v
.decl_writer
= decl_writer_old
429 var init_table_size
= cshe
.greaters
.length
+ 1
430 var init_table_decl
= "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
432 for g
in global_properties
do
434 # FIXME skip invisible constructors
435 if not p
.global
.is_init_for
(self) then continue
436 assert p
isa MMMethod
438 var cname
= "NEW_{self}_{p.global.intro.cname}"
439 var new_args
= new_instance_iroutine
[p
].compile_signature_to_c
(v
, cname
, "new {self} {p.full_name}", null, null)
440 var decl_writer_old
= v
.decl_writer
441 v
.decl_writer
= v
.writer
.sub
442 v
.add_instr
(init_table_decl
)
443 var e
= new_instance_iroutine
[p
].compile_to_c
(v
, cname
, new_args
).as(not null)
444 v
.add_instr
("return {e};")
445 v
.decl_writer
= decl_writer_old
449 else if not pi
.tagged
then
451 var tbox
= "struct TBOX_{name}"
452 v
.add_instr
("val_t BOX_{name}({t} val) \{")
454 v
.add_instr
("{tbox} *box = ({tbox}*)alloc(sizeof({tbox}));")
455 v
.add_instr
("box->vft = VFT_{name};")
456 v
.add_instr
("box->val = val;")
457 v
.add_instr
("box->object_id = object_id_counter;")
458 v
.add_instr
("object_id_counter = object_id_counter + 1;")
459 v
.add_instr
("return OBJ2VAL(box);")
467 fun compile_property_to_c
(v
: CompilerVisitor)
472 var more_params
: nullable String = null
473 if global
.is_init
then more_params
= "int* init_table"
474 var args
= ir
.compile_signature_to_c
(v
, cname
, full_name
, null, more_params
)
475 var writer_old
= v
.writer
476 v
.writer
= v
.writer
.sub
477 var decl_writer_old
= v
.decl_writer
478 v
.decl_writer
= v
.writer
.sub
480 var itpos
: nullable String = null
481 if global
.is_init
then
482 itpos
= "itpos{v.new_number}"
483 v
.add_decl
("int {itpos} = VAL2OBJ({args.first})->vft[{local_class.global.init_table_pos_id}].i;")
484 v
.add_instr
("if (init_table[{itpos}]) return;")
487 var s
= ir
.compile_to_c
(v
, cname
, args
)
489 if itpos
!= null then
490 v
.add_instr
("init_table[{itpos}] = 1;")
493 v
.add_instr
("return;")
495 v
.add_instr
("return {s};")
500 v
.writer
= writer_old
501 v
.decl_writer
= decl_writer_old