paul@261 | 1 | #!/usr/bin/env python |
paul@261 | 2 | |
paul@261 | 3 | """ |
paul@261 | 4 | A native function library for a really simple virtual processor. |
paul@261 | 5 | |
paul@401 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@261 | 7 | |
paul@261 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@261 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@261 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@261 | 11 | version. |
paul@261 | 12 | |
paul@261 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@261 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@261 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@261 | 16 | details. |
paul@261 | 17 | |
paul@261 | 18 | You should have received a copy of the GNU General Public License along with |
paul@261 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@261 | 20 | """ |
paul@261 | 21 | |
paul@264 | 22 | from micropython.program import DataValue |
paul@261 | 23 | import operator |
paul@261 | 24 | |
paul@261 | 25 | class Library: |
paul@261 | 26 | |
paul@261 | 27 | "Native function implementations." |
paul@261 | 28 | |
paul@395 | 29 | # NOTE: These attributes need changing if the instance layout changes. |
paul@395 | 30 | |
paul@401 | 31 | instance_template_size = instance_data_offset = 1 |
paul@401 | 32 | instance_size = instance_template_size + 1 |
paul@408 | 33 | fragment_data_offset = 1 |
paul@395 | 34 | |
paul@354 | 35 | def __init__(self, machine, constants): |
paul@261 | 36 | |
paul@261 | 37 | """ |
paul@354 | 38 | Initialise the library with the 'machine' and the 'constants' addresses |
paul@354 | 39 | dictionary. |
paul@261 | 40 | """ |
paul@261 | 41 | |
paul@261 | 42 | self.machine = machine |
paul@354 | 43 | self.constants = constants |
paul@261 | 44 | |
paul@261 | 45 | # Native class constants. |
paul@261 | 46 | |
paul@412 | 47 | self.int_class, self.int_instance = self._get_builtin_class_and_template("int") |
paul@412 | 48 | self.list_class, self.list_instance = self._get_builtin_class_and_template("list") |
paul@412 | 49 | self.index_error, self.index_error_instance = self._get_builtin_class_and_template("IndexError") |
paul@412 | 50 | self.str_class, self.str_instance = self._get_builtin_class_and_template("basestring") |
paul@412 | 51 | self.accessor_class, self.accessor_instance = self._get_builtin_class_and_template("_accessor") |
paul@261 | 52 | |
paul@261 | 53 | self.tuple_class = self.machine.tuple_class |
paul@408 | 54 | self.tuple_instance = self.machine.tuple_instance |
paul@412 | 55 | |
paul@412 | 56 | self.attr_error_instance = self.machine.attr_error_instance |
paul@261 | 57 | self.type_error_instance = self.machine.type_error_instance |
paul@261 | 58 | |
paul@261 | 59 | self.frame_stack = self.machine.frame_stack |
paul@261 | 60 | self.local_sp_stack = self.machine.local_sp_stack |
paul@261 | 61 | |
paul@412 | 62 | def _get_builtin_class_and_template(self, name): |
paul@412 | 63 | cls = self.machine._get_class("__builtins__", name) |
paul@412 | 64 | if cls is not None: |
paul@412 | 65 | return cls.location, cls.instance_template_location |
paul@412 | 66 | else: |
paul@412 | 67 | return None, None |
paul@412 | 68 | |
paul@391 | 69 | def _check_index(self, pos, nelements): |
paul@391 | 70 | return pos >= 0 and pos < nelements |
paul@390 | 71 | |
paul@261 | 72 | def builtins_int_arithmetic_op(self, op): |
paul@261 | 73 | frame = self.local_sp_stack[-1] |
paul@261 | 74 | |
paul@261 | 75 | # Get operands addresses. |
paul@261 | 76 | |
paul@264 | 77 | left_value = self.frame_stack[frame] |
paul@264 | 78 | right_value = self.frame_stack[frame + 1] |
paul@261 | 79 | |
paul@261 | 80 | # Test operand suitability. |
paul@261 | 81 | # NOTE: Support other types. |
paul@261 | 82 | |
paul@264 | 83 | if not (self.machine._CheckInstance(left_value.ref, self.int_class) and |
paul@264 | 84 | self.machine._CheckInstance(right_value.ref, self.int_class)): |
paul@264 | 85 | |
paul@395 | 86 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.type_error_instance) |
paul@261 | 87 | return self.machine.RaiseException() |
paul@261 | 88 | |
paul@395 | 89 | left_data = left_value.ref + self.instance_data_offset |
paul@395 | 90 | right_data = right_value.ref + self.instance_data_offset |
paul@261 | 91 | |
paul@261 | 92 | # Make a new object. |
paul@261 | 93 | |
paul@395 | 94 | addr = self.machine._MakeObject(self.instance_size, self.int_instance) |
paul@261 | 95 | |
paul@261 | 96 | # Store the result. |
paul@261 | 97 | # NOTE: The data is considered ready to use. |
paul@261 | 98 | |
paul@395 | 99 | self.machine.save(addr + self.instance_data_offset, op(self.machine.load(left_data), self.machine.load(right_data))) |
paul@261 | 100 | |
paul@261 | 101 | # Return the new object. |
paul@261 | 102 | # Introduce object as context for the new object. |
paul@261 | 103 | |
paul@429 | 104 | self.machine.result_context = addr |
paul@429 | 105 | self.machine.result_value = addr |
paul@261 | 106 | |
paul@354 | 107 | def builtins_logical_op(self, operand_class, op): |
paul@262 | 108 | frame = self.local_sp_stack[-1] |
paul@262 | 109 | |
paul@262 | 110 | # Get operands addresses. |
paul@262 | 111 | |
paul@264 | 112 | left_value = self.frame_stack[frame] |
paul@264 | 113 | right_value = self.frame_stack[frame + 1] |
paul@262 | 114 | |
paul@262 | 115 | # Test operand suitability. |
paul@262 | 116 | # NOTE: Handle comparisons of incompatible types more appropriately. |
paul@354 | 117 | # NOTE: Return NotImplemented. |
paul@262 | 118 | |
paul@264 | 119 | if not (self.machine._CheckInstance(left_value.ref, operand_class) and |
paul@264 | 120 | self.machine._CheckInstance(right_value.ref, operand_class)): |
paul@264 | 121 | |
paul@354 | 122 | notimpl = self.constants[NotImplemented] |
paul@429 | 123 | self.machine.result_context = notimpl |
paul@429 | 124 | self.machine.result_value = notimpl |
paul@262 | 125 | return |
paul@262 | 126 | |
paul@395 | 127 | left_data = left_value.ref + self.instance_data_offset |
paul@395 | 128 | right_data = right_value.ref + self.instance_data_offset |
paul@262 | 129 | |
paul@262 | 130 | # Test the data. |
paul@262 | 131 | # NOTE: The data is considered ready to use. |
paul@262 | 132 | |
paul@262 | 133 | if op(self.machine.load(left_data), self.machine.load(right_data)): |
paul@429 | 134 | self.machine.result_context = self.constants[True] |
paul@429 | 135 | self.machine.result_value = self.constants[True] |
paul@262 | 136 | else: |
paul@429 | 137 | self.machine.result_context = self.constants[False] |
paul@429 | 138 | self.machine.result_value = self.constants[False] |
paul@262 | 139 | |
paul@262 | 140 | # Operators. |
paul@262 | 141 | # Although this takes a short-cut by using the operator module, testing is |
paul@262 | 142 | # still performed on the operands to ensure that they qualify for these |
paul@262 | 143 | # native operations. |
paul@262 | 144 | |
paul@262 | 145 | def builtins_int_add(self): |
paul@262 | 146 | return self.builtins_int_arithmetic_op(operator.add) |
paul@262 | 147 | |
paul@262 | 148 | def builtins_int_sub(self): |
paul@262 | 149 | return self.builtins_int_arithmetic_op(operator.sub) |
paul@262 | 150 | |
paul@262 | 151 | def builtins_int_pow(self): |
paul@262 | 152 | return self.builtins_int_arithmetic_op(operator.pow) |
paul@262 | 153 | |
paul@262 | 154 | def builtins_int_lt(self): |
paul@354 | 155 | return self.builtins_logical_op(self.int_class, operator.lt) |
paul@262 | 156 | |
paul@262 | 157 | def builtins_int_le(self): |
paul@354 | 158 | return self.builtins_logical_op(self.int_class, operator.le) |
paul@262 | 159 | |
paul@262 | 160 | def builtins_int_gt(self): |
paul@354 | 161 | return self.builtins_logical_op(self.int_class, operator.gt) |
paul@262 | 162 | |
paul@262 | 163 | def builtins_int_ge(self): |
paul@354 | 164 | return self.builtins_logical_op(self.int_class, operator.ge) |
paul@262 | 165 | |
paul@262 | 166 | def builtins_int_eq(self): |
paul@354 | 167 | return self.builtins_logical_op(self.int_class, operator.eq) |
paul@262 | 168 | |
paul@262 | 169 | def builtins_int_ne(self): |
paul@354 | 170 | return self.builtins_logical_op(self.int_class, operator.ne) |
paul@262 | 171 | |
paul@262 | 172 | def builtins_str_lt(self): |
paul@354 | 173 | return self.builtins_logical_op(self.str_class, operator.lt) |
paul@262 | 174 | |
paul@262 | 175 | def builtins_str_le(self): |
paul@354 | 176 | return self.builtins_logical_op(self.str_class, operator.le) |
paul@262 | 177 | |
paul@262 | 178 | def builtins_str_gt(self): |
paul@354 | 179 | return self.builtins_logical_op(self.str_class, operator.gt) |
paul@262 | 180 | |
paul@262 | 181 | def builtins_str_ge(self): |
paul@354 | 182 | return self.builtins_logical_op(self.str_class, operator.ge) |
paul@262 | 183 | |
paul@262 | 184 | def builtins_str_eq(self): |
paul@354 | 185 | return self.builtins_logical_op(self.str_class, operator.eq) |
paul@262 | 186 | |
paul@262 | 187 | def builtins_str_ne(self): |
paul@354 | 188 | return self.builtins_logical_op(self.str_class, operator.ne) |
paul@262 | 189 | |
paul@262 | 190 | def builtins_int_and(self): |
paul@262 | 191 | return self.builtins_int_arithmetic_op(operator.and_) |
paul@262 | 192 | |
paul@262 | 193 | def builtins_int_or(self): |
paul@262 | 194 | return self.builtins_int_arithmetic_op(operator.or_) |
paul@262 | 195 | |
paul@262 | 196 | # Specific operator methods. |
paul@262 | 197 | |
paul@261 | 198 | def builtins_int_bool(self): |
paul@261 | 199 | frame = self.local_sp_stack[-1] |
paul@261 | 200 | |
paul@261 | 201 | # Get operands addresses. |
paul@261 | 202 | |
paul@264 | 203 | left_value = self.frame_stack[frame] |
paul@261 | 204 | |
paul@261 | 205 | # Test operand suitability. |
paul@261 | 206 | |
paul@264 | 207 | if not self.machine._CheckInstance(left_value.ref, self.int_class): |
paul@395 | 208 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.type_error_instance) |
paul@261 | 209 | return self.machine.RaiseException() |
paul@261 | 210 | |
paul@395 | 211 | left_data = left_value.ref + self.instance_data_offset |
paul@261 | 212 | |
paul@261 | 213 | # Test the data. |
paul@261 | 214 | # NOTE: The data is considered ready to use. |
paul@261 | 215 | |
paul@261 | 216 | if self.machine.load(left_data) != 0: |
paul@429 | 217 | self.machine.result_context = self.constants[True] |
paul@429 | 218 | self.machine.result_value = self.constants[True] |
paul@261 | 219 | else: |
paul@429 | 220 | self.machine.result_context = self.constants[False] |
paul@429 | 221 | self.machine.result_value = self.constants[False] |
paul@261 | 222 | |
paul@261 | 223 | def builtins_int_neg(self): |
paul@261 | 224 | frame = self.local_sp_stack[-1] |
paul@261 | 225 | |
paul@261 | 226 | # Get operands addresses. |
paul@261 | 227 | |
paul@264 | 228 | left_value = self.frame_stack[frame] |
paul@261 | 229 | |
paul@261 | 230 | # Test operand suitability. |
paul@261 | 231 | |
paul@264 | 232 | if not self.machine._CheckInstance(left_value.ref, self.int_class): |
paul@395 | 233 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.type_error_instance) |
paul@261 | 234 | return self.machine.RaiseException() |
paul@261 | 235 | |
paul@395 | 236 | left_data = left_value.ref + self.instance_data_offset |
paul@261 | 237 | |
paul@261 | 238 | # Make a new object. |
paul@261 | 239 | |
paul@395 | 240 | addr = self.machine._MakeObject(self.instance_size, self.int_instance) |
paul@261 | 241 | |
paul@261 | 242 | # Store the result. |
paul@261 | 243 | # NOTE: The data is considered ready to use. |
paul@261 | 244 | |
paul@395 | 245 | self.machine.save(addr + self.instance_data_offset, -self.machine.load(left_data)) |
paul@261 | 246 | |
paul@261 | 247 | # Return the new object. |
paul@261 | 248 | # Introduce object as context for the new object. |
paul@261 | 249 | |
paul@429 | 250 | self.machine.result_context = addr |
paul@429 | 251 | self.machine.result_value = addr |
paul@261 | 252 | |
paul@262 | 253 | # Various built-in methods. |
paul@261 | 254 | |
paul@261 | 255 | def builtins_bool_bool(self): |
paul@261 | 256 | frame = self.local_sp_stack[-1] |
paul@261 | 257 | |
paul@261 | 258 | # Get operands addresses. |
paul@261 | 259 | |
paul@264 | 260 | left_value = self.frame_stack[frame] |
paul@429 | 261 | self.machine.result_context = left_value.ref |
paul@429 | 262 | self.machine.result_value = left_value.ref |
paul@261 | 263 | |
paul@261 | 264 | def builtins_list_new(self): |
paul@261 | 265 | frame = self.local_sp_stack[-1] |
paul@261 | 266 | |
paul@332 | 267 | # The first parameter should be the instance. |
paul@261 | 268 | |
paul@332 | 269 | list_value = self.frame_stack[frame] |
paul@261 | 270 | |
paul@332 | 271 | # Make a new sequence. |
paul@332 | 272 | # NOTE: Using an arbitrary size. |
paul@261 | 273 | |
paul@408 | 274 | new_fragment = self.machine._MakeFragment(self.fragment_data_offset, 5) # include the header |
paul@261 | 275 | |
paul@332 | 276 | # Complete the list instance by saving the fragment reference. |
paul@336 | 277 | # NOTE: This requires an attribute in the list structure. |
paul@261 | 278 | |
paul@395 | 279 | addr = list_value.ref + self.instance_data_offset |
paul@332 | 280 | self.machine.save(addr, DataValue(None, new_fragment)) |
paul@261 | 281 | |
paul@390 | 282 | def builtins_list_get_single_item(self): |
paul@261 | 283 | frame = self.local_sp_stack[-1] |
paul@261 | 284 | |
paul@261 | 285 | # Get the operand address. |
paul@261 | 286 | |
paul@264 | 287 | item_value = self.frame_stack[frame + 1] |
paul@261 | 288 | |
paul@261 | 289 | # Get the list address. |
paul@261 | 290 | |
paul@264 | 291 | obj_value = self.frame_stack[frame] |
paul@261 | 292 | |
paul@261 | 293 | # Get the fragment address. |
paul@261 | 294 | |
paul@395 | 295 | fragment = self.machine.load(obj_value.ref + self.instance_data_offset) |
paul@261 | 296 | |
paul@261 | 297 | # Get the fragment header. |
paul@261 | 298 | |
paul@264 | 299 | header = self.machine.load(fragment.ref) |
paul@408 | 300 | nelements = header.occupied_size - self.fragment_data_offset |
paul@261 | 301 | |
paul@395 | 302 | # Get the item position. |
paul@261 | 303 | |
paul@395 | 304 | item_pos = self.machine.load(item_value.ref + self.instance_data_offset) |
paul@261 | 305 | |
paul@391 | 306 | if not self._check_index(item_pos, nelements): |
paul@395 | 307 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.index_error_instance) |
paul@261 | 308 | return self.machine.RaiseException() |
paul@261 | 309 | |
paul@395 | 310 | # Get the item itself. |
paul@261 | 311 | |
paul@429 | 312 | data = self.machine.load(fragment.ref + self.fragment_data_offset + item_pos) |
paul@429 | 313 | self.machine.result_context = data.context |
paul@429 | 314 | self.machine.result_value = data.ref |
paul@261 | 315 | |
paul@261 | 316 | def builtins_list_len(self): |
paul@261 | 317 | frame = self.local_sp_stack[-1] |
paul@261 | 318 | |
paul@261 | 319 | # Get the list address. |
paul@261 | 320 | |
paul@264 | 321 | obj_value = self.frame_stack[frame] |
paul@261 | 322 | |
paul@261 | 323 | # Get the fragment address. |
paul@261 | 324 | |
paul@395 | 325 | fragment = self.machine.load(obj_value.ref + self.instance_data_offset) |
paul@261 | 326 | |
paul@261 | 327 | # Get the fragment header. |
paul@261 | 328 | |
paul@264 | 329 | header = self.machine.load(fragment.ref) |
paul@408 | 330 | nelements = header.occupied_size - self.fragment_data_offset |
paul@261 | 331 | |
paul@261 | 332 | # Make a new object. |
paul@261 | 333 | |
paul@395 | 334 | addr = self.machine._MakeObject(self.instance_size, self.int_instance) |
paul@261 | 335 | |
paul@261 | 336 | # Store the result. |
paul@261 | 337 | # NOTE: The data is considered ready to use. |
paul@261 | 338 | |
paul@395 | 339 | self.machine.save(addr + self.instance_data_offset, nelements) |
paul@261 | 340 | |
paul@261 | 341 | # Return the new object. |
paul@261 | 342 | # Introduce object as context for the new object. |
paul@261 | 343 | |
paul@429 | 344 | self.machine.result_context = addr |
paul@429 | 345 | self.machine.result_value = addr |
paul@261 | 346 | |
paul@261 | 347 | def builtins_list_append(self): |
paul@261 | 348 | frame = self.local_sp_stack[-1] |
paul@261 | 349 | |
paul@261 | 350 | # Get operand address. |
paul@261 | 351 | |
paul@264 | 352 | arg_value = self.frame_stack[frame + 1] |
paul@261 | 353 | |
paul@261 | 354 | # Get the list address. |
paul@261 | 355 | |
paul@264 | 356 | obj_value = self.frame_stack[frame] |
paul@261 | 357 | |
paul@261 | 358 | # Get the fragment address. |
paul@261 | 359 | |
paul@395 | 360 | fragment = self.machine.load(obj_value.ref + self.instance_data_offset) |
paul@261 | 361 | |
paul@261 | 362 | # Get the fragment header. |
paul@261 | 363 | |
paul@264 | 364 | header = self.machine.load(fragment.ref) |
paul@261 | 365 | |
paul@261 | 366 | # Attempt to add the reference. |
paul@261 | 367 | |
paul@261 | 368 | if header.occupied_size < header.allocated_size: |
paul@264 | 369 | self.machine.save(fragment.ref + header.occupied_size, arg_value) |
paul@261 | 370 | header.occupied_size += 1 |
paul@261 | 371 | else: |
paul@261 | 372 | |
paul@261 | 373 | # Make a new fragment, maintaining more space than currently |
paul@261 | 374 | # occupied in order to avoid reallocation. |
paul@261 | 375 | |
paul@337 | 376 | new_fragment = self.machine._MakeFragment(header.occupied_size + 1, header.occupied_size * 2) |
paul@261 | 377 | |
paul@261 | 378 | # Copy existing elements. |
paul@261 | 379 | |
paul@408 | 380 | for i in range(self.fragment_data_offset, header.occupied_size): |
paul@264 | 381 | self.machine.save(new_fragment + i, self.machine.load(fragment.ref + i)) |
paul@261 | 382 | |
paul@336 | 383 | self.machine.save(new_fragment + header.occupied_size, arg_value) |
paul@261 | 384 | |
paul@261 | 385 | # Set the new fragment in the object. |
paul@261 | 386 | # NOTE: The old fragment could be deallocated. |
paul@261 | 387 | |
paul@395 | 388 | self.machine.save(obj_value.ref + self.instance_data_offset, DataValue(None, new_fragment)) |
paul@261 | 389 | |
paul@422 | 390 | def builtins_tuple_new(self): |
paul@422 | 391 | frame = self.local_sp_stack[-1] |
paul@422 | 392 | |
paul@422 | 393 | # Get the sequence address. |
paul@426 | 394 | # The first argument should be empty since this function acts as an |
paul@426 | 395 | # instantiator, and in instantiators the first argument is reserved so |
paul@426 | 396 | # that it can be filled in for the call to an initialiser without |
paul@426 | 397 | # allocating a new frame. |
paul@422 | 398 | |
paul@422 | 399 | obj_value = self.frame_stack[frame + 1] |
paul@422 | 400 | return self._builtins_tuple(obj_value) |
paul@422 | 401 | |
paul@408 | 402 | def builtins_tuple(self): |
paul@408 | 403 | frame = self.local_sp_stack[-1] |
paul@408 | 404 | |
paul@422 | 405 | # Get the sequence address. |
paul@408 | 406 | |
paul@408 | 407 | obj_value = self.frame_stack[frame] |
paul@422 | 408 | return self._builtins_tuple(obj_value) |
paul@408 | 409 | |
paul@422 | 410 | def _builtins_tuple(self, obj_value): |
paul@422 | 411 | |
paul@422 | 412 | if self.machine._CheckInstance(obj_value.ref, self.tuple_class): |
paul@429 | 413 | self.machine.result_context = obj_value.context |
paul@429 | 414 | self.machine.result_value = obj_value.ref |
paul@422 | 415 | return |
paul@422 | 416 | |
paul@422 | 417 | # Reject non-list, non-tuple types. |
paul@422 | 418 | # NOTE: This should probably accept any sequence. |
paul@422 | 419 | |
paul@422 | 420 | elif not self.machine._CheckInstance(obj_value.ref, self.list_class): |
paul@408 | 421 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.type_error_instance) |
paul@408 | 422 | return self.machine.RaiseException() |
paul@408 | 423 | |
paul@408 | 424 | # Get the fragment address. |
paul@408 | 425 | |
paul@408 | 426 | fragment = self.machine.load(obj_value.ref + self.instance_data_offset) |
paul@408 | 427 | |
paul@408 | 428 | # Get the fragment header. |
paul@408 | 429 | |
paul@408 | 430 | header = self.machine.load(fragment.ref) |
paul@408 | 431 | |
paul@408 | 432 | # Make a new object. |
paul@408 | 433 | |
paul@408 | 434 | addr = self.machine._MakeObject(self.instance_data_offset + header.occupied_size - self.fragment_data_offset, self.tuple_instance) |
paul@408 | 435 | |
paul@408 | 436 | # Copy the fragment contents into the tuple. |
paul@408 | 437 | # NOTE: This might be done by repurposing the fragment in some situations. |
paul@408 | 438 | |
paul@408 | 439 | for i in range(self.fragment_data_offset, header.occupied_size): |
paul@408 | 440 | self.machine.save(addr + self.instance_data_offset + i - self.fragment_data_offset, self.machine.load(fragment.ref + i)) |
paul@408 | 441 | |
paul@408 | 442 | # Return the new object. |
paul@408 | 443 | # Introduce object as context for the new object. |
paul@408 | 444 | |
paul@429 | 445 | self.machine.result_context = addr |
paul@429 | 446 | self.machine.result_value = addr |
paul@408 | 447 | |
paul@261 | 448 | def builtins_tuple_len(self): |
paul@261 | 449 | frame = self.local_sp_stack[-1] |
paul@261 | 450 | |
paul@261 | 451 | # Get the tuple address. |
paul@261 | 452 | |
paul@264 | 453 | obj_value = self.frame_stack[frame] |
paul@261 | 454 | |
paul@261 | 455 | # Get the header. |
paul@261 | 456 | |
paul@264 | 457 | header = self.machine.load(obj_value.ref) |
paul@395 | 458 | nelements = header.size - self.instance_data_offset |
paul@261 | 459 | |
paul@261 | 460 | # Make a new object. |
paul@261 | 461 | |
paul@395 | 462 | addr = self.machine._MakeObject(self.instance_size, self.int_instance) |
paul@261 | 463 | |
paul@261 | 464 | # Store the result. |
paul@261 | 465 | # NOTE: The data is considered ready to use. |
paul@261 | 466 | |
paul@395 | 467 | self.machine.save(addr + self.instance_data_offset, nelements) |
paul@261 | 468 | |
paul@261 | 469 | # Return the new object. |
paul@261 | 470 | # Introduce object as context for the new object. |
paul@261 | 471 | |
paul@429 | 472 | self.machine.result_context = addr |
paul@429 | 473 | self.machine.result_value = addr |
paul@261 | 474 | |
paul@390 | 475 | def builtins_tuple_get_single_item(self): |
paul@261 | 476 | frame = self.local_sp_stack[-1] |
paul@261 | 477 | |
paul@261 | 478 | # Get the operand address. |
paul@261 | 479 | |
paul@264 | 480 | item_value = self.frame_stack[frame + 1] |
paul@261 | 481 | |
paul@261 | 482 | # Get the tuple address. |
paul@261 | 483 | |
paul@264 | 484 | obj_value = self.frame_stack[frame] |
paul@261 | 485 | |
paul@261 | 486 | # Get the header. |
paul@261 | 487 | |
paul@264 | 488 | header = self.machine.load(obj_value.ref) |
paul@395 | 489 | nelements = header.size - self.instance_data_offset |
paul@261 | 490 | |
paul@261 | 491 | # NOTE: Assume single location for data and header. |
paul@261 | 492 | |
paul@395 | 493 | item_pos = self.machine.load(item_value.ref + self.instance_data_offset) |
paul@261 | 494 | |
paul@391 | 495 | if not self._check_index(item_pos, nelements): |
paul@395 | 496 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.index_error_instance) |
paul@261 | 497 | return self.machine.RaiseException() |
paul@261 | 498 | |
paul@395 | 499 | # Get the item. |
paul@261 | 500 | |
paul@429 | 501 | data = self.machine.load(obj_value.ref + self.instance_data_offset + item_pos) |
paul@429 | 502 | self.machine.result_context = data.context |
paul@429 | 503 | self.machine.result_value = data.ref |
paul@261 | 504 | |
paul@261 | 505 | def builtins_object_init(self): |
paul@261 | 506 | pass |
paul@261 | 507 | |
paul@412 | 508 | def builtins_getattr(self): |
paul@412 | 509 | frame = self.local_sp_stack[-1] |
paul@412 | 510 | |
paul@412 | 511 | # Get the object, attribute name. |
paul@412 | 512 | |
paul@412 | 513 | obj_value = self.frame_stack[frame] |
paul@412 | 514 | name_value = self.frame_stack[frame + 1] |
paul@412 | 515 | |
paul@412 | 516 | if not self.machine._CheckInstance(name_value.ref, self.accessor_class): |
paul@412 | 517 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.attr_error_instance) |
paul@412 | 518 | return self.machine.RaiseException() |
paul@412 | 519 | |
paul@412 | 520 | # Get the object table index from the name. It is a bare integer, not a reference. |
paul@412 | 521 | |
paul@412 | 522 | index = self.machine.load(name_value.ref + self.instance_data_offset + 1) |
paul@412 | 523 | |
paul@416 | 524 | # NOTE: This is very much like LoadAttrIndexContextCond. |
paul@412 | 525 | |
paul@412 | 526 | data = self.machine.load(obj_value.ref) |
paul@412 | 527 | element = self.machine.objlist[data.classcode + index] |
paul@412 | 528 | |
paul@412 | 529 | if element is not None: |
paul@412 | 530 | attr_index, static_attr, offset = element |
paul@412 | 531 | if attr_index == index: |
paul@412 | 532 | if static_attr: |
paul@429 | 533 | loaded_data = self.machine.load(offset) # offset is address of class/module attribute |
paul@429 | 534 | if data.attrcode is not None: # absent attrcode == class/module |
paul@429 | 535 | loaded_data = self.machine._LoadAddressContextCond(loaded_data.context, loaded_data.ref, obj_value.ref) |
paul@412 | 536 | else: |
paul@429 | 537 | loaded_data = self.machine.load(obj_value.ref + offset) |
paul@429 | 538 | self.machine.result_context = loaded_data.context |
paul@429 | 539 | self.machine.result_value = loaded_data.ref |
paul@412 | 540 | return |
paul@412 | 541 | |
paul@412 | 542 | self.machine.exception = self.machine._MakeObject(self.instance_size, self.attr_error_instance) |
paul@412 | 543 | return self.machine.RaiseException() |
paul@412 | 544 | |
paul@377 | 545 | def builtins_isinstance(self): |
paul@377 | 546 | frame = self.local_sp_stack[-1] |
paul@377 | 547 | |
paul@377 | 548 | # Get the operand addresses. |
paul@377 | 549 | |
paul@377 | 550 | obj_value = self.frame_stack[frame] |
paul@377 | 551 | cls_value = self.frame_stack[frame + 1] |
paul@377 | 552 | |
paul@377 | 553 | if self.machine._CheckInstance(obj_value.ref, cls_value.ref): |
paul@429 | 554 | self.machine.result_context = self.constants[True] |
paul@429 | 555 | self.machine.result_value = self.constants[True] |
paul@377 | 556 | else: |
paul@429 | 557 | self.machine.result_context = self.constants[False] |
paul@429 | 558 | self.machine.result_value = self.constants[False] |
paul@377 | 559 | |
paul@402 | 560 | def builtins_print(self): |
paul@402 | 561 | # NOTE: Do nothing for now. |
paul@402 | 562 | pass |
paul@402 | 563 | |
paul@402 | 564 | def builtins_printnl(self): |
paul@402 | 565 | # NOTE: Do nothing for now. |
paul@402 | 566 | pass |
paul@402 | 567 | |
paul@261 | 568 | native_functions = { |
paul@261 | 569 | |
paul@261 | 570 | # Native method implementations: |
paul@261 | 571 | |
paul@422 | 572 | "__builtins__.basestring.__lt__" : builtins_str_lt, |
paul@422 | 573 | "__builtins__.basestring.__le__" : builtins_str_le, |
paul@422 | 574 | "__builtins__.basestring.__gt__" : builtins_str_gt, |
paul@422 | 575 | "__builtins__.basestring.__ge__" : builtins_str_ge, |
paul@422 | 576 | "__builtins__.basestring.__eq__" : builtins_str_eq, |
paul@422 | 577 | "__builtins__.basestring.__ne__" : builtins_str_ne, |
paul@422 | 578 | "__builtins__.bool.__bool__" : builtins_bool_bool, |
paul@261 | 579 | "__builtins__.int.__add__" : builtins_int_add, |
paul@261 | 580 | "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct. |
paul@261 | 581 | "__builtins__.int.__sub__" : builtins_int_sub, |
paul@262 | 582 | "__builtins__.int.__pow__" : builtins_int_pow, |
paul@261 | 583 | "__builtins__.int.__iadd__" : builtins_int_add, |
paul@261 | 584 | "__builtins__.int.__bool__" : builtins_int_bool, |
paul@261 | 585 | "__builtins__.int.__neg__" : builtins_int_neg, |
paul@261 | 586 | "__builtins__.int.__lt__" : builtins_int_lt, |
paul@261 | 587 | "__builtins__.int.__le__" : builtins_int_le, |
paul@261 | 588 | "__builtins__.int.__gt__" : builtins_int_gt, |
paul@261 | 589 | "__builtins__.int.__ge__" : builtins_int_ge, |
paul@261 | 590 | "__builtins__.int.__eq__" : builtins_int_eq, |
paul@261 | 591 | "__builtins__.int.__ne__" : builtins_int_ne, |
paul@261 | 592 | "__builtins__.int.__and__" : builtins_int_and, |
paul@261 | 593 | "__builtins__.int.__rand__" : builtins_int_and, |
paul@261 | 594 | "__builtins__.int.__or__" : builtins_int_or, |
paul@261 | 595 | "__builtins__.int.__ror__" : builtins_int_or, |
paul@390 | 596 | "__builtins__.list.__get_single_item__" : builtins_list_get_single_item, |
paul@261 | 597 | "__builtins__.list.__len__" : builtins_list_len, |
paul@261 | 598 | "__builtins__.list.append" : builtins_list_append, |
paul@422 | 599 | "__builtins__.tuple" : builtins_tuple_new, |
paul@261 | 600 | "__builtins__.tuple.__len__" : builtins_tuple_len, |
paul@390 | 601 | "__builtins__.tuple.__get_single_item__" : builtins_tuple_get_single_item, |
paul@261 | 602 | |
paul@261 | 603 | # Native initialisers: |
paul@261 | 604 | |
paul@261 | 605 | "__builtins__.object.__init__" : builtins_object_init, # NOTE: A no-operation. |
paul@261 | 606 | "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module. |
paul@261 | 607 | |
paul@412 | 608 | # Native functions: |
paul@412 | 609 | |
paul@412 | 610 | "__builtins__._getattr" : builtins_getattr, |
paul@412 | 611 | |
paul@332 | 612 | # Native instantiator helpers: |
paul@261 | 613 | |
paul@332 | 614 | "__builtins__.list.__new__" : builtins_list_new, |
paul@377 | 615 | |
paul@377 | 616 | # Native helper functions: |
paul@377 | 617 | |
paul@377 | 618 | "__builtins__._isinstance" : builtins_isinstance, |
paul@402 | 619 | "__builtins__._print" : builtins_print, |
paul@402 | 620 | "__builtins__._printnl" : builtins_printnl, |
paul@408 | 621 | "__builtins__._tuple" : builtins_tuple, |
paul@261 | 622 | } |
paul@261 | 623 | |
paul@261 | 624 | # vim: tabstop=4 expandtab shiftwidth=4 |