f46ff5bd89586fc24ad1bddd67f6f7419fb37451
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 # Base classes for Nit intermediate code representation
25 # Register store local variable and intermediate results
28 readable var _stype
: MMType
35 # A Closure declaration
37 # The associated closure definition
38 readable var _closure
: MMClosure
40 # The default implementation
41 readable writable var _default
: nullable IRoutine
43 init(c
: MMClosure) do _closure
= c
46 # A routine is a sequence of icodes with entry iregisters (params) and an exit iregister (result)
48 # The parameters of the routine
49 readable var _params
: IndexedCollection[IRegister]
51 # The closure declared
52 readable writable var _closure_decls
: nullable IndexedCollection[IClosureDecl] = null
54 # The result of the routine
55 readable var _result
: nullable IRegister
57 # The sequence of icode
58 readable var _body
: ISeq = new ISeq
60 # The location of the iroutine (if any)
61 readable writable var _location
: nullable Location = null
63 init(p
: IndexedCollection[IRegister], r
: nullable IRegister)
70 # A closure definition in a iroutine body
73 init(p
: Array[IRegister], r
: nullable IRegister)
79 ## INTERMEDIATE CODE ##
81 # The root of the intermediate code representation
83 # The number of registers used by the icode
84 fun arity
: Int is abstract
86 # The result of the icode (if any)
87 readable writable var _result
: nullable IRegister = null
89 # The location of the icode (if any)
90 readable writable var _location
: nullable Location = null
93 # An icode that uses no registers (no args)
96 redef fun arity
do return 0
99 # An icode that uses a single register (1 arg)
100 abstract class ICode1
102 redef fun arity
do return 1
104 # The single argument
105 readable var _expr
: IRegister
107 init(e
: IRegister) do _expr
= e
110 # An icode that uses two single registers (2 args)
111 abstract class ICode2
113 redef fun arity
do return 2
116 readable var _expr1
: IRegister
118 # The second argument
119 readable var _expr2
: IRegister
121 init(e1
, e2
: IRegister)
128 # An icode that uses a variable number of registers (n args) and a variable number of closure definitions
129 abstract class ICodeN
131 redef fun arity
do return _exprs
.length
134 readable var _exprs
: IndexedCollection[IRegister]
136 # All closure definition
137 readable writable var _closure_defs
: nullable IndexedCollection[nullable IClosureDef]
139 init(e
: nullable IndexedCollection[IRegister])
142 _exprs
= new Array[IRegister]
149 #################################################
151 # A linear sequence of ICode
154 # The sequence of icode
155 readable var _icodes
: List[ICode] = new List[ICode]
159 # An infinite loop of ICode
160 # Use IEscape to exit
166 # A Condidianal if-then-else statement
167 # expr is the condition
170 # The 'then' sequence of icode
171 readable var _then_seq
: ISeq = new ISeq
172 # The 'else' sequence of icode
173 readable var _else_seq
: ISeq = new ISeq
174 init(e
: IRegister) do super
177 # Escape to to end of a parent sequence
180 # The seqeuence to escape
181 # The control flow continues at the next icode after the sequence
182 readable var _seq
: ISeq
183 init(seq
: ISeq) do _seq
= seq
189 # The reason the abort occured
190 # tests.first is the format
191 readable var _texts
: Array[String]
192 # The local property that has the abort (if any)
193 readable var _property_location
: nullable MMLocalProperty
194 # The module that has the abort
195 readable var _module_location
: MMModule
196 init(t
: Array[String], pl
: nullable MMLocalProperty, ml
: MMModule)
199 _property_location
= pl
200 _module_location
= ml
204 #################################################
206 # The root of all method invocations
207 abstract class IAbsCall
210 readable var _property
: MMMethod
212 init(p
: MMMethod, e
: IndexedCollection[IRegister])
219 # A simple procedure or function call
220 # exprs.first is the reciever, other are arguments
226 # A super method call
227 # exprs.first is the reciever, other are arguments
234 # no reciever, all exprs are arguments
237 # The type to instantiate
238 readable var _stype
: MMType
239 init(t
: MMType, p
: MMMethod, a
: IndexedCollection[IRegister])
247 # exprs are the arguments
251 readable var _closure_decl
: IClosureDecl
253 # The !break sequence (if any)
254 readable writable var _break_seq
: nullable ISeq = null
256 init(c
: IClosureDecl, e
: IndexedCollection[IRegister])
264 # Mainly used to implements things that do not have a specific ICode yet
265 # expr are the arguments
269 # Special character sequence '@@@' will be substitued in order with the arguments
270 readable var _code
: String
272 init(c
: String, e
: nullable IndexedCollection[IRegister])
279 # A register assigment
280 # expr is the assigned value
281 # result is the register assigned
284 init(r
: IRegister, e
: IRegister)
291 # An attribute read access
292 # expr is the reciever
295 # The accessed attribute
296 readable var _property
: MMAttribute
298 init(p
: MMAttribute, r
: IRegister)
305 # An attribute assignment
306 # expr1 is the receiver, expr2 is the assigned value
309 # The accessed attribute
310 readable var _property
: MMAttribute
312 init(p
: MMAttribute, r
: IRegister, v
: IRegister)
320 # An attribute is_set check
321 # expr is the reciever
324 # The accessed attribute
325 readable var _property
: MMAttribute
327 init(p
: MMAttribute, r
: IRegister)
335 # expr is the expression checked
338 # The static type checkes to
339 readable var _stype
: MMType
341 init(e
: IRegister, t
: MMType)
349 # expr1 and expr2 are both operands
352 init(e1
, e2
: IRegister)
358 # The unary 'not' operation
359 # expr is the operand
368 # Evaluate body once them return the same value again and again
369 # if result is not null, then it must also be assigned in the body
372 readable var _body
: ISeq = new ISeq
376 # Is a closure given as a parameter?
380 readable var _closure_decl
: IClosureDecl
382 init(c
: IClosureDecl)
388 #################################################
390 redef class MMAttribute
391 # The attached initialisation iroutine if any
392 # To call between the allocate-instance and the initialize-instance
393 fun iroutine
: nullable IRoutine is abstract
397 # The attached body iroutine if any
398 fun iroutine
: nullable IRoutine is abstract