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
41 if s
[0] == ' ' or cfc
.is_valid_variable
(s
) then
44 var v
= cfc
.get_var
("Result for expr {n.locate}")
49 # Ensure that a c expression is a var
50 meth ensure_var
(s
: String, comment
: String): String
52 if cfc
.is_valid_variable
(s
) then
53 add_instr
("/* Ensure var {s}: {comment}*/")
56 var v
= cfc
.get_var
(null)
57 add_assignment
(v
, "{s} /* Ensure var: {comment}*/")
61 # Add a assignment between a variable and an expression
62 meth add_assignment
(v
: String, s
: String)
65 add_instr
("{v} = {s};")
69 readable writable attr _cfc
: CFunctionContext
71 readable writable attr _nmc
: NitMethodContext
73 # C outputs written outside the current C function.
74 readable writable attr _out_contexts
: Array[CContext] = new Array[CContext]
76 # Generate an fprintf to display an error location
77 meth printf_locate_error
(node
: PNode): String
79 var s
= new Buffer.from
("fprintf(stderr, \"")
80 if nmc != null then s.append(" in %s
")
81 s.append(" (%s
:%d
)\\n\
", ")
82 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
83 s
.append
("LOCATE_{module.name}, {node.line_number});")
87 redef init(module: MMSrcModule)
92 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
94 var n
= nmc
.method
.node
95 assert n
isa AConcreteInitPropdef
97 if n
.super_init_calls
.is_empty
then return
101 if start_prop
!= null then
102 while n
.super_init_calls
[i
] != start_prop
do
103 #s.append(" {n.super_init_calls[i]}")
107 #s.append(" {start_prop}")
109 while n
.explicit_super_init_calls
[j
] != start_prop
do
114 var stop_prop
: MMMethod = null
115 if j
< n
.explicit_super_init_calls
.length
then
116 stop_prop
= n
.explicit_super_init_calls
[j
]
118 var l
= n
.super_init_calls
.length
121 var p
= n
.super_init_calls
[i
]
122 if p
== stop_prop
then break
123 var cargs
= new Array[String]
124 if p
.signature
.arity
== 0 then
125 cargs
.add
(cfc
.varname
(nmc
.method_params
[0]))
127 for va
in nmc
.method_params
do
128 cargs
.add
(cfc
.varname
(va
))
132 p
.compile_call
(self, cargs
)
137 # s.append(" {n.super_init_calls[i]}")
140 #if stop_prop != null then s.append(" (stop at {stop_prop})")
141 #n.printl("implicit calls in {n.method}: {s}")
145 # A C function currently written
146 class CFunctionContext
147 readable attr _visitor
: CompilerVisitor
149 # Next available variable number
150 attr _variable_index
: Int = 0
152 # Total number of variable
153 attr _variable_index_max
: Int = 0
155 # Association between nit variable and the corrsponding c variable index
156 attr _varindexes
: Map[Variable, Int] = new HashMap[Variable, Int]
158 # Are we currenlty in a closure definition?
159 readable writable attr _closure
: NitMethodContext = null
161 # Return the cvariable of a Nit variable
162 meth varname
(v
: Variable): String
164 if v
isa ClosureVariable then
165 return closure_variable
(_varindexes
[v
])
167 return variable
(_varindexes
[v
])
171 # Return the next available variable
172 meth get_var
(comment
: String): String
174 var v
= variable
(_variable_index
)
175 _variable_index
= _variable_index
+ 1
176 if _variable_index
> _variable_index_max
then
177 _variable_index_max
= _variable_index
179 if comment
!= null then
180 visitor
.add_instr
("/* Register {v}: {comment} */")
185 meth register_variable
(v
: Variable): String
187 _varindexes
[v
] = _variable_index
188 var s
= get_var
("Local variable")
192 # Next available closure variable number
193 attr _closurevariable_index
: Int = 0
195 meth register_closurevariable
(v
: ClosureVariable): String
197 var s
= "closurevariable[{_closurevariable_index}]"
198 _varindexes
[v
] = _closurevariable_index
199 _closurevariable_index
+= 1
200 if _closure
!= null then
201 return "(closctx->{s})"
207 # Return the ith cvariable
208 protected meth variable
(i
: Int): String
210 if closure
!= null then
211 var vn
= once
new Array[String]
212 if vn
.length
<= i
then
213 for j
in [vn
.length
..i
] do
214 vn
[j
] = "(closctx->variable[{j}])"
219 var vn
= once
new Array[String]
220 if vn
.length
<= i
then
221 for j
in [vn
.length
..i
] do
222 vn
[j
] = "variable[{j}]"
229 # Return the ith closurevariable
230 protected meth closure_variable
(i
: Int): String
232 if closure
!= null then
233 return "(closctx->closurevariable[{i}])"
235 return "closurevariable[{i}]"
239 # Is s a valid variable
240 protected meth is_valid_variable
(s
: String): Bool
242 for i
in [0.._variable_index
[ do
243 if s
== variable
(i
) then return true
248 # Mark the variable available
249 meth free_var
(v
: String)
252 if v
== variable
(_variable_index-1
) then
253 _variable_index
= _variable_index
- 1
257 # Generate the local variable declarations
258 # To use at the end of the C function once all variables are known
259 meth generate_var_decls
261 if _variable_index_max
> 0 then
262 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
264 visitor
.add_decl
("val_t *variable = NULL;")
266 if _closurevariable_index
> 0 then
267 visitor
.add_decl
("struct WBT_ *closurevariable[{_closurevariable_index}];")
269 visitor
.add_decl
("struct WBT_ **closurevariable = NULL;")
273 init(v
: CompilerVisitor) do _visitor
= v
276 # A Nit method currenlty compiled
277 class NitMethodContext
278 # Current method compiled
279 readable attr _method
: MMSrcMethod
281 # Association between parameters and the corresponding variables
282 readable writable attr _method_params
: Array[ParamVariable]
284 # Where a nit return must branch
285 readable writable attr _return_label
: String
287 # Where a nit break must branch
288 readable writable attr _break_label
: String
290 # Where a nit continue must branch
291 readable writable attr _continue_label
: String
293 # Variable where a functionnal nit return must store its value
294 readable writable attr _return_value
: String
296 # Variable where a functionnal nit break must store its value
297 readable writable attr _break_value
: String
299 # Variable where a functionnal nit continue must store its value
300 readable writable attr _continue_value
: String
302 init(method
: MMSrcMethod)
308 ###############################################################################
310 redef class ClosureVariable
311 readable writable attr _ctypename
: String
315 # Compile a call on self for given arguments
316 # Most calls are compiled with a table access,
317 # primitive calles are inlined
318 # == and != are guarded and possibly inlined
319 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
322 if i
isa MMSrcMethod then
323 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
324 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
326 var e
= i
.do_compile_inside
(v
, cargs
)
330 var ee
= once
"==".to_symbol
331 var ne
= once
"!=".to_symbol
333 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
334 var eqcall
= eqp
.compile_call
(v
, cargs
)
335 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
337 if global
.is_init
then
339 cargs
.add
("init_table /*YYY*/")
342 var m
= "{global.meth_call}({cargs[0]})"
343 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
345 vcall
= "UNTAG_Bool({vcall})"
346 var obj
= once
"Object".to_symbol
347 if i
.local_class
.name
== obj
then
348 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
350 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
352 if signature
.return_type
!= null then
355 v
.add_instr
(vcall
+ ";")
360 # Compile a call on self for given arguments and given closures
361 meth compile_call_and_closures
(v
: CompilerVisitor, cargs
: Array[String], clos_defs
: Array[PClosureDef]): String
363 var ve
: String = null
365 if clos_defs
!= null then arity
= clos_defs
.length
367 # Prepare result value.
368 # In case of procedure, the return value is still used to intercept breaks
369 var old_bv
= v
.nmc
.break_value
370 ve
= v
.cfc
.get_var
("Closure return value and escape marker")
371 v
.nmc
.break_value
= ve
373 # Compile closure to c function
374 var realcargs
= new Array[String] # Args to pass to the C function call
375 var closcns
= new Array[String] # Closure C structure names
376 realcargs
.add_all
(cargs
)
377 for i
in [0..arity
[ do
378 var cn
= clos_defs
[i
].compile_closure
(v
, closure_cname
(i
))
382 for i
in [arity
..signature
.closures
.length
[ do
383 realcargs
.add
("NULL")
386 v
.nmc
.break_value
= old_bv
389 var e
= compile_call
(v
, realcargs
)
391 v
.add_assignment
(ve
, e
)
395 # Intercept returns and breaks
396 for i
in [0..arity
[ do
397 # A break or a return is intercepted
398 v
.add_instr
("if ({closcns[i]}->has_broke != NULL) \{")
400 # A passtrought break or a return is intercepted: go the the next closure
401 v
.add_instr
("if ({closcns[i]}->has_broke != &({ve})) \{")
403 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = {closcns[i]}->has_broke; closctx->broke_value = {closcns[i]}->broke_value;")
404 v
.add_instr
("goto {v.nmc.return_label};")
406 # A direct break is interpected
408 # overwrite the returned value in a function
409 v
.add_instr
("\} else {ve} = {closcns[i]}->broke_value;")
411 # Do nothing in a procedure
420 # Compile a call as constructor with given args
421 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
423 return "NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}) /*new {recvtype}*/"
426 # Compile a call as call-next-method on self with given args
427 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
429 var m
= "{super_meth_call}({cargs[0]})"
430 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
434 # Cname of the i-th closure C struct type
435 protected meth closure_cname
(i
: Int): String
437 return "FWBT_{cname}_{i}"
441 redef class MMAttribute
442 # Compile an acces on selffor a given reciever.
443 # Result is a valid C left-value for assigment
444 meth compile_access
(v
: CompilerVisitor, recv
: String): String
446 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
450 redef class MMLocalProperty
451 # Compile the property as a C property
452 meth compile_property_to_c
(v
: CompilerVisitor) do end
455 redef class MMSrcMethod
457 # Compile and declare the signature to C
458 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
460 var params
= new Array[String]
461 params
.add
("val_t {args[0]}")
462 for i
in [0..signature
.arity
[ do
463 var p
= "val_t {args[i+1]}"
467 var first_closure_index
= signature
.arity
+ 1 # Wich parameter is the first closure
468 for i
in [0..signature
.closures
.length
[ do
469 var closcn
= closure_cname
(i
)
470 var cs
= signature
.closures
[i
].signature
# Closure signature
471 var subparams
= new Array[String] # Parameters of the closure
472 subparams
.add
("struct WBT_ *")
473 for j
in [0..cs
.arity
[ do
478 if cs
.return_type
!= null then r
= "val_t"
479 params
.add
("struct WBT_ *{args[first_closure_index+i]}")
480 v
.add_decl
("typedef {r} (*{closcn})({subparams.join(", ")});")
483 if global
.is_init
then
484 params
.add
("int* init_table")
488 if signature
.return_type
!= null then
494 var p
= params
.join
(", ")
495 var s
= "{ret} {cname}({p})"
496 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
501 redef meth compile_property_to_c
(v
)
503 v
.cfc
= new CFunctionContext(v
)
505 var args
= new Array[String]
507 for i
in [0..signature
.arity
[ do
508 args
.add
(" param{i}")
510 for i
in [0..signature
.closures
.length
[ do
513 var cs
= decl_csignature
(v
, args
)
514 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
516 v
.add_instr
("{cs} \{")
525 if s
.node
!= null then ln
= s
.node
.line_number
526 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
527 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
528 v
.add_instr
("trace.file = LOCATE_{module.name};")
529 var s
= do_compile_inside
(v
, args
)
530 v
.add_instr
("tracehead = trace.prev;")
532 v
.add_instr
("return;")
534 v
.add_instr
("return {s};")
537 v
.cfc
.generate_var_decls
539 ctx_old
.append
(v
.ctx
)
544 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)
547 # Compile the method body inline
548 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
551 redef class MMReadImplementationMethod
552 redef meth do_compile_inside
(v
, params
)
554 return node
.prop
.compile_access
(v
, params
[0])
558 redef class MMWriteImplementationMethod
559 redef meth do_compile_inside
(v
, params
)
561 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
566 redef class MMMethSrcMethod
567 redef meth do_compile_inside
(v
, params
)
569 return node
.do_compile_inside
(v
, self, params
)
573 redef class MMImplicitInit
574 redef meth do_compile_inside
(v
, params
)
576 var f
= params
.length
- unassigned_attributes
.length
577 var recv
= params
.first
578 for sp
in super_inits
do
579 assert sp
isa MMMethod
580 var args_recv
= [recv
]
581 if sp
== super_init
then
582 var args
= new Array[String].with_capacity
(f
)
587 sp
.compile_call
(v
, args
)
589 sp
.compile_call
(v
, args_recv
)
592 for i
in [f
..params
.length
[ do
593 var attribute
= unassigned_attributes
[i-f
]
594 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
601 # Compile a subtype check to self
603 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
605 # Fixme: handle formaltypes
606 var g
= local_class
.global
607 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
610 # Compile a cast assertion
611 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
613 # Fixme: handle formaltypes
614 var g
= local_class
.global
615 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}*/;")
619 ###############################################################################
621 redef class AMethPropdef
622 # Compile the method body
623 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
626 redef class PSignature
627 meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String]) is abstract
630 redef class ASignature
631 redef meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String])
633 for ap
in n_params
do
634 var cname
= v
.cfc
.register_variable
(ap
.variable
)
635 v
.nmc
.method_params
.add
(ap
.variable
)
636 var orig_type
= orig_sig
[ap
.position
]
637 if not orig_type
< ap
.variable
.stype
then
638 # FIXME: do not test always
639 # FIXME: handle formal types
640 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
641 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
], ap
)
643 v
.add_assignment
(cname
, params
[ap
.position
])
645 for i
in [0..n_closure_decls
.length
[ do
646 var wd
= n_closure_decls
[i
]
647 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
648 wd
.variable
.ctypename
= v
.nmc
.method
.closure_cname
(i
)
649 v
.add_assignment
(cname
, "{params[orig_sig.arity + i]}")
654 redef class AConcreteMethPropdef
655 redef meth do_compile_inside
(v
, method
, params
)
658 v
.nmc
= new NitMethodContext(method
)
660 var selfcname
= v
.cfc
.register_variable
(self_var
)
661 v
.add_assignment
(selfcname
, params
[0])
663 v
.nmc
.method_params
= [self_var
]
665 if n_signature
!= null then
666 var orig_meth
: MMLocalProperty = method
.global
.intro
667 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
668 n_signature
.compile_parameters
(v
, orig_sig
, params
)
671 var itpos
: String = null
672 if self isa AConcreteInitPropdef then
673 itpos
= "VAL2OBJ({selfcname})->vft[{method.local_class.global.init_table_pos_id}].i"
674 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
675 v
.add_instr
("if (init_table[{itpos}]) return;")
678 v
.nmc
.return_label
= "return_label{v.new_number}"
679 v
.nmc
.return_value
= v
.cfc
.get_var
("Method return value and escape marker")
680 if self isa AConcreteInitPropdef then
681 v
.invoke_super_init_calls_after
(null)
683 if n_block
!= null then
684 v
.compile_stmt
(n_block
)
686 v
.add_instr
("{v.nmc.return_label}: while(false);")
687 if self isa AConcreteInitPropdef then
688 v
.add_instr
("init_table[{itpos}] = 1;")
691 var ret
: String = null
692 if method
.signature
.return_type
!= null then
693 ret
= v
.nmc
.return_value
701 redef class ADeferredMethPropdef
702 redef meth do_compile_inside
(v
, method
, params
)
704 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
705 v
.add_instr
(v
.printf_locate_error
(self))
706 v
.add_instr
("nit_exit(1);")
707 if method
.signature
.return_type
!= null then
715 redef class AExternMethPropdef
716 redef meth do_compile_inside
(v
, method
, params
)
718 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
719 if n_extern
!= null then
720 ename
= n_extern
.text
721 ename
= ename
.substring
(1, ename
.length-2
)
723 var sig
= method
.signature
724 if params
.length
!= sig
.arity
+ 1 then
725 printl
("par:{params.length} sig:{sig.arity}")
727 var args
= new Array[String]
728 args
.add
(sig
.recv
.unboxtype
(params
[0]))
729 for i
in [0..sig
.arity
[ do
730 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
732 var s
= "{ename}({args.join(", ")})"
733 if sig
.return_type
!= null then
734 return sig
.return_type
.boxtype
(s
)
742 redef class AInternMethPropdef
743 redef meth do_compile_inside
(v
, method
, p
)
745 var c
= method
.local_class
.name
748 if c
== once
"Int".to_symbol
then
749 if n
== once
"object_id".to_symbol
then
751 else if n
== once
"unary -".to_symbol
then
752 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
753 else if n
== once
"output".to_symbol
then
754 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
755 else if n
== once
"ascii".to_symbol
then
756 s
= "TAG_Char(UNTAG_Int({p[0]}))"
757 else if n
== once
"succ".to_symbol
then
758 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
759 else if n
== once
"prec".to_symbol
then
760 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
761 else if n
== once
"to_f".to_symbol
then
762 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
763 else if n
== once
"+".to_symbol
then
764 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
765 else if n
== once
"-".to_symbol
then
766 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
767 else if n
== once
"*".to_symbol
then
768 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
769 else if n
== once
"/".to_symbol
then
770 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
771 else if n
== once
"%".to_symbol
then
772 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
773 else if n
== once
"<".to_symbol
then
774 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
775 else if n
== once
">".to_symbol
then
776 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
777 else if n
== once
"<=".to_symbol
then
778 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
779 else if n
== once
">=".to_symbol
then
780 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
781 else if n
== once
"lshift".to_symbol
then
782 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
783 else if n
== once
"rshift".to_symbol
then
784 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
785 else if n
== once
"==".to_symbol
then
786 s
= "TAG_Bool(({p[0]})==({p[1]}))"
787 else if n
== once
"!=".to_symbol
then
788 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
790 else if c
== once
"Float".to_symbol
then
791 if n
== once
"object_id".to_symbol
then
792 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
793 else if n
== once
"unary -".to_symbol
then
794 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
795 else if n
== once
"output".to_symbol
then
796 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
797 else if n
== once
"to_i".to_symbol
then
798 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
799 else if n
== once
"+".to_symbol
then
800 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
801 else if n
== once
"-".to_symbol
then
802 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
803 else if n
== once
"*".to_symbol
then
804 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
805 else if n
== once
"/".to_symbol
then
806 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
807 else if n
== once
"<".to_symbol
then
808 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
809 else if n
== once
">".to_symbol
then
810 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
811 else if n
== once
"<=".to_symbol
then
812 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
813 else if n
== once
">=".to_symbol
then
814 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
816 else if c
== once
"Char".to_symbol
then
817 if n
== once
"object_id".to_symbol
then
818 s
= "TAG_Int(UNTAG_Char({p[0]}))"
819 else if n
== once
"unary -".to_symbol
then
820 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
821 else if n
== once
"output".to_symbol
then
822 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
823 else if n
== once
"ascii".to_symbol
then
824 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
825 else if n
== once
"succ".to_symbol
then
826 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
827 else if n
== once
"prec".to_symbol
then
828 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
829 else if n
== once
"to_i".to_symbol
then
830 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
831 else if n
== once
"+".to_symbol
then
832 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
833 else if n
== once
"-".to_symbol
then
834 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
835 else if n
== once
"*".to_symbol
then
836 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
837 else if n
== once
"/".to_symbol
then
838 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
839 else if n
== once
"%".to_symbol
then
840 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
841 else if n
== once
"<".to_symbol
then
842 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
843 else if n
== once
">".to_symbol
then
844 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
845 else if n
== once
"<=".to_symbol
then
846 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
847 else if n
== once
">=".to_symbol
then
848 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
849 else if n
== once
"==".to_symbol
then
850 s
= "TAG_Bool(({p[0]})==({p[1]}))"
851 else if n
== once
"!=".to_symbol
then
852 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
854 else if c
== once
"Bool".to_symbol
then
855 if n
== once
"object_id".to_symbol
then
856 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
857 else if n
== once
"unary -".to_symbol
then
858 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
859 else if n
== once
"output".to_symbol
then
860 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
861 else if n
== once
"ascii".to_symbol
then
862 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
863 else if n
== once
"to_i".to_symbol
then
864 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
865 else if n
== once
"==".to_symbol
then
866 s
= "TAG_Bool(({p[0]})==({p[1]}))"
867 else if n
== once
"!=".to_symbol
then
868 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
870 else if c
== once
"NativeArray".to_symbol
then
871 if n
== once
"object_id".to_symbol
then
872 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
873 else if n
== once
"[]".to_symbol
then
874 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
875 else if n
== once
"[]=".to_symbol
then
876 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
877 else if n
== once
"copy_to".to_symbol
then
878 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
880 else if c
== once
"NativeString".to_symbol
then
881 if n
== once
"object_id".to_symbol
then
882 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
883 else if n
== once
"atoi".to_symbol
then
884 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
885 else if n
== once
"[]".to_symbol
then
886 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
887 else if n
== once
"[]=".to_symbol
then
888 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
889 else if n
== once
"copy_to".to_symbol
then
890 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]}));")
892 else if n
== once
"object_id".to_symbol
then
893 s
= "TAG_Int((bigint){p[0]})"
894 else if n
== once
"sys".to_symbol
then
896 else if n
== once
"is_same_type".to_symbol
then
897 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
898 else if n
== once
"exit".to_symbol
then
899 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
900 else if n
== once
"calloc_array".to_symbol
then
901 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
902 else if n
== once
"calloc_string".to_symbol
then
903 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
906 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
908 if method
.signature
.return_type
!= null and s
== null then
909 s
= "NIT_NULL /*stub*/"
915 ###############################################################################
918 # Compile the node as an expression
919 # Only the visitor should call it
920 meth compile_expr
(v
: CompilerVisitor): String is abstract
922 # Prepare a call of node as a statement
923 # Only the visitor should call it
924 # It's used for local variable managment
925 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
927 # Compile the node as a statement
928 # Only the visitor should call it
929 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
932 redef class ABlockExpr
933 redef meth compile_stmt
(v
)
941 redef class AVardeclExpr
942 redef meth prepare_compile_stmt
(v
)
944 v
.cfc
.register_variable
(variable
)
947 redef meth compile_stmt
(v
)
949 var cname
= v
.cfc
.varname
(variable
)
950 if n_expr
== null then
951 v
.add_instr
("/*{cname} is variable {variable.name}*/")
953 var e
= v
.compile_expr
(n_expr
)
954 v
.add_assignment
(cname
, e
)
959 redef class AReturnExpr
960 redef meth compile_stmt
(v
)
962 if n_expr
!= null then
963 var e
= v
.compile_expr
(n_expr
)
964 v
.add_assignment
(v
.nmc
.return_value
, e
)
966 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
967 v
.add_instr
("goto {v.nmc.return_label};")
971 redef class ABreakExpr
972 redef meth compile_stmt
(v
)
974 if n_expr
!= null then
975 var e
= v
.compile_expr
(n_expr
)
976 v
.add_assignment
(v
.nmc
.break_value
, e
)
978 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
979 v
.add_instr
("goto {v.nmc.break_label};")
983 redef class AContinueExpr
984 redef meth compile_stmt
(v
)
986 if n_expr
!= null then
987 var e
= v
.compile_expr
(n_expr
)
988 v
.add_assignment
(v
.nmc
.continue_value
, e
)
990 v
.add_instr
("goto {v.nmc.continue_label};")
994 redef class AAbortExpr
995 redef meth compile_stmt
(v
)
997 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
1002 redef meth compile_stmt
(v
)
1004 if n_block
!= null then
1005 v
.compile_stmt
(n_block
)
1011 redef meth compile_stmt
(v
)
1013 var e
= v
.compile_expr
(n_expr
)
1014 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1016 if n_then
!= null then
1018 v
.compile_stmt
(n_then
)
1021 if n_else
!= null then
1022 v
.add_instr
("} else \{ /*if*/")
1024 v
.compile_stmt
(n_else
)
1031 redef class AIfexprExpr
1032 redef meth compile_expr
(v
)
1034 var e
= v
.compile_expr
(n_expr
)
1035 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1038 var e
= v
.ensure_var
(v
.compile_expr
(n_then
), "Then value")
1040 v
.add_instr
("} else \{ /*if*/")
1043 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
), "Else value")
1044 v
.add_assignment
(e
, e2
)
1051 redef class AControlableBlock
1052 meth compile_inside_block
(v
: CompilerVisitor) is abstract
1053 redef meth compile_stmt
(v
)
1055 var old_break_label
= v
.nmc
.break_label
1056 var old_continue_label
= v
.nmc
.continue_label
1057 var id
= v
.new_number
1058 v
.nmc
.break_label
= "break_{id}"
1059 v
.nmc
.continue_label
= "continue_{id}"
1061 compile_inside_block
(v
)
1064 v
.nmc
.break_label
= old_break_label
1065 v
.nmc
.continue_label
= old_continue_label
1069 redef class AWhileExpr
1070 redef meth compile_inside_block
(v
)
1072 v
.add_instr
("while (true) \{ /*while*/")
1074 var e
= v
.compile_expr
(n_expr
)
1075 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
1077 if n_block
!= null then
1078 v
.compile_stmt
(n_block
)
1080 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1083 v
.add_instr
("{v.nmc.break_label}: while(0);")
1087 redef class AForExpr
1088 redef meth compile_inside_block
(v
)
1090 var e
= v
.compile_expr
(n_expr
)
1091 var ittype
= meth_iterator
.signature
.return_type
1093 var iter
= v
.cfc
.get_var
("For iterator")
1094 v
.add_assignment
(iter
, meth_iterator
.compile_call
(v
, [e
]))
1095 v
.add_instr
("while (true) \{ /*for*/")
1097 var ok
= v
.cfc
.get_var
("For 'is_ok' result")
1098 v
.add_assignment
(ok
, meth_is_ok
.compile_call
(v
, [iter
]))
1099 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1101 var e
= meth_item
.compile_call
(v
, [iter
])
1102 e
= v
.ensure_var
(e
, "For item")
1103 var cname
= v
.cfc
.register_variable
(variable
)
1104 v
.add_assignment
(cname
, e
)
1105 var n_block
= n_block
1106 if n_block
!= null then
1107 v
.compile_stmt
(n_block
)
1109 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1110 e
= meth_next
.compile_call
(v
, [iter
])
1114 v
.add_instr
("{v.nmc.break_label}: while(0);")
1118 redef class AAssertExpr
1119 redef meth compile_stmt
(v
)
1121 var e
= v
.compile_expr
(n_expr
)
1123 if n_id
!= null then
1124 s
= " '{n_id.text}' "
1126 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1130 redef class AVarExpr
1131 redef meth compile_expr(v)
1133 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1137 redef class AVarAssignExpr
1138 redef meth compile_stmt(v)
1140 var e = v.compile_expr(n_value)
1141 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1145 redef class AVarReassignExpr
1146 redef meth compile_stmt(v)
1148 var e1 = v.cfc.varname(variable)
1149 var e2 = v.compile_expr(n_value)
1150 var e3 = assign_method.compile_call(v, [e1, e2])
1151 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1155 redef class ASelfExpr
1156 redef meth compile_expr(v)
1158 return v.cfc.varname(v.nmc.method_params[0])
1163 redef meth compile_expr(v)
1165 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'or' operand
")
1166 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1169 var e2 = v.compile_expr(n_expr2)
1170 v.add_assignment(e, e2)
1177 redef class AAndExpr
1178 redef meth compile_expr(v)
1180 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'and' operand
")
1181 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1184 var e2 = v.compile_expr(n_expr2)
1185 v.add_assignment(e, e2)
1192 redef class ANotExpr
1193 redef meth compile_expr(v)
1195 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1200 redef meth compile_expr(v)
1202 var e = v.compile_expr(n_expr)
1203 var e2 = v.compile_expr(n_expr2)
1204 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1208 redef class AIsaExpr
1209 redef meth compile_expr(v)
1211 var e = v.compile_expr(n_expr)
1212 return n_type.stype.compile_cast(v, e)
1216 redef class AAsCastExpr
1217 redef meth compile_expr(v)
1219 var e = v.compile_expr(n_expr)
1220 n_type.stype.compile_type_check(v, e, self)
1225 redef class ATrueExpr
1226 redef meth compile_expr(v)
1228 return " TAG_Bool(true)"
1232 redef class AFalseExpr
1233 redef meth compile_expr(v)
1235 return " TAG_Bool(false)"
1239 redef class AIntExpr
1240 redef meth compile_expr(v)
1242 return " TAG_Int({n_number.text})"
1246 redef class AFloatExpr
1247 redef meth compile_expr(v)
1249 return "BOX_Float({n_float.text})"
1253 redef class ACharExpr
1254 redef meth compile_expr(v)
1256 return " TAG_Char({n_char.text})"
1260 redef class AStringFormExpr
1261 redef meth compile_expr(v)
1264 var i = v.new_number
1265 var cvar = v.cfc.get_var("Once String constant
")
1266 v.add_decl("static val_t once_value_
{i} = NIT_NULL; /* Once value
for string
{cvar}*/")
1267 v.add_instr("if (once_value_
{i} != NIT_NULL) {cvar} = once_value_
{i};")
1268 v.add_instr("else \
{")
1270 v.cfc.free_var(cvar)
1271 var e = meth_with_native.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1272 v.add_assignment(cvar, e)
1273 v.add_instr("once_value_
{i} = {cvar};")
1279 # The raw string value
1280 protected meth string_text: String is abstract
1282 # The string in a C native format
1283 protected attr _cstring: String
1285 # The string length in bytes
1286 protected attr _cstring_length: Int
1288 # Compute _cstring and _cstring_length using string_text
1289 protected meth compute_string_info
1292 var str = string_text
1293 var res = new Buffer
1295 while i < str.length do
1300 if c2 != '{' and c2 != '}' then
1310 _cstring_length = len
1314 redef class AStringExpr
1315 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1317 redef class AStartStringExpr
1318 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1320 redef class AMidStringExpr
1321 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1323 redef class AEndStringExpr
1324 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1327 redef class ASuperstringExpr
1328 redef meth compile_expr(v)
1330 var array = meth_with_capacity.compile_constructor_call(v, atype, ["TAG_Int({n_exprs.length})"])
1331 array = v.ensure_var(array, "Array (for super-string
)")
1333 for ne in n_exprs do
1334 var e = v.ensure_var(v.compile_expr(ne), "super-string element
")
1335 if ne.stype != stype then
1337 e = meth_to_s.compile_call(v, [e])
1340 meth_add.compile_call(v, [array, e])
1343 return meth_to_s.compile_call(v, [array])
1347 redef class ANullExpr
1348 redef meth compile_expr(v)
1350 return " NIT_NULL /*null*/"
1354 redef class AArrayExpr
1355 redef meth compile_expr(v)
1357 var recv = meth_with_capacity.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1358 recv = v.ensure_var(recv, "Literal array
")
1360 for ne in n_exprs do
1361 var e = v.compile_expr(ne)
1362 meth_add.compile_call(v, [recv, e])
1368 redef class ARangeExpr
1369 redef meth compile_expr(v)
1371 var e = v.compile_expr(n_expr)
1372 var e2 = v.compile_expr(n_expr2)
1373 return meth_init.compile_constructor_call(v, stype, [e, e2])
1377 redef class ASuperExpr
1378 redef meth compile_stmt(v)
1380 var e = compile_expr(v)
1381 if e != null then v.add_instr("{e};")
1384 redef meth compile_expr(v)
1386 var arity = v.nmc.method_params.length - 1
1387 if init_in_superclass != null then
1388 arity = init_in_superclass.signature.arity
1390 var args = new Array[String].with_capacity(arity + 1)
1391 args.add(v.cfc.varname(v.nmc.method_params[0]))
1392 if n_args.length != arity then
1393 for i in [0..arity[ do
1394 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1398 args.add(v.compile_expr(na))
1401 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1402 if init_in_superclass != null then
1403 return init_in_superclass.compile_call(v, args)
1405 if prop.global.is_init then args.add("init_table
")
1406 return prop.compile_super_call(v, args)
1411 redef class AAttrExpr
1412 redef meth compile_expr(v)
1414 var e = v.compile_expr(n_expr)
1415 return prop.compile_access(v, e)
1419 redef class AAttrAssignExpr
1420 redef meth compile_stmt(v)
1422 var e = v.compile_expr(n_expr)
1423 var e2 = v.compile_expr(n_value)
1424 v.add_assignment(prop.compile_access(v, e), e2)
1427 redef class AAttrReassignExpr
1428 redef meth compile_stmt(v)
1430 var e1 = v.compile_expr(n_expr)
1431 var e2 = prop.compile_access(v, e1)
1432 var e3 = v.compile_expr(n_value)
1433 var e4 = assign_method.compile_call(v, [e2, e3])
1434 v.add_assignment(e2, e4)
1438 redef class ASendExpr
1439 redef meth compile_expr(v)
1441 var recv = v.compile_expr(n_expr)
1442 var cargs = new Array[String]
1444 for a in arguments do
1445 cargs.add(v.compile_expr(a))
1449 if prop_signature.closures.is_empty then
1450 e = prop.compile_call(v, cargs)
1452 e = prop.compile_call_and_closures(v, cargs, closure_defs)
1455 if prop.global.is_init then
1456 v.invoke_super_init_calls_after(prop)
1461 redef meth compile_stmt(v)
1463 var e = compile_expr(v)
1465 v.add_instr(e + ";")
1470 redef class ASendReassignExpr
1471 redef meth compile_expr(v)
1473 var recv = v.compile_expr(n_expr)
1474 var cargs = new Array[String]
1476 for a in arguments do
1477 cargs.add(v.compile_expr(a))
1480 var e2 = read_prop.compile_call(v, cargs)
1481 var e3 = v.compile_expr(n_value)
1482 var e4 = assign_method.compile_call(v, [e2, e3])
1484 return prop.compile_call(v, cargs)
1488 redef class ANewExpr
1489 redef meth compile_expr(v)
1491 var cargs = new Array[String]
1492 for a in arguments do
1493 cargs.add(v.compile_expr(a))
1495 return prop.compile_constructor_call(v, stype, cargs)
1499 redef class PClosureDef
1500 # Compile the closure definition as a function in v.out_contexts
1501 # Return the cname of the function
1502 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1504 # Compile the closure definition inside the current C function.
1505 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1508 redef class AClosureDef
1509 # The cname of the function
1510 readable attr _cname: String
1512 redef meth compile_closure(v, closcn)
1515 v.ctx = new CContext
1516 v.out_contexts.add(v.ctx)
1518 var cfc_old = v.cfc.closure
1519 v.cfc.closure = v.nmc
1521 var old_rv = v.nmc.return_value
1522 var old_bv = v.nmc.break_value
1523 if cfc_old == null then
1524 v.nmc.return_value = "closctx-
>{old_rv}"
1525 v.nmc.break_value = "closctx-
>{old_bv}"
1528 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1530 var args = new Array[String]
1531 for i in [0..closure.signature.arity[ do
1532 args.add(" param
{i}")
1535 var cs = decl_csignature(v, args, closcn)
1537 v.add_instr("{cs} \
{")
1539 var ctx_old2 = v.ctx
1540 v.ctx = new CContext
1542 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1543 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1545 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1546 var s = do_compile_inside(v, args)
1548 v.add_instr("{v.nmc.return_label}:")
1549 v.add_instr("tracehead
= trace
.prev
;")
1551 v.add_instr("return;")
1553 v.add_instr("return {s};")
1556 ctx_old2.append(v.ctx)
1562 v.cfc.closure = cfc_old
1563 v.nmc.return_value = old_rv
1564 v.nmc.break_value = old_bv
1567 var closcnv = "wbclos
{v.new_number}"
1568 v.add_decl("struct
WBT_ {closcnv};")
1569 v.add_instr("{closcnv}.fun = (fun_t
){cname};")
1570 v.add_instr("{closcnv}.has_broke
= NULL;")
1571 if cfc_old != null then
1572 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1573 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1575 v.add_instr("{closcnv}.variable
= variable
;")
1576 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1579 return "(&{closcnv})"
1582 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1584 var params = new Array[String]
1585 params.add("struct
WBT_ *closctx
")
1586 for i in [0..closure.signature.arity[ do
1587 var p = "val_t
{args[i]}"
1591 if closure.signature.return_type != null then
1596 var p = params.join(", ")
1597 var s = "{ret} {cname}({p})"
1598 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1603 redef meth do_compile_inside(v, params)
1605 for i in [0..variables.length[ do
1606 var vacname = v.cfc.register_variable(variables[i])
1607 v.add_assignment(vacname, params[i])
1610 var old_cv = v.nmc.continue_value
1611 var old_cl = v.nmc.continue_label
1612 var old_bl = v.nmc.break_label
1614 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1615 v.nmc.continue_label = "continue_label
{v.new_number}"
1616 v.nmc.break_label = v.nmc.return_label
1618 if n_expr != null then v.compile_stmt(n_expr)
1620 v.add_instr("{v.nmc.continue_label}: while(false);")
1622 var ret: String = null
1623 if closure.signature.return_type != null then ret = v.nmc.continue_value
1625 v.nmc.continue_value = old_cv
1626 v.nmc.continue_label = old_cl
1627 v.nmc.break_label = old_bl
1633 redef class PClosureDecl
1634 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1636 redef class AClosureDecl
1637 redef meth do_compile_inside(v, params)
1639 if n_signature != null then n_signature.compile_parameters(v, variable.closure.signature, params)
1641 var old_cv = v.nmc.continue_value
1642 var old_cl = v.nmc.continue_label
1643 var old_bl = v.nmc.break_label
1645 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1646 v.nmc.continue_label = "continue_label
{v.new_number}"
1647 v.nmc.break_label = v.nmc.return_label
1649 if n_expr != null then v.compile_stmt(n_expr)
1651 v.add_instr("{v.nmc.continue_label}: while(false);")
1653 var ret: String = null
1654 if variable.closure.signature.return_type != null then ret = v.nmc.continue_value
1656 v.nmc.continue_value = old_cv
1657 v.nmc.continue_label = old_cl
1658 v.nmc.break_label = old_bl
1664 redef class AClosureCallExpr
1665 redef meth compile_expr(v)
1667 var cargs = new Array[String]
1668 for a in arguments do cargs.add(v.compile_expr(a))
1669 var va: String = null
1670 if variable.closure.signature.return_type != null then va = v.cfc.get_var("Closure call result value
")
1672 if variable.closure.is_optional then
1673 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1675 var n = variable.decl
1676 assert n isa AClosureDecl
1677 var s = n.do_compile_inside(v, cargs)
1678 if s != null then v.add_assignment(va, s)
1680 v.add_instr("} else \
{")
1684 var ivar = v.cfc.varname(variable)
1686 cargs2.append(cargs)
1687 var s = "(({variable.ctypename})({ivar}->fun))({cargs2.join(", ")}) /* Invoke closure
{variable} */"
1689 v.add_assignment(va, s)
1693 v.add_instr("if ({ivar}->has_broke
) \
{")
1695 if n_closure_defs != null and n_closure_defs.length == 1 then do
1696 n_closure_defs.first.do_compile_inside(v, null)
1698 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
;\
}")
1699 v.add_instr("goto
{v.nmc.return_label};")
1703 if variable.closure.is_optional then
1711 redef class AProxyExpr
1712 redef meth compile_expr(v)
1714 return v.compile_expr(n_expr)
1718 redef class AOnceExpr
1719 redef meth compile_expr(v)
1721 var i = v.new_number
1722 var cvar = v.cfc.get_var("Once expression result
")
1723 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1724 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1725 v.add_instr("else \
{")
1727 v.cfc.free_var(cvar)
1728 var e = v.compile_expr(n_expr)
1729 v.add_assignment(cvar, e)
1730 v.add_instr("once_value_
{i} = {cvar};")
1731 v.add_instr("once_bool_
{i} = true;")