def watcher(new_value): if not branches: branches[value] = bs.program.render_code[label.position:label.position + patch_len] if new_value in branches: data = branches[value] else: prgm = get_redirection(lambda rdi: get_jmp(compiler.translate("branch_on_watched", flow, stack=call_stack, this=func(new_value)))) data = prgm.render_code prgm.render_code = OffsetListProxy(bs.program.render_code, label.position) branches[value] = data bs.program.render_code[label.position:label.position + patch_len] = data
def main(): global sketchname sketchname = os.path.split(args.file)[1].split('.py')[0] sketchfile = open(args.file) translated = translate(sketchfile.read()) write_translation(translated['code'], sketchname) if args.compile: run(sketchname) elif args.upload: run(sketchname, upload=True)
def compile(source, isFile=False): text = InputStream(source) if isFile: text = FileStream(source) lexer = DecafeLexer(text) stream = CommonTokenStream(lexer) parser = MyParser(stream) tree = parser.program() errors = parser.errMsg # print(Trees.toStringTree(tree, None, parser)) tsymbol = MyVistor() # make symbol table tsymbol.visit(tree) # tsymbol.symTable.Print() # print table symTable = tsymbol.symTable.ToString() # get json of tables # make tree treeView, _ = convertor.convertInit(tree)(tree, 0) treeView.render('tree.gv', "./web/static/img") # intermediate code inCode = IntermediateCodeGenerator(tsymbol.symTable) inCode.visit(tree) iCode = [] for l in inCode.lines: if l.type != "label": iCode.append("\t" + str(l)) else: iCode.append(str(l)) iCode = "\n".join(iCode) # compile to NASM symT = tsymbol.symTable with open('code.asm', 'w') as f: for line in translate(symT, inCode.lines): f.write("%s\n" % line) print(iCode) errors.extend(tsymbol.errorMsg) errors = list(set(errors)) return symTable, errors, iCode
def make_root(): return compiler.translate("make_root", compiler.Flow(), this=[ lambda bs: bs.code.add(isa.push(registers.rbp)), lambda bs: bs.code.add(isa.mov(registers.rbp, registers.rsp)), lambda bs: bs.flow.try_stack.append(uncaught_exception), #main_module.load(), #ast.Call( # func=lambda bs: None, # args=[], # keywords=[], # starargs=None, # kwargs=None, # ), tree.body, lambda bs: bs.code.add(isa.mov(registers.rsp, registers.rbp)), lambda bs: bs.code.add(isa.pop(registers.rbp)), lambda bs: bs.code.add(isa.ret()), compiler.end, ])
def watcher(new_value): if not branches: branches[value] = bs.program.render_code[label. position:label.position + patch_len] if new_value in branches: data = branches[value] else: prgm = get_redirection(lambda rdi: get_jmp( compiler.translate("branch_on_watched", flow, stack=call_stack, this=func(new_value)))) data = prgm.render_code prgm.render_code = OffsetListProxy(bs.program.render_code, label.position) branches[value] = data bs.program.render_code[label.position:label.position + patch_len] = data
def make_root(): return compiler.translate( "make_root", compiler.Flow(), this=[ lambda bs: bs.code.add(isa.push(registers.rbp)), lambda bs: bs.code.add(isa.mov(registers.rbp, registers.rsp)), lambda bs: bs.flow.try_stack.append(uncaught_exception), #main_module.load(), #ast.Call( # func=lambda bs: None, # args=[], # keywords=[], # starargs=None, # kwargs=None, # ), tree.body, lambda bs: bs.code.add(isa.mov(registers.rsp, registers.rbp)), lambda bs: bs.code.add(isa.pop(registers.rbp)), lambda bs: bs.code.add(isa.ret()), compiler.end, ])