1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rsvplib.py Wed Sep 30 00:24:41 2009 +0200
1.3 @@ -0,0 +1,464 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A native function library for a really simple virtual processor.
1.8 +
1.9 +Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +import operator
1.26 +
1.27 +class Library:
1.28 +
1.29 + "Native function implementations."
1.30 +
1.31 + def __init__(self, machine, true_constant, false_constant):
1.32 +
1.33 + """
1.34 + Initialise the library with the 'machine' and the addresses
1.35 + 'true_constant' and 'false_constant'.
1.36 + """
1.37 +
1.38 + self.machine = machine
1.39 + self.true_constant = true_constant
1.40 + self.false_constant = false_constant
1.41 +
1.42 + # Native class constants.
1.43 +
1.44 + cls = self.machine._get_class("__builtins__", "int")
1.45 + self.int_class = cls.location
1.46 + self.int_instance = cls.instance_template_location
1.47 + cls = self.machine._get_class("__builtins__", "list")
1.48 + self.list_class = cls.location
1.49 + self.list_instance = cls.instance_template_location
1.50 + cls = self.machine._get_class("__builtins__", "IndexError")
1.51 + self.index_error = cls.location
1.52 + self.index_error_instance = cls.instance_template_location
1.53 +
1.54 + self.tuple_class = self.machine.tuple_class
1.55 + self.type_error_instance = self.machine.type_error_instance
1.56 +
1.57 + self.frame_stack = self.machine.frame_stack
1.58 + self.local_sp_stack = self.machine.local_sp_stack
1.59 +
1.60 + def builtins_int_arithmetic_op(self, op):
1.61 + frame = self.local_sp_stack[-1]
1.62 +
1.63 + # Get operands addresses.
1.64 +
1.65 + left_context, left = self.frame_stack[frame]
1.66 + right_context, right = self.frame_stack[frame + 1]
1.67 +
1.68 + # Test operand suitability.
1.69 + # NOTE: Support other types.
1.70 +
1.71 + if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
1.72 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
1.73 + return self.machine.RaiseException()
1.74 +
1.75 + # NOTE: Assume single location for data.
1.76 +
1.77 + left_data = left + 1
1.78 + right_data = right + 1
1.79 +
1.80 + # Make a new object.
1.81 +
1.82 + addr = self.machine._MakeObject(2, self.int_instance)
1.83 +
1.84 + # Store the result.
1.85 + # NOTE: The data is considered ready to use.
1.86 +
1.87 + self.machine.save(addr + 1, op(self.machine.load(left_data), self.machine.load(right_data)))
1.88 +
1.89 + # Return the new object.
1.90 + # Introduce object as context for the new object.
1.91 +
1.92 + self.machine.result = addr, addr
1.93 +
1.94 + def builtins_int_bool(self):
1.95 + frame = self.local_sp_stack[-1]
1.96 +
1.97 + # Get operands addresses.
1.98 +
1.99 + left_context, left = self.frame_stack[frame]
1.100 +
1.101 + # Test operand suitability.
1.102 +
1.103 + if not self.machine._CheckInstance(left, self.int_class):
1.104 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
1.105 + return self.machine.RaiseException()
1.106 +
1.107 + # NOTE: Assume single location for data.
1.108 +
1.109 + left_data = left + 1
1.110 +
1.111 + # Test the data.
1.112 + # NOTE: The data is considered ready to use.
1.113 +
1.114 + if self.machine.load(left_data) != 0:
1.115 + self.machine.result = self.true_constant, self.true_constant
1.116 + else:
1.117 + self.machine.result = self.false_constant, self.false_constant
1.118 +
1.119 + def builtins_int_neg(self):
1.120 + frame = self.local_sp_stack[-1]
1.121 +
1.122 + # Get operands addresses.
1.123 +
1.124 + left_context, left = self.frame_stack[frame]
1.125 +
1.126 + # Test operand suitability.
1.127 +
1.128 + if not self.machine._CheckInstance(left, self.int_class):
1.129 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
1.130 + return self.machine.RaiseException()
1.131 +
1.132 + # NOTE: Assume single location for data.
1.133 +
1.134 + left_data = left + 1
1.135 +
1.136 + # Make a new object.
1.137 +
1.138 + addr = self.machine._MakeObject(2, self.int_instance)
1.139 +
1.140 + # Store the result.
1.141 + # NOTE: The data is considered ready to use.
1.142 +
1.143 + self.machine.save(addr + 1, -self.machine.load(left_data))
1.144 +
1.145 + # Return the new object.
1.146 + # Introduce object as context for the new object.
1.147 +
1.148 + self.machine.result = addr, addr
1.149 +
1.150 + def builtins_int_op(self, op, true_if_incompatible):
1.151 + frame = self.local_sp_stack[-1]
1.152 +
1.153 + # Get operands addresses.
1.154 +
1.155 + left_context, left = self.frame_stack[frame]
1.156 + right_context, right = self.frame_stack[frame + 1]
1.157 +
1.158 + # Test operand suitability.
1.159 + # NOTE: Support other types.
1.160 + # NOTE: Handle comparisons of incompatible types more appropriately.
1.161 +
1.162 + if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
1.163 + if true_if_incompatible:
1.164 + self.machine.result = self.true_constant, self.true_constant
1.165 + else:
1.166 + self.machine.result = self.false_constant, self.false_constant
1.167 + return
1.168 +
1.169 + # NOTE: Assume single location for data.
1.170 +
1.171 + left_data = left + 1
1.172 + right_data = right + 1
1.173 +
1.174 + # Test the data.
1.175 + # NOTE: The data is considered ready to use.
1.176 +
1.177 + if op(self.machine.load(left_data), self.machine.load(right_data)):
1.178 + self.machine.result = self.true_constant, self.true_constant
1.179 + else:
1.180 + self.machine.result = self.false_constant, self.false_constant
1.181 +
1.182 + def builtins_int_add(self):
1.183 + return self.builtins_int_arithmetic_op(operator.add)
1.184 +
1.185 + def builtins_int_sub(self):
1.186 + return self.builtins_int_arithmetic_op(operator.sub)
1.187 +
1.188 + def builtins_int_lt(self):
1.189 + return self.builtins_int_op(operator.lt, 0)
1.190 +
1.191 + def builtins_int_le(self):
1.192 + return self.builtins_int_op(operator.le, 0)
1.193 +
1.194 + def builtins_int_gt(self):
1.195 + return self.builtins_int_op(operator.gt, 0)
1.196 +
1.197 + def builtins_int_ge(self):
1.198 + return self.builtins_int_op(operator.ge, 0)
1.199 +
1.200 + def builtins_int_eq(self):
1.201 + return self.builtins_int_op(operator.eq, 0)
1.202 +
1.203 + def builtins_int_ne(self):
1.204 + return self.builtins_int_op(operator.ne, 1)
1.205 +
1.206 + def builtins_int_and(self):
1.207 + return self.builtins_int_arithmetic_op(operator.and_)
1.208 +
1.209 + def builtins_int_or(self):
1.210 + return self.builtins_int_arithmetic_op(operator.or_)
1.211 +
1.212 + def builtins_bool_bool(self):
1.213 + frame = self.local_sp_stack[-1]
1.214 +
1.215 + # Get operands addresses.
1.216 +
1.217 + left_context, left = self.frame_stack[frame]
1.218 + self.machine.result = left, left
1.219 +
1.220 + def builtins_list_new(self):
1.221 + frame = self.local_sp_stack[-1]
1.222 +
1.223 + # The first parameter should be empty.
1.224 + # NOTE: Specific copying of tuples/lists.
1.225 +
1.226 + args_context, args = self.frame_stack[frame + 1]
1.227 +
1.228 + # Test operand suitability.
1.229 +
1.230 + if self.machine._CheckInstance(args, self.list_class):
1.231 + _x, sequence = self.machine.load(args + 1)
1.232 + header = self.machine.load(sequence)
1.233 + size = header.occupied_size
1.234 + elif self.machine._CheckInstance(args, self.tuple_class):
1.235 + sequence = args
1.236 + header = self.machine.load(sequence)
1.237 + size = header.size
1.238 + else:
1.239 + self.machine.exception = self.machine._MakeObject(2, self.type_error_instance)
1.240 + return self.machine.RaiseException()
1.241 +
1.242 + # Copy the sequence contents.
1.243 +
1.244 + new_fragment = self.machine._MakeFragment(size)
1.245 + for i in range(1, size):
1.246 + self.machine.save(new_fragment + i, self.machine.load(sequence + i))
1.247 +
1.248 + # Make the list instance.
1.249 +
1.250 + addr = self.machine._MakeObject(2, self.list_instance)
1.251 + self.machine.save(addr + 1, (None, new_fragment))
1.252 +
1.253 + self.machine.result = addr, addr
1.254 +
1.255 + def builtins_list_getitem(self):
1.256 + frame = self.local_sp_stack[-1]
1.257 +
1.258 + # Get the operand address.
1.259 +
1.260 + item_context, item = self.frame_stack[frame + 1]
1.261 +
1.262 + # Get the list address.
1.263 +
1.264 + obj_context, obj = self.frame_stack[frame]
1.265 +
1.266 + # Get the fragment address.
1.267 + # NOTE: Assume single location for header.
1.268 +
1.269 + _x, fragment = self.machine.load(obj + 1)
1.270 +
1.271 + # Get the fragment header.
1.272 +
1.273 + header = self.machine.load(fragment)
1.274 + nelements = header.occupied_size - 1
1.275 +
1.276 + # NOTE: Assume single location for data and header.
1.277 +
1.278 + item_pos = self.machine.load(item + 1)
1.279 +
1.280 + if item_pos >= 0 and item_pos < nelements:
1.281 + pass
1.282 + elif item_pos < 0 and item_pos >= -nelements:
1.283 + item_pos = nelements + item_pos
1.284 + else:
1.285 + self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
1.286 + return self.machine.RaiseException()
1.287 +
1.288 + # NOTE: Assume single location for header.
1.289 +
1.290 + self.machine.result = self.machine.load(fragment + 1 + item_pos)
1.291 +
1.292 + def builtins_list_len(self):
1.293 + frame = self.local_sp_stack[-1]
1.294 +
1.295 + # Get the list address.
1.296 +
1.297 + obj_context, obj = self.frame_stack[frame]
1.298 +
1.299 + # Get the fragment address.
1.300 + # NOTE: Assume single location for header.
1.301 +
1.302 + _x, fragment = self.machine.load(obj + 1)
1.303 +
1.304 + # Get the fragment header.
1.305 +
1.306 + header = self.machine.load(fragment)
1.307 + nelements = header.occupied_size - 1
1.308 +
1.309 + # Make a new object.
1.310 +
1.311 + addr = self.machine._MakeObject(2, self.int_instance)
1.312 +
1.313 + # Store the result.
1.314 + # NOTE: The data is considered ready to use.
1.315 +
1.316 + self.machine.save(addr + 1, nelements)
1.317 +
1.318 + # Return the new object.
1.319 + # Introduce object as context for the new object.
1.320 +
1.321 + self.machine.result = addr, addr
1.322 +
1.323 + def builtins_list_append(self):
1.324 + frame = self.local_sp_stack[-1]
1.325 +
1.326 + # Get operand address.
1.327 +
1.328 + arg_context, arg = self.frame_stack[frame + 1]
1.329 +
1.330 + # Get the list address.
1.331 +
1.332 + obj_context, obj = self.frame_stack[frame]
1.333 +
1.334 + # Get the fragment address.
1.335 + # NOTE: Assume single location for header.
1.336 +
1.337 + _x, fragment = self.machine.load(obj + 1)
1.338 +
1.339 + # Get the fragment header.
1.340 +
1.341 + header = self.machine.load(fragment)
1.342 +
1.343 + # Attempt to add the reference.
1.344 +
1.345 + if header.occupied_size < header.allocated_size:
1.346 + self.machine.save(fragment + header.occupied_size, (arg_context, arg))
1.347 + header.occupied_size += 1
1.348 + else:
1.349 +
1.350 + # Make a new fragment, maintaining more space than currently
1.351 + # occupied in order to avoid reallocation.
1.352 +
1.353 + new_fragment = self.machine._MakeFragment(header.allocated_size + 1)
1.354 +
1.355 + # Copy existing elements.
1.356 +
1.357 + for i in range(1, header.allocated_size):
1.358 + self.machine.save(new_fragment + i, self.machine.load(fragment + i))
1.359 +
1.360 + self.machine.save(new_fragment + header.allocated_size, (arg_context, arg))
1.361 +
1.362 + # Set the new fragment in the object.
1.363 + # NOTE: The old fragment could be deallocated.
1.364 +
1.365 + self.machine.save(obj + 1, (None, new_fragment))
1.366 +
1.367 + def builtins_tuple_len(self):
1.368 + frame = self.local_sp_stack[-1]
1.369 +
1.370 + # Get the tuple address.
1.371 +
1.372 + obj_context, obj = self.frame_stack[frame]
1.373 +
1.374 + # Get the header.
1.375 + # NOTE: Assume single location for header.
1.376 +
1.377 + header = self.machine.load(obj)
1.378 + nelements = header.size - 1
1.379 +
1.380 + # Make a new object.
1.381 +
1.382 + addr = self.machine._MakeObject(2, self.int_instance)
1.383 +
1.384 + # Store the result.
1.385 + # NOTE: The data is considered ready to use.
1.386 +
1.387 + self.machine.save(addr + 1, nelements)
1.388 +
1.389 + # Return the new object.
1.390 + # Introduce object as context for the new object.
1.391 +
1.392 + self.machine.result = addr, addr
1.393 +
1.394 + def builtins_tuple_getitem(self):
1.395 + frame = self.local_sp_stack[-1]
1.396 +
1.397 + # Get the operand address.
1.398 +
1.399 + item_context, item = self.frame_stack[frame + 1]
1.400 +
1.401 + # Get the tuple address.
1.402 +
1.403 + obj_context, obj = self.frame_stack[frame]
1.404 +
1.405 + # Get the header.
1.406 + # NOTE: Assume single location for header.
1.407 +
1.408 + header = self.machine.load(obj)
1.409 + nelements = header.size - 1
1.410 +
1.411 + # NOTE: Assume single location for data and header.
1.412 +
1.413 + item_pos = self.machine.load(item + 1)
1.414 +
1.415 + if item_pos >= 0 and item_pos < nelements:
1.416 + pass
1.417 + elif item_pos < 0 and item_pos >= -nelements:
1.418 + item_pos = nelements + item_pos
1.419 + else:
1.420 + self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
1.421 + return self.machine.RaiseException()
1.422 +
1.423 + # NOTE: Assume single location for header.
1.424 +
1.425 + self.machine.result = self.machine.load(obj + 1 + item_pos)
1.426 +
1.427 + def builtins_object_init(self):
1.428 + pass
1.429 +
1.430 + native_functions = {
1.431 +
1.432 + # Native method implementations:
1.433 +
1.434 + "__builtins__.int.__add__" : builtins_int_add,
1.435 + "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct.
1.436 + "__builtins__.int.__sub__" : builtins_int_sub,
1.437 + "__builtins__.int.__iadd__" : builtins_int_add,
1.438 + "__builtins__.int.__bool__" : builtins_int_bool,
1.439 + "__builtins__.int.__neg__" : builtins_int_neg,
1.440 + "__builtins__.int.__lt__" : builtins_int_lt,
1.441 + "__builtins__.int.__le__" : builtins_int_le,
1.442 + "__builtins__.int.__gt__" : builtins_int_gt,
1.443 + "__builtins__.int.__ge__" : builtins_int_ge,
1.444 + "__builtins__.int.__eq__" : builtins_int_eq,
1.445 + "__builtins__.int.__ne__" : builtins_int_ne,
1.446 + "__builtins__.int.__and__" : builtins_int_and,
1.447 + "__builtins__.int.__rand__" : builtins_int_and,
1.448 + "__builtins__.int.__or__" : builtins_int_or,
1.449 + "__builtins__.int.__ror__" : builtins_int_or,
1.450 + "__builtins__.bool.__bool__" : builtins_bool_bool,
1.451 + "__builtins__.list.__getitem__" : builtins_list_getitem,
1.452 + "__builtins__.list.__len__" : builtins_list_len,
1.453 + "__builtins__.list.append" : builtins_list_append,
1.454 + "__builtins__.tuple.__len__" : builtins_tuple_len,
1.455 + "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
1.456 +
1.457 + # Native initialisers:
1.458 +
1.459 + "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation.
1.460 + "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module.
1.461 +
1.462 + # Native instantiators:
1.463 +
1.464 + "__builtins__.list" : builtins_list_new,
1.465 + }
1.466 +
1.467 +# vim: tabstop=4 expandtab shiftwidth=4