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
= new Buffer.from
("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
= "{global.meth_call}({cargs[0]})"
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
= "{super_meth_call}({cargs[0]})"
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 PSignature
534 meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String]) is abstract
537 redef class ASignature
538 redef meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String])
540 for ap
in n_params
do
541 var cname
= v
.cfc
.register_variable
(ap
.variable
)
542 v
.nmc
.method_params
.add
(ap
.variable
)
543 var orig_type
= orig_sig
[ap
.position
]
544 if not orig_type
< ap
.variable
.stype
then
545 # FIXME: do not test always
546 # FIXME: handle formal types
547 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
548 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
], ap
)
550 v
.add_assignment
(cname
, params
[ap
.position
])
552 for i
in [0..n_closure_decls
.length
[ do
553 var wd
= n_closure_decls
[i
]
554 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
555 wd
.variable
.ctypename
= "struct {v.nmc.method.closure_cname(i)} *"
556 v
.add_assignment
(cname
, "{params[orig_sig.arity + i]}")
561 redef class AConcreteMethPropdef
562 redef meth do_compile_inside
(v
, method
, params
)
565 v
.nmc
= new NitMethodContext(method
)
567 var selfcname
= v
.cfc
.register_variable
(self_var
)
568 v
.add_assignment
(selfcname
, params
[0])
570 v
.nmc
.method_params
= [self_var
]
572 if n_signature
!= null then
573 var orig_meth
: MMLocalProperty = method
.global
.intro
574 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
575 n_signature
.compile_parameters
(v
, orig_sig
, params
)
578 var itpos
: String = null
579 if self isa AConcreteInitPropdef then
580 itpos
= "VAL2OBJ({selfcname})->vft[{method.local_class.global.init_table_pos_id}].i"
581 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
582 v
.add_instr
("if (init_table[{itpos}]) return;")
585 v
.nmc
.return_label
= "return_label{v.new_number}"
586 v
.nmc
.return_value
= v
.cfc
.get_var
587 if self isa AConcreteInitPropdef then
588 v
.invoke_super_init_calls_after
(null)
590 if n_block
!= null then
591 v
.compile_stmt
(n_block
)
593 v
.add_instr
("{v.nmc.return_label}: while(false);")
594 if self isa AConcreteInitPropdef then
595 v
.add_instr
("init_table[{itpos}] = 1;")
598 var ret
: String = null
599 if method
.signature
.return_type
!= null then
600 ret
= v
.nmc
.return_value
608 redef class ADeferredMethPropdef
609 redef meth do_compile_inside
(v
, method
, params
)
611 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
612 v
.add_instr
(v
.printf_locate_error
(self))
613 v
.add_instr
("nit_exit(1);")
614 if method
.signature
.return_type
!= null then
622 redef class AExternMethPropdef
623 redef meth do_compile_inside
(v
, method
, params
)
625 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
626 if n_extern
!= null then
627 ename
= n_extern
.text
628 ename
= ename
.substring
(1, ename
.length-2
)
630 var sig
= method
.signature
631 if params
.length
!= sig
.arity
+ 1 then
632 printl
("par:{params.length} sig:{sig.arity}")
634 var args
= new Array[String]
635 args
.add
(sig
.recv
.unboxtype
(params
[0]))
636 for i
in [0..sig
.arity
[ do
637 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
639 var s
= "{ename}({args.join(", ")})"
640 if sig
.return_type
!= null then
641 return sig
.return_type
.boxtype
(s
)
649 redef class AInternMethPropdef
650 redef meth do_compile_inside
(v
, method
, p
)
652 var c
= method
.local_class
.name
655 if c
== once
"Int".to_symbol
then
656 if n
== once
"object_id".to_symbol
then
658 else if n
== once
"unary -".to_symbol
then
659 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
660 else if n
== once
"output".to_symbol
then
661 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
662 else if n
== once
"ascii".to_symbol
then
663 s
= "TAG_Char(UNTAG_Int({p[0]}))"
664 else if n
== once
"succ".to_symbol
then
665 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
666 else if n
== once
"prec".to_symbol
then
667 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
668 else if n
== once
"to_f".to_symbol
then
669 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
670 else if n
== once
"+".to_symbol
then
671 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
672 else if n
== once
"-".to_symbol
then
673 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
674 else if n
== once
"*".to_symbol
then
675 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
676 else if n
== once
"/".to_symbol
then
677 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
678 else if n
== once
"%".to_symbol
then
679 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
680 else if n
== once
"<".to_symbol
then
681 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
682 else if n
== once
">".to_symbol
then
683 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
684 else if n
== once
"<=".to_symbol
then
685 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
686 else if n
== once
">=".to_symbol
then
687 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
688 else if n
== once
"lshift".to_symbol
then
689 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
690 else if n
== once
"rshift".to_symbol
then
691 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
692 else if n
== once
"==".to_symbol
then
693 s
= "TAG_Bool(({p[0]})==({p[1]}))"
694 else if n
== once
"!=".to_symbol
then
695 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
697 else if c
== once
"Float".to_symbol
then
698 if n
== once
"object_id".to_symbol
then
699 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
700 else if n
== once
"unary -".to_symbol
then
701 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
702 else if n
== once
"output".to_symbol
then
703 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
704 else if n
== once
"to_i".to_symbol
then
705 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
706 else if n
== once
"+".to_symbol
then
707 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
708 else if n
== once
"-".to_symbol
then
709 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
710 else if n
== once
"*".to_symbol
then
711 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
712 else if n
== once
"/".to_symbol
then
713 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
714 else if n
== once
"<".to_symbol
then
715 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
716 else if n
== once
">".to_symbol
then
717 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
718 else if n
== once
"<=".to_symbol
then
719 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
720 else if n
== once
">=".to_symbol
then
721 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
723 else if c
== once
"Char".to_symbol
then
724 if n
== once
"object_id".to_symbol
then
725 s
= "TAG_Int(UNTAG_Char({p[0]}))"
726 else if n
== once
"unary -".to_symbol
then
727 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
728 else if n
== once
"output".to_symbol
then
729 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
730 else if n
== once
"ascii".to_symbol
then
731 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
732 else if n
== once
"succ".to_symbol
then
733 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
734 else if n
== once
"prec".to_symbol
then
735 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
736 else if n
== once
"to_i".to_symbol
then
737 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
738 else if n
== once
"+".to_symbol
then
739 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
740 else if n
== once
"-".to_symbol
then
741 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
742 else if n
== once
"*".to_symbol
then
743 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
744 else if n
== once
"/".to_symbol
then
745 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
746 else if n
== once
"%".to_symbol
then
747 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
748 else if n
== once
"<".to_symbol
then
749 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
750 else if n
== once
">".to_symbol
then
751 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
752 else if n
== once
"<=".to_symbol
then
753 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
754 else if n
== once
">=".to_symbol
then
755 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
756 else if n
== once
"==".to_symbol
then
757 s
= "TAG_Bool(({p[0]})==({p[1]}))"
758 else if n
== once
"!=".to_symbol
then
759 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
761 else if c
== once
"Bool".to_symbol
then
762 if n
== once
"object_id".to_symbol
then
763 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
764 else if n
== once
"unary -".to_symbol
then
765 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
766 else if n
== once
"output".to_symbol
then
767 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
768 else if n
== once
"ascii".to_symbol
then
769 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
770 else if n
== once
"to_i".to_symbol
then
771 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
772 else if n
== once
"==".to_symbol
then
773 s
= "TAG_Bool(({p[0]})==({p[1]}))"
774 else if n
== once
"!=".to_symbol
then
775 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
777 else if c
== once
"NativeArray".to_symbol
then
778 if n
== once
"object_id".to_symbol
then
779 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
780 else if n
== once
"[]".to_symbol
then
781 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
782 else if n
== once
"[]=".to_symbol
then
783 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
784 else if n
== once
"copy_to".to_symbol
then
785 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
787 else if c
== once
"NativeString".to_symbol
then
788 if n
== once
"object_id".to_symbol
then
789 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
790 else if n
== once
"atoi".to_symbol
then
791 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
792 else if n
== once
"[]".to_symbol
then
793 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
794 else if n
== once
"[]=".to_symbol
then
795 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
796 else if n
== once
"copy_to".to_symbol
then
797 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]}));")
799 else if n
== once
"object_id".to_symbol
then
800 s
= "TAG_Int((bigint){p[0]})"
801 else if n
== once
"sys".to_symbol
then
803 else if n
== once
"is_same_type".to_symbol
then
804 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
805 else if n
== once
"exit".to_symbol
then
806 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
807 else if n
== once
"calloc_array".to_symbol
then
808 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
809 else if n
== once
"calloc_string".to_symbol
then
810 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
813 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
815 if method
.signature
.return_type
!= null and s
== null then
816 s
= "NIT_NULL /*stub*/"
822 ###############################################################################
825 # Compile the node as an expression
826 # Only the visitor should call it
827 meth compile_expr
(v
: CompilerVisitor): String is abstract
829 # Prepare a call of node as a statement
830 # Only the visitor should call it
831 # It's used for local variable managment
832 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
834 # Compile the node as a statement
835 # Only the visitor should call it
836 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
839 redef class ABlockExpr
840 redef meth compile_stmt
(v
)
848 redef class AVardeclExpr
849 redef meth prepare_compile_stmt
(v
)
851 v
.cfc
.register_variable
(variable
)
854 redef meth compile_stmt
(v
)
856 var cname
= v
.cfc
.varname
(variable
)
857 if n_expr
== null then
858 v
.add_instr
("/*{cname} is variable {variable.name}*/")
860 var e
= v
.compile_expr
(n_expr
)
861 v
.add_assignment
(cname
, e
)
866 redef class AReturnExpr
867 redef meth compile_stmt
(v
)
869 if n_expr
!= null then
870 var e
= v
.compile_expr
(n_expr
)
871 v
.add_assignment
(v
.nmc
.return_value
, e
)
873 if v
.cfc
.in_closure
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
874 v
.add_instr
("goto {v.nmc.return_label};")
878 redef class ABreakExpr
879 redef meth compile_stmt
(v
)
881 if n_expr
!= null then
882 var e
= v
.compile_expr
(n_expr
)
883 v
.add_assignment
(v
.nmc
.break_value
, e
)
885 if v
.cfc
.in_closure
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
886 v
.add_instr
("goto {v.nmc.break_label};")
890 redef class AContinueExpr
891 redef meth compile_stmt
(v
)
893 if n_expr
!= null then
894 var e
= v
.compile_expr
(n_expr
)
895 v
.add_assignment
(v
.nmc
.continue_value
, e
)
897 v
.add_instr
("goto {v.nmc.continue_label};")
901 redef class AAbortExpr
902 redef meth compile_stmt
(v
)
904 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
909 redef meth compile_stmt
(v
)
911 if n_block
!= null then
912 v
.compile_stmt
(n_block
)
918 redef meth compile_stmt
(v
)
920 var e
= v
.compile_expr
(n_expr
)
921 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
923 if n_then
!= null then
925 v
.compile_stmt
(n_then
)
928 if n_else
!= null then
929 v
.add_instr
("} else \{ /*if*/")
931 v
.compile_stmt
(n_else
)
938 redef class AIfexprExpr
939 redef meth compile_expr
(v
)
941 var e
= v
.compile_expr
(n_expr
)
942 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
945 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
947 v
.add_instr
("} else \{ /*if*/")
950 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
951 v
.add_assignment
(e
, e2
)
958 redef class AControlableBlock
959 meth compile_inside_block
(v
: CompilerVisitor) is abstract
960 redef meth compile_stmt
(v
)
962 var old_break_label
= v
.nmc
.break_label
963 var old_continue_label
= v
.nmc
.continue_label
964 var id
= v
.new_number
965 v
.nmc
.break_label
= "break_{id}"
966 v
.nmc
.continue_label
= "continue_{id}"
968 compile_inside_block
(v
)
971 v
.nmc
.break_label
= old_break_label
972 v
.nmc
.continue_label
= old_continue_label
976 redef class AWhileExpr
977 redef meth compile_inside_block
(v
)
979 v
.add_instr
("while (true) \{ /*while*/")
981 var e
= v
.compile_expr
(n_expr
)
982 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
984 if n_block
!= null then
985 v
.compile_stmt
(n_block
)
987 v
.add_instr
("{v.nmc.continue_label}: while(0);")
990 v
.add_instr
("{v.nmc.break_label}: while(0);")
995 redef meth compile_inside_block
(v
)
997 v
.compile_stmt
(n_vardecl
)
1001 redef class AForVardeclExpr
1002 redef meth compile_stmt
(v
)
1004 var e
= v
.compile_expr
(n_expr
)
1005 var ittype
= meth_iterator
.signature
.return_type
1007 var iter
= v
.cfc
.get_var
1008 v
.add_assignment
(iter
, meth_iterator
.compile_call
(v
, [e
]))
1009 v
.add_instr
("while (true) \{ /*for*/")
1011 var ok
= v
.cfc
.get_var
1012 v
.add_assignment
(ok
, meth_is_ok
.compile_call
(v
, [iter
]))
1013 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1015 var e
= meth_item
.compile_call
(v
, [iter
])
1017 var cname
= v
.cfc
.register_variable
(variable
)
1018 v
.add_assignment
(cname
, e
)
1020 assert par
isa AForExpr
1021 var n_block
= par
.n_block
1022 if n_block
!= null then
1023 v
.compile_stmt
(n_block
)
1025 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1026 e
= meth_next
.compile_call
(v
, [iter
])
1030 v
.add_instr
("{v.nmc.break_label}: while(0);")
1034 redef class AAssertExpr
1035 redef meth compile_stmt
(v
)
1037 var e
= v
.compile_expr
(n_expr
)
1039 if n_id
!= null then
1040 s
= " '{n_id.text}' "
1042 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1046 redef class AVarExpr
1047 redef meth compile_expr(v)
1049 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1053 redef class AVarAssignExpr
1054 redef meth compile_stmt(v)
1056 var e = v.compile_expr(n_value)
1057 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1061 redef class AVarReassignExpr
1062 redef meth compile_stmt(v)
1064 var e1 = v.cfc.varname(variable)
1065 var e2 = v.compile_expr(n_value)
1066 var e3 = assign_method.compile_call(v, [e1, e2])
1067 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1071 redef class ASelfExpr
1072 redef meth compile_expr(v)
1074 return v.cfc.varname(v.nmc.method_params[0])
1079 redef meth compile_expr(v)
1081 var e = v.ensure_var(v.compile_expr(n_expr))
1082 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1085 var e2 = v.compile_expr(n_expr2)
1086 v.add_assignment(e, e2)
1093 redef class AAndExpr
1094 redef meth compile_expr(v)
1096 var e = v.ensure_var(v.compile_expr(n_expr))
1097 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1100 var e2 = v.compile_expr(n_expr2)
1101 v.add_assignment(e, e2)
1108 redef class ANotExpr
1109 redef meth compile_expr(v)
1111 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1116 redef meth compile_expr(v)
1118 var e = v.compile_expr(n_expr)
1119 var e2 = v.compile_expr(n_expr2)
1120 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1124 redef class AIsaExpr
1125 redef meth compile_expr(v)
1127 var e = v.compile_expr(n_expr)
1128 return n_type.stype.compile_cast(v, e)
1132 redef class AAsCastExpr
1133 redef meth compile_expr(v)
1135 var e = v.compile_expr(n_expr)
1136 n_type.stype.compile_type_check(v, e, self)
1141 redef class ATrueExpr
1142 redef meth compile_expr(v)
1144 return " TAG_Bool(true)"
1148 redef class AFalseExpr
1149 redef meth compile_expr(v)
1151 return " TAG_Bool(false)"
1155 redef class AIntExpr
1156 redef meth compile_expr(v)
1158 return " TAG_Int({n_number.text})"
1162 redef class AFloatExpr
1163 redef meth compile_expr(v)
1165 return "BOX_Float({n_float.text})"
1169 redef class ACharExpr
1170 redef meth compile_expr(v)
1172 return " TAG_Char({n_char.text})"
1176 redef class AStringFormExpr
1177 redef meth compile_expr(v)
1180 return meth_with_native.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1183 # The raw string value
1184 protected meth string_text: String is abstract
1186 # The string in a C native format
1187 protected attr _cstring: String
1189 # The string length in bytes
1190 protected attr _cstring_length: Int
1192 # Compute _cstring and _cstring_length using string_text
1193 protected meth compute_string_info
1196 var str = string_text
1197 var res = new Buffer
1199 while i < str.length do
1204 if c2 != '{' and c2 != '}' then
1214 _cstring_length = len
1218 redef class AStringExpr
1219 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1221 redef class AStartStringExpr
1222 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1224 redef class AMidStringExpr
1225 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1227 redef class AEndStringExpr
1228 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1231 redef class ASuperstringExpr
1232 redef meth compile_expr(v)
1234 var array = meth_with_capacity.compile_constructor_call(v, atype, ["TAG_Int({n_exprs.length})"])
1236 for ne in n_exprs do
1237 var e = v.ensure_var(v.compile_expr(ne))
1238 if ne.stype != stype then
1239 v.add_assignment(e, meth_to_s.compile_call(v, [e]))
1241 meth_add.compile_call(v, [array, e])
1244 return meth_to_s.compile_call(v, [array])
1248 redef class ANullExpr
1249 redef meth compile_expr(v)
1251 return " NIT_NULL /*null*/"
1255 redef class AArrayExpr
1256 redef meth compile_expr(v)
1258 var recv = meth_with_capacity.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1260 for ne in n_exprs do
1261 var e = v.compile_expr(ne)
1262 meth_add.compile_call(v, [recv, e])
1268 redef class ARangeExpr
1269 redef meth compile_expr(v)
1271 var e = v.compile_expr(n_expr)
1272 var e2 = v.compile_expr(n_expr2)
1273 return meth_init.compile_constructor_call(v, stype, [e, e2])
1277 redef class ASuperExpr
1278 redef meth compile_stmt(v)
1280 var e = compile_expr(v)
1281 if e != null then v.add_instr("{e};")
1284 redef meth compile_expr(v)
1286 var arity = v.nmc.method_params.length - 1
1287 if init_in_superclass != null then
1288 arity = init_in_superclass.signature.arity
1290 var args = new Array[String].with_capacity(arity + 1)
1291 args.add(v.cfc.varname(v.nmc.method_params[0]))
1292 if n_args.length != arity then
1293 for i in [0..arity[ do
1294 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1298 args.add(v.compile_expr(na))
1301 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1302 if init_in_superclass != null then
1303 return init_in_superclass.compile_call(v, args)
1305 if prop.global.is_init then args.add("init_table
")
1306 return prop.compile_super_call(v, args)
1311 redef class AAttrExpr
1312 redef meth compile_expr(v)
1314 var e = v.compile_expr(n_expr)
1315 return prop.compile_access(v, e)
1319 redef class AAttrAssignExpr
1320 redef meth compile_stmt(v)
1322 var e = v.compile_expr(n_expr)
1323 var e2 = v.compile_expr(n_value)
1324 v.add_assignment(prop.compile_access(v, e), e2)
1327 redef class AAttrReassignExpr
1328 redef meth compile_stmt(v)
1330 var e1 = v.compile_expr(n_expr)
1331 var e2 = prop.compile_access(v, e1)
1332 var e3 = v.compile_expr(n_value)
1333 var e4 = assign_method.compile_call(v, [e2, e3])
1334 v.add_assignment(e2, e4)
1338 redef class ASendExpr
1339 redef meth compile_expr(v)
1341 var recv = v.compile_expr(n_expr)
1342 var cargs = new Array[String]
1344 for a in arguments do
1345 cargs.add(v.compile_expr(a))
1349 if prop_signature.closures.is_empty then
1350 e = prop.compile_call(v, cargs)
1352 var cd = closure_defs
1354 if cd != null then arity = cd.length
1355 var closcns = new Array[String]
1356 var ve: String = null
1358 # Prepare result value.
1359 # In case of procedure, the return value is still used to intercept breaks
1360 var old_bv = v.nmc.break_value
1362 v.nmc.break_value = ve
1364 # Compile closure to c function
1365 for i in [0..arity[ do
1366 var cn = cd[i].compile_closure(v, prop.closure_cname(i))
1370 for i in [arity..prop_signature.closures.length[ do
1374 v.nmc.break_value = old_bv
1377 e = prop.compile_call(v, cargs)
1379 v.add_assignment(ve, e)
1383 # Intercept returns and breaks
1384 for i in [0..arity[ do
1385 # A break or a return is intercepted
1386 v.add_instr("if ({closcns[i]}->has_broke
!= NULL) \
{")
1388 # A passtrought break or a return is intercepted: go the the next closure
1389 v.add_instr("if ({closcns[i]}->has_broke
!= &({ve})) \
{")
1391 if v.cfc.in_closure then v.add_instr("closctx-
>has_broke
= {closcns[i]}->has_broke
; closctx-
>broke_value
= {closcns[i]}->broke_value
;")
1392 v.add_instr("goto
{v.nmc.return_label};")
1394 # A direct break is interpected
1396 # overwrite the returned value in a function
1397 v.add_instr("\
} else {ve} = {closcns[i]}->broke_value
;")
1399 # Do nothing in a procedure
1407 if prop.global.is_init then
1408 v.invoke_super_init_calls_after(prop)
1413 redef meth compile_stmt(v)
1415 var e = compile_expr(v)
1417 v.add_instr(e + ";")
1422 redef class ASendReassignExpr
1423 redef meth compile_expr(v)
1425 var recv = v.compile_expr(n_expr)
1426 var cargs = new Array[String]
1428 for a in arguments do
1429 cargs.add(v.compile_expr(a))
1432 var e2 = read_prop.compile_call(v, cargs)
1433 var e3 = v.compile_expr(n_value)
1434 var e4 = assign_method.compile_call(v, [e2, e3])
1436 return prop.compile_call(v, cargs)
1440 redef class ANewExpr
1441 redef meth compile_expr(v)
1443 var cargs = new Array[String]
1444 for a in arguments do
1445 cargs.add(v.compile_expr(a))
1447 return prop.compile_constructor_call(v, stype, cargs)
1451 redef class PClosureDef
1452 # Compile the closure definition as a function in v.out_contexts
1453 # Return the cname of the function
1454 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1456 # Compile the closure definition inside the current C function.
1457 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1460 redef class AClosureDef
1461 # The cname of the function
1462 readable attr _cname: String
1464 redef meth compile_closure(v, closcn)
1467 v.ctx = new CContext
1468 v.out_contexts.add(v.ctx)
1470 var cfc_old = v.cfc.in_closure
1471 v.cfc.in_closure = true
1473 var old_rv = v.nmc.return_value
1474 var old_bv = v.nmc.break_value
1476 v.nmc.return_value = "closctx-
>{old_rv}"
1477 v.nmc.break_value = "closctx-
>{old_bv}"
1480 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1482 var args = new Array[String]
1483 for i in [0..closure.signature.arity[ do
1484 args.add(" param
{i}")
1487 var cs = decl_csignature(v, args, closcn)
1489 v.add_instr("{cs} \
{")
1491 var ctx_old2 = v.ctx
1492 v.ctx = new CContext
1494 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1495 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1497 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1498 var s = do_compile_inside(v, args)
1500 v.add_instr("{v.nmc.return_label}:")
1501 v.add_instr("tracehead
= trace
.prev
;")
1503 v.add_instr("return;")
1505 v.add_instr("return {s};")
1508 ctx_old2.append(v.ctx)
1514 v.cfc.in_closure = cfc_old
1515 v.nmc.return_value = old_rv
1516 v.nmc.break_value = old_bv
1519 var closcnv = "wbclos
{v.new_number}"
1520 v.add_decl("struct
{closcn} {closcnv} = \
{{cname}, NULL\
};")
1522 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1523 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1525 v.add_instr("{closcnv}.variable
= variable
;")
1526 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1529 return "(&{closcnv})"
1532 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1534 var params = new Array[String]
1535 params.add("struct
{closcn}* closctx
")
1536 for i in [0..closure.signature.arity[ do
1537 var p = "val_t
{args[i]}"
1541 if closure.signature.return_type != null then
1546 var p = params.join(", ")
1547 var s = "{ret} {cname}({p})"
1548 v.add_decl("struct
{closcn};")
1549 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1554 redef meth do_compile_inside(v, params)
1556 for i in [0..variables.length[ do
1557 var vacname = v.cfc.register_variable(variables[i])
1558 v.add_assignment(vacname, params[i])
1561 var old_cv = v.nmc.continue_value
1562 var old_cl = v.nmc.continue_label
1563 var old_bl = v.nmc.break_label
1565 v.nmc.continue_value = v.cfc.get_var
1566 v.nmc.continue_label = "continue_label
{v.new_number}"
1567 v.nmc.break_label = v.nmc.return_label
1569 if n_expr != null then v.compile_stmt(n_expr)
1571 v.add_instr("{v.nmc.continue_label}: while(false);")
1573 var ret: String = null
1574 if closure.signature.return_type != null then ret = v.nmc.continue_value
1576 v.nmc.continue_value = old_cv
1577 v.nmc.continue_label = old_cl
1578 v.nmc.break_label = old_bl
1584 redef class PClosureDecl
1585 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1587 redef class AClosureDecl
1588 redef meth do_compile_inside(v, params)
1590 if n_signature != null then n_signature.compile_parameters(v, variable.closure.signature, params)
1592 var old_cv = v.nmc.continue_value
1593 var old_cl = v.nmc.continue_label
1594 var old_bl = v.nmc.break_label
1596 v.nmc.continue_value = v.cfc.get_var
1597 v.nmc.continue_label = "continue_label
{v.new_number}"
1598 v.nmc.break_label = v.nmc.return_label
1600 if n_expr != null then v.compile_stmt(n_expr)
1602 v.add_instr("{v.nmc.continue_label}: while(false);")
1604 var ret: String = null
1605 if variable.closure.signature.return_type != null then ret = v.nmc.continue_value
1607 v.nmc.continue_value = old_cv
1608 v.nmc.continue_label = old_cl
1609 v.nmc.break_label = old_bl
1615 redef class AClosureCallExpr
1616 redef meth compile_expr(v)
1618 var cargs = new Array[String]
1619 for a in arguments do cargs.add(v.compile_expr(a))
1620 var va: String = null
1621 if variable.closure.signature.return_type != null then va = v.cfc.get_var
1623 if variable.closure.is_optional then
1624 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1626 var n = variable.decl
1627 assert n isa AClosureDecl
1628 var s = n.do_compile_inside(v, cargs)
1629 if s != null then v.add_assignment(va, s)
1631 v.add_instr("} else \
{")
1635 var ivar = "(({variable.ctypename})({v.cfc.varname(variable)}))"
1637 cargs2.append(cargs)
1638 var s = "({ivar}->fun({cargs2.join(", ")})) /* Invoke closure
{variable} */"
1640 v.add_assignment(va, s)
1644 v.add_instr("if ({ivar}->has_broke
) \
{")
1646 if n_closure_defs != null and n_closure_defs.length == 1 then do
1647 n_closure_defs.first.do_compile_inside(v, null)
1649 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
;\
}")
1650 v.add_instr("goto
{v.nmc.return_label};")
1654 if variable.closure.is_optional then
1662 redef class AProxyExpr
1663 redef meth compile_expr(v)
1665 return v.compile_expr(n_expr)
1669 redef class AOnceExpr
1670 redef meth compile_expr(v)
1672 var i = v.new_number
1673 var cvar = v.cfc.get_var
1674 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1675 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1676 v.add_instr("else \
{")
1678 v.cfc.free_var(cvar)
1679 var e = v.compile_expr(n_expr)
1680 v.add_assignment(cvar, e)
1681 v.add_instr("once_value_
{i} = {cvar};")
1682 v.add_instr("once_bool_
{i} = true;")