1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Compile method bodies, statments and expressions to C.
18 package compiling_methods
23 redef class CompilerVisitor
24 # Compile a statment node
25 meth compile_stmt
(n
: PExpr)
27 n
.prepare_compile_stmt
(self)
28 var i
= cfc
._variable_index
30 cfc
._variable_index
= i
33 # Compile is expression node
34 meth compile_expr
(n
: PExpr): String
36 var i
= cfc
._variable_index
37 var s
= n
.compile_expr
(self)
38 cfc
._variable_index
= i
42 if s
== cfc
.variable
(cfc
._variable_index-1
) then
50 # Ensure that a c expression is a var
51 meth ensure_var
(s
: String): String
53 if s
.substring
(0,3) == "variable" then
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 # Generate an fprintf to display an error location
74 meth printf_locate_error
(node
: PNode): String
76 var s
= "fprintf(stderr, \""
77 if nmc != null then s.append(" in %s
")
78 s.append(" (%s
:%d
)\\n\
", ")
79 if nmc
!= null then s
.append
("LOCATE_{nmc.method.cname}, ")
80 s
.append
("LOCATE_{module.name}, {node.line_number});")
84 redef init(module: MMSrcModule)
89 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
91 var n
= nmc
.method
.node
92 assert n
isa AConcreteInitPropdef
94 if n
.super_init_calls
.is_empty
then return
98 if start_prop
!= null then
99 while n
.super_init_calls
[i
] != start_prop
do
100 #s.append(" {n.super_init_calls[i]}")
104 #s.append(" {start_prop}")
106 while n
.explicit_super_init_calls
[j
] != start_prop
do
111 var stop_prop
: MMMethod = null
112 if j
< n
.explicit_super_init_calls
.length
then
113 stop_prop
= n
.explicit_super_init_calls
[j
]
115 var l
= n
.super_init_calls
.length
118 var p
= n
.super_init_calls
[i
]
119 if p
== stop_prop
then break
120 var cargs
= nmc
.method_params
121 if p
.signature
.arity
== 0 then
122 cargs
= [nmc
.method_params
[0]]
125 p
.compile_call
(self, cargs
)
130 # s.append(" {n.super_init_calls[i]}")
133 #if stop_prop != null then s.append(" (stop at {stop_prop})")
134 #n.printl("implicit calls in {n.method}: {s}")
138 # A C function currently written
139 class CFunctionContext
140 readable attr _visitor
: CompilerVisitor
142 # Next available variable number
143 attr _variable_index
: Int = 0
145 # Total number of variable
146 attr _variable_index_max
: Int = 0
148 # Association between nit variable and the corrsponding c variable
149 readable attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
151 # Return the next available variable
154 var v
= variable
(_variable_index
)
155 _variable_index
= _variable_index
+ 1
156 if _variable_index
> _variable_index_max
then
157 visitor
.add_decl
("val_t {v};")
158 _variable_index_max
= _variable_index
163 # Return the ith variable
164 protected meth variable
(i
: Int): String
170 # Mark the variable available
171 meth free_var
(v
: String)
174 if v
== variable
(_variable_index-1
) then
175 _variable_index
= _variable_index
- 1
179 init(v
: CompilerVisitor) do _visitor
= v
182 # A Nit method currenlty compiled
183 class NitMethodContext
184 # Current method compiled
185 readable attr _method
: MMSrcMethod
187 # Is a "return" found in the method body
188 readable writable attr _has_return
: Bool = false
190 # Association between parameters and the corresponding c variables
191 readable writable attr _method_params
: Array[String]
193 # Where a nit return must branch
194 readable writable attr _return_label
: String
196 # Where a nit break must branch
197 readable writable attr _break_label
: String
199 # Where a nit continue must branch
200 readable writable attr _continue_label
: String
202 # Variable where a functionnal nit return must store its value
203 readable writable attr _return_value
: String
205 init(method
: MMSrcMethod)
211 ###############################################################################
214 # Compile a call on self for given arguments
215 # Most calls are compiled with a table access,
216 # primitive calles are inlined
217 # == and != are guarded and possibly inlined
218 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
221 if i
isa MMSrcMethod then
222 if i
isa MMMethSrcMethod and i
.node
isa AInternMethPropdef or
223 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
225 var e
= i
.do_compile_inside
(v
, cargs
)
229 var ee
= once
"==".to_symbol
230 var ne
= once
"!=".to_symbol
232 var eqp
= signature
.recv
.local_class
.select_method
(ee
)
233 var eqcall
= eqp
.compile_call
(v
, cargs
)
234 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
236 if global
.is_init
then
238 cargs
.add
("init_table /*YYY*/")
241 var m
= "(({cname}_t)CALL({cargs[0]},{global.color_id}))"
242 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
244 vcall
= "UNTAG_Bool({vcall})"
245 var obj
= once
"Object".to_symbol
246 if i
.local_class
.name
== obj
then
247 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
249 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
251 if signature
.return_type
!= null then
254 v
.add_instr
(vcall
+ ";")
259 # Compile a call as constructor with given args
260 meth compile_constructor_call
(v
: CompilerVisitor, recvtype
: MMType, cargs
: Array[String]): String
262 var recv
= v
.cfc
.get_var
263 v
.add_instr
("{recv} = NEW_{recvtype.local_class}_{global.intro.cname}({cargs.join(", ")}); /*new {recvtype}*/")
267 # Compile a call as call-next-method on self with given args
268 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
270 var m
= "(({cname}_t)CALL({cargs[0]},{color_id_for_super}))"
271 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
276 redef class MMAttribute
277 # Compile an acces on selffor a given reciever.
278 # Result is a valid C left-value for assigment
279 meth compile_access
(v
: CompilerVisitor, recv
: String): String
281 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
285 redef class MMLocalProperty
286 # Compile the property as a C property
287 meth compile_property_to_c
(v
: CompilerVisitor) do end
290 redef class MMSrcMethod
291 # Compile and declare the signature to C
292 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
294 var params
= new Array[String]
295 var params_new
: Array[String] = null
296 if global
.is_init
then
297 params_new
= new Array[String]
299 params
.add
("val_t {args[0]}")
300 for i
in [0..signature
.arity
[ do
301 var p
= "val_t {args[i+1]}"
303 if params_new
!= null then params_new
.add
(p
)
305 if global
.is_init
then
306 params
.add
("int* init_table")
309 if signature
.return_type
!= null then
314 var p
= params
.join
(", ")
315 var s
= "{ret} {cname}({p})"
316 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
318 if params_new
!= null then
319 v
.add_decl
("val_t NEW_{cname}({params_new.join(", ")});")
324 redef meth compile_property_to_c
(v
)
326 v
.cfc
= new CFunctionContext(v
)
328 var args
= new Array[String]
330 for i
in [0..signature
.arity
[ do
331 args
.add
(" param{i}")
333 var cs
= decl_csignature
(v
, args
)
334 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
336 v
.add_instr
("{cs} \{")
343 if s
.node
!= null then ln
= s
.node
.line_number
344 v
.add_decl
("struct trace_t trace = \{NULL, NULL, {ln}, LOCATE_{cname}};")
345 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
346 v
.add_instr
("trace.file = LOCATE_{module.name};")
347 var s
= do_compile_inside
(v
, args
)
348 v
.add_instr
("tracehead = trace.prev;")
350 v
.add_instr
("return;")
352 v
.add_instr
("return {s};")
355 ctx_old
.append
(v
.ctx
)
361 # Compile the method body inline
362 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
365 redef class MMReadImplementationMethod
366 redef meth do_compile_inside
(v
, params
)
368 return node
.prop
.compile_access
(v
, params
[0])
372 redef class MMWriteImplementationMethod
373 redef meth do_compile_inside
(v
, params
)
375 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
380 redef class MMMethSrcMethod
381 redef meth do_compile_inside
(v
, params
)
383 return node
.do_compile_inside
(v
, self, params
)
387 redef class MMImplicitInit
388 redef meth do_compile_inside
(v
, params
)
390 var f
= params
.length
- unassigned_attributes
.length
391 var recv
= params
.first
392 for sp
in super_inits
do
393 assert sp
isa MMMethod
394 var args_recv
= [recv
]
395 if sp
== super_init
then
396 var args
= new Array[String].with_capacity
(f
)
401 sp
.compile_call
(v
, args
)
403 sp
.compile_call
(v
, args_recv
)
406 for i
in [f
..params
.length
[ do
407 var attribute
= unassigned_attributes
[i-f
]
408 v
.add_assignment
(attribute
.compile_access
(v
, recv
), params
[i
])
415 # Compile a subtype check to self
417 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
419 # Fixme: handle formaltypes
420 var g
= local_class
.global
421 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
424 # Compile a cast assertion
425 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
427 # Fixme: handle formaltypes
428 var g
= local_class
.global
429 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}*/;")
433 ###############################################################################
435 redef class AMethPropdef
436 # Compile the method body
437 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
440 redef class AConcreteMethPropdef
441 redef meth do_compile_inside
(v
, method
, params
)
444 v
.nmc
= new NitMethodContext(method
)
446 var orig_meth
: MMLocalProperty = method
.global
.intro
447 var orig_sig
= orig_meth
.signature_for
(method
.signature
.recv
)
448 if n_signature
!= null then
449 var sig
= n_signature
450 assert sig
isa ASignature
451 for ap
in sig
.n_params
do
452 var cname
= v
.cfc
.get_var
453 v
.cfc
.varnames
[ap
.variable
] = cname
454 var orig_type
= orig_sig
[ap
.position
]
455 if not orig_type
< ap
.variable
.stype
then
456 # FIXME: do not test always
457 # FIXME: handle formal types
458 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
459 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
+ 1], ap
)
461 v
.add_assignment
(cname
, params
[ap
.position
+ 1])
465 var itpos
: String = null
466 if self isa AConcreteInitPropdef then
467 itpos
= "VAL2OBJ({params[0]})->vft[{method.local_class.global.init_table_pos_id}].i"
468 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
469 v
.add_instr
("if (init_table[{itpos}]) return;")
472 v
.nmc
.method_params
= params
473 v
.nmc
.return_label
= "return_label{v.new_number}"
474 if method
.signature
.return_type
!= null then
475 v
.nmc
.return_value
= v
.cfc
.get_var
476 v
.cfc
.free_var
(v
.nmc
.return_value
)
478 v
.nmc
.return_value
= null
480 if self isa AConcreteInitPropdef then
481 v
.invoke_super_init_calls_after
(null)
483 if n_block
!= null then
484 v
.compile_stmt
(n_block
)
486 if v
.nmc
.has_return
then
487 v
.add_instr
("{v.nmc.return_label}: while(false);")
489 if self isa AConcreteInitPropdef then
490 v
.add_instr
("init_table[{itpos}] = 1;")
492 var ret
= v
.nmc
.return_value
499 redef class ADeferredMethPropdef
500 redef meth do_compile_inside
(v
, method
, params
)
502 v
.add_instr
("fprintf(stderr, \"Deferred method
%s called\
");")
503 v
.add_instr
(v
.printf_locate_error
(self))
504 v
.add_instr
("nit_exit(1);")
505 if method
.signature
.return_type
!= null then
513 redef class AExternMethPropdef
514 redef meth do_compile_inside
(v
, method
, params
)
516 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
517 if n_extern
!= null then
518 ename
= n_extern
.text
519 ename
= ename
.substring
(1, ename
.length-2
)
521 var sig
= method
.signature
522 if params
.length
!= sig
.arity
+ 1 then
523 printl
("par:{params.length} sig:{sig.arity}")
525 var args
= new Array[String]
526 args
.add
(sig
.recv
.unboxtype
(params
[0]))
527 for i
in [0..sig
.arity
[ do
528 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
530 var s
= "{ename}({args.join(", ")})"
531 if sig
.return_type
!= null then
532 return sig
.return_type
.boxtype
(s
)
540 redef class AInternMethPropdef
541 redef meth do_compile_inside
(v
, method
, p
)
543 var c
= method
.local_class
.name
546 if c
== once
"Int".to_symbol
then
547 if n
== once
"object_id".to_symbol
then
549 else if n
== once
"unary -".to_symbol
then
550 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
551 else if n
== once
"output".to_symbol
then
552 v
.add_instr
("printf(\"%d\\n\
", UNTAG_Int({p[0]}));")
553 else if n
== once
"ascii".to_symbol
then
554 s
= "TAG_Char(UNTAG_Int({p[0]}))"
555 else if n
== once
"succ".to_symbol
then
556 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
557 else if n
== once
"prec".to_symbol
then
558 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
559 else if n
== once
"to_f".to_symbol
then
560 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
561 else if n
== once
"+".to_symbol
then
562 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
563 else if n
== once
"-".to_symbol
then
564 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
565 else if n
== once
"*".to_symbol
then
566 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
567 else if n
== once
"/".to_symbol
then
568 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
569 else if n
== once
"%".to_symbol
then
570 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
571 else if n
== once
"<".to_symbol
then
572 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
573 else if n
== once
">".to_symbol
then
574 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
575 else if n
== once
"<=".to_symbol
then
576 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
577 else if n
== once
">=".to_symbol
then
578 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
579 else if n
== once
"lshift".to_symbol
then
580 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
581 else if n
== once
"rshift".to_symbol
then
582 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
583 else if n
== once
"==".to_symbol
then
584 s
= "TAG_Bool(({p[0]})==({p[1]}))"
585 else if n
== once
"!=".to_symbol
then
586 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
588 else if c
== once
"Float".to_symbol
then
589 if n
== once
"object_id".to_symbol
then
590 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
591 else if n
== once
"unary -".to_symbol
then
592 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
593 else if n
== once
"output".to_symbol
then
594 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
595 else if n
== once
"to_i".to_symbol
then
596 s
= "TAG_Int((bigint)UNBOX_Float({p[0]}))"
597 else if n
== once
"+".to_symbol
then
598 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
599 else if n
== once
"-".to_symbol
then
600 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
601 else if n
== once
"*".to_symbol
then
602 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
603 else if n
== once
"/".to_symbol
then
604 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
605 else if n
== once
"<".to_symbol
then
606 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
607 else if n
== once
">".to_symbol
then
608 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
609 else if n
== once
"<=".to_symbol
then
610 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
611 else if n
== once
">=".to_symbol
then
612 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
614 else if c
== once
"Char".to_symbol
then
615 if n
== once
"object_id".to_symbol
then
616 s
= "TAG_Int(UNTAG_Char({p[0]}))"
617 else if n
== once
"unary -".to_symbol
then
618 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
619 else if n
== once
"output".to_symbol
then
620 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
621 else if n
== once
"ascii".to_symbol
then
622 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
623 else if n
== once
"succ".to_symbol
then
624 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
625 else if n
== once
"prec".to_symbol
then
626 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
627 else if n
== once
"to_i".to_symbol
then
628 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
629 else if n
== once
"+".to_symbol
then
630 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
631 else if n
== once
"-".to_symbol
then
632 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
633 else if n
== once
"*".to_symbol
then
634 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
635 else if n
== once
"/".to_symbol
then
636 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
637 else if n
== once
"%".to_symbol
then
638 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
639 else if n
== once
"<".to_symbol
then
640 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
641 else if n
== once
">".to_symbol
then
642 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
643 else if n
== once
"<=".to_symbol
then
644 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
645 else if n
== once
">=".to_symbol
then
646 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
647 else if n
== once
"==".to_symbol
then
648 s
= "TAG_Bool(({p[0]})==({p[1]}))"
649 else if n
== once
"!=".to_symbol
then
650 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
652 else if c
== once
"Bool".to_symbol
then
653 if n
== once
"object_id".to_symbol
then
654 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
655 else if n
== once
"unary -".to_symbol
then
656 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
657 else if n
== once
"output".to_symbol
then
658 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
659 else if n
== once
"ascii".to_symbol
then
660 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
661 else if n
== once
"to_i".to_symbol
then
662 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
663 else if n
== once
"==".to_symbol
then
664 s
= "TAG_Bool(({p[0]})==({p[1]}))"
665 else if n
== once
"!=".to_symbol
then
666 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
668 else if c
== once
"NativeArray".to_symbol
then
669 if n
== once
"object_id".to_symbol
then
670 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
671 else if n
== once
"[]".to_symbol
then
672 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
673 else if n
== once
"[]=".to_symbol
then
674 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
675 else if n
== once
"copy_to".to_symbol
then
676 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
678 else if c
== once
"NativeString".to_symbol
then
679 if n
== once
"object_id".to_symbol
then
680 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
681 else if n
== once
"atoi".to_symbol
then
682 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
683 else if n
== once
"[]".to_symbol
then
684 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
685 else if n
== once
"[]=".to_symbol
then
686 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
687 else if n
== once
"copy_to".to_symbol
then
688 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]}));")
690 else if n
== once
"object_id".to_symbol
then
691 s
= "TAG_Int((bigint){p[0]})"
692 else if n
== once
"sys".to_symbol
then
694 else if n
== once
"is_same_type".to_symbol
then
695 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
696 else if n
== once
"exit".to_symbol
then
697 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
698 else if n
== once
"calloc_array".to_symbol
then
699 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
700 else if n
== once
"calloc_string".to_symbol
then
701 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
704 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
706 if method
.signature
.return_type
!= null and s
== null then
707 s
= "NIT_NULL /*stub*/"
713 ###############################################################################
716 # Compile the node as an expression
717 # Only the visitor should call it
718 meth compile_expr
(v
: CompilerVisitor): String is abstract
720 # Prepare a call of node as a statement
721 # Only the visitor should call it
722 # It's used for local variable managment
723 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
725 # Compile the node as a statement
726 # Only the visitor should call it
727 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
730 redef class ABlockExpr
731 redef meth compile_stmt
(v
)
739 redef class AVardeclExpr
740 redef meth prepare_compile_stmt
(v
)
742 var cname
= v
.cfc
.get_var
743 v
.cfc
.varnames
[variable
] = cname
746 redef meth compile_stmt
(v
)
748 var cname
= v
.cfc
.varnames
[variable
]
749 if n_expr
== null then
750 var t
= variable
.stype
751 v
.add_assignment
(cname
, "{t.default_cvalue} /*decl variable {variable.name}*/")
753 var e
= v
.compile_expr
(n_expr
)
754 v
.add_assignment
(cname
, e
)
759 redef class AReturnExpr
760 redef meth compile_stmt
(v
)
762 v
.nmc
.has_return
= true
763 if n_expr
!= null then
764 var e
= v
.compile_expr
(n_expr
)
765 v
.add_assignment
(v
.nmc
.return_value
, e
)
767 v
.add_instr
("goto {v.nmc.return_label};")
771 redef class ABreakExpr
772 redef meth compile_stmt
(v
)
774 v
.add_instr
("goto {v.nmc.break_label};")
778 redef class AContinueExpr
779 redef meth compile_stmt
(v
)
781 v
.add_instr
("goto {v.nmc.continue_label};")
785 redef class AAbortExpr
786 redef meth compile_stmt
(v
)
788 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
793 redef meth compile_stmt
(v
)
795 if n_block
!= null then
796 v
.compile_stmt
(n_block
)
802 redef meth compile_stmt
(v
)
804 var e
= v
.compile_expr
(n_expr
)
805 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
807 if n_then
!= null then
809 v
.compile_stmt
(n_then
)
812 if n_else
!= null then
813 v
.add_instr
("} else \{ /*if*/")
815 v
.compile_stmt
(n_else
)
822 redef class AIfexprExpr
823 redef meth compile_expr
(v
)
825 var e
= v
.compile_expr
(n_expr
)
826 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
829 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
831 v
.add_instr
("} else \{ /*if*/")
834 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
835 v
.add_assignment
(e
, e2
)
842 redef class AControlableBlock
843 meth compile_inside_block
(v
: CompilerVisitor) is abstract
844 redef meth compile_stmt
(v
)
846 var old_break_label
= v
.nmc
.break_label
847 var old_continue_label
= v
.nmc
.continue_label
848 var id
= v
.new_number
849 v
.nmc
.break_label
= "break_{id}"
850 v
.nmc
.continue_label
= "continue_{id}"
852 compile_inside_block
(v
)
855 v
.nmc
.break_label
= old_break_label
856 v
.nmc
.continue_label
= old_continue_label
860 redef class AWhileExpr
861 redef meth compile_inside_block
(v
)
863 v
.add_instr
("while (true) \{ /*while*/")
865 var e
= v
.compile_expr
(n_expr
)
866 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
868 if n_block
!= null then
869 v
.compile_stmt
(n_block
)
871 v
.add_instr
("{v.nmc.continue_label}: while(0);")
874 v
.add_instr
("{v.nmc.break_label}: while(0);")
879 redef meth compile_inside_block
(v
)
881 v
.compile_stmt
(n_vardecl
)
885 redef class AForVardeclExpr
886 redef meth compile_stmt
(v
)
888 var e
= v
.compile_expr
(n_expr
)
889 var prop
= n_expr
.stype
.local_class
.select_method
(once
"iterator".to_symbol
)
891 printl
("No iterator")
894 var ittype
= prop
.signature
.return_type
896 var iter
= v
.cfc
.get_var
897 v
.add_assignment
(iter
, prop
.compile_call
(v
, [e
]))
898 var prop2
= ittype
.local_class
.select_method
(once
"is_ok".to_symbol
)
899 if prop2
== null then
903 var prop3
= ittype
.local_class
.select_method
(once
"item".to_symbol
)
904 if prop3
== null then
908 var prop4
= ittype
.local_class
.select_method
(once
"next".to_symbol
)
909 if prop4
== null then
913 v
.add_instr
("while (true) \{ /*for*/")
915 var ok
= v
.cfc
.get_var
916 v
.add_assignment
(ok
, prop2
.compile_call
(v
, [iter
]))
917 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
919 var e
= prop3
.compile_call
(v
, [iter
])
921 v
.cfc
.varnames
[variable
] = e
923 assert par
isa AForExpr
924 var n_block
= par
.n_block
925 if n_block
!= null then
926 v
.compile_stmt
(n_block
)
928 v
.add_instr
("{v.nmc.continue_label}: while(0);")
929 e
= prop4
.compile_call
(v
, [iter
])
933 v
.add_instr
("{v.nmc.break_label}: while(0);")
937 redef class AAssertExpr
938 redef meth compile_stmt
(v
)
940 var e
= v
.compile_expr
(n_expr
)
943 s
= " '{n_id.text}' "
945 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
950 redef meth compile_expr(v)
952 return " {v.cfc.varnames[variable]} /*{variable.name}*/"
956 redef class AVarAssignExpr
957 redef meth compile_stmt(v)
959 var e = v.compile_expr(n_value)
960 v.add_assignment(v.cfc.varnames[variable], "{e} /*{variable.name}=*/")
964 redef class AVarReassignExpr
965 redef meth compile_stmt(v)
967 var e1 = v.cfc.varnames[variable]
968 var e2 = v.compile_expr(n_value)
969 var e3 = assign_method.compile_call(v, [e1, e2])
970 v.add_assignment(v.cfc.varnames[variable], "{e3} /*{variable.name}*/")
974 redef class ASelfExpr
975 redef meth compile_expr(v)
977 return v.nmc.method_params[0]
982 redef meth compile_expr(v)
984 var e = v.ensure_var(v.compile_expr(n_expr))
985 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
988 var e2 = v.compile_expr(n_expr2)
989 v.add_assignment(e, e2)
997 redef meth compile_expr(v)
999 var e = v.ensure_var(v.compile_expr(n_expr))
1000 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
1003 var e2 = v.compile_expr(n_expr2)
1004 v.add_assignment(e, e2)
1011 redef class ANotExpr
1012 redef meth compile_expr(v)
1014 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
1019 redef meth compile_expr(v)
1021 var e = v.compile_expr(n_expr)
1022 var e2 = v.compile_expr(n_expr2)
1023 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
1027 redef class AIsaExpr
1028 redef meth compile_expr(v)
1030 var e = v.compile_expr(n_expr)
1031 return n_type.stype.compile_cast(v, e)
1035 redef class AAsCastExpr
1036 redef meth compile_expr(v)
1038 var e = v.compile_expr(n_expr)
1039 n_type.stype.compile_type_check(v, e, self)
1044 redef class ATrueExpr
1045 redef meth compile_expr(v)
1047 return " TAG_Bool(true)"
1051 redef class AFalseExpr
1052 redef meth compile_expr(v)
1054 return " TAG_Bool(false)"
1058 redef class AIntExpr
1059 redef meth compile_expr(v)
1061 return " TAG_Int({n_number.text})"
1065 redef class AFloatExpr
1066 redef meth compile_expr(v)
1068 return "BOX_Float({n_float.text})"
1072 redef class ACharExpr
1073 redef meth compile_expr(v)
1075 return " TAG_Char({n_char.text})"
1079 redef class AStringFormExpr
1080 redef meth compile_expr(v)
1082 var prop = stype.local_class.select_method(once "with_native
".to_symbol)
1084 return prop.compile_constructor_call(v, stype , ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1087 # The raw string value
1088 protected meth string_text: String is abstract
1090 # The string in a C native format
1091 protected attr _cstring: String
1093 # The string length in bytes
1094 protected attr _cstring_length: Int
1096 # Compute _cstring and _cstring_length using string_text
1097 protected meth compute_string_info
1100 var str = string_text
1101 var res = new String
1103 while i < str.length do
1108 if c2 != '{' and c2 != '}' then
1118 _cstring_length = len
1122 redef class AStringExpr
1123 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1125 redef class AStartStringExpr
1126 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1128 redef class AMidStringExpr
1129 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1131 redef class AEndStringExpr
1132 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1135 redef class ASuperstringExpr
1136 redef meth compile_expr(v)
1138 var prop = stype.local_class.select_method(once "init".to_symbol)
1139 var recv = prop.compile_constructor_call(v, stype, new Array[String])
1141 var prop2 = stype.local_class.select_method(once "append
".to_symbol)
1143 var prop3 = stype.local_class.select_method(once "to_s
".to_symbol)
1144 for ne in n_exprs do
1145 var e = v.ensure_var(v.compile_expr(ne))
1146 if ne.stype != stype then
1147 v.add_assignment(e, prop3.compile_call(v, [e]))
1149 prop2.compile_call(v, [recv, e])
1156 redef class ANullExpr
1157 redef meth compile_expr(v)
1159 return " NIT_NULL /*null*/"
1163 redef class AArrayExpr
1164 redef meth compile_expr(v)
1166 var prop = stype.local_class.select_method(once "with_capacity
".to_symbol)
1167 var recv = prop.compile_constructor_call(v, stype, ["TAG_Int({n_exprs.length})"])
1169 var prop2 = stype.local_class.select_method(once "add
".to_symbol)
1170 for ne in n_exprs do
1171 var e = v.compile_expr(ne)
1172 prop2.compile_call(v, [recv, e])
1178 redef class ARangeExpr
1179 redef meth compile_expr(v)
1181 var prop = stype.local_class.select_method(propname)
1182 var e = v.compile_expr(n_expr)
1183 var e2 = v.compile_expr(n_expr2)
1184 return prop.compile_constructor_call(v, stype, [e, e2])
1186 # The constructor that must be used for the range
1187 protected meth propname: Symbol is abstract
1190 redef class ACrangeExpr
1191 redef meth propname do return once "init".to_symbol
1193 redef class AOrangeExpr
1194 redef meth propname do return once "without_last
".to_symbol
1197 redef class ASuperExpr
1198 redef meth compile_stmt(v)
1200 var e = compile_expr(v)
1201 if e != null then v.add_instr("{e};")
1204 redef meth compile_expr(v)
1206 var arity = v.nmc.method_params.length - 1
1207 if init_in_superclass != null then
1208 arity = init_in_superclass.signature.arity
1210 var args = new Array[String].with_capacity(arity + 1)
1211 args.add(v.nmc.method_params[0])
1212 if n_args.length != arity then
1213 for i in [0..arity[ do
1214 args.add(v.nmc.method_params[i + 1])
1218 args.add(v.compile_expr(na))
1221 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1222 if init_in_superclass != null then
1223 return init_in_superclass.compile_call(v, args)
1225 if prop.global.is_init then args.add("init_table
")
1226 return prop.compile_super_call(v, args)
1231 redef class AAttrExpr
1232 redef meth compile_expr(v)
1234 var e = v.compile_expr(n_expr)
1235 return prop.compile_access(v, e)
1239 redef class AAttrAssignExpr
1240 redef meth compile_stmt(v)
1242 var e = v.compile_expr(n_expr)
1243 var e2 = v.compile_expr(n_value)
1244 v.add_assignment(prop.compile_access(v, e), e2)
1247 redef class AAttrReassignExpr
1248 redef meth compile_stmt(v)
1250 var e1 = v.compile_expr(n_expr)
1251 var e2 = prop.compile_access(v, e1)
1252 var e3 = v.compile_expr(n_value)
1253 var e4 = assign_method.compile_call(v, [e2, e3])
1254 v.add_assignment(e2, e4)
1258 redef class ASendExpr
1259 redef meth compile_expr(v)
1261 var recv = v.compile_expr(n_expr)
1262 var cargs = new Array[String]
1264 for a in arguments do
1265 cargs.add(v.compile_expr(a))
1268 var e = prop.compile_call(v, cargs)
1269 if prop.global.is_init then
1270 v.invoke_super_init_calls_after(prop)
1275 redef meth compile_stmt(v)
1277 var e = compile_expr(v)
1279 v.add_instr(e + ";")
1284 redef class ASendReassignExpr
1285 redef meth compile_expr(v)
1287 var recv = v.compile_expr(n_expr)
1288 var cargs = new Array[String]
1290 for a in arguments do
1291 cargs.add(v.compile_expr(a))
1294 var e2 = read_prop.compile_call(v, cargs)
1295 var e3 = v.compile_expr(n_value)
1296 var e4 = assign_method.compile_call(v, [e2, e3])
1298 return prop.compile_call(v, cargs)
1302 redef class ANewExpr
1303 redef meth compile_expr(v)
1305 var cargs = new Array[String]
1306 for a in arguments do
1307 cargs.add(v.compile_expr(a))
1309 return prop.compile_constructor_call(v, stype, cargs)
1313 redef class AProxyExpr
1314 redef meth compile_expr(v)
1316 return v.compile_expr(n_expr)
1320 redef class AOnceExpr
1321 redef meth compile_expr(v)
1323 var i = v.new_number
1324 var cvar = v.cfc.get_var
1325 v.add_decl("static val_t once_value_
{cvar}_
{i}; static int once_bool_
{cvar}_
{i};")
1326 v.add_instr("if (once_bool_
{cvar}_
{i}) {cvar} = once_value_
{cvar}_
{i};")
1327 v.add_instr("else \
{")
1329 v.cfc.free_var(cvar)
1330 var e = v.compile_expr(n_expr)
1331 v.add_assignment(cvar, e)
1332 v.add_instr("once_value_
{cvar}_
{i} = {cvar};")
1333 v.add_instr("once_bool_
{cvar}_
{i} = true;")