1.1 --- a/README.txt Fri Sep 25 00:35:17 2009 +0200
1.2 +++ b/README.txt Wed Sep 30 00:24:41 2009 +0200
1.3 @@ -205,3 +205,23 @@
1.4 could be costly. Another less ambitious solution might only involve the
1.5 optimisation of such internal method calls if an unambiguous target can be
1.6 resolved.
1.7 +
1.8 +Narrowing Type Candidates using Attribute Names
1.9 +-----------------------------------------------
1.10 +
1.11 +Within functions, it might be useful to record attribute accesses on local
1.12 +names and to collect the names in order to help resolve targets in the
1.13 +generated code. For example:
1.14 +
1.15 + def f(x, y):
1.16 + x.method(y)
1.17 + if x.something:
1.18 + ...
1.19 + return x.attr
1.20 +
1.21 +Here, the object referenced via x should have "method", "something" and "attr"
1.22 +as attributes. We could impose a test for a type providing these attributes at
1.23 +the earliest opportunity and then specialise the attribute accesses, perhaps
1.24 +also invocations, in the generated code. AttributeError occurrences would need
1.25 +to be considered, however, potentially disqualifying certain attributes from
1.26 +any optimisations, and control flow would also need to be considered.
2.1 --- a/rsvp.py Fri Sep 25 00:35:17 2009 +0200
2.2 +++ b/rsvp.py Wed Sep 30 00:24:41 2009 +0200
2.3 @@ -53,7 +53,7 @@
2.4 """
2.5
2.6 from micropython.program import ReplaceableContext, PlaceholderContext, FragmentObject
2.7 -import operator
2.8 +from rsvplib import Library
2.9
2.10 class IllegalInstruction(Exception):
2.11 pass
2.12 @@ -791,446 +791,6 @@
2.13 else:
2.14 return context, ref
2.15
2.16 -class Library:
2.17 -
2.18 - "Native function implementations."
2.19 -
2.20 - def __init__(self, machine, true_constant, false_constant):
2.21 -
2.22 - """
2.23 - Initialise the library with the 'machine' and the addresses
2.24 - 'true_constant' and 'false_constant'.
2.25 - """
2.26 -
2.27 - self.machine = machine
2.28 - self.true_constant = true_constant
2.29 - self.false_constant = false_constant
2.30 -
2.31 - # Native class constants.
2.32 -
2.33 - cls = self.machine._get_class("__builtins__", "int")
2.34 - self.int_class = cls.location
2.35 - self.int_instance = cls.instance_template_location
2.36 - cls = self.machine._get_class("__builtins__", "list")
2.37 - self.list_class = cls.location
2.38 - self.list_instance = cls.instance_template_location
2.39 - cls = self.machine._get_class("__builtins__", "IndexError")
2.40 - self.index_error = cls.location
2.41 - self.index_error_instance = cls.instance_template_location
2.42 -
2.43 - self.tuple_class = self.machine.tuple_class
2.44 - self.type_error_instance = self.machine.type_error_instance
2.45 -
2.46 - self.frame_stack = self.machine.frame_stack
2.47 - self.local_sp_stack = self.machine.local_sp_stack
2.48 -
2.49 - def builtins_int_arithmetic_op(self, op):
2.50 - frame = self.local_sp_stack[-1]
2.51 -
2.52 - # Get operands addresses.
2.53 -
2.54 - left_context, left = self.frame_stack[frame]
2.55 - right_context, right = self.frame_stack[frame + 1]
2.56 -
2.57 - # Test operand suitability.
2.58 - # NOTE: Support other types.
2.59 -
2.60 - if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
2.61 - self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
2.62 - return self.machine.RaiseException()
2.63 -
2.64 - # NOTE: Assume single location for data.
2.65 -
2.66 - left_data = left + 1
2.67 - right_data = right + 1
2.68 -
2.69 - # Make a new object.
2.70 -
2.71 - addr = self.machine._MakeObject(2, self.int_instance)
2.72 -
2.73 - # Store the result.
2.74 - # NOTE: The data is considered ready to use.
2.75 -
2.76 - self.machine.save(addr + 1, op(self.machine.load(left_data), self.machine.load(right_data)))
2.77 -
2.78 - # Return the new object.
2.79 - # Introduce object as context for the new object.
2.80 -
2.81 - self.machine.result = addr, addr
2.82 -
2.83 - def builtins_int_bool(self):
2.84 - frame = self.local_sp_stack[-1]
2.85 -
2.86 - # Get operands addresses.
2.87 -
2.88 - left_context, left = self.frame_stack[frame]
2.89 -
2.90 - # Test operand suitability.
2.91 -
2.92 - if not self.machine._CheckInstance(left, self.int_class):
2.93 - self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
2.94 - return self.machine.RaiseException()
2.95 -
2.96 - # NOTE: Assume single location for data.
2.97 -
2.98 - left_data = left + 1
2.99 -
2.100 - # Test the data.
2.101 - # NOTE: The data is considered ready to use.
2.102 -
2.103 - if self.machine.load(left_data) != 0:
2.104 - self.machine.result = self.true_constant, self.true_constant
2.105 - else:
2.106 - self.machine.result = self.false_constant, self.false_constant
2.107 -
2.108 - def builtins_int_neg(self):
2.109 - frame = self.local_sp_stack[-1]
2.110 -
2.111 - # Get operands addresses.
2.112 -
2.113 - left_context, left = self.frame_stack[frame]
2.114 -
2.115 - # Test operand suitability.
2.116 -
2.117 - if not self.machine._CheckInstance(left, self.int_class):
2.118 - self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
2.119 - return self.machine.RaiseException()
2.120 -
2.121 - # NOTE: Assume single location for data.
2.122 -
2.123 - left_data = left + 1
2.124 -
2.125 - # Make a new object.
2.126 -
2.127 - addr = self.machine._MakeObject(2, self.int_instance)
2.128 -
2.129 - # Store the result.
2.130 - # NOTE: The data is considered ready to use.
2.131 -
2.132 - self.machine.save(addr + 1, -self.machine.load(left_data))
2.133 -
2.134 - # Return the new object.
2.135 - # Introduce object as context for the new object.
2.136 -
2.137 - self.machine.result = addr, addr
2.138 -
2.139 - def builtins_int_op(self, op, true_if_incompatible):
2.140 - frame = self.local_sp_stack[-1]
2.141 -
2.142 - # Get operands addresses.
2.143 -
2.144 - left_context, left = self.frame_stack[frame]
2.145 - right_context, right = self.frame_stack[frame + 1]
2.146 -
2.147 - # Test operand suitability.
2.148 - # NOTE: Support other types.
2.149 - # NOTE: Handle comparisons of incompatible types more appropriately.
2.150 -
2.151 - if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
2.152 - if true_if_incompatible:
2.153 - self.machine.result = self.true_constant, self.true_constant
2.154 - else:
2.155 - self.machine.result = self.false_constant, self.false_constant
2.156 - return
2.157 -
2.158 - # NOTE: Assume single location for data.
2.159 -
2.160 - left_data = left + 1
2.161 - right_data = right + 1
2.162 -
2.163 - # Test the data.
2.164 - # NOTE: The data is considered ready to use.
2.165 -
2.166 - if op(self.machine.load(left_data), self.machine.load(right_data)):
2.167 - self.machine.result = self.true_constant, self.true_constant
2.168 - else:
2.169 - self.machine.result = self.false_constant, self.false_constant
2.170 -
2.171 - def builtins_int_add(self):
2.172 - return self.builtins_int_arithmetic_op(operator.add)
2.173 -
2.174 - def builtins_int_sub(self):
2.175 - return self.builtins_int_arithmetic_op(operator.sub)
2.176 -
2.177 - def builtins_int_lt(self):
2.178 - return self.builtins_int_op(operator.lt, 0)
2.179 -
2.180 - def builtins_int_le(self):
2.181 - return self.builtins_int_op(operator.le, 0)
2.182 -
2.183 - def builtins_int_gt(self):
2.184 - return self.builtins_int_op(operator.gt, 0)
2.185 -
2.186 - def builtins_int_ge(self):
2.187 - return self.builtins_int_op(operator.ge, 0)
2.188 -
2.189 - def builtins_int_eq(self):
2.190 - return self.builtins_int_op(operator.eq, 0)
2.191 -
2.192 - def builtins_int_ne(self):
2.193 - return self.builtins_int_op(operator.ne, 1)
2.194 -
2.195 - def builtins_int_and(self):
2.196 - return self.builtins_int_arithmetic_op(operator.and_)
2.197 -
2.198 - def builtins_int_or(self):
2.199 - return self.builtins_int_arithmetic_op(operator.or_)
2.200 -
2.201 - def builtins_bool_bool(self):
2.202 - frame = self.local_sp_stack[-1]
2.203 -
2.204 - # Get operands addresses.
2.205 -
2.206 - left_context, left = self.frame_stack[frame]
2.207 - self.machine.result = left, left
2.208 -
2.209 - def builtins_list_new(self):
2.210 - frame = self.local_sp_stack[-1]
2.211 -
2.212 - # The first parameter should be empty.
2.213 - # NOTE: Specific copying of tuples/lists.
2.214 -
2.215 - args_context, args = self.frame_stack[frame + 1]
2.216 -
2.217 - # Test operand suitability.
2.218 -
2.219 - if self.machine._CheckInstance(args, self.list_class):
2.220 - _x, sequence = self.machine.load(args + 1)
2.221 - header = self.machine.load(sequence)
2.222 - size = header.occupied_size
2.223 - elif self.machine._CheckInstance(args, self.tuple_class):
2.224 - sequence = args
2.225 - header = self.machine.load(sequence)
2.226 - size = header.size
2.227 - else:
2.228 - self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
2.229 - return self.machine.RaiseException()
2.230 -
2.231 - # Copy the sequence contents.
2.232 -
2.233 - new_fragment = self.machine._MakeFragment(size)
2.234 - for i in range(1, size):
2.235 - self.machine.save(new_fragment + i, self.machine.load(sequence + i))
2.236 -
2.237 - # Make the list instance.
2.238 -
2.239 - addr = self.machine._MakeObject(2, self.list_instance)
2.240 - self.machine.save(addr + 1, (None, new_fragment))
2.241 -
2.242 - self.machine.result = addr, addr
2.243 -
2.244 - def builtins_list_getitem(self):
2.245 - frame = self.local_sp_stack[-1]
2.246 -
2.247 - # Get the operand address.
2.248 -
2.249 - item_context, item = self.frame_stack[frame + 1]
2.250 -
2.251 - # Get the list address.
2.252 -
2.253 - obj_context, obj = self.frame_stack[frame]
2.254 -
2.255 - # Get the fragment address.
2.256 - # NOTE: Assume single location for header.
2.257 -
2.258 - _x, fragment = self.machine.load(obj + 1)
2.259 -
2.260 - # Get the fragment header.
2.261 -
2.262 - header = self.machine.load(fragment)
2.263 - nelements = header.occupied_size - 1
2.264 -
2.265 - # NOTE: Assume single location for data and header.
2.266 -
2.267 - item_pos = self.machine.load(item + 1)
2.268 -
2.269 - if item_pos >= 0 and item_pos < nelements:
2.270 - pass
2.271 - elif item_pos < 0 and item_pos >= -nelements:
2.272 - item_pos = nelements + item_pos
2.273 - else:
2.274 - self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
2.275 - return self.machine.RaiseException()
2.276 -
2.277 - # NOTE: Assume single location for header.
2.278 -
2.279 - self.machine.result = self.machine.load(fragment + 1 + item_pos)
2.280 -
2.281 - def builtins_list_len(self):
2.282 - frame = self.local_sp_stack[-1]
2.283 -
2.284 - # Get the list address.
2.285 -
2.286 - obj_context, obj = self.frame_stack[frame]
2.287 -
2.288 - # Get the fragment address.
2.289 - # NOTE: Assume single location for header.
2.290 -
2.291 - _x, fragment = self.machine.load(obj + 1)
2.292 -
2.293 - # Get the fragment header.
2.294 -
2.295 - header = self.machine.load(fragment)
2.296 - nelements = header.occupied_size - 1
2.297 -
2.298 - # Make a new object.
2.299 -
2.300 - addr = self.machine._MakeObject(2, self.int_instance)
2.301 -
2.302 - # Store the result.
2.303 - # NOTE: The data is considered ready to use.
2.304 -
2.305 - self.machine.save(addr + 1, nelements)
2.306 -
2.307 - # Return the new object.
2.308 - # Introduce object as context for the new object.
2.309 -
2.310 - self.machine.result = addr, addr
2.311 -
2.312 - def builtins_list_append(self):
2.313 - frame = self.local_sp_stack[-1]
2.314 -
2.315 - # Get operand address.
2.316 -
2.317 - arg_context, arg = self.frame_stack[frame + 1]
2.318 -
2.319 - # Get the list address.
2.320 -
2.321 - obj_context, obj = self.frame_stack[frame]
2.322 -
2.323 - # Get the fragment address.
2.324 - # NOTE: Assume single location for header.
2.325 -
2.326 - _x, fragment = self.machine.load(obj + 1)
2.327 -
2.328 - # Get the fragment header.
2.329 -
2.330 - header = self.machine.load(fragment)
2.331 -
2.332 - # Attempt to add the reference.
2.333 -
2.334 - if header.occupied_size < header.allocated_size:
2.335 - self.machine.save(fragment + header.occupied_size, (arg_context, arg))
2.336 - header.occupied_size += 1
2.337 - else:
2.338 -
2.339 - # Make a new fragment, maintaining more space than currently
2.340 - # occupied in order to avoid reallocation.
2.341 -
2.342 - new_fragment = self.machine._MakeFragment(header.allocated_size + 1)
2.343 -
2.344 - # Copy existing elements.
2.345 -
2.346 - for i in range(1, header.allocated_size):
2.347 - self.machine.save(new_fragment + i, self.machine.load(fragment + i))
2.348 -
2.349 - self.machine.save(new_fragment + header.allocated_size, (arg_context, arg))
2.350 -
2.351 - # Set the new fragment in the object.
2.352 - # NOTE: The old fragment could be deallocated.
2.353 -
2.354 - self.machine.save(obj + 1, (None, new_fragment))
2.355 -
2.356 - def builtins_tuple_len(self):
2.357 - frame = self.local_sp_stack[-1]
2.358 -
2.359 - # Get the tuple address.
2.360 -
2.361 - obj_context, obj = self.frame_stack[frame]
2.362 -
2.363 - # Get the header.
2.364 - # NOTE: Assume single location for header.
2.365 -
2.366 - header = self.machine.load(obj)
2.367 - nelements = header.size - 1
2.368 -
2.369 - # Make a new object.
2.370 -
2.371 - addr = self.machine._MakeObject(2, self.int_instance)
2.372 -
2.373 - # Store the result.
2.374 - # NOTE: The data is considered ready to use.
2.375 -
2.376 - self.machine.save(addr + 1, nelements)
2.377 -
2.378 - # Return the new object.
2.379 - # Introduce object as context for the new object.
2.380 -
2.381 - self.machine.result = addr, addr
2.382 -
2.383 - def builtins_tuple_getitem(self):
2.384 - frame = self.local_sp_stack[-1]
2.385 -
2.386 - # Get the operand address.
2.387 -
2.388 - item_context, item = self.frame_stack[frame + 1]
2.389 -
2.390 - # Get the tuple address.
2.391 -
2.392 - obj_context, obj = self.frame_stack[frame]
2.393 -
2.394 - # Get the header.
2.395 - # NOTE: Assume single location for header.
2.396 -
2.397 - header = self.machine.load(obj)
2.398 - nelements = header.size - 1
2.399 -
2.400 - # NOTE: Assume single location for data and header.
2.401 -
2.402 - item_pos = self.machine.load(item + 1)
2.403 -
2.404 - if item_pos >= 0 and item_pos < nelements:
2.405 - pass
2.406 - elif item_pos < 0 and item_pos >= -nelements:
2.407 - item_pos = nelements + item_pos
2.408 - else:
2.409 - self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
2.410 - return self.machine.RaiseException()
2.411 -
2.412 - # NOTE: Assume single location for header.
2.413 -
2.414 - self.machine.result = self.machine.load(obj + 1 + item_pos)
2.415 -
2.416 - def builtins_object_init(self):
2.417 - pass
2.418 -
2.419 - native_functions = {
2.420 -
2.421 - # Native method implementations:
2.422 -
2.423 - "__builtins__.int.__add__" : builtins_int_add,
2.424 - "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct.
2.425 - "__builtins__.int.__sub__" : builtins_int_sub,
2.426 - "__builtins__.int.__iadd__" : builtins_int_add,
2.427 - "__builtins__.int.__bool__" : builtins_int_bool,
2.428 - "__builtins__.int.__neg__" : builtins_int_neg,
2.429 - "__builtins__.int.__lt__" : builtins_int_lt,
2.430 - "__builtins__.int.__le__" : builtins_int_le,
2.431 - "__builtins__.int.__gt__" : builtins_int_gt,
2.432 - "__builtins__.int.__ge__" : builtins_int_ge,
2.433 - "__builtins__.int.__eq__" : builtins_int_eq,
2.434 - "__builtins__.int.__ne__" : builtins_int_ne,
2.435 - "__builtins__.int.__and__" : builtins_int_and,
2.436 - "__builtins__.int.__rand__" : builtins_int_and,
2.437 - "__builtins__.int.__or__" : builtins_int_or,
2.438 - "__builtins__.int.__ror__" : builtins_int_or,
2.439 - "__builtins__.bool.__bool__" : builtins_bool_bool,
2.440 - "__builtins__.list.__getitem__" : builtins_list_getitem,
2.441 - "__builtins__.list.__len__" : builtins_list_len,
2.442 - "__builtins__.list.append" : builtins_list_append,
2.443 - "__builtins__.tuple.__len__" : builtins_tuple_len,
2.444 - "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
2.445 -
2.446 - # Native initialisers:
2.447 -
2.448 - "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation.
2.449 - "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module.
2.450 -
2.451 - # Native instantiators:
2.452 -
2.453 - "__builtins__.list" : builtins_list_new,
2.454 - }
2.455 -
2.456 # Convenience functions.
2.457
2.458 def machine(program, with_builtins=0, debug=0, abort_upon_exception=0):
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/rsvplib.py Wed Sep 30 00:24:41 2009 +0200
3.3 @@ -0,0 +1,464 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +A native function library for a really simple virtual processor.
3.8 +
3.9 +Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk>
3.10 +
3.11 +This program is free software; you can redistribute it and/or modify it under
3.12 +the terms of the GNU General Public License as published by the Free Software
3.13 +Foundation; either version 3 of the License, or (at your option) any later
3.14 +version.
3.15 +
3.16 +This program is distributed in the hope that it will be useful, but WITHOUT
3.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
3.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
3.19 +details.
3.20 +
3.21 +You should have received a copy of the GNU General Public License along with
3.22 +this program. If not, see <http://www.gnu.org/licenses/>.
3.23 +"""
3.24 +
3.25 +import operator
3.26 +
3.27 +class Library:
3.28 +
3.29 + "Native function implementations."
3.30 +
3.31 + def __init__(self, machine, true_constant, false_constant):
3.32 +
3.33 + """
3.34 + Initialise the library with the 'machine' and the addresses
3.35 + 'true_constant' and 'false_constant'.
3.36 + """
3.37 +
3.38 + self.machine = machine
3.39 + self.true_constant = true_constant
3.40 + self.false_constant = false_constant
3.41 +
3.42 + # Native class constants.
3.43 +
3.44 + cls = self.machine._get_class("__builtins__", "int")
3.45 + self.int_class = cls.location
3.46 + self.int_instance = cls.instance_template_location
3.47 + cls = self.machine._get_class("__builtins__", "list")
3.48 + self.list_class = cls.location
3.49 + self.list_instance = cls.instance_template_location
3.50 + cls = self.machine._get_class("__builtins__", "IndexError")
3.51 + self.index_error = cls.location
3.52 + self.index_error_instance = cls.instance_template_location
3.53 +
3.54 + self.tuple_class = self.machine.tuple_class
3.55 + self.type_error_instance = self.machine.type_error_instance
3.56 +
3.57 + self.frame_stack = self.machine.frame_stack
3.58 + self.local_sp_stack = self.machine.local_sp_stack
3.59 +
3.60 + def builtins_int_arithmetic_op(self, op):
3.61 + frame = self.local_sp_stack[-1]
3.62 +
3.63 + # Get operands addresses.
3.64 +
3.65 + left_context, left = self.frame_stack[frame]
3.66 + right_context, right = self.frame_stack[frame + 1]
3.67 +
3.68 + # Test operand suitability.
3.69 + # NOTE: Support other types.
3.70 +
3.71 + if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
3.72 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
3.73 + return self.machine.RaiseException()
3.74 +
3.75 + # NOTE: Assume single location for data.
3.76 +
3.77 + left_data = left + 1
3.78 + right_data = right + 1
3.79 +
3.80 + # Make a new object.
3.81 +
3.82 + addr = self.machine._MakeObject(2, self.int_instance)
3.83 +
3.84 + # Store the result.
3.85 + # NOTE: The data is considered ready to use.
3.86 +
3.87 + self.machine.save(addr + 1, op(self.machine.load(left_data), self.machine.load(right_data)))
3.88 +
3.89 + # Return the new object.
3.90 + # Introduce object as context for the new object.
3.91 +
3.92 + self.machine.result = addr, addr
3.93 +
3.94 + def builtins_int_bool(self):
3.95 + frame = self.local_sp_stack[-1]
3.96 +
3.97 + # Get operands addresses.
3.98 +
3.99 + left_context, left = self.frame_stack[frame]
3.100 +
3.101 + # Test operand suitability.
3.102 +
3.103 + if not self.machine._CheckInstance(left, self.int_class):
3.104 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
3.105 + return self.machine.RaiseException()
3.106 +
3.107 + # NOTE: Assume single location for data.
3.108 +
3.109 + left_data = left + 1
3.110 +
3.111 + # Test the data.
3.112 + # NOTE: The data is considered ready to use.
3.113 +
3.114 + if self.machine.load(left_data) != 0:
3.115 + self.machine.result = self.true_constant, self.true_constant
3.116 + else:
3.117 + self.machine.result = self.false_constant, self.false_constant
3.118 +
3.119 + def builtins_int_neg(self):
3.120 + frame = self.local_sp_stack[-1]
3.121 +
3.122 + # Get operands addresses.
3.123 +
3.124 + left_context, left = self.frame_stack[frame]
3.125 +
3.126 + # Test operand suitability.
3.127 +
3.128 + if not self.machine._CheckInstance(left, self.int_class):
3.129 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
3.130 + return self.machine.RaiseException()
3.131 +
3.132 + # NOTE: Assume single location for data.
3.133 +
3.134 + left_data = left + 1
3.135 +
3.136 + # Make a new object.
3.137 +
3.138 + addr = self.machine._MakeObject(2, self.int_instance)
3.139 +
3.140 + # Store the result.
3.141 + # NOTE: The data is considered ready to use.
3.142 +
3.143 + self.machine.save(addr + 1, -self.machine.load(left_data))
3.144 +
3.145 + # Return the new object.
3.146 + # Introduce object as context for the new object.
3.147 +
3.148 + self.machine.result = addr, addr
3.149 +
3.150 + def builtins_int_op(self, op, true_if_incompatible):
3.151 + frame = self.local_sp_stack[-1]
3.152 +
3.153 + # Get operands addresses.
3.154 +
3.155 + left_context, left = self.frame_stack[frame]
3.156 + right_context, right = self.frame_stack[frame + 1]
3.157 +
3.158 + # Test operand suitability.
3.159 + # NOTE: Support other types.
3.160 + # NOTE: Handle comparisons of incompatible types more appropriately.
3.161 +
3.162 + if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
3.163 + if true_if_incompatible:
3.164 + self.machine.result = self.true_constant, self.true_constant
3.165 + else:
3.166 + self.machine.result = self.false_constant, self.false_constant
3.167 + return
3.168 +
3.169 + # NOTE: Assume single location for data.
3.170 +
3.171 + left_data = left + 1
3.172 + right_data = right + 1
3.173 +
3.174 + # Test the data.
3.175 + # NOTE: The data is considered ready to use.
3.176 +
3.177 + if op(self.machine.load(left_data), self.machine.load(right_data)):
3.178 + self.machine.result = self.true_constant, self.true_constant
3.179 + else:
3.180 + self.machine.result = self.false_constant, self.false_constant
3.181 +
3.182 + def builtins_int_add(self):
3.183 + return self.builtins_int_arithmetic_op(operator.add)
3.184 +
3.185 + def builtins_int_sub(self):
3.186 + return self.builtins_int_arithmetic_op(operator.sub)
3.187 +
3.188 + def builtins_int_lt(self):
3.189 + return self.builtins_int_op(operator.lt, 0)
3.190 +
3.191 + def builtins_int_le(self):
3.192 + return self.builtins_int_op(operator.le, 0)
3.193 +
3.194 + def builtins_int_gt(self):
3.195 + return self.builtins_int_op(operator.gt, 0)
3.196 +
3.197 + def builtins_int_ge(self):
3.198 + return self.builtins_int_op(operator.ge, 0)
3.199 +
3.200 + def builtins_int_eq(self):
3.201 + return self.builtins_int_op(operator.eq, 0)
3.202 +
3.203 + def builtins_int_ne(self):
3.204 + return self.builtins_int_op(operator.ne, 1)
3.205 +
3.206 + def builtins_int_and(self):
3.207 + return self.builtins_int_arithmetic_op(operator.and_)
3.208 +
3.209 + def builtins_int_or(self):
3.210 + return self.builtins_int_arithmetic_op(operator.or_)
3.211 +
3.212 + def builtins_bool_bool(self):
3.213 + frame = self.local_sp_stack[-1]
3.214 +
3.215 + # Get operands addresses.
3.216 +
3.217 + left_context, left = self.frame_stack[frame]
3.218 + self.machine.result = left, left
3.219 +
3.220 + def builtins_list_new(self):
3.221 + frame = self.local_sp_stack[-1]
3.222 +
3.223 + # The first parameter should be empty.
3.224 + # NOTE: Specific copying of tuples/lists.
3.225 +
3.226 + args_context, args = self.frame_stack[frame + 1]
3.227 +
3.228 + # Test operand suitability.
3.229 +
3.230 + if self.machine._CheckInstance(args, self.list_class):
3.231 + _x, sequence = self.machine.load(args + 1)
3.232 + header = self.machine.load(sequence)
3.233 + size = header.occupied_size
3.234 + elif self.machine._CheckInstance(args, self.tuple_class):
3.235 + sequence = args
3.236 + header = self.machine.load(sequence)
3.237 + size = header.size
3.238 + else:
3.239 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
3.240 + return self.machine.RaiseException()
3.241 +
3.242 + # Copy the sequence contents.
3.243 +
3.244 + new_fragment = self.machine._MakeFragment(size)
3.245 + for i in range(1, size):
3.246 + self.machine.save(new_fragment + i, self.machine.load(sequence + i))
3.247 +
3.248 + # Make the list instance.
3.249 +
3.250 + addr = self.machine._MakeObject(2, self.list_instance)
3.251 + self.machine.save(addr + 1, (None, new_fragment))
3.252 +
3.253 + self.machine.result = addr, addr
3.254 +
3.255 + def builtins_list_getitem(self):
3.256 + frame = self.local_sp_stack[-1]
3.257 +
3.258 + # Get the operand address.
3.259 +
3.260 + item_context, item = self.frame_stack[frame + 1]
3.261 +
3.262 + # Get the list address.
3.263 +
3.264 + obj_context, obj = self.frame_stack[frame]
3.265 +
3.266 + # Get the fragment address.
3.267 + # NOTE: Assume single location for header.
3.268 +
3.269 + _x, fragment = self.machine.load(obj + 1)
3.270 +
3.271 + # Get the fragment header.
3.272 +
3.273 + header = self.machine.load(fragment)
3.274 + nelements = header.occupied_size - 1
3.275 +
3.276 + # NOTE: Assume single location for data and header.
3.277 +
3.278 + item_pos = self.machine.load(item + 1)
3.279 +
3.280 + if item_pos >= 0 and item_pos < nelements:
3.281 + pass
3.282 + elif item_pos < 0 and item_pos >= -nelements:
3.283 + item_pos = nelements + item_pos
3.284 + else:
3.285 + self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
3.286 + return self.machine.RaiseException()
3.287 +
3.288 + # NOTE: Assume single location for header.
3.289 +
3.290 + self.machine.result = self.machine.load(fragment + 1 + item_pos)
3.291 +
3.292 + def builtins_list_len(self):
3.293 + frame = self.local_sp_stack[-1]
3.294 +
3.295 + # Get the list address.
3.296 +
3.297 + obj_context, obj = self.frame_stack[frame]
3.298 +
3.299 + # Get the fragment address.
3.300 + # NOTE: Assume single location for header.
3.301 +
3.302 + _x, fragment = self.machine.load(obj + 1)
3.303 +
3.304 + # Get the fragment header.
3.305 +
3.306 + header = self.machine.load(fragment)
3.307 + nelements = header.occupied_size - 1
3.308 +
3.309 + # Make a new object.
3.310 +
3.311 + addr = self.machine._MakeObject(2, self.int_instance)
3.312 +
3.313 + # Store the result.
3.314 + # NOTE: The data is considered ready to use.
3.315 +
3.316 + self.machine.save(addr + 1, nelements)
3.317 +
3.318 + # Return the new object.
3.319 + # Introduce object as context for the new object.
3.320 +
3.321 + self.machine.result = addr, addr
3.322 +
3.323 + def builtins_list_append(self):
3.324 + frame = self.local_sp_stack[-1]
3.325 +
3.326 + # Get operand address.
3.327 +
3.328 + arg_context, arg = self.frame_stack[frame + 1]
3.329 +
3.330 + # Get the list address.
3.331 +
3.332 + obj_context, obj = self.frame_stack[frame]
3.333 +
3.334 + # Get the fragment address.
3.335 + # NOTE: Assume single location for header.
3.336 +
3.337 + _x, fragment = self.machine.load(obj + 1)
3.338 +
3.339 + # Get the fragment header.
3.340 +
3.341 + header = self.machine.load(fragment)
3.342 +
3.343 + # Attempt to add the reference.
3.344 +
3.345 + if header.occupied_size < header.allocated_size:
3.346 + self.machine.save(fragment + header.occupied_size, (arg_context, arg))
3.347 + header.occupied_size += 1
3.348 + else:
3.349 +
3.350 + # Make a new fragment, maintaining more space than currently
3.351 + # occupied in order to avoid reallocation.
3.352 +
3.353 + new_fragment = self.machine._MakeFragment(header.allocated_size + 1)
3.354 +
3.355 + # Copy existing elements.
3.356 +
3.357 + for i in range(1, header.allocated_size):
3.358 + self.machine.save(new_fragment + i, self.machine.load(fragment + i))
3.359 +
3.360 + self.machine.save(new_fragment + header.allocated_size, (arg_context, arg))
3.361 +
3.362 + # Set the new fragment in the object.
3.363 + # NOTE: The old fragment could be deallocated.
3.364 +
3.365 + self.machine.save(obj + 1, (None, new_fragment))
3.366 +
3.367 + def builtins_tuple_len(self):
3.368 + frame = self.local_sp_stack[-1]
3.369 +
3.370 + # Get the tuple address.
3.371 +
3.372 + obj_context, obj = self.frame_stack[frame]
3.373 +
3.374 + # Get the header.
3.375 + # NOTE: Assume single location for header.
3.376 +
3.377 + header = self.machine.load(obj)
3.378 + nelements = header.size - 1
3.379 +
3.380 + # Make a new object.
3.381 +
3.382 + addr = self.machine._MakeObject(2, self.int_instance)
3.383 +
3.384 + # Store the result.
3.385 + # NOTE: The data is considered ready to use.
3.386 +
3.387 + self.machine.save(addr + 1, nelements)
3.388 +
3.389 + # Return the new object.
3.390 + # Introduce object as context for the new object.
3.391 +
3.392 + self.machine.result = addr, addr
3.393 +
3.394 + def builtins_tuple_getitem(self):
3.395 + frame = self.local_sp_stack[-1]
3.396 +
3.397 + # Get the operand address.
3.398 +
3.399 + item_context, item = self.frame_stack[frame + 1]
3.400 +
3.401 + # Get the tuple address.
3.402 +
3.403 + obj_context, obj = self.frame_stack[frame]
3.404 +
3.405 + # Get the header.
3.406 + # NOTE: Assume single location for header.
3.407 +
3.408 + header = self.machine.load(obj)
3.409 + nelements = header.size - 1
3.410 +
3.411 + # NOTE: Assume single location for data and header.
3.412 +
3.413 + item_pos = self.machine.load(item + 1)
3.414 +
3.415 + if item_pos >= 0 and item_pos < nelements:
3.416 + pass
3.417 + elif item_pos < 0 and item_pos >= -nelements:
3.418 + item_pos = nelements + item_pos
3.419 + else:
3.420 + self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
3.421 + return self.machine.RaiseException()
3.422 +
3.423 + # NOTE: Assume single location for header.
3.424 +
3.425 + self.machine.result = self.machine.load(obj + 1 + item_pos)
3.426 +
3.427 + def builtins_object_init(self):
3.428 + pass
3.429 +
3.430 + native_functions = {
3.431 +
3.432 + # Native method implementations:
3.433 +
3.434 + "__builtins__.int.__add__" : builtins_int_add,
3.435 + "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct.
3.436 + "__builtins__.int.__sub__" : builtins_int_sub,
3.437 + "__builtins__.int.__iadd__" : builtins_int_add,
3.438 + "__builtins__.int.__bool__" : builtins_int_bool,
3.439 + "__builtins__.int.__neg__" : builtins_int_neg,
3.440 + "__builtins__.int.__lt__" : builtins_int_lt,
3.441 + "__builtins__.int.__le__" : builtins_int_le,
3.442 + "__builtins__.int.__gt__" : builtins_int_gt,
3.443 + "__builtins__.int.__ge__" : builtins_int_ge,
3.444 + "__builtins__.int.__eq__" : builtins_int_eq,
3.445 + "__builtins__.int.__ne__" : builtins_int_ne,
3.446 + "__builtins__.int.__and__" : builtins_int_and,
3.447 + "__builtins__.int.__rand__" : builtins_int_and,
3.448 + "__builtins__.int.__or__" : builtins_int_or,
3.449 + "__builtins__.int.__ror__" : builtins_int_or,
3.450 + "__builtins__.bool.__bool__" : builtins_bool_bool,
3.451 + "__builtins__.list.__getitem__" : builtins_list_getitem,
3.452 + "__builtins__.list.__len__" : builtins_list_len,
3.453 + "__builtins__.list.append" : builtins_list_append,
3.454 + "__builtins__.tuple.__len__" : builtins_tuple_len,
3.455 + "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
3.456 +
3.457 + # Native initialisers:
3.458 +
3.459 + "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation.
3.460 + "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module.
3.461 +
3.462 + # Native instantiators:
3.463 +
3.464 + "__builtins__.list" : builtins_list_new,
3.465 + }
3.466 +
3.467 +# vim: tabstop=4 expandtab shiftwidth=4