universe.py 21.9 KB
Newer Older
1 2
from rpython.rlib.rrandom import Random

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

Stefan Marr's avatar
Stefan Marr committed
18 19
from som.vm.shell import Shell

20 21
import som.compiler.sourcecode_compiler as sourcecode_compiler

22
import os
23 24
import time

25

26 27
from rlib.exit  import Exit
from rlib.osext import path_split
28 29 30

class Universe(object):
    
31
    def __init__(self, avoid_exit = False):
32 33 34 35 36
        self._interpreter    = Interpreter(self)
        self._symbol_table   = SymbolTable()
        
        self._globals        = {}
        
37 38 39 40 41 42
        self.nilObject      = None
        self.trueObject     = None
        self.falseObject    = None
        self.objectClass    = None
        self.classClass     = None
        self.metaclassClass = None
43
        
44 45 46 47 48 49 50 51 52 53
        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
54
        self.blockClasses   = None
55 56
        self.stringClass    = None
        self.doubleClass    = None
57 58
        
        self._last_exit_code = 0
59
        self._avoid_exit     = avoid_exit
60
        self._dump_bytecodes = False
Stefan Marr's avatar
Stefan Marr committed
61
        self.classpath       = None
62 63 64
        self.start_time      = time.time() # a float of the time in seconds
        self.random          = Random(abs(int(time.clock() * time.time())))

65

66 67 68 69
    def exit(self, error_code):
        if self._avoid_exit:
            self._last_exit_code = error_code
        else:
70
            raise Exit(error_code)
71 72 73 74
    
    def last_exit_code(self):
        return self._last_exit_code
    
75 76 77
    def get_interpreter(self):
        return self._interpreter
    
78 79 80 81 82 83 84 85 86 87
    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)
88
        
89
        return self.start(bootstrap_frame, invokable)
90
    
91 92
    def _create_bootstrap_method(self):
        # Create a fake bootstrap method to simplify later frame traversal
93 94 95
        bootstrap_method = self.new_method(self.symbol_for("bootstrap"), 1, 0,
                                           self.new_integer(0),
                                           self.new_integer(2))
96
        bootstrap_method.set_bytecode(0, Bytecodes.halt)
97
        bootstrap_method.set_holder(self.systemClass)
98
        return bootstrap_method
99
    
100 101
    def _create_bootstrap_frame(self, bootstrap_method, receiver, arguments = None):
        # Create a fake bootstrap frame with the system object on the stack
102
        bootstrap_frame = self._interpreter.push_new_frame(bootstrap_method, self.nilObject)
103 104 105
        bootstrap_frame.push(receiver)
        
        if arguments:
106
            bootstrap_frame.push(arguments)
107
        return bootstrap_frame
108 109
        
    
110 111
    def interpret(self, arguments):
        # Check for command line switches
112
        arguments = self.handle_arguments(arguments)
113 114

        # Initialize the known universe
115 116
        system_object = self._initialize_object_system()
        bootstrap_method = self._create_bootstrap_method()
117
        
118 119 120 121 122 123 124
        # 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:
125 126 127
            # 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)
128
            # Lookup the initialize invokable on the system class
129
            initialize = self.systemClass.lookup_invokable(self.symbol_for("initialize:"))
130 131 132 133 134 135 136 137 138

            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()
139
    
140
    def handle_arguments(self, arguments):
141 142 143 144 145 146 147 148
        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()
149
                self.setup_classpath(arguments[i + 1])
150 151 152 153
                i += 1    # skip class path
                got_classpath = True
            elif arguments[i] == "-d":
                self._dump_bytecodes = True
Tobias Pape's avatar
Tobias Pape committed
154 155
            elif arguments[i] in ["-h", "--help", "-?"]:
                self._print_usage_and_exit()
156 157
            else:
                remaining_args.append(arguments[i])
Tobias Pape's avatar
Tobias Pape committed
158
            i += 1
159 160 161
    
        if not got_classpath:
            # Get the default class path of the appropriate size
162
            self.classpath = self._setup_default_classpath()
163 164 165 166 167 168 169

        # 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
