Exemplo n.º 1
0
def _initialize():
    global _initialized
    if _initialized:
        return
    _initialized = True
    with utils.openCommonFile("flags.yaml") as flagsFile:
        flagList = yaml.load(flagsFile.read())
    code = 1
    for flagName in flagList:
        globals()[flagName] = flagName

        _flagsToCodes[flagName] = code
        _codesToFlags[code] = flagName
        _flagNames[flagName.lower()] = flagName
        code <<= 1
    _flagGroups.append(frozenset([PUBLIC, PROTECTED, PRIVATE]))
    _flagGroups.append(frozenset([COVARIANT, CONTRAVARIANT]))
    _flagGroups.append(frozenset([ABSTRACT, FINAL]))
    _flagGroups.append(frozenset([ABSTRACT, NATIVE]))
Exemplo n.º 2
0
def _initialize():
    global _initialized
    if _initialized:
        return
    _initialized = True
    with utils.openCommonFile("flags.yaml") as flagsFile:
        flagList = yaml.load(flagsFile.read())
    code = 1
    for flagName in flagList:
        globals()[flagName] = flagName

        _flagsToCodes[flagName] = code
        _codesToFlags[code] = flagName
        _canonicalFlags[flagName] = flagName
        _flagNames[flagName.lower()] = flagName
        code <<= 1
    _flagGroups.append(frozenset([PUBLIC, PROTECTED, PRIVATE]))
    _flagGroups.append(frozenset([COVARIANT, CONTRAVARIANT]))
    _flagGroups.append(frozenset([ABSTRACT, FINAL]))
    _flagGroups.append(frozenset([ABSTRACT, NATIVE]))
Exemplo n.º 3
0
def _initialize():
    global _initialized
    if _initialized:
        return
    _initialized = True

    def buildType(typeName):
        if typeName == "unit":
            return ir_types.UnitType
        elif typeName == "boolean":
            return ir_types.BooleanType
        elif typeName == "i8":
            return ir_types.I8Type
        elif typeName == "i16":
            return ir_types.I16Type
        elif typeName == "i32":
            return ir_types.I32Type
        elif typeName == "i64":
            return ir_types.I64Type
        elif typeName == "f32":
            return ir_types.F32Type
        elif typeName == "f64":
            return ir_types.F64Type
        else:
            m = re.match(r"([A-Za-z0-9_-]+)(\??)", typeName)
            clas = _builtinClassNameMap[m.group(1)]
            flags = frozenset([ir_types.NULLABLE_TYPE_FLAG] if m.group(2) ==
                              "?" else [])
            return ir_types.ClassType(clas, (), flags)

    def buildFunction(functionData, namePrefix=None):
        nameComponents = []
        if namePrefix is not None:
            nameComponents.append(namePrefix)
        sourceName = functionData.get("name", CONSTRUCTOR_SUFFIX)
        nameComponents.append(sourceName)
        name = Name(nameComponents)
        id = getattr(bytecode, functionData["id"])
        flags = buildFlags(functionData["flags"])
        function = ir.Function(
            name,
            id,
            sourceName=sourceName,
            returnType=buildType(functionData["returnType"]),
            typeParameters=[],
            parameterTypes=map(buildType, functionData["parameterTypes"]),
            flags=flags,
            insts=functionData.get("insts"))
        _builtinFunctionIdMap[id] = function
        return function

    def buildMethod(functionData, clas):
        function = buildFunction(functionData, clas.name.short())
        assert flags.METHOD in function.flags
        function.definingClass = clas
        function.overriddenBy = {}
        if "overrides" in functionData:
            function.overrides = []
            for overrideName in functionData["overrides"]:
                overrideId = getattr(bytecode, overrideName)
                override = _builtinFunctionIdMap[overrideId]
                function.overrides.append(override)
                override.overriddenBy[clas.id] = function
        return function

    def buildConstructor(functionData, clas):
        function = buildMethod(functionData, clas)
        assert flags.CONSTRUCTOR in function.flags
        return function

    def buildField(fieldData, index, clas):
        name = Name([clas.name.short(), fieldData["name"]])
        ty = buildType(fieldData["type"])
        flags = buildFlags(fieldData["flags"])
        return ir.Field(name,
                        sourceName=fieldData["name"],
                        type=ty,
                        flags=flags,
                        index=index,
                        definingClass=clas)

    def declareClass(classData):
        name = Name([classData["name"]])
        flags = buildFlags(classData["flags"])
        clas = ir.Class(name,
                        None,
                        sourceName=classData["name"],
                        typeParameters=[],
                        methods=[],
                        flags=flags)
        _builtinClasses.append(clas)
        _builtinClassNameMap[classData["name"]] = clas

    def defineClass(classData):
        clas = _builtinClassNameMap[classData["name"]]
        clas.id = getattr(bytecode, classData["id"])
        if not classData["isPrimitive"]:
            if classData["supertype"] is not None:
                superclass = _builtinClassNameMap[classData["supertype"]]
                clas.supertypes = [ir_types.ClassType(superclass)]
            else:
                clas.supertypes = []
            clas.constructors = [
                buildConstructor(ctorData, clas)
                for ctorData in classData["constructors"]
            ]
            clas.fields = [
                buildField(fieldData, index, clas)
                for index, fieldData in enumerate(classData["fields"])
            ]
        else:
            clas.supertypes = []
            clas.fields = []
            clas.isPrimitive = True
        inheritedMethodCount = len(clas.methods)
        for m in classData["methods"]:
            method = buildMethod(m, clas)
            clas.methods.append(method)

        _builtinClassTypeMap[classData["name"]] = clas
        _builtinClassIdMap[clas.id] = clas

    def defineFunction(functionData):
        function = buildFunction(functionData)
        _builtinFunctions.append(function)
        _builtinFunctionNameMap[function.name] = function

    def buildFlags(flagsData):
        return frozenset(map(flags.canonicalizeFlagName, flagsData))

    with utils.openCommonFile("builtins.yaml") as builtinsFile:
        classes, functions = yaml.load_all(builtinsFile.read())
    for ty in classes:
        declareClass(ty)
    for ty in classes:
        defineClass(ty)
    for fn in functions:
        defineFunction(fn)
