universe.py 22.2 KB
Newer Older
1
from som.interpreter.interpreter import Interpreter
Stefan Marr's avatar
Stefan Marr committed
2
from som.interpreter.bytecodes   import Bytecodes 
3 4
from som.vm.symbol_table         import SymbolTable
from som.vmobjects.object        import Object
5 6 7
from som.vmobjects.clazz         import Class
from som.vmobjects.array         import Array
from som.vmobjects.symbol        import Symbol
8
from som.vmobjects.method        import Method
Stefan Marr's avatar
Stefan Marr committed
9
from som.vmobjects.integer       import Integer
10
from som.vmobjects.string        import String
11
from som.vmobjects.block         import Block, block_evaluation_primitive
12
from som.vmobjects.frame         import Frame
13
from som.vmobjects.biginteger    import BigInteger
14
from som.vmobjects.double        import Double
15

Stefan Marr's avatar
Stefan Marr committed
16 17
from som.vm.shell import Shell

18 19
import som.compiler.sourcecode_compiler as sourcecode_compiler

20
import os
21

22 23
from rlib.exit  import Exit
from rlib.osext import path_split
24 25 26

class Universe(object):
    
27
    def __init__(self, avoid_exit = False):
28 29 30 31 32
        self._interpreter    = Interpreter(self)
        self._symbol_table   = SymbolTable()
        
        self._globals        = {}
        
33 34 35 36 37 38
        self.nilObject      = None
        self.trueObject     = None
        self.falseObject    = None
        self.objectClass    = None
        self.classClass     = None
        self.metaclassClass = None
39
        
40 41 42 43 44 45 46 47 48 49 50 51
        self.nilClass       = None
        self.integerClass   = None
        self.bigintegerClass= None
        self.arrayClass     = None
        self.methodClass    = None
        self.symbolClass    = None
        self.frameClass     = None
        self.primitiveClass = None
        self.systemClass    = None
        self.blockClass     = None
        self.stringClass    = None
        self.doubleClass    = None
52 53
        
        self._last_exit_code = 0
54
        self._avoid_exit     = avoid_exit
55
        self._dump_bytecodes = False
Stefan Marr's avatar
Stefan Marr committed
56
        self.classpath       = None
57

58 59 60 61
    def exit(self, error_code):
        if self._avoid_exit:
            self._last_exit_code = error_code
        else:
62
            raise Exit(error_code)
63 64 65 66
    
    def last_exit_code(self):
        return self._last_exit_code
    
67 68 69
    def get_interpreter(self):
        return self._interpreter
    
70 71 72 73 74 75 76 77 78 79
    def execute_method(self, class_name, selector):
        self._initialize_object_system()

        clazz = self.load_class(self.symbol_for(class_name))

        # Lookup the invokable on class
        invokable = clazz.get_class().lookup_invokable(self.symbol_for(selector))

        bootstrap_method = self._create_bootstrap_method()
        bootstrap_frame  = self._create_bootstrap_frame(bootstrap_method, clazz)
80
        
81
        return self.start(bootstrap_frame, invokable)
82
    
83 84 85 86 87 88
    def _create_bootstrap_method(self):
        # Create a fake bootstrap method to simplify later frame traversal
        bootstrap_method = self.new_method(self.symbol_for("bootstrap"), 1, 0)
        bootstrap_method.set_bytecode(0, Bytecodes.halt)
        bootstrap_method.set_number_of_locals(self.new_integer(0))
        bootstrap_method.set_maximum_number_of_stack_elements(self.new_integer(2))
89
        bootstrap_method.set_holder(self.systemClass)
90
        return bootstrap_method
91
    
92 93 94 95 96 97
    def _create_bootstrap_frame(self, bootstrap_method, receiver, arguments = None):
        # Create a fake bootstrap frame with the system object on the stack
        bootstrap_frame = self._interpreter.push_new_frame(bootstrap_method)
        bootstrap_frame.push(receiver)
        
        if arguments:
98
            bootstrap_frame.push(arguments)
99
        return bootstrap_frame
100 101
        
    
102 103
    def interpret(self, arguments):
        # Check for command line switches
104
        arguments = self.handle_arguments(arguments)
105 106

        # Initialize the known universe
107 108
        system_object = self._initialize_object_system()
        bootstrap_method = self._create_bootstrap_method()
109
        