170
                self.classpath.insert(0, split[0])
171 172 173 174 175 176
        
            remaining_args[i] = split[1]
            i += 1
        
        return remaining_args
    
177
    def setup_classpath(self, cp):
178
        self.classpath = cp.split(os.pathsep)
179
    
180 181 182
    def _setup_default_classpath(self):
        return ['.']
    
183 184 185
    # take argument of the form "../foo/Test.som" and return
    # "../foo", "Test", "som"
    def _get_path_class_ext(self, path):
186
        return path_split(path)
187 188 189
    
    def _print_usage_and_exit(self):
        # Print the usage
190 191 192 193
        std_println("Usage: som [-options] [args...]                          ")
        std_println("                                                         ")
        std_println("where options include:                                   ")
        std_println("    -cp <directories separated by " + os.pathsep     + ">")
Tobias Pape's avatar
Tobias Pape committed
194 195 196
        std_println("        set search path for application classes")
        std_println("    -d  enable disassembling")
        std_println("    -h  print this help")
197 198 199

        # Exit
        self.exit(0)
200

201
    def _initialize_object_system(self):
202
        # Allocate the nil object
203
        self.nilObject = Object(None)
204 205

        # Allocate the Metaclass classes
206
        self.metaclassClass = self.new_metaclass_class()
207 208

        # Allocate the rest of the system classes
209 210 211 212 213 214 215 216 217 218 219 220
        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()
221 222

        # Setup the class reference for the nil object
223
        self.nilObject.set_class(self.nilClass)
224 225

        # Initialize the system classes
226 227 228 229 230 231 232 233 234 235 236 237 238
        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")
239 240

        # Load methods and fields into the system classes
241 242 243 244 245 246 247 248 249 250 251 252 253
        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)
254 255

        # Load the generic block class
256
        self.blockClass = self.load_class(self.symbol_for("Block"))
257 258

        # Setup the true and false objects
259 260 261 262 263 264 265
        trueClassName    = self.symbol_for("True")
        trueClass        = self.load_class(trueClassName)
        self.trueObject  = self.new_instance(trueClass)
        
        falseClassName   = self.symbol_for("False")
        falseClass       = self.load_class(falseClassName)
        self.falseObject = self.new_instance(falseClass)
266 267

        # Load the system class and create an instance of it
268 269
        self.systemClass = self.load_class(self.symbol_for("System"))
        system_object = self.new_instance(self.systemClass)
270 271

        # Put special objects and classes into the dictionary of globals
272 273 274
        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)
275
        self.set_global(self.symbol_for("system"), system_object)
276 277
        self.set_global(self.symbol_for("System"), self.systemClass)
        self.set_global(self.symbol_for("Block"),  self.blockClass)
278 279 280 281 282
        
        self.set_global(self.symbol_for("Nil"),    self.nilClass)
        
        self.set_global( trueClassName,  trueClass)
        self.set_global(falseClassName, falseClass)
283 284 285 286

        self.blockClasses = [self.blockClass] + \
                [self._make_block_class(i) for i in [1, 2, 3]]

287 288
        return system_object
    
289 290 291 292 293 294 295 296 297
    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
298 299 300
    
    def new_array_with_length(self, length):
        # Allocate a new array and set its class to be the array class
301
        result = Array(self.nilObject, length)
302
        result.set_class(self.arrayClass)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

        # 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
328
    
329 330
    def new_block(self, method, context_frame, arguments):
        # Allocate a new block and set its class to be the block class
331
        result = Block(self.nilObject, method, context_frame)
332 333 334 335 336 337 338
        result.set_class(self._get_block_class(arguments))

        # 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
339
        result = Class(self, class_class.get_number_of_instance_fields())
340 341 342 343 344
        result.set_class(class_class)

        # Return the freshly allocated class
        return result

345
    def new_frame(self, previous_frame, method, context):
346 347 348 349 350 351 352
        # 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)

353 354 355
        # Allocate a new frame and set its class to be the frame class
        result = Frame(self.nilObject, length, method, context, previous_frame)
        result.set_class(self.frameClass)
