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
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 s
.substring
(0,3) == "variable" then
55 var v
= cfc
.get_var
(null)
56 add_assignment
(v
, "{s} /* Ensure var: {comment}*/")
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 index
155 attr _varindexes
: Map[Variable, Int] = new HashMap[Variable, Int]
157 # Are we currenlty in a closure definition?
158 readable writable attr _closure
: NitMethodContext = null
160 # Return the cvariable of a Nit variable
161 meth varname
(v
: Variable): String
163 if v
isa ClosureVariable then
164 return closure_variable
(_varindexes
[v
])
166 return variable
(_varindexes
[v
])
170 # Return the next available variable
171 meth get_var
(comment
: String): String
173 var v
= variable
(_variable_index
)
174 _variable_index
= _variable_index
+ 1
175 if _variable_index
> _variable_index_max
then
176 _variable_index_max
= _variable_index
178 if comment
!= null then
179 visitor
.add_instr
("/* Register {v}: {comment} */")
184 meth register_variable
(v
: Variable): String
186 _varindexes
[v
] = _variable_index
187 var s
= get_var
("Local variable")
191 # Next available closure variable number
192 attr _closurevariable_index
: Int = 0
194 meth register_closurevariable
(v
: ClosureVariable): String
196 var s
= "closurevariable[{_closurevariable_index}]"
197 _varindexes
[v
] = _closurevariable_index
198 _closurevariable_index
+= 1
199 if _closure
!= null then
200 return "(closctx->{s})"
206 # Return the ith cvariable
207 protected meth variable
(i
: Int): String
209 if closure
!= null then
210 var vn
= once
new Array[String]
211 if vn
.length
<= i
then
212 for j
in [vn
.length
..i
] do
213 vn
[j
] = "(closctx->variable[{j}])"
218 var vn
= once
new Array[String]
219 if vn
.length
<= i
then
220 for j
in [vn
.length
..i
] do
221 vn
[j
] = "variable[{j}]"
228 # Return the ith closurevariable
229 protected meth closure_variable
(i
: Int): String
231 if closure
!= null then
232 return "(closctx->closurevariable[{i}])"
234 return "closurevariable[{i}]"
238 # Mark the variable available
239 meth free_var
(v
: String)
242 if v
== variable
(_variable_index-1
) then
243 _variable_index
= _variable_index
- 1
247 # Generate the local variable declarations
248 # To use at the end of the C function once all variables are known
249 meth generate_var_decls
251 if _variable_index_max
> 0 then
252 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
254 visitor
.add_decl
("val_t *variable = NULL;")
256 if _closurevariable_index
> 0 then
257 visitor
.add_decl
("struct WBT_ *closurevariable[{_closurevariable_index}];")
259 visitor
.add_decl
("struct WBT_ **closurevariable = NULL;")
263 init(v
: CompilerVisitor) do _visitor
= v
266 # A Nit method currenlty compiled
267 class NitMethodContext
268 # Current method compiled
269 readable attr _method
: MMSrcMethod
271 # Association between parameters and the corresponding variables
272 readable writable attr _method_params
: Array[ParamVariable]
274 # Where a nit return must branch
275 readable writable attr _return_label
: String
277 # Where a nit break must branch
278 readable writable attr _break_label
: String
280 # Where a nit continue must branch
281 readable writable attr _continue_label
: String
283 # Variable where a functionnal nit return must store its value
284 readable writable attr _return_value
: String
286 # Variable where a functionnal nit break must store its value
287 readable writable attr _break_value
: String
289 # Variable where a functionnal nit continue must store its value
290 readable writable attr _continue_value
: String
292 init(method
: MMSrcMethod)
298 ###############################################################################
300 redef class ClosureVariable
301 readable writable attr _ctypename
: String
305 # Compile a call on self for given arguments
306 # Most calls are compiled with a table access,
307 # primitive calles are inlined
308 # == and != are guarded and possibly inlined
309 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
312 if i
isa MMSrcMethod then
313 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
314 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
316 var e
= i
.do_compile_inside
(v
, cargs
)
320 var ee
= once
"==".to_symbol
321 var ne
= once
"!=".to_symbol
323 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
324 var eqcall
= eqp
.compile_call
(v
, cargs
)
325 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
327 if global
.is_init
then
329 cargs
.add
("init_table /*YYY*/")
332 var m
= "{global.meth_call}({cargs[0]})"
333 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
335 vcall
= "UNTAG_Bool({vcall})"
336 var obj
= once
"Object".to_symbol
337 if i
.local_class
.name
== obj
then
338 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
340 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
342 if signature
.return_type
!= null then
345 v
.add_instr
(vcall
+ ";")
350 # Compile a call on self for given arguments and given closures
351 meth compile_call_and_closures
(v
: CompilerVisitor, cargs
: Array[String], clos_defs
: Array[PClosureDef]): String
353 var ve
: String = null
355 if clos_defs
!= null then arity
= clos_defs
.length
357 # Prepare result value.
358 # In case of procedure, the return value is still used to intercept breaks
359 var old_bv
= v
.nmc
.break_value
360 ve
= v
.cfc
.get_var
("Closure return value and escape marker")
361 v
.nmc
.break_value
= ve
363 # Compile closure to c function
364 var realcargs
= new Array[String] # Args to pass to the C function call
365 var closcns
= new Array[String] # Closure C structure names
366 realcargs
.add_all
(cargs
)
367 for i
in [0..arity
[ do
368 var cn
= clos_defs
[i
].compile_closure
(v
, closure_cname
(i
))
372 for i
in [arity
..signature
.closures
.length
[ do
373 realcargs
.add
("NULL")
376 v
.nmc
.break_value
= old_bv
379 var e
= compile_call
(v
, realcargs
)
381 v
.add_assignment
(ve
, e
)
385 # Intercept returns and breaks
386 for i
in [0..arity
[ do
387 # A break or a return is intercepted
388 v
.add_instr
("if ({closcns[i]}->has_broke != NULL) \{")
390 # A passtrought break or a return is intercepted: go the the next closure
391 v
.add_instr
("if ({closcns[i]}->has_broke != &({ve})) \{")
393 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = {closcns[i]}->has_broke; closctx->broke_value = {closcns[i]}->broke_value;")
394 v
.add_instr
("goto {v.nmc.return_label};")
396 # A direct break is interpected
398 # overwrite the returned value in a function
399 v
.add_instr
("\} else {ve} = {closcns[i]}->broke_value;")
401 # Do nothing in a procedure
410 # Compile a call as constructor with given args
411 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
413 return "NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}) /*new {recvtype}*/"
416 # Compile a call as call-next-method on self with given args
417 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
419 var m
= "{super_meth_call}({cargs[0]})"
420 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
424 # Cname of the i-th closure C struct type
425 protected meth closure_cname
(i
: Int): String
427 return "FWBT_{cname}_{i}"
431 redef class MMAttribute
432 # Compile an acces on selffor a given reciever.
433 # Result is a valid C left-value for assigment
434 meth compile_access
(v
: CompilerVisitor, recv
: String): String
436 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
440 redef class MMLocalProperty
441 # Compile the property as a C property
442 meth compile_property_to_c
(v
: CompilerVisitor) do end
445 redef class MMSrcMethod
447 # Compile and declare the signature to C
448 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
450 var params
= new Array[String]
451 params
.add
("val_t {args[0]}")
452 for i
in [0..signature
.arity
[ do
453 var p
= "val_t {args[i+1]}"
457 var first_closure_index
= signature
.arity
+ 1 # Wich parameter is the first closure
458 for i
in [0..signature
.closures
.length
[ do
459 var closcn
= closure_cname
(i
)
460 var cs
= signature
.closures
[i
].signature
# Closure signature
461 var subparams
= new Array[String] # Parameters of the closure
462 subparams
.add
("struct WBT_ *")
463 for j
in [0..cs
.arity
[ do
468 if cs
.return_type
!= null then r
= "val_t"
469 params
.add
("struct WBT_ *{args[first_closure_index+i]}")
470 v
.add_decl
("typedef {r} (*{closcn})({subparams.join(", ")});")
473 if global
.is_init
then
474 params
.add
("int* init_table")
478 if signature
.return_type
!= null then
484 var p
= params
.join
(", ")
485 var s
= "{ret} {cname}({p})"
486 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
491 redef meth compile_property_to_c
(v
)
493 v
.cfc
= new CFunctionContext(v
)
495 var args
= new Array[String]
497 for i
in [0..signature
.arity
[ do
498 args
.add
(" param{i}")
500 for i
in [0..signature
.closures
.length
[ do
503 var cs
= decl_csignature
(v
, args
)
504 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
506 v
.add_instr
("{cs} \{")
515 if s
.node
!= null then ln
= s
.node
.line_number
516 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
517 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
518 v
.add_instr
("trace.file = LOCATE_{module.name};")
519 var s
= do_compile_inside
(v
, args
)
520 v
.add_instr
("tracehead = trace.prev;")
522 v
.add_instr
("return;")
524 v
.add_instr
("return {s};")
527 v
.cfc
.generate_var_decls
529 ctx_old
.append
(v
.ctx
)
534 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)
537 # Compile the method body inline
538 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
541 redef class MMReadImplementationMethod
542 redef meth do_compile_inside
(v
, params
)
544 return node
.prop
.compile_access
(v
, params
[0])
548 redef class MMWriteImplementationMethod
549 redef meth do_compile_inside
(v
, params
)
551 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
556 redef class MMMethSrcMethod
557 redef meth do_compile_inside
(v
, params
)
559 return node
.do_compile_inside
(v
, self, params
)
563 redef class MMImplicitInit
564 redef meth do_compile_inside
(v
, params
)
566 var f
= params
.length
- unassigned_attributes
.length
567 var recv
= params
.first
568 for sp
in super_inits
do
569 assert sp
isa MMMethod
570 var args_recv
= [recv
]
571 if sp
== super_init
then
572 var args
= new Array[String].with_capacity
(f
)
577 sp
.compile_call
(v
, args
)
579 sp
.compile_call
(v
, args_recv
)
582 for i
in [f
..params
.length
[ do
583 var attribute
= unassigned_attributes
[i-f
]
584 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
591 # Compile a subtype check to self
593 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
595 # Fixme: handle formaltypes
596 var g
= local_class
.global
597 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
600 # Compile a cast assertion
601 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
603 # Fixme: handle formaltypes
604 var g
= local_class
.global
605 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}*/;")
609 ###############################################################################
611 redef class AMethPropdef
612 # Compile the method body
613 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
616 redef class PSignature
617 meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String]) is abstract
620 redef class ASignature
621 redef meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String])
623 for ap
in n_params
do
624 var cname
= v
.cfc
.register_variable
(ap
.variable
)
625 v
.nmc
.method_params
.add
(ap
.variable
)
626 var orig_type
= orig_sig
[ap
.position
]
627 if not orig_type
< ap
.variable
.stype
then
628 # FIXME: do not test always
629 # FIXME: handle formal types
630 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
631 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
], ap
)
633 v
.add_assignment
(cname
, params
[ap
.position
])
635 for i
in [0..n_closure_decls
.length
[ do
636 var wd
= n_closure_decls
[i
]
637 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
638 wd
.variable
.ctypename
= v
.nmc
.method
.closure_cname
(i
)
639 v
.add_assignment
(cname
, "{params[orig_sig.arity + i]}")
644 redef class AConcreteMethPropdef
645 redef meth do_compile_inside
(v
, method
, params
)
648 v
.nmc
= new NitMethodContext(method
)
650 var selfcname
= v
.cfc
.register_variable
(self_var
)
651 v
.add_assignment
(selfcname
, params
[0])
653 v
.nmc
.method_params
= [self_var
]
655 if n_signature
!= null then
656 var orig_meth
: MMLocalProperty = method
.global
.intro
657 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
658 n_signature
.compile_parameters
(v
, orig_sig
, params
)
661 var itpos
: String = null
662 if self isa AConcreteInitPropdef then
663 itpos
= "VAL2OBJ({selfcname})->vft[{method.local_class.global.init_table_pos_id}].i"
664 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
665 v
.add_instr
("if (init_table[{itpos}]) return;")
668 v
.nmc
.return_label
= "return_label{v.new_number}"
669 v
.nmc
.return_value
= v
.cfc
.get_var
("Method return value and escape marker")
670 if self isa AConcreteInitPropdef then
671 v
.invoke_super_init_calls_after
(null)
673 if n_block
!= null then
674 v
.compile_stmt
(n_block
)
676 v
.add_instr
("{v.nmc.return_label}: while(false);")
677 if self isa AConcreteInitPropdef then
678 v
.add_instr
("init_table[{itpos}] = 1;")
681 var ret
: String = null
682 if method
.signature
.return_type
!= null then
683 ret
= v
.nmc
.return_value
691 redef class ADeferredMethPropdef
692 redef meth do_compile_inside
(v
, method
, params
)
694 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
695 v
.add_instr
(v
.printf_locate_error
(self))
696 v
.add_instr
("nit_exit(1);")
697 if method
.signature
.return_type
!= null then
705 redef class AExternMethPropdef
706 redef meth do_compile_inside
(v
, method
, params
)
708 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
709 if n_extern
!= null then
710 ename
= n_extern
.text
711 ename
= ename
.substring
(1, ename
.length-2
)
713 var sig
= method
.signature
714 if params
.length
!= sig
.arity
+ 1 then
715 printl
("par:{params.length} sig:{sig.arity}")
717 var args
= new Array[String]
718 args
.add
(sig
.recv
.unboxtype
(params
[0]))
719 for i
in [0..sig
.arity
[ do
720 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
722 var s
= "{ename}({args.join(", ")})"
723 if sig
.return_type
!= null then
724 return sig
.return_type
.boxtype
(s
)
732 redef class AInternMethPropdef
733 redef meth do_compile_inside
(v
, method
, p
)
735 var c
= method
.local_class
.name
738 if c
== once
"Int".to_symbol
then
739 if n
== once
"object_id".to_symbol
then
741 else if n
== once
"unary -".to_symbol
then
742 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
743 else if n
== once
"output".to_symbol
then
744 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
745 else if n
== once
"ascii".to_symbol
then
746 s
= "TAG_Char(UNTAG_Int({p[0]}))"
747 else if n
== once
"succ".to_symbol
then
748 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
749 else if n
== once
"prec".to_symbol
then
750 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
751 else if n
== once
"to_f".to_symbol
then
752 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
753 else if n
== once
"+".to_symbol
then
754 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
755 else if n
== once
"-".to_symbol
then
756 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
757 else if n
== once
"*".to_symbol
then
758 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
759 else if n
== once
"/".to_symbol
then
760 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
761 else if n
== once
"%".to_symbol
then
762 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
763 else if n
== once
"<".to_symbol
then
764 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
765 else if n
== once
">".to_symbol
then
766 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
767 else if n
== once
"<=".to_symbol
then
768 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
769 else if n
== once
">=".to_symbol
then
770 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
771 else if n
== once
"lshift".to_symbol
then
772 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
773 else if n
== once
"rshift".to_symbol
then
774 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
775 else if n
== once
"==".to_symbol
then
776 s
= "TAG_Bool(({p[0]})==({p[1]}))"
777 else if n
== once
"!=".to_symbol
then
778 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
780 else if c
== once
"Float".to_symbol
then
781 if n
== once
"object_id".to_symbol
then
782 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
783 else if n
== once
"unary -".to_symbol
then
784 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
785 else if n
== once
"output".to_symbol
then
786 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
787 else if n
== once
"to_i".to_symbol
then
788 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
789 else if n
== once
"+".to_symbol
then
790 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
791 else if n
== once
"-".to_symbol
then
792 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
793 else if n
== once
"*".to_symbol
then
794 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
795 else if n
== once
"/".to_symbol
then
796 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
797 else if n
== once
"<".to_symbol
then
798 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
799 else if n
== once
">".to_symbol
then
800 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
801 else if n
== once
"<=".to_symbol
then
802 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
803 else if n
== once
">=".to_symbol
then
804 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
806 else if c
== once
"Char".to_symbol
then
807 if n
== once
"object_id".to_symbol
then
808 s
= "TAG_Int(UNTAG_Char({p[0]}))"
809 else if n
== once
"unary -".to_symbol
then
810 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
811 else if n
== once
"output".to_symbol
then
812 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
813 else if n
== once
"ascii".to_symbol
then
814 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
815 else if n
== once
"succ".to_symbol
then
816 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
817 else if n
== once
"prec".to_symbol
then
818 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
819 else if n
== once
"to_i".to_symbol
then
820 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
821 else if n
== once
"+".to_symbol
then
822 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
823 else if n
== once
"-".to_symbol
then
824 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
825 else if n
== once
"*".to_symbol
then
826 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
827 else if n
== once
"/".to_symbol
then
828 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
829 else if n
== once
"%".to_symbol
then
830 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
831 else if n
== once
"<".to_symbol
then
832 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
833 else if n
== once
">".to_symbol
then
834 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
835 else if n
== once
"<=".to_symbol
then
836 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
837 else if n
== once
">=".to_symbol
then
838 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
839 else if n
== once
"==".to_symbol
then
840 s
= "TAG_Bool(({p[0]})==({p[1]}))"
841 else if n
== once
"!=".to_symbol
then
842 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
844 else if c
== once
"Bool".to_symbol
then
845 if n
== once
"object_id".to_symbol
then
846 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
847 else if n
== once
"unary -".to_symbol
then
848 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
849 else if n
== once
"output".to_symbol
then
850 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
851 else if n
== once
"ascii".to_symbol
then
852 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
853 else if n
== once
"to_i".to_symbol
then
854 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
855 else if n
== once
"==".to_symbol
then
856 s
= "TAG_Bool(({p[0]})==({p[1]}))"
857 else if n
== once
"!=".to_symbol
then
858 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
860 else if c
== once
"NativeArray".to_symbol
then
861 if n
== once
"object_id".to_symbol
then
862 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
863 else if n
== once
"[]".to_symbol
then
864 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
865 else if n
== once
"[]=".to_symbol
then
866 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
867 else if n
== once
"copy_to".to_symbol
then
868 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
870 else if c
== once
"NativeString".to_symbol
then
871 if n
== once
"object_id".to_symbol
then
872 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
873 else if n
== once
"atoi".to_symbol
then
874 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
875 else if n
== once
"[]".to_symbol
then
876 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
877 else if n
== once
"[]=".to_symbol
then
878 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
879 else if n
== once
"copy_to".to_symbol
then
880 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]}));")
882 else if n
== once
"object_id".to_symbol
then
883 s
= "TAG_Int((bigint){p[0]})"
884 else if n
== once
"sys".to_symbol
then
886 else if n
== once
"is_same_type".to_symbol
then
887 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
888 else if n
== once
"exit".to_symbol
then
889 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
890 else if n
== once
"calloc_array".to_symbol
then
891 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
892 else if n
== once
"calloc_string".to_symbol
then
893 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
896 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
898 if method
.signature
.return_type
!= null and s
== null then
899 s
= "NIT_NULL /*stub*/"
905 ###############################################################################
908 # Compile the node as an expression
909 # Only the visitor should call it
910 meth compile_expr
(v
: CompilerVisitor): String is abstract
912 # Prepare a call of node as a statement
913 # Only the visitor should call it
914 # It's used for local variable managment
915 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
917 # Compile the node as a statement
918 # Only the visitor should call it
919 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
922 redef class ABlockExpr
923 redef meth compile_stmt
(v
)
931 redef class AVardeclExpr
932 redef meth prepare_compile_stmt
(v
)
934 v
.cfc
.register_variable
(variable
)
937 redef meth compile_stmt
(v
)
939 var cname
= v
.cfc
.varname
(variable
)
940 if n_expr
== null then
941 v
.add_instr
("/*{cname} is variable {variable.name}*/")
943 var e
= v
.compile_expr
(n_expr
)
944 v
.add_assignment
(cname
, e
)
949 redef class AReturnExpr
950 redef meth compile_stmt
(v
)
952 if n_expr
!= null then
953 var e
= v
.compile_expr
(n_expr
)
954 v
.add_assignment
(v
.nmc
.return_value
, e
)
956 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
957 v
.add_instr
("goto {v.nmc.return_label};")
961 redef class ABreakExpr
962 redef meth compile_stmt
(v
)
964 if n_expr
!= null then
965 var e
= v
.compile_expr
(n_expr
)
966 v
.add_assignment
(v
.nmc
.break_value
, e
)
968 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
969 v
.add_instr
("goto {v.nmc.break_label};")
973 redef class AContinueExpr
974 redef meth compile_stmt
(v
)
976 if n_expr
!= null then
977 var e
= v
.compile_expr
(n_expr
)
978 v
.add_assignment
(v
.nmc
.continue_value
, e
)
980 v
.add_instr
("goto {v.nmc.continue_label};")
984 redef class AAbortExpr
985 redef meth compile_stmt
(v
)
987 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
992 redef meth compile_stmt
(v
)
994 if n_block
!= null then
995 v
.compile_stmt
(n_block
)
1001 redef meth compile_stmt
(v
)
1003 var e
= v
.compile_expr
(n_expr
)
1004 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1006 if n_then
!= null then
1008 v
.compile_stmt
(n_then
)
1011 if n_else
!= null then
1012 v
.add_instr
("} else \{ /*if*/")
1014 v
.compile_stmt
(n_else
)
1021 redef class AIfexprExpr
1022 redef meth compile_expr
(v
)
1024 var e
= v
.compile_expr
(n_expr
)
1025 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1028 var e
= v
.ensure_var
(v
.compile_expr
(n_then
), "Then value")
1030 v
.add_instr
("} else \{ /*if*/")
1033 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
), "Else value")
1034 v
.add_assignment
(e
, e2
)
1041 redef class AControlableBlock
1042 meth compile_inside_block
(v
: CompilerVisitor) is abstract
1043 redef meth compile_stmt
(v
)
1045 var old_break_label
= v
.nmc
.break_label
1046 var old_continue_label
= v
.nmc
.continue_label
1047 var id
= v
.new_number
1048 v
.nmc
.break_label
= "break_{id}"
1049 v
.nmc
.continue_label
= "continue_{id}"
1051 compile_inside_block
(v
)
1054 v
.nmc
.break_label
= old_break_label
1055 v
.nmc
.continue_label
= old_continue_label
1059 redef class AWhileExpr
1060 redef meth compile_inside_block
(v
)
1062 v
.add_instr
("while (true) \{ /*while*/")
1064 var e
= v
.compile_expr
(n_expr
)
1065 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
1067 if n_block
!= null then
1068 v
.compile_stmt
(n_block
)
1070 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1073 v
.add_instr
("{v.nmc.break_label}: while(0);")
1077 redef class AForExpr
1078 redef meth compile_inside_block
(v
)
1080 var e
= v
.compile_expr
(n_expr
)
1081 var ittype
= meth_iterator
.signature
.return_type
1083 var iter
= v
.cfc
.get_var
("For iterator")
1084 v
.add_assignment
(iter
, meth_iterator
.compile_call
(v
, [e
]))
1085 v
.add_instr
("while (true) \{ /*for*/")
1087 var ok
= v
.cfc
.get_var
("For 'is_ok' result")
1088 v
.add_assignment
(ok
, meth_is_ok
.compile_call
(v
, [iter
]))
1089 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1091 var e
= meth_item
.compile_call
(v
, [iter
])
1092 e
= v
.ensure_var
(e
, "For item")
1093 var cname
= v
.cfc
.register_variable
(variable
)
1094 v
.add_assignment
(cname
, e
)
1095 var n_block
= n_block
1096 if n_block
!= null then
1097 v
.compile_stmt
(n_block
)
1099 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1100 e
= meth_next
.compile_call
(v
, [iter
])
1104 v
.add_instr
("{v.nmc.break_label}: while(0);")
1108 redef class AAssertExpr
1109 redef meth compile_stmt
(v
)
1111 var e
= v
.compile_expr
(n_expr
)
1113 if n_id
!= null then
1114 s
= " '{n_id.text}' "
1116 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1120 redef class AVarExpr
1121 redef meth compile_expr(v)
1123 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1127 redef class AVarAssignExpr
1128 redef meth compile_stmt(v)
1130 var e = v.compile_expr(n_value)
1131 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1135 redef class AVarReassignExpr
1136 redef meth compile_stmt(v)
1138 var e1 = v.cfc.varname(variable)
1139 var e2 = v.compile_expr(n_value)
1140 var e3 = assign_method.compile_call(v, [e1, e2])
1141 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1145 redef class ASelfExpr
1146 redef meth compile_expr(v)
1148 return v.cfc.varname(v.nmc.method_params[0])
1153 redef meth compile_expr(v)
1155 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'or' operand
")
1156 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1159 var e2 = v.compile_expr(n_expr2)
1160 v.add_assignment(e, e2)
1167 redef class AAndExpr
1168 redef meth compile_expr(v)
1170 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'and' operand
")
1171 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1174 var e2 = v.compile_expr(n_expr2)
1175 v.add_assignment(e, e2)
1182 redef class ANotExpr
1183 redef meth compile_expr(v)
1185 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1190 redef meth compile_expr(v)
1192 var e = v.compile_expr(n_expr)
1193 var e2 = v.compile_expr(n_expr2)
1194 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1198 redef class AIsaExpr
1199 redef meth compile_expr(v)
1201 var e = v.compile_expr(n_expr)
1202 return n_type.stype.compile_cast(v, e)
1206 redef class AAsCastExpr
1207 redef meth compile_expr(v)
1209 var e = v.compile_expr(n_expr)
1210 n_type.stype.compile_type_check(v, e, self)
1215 redef class ATrueExpr
1216 redef meth compile_expr(v)
1218 return " TAG_Bool(true)"
1222 redef class AFalseExpr
1223 redef meth compile_expr(v)
1225 return " TAG_Bool(false)"
1229 redef class AIntExpr
1230 redef meth compile_expr(v)
1232 return " TAG_Int({n_number.text})"
1236 redef class AFloatExpr
1237 redef meth compile_expr(v)
1239 return "BOX_Float({n_float.text})"
1243 redef class ACharExpr
1244 redef meth compile_expr(v)
1246 return " TAG_Char({n_char.text})"
1250 redef class AStringFormExpr
1251 redef meth compile_expr(v)
1254 var i = v.new_number
1255 var cvar = v.cfc.get_var("Once String constant
")
1256 v.add_decl("static val_t once_value_
{i} = NIT_NULL; /* Once value
for string
{cvar}*/")
1257 v.add_instr("if (once_value_
{i} != NIT_NULL) {cvar} = once_value_
{i};")
1258 v.add_instr("else \
{")
1260 v.cfc.free_var(cvar)
1261 var e = meth_with_native.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1262 v.add_assignment(cvar, e)
1263 v.add_instr("once_value_
{i} = {cvar};")
1269 # The raw string value
1270 protected meth string_text: String is abstract
1272 # The string in a C native format
1273 protected attr _cstring: String
1275 # The string length in bytes
1276 protected attr _cstring_length: Int
1278 # Compute _cstring and _cstring_length using string_text
1279 protected meth compute_string_info
1282 var str = string_text
1283 var res = new Buffer
1285 while i < str.length do
1290 if c2 != '{' and c2 != '}' then
1300 _cstring_length = len
1304 redef class AStringExpr
1305 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1307 redef class AStartStringExpr
1308 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1310 redef class AMidStringExpr
1311 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1313 redef class AEndStringExpr
1314 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1317 redef class ASuperstringExpr
1318 redef meth compile_expr(v)
1320 var array = meth_with_capacity.compile_constructor_call(v, atype, ["TAG_Int({n_exprs.length})"])
1321 array = v.ensure_var(array, "Array (for super-string
)")
1323 for ne in n_exprs do
1324 var e = v.ensure_var(v.compile_expr(ne), "super-string element
")
1325 if ne.stype != stype then
1326 v.add_assignment(e, meth_to_s.compile_call(v, [e]))
1328 meth_add.compile_call(v, [array, e])
1331 return meth_to_s.compile_call(v, [array])
1335 redef class ANullExpr
1336 redef meth compile_expr(v)
1338 return " NIT_NULL /*null*/"
1342 redef class AArrayExpr
1343 redef meth compile_expr(v)
1345 var recv = meth_with_capacity.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1346 recv = v.ensure_var(recv, "Literal array
")
1348 for ne in n_exprs do
1349 var e = v.compile_expr(ne)
1350 meth_add.compile_call(v, [recv, e])
1356 redef class ARangeExpr
1357 redef meth compile_expr(v)
1359 var e = v.compile_expr(n_expr)
1360 var e2 = v.compile_expr(n_expr2)
1361 return meth_init.compile_constructor_call(v, stype, [e, e2])
1365 redef class ASuperExpr
1366 redef meth compile_stmt(v)
1368 var e = compile_expr(v)
1369 if e != null then v.add_instr("{e};")
1372 redef meth compile_expr(v)
1374 var arity = v.nmc.method_params.length - 1
1375 if init_in_superclass != null then
1376 arity = init_in_superclass.signature.arity
1378 var args = new Array[String].with_capacity(arity + 1)
1379 args.add(v.cfc.varname(v.nmc.method_params[0]))
1380 if n_args.length != arity then
1381 for i in [0..arity[ do
1382 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1386 args.add(v.compile_expr(na))
1389 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1390 if init_in_superclass != null then
1391 return init_in_superclass.compile_call(v, args)
1393 if prop.global.is_init then args.add("init_table
")
1394 return prop.compile_super_call(v, args)
1399 redef class AAttrExpr
1400 redef meth compile_expr(v)
1402 var e = v.compile_expr(n_expr)
1403 return prop.compile_access(v, e)
1407 redef class AAttrAssignExpr
1408 redef meth compile_stmt(v)
1410 var e = v.compile_expr(n_expr)
1411 var e2 = v.compile_expr(n_value)
1412 v.add_assignment(prop.compile_access(v, e), e2)
1415 redef class AAttrReassignExpr
1416 redef meth compile_stmt(v)
1418 var e1 = v.compile_expr(n_expr)
1419 var e2 = prop.compile_access(v, e1)
1420 var e3 = v.compile_expr(n_value)
1421 var e4 = assign_method.compile_call(v, [e2, e3])
1422 v.add_assignment(e2, e4)
1426 redef class ASendExpr
1427 redef meth compile_expr(v)
1429 var recv = v.compile_expr(n_expr)
1430 var cargs = new Array[String]
1432 for a in arguments do
1433 cargs.add(v.compile_expr(a))
1437 if prop_signature.closures.is_empty then
1438 e = prop.compile_call(v, cargs)
1440 e = prop.compile_call_and_closures(v, cargs, closure_defs)
1443 if prop.global.is_init then
1444 v.invoke_super_init_calls_after(prop)
1449 redef meth compile_stmt(v)
1451 var e = compile_expr(v)
1453 v.add_instr(e + ";")
1458 redef class ASendReassignExpr
1459 redef meth compile_expr(v)
1461 var recv = v.compile_expr(n_expr)
1462 var cargs = new Array[String]
1464 for a in arguments do
1465 cargs.add(v.compile_expr(a))
1468 var e2 = read_prop.compile_call(v, cargs)
1469 var e3 = v.compile_expr(n_value)
1470 var e4 = assign_method.compile_call(v, [e2, e3])
1472 return prop.compile_call(v, cargs)
1476 redef class ANewExpr
1477 redef meth compile_expr(v)
1479 var cargs = new Array[String]
1480 for a in arguments do
1481 cargs.add(v.compile_expr(a))
1483 return prop.compile_constructor_call(v, stype, cargs)
1487 redef class PClosureDef
1488 # Compile the closure definition as a function in v.out_contexts
1489 # Return the cname of the function
1490 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1492 # Compile the closure definition inside the current C function.
1493 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1496 redef class AClosureDef
1497 # The cname of the function
1498 readable attr _cname: String
1500 redef meth compile_closure(v, closcn)
1503 v.ctx = new CContext
1504 v.out_contexts.add(v.ctx)
1506 var cfc_old = v.cfc.closure
1507 v.cfc.closure = v.nmc
1509 var old_rv = v.nmc.return_value
1510 var old_bv = v.nmc.break_value
1511 if cfc_old == null then
1512 v.nmc.return_value = "closctx-
>{old_rv}"
1513 v.nmc.break_value = "closctx-
>{old_bv}"
1516 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1518 var args = new Array[String]
1519 for i in [0..closure.signature.arity[ do
1520 args.add(" param
{i}")
1523 var cs = decl_csignature(v, args, closcn)
1525 v.add_instr("{cs} \
{")
1527 var ctx_old2 = v.ctx
1528 v.ctx = new CContext
1530 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1531 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1533 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1534 var s = do_compile_inside(v, args)
1536 v.add_instr("{v.nmc.return_label}:")
1537 v.add_instr("tracehead
= trace
.prev
;")
1539 v.add_instr("return;")
1541 v.add_instr("return {s};")
1544 ctx_old2.append(v.ctx)
1550 v.cfc.closure = cfc_old
1551 v.nmc.return_value = old_rv
1552 v.nmc.break_value = old_bv
1555 var closcnv = "wbclos
{v.new_number}"
1556 v.add_decl("struct
WBT_ {closcnv};")
1557 v.add_instr("{closcnv}.fun = (fun_t
){cname};")
1558 v.add_instr("{closcnv}.has_broke
= NULL;")
1559 if cfc_old != null then
1560 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1561 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1563 v.add_instr("{closcnv}.variable
= variable
;")
1564 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1567 return "(&{closcnv})"
1570 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1572 var params = new Array[String]
1573 params.add("struct
WBT_ *closctx
")
1574 for i in [0..closure.signature.arity[ do
1575 var p = "val_t
{args[i]}"
1579 if closure.signature.return_type != null then
1584 var p = params.join(", ")
1585 var s = "{ret} {cname}({p})"
1586 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1591 redef meth do_compile_inside(v, params)
1593 for i in [0..variables.length[ do
1594 var vacname = v.cfc.register_variable(variables[i])
1595 v.add_assignment(vacname, params[i])
1598 var old_cv = v.nmc.continue_value
1599 var old_cl = v.nmc.continue_label
1600 var old_bl = v.nmc.break_label
1602 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1603 v.nmc.continue_label = "continue_label
{v.new_number}"
1604 v.nmc.break_label = v.nmc.return_label
1606 if n_expr != null then v.compile_stmt(n_expr)
1608 v.add_instr("{v.nmc.continue_label}: while(false);")
1610 var ret: String = null
1611 if closure.signature.return_type != null then ret = v.nmc.continue_value
1613 v.nmc.continue_value = old_cv
1614 v.nmc.continue_label = old_cl
1615 v.nmc.break_label = old_bl
1621 redef class PClosureDecl
1622 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1624 redef class AClosureDecl
1625 redef meth do_compile_inside(v, params)
1627 if n_signature != null then n_signature.compile_parameters(v, variable.closure.signature, params)
1629 var old_cv = v.nmc.continue_value
1630 var old_cl = v.nmc.continue_label
1631 var old_bl = v.nmc.break_label
1633 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1634 v.nmc.continue_label = "continue_label
{v.new_number}"
1635 v.nmc.break_label = v.nmc.return_label
1637 if n_expr != null then v.compile_stmt(n_expr)
1639 v.add_instr("{v.nmc.continue_label}: while(false);")
1641 var ret: String = null
1642 if variable.closure.signature.return_type != null then ret = v.nmc.continue_value
1644 v.nmc.continue_value = old_cv
1645 v.nmc.continue_label = old_cl
1646 v.nmc.break_label = old_bl
1652 redef class AClosureCallExpr
1653 redef meth compile_expr(v)
1655 var cargs = new Array[String]
1656 for a in arguments do cargs.add(v.compile_expr(a))
1657 var va: String = null
1658 if variable.closure.signature.return_type != null then va = v.cfc.get_var("Closure call result value
")
1660 if variable.closure.is_optional then
1661 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1663 var n = variable.decl
1664 assert n isa AClosureDecl
1665 var s = n.do_compile_inside(v, cargs)
1666 if s != null then v.add_assignment(va, s)
1668 v.add_instr("} else \
{")
1672 var ivar = v.cfc.varname(variable)
1674 cargs2.append(cargs)
1675 var s = "(({variable.ctypename})({ivar}->fun))({cargs2.join(", ")}) /* Invoke closure
{variable} */"
1677 v.add_assignment(va, s)
1681 v.add_instr("if ({ivar}->has_broke
) \
{")
1683 if n_closure_defs != null and n_closure_defs.length == 1 then do
1684 n_closure_defs.first.do_compile_inside(v, null)
1686 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
;\
}")
1687 v.add_instr("goto
{v.nmc.return_label};")
1691 if variable.closure.is_optional then
1699 redef class AProxyExpr
1700 redef meth compile_expr(v)
1702 return v.compile_expr(n_expr)
1706 redef class AOnceExpr
1707 redef meth compile_expr(v)
1709 var i = v.new_number
1710 var cvar = v.cfc.get_var("Once expression result
")
1711 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1712 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1713 v.add_instr("else \
{")
1715 v.cfc.free_var(cvar)
1716 var e = v.compile_expr(n_expr)
1717 v.add_assignment(cvar, e)
1718 v.add_instr("once_value_
{i} = {cvar};")
1719 v.add_instr("once_bool_
{i} = true;")