Exemplo n.º 4
0
def _initialize():
    global _initialized
    if _initialized:
        return
    _initialized = True

    def buildType(typeName):
        if typeName == "unit":
            return ir_types.UnitType
        elif typeName == "boolean":
            return ir_types.BooleanType
        elif typeName == "i8":
            return ir_types.I8Type
        elif typeName == "i16":
            return ir_types.I16Type
        elif typeName == "i32":
            return ir_types.I32Type
        elif typeName == "i64":
            return ir_types.I64Type
        elif typeName == "f32":
            return ir_types.F32Type
        elif typeName == "f64":
            return ir_types.F64Type
        else:
            m = re.match(r"([A-Za-z0-9_-]+)(\??)", typeName)
            clas = _builtinClassNameMap[m.group(1)]
            flags = frozenset([ir_types.NULLABLE_TYPE_FLAG] if m.group(2) == "?" else [])
            return ir_types.ClassType(clas, (), flags)

    def buildFunction(functionData, namePrefix=None):
        nameComponents = []
        if namePrefix is not None:
            nameComponents.append(namePrefix)
        nameComponents.append(functionData.get("name", ir.CONSTRUCTOR_SUFFIX))
        name = ir.Name(nameComponents)
        id = getattr(bytecode, functionData["id"])
        function = ir.Function(name, id,
                               returnType=buildType(functionData["returnType"]),
                               typeParameters=[],
                               parameterTypes=map(buildType, functionData["parameterTypes"]),
                               flags=frozenset([flags.PUBLIC]), insts=functionData.get("insts"))
        _builtinFunctionIdMap[id] = function
        return function

    def buildMethod(functionData, clas):
        function = buildFunction(functionData, clas.name.short())
        function.flags |= frozenset([flags.METHOD])
        function.definingClass = clas
        return function

    def buildConstructor(functionData, clas):
        function = buildMethod(functionData, clas)
        function.flags |= frozenset([flags.CONSTRUCTOR])
        return function

    def buildField(fieldData, clas):
        name = ir.Name([clas.name.short(), fieldData["name"]])
        ty = buildType(fieldData["type"])
        return ir.Field(name, type=ty, flags=frozenset([flags.PUBLIC]))

    def declareClass(classData):
        name = ir.Name([classData["name"]])
        clas = ir.Class(name, None, typeParameters=[], flags=frozenset([flags.PUBLIC]))
        _builtinClasses.append(clas)
        _builtinClassNameMap[classData["name"]] = clas

    def defineClass(classData):
        clas = _builtinClassNameMap[classData["name"]]
        clas.id = getattr(bytecode,classData["id"])
        if not classData["isPrimitive"]:
            if classData["supertype"] is not None:
                superclass = _builtinClassNameMap[classData["supertype"]]
                clas.supertypes = [ir_types.ClassType(superclass)]
                clas.fields = list(superclass.fields)
                clas.methods = list(superclass.methods)
            else:
                clas.supertypes = []
                clas.fields = []
                clas.methods = []
            clas.constructors = [buildConstructor(ctorData, clas)
                                 for ctorData in classData["constructors"]]
            clas.fields += [buildField(fieldData, clas)
                            for fieldData in classData["fields"]]
        else:
            clas.supertypes = []
            clas.fields = []
            clas.methods = []
            clas.isPrimitive = True
        inheritedMethodCount = len(clas.methods)
        for m in classData["methods"]:
            addMethod(clas.methods, inheritedMethodCount, buildMethod(m, clas))

        _builtinClassTypeMap[classData["name"]] = clas
        _builtinClassIdMap[clas.id] = clas

    def addMethod(methods, inheritedCount, method):
        for i, m in enumerate(methods[:inheritedCount]):
            if method.name.short() == m.name.short() and method.mayOverride(m):
                method.override = m
                methods[i] = method
                return
        methods.append(method)

    def defineFunction(functionData):
        function = buildFunction(functionData)
        _builtinFunctions.append(function)
        _builtinFunctionNameMap[function.name] = function

    with utils.openCommonFile("builtins.yaml") as builtinsFile:
        classes, functions = yaml.load_all(builtinsFile.read())
    for ty in classes:
        declareClass(ty)
    for ty in classes:
        defineClass(ty)
    for fn in functions:
        defineFunction(fn)
