universe.py 20.7 KB
Newer Older
1
from rpython.rlib.rrandom import Random
2
from rpython.rlib import jit
3

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

Stefan Marr's avatar
Stefan Marr committed
20 21
from som.vm.shell import Shell

22 23
import som.compiler.sourcecode_compiler as sourcecode_compiler

24
import os
25 26
import time

27

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

31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class Assoc(object):

    _immutable_fields_ = ["_global_name", "_value?"]

    def __init__(self, global_name, value):
        self._global_name = global_name
        self._value       = value

    def get_value(self):
        return self._value

    def set_value(self, value):
        self._value = value

    def __str__(self):
        return "(%s => %s)" % (self._global_name, self._value)

49

50
class Universe(object):
51 52 53
    
    CURRENT = None
    
54
    _immutable_fields_ = [
55 56 57 58
            "nilObject",
            "trueObject",
            "falseObject",
            "objectClass",
59 60 61
            "classClass",
            "metaclassClass",
            "nilClass",
62
            "integerClass",
63 64 65 66
            "bigintegerClass",
            "arrayClass",
            "methodClass",
            "symbolClass",
67
            "primitiveClass",
68 69 70 71 72 73 74
            "systemClass",
            "blockClass",
            "blockClasses[*]",
            "stringClass",
            "doubleClass",
            "_symbol_table",
            "_globals"]
75

76
    def __init__(self, avoid_exit = False):
77
        self._interpreter    = Interpreter(self)
78
        self._symbol_table   = {}
79
        self._globals        = {}
80

81 82 83 84 85 86
        self.nilObject      = None
        self.trueObject     = None
        self.falseObject    = None
        self.objectClass    = None
        self.classClass     = None
        self.metaclassClass = None
87
        
88 89 90 91 92 93 94 95 96
        self.nilClass       = None
        self.integerClass   = None
        self.bigintegerClass= None
        self.arrayClass     = None
        self.methodClass    = None
        self.symbolClass    = None
        self.primitiveClass = None
        self.systemClass    = None
        self.blockClass     = None
97
        self.blockClasses   = None
98 99
        self.stringClass    = None
        self.doubleClass    = None
100

101
        self._last_exit_code = 0
102
        self._avoid_exit     = avoid_exit
103
        self._dump_bytecodes = False
Stefan Marr's avatar
Stefan Marr committed
104
        self.classpath       = None
105 106 107
        self.start_time      = time.time() # a float of the time in seconds
        self.random          = Random(abs(int(time.clock() * time.time())))

108
        CURRENT = self
109

110 111 112 113
    def exit(self, error_code):
        if self._avoid_exit:
            self._last_exit_code = error_code
        else:
114
            raise Exit(error_code)
115 116 117 118
    
    def last_exit_code(self):
        return self._last_exit_code
    
119 120 121
    def get_interpreter(self):
        return self._interpreter
    
122 123 124 125 126 127 128
    def execute_method(self, class_name, selector):
        self._initialize_object_system()

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

        bootstrap_method = self._create_bootstrap_method()
        bootstrap_frame  = self._create_bootstrap_frame(bootstrap_method, clazz)
129
        
130 131 132 133 134
        # Lookup the invokable on class
        invokable = clazz.get_class(self).lookup_invokable(self.symbol_for(selector))
        
        invokable.invoke(bootstrap_frame, self._interpreter)
        return bootstrap_frame.pop()
135
    
136 137
    def _create_bootstrap_method(self):
        # Create a fake bootstrap method to simplify later frame traversal
138
        bootstrap_method = self.new_method(self.symbol_for("bootstrap"), 1, [],
139 140
                                           self.new_integer(0),
                                           self.new_integer(2))
141
        bootstrap_method.set_bytecode(0, Bytecodes.halt)
142
        bootstrap_method.set_holder(self.systemClass)
143
        return bootstrap_method
144
    
145 146
    def _create_bootstrap_frame(self, bootstrap_method, receiver, arguments = None):
        # Create a fake bootstrap frame with the system object on the stack
147
        bootstrap_frame = self._interpreter.new_frame(None, bootstrap_method, None)
148 149 150
        bootstrap_frame.push(receiver)
        
        if arguments:
151
            bootstrap_frame.push(arguments)
152
        return bootstrap_frame
153 154
        
    
155 156
    def interpret(self, arguments):
        # Check for command line switches
157
        arguments = self.handle_arguments(arguments)
158 159

        # Initialize the known universe
160 161
        system_object = self._initialize_object_system()
        bootstrap_method = self._create_bootstrap_method()
162
        
163 164 165 166 167 168 169
        # 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:
170 171 172
            # 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)
173
            # Lookup the initialize invokable on the system class
174
            initialize = self.systemClass.lookup_invokable(self.symbol_for("initialize:"))
175
            return initialize.invoke(bootstrap_frame, self._interpreter)
176
    
