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))
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
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()
def run(self): Report().begin("NativeGen") for cls in SymbolTable().get_classes(): self.gen_class(cls) Report().end()
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 = {}
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()
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)
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)
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()
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
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))
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
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()
def __init__(self): self.Tab = SymbolTable() self.var_list = []
def __init__(self): self.table = SymbolTable()
def run(self): Report().begin("Build hierarchy") for cls in SymbolTable().get_classes(): self.build(cls) Report().end()
def run(self): Report().begin("Resolve import") for cls in SymbolTable().get_classes(): self.resolve(cls.source) Report().end()