110 111 112 113 114 115 116
        # Start the shell if no filename is given
        if len(arguments) == 0:
            shell = Shell(self, self._interpreter)
            shell.set_bootstrap_method(bootstrap_method)
            shell.start()
            return
        else:
117 118 119
            # Convert the arguments into an array
            arguments_array = self.new_array_with_strings(arguments)
            bootstrap_frame = self._create_bootstrap_frame(bootstrap_method, system_object, arguments_array)
120
            # Lookup the initialize invokable on the system class
121
            initialize = self.systemClass.lookup_invokable(self.symbol_for("initialize:"))
122 123 124 125 126 127 128 129 130

            self.start(bootstrap_frame, initialize)
    
    def start(self, bootstrap_frame, invokable):
        # Invoke the initialize invokable
        invokable.invoke(bootstrap_frame, self._interpreter)

        # Start the interpreter
        return self._interpreter.start()
131
    
132
    def handle_arguments(self, arguments):
133 134 135 136 137 138 139 140
        got_classpath  = False
        remaining_args = []

        i = 0
        while i < len(arguments):
            if arguments[i] == "-cp":
                if i + 1 >= len(arguments):
                    self._print_usage_and_exit()
141
                self.setup_classpath(arguments[i + 1])
142 143 144 145 146 147
                i += 1    # skip class path
                got_classpath = True
            elif arguments[i] == "-d":
                self._dump_bytecodes = True
            else:
                remaining_args.append(arguments[i])
Tobias Pape's avatar
Tobias Pape committed
148
            i += 1
149 150 151
    
        if not got_classpath:
            # Get the default class path of the appropriate size
152
            self.classpath = self._setup_default_classpath()
153 154 155 156 157 158 159

        # check remaining args for class paths, and strip file extension
        i = 0
        while i < len(remaining_args):
            split = self._get_path_class_ext(remaining_args[i])

            if split[0] != "":  # there was a path
160
                self.classpath.insert(0, split[0])
161 162 163 164 165 166
        
            remaining_args[i] = split[1]
            i += 1
        
        return remaining_args
    
167
    def setup_classpath(self, cp):
168
        self.classpath = cp.split(os.pathsep)
169
    
170 171 172
    def _setup_default_classpath(self):
        return ['.']
    
173 174 175
    # take argument of the form "../foo/Test.som" and return
    # "../foo", "Test", "som"
    def _get_path_class_ext(self, path):
176
        return path_split(path)
177 178 179
    
    def _print_usage_and_exit(self):
        # Print the usage
180 181 182 183 184 185
        std_println("Usage: som [-options] [args...]                          ")
        std_println("                                                         ")
        std_println("where options include:                                   ")
        std_println("    -cp <directories separated by " + os.pathsep     + ">")
        std_println("                  set search path for application classes")
        std_println("    -d            enable disassembling")
186 187 188

        # Exit
        self.exit(0)
189

190
    def _initialize_object_system(self):
191
        # Allocate the nil object
192
        self.nilObject = Object(None)
193 194

        # Allocate the Metaclass classes
195
        self.metaclassClass = self.new_metaclass_class()
196 197

        # Allocate the rest of the system classes
198 199 200 201 202 203 204 205 206 207 208 209
        self.objectClass     = self.new_system_class()
        self.nilClass        = self.new_system_class()
        self.classClass      = self.new_system_class()
        self.arrayClass      = self.new_system_class()
        self.symbolClass     = self.new_system_class()
        self.methodClass     = self.new_system_class()
        self.integerClass    = self.new_system_class()
        self.bigintegerClass = self.new_system_class()
        self.frameClass      = self.new_system_class()
        self.primitiveClass  = self.new_system_class()
        self.stringClass     = self.new_system_class()
        self.doubleClass     = self.new_system_class()
210 211

        # Setup the class reference for the nil object
212
        self.nilObject.set_class(self.nilClass)
213 214

        # Initialize the system classes
