def emit(op): global depth # print depth, op if op == "+": x86.add_gv_ev(eax, reg(ebx)) pop_gv(ebx) elif op == "-": x86.sub_gv_ev(eax, reg(ebx)) pop_gv(eax) x86.mov_gv_ev(ebx, reg(eax)) elif op == "*": x86.imul_ev(reg(ebx)) pop_gv(ebx) elif op == "/": x86.idiv_ev(reg(ebx)) x86.mov_gv_iv(edx, 0) pop_gv(eax) x86.mov_gv_ev(ebx, reg(eax)) elif op == "%": x86.mov_gv_ev(eax, reg(edx)) x86.idiv_ev(reg(ebx)) x86.mov_gv_iv(edx, 0) pop_gv(eax) x86.mov_gv_ev(ebx, reg(eax)) else: gen_push(op)
def compile(str): global depth, stack_address x86.ret() epilog() tokens = str.split() tokens.reverse() for token in tokens: try: op = int(token) depth = depth - 1 except ValueError: op = token depth = depth + 1 emit(op) x86.mov_gv_iv(esi, stack_address) prolog()
def gen_push(i): global depth x86.mov_gv_iv(eax, i) if 0 < depth: push_gv(eax)
def emit_add42(): x86.ret() epilog() x86.add_gv_ev(eax, atv(ebp, 8)) x86.mov_gv_iv(eax, 42) prolog()