Exemplo n.º 5
0
import utils

InstInfo = namedtuple("InstInfo", [
    "name",
    "opcode",
    "operandCount",
    "pushCount",
    "popCount",
    "isTerminator",
])

_c = utils.Counter()
instInfoByName = {}
instInfoByCode = []

with utils.openCommonFile("opcodes.yaml") as _opcodesFile:
    for _opc in yaml.load(_opcodesFile.read()):
        _info = InstInfo(_opc["name"], _c(), _opc["iops"],
                         _opc["push"], _opc["pop"], _opc["term"])
        instInfoByName[_opc["name"]] = _info
        instInfoByCode.append(_info)

# Instructions and types may have one of the widths below. This number can be added to a base
# instruction like "addi8" to get the appropriate variant.
W8 = 0
W16 = 1
W32 = 2
W64 = 3

WORD = W64
WORDSIZE = 8
Exemplo n.º 6
0
import utils

InstInfo = namedtuple("InstInfo", [
    "name",
    "opcode",
    "operandCount",
    "pushCount",
    "popCount",
    "isTerminator",
])

_c = utils.Counter()
instInfoByName = {}
instInfoByCode = []

with utils.openCommonFile("opcodes.yaml") as _opcodesFile:
    for _opc in yaml.load(_opcodesFile.read()):
        _info = InstInfo(_opc["name"], _c(), _opc["iops"], _opc["push"],
                         _opc["pop"], _opc["term"])
        instInfoByName[_opc["name"]] = _info
        instInfoByCode.append(_info)

# Instructions and types may have one of the widths below. This number can be added to a base
# instruction like "addi8" to get the appropriate variant.
W8 = 0
W16 = 1
W32 = 2
W64 = 3

WORD = W64
WORDSIZE = 8