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
= _variable_index
33 # Compile is expression node
34 meth compile_expr
(n
: PExpr): String
36 var i
= _variable_index
37 var s
= n
.compile_expr
(self)
42 if s
== variable
(_variable_index-1
) then
50 # Ensure that a c expression is a var
51 meth ensure_var
(s
: String): String
53 if s
.substring
(0,3) == "variable" then
61 # Add a assignment between a variable and an expression
62 meth add_assignment
(v
: String, s
: String)
65 add_instr
("{v} = {s};")
69 # Return the ith variable
70 protected meth variable
(i
: Int): String
75 # Next available variable number
76 attr _variable_index
: Int
78 # Total number of variable
79 attr _variable_index_max
: Int
81 # Return the next available variable
84 var v
= variable
(_variable_index
)
85 _variable_index
= _variable_index
+ 1
86 if _variable_index
> _variable_index_max
then
87 add_decl
("val_t {v};")
88 _variable_index_max
= _variable_index
93 # Mark the variable available
94 meth free_var
(v
: String)
97 if v
== variable
(_variable_index-1
) then
98 _variable_index
= _variable_index
- 1
102 # Clear all status related to a method body
108 _variable_index_max
= 0
111 # Association between nit variable and the corrsponding c variable
112 readable attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
114 # Is a "return" found in the method body
115 readable writable attr _has_return
: Bool
117 # Association between parameters and the corresponding c variables
118 readable writable attr _method_params
: Array[String]
120 # Current method compiled
121 readable writable attr _method
: MMSrcMethod
123 # Where a nit return must branch
124 readable writable attr _return_label
: String
126 # Where a nit break must branch
127 readable writable attr _break_label
: String
129 # Where a nit continue must branch
130 readable writable attr _continue_label
: String
132 # Variable where a functionnal nit return must store its value
133 readable writable attr _return_value
: String
135 redef init(module: MMSrcModule)
141 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
144 assert n
isa AConcreteInitPropdef
146 if n
.super_init_calls
.is_empty
then return
150 if start_prop
!= null then
151 while n
.super_init_calls
[i
] != start_prop
do
152 #s.append(" {n.super_init_calls[i]}")
156 #s.append(" {start_prop}")
158 while n
.explicit_super_init_calls
[j
] != start_prop
do
163 var stop_prop
: MMMethod
164 if j
< n
.explicit_super_init_calls
.length
then
165 stop_prop
= n
.explicit_super_init_calls
[j
]
167 var l
= n
.super_init_calls
.length
170 var p
= n
.super_init_calls
[i
]
171 if p
== stop_prop
then break
172 var cargs
= method_params
173 if p
.signature
.arity
== 0 then
174 cargs
= [method_params
[0]]
177 p
.compile_call
(self, cargs
)
182 # s.append(" {n.super_init_calls[i]}")
185 #if stop_prop != null then s.append(" (stop at {stop_prop})")
186 #n.printl("implicit calls in {n.method}: {s}")
190 ###############################################################################
193 # Compile a call on self for given arguments
194 # Most calls are compiled with a table access,
195 # primitive calles are inlined
196 # == and != are guarded and possibly inlined
197 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
199 var i
= concrete_property
200 assert i
isa MMSrcMethod
201 if i
.node
isa AInternMethPropdef or
202 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
204 var e
= i
.do_compile_inside
(v
, cargs
)
207 var ee
= once
"==".to_symbol
208 var ne
= once
"!=".to_symbol
210 var eqp
= signature
.recv
.select_method
(ee
)
211 var eqcall
= eqp
.compile_call
(v
, cargs
)
212 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
214 if global
.is_init
then
216 cargs
.add
("init_table /*YYY*/")
219 var m
= "(({cname}_t)CALL({cargs[0]},{global.color_id}))"
220 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
222 vcall
= "UNTAG_Bool({vcall})"
223 var obj
= once
"Object".to_symbol
224 if i
.local_class
.name
== obj
then
225 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
227 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
229 if signature
.return_type
!= null then
232 v
.add_instr
(vcall
+ ";")
237 # Compile a call as constructor with given args
238 meth compile_constructor_call
(v
: CompilerVisitor, cargs
: Array[String]): String
241 var stype
= signature
.recv
242 v
.add_instr
("{recv} = NEW_{global.intro.cname}({cargs.join(", ")}); /*new {stype}*/")
246 # Compile a call as call-next-method on self with given args
247 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
249 var m
= "(({cname}_t)CALL({cargs[0]},{color_id_for_super}))"
250 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
255 redef class MMAttribute
256 # Compile an acces on selffor a given reciever.
257 # Result is a valid C left-value for assigment
258 meth compile_access
(v
: CompilerVisitor, recv
: String): String
260 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
264 redef class MMSrcLocalProperty
265 # Compile the property as a C property
266 meth compile_property_to_c
(v
: CompilerVisitor) do end
269 redef class MMSrcMethod
270 # Compile and declare the signature to C
271 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
273 var params
= new Array[String]
274 var params_new
: Array[String]
275 if global
.is_init
then
276 params_new
= new Array[String]
278 params
.add
("val_t {args[0]}")
279 for i
in [0..signature
.arity
[ do
280 var p
= "val_t {args[i+1]}"
282 if params_new
!= null then params_new
.add
(p
)
284 if global
.is_init
then
285 params
.add
("int* init_table")
288 if signature
.return_type
!= null then
293 var p
= params
.join
(", ")
294 var s
= "{ret} {cname}({p})"
295 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
297 if params_new
!= null then
298 v
.add_decl
("val_t NEW_{cname}({params_new.join(", ")});")
303 redef meth compile_property_to_c
(v
)
306 var args
= new Array[String]
308 for i
in [0..signature
.arity
[ do
309 args
.add
(" param{i}")
311 var cs
= decl_csignature
(v
, args
)
313 v
.add_instr
("{cs} \{")
318 v
.add_decl
("struct trace_t trace = \{NULL, \"{module.name}::{local_class.name}::{name} ({node.locate})\"};")
319 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
320 var s = do_compile_inside(v, args)
321 v.add_instr("tracehead
= trace
.prev
;")
323 v.add_instr("return;")
325 v.add_instr("return {s};")
328 ctx_old.append(v.ctx)
334 # Compile the method body inline
335 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
338 redef class MMReadImplementationMethod
339 redef meth do_compile_inside(v, params)
341 return node.prop.compile_access(v, params[0])
345 redef class MMWriteImplementationMethod
346 redef meth do_compile_inside(v, params)
348 v.add_assignment(node.prop.compile_access(v, params[0]), params[1])
353 redef class MMMethSrcMethod
354 redef meth do_compile_inside(v, params)
356 return node.do_compile_inside(v, self, params)
361 # Compile a subtype check to self
363 meth compile_cast(v: CompilerVisitor, recv: String): String
365 # Fixme: handle formaltypes
366 var g = local_class.global
367 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast
{self}*/"
370 # Compile a cast assertion
371 meth compile_type_check(v: CompilerVisitor, recv: String, n: PNode)
373 # Fixme: handle formaltypes
374 var g = local_class.global
375 v.add_instr("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \
{ fprintf(stderr, \"Cast failled at {n.locate}\\n\
"); nit_exit(1); } /*cast {self}*/;")
379 ###############################################################################
381 redef class AMethPropdef
382 # Compile the method body
383 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
386 redef class AConcreteMethPropdef
387 redef meth do_compile_inside
(v
, method
, params
)
389 var orig_meth
: MMLocalProperty = method
.global
.intro
390 var orig_sig
= orig_meth
.signature
.adaptation_to
(method
.signature
.recv
)
391 if n_signature
!= null then
392 var sig
= n_signature
393 assert sig
isa ASignature
394 for ap
in sig
.n_params
do
395 var cname
= v
.get_var
396 v
.varnames
[ap
.variable
] = cname
397 var orig_type
= orig_sig
[ap
.position
]
398 if not orig_type
< ap
.variable
.stype
then
399 # FIXME: do not test always
400 # FIXME: handle formal types
401 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
402 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
+ 1], ap
)
404 v
.add_assignment
(cname
, params
[ap
.position
+ 1])
407 var old_method_params
= v
.method_params
408 var old_return_label
= v
.return_label
409 var old_return_value
= v
.return_value
410 var old_has_return
= v
.has_return
413 if self isa AConcreteInitPropdef then
414 itpos
= "VAL2OBJ({params[0]})->vft[{method.local_class.global.init_table_pos_id}].i"
415 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
416 v
.add_instr
("if (init_table[{itpos}]) return;")
419 v
.method_params
= params
421 v
.return_label
= "return_label{v.new_number}"
422 if method
.signature
.return_type
!= null then
423 v
.return_value
= v
.get_var
424 v
.free_var
(v
.return_value
)
426 v
.return_value
= null
428 if self isa AConcreteInitPropdef then
430 v
.invoke_super_init_calls_after
(null)
432 if n_block
!= null then
433 v
.compile_stmt
(n_block
)
436 v
.add_instr
("{v.return_label}: while(false);")
438 if self isa AConcreteInitPropdef then
439 v
.add_instr
("init_table[{itpos}] = 1;")
441 var ret
= v
.return_value
442 v
.method_params
= old_method_params
443 v
.return_label
= old_return_label
444 v
.return_value
= old_return_value
445 v
.has_return
= old_has_return
450 redef class ADeferredMethPropdef
451 redef meth do_compile_inside
(v
, method
, params
)
453 v
.add_instr
("fprintf(stderr, \"Deferred method
{name} called
({first_token.locate})\\n\
");")
454 v
.add_instr
("nit_exit(1);")
455 if method
.signature
.return_type
!= null then
463 redef class AExternMethPropdef
464 redef meth do_compile_inside
(v
, method
, params
)
466 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
467 if n_extern
!= null then
468 ename
= n_extern
.text
469 ename
= ename
.substring
(1, ename
.length-2
)
471 var sig
= method
.signature
472 if params
.length
!= sig
.arity
+ 1 then
473 printl
("par:{params.length} sig:{sig.arity}")
475 var args
= new Array[String]
476 args
.add
(sig
.recv
.unboxtype
(params
[0]))
477 for i
in [0..sig
.arity
[ do
478 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
480 var s
= "{ename}({args.join(", ")})"
481 if sig
.return_type
!= null then
482 return sig
.return_type
.boxtype
(s
)
490 redef class AInternMethPropdef
491 redef meth do_compile_inside
(v
, method
, p
)
493 var c
= method
.local_class
.name
496 if c
== once
"Int".to_symbol
then
497 if n
== once
"object_id".to_symbol
then
499 else if n
== once
"unary -".to_symbol
then
500 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
501 else if n
== once
"output".to_symbol
then
502 v
.add_instr
("printf(\"%d\\n\
", UNTAG_Int({p[0]}));")
503 else if n
== once
"ascii".to_symbol
then
504 s
= "TAG_Char(UNTAG_Int({p[0]}))"
505 else if n
== once
"succ".to_symbol
then
506 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
507 else if n
== once
"prec".to_symbol
then
508 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
509 else if n
== once
"to_f".to_symbol
then
510 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
511 else if n
== once
"+".to_symbol
then
512 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
513 else if n
== once
"-".to_symbol
then
514 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
515 else if n
== once
"*".to_symbol
then
516 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
517 else if n
== once
"/".to_symbol
then
518 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
519 else if n
== once
"%".to_symbol
then
520 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
521 else if n
== once
"<".to_symbol
then
522 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
523 else if n
== once
">".to_symbol
then
524 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
525 else if n
== once
"<=".to_symbol
then
526 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
527 else if n
== once
">=".to_symbol
then
528 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
529 else if n
== once
"lshift".to_symbol
then
530 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
531 else if n
== once
"rshift".to_symbol
then
532 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
533 else if n
== once
"==".to_symbol
then
534 s
= "TAG_Bool(({p[0]})==({p[1]}))"
535 else if n
== once
"!=".to_symbol
then
536 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
538 else if c
== once
"Float".to_symbol
then
539 if n
== once
"object_id".to_symbol
then
540 s
= "TAG_Int((int)UNBOX_Float({p[0]}))"
541 else if n
== once
"unary -".to_symbol
then
542 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
543 else if n
== once
"output".to_symbol
then
544 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
545 else if n
== once
"to_i".to_symbol
then
546 s
= "TAG_Int((int)UNBOX_Float({p[0]}))"
547 else if n
== once
"+".to_symbol
then
548 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
549 else if n
== once
"-".to_symbol
then
550 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
551 else if n
== once
"*".to_symbol
then
552 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
553 else if n
== once
"/".to_symbol
then
554 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
555 else if n
== once
"<".to_symbol
then
556 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
557 else if n
== once
">".to_symbol
then
558 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
559 else if n
== once
"<=".to_symbol
then
560 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
561 else if n
== once
">=".to_symbol
then
562 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
564 else if c
== once
"Char".to_symbol
then
565 if n
== once
"object_id".to_symbol
then
566 s
= "TAG_Int(UNTAG_Char({p[0]}))"
567 else if n
== once
"unary -".to_symbol
then
568 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
569 else if n
== once
"output".to_symbol
then
570 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
571 else if n
== once
"ascii".to_symbol
then
572 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
573 else if n
== once
"succ".to_symbol
then
574 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
575 else if n
== once
"prec".to_symbol
then
576 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
577 else if n
== once
"to_i".to_symbol
then
578 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
579 else if n
== once
"+".to_symbol
then
580 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
581 else if n
== once
"-".to_symbol
then
582 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
583 else if n
== once
"*".to_symbol
then
584 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
585 else if n
== once
"/".to_symbol
then
586 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
587 else if n
== once
"%".to_symbol
then
588 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
589 else if n
== once
"<".to_symbol
then
590 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
591 else if n
== once
">".to_symbol
then
592 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
593 else if n
== once
"<=".to_symbol
then
594 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
595 else if n
== once
">=".to_symbol
then
596 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
597 else if n
== once
"==".to_symbol
then
598 s
= "TAG_Bool(({p[0]})==({p[1]}))"
599 else if n
== once
"!=".to_symbol
then
600 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
602 else if c
== once
"Bool".to_symbol
then
603 if n
== once
"object_id".to_symbol
then
604 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
605 else if n
== once
"unary -".to_symbol
then
606 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
607 else if n
== once
"output".to_symbol
then
608 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
609 else if n
== once
"ascii".to_symbol
then
610 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
611 else if n
== once
"to_i".to_symbol
then
612 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
613 else if n
== once
"==".to_symbol
then
614 s
= "TAG_Bool(({p[0]})==({p[1]}))"
615 else if n
== once
"!=".to_symbol
then
616 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
618 else if c
== once
"NativeArray".to_symbol
then
619 if n
== once
"object_id".to_symbol
then
620 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
621 else if n
== once
"[]".to_symbol
then
622 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
623 else if n
== once
"[]=".to_symbol
then
624 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
625 else if n
== once
"copy_to".to_symbol
then
626 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
628 else if c
== once
"NativeString".to_symbol
then
629 if n
== once
"object_id".to_symbol
then
630 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
631 else if n
== once
"atoi".to_symbol
then
632 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
633 else if n
== once
"[]".to_symbol
then
634 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
635 else if n
== once
"[]=".to_symbol
then
636 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
637 else if n
== once
"copy_to".to_symbol
then
638 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]}));")
640 else if n
== once
"object_id".to_symbol
then
641 s
= "TAG_Int((int){p[0]})"
642 else if n
== once
"sys".to_symbol
then
644 else if n
== once
"is_same_type".to_symbol
then
645 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
646 else if n
== once
"exit".to_symbol
then
647 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
648 else if n
== once
"calloc_array".to_symbol
then
649 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
650 else if n
== once
"calloc_string".to_symbol
then
651 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
654 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
656 if method
.signature
.return_type
!= null and s
== null then
657 s
= "NIT_NULL /*stub*/"
663 ###############################################################################
666 # Compile the node as an expression
667 # Only the visitor should call it
668 meth compile_expr
(v
: CompilerVisitor): String is abstract
670 # Prepare a call of node as a statement
671 # Only the visitor should call it
672 # It's used for local variable managment
673 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
675 # Compile the node as a statement
676 # Only the visitor should call it
677 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
680 redef class ABlockExpr
681 redef meth compile_stmt
(v
)
689 redef class AVardeclExpr
690 redef meth prepare_compile_stmt
(v
)
692 var cname
= v
.get_var
693 v
.varnames
[variable
] = cname
696 redef meth compile_stmt
(v
)
698 var cname
= v
.varnames
[variable
]
699 if n_expr
== null then
700 var t
= variable
.stype
701 v
.add_assignment
(cname
, "{t.default_cvalue} /*decl variable {variable.name}*/")
703 var e
= v
.compile_expr
(n_expr
)
704 v
.add_assignment
(cname
, e
)
709 redef class AReturnExpr
710 redef meth compile_stmt
(v
)
713 if n_expr
!= null then
714 var e
= v
.compile_expr
(n_expr
)
715 v
.add_assignment
(v
.return_value
, e
)
717 v
.add_instr
("goto {v.return_label};")
721 redef class ABreakExpr
722 redef meth compile_stmt
(v
)
724 v
.add_instr
("goto {v.break_label};")
728 redef class AContinueExpr
729 redef meth compile_stmt
(v
)
731 v
.add_instr
("goto {v.continue_label};")
735 redef class AAbortExpr
736 redef meth compile_stmt
(v
)
738 v
.add_instr
("fprintf(stderr, \"Aborted: {locate}\\n\
"); nit_exit(1);")
743 redef meth compile_stmt
(v
)
745 if n_block
!= null then
746 v
.compile_stmt
(n_block
)
752 redef meth compile_stmt
(v
)
754 var e
= v
.compile_expr
(n_expr
)
755 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
757 if n_then
!= null then
759 v
.compile_stmt
(n_then
)
762 if n_else
!= null then
763 v
.add_instr
("} else \{ /*if*/")
765 v
.compile_stmt
(n_else
)
772 redef class AIfexprExpr
773 redef meth compile_expr
(v
)
775 var e
= v
.compile_expr
(n_expr
)
776 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
779 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
781 v
.add_instr
("} else \{ /*if*/")
784 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
785 v
.add_assignment
(e
, e2
)
792 redef class AControlableBlock
793 meth compile_inside_block
(v
: CompilerVisitor) is abstract
794 redef meth compile_stmt
(v
)
796 var old_break_label
= v
.break_label
797 var old_continue_label
= v
.continue_label
798 var id
= v
.new_number
799 v
.break_label
= "break_{id}"
800 v
.continue_label
= "continue_{id}"
802 compile_inside_block
(v
)
805 v
.break_label
= old_break_label
806 v
.continue_label
= old_continue_label
810 redef class AWhileExpr
811 redef meth compile_inside_block
(v
)
813 v
.add_instr
("while (true) \{ /*while*/")
815 var e
= v
.compile_expr
(n_expr
)
816 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
818 if n_block
!= null then
819 v
.compile_stmt
(n_block
)
821 v
.add_instr
("{v.continue_label}: while(0);")
824 v
.add_instr
("{v.break_label}: while(0);")
829 redef meth compile_inside_block
(v
)
831 v
.compile_stmt
(n_vardecl
)
835 redef class AForVardeclExpr
836 redef meth compile_stmt
(v
)
838 var e
= v
.compile_expr
(n_expr
)
839 var prop
= n_expr
.stype
.select_method
(once
"iterator".to_symbol
)
841 printl
("No iterator")
844 var ittype
= prop
.signature
.return_type
847 v
.add_assignment
(iter
, prop
.compile_call
(v
, [e
]))
848 var prop2
= ittype
.select_method
(once
"is_ok".to_symbol
)
849 if prop2
== null then
853 var prop3
= ittype
.select_method
(once
"item".to_symbol
)
854 if prop3
== null then
858 var prop4
= ittype
.select_method
(once
"next".to_symbol
)
859 if prop4
== null then
863 v
.add_instr
("while (true) \{ /*for*/")
866 v
.add_assignment
(ok
, prop2
.compile_call
(v
, [iter
]))
867 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
869 var e
= prop3
.compile_call
(v
, [iter
])
871 v
.varnames
[variable
] = e
873 assert par
isa AForExpr
874 var n_block
= par
.n_block
875 if n_block
!= null then
876 v
.compile_stmt
(n_block
)
878 v
.add_instr
("{v.continue_label}: while(0);")
879 e
= prop4
.compile_call
(v
, [iter
])
883 v
.add_instr
("{v.break_label}: while(0);")
887 redef class AAssertExpr
888 redef meth compile_stmt
(v
)
890 var e
= v
.compile_expr
(n_expr
)
893 s
= "Assert '{n_id.text}' "
895 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"{s} failed: {locate}\\n\"); nit_exit
(1);}")
900 redef meth compile_expr(v)
902 return " {v.varnames[variable]} /*{variable.name}*/"
906 redef class AVarAssignExpr
907 redef meth compile_stmt(v)
909 var e = v.compile_expr(n_value)
910 v.add_assignment(v.varnames[variable], "{e} /*{variable.name}=*/")
914 redef class AVarReassignExpr
915 redef meth compile_stmt(v)
917 var e1 = v.varnames[variable]
918 var e2 = v.compile_expr(n_value)
919 var e3 = assign_method.compile_call(v, [e1, e2])
920 v.add_assignment(v.varnames[variable], "{e3} /*{variable.name}*/")
924 redef class ASelfExpr
925 redef meth compile_expr(v)
927 return v.method_params[0]
932 redef meth compile_expr(v)
934 var e = v.ensure_var(v.compile_expr(n_expr))
935 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
938 var e2 = v.compile_expr(n_expr2)
939 v.add_assignment(e, e2)
947 redef meth compile_expr(v)
949 var e = v.ensure_var(v.compile_expr(n_expr))
950 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
953 var e2 = v.compile_expr(n_expr2)
954 v.add_assignment(e, e2)
962 redef meth compile_expr(v)
964 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
969 redef meth compile_expr(v)
971 var e = v.compile_expr(n_expr)
972 var e2 = v.compile_expr(n_expr2)
973 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
978 redef meth compile_expr(v)
980 var e = v.compile_expr(n_expr)
981 return n_type.stype.compile_cast(v, e)
985 redef class ATrueExpr
986 redef meth compile_expr(v)
988 return " TAG_Bool(true)"
992 redef class AFalseExpr
993 redef meth compile_expr(v)
995 return " TAG_Bool(false)"
1000 redef meth compile_expr(v)
1002 return " TAG_Int({n_number.text})"
1006 redef class AFloatExpr
1007 redef meth compile_expr(v)
1009 return "BOX_Float({n_float.text})"
1013 redef class ACharExpr
1014 redef meth compile_expr(v)
1016 return " TAG_Char({n_char.text})"
1020 redef class AStringFormExpr
1021 redef meth compile_expr(v)
1023 var prop = stype.select_method(once "with_native
".to_symbol)
1025 return prop.compile_constructor_call(v, ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1028 # The raw string value
1029 protected meth string_text: String is abstract
1031 # The string in a C native format
1032 protected attr _cstring: String
1034 # The string length in bytes
1035 protected attr _cstring_length: Int
1037 # Compute _cstring and _cstring_length using string_text
1038 protected meth compute_string_info
1041 var str = string_text
1042 var res = new String
1044 while i < str.length do
1049 if c2 != '{' and c2 != '}' then
1059 _cstring_length = len
1063 redef class AStringExpr
1064 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1066 redef class AStartStringExpr
1067 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1069 redef class AMidStringExpr
1070 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1072 redef class AEndStringExpr
1073 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1076 redef class ASuperstringExpr
1077 redef meth compile_expr(v)
1079 var prop = stype.select_method(once "init".to_symbol)
1080 var recv = prop.compile_constructor_call(v, new Array[String])
1082 var prop2 = stype.select_method(once "append
".to_symbol)
1084 var prop3 = stype.select_method(once "to_s
".to_symbol)
1085 for ne in n_exprs do
1086 var e = v.ensure_var(v.compile_expr(ne))
1087 if ne.stype != stype then
1088 v.add_assignment(e, prop3.compile_call(v, [e]))
1090 prop2.compile_call(v, [recv, e])
1097 redef class ANullExpr
1098 redef meth compile_expr(v)
1100 return " NIT_NULL /*null*/"
1104 redef class AArrayExpr
1105 redef meth compile_expr(v)
1107 var prop = stype.select_method(once "with_capacity
".to_symbol)
1108 var recv = prop.compile_constructor_call(v,["TAG_Int({n_exprs.length})"])
1110 var prop2 = stype.select_method(once "add
".to_symbol)
1111 for ne in n_exprs do
1112 var e = v.compile_expr(ne)
1113 prop2.compile_call(v, [recv, e])
1119 redef class ARangeExpr
1120 redef meth compile_expr(v)
1122 var prop = stype.select_method(propname)
1123 var e = v.compile_expr(n_expr)
1124 var e2 = v.compile_expr(n_expr2)
1125 return prop.compile_constructor_call(v, [e, e2])
1127 # The constructor that must be used for the range
1128 protected meth propname: Symbol is abstract
1131 redef class ACrangeExpr
1132 redef meth propname do return once "init".to_symbol
1134 redef class AOrangeExpr
1135 redef meth propname do return once "without_last
".to_symbol
1138 redef class ASuperExpr
1139 redef meth compile_stmt(v)
1141 var e = compile_expr(v)
1142 if e != null then v.add_instr("{e};")
1145 redef meth compile_expr(v)
1147 var arity = v.method_params.length - 1
1148 if init_in_superclass != null then
1149 arity = init_in_superclass.signature.arity
1151 var args = new Array[String].with_capacity(arity + 1)
1152 args.add(v.method_params[0])
1153 if n_args.length != arity then
1154 for i in [0..arity[ do
1155 args.add(v.method_params[i + 1])
1159 args.add(v.compile_expr(na))
1162 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1163 if init_in_superclass != null then
1164 return init_in_superclass.compile_call(v, args)
1166 if prop.global.is_init then args.add("init_table
")
1167 return prop.compile_super_call(v, args)
1172 redef class AAttrExpr
1173 redef meth compile_expr(v)
1175 var e = v.compile_expr(n_expr)
1176 return prop.compile_access(v, e)
1180 redef class AAttrAssignExpr
1181 redef meth compile_stmt(v)
1183 var e = v.compile_expr(n_expr)
1184 var e2 = v.compile_expr(n_value)
1185 v.add_assignment(prop.compile_access(v, e), e2)
1188 redef class AAttrReassignExpr
1189 redef meth compile_stmt(v)
1191 var e1 = v.compile_expr(n_expr)
1192 var e2 = prop.compile_access(v, e1)
1193 var e3 = v.compile_expr(n_value)
1194 var e4 = assign_method.compile_call(v, [e2, e3])
1195 v.add_assignment(e2, e4)
1199 redef class ASendExpr
1200 redef meth compile_expr(v)
1202 var recv = v.compile_expr(n_expr)
1203 var cargs = new Array[String]
1205 for a in arguments do
1206 cargs.add(v.compile_expr(a))
1209 var e = prop.compile_call(v, cargs)
1210 if prop.global.is_init then
1211 v.invoke_super_init_calls_after(prop)
1216 redef meth compile_stmt(v)
1218 var e = compile_expr(v)
1220 v.add_instr(e + ";")
1225 redef class ASendReassignExpr
1226 redef meth compile_expr(v)
1228 var recv = v.compile_expr(n_expr)
1229 var cargs = new Array[String]
1231 for a in arguments do
1232 cargs.add(v.compile_expr(a))
1235 var e2 = read_prop.compile_call(v, cargs)
1236 var e3 = v.compile_expr(n_value)
1237 var e4 = assign_method.compile_call(v, [e2, e3])
1239 return prop.compile_call(v, cargs)
1243 redef class ANewExpr
1244 redef meth compile_expr(v)
1246 var cargs = new Array[String]
1247 for a in arguments do
1248 cargs.add(v.compile_expr(a))
1250 return prop.compile_constructor_call(v, cargs)
1254 redef class AProxyExpr
1255 redef meth compile_expr(v)
1257 return v.compile_expr(n_expr)
1261 redef class AOnceExpr
1262 redef meth compile_expr(v)
1264 var i = v.new_number
1265 var cvar = v.get_var
1266 v.add_decl("static val_t once_value_
{cvar}_
{i}; static int once_bool_
{cvar}_
{i};")
1267 v.add_instr("if (once_bool_
{cvar}_
{i}) {cvar} = once_value_
{cvar}_
{i};")
1268 v.add_instr("else \
{")
1271 var e = v.compile_expr(n_expr)
1272 v.add_assignment(cvar, e)
1273 v.add_instr("once_value_
{cvar}_
{i} = {cvar};")
1274 v.add_instr("once_bool_
{cvar}_
{i} = true;")