1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 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 # Generate C code from intermediate code representation
18 package compiling_icode
21 private import analysis
25 # Compiler context from ICode to C
26 class I2CCompilerVisitor
28 var _ids
: HashMap[Object, String] = new HashMap[Object, String]
29 # Associate other things
30 var _ids2
: HashMap[Object, String] = new HashMap[Object, String]
32 # Return the string associated with a register
33 fun register
(e
: IRegister): String
35 if e
.stype
isa MMTypeNone then return "NIT_NULL"
37 if closure
and not e
.is_local
then ids
= _ids2
38 if ids
.has_key
(e
) then
43 # The register is dead
49 var strs
: HashMap[Int, String]
50 if e
.in_tag_slots
then
51 strs
= once
new HashMap[Int, String]
52 if not strs
.has_key
(i
) then strs
[i
] = "REGB{i}"
53 else if closure
and not e
.is_local
then
54 strs
= once
new HashMap[Int, String]
55 if not strs
.has_key
(i
) then strs
[i
] = "closctx->REG[{i}]"
57 strs
= once
new HashMap[Int, String]
58 if not strs
.has_key
(i
) then strs
[i
] = "fra.me.REG[{i}]"
67 # Return the strings associated with registers
68 fun registers
(a
: Collection[IRegister]): Array[String]
70 var r
= new Array[String].with_capacity
(a
.length
)
77 var _last_number
: Int = 0
78 # Give a new unique number (unique for the visitor)
85 # Return the string associated with a escape label
86 fun lab
(e
: ISeq): String
88 if _ids
.has_key
(e
) then
91 var s
= "label{new_number}"
97 # The rank (number) of each closure
98 readable var _closures
: HashMap[IClosureDecl, String] = new HashMap[IClosureDecl, String]
100 # The functionnal type of each closure
101 readable var _clostypes
: HashMap[IClosureDecl, String] = new HashMap[IClosureDecl, String]
103 # label locally accessibles
104 readable writable var _local_labels
: HashSet[ISeq] = new HashSet[ISeq]
106 # Not local escaped labels
107 # The integer value is an index identifying the label
108 readable writable var _escaped_labels
: HashMap[ISeq, Int] = new HashMap[ISeq, Int]
110 # Register a escape to a non local label and return an index identifying the label
111 fun register_escape_label
(e
: ISeq): Int
113 if _escaped_labels
.has_key
(e
) then
114 return _escaped_labels
[e
]
116 var res
= _escaped_labels
.length
+ 1
117 _escaped_labels
[e
] = res
122 # Add a C label mark (if needed)
123 fun add_label
(e
: ISeq)
125 if _ids
.has_key
(e
) then
126 add_instr
("{_ids[e]}: while(0);")
130 # Add a goto to a label (even outside a closure)
131 fun add_goto
(seq
: ISeq)
133 if local_labels
.has
(seq
) then
134 add_instr
("goto {lab(seq)};")
137 var ind
= register_escape_label
(seq
)
138 add_instr
("closctx->has_broke = {ind};")
139 add_instr
("goto {lab(return_label.as(not null))};")
143 # Association between IEscapeMarks and visited ISeq
144 readable var _marks_to_seq
: Map[IEscapeMark, ISeq] = new HashMap[IEscapeMark, ISeq]
146 # Are we in a closure ?
147 readable writable var _closure
: Bool = false
149 # The current compiler visitor
150 readable var _visitor
: CompilerVisitor
152 # The current compiled iroutine
153 readable var _iroutine
: IRoutine
155 # The return label of the current compiling C function
156 readable writable var _return_label
: nullable ISeq = null
158 fun add_decl
(s
: String)
163 # Prepare a new instuction (indent, comment)
164 # Caller must ensure to add a new line to finish its instr
165 fun new_instr
: Writer
167 var w
= visitor
.writer
168 var l
= _next_location
170 visitor
.add_indent
(w
)
172 w
.add
(l
.file
.filename
)
174 w
.add
(l
.line_start
.to_s
)
176 _next_location
= null
178 visitor
.add_indent
(w
)
182 fun add_instr
(s
: String)
184 new_instr
.add
(s
).add
("\n")
197 fun add_assignment
(to
, from
: String)
199 visitor
.add_assignment
(to
, from
)
202 var _last_location
: nullable Location = null
203 var _next_location
: nullable Location = null
205 # Add location information in a comment
206 # Do nothing if the last location added is the same
207 fun add_location
(l
: nullable Location)
209 var last
= _last_location
210 if last
== l
or l
== null then return
212 if last
!= null and last
.file
== l
.file
and last
.line_start
== l
.line_start
then
219 # The C fonction name of the iroutine
220 readable var _basecname
: String
222 init(v
: CompilerVisitor, ir
: IRoutine, cname
: String)
231 # Declare and start a C function that match the routine
232 # Return what must be given to compile_inside_to_c or to compile_to_c
233 # After the method, an openinig { and and indent is added.
234 # So, do not forget to add a sub_context, to unintent and to add a closing }
235 fun compile_signature_to_c
(v
: CompilerVisitor, cname
: String, human_name
: nullable String, before_params
, after_params
: nullable String): Array[String]
237 var cargs
= new Array[String]
238 var cparams
= new Array[String]
239 if before_params
!= null then cparams
.add
(before_params
)
240 for i
in [0..params
.length
[ do
242 cparams
.add
("val_t p{i}")
244 if closure_decls
!= null then
245 cparams
.add
("struct stack_frame_t *closctx_param")
246 for i
in [0..closure_decls
.length
[ do
247 var closcn
= "CLOS_{cname}_{i}"
248 var cs
= closure_decls
[i
].closure
.signature
249 var subparams
= new Array[String] # Parameters of the closure
250 subparams
.add
("struct stack_frame_t *")
251 for j
in [0..cs
.arity
[ do
252 subparams
.add
("val_t")
255 if cs
.return_type
!= null then rr
= "val_t"
256 v
.add_decl
("typedef {rr} (*{closcn})({subparams.join(", ")});")
257 cargs
.add
("clos_fun{i}")
258 cparams
.add
("fun_t clos_fun{i}")
261 if after_params
!= null then cparams
.add
(after_params
)
263 if result
!= null then r
= "val_t"
265 if cparams
.is_empty
then
268 p
= cparams
.join
(", ")
270 if human_name
!= null then v
.add_decl
("static const char * const LOCATE_{cname} = \"{human_name}\
";")
271 v
.add_decl
("{r} {cname}({p});")
272 v
.add_decl
("typedef {r} (*{cname}_t)({p});")
273 v
.add_instr
("{r} {cname}({p})\{")
278 # Compile the body of the routine, return the result value is any
279 fun compile_inside_to_c
(v
: I2CCompilerVisitor, args
: Array[String]): nullable String
281 # Create and push the stack frame
283 if location
!= null then
284 ll
= location
.line_start
286 # Encapsulate the frame ('me') in a larger structure ('fra') that has enough space to store the local variables (REG)
287 if std_slots_nb
> 1 then
288 v
.add_decl
("struct \{struct stack_frame_t me; val_t MORE_REG[{std_slots_nb-1}];\} fra;")
290 v
.add_decl
("struct \{struct stack_frame_t me;\} fra;")
292 v
.add_instr
("fra.me.prev = stack_frame_head; stack_frame_head = &fra.me;")
293 v
.add_instr
("fra.me.file = LOCATE_{v.visitor.mmmodule.cname};")
294 v
.add_instr
("fra.me.line = {ll};")
295 v
.add_instr
("fra.me.meth = LOCATE_{v.basecname};")
296 v
.add_instr
("fra.me.has_broke = 0;")
297 v
.add_instr
("fra.me.REG_size = {std_slots_nb};")
299 # Declare/initialize local variables
300 for i
in [0..std_slots_nb
[ do
301 v
.add_instr
("fra.me.REG[{i}] = NIT_NULL;")
303 for i
in [0..tag_slots_nb
[ do
304 v
.add_decl
("val_t REGB{i};")
306 var iclosdecls
= closure_decls
307 if iclosdecls
!= null then
308 v
.add_decl
("fun_t CREG[{iclosdecls.length}];")
309 v
.add_instr
("fra.me.closure_ctx = closctx_param;")
310 v
.add_instr
("fra.me.closure_funs = CREG;")
314 if r
.slot_index
!= null then v
.add_assignment
(v
.register
(r
), args
[k
])
317 if iclosdecls
!= null then
318 for i
in [0..iclosdecls
.length
[ do
319 var iclosdecl
= iclosdecls
[i
]
320 v
.add_instr
("CREG[{i}] = {args[params.length+i]};")
321 v
.closures
[iclosdecl
] = i
.to_s
322 var cs
= iclosdecl
.closure
.signature
# Closure signature
323 var subparams
= new Array[String] # Parameters of the closure
324 subparams
.add
("struct stack_frame_t *")
325 for j
in [0..cs
.arity
[ do
330 if cs
.return_type
!= null then r
= "val_t"
331 v
.clostypes
[iclosdecl
] = "{r} (*)({subparams.join(", ")})"
334 v
.add_decl
("val_t tmp;")
337 var old_rl
= v
.return_label
338 v
.return_label
= body
343 v
.add_instr
("stack_frame_head = fra.me.prev;")
344 v
.return_label
= old_rl
353 # Full compilation of the routine
354 # cv must be in the correct function
355 fun compile_to_c
(cv
: CompilerVisitor, cname
: String, args
: Array[String]): nullable String
357 var v
= new I2CCompilerVisitor(cv
, self, cname
)
358 return compile_inside_to_c
(v
, args
)
363 # Full compilation of the icode
364 fun compile_to_c
(v
: I2CCompilerVisitor) is abstract
366 # Is a result really needed
367 private fun need_result
: Bool
370 return r
!= null and r
.slot_index
!= null
373 # Store s in the result value of self
374 private fun store_result
(v
: I2CCompilerVisitor, w
: nullable Writer)
377 if r
!= null and r
.slot_index
!= null then
380 w2
.add
(v
.register
(r
))
384 else if w
!= null and not is_pure
then
385 # ICode with side effects must be evaluated
386 # even if the result is not wanted
393 # Prepare a writer if the expression icode need to be compiled
394 # * Result assigment is automatic if needed
395 private fun new_result
(v
: I2CCompilerVisitor): Writer
397 assert need_result
or not is_pure
400 if r
!= null and r
.slot_index
!= null then
401 w2
.add
(v
.register
(r
))
411 redef fun compile_to_c
(v
)
413 v
.add_location
(location
)
414 v
.local_labels
.add
(self)
415 var mark
= iescape_mark
416 if mark
!= null then v
.marks_to_seq
[mark
] = self
425 redef fun compile_to_c
(v
)
427 v
.add_location
(location
)
429 w
.add
("if (UNTAG_Bool(")
430 w
.add
(v
.register
(expr
))
432 if not then_seq
.icodes
.is_empty
then
434 then_seq
.compile_to_c
(v
)
437 if not else_seq
.icodes
.is_empty
then
438 v
.add_instr
("\} else \{")
440 else_seq
.compile_to_c
(v
)
448 redef fun compile_to_c
(v
)
450 v
.add_location
(location
)
451 v
.local_labels
.add
(self)
452 var mark
= iescape_mark
453 if mark
!= null then v
.marks_to_seq
[mark
] = self
454 v
.add_instr
("while(1) \{")
466 redef fun compile_to_c
(v
)
468 v
.add_location
(location
)
469 v
.add_goto
(v
.marks_to_seq
[iescape_mark
])
474 redef fun compile_to_c
(v
)
476 v
.add_location
(location
)
477 var args
= v
.registers
(exprs
)
479 # Compile closure definitions
480 var old_el
= v
.escaped_labels
481 var closdefs
= closure_defs
482 var closctx
: nullable String = null # The closure context of closdefs
483 if closdefs
!= null then
484 # Get the closure context
488 closctx
= "(&(fra.me))"
491 # First aditionnal arguments is the closure context
494 # We are in a new escape boundary
495 v
.escaped_labels
= new HashMap[ISeq, Int]
497 # Compile each closures and add each sub-function as an other additionnal parameter
498 for cd
in closdefs
do
500 var cn
= cd
.compile_closure
(v
)
508 # Compile the real call
509 var call
= compile_call_to_c
(v
, args
)
510 var res
: nullable Writer = call
513 if closctx
!= null then
514 var els
= v
.escaped_labels
515 v
.escaped_labels
= old_el
516 # Is there possible escapes?
517 if not els
.is_empty
then
518 # Call in a tmp variable to avoid 'break' overwrite
532 # What are the expected escape indexes
533 v
.new_instr
.add
("switch (").add
(closctx
).add
("->has_broke) \{\n")
535 # No escape occured, continue as usual
536 v
.add_instr
("case 0: break;")
537 var lls
= v
.local_labels
538 var iels
= els
.iterator
539 var forward_escape
= false
543 # Local escape occured
544 # Clear the has_broke information and go to the target
545 v
.new_instr
.add
("case ").add
(iels
.item
.to_s
).add
(": ").add
(closctx
).add
("->has_broke = 0; goto ").add
(v
.lab
(seq
)).add
(";\n")
547 # Forward escape occured: register the escape label
549 v
.register_escape_label
(seq
)
550 forward_escape
= true
554 # If forward escape occured, just pass to the next one
555 if forward_escape
then
556 # Do not need to copy 'has_broke' value since it is shared by the next one.
557 # So just exit the C function.
558 v
.new_instr
.add
("default: goto ").add
(v
.lab
(v
.return_label
.as(not null))).add
(";\n")
566 var w
= new_result
(v
)
571 # The single invocation witout fancy stuffs
572 private fun compile_call_to_c
(v
: I2CCompilerVisitor, args
: Array[String]): Writer is abstract
576 redef fun compile_call_to_c
(v
, args
)
580 # do not compile explicit calls from native methods
581 # theses are really manually called in the native implementation
582 if is_explicit_from_extern
then return w
585 if prop
.global
.is_init
then args
.add
("init_table")
586 w
.add
(prop
.global
.meth_call
)
590 w
.add_all
(args
, ", ")
597 redef fun compile_call_to_c
(v
, args
)
599 # do not compile explicit calls from native methods
600 # theses are really manually called in the native implementation
601 if is_explicit_from_extern
then return new Writer
604 if prop
.global
.is_init
then args
.add
("init_table")
606 w
.add
(prop
.super_meth_call
)
610 w
.add_all
(args
, ", ")
617 redef fun compile_call_to_c
(v
, args
)
621 # do not compile explicit calls from native methods
622 # theses are really manually called in the native implementation
623 if is_explicit_from_extern
then return w
626 w
.add
(stype
.local_class
.to_s
)
628 w
.add
(property
.global
.intro
.cname
)
630 w
.add_all
(args
, ", ")
636 redef class IAllocateInstance
637 redef fun compile_to_c
(v
)
639 v
.add_location
(location
)
640 var w
= new_result
(v
)
642 w
.add
(stype
.local_class
.cname
)
647 redef class ICheckInstance
648 redef fun compile_to_c
(v
)
650 v
.add_location
(location
)
651 var w
= new_result
(v
)
653 w
.add
(stype
.local_class
.cname
)
655 w
.add
(v
.register
(expr
))
660 redef class IInitAttributes
661 redef fun compile_to_c
(v
)
663 v
.add_location
(location
)
665 w
.add
("INIT_ATTRIBUTES__")
666 w
.add
(stype
.local_class
.cname
)
668 w
.add
(v
.register
(expr
))
673 redef class IStaticCall
674 redef fun compile_call_to_c
(v
, args
)
677 if prop
.global
.is_init
then args
.add
("init_table")
679 w
.add
(property
.cname
)
681 w
.add_all
(args
, ", ")
688 redef fun compile_to_c
(v
)
690 v
.add_location
(location
)
691 if method
.is_intern
then
692 compile_intern_method_to_c
(v
)
693 else if not method
.global
.is_init
then
694 compile_extern_method_to_c
(v
)
698 fun compile_extern_method_to_c
(v
: I2CCompilerVisitor)
700 var ename
= "{method.friendly_extern_name(method.local_class)}___out"
702 var sig
= method
.signature
703 assert exprs
.length
== sig
.arity
+ 1
705 var regs
= v
.registers
(exprs
)
707 var args
= new Array[String]
709 for i
in [0..sig
.arity
[ do
712 var s
= "{ename}({args.join(", ")})"
714 if need_result
then s
= s
# sig.return_type.boxtype(s)
715 var w
= new_result
(v
)
719 fun compile_intern_method_to_c
(v
: I2CCompilerVisitor)
721 var sig
= method
.signature
722 assert exprs
.length
== sig
.arity
+ 1
723 var c
= method
.local_class
.name
725 var regs
= v
.registers
(exprs
)
726 var s
: nullable String = null
727 if c
== once
"Int".to_symbol
then
728 if n
== once
"object_id".to_symbol
then
730 else if n
== once
"unary -".to_symbol
then
731 s
= "TAG_Int(-UNTAG_Int({regs[0]}))"
732 else if n
== once
"output".to_symbol
then
733 s
= "printf(\"%ld\\n\
", UNTAG_Int({regs[0]}));"
734 else if n
== once
"ascii".to_symbol
then
735 s
= "TAG_Char(UNTAG_Int({regs[0]}))"
736 else if n
== once
"succ".to_symbol
then
737 s
= "TAG_Int(UNTAG_Int({regs[0]})+1)"
738 else if n
== once
"prec".to_symbol
then
739 s
= "TAG_Int(UNTAG_Int({regs[0]})-1)"
740 else if n
== once
"to_f".to_symbol
then
741 s
= "BOX_Float((float)UNTAG_Int({regs[0]}))"
742 else if n
== once
"+".to_symbol
then
743 s
= "TAG_Int(UNTAG_Int({regs[0]})+UNTAG_Int({regs[1]}))"
744 else if n
== once
"-".to_symbol
then
745 s
= "TAG_Int(UNTAG_Int({regs[0]})-UNTAG_Int({regs[1]}))"
746 else if n
== once
"*".to_symbol
then
747 s
= "TAG_Int(UNTAG_Int({regs[0]})*UNTAG_Int({regs[1]}))"
748 else if n
== once
"/".to_symbol
then
749 s
= "TAG_Int(UNTAG_Int({regs[0]})/UNTAG_Int({regs[1]}))"
750 else if n
== once
"%".to_symbol
then
751 s
= "TAG_Int(UNTAG_Int({regs[0]})%UNTAG_Int({regs[1]}))"
752 else if n
== once
"<".to_symbol
then
753 s
= "TAG_Bool(UNTAG_Int({regs[0]})<UNTAG_Int({regs[1]}))"
754 else if n
== once
">".to_symbol
then
755 s
= "TAG_Bool(UNTAG_Int({regs[0]})>UNTAG_Int({regs[1]}))"
756 else if n
== once
"<=".to_symbol
then
757 s
= "TAG_Bool(UNTAG_Int({regs[0]})<=UNTAG_Int({regs[1]}))"
758 else if n
== once
">=".to_symbol
then
759 s
= "TAG_Bool(UNTAG_Int({regs[0]})>=UNTAG_Int({regs[1]}))"
760 else if n
== once
"lshift".to_symbol
then
761 s
= "TAG_Int(UNTAG_Int({regs[0]})<<UNTAG_Int({regs[1]}))"
762 else if n
== once
"rshift".to_symbol
then
763 s
= "TAG_Int(UNTAG_Int({regs[0]})>>UNTAG_Int({regs[1]}))"
764 else if n
== once
"==".to_symbol
then
765 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
766 else if n
== once
"!=".to_symbol
then
767 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
769 else if c
== once
"Float".to_symbol
then
770 if n
== once
"object_id".to_symbol
then
771 s
= "TAG_Int((bigint)UNBOX_Float({regs[0]}))"
772 else if n
== once
"unary -".to_symbol
then
773 s
= "BOX_Float(-UNBOX_Float({regs[0]}))"
774 else if n
== once
"output".to_symbol
then
775 s
= "printf(\"%f\\n\
", UNBOX_Float({regs[0]}));"
776 else if n
== once
"to_i".to_symbol
then
777 s
= "TAG_Int((bigint)UNBOX_Float({regs[0]}))"
778 else if n
== once
"+".to_symbol
then
779 s
= "BOX_Float(UNBOX_Float({regs[0]})+UNBOX_Float({regs[1]}))"
780 else if n
== once
"-".to_symbol
then
781 s
= "BOX_Float(UNBOX_Float({regs[0]})-UNBOX_Float({regs[1]}))"
782 else if n
== once
"*".to_symbol
then
783 s
= "BOX_Float(UNBOX_Float({regs[0]})*UNBOX_Float({regs[1]}))"
784 else if n
== once
"/".to_symbol
then
785 s
= "BOX_Float(UNBOX_Float({regs[0]})/UNBOX_Float({regs[1]}))"
786 else if n
== once
"<".to_symbol
then
787 s
= "TAG_Bool(UNBOX_Float({regs[0]})<UNBOX_Float({regs[1]}))"
788 else if n
== once
">".to_symbol
then
789 s
= "TAG_Bool(UNBOX_Float({regs[0]})>UNBOX_Float({regs[1]}))"
790 else if n
== once
"<=".to_symbol
then
791 s
= "TAG_Bool(UNBOX_Float({regs[0]})<=UNBOX_Float({regs[1]}))"
792 else if n
== once
">=".to_symbol
then
793 s
= "TAG_Bool(UNBOX_Float({regs[0]})>=UNBOX_Float({regs[1]}))"
795 else if c
== once
"Char".to_symbol
then
796 if n
== once
"object_id".to_symbol
then
797 s
= "TAG_Int(UNTAG_Char({regs[0]}))"
798 else if n
== once
"unary -".to_symbol
then
799 s
= "TAG_Char(-UNTAG_Char({regs[0]}))"
800 else if n
== once
"output".to_symbol
then
801 s
= "printf(\"%c\
", (unsigned char)UNTAG_Char({regs[0]}));"
802 else if n
== once
"ascii".to_symbol
then
803 s
= "TAG_Int((unsigned char)UNTAG_Char({regs[0]}))"
804 else if n
== once
"succ".to_symbol
then
805 s
= "TAG_Char(UNTAG_Char({regs[0]})+1)"
806 else if n
== once
"prec".to_symbol
then
807 s
= "TAG_Char(UNTAG_Char({regs[0]})-1)"
808 else if n
== once
"to_i".to_symbol
then
809 s
= "TAG_Int(UNTAG_Char({regs[0]})-'0')"
810 else if n
== once
"+".to_symbol
then
811 s
= "TAG_Char(UNTAG_Char({regs[0]})+UNTAG_Char({regs[1]}))"
812 else if n
== once
"-".to_symbol
then
813 s
= "TAG_Char(UNTAG_Char({regs[0]})-UNTAG_Char({regs[1]}))"
814 else if n
== once
"*".to_symbol
then
815 s
= "TAG_Char(UNTAG_Char({regs[0]})*UNTAG_Char({regs[1]}))"
816 else if n
== once
"/".to_symbol
then
817 s
= "TAG_Char(UNTAG_Char({regs[0]})/UNTAG_Char({regs[1]}))"
818 else if n
== once
"%".to_symbol
then
819 s
= "TAG_Char(UNTAG_Char({regs[0]})%UNTAG_Char({regs[1]}))"
820 else if n
== once
"<".to_symbol
then
821 s
= "TAG_Bool(UNTAG_Char({regs[0]})<UNTAG_Char({regs[1]}))"
822 else if n
== once
">".to_symbol
then
823 s
= "TAG_Bool(UNTAG_Char({regs[0]})>UNTAG_Char({regs[1]}))"
824 else if n
== once
"<=".to_symbol
then
825 s
= "TAG_Bool(UNTAG_Char({regs[0]})<=UNTAG_Char({regs[1]}))"
826 else if n
== once
">=".to_symbol
then
827 s
= "TAG_Bool(UNTAG_Char({regs[0]})>=UNTAG_Char({regs[1]}))"
828 else if n
== once
"==".to_symbol
then
829 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
830 else if n
== once
"!=".to_symbol
then
831 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
833 else if c
== once
"Bool".to_symbol
then
834 if n
== once
"object_id".to_symbol
then
835 s
= "TAG_Int(UNTAG_Bool({regs[0]}))"
836 else if n
== once
"unary -".to_symbol
then
837 s
= "TAG_Bool(-UNTAG_Bool({regs[0]}))"
838 else if n
== once
"output".to_symbol
then
839 s
= "(void)printf(UNTAG_Bool({regs[0]})?\"true\\n\
":\"false\\n\
");"
840 else if n
== once
"ascii".to_symbol
then
841 s
= "TAG_Bool(UNTAG_Bool({regs[0]}))"
842 else if n
== once
"to_i".to_symbol
then
843 s
= "TAG_Int(UNTAG_Bool({regs[0]}))"
844 else if n
== once
"==".to_symbol
then
845 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
846 else if n
== once
"!=".to_symbol
then
847 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
849 else if c
== once
"NativeArray".to_symbol
then
850 if n
== once
"object_id".to_symbol
then
851 s
= "TAG_Int(((Nit_NativeArray){regs[0]})->object_id)"
852 else if n
== once
"[]".to_symbol
then
853 s
= "((Nit_NativeArray){regs[0]})->val[UNTAG_Int({regs[1]})]"
854 else if n
== once
"[]=".to_symbol
then
855 s
= "((Nit_NativeArray){regs[0]})->val[UNTAG_Int({regs[1]})]={regs[2]}"
856 else if n
== once
"copy_to".to_symbol
then
857 s
= "(void)memcpy(((Nit_NativeArray ){regs[1]})->val, ((Nit_NativeArray){regs[0]})->val, UNTAG_Int({regs[2]})*sizeof(val_t))"
859 else if c
== once
"NativeString".to_symbol
then
860 if n
== once
"object_id".to_symbol
then
861 s
= "TAG_Int(UNBOX_NativeString({regs[0]}))"
862 else if n
== once
"atoi".to_symbol
then
863 s
= "TAG_Int(atoi(UNBOX_NativeString({regs[0]})))"
864 else if n
== once
"[]".to_symbol
then
865 s
= "TAG_Char(UNBOX_NativeString({regs[0]})[UNTAG_Int({regs[1]})])"
866 else if n
== once
"[]=".to_symbol
then
867 s
= "UNBOX_NativeString({regs[0]})[UNTAG_Int({regs[1]})]=UNTAG_Char({regs[2]});"
868 else if n
== once
"copy_to".to_symbol
then
869 s
= "(void)memcpy(UNBOX_NativeString({regs[1]})+UNTAG_Int({regs[4]}), UNBOX_NativeString({regs[0]})+UNTAG_Int({regs[3]}), UNTAG_Int({regs[2]}));"
871 else if n
== once
"object_id".to_symbol
then
872 s
= "TAG_Int((bigint)((obj_t){regs[0]})[1].object_id)"
873 else if n
== once
"sys".to_symbol
then
875 else if n
== once
"is_same_type".to_symbol
then
876 s
= "TAG_Bool((VAL2VFT({regs[0]})==VAL2VFT({regs[1]})))"
877 else if n
== once
"exit".to_symbol
then
878 s
= "exit(UNTAG_Int({regs[1]}));"
879 else if n
== once
"calloc_array".to_symbol
then
880 s
= "NEW_NativeArray(UNTAG_Int({regs[1]}), sizeof(val_t))"
881 else if n
== once
"calloc_string".to_symbol
then
882 s
= "BOX_NativeString((char*)raw_alloc((UNTAG_Int({regs[1]}) * sizeof(char))))"
883 # Add output_class_name native implementation
884 else if n
== once
"output_class_name".to_symbol
then
885 s
= "printf(\"%s\\n\
", VAL2VFT({regs[0]})[2].cname);"
886 # Add class_name implementation
887 else if n
== once
"native_class_name".to_symbol
then
888 s
= "BOX_NativeString(VAL2VFT({regs[0]})[2].cname);"
893 if ll
!= null then v
.add_instr
("fprintf(stderr, \"{ll.to_s}: \
");")
894 v
.add_instr
("fprintf(stderr, \"Fatal error
: unknown
intern method
{method.full_name}.\\n\
");")
895 v
.add_instr
("nit_exit(1);")
898 if result
== null then
899 v
.new_instr
.add
(s
).add
(";\n")
900 else if need_result
then
901 var w
= new_result
(v
)
907 redef class IIntValue
908 redef fun compile_to_c
(v
)
910 v
.add_location
(location
)
911 var w
= new_result
(v
)
912 w
.add
("TAG_Int(").add
(value
.to_s
).add
(")")
916 redef class IBoolValue
917 redef fun compile_to_c
(v
)
919 v
.add_location
(location
)
920 var w
= new_result
(v
)
922 if value
then w
.add
("true") else w
.add
("false")
927 redef class ICharValue
928 redef fun compile_to_c
(v
)
930 v
.add_location
(location
)
931 var w
= new_result
(v
)
932 w
.add
("TAG_Char(").add
(value
).add
(")")
936 redef class IFloatValue
937 redef fun compile_to_c
(v
)
939 v
.add_location
(location
)
940 var w
= new_result
(v
)
941 w
.add
("BOX_Float(").add
(value
).add
(")")
945 redef class IStringValue
946 redef fun compile_to_c
(v
)
948 v
.add_location
(location
)
949 var w
= new_result
(v
)
950 w
.add
("BOX_NativeString(\"").add(value).add("\
")")
955 redef fun compile_to_c
(v
)
957 v
.add_location
(location
)
959 w
.add
("nit_abort(\"")
961 if texts.length > 1 then
969 w.add(module_location.cname)
973 w.add(ll.line_start.to_s)
982 redef fun compile_to_c(v)
984 if not need_result then return
985 var e = v.register(expr)
986 var r = v.register(result.as(not null))
987 if e == r then return
988 v.add_location(location)
997 redef class IAttrRead
998 redef fun compile_to_c(v)
1000 if not need_result then return
1001 v.add_location(location)
1002 var w = new_result(v)
1003 w.add(property.global.attr_access)
1005 w.add(v.register(expr))
1010 redef class IAttrIsset
1011 redef fun compile_to_c(v)
1013 if not need_result then return
1014 v.add_location(location)
1015 var w = new_result(v)
1017 w.add(property.global.attr_access)
1019 w.add(v.register(expr))
1020 w.add(")!=NIT_NULL)")
1024 redef class IAttrWrite
1025 redef fun compile_to_c(v)
1027 v.add_location(location)
1029 w.add(property.global.attr_access)
1031 w.add(v.register(expr1))
1033 w.add(v.register(expr2))
1038 redef class ITypeCheck
1039 redef fun compile_to_c(v)
1041 if not need_result then return
1042 v.add_location(location)
1043 var recv = v.register(expr2)
1044 var w = new_result(v)
1046 if expr2.stype.is_nullable then
1047 if stype.is_nullable then
1050 w.add("==NIT_NULL) || ")
1051 else if stype.as_nullable == expr2.stype then
1053 w.add("!=NIT_NULL)")
1058 w.add("!=NIT_NULL) && ")
1061 # FIXME handle formaltypes
1063 if t isa MMVirtualType then
1064 var slf = v.register(expr1)
1065 var g = t.property.global
1069 w.add(g.vt_class_color)
1074 w.add(g.vt_class_id)
1082 var g = t.local_class.global
1097 redef fun compile_to_c(v)
1099 if not need_result then return
1100 v.add_location(location)
1101 var w = new_result(v)
1103 var t1 = expr1.stype
1104 var t2 = expr2.stype
1105 if t1 isa MMTypeNone then
1106 if t2 isa MMTypeNone then
1109 else if t2.is_nullable then
1110 w.add(v.register(expr2))
1111 w.add("==NIT_NULL)")
1117 else if t1.is_nullable then
1118 if t2 isa MMTypeNone then
1119 w.add(v.register(expr1))
1120 w.add("==NIT_NULL)")
1122 else if t2.is_nullable then
1123 w.add("IS_EQUAL_NN(")
1125 w.add("IS_EQUAL_ON(")
1126 w.add(v.register(expr2))
1128 w.add(v.register(expr1))
1133 if t2 isa MMTypeNone then
1136 else if t2.is_nullable then
1137 w.add("IS_EQUAL_ON(")
1139 w.add("IS_EQUAL_OO(")
1142 w.add(v.register(expr1))
1144 w.add(v.register(expr2))
1150 redef fun compile_to_c(v)
1152 if not need_result then return
1153 v.add_location(location)
1154 var w = new_result(v)
1155 w.add("TAG_Bool(!UNTAG_Bool(")
1156 w.add(v.register(expr))
1162 redef fun compile_to_c(v)
1164 v.add_location(location)
1165 var i = v.new_number
1166 var res = result.as(not null)
1167 if res.stype.is_nullable then
1168 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
*/")
1169 v.add_instr("if (!once_bool_
{i}) \
{")
1171 # Since the value is not nullable, we use the null value to represent the boolean
1172 v.add_decl("static val_t once_value_
{i}; /* Once value
*/")
1173 v.add_instr("if (!once_value_
{i}) \
{")
1176 body.compile_to_c(v)
1177 var e = v.register(res)
1178 v.add_instr("once_value_
{i} = {e};")
1179 v.add_instr("register_static_object
(&once_value_
{i});")
1180 if res.stype.is_nullable then v.add_instr("once_bool_
{i} = true;")
1182 v.add_instr("\
} else {e} = once_value_
{i};")
1183 var w = new_result(v)
1188 redef class IClosCall
1189 redef fun compile_to_c(v: I2CCompilerVisitor)
1191 v.add_location(location)
1193 var args: Array[String]
1195 ivar = "closctx-
>closure_funs
[{v.closures[closure_decl]}]"
1196 args = ["closctx-
>closure_ctx
"]
1198 ivar = "CREG[{v.closures[closure_decl]}]"
1199 args = ["closctx_param
"]
1201 args.append(v.registers(exprs))
1203 var s = "(({v.clostypes[closure_decl]})({ivar}))({args.join(", ")})"
1209 v.add_instr("if ({args.first}->has_broke
) \
{")
1215 v.add_goto(v.iroutine.body)
1221 redef class IHasClos
1222 redef fun compile_to_c(v)
1224 if not need_result then return
1225 v.add_location(location)
1226 var w = new_result(v)
1229 w.add("closctx-
>closure_funs
[")
1230 w.add(v.closures[closure_decl])
1234 w.add(v.closures[closure_decl])
1241 redef class IClosureDef
1242 # Compile the closure as a separate C function in the visitor out_contexts.
1243 # Return a fun_t pointer to the function.
1244 fun compile_closure(v: I2CCompilerVisitor): String
1248 # We are now in a closure
1249 var cfc_old = v.closure
1252 # We are now in a escape boundary
1253 var lls_old = v.local_labels
1254 v.local_labels = new HashSet[ISeq]
1256 # We are now in a new C context
1257 var decl_writer_old = cv.decl_writer
1258 var writer_old = cv.writer
1259 cv.writer = cv.top_writer.sub
1260 cv.decl_writer = cv.header_writer.sub
1262 # Generate the C function
1263 var cname = "OC_{v.basecname}_
{v.new_number}"
1264 var args = compile_signature_to_c(v.visitor, cname, null, "struct stack_frame_t
*closctx
", null)
1265 cv.decl_writer = cv.writer.sub
1267 var s = compile_inside_to_c(v, args)
1269 v.add_instr("return;")
1271 v.add_instr("return {s};")
1277 cv.writer = writer_old
1278 cv.decl_writer = decl_writer_old
1280 v.local_labels = lls_old
1281 return "((fun_t
){cname})"