1 #!/usr/bin/env python 2 3 """ 4 A native function library for a really simple virtual processor. 5 6 Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from micropython.program import DataValue 23 import operator 24 25 class Library: 26 27 "Native function implementations." 28 29 def __init__(self, machine, true_constant, false_constant): 30 31 """ 32 Initialise the library with the 'machine' and the addresses 33 'true_constant' and 'false_constant'. 34 """ 35 36 self.machine = machine 37 self.true_constant = true_constant 38 self.false_constant = false_constant 39 40 # Native class constants. 41 42 cls = self.machine._get_class("__builtins__", "int") 43 self.int_class = cls.location 44 self.int_instance = cls.instance_template_location 45 cls = self.machine._get_class("__builtins__", "list") 46 self.list_class = cls.location 47 self.list_instance = cls.instance_template_location 48 cls = self.machine._get_class("__builtins__", "IndexError") 49 self.index_error = cls.location 50 self.index_error_instance = cls.instance_template_location 51 cls = self.machine._get_class("__builtins__", "basestring") 52 self.str_class = cls.location 53 self.str_instance = cls.instance_template_location 54 55 self.tuple_class = self.machine.tuple_class 56 self.type_error_instance = self.machine.type_error_instance 57 58 self.frame_stack = self.machine.frame_stack 59 self.local_sp_stack = self.machine.local_sp_stack 60 61 def builtins_int_arithmetic_op(self, op): 62 frame = self.local_sp_stack[-1] 63 64 # Get operands addresses. 65 66 left_value = self.frame_stack[frame] 67 right_value = self.frame_stack[frame + 1] 68 69 # Test operand suitability. 70 # NOTE: Support other types. 71 72 if not (self.machine._CheckInstance(left_value.ref, self.int_class) and 73 self.machine._CheckInstance(right_value.ref, self.int_class)): 74 75 self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) 76 return self.machine.RaiseException() 77 78 # NOTE: Assume single location for data. 79 80 left_data = left_value.ref + 1 81 right_data = right_value.ref + 1 82 83 # Make a new object. 84 85 addr = self.machine._MakeObject(2, self.int_instance) 86 87 # Store the result. 88 # NOTE: The data is considered ready to use. 89 90 self.machine.save(addr + 1, op(self.machine.load(left_data), self.machine.load(right_data))) 91 92 # Return the new object. 93 # Introduce object as context for the new object. 94 95 self.machine.result = DataValue(addr, addr) 96 97 def builtins_logical_op(self, operand_class, op, true_if_incompatible): 98 frame = self.local_sp_stack[-1] 99 100 # Get operands addresses. 101 102 left_value = self.frame_stack[frame] 103 right_value = self.frame_stack[frame + 1] 104 105 # Test operand suitability. 106 # NOTE: Handle comparisons of incompatible types more appropriately. 107 108 if not (self.machine._CheckInstance(left_value.ref, operand_class) and 109 self.machine._CheckInstance(right_value.ref, operand_class)): 110 111 if true_if_incompatible: 112 self.machine.result = DataValue(self.true_constant, self.true_constant) 113 else: 114 self.machine.result = DataValue(self.false_constant, self.false_constant) 115 return 116 117 # NOTE: Assume single location for data. 118 119 left_data = left_value.ref + 1 120 right_data = right_value.ref + 1 121 122 # Test the data. 123 # NOTE: The data is considered ready to use. 124 125 if op(self.machine.load(left_data), self.machine.load(right_data)): 126 self.machine.result = DataValue(self.true_constant, self.true_constant) 127 else: 128 self.machine.result = DataValue(self.false_constant, self.false_constant) 129 130 # Operators. 131 # Although this takes a short-cut by using the operator module, testing is 132 # still performed on the operands to ensure that they qualify for these 133 # native operations. 134 135 def builtins_int_add(self): 136 return self.builtins_int_arithmetic_op(operator.add) 137 138 def builtins_int_sub(self): 139 return self.builtins_int_arithmetic_op(operator.sub) 140 141 def builtins_int_pow(self): 142 return self.builtins_int_arithmetic_op(operator.pow) 143 144 def builtins_int_lt(self): 145 return self.builtins_logical_op(self.int_class, operator.lt, 0) 146 147 def builtins_int_le(self): 148 return self.builtins_logical_op(self.int_class, operator.le, 0) 149 150 def builtins_int_gt(self): 151 return self.builtins_logical_op(self.int_class, operator.gt, 0) 152 153 def builtins_int_ge(self): 154 return self.builtins_logical_op(self.int_class, operator.ge, 0) 155 156 def builtins_int_eq(self): 157 return self.builtins_logical_op(self.int_class, operator.eq, 0) 158 159 def builtins_int_ne(self): 160 return self.builtins_logical_op(self.int_class, operator.ne, 1) 161 162 def builtins_str_lt(self): 163 return self.builtins_logical_op(self.str_class, operator.lt, 0) 164 165 def builtins_str_le(self): 166 return self.builtins_logical_op(self.str_class, operator.le, 0) 167 168 def builtins_str_gt(self): 169 return self.builtins_logical_op(self.str_class, operator.gt, 0) 170 171 def builtins_str_ge(self): 172 return self.builtins_logical_op(self.str_class, operator.ge, 0) 173 174 def builtins_str_eq(self): 175 return self.builtins_logical_op(self.str_class, operator.eq, 0) 176 177 def builtins_str_ne(self): 178 return self.builtins_logical_op(self.str_class, operator.ne, 1) 179 180 def builtins_int_and(self): 181 return self.builtins_int_arithmetic_op(operator.and_) 182 183 def builtins_int_or(self): 184 return self.builtins_int_arithmetic_op(operator.or_) 185 186 # Specific operator methods. 187 188 def builtins_int_bool(self): 189 frame = self.local_sp_stack[-1] 190 191 # Get operands addresses. 192 193 left_value = self.frame_stack[frame] 194 195 # Test operand suitability. 196 197 if not self.machine._CheckInstance(left_value.ref, self.int_class): 198 self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) 199 return self.machine.RaiseException() 200 201 # NOTE: Assume single location for data. 202 203 left_data = left_value.ref + 1 204 205 # Test the data. 206 # NOTE: The data is considered ready to use. 207 208 if self.machine.load(left_data) != 0: 209 self.machine.result = DataValue(self.true_constant, self.true_constant) 210 else: 211 self.machine.result = DataValue(self.false_constant, self.false_constant) 212 213 def builtins_int_neg(self): 214 frame = self.local_sp_stack[-1] 215 216 # Get operands addresses. 217 218 left_value = self.frame_stack[frame] 219 220 # Test operand suitability. 221 222 if not self.machine._CheckInstance(left_value.ref, self.int_class): 223 self.machine.exception = self.machine._MakeObject(2, self.type_error_instance) 224 return self.machine.RaiseException() 225 226 # NOTE: Assume single location for data. 227 228 left_data = left_value.ref + 1 229 230 # Make a new object. 231 232 addr = self.machine._MakeObject(2, self.int_instance) 233 234 # Store the result. 235 # NOTE: The data is considered ready to use. 236 237 self.machine.save(addr + 1, -self.machine.load(left_data)) 238 239 # Return the new object. 240 # Introduce object as context for the new object. 241 242 self.machine.result = DataValue(addr, addr) 243 244 # Various built-in methods. 245 246 def builtins_bool_bool(self): 247 frame = self.local_sp_stack[-1] 248 249 # Get operands addresses. 250 251 left_value = self.frame_stack[frame] 252 self.machine.result = DataValue(left_value.ref, left_value.ref) 253 254 def builtins_list_new(self): 255 frame = self.local_sp_stack[-1] 256 257 # The first parameter should be the instance. 258 259 list_value = self.frame_stack[frame] 260 261 # Make a new sequence. 262 # NOTE: Using an arbitrary size. 263 264 new_fragment = self.machine._MakeFragment(1, 5) # include the header 265 266 # Complete the list instance by saving the fragment reference. 267 # NOTE: This requires an attribute in the list structure. 268 269 addr = list_value.ref + 1 270 self.machine.save(addr, DataValue(None, new_fragment)) 271 272 def builtins_list_getitem(self): 273 frame = self.local_sp_stack[-1] 274 275 # Get the operand address. 276 277 item_value = self.frame_stack[frame + 1] 278 279 # Get the list address. 280 281 obj_value = self.frame_stack[frame] 282 283 # Get the fragment address. 284 # NOTE: Assume single location for header. 285 286 fragment = self.machine.load(obj_value.ref + 1) 287 288 # Get the fragment header. 289 290 header = self.machine.load(fragment.ref) 291 nelements = header.occupied_size - 1 292 293 # NOTE: Assume single location for data and header. 294 295 item_pos = self.machine.load(item_value.ref + 1) 296 297 if item_pos >= 0 and item_pos < nelements: 298 pass 299 elif item_pos < 0 and item_pos >= -nelements: 300 item_pos = nelements + item_pos 301 else: 302 self.machine.exception = self.machine._MakeObject(2, self.index_error_instance) 303 return self.machine.RaiseException() 304 305 # NOTE: Assume single location for header. 306 307 self.machine.result = self.machine.load(fragment.ref + 1 + item_pos) 308 309 def builtins_list_len(self): 310 frame = self.local_sp_stack[-1] 311 312 # Get the list address. 313 314 obj_value = self.frame_stack[frame] 315 316 # Get the fragment address. 317 # NOTE: Assume single location for header. 318 319 fragment = self.machine.load(obj_value.ref + 1) 320 321 # Get the fragment header. 322 323 header = self.machine.load(fragment.ref) 324 nelements = header.occupied_size - 1 325 326 # Make a new object. 327 328 addr = self.machine._MakeObject(2, self.int_instance) 329 330 # Store the result. 331 # NOTE: The data is considered ready to use. 332 333 self.machine.save(addr + 1, nelements) 334 335 # Return the new object. 336 # Introduce object as context for the new object. 337 338 self.machine.result = DataValue(addr, addr) 339 340 def builtins_list_append(self): 341 frame = self.local_sp_stack[-1] 342 343 # Get operand address. 344 345 arg_value = self.frame_stack[frame + 1] 346 347 # Get the list address. 348 349 obj_value = self.frame_stack[frame] 350 351 # Get the fragment address. 352 # NOTE: Assume single location for header. 353 354 fragment = self.machine.load(obj_value.ref + 1) 355 356 # Get the fragment header. 357 358 header = self.machine.load(fragment.ref) 359 360 # Attempt to add the reference. 361 362 if header.occupied_size < header.allocated_size: 363 self.machine.save(fragment.ref + header.occupied_size, arg_value) 364 header.occupied_size += 1 365 else: 366 367 # Make a new fragment, maintaining more space than currently 368 # occupied in order to avoid reallocation. 369 370 new_fragment = self.machine._MakeFragment(header.occupied_size + 1, header.occupied_size * 2) 371 372 # Copy existing elements. 373 374 for i in range(1, header.occupied_size): 375 self.machine.save(new_fragment + i, self.machine.load(fragment.ref + i)) 376 377 self.machine.save(new_fragment + header.occupied_size, arg_value) 378 379 # Set the new fragment in the object. 380 # NOTE: The old fragment could be deallocated. 381 382 self.machine.save(obj_value.ref + 1, DataValue(None, new_fragment)) 383 384 def builtins_tuple_len(self): 385 frame = self.local_sp_stack[-1] 386 387 # Get the tuple address. 388 389 obj_value = self.frame_stack[frame] 390 391 # Get the header. 392 # NOTE: Assume single location for header. 393 394 header = self.machine.load(obj_value.ref) 395 nelements = header.size - 1 396 397 # Make a new object. 398 399 addr = self.machine._MakeObject(2, self.int_instance) 400 401 # Store the result. 402 # NOTE: The data is considered ready to use. 403 404 self.machine.save(addr + 1, nelements) 405 406 # Return the new object. 407 # Introduce object as context for the new object. 408 409 self.machine.result = DataValue(addr, addr) 410 411 def builtins_tuple_getitem(self): 412 frame = self.local_sp_stack[-1] 413 414 # Get the operand address. 415 416 item_value = self.frame_stack[frame + 1] 417 418 # Get the tuple address. 419 420 obj_value = self.frame_stack[frame] 421 422 # Get the header. 423 # NOTE: Assume single location for header. 424 425 header = self.machine.load(obj_value.ref) 426 nelements = header.size - 1 427 428 # NOTE: Assume single location for data and header. 429 430 item_pos = self.machine.load(item_value.ref + 1) 431 432 if item_pos >= 0 and item_pos < nelements: 433 pass 434 elif item_pos < 0 and item_pos >= -nelements: 435 item_pos = nelements + item_pos 436 else: 437 self.machine.exception = self.machine._MakeObject(2, self.index_error_instance) 438 return self.machine.RaiseException() 439 440 # NOTE: Assume single location for header. 441 442 self.machine.result = self.machine.load(obj_value.ref + 1 + item_pos) 443 444 def builtins_object_init(self): 445 pass 446 447 native_functions = { 448 449 # Native method implementations: 450 451 "__builtins__.int.__add__" : builtins_int_add, 452 "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct. 453 "__builtins__.int.__sub__" : builtins_int_sub, 454 "__builtins__.int.__pow__" : builtins_int_pow, 455 "__builtins__.int.__iadd__" : builtins_int_add, 456 "__builtins__.int.__bool__" : builtins_int_bool, 457 "__builtins__.int.__neg__" : builtins_int_neg, 458 "__builtins__.int.__lt__" : builtins_int_lt, 459 "__builtins__.int.__le__" : builtins_int_le, 460 "__builtins__.int.__gt__" : builtins_int_gt, 461 "__builtins__.int.__ge__" : builtins_int_ge, 462 "__builtins__.int.__eq__" : builtins_int_eq, 463 "__builtins__.int.__ne__" : builtins_int_ne, 464 "__builtins__.int.__and__" : builtins_int_and, 465 "__builtins__.int.__rand__" : builtins_int_and, 466 "__builtins__.int.__or__" : builtins_int_or, 467 "__builtins__.int.__ror__" : builtins_int_or, 468 "__builtins__.bool.__bool__" : builtins_bool_bool, 469 "__builtins__.list.__getitem__" : builtins_list_getitem, 470 "__builtins__.list.__len__" : builtins_list_len, 471 "__builtins__.list.append" : builtins_list_append, 472 "__builtins__.tuple.__len__" : builtins_tuple_len, 473 "__builtins__.tuple.__getitem__" : builtins_tuple_getitem, 474 "__builtins__.basestring.__lt__" : builtins_str_lt, 475 "__builtins__.basestring.__le__" : builtins_str_le, 476 "__builtins__.basestring.__gt__" : builtins_str_gt, 477 "__builtins__.basestring.__ge__" : builtins_str_ge, 478 "__builtins__.basestring.__eq__" : builtins_str_eq, 479 "__builtins__.basestring.__ne__" : builtins_str_ne, 480 481 # Native initialisers: 482 483 "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation. 484 "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module. 485 486 # Native instantiator helpers: 487 488 "__builtins__.list.__new__" : builtins_list_new, 489 } 490 491 # vim: tabstop=4 expandtab shiftwidth=4