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 if n
== null then return
28 #add_instr("/* Compile stmt {n.locate} */")
29 n
.prepare_compile_stmt
(self)
30 var i
= cfc
._variable_index
32 cfc
._variable_index
= i
35 # Compile is expression node
36 meth compile_expr
(n
: PExpr): String
38 #add_instr("/* Compile expr {n.locate} */")
39 var i
= cfc
._variable_index
40 var s
= n
.compile_expr
(self)
41 cfc
._variable_index
= i
42 if s
[0] == ' ' or cfc
.is_valid_variable
(s
) then
45 var v
= cfc
.get_var
("Result")
50 # Ensure that a c expression is a var
51 meth ensure_var
(s
: String, comment
: String): String
53 if cfc
.is_valid_variable
(s
) then
54 add_instr
("/* Ensure var {s}: {comment}*/")
57 var v
= cfc
.get_var
(null)
58 add_assignment
(v
, "{s} /* Ensure var: {comment}*/")
62 # Add a assignment between a variable and an expression
63 meth add_assignment
(v
: String, s
: String)
66 add_instr
("{v} = {s};")
70 readable writable attr _cfc
: CFunctionContext
72 readable writable attr _nmc
: NitMethodContext
74 # C outputs written outside the current C function.
75 readable writable attr _out_contexts
: Array[CContext] = new Array[CContext]
77 # Generate an fprintf to display an error location
78 meth printf_locate_error
(node
: PNode): String
80 var s
= new Buffer.from
("fprintf(stderr, \"")
81 if nmc != null then s.append(" in %s
")
82 s.append(" (%s
:%d
)\\n\
", ")
83 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
84 s
.append
("LOCATE_{module.name}, {node.line_number});")
88 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
90 var n
= nmc
.method
.node
91 assert n
isa AConcreteInitPropdef
93 if n
.super_init_calls
.is_empty
then return
97 if start_prop
!= null then
98 while n
.super_init_calls
[i
] != start_prop
do
99 #s.append(" {n.super_init_calls[i]}")
103 #s.append(" {start_prop}")
105 while n
.explicit_super_init_calls
[j
] != start_prop
do
110 var stop_prop
: MMMethod = null
111 if j
< n
.explicit_super_init_calls
.length
then
112 stop_prop
= n
.explicit_super_init_calls
[j
]
114 var l
= n
.super_init_calls
.length
117 var p
= n
.super_init_calls
[i
]
118 if p
== stop_prop
then break
119 var cargs
= new Array[String]
120 if p
.signature
.arity
== 0 then
121 cargs
.add
(cfc
.varname
(nmc
.method_params
[0]))
123 for va
in nmc
.method_params
do
124 cargs
.add
(cfc
.varname
(va
))
128 p
.compile_stmt_call
(self, cargs
)
133 # s.append(" {n.super_init_calls[i]}")
136 #if stop_prop != null then s.append(" (stop at {stop_prop})")
137 #n.printl("implicit calls in {n.method}: {s}")
141 # A C function currently written
142 class CFunctionContext
143 readable attr _visitor
: CompilerVisitor
145 # Next available variable number
146 attr _variable_index
: Int = 0
148 # Total number of variable
149 attr _variable_index_max
: Int = 0
151 # Association between nit variable and the corrsponding c variable index
152 attr _varindexes
: Map[Variable, Int] = new HashMap[Variable, Int]
154 # Are we currenlty in a closure definition?
155 readable writable attr _closure
: NitMethodContext = null
157 # Return the cvariable of a Nit variable
158 meth varname
(v
: Variable): String
160 if v
isa ClosureVariable then
161 return closure_variable
(_varindexes
[v
])
163 return variable
(_varindexes
[v
])
167 # Return the next available variable
168 meth get_var
(comment
: String): String
170 var v
= variable
(_variable_index
)
171 _variable_index
= _variable_index
+ 1
172 if _variable_index
> _variable_index_max
then
173 _variable_index_max
= _variable_index
175 if comment
!= null then
176 visitor
.add_instr
("/* Register {v}: {comment} */")
181 meth register_variable
(v
: Variable): String
183 _varindexes
[v
] = _variable_index
184 var s
= get_var
("Local variable")
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 _varindexes
[v
] = _closurevariable_index
195 _closurevariable_index
+= 1
196 if _closure
!= null then
197 return "(closctx->{s})"
203 # Return the ith cvariable
204 protected meth variable
(i
: Int): String
206 if closure
!= null then
207 var vn
= once
new Array[String]
208 if vn
.length
<= i
then
209 for j
in [vn
.length
..i
] do
210 vn
[j
] = "(closctx->variable[{j}])"
215 var vn
= once
new Array[String]
216 if vn
.length
<= i
then
217 for j
in [vn
.length
..i
] do
218 vn
[j
] = "variable[{j}]"
225 # Return the ith closurevariable
226 protected meth closure_variable
(i
: Int): String
228 if closure
!= null then
229 return "(closctx->closurevariable[{i}])"
231 return "closurevariable[{i}]"
235 # Is s a valid variable
236 protected meth is_valid_variable
(s
: String): Bool
238 for i
in [0.._variable_index
[ do
239 if s
== variable
(i
) then return true
244 # Mark the variable available
245 meth free_var
(v
: String)
248 if v
== variable
(_variable_index-1
) then
249 _variable_index
= _variable_index
- 1
253 # Generate the local variable declarations
254 # To use at the end of the C function once all variables are known
255 meth generate_var_decls
257 if _variable_index_max
> 0 then
258 visitor
.add_decl
("val_t variable[{_variable_index_max}];")
260 visitor
.add_decl
("val_t *variable = NULL;")
262 if _closurevariable_index
> 0 then
263 visitor
.add_decl
("struct WBT_ *closurevariable[{_closurevariable_index}];")
265 visitor
.add_decl
("struct WBT_ **closurevariable = NULL;")
269 init(v
: CompilerVisitor) do _visitor
= v
272 # A Nit method currenlty compiled
273 class NitMethodContext
274 # Current method compiled
275 readable attr _method
: MMSrcMethod
277 # Association between parameters and the corresponding variables
278 readable writable attr _method_params
: Array[ParamVariable]
280 # Where a nit return must branch
281 readable writable attr _return_label
: String
283 # Where a nit break must branch
284 readable writable attr _break_label
: String
286 # Where a nit continue must branch
287 readable writable attr _continue_label
: String
289 # Variable where a functionnal nit return must store its value
290 readable writable attr _return_value
: String
292 # Variable where a functionnal nit break must store its value
293 readable writable attr _break_value
: String
295 # Variable where a functionnal nit continue must store its value
296 readable writable attr _continue_value
: String
298 init(method
: MMSrcMethod)
304 ###############################################################################
306 redef class ClosureVariable
307 readable writable attr _ctypename
: String
311 # Compile as an expression.
312 # require that signature.return_type != null
313 meth compile_expr_call
(v
: CompilerVisitor, cargs
: Array[String]): String
315 assert signature
.return_type
!= null
316 var s
= intern_compile_call
(v
, cargs
)
321 # Compile as a statement.
322 # require that signature.return_type == null
323 meth compile_stmt_call
(v
: CompilerVisitor, cargs
: Array[String])
325 assert signature
.return_type
== null
326 var s
= intern_compile_call
(v
, cargs
)
330 # Compile a call on self for given arguments
331 # Most calls are compiled with a table access,
332 # primitive calles are inlined
333 # == and != are guarded and possibly inlined
334 private meth intern_compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
337 if i
isa MMSrcMethod then
338 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
339 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
341 var e
= i
.do_compile_inside
(v
, cargs
)
345 var ee
= once
"==".to_symbol
346 var ne
= once
"!=".to_symbol
348 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
349 var eqcall
= eqp
.compile_expr_call
(v
, cargs
)
350 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
352 if global
.is_init
then
354 cargs
.add
("init_table /*YYY*/")
357 var m
= "{global.meth_call}({cargs[0]})"
358 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
360 vcall
= "UNTAG_Bool({vcall})"
361 var obj
= once
"Object".to_symbol
362 if i
.local_class
.name
== obj
then
363 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
365 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
367 if signature
.return_type
!= null then
370 v
.add_instr
(vcall
+ ";")
375 # Compile a call on self for given arguments and given closures
376 meth compile_call_and_closures
(v
: CompilerVisitor, cargs
: Array[String], clos_defs
: Array[PClosureDef]): String
378 var ve
: String = null
380 if clos_defs
!= null then arity
= clos_defs
.length
382 # Prepare result value.
383 # In case of procedure, the return value is still used to intercept breaks
384 var old_bv
= v
.nmc
.break_value
385 ve
= v
.cfc
.get_var
("Closure return value and escape marker")
386 v
.nmc
.break_value
= ve
388 # Compile closure to c function
389 var realcargs
= new Array[String] # Args to pass to the C function call
390 var closcns
= new Array[String] # Closure C structure names
391 realcargs
.add_all
(cargs
)
392 for i
in [0..arity
[ do
393 var cn
= clos_defs
[i
].compile_closure
(v
, closure_cname
(i
))
397 for i
in [arity
..signature
.closures
.length
[ do
398 realcargs
.add
("NULL")
401 v
.nmc
.break_value
= old_bv
404 var e
= intern_compile_call
(v
, realcargs
)
406 v
.add_assignment
(ve
, e
)
410 # Intercept returns and breaks
411 for i
in [0..arity
[ do
412 # A break or a return is intercepted
413 v
.add_instr
("if ({closcns[i]}->has_broke != NULL) \{")
415 # A passtrought break or a return is intercepted: go the the next closure
416 v
.add_instr
("if ({closcns[i]}->has_broke != &({ve})) \{")
418 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = {closcns[i]}->has_broke; closctx->broke_value = {closcns[i]}->broke_value;")
419 v
.add_instr
("goto {v.nmc.return_label};")
421 # A direct break is interpected
423 # overwrite the returned value in a function
424 v
.add_instr
("\} else {ve} = {closcns[i]}->broke_value;")
426 # Do nothing in a procedure
435 # Compile a call as constructor with given args
436 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
438 return "NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}) /*new {recvtype}*/"
441 # Compile a call as call-next-method on self with given args
442 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
444 var m
= "{super_meth_call}({cargs[0]})"
445 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
449 # Cname of the i-th closure C struct type
450 protected meth closure_cname
(i
: Int): String
452 return "FWBT_{cname}_{i}"
456 redef class MMAttribute
457 # Compile an acces on selffor a given reciever.
458 # Result is a valid C left-value for assigment
459 meth compile_access
(v
: CompilerVisitor, recv
: String): String
461 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
465 redef class MMLocalProperty
466 # Compile the property as a C property
467 meth compile_property_to_c
(v
: CompilerVisitor) do end
470 redef class MMSrcMethod
472 # Compile and declare the signature to C
473 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
475 var params
= new Array[String]
476 params
.add
("val_t {args[0]}")
477 for i
in [0..signature
.arity
[ do
478 var p
= "val_t {args[i+1]}"
482 var first_closure_index
= signature
.arity
+ 1 # Wich parameter is the first closure
483 for i
in [0..signature
.closures
.length
[ do
484 var closcn
= closure_cname
(i
)
485 var cs
= signature
.closures
[i
].signature
# Closure signature
486 var subparams
= new Array[String] # Parameters of the closure
487 subparams
.add
("struct WBT_ *")
488 for j
in [0..cs
.arity
[ do
493 if cs
.return_type
!= null then r
= "val_t"
494 params
.add
("struct WBT_ *{args[first_closure_index+i]}")
495 v
.add_decl
("typedef {r} (*{closcn})({subparams.join(", ")});")
498 if global
.is_init
then
499 params
.add
("int* init_table")
503 if signature
.return_type
!= null then
509 var p
= params
.join
(", ")
510 var s
= "{ret} {cname}({p})"
511 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
516 redef meth compile_property_to_c
(v
)
518 v
.cfc
= new CFunctionContext(v
)
520 var args
= new Array[String]
522 for i
in [0..signature
.arity
[ do
523 args
.add
(" param{i}")
525 for i
in [0..signature
.closures
.length
[ do
528 var cs
= decl_csignature
(v
, args
)
529 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
531 v
.add_instr
("{cs} \{")
540 if s
.node
!= null then ln
= s
.node
.line_number
541 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
542 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
543 v
.add_instr
("trace.file = LOCATE_{module.name};")
544 var s
= do_compile_inside
(v
, args
)
545 v
.add_instr
("tracehead = trace.prev;")
547 v
.add_instr
("return;")
549 v
.add_instr
("return {s};")
552 v
.cfc
.generate_var_decls
554 ctx_old
.append
(v
.ctx
)
559 for ctx
in v
.out_contexts
do v
.ctx
.merge
(ctx
)
562 # Compile the method body inline
563 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
566 redef class MMReadImplementationMethod
567 redef meth do_compile_inside
(v
, params
)
569 return node
.prop
.compile_access
(v
, params
[0])
573 redef class MMWriteImplementationMethod
574 redef meth do_compile_inside
(v
, params
)
576 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
581 redef class MMMethSrcMethod
582 redef meth do_compile_inside
(v
, params
)
584 return node
.do_compile_inside
(v
, self, params
)
588 redef class MMImplicitInit
589 redef meth do_compile_inside
(v
, params
)
591 var f
= params
.length
- unassigned_attributes
.length
592 var recv
= params
.first
593 for sp
in super_inits
do
594 assert sp
isa MMMethod
595 var args_recv
= [recv
]
596 if sp
== super_init
then
597 var args
= new Array[String].with_capacity
(f
)
602 sp
.compile_stmt_call
(v
, args
)
604 sp
.compile_stmt_call
(v
, args_recv
)
607 for i
in [f
..params
.length
[ do
608 var attribute
= unassigned_attributes
[i-f
]
609 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
616 # Compile a subtype check to self
618 meth compile_cast
(v
: CompilerVisitor, recv
: String, fromtype
: MMType): String
620 # Fixme: handle formaltypes
621 var g
= local_class
.global
623 if fromtype
.is_nullable
then
624 if self.is_nullable
then
625 s
= "({recv}==NIT_NULL) || "
627 s
= "({recv}!=NIT_NULL) && "
630 # FIXME This is used to not break code without the nullable KW
631 s
= "({recv}==NIT_NULL) || "
633 return "TAG_Bool({s}VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
636 # Compile a cast assertion
637 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode, fromtype
: MMType)
639 # Fixme: handle formaltypes
640 var g
= local_class
.global
642 if fromtype
.is_nullable
then
643 if self.is_nullable
then
644 s
= "({recv}!=NIT_NULL) && "
646 s
= "({recv}==NIT_NULL) || "
649 # FIXME This is used to not break code without the nullable KW
650 s
= "({recv}!=NIT_NULL) && "
652 v
.add_instr
("if ({s}!VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
655 # Compile a notnull cast assertion
656 meth compile_notnull_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
659 v
.add_instr
("if (({recv}==NIT_NULL)) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
664 ###############################################################################
666 redef class AMethPropdef
667 # Compile the method body
668 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
671 redef class PSignature
672 meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String]) is abstract
675 redef class ASignature
676 redef meth compile_parameters
(v
: CompilerVisitor, orig_sig
: MMSignature, params
: Array[String])
678 for ap
in n_params
do
679 var cname
= v
.cfc
.register_variable
(ap
.variable
)
680 v
.nmc
.method_params
.add
(ap
.variable
)
681 var orig_type
= orig_sig
[ap
.position
]
682 if not orig_type
< ap
.variable
.stype
then
683 # FIXME: do not test always
684 # FIXME: handle formal types
685 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
686 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
], ap
, orig_type
)
688 v
.add_assignment
(cname
, params
[ap
.position
])
690 for i
in [0..n_closure_decls
.length
[ do
691 var wd
= n_closure_decls
[i
]
692 var cname
= v
.cfc
.register_closurevariable
(wd
.variable
)
693 wd
.variable
.ctypename
= v
.nmc
.method
.closure_cname
(i
)
694 v
.add_assignment
(cname
, "{params[orig_sig.arity + i]}")
699 redef class AConcreteMethPropdef
700 redef meth do_compile_inside
(v
, method
, params
)
703 v
.nmc
= new NitMethodContext(method
)
705 var selfcname
= v
.cfc
.register_variable
(self_var
)
706 v
.add_assignment
(selfcname
, params
[0])
708 v
.nmc
.method_params
= [self_var
]
710 if n_signature
!= null then
711 var orig_meth
: MMLocalProperty = method
.global
.intro
712 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
713 n_signature
.compile_parameters
(v
, orig_sig
, params
)
716 var itpos
: String = null
717 if self isa AConcreteInitPropdef then
718 itpos
= "VAL2OBJ({selfcname})->vft[{method.local_class.global.init_table_pos_id}].i"
719 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
720 v
.add_instr
("if (init_table[{itpos}]) return;")
723 v
.nmc
.return_label
= "return_label{v.new_number}"
724 v
.nmc
.return_value
= v
.cfc
.get_var
("Method return value and escape marker")
725 if self isa AConcreteInitPropdef then
726 v
.invoke_super_init_calls_after
(null)
728 v
.compile_stmt
(n_block
)
729 v
.add_instr
("{v.nmc.return_label}: while(false);")
730 if self isa AConcreteInitPropdef then
731 v
.add_instr
("init_table[{itpos}] = 1;")
734 var ret
: String = null
735 if method
.signature
.return_type
!= null then
736 ret
= v
.nmc
.return_value
744 redef class ADeferredMethPropdef
745 redef meth do_compile_inside
(v
, method
, params
)
747 v
.add_instr
("fprintf(stderr, \"Deferred method called\
");")
748 v
.add_instr
(v
.printf_locate_error
(self))
749 v
.add_instr
("nit_exit(1);")
750 if method
.signature
.return_type
!= null then
758 redef class AExternMethPropdef
759 redef meth do_compile_inside
(v
, method
, params
)
761 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
762 if n_extern
!= null then
763 ename
= n_extern
.text
764 ename
= ename
.substring
(1, ename
.length-2
)
766 var sig
= method
.signature
767 if params
.length
!= sig
.arity
+ 1 then
768 printl
("par:{params.length} sig:{sig.arity}")
770 var args
= new Array[String]
771 args
.add
(sig
.recv
.unboxtype
(params
[0]))
772 for i
in [0..sig
.arity
[ do
773 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
775 var s
= "{ename}({args.join(", ")})"
776 if sig
.return_type
!= null then
777 return sig
.return_type
.boxtype
(s
)
785 redef class AInternMethPropdef
786 redef meth do_compile_inside
(v
, method
, p
)
788 var c
= method
.local_class
.name
791 if c
== once
"Int".to_symbol
then
792 if n
== once
"object_id".to_symbol
then
794 else if n
== once
"unary -".to_symbol
then
795 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
796 else if n
== once
"output".to_symbol
then
797 v
.add_instr
("printf(\"%ld\\n\
", UNTAG_Int({p[0]}));")
798 else if n
== once
"ascii".to_symbol
then
799 s
= "TAG_Char(UNTAG_Int({p[0]}))"
800 else if n
== once
"succ".to_symbol
then
801 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
802 else if n
== once
"prec".to_symbol
then
803 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
804 else if n
== once
"to_f".to_symbol
then
805 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
806 else if n
== once
"+".to_symbol
then
807 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
808 else if n
== once
"-".to_symbol
then
809 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
810 else if n
== once
"*".to_symbol
then
811 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
812 else if n
== once
"/".to_symbol
then
813 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
814 else if n
== once
"%".to_symbol
then
815 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
816 else if n
== once
"<".to_symbol
then
817 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
818 else if n
== once
">".to_symbol
then
819 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
820 else if n
== once
"<=".to_symbol
then
821 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
822 else if n
== once
">=".to_symbol
then
823 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
824 else if n
== once
"lshift".to_symbol
then
825 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
826 else if n
== once
"rshift".to_symbol
then
827 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
828 else if n
== once
"==".to_symbol
then
829 s
= "TAG_Bool(({p[0]})==({p[1]}))"
830 else if n
== once
"!=".to_symbol
then
831 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
833 else if c
== once
"Float".to_symbol
then
834 if n
== once
"object_id".to_symbol
then
835 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
836 else if n
== once
"unary -".to_symbol
then
837 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
838 else if n
== once
"output".to_symbol
then
839 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
840 else if n
== once
"to_i".to_symbol
then
841 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
842 else if n
== once
"+".to_symbol
then
843 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
844 else if n
== once
"-".to_symbol
then
845 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
846 else if n
== once
"*".to_symbol
then
847 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
848 else if n
== once
"/".to_symbol
then
849 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
850 else if n
== once
"<".to_symbol
then
851 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
852 else if n
== once
">".to_symbol
then
853 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
854 else if n
== once
"<=".to_symbol
then
855 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
856 else if n
== once
">=".to_symbol
then
857 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
859 else if c
== once
"Char".to_symbol
then
860 if n
== once
"object_id".to_symbol
then
861 s
= "TAG_Int(UNTAG_Char({p[0]}))"
862 else if n
== once
"unary -".to_symbol
then
863 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
864 else if n
== once
"output".to_symbol
then
865 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
866 else if n
== once
"ascii".to_symbol
then
867 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
868 else if n
== once
"succ".to_symbol
then
869 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
870 else if n
== once
"prec".to_symbol
then
871 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
872 else if n
== once
"to_i".to_symbol
then
873 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
874 else if n
== once
"+".to_symbol
then
875 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
876 else if n
== once
"-".to_symbol
then
877 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
878 else if n
== once
"*".to_symbol
then
879 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
880 else if n
== once
"/".to_symbol
then
881 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
882 else if n
== once
"%".to_symbol
then
883 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
884 else if n
== once
"<".to_symbol
then
885 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
886 else if n
== once
">".to_symbol
then
887 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
888 else if n
== once
"<=".to_symbol
then
889 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
890 else if n
== once
">=".to_symbol
then
891 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
892 else if n
== once
"==".to_symbol
then
893 s
= "TAG_Bool(({p[0]})==({p[1]}))"
894 else if n
== once
"!=".to_symbol
then
895 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
897 else if c
== once
"Bool".to_symbol
then
898 if n
== once
"object_id".to_symbol
then
899 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
900 else if n
== once
"unary -".to_symbol
then
901 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
902 else if n
== once
"output".to_symbol
then
903 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
904 else if n
== once
"ascii".to_symbol
then
905 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
906 else if n
== once
"to_i".to_symbol
then
907 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
908 else if n
== once
"==".to_symbol
then
909 s
= "TAG_Bool(({p[0]})==({p[1]}))"
910 else if n
== once
"!=".to_symbol
then
911 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
913 else if c
== once
"NativeArray".to_symbol
then
914 if n
== once
"object_id".to_symbol
then
915 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
916 else if n
== once
"[]".to_symbol
then
917 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
918 else if n
== once
"[]=".to_symbol
then
919 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
920 else if n
== once
"copy_to".to_symbol
then
921 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
923 else if c
== once
"NativeString".to_symbol
then
924 if n
== once
"object_id".to_symbol
then
925 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
926 else if n
== once
"atoi".to_symbol
then
927 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
928 else if n
== once
"[]".to_symbol
then
929 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
930 else if n
== once
"[]=".to_symbol
then
931 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
932 else if n
== once
"copy_to".to_symbol
then
933 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]}));")
935 else if n
== once
"object_id".to_symbol
then
936 s
= "TAG_Int((bigint){p[0]})"
937 else if n
== once
"sys".to_symbol
then
939 else if n
== once
"is_same_type".to_symbol
then
940 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
941 else if n
== once
"exit".to_symbol
then
942 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
943 else if n
== once
"calloc_array".to_symbol
then
944 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
945 else if n
== once
"calloc_string".to_symbol
then
946 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
949 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
951 if method
.signature
.return_type
!= null and s
== null then
952 s
= "NIT_NULL /*stub*/"
958 ###############################################################################
961 # Compile the node as an expression
962 # Only the visitor should call it
963 meth compile_expr
(v
: CompilerVisitor): String is abstract
965 # Prepare a call of node as a statement
966 # Only the visitor should call it
967 # It's used for local variable managment
968 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
970 # Compile the node as a statement
971 # Only the visitor should call it
972 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
975 redef class ABlockExpr
976 redef meth compile_stmt
(v
)
984 redef class AVardeclExpr
985 redef meth prepare_compile_stmt
(v
)
987 v
.cfc
.register_variable
(variable
)
990 redef meth compile_stmt
(v
)
992 var cname
= v
.cfc
.varname
(variable
)
993 if n_expr
== null then
994 v
.add_instr
("/*{cname} is variable {variable.name}*/")
996 var e
= v
.compile_expr
(n_expr
)
997 v
.add_assignment
(cname
, e
)
1002 redef class AReturnExpr
1003 redef meth compile_stmt
(v
)
1005 if n_expr
!= null then
1006 var e
= v
.compile_expr
(n_expr
)
1007 v
.add_assignment
(v
.nmc
.return_value
, e
)
1009 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.return_value});")
1010 v
.add_instr
("goto {v.nmc.return_label};")
1014 redef class ABreakExpr
1015 redef meth compile_stmt
(v
)
1017 if n_expr
!= null then
1018 var e
= v
.compile_expr
(n_expr
)
1019 v
.add_assignment
(v
.nmc
.break_value
, e
)
1021 if v
.cfc
.closure
== v
.nmc
then v
.add_instr
("closctx->has_broke = &({v.nmc.break_value}); closctx->broke_value = *closctx->has_broke;")
1022 v
.add_instr
("goto {v.nmc.break_label};")
1026 redef class AContinueExpr
1027 redef meth compile_stmt
(v
)
1029 if n_expr
!= null then
1030 var e
= v
.compile_expr
(n_expr
)
1031 v
.add_assignment
(v
.nmc
.continue_value
, e
)
1033 v
.add_instr
("goto {v.nmc.continue_label};")
1037 redef class AAbortExpr
1038 redef meth compile_stmt
(v
)
1040 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
1045 redef meth compile_stmt
(v
)
1047 v
.compile_stmt
(n_block
)
1052 redef meth compile_stmt
(v
)
1054 var e
= v
.compile_expr
(n_expr
)
1055 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1057 if n_then
!= null then
1059 v
.compile_stmt
(n_then
)
1062 if n_else
!= null then
1063 v
.add_instr
("} else \{ /*if*/")
1065 v
.compile_stmt
(n_else
)
1072 redef class AIfexprExpr
1073 redef meth compile_expr
(v
)
1075 var e
= v
.compile_expr
(n_expr
)
1076 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
1079 var e
= v
.ensure_var
(v
.compile_expr
(n_then
), "Then value")
1081 v
.add_instr
("} else \{ /*if*/")
1084 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
), "Else value")
1085 v
.add_assignment
(e
, e2
)
1092 class AControlableBlock
1094 meth compile_inside_block
(v
: CompilerVisitor) is abstract
1095 redef meth compile_stmt
(v
)
1097 var old_break_label
= v
.nmc
.break_label
1098 var old_continue_label
= v
.nmc
.continue_label
1099 var id
= v
.new_number
1100 v
.nmc
.break_label
= "break_{id}"
1101 v
.nmc
.continue_label
= "continue_{id}"
1103 compile_inside_block
(v
)
1106 v
.nmc
.break_label
= old_break_label
1107 v
.nmc
.continue_label
= old_continue_label
1111 redef class AWhileExpr
1112 special AControlableBlock
1113 redef meth compile_inside_block
(v
)
1115 v
.add_instr
("while (true) \{ /*while*/")
1117 var e
= v
.compile_expr
(n_expr
)
1118 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
1120 v
.compile_stmt
(n_block
)
1121 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1124 v
.add_instr
("{v.nmc.break_label}: while(0);")
1128 redef class AForExpr
1129 special AControlableBlock
1130 redef meth compile_inside_block
(v
)
1132 var e
= v
.compile_expr
(n_expr
)
1133 var ittype
= meth_iterator
.signature
.return_type
1135 var iter
= v
.cfc
.get_var
("For iterator")
1136 v
.add_assignment
(iter
, meth_iterator
.compile_expr_call
(v
, [e
]))
1137 v
.add_instr
("while (true) \{ /*for*/")
1139 var ok
= v
.cfc
.get_var
("For 'is_ok' result")
1140 v
.add_assignment
(ok
, meth_is_ok
.compile_expr_call
(v
, [iter
]))
1141 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
1143 var e
= meth_item
.compile_expr_call
(v
, [iter
])
1144 e
= v
.ensure_var
(e
, "For item")
1145 var cname
= v
.cfc
.register_variable
(variable
)
1146 v
.add_assignment
(cname
, e
)
1147 v
.compile_stmt
(n_block
)
1148 v
.add_instr
("{v.nmc.continue_label}: while(0);")
1149 meth_next
.compile_stmt_call
(v
, [iter
])
1152 v
.add_instr
("{v.nmc.break_label}: while(0);")
1156 redef class AAssertExpr
1157 redef meth compile_stmt
(v
)
1159 var e
= v
.compile_expr
(n_expr
)
1161 if n_id
!= null then
1162 s
= " '{n_id.text}' "
1164 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
1168 redef class AVarExpr
1169 redef meth compile_expr(v)
1171 return " {v.cfc.varname(variable)} /*{variable.name}*/"
1175 redef class AVarAssignExpr
1176 redef meth compile_stmt(v)
1178 var e = v.compile_expr(n_value)
1179 v.add_assignment(v.cfc.varname(variable), "{e} /*{variable.name}=*/")
1183 redef class AVarReassignExpr
1184 redef meth compile_stmt(v)
1186 var e1 = v.cfc.varname(variable)
1187 var e2 = v.compile_expr(n_value)
1188 var e3 = assign_method.compile_expr_call(v, [e1, e2])
1189 v.add_assignment(v.cfc.varname(variable), "{e3} /*{variable.name}*/")
1193 redef class ASelfExpr
1194 redef meth compile_expr(v)
1196 return v.cfc.varname(v.nmc.method_params[0])
1201 redef meth compile_expr(v)
1203 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'or' operand
")
1204 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
1207 var e2 = v.compile_expr(n_expr2)
1208 v.add_assignment(e, e2)
1215 redef class AAndExpr
1216 redef meth compile_expr(v)
1218 var e = v.ensure_var(v.compile_expr(n_expr), "Left 'and' operand
")
1219 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1222 var e2 = v.compile_expr(n_expr2)
1223 v.add_assignment(e, e2)
1230 redef class ANotExpr
1231 redef meth compile_expr(v)
1233 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1238 redef meth compile_expr(v)
1240 var e = v.compile_expr(n_expr)
1241 var e2 = v.compile_expr(n_expr2)
1242 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1246 redef class AIsaExpr
1247 redef meth compile_expr(v)
1249 var e = v.compile_expr(n_expr)
1250 return n_type.stype.compile_cast(v, e, n_expr.stype)
1254 redef class AAsCastExpr
1255 redef meth compile_expr(v)
1257 var e = v.compile_expr(n_expr)
1258 n_type.stype.compile_type_check(v, e, self, n_expr.stype)
1263 redef class AAsNotnullExpr
1264 redef meth compile_expr(v)
1266 var e = v.compile_expr(n_expr)
1267 n_expr.stype.compile_notnull_check(v, e, self)
1272 redef class ATrueExpr
1273 redef meth compile_expr(v)
1275 return " TAG_Bool(true)"
1279 redef class AFalseExpr
1280 redef meth compile_expr(v)
1282 return " TAG_Bool(false)"
1286 redef class AIntExpr
1287 redef meth compile_expr(v)
1289 return " TAG_Int({n_number.text})"
1293 redef class AFloatExpr
1294 redef meth compile_expr(v)
1296 return "BOX_Float({n_float.text})"
1300 redef class ACharExpr
1301 redef meth compile_expr(v)
1303 return " TAG_Char({n_char.text})"
1307 redef class AStringFormExpr
1308 redef meth compile_expr(v)
1311 var i = v.new_number
1312 var cvar = v.cfc.get_var("Once String constant
")
1313 v.add_decl("static val_t once_value_
{i} = NIT_NULL; /* Once value
for string
{cvar}*/")
1314 v.add_instr("if (once_value_
{i} != NIT_NULL) {cvar} = once_value_
{i};")
1315 v.add_instr("else \
{")
1317 v.cfc.free_var(cvar)
1318 var e = meth_with_native.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1319 v.add_assignment(cvar, e)
1320 v.add_instr("once_value_
{i} = {cvar};")
1326 # The raw string value
1327 protected meth string_text: String is abstract
1329 # The string in a C native format
1330 protected attr _cstring: String
1332 # The string length in bytes
1333 protected attr _cstring_length: Int
1335 # Compute _cstring and _cstring_length using string_text
1336 protected meth compute_string_info
1339 var str = string_text
1340 var res = new Buffer
1342 while i < str.length do
1347 if c2 != '{' and c2 != '}' then
1357 _cstring_length = len
1361 redef class AStringExpr
1362 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1364 redef class AStartStringExpr
1365 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1367 redef class AMidStringExpr
1368 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1370 redef class AEndStringExpr
1371 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1374 redef class ASuperstringExpr
1375 redef meth compile_expr(v)
1377 var array = meth_with_capacity.compile_constructor_call(v, atype, ["TAG_Int({n_exprs.length})"])
1378 array = v.ensure_var(array, "Array (for super-string
)")
1380 for ne in n_exprs do
1381 var e = v.ensure_var(v.compile_expr(ne), "super-string element
")
1382 if ne.stype != stype then
1384 e = meth_to_s.compile_expr_call(v, [e])
1387 meth_add.compile_stmt_call(v, [array, e])
1390 return meth_to_s.compile_expr_call(v, [array])
1394 redef class ANullExpr
1395 redef meth compile_expr(v)
1397 return " NIT_NULL /*null*/"
1401 redef class AArrayExpr
1402 redef meth compile_expr(v)
1404 var recv = meth_with_capacity.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1405 recv = v.ensure_var(recv, "Literal array
")
1407 for ne in n_exprs do
1408 var e = v.compile_expr(ne)
1409 meth_add.compile_stmt_call(v, [recv, e])
1415 redef class ARangeExpr
1416 redef meth compile_expr(v)
1418 var e = v.compile_expr(n_expr)
1419 var e2 = v.compile_expr(n_expr2)
1420 return meth_init.compile_constructor_call(v, stype, [e, e2])
1424 redef class ASuperExpr
1425 redef meth compile_stmt(v)
1427 var e = intern_compile_call(v)
1429 v.add_instr(e + ";")
1433 redef meth compile_expr(v)
1435 var e = intern_compile_call(v)
1440 private meth intern_compile_call(v: CompilerVisitor): String
1442 var arity = v.nmc.method_params.length - 1
1443 if init_in_superclass != null then
1444 arity = init_in_superclass.signature.arity
1446 var args = new Array[String].with_capacity(arity + 1)
1447 args.add(v.cfc.varname(v.nmc.method_params[0]))
1448 if n_args.length != arity then
1449 for i in [0..arity[ do
1450 args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
1454 args.add(v.compile_expr(na))
1457 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1458 if init_in_superclass != null then
1459 return init_in_superclass.intern_compile_call(v, args)
1461 if prop.global.is_init then args.add("init_table
")
1462 return prop.compile_super_call(v, args)
1467 redef class AAttrExpr
1468 redef meth compile_expr(v)
1470 var e = v.compile_expr(n_expr)
1471 return prop.compile_access(v, e)
1475 redef class AAttrAssignExpr
1476 redef meth compile_stmt(v)
1478 var e = v.compile_expr(n_expr)
1479 var e2 = v.compile_expr(n_value)
1480 v.add_assignment(prop.compile_access(v, e), e2)
1483 redef class AAttrReassignExpr
1484 redef meth compile_stmt(v)
1486 var e1 = v.compile_expr(n_expr)
1487 var e2 = prop.compile_access(v, e1)
1488 var e3 = v.compile_expr(n_value)
1489 var e4 = assign_method.compile_expr_call(v, [e2, e3])
1490 v.add_assignment(e2, e4)
1494 redef class AAbsAbsSendExpr
1495 # Compile each argument and add them to the array
1496 meth compile_arguments_in(v: CompilerVisitor, cargs: Array[String])
1498 for a in arguments do
1499 cargs.add(v.compile_expr(a))
1505 redef class ASendExpr
1506 private meth intern_compile_call(v: CompilerVisitor): String
1508 var recv = v.compile_expr(n_expr)
1509 var cargs = new Array[String]
1511 compile_arguments_in(v, cargs)
1514 if prop_signature.closures.is_empty then
1515 e = prop.intern_compile_call(v, cargs)
1517 e = prop.compile_call_and_closures(v, cargs, closure_defs)
1520 if prop.global.is_init then
1521 v.invoke_super_init_calls_after(prop)
1526 redef meth compile_expr(v)
1528 var e = intern_compile_call(v)
1533 redef meth compile_stmt(v)
1535 var e = intern_compile_call(v)
1537 v.add_instr(e + ";")
1542 redef class ASendReassignExpr
1543 redef meth compile_expr(v) do abort
1545 redef meth compile_stmt(v)
1547 var recv = v.compile_expr(n_expr)
1548 var cargs = new Array[String]
1550 compile_arguments_in(v, cargs)
1552 var e2 = read_prop.compile_expr_call(v, cargs)
1553 var e3 = v.compile_expr(n_value)
1554 var e4 = assign_method.compile_expr_call(v, [e2, e3])
1556 prop.compile_stmt_call(v, cargs)
1560 redef class ANewExpr
1561 redef meth compile_expr(v)
1563 var cargs = new Array[String]
1564 compile_arguments_in(v, cargs)
1565 return prop.compile_constructor_call(v, stype, cargs)
1568 redef meth compile_stmt(v) do abort
1571 redef class PClosureDef
1572 # Compile the closure definition as a function in v.out_contexts
1573 # Return the cname of the function
1574 meth compile_closure(v: CompilerVisitor, closcn: String): String is abstract
1576 # Compile the closure definition inside the current C function.
1577 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1580 redef class AClosureDef
1581 # The cname of the function
1582 readable attr _cname: String
1584 redef meth compile_closure(v, closcn)
1587 v.ctx = new CContext
1588 v.out_contexts.add(v.ctx)
1590 var cfc_old = v.cfc.closure
1591 v.cfc.closure = v.nmc
1593 var old_rv = v.nmc.return_value
1594 var old_bv = v.nmc.break_value
1595 if cfc_old == null then
1596 v.nmc.return_value = "closctx-
>{old_rv}"
1597 v.nmc.break_value = "closctx-
>{old_bv}"
1600 var cname = "OC_{v.nmc.method.cname}_
{v.out_contexts.length}"
1602 var args = new Array[String]
1603 for i in [0..closure.signature.arity[ do
1604 args.add(" param
{i}")
1607 var cs = decl_csignature(v, args, closcn)
1609 v.add_instr("{cs} \
{")
1611 var ctx_old2 = v.ctx
1612 v.ctx = new CContext
1614 v.add_decl("struct trace_t trace
= \
{NULL, NULL, {line_number}, LOCATE_{v.nmc.method.cname}};")
1615 v.add_instr("trace
.prev
= tracehead
; tracehead
= &trace
;")
1617 v.add_instr("trace
.file
= LOCATE_{v.module.name};")
1618 var s = do_compile_inside(v, args)
1620 v.add_instr("{v.nmc.return_label}:")
1621 v.add_instr("tracehead
= trace
.prev
;")
1623 v.add_instr("return;")
1625 v.add_instr("return {s};")
1628 ctx_old2.append(v.ctx)
1634 v.cfc.closure = cfc_old
1635 v.nmc.return_value = old_rv
1636 v.nmc.break_value = old_bv
1639 var closcnv = "wbclos
{v.new_number}"
1640 v.add_decl("struct
WBT_ {closcnv};")
1641 v.add_instr("{closcnv}.fun = (fun_t
){cname};")
1642 v.add_instr("{closcnv}.has_broke
= NULL;")
1643 if cfc_old != null then
1644 v.add_instr("{closcnv}.variable
= closctx-
>variable
;")
1645 v.add_instr("{closcnv}.closurevariable
= closctx-
>closurevariable
;")
1647 v.add_instr("{closcnv}.variable
= variable
;")
1648 v.add_instr("{closcnv}.closurevariable
= closurevariable
;")
1651 return "(&{closcnv})"
1654 protected meth decl_csignature(v: CompilerVisitor, args: Array[String], closcn: String): String
1656 var params = new Array[String]
1657 params.add("struct
WBT_ *closctx
")
1658 for i in [0..closure.signature.arity[ do
1659 var p = "val_t
{args[i]}"
1663 if closure.signature.return_type != null then
1668 var p = params.join(", ")
1669 var s = "{ret} {cname}({p})"
1670 v.add_decl("typedef
{ret} (* {cname}_t
)({p});")
1675 redef meth do_compile_inside(v, params)
1677 for i in [0..variables.length[ do
1678 var vacname = v.cfc.register_variable(variables[i])
1679 v.add_assignment(vacname, params[i])
1682 var old_cv = v.nmc.continue_value
1683 var old_cl = v.nmc.continue_label
1684 var old_bl = v.nmc.break_label
1686 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1687 v.nmc.continue_label = "continue_label
{v.new_number}"
1688 v.nmc.break_label = v.nmc.return_label
1690 v.compile_stmt(n_expr)
1692 v.add_instr("{v.nmc.continue_label}: while(false);")
1694 var ret: String = null
1695 if closure.signature.return_type != null then ret = v.nmc.continue_value
1697 v.nmc.continue_value = old_cv
1698 v.nmc.continue_label = old_cl
1699 v.nmc.break_label = old_bl
1705 redef class PClosureDecl
1706 meth do_compile_inside(v: CompilerVisitor, params: Array[String]): String is abstract
1708 redef class AClosureDecl
1709 redef meth do_compile_inside(v, params)
1711 if n_signature != null then n_signature.compile_parameters(v, variable.closure.signature, params)
1713 var old_cv = v.nmc.continue_value
1714 var old_cl = v.nmc.continue_label
1715 var old_bl = v.nmc.break_label
1717 v.nmc.continue_value = v.cfc.get_var("Continue value
and escape marker
")
1718 v.nmc.continue_label = "continue_label
{v.new_number}"
1719 v.nmc.break_label = v.nmc.return_label
1721 v.compile_stmt(n_expr)
1723 v.add_instr("{v.nmc.continue_label}: while(false);")
1725 var ret: String = null
1726 if variable.closure.signature.return_type != null then ret = v.nmc.continue_value
1728 v.nmc.continue_value = old_cv
1729 v.nmc.continue_label = old_cl
1730 v.nmc.break_label = old_bl
1736 redef class AClosureCallExpr
1737 meth intern_compile_call(v: CompilerVisitor): String
1739 var cargs = new Array[String]
1740 compile_arguments_in(v, cargs)
1741 var va: String = null
1742 if variable.closure.signature.return_type != null then va = v.cfc.get_var("Closure call result value
")
1744 if variable.closure.is_optional then
1745 v.add_instr("if({v.cfc.varname(variable)}==NULL) \
{")
1747 var n = variable.decl
1748 assert n isa AClosureDecl
1749 var s = n.do_compile_inside(v, cargs)
1750 if s != null then v.add_assignment(va, s)
1752 v.add_instr("} else \
{")
1756 var ivar = v.cfc.varname(variable)
1758 cargs2.append(cargs)
1759 var s = "(({variable.ctypename})({ivar}->fun))({cargs2.join(", ")}) /* Invoke closure
{variable} */"
1761 v.add_assignment(va, s)
1765 v.add_instr("if ({ivar}->has_broke
) \
{")
1767 if n_closure_defs != null and n_closure_defs.length == 1 then do
1768 n_closure_defs.first.do_compile_inside(v, null)
1770 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
;\
}")
1771 v.add_instr("goto
{v.nmc.return_label};")
1775 if variable.closure.is_optional then
1782 redef meth compile_expr(v)
1784 var e = intern_compile_call(v)
1789 redef meth compile_stmt(v)
1791 var e = intern_compile_call(v)
1793 v.add_instr(e + ";")
1798 redef class AProxyExpr
1799 redef meth compile_expr(v)
1801 return v.compile_expr(n_expr)
1805 redef class AOnceExpr
1806 redef meth compile_expr(v)
1808 var i = v.new_number
1809 var cvar = v.cfc.get_var("Once expression result
")
1810 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
for {cvar}*/")
1811 v.add_instr("if (once_bool_
{i}) {cvar} = once_value_
{i};")
1812 v.add_instr("else \
{")
1814 v.cfc.free_var(cvar)
1815 var e = v.compile_expr(n_expr)
1816 v.add_assignment(cvar, e)
1817 v.add_instr("once_value_
{i} = {cvar};")
1818 v.add_instr("once_bool_
{i} = true;")