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 add_instr
("/* Compile stmt {n.locate} */")
28 n
.prepare_compile_stmt
(self)
29 var i
= cfc
._variable_index
31 cfc
._variable_index
= i
34 # Compile is expression node
35 meth compile_expr
(n
: PExpr): String
37 add_instr
("/* Compile expr {n.locate} */")
38 var i
= cfc
._variable_index
39 var s
= n
.compile_expr
(self)
40 cfc
._variable_index
= i
49 # Ensure that a c expression is a var
50 meth ensure_var
(s
: String): String
52 if s
.substring
(0,3) == "variable" then
60 # Add a assignment between a variable and an expression
61 meth add_assignment
(v
: String, s
: String)
64 add_instr
("{v} = {s};")
68 readable writable attr _cfc
: CFunctionContext
70 readable writable attr _nmc
: NitMethodContext
72 # C outputs written outside the current C function.
73 readable writable attr _out_contexts
: Array[CContext] = new Array[CContext]
75 # Generate an fprintf to display an error location
76 meth printf_locate_error
(node
: PNode): String
78 var s
= new Buffer.from
("fprintf(stderr, \"")
79 if nmc != null then s.append(" in %s
")
80 s.append(" (%s
:%d
)\\n\
", ")
81 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
82 s
.append
("LOCATE_{module.name}, {node.line_number});")
86 redef init(module: MMSrcModule)
91 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
93 var n
= nmc
.method
.node
94 assert n
isa AConcreteInitPropdef
96 if n
.super_init_calls
.is_empty
then return
100 if start_prop
!= null then
101 while n
.super_init_calls
[i
] != start_prop
do
102 #s.append(" {n.super_init_calls[i]}")
106 #s.append(" {start_prop}")
108 while n
.explicit_super_init_calls
[j
] != start_prop
do
113 var stop_prop
: MMMethod = null
114 if j
< n
.explicit_super_init_calls
.length
then
115 stop_prop
= n
.explicit_super_init_calls
[j
]
117 var l
= n
.super_init_calls
.length
120 var p
= n
.super_init_calls
[i
]
121 if p
== stop_prop
then break
122 var cargs
= new Array[String]
123 if p
.signature
.arity
== 0 then
124 cargs
.add
(cfc
.varname
(nmc
.method_params
[0]))
126 for va
in nmc
.method_params
do
127 cargs
.add
(cfc
.varname
(va
))
131 p
.compile_call
(self, cargs
)
136 # s.append(" {n.super_init_calls[i]}")
139 #if stop_prop != null then s.append(" (stop at {stop_prop})")
140 #n.printl("implicit calls in {n.method}: {s}")
144 # A C function currently written
145 class CFunctionContext
146 readable attr _visitor
: CompilerVisitor
148 # Next available variable number
149 attr _variable_index
: Int = 0
151 # Total number of variable
152 attr _variable_index_max
: Int = 0
154 # Association between nit variable and the corrsponding c variable
155 attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
157 # Are we currenlty in a closure definition?
158 readable writable attr _closure
: NitMethodContext = null
160 meth varname
(v
: Variable): String
162 if _closure
!= null then
163 return "closctx->{_varnames[v]}"
169 # Return the next available variable
172 var v
= variable
(_variable_index
)
173 _variable_index
= _variable_index
+ 1
174 if _variable_index
> _variable_index_max
then
175 #visitor.add_decl("val_t {v};")
176 _variable_index_max
= _variable_index
181 meth register_variable
(v
: Variable): String
184 _varnames
[v
] = "variable[{_variable_index-1}]"
188 # Next available closure variable number
189 attr _closurevariable_index
: Int = 0
191 meth register_closurevariable
(v
: ClosureVariable): String
193 var s
= "closurevariable[{_closurevariable_index}]"
194 _closurevariable_index
+= 1
196 if _closure
!= null then
197 return "(closctx->{s})"
203 # Return the ith variable
204 protected meth variable
(i
: Int): String
206 if _closure
!= null then
207 return "(closctx->variable[{i}])"
209 return "variable[{i}]"
213 # Mark the variable available
214 meth free_var
(v
: String)
217 if v
== variable
(_variable_index-1
) then
218 _variable_index
= _variable_index
- 1
222 # Generate the local variable declarations
223 # To use at the end of the C function once all variables are known
224 meth generate_var_decls
226 if _variable_index_max
> 0 then
227 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
229 visitor
.add_decl
("val_t *variable = NULL;")
231 if _closurevariable_index
> 0 then
232 visitor
.add_decl
("struct WBT_ *closurevariable[{_closurevariable_index}];")
234 visitor
.add_decl
("struct WBT_ **closurevariable = NULL;")
238 init(v
: CompilerVisitor) do _visitor
= v
241 # A Nit method currenlty compiled
242 class NitMethodContext
243 # Current method compiled
244 readable attr _method
: MMSrcMethod
246 # Association between parameters and the corresponding variables
247 readable writable attr _method_params
: Array[ParamVariable]
249 # Where a nit return must branch
250 readable writable attr _return_label
: String
252 # Where a nit break must branch
253 readable writable attr _break_label
: String
255 # Where a nit continue must branch
256 readable writable attr _continue_label
: String
258 # Variable where a functionnal nit return must store its value
259 readable writable attr _return_value
: String
261 # Variable where a functionnal nit break must store its value
262 readable writable attr _break_value
: String
264 # Variable where a functionnal nit continue must store its value
265 readable writable attr _continue_value
: String
267 init(method
: MMSrcMethod)
273 ###############################################################################
275 redef class ClosureVariable
276 readable writable attr _ctypename
: String
280 # Compile a call on self for given arguments
281 # Most calls are compiled with a table access,
282 # primitive calles are inlined
283 # == and != are guarded and possibly inlined
284 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
287 if i
isa MMSrcMethod then
288 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
289 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
291 var e
= i
.do_compile_inside
(v
, cargs
)
295 var ee
= once
"==".to_symbol
296 var ne
= once
"!=".to_symbol
298 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
299 var eqcall
= eqp
.compile_call
(v
, cargs
)
300 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
302 if global
.is_init
then
304 cargs
.add
("init_table /*YYY*/")
307 var m
= "{global.meth_call}({cargs[0]})"
308 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
310 vcall
= "UNTAG_Bool({vcall})"
311 var obj
= once
"Object".to_symbol
312 if i
.local_class
.name
== obj
then
313 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
315 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
317 if signature
.return_type
!= null then
320 v
.add_instr
(vcall
+ ";")
325 # Compile a call on self for given arguments and given closures
326 meth compile_call_and_closures
(v
: CompilerVisitor, cargs
: Array[String], clos_defs
: Array[PClosureDef]): String
328 var ve
: String = null
330 if clos_defs
!= null then arity
= clos_defs
.length
332 # Prepare result value.
333 # In case of procedure, the return value is still used to intercept breaks
334 var old_bv
= v
.nmc
.break_value
336 v
.nmc
.break_value
= ve
338 # Compile closure to c function
339 var realcargs
= new Array[String] # Args to pass to the C function call
340 var closcns
= new Array[String] # Closure C structure names
341 realcargs
.add_all
(cargs
)
342 for i
in [0..arity
[ do
343 var cn
= clos_defs
[i
].compile_closure
(v
, closure_cname
(i
))
347 for i
in [arity
..signature
.closures
.length
[ do
348 realcargs
.add
("NULL")
351 v
.nmc
.break_value
= old_bv
354 var e
= compile_call
(v
, realcargs
)
356 v
.add_assignment
(ve
, e
)
360 # Intercept returns and breaks
361 for i
in [0..arity
[ do
362 # A break or a return is intercepted
363 v
.add_instr
("if ({closcns[i]}->has_broke != NULL) \{")
365 # A passtrought break or a return is intercepted: go the the next closure
366 v
.add_instr
("if ({closcns[i]}->has_broke != &({ve})) \{")
368 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = {closcns[i]}->has_broke; closctx->broke_value = {closcns[i]}->broke_value;")
369 v
.add_instr
("goto {v.nmc.return_label};")
371 # A direct break is interpected
373 # overwrite the returned value in a function
374 v
.add_instr
("\} else {ve} = {closcns[i]}->broke_value;")
376 # Do nothing in a procedure
385 # Compile a call as constructor with given args
386 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
388 var recv
= v
.cfc
.get_var
389 v
.add_instr
("{recv} = NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}); /*new {recvtype}*/")
393 # Compile a call as call-next-method on self with given args
394 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
396 var m
= "{super_meth_call}({cargs[0]})"
397 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
401 # Cname of the i-th closure C struct type
402 protected meth closure_cname
(i
: Int): String
404 return "FWBT_{cname}_{i}"
408 redef class MMAttribute
409 # Compile an acces on selffor a given reciever.
410 # Result is a valid C left-value for assigment
411 meth compile_access
(v
: CompilerVisitor, recv
: String): String
413 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
417 redef class MMLocalProperty
418 # Compile the property as a C property
419 meth compile_property_to_c
(v
: CompilerVisitor) do end
422 redef class MMSrcMethod
424 # Compile and declare the signature to C
425 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
427 var params
= new Array[String]
428 params
.add
("val_t {args[0]}")
429 for i
in [0..signature
.arity
[ do
430 var p
= "val_t {args[i+1]}"
434 var first_closure_index
= signature
.arity
+ 1 # Wich parameter is the first closure
435 for i
in [0..signature
.closures
.length
[ do
436 var closcn
= closure_cname
(i
)
437 var cs
= signature
.closures
[i
].signature
# Closure signature
438 var subparams
= new Array[String] # Parameters of the closure
439 subparams
.add
("struct WBT_ *")
440 for j
in [0..cs
.arity
[ do
445 if cs
.return_type
!= null then r
= "val_t"
446 params
.add
("struct WBT_ *{args[first_closure_index+i]}")
447 v
.add_decl
("typedef {r} (*{closcn})({subparams.join(", ")});")
450 if global
.is_init
then
451 params
.add
("int* init_table")
455 if signature
.return_type
!= null then
461 var p
= params
.join
(", ")
462 var s
= "{ret} {cname}({p})"
463 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
468 redef meth compile_property_to_c
(v
)
470 v
.cfc
= new CFunctionContext(v
)
472 var args
= new Array[String]
474 for i
in [0..signature
.arity
[ do
475 args
.add
(" param{i}")
477 for i
in [0..signature
.closures
.length
[ do
480 var cs
= decl_csignature
(v
, args
)
481 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
483 v
.add_instr
("{cs} \{")
492 if s
.node
!= null then ln
= s
.node
.line_number
493 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
494 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
495 v
.add_instr
("trace.file = LOCATE_{module.name};")
496 var s
= do_compile_inside
(v
, args
)
497 v
.add_instr
("tracehead = trace.prev;")
499 v
.add_instr
("return;")
501 v
.add_instr
("return {s};")
504 v
.cfc
.generate_var_decls
506 ctx_old
.append
(v
.ctx
)
511 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)
514 # Compile the method body inline
515 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
518 redef class MMReadImplementationMethod
519 redef meth do_compile_inside
(v
, params
)
521 return node
.prop
.compile_access
(v
, params
[0])
525 redef class MMWriteImplementationMethod
526 redef meth do_compile_inside
(v
, params
)
528 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
533 redef class MMMethSrcMethod
534 redef meth do_compile_inside
(v
, params
)
536 return node
.do_compile_inside
(v
, self, params
)
540 redef class MMImplicitInit
541 redef meth do_compile_inside
(v
, params
)
543 var f
= params
.length
- unassigned_attributes
.length
544 var recv
= params
.first
545 for sp
in super_inits
do
546 assert sp
isa MMMethod
547 var args_recv
= [recv
]
548 if sp
== super_init
then
549 var args
= new Array[String].with_capacity
(f
)
554 sp
.compile_call
(v
, args
)
556 sp
.compile_call
(v
, args_recv
)
559 for i
in [f
..params
.length
[ do
560 var attribute
= unassigned_attributes
[i-f
]
561 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
568 # Compile a subtype check to self
570 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
572 # Fixme: handle formaltypes
573 var g
= local_class
.global
574 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
577 # Compile a cast assertion
578 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
580 # Fixme: handle formaltypes
581 var g
= local_class
.global
582 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}*/;")
586 ###############################################################################
588 redef class AMethPropdef
589 # Compile the method body
590 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
593 redef class PSignature
594 meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String]) is abstract
597 redef class ASignature
598 redef meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String])
600 for ap
in n_params
do
601 var cname
= v
.cfc
.register_variable
(ap
.variable
)
602 v
.nmc
.method_params
.add
(ap
.variable
)
603 var orig_type
= orig_sig
[ap
.position
]
604 if not orig_type
< ap
.variable
.stype
then
605 # FIXME: do not test always
606 # FIXME: handle formal types
607 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
608 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
], ap
)
610 v
.add_assignment
(cname
, params
[ap
.position
])
612 for i
in [0..n_closure_decls
.length
[ do
613 var wd
= n_closure_decls
[i
]
614 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
615 wd
.variable
.ctypename
= v
.nmc
.method
.closure_cname
(i
)
616 v
.add_assignment
(cname
, "{params[orig_sig.arity + i]}")
621 redef class AConcreteMethPropdef
622 redef meth do_compile_inside
(v
, method
, params
)
625 v
.nmc
= new NitMethodContext(method
)
627 var selfcname
= v
.cfc
.register_variable
(self_var
)
628 v
.add_assignment
(selfcname
, params
[0])
630 v
.nmc
.method_params
= [self_var
]
632 if n_signature
!= null then
633 var orig_meth
: MMLocalProperty = method
.global
.intro
634 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
635 n_signature
.compile_parameters
(v
, orig_sig
, params
)
638 var itpos
: String = null
639 if self isa AConcreteInitPropdef then
640 itpos
= "VAL2OBJ({selfcname})->vft[{method.local_class.global.init_table_pos_id}].i"
641 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
642 v
.add_instr
("if (init_table[{itpos}]) return;")
645 v
.nmc
.return_label
= "return_label{v.new_number}"
646 v
.nmc
.return_value
= v
.cfc
.get_var
647 if self isa AConcreteInitPropdef then
648 v
.invoke_super_init_calls_after
(null)
650 if n_block
!= null then
651 v
.compile_stmt
(n_block
)
653 v
.add_instr
("{v.nmc.return_label}: while(false);")
654 if self isa AConcreteInitPropdef then
655 v
.add_instr
("init_table[{itpos}] = 1;")
658 var ret
: String = null
659 if method
.signature
.return_type
!= null then
660 ret
= v
.nmc
.return_value
668 redef class ADeferredMethPropdef
669 redef meth do_compile_inside
(v
, method
, params
)
671 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
672 v
.add_instr
(v
.printf_locate_error
(self))
673 v
.add_instr
("nit_exit(1);")
674 if method
.signature
.return_type
!= null then
682 redef class AExternMethPropdef
683 redef meth do_compile_inside
(v
, method
, params
)
685 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
686 if n_extern
!= null then
687 ename
= n_extern
.text
688 ename
= ename
.substring
(1, ename
.length-2
)
690 var sig
= method
.signature
691 if params
.length
!= sig
.arity
+ 1 then
692 printl
("par:{params.length} sig:{sig.arity}")
694 var args
= new Array[String]
695 args
.add
(sig
.recv
.unboxtype
(params
[0]))
696 for i
in [0..sig
.arity
[ do
697 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
699 var s
= "{ename}({args.join(", ")})"
700 if sig
.return_type
!= null then
701 return sig
.return_type
.boxtype
(s
)
709 redef class AInternMethPropdef
710 redef meth do_compile_inside
(v
, method
, p
)
712 var c
= method
.local_class
.name
715 if c
== once
"Int".to_symbol
then
716 if n
== once
"object_id".to_symbol
then
718 else if n
== once
"unary -".to_symbol
then
719 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
720 else if n
== once
"output".to_symbol
then
721 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
722 else if n
== once
"ascii".to_symbol
then
723 s
= "TAG_Char(UNTAG_Int({p[0]}))"
724 else if n
== once
"succ".to_symbol
then
725 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
726 else if n
== once
"prec".to_symbol
then
727 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
728 else if n
== once
"to_f".to_symbol
then
729 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
730 else if n
== once
"+".to_symbol
then
731 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
732 else if n
== once
"-".to_symbol
then
733 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
734 else if n
== once
"*".to_symbol
then
735 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
736 else if n
== once
"/".to_symbol
then
737 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
738 else if n
== once
"%".to_symbol
then
739 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
740 else if n
== once
"<".to_symbol
then
741 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
742 else if n
== once
">".to_symbol
then
743 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
744 else if n
== once
"<=".to_symbol
then
745 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
746 else if n
== once
">=".to_symbol
then
747 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
748 else if n
== once
"lshift".to_symbol
then
749 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
750 else if n
== once
"rshift".to_symbol
then
751 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
752 else if n
== once
"==".to_symbol
then
753 s
= "TAG_Bool(({p[0]})==({p[1]}))"
754 else if n
== once
"!=".to_symbol
then
755 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
757 else if c
== once
"Float".to_symbol
then
758 if n
== once
"object_id".to_symbol
then
759 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
760 else if n
== once
"unary -".to_symbol
then
761 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
762 else if n
== once
"output".to_symbol
then
763 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
764 else if n
== once
"to_i".to_symbol
then
765 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
766 else if n
== once
"+".to_symbol
then
767 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
768 else if n
== once
"-".to_symbol
then
769 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
770 else if n
== once
"*".to_symbol
then
771 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
772 else if n
== once
"/".to_symbol
then
773 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
774 else if n
== once
"<".to_symbol
then
775 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
776 else if n
== once
">".to_symbol
then
777 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
778 else if n
== once
"<=".to_symbol
then
779 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
780 else if n
== once
">=".to_symbol
then
781 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
783 else if c
== once
"Char".to_symbol
then
784 if n
== once
"object_id".to_symbol
then
785 s
= "TAG_Int(UNTAG_Char({p[0]}))"
786 else if n
== once
"unary -".to_symbol
then
787 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
788 else if n
== once
"output".to_symbol
then
789 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
790 else if n
== once
"ascii".to_symbol
then
791 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
792 else if n
== once
"succ".to_symbol
then
793 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
794 else if n
== once
"prec".to_symbol
then
795 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
796 else if n
== once
"to_i".to_symbol
then
797 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
798 else if n
== once
"+".to_symbol
then
799 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
800 else if n
== once
"-".to_symbol
then
801 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
802 else if n
== once
"*".to_symbol
then
803 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
804 else if n
== once
"/".to_symbol
then
805 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
806 else if n
== once
"%".to_symbol
then
807 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
808 else if n
== once
"<".to_symbol
then
809 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
810 else if n
== once
">".to_symbol
then
811 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
812 else if n
== once
"<=".to_symbol
then
813 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
814 else if n
== once
">=".to_symbol
then
815 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
816 else if n
== once
"==".to_symbol
then
817 s
= "TAG_Bool(({p[0]})==({p[1]}))"
818 else if n
== once
"!=".to_symbol
then
819 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
821 else if c
== once
"Bool".to_symbol
then
822 if n
== once
"object_id".to_symbol
then
823 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
824 else if n
== once
"unary -".to_symbol
then
825 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
826 else if n
== once
"output".to_symbol
then
827 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
828 else if n
== once
"ascii".to_symbol
then
829 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
830 else if n
== once
"to_i".to_symbol
then
831 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
832 else if n
== once
"==".to_symbol
then
833 s
= "TAG_Bool(({p[0]})==({p[1]}))"
834 else if n
== once
"!=".to_symbol
then
835 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
837 else if c
== once
"NativeArray".to_symbol
then
838 if n
== once
"object_id".to_symbol
then
839 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
840 else if n
== once
"[]".to_symbol
then
841 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
842 else if n
== once
"[]=".to_symbol
then
843 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
844 else if n
== once
"copy_to".to_symbol
then
845 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
847 else if c
== once
"NativeString".to_symbol
then
848 if n
== once
"object_id".to_symbol
then
849 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
850 else if n
== once
"atoi".to_symbol
then
851 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
852 else if n
== once
"[]".to_symbol
then
853 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
854 else if n
== once
"[]=".to_symbol
then
855 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
856 else if n
== once
"copy_to".to_symbol
then
857 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]}));")
859 else if n
== once
"object_id".to_symbol
then
860 s
= "TAG_Int((bigint){p[0]})"
861 else if n
== once
"sys".to_symbol
then
863 else if n
== once
"is_same_type".to_symbol
then
864 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
865 else if n
== once
"exit".to_symbol
then
866 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
867 else if n
== once
"calloc_array".to_symbol
then
868 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
869 else if n
== once
"calloc_string".to_symbol
then
870 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
873 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
875 if method
.signature
.return_type
!= null and s
== null then
876 s
= "NIT_NULL /*stub*/"
882 ###############################################################################
885 # Compile the node as an expression
886 # Only the visitor should call it
887 meth compile_expr
(v
: CompilerVisitor): String is abstract
889 # Prepare a call of node as a statement
890 # Only the visitor should call it
891 # It's used for local variable managment
892 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
894 # Compile the node as a statement
895 # Only the visitor should call it
896 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
899 redef class ABlockExpr
900 redef meth compile_stmt
(v
)
908 redef class AVardeclExpr
909 redef meth prepare_compile_stmt
(v
)
911 v
.cfc
.register_variable
(variable
)
914 redef meth compile_stmt
(v
)
916 var cname
= v
.cfc
.varname
(variable
)
917 if n_expr
== null then
918 v
.add_instr
("/*{cname} is variable {variable.name}*/")
920 var e
= v
.compile_expr
(n_expr
)
921 v
.add_assignment
(cname
, e
)
926 redef class AReturnExpr
927 redef meth compile_stmt
(v
)
929 if n_expr
!= null then
930 var e
= v
.compile_expr
(n_expr
)
931 v
.add_assignment
(v
.nmc
.return_value
, e
)
933 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
934 v
.add_instr
("goto {v.nmc.return_label};")
938 redef class ABreakExpr
939 redef meth compile_stmt
(v
)
941 if n_expr
!= null then
942 var e
= v
.compile_expr
(n_expr
)
943 v
.add_assignment
(v
.nmc
.break_value
, e
)
945 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
946 v
.add_instr
("goto {v.nmc.break_label};")
950 redef class AContinueExpr
951 redef meth compile_stmt
(v
)
953 if n_expr
!= null then
954 var e
= v
.compile_expr
(n_expr
)
955 v
.add_assignment
(v
.nmc
.continue_value
, e
)
957 v
.add_instr
("goto {v.nmc.continue_label};")
961 redef class AAbortExpr
962 redef meth compile_stmt
(v
)
964 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
969 redef meth compile_stmt
(v
)
971 if n_block
!= null then
972 v
.compile_stmt
(n_block
)
978 redef meth compile_stmt
(v
)
980 var e
= v
.compile_expr
(n_expr
)
981 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
983 if n_then
!= null then
985 v
.compile_stmt
(n_then
)
988 if n_else
!= null then
989 v
.add_instr
("} else \{ /*if*/")
991 v
.compile_stmt
(n_else
)
998 redef class AIfexprExpr
999 redef meth compile_expr
(v
)
1001 var e
= v
.compile_expr
(n_expr
)
1002 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1005 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
1007 v
.add_instr
("} else \{ /*if*/")
1010 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
1011 v
.add_assignment
(e
, e2
)
1018 redef class AControlableBlock
1019 meth compile_inside_block
(v
: CompilerVisitor) is abstract
1020 redef meth compile_stmt
(v
)
1022 var old_break_label
= v
.nmc
.break_label
1023 var old_continue_label
= v
.nmc
.continue_label
1024 var id
= v
.new_number
1025 v
.nmc
.break_label
= "break_{id}"
1026 v
.nmc
.continue_label
= "continue_{id}"
1028 compile_inside_block
(v
)
1031 v
.nmc
.break_label
= old_break_label
1032 v
.nmc
.continue_label
= old_continue_label
1036 redef class AWhileExpr
1037 redef meth compile_inside_block
(v
)
1039 v
.add_instr
("while (true) \{ /*while*/")
1041 var e
= v
.compile_expr
(n_expr
)
1042 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
1044 if n_block
!= null then
1045 v
.compile_stmt
(n_block
)
1047 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1050 v
.add_instr
("{v.nmc.break_label}: while(0);")
1054 redef class AForExpr
1055 redef meth compile_inside_block
(v
)
1057 var e
= v
.compile_expr
(n_expr
)
1058 var ittype
= meth_iterator
.signature
.return_type
1060 var iter
= v
.cfc
.get_var
1061 v
.add_assignment
(iter
, meth_iterator
.compile_call
(v
, [e
]))
1062 v
.add_instr
("while (true) \{ /*for*/")
1064 var ok
= v
.cfc
.get_var
1065 v
.add_assignment
(ok
, meth_is_ok
.compile_call
(v
, [iter
]))
1066 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1068 var e
= meth_item
.compile_call
(v
, [iter
])
1070 var cname
= v
.cfc
.register_variable
(variable
)
1071 v
.add_assignment
(cname
, e
)
1072 var n_block
= n_block
1073 if n_block
!= null then
1074 v
.compile_stmt
(n_block
)
1076 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1077 e
= meth_next
.compile_call
(v
, [iter
])
1081 v
.add_instr
("{v.nmc.break_label}: while(0);")
1085 redef class AAssertExpr
1086 redef meth compile_stmt
(v
)
1088 var e
= v
.compile_expr
(n_expr
)
1090 if n_id
!= null then
1091 s
= " '{n_id.text}' "
1093 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1097 redef class AVarExpr
1098 redef meth compile_expr(v)
1100 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1104 redef class AVarAssignExpr
1105 redef meth compile_stmt(v)
1107 var e = v.compile_expr(n_value)
1108 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1112 redef class AVarReassignExpr
1113 redef meth compile_stmt(v)
1115 var e1 = v.cfc.varname(variable)
1116 var e2 = v.compile_expr(n_value)
1117 var e3 = assign_method.compile_call(v, [e1, e2])
1118 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1122 redef class ASelfExpr
1123 redef meth compile_expr(v)
1125 return v.cfc.varname(v.nmc.method_params[0])
1130 redef meth compile_expr(v)
1132 var e = v.ensure_var(v.compile_expr(n_expr))
1133 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1136 var e2 = v.compile_expr(n_expr2)
1137 v.add_assignment(e, e2)
1144 redef class AAndExpr
1145 redef meth compile_expr(v)
1147 var e = v.ensure_var(v.compile_expr(n_expr))
1148 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1151 var e2 = v.compile_expr(n_expr2)
1152 v.add_assignment(e, e2)
1159 redef class ANotExpr
1160 redef meth compile_expr(v)
1162 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1167 redef meth compile_expr(v)
1169 var e = v.compile_expr(n_expr)
1170 var e2 = v.compile_expr(n_expr2)
1171 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1175 redef class AIsaExpr
1176 redef meth compile_expr(v)
1178 var e = v.compile_expr(n_expr)
1179 return n_type.stype.compile_cast(v, e)
1183 redef class AAsCastExpr
1184 redef meth compile_expr(v)
1186 var e = v.compile_expr(n_expr)
1187 n_type.stype.compile_type_check(v, e, self)
1192 redef class ATrueExpr
1193 redef meth compile_expr(v)
1195 return " TAG_Bool(true)"
1199 redef class AFalseExpr
1200 redef meth compile_expr(v)
1202 return " TAG_Bool(false)"
1206 redef class AIntExpr
1207 redef meth compile_expr(v)
1209 return " TAG_Int({n_number.text})"
1213 redef class AFloatExpr
1214 redef meth compile_expr(v)
1216 return "BOX_Float({n_float.text})"
1220 redef class ACharExpr
1221 redef meth compile_expr(v)
1223 return " TAG_Char({n_char.text})"
1227 redef class AStringFormExpr
1228 redef meth compile_expr(v)
1231 var i = v.new_number
1232 var cvar = v.cfc.get_var
1233 v.add_decl("static val_t once_value_
{i} = NIT_NULL; /* Once value
for string
{cvar}*/")
1234 v.add_instr("if (once_value_
{i} != NIT_NULL) {cvar} = once_value_
{i};")
1235 v.add_instr("else \
{")
1237 v.cfc.free_var(cvar)
1238 var e = meth_with_native.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1239 v.add_assignment(cvar, e)
1240 v.add_instr("once_value_
{i} = {cvar};")
1246 # The raw string value
1247 protected meth string_text: String is abstract
1249 # The string in a C native format
1250 protected attr _cstring: String
1252 # The string length in bytes
1253 protected attr _cstring_length: Int
1255 # Compute _cstring and _cstring_length using string_text
1256 protected meth compute_string_info
1259 var str = string_text
1260 var res = new Buffer
1262 while i < str.length do
1267 if c2 != '{' and c2 != '}' then
1277 _cstring_length = len
1281 redef class AStringExpr
1282 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1284 redef class AStartStringExpr
1285 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1287 redef class AMidStringExpr
1288 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1290 redef class AEndStringExpr
1291 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1294 redef class ASuperstringExpr
1295 redef meth compile_expr(v)
1297 var array = meth_with_capacity.compile_constructor_call(v, atype, ["TAG_Int({n_exprs.length})"])
1299 for ne in n_exprs do
1300 var e = v.ensure_var(v.compile_expr(ne))
1301 if ne.stype != stype then
1302 v.add_assignment(e, meth_to_s.compile_call(v, [e]))
1304 meth_add.compile_call(v, [array, e])
1307 return meth_to_s.compile_call(v, [array])
1311 redef class ANullExpr
1312 redef meth compile_expr(v)
1314 return " NIT_NULL /*null*/"
1318 redef class AArrayExpr
1319 redef meth compile_expr(v)
1321 var recv = meth_with_capacity.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1323 for ne in n_exprs do
1324 var e = v.compile_expr(ne)
1325 meth_add.compile_call(v, [recv, e])
1331 redef class ARangeExpr
1332 redef meth compile_expr(v)
1334 var e = v.compile_expr(n_expr)
1335 var e2 = v.compile_expr(n_expr2)
1336 return meth_init.compile_constructor_call(v, stype, [e, e2])
1340 redef class ASuperExpr
1341 redef meth compile_stmt(v)
1343 var e = compile_expr(v)
1344 if e != null then v.add_instr("{e};")
1347 redef meth compile_expr(v)
1349 var arity = v.nmc.method_params.length - 1
1350 if init_in_superclass != null then
1351 arity = init_in_superclass.signature.arity
1353 var args = new Array[String].with_capacity(arity + 1)
1354 args.add(v.cfc.varname(v.nmc.method_params[0]))
1355 if n_args.length != arity then
1356 for i in [0..arity[ do
1357 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1361 args.add(v.compile_expr(na))
1364 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1365 if init_in_superclass != null then
1366 return init_in_superclass.compile_call(v, args)
1368 if prop.global.is_init then args.add("init_table
")
1369 return prop.compile_super_call(v, args)
1374 redef class AAttrExpr
1375 redef meth compile_expr(v)
1377 var e = v.compile_expr(n_expr)
1378 return prop.compile_access(v, e)
1382 redef class AAttrAssignExpr
1383 redef meth compile_stmt(v)
1385 var e = v.compile_expr(n_expr)
1386 var e2 = v.compile_expr(n_value)
1387 v.add_assignment(prop.compile_access(v, e), e2)
1390 redef class AAttrReassignExpr
1391 redef meth compile_stmt(v)
1393 var e1 = v.compile_expr(n_expr)
1394 var e2 = prop.compile_access(v, e1)
1395 var e3 = v.compile_expr(n_value)
1396 var e4 = assign_method.compile_call(v, [e2, e3])
1397 v.add_assignment(e2, e4)
1401 redef class ASendExpr
1402 redef meth compile_expr(v)
1404 var recv = v.compile_expr(n_expr)
1405 var cargs = new Array[String]
1407 for a in arguments do
1408 cargs.add(v.compile_expr(a))
1412 if prop_signature.closures.is_empty then
1413 e = prop.compile_call(v, cargs)
1415 e = prop.compile_call_and_closures(v, cargs, closure_defs)
1418 if prop.global.is_init then
1419 v.invoke_super_init_calls_after(prop)
1424 redef meth compile_stmt(v)
1426 var e = compile_expr(v)
1428 v.add_instr(e + ";")
1433 redef class ASendReassignExpr
1434 redef meth compile_expr(v)
1436 var recv = v.compile_expr(n_expr)
1437 var cargs = new Array[String]
1439 for a in arguments do
1440 cargs.add(v.compile_expr(a))
1443 var e2 = read_prop.compile_call(v, cargs)
1444 var e3 = v.compile_expr(n_value)
1445 var e4 = assign_method.compile_call(v, [e2, e3])
1447 return prop.compile_call(v, cargs)
1451 redef class ANewExpr
1452 redef meth compile_expr(v)
1454 var cargs = new Array[String]
1455 for a in arguments do
1456 cargs.add(v.compile_expr(a))
1458 return prop.compile_constructor_call(v, stype, cargs)
1462 redef class PClosureDef
1463 # Compile the closure definition as a function in v.out_contexts
1464 # Return the cname of the function
1465 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1467 # Compile the closure definition inside the current C function.
1468 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1471 redef class AClosureDef
1472 # The cname of the function
1473 readable attr _cname: String
1475 redef meth compile_closure(v, closcn)
1478 v.ctx = new CContext
1479 v.out_contexts.add(v.ctx)
1481 var cfc_old = v.cfc.closure
1482 v.cfc.closure = v.nmc
1484 var old_rv = v.nmc.return_value
1485 var old_bv = v.nmc.break_value
1486 if cfc_old == null then
1487 v.nmc.return_value = "closctx-
>{old_rv}"
1488 v.nmc.break_value = "closctx-
>{old_bv}"
1491 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1493 var args = new Array[String]
1494 for i in [0..closure.signature.arity[ do
1495 args.add(" param
{i}")
1498 var cs = decl_csignature(v, args, closcn)
1500 v.add_instr("{cs} \
{")
1502 var ctx_old2 = v.ctx
1503 v.ctx = new CContext
1505 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1506 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1508 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1509 var s = do_compile_inside(v, args)
1511 v.add_instr("{v.nmc.return_label}:")
1512 v.add_instr("tracehead
= trace
.prev
;")
1514 v.add_instr("return;")
1516 v.add_instr("return {s};")
1519 ctx_old2.append(v.ctx)
1525 v.cfc.closure = cfc_old
1526 v.nmc.return_value = old_rv
1527 v.nmc.break_value = old_bv
1530 var closcnv = "wbclos
{v.new_number}"
1531 v.add_decl("struct
WBT_ {closcnv};")
1532 v.add_instr("{closcnv}.fun = (fun_t
){cname};")
1533 v.add_instr("{closcnv}.has_broke
= NULL;")
1534 if cfc_old != null then
1535 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1536 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1538 v.add_instr("{closcnv}.variable
= variable
;")
1539 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1542 return "(&{closcnv})"
1545 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1547 var params = new Array[String]
1548 params.add("struct
WBT_ *closctx
")
1549 for i in [0..closure.signature.arity[ do
1550 var p = "val_t
{args[i]}"
1554 if closure.signature.return_type != null then
1559 var p = params.join(", ")
1560 var s = "{ret} {cname}({p})"
1561 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1566 redef meth do_compile_inside(v, params)
1568 for i in [0..variables.length[ do
1569 var vacname = v.cfc.register_variable(variables[i])
1570 v.add_assignment(vacname, params[i])
1573 var old_cv = v.nmc.continue_value
1574 var old_cl = v.nmc.continue_label
1575 var old_bl = v.nmc.break_label
1577 v.nmc.continue_value = v.cfc.get_var
1578 v.nmc.continue_label = "continue_label
{v.new_number}"
1579 v.nmc.break_label = v.nmc.return_label
1581 if n_expr != null then v.compile_stmt(n_expr)
1583 v.add_instr("{v.nmc.continue_label}: while(false);")
1585 var ret: String = null
1586 if closure.signature.return_type != null then ret = v.nmc.continue_value
1588 v.nmc.continue_value = old_cv
1589 v.nmc.continue_label = old_cl
1590 v.nmc.break_label = old_bl
1596 redef class PClosureDecl
1597 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1599 redef class AClosureDecl
1600 redef meth do_compile_inside(v, params)
1602 if n_signature != null then n_signature.compile_parameters(v, variable.closure.signature, params)
1604 var old_cv = v.nmc.continue_value
1605 var old_cl = v.nmc.continue_label
1606 var old_bl = v.nmc.break_label
1608 v.nmc.continue_value = v.cfc.get_var
1609 v.nmc.continue_label = "continue_label
{v.new_number}"
1610 v.nmc.break_label = v.nmc.return_label
1612 if n_expr != null then v.compile_stmt(n_expr)
1614 v.add_instr("{v.nmc.continue_label}: while(false);")
1616 var ret: String = null
1617 if variable.closure.signature.return_type != null then ret = v.nmc.continue_value
1619 v.nmc.continue_value = old_cv
1620 v.nmc.continue_label = old_cl
1621 v.nmc.break_label = old_bl
1627 redef class AClosureCallExpr
1628 redef meth compile_expr(v)
1630 var cargs = new Array[String]
1631 for a in arguments do cargs.add(v.compile_expr(a))
1632 var va: String = null
1633 if variable.closure.signature.return_type != null then va = v.cfc.get_var
1635 if variable.closure.is_optional then
1636 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1638 var n = variable.decl
1639 assert n isa AClosureDecl
1640 var s = n.do_compile_inside(v, cargs)
1641 if s != null then v.add_assignment(va, s)
1643 v.add_instr("} else \
{")
1647 var ivar = v.cfc.varname(variable)
1649 cargs2.append(cargs)
1650 var s = "(({variable.ctypename})({ivar}->fun))({cargs2.join(", ")}) /* Invoke closure
{variable} */"
1652 v.add_assignment(va, s)
1656 v.add_instr("if ({ivar}->has_broke
) \
{")
1658 if n_closure_defs != null and n_closure_defs.length == 1 then do
1659 n_closure_defs.first.do_compile_inside(v, null)
1661 if v.cfc.closure == v.nmc then v.add_instr("if ({ivar}->has_broke
) \
{ closctx->has_broke = {ivar}->has_broke
; closctx-
>broke_value
= {ivar}->broke_value
;\
}")
1662 v.add_instr("goto
{v.nmc.return_label};")
1666 if variable.closure.is_optional then
1674 redef class AProxyExpr
1675 redef meth compile_expr(v)
1677 return v.compile_expr(n_expr)
1681 redef class AOnceExpr
1682 redef meth compile_expr(v)
1684 var i = v.new_number
1685 var cvar = v.cfc.get_var
1686 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1687 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1688 v.add_instr("else \
{")
1690 v.cfc.free_var(cvar)
1691 var e = v.compile_expr(n_expr)
1692 v.add_assignment(cvar, e)
1693 v.add_instr("once_value_
{i} = {cvar};")
1694 v.add_instr("once_bool_
{i} = true;")