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 # C outputs written outside the current C function.
71 readable writable attr _out_contexts
: Array[CContext] = new Array[CContext]
73 # Generate an fprintf to display an error location
74 meth printf_locate_error
(node
: PNode): String
76 var s
= "fprintf(stderr, \""
77 if nmc != null then s.append(" in %s
")
78 s.append(" (%s
:%d
)\\n\
", ")
79 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
80 s
.append
("LOCATE_{module.name}, {node.line_number});")
84 redef init(module: MMSrcModule)
89 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
91 var n
= nmc
.method
.node
92 assert n
isa AConcreteInitPropdef
94 if n
.super_init_calls
.is_empty
then return
98 if start_prop
!= null then
99 while n
.super_init_calls
[i
] != start_prop
do
100 #s.append(" {n.super_init_calls[i]}")
104 #s.append(" {start_prop}")
106 while n
.explicit_super_init_calls
[j
] != start_prop
do
111 var stop_prop
: MMMethod = null
112 if j
< n
.explicit_super_init_calls
.length
then
113 stop_prop
= n
.explicit_super_init_calls
[j
]
115 var l
= n
.super_init_calls
.length
118 var p
= n
.super_init_calls
[i
]
119 if p
== stop_prop
then break
120 var cargs
= new Array[String]
121 if p
.signature
.arity
== 0 then
122 cargs
.add
(cfc
.varname
(nmc
.method_params
[0]))
124 for va
in nmc
.method_params
do
125 cargs
.add
(cfc
.varname
(va
))
129 p
.compile_call
(self, cargs
)
134 # s.append(" {n.super_init_calls[i]}")
137 #if stop_prop != null then s.append(" (stop at {stop_prop})")
138 #n.printl("implicit calls in {n.method}: {s}")
142 # A C function currently written
143 class CFunctionContext
144 readable attr _visitor
: CompilerVisitor
146 # Next available variable number
147 attr _variable_index
: Int = 0
149 # Total number of variable
150 attr _variable_index_max
: Int = 0
152 # Association between nit variable and the corrsponding c variable
153 attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
155 # Are we currenlty in a closure definition?
156 readable writable attr _in_closure
: Bool = false
158 meth varname
(v
: Variable): String
161 return "closctx->{_varnames[v]}"
167 # Return the next available variable
170 var v
= variable
(_variable_index
)
171 _variable_index
= _variable_index
+ 1
172 if _variable_index
> _variable_index_max
then
173 #visitor.add_decl("val_t {v};")
174 _variable_index_max
= _variable_index
179 meth register_variable
(v
: Variable): String
182 _varnames
[v
] = "variable[{_variable_index-1}]"
186 # Next available closure variable number
187 attr _closurevariable_index
: Int = 0
189 meth register_closurevariable
(v
: ClosureVariable): String
191 var s
= "closurevariable[{_closurevariable_index}]"
192 _closurevariable_index
+= 1
195 return "(closctx->{s})"
201 # Return the ith variable
202 protected meth variable
(i
: Int): String
205 return "(closctx->variable[{i}])"
207 return "variable[{i}]"
211 # Mark the variable available
212 meth free_var
(v
: String)
215 if v
== variable
(_variable_index-1
) then
216 _variable_index
= _variable_index
- 1
220 # Generate the local variable declarations
221 # To use at the end of the C function once all variables are known
222 meth generate_var_decls
224 if _variable_index_max
> 0 then
225 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
227 visitor
.add_decl
("val_t *variable = NULL;")
229 if _closurevariable_index
> 0 then
230 visitor
.add_decl
("void *closurevariable[{_closurevariable_index}];")
232 visitor
.add_decl
("void **closurevariable = NULL;")
236 init(v
: CompilerVisitor) do _visitor
= v
239 # A Nit method currenlty compiled
240 class NitMethodContext
241 # Current method compiled
242 readable attr _method
: MMSrcMethod
244 # Association between parameters and the corresponding variables
245 readable writable attr _method_params
: Array[ParamVariable]
247 # Where a nit return must branch
248 readable writable attr _return_label
: String
250 # Where a nit break must branch
251 readable writable attr _break_label
: String
253 # Where a nit continue must branch
254 readable writable attr _continue_label
: String
256 # Variable where a functionnal nit return must store its value
257 readable writable attr _return_value
: String
259 # Variable where a functionnal nit break must store its value
260 readable writable attr _break_value
: String
262 # Variable where a functionnal nit continue must store its value
263 readable writable attr _continue_value
: String
265 init(method
: MMSrcMethod)
271 ###############################################################################
273 redef class ClosureVariable
274 readable writable attr _ctypename
: String
278 # Compile a call on self for given arguments
279 # Most calls are compiled with a table access,
280 # primitive calles are inlined
281 # == and != are guarded and possibly inlined
282 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
285 if i
isa MMSrcMethod then
286 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
287 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
289 var e
= i
.do_compile_inside
(v
, cargs
)
293 var ee
= once
"==".to_symbol
294 var ne
= once
"!=".to_symbol
296 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
297 var eqcall
= eqp
.compile_call
(v
, cargs
)
298 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
300 if global
.is_init
then
302 cargs
.add
("init_table /*YYY*/")
305 var m
= "(({cname}_t)CALL({cargs[0]},{global.color_id}))"
306 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
308 vcall
= "UNTAG_Bool({vcall})"
309 var obj
= once
"Object".to_symbol
310 if i
.local_class
.name
== obj
then
311 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
313 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
315 if signature
.return_type
!= null then
318 v
.add_instr
(vcall
+ ";")
323 # Compile a call as constructor with given args
324 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
326 var recv
= v
.cfc
.get_var
327 v
.add_instr
("{recv} = NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}); /*new {recvtype}*/")
331 # Compile a call as call-next-method on self with given args
332 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
334 var m
= "(({cname}_t)CALL({cargs[0]},{color_id_for_super}))"
335 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
339 # Cname of the i-th closure C struct type
340 protected meth closure_cname
(i
: Int): String
342 return "WBT_{cname}_{i}"
346 redef class MMAttribute
347 # Compile an acces on selffor a given reciever.
348 # Result is a valid C left-value for assigment
349 meth compile_access
(v
: CompilerVisitor, recv
: String): String
351 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
355 redef class MMLocalProperty
356 # Compile the property as a C property
357 meth compile_property_to_c
(v
: CompilerVisitor) do end
360 redef class MMSrcMethod
362 # Compile and declare the signature to C
363 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
365 var params
= new Array[String]
366 params
.add
("val_t {args[0]}")
367 for i
in [0..signature
.arity
[ do
368 var p
= "val_t {args[i+1]}"
372 var first_closure_index
= signature
.arity
+ 1 # Wich parameter is the first closure
373 for i
in [0..signature
.closures
.length
[ do
374 var closcn
= closure_cname
(i
)
375 var cs
= signature
.closures
[i
].signature
# Closure signature
376 var subparams
= new Array[String] # Parameters of the closure
377 subparams
.add
("struct {closcn}*")
378 for j
in [0..cs
.arity
[ do
383 if cs
.return_type
!= null then r
= "val_t"
384 params
.add
("struct {closcn} *{args[first_closure_index+i]}")
385 v
.add_decl
("struct {closcn};")
386 v
.add_decl
("typedef {r} (*F{closcn})({subparams.join(", ")});")
387 v
.add_decl
("struct {closcn} \{F{closcn} fun; val_t *has_broke; val_t broke_value; val_t *variable; void **closurevariable;\};")
390 if global
.is_init
then
391 params
.add
("int* init_table")
395 if signature
.return_type
!= null then
401 var p
= params
.join
(", ")
402 var s
= "{ret} {cname}({p})"
403 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
408 redef meth compile_property_to_c
(v
)
410 v
.cfc
= new CFunctionContext(v
)
412 var args
= new Array[String]
414 for i
in [0..signature
.arity
[ do
415 args
.add
(" param{i}")
417 for i
in [0..signature
.closures
.length
[ do
420 var cs
= decl_csignature
(v
, args
)
421 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
423 v
.add_instr
("{cs} \{")
432 if s
.node
!= null then ln
= s
.node
.line_number
433 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
434 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
435 v
.add_instr
("trace.file = LOCATE_{module.name};")
436 var s
= do_compile_inside
(v
, args
)
437 v
.add_instr
("tracehead = trace.prev;")
439 v
.add_instr
("return;")
441 v
.add_instr
("return {s};")
444 v
.cfc
.generate_var_decls
446 ctx_old
.append
(v
.ctx
)
451 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)
454 # Compile the method body inline
455 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
458 redef class MMReadImplementationMethod
459 redef meth do_compile_inside
(v
, params
)
461 return node
.prop
.compile_access
(v
, params
[0])
465 redef class MMWriteImplementationMethod
466 redef meth do_compile_inside
(v
, params
)
468 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
473 redef class MMMethSrcMethod
474 redef meth do_compile_inside
(v
, params
)
476 return node
.do_compile_inside
(v
, self, params
)
480 redef class MMImplicitInit
481 redef meth do_compile_inside
(v
, params
)
483 var f
= params
.length
- unassigned_attributes
.length
484 var recv
= params
.first
485 for sp
in super_inits
do
486 assert sp
isa MMMethod
487 var args_recv
= [recv
]
488 if sp
== super_init
then
489 var args
= new Array[String].with_capacity
(f
)
494 sp
.compile_call
(v
, args
)
496 sp
.compile_call
(v
, args_recv
)
499 for i
in [f
..params
.length
[ do
500 var attribute
= unassigned_attributes
[i-f
]
501 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
508 # Compile a subtype check to self
510 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
512 # Fixme: handle formaltypes
513 var g
= local_class
.global
514 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
517 # Compile a cast assertion
518 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
520 # Fixme: handle formaltypes
521 var g
= local_class
.global
522 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}*/;")
526 ###############################################################################
528 redef class AMethPropdef
529 # Compile the method body
530 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
533 redef class AConcreteMethPropdef
534 redef meth do_compile_inside
(v
, method
, params
)
537 v
.nmc
= new NitMethodContext(method
)
539 var cname
= v
.cfc
.register_variable
(self_var
)
540 v
.add_assignment
(cname
, params
[0])
541 v
.nmc
.method_params
= [self_var
]
543 var orig_meth
: MMLocalProperty = method
.global
.intro
544 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
545 if n_signature
!= null then
546 var sig
= n_signature
547 assert sig
isa ASignature
548 for ap
in sig
.n_params
do
549 var cname
= v
.cfc
.register_variable
(ap
.variable
)
550 v
.nmc
.method_params
.add
(ap
.variable
)
551 var orig_type
= orig_sig
[ap
.position
]
552 if not orig_type
< ap
.variable
.stype
then
553 # FIXME: do not test always
554 # FIXME: handle formal types
555 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
556 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
+ 1], ap
)
558 v
.add_assignment
(cname
, params
[ap
.position
+ 1])
560 for i
in [0..sig
.n_closure_decls
.length
[ do
561 var wd
= sig
.n_closure_decls
[i
]
562 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
563 wd
.variable
.ctypename
= "struct {method.closure_cname(i)} *"
564 v
.add_assignment
(cname
, "{params[orig_sig.arity + i + 1]}")
568 var itpos
: String = null
569 if self isa AConcreteInitPropdef then
570 itpos
= "VAL2OBJ({params[0]})->vft[{method.local_class.global.init_table_pos_id}].i"
571 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
572 v
.add_instr
("if (init_table[{itpos}]) return;")
575 v
.nmc
.return_label
= "return_label{v.new_number}"
576 v
.nmc
.return_value
= v
.cfc
.get_var
577 if self isa AConcreteInitPropdef then
578 v
.invoke_super_init_calls_after
(null)
580 if n_block
!= null then
581 v
.compile_stmt
(n_block
)
583 v
.add_instr
("{v.nmc.return_label}: while(false);")
584 if self isa AConcreteInitPropdef then
585 v
.add_instr
("init_table[{itpos}] = 1;")
588 var ret
: String = null
589 if method
.signature
.return_type
!= null then
590 ret
= v
.nmc
.return_value
598 redef class ADeferredMethPropdef
599 redef meth do_compile_inside
(v
, method
, params
)
601 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
602 v
.add_instr
(v
.printf_locate_error
(self))
603 v
.add_instr
("nit_exit(1);")
604 if method
.signature
.return_type
!= null then
612 redef class AExternMethPropdef
613 redef meth do_compile_inside
(v
, method
, params
)
615 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
616 if n_extern
!= null then
617 ename
= n_extern
.text
618 ename
= ename
.substring
(1, ename
.length-2
)
620 var sig
= method
.signature
621 if params
.length
!= sig
.arity
+ 1 then
622 printl
("par:{params.length} sig:{sig.arity}")
624 var args
= new Array[String]
625 args
.add
(sig
.recv
.unboxtype
(params
[0]))
626 for i
in [0..sig
.arity
[ do
627 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
629 var s
= "{ename}({args.join(", ")})"
630 if sig
.return_type
!= null then
631 return sig
.return_type
.boxtype
(s
)
639 redef class AInternMethPropdef
640 redef meth do_compile_inside
(v
, method
, p
)
642 var c
= method
.local_class
.name
645 if c
== once
"Int".to_symbol
then
646 if n
== once
"object_id".to_symbol
then
648 else if n
== once
"unary -".to_symbol
then
649 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
650 else if n
== once
"output".to_symbol
then
651 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
652 else if n
== once
"ascii".to_symbol
then
653 s
= "TAG_Char(UNTAG_Int({p[0]}))"
654 else if n
== once
"succ".to_symbol
then
655 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
656 else if n
== once
"prec".to_symbol
then
657 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
658 else if n
== once
"to_f".to_symbol
then
659 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
660 else if n
== once
"+".to_symbol
then
661 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
662 else if n
== once
"-".to_symbol
then
663 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
664 else if n
== once
"*".to_symbol
then
665 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
666 else if n
== once
"/".to_symbol
then
667 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
668 else if n
== once
"%".to_symbol
then
669 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
670 else if n
== once
"<".to_symbol
then
671 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
672 else if n
== once
">".to_symbol
then
673 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
674 else if n
== once
"<=".to_symbol
then
675 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
676 else if n
== once
">=".to_symbol
then
677 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
678 else if n
== once
"lshift".to_symbol
then
679 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
680 else if n
== once
"rshift".to_symbol
then
681 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
682 else if n
== once
"==".to_symbol
then
683 s
= "TAG_Bool(({p[0]})==({p[1]}))"
684 else if n
== once
"!=".to_symbol
then
685 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
687 else if c
== once
"Float".to_symbol
then
688 if n
== once
"object_id".to_symbol
then
689 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
690 else if n
== once
"unary -".to_symbol
then
691 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
692 else if n
== once
"output".to_symbol
then
693 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
694 else if n
== once
"to_i".to_symbol
then
695 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
696 else if n
== once
"+".to_symbol
then
697 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
698 else if n
== once
"-".to_symbol
then
699 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
700 else if n
== once
"*".to_symbol
then
701 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
702 else if n
== once
"/".to_symbol
then
703 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
704 else if n
== once
"<".to_symbol
then
705 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
706 else if n
== once
">".to_symbol
then
707 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
708 else if n
== once
"<=".to_symbol
then
709 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
710 else if n
== once
">=".to_symbol
then
711 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
713 else if c
== once
"Char".to_symbol
then
714 if n
== once
"object_id".to_symbol
then
715 s
= "TAG_Int(UNTAG_Char({p[0]}))"
716 else if n
== once
"unary -".to_symbol
then
717 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
718 else if n
== once
"output".to_symbol
then
719 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
720 else if n
== once
"ascii".to_symbol
then
721 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
722 else if n
== once
"succ".to_symbol
then
723 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
724 else if n
== once
"prec".to_symbol
then
725 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
726 else if n
== once
"to_i".to_symbol
then
727 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
728 else if n
== once
"+".to_symbol
then
729 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
730 else if n
== once
"-".to_symbol
then
731 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
732 else if n
== once
"*".to_symbol
then
733 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
734 else if n
== once
"/".to_symbol
then
735 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
736 else if n
== once
"%".to_symbol
then
737 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
738 else if n
== once
"<".to_symbol
then
739 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
740 else if n
== once
">".to_symbol
then
741 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
742 else if n
== once
"<=".to_symbol
then
743 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
744 else if n
== once
">=".to_symbol
then
745 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
746 else if n
== once
"==".to_symbol
then
747 s
= "TAG_Bool(({p[0]})==({p[1]}))"
748 else if n
== once
"!=".to_symbol
then
749 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
751 else if c
== once
"Bool".to_symbol
then
752 if n
== once
"object_id".to_symbol
then
753 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
754 else if n
== once
"unary -".to_symbol
then
755 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
756 else if n
== once
"output".to_symbol
then
757 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
758 else if n
== once
"ascii".to_symbol
then
759 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
760 else if n
== once
"to_i".to_symbol
then
761 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
762 else if n
== once
"==".to_symbol
then
763 s
= "TAG_Bool(({p[0]})==({p[1]}))"
764 else if n
== once
"!=".to_symbol
then
765 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
767 else if c
== once
"NativeArray".to_symbol
then
768 if n
== once
"object_id".to_symbol
then
769 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
770 else if n
== once
"[]".to_symbol
then
771 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
772 else if n
== once
"[]=".to_symbol
then
773 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
774 else if n
== once
"copy_to".to_symbol
then
775 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
777 else if c
== once
"NativeString".to_symbol
then
778 if n
== once
"object_id".to_symbol
then
779 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
780 else if n
== once
"atoi".to_symbol
then
781 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
782 else if n
== once
"[]".to_symbol
then
783 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
784 else if n
== once
"[]=".to_symbol
then
785 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
786 else if n
== once
"copy_to".to_symbol
then
787 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]}));")
789 else if n
== once
"object_id".to_symbol
then
790 s
= "TAG_Int((bigint){p[0]})"
791 else if n
== once
"sys".to_symbol
then
793 else if n
== once
"is_same_type".to_symbol
then
794 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
795 else if n
== once
"exit".to_symbol
then
796 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
797 else if n
== once
"calloc_array".to_symbol
then
798 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
799 else if n
== once
"calloc_string".to_symbol
then
800 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
803 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
805 if method
.signature
.return_type
!= null and s
== null then
806 s
= "NIT_NULL /*stub*/"
812 ###############################################################################
815 # Compile the node as an expression
816 # Only the visitor should call it
817 meth compile_expr
(v
: CompilerVisitor): String is abstract
819 # Prepare a call of node as a statement
820 # Only the visitor should call it
821 # It's used for local variable managment
822 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
824 # Compile the node as a statement
825 # Only the visitor should call it
826 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
829 redef class ABlockExpr
830 redef meth compile_stmt
(v
)
838 redef class AVardeclExpr
839 redef meth prepare_compile_stmt
(v
)
841 v
.cfc
.register_variable
(variable
)
844 redef meth compile_stmt
(v
)
846 var cname
= v
.cfc
.varname
(variable
)
847 if n_expr
== null then
848 v
.add_instr
("/*{cname} is variable {variable.name}*/")
850 var e
= v
.compile_expr
(n_expr
)
851 v
.add_assignment
(cname
, e
)
856 redef class AReturnExpr
857 redef meth compile_stmt
(v
)
859 if n_expr
!= null then
860 var e
= v
.compile_expr
(n_expr
)
861 v
.add_assignment
(v
.nmc
.return_value
, e
)
863 if v
.cfc
.in_closure
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
864 v
.add_instr
("goto {v.nmc.return_label};")
868 redef class ABreakExpr
869 redef meth compile_stmt
(v
)
871 if n_expr
!= null then
872 var e
= v
.compile_expr
(n_expr
)
873 v
.add_assignment
(v
.nmc
.break_value
, e
)
875 if v
.cfc
.in_closure
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
876 v
.add_instr
("goto {v.nmc.break_label};")
880 redef class AContinueExpr
881 redef meth compile_stmt
(v
)
883 if n_expr
!= null then
884 var e
= v
.compile_expr
(n_expr
)
885 v
.add_assignment
(v
.nmc
.continue_value
, e
)
887 v
.add_instr
("goto {v.nmc.continue_label};")
891 redef class AAbortExpr
892 redef meth compile_stmt
(v
)
894 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
899 redef meth compile_stmt
(v
)
901 if n_block
!= null then
902 v
.compile_stmt
(n_block
)
908 redef meth compile_stmt
(v
)
910 var e
= v
.compile_expr
(n_expr
)
911 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
913 if n_then
!= null then
915 v
.compile_stmt
(n_then
)
918 if n_else
!= null then
919 v
.add_instr
("} else \{ /*if*/")
921 v
.compile_stmt
(n_else
)
928 redef class AIfexprExpr
929 redef meth compile_expr
(v
)
931 var e
= v
.compile_expr
(n_expr
)
932 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
935 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
937 v
.add_instr
("} else \{ /*if*/")
940 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
941 v
.add_assignment
(e
, e2
)
948 redef class AControlableBlock
949 meth compile_inside_block
(v
: CompilerVisitor) is abstract
950 redef meth compile_stmt
(v
)
952 var old_break_label
= v
.nmc
.break_label
953 var old_continue_label
= v
.nmc
.continue_label
954 var id
= v
.new_number
955 v
.nmc
.break_label
= "break_{id}"
956 v
.nmc
.continue_label
= "continue_{id}"
958 compile_inside_block
(v
)
961 v
.nmc
.break_label
= old_break_label
962 v
.nmc
.continue_label
= old_continue_label
966 redef class AWhileExpr
967 redef meth compile_inside_block
(v
)
969 v
.add_instr
("while (true) \{ /*while*/")
971 var e
= v
.compile_expr
(n_expr
)
972 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
974 if n_block
!= null then
975 v
.compile_stmt
(n_block
)
977 v
.add_instr
("{v.nmc.continue_label}: while(0);")
980 v
.add_instr
("{v.nmc.break_label}: while(0);")
985 redef meth compile_inside_block
(v
)
987 v
.compile_stmt
(n_vardecl
)
991 redef class AForVardeclExpr
992 redef meth compile_stmt
(v
)
994 var e
= v
.compile_expr
(n_expr
)
995 var prop
= n_expr
.stype
.local_class
.select_method
(once
"iterator".to_symbol
)
997 printl
("No iterator")
1000 var ittype
= prop
.signature
.return_type
1002 var iter
= v
.cfc
.get_var
1003 v
.add_assignment
(iter
, prop
.compile_call
(v
, [e
]))
1004 var prop2
= ittype
.local_class
.select_method
(once
"is_ok".to_symbol
)
1005 if prop2
== null then
1009 var prop3
= ittype
.local_class
.select_method
(once
"item".to_symbol
)
1010 if prop3
== null then
1014 var prop4
= ittype
.local_class
.select_method
(once
"next".to_symbol
)
1015 if prop4
== null then
1019 v
.add_instr
("while (true) \{ /*for*/")
1021 var ok
= v
.cfc
.get_var
1022 v
.add_assignment
(ok
, prop2
.compile_call
(v
, [iter
]))
1023 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1025 var e
= prop3
.compile_call
(v
, [iter
])
1027 var cname
= v
.cfc
.register_variable
(variable
)
1028 v
.add_assignment
(cname
, e
)
1030 assert par
isa AForExpr
1031 var n_block
= par
.n_block
1032 if n_block
!= null then
1033 v
.compile_stmt
(n_block
)
1035 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1036 e
= prop4
.compile_call
(v
, [iter
])
1040 v
.add_instr
("{v.nmc.break_label}: while(0);")
1044 redef class AAssertExpr
1045 redef meth compile_stmt
(v
)
1047 var e
= v
.compile_expr
(n_expr
)
1049 if n_id
!= null then
1050 s
= " '{n_id.text}' "
1052 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1056 redef class AVarExpr
1057 redef meth compile_expr(v)
1059 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1063 redef class AVarAssignExpr
1064 redef meth compile_stmt(v)
1066 var e = v.compile_expr(n_value)
1067 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1071 redef class AVarReassignExpr
1072 redef meth compile_stmt(v)
1074 var e1 = v.cfc.varname(variable)
1075 var e2 = v.compile_expr(n_value)
1076 var e3 = assign_method.compile_call(v, [e1, e2])
1077 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1081 redef class ASelfExpr
1082 redef meth compile_expr(v)
1084 return v.cfc.varname(v.nmc.method_params[0])
1089 redef meth compile_expr(v)
1091 var e = v.ensure_var(v.compile_expr(n_expr))
1092 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1095 var e2 = v.compile_expr(n_expr2)
1096 v.add_assignment(e, e2)
1103 redef class AAndExpr
1104 redef meth compile_expr(v)
1106 var e = v.ensure_var(v.compile_expr(n_expr))
1107 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1110 var e2 = v.compile_expr(n_expr2)
1111 v.add_assignment(e, e2)
1118 redef class ANotExpr
1119 redef meth compile_expr(v)
1121 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1126 redef meth compile_expr(v)
1128 var e = v.compile_expr(n_expr)
1129 var e2 = v.compile_expr(n_expr2)
1130 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1134 redef class AIsaExpr
1135 redef meth compile_expr(v)
1137 var e = v.compile_expr(n_expr)
1138 return n_type.stype.compile_cast(v, e)
1142 redef class AAsCastExpr
1143 redef meth compile_expr(v)
1145 var e = v.compile_expr(n_expr)
1146 n_type.stype.compile_type_check(v, e, self)
1151 redef class ATrueExpr
1152 redef meth compile_expr(v)
1154 return " TAG_Bool(true)"
1158 redef class AFalseExpr
1159 redef meth compile_expr(v)
1161 return " TAG_Bool(false)"
1165 redef class AIntExpr
1166 redef meth compile_expr(v)
1168 return " TAG_Int({n_number.text})"
1172 redef class AFloatExpr
1173 redef meth compile_expr(v)
1175 return "BOX_Float({n_float.text})"
1179 redef class ACharExpr
1180 redef meth compile_expr(v)
1182 return " TAG_Char({n_char.text})"
1186 redef class AStringFormExpr
1187 redef meth compile_expr(v)
1189 var prop = stype.local_class.select_method(once "with_native
".to_symbol)
1191 return prop.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1194 # The raw string value
1195 protected meth string_text: String is abstract
1197 # The string in a C native format
1198 protected attr _cstring: String
1200 # The string length in bytes
1201 protected attr _cstring_length: Int
1203 # Compute _cstring and _cstring_length using string_text
1204 protected meth compute_string_info
1207 var str = string_text
1208 var res = new String
1210 while i < str.length do
1215 if c2 != '{' and c2 != '}' then
1225 _cstring_length = len
1229 redef class AStringExpr
1230 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1232 redef class AStartStringExpr
1233 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1235 redef class AMidStringExpr
1236 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1238 redef class AEndStringExpr
1239 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1242 redef class ASuperstringExpr
1243 redef meth compile_expr(v)
1245 var prop = stype.local_class.select_method(once "init".to_symbol)
1246 var recv = prop.compile_constructor_call(v, stype, new Array[String])
1248 var prop2 = stype.local_class.select_method(once "append
".to_symbol)
1250 var prop3 = stype.local_class.select_method(once "to_s
".to_symbol)
1251 for ne in n_exprs do
1252 var e = v.ensure_var(v.compile_expr(ne))
1253 if ne.stype != stype then
1254 v.add_assignment(e, prop3.compile_call(v, [e]))
1256 prop2.compile_call(v, [recv, e])
1263 redef class ANullExpr
1264 redef meth compile_expr(v)
1266 return " NIT_NULL /*null*/"
1270 redef class AArrayExpr
1271 redef meth compile_expr(v)
1273 var prop = stype.local_class.select_method(once "with_capacity
".to_symbol)
1274 var recv = prop.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1276 var prop2 = stype.local_class.select_method(once "add
".to_symbol)
1277 for ne in n_exprs do
1278 var e = v.compile_expr(ne)
1279 prop2.compile_call(v, [recv, e])
1285 redef class ARangeExpr
1286 redef meth compile_expr(v)
1288 var prop = stype.local_class.select_method(propname)
1289 var e = v.compile_expr(n_expr)
1290 var e2 = v.compile_expr(n_expr2)
1291 return prop.compile_constructor_call(v, stype, [e, e2])
1293 # The constructor that must be used for the range
1294 protected meth propname: Symbol is abstract
1297 redef class ACrangeExpr
1298 redef meth propname do return once "init".to_symbol
1300 redef class AOrangeExpr
1301 redef meth propname do return once "without_last
".to_symbol
1304 redef class ASuperExpr
1305 redef meth compile_stmt(v)
1307 var e = compile_expr(v)
1308 if e != null then v.add_instr("{e};")
1311 redef meth compile_expr(v)
1313 var arity = v.nmc.method_params.length - 1
1314 if init_in_superclass != null then
1315 arity = init_in_superclass.signature.arity
1317 var args = new Array[String].with_capacity(arity + 1)
1318 args.add(v.cfc.varname(v.nmc.method_params[0]))
1319 if n_args.length != arity then
1320 for i in [0..arity[ do
1321 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1325 args.add(v.compile_expr(na))
1328 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1329 if init_in_superclass != null then
1330 return init_in_superclass.compile_call(v, args)
1332 if prop.global.is_init then args.add("init_table
")
1333 return prop.compile_super_call(v, args)
1338 redef class AAttrExpr
1339 redef meth compile_expr(v)
1341 var e = v.compile_expr(n_expr)
1342 return prop.compile_access(v, e)
1346 redef class AAttrAssignExpr
1347 redef meth compile_stmt(v)
1349 var e = v.compile_expr(n_expr)
1350 var e2 = v.compile_expr(n_value)
1351 v.add_assignment(prop.compile_access(v, e), e2)
1354 redef class AAttrReassignExpr
1355 redef meth compile_stmt(v)
1357 var e1 = v.compile_expr(n_expr)
1358 var e2 = prop.compile_access(v, e1)
1359 var e3 = v.compile_expr(n_value)
1360 var e4 = assign_method.compile_call(v, [e2, e3])
1361 v.add_assignment(e2, e4)
1365 redef class ASendExpr
1366 redef meth compile_expr(v)
1368 var recv = v.compile_expr(n_expr)
1369 var cargs = new Array[String]
1371 for a in arguments do
1372 cargs.add(v.compile_expr(a))
1376 if prop_signature.closures.is_empty then
1377 e = prop.compile_call(v, cargs)
1379 var cd = closure_defs
1381 if cd != null then arity = cd.length
1382 var closcns = new Array[String]
1383 var ve: String = null
1385 # Prepare result value.
1386 # In case of procedure, the return value is still used to intercept breaks
1387 var old_bv = v.nmc.break_value
1389 v.nmc.break_value = ve
1391 # Compile closure to c function
1392 for i in [0..arity[ do
1393 var cn = cd[i].compile_closure(v, prop.closure_cname(i))
1397 for i in [arity..prop_signature.closures.length[ do
1401 v.nmc.break_value = old_bv
1404 e = prop.compile_call(v, cargs)
1406 v.add_assignment(ve, e)
1410 # Intercept returns and breaks
1411 for i in [0..arity[ do
1412 # A break or a return is intercepted
1413 v.add_instr("if ({closcns[i]}->has_broke
!= NULL) \
{")
1415 # A passtrought break or a return is intercepted: go the the next closure
1416 v.add_instr("if ({closcns[i]}->has_broke
!= &({ve})) \
{")
1418 if v.cfc.in_closure then v.add_instr("closctx-
>has_broke
= {closcns[i]}->has_broke
; closctx-
>broke_value
= {closcns[i]}->broke_value
;")
1419 v.add_instr("goto
{v.nmc.return_label};")
1421 # A direct break is interpected
1423 # overwrite the returned value in a function
1424 v.add_instr("\
} else {ve} = {closcns[i]}->broke_value
;")
1426 # Do nothing in a procedure
1434 if prop.global.is_init then
1435 v.invoke_super_init_calls_after(prop)
1440 redef meth compile_stmt(v)
1442 var e = compile_expr(v)
1444 v.add_instr(e + ";")
1449 redef class ASendReassignExpr
1450 redef meth compile_expr(v)
1452 var recv = v.compile_expr(n_expr)
1453 var cargs = new Array[String]
1455 for a in arguments do
1456 cargs.add(v.compile_expr(a))
1459 var e2 = read_prop.compile_call(v, cargs)
1460 var e3 = v.compile_expr(n_value)
1461 var e4 = assign_method.compile_call(v, [e2, e3])
1463 return prop.compile_call(v, cargs)
1467 redef class ANewExpr
1468 redef meth compile_expr(v)
1470 var cargs = new Array[String]
1471 for a in arguments do
1472 cargs.add(v.compile_expr(a))
1474 return prop.compile_constructor_call(v, stype, cargs)
1478 redef class PClosureDef
1479 # Compile the closure definition as a function in v.out_contexts
1480 # Return the cname of the function
1481 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1483 # Compile the closure definition inside the current C function.
1484 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1487 redef class AClosureDef
1488 # The cname of the function
1489 readable attr _cname: String
1491 redef meth compile_closure(v, closcn)
1494 v.ctx = new CContext
1495 v.out_contexts.add(v.ctx)
1497 var cfc_old = v.cfc.in_closure
1498 v.cfc.in_closure = true
1500 var old_rv = v.nmc.return_value
1501 var old_bv = v.nmc.break_value
1503 v.nmc.return_value = "closctx-
>{old_rv}"
1504 v.nmc.break_value = "closctx-
>{old_bv}"
1507 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1509 var args = new Array[String]
1510 for i in [0..closure.signature.arity[ do
1511 args.add(" param
{i}")
1514 var cs = decl_csignature(v, args, closcn)
1516 v.add_instr("{cs} \
{")
1518 var ctx_old2 = v.ctx
1519 v.ctx = new CContext
1521 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1522 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1524 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1525 var s = do_compile_inside(v, args)
1527 v.add_instr("{v.nmc.return_label}:")
1528 v.add_instr("tracehead
= trace
.prev
;")
1530 v.add_instr("return;")
1532 v.add_instr("return {s};")
1535 ctx_old2.append(v.ctx)
1541 v.cfc.in_closure = cfc_old
1542 v.nmc.return_value = old_rv
1543 v.nmc.break_value = old_bv
1546 var closcnv = "wbclos
{v.new_number}"
1547 v.add_decl("struct
{closcn} {closcnv} = \
{{cname}, NULL\
};")
1549 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1550 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1552 v.add_instr("{closcnv}.variable
= variable
;")
1553 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1556 return "(&{closcnv})"
1559 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1561 var params = new Array[String]
1562 params.add("struct
{closcn}* closctx
")
1563 for i in [0..closure.signature.arity[ do
1564 var p = "val_t
{args[i]}"
1568 if closure.signature.return_type != null then
1573 var p = params.join(", ")
1574 var s = "{ret} {cname}({p})"
1575 v.add_decl("struct
{closcn};")
1576 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1581 redef meth do_compile_inside(v, params)
1583 for i in [0..variables.length[ do
1584 var vacname = v.cfc.register_variable(variables[i])
1585 v.add_assignment(vacname, params[i])
1588 var old_cv = v.nmc.continue_value
1589 var old_cl = v.nmc.continue_label
1590 var old_bl = v.nmc.break_label
1592 v.nmc.continue_value = v.cfc.get_var
1593 v.nmc.continue_label = "continue_label
{v.new_number}"
1594 v.nmc.break_label = v.nmc.return_label
1596 if n_expr != null then v.compile_stmt(n_expr)
1598 v.add_instr("{v.nmc.continue_label}: while(false);")
1600 var ret: String = null
1601 if closure.signature.return_type != null then ret = v.nmc.continue_value
1603 v.nmc.continue_value = old_cv
1604 v.nmc.continue_label = old_cl
1605 v.nmc.break_label = old_bl
1611 redef class AClosureCallExpr
1612 redef meth compile_expr(v)
1614 if variable.closure.is_optional then
1615 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1617 var n = variable.decl
1618 assert n isa AClosureDecl
1619 v.compile_stmt(n.n_expr)
1621 v.add_instr("} else \
{")
1625 var cargs = new Array[String]
1626 var ivar = "(({variable.ctypename})({v.cfc.varname(variable)}))"
1628 for a in arguments do
1629 cargs.add(v.compile_expr(a))
1631 var s = "({ivar}->fun({cargs.join(", ")})) /* Invoke closure
{variable} */"
1632 var va: String = null
1633 if variable.closure.signature.return_type != null then
1635 v.add_assignment(va, s)
1639 v.add_instr("if ({ivar}->has_broke
) \
{")
1641 if n_closure_defs != null and n_closure_defs.length == 1 then do
1642 n_closure_defs.first.do_compile_inside(v, null)
1644 if v.cfc.in_closure then v.add_instr("if ({ivar}->has_broke
) \
{ closctx->has_broke = {ivar}->has_broke
; closctx-
>broke_value
= {ivar}->broke_value
;\
}")
1645 v.add_instr("goto
{v.nmc.return_label};")
1649 if variable.closure.is_optional then
1657 redef class AProxyExpr
1658 redef meth compile_expr(v)
1660 return v.compile_expr(n_expr)
1664 redef class AOnceExpr
1665 redef meth compile_expr(v)
1667 var i = v.new_number
1668 var cvar = v.cfc.get_var
1669 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1670 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1671 v.add_instr("else \
{")
1673 v.cfc.free_var(cvar)
1674 var e = v.compile_expr(n_expr)
1675 v.add_assignment(cvar, e)
1676 v.add_instr("once_value_
{i} = {cvar};")
1677 v.add_instr("once_bool_
{i} = true;")