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 # Compile method bodies, statments and expressions to C.
18 package compiling_methods
23 redef class CompilerVisitor
24 # Compile a statment node
25 meth compile_stmt
(n
: PExpr)
27 n
.prepare_compile_stmt
(self)
28 var i
= cfc
._variable_index
30 cfc
._variable_index
= i
33 # Compile is expression node
34 meth compile_expr
(n
: PExpr): String
36 var i
= cfc
._variable_index
37 var s
= n
.compile_expr
(self)
38 cfc
._variable_index
= i
47 # Ensure that a c expression is a var
48 meth ensure_var
(s
: String): String
50 if s
.substring
(0,3) == "variable" then
58 # Add a assignment between a variable and an expression
59 meth add_assignment
(v
: String, s
: String)
62 add_instr
("{v} = {s};")
66 readable writable attr _cfc
: CFunctionContext
68 readable writable attr _nmc
: NitMethodContext
70 # Generate an fprintf to display an error location
71 meth printf_locate_error
(node
: PNode): String
73 var s
= "fprintf(stderr, \""
74 if nmc != null then s.append(" in %s
")
75 s.append(" (%s
:%d
)\\n\
", ")
76 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
77 s
.append
("LOCATE_{module.name}, {node.line_number});")
81 redef init(module: MMSrcModule)
86 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
88 var n
= nmc
.method
.node
89 assert n
isa AConcreteInitPropdef
91 if n
.super_init_calls
.is_empty
then return
95 if start_prop
!= null then
96 while n
.super_init_calls
[i
] != start_prop
do
97 #s.append(" {n.super_init_calls[i]}")
101 #s.append(" {start_prop}")
103 while n
.explicit_super_init_calls
[j
] != start_prop
do
108 var stop_prop
: MMMethod = null
109 if j
< n
.explicit_super_init_calls
.length
then
110 stop_prop
= n
.explicit_super_init_calls
[j
]
112 var l
= n
.super_init_calls
.length
115 var p
= n
.super_init_calls
[i
]
116 if p
== stop_prop
then break
117 var cargs
= new Array[String]
118 if p
.signature
.arity
== 0 then
119 cargs
.add
(cfc
.varname
(nmc
.method_params
[0]))
121 for va
in nmc
.method_params
do
122 cargs
.add
(cfc
.varname
(va
))
126 p
.compile_call
(self, cargs
)
131 # s.append(" {n.super_init_calls[i]}")
134 #if stop_prop != null then s.append(" (stop at {stop_prop})")
135 #n.printl("implicit calls in {n.method}: {s}")
139 # A C function currently written
140 class CFunctionContext
141 readable attr _visitor
: CompilerVisitor
143 # Next available variable number
144 attr _variable_index
: Int = 0
146 # Total number of variable
147 attr _variable_index_max
: Int = 0
149 # Association between nit variable and the corrsponding c variable
150 attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
152 meth varname
(v
: Variable): String
157 # Return the next available variable
160 var v
= variable
(_variable_index
)
161 _variable_index
= _variable_index
+ 1
162 if _variable_index
> _variable_index_max
then
163 _variable_index_max
= _variable_index
168 meth register_variable
(v
: Variable): String
171 _varnames
[v
] = "variable[{_variable_index-1}]"
175 # Return the ith variable
176 protected meth variable
(i
: Int): String
178 return "variable[{i}]"
181 # Mark the variable available
182 meth free_var
(v
: String)
185 if v
== variable
(_variable_index-1
) then
186 _variable_index
= _variable_index
- 1
190 # Generate the local variable declarations
191 # To use at the end of the C function once all variables are known
192 meth generate_var_decls
194 if _variable_index_max
> 0 then
195 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
197 visitor
.add_decl
("val_t *variable = NULL;")
201 init(v
: CompilerVisitor) do _visitor
= v
204 # A Nit method currenlty compiled
205 class NitMethodContext
206 # Current method compiled
207 readable attr _method
: MMSrcMethod
209 # Is a "return" found in the method body
210 readable writable attr _has_return
: Bool = false
212 # Association between parameters and the corresponding variables
213 readable writable attr _method_params
: Array[ParamVariable]
215 # Where a nit return must branch
216 readable writable attr _return_label
: String
218 # Where a nit break must branch
219 readable writable attr _break_label
: String
221 # Where a nit continue must branch
222 readable writable attr _continue_label
: String
224 # Variable where a functionnal nit return must store its value
225 readable writable attr _return_value
: String
227 init(method
: MMSrcMethod)
233 ###############################################################################
236 # Compile a call on self for given arguments
237 # Most calls are compiled with a table access,
238 # primitive calles are inlined
239 # == and != are guarded and possibly inlined
240 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
243 if i
isa MMSrcMethod then
244 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
245 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
247 var e
= i
.do_compile_inside
(v
, cargs
)
251 var ee
= once
"==".to_symbol
252 var ne
= once
"!=".to_symbol
254 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
255 var eqcall
= eqp
.compile_call
(v
, cargs
)
256 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
258 if global
.is_init
then
260 cargs
.add
("init_table /*YYY*/")
263 var m
= "(({cname}_t)CALL({cargs[0]},{global.color_id}))"
264 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
266 vcall
= "UNTAG_Bool({vcall})"
267 var obj
= once
"Object".to_symbol
268 if i
.local_class
.name
== obj
then
269 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
271 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
273 if signature
.return_type
!= null then
276 v
.add_instr
(vcall
+ ";")
281 # Compile a call as constructor with given args
282 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
284 var recv
= v
.cfc
.get_var
285 v
.add_instr
("{recv} = NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}); /*new {recvtype}*/")
289 # Compile a call as call-next-method on self with given args
290 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
292 var m
= "(({cname}_t)CALL({cargs[0]},{color_id_for_super}))"
293 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
298 redef class MMAttribute
299 # Compile an acces on selffor a given reciever.
300 # Result is a valid C left-value for assigment
301 meth compile_access
(v
: CompilerVisitor, recv
: String): String
303 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
307 redef class MMLocalProperty
308 # Compile the property as a C property
309 meth compile_property_to_c
(v
: CompilerVisitor) do end
312 redef class MMSrcMethod
313 # Compile and declare the signature to C
314 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
316 var params
= new Array[String]
317 var params_new
: Array[String] = null
318 if global
.is_init
then
319 params_new
= new Array[String]
321 params
.add
("val_t {args[0]}")
322 for i
in [0..signature
.arity
[ do
323 var p
= "val_t {args[i+1]}"
325 if params_new
!= null then params_new
.add
(p
)
327 if global
.is_init
then
328 params
.add
("int* init_table")
331 if signature
.return_type
!= null then
336 var p
= params
.join
(", ")
337 var s
= "{ret} {cname}({p})"
338 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
340 if params_new
!= null then
341 v
.add_decl
("val_t NEW_{cname}({params_new.join(", ")});")
346 redef meth compile_property_to_c
(v
)
348 v
.cfc
= new CFunctionContext(v
)
350 var args
= new Array[String]
352 for i
in [0..signature
.arity
[ do
353 args
.add
(" param{i}")
355 var cs
= decl_csignature
(v
, args
)
356 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
358 v
.add_instr
("{cs} \{")
365 if s
.node
!= null then ln
= s
.node
.line_number
366 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
367 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
368 v
.add_instr
("trace.file = LOCATE_{module.name};")
369 var s
= do_compile_inside
(v
, args
)
370 v
.add_instr
("tracehead = trace.prev;")
372 v
.add_instr
("return;")
374 v
.add_instr
("return {s};")
377 v
.cfc
.generate_var_decls
379 ctx_old
.append
(v
.ctx
)
385 # Compile the method body inline
386 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
389 redef class MMReadImplementationMethod
390 redef meth do_compile_inside
(v
, params
)
392 return node
.prop
.compile_access
(v
, params
[0])
396 redef class MMWriteImplementationMethod
397 redef meth do_compile_inside
(v
, params
)
399 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
404 redef class MMMethSrcMethod
405 redef meth do_compile_inside
(v
, params
)
407 return node
.do_compile_inside
(v
, self, params
)
411 redef class MMImplicitInit
412 redef meth do_compile_inside
(v
, params
)
414 var f
= params
.length
- unassigned_attributes
.length
415 var recv
= params
.first
416 for sp
in super_inits
do
417 assert sp
isa MMMethod
418 var args_recv
= [recv
]
419 if sp
== super_init
then
420 var args
= new Array[String].with_capacity
(f
)
425 sp
.compile_call
(v
, args
)
427 sp
.compile_call
(v
, args_recv
)
430 for i
in [f
..params
.length
[ do
431 var attribute
= unassigned_attributes
[i-f
]
432 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
439 # Compile a subtype check to self
441 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
443 # Fixme: handle formaltypes
444 var g
= local_class
.global
445 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
448 # Compile a cast assertion
449 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
451 # Fixme: handle formaltypes
452 var g
= local_class
.global
453 v
.add_instr
("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
457 ###############################################################################
459 redef class AMethPropdef
460 # Compile the method body
461 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
464 redef class AConcreteMethPropdef
465 redef meth do_compile_inside
(v
, method
, params
)
468 v
.nmc
= new NitMethodContext(method
)
470 var cname
= v
.cfc
.register_variable
(self_var
)
471 v
.add_assignment
(cname
, params
[0])
472 v
.nmc
.method_params
= [self_var
]
474 var orig_meth
: MMLocalProperty = method
.global
.intro
475 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
476 if n_signature
!= null then
477 var sig
= n_signature
478 assert sig
isa ASignature
479 for ap
in sig
.n_params
do
480 var cname
= v
.cfc
.register_variable
(ap
.variable
)
481 v
.nmc
.method_params
.add
(ap
.variable
)
482 var orig_type
= orig_sig
[ap
.position
]
483 if not orig_type
< ap
.variable
.stype
then
484 # FIXME: do not test always
485 # FIXME: handle formal types
486 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
487 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
+ 1], ap
)
489 v
.add_assignment
(cname
, params
[ap
.position
+ 1])
493 var itpos
: String = null
494 if self isa AConcreteInitPropdef then
495 itpos
= "VAL2OBJ({params[0]})->vft[{method.local_class.global.init_table_pos_id}].i"
496 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
497 v
.add_instr
("if (init_table[{itpos}]) return;")
500 v
.nmc
.return_label
= "return_label{v.new_number}"
501 if method
.signature
.return_type
!= null then
502 v
.nmc
.return_value
= v
.cfc
.get_var
503 v
.cfc
.free_var
(v
.nmc
.return_value
)
505 v
.nmc
.return_value
= null
507 if self isa AConcreteInitPropdef then
508 v
.invoke_super_init_calls_after
(null)
510 if n_block
!= null then
511 v
.compile_stmt
(n_block
)
513 if v
.nmc
.has_return
then
514 v
.add_instr
("{v.nmc.return_label}: while(false);")
516 if self isa AConcreteInitPropdef then
517 v
.add_instr
("init_table[{itpos}] = 1;")
519 var ret
= v
.nmc
.return_value
526 redef class ADeferredMethPropdef
527 redef meth do_compile_inside
(v
, method
, params
)
529 v
.add_instr
("fprintf(stderr, \"Deferred method
%s called\
");")
530 v
.add_instr
(v
.printf_locate_error
(self))
531 v
.add_instr
("nit_exit(1);")
532 if method
.signature
.return_type
!= null then
540 redef class AExternMethPropdef
541 redef meth do_compile_inside
(v
, method
, params
)
543 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
544 if n_extern
!= null then
545 ename
= n_extern
.text
546 ename
= ename
.substring
(1, ename
.length-2
)
548 var sig
= method
.signature
549 if params
.length
!= sig
.arity
+ 1 then
550 printl
("par:{params.length} sig:{sig.arity}")
552 var args
= new Array[String]
553 args
.add
(sig
.recv
.unboxtype
(params
[0]))
554 for i
in [0..sig
.arity
[ do
555 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
557 var s
= "{ename}({args.join(", ")})"
558 if sig
.return_type
!= null then
559 return sig
.return_type
.boxtype
(s
)
567 redef class AInternMethPropdef
568 redef meth do_compile_inside
(v
, method
, p
)
570 var c
= method
.local_class
.name
573 if c
== once
"Int".to_symbol
then
574 if n
== once
"object_id".to_symbol
then
576 else if n
== once
"unary -".to_symbol
then
577 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
578 else if n
== once
"output".to_symbol
then
579 v
.add_instr
("printf(\"%d\\n\
", UNTAG_Int({p[0]}));")
580 else if n
== once
"ascii".to_symbol
then
581 s
= "TAG_Char(UNTAG_Int({p[0]}))"
582 else if n
== once
"succ".to_symbol
then
583 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
584 else if n
== once
"prec".to_symbol
then
585 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
586 else if n
== once
"to_f".to_symbol
then
587 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
588 else if n
== once
"+".to_symbol
then
589 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
590 else if n
== once
"-".to_symbol
then
591 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
592 else if n
== once
"*".to_symbol
then
593 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
594 else if n
== once
"/".to_symbol
then
595 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
596 else if n
== once
"%".to_symbol
then
597 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
598 else if n
== once
"<".to_symbol
then
599 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
600 else if n
== once
">".to_symbol
then
601 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
602 else if n
== once
"<=".to_symbol
then
603 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
604 else if n
== once
">=".to_symbol
then
605 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
606 else if n
== once
"lshift".to_symbol
then
607 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
608 else if n
== once
"rshift".to_symbol
then
609 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
610 else if n
== once
"==".to_symbol
then
611 s
= "TAG_Bool(({p[0]})==({p[1]}))"
612 else if n
== once
"!=".to_symbol
then
613 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
615 else if c
== once
"Float".to_symbol
then
616 if n
== once
"object_id".to_symbol
then
617 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
618 else if n
== once
"unary -".to_symbol
then
619 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
620 else if n
== once
"output".to_symbol
then
621 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
622 else if n
== once
"to_i".to_symbol
then
623 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
624 else if n
== once
"+".to_symbol
then
625 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
626 else if n
== once
"-".to_symbol
then
627 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
628 else if n
== once
"*".to_symbol
then
629 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
630 else if n
== once
"/".to_symbol
then
631 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
632 else if n
== once
"<".to_symbol
then
633 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
634 else if n
== once
">".to_symbol
then
635 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
636 else if n
== once
"<=".to_symbol
then
637 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
638 else if n
== once
">=".to_symbol
then
639 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
641 else if c
== once
"Char".to_symbol
then
642 if n
== once
"object_id".to_symbol
then
643 s
= "TAG_Int(UNTAG_Char({p[0]}))"
644 else if n
== once
"unary -".to_symbol
then
645 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
646 else if n
== once
"output".to_symbol
then
647 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
648 else if n
== once
"ascii".to_symbol
then
649 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
650 else if n
== once
"succ".to_symbol
then
651 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
652 else if n
== once
"prec".to_symbol
then
653 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
654 else if n
== once
"to_i".to_symbol
then
655 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
656 else if n
== once
"+".to_symbol
then
657 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
658 else if n
== once
"-".to_symbol
then
659 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
660 else if n
== once
"*".to_symbol
then
661 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
662 else if n
== once
"/".to_symbol
then
663 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
664 else if n
== once
"%".to_symbol
then
665 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
666 else if n
== once
"<".to_symbol
then
667 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
668 else if n
== once
">".to_symbol
then
669 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
670 else if n
== once
"<=".to_symbol
then
671 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
672 else if n
== once
">=".to_symbol
then
673 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
674 else if n
== once
"==".to_symbol
then
675 s
= "TAG_Bool(({p[0]})==({p[1]}))"
676 else if n
== once
"!=".to_symbol
then
677 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
679 else if c
== once
"Bool".to_symbol
then
680 if n
== once
"object_id".to_symbol
then
681 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
682 else if n
== once
"unary -".to_symbol
then
683 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
684 else if n
== once
"output".to_symbol
then
685 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
686 else if n
== once
"ascii".to_symbol
then
687 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
688 else if n
== once
"to_i".to_symbol
then
689 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
690 else if n
== once
"==".to_symbol
then
691 s
= "TAG_Bool(({p[0]})==({p[1]}))"
692 else if n
== once
"!=".to_symbol
then
693 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
695 else if c
== once
"NativeArray".to_symbol
then
696 if n
== once
"object_id".to_symbol
then
697 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
698 else if n
== once
"[]".to_symbol
then
699 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
700 else if n
== once
"[]=".to_symbol
then
701 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
702 else if n
== once
"copy_to".to_symbol
then
703 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
705 else if c
== once
"NativeString".to_symbol
then
706 if n
== once
"object_id".to_symbol
then
707 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
708 else if n
== once
"atoi".to_symbol
then
709 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
710 else if n
== once
"[]".to_symbol
then
711 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
712 else if n
== once
"[]=".to_symbol
then
713 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
714 else if n
== once
"copy_to".to_symbol
then
715 v
.add_instr
("(void)memcpy(UNBOX_NativeString({p[1]})+UNTAG_Int({p[4]}), UNBOX_NativeString({p[0]})+UNTAG_Int({p[3]}), UNTAG_Int({p[2]}));")
717 else if n
== once
"object_id".to_symbol
then
718 s
= "TAG_Int((bigint){p[0]})"
719 else if n
== once
"sys".to_symbol
then
721 else if n
== once
"is_same_type".to_symbol
then
722 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
723 else if n
== once
"exit".to_symbol
then
724 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
725 else if n
== once
"calloc_array".to_symbol
then
726 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
727 else if n
== once
"calloc_string".to_symbol
then
728 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
731 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
733 if method
.signature
.return_type
!= null and s
== null then
734 s
= "NIT_NULL /*stub*/"
740 ###############################################################################
743 # Compile the node as an expression
744 # Only the visitor should call it
745 meth compile_expr
(v
: CompilerVisitor): String is abstract
747 # Prepare a call of node as a statement
748 # Only the visitor should call it
749 # It's used for local variable managment
750 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
752 # Compile the node as a statement
753 # Only the visitor should call it
754 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
757 redef class ABlockExpr
758 redef meth compile_stmt
(v
)
766 redef class AVardeclExpr
767 redef meth prepare_compile_stmt
(v
)
769 v
.cfc
.register_variable
(variable
)
772 redef meth compile_stmt
(v
)
774 var cname
= v
.cfc
.varname
(variable
)
775 if n_expr
== null then
776 var t
= variable
.stype
777 v
.add_assignment
(cname
, "{t.default_cvalue} /*decl variable {variable.name}*/")
779 var e
= v
.compile_expr
(n_expr
)
780 v
.add_assignment
(cname
, e
)
785 redef class AReturnExpr
786 redef meth compile_stmt
(v
)
788 v
.nmc
.has_return
= true
789 if n_expr
!= null then
790 var e
= v
.compile_expr
(n_expr
)
791 v
.add_assignment
(v
.nmc
.return_value
, e
)
793 v
.add_instr
("goto {v.nmc.return_label};")
797 redef class ABreakExpr
798 redef meth compile_stmt
(v
)
800 v
.add_instr
("goto {v.nmc.break_label};")
804 redef class AContinueExpr
805 redef meth compile_stmt
(v
)
807 v
.add_instr
("goto {v.nmc.continue_label};")
811 redef class AAbortExpr
812 redef meth compile_stmt
(v
)
814 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
819 redef meth compile_stmt
(v
)
821 if n_block
!= null then
822 v
.compile_stmt
(n_block
)
828 redef meth compile_stmt
(v
)
830 var e
= v
.compile_expr
(n_expr
)
831 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
833 if n_then
!= null then
835 v
.compile_stmt
(n_then
)
838 if n_else
!= null then
839 v
.add_instr
("} else \{ /*if*/")
841 v
.compile_stmt
(n_else
)
848 redef class AIfexprExpr
849 redef meth compile_expr
(v
)
851 var e
= v
.compile_expr
(n_expr
)
852 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
855 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
857 v
.add_instr
("} else \{ /*if*/")
860 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
861 v
.add_assignment
(e
, e2
)
868 redef class AControlableBlock
869 meth compile_inside_block
(v
: CompilerVisitor) is abstract
870 redef meth compile_stmt
(v
)
872 var old_break_label
= v
.nmc
.break_label
873 var old_continue_label
= v
.nmc
.continue_label
874 var id
= v
.new_number
875 v
.nmc
.break_label
= "break_{id}"
876 v
.nmc
.continue_label
= "continue_{id}"
878 compile_inside_block
(v
)
881 v
.nmc
.break_label
= old_break_label
882 v
.nmc
.continue_label
= old_continue_label
886 redef class AWhileExpr
887 redef meth compile_inside_block
(v
)
889 v
.add_instr
("while (true) \{ /*while*/")
891 var e
= v
.compile_expr
(n_expr
)
892 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
894 if n_block
!= null then
895 v
.compile_stmt
(n_block
)
897 v
.add_instr
("{v.nmc.continue_label}: while(0);")
900 v
.add_instr
("{v.nmc.break_label}: while(0);")
905 redef meth compile_inside_block
(v
)
907 v
.compile_stmt
(n_vardecl
)
911 redef class AForVardeclExpr
912 redef meth compile_stmt
(v
)
914 var e
= v
.compile_expr
(n_expr
)
915 var prop
= n_expr
.stype
.local_class
.select_method
(once
"iterator".to_symbol
)
917 printl
("No iterator")
920 var ittype
= prop
.signature
.return_type
922 var iter
= v
.cfc
.get_var
923 v
.add_assignment
(iter
, prop
.compile_call
(v
, [e
]))
924 var prop2
= ittype
.local_class
.select_method
(once
"is_ok".to_symbol
)
925 if prop2
== null then
929 var prop3
= ittype
.local_class
.select_method
(once
"item".to_symbol
)
930 if prop3
== null then
934 var prop4
= ittype
.local_class
.select_method
(once
"next".to_symbol
)
935 if prop4
== null then
939 v
.add_instr
("while (true) \{ /*for*/")
941 var ok
= v
.cfc
.get_var
942 v
.add_assignment
(ok
, prop2
.compile_call
(v
, [iter
]))
943 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
945 var e
= prop3
.compile_call
(v
, [iter
])
947 var cname
= v
.cfc
.register_variable
(variable
)
948 v
.add_assignment
(cname
, e
)
950 assert par
isa AForExpr
951 var n_block
= par
.n_block
952 if n_block
!= null then
953 v
.compile_stmt
(n_block
)
955 v
.add_instr
("{v.nmc.continue_label}: while(0);")
956 e
= prop4
.compile_call
(v
, [iter
])
960 v
.add_instr
("{v.nmc.break_label}: while(0);")
964 redef class AAssertExpr
965 redef meth compile_stmt
(v
)
967 var e
= v
.compile_expr
(n_expr
)
970 s
= " '{n_id.text}' "
972 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
977 redef meth compile_expr(v)
979 return " {v.cfc.varname(variable)} /*{variable.name}*/"
983 redef class AVarAssignExpr
984 redef meth compile_stmt(v)
986 var e = v.compile_expr(n_value)
987 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
991 redef class AVarReassignExpr
992 redef meth compile_stmt(v)
994 var e1 = v.cfc.varname(variable)
995 var e2 = v.compile_expr(n_value)
996 var e3 = assign_method.compile_call(v, [e1, e2])
997 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1001 redef class ASelfExpr
1002 redef meth compile_expr(v)
1004 return v.cfc.varname(v.nmc.method_params[0])
1009 redef meth compile_expr(v)
1011 var e = v.ensure_var(v.compile_expr(n_expr))
1012 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1015 var e2 = v.compile_expr(n_expr2)
1016 v.add_assignment(e, e2)
1023 redef class AAndExpr
1024 redef meth compile_expr(v)
1026 var e = v.ensure_var(v.compile_expr(n_expr))
1027 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1030 var e2 = v.compile_expr(n_expr2)
1031 v.add_assignment(e, e2)
1038 redef class ANotExpr
1039 redef meth compile_expr(v)
1041 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1046 redef meth compile_expr(v)
1048 var e = v.compile_expr(n_expr)
1049 var e2 = v.compile_expr(n_expr2)
1050 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1054 redef class AIsaExpr
1055 redef meth compile_expr(v)
1057 var e = v.compile_expr(n_expr)
1058 return n_type.stype.compile_cast(v, e)
1062 redef class AAsCastExpr
1063 redef meth compile_expr(v)
1065 var e = v.compile_expr(n_expr)
1066 n_type.stype.compile_type_check(v, e, self)
1071 redef class ATrueExpr
1072 redef meth compile_expr(v)
1074 return " TAG_Bool(true)"
1078 redef class AFalseExpr
1079 redef meth compile_expr(v)
1081 return " TAG_Bool(false)"
1085 redef class AIntExpr
1086 redef meth compile_expr(v)
1088 return " TAG_Int({n_number.text})"
1092 redef class AFloatExpr
1093 redef meth compile_expr(v)
1095 return "BOX_Float({n_float.text})"
1099 redef class ACharExpr
1100 redef meth compile_expr(v)
1102 return " TAG_Char({n_char.text})"
1106 redef class AStringFormExpr
1107 redef meth compile_expr(v)
1109 var prop = stype.local_class.select_method(once "with_native
".to_symbol)
1111 return prop.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1114 # The raw string value
1115 protected meth string_text: String is abstract
1117 # The string in a C native format
1118 protected attr _cstring: String
1120 # The string length in bytes
1121 protected attr _cstring_length: Int
1123 # Compute _cstring and _cstring_length using string_text
1124 protected meth compute_string_info
1127 var str = string_text
1128 var res = new String
1130 while i < str.length do
1135 if c2 != '{' and c2 != '}' then
1145 _cstring_length = len
1149 redef class AStringExpr
1150 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1152 redef class AStartStringExpr
1153 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1155 redef class AMidStringExpr
1156 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1158 redef class AEndStringExpr
1159 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1162 redef class ASuperstringExpr
1163 redef meth compile_expr(v)
1165 var prop = stype.local_class.select_method(once "init".to_symbol)
1166 var recv = prop.compile_constructor_call(v, stype, new Array[String])
1168 var prop2 = stype.local_class.select_method(once "append
".to_symbol)
1170 var prop3 = stype.local_class.select_method(once "to_s
".to_symbol)
1171 for ne in n_exprs do
1172 var e = v.ensure_var(v.compile_expr(ne))
1173 if ne.stype != stype then
1174 v.add_assignment(e, prop3.compile_call(v, [e]))
1176 prop2.compile_call(v, [recv, e])
1183 redef class ANullExpr
1184 redef meth compile_expr(v)
1186 return " NIT_NULL /*null*/"
1190 redef class AArrayExpr
1191 redef meth compile_expr(v)
1193 var prop = stype.local_class.select_method(once "with_capacity
".to_symbol)
1194 var recv = prop.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1196 var prop2 = stype.local_class.select_method(once "add
".to_symbol)
1197 for ne in n_exprs do
1198 var e = v.compile_expr(ne)
1199 prop2.compile_call(v, [recv, e])
1205 redef class ARangeExpr
1206 redef meth compile_expr(v)
1208 var prop = stype.local_class.select_method(propname)
1209 var e = v.compile_expr(n_expr)
1210 var e2 = v.compile_expr(n_expr2)
1211 return prop.compile_constructor_call(v, stype, [e, e2])
1213 # The constructor that must be used for the range
1214 protected meth propname: Symbol is abstract
1217 redef class ACrangeExpr
1218 redef meth propname do return once "init".to_symbol
1220 redef class AOrangeExpr
1221 redef meth propname do return once "without_last
".to_symbol
1224 redef class ASuperExpr
1225 redef meth compile_stmt(v)
1227 var e = compile_expr(v)
1228 if e != null then v.add_instr("{e};")
1231 redef meth compile_expr(v)
1233 var arity = v.nmc.method_params.length - 1
1234 if init_in_superclass != null then
1235 arity = init_in_superclass.signature.arity
1237 var args = new Array[String].with_capacity(arity + 1)
1238 args.add(v.cfc.varname(v.nmc.method_params[0]))
1239 if n_args.length != arity then
1240 for i in [0..arity[ do
1241 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1245 args.add(v.compile_expr(na))
1248 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1249 if init_in_superclass != null then
1250 return init_in_superclass.compile_call(v, args)
1252 if prop.global.is_init then args.add("init_table
")
1253 return prop.compile_super_call(v, args)
1258 redef class AAttrExpr
1259 redef meth compile_expr(v)
1261 var e = v.compile_expr(n_expr)
1262 return prop.compile_access(v, e)
1266 redef class AAttrAssignExpr
1267 redef meth compile_stmt(v)
1269 var e = v.compile_expr(n_expr)
1270 var e2 = v.compile_expr(n_value)
1271 v.add_assignment(prop.compile_access(v, e), e2)
1274 redef class AAttrReassignExpr
1275 redef meth compile_stmt(v)
1277 var e1 = v.compile_expr(n_expr)
1278 var e2 = prop.compile_access(v, e1)
1279 var e3 = v.compile_expr(n_value)
1280 var e4 = assign_method.compile_call(v, [e2, e3])
1281 v.add_assignment(e2, e4)
1285 redef class ASendExpr
1286 redef meth compile_expr(v)
1288 var recv = v.compile_expr(n_expr)
1289 var cargs = new Array[String]
1291 for a in arguments do
1292 cargs.add(v.compile_expr(a))
1295 var e = prop.compile_call(v, cargs)
1296 if prop.global.is_init then
1297 v.invoke_super_init_calls_after(prop)
1302 redef meth compile_stmt(v)
1304 var e = compile_expr(v)
1306 v.add_instr(e + ";")
1311 redef class ASendReassignExpr
1312 redef meth compile_expr(v)
1314 var recv = v.compile_expr(n_expr)
1315 var cargs = new Array[String]
1317 for a in arguments do
1318 cargs.add(v.compile_expr(a))
1321 var e2 = read_prop.compile_call(v, cargs)
1322 var e3 = v.compile_expr(n_value)
1323 var e4 = assign_method.compile_call(v, [e2, e3])
1325 return prop.compile_call(v, cargs)
1329 redef class ANewExpr
1330 redef meth compile_expr(v)
1332 var cargs = new Array[String]
1333 for a in arguments do
1334 cargs.add(v.compile_expr(a))
1336 return prop.compile_constructor_call(v, stype, cargs)
1340 redef class AProxyExpr
1341 redef meth compile_expr(v)
1343 return v.compile_expr(n_expr)
1347 redef class AOnceExpr
1348 redef meth compile_expr(v)
1350 var i = v.new_number
1351 var cvar = v.cfc.get_var
1352 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1353 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1354 v.add_instr("else \
{")
1356 v.cfc.free_var(cvar)
1357 var e = v.compile_expr(n_expr)
1358 v.add_assignment(cvar, e)
1359 v.add_instr("once_value_
{i} = {cvar};")
1360 v.add_instr("once_bool_
{i} = true;")