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 # Common things for NIT compilation and C generation
18 package compiling_base
23 redef class ToolContext
24 readable writable attr _global
: Bool = false
25 readable writable attr _compdir
: String
26 readable writable attr _clibdir
: String
27 readable writable attr _bindir
: String
28 readable writable attr _output_file
: String
29 readable writable attr _boost
: Bool = false
30 readable writable attr _no_cc
: Bool = false
31 readable writable attr _ext_prefix
: String
34 # Class used to generate files.
35 # Note that in fact it is not a visitor.
36 # Note also that this class is unefficient and poorly designed thus requires love.
38 # Add a line in the current declaration block
39 meth add_decl
(s
: String)
41 if _indent_level
>= 8 then
42 _ctx
.decls
.add
("\t\t" + s
)
44 _ctx
.decls
.add
(" " * _indent_level
+ s
)
48 # Add a line in the current instr block
49 meth add_instr
(s
: String)
51 if _indent_level
>= 8 then
52 _ctx
.instrs
.add
("\t\t" + s
)
54 _ctx
.instrs
.add
(" " * _indent_level
+ s
)
58 # Return a unique new number for the instance
65 # next number for new_number
66 attr _number_cpt
: Int = 0
68 # Add an indent level.
69 # New decl and instr will be indented.
70 meth indent
do _indent_level
+= 1
72 # Remove an indent level.
76 if _indent_level
< 0 then _indent_level
= 0
79 # Return a big string containing all decl and instr
82 var out
= new Array[String]
83 out
.append
(_ctx
.decls
)
84 out
.append
(_ctx
.instrs
)
89 # The current module processed
90 readable writable attr _module
: MMSrcModule
92 # Where instr and decl are stored
93 readable writable attr _ctx
: CContext = new CContext
95 # The current indent lever
96 readable writable attr _indent_level
: Int = 0
98 # The current ToolContext
99 readable writable attr _tc
: ToolContext
101 # Create a new CompilerVisitor based on a module
102 init(module: MMSrcModule) do _module
= module
105 # Where instr and decl are stored for a module
106 # Note that this class is as badly designed as CompilerVisitor
108 readable attr _decls
: Array[String] = new Array[String]
109 readable attr _instrs
: Array[String] = new Array[String]
111 meth append
(c
: CContext)
113 _instrs
.append
(c
.decls
)
114 _instrs
.append
(c
.instrs
)
117 meth merge
(c
: CContext)
119 _decls
.append
(c
.decls
)
120 _instrs
.append
(c
.instrs
)
126 redef class MMGlobalProperty
127 # C symbol refering a method inocation
128 meth meth_call
: String
130 return "CALL_{intro.cname}"
133 # C symbol refering an attribure access
134 meth attr_access
: String
136 return "ATTR_{intro.cname}"
140 redef class MMGlobalClass
141 # C symbol refering the identifier of the class
144 return "ID_{intro.name}"
147 # C symbol refering the color of the class (for subtype tests)
148 meth color_id
: String
150 return "COLOR_{intro.name}"
153 # C symbol refering the init table position of the class (for constructor linearization)
154 meth init_table_pos_id
: String
156 return "INIT_TABLE_POS_{intro.name}"
160 redef class MMLocalClass
161 # Cached primitive_info result
162 attr _primitive_info_cache
: PrimitiveInfo
164 # If primitive_info result cached?
165 attr _primitive_info_b
: Bool = false
167 # Return the primitive information of the class.
168 # Return null if the class is not primitive
169 # FIXME: Only here since there is no universal type yet
170 meth primitive_info
: PrimitiveInfo
172 if _primitive_info_b
== true then return _primitive_info_cache
174 var ctypes
= once primitive_ctypes
175 if ctypes
.has_key
(name
) then
176 _primitive_info_cache
= ctypes
[name
]
177 _primitive_info_b
= true
178 return _primitive_info_cache
180 var i
= ctypes
.iterator
183 if module.has_global_class_named
(n
) then
184 var c
= module.class_by_name
(n
)
186 _primitive_info_cache
= i
.item
187 _primitive_info_b
= true
188 return _primitive_info_cache
193 _primitive_info_b
= true
197 # Static information of primitive types
198 private meth primitive_ctypes
: HashMap[Symbol, PrimitiveInfo]
200 var res
= new HashMap[Symbol, PrimitiveInfo]
201 var pnames
= ["Int", "Char", "Bool", "Float", "NativeString", "NativeArray", "Pointer"]
202 var tagged
= [true, true, true, false, false, false, false]
203 var cnames
= ["bigint", "char", "int", "float", "char *", "val_t *", "void *"]
204 for i
in [0..pnames
.length
[ do
205 var n
= pnames
[i
].to_symbol
206 var pi
= new PrimitiveInfo
208 pi
.tagged
= tagged
[i
]
216 # Information about a primitive class
218 # The name of the class
219 readable writable attr _name
: Symbol
221 # Is the class tagged (aka not boxed)
222 readable writable attr _tagged
: Bool
224 # The corresponding c type for the primitive value
225 readable writable attr _cname
: String
231 # The corresponding c type
234 var pi
= local_class
.primitive_info
242 # The default c value for uninitialized types.
243 # Return "null" for non primitive types and something more specific for primitive types
244 meth default_cvalue
: String
246 var pi
= local_class
.primitive_info
247 if pi
!= null and pi
.tagged
then
248 return "TAG_{local_class.name}(({pi.cname})0)"
254 # Box (or tag) a primitive value
255 # Is identity if not primitive
256 meth boxtype
(s
: String): String
258 var pi
= local_class
.primitive_info
261 else if pi
.tagged
then
262 return "TAG_{local_class.name}({s})"
264 return "BOX_{local_class.name}({s})"
268 # Unbox (or untag) a primitive value
269 # Is identity if not primitive
270 meth unboxtype
(s
: String): String
272 var pi
= local_class
.primitive_info
275 else if pi
.tagged
then
276 return "UNTAG_{local_class.name}({s})"
278 return "UNBOX_{local_class.name}({s})"
283 redef class MMLocalProperty
284 # Cacher result of cname
285 attr _cname_cache
: String
287 # The mangled name of the method
290 if _cname_cache
== null then
291 _cname_cache
= cmangle
(module.name
, local_class
.name
, name
)
296 # C macro used to get the function for the call of a super property
297 meth super_meth_call
: String
299 return "CALL_SUPER_{cname}"