2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import string, re, os, sys, shutil
|
|
|
|
|
2009-03-04 17:30:39 -05:00
|
|
|
debug = False
|
|
|
|
dot_labels = False
|
|
|
|
is_linux = False
|
2009-05-06 14:20:52 -04:00
|
|
|
|
|
|
|
# translate 64 bit registers to 8 bit form
|
|
|
|
|
|
|
|
r08 = { 'rax' : 'al', 'rbx' : 'bl', 'rcx' : 'cl', 'rdx' : 'dl',
|
|
|
|
'rsi' : 'sil', 'rdi' : 'dil', 'rbp' : 'bpl',
|
|
|
|
'r8' : 'r8b', 'r9' : 'r9b', 'r10' : 'r10b', 'r11' : 'r11b',
|
|
|
|
'r12' : 'r12b', 'r13' : 'r13b', 'r14' : 'r14b', 'r15' : 'r15b' }
|
|
|
|
|
|
|
|
# translate 64 bit registers to 32 bit form
|
|
|
|
|
|
|
|
r32 = { 'rax' : 'eax', 'rbx' : 'ebx', 'rcx' : 'ecx', 'rdx' : 'edx',
|
|
|
|
'rsi' : 'esi', 'rdi' : 'edi', 'rbp' : 'ebp',
|
|
|
|
'r8' : 'r8d', 'r9' : 'r9d', 'r10' : 'r10d', 'r11' : 'r11d',
|
|
|
|
'r12' : 'r12d', 'r13' : 'r13d', 'r14' : 'r14d', 'r15' : 'r15d' }
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# regular expression for registers
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
r_q = r'(?:r[abcd]x|r[sd]i|r[bsi]p)|(?:r8|r9|r1[0-5])' # 64 bit
|
|
|
|
r_d = r'(?:e[abcd]x|e[sd]i|e[bsi]p|r[89]d|r1[0-5]d)|' # 32 bit
|
|
|
|
r_w = r'(?:[abcd]x|[sd]i|[bsi]p|r[89]w|r1[0-5]w)|' # 16 bit
|
|
|
|
r_b = r'(?:[abcd]l|[ds]il|[bsi]pl|r[89]b|r1[0-5]b)|' # 8 bit
|
2009-08-30 16:56:54 -04:00
|
|
|
r_x = r'(?:x?mm\d|x?mm1[0-5])|(?:mmx\d|mmx1[0-5])|(?:st\([0-7]\))'
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
p_rg = r'(?:\s*%(' + r_b + r_w + r_d + r_q + '|' + r_x + r'))'
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
# regular expression for dis(r1, r2, mul) forms
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
p_di = r'(?:\s*([-+]?[0-9]*)(?=\())?' # numeric displacement
|
|
|
|
p_mu = r'(?:\s*,\s*([1248]))?\s*(?=\))' # multiplier (1, 2, 4, 8)
|
|
|
|
p_t1 = p_di + r'\s*\(' + p_rg + r'(?:\s*\)|\s*,' + p_rg + p_mu + r'\s*\))'
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
# regular expression for immediate (numeric, not symbolic)
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
p_im = r'\s+\`?\$\'?([\+\-]?[0-9]+|0x[a-zA-Z0-9]+)'
|
|
|
|
|
|
|
|
# regular expressions for labels
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
if True :
|
|
|
|
p_lr = r'\s*([a-zA-Z$_][a-zA-Z0-9\$_]*|\.[0-9]+)'
|
|
|
|
else :
|
|
|
|
p_lr = r'\s*L\(([a-zA-Z0-9\$_]+)\)'
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
p_la = p_lr + r'(:)'
|
|
|
|
p_ri = p_lr + r'\(\%rip\)'
|
|
|
|
p_jt = r'\s+\.long\s+' + p_lr + r'\-' + p_lr
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
# regular expression for instructions
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
p_in = r'\s*([a-zA-Z][a-zA-Z0-9]*)'
|
|
|
|
|
|
|
|
p_def = r'define\s*\(\s*\`([a-zA-Z_][a-zA-Z0-9_]*)\'\s*\,\s*\`' + p_rg + '\'\s*\)'
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m_f1 = re.compile(p_in + p_rg + r'\s*,' + p_t1)
|
|
|
|
m_f2 = re.compile(p_in + p_t1 + r'\s*,' + p_rg)
|
2009-01-05 05:26:59 -05:00
|
|
|
m_f3 = re.compile(p_in + p_t1)
|
2009-05-06 14:20:52 -04:00
|
|
|
m_f4 = re.compile(p_in + p_im + r'\s*,' + p_t1)
|
|
|
|
m_f5 = re.compile(p_in + p_im + r'\s*,' + p_rg)
|
|
|
|
m_f6 = re.compile(p_in + p_rg + r'\s*,' + p_rg)
|
2009-01-05 05:26:59 -05:00
|
|
|
m_f7 = re.compile(p_in + p_rg)
|
2009-05-06 14:20:52 -04:00
|
|
|
m_g7 = re.compile(p_in + r'\s+\*' + p_rg)
|
2009-01-05 05:26:59 -05:00
|
|
|
m_f8 = re.compile(p_in + p_im)
|
|
|
|
m_f9 = re.compile(p_in + p_lr)
|
2009-05-06 14:20:52 -04:00
|
|
|
m_fa = re.compile(p_in + '(?:' + p_im + '|' + p_rg + r')\s*,' + p_rg + r'\s*,' + p_rg)
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
m_la = re.compile(p_la)
|
2009-05-06 14:20:52 -04:00
|
|
|
m_jt = re.compile(p_jt)
|
|
|
|
m_ri = re.compile(p_in + p_ri)
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
r_mac = re.compile(r"^\s*(?:define|DEFINE)\s*\(\s*`"
|
|
|
|
"([A-Z$_][A-Z0-9$_]*)'\s*,\s*`\s*$")
|
|
|
|
r_mnd = re.compile(r"^\s*'\s*\)\s*$")
|
2009-05-06 14:20:52 -04:00
|
|
|
r_dlr = re.compile(r'\$([0-9]+)')
|
|
|
|
r_def = re.compile(p_def)
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
r1 = r'\s*(\%{0,1}[0-9]+){0,1}\s*'
|
|
|
|
r2 = r'(?:,\s*(\%{0,1}[0-9]+))?\s*'
|
2009-05-19 15:53:15 -04:00
|
|
|
r_mrz = re.compile(r'([A-Z$_][A-Z0-9$_]*)\s*$')
|
2009-05-06 14:20:52 -04:00
|
|
|
r_mrf = re.compile(r'([A-Z$_][A-Z0-9$_]*)\s*\(' + r1 + r2 +
|
|
|
|
r2 + r2 + r2 + r2 + r2 + r2 + r2 + r2 + r'\)')
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
def pass_one(code) :
|
|
|
|
labels = []
|
|
|
|
for l in code :
|
|
|
|
m = m_la.search(l)
|
|
|
|
if m :
|
|
|
|
labels += [m.group(1)]
|
|
|
|
return labels
|
|
|
|
|
|
|
|
def pass_two(code, labels) :
|
|
|
|
|
|
|
|
mac_name = ''
|
|
|
|
mac_dict = {}
|
|
|
|
lab_idx = 0
|
|
|
|
for ln in range(len(code)) :
|
|
|
|
l = code[ln]
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
m = r_mac.search(l)
|
|
|
|
if m and not mac_name :
|
|
|
|
mac_name = m.group(1)
|
|
|
|
mac_dict[mac_name] = [ln, 0, 0, {}]
|
|
|
|
continue
|
|
|
|
|
|
|
|
m = r_mnd.search(l)
|
|
|
|
if m and mac_name:
|
|
|
|
mac_dict[mac_name][1] = ln
|
|
|
|
mac_name = ''
|
|
|
|
lab_idx = 0
|
|
|
|
continue
|
|
|
|
|
|
|
|
if mac_name :
|
|
|
|
m = r_dlr.findall(l)
|
|
|
|
if m :
|
|
|
|
mn = max(int(i) for i in m)
|
|
|
|
if mn > mac_dict[mac_name][2] :
|
|
|
|
mac_dict[mac_name][2] = mn
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_la.search(l)
|
|
|
|
if m and m.group(1) in labels :
|
|
|
|
lab_idx += 1
|
|
|
|
mac_dict[mac_name][3][m.group(1)] = lab_idx
|
|
|
|
|
|
|
|
return mac_dict
|
|
|
|
|
|
|
|
def proc_m(m, i, j) :
|
|
|
|
v = list(m.groups())
|
|
|
|
n = len(v)
|
|
|
|
if n <= i :
|
|
|
|
return ([], '')
|
|
|
|
ss = (v[i] if not v[i] or v[i][0] in '+-' else '+' + v[i]) + ']'
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
if n > i + 3 and i + 3 < j and v[i + 3] :
|
|
|
|
ss = ('' if not v[i + 3] else '*' + v[i + 3]) + ss
|
|
|
|
if n > i + 2 and i + 2 < j and v[i + 2] :
|
|
|
|
ss = '+' + v[i + 2] + ss
|
|
|
|
if n > i + 1 and i + 1 < j and v[i + 1] :
|
|
|
|
ss = v[i + 1] + ss
|
|
|
|
return (v[ 0 : i ] + v[ j : ], '[' + ss)
|
|
|
|
|
2009-04-01 10:58:15 -04:00
|
|
|
def pass_three(code, labels, macros, level) :
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
lo = []
|
|
|
|
mac_name = ''
|
|
|
|
for l in code :
|
|
|
|
lp = '\n'
|
|
|
|
# labels
|
|
|
|
m = m_la.search(l)
|
|
|
|
if m :
|
|
|
|
l = l[ m.end(2) : -1 ]
|
|
|
|
if mac_name :
|
|
|
|
if m.group(1) in macros[mac_name][3] :
|
|
|
|
ii = macros[mac_name][3][m.group(1)]
|
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
print('internal error')
|
2009-08-30 16:56:54 -04:00
|
|
|
else :
|
2009-01-05 05:26:59 -05:00
|
|
|
ii = labels.index(m.group(1))
|
|
|
|
lab = re.sub('\$', '%', m.group(1))
|
|
|
|
if not l :
|
|
|
|
if mac_name :
|
2009-08-30 16:56:54 -04:00
|
|
|
lo += ['\n%%{0}:'.format(ii)]
|
2009-01-05 05:26:59 -05:00
|
|
|
elif dot_labels :
|
|
|
|
lo += ['\n.{0}:'.format(ii)]
|
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += ['\nL_{0}:'.format(lab)]
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
else :
|
|
|
|
if mac_name :
|
|
|
|
lp = '\n%%{0}:'.format(ii)
|
|
|
|
elif dot_labels :
|
|
|
|
lp = '\n.{0}:'.format(ii)
|
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
lp = '\nL_{0}:'.format(lab)
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'(\s*)#\s*(.*)', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
lo += [lp + '{0[0]}; {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'dnl(.*)', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m :
|
|
|
|
lo += [lp + ';{0}'.format(m.group(1))]
|
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
# three operand instructions
|
2009-03-03 11:08:29 -05:00
|
|
|
m = m_fa.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
2009-05-06 14:20:52 -04:00
|
|
|
if v[1] == None or v[2] == None :
|
|
|
|
if v[1] == None :
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[4]}, {0[3]}, {0[2]}'.format(v)]
|
|
|
|
else :
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[4]}, {0[3]}, {0[1]}'.format(v)]
|
|
|
|
continue
|
2009-03-03 11:08:29 -05:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
# ins reg, dis(reg, reg, off)
|
|
|
|
m = m_f1.search(l)
|
|
|
|
if m :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
v, s = proc_m(m, 2, 6)
|
|
|
|
lo += [lp + '\t{0[0]:7s} {1}, {0[1]}'.format(v, s)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins dis(reg, reg, off), reg
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f2.search(l)
|
|
|
|
if m :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
v, s = proc_m(m, 1, 5)
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[1]}, {1}'.format(v, s)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins dis(reg, reg, off)
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f3.search(l)
|
|
|
|
if m :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
v, s = proc_m(m, 1, 5)
|
|
|
|
lo += [lp + '\t{0[0]:7s} {1}'.format(v, s)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins imm, dis(reg, reg, off)
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f4.search(l)
|
|
|
|
if m :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
v, s = proc_m(m, 2, 6)
|
|
|
|
lo += [lp + '\t{0[0]:7s} {1}, {0[1]}'.format(v, s)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins imm, reg
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f5.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[2]}, {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins reg, reg
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f6.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[2]}, {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins reg
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f7.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
# ins *reg
|
|
|
|
m = m_g7.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins imm
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f8.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
lo += [lp + '\t{0[0]:7s} {0[1]}'.format(v)]
|
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
# jump table
|
|
|
|
m = m_jt.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if v[1] in labels :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
if mac_name and v[0] in macros[mac_name][3] :
|
|
|
|
ii = macros[mac_name][3][v[0]]
|
|
|
|
st1 = '%%{0}'.format(ii)
|
|
|
|
elif dot_labels :
|
|
|
|
ii = labels.index(v[0])
|
|
|
|
st1 = '.{0}'.format(ii)
|
|
|
|
else :
|
|
|
|
lab = re.sub('\$', '%', v[0])
|
|
|
|
st1 = 'L_{0}'.format(lab)
|
|
|
|
if mac_name and v[1] in macros[mac_name][3] :
|
|
|
|
ii = macros[mac_name][3][v[1]]
|
|
|
|
st2 = '%%{1}'.format(ii)
|
|
|
|
elif dot_labels :
|
|
|
|
ii = labels.index(v[1])
|
|
|
|
st2 = '.{1}'.format(ii)
|
|
|
|
else :
|
|
|
|
lab = re.sub('\$', '%', v[1])
|
|
|
|
st2 = 'L_{0}'.format(lab)
|
|
|
|
lo += [lp + '\tdd ' + st1 + ' - ' + st2]
|
|
|
|
continue
|
|
|
|
|
|
|
|
# RIP relative jump
|
|
|
|
m = m_ri.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if v[1] in labels :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
if mac_name and v[1] in macros[mac_name][3] :
|
|
|
|
ii = macros[mac_name][3][v[1]]
|
|
|
|
lo += [lp + '\t{0[0]:7s} %%{1}[rip]'.format(v, ii)]
|
|
|
|
continue
|
|
|
|
if dot_labels :
|
|
|
|
ii = labels.index(v[1])
|
|
|
|
lo += [lp + '\t{0[0]:7s} .{1}[rip]'.format(v, ii)]
|
|
|
|
else :
|
|
|
|
lab = re.sub('\$', '%', v[1])
|
|
|
|
lo += [lp + '\t{0[0]:7s} L_{1}[rip]'.format(v, lab)]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# jump label
|
2009-01-05 05:26:59 -05:00
|
|
|
m = m_f9.search(l)
|
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if v[1] in labels :
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
if mac_name and v[1] in macros[mac_name][3] :
|
|
|
|
ii = macros[mac_name][3][v[1]]
|
|
|
|
lo += [lp + '\t{0[0]:7s} %%{1}'.format(v, ii)]
|
|
|
|
continue
|
|
|
|
if dot_labels :
|
|
|
|
ii = labels.index(v[1])
|
|
|
|
lo += [lp + '\t{0[0]:7s} .{1}'.format(v, ii)]
|
|
|
|
else :
|
|
|
|
lab = re.sub('\$', '%', v[1])
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '\t{0[0]:7s} L_{1}'.format(v, lab)]
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'\s*\.byte\s+((?:0x|0X)[0-9a-fA-F]+|[0-9]+)\s*', l)
|
2009-03-03 11:08:29 -05:00
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
lo += [lp + '\tdb {0[0]}'.format(v)]
|
|
|
|
continue
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
# macro definitions
|
|
|
|
m = r_mac.search(l)
|
|
|
|
if m :
|
|
|
|
mac_name = m.group(1)
|
|
|
|
if mac_name in macros :
|
|
|
|
lo += [lp + '%macro ' + mac_name.lower() + ' '
|
|
|
|
+ str(macros[mac_name][2])]
|
|
|
|
continue
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
m = r_mnd.search(l)
|
|
|
|
if m and mac_name :
|
|
|
|
mac_name = ''
|
|
|
|
lab_ofs = 0
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '%endmacro']
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
m = r_dlr.findall(l)
|
|
|
|
if m :
|
2009-05-06 14:20:52 -04:00
|
|
|
l = re.sub(r'\$([0-9]+)', r'%\1', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search('PROLOGUE\(([a-zA-Z$_][a-zA-Z0-9$_]*)\)', l)
|
2009-01-05 18:23:23 -05:00
|
|
|
if m :
|
2009-03-04 17:30:39 -05:00
|
|
|
if is_linux :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '\tGLOBAL_FUNC {0}'.format(m.group(1))]
|
2009-03-04 17:30:39 -05:00
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '\tWIN64_GCC_PROC {0}'.format(m.group(1))]
|
2009-01-05 18:23:23 -05:00
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search('EPILOGUE\(\)', l)
|
2009-01-05 18:23:23 -05:00
|
|
|
if m :
|
2009-03-04 17:30:39 -05:00
|
|
|
if is_linux :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '\tend']
|
2009-03-04 17:30:39 -05:00
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + '\tWIN64_GCC_END']
|
2009-01-05 18:23:23 -05:00
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# macro calls
|
2009-05-19 15:53:15 -04:00
|
|
|
m = r_mrz.search(l)
|
|
|
|
if not m :
|
|
|
|
m = r_mrf.search(l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m :
|
|
|
|
if m.group(1).lower() == 'align' :
|
2009-04-01 10:58:15 -04:00
|
|
|
fs = '\talign {0}' if is_linux else '\txalign {0}'
|
|
|
|
lo += [lp + fs.format(m.group(2))]
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
elif m.group(1) in macros :
|
|
|
|
lp += '\t{0:7s}'.format(m.group(1).lower())
|
|
|
|
i = 2
|
|
|
|
while i <= m.lastindex and m.group(i) :
|
|
|
|
lp += ' {0}'.format(m.group(i))
|
|
|
|
i += 1
|
|
|
|
lo += [lp]
|
|
|
|
continue
|
2009-08-30 16:56:54 -04:00
|
|
|
elif m.group(1) and (m.lastindex == 1 or not m.group(2)) :
|
2009-01-05 05:26:59 -05:00
|
|
|
lo += [lp + '\t{0}'.format(m.group(1))]
|
|
|
|
continue
|
2009-08-30 16:56:54 -04:00
|
|
|
|
2009-01-05 05:26:59 -05:00
|
|
|
if mac_name :
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'\s*([^%]+)%([0-9]+)\s*', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m and m.lastindex == 2 and int(m.group(2)) <= macros[mac_name][2] :
|
|
|
|
lo += [lp + '\t{0}%{1}'.format(m.group(1).lower(),m.group(2))]
|
|
|
|
continue
|
|
|
|
|
2009-08-30 16:56:54 -04:00
|
|
|
# ins
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(p_in + r'\s+(.*)', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m :
|
|
|
|
v = list(m.groups())
|
|
|
|
if debug :
|
|
|
|
print(l, end = '')
|
|
|
|
if len(v) == 1 or len(v) == 2 and v[1] == '' :
|
2009-03-04 17:30:39 -05:00
|
|
|
if v[0] in macros :
|
|
|
|
lo += [lp + '\t{0}'.format(v[0].lower())]
|
|
|
|
else :
|
|
|
|
lo += [lp + '\t{0[0]}'.format(v)]
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
elif v[0] == 'C' :
|
|
|
|
lo += [lp + ';\t{0}'.format(v[1])]
|
|
|
|
continue
|
2009-03-04 17:30:39 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'include\(.+config.m4.+\)', l)
|
2009-03-04 17:30:39 -05:00
|
|
|
if m :
|
|
|
|
if is_linux :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + "%include 'yasm_mac.inc'"]
|
2009-03-04 17:30:39 -05:00
|
|
|
else :
|
2009-05-06 14:20:52 -04:00
|
|
|
lo += [lp + "%include '"
|
|
|
|
+ ''.join(['..\\'] * level) + "yasm_mac.inc'"]
|
2009-03-04 17:30:39 -05:00
|
|
|
continue
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
m = re.search(r'\s*(\S+)', l)
|
2009-01-05 05:26:59 -05:00
|
|
|
if m :
|
2009-05-19 15:53:15 -04:00
|
|
|
if len(l) :
|
|
|
|
t = None if l[-1].isprintable() else None
|
|
|
|
lo += [lp + '{0} ; < not translated >'.format(l[:t])]
|
|
|
|
else :
|
|
|
|
lo += [lp]
|
2009-01-05 05:26:59 -05:00
|
|
|
else :
|
2009-05-19 15:53:15 -04:00
|
|
|
lo += [lp]
|
2009-05-14 16:14:04 -04:00
|
|
|
return lo + ['\n']
|
2009-01-05 05:26:59 -05:00
|
|
|
|
|
|
|
def form_path(p) :
|
2009-02-22 16:03:08 -05:00
|
|
|
n = str.rfind(p, '\\', 0, -1)
|
2009-01-05 05:26:59 -05:00
|
|
|
if n != -1 :
|
|
|
|
if not os.path.exists(p[ : n + 1]) :
|
|
|
|
form_path(p[ : n + 1])
|
|
|
|
if p[-1] == '\\' and not os.path.exists(p) :
|
|
|
|
os.mkdir(p)
|
2009-05-05 16:43:45 -04:00
|
|
|
|
|
|
|
def conv_lines(code, l) :
|
|
|
|
labels = pass_one(code)
|
|
|
|
macros = pass_two(code, labels)
|
|
|
|
code = pass_three(code, labels, macros, l)
|
|
|
|
return (labels, macros, code)
|
|
|
|
|
|
|
|
def conv_file(f_in, f_out, l) :
|
2009-05-06 14:20:52 -04:00
|
|
|
f = open(f_in, 'r')
|
2009-05-05 16:43:45 -04:00
|
|
|
code = f.readlines()
|
|
|
|
f.close()
|
|
|
|
labels, macros, code = conv_lines(code, l)
|
2009-05-06 14:20:52 -04:00
|
|
|
f = open(f_out, 'w')
|
2009-05-05 16:43:45 -04:00
|
|
|
f.writelines(code)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def conv_dirs(s, d, l) :
|
2009-01-05 05:26:59 -05:00
|
|
|
fd = os.listdir(s)
|
|
|
|
for f in fd :
|
|
|
|
sp = os.path.join(s,f)
|
|
|
|
dp = os.path.join(d,f)
|
|
|
|
if os.path.isdir(sp) :
|
2009-05-05 16:43:45 -04:00
|
|
|
conv_dirs(sp, dp, l + 1)
|
2009-01-05 05:26:59 -05:00
|
|
|
elif os.path.isfile(sp) :
|
2009-01-08 09:51:11 -05:00
|
|
|
if sp != dp and os.path.exists(dp) :
|
2009-01-05 05:26:59 -05:00
|
|
|
continue
|
|
|
|
fn = os.path.basename(f)
|
|
|
|
x = os.path.splitext(fn)
|
|
|
|
if x[1] == '.asm' :
|
|
|
|
form_path(dp)
|
2009-01-08 09:51:11 -05:00
|
|
|
print("translating '{0}'".format(sp))
|
2009-05-06 14:20:52 -04:00
|
|
|
f = open(sp, 'r')
|
2009-01-05 05:26:59 -05:00
|
|
|
code = f.readlines()
|
|
|
|
f.close()
|
2009-01-08 09:51:11 -05:00
|
|
|
if sp == dp :
|
2009-04-01 10:58:15 -04:00
|
|
|
rp = os.path.join(s, x[0] + ('.as' if is_linux else '.old'))
|
2009-01-08 09:51:11 -05:00
|
|
|
os.rename(sp, rp)
|
2009-05-05 16:43:45 -04:00
|
|
|
labels, macros, code = conv_lines(code, l)
|
2009-05-06 14:20:52 -04:00
|
|
|
f = open(dp, 'w')
|
2009-01-05 05:26:59 -05:00
|
|
|
f.writelines(code)
|
|
|
|
f.close()
|
|
|
|
elif False :
|
|
|
|
form_path(dp)
|
|
|
|
shutil.copyfile(sp, dp)
|
|
|
|
|
2009-05-05 16:43:45 -04:00
|
|
|
if len(sys.argv) == 1 :
|
|
|
|
cd = os.getcwd() # if run in the build.vc9 directory
|
2009-05-06 14:20:52 -04:00
|
|
|
if cd.endswith('build.vc9') :
|
|
|
|
cd1 = cd + '\\..\\mpn\\x86_64' # the GAS assembler directory
|
|
|
|
cd2 = cd + '\\..\\mpn\\x86_64w' # the YASM (Windows) assembler directory
|
|
|
|
elif cd.endswith('x86_64') : # if run in the GAS assembler directory
|
|
|
|
if os.path.exists(cd + '\\..\\x86_64w') :
|
2009-05-05 16:43:45 -04:00
|
|
|
cd1 = cd
|
2009-05-06 14:20:52 -04:00
|
|
|
cd2 = cd + '\\..\\x86_64w'
|
2009-05-05 16:43:45 -04:00
|
|
|
else :
|
|
|
|
cd1 = cd2 = cd
|
|
|
|
elif len(sys.argv) == 3 :
|
|
|
|
cd1 = sys.argv[1]
|
|
|
|
cd2 = sys.argv[2]
|
|
|
|
else :
|
2009-01-08 09:51:11 -05:00
|
|
|
cd1 = cd2 = None
|
|
|
|
|
|
|
|
if cd1 and os.path.exists(cd1) :
|
2009-05-05 16:43:45 -04:00
|
|
|
if os.path.isdir(cd1) :
|
|
|
|
if os.path.exists(cd2) and os.path.isdir(cd2) :
|
|
|
|
print("warning: output directory '{0}' already exists".format(cd2))
|
2009-07-28 11:00:00 -04:00
|
|
|
if cd1 != cd2 :
|
|
|
|
conv_dirs(cd1, cd2, 0) # convert format from GAS to YASM
|
2009-05-05 16:43:45 -04:00
|
|
|
elif os.path.isfile(cd1) :
|
|
|
|
if not os.path.exists(cd2) :
|
|
|
|
conv_file(cd1, cd2, 0)
|
2009-01-05 05:26:59 -05:00
|
|
|
else :
|
2009-05-05 16:43:45 -04:00
|
|
|
print('{0} is not a file or directory'.format(cd1))
|