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
= _variable_index
33 # Compile is expression node
34 meth compile_expr
(n
: PExpr): String
36 var i
= _variable_index
37 var s
= n
.compile_expr
(self)
42 if s
== variable
(_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 # Return the ith variable
70 protected meth variable
(i
: Int): String
75 # Next available variable number
76 attr _variable_index
: Int
78 # Total number of variable
79 attr _variable_index_max
: Int
81 # Return the next available variable
84 var v
= variable
(_variable_index
)
85 _variable_index
= _variable_index
+ 1
86 if _variable_index
> _variable_index_max
then
87 add_decl
("val_t {v};")
88 _variable_index_max
= _variable_index
93 # Mark the variable available
94 meth free_var
(v
: String)
97 if v
== variable
(_variable_index-1
) then
98 _variable_index
= _variable_index
- 1
102 # Clear all status related to a method body
108 _variable_index_max
= 0
111 # Association between nit variable and the corrsponding c variable
112 readable attr _varnames
: Map[Variable, String] = new HashMap[Variable, String]
114 # Is a "return" found in the method body
115 readable writable attr _has_return
: Bool
117 # Association between parameters and the corresponding c variables
118 readable writable attr _method_params
: Array[String]
120 # Current method compiled
121 readable writable attr _method
: MMSrcMethod
123 # Where a nit return must branch
124 readable writable attr _return_label
: String
126 # Where a nit break must branch
127 readable writable attr _break_label
: String
129 # Where a nit continue must branch
130 readable writable attr _continue_label
: String
132 # Variable where a functionnal nit return must store its value
133 readable writable attr _return_value
: String
135 # Generate an fprintf to display an error location
136 meth printf_locate_error
(node
: PNode): String
138 var s
= "fprintf(stderr, \""
139 if method != null then s.append(" in %s
")
140 s.append(" (%s
:%d
)\\n\
", ")
141 if method
!= null then s
.append
("LOCATE_{method.cname}, ")
142 s
.append
("LOCATE_{module.name}, {node.line_number});")
146 redef init(module: MMSrcModule)
152 meth invoke_super_init_calls_after
(start_prop
: MMMethod)
155 assert n
isa AConcreteInitPropdef
157 if n
.super_init_calls
.is_empty
then return
161 if start_prop
!= null then
162 while n
.super_init_calls
[i
] != start_prop
do
163 #s.append(" {n.super_init_calls[i]}")
167 #s.append(" {start_prop}")
169 while n
.explicit_super_init_calls
[j
] != start_prop
do
174 var stop_prop
: MMMethod
175 if j
< n
.explicit_super_init_calls
.length
then
176 stop_prop
= n
.explicit_super_init_calls
[j
]
178 var l
= n
.super_init_calls
.length
181 var p
= n
.super_init_calls
[i
]
182 if p
== stop_prop
then break
183 var cargs
= method_params
184 if p
.signature
.arity
== 0 then
185 cargs
= [method_params
[0]]
188 p
.compile_call
(self, cargs
)
193 # s.append(" {n.super_init_calls[i]}")
196 #if stop_prop != null then s.append(" (stop at {stop_prop})")
197 #n.printl("implicit calls in {n.method}: {s}")
201 ###############################################################################
204 # Compile a call on self for given arguments
205 # Most calls are compiled with a table access,
206 # primitive calles are inlined
207 # == and != are guarded and possibly inlined
208 meth compile_call
(v
: CompilerVisitor, cargs
: Array[String]): String
210 var i
= concrete_property
211 assert i
isa MMSrcMethod
212 if i
.node
isa AInternMethPropdef or
213 (i
.local_class
.name
== (once
"Array".to_symbol
) and name
== (once
"[]".to_symbol
))
215 var e
= i
.do_compile_inside
(v
, cargs
)
218 var ee
= once
"==".to_symbol
219 var ne
= once
"!=".to_symbol
221 var eqp
= signature
.recv
.select_method
(ee
)
222 var eqcall
= eqp
.compile_call
(v
, cargs
)
223 return "TAG_Bool(!UNTAG_Bool({eqcall}))"
225 if global
.is_init
then
227 cargs
.add
("init_table /*YYY*/")
230 var m
= "(({cname}_t)CALL({cargs[0]},{global.color_id}))"
231 var vcall
= "{m}({cargs.join(", ")}) /*{local_class}::{name}*/"
233 vcall
= "UNTAG_Bool({vcall})"
234 var obj
= once
"Object".to_symbol
235 if i
.local_class
.name
== obj
then
236 vcall
= "(({m}=={i.cname})?(IS_EQUAL_NN({cargs[0]},{cargs[1]})):({vcall}))"
238 vcall
= "TAG_Bool(({cargs.first} == {cargs[1]}) || (({cargs.first} != NIT_NULL) && {vcall}))"
240 if signature
.return_type
!= null then
243 v
.add_instr
(vcall
+ ";")
248 # Compile a call as constructor with given args
249 meth compile_constructor_call
(v
: CompilerVisitor, cargs
: Array[String]): String
252 var stype
= signature
.recv
253 v
.add_instr
("{recv} = NEW_{global.intro.cname}({cargs.join(", ")}); /*new {stype}*/")
257 # Compile a call as call-next-method on self with given args
258 meth compile_super_call
(v
: CompilerVisitor, cargs
: Array[String]): String
260 var m
= "(({cname}_t)CALL({cargs[0]},{color_id_for_super}))"
261 var vcall
= "{m}({cargs.join(", ")}) /*super {local_class}::{name}*/"
266 redef class MMAttribute
267 # Compile an acces on selffor a given reciever.
268 # Result is a valid C left-value for assigment
269 meth compile_access
(v
: CompilerVisitor, recv
: String): String
271 return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
275 redef class MMConcreteProperty
276 # Compile the property as a C property
277 meth compile_property_to_c
(v
: CompilerVisitor) do end
280 redef class MMSrcMethod
281 # Compile and declare the signature to C
282 protected meth decl_csignature
(v
: CompilerVisitor, args
: Array[String]): String
284 var params
= new Array[String]
285 var params_new
: Array[String]
286 if global
.is_init
then
287 params_new
= new Array[String]
289 params
.add
("val_t {args[0]}")
290 for i
in [0..signature
.arity
[ do
291 var p
= "val_t {args[i+1]}"
293 if params_new
!= null then params_new
.add
(p
)
295 if global
.is_init
then
296 params
.add
("int* init_table")
299 if signature
.return_type
!= null then
304 var p
= params
.join
(", ")
305 var s
= "{ret} {cname}({p})"
306 v
.add_decl
("typedef {ret} (* {cname}_t)({p});")
308 if params_new
!= null then
309 v
.add_decl
("val_t NEW_{cname}({params_new.join(", ")});")
314 redef meth compile_property_to_c
(v
)
317 var args
= new Array[String]
319 for i
in [0..signature
.arity
[ do
320 args
.add
(" param{i}")
322 var cs
= decl_csignature
(v
, args
)
323 v
.add_decl
("#define LOCATE_{cname} \"{full_name}\
"")
325 v
.add_instr
("{cs} \{")
330 v
.add_decl
("struct trace_t trace = \{NULL, LOCATE_{module.name}, {node.line_number}, LOCATE_{cname}};")
331 v
.add_instr
("trace.prev = tracehead; tracehead = &trace;")
332 var s
= do_compile_inside
(v
, args
)
333 v
.add_instr
("tracehead = trace.prev;")
335 v
.add_instr
("return;")
337 v
.add_instr
("return {s};")
340 ctx_old
.append
(v
.ctx
)
346 # Compile the method body inline
347 meth do_compile_inside
(v
: CompilerVisitor, params
: Array[String]): String is abstract
350 redef class MMReadImplementationMethod
351 redef meth do_compile_inside
(v
, params
)
353 return node
.prop
.compile_access
(v
, params
[0])
357 redef class MMWriteImplementationMethod
358 redef meth do_compile_inside
(v
, params
)
360 v
.add_assignment
(node
.prop
.compile_access
(v
, params
[0]), params
[1])
365 redef class MMMethSrcMethod
366 redef meth do_compile_inside
(v
, params
)
368 return node
.do_compile_inside
(v
, self, params
)
373 # Compile a subtype check to self
375 meth compile_cast
(v
: CompilerVisitor, recv
: String): String
377 # Fixme: handle formaltypes
378 var g
= local_class
.global
379 return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
382 # Compile a cast assertion
383 meth compile_type_check
(v
: CompilerVisitor, recv
: String, n
: PNode)
385 # Fixme: handle formaltypes
386 var g
= local_class
.global
387 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}*/;")
391 ###############################################################################
393 redef class AMethPropdef
394 # Compile the method body
395 meth do_compile_inside
(v
: CompilerVisitor, method
: MMSrcMethod, params
: Array[String]): String is abstract
398 redef class AConcreteMethPropdef
399 redef meth do_compile_inside
(v
, method
, params
)
401 var orig_meth
: MMLocalProperty = method
.global
.intro
402 var orig_sig
= orig_meth
.signature
.adaptation_to
(method
.signature
.recv
)
403 if n_signature
!= null then
404 var sig
= n_signature
405 assert sig
isa ASignature
406 for ap
in sig
.n_params
do
407 var cname
= v
.get_var
408 v
.varnames
[ap
.variable
] = cname
409 var orig_type
= orig_sig
[ap
.position
]
410 if not orig_type
< ap
.variable
.stype
then
411 # FIXME: do not test always
412 # FIXME: handle formal types
413 v
.add_instr
("/* check if p<{ap.variable.stype} with p:{orig_type} */")
414 ap
.variable
.stype
.compile_type_check
(v
, params
[ap
.position
+ 1], ap
)
416 v
.add_assignment
(cname
, params
[ap
.position
+ 1])
419 var old_method_params
= v
.method_params
420 var old_return_label
= v
.return_label
421 var old_return_value
= v
.return_value
422 var old_has_return
= v
.has_return
425 if self isa AConcreteInitPropdef then
426 itpos
= "VAL2OBJ({params[0]})->vft[{method.local_class.global.init_table_pos_id}].i"
427 # v.add_instr("printf(\"{method.full_name}: inittable[%d] = %d\\n\", {itpos}, init_table[{itpos}]);")
428 v
.add_instr
("if (init_table[{itpos}]) return;")
431 v
.method_params
= params
433 v
.return_label
= "return_label{v.new_number}"
434 if method
.signature
.return_type
!= null then
435 v
.return_value
= v
.get_var
436 v
.free_var
(v
.return_value
)
438 v
.return_value
= null
441 if self isa AConcreteInitPropdef then
442 v
.invoke_super_init_calls_after
(null)
444 if n_block
!= null then
445 v
.compile_stmt
(n_block
)
448 v
.add_instr
("{v.return_label}: while(false);")
450 if self isa AConcreteInitPropdef then
451 v
.add_instr
("init_table[{itpos}] = 1;")
453 var ret
= v
.return_value
454 v
.method_params
= old_method_params
455 v
.return_label
= old_return_label
456 v
.return_value
= old_return_value
457 v
.has_return
= old_has_return
462 redef class ADeferredMethPropdef
463 redef meth do_compile_inside
(v
, method
, params
)
465 v
.add_instr
("fprintf(stderr, \"Deferred method
%s called\
");")
466 v
.add_instr
(v
.printf_locate_error
(self))
467 v
.add_instr
("nit_exit(1);")
468 if method
.signature
.return_type
!= null then
476 redef class AExternMethPropdef
477 redef meth do_compile_inside
(v
, method
, params
)
479 var ename
= "{method.module.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
480 if n_extern
!= null then
481 ename
= n_extern
.text
482 ename
= ename
.substring
(1, ename
.length-2
)
484 var sig
= method
.signature
485 if params
.length
!= sig
.arity
+ 1 then
486 printl
("par:{params.length} sig:{sig.arity}")
488 var args
= new Array[String]
489 args
.add
(sig
.recv
.unboxtype
(params
[0]))
490 for i
in [0..sig
.arity
[ do
491 args
.add
(sig
[i
].unboxtype
(params
[i
+1]))
493 var s
= "{ename}({args.join(", ")})"
494 if sig
.return_type
!= null then
495 return sig
.return_type
.boxtype
(s
)
503 redef class AInternMethPropdef
504 redef meth do_compile_inside
(v
, method
, p
)
506 var c
= method
.local_class
.name
509 if c
== once
"Int".to_symbol
then
510 if n
== once
"object_id".to_symbol
then
512 else if n
== once
"unary -".to_symbol
then
513 s
= "TAG_Int(-UNTAG_Int({p[0]}))"
514 else if n
== once
"output".to_symbol
then
515 v
.add_instr
("printf(\"%d\\n\
", UNTAG_Int({p[0]}));")
516 else if n
== once
"ascii".to_symbol
then
517 s
= "TAG_Char(UNTAG_Int({p[0]}))"
518 else if n
== once
"succ".to_symbol
then
519 s
= "TAG_Int(UNTAG_Int({p[0]})+1)"
520 else if n
== once
"prec".to_symbol
then
521 s
= "TAG_Int(UNTAG_Int({p[0]})-1)"
522 else if n
== once
"to_f".to_symbol
then
523 s
= "BOX_Float((float)UNTAG_Int({p[0]}))"
524 else if n
== once
"+".to_symbol
then
525 s
= "TAG_Int(UNTAG_Int({p[0]})+UNTAG_Int({p[1]}))"
526 else if n
== once
"-".to_symbol
then
527 s
= "TAG_Int(UNTAG_Int({p[0]})-UNTAG_Int({p[1]}))"
528 else if n
== once
"*".to_symbol
then
529 s
= "TAG_Int(UNTAG_Int({p[0]})*UNTAG_Int({p[1]}))"
530 else if n
== once
"/".to_symbol
then
531 s
= "TAG_Int(UNTAG_Int({p[0]})/UNTAG_Int({p[1]}))"
532 else if n
== once
"%".to_symbol
then
533 s
= "TAG_Int(UNTAG_Int({p[0]})%UNTAG_Int({p[1]}))"
534 else if n
== once
"<".to_symbol
then
535 s
= "TAG_Bool(UNTAG_Int({p[0]})<UNTAG_Int({p[1]}))"
536 else if n
== once
">".to_symbol
then
537 s
= "TAG_Bool(UNTAG_Int({p[0]})>UNTAG_Int({p[1]}))"
538 else if n
== once
"<=".to_symbol
then
539 s
= "TAG_Bool(UNTAG_Int({p[0]})<=UNTAG_Int({p[1]}))"
540 else if n
== once
">=".to_symbol
then
541 s
= "TAG_Bool(UNTAG_Int({p[0]})>=UNTAG_Int({p[1]}))"
542 else if n
== once
"lshift".to_symbol
then
543 s
= "TAG_Int(UNTAG_Int({p[0]})<<UNTAG_Int({p[1]}))"
544 else if n
== once
"rshift".to_symbol
then
545 s
= "TAG_Int(UNTAG_Int({p[0]})>>UNTAG_Int({p[1]}))"
546 else if n
== once
"==".to_symbol
then
547 s
= "TAG_Bool(({p[0]})==({p[1]}))"
548 else if n
== once
"!=".to_symbol
then
549 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
551 else if c
== once
"Float".to_symbol
then
552 if n
== once
"object_id".to_symbol
then
553 s
= "TAG_Int((int)UNBOX_Float({p[0]}))"
554 else if n
== once
"unary -".to_symbol
then
555 s
= "BOX_Float(-UNBOX_Float({p[0]}))"
556 else if n
== once
"output".to_symbol
then
557 v
.add_instr
("printf(\"%f\\n\
", UNBOX_Float({p[0]}));")
558 else if n
== once
"to_i".to_symbol
then
559 s
= "TAG_Int((int)UNBOX_Float({p[0]}))"
560 else if n
== once
"+".to_symbol
then
561 s
= "BOX_Float(UNBOX_Float({p[0]})+UNBOX_Float({p[1]}))"
562 else if n
== once
"-".to_symbol
then
563 s
= "BOX_Float(UNBOX_Float({p[0]})-UNBOX_Float({p[1]}))"
564 else if n
== once
"*".to_symbol
then
565 s
= "BOX_Float(UNBOX_Float({p[0]})*UNBOX_Float({p[1]}))"
566 else if n
== once
"/".to_symbol
then
567 s
= "BOX_Float(UNBOX_Float({p[0]})/UNBOX_Float({p[1]}))"
568 else if n
== once
"<".to_symbol
then
569 s
= "TAG_Bool(UNBOX_Float({p[0]})<UNBOX_Float({p[1]}))"
570 else if n
== once
">".to_symbol
then
571 s
= "TAG_Bool(UNBOX_Float({p[0]})>UNBOX_Float({p[1]}))"
572 else if n
== once
"<=".to_symbol
then
573 s
= "TAG_Bool(UNBOX_Float({p[0]})<=UNBOX_Float({p[1]}))"
574 else if n
== once
">=".to_symbol
then
575 s
= "TAG_Bool(UNBOX_Float({p[0]})>=UNBOX_Float({p[1]}))"
577 else if c
== once
"Char".to_symbol
then
578 if n
== once
"object_id".to_symbol
then
579 s
= "TAG_Int(UNTAG_Char({p[0]}))"
580 else if n
== once
"unary -".to_symbol
then
581 s
= "TAG_Char(-UNTAG_Char({p[0]}))"
582 else if n
== once
"output".to_symbol
then
583 v
.add_instr
("printf(\"%c\
", (unsigned char)UNTAG_Char({p[0]}));")
584 else if n
== once
"ascii".to_symbol
then
585 s
= "TAG_Int((unsigned char)UNTAG_Char({p[0]}))"
586 else if n
== once
"succ".to_symbol
then
587 s
= "TAG_Char(UNTAG_Char({p[0]})+1)"
588 else if n
== once
"prec".to_symbol
then
589 s
= "TAG_Char(UNTAG_Char({p[0]})-1)"
590 else if n
== once
"to_i".to_symbol
then
591 s
= "TAG_Int(UNTAG_Char({p[0]})-'0')"
592 else if n
== once
"+".to_symbol
then
593 s
= "TAG_Char(UNTAG_Char({p[0]})+UNTAG_Char({p[1]}))"
594 else if n
== once
"-".to_symbol
then
595 s
= "TAG_Char(UNTAG_Char({p[0]})-UNTAG_Char({p[1]}))"
596 else if n
== once
"*".to_symbol
then
597 s
= "TAG_Char(UNTAG_Char({p[0]})*UNTAG_Char({p[1]}))"
598 else if n
== once
"/".to_symbol
then
599 s
= "TAG_Char(UNTAG_Char({p[0]})/UNTAG_Char({p[1]}))"
600 else if n
== once
"%".to_symbol
then
601 s
= "TAG_Char(UNTAG_Char({p[0]})%UNTAG_Char({p[1]}))"
602 else if n
== once
"<".to_symbol
then
603 s
= "TAG_Bool(UNTAG_Char({p[0]})<UNTAG_Char({p[1]}))"
604 else if n
== once
">".to_symbol
then
605 s
= "TAG_Bool(UNTAG_Char({p[0]})>UNTAG_Char({p[1]}))"
606 else if n
== once
"<=".to_symbol
then
607 s
= "TAG_Bool(UNTAG_Char({p[0]})<=UNTAG_Char({p[1]}))"
608 else if n
== once
">=".to_symbol
then
609 s
= "TAG_Bool(UNTAG_Char({p[0]})>=UNTAG_Char({p[1]}))"
610 else if n
== once
"==".to_symbol
then
611 s
= "TAG_Bool(({p[0]})==({p[1]}))"
612 else if n
== once
"!=".to_symbol
then
613 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
615 else if c
== once
"Bool".to_symbol
then
616 if n
== once
"object_id".to_symbol
then
617 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
618 else if n
== once
"unary -".to_symbol
then
619 s
= "TAG_Bool(-UNTAG_Bool({p[0]}))"
620 else if n
== once
"output".to_symbol
then
621 v
.add_instr
("(void)printf(UNTAG_Bool({p[0]})?\"true\\n\
":\"false\\n\
");")
622 else if n
== once
"ascii".to_symbol
then
623 s
= "TAG_Bool(UNTAG_Bool({p[0]}))"
624 else if n
== once
"to_i".to_symbol
then
625 s
= "TAG_Int(UNTAG_Bool({p[0]}))"
626 else if n
== once
"==".to_symbol
then
627 s
= "TAG_Bool(({p[0]})==({p[1]}))"
628 else if n
== once
"!=".to_symbol
then
629 s
= "TAG_Bool(({p[0]})!=({p[1]}))"
631 else if c
== once
"NativeArray".to_symbol
then
632 if n
== once
"object_id".to_symbol
then
633 s
= "TAG_Int(UNBOX_NativeArray({p[0]}))"
634 else if n
== once
"[]".to_symbol
then
635 s
= "UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]"
636 else if n
== once
"[]=".to_symbol
then
637 v
.add_instr
("UNBOX_NativeArray({p[0]})[UNTAG_Int({p[1]})]={p[2]};")
638 else if n
== once
"copy_to".to_symbol
then
639 v
.add_instr
("(void)memcpy(UNBOX_NativeArray({p[1]}), UNBOX_NativeArray({p[0]}), UNTAG_Int({p[2]})*sizeof(val_t));")
641 else if c
== once
"NativeString".to_symbol
then
642 if n
== once
"object_id".to_symbol
then
643 s
= "TAG_Int(UNBOX_NativeString({p[0]}))"
644 else if n
== once
"atoi".to_symbol
then
645 s
= "TAG_Int(atoi(UNBOX_NativeString({p[0]})))"
646 else if n
== once
"[]".to_symbol
then
647 s
= "TAG_Char(UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})])"
648 else if n
== once
"[]=".to_symbol
then
649 v
.add_instr
("UNBOX_NativeString({p[0]})[UNTAG_Int({p[1]})]=UNTAG_Char({p[2]});")
650 else if n
== once
"copy_to".to_symbol
then
651 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]}));")
653 else if n
== once
"object_id".to_symbol
then
654 s
= "TAG_Int((int){p[0]})"
655 else if n
== once
"sys".to_symbol
then
657 else if n
== once
"is_same_type".to_symbol
then
658 s
= "TAG_Bool((VAL2VFT({p[0]})==VAL2VFT({p[1]})))"
659 else if n
== once
"exit".to_symbol
then
660 v
.add_instr
("exit(UNTAG_Int({p[1]}));")
661 else if n
== once
"calloc_array".to_symbol
then
662 s
= "BOX_NativeArray((val_t*)malloc((UNTAG_Int({p[1]}) * sizeof(val_t))))"
663 else if n
== once
"calloc_string".to_symbol
then
664 s
= "BOX_NativeString((char*)malloc((UNTAG_Int({p[1]}) * sizeof(char))))"
667 v
.add_instr
("fprintf(stderr, \"Intern {n}\\n\
"); nit_exit(1);")
669 if method
.signature
.return_type
!= null and s
== null then
670 s
= "NIT_NULL /*stub*/"
676 ###############################################################################
679 # Compile the node as an expression
680 # Only the visitor should call it
681 meth compile_expr
(v
: CompilerVisitor): String is abstract
683 # Prepare a call of node as a statement
684 # Only the visitor should call it
685 # It's used for local variable managment
686 meth prepare_compile_stmt
(v
: CompilerVisitor) do end
688 # Compile the node as a statement
689 # Only the visitor should call it
690 meth compile_stmt
(v
: CompilerVisitor) do printl
("Error!")
693 redef class ABlockExpr
694 redef meth compile_stmt
(v
)
702 redef class AVardeclExpr
703 redef meth prepare_compile_stmt
(v
)
705 var cname
= v
.get_var
706 v
.varnames
[variable
] = cname
709 redef meth compile_stmt
(v
)
711 var cname
= v
.varnames
[variable
]
712 if n_expr
== null then
713 var t
= variable
.stype
714 v
.add_assignment
(cname
, "{t.default_cvalue} /*decl variable {variable.name}*/")
716 var e
= v
.compile_expr
(n_expr
)
717 v
.add_assignment
(cname
, e
)
722 redef class AReturnExpr
723 redef meth compile_stmt
(v
)
726 if n_expr
!= null then
727 var e
= v
.compile_expr
(n_expr
)
728 v
.add_assignment
(v
.return_value
, e
)
730 v
.add_instr
("goto {v.return_label};")
734 redef class ABreakExpr
735 redef meth compile_stmt
(v
)
737 v
.add_instr
("goto {v.break_label};")
741 redef class AContinueExpr
742 redef meth compile_stmt
(v
)
744 v
.add_instr
("goto {v.continue_label};")
748 redef class AAbortExpr
749 redef meth compile_stmt
(v
)
751 v
.add_instr
("fprintf(stderr, \"Aborted\
"); {v.printf_locate_error(self)} nit_exit(1);")
756 redef meth compile_stmt
(v
)
758 if n_block
!= null then
759 v
.compile_stmt
(n_block
)
765 redef meth compile_stmt
(v
)
767 var e
= v
.compile_expr
(n_expr
)
768 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
770 if n_then
!= null then
772 v
.compile_stmt
(n_then
)
775 if n_else
!= null then
776 v
.add_instr
("} else \{ /*if*/")
778 v
.compile_stmt
(n_else
)
785 redef class AIfexprExpr
786 redef meth compile_expr
(v
)
788 var e
= v
.compile_expr
(n_expr
)
789 v
.add_instr
("if (UNTAG_Bool({e})) \{ /*if*/")
792 var e
= v
.ensure_var
(v
.compile_expr
(n_then
))
794 v
.add_instr
("} else \{ /*if*/")
797 var e2
= v
.ensure_var
(v
.compile_expr
(n_else
))
798 v
.add_assignment
(e
, e2
)
805 redef class AControlableBlock
806 meth compile_inside_block
(v
: CompilerVisitor) is abstract
807 redef meth compile_stmt
(v
)
809 var old_break_label
= v
.break_label
810 var old_continue_label
= v
.continue_label
811 var id
= v
.new_number
812 v
.break_label
= "break_{id}"
813 v
.continue_label
= "continue_{id}"
815 compile_inside_block
(v
)
818 v
.break_label
= old_break_label
819 v
.continue_label
= old_continue_label
823 redef class AWhileExpr
824 redef meth compile_inside_block
(v
)
826 v
.add_instr
("while (true) \{ /*while*/")
828 var e
= v
.compile_expr
(n_expr
)
829 v
.add_instr
("if (!UNTAG_Bool({e})) break; /* while*/")
831 if n_block
!= null then
832 v
.compile_stmt
(n_block
)
834 v
.add_instr
("{v.continue_label}: while(0);")
837 v
.add_instr
("{v.break_label}: while(0);")
842 redef meth compile_inside_block
(v
)
844 v
.compile_stmt
(n_vardecl
)
848 redef class AForVardeclExpr
849 redef meth compile_stmt
(v
)
851 var e
= v
.compile_expr
(n_expr
)
852 var prop
= n_expr
.stype
.select_method
(once
"iterator".to_symbol
)
854 printl
("No iterator")
857 var ittype
= prop
.signature
.return_type
860 v
.add_assignment
(iter
, prop
.compile_call
(v
, [e
]))
861 var prop2
= ittype
.select_method
(once
"is_ok".to_symbol
)
862 if prop2
== null then
866 var prop3
= ittype
.select_method
(once
"item".to_symbol
)
867 if prop3
== null then
871 var prop4
= ittype
.select_method
(once
"next".to_symbol
)
872 if prop4
== null then
876 v
.add_instr
("while (true) \{ /*for*/")
879 v
.add_assignment
(ok
, prop2
.compile_call
(v
, [iter
]))
880 v
.add_instr
("if (!UNTAG_Bool({ok})) break; /*for*/")
882 var e
= prop3
.compile_call
(v
, [iter
])
884 v
.varnames
[variable
] = e
886 assert par
isa AForExpr
887 var n_block
= par
.n_block
888 if n_block
!= null then
889 v
.compile_stmt
(n_block
)
891 v
.add_instr
("{v.continue_label}: while(0);")
892 e
= prop4
.compile_call
(v
, [iter
])
896 v
.add_instr
("{v.break_label}: while(0);")
900 redef class AAssertExpr
901 redef meth compile_stmt
(v
)
903 var e
= v
.compile_expr
(n_expr
)
906 s
= " '{n_id.text}' "
908 v
.add_instr
("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit
(1);}")
913 redef meth compile_expr(v)
915 return " {v.varnames[variable]} /*{variable.name}*/"
919 redef class AVarAssignExpr
920 redef meth compile_stmt(v)
922 var e = v.compile_expr(n_value)
923 v.add_assignment(v.varnames[variable], "{e} /*{variable.name}=*/")
927 redef class AVarReassignExpr
928 redef meth compile_stmt(v)
930 var e1 = v.varnames[variable]
931 var e2 = v.compile_expr(n_value)
932 var e3 = assign_method.compile_call(v, [e1, e2])
933 v.add_assignment(v.varnames[variable], "{e3} /*{variable.name}*/")
937 redef class ASelfExpr
938 redef meth compile_expr(v)
940 return v.method_params[0]
945 redef meth compile_expr(v)
947 var e = v.ensure_var(v.compile_expr(n_expr))
948 v.add_instr("if (!UNTAG_Bool({e})) \
{ /* or */")
951 var e2 = v.compile_expr(n_expr2)
952 v.add_assignment(e, e2)
960 redef meth compile_expr(v)
962 var e = v.ensure_var(v.compile_expr(n_expr))
963 v.add_instr("if (UNTAG_Bool({e})) \
{ /* and */")
966 var e2 = v.compile_expr(n_expr2)
967 v.add_assignment(e, e2)
975 redef meth compile_expr(v)
977 return " TAG_Bool(!UNTAG_Bool({v.compile_expr(n_expr)}))"
982 redef meth compile_expr(v)
984 var e = v.compile_expr(n_expr)
985 var e2 = v.compile_expr(n_expr2)
986 return "TAG_Bool(IS_EQUAL_NN({e},{e2}))"
991 redef meth compile_expr(v)
993 var e = v.compile_expr(n_expr)
994 return n_type.stype.compile_cast(v, e)
998 redef class AAsCastExpr
999 redef meth compile_expr(v)
1001 var e = v.compile_expr(n_expr)
1002 n_type.stype.compile_type_check(v, e, self)
1007 redef class ATrueExpr
1008 redef meth compile_expr(v)
1010 return " TAG_Bool(true)"
1014 redef class AFalseExpr
1015 redef meth compile_expr(v)
1017 return " TAG_Bool(false)"
1021 redef class AIntExpr
1022 redef meth compile_expr(v)
1024 return " TAG_Int({n_number.text})"
1028 redef class AFloatExpr
1029 redef meth compile_expr(v)
1031 return "BOX_Float({n_float.text})"
1035 redef class ACharExpr
1036 redef meth compile_expr(v)
1038 return " TAG_Char({n_char.text})"
1042 redef class AStringFormExpr
1043 redef meth compile_expr(v)
1045 var prop = stype.select_method(once "with_native
".to_symbol)
1047 return prop.compile_constructor_call(v, ["BOX_NativeString(\
"{_cstring}\")", "TAG_Int({_cstring_length})"])
1050 # The raw string value
1051 protected meth string_text: String is abstract
1053 # The string in a C native format
1054 protected attr _cstring: String
1056 # The string length in bytes
1057 protected attr _cstring_length: Int
1059 # Compute _cstring and _cstring_length using string_text
1060 protected meth compute_string_info
1063 var str = string_text
1064 var res = new String
1066 while i < str.length do
1071 if c2 != '{' and c2 != '}' then
1081 _cstring_length = len
1085 redef class AStringExpr
1086 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1088 redef class AStartStringExpr
1089 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1091 redef class AMidStringExpr
1092 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1094 redef class AEndStringExpr
1095 redef meth string_text do return n_string.text.substring(1, n_string.text.length - 2)
1098 redef class ASuperstringExpr
1099 redef meth compile_expr(v)
1101 var prop = stype.select_method(once "init".to_symbol)
1102 var recv = prop.compile_constructor_call(v, new Array[String])
1104 var prop2 = stype.select_method(once "append
".to_symbol)
1106 var prop3 = stype.select_method(once "to_s
".to_symbol)
1107 for ne in n_exprs do
1108 var e = v.ensure_var(v.compile_expr(ne))
1109 if ne.stype != stype then
1110 v.add_assignment(e, prop3.compile_call(v, [e]))
1112 prop2.compile_call(v, [recv, e])
1119 redef class ANullExpr
1120 redef meth compile_expr(v)
1122 return " NIT_NULL /*null*/"
1126 redef class AArrayExpr
1127 redef meth compile_expr(v)
1129 var prop = stype.select_method(once "with_capacity
".to_symbol)
1130 var recv = prop.compile_constructor_call(v,["TAG_Int({n_exprs.length})"])
1132 var prop2 = stype.select_method(once "add
".to_symbol)
1133 for ne in n_exprs do
1134 var e = v.compile_expr(ne)
1135 prop2.compile_call(v, [recv, e])
1141 redef class ARangeExpr
1142 redef meth compile_expr(v)
1144 var prop = stype.select_method(propname)
1145 var e = v.compile_expr(n_expr)
1146 var e2 = v.compile_expr(n_expr2)
1147 return prop.compile_constructor_call(v, [e, e2])
1149 # The constructor that must be used for the range
1150 protected meth propname: Symbol is abstract
1153 redef class ACrangeExpr
1154 redef meth propname do return once "init".to_symbol
1156 redef class AOrangeExpr
1157 redef meth propname do return once "without_last
".to_symbol
1160 redef class ASuperExpr
1161 redef meth compile_stmt(v)
1163 var e = compile_expr(v)
1164 if e != null then v.add_instr("{e};")
1167 redef meth compile_expr(v)
1169 var arity = v.method_params.length - 1
1170 if init_in_superclass != null then
1171 arity = init_in_superclass.signature.arity
1173 var args = new Array[String].with_capacity(arity + 1)
1174 args.add(v.method_params[0])
1175 if n_args.length != arity then
1176 for i in [0..arity[ do
1177 args.add(v.method_params[i + 1])
1181 args.add(v.compile_expr(na))
1184 #return "{prop.cname}({args.join(", ")}) /*super {prop.local_class}::{prop.name}*/"
1185 if init_in_superclass != null then
1186 return init_in_superclass.compile_call(v, args)
1188 if prop.global.is_init then args.add("init_table
")
1189 return prop.compile_super_call(v, args)
1194 redef class AAttrExpr
1195 redef meth compile_expr(v)
1197 var e = v.compile_expr(n_expr)
1198 return prop.compile_access(v, e)
1202 redef class AAttrAssignExpr
1203 redef meth compile_stmt(v)
1205 var e = v.compile_expr(n_expr)
1206 var e2 = v.compile_expr(n_value)
1207 v.add_assignment(prop.compile_access(v, e), e2)
1210 redef class AAttrReassignExpr
1211 redef meth compile_stmt(v)
1213 var e1 = v.compile_expr(n_expr)
1214 var e2 = prop.compile_access(v, e1)
1215 var e3 = v.compile_expr(n_value)
1216 var e4 = assign_method.compile_call(v, [e2, e3])
1217 v.add_assignment(e2, e4)
1221 redef class ASendExpr
1222 redef meth compile_expr(v)
1224 var recv = v.compile_expr(n_expr)
1225 var cargs = new Array[String]
1227 for a in arguments do
1228 cargs.add(v.compile_expr(a))
1231 var e = prop.compile_call(v, cargs)
1232 if prop.global.is_init then
1233 v.invoke_super_init_calls_after(prop)
1238 redef meth compile_stmt(v)
1240 var e = compile_expr(v)
1242 v.add_instr(e + ";")
1247 redef class ASendReassignExpr
1248 redef meth compile_expr(v)
1250 var recv = v.compile_expr(n_expr)
1251 var cargs = new Array[String]
1253 for a in arguments do
1254 cargs.add(v.compile_expr(a))
1257 var e2 = read_prop.compile_call(v, cargs)
1258 var e3 = v.compile_expr(n_value)
1259 var e4 = assign_method.compile_call(v, [e2, e3])
1261 return prop.compile_call(v, cargs)
1265 redef class ANewExpr
1266 redef meth compile_expr(v)
1268 var cargs = new Array[String]
1269 for a in arguments do
1270 cargs.add(v.compile_expr(a))
1272 return prop.compile_constructor_call(v, cargs)
1276 redef class AProxyExpr
1277 redef meth compile_expr(v)
1279 return v.compile_expr(n_expr)
1283 redef class AOnceExpr
1284 redef meth compile_expr(v)
1286 var i = v.new_number
1287 var cvar = v.get_var
1288 v.add_decl("static val_t once_value_
{cvar}_
{i}; static int once_bool_
{cvar}_
{i};")
1289 v.add_instr("if (once_bool_
{cvar}_
{i}) {cvar} = once_value_
{cvar}_
{i};")
1290 v.add_instr("else \
{")
1293 var e = v.compile_expr(n_expr)
1294 v.add_assignment(cvar, e)
1295 v.add_instr("once_value_
{cvar}_
{i} = {cvar};")
1296 v.add_instr("once_bool_
{cvar}_
{i} = true;")