Example #1
0
 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
Example #2
0
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)
Example #3
0
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
Example #4
0
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,
    ])
Example #5
0
 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
Example #6
0
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,
        ])