177
    def handle_arguments(self, arguments):
178 179 180 181 182 183 184 185
        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()
186
                self.setup_classpath(arguments[i + 1])
187 188 189 190
                i += 1    # skip class path
                got_classpath = True
            elif arguments[i] == "-d":
                self._dump_bytecodes = True
Tobias Pape's avatar
Tobias Pape committed
191 192
            elif arguments[i] in ["-h", "--help", "-?"]:
                self._print_usage_and_exit()
193 194
            else:
                remaining_args.append(arguments[i])
Tobias Pape's avatar
Tobias Pape committed
195
            i += 1
196 197 198
    
        if not got_classpath:
            # Get the default class path of the appropriate size
199
            self.classpath = self._default_classpath()
200 201 202 203 204 205 206

        # 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
207
                self.classpath.insert(0, split[0])
208 209 210 211 212 213
        
            remaining_args[i] = split[1]
            i += 1
        
        return remaining_args
    
214
    def setup_classpath(self, cp):
215
        self.classpath = cp.split(os.pathsep)
216 217 218

    @staticmethod
    def _default_classpath():
219 220
        return ['.']
    
221 222
    # take argument of the form "../foo/Test.som" and return
    # "../foo", "Test", "som"
223 224
    @staticmethod
    def _get_path_class_ext(path):
225
        return path_split(path)
226 227 228
    
    def _print_usage_and_exit(self):
        # Print the usage
229 230 231 232
        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
233 234 235
        std_println("        set search path for application classes")
        std_println("    -d  enable disassembling")
        std_println("    -h  print this help")
236 237 238

        # Exit
        self.exit(0)
239

240
    def _initialize_object_system(self):
241
        # Allocate the nil object
242
        self.nilObject = Object(None)
243 244

        # Allocate the Metaclass classes
245
        self.metaclassClass = self.new_metaclass_class()
246 247

        # Allocate the rest of the system classes
248 249 250 251 252 253 254 255 256 257 258
        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.primitiveClass  = self.new_system_class()
        self.stringClass     = self.new_system_class()
        self.doubleClass     = self.new_system_class()
259 260

        # Setup the class reference for the nil object
261
        self.nilObject.set_class(self.nilClass)
262 263

        # Initialize the system classes
264 265 266 267 268 269 270 271 272 273 274 275
        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.primitiveClass,  self.objectClass, "Primitive")
        self._initialize_system_class(self.stringClass,     self.objectClass, "String")
        self._initialize_system_class(self.doubleClass,     self.objectClass, "Double")
276 277

        # Load methods and fields into the system classes
278 279 280 281 282 283 284 285 286 287 288 289
        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.primitiveClass)
        self._load_system_class(self.stringClass)
        self._load_system_class(self.doubleClass)
290 291

        # Load the generic block class
292
        self.blockClass = self.load_class(self.symbol_for("Block"))
293 294

        # Setup the true and false objects
295 296 297 298 299 300 301
        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)
302 303

        # Load the system class and create an instance of it
304 305
        self.systemClass = self.load_class(self.symbol_for("System"))
        system_object = self.new_instance(self.systemClass)
306 307

        # Put special objects and classes into the dictionary of globals
308 309 310
        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)
311
        self.set_global(self.symbol_for("system"), system_object)
312 313
        self.set_global(self.symbol_for("System"), self.systemClass)
        self.set_global(self.symbol_for("Block"),  self.blockClass)
314 315 316 317 318
        
        self.set_global(self.symbol_for("Nil"),    self.nilClass)
        
        self.set_global( trueClassName,  trueClass)
        self.set_global(falseClassName, falseClass)
319 320 321 322

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

323
        return system_object
324 325

    @jit.elidable
326 327
    def symbol_for(self, string):
        # Lookup the symbol in the symbol table
328 329
        result = self._symbol_table.get(string, None)
        if result is not None:
330 331 332
            return result
        
        # Create a new symbol and return it
333
        result = self._new_symbol(string)
334
        return result
335 336
    
    def new_array_with_length(self, length):
337
        return Array(self.nilObject, length)
338 339
  
    def new_array_from_list(self, values):
340 341
        make_sure_not_resized(values)
        return Array(self.nilObject, 0, values)
342 343 344 345 346 347 348 349 350 351

    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 result
352 353 354

    @staticmethod
    def new_block(method, context_frame):
355
        return Block(method, context_frame)
356 357 358

    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
359
        result = Class(self, class_class.get_number_of_instance_fields())
360 361 362
        result.set_class(class_class)
        return result

363 364
    @staticmethod
    def new_frame(previous_frame, method, context):
365 366 367 368 369 370 371
        # 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)

372
        return Frame(length, method, context, previous_frame)
373

374 375
    @staticmethod
    def new_method(signature, num_bytecodes, literals,
376
                   num_locals, maximum_number_of_stack_elements):
