示例#1
0
文件: codegen.py 项目: fxxing/play
    def gen_main(self):
        type = ir.FunctionType(int32, [])
        func = ir.Function(self.module, type, name='playMain')
        entry = func.append_basic_block('entry')
        builder = ir.IRBuilder(entry)

        bootstrap_class = SymbolTable().get_class(Option().bootstrap_class)
        method = bootstrap_class.lookup_method('main', [], is_static=True)
        builder.call(self.get_method(method), [])
        builder.ret(ir.Constant(int32, 0))
示例#2
0
def resolve_symbol(symbol: Union[int, str], table: symbol.SymbolTable) -> int:
    """convert symbol to corresponding address,
    and add to symbol table if needed"""
    if isinstance(symbol, str):
        if table.contains(symbol):
            return table.get_address(symbol)
        else:
            return table.add_entry(symbol)
    else:
        return symbol
示例#3
0
文件: cgen.py 项目: fxxing/play
    def run(self):
        Report().begin("RuntimeGen")

        table = SymbolTable()
        self.classes = sorted(table.get_classes(),
                              key=lambda c: c.qualified_name)
        self.packages = sorted(table.get_packages(),
                               key=lambda pkg: pkg.qualified_name)
        self.gen_global()

        Report().end()
示例#4
0
文件: cgen.py 项目: fxxing/play
    def run(self):
        Report().begin("NativeGen")

        for cls in SymbolTable().get_classes():
            self.gen_class(cls)

        Report().end()
示例#5
0
文件: translate.py 项目: fxxing/play
    def run(self):
        Report().begin("Translate")
        for cls in SymbolTable().get_classes():
            Report().report('translate {}'.format(cls),
                            lambda: self.translate(cls))
        Report().end()

        Option().nodes = {}
示例#6
0
    def run(self):
        Report().begin("ControlFlowAnalyse")

        for cls in SymbolTable().get_classes():
            Report().report('analyse {}'.format(cls))
            for method in cls.methods + cls.static_methods:
                self.analyse(method)

        Report().end()
示例#7
0
 def enter_class(self, package: Package, src: SourceFile,
                 ctx: PlayParser.ClassDeclarationContext):
     cls = Class(ctx.IDENTIFIER().getText(), package, src)
     cls.is_interface = ctx.INTERFACE() is not None
     cls.is_native = ctx.NATIVE() is not None
     if cls.is_native and cls.is_interface:
         raise CompileException('interface {} cannot be native'.format(cls))
     Option().nodes[cls] = ctx
     package.put(cls)
     SymbolTable().enter_class(cls)
示例#8
0
 def parse(self, path, rel_path):
     package = ''
     if '/' in rel_path:
         package = rel_path[:rel_path.rfind('/')].replace('/', '.')
     package = SymbolTable().enter_package(package)
     src = SourceFile(path)
     parser = PlayParser(
         CommonTokenStream(PlayLexer(FileStream(path, encoding='utf-8'))))
     Option().nodes[src] = parser.compilationUnit()
     package.source_files.append(src)
示例#9
0
 def run(self):
     Report().begin("Enter member")
     for cls in SymbolTable().get_classes():
         ctx: PlayParser.ClassDeclarationContext = Option().nodes[cls]
         for member in ctx.memberDeclaration():
             if member.methodDeclaration():
                 self.enter_method(cls, member.methodDeclaration())
             else:
                 self.enter_field(cls, member.fieldDeclaration())
         if not list(filter(lambda m: m.name == '<init>', cls.methods)):
             cls.methods.append(
                 Method('<init>', cls, parameters=[], body=Block([])))
     Report().end()
示例#10
0
class HackAsmParser:
    def __init__(self):
        self.Tab = SymbolTable()
        self.var_list = []

    def parse(self, line, line_num):
        if line[0] == '(':
            label = line[1:-1]
            self.Tab.loc_label(label, line_num)
            label_type = 'location label'
            if label in self.var_list:
                self.var_list.remove(label)
                label_type = 'var -> location label'
        elif line[0] == '@':
            label = line[1:]
            if not label.isdigit():
                if label not in self.Tab.constTab:
                    if label not in self.var_list:
                        self.var_list.append(label)
                label_type = 'var'
            else:
                label_type = 'const'
        else:
            label = ''
            label_type = 'null'

        return label + '  ' + label_type

    def alloc_all_var(self):
        print self.var_list
        for name in self.var_list:
            self.Tab.alloc(name)
        return self.get_tab()

    def get_tab(self):
        return self.Tab
示例#11
0
文件: codegen.py 项目: fxxing/play
    def run(self):
        obj_ptr = gen_class_struct(
            PLAY_PACKAGE.children['Object']).as_pointer()
        str_ptr = gen_class_struct(
            PLAY_PACKAGE.children['String']).as_pointer()

        self.create_function(self.module, 'new', obj_ptr, [int32])
        self.create_function(self.module, 'newString', str_ptr,
                             [int8.as_pointer()])
        self.create_function(self.module, 'cast', obj_ptr.as_pointer(),
                             [obj_ptr, int32])
        self.create_function(self.module, 'loadMethod', int32.as_pointer(),
                             [obj_ptr, int32])

        Report().begin("Codegen")
        for cls in SymbolTable().get_classes():
            self.gen_class(cls)

        Report().end()

        self.gen_main()

        with open(Option().code_gen_file, 'w') as f:
            f.write(str(self.module))
示例#12
0
 def resolve(self, src: SourceFile):
     ctx: PlayParser.CompilationUnitContext = Option().nodes[src]
     for i in ctx.importDeclaration():
         cls = SymbolTable().get_class(i.qualifiedName().getText())
         src.imports[cls.name] = cls
示例#13
0
 def run(self):
     Report().begin("Check circular dependency")
     for cls in SymbolTable().get_classes():
         if cls not in self.checked:
             self.check(cls, [])
     Report().end()
示例#14
0
 def __init__(self):
     self.Tab = SymbolTable()
     self.var_list = []
示例#15
0
 def __init__(self):
     self.table = SymbolTable()
示例#16
0
 def run(self):
     Report().begin("Build hierarchy")
     for cls in SymbolTable().get_classes():
         self.build(cls)
     Report().end()
示例#17
0
 def run(self):
     Report().begin("Resolve import")
     for cls in SymbolTable().get_classes():
         self.resolve(cls.source)
     Report().end()