356 357 358 359 360 361 362 363

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

        # Return the freshly allocated frame
        return result

364 365
    def new_method(self, signature, num_bytecodes, num_literals,
                   num_locals, maximum_number_of_stack_elements):
366
        # Allocate a new method and set its class to be the method class
367 368 369 370 371 372
        result = Method(self.nilObject,
                        num_literals,
                        num_locals,
                        maximum_number_of_stack_elements,
                        num_bytecodes,
                        signature)
373
        result.set_class(self.methodClass)
374 375 376 377 378 379

        # 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
380
        result = Object(self.nilObject, instance_class.get_number_of_instance_fields())
381 382 383 384 385 386 387
        result.set_class(instance_class)
 
        # Return the freshly allocated instance
        return result

 
    def new_integer(self, value):
Tobias Pape's avatar
Tobias Pape committed
388
        assert isinstance(value, int)
389
        # Allocate a new integer and set its class to be the integer class
390
        result = Integer(self.nilObject, value)
391
        result.set_class(self.integerClass)
392 393 394 395
     
        # Return the freshly allocated integer
        return result
 
396
    def new_biginteger(self, value):
397
        # Allocate a new integer and set its class to be the integer class
398
        result = BigInteger(self.nilObject, value)
399
        result.set_class(self.bigintegerClass)
400 401 402 403 404 405 406
 
        # 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
407
        result = Double(self.nilObject, value)
408
        result.set_class(self.doubleClass)
409 410 411 412
 
        # Return the freshly allocated double
        return result
    
413 414 415 416 417 418 419 420 421 422
    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
423 424 425

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

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

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

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

        # 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:
457
            system_class.set_super_class(super_class)
458 459
            system_class.get_class().set_super_class(super_class.get_class())
        else:
460
            system_class.get_class().set_super_class(self.classClass)
461 462 463 464 465 466 467 468 469 470 471 472
    

        # 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))
473
        system_class.get_class().set_name(self.symbol_for(name + " class"))
474 475 476 477 478 479 480

        # 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
481
        return self._globals.get(name, None)
482 483 484 485 486 487 488 489 490

    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
491 492 493 494 495

    def _get_block_class(self, number_of_arguments):
        return self.blockClasses[number_of_arguments]

    def _make_block_class(self, number_of_arguments):
496 497 498 499 500
        # Compute the name of the block class with the given number of
        # arguments
        name = self.symbol_for("Block" + str(number_of_arguments))

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

        # Add the appropriate value primitive to the block class
504
        result.add_instance_primitive(block_evaluation_primitive(number_of_arguments, self))
505 506 507 508 509 510 511 512 513

        # 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
514 515 516
        result = self.get_global(name)
        if result is not None:
            return result
517 518 519 520 521 522 523 524 525 526 527 528 529 530

        # 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)

531 532 533 534 535 536 537
        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)

538 539 540 541 542 543
        # 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
544
        for cpEntry in self.classpath:
545 546
            try:
                # Load the class from a file and return the loaded class
547
                result = sourcecode_compiler.compile_class_from_file(cpEntry, name.get_string(), system_class, self)
548
                if self._dump_bytecodes:
549 550 551
                    from som.compiler.disassembler import dump
                    dump(result.get_class())
                    dump(result)
552 553 554 555 556 557 558 559

                return result
            except IOError:
                # Continue trying different paths
                pass

        # The class could not be found.
        return None
560 561 562 563
    
    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
564
        if self._dump_bytecodes:
565 566
            from som.compiler.disassembler import dump
            dump(result)
567 568
        return result

569
def error_print(msg):
570
    os.write(2, msg)
571

572
def error_println(msg = ""):
573
    os.write(2, msg + "\n")
574

575
def std_print(msg):
576
    print msg,
577

578
def std_println(msg=""):
579 580
    print msg

581 582 583 584 585 586
def main(args):
    u = Universe()
    u.interpret(args[1:])
    u.exit(0)

if __name__ == '__main__':
587 588 589 590 591
    import sys
    try:
        main(sys.argv)
    except Exit as e:
        sys.exit(e.code)