215 216 217 218 219 220 221 222 223 224 225 226 227
        self._initialize_system_class(self.objectClass,                 None, "Object")
        self._initialize_system_class(self.classClass,      self.objectClass, "Class")
        self._initialize_system_class(self.metaclassClass,   self.classClass, "Metaclass")
        self._initialize_system_class(self.nilClass,        self.objectClass, "Nil")
        self._initialize_system_class(self.arrayClass,      self.objectClass, "Array")
        self._initialize_system_class(self.methodClass,      self.arrayClass, "Method")
        self._initialize_system_class(self.symbolClass,     self.objectClass, "Symbol")
        self._initialize_system_class(self.integerClass,    self.objectClass, "Integer")
        self._initialize_system_class(self.bigintegerClass, self.objectClass, "BigInteger")
        self._initialize_system_class(self.frameClass,       self.arrayClass, "Frame")
        self._initialize_system_class(self.primitiveClass,  self.objectClass, "Primitive")
        self._initialize_system_class(self.stringClass,     self.objectClass, "String")
        self._initialize_system_class(self.doubleClass,     self.objectClass, "Double")
228 229

        # Load methods and fields into the system classes
230 231 232 233 234 235 236 237 238 239 240 241 242
        self._load_system_class(self.objectClass)
        self._load_system_class(self.classClass)
        self._load_system_class(self.metaclassClass)
        self._load_system_class(self.nilClass)
        self._load_system_class(self.arrayClass)
        self._load_system_class(self.methodClass)
        self._load_system_class(self.symbolClass)
        self._load_system_class(self.integerClass)
        self._load_system_class(self.bigintegerClass)
        self._load_system_class(self.frameClass)
        self._load_system_class(self.primitiveClass)
        self._load_system_class(self.stringClass)
        self._load_system_class(self.doubleClass)
243 244

        # Load the generic block class
245
        self.blockClass = self.load_class(self.symbol_for("Block"))
246 247

        # Setup the true and false objects
248 249
        self.trueObject  = self.new_instance(self.load_class(self.symbol_for("True")))
        self.falseObject = self.new_instance(self.load_class(self.symbol_for("False")))
250 251

        # Load the system class and create an instance of it
252 253
        self.systemClass = self.load_class(self.symbol_for("System"))
        system_object = self.new_instance(self.systemClass)
254 255

        # Put special objects and classes into the dictionary of globals
256 257 258
        self.set_global(self.symbol_for("nil"),    self.nilObject)
        self.set_global(self.symbol_for("true"),   self.trueObject)
        self.set_global(self.symbol_for("false"),  self.falseObject)
259
        self.set_global(self.symbol_for("system"), system_object)
260 261
        self.set_global(self.symbol_for("System"), self.systemClass)
        self.set_global(self.symbol_for("Block"),  self.blockClass)
262 263
        return system_object
    
264 265 266 267 268 269 270 271 272
    def symbol_for(self, string):
        # Lookup the symbol in the symbol table
        result = self._symbol_table.lookup(string)
        if result:
            return result
        
        # Create a new symbol and return it
        result = self.new_symbol(string)
        return result
273 274 275
    
    def new_array_with_length(self, length):
        # Allocate a new array and set its class to be the array class
276 277
        result = Array(self.nilObject)
        result.set_class(self.arrayClass)
278 279

        # Set the number of indexable fields to the given value (length)
280
        result.set_number_of_indexable_fields_and_clear(length, self.nilObject)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

        # Return the freshly allocated array
        return result
  
    def new_array_from_list(self, values):
        # Allocate a new array with the same length as the list
        result = self.new_array_with_length(len(values))

        # Copy all elements from the list into the array
        for i in range(len(values)):
            result.set_indexable_field(i, values[i])
    
        # Return the allocated and initialized array
        return result
  
    def new_array_with_strings(self, strings):
        # Allocate a new array with the same length as the string array
        result = self.new_array_with_length(len(strings))

        # Copy all elements from the string array into the array
        for i in range(len(strings)):
            result.set_indexable_field(i, self.new_string(strings[i]))
    
        # Return the allocated and initialized array
        return result
306
    
307 308
    def new_block(self, method, context_frame, arguments):
        # Allocate a new block and set its class to be the block class
309
        result = Block(self.nilObject)
310 311 312 313 314 315 316 317 318 319 320
        result.set_class(self._get_block_class(arguments))

        # Set the method and context of block
        result.set_method(method)
        result.set_context(context_frame)

        # Return the freshly allocated block
        return result

    def new_class(self, class_class):
        # Allocate a new class and set its class to be the given class class
Stefan Marr's avatar
Stefan Marr committed
321
        result = Class(self, class_class.get_number_of_instance_fields())
322 323 324 325 326 327 328
        result.set_class(class_class)

        # Return the freshly allocated class
        return result

    def new_frame(self, previous_frame, method):
        # Allocate a new frame and set its class to be the frame class
