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};")
298 v
.add_instr
("fra.me.nitni_local_ref_head = NULL;")
300 # Declare/initialize local variables
301 for i
in [0..std_slots_nb
[ do
302 v
.add_instr
("fra.me.REG[{i}] = NIT_NULL;")
304 for i
in [0..tag_slots_nb
[ do
305 v
.add_decl
("val_t REGB{i};")
307 var iclosdecls
= closure_decls
308 if iclosdecls
!= null then
309 v
.add_decl
("fun_t CREG[{iclosdecls.length}];")
310 v
.add_instr
("fra.me.closure_ctx = closctx_param;")
311 v
.add_instr
("fra.me.closure_funs = CREG;")
315 if r
.slot_index
!= null then v
.add_assignment
(v
.register
(r
), args
[k
])
318 if iclosdecls
!= null then
319 for i
in [0..iclosdecls
.length
[ do
320 var iclosdecl
= iclosdecls
[i
]
321 v
.add_instr
("CREG[{i}] = {args[params.length+i]};")
322 v
.closures
[iclosdecl
] = i
.to_s
323 var cs
= iclosdecl
.closure
.signature
# Closure signature
324 var subparams
= new Array[String] # Parameters of the closure
325 subparams
.add
("struct stack_frame_t *")
326 for j
in [0..cs
.arity
[ do
331 if cs
.return_type
!= null then r
= "val_t"
332 v
.clostypes
[iclosdecl
] = "{r} (*)({subparams.join(", ")})"
335 v
.add_decl
("val_t tmp;")
338 var old_rl
= v
.return_label
339 v
.return_label
= body
344 v
.add_instr
("stack_frame_head = fra.me.prev;")
345 v
.return_label
= old_rl
354 # Full compilation of the routine
355 # cv must be in the correct function
356 fun compile_to_c
(cv
: CompilerVisitor, cname
: String, args
: Array[String]): nullable String
358 var v
= new I2CCompilerVisitor(cv
, self, cname
)
359 return compile_inside_to_c
(v
, args
)
364 # Full compilation of the icode
365 fun compile_to_c
(v
: I2CCompilerVisitor) is abstract
367 # Is a result really needed
368 private fun need_result
: Bool
371 return r
!= null and r
.slot_index
!= null
374 # Store s in the result value of self
375 private fun store_result
(v
: I2CCompilerVisitor, w
: nullable Writer)
378 if r
!= null and r
.slot_index
!= null then
381 w2
.add
(v
.register
(r
))
385 else if w
!= null and not is_pure
then
386 # ICode with side effects must be evaluated
387 # even if the result is not wanted
394 # Prepare a writer if the expression icode need to be compiled
395 # * Result assigment is automatic if needed
396 private fun new_result
(v
: I2CCompilerVisitor): Writer
398 assert need_result
or not is_pure
401 if r
!= null and r
.slot_index
!= null then
402 w2
.add
(v
.register
(r
))
412 redef fun compile_to_c
(v
)
414 v
.add_location
(location
)
415 v
.local_labels
.add
(self)
416 var mark
= iescape_mark
417 if mark
!= null then v
.marks_to_seq
[mark
] = self
426 redef fun compile_to_c
(v
)
428 v
.add_location
(location
)
430 w
.add
("if (UNTAG_Bool(")
431 w
.add
(v
.register
(expr
))
433 if not then_seq
.icodes
.is_empty
then
435 then_seq
.compile_to_c
(v
)
438 if not else_seq
.icodes
.is_empty
then
439 v
.add_instr
("\} else \{")
441 else_seq
.compile_to_c
(v
)
449 redef fun compile_to_c
(v
)
451 v
.add_location
(location
)
452 v
.local_labels
.add
(self)
453 var mark
= iescape_mark
454 if mark
!= null then v
.marks_to_seq
[mark
] = self
455 v
.add_instr
("while(1) \{")
467 redef fun compile_to_c
(v
)
469 v
.add_location
(location
)
470 v
.add_goto
(v
.marks_to_seq
[iescape_mark
])
475 redef fun compile_to_c
(v
)
477 v
.add_location
(location
)
478 var args
= v
.registers
(exprs
)
480 # Compile closure definitions
481 var old_el
= v
.escaped_labels
482 var closdefs
= closure_defs
483 var closctx
: nullable String = null # The closure context of closdefs
484 if closdefs
!= null then
485 # Get the closure context
489 closctx
= "(&(fra.me))"
492 # First aditionnal arguments is the closure context
495 # We are in a new escape boundary
496 v
.escaped_labels
= new HashMap[ISeq, Int]
498 # Compile each closures and add each sub-function as an other additionnal parameter
499 for cd
in closdefs
do
501 var cn
= cd
.compile_closure
(v
)
509 # Compile the real call
510 var call
= compile_call_to_c
(v
, args
)
511 var res
: nullable Writer = call
514 if closctx
!= null then
515 var els
= v
.escaped_labels
516 v
.escaped_labels
= old_el
517 # Is there possible escapes?
518 if not els
.is_empty
then
519 # Call in a tmp variable to avoid 'break' overwrite
533 # What are the expected escape indexes
534 v
.new_instr
.add
("switch (").add
(closctx
).add
("->has_broke) \{\n")
536 # No escape occured, continue as usual
537 v
.add_instr
("case 0: break;")
538 var lls
= v
.local_labels
539 var iels
= els
.iterator
540 var forward_escape
= false
544 # Local escape occured
545 # Clear the has_broke information and go to the target
546 v
.new_instr
.add
("case ").add
(iels
.item
.to_s
).add
(": ").add
(closctx
).add
("->has_broke = 0; goto ").add
(v
.lab
(seq
)).add
(";\n")
548 # Forward escape occured: register the escape label
550 v
.register_escape_label
(seq
)
551 forward_escape
= true
555 # If forward escape occured, just pass to the next one
556 if forward_escape
then
557 # Do not need to copy 'has_broke' value since it is shared by the next one.
558 # So just exit the C function.
559 v
.new_instr
.add
("default: goto ").add
(v
.lab
(v
.return_label
.as(not null))).add
(";\n")
567 var w
= new_result
(v
)
572 # The single invocation witout fancy stuffs
573 private fun compile_call_to_c
(v
: I2CCompilerVisitor, args
: Array[String]): Writer is abstract
577 redef fun compile_call_to_c
(v
, args
)
581 # do not compile explicit calls from native methods
582 # theses are really manually called in the native implementation
583 if is_explicit_from_extern
then return w
586 if prop
.global
.is_init
then args
.add
("init_table")
587 w
.add
(prop
.global
.meth_call
)
591 w
.add_all
(args
, ", ")
598 redef fun compile_call_to_c
(v
, args
)
600 # do not compile explicit calls from native methods
601 # theses are really manually called in the native implementation
602 if is_explicit_from_extern
then return new Writer
605 if prop
.global
.is_init
then args
.add
("init_table")
607 w
.add
(prop
.super_meth_call
)
611 w
.add_all
(args
, ", ")
618 redef fun compile_call_to_c
(v
, args
)
622 # do not compile explicit calls from native methods
623 # theses are really manually called in the native implementation
624 if is_explicit_from_extern
then return w
627 w
.add
(stype
.local_class
.to_s
)
629 w
.add
(property
.global
.intro
.cname
)
631 w
.add_all
(args
, ", ")
637 redef class IAllocateInstance
638 redef fun compile_to_c
(v
)
640 v
.add_location
(location
)
641 var w
= new_result
(v
)
643 w
.add
(stype
.local_class
.cname
)
648 redef class ICheckInstance
649 redef fun compile_to_c
(v
)
651 v
.add_location
(location
)
652 var w
= new_result
(v
)
654 w
.add
(stype
.local_class
.cname
)
656 w
.add
(v
.register
(expr
))
661 redef class IInitAttributes
662 redef fun compile_to_c
(v
)
664 v
.add_location
(location
)
666 w
.add
("INIT_ATTRIBUTES__")
667 w
.add
(stype
.local_class
.cname
)
669 w
.add
(v
.register
(expr
))
674 redef class IStaticCall
675 redef fun compile_call_to_c
(v
, args
)
678 if prop
.global
.is_init
then args
.add
("init_table")
680 w
.add
(property
.cname
)
682 w
.add_all
(args
, ", ")
689 redef fun compile_to_c
(v
)
691 v
.add_location
(location
)
692 if method
.is_intern
then
693 compile_intern_method_to_c
(v
)
694 else if not method
.global
.is_init
then
695 compile_extern_method_to_c
(v
)
699 fun compile_extern_method_to_c
(v
: I2CCompilerVisitor)
701 var ename
= "{method.friendly_extern_name(method.local_class)}___out"
703 var sig
= method
.signature
704 assert exprs
.length
== sig
.arity
+ 1
706 var regs
= v
.registers
(exprs
)
708 var args
= new Array[String]
710 for i
in [0..sig
.arity
[ do
713 var s
= "{ename}({args.join(", ")})"
715 if need_result
then s
= s
# sig.return_type.boxtype(s)
716 var w
= new_result
(v
)
720 fun compile_intern_method_to_c
(v
: I2CCompilerVisitor)
722 var sig
= method
.signature
723 assert exprs
.length
== sig
.arity
+ 1
724 var c
= method
.local_class
.name
726 var regs
= v
.registers
(exprs
)
727 var s
: nullable String = null
728 if c
== once
"Int".to_symbol
then
729 if n
== once
"object_id".to_symbol
then
731 else if n
== once
"unary -".to_symbol
then
732 s
= "TAG_Int(-UNTAG_Int({regs[0]}))"
733 else if n
== once
"output".to_symbol
then
734 s
= "printf(\"%ld\\n\
", UNTAG_Int({regs[0]}));"
735 else if n
== once
"ascii".to_symbol
then
736 s
= "TAG_Char(UNTAG_Int({regs[0]}))"
737 else if n
== once
"succ".to_symbol
then
738 s
= "TAG_Int(UNTAG_Int({regs[0]})+1)"
739 else if n
== once
"prec".to_symbol
then
740 s
= "TAG_Int(UNTAG_Int({regs[0]})-1)"
741 else if n
== once
"to_f".to_symbol
then
742 s
= "BOX_Float((float)UNTAG_Int({regs[0]}))"
743 else if n
== once
"+".to_symbol
then
744 s
= "TAG_Int(UNTAG_Int({regs[0]})+UNTAG_Int({regs[1]}))"
745 else if n
== once
"-".to_symbol
then
746 s
= "TAG_Int(UNTAG_Int({regs[0]})-UNTAG_Int({regs[1]}))"
747 else if n
== once
"*".to_symbol
then
748 s
= "TAG_Int(UNTAG_Int({regs[0]})*UNTAG_Int({regs[1]}))"
749 else if n
== once
"/".to_symbol
then
750 s
= "TAG_Int(UNTAG_Int({regs[0]})/UNTAG_Int({regs[1]}))"
751 else if n
== once
"%".to_symbol
then
752 s
= "TAG_Int(UNTAG_Int({regs[0]})%UNTAG_Int({regs[1]}))"
753 else if n
== once
"<".to_symbol
then
754 s
= "TAG_Bool(UNTAG_Int({regs[0]})<UNTAG_Int({regs[1]}))"
755 else if n
== once
">".to_symbol
then
756 s
= "TAG_Bool(UNTAG_Int({regs[0]})>UNTAG_Int({regs[1]}))"
757 else if n
== once
"<=".to_symbol
then
758 s
= "TAG_Bool(UNTAG_Int({regs[0]})<=UNTAG_Int({regs[1]}))"
759 else if n
== once
">=".to_symbol
then
760 s
= "TAG_Bool(UNTAG_Int({regs[0]})>=UNTAG_Int({regs[1]}))"
761 else if n
== once
"lshift".to_symbol
then
762 s
= "TAG_Int(UNTAG_Int({regs[0]})<<UNTAG_Int({regs[1]}))"
763 else if n
== once
"rshift".to_symbol
then
764 s
= "TAG_Int(UNTAG_Int({regs[0]})>>UNTAG_Int({regs[1]}))"
765 else if n
== once
"==".to_symbol
then
766 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
767 else if n
== once
"!=".to_symbol
then
768 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
770 else if c
== once
"Float".to_symbol
then
771 if n
== once
"object_id".to_symbol
then
772 s
= "TAG_Int((bigint)UNBOX_Float({regs[0]}))"
773 else if n
== once
"unary -".to_symbol
then
774 s
= "BOX_Float(-UNBOX_Float({regs[0]}))"
775 else if n
== once
"output".to_symbol
then
776 s
= "printf(\"%f\\n\
", UNBOX_Float({regs[0]}));"
777 else if n
== once
"to_i".to_symbol
then
778 s
= "TAG_Int((bigint)UNBOX_Float({regs[0]}))"
779 else if n
== once
"+".to_symbol
then
780 s
= "BOX_Float(UNBOX_Float({regs[0]})+UNBOX_Float({regs[1]}))"
781 else if n
== once
"-".to_symbol
then
782 s
= "BOX_Float(UNBOX_Float({regs[0]})-UNBOX_Float({regs[1]}))"
783 else if n
== once
"*".to_symbol
then
784 s
= "BOX_Float(UNBOX_Float({regs[0]})*UNBOX_Float({regs[1]}))"
785 else if n
== once
"/".to_symbol
then
786 s
= "BOX_Float(UNBOX_Float({regs[0]})/UNBOX_Float({regs[1]}))"
787 else if n
== once
"<".to_symbol
then
788 s
= "TAG_Bool(UNBOX_Float({regs[0]})<UNBOX_Float({regs[1]}))"
789 else if n
== once
">".to_symbol
then
790 s
= "TAG_Bool(UNBOX_Float({regs[0]})>UNBOX_Float({regs[1]}))"
791 else if n
== once
"<=".to_symbol
then
792 s
= "TAG_Bool(UNBOX_Float({regs[0]})<=UNBOX_Float({regs[1]}))"
793 else if n
== once
">=".to_symbol
then
794 s
= "TAG_Bool(UNBOX_Float({regs[0]})>=UNBOX_Float({regs[1]}))"
796 else if c
== once
"Char".to_symbol
then
797 if n
== once
"object_id".to_symbol
then
798 s
= "TAG_Int(UNTAG_Char({regs[0]}))"
799 else if n
== once
"unary -".to_symbol
then
800 s
= "TAG_Char(-UNTAG_Char({regs[0]}))"
801 else if n
== once
"output".to_symbol
then
802 s
= "printf(\"%c\
", (unsigned char)UNTAG_Char({regs[0]}));"
803 else if n
== once
"ascii".to_symbol
then
804 s
= "TAG_Int((unsigned char)UNTAG_Char({regs[0]}))"
805 else if n
== once
"succ".to_symbol
then
806 s
= "TAG_Char(UNTAG_Char({regs[0]})+1)"
807 else if n
== once
"prec".to_symbol
then
808 s
= "TAG_Char(UNTAG_Char({regs[0]})-1)"
809 else if n
== once
"to_i".to_symbol
then
810 s
= "TAG_Int(UNTAG_Char({regs[0]})-'0')"
811 else if n
== once
"+".to_symbol
then
812 s
= "TAG_Char(UNTAG_Char({regs[0]})+UNTAG_Char({regs[1]}))"
813 else if n
== once
"-".to_symbol
then
814 s
= "TAG_Char(UNTAG_Char({regs[0]})-UNTAG_Char({regs[1]}))"
815 else if n
== once
"*".to_symbol
then
816 s
= "TAG_Char(UNTAG_Char({regs[0]})*UNTAG_Char({regs[1]}))"
817 else if n
== once
"/".to_symbol
then
818 s
= "TAG_Char(UNTAG_Char({regs[0]})/UNTAG_Char({regs[1]}))"
819 else if n
== once
"%".to_symbol
then
820 s
= "TAG_Char(UNTAG_Char({regs[0]})%UNTAG_Char({regs[1]}))"
821 else if n
== once
"<".to_symbol
then
822 s
= "TAG_Bool(UNTAG_Char({regs[0]})<UNTAG_Char({regs[1]}))"
823 else if n
== once
">".to_symbol
then
824 s
= "TAG_Bool(UNTAG_Char({regs[0]})>UNTAG_Char({regs[1]}))"
825 else if n
== once
"<=".to_symbol
then
826 s
= "TAG_Bool(UNTAG_Char({regs[0]})<=UNTAG_Char({regs[1]}))"
827 else if n
== once
">=".to_symbol
then
828 s
= "TAG_Bool(UNTAG_Char({regs[0]})>=UNTAG_Char({regs[1]}))"
829 else if n
== once
"==".to_symbol
then
830 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
831 else if n
== once
"!=".to_symbol
then
832 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
834 else if c
== once
"Bool".to_symbol
then
835 if n
== once
"object_id".to_symbol
then
836 s
= "TAG_Int(UNTAG_Bool({regs[0]}))"
837 else if n
== once
"unary -".to_symbol
then
838 s
= "TAG_Bool(-UNTAG_Bool({regs[0]}))"
839 else if n
== once
"output".to_symbol
then
840 s
= "(void)printf(UNTAG_Bool({regs[0]})?\"true\\n\
":\"false\\n\
");"
841 else if n
== once
"ascii".to_symbol
then
842 s
= "TAG_Bool(UNTAG_Bool({regs[0]}))"
843 else if n
== once
"to_i".to_symbol
then
844 s
= "TAG_Int(UNTAG_Bool({regs[0]}))"
845 else if n
== once
"==".to_symbol
then
846 s
= "TAG_Bool(({regs[0]})==({regs[1]}))"
847 else if n
== once
"!=".to_symbol
then
848 s
= "TAG_Bool(({regs[0]})!=({regs[1]}))"
850 else if c
== once
"NativeArray".to_symbol
then
851 if n
== once
"object_id".to_symbol
then
852 s
= "TAG_Int(((Nit_NativeArray){regs[0]})->object_id)"
853 else if n
== once
"[]".to_symbol
then
854 s
= "((Nit_NativeArray){regs[0]})->val[UNTAG_Int({regs[1]})]"
855 else if n
== once
"[]=".to_symbol
then
856 s
= "((Nit_NativeArray){regs[0]})->val[UNTAG_Int({regs[1]})]={regs[2]}"
857 else if n
== once
"copy_to".to_symbol
then
858 s
= "(void)memcpy(((Nit_NativeArray ){regs[1]})->val, ((Nit_NativeArray){regs[0]})->val, UNTAG_Int({regs[2]})*sizeof(val_t))"
860 else if c
== once
"NativeString".to_symbol
then
861 if n
== once
"object_id".to_symbol
then
862 s
= "TAG_Int(UNBOX_NativeString({regs[0]}))"
863 else if n
== once
"atoi".to_symbol
then
864 s
= "TAG_Int(atoi(UNBOX_NativeString({regs[0]})))"
865 else if n
== once
"[]".to_symbol
then
866 s
= "TAG_Char(UNBOX_NativeString({regs[0]})[UNTAG_Int({regs[1]})])"
867 else if n
== once
"[]=".to_symbol
then
868 s
= "UNBOX_NativeString({regs[0]})[UNTAG_Int({regs[1]})]=UNTAG_Char({regs[2]});"
869 else if n
== once
"copy_to".to_symbol
then
870 s
= "(void)memcpy(UNBOX_NativeString({regs[1]})+UNTAG_Int({regs[4]}), UNBOX_NativeString({regs[0]})+UNTAG_Int({regs[3]}), UNTAG_Int({regs[2]}));"
872 else if c
== once
"Sys".to_symbol
then
873 if n
== once
"force_garbage_collection".to_symbol
then
874 s
= "Nit_gc_force_garbage_collection()"
876 else if n
== once
"object_id".to_symbol
then
877 s
= "TAG_Int((bigint)((obj_t){regs[0]})[1].object_id)"
878 else if n
== once
"sys".to_symbol
then
880 else if n
== once
"is_same_type".to_symbol
then
881 s
= "TAG_Bool((VAL2VFT({regs[0]})==VAL2VFT({regs[1]})))"
882 else if n
== once
"exit".to_symbol
then
883 s
= "exit(UNTAG_Int({regs[1]}));"
884 else if n
== once
"calloc_array".to_symbol
then
885 s
= "NEW_NativeArray(UNTAG_Int({regs[1]}), sizeof(val_t))"
886 else if n
== once
"calloc_string".to_symbol
then
887 s
= "BOX_NativeString((char*)raw_alloc((UNTAG_Int({regs[1]}) * sizeof(char))))"
888 # Add output_class_name native implementation
889 else if n
== once
"output_class_name".to_symbol
then
890 s
= "printf(\"%s\\n\
", VAL2VFT({regs[0]})[2].cname);"
891 # Add class_name implementation
892 else if n
== once
"native_class_name".to_symbol
then
893 s
= "BOX_NativeString(VAL2VFT({regs[0]})[2].cname);"
898 if ll
!= null then v
.add_instr
("fprintf(stderr, \"{ll.to_s}: \
");")
899 v
.add_instr
("fprintf(stderr, \"Fatal error
: unknown
intern method
{method.full_name}.\\n\
");")
900 v
.add_instr
("nit_exit(1);")
903 if result
== null then
904 v
.new_instr
.add
(s
).add
(";\n")
905 else if need_result
then
906 var w
= new_result
(v
)
912 redef class IIntValue
913 redef fun compile_to_c
(v
)
915 v
.add_location
(location
)
916 var w
= new_result
(v
)
917 w
.add
("TAG_Int(").add
(value
.to_s
).add
(")")
921 redef class IBoolValue
922 redef fun compile_to_c
(v
)
924 v
.add_location
(location
)
925 var w
= new_result
(v
)
927 if value
then w
.add
("true") else w
.add
("false")
932 redef class ICharValue
933 redef fun compile_to_c
(v
)
935 v
.add_location
(location
)
936 var w
= new_result
(v
)
937 w
.add
("TAG_Char(").add
(value
).add
(")")
941 redef class IFloatValue
942 redef fun compile_to_c
(v
)
944 v
.add_location
(location
)
945 var w
= new_result
(v
)
946 w
.add
("BOX_Float(").add
(value
).add
(")")
950 redef class IStringValue
951 redef fun compile_to_c
(v
)
953 v
.add_location
(location
)
954 var w
= new_result
(v
)
955 w
.add
("BOX_NativeString(\"").add(value).add("\
")")
960 redef fun compile_to_c
(v
)
962 v
.add_location
(location
)
964 w
.add
("nit_abort(\"")
966 if texts.length > 1 then
974 w.add(module_location.cname)
978 w.add(ll.line_start.to_s)
987 redef fun compile_to_c(v)
989 if not need_result then return
990 var e = v.register(expr)
991 var r = v.register(result.as(not null))
992 if e == r then return
993 v.add_location(location)
1002 redef class IAttrRead
1003 redef fun compile_to_c(v)
1005 if not need_result then return
1006 v.add_location(location)
1007 var w = new_result(v)
1008 w.add(property.global.attr_access)
1010 w.add(v.register(expr))
1015 redef class IAttrIsset
1016 redef fun compile_to_c(v)
1018 if not need_result then return
1019 v.add_location(location)
1020 var w = new_result(v)
1022 w.add(property.global.attr_access)
1024 w.add(v.register(expr))
1025 w.add(")!=NIT_NULL)")
1029 redef class IAttrWrite
1030 redef fun compile_to_c(v)
1032 v.add_location(location)
1034 w.add(property.global.attr_access)
1036 w.add(v.register(expr1))
1038 w.add(v.register(expr2))
1043 redef class ITypeCheck
1044 redef fun compile_to_c(v)
1046 if not need_result then return
1047 v.add_location(location)
1048 var recv = v.register(expr2)
1049 var w = new_result(v)
1051 if expr2.stype.is_nullable then
1052 if stype.is_nullable then
1055 w.add("==NIT_NULL) || ")
1056 else if stype.as_nullable == expr2.stype then
1058 w.add("!=NIT_NULL)")
1063 w.add("!=NIT_NULL) && ")
1066 # FIXME handle formaltypes
1068 if t isa MMVirtualType then
1069 var slf = v.register(expr1)
1070 var g = t.property.global
1074 w.add(g.vt_class_color)
1079 w.add(g.vt_class_id)
1087 var g = t.local_class.global
1102 redef fun compile_to_c(v)
1104 if not need_result then return
1105 v.add_location(location)
1106 var w = new_result(v)
1108 var t1 = expr1.stype
1109 var t2 = expr2.stype
1110 if t1 isa MMTypeNone then
1111 if t2 isa MMTypeNone then
1114 else if t2.is_nullable then
1115 w.add(v.register(expr2))
1116 w.add("==NIT_NULL)")
1122 else if t1.is_nullable then
1123 if t2 isa MMTypeNone then
1124 w.add(v.register(expr1))
1125 w.add("==NIT_NULL)")
1127 else if t2.is_nullable then
1128 w.add("IS_EQUAL_NN(")
1130 w.add("IS_EQUAL_ON(")
1131 w.add(v.register(expr2))
1133 w.add(v.register(expr1))
1138 if t2 isa MMTypeNone then
1141 else if t2.is_nullable then
1142 w.add("IS_EQUAL_ON(")
1144 w.add("IS_EQUAL_OO(")
1147 w.add(v.register(expr1))
1149 w.add(v.register(expr2))
1155 redef fun compile_to_c(v)
1157 if not need_result then return
1158 v.add_location(location)
1159 var w = new_result(v)
1160 w.add("TAG_Bool(!UNTAG_Bool(")
1161 w.add(v.register(expr))
1167 redef fun compile_to_c(v)
1169 v.add_location(location)
1170 var i = v.new_number
1171 var res = result.as(not null)
1172 if res.stype.is_nullable then
1173 v.add_decl("static val_t once_value_
{i}; static int once_bool_
{i}; /* Once value
*/")
1174 v.add_instr("if (!once_bool_
{i}) \
{")
1176 # Since the value is not nullable, we use the null value to represent the boolean
1177 v.add_decl("static val_t once_value_
{i}; /* Once value
*/")
1178 v.add_instr("if (!once_value_
{i}) \
{")
1181 body.compile_to_c(v)
1182 var e = v.register(res)
1183 v.add_instr("once_value_
{i} = {e};")
1184 v.add_instr("register_static_object
(&once_value_
{i});")
1185 if res.stype.is_nullable then v.add_instr("once_bool_
{i} = true;")
1187 v.add_instr("\
} else {e} = once_value_
{i};")
1188 var w = new_result(v)
1193 redef class IClosCall
1194 redef fun compile_to_c(v: I2CCompilerVisitor)
1196 v.add_location(location)
1198 var args: Array[String]
1200 ivar = "closctx-
>closure_funs
[{v.closures[closure_decl]}]"
1201 args = ["closctx-
>closure_ctx
"]
1203 ivar = "CREG[{v.closures[closure_decl]}]"
1204 args = ["closctx_param
"]
1206 args.append(v.registers(exprs))
1208 var s = "(({v.clostypes[closure_decl]})({ivar}))({args.join(", ")})"
1214 v.add_instr("if ({args.first}->has_broke
) \
{")
1220 v.add_goto(v.iroutine.body)
1226 redef class IHasClos
1227 redef fun compile_to_c(v)
1229 if not need_result then return
1230 v.add_location(location)
1231 var w = new_result(v)
1234 w.add("closctx-
>closure_funs
[")
1235 w.add(v.closures[closure_decl])
1239 w.add(v.closures[closure_decl])
1246 redef class IClosureDef
1247 # Compile the closure as a separate C function in the visitor out_contexts.
1248 # Return a fun_t pointer to the function.
1249 fun compile_closure(v: I2CCompilerVisitor): String
1253 # We are now in a closure
1254 var cfc_old = v.closure
1257 # We are now in a escape boundary
1258 var lls_old = v.local_labels
1259 v.local_labels = new HashSet[ISeq]
1261 # We are now in a new C context
1262 var decl_writer_old = cv.decl_writer
1263 var writer_old = cv.writer
1264 cv.writer = cv.top_writer.sub
1265 cv.decl_writer = cv.header_writer.sub
1267 # Generate the C function
1268 var cname = "OC_{v.basecname}_
{v.new_number}"
1269 var args = compile_signature_to_c(v.visitor, cname, null, "struct stack_frame_t
*closctx
", null)
1270 cv.decl_writer = cv.writer.sub
1272 var s = compile_inside_to_c(v, args)
1274 v.add_instr("return;")
1276 v.add_instr("return {s};")
1282 cv.writer = writer_old
1283 cv.decl_writer = decl_writer_old
1285 v.local_labels = lls_old
1286 return "((fun_t
){cname})"