def do_it(o, code, var_map): code = '\n'.join(P.cutoff_comments(code)) code = P.replace_symbolic_names_wr(code, var_map) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, P.guess_subroutine_name(sys.argv[1])) P.write_asm_inside(o, code + '\nretq')
def do_it(o): me = os.path.basename(sys.argv[0]) comment = P.g_autogenerated_patt % me o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, me[4:-3]) # form reg_map, deal with save/restore reg_map = dict(x.split(' ') for x in g_reg_map.split(',')) reg_map['vv'] = 'xmm15' k = 15 code = g_code.strip() for r in g_saved_in_xmm.split(' '): n = r[1] k -= 1 reg_map['s' + n] = 'xmm%s' % k # TODO: test and benchmark mpn_add2_4arg code = code.replace('!restore ' + r, 'movq s@, w@'.replace('@', n)) code = code.replace('!save ' + r, 'movq w@, s@'.replace('@', n)) # fix offsets code = P.replace_positive_offsets(code) code = P.replace_negative_offsets(code) # save/restore to red zone code = P.replace_amp_save(code) code = P.replace_amp_restore(code) # apply reg_map for k, v in reg_map.items(): code = re.sub(r'\b%s\b' % k, '%' + v, code) # write file P.write_asm_inside(o, code + '\nretq')
def cook_asm(o, cc, name): cc = P.g_std_start + cc + P.g_std_end cc = '\n'.join(cc) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, name) P.write_asm_inside(o, cc + '\nretq')
def cook_asm(o, code): code = '\n'.join(code) # replace ymm with xmm in all movq and vpextrq code = '\n'.join() if g_flag & 1: code = code.replace('vmovdqu', 'vmovdqa') comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, 'mul8x2_zen') P.write_asm_inside(o, code + '\nretq')
def do_4arg(o): comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, g_subroutine) code = P.cutoff_comments(g_code) code = '\n'.join(code) symb = dict() for v_k in g_vars_map.split(' '): v, k = v_k.split(',') symb[k] = '%' + v code = P.replace_symbolic_vars_name(code, symb) P.write_asm_inside(o, code)
def cook_asm(o, code, xmm_save, var_map): if g_writing_macro: cook_macro(o, code, xmm_save, var_map) return P.insert_restore(code, xmm_save) code = '\n'.join(code) for k, v in xmm_save.items(): code = code.replace('!restore ' + k, 'movq %s, %s' % (v, k)) code = P.replace_symbolic_names_wr(code, var_map) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, P.guess_subroutine_name(sys.argv[1])) P.write_asm_inside(o, code + '\nretq')
def do_it(o, i_name): with open(i_name, 'rb') as i: code = '\n'.join(chew_code(i)) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, g_hooligan) P.write_asm_inside(o, code) code = P.cutoff_comments(g_code) xmm_save = P.save_registers_in_xmm(code, 5) P.save_in_xmm(code, xmm_save) code = '\n'.join(code).replace('@call', 'call ' + g_hooligan) for k, v in xmm_save.items(): code = code.replace('!restore ' + k, 'movq %s, %s' % (v, k)) code = P.replace_symbolic_names_wr(code, g_var_map) o.write('\n') P.write_asm_procedure_header(o, P.guess_subroutine_name(sys.argv[2])) P.write_asm_inside(o, code + '\nretq')
def cook_asm(name, o, code): code = P.g_std_start + code + P.g_std_end code = '\n'.join(code) m = 'rp,rdi up,rsi w7,rcx wB,rbp wA,rbx w9,r12 w8,r13 w6,r14 w5,r15 ' m += 'w0,rax w1,r8 w2,r9 w3,r10 w4,r11 dd,rdx t0,xmm14 ' m += 't1,xmm13 t2,xmm12 t3,xmm11 t4,xmm10' r = {} for x in m.split(' '): y = x.split(',') r[y[0]] = '%' + y[1] code = P.replace_symbolic_vars_name(code, r) # replace ymm with xmm in all movq code = '\n'.join([replace_ymm_by_xmm(x) for x in code.split('\n')]) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, P.guess_subroutine_name(name)) P.write_asm_inside(o, code + '\nretq')
def do_16_s(o): comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, g_subroutine + '%s' % g_n) code = g_code_16.strip() while 1: m = re.search(g_spread_carry_pattern, code) if not m: break code = code.replace(m.group(0), spread_carry_code(g_spread_carry, m.group(1))) register_map = {'VZ': 'xmm15', 'V0': 'ymm14', '128_V0': 'xmm14', 'V1': 'ymm13', '128_V1': 'xmm13', 'rp': 'rdi', 'gp': 'rsi', 'w6': 'rdx'} for i in range(7, 0xB + 1): j = 1 + i register_map['S%X' % i] = 'ymm%s' % j register_map['s%X' % i] = 'xmm%s' % j scratch_regs = 'rax rcx r8 r9 r10 r11'.split(' ') for i in range(len(scratch_regs)): register_map['w%s' % i] = scratch_regs[i] protected_regs = 'rbx rbp r12 r13 r14 r15'.split(' ') for i in range(len(protected_regs)): register_map['w%X' % (7 + i)] = protected_regs[i] register_map = dict([(k,'%' + v) for k,v in register_map.items()]) # fix offsets code = P.replace_positive_offsets(code) # replace '@save wY' by 'store below sp' instructon, '@restore' by ... code = P.replace_amp_save(code) code = P.replace_amp_restore(code) while 1: m = re.search(g_xmm_restore_pattern, code) if not m: break i = m.group(1) # !save wY means put wY into an xmm register code = code.replace('!restore w' + i, 'movq s@, w@'.replace('@', i)) code = code.replace('!save w' + i, 'movq w@, s@'.replace('@', i)) for k,v in register_map.items(): code = re.sub(r'\b%s\b' % k, v, code) P.write_asm_inside(o, code)
def cook_asm(name, o, code): xmm_save = P.save_registers_in_xmm(code, 9) P.insert_restore(code, xmm_save) code = '\n'.join(code) for k, v in xmm_save.items(): code = code.replace('!restore ' + k, 'movq %s, %s' % (v, k)) m = 'rp,rdi up,rsi w7,rcx wB,rbp wA,rbx w9,r12 w8,r13 w6,r14 w5,r15 ' m += 'w0,rax w1,r8 w2,r9 w3,r10 w4,r11 dd,rdx v14,ymm15 v47,ymm14 t0,xmm13' r = {} for x in m.split(' '): y = x.split(',') r[y[0]] = '%' + y[1] code = P.replace_symbolic_vars_name(code, r) # replace ymm with xmm in all movq code = '\n'.join([replace_ymm_by_xmm(x) for x in code.split('\n')]) comment = P.g_autogenerated_patt % os.path.basename(sys.argv[0]) o.write(comment.replace('//', '#')) P.write_asm_procedure_header(o, P.guess_subroutine_name(name)) P.write_asm_inside(o, code + '\nretq')