329 330
        result = Frame(self.nilObject)
        result.set_class(self.frameClass)
331 332 333 334 335 336 337

        # Compute the maximum number of stack locations (including arguments,
        # locals and extra buffer to support doesNotUnderstand) and set the
        # number of indexable fields accordingly
        length = (method.get_number_of_arguments() +
                  method.get_number_of_locals().get_embedded_integer() +
                  method.get_maximum_number_of_stack_elements().get_embedded_integer() + 2)
338
        result.set_number_of_indexable_fields_and_clear(length, self.nilObject)
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

        # Set the method of the frame and the previous frame
        result.set_method(method)
        if previous_frame:
            result.set_previous_frame(previous_frame)

        # Reset the stack pointer and the bytecode index
        result.reset_stack_pointer()
        result.set_bytecode_index(0)

        # Return the freshly allocated frame
        return result

    def new_method(self, signature, num_bytecodes, num_literals):
        # Allocate a new method and set its class to be the method class
354 355
        result = Method(self.nilObject)
        result.set_class(self.methodClass)
356 357 358 359

        # Set the signature and the number of bytecodes
        result.set_signature(signature)
        result.set_number_of_bytecodes(num_bytecodes)
360
        result.set_number_of_indexable_fields_and_clear(num_literals, self.nilObject)
361 362 363 364 365 366

        # Return the freshly allocated method
        return result

    def new_instance(self, instance_class):
        # Allocate a new instance and set its class to be the given class
367
        result = Object(self.nilObject, instance_class.get_number_of_instance_fields())
368 369 370 371 372 373 374 375
        result.set_class(instance_class)
 
        # Return the freshly allocated instance
        return result

 
    def new_integer(self, value):
        # Allocate a new integer and set its class to be the integer class
376 377
        result = Integer(self.nilObject)
        result.set_class(self.integerClass)
378 379 380 381 382 383 384
     
        # Set the embedded integer of the newly allocated integer
        result.set_embedded_integer(value)
     
        # Return the freshly allocated integer
        return result
 
385
    def new_biginteger(self, value):
386
        # Allocate a new integer and set its class to be the integer class
387 388
        result = BigInteger(self.nilObject)
        result.set_class(self.bigintegerClass)
389 390
 
        # Set the embedded integer of the newly allocated integer
391
        result.set_embedded_biginteger(value)
392 393 394 395 396 397 398
 
        # Return the freshly allocated integer
        return result
 
 
    def new_double(self, value):
        # Allocate a new integer and set its class to be the double class
399 400
        result = Double(self.nilObject)
        result.set_class(self.doubleClass)
401 402 403 404 405 406 407
 
        # Set the embedded double of the newly allocated double
        result.set_embedded_double(value)
 
        # Return the freshly allocated double
        return result
    
408 409 410 411 412 413 414 415 416 417
    def new_metaclass_class(self):
        # Allocate the metaclass classes
        result = Class(self)
        result.set_class(Class(self))

        # Setup the metaclass hierarchy
        result.get_class().set_class(result)

        # Return the freshly allocated metaclass class
        return result
418 419 420

    def new_string(self, embedded_string):
        # Allocate a new string and set its class to be the string class
421 422
        result = String(self.nilObject)
        result.set_class(self.stringClass)
423 424 425 426 427 428
 
        # Put the embedded string into the new string
        result.set_embedded_string(embedded_string)
 
        # Return the freshly allocated string
        return result
429
    
430 431
    def new_symbol(self, string):
        # Allocate a new symbol and set its class to be the symbol class
432 433
        result = Symbol(self.nilObject)
        result.set_class(self.symbolClass)
434 435 436 437 438 439 440 441 442 443

        # Put the string into the symbol
        result.set_string(string)

        # Insert the new symbol into the symbol table
        self._symbol_table.insert(result)

        # Return the freshly allocated symbol
        return result
      
444 445 446 447 448 449
    def new_system_class(self):
        # Allocate the new system class
        system_class = Class(self)

        # Setup the metaclass hierarchy
        system_class.set_class(Class(self))
450
        system_class.get_class().set_class(self.metaclassClass)
451 452 453 454 455 456 457

        # Return the freshly allocated system class
        return system_class
    
    def _initialize_system_class(self, system_class, super_class, name):
        # Initialize the superclass hierarchy
        if super_class:
458
            system_class.set_super_class(super_class)