377 378
        return Method(literals, num_locals, maximum_number_of_stack_elements,
                      num_bytecodes, signature)
379 380

    def new_instance(self, instance_class):
381
        result = Object(self.nilObject, instance_class.get_number_of_instance_fields())
382 383 384
        result.set_class(instance_class)
        return result

385 386
    @staticmethod
    def new_integer(value):
Tobias Pape's avatar
Tobias Pape committed
387
        assert isinstance(value, int)
388
        return Integer(value)
389 390 391

    @staticmethod
    def new_biginteger(value):
392
        return BigInteger(value)
393 394 395

    @staticmethod
    def new_double(value):
396
        return Double(value)
397
    
398 399 400 401 402 403
    def new_metaclass_class(self):
        # Allocate the metaclass classes
        result = Class(self)
        result.set_class(Class(self))

        # Setup the metaclass hierarchy
404
        result.get_class(self).set_class(result)
405 406 407

        # Return the freshly allocated metaclass class
        return result
408

409 410
    @staticmethod
    def new_string(embedded_string):
411
        return String(embedded_string)
412 413

    def _new_symbol(self, string):
414
        result = Symbol(string)
415 416

        # Insert the new symbol into the symbol table
417
        self._symbol_table[string] = result
418 419
        return result
      
420 421 422 423 424 425
    def new_system_class(self):
        # Allocate the new system class
        system_class = Class(self)

        # Setup the metaclass hierarchy
        system_class.set_class(Class(self))
426
        system_class.get_class(self).set_class(self.metaclassClass)
427 428 429 430 431 432 433

        # 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:
434
            system_class.set_super_class(super_class)
435
            system_class.get_class(self).set_super_class(super_class.get_class(self))
436
        else:
437
            system_class.get_class(self).set_super_class(self.classClass)
438 439 440

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

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

        # Initialize the name of the system class
        system_class.set_name(self.symbol_for(name))
449
        system_class.get_class(self).set_name(self.symbol_for(name + " class"))
450 451 452 453 454 455

        # 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
456 457
        # if not, return None
        jit.promote(self)
458 459 460 461 462
        assoc = self._get_global(name)
        if assoc:
            return assoc.get_value()
        else:
            return None
463 464

    @jit.elidable
465
    def _get_global(self, name):
466
        return self._globals.get(name, None)
467 468

    def set_global(self, name, value):
469
        self.get_globals_association(name).set_value(value)
470 471 472

    def has_global(self, name):
        return name in self._globals
473

474 475 476 477 478 479 480 481
    @jit.elidable_promote("all")
    def get_globals_association(self, name):
        assoc = self._globals.get(name, None)
        if assoc is None:
            assoc = Assoc(name, self.nilObject)
            self._globals[name] = assoc
        return assoc

482 483 484 485
    def _get_block_class(self, number_of_arguments):
        return self.blockClasses[number_of_arguments]

    def _make_block_class(self, number_of_arguments):
486 487 488 489 490
        # 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
491
        result = self._load_class(name, None)
492 493

        # Add the appropriate value primitive to the block class
494
        result.add_instance_primitive(block_evaluation_primitive(number_of_arguments, self))
495 496 497 498 499 500 501 502 503

        # 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
504 505 506
        result = self.get_global(name)
        if result is not None:
            return result
507 508 509 510 511 512 513

        # 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()
514 515

        self.set_global(name, result)
516 517 518 519 520 521
        return result

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

522 523
        if not result:
            error_println(system_class.get_name().get_string()
524 525 526
                   + " 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.")
527 528
            self.exit(200)

529 530 531 532 533 534
        # 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
535
        for cpEntry in self.classpath:
536 537
            try:
                # Load the class from a file and return the loaded class
538
                result = sourcecode_compiler.compile_class_from_file(cpEntry, name.get_string(), system_class, self)
539
                if self._dump_bytecodes:
540
                    from som.compiler.disassembler import dump
541
                    dump(result.get_class(self))
542
                    dump(result)
543 544 545 546 547 548 549 550

                return result
            except IOError:
                # Continue trying different paths
                pass

        # The class could not be found.
        return None
551 552 553 554
    
    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
555
        if self._dump_bytecodes:
556 557
            from som.compiler.disassembler import dump
            dump(result)
558 559
        return result

560
def error_print(msg):
561
    os.write(2, msg or "")
562

563
def error_println(msg = ""):
564
    os.write(2, msg + "\n")
565

566
def std_print(msg):
567
    os.write(1, msg or "")
568

569 570
def std_println(msg = ""):
    os.write(1, msg + "\n")
571

572 573 574 575 576
def main(args):
    u = Universe()
    u.interpret(args[1:])
    u.exit(0)

577
def get_current():
578
    return Universe.CURRENT
579

580
if __name__ == '__main__':
581 582 583 584 585
    import sys
    try:
        main(sys.argv)
    except Exit as e:
        sys.exit(e.code)