459 460
            system_class.get_class().set_super_class(super_class.get_class())
        else:
461
            system_class.get_class().set_super_class(self.classClass)
462 463 464 465 466 467 468 469 470 471 472 473
    

        # Initialize the array of instance fields
        system_class.set_instance_fields(self.new_array_with_length(0))
        system_class.get_class().set_instance_fields(self.new_array_with_length(0))

        # Initialize the array of instance invokables
        system_class.set_instance_invokables(self.new_array_with_length(0))
        system_class.get_class().set_instance_invokables(self.new_array_with_length(0))

        # Initialize the name of the system class
        system_class.set_name(self.symbol_for(name))
474
        system_class.get_class().set_name(self.symbol_for(name + " class"))
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

        # Insert the system class into the dictionary of globals
        self.set_global(system_class.get_name(), system_class)
    
    
    def get_global(self, name):
        # Return the global with the given name if it's in the dictionary of globals
        if self.has_global(name):
            return self._globals[name]

        # Global not found
        return None

    def set_global(self, name, value):
        # Insert the given value into the dictionary of globals
        self._globals[name] = value
  

    def has_global(self, name):
        # Returns if the universe has a value for the global of the given name
        return name in self._globals
496
    
497
    def _get_block_class(self, number_of_arguments = None):
498 499
        if not number_of_arguments:
            # Get the generic block class
500
            return self.blockClass
501 502 503 504 505 506 507 508 509 510 511
        
        # Compute the name of the block class with the given number of
        # arguments
        name = self.symbol_for("Block" + str(number_of_arguments))

        # Lookup the specific block class in the dictionary of globals and
        # return it
        if self.has_global(name):
            return self.get_global(name)

        # Get the block class for blocks with the given number of arguments
Stefan Marr's avatar
Stefan Marr committed
512
        result = self._load_class(name, None)
513 514

        # Add the appropriate value primitive to the block class
515
        result.add_instance_primitive(block_evaluation_primitive(number_of_arguments, self))
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

        # Insert the block class into the dictionary of globals
        self.set_global(name, result)

        # Return the loaded block class
        return result

    def load_class(self, name):
        # Check if the requested class is already in the dictionary of globals
        if self.has_global(name):
            return self.get_global(name)

        # Load the class
        result = self._load_class(name, None)

        # Load primitives (if necessary) and return the resulting class
        if result and result.has_primitives():
            result.load_primitives()
    
        return result

    def _load_system_class(self, system_class):
        # Load the system class
        result = self._load_class(system_class.get_name(), system_class)

541 542 543 544 545 546 547
        if not result:
            error_println(system_class.get_name().get_string()
                   + " class could not be loaded. It is likely that the "
                   + " class path has not been initialized properly. "
                   + "Please make sure that the '-cp' parameter is given on the command-line.")
            self.exit(200)

548 549 550 551 552 553
        # Load primitives if necessary
        if result.has_primitives():
            result.load_primitives()

    def _load_class(self, name, system_class):
        # Try loading the class from all different paths
554
        for cpEntry in self.classpath:
555 556
            try:
                # Load the class from a file and return the loaded class
557
                result = sourcecode_compiler.compile_class_from_file(cpEntry, name.get_string(), system_class, self)
558
                if self._dump_bytecodes:
559 560 561
                    from som.compiler.disassembler import dump
                    dump(result.get_class())
                    dump(result)
562 563 564 565 566 567 568 569

                return result
            except IOError:
                # Continue trying different paths
                pass

        # The class could not be found.
        return None
570 571 572 573
    
    def load_shell_class(self, stmt):
        # Load the class from a stream and return the loaded class
        result = sourcecode_compiler.compile_class_from_string(stmt, None, self)
Stefan Marr's avatar
Stefan Marr committed
574
        if self._dump_bytecodes:
575 576
            from som.compiler.disassembler import dump
            dump(result)
577 578
        return result

579
def error_print(msg):
580
    os.write(2, msg)
581

582
def error_println(msg = ""):
583
    os.write(2, msg + "\n")
584

585
def std_print(msg):
586
    print msg,
587

588
def std_println(msg=""):
589 590
    print msg

591 592 593 594 595 596
def main(args):
    u = Universe()
    u.interpret(args[1:])
    u.exit(0)

if __name__ == '__main__':
597 598 599 600 601
    import sys
    try:
        main(sys.argv)
    except Exit as e:
        sys.exit(e.code)