74 lines
1.7 KiB
Python
74 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""带对齐计算 Generator 布局中 ClassGenEnum 的偏移,对比 this_adjust 464"""
|
|
import re
|
|
|
|
src = open(r'd:\Users\TermiNexus\Desktop\TransPyC\TransPyV\output\031de2bf1006ef88.ll',
|
|
encoding='utf-8', errors='replace').read()
|
|
|
|
def parse_fields(body):
|
|
depth = 0
|
|
fields = []
|
|
cur = ''
|
|
for ch in body:
|
|
if ch == '{':
|
|
depth += 1
|
|
elif ch == '}':
|
|
depth -= 1
|
|
if ch == ',' and depth == 0:
|
|
fields.append(cur.strip())
|
|
cur = ''
|
|
else:
|
|
cur += ch
|
|
if cur.strip():
|
|
fields.append(cur.strip())
|
|
return fields
|
|
|
|
|
|
def align_of(t):
|
|
t = t.strip()
|
|
if t.endswith('*') or t == 'i64':
|
|
return 8
|
|
if t == 'i32':
|
|
return 4
|
|
if t == 'i16':
|
|
return 2
|
|
if t in ('i8', 'i1'):
|
|
return 1
|
|
return 8
|
|
|
|
|
|
def size_of(t):
|
|
t = t.strip()
|
|
if t.endswith('*') or t == 'i64':
|
|
return 8
|
|
if t == 'i32':
|
|
return 4
|
|
if t == 'i16':
|
|
return 2
|
|
if t in ('i8', 'i1'):
|
|
return 1
|
|
return 8
|
|
|
|
|
|
m = re.search(r'%\"273007d50d5852ac.Generator\" = type \{ (.*?) \}', src)
|
|
gfields = parse_fields(m.group(1))
|
|
|
|
m2 = re.search(r'%\"fdad6914c4a7d3ce.ClassGenEnum\" = type \{ (.*?) \}', src)
|
|
cfields = parse_fields(m2.group(1))
|
|
print('ClassGenEnum 字段数:', len(cfields))
|
|
|
|
# 计算每个字段在 Generator 中的对齐偏移
|
|
offs = []
|
|
cur = 0
|
|
for f in gfields:
|
|
a = align_of(f)
|
|
if cur % a != 0:
|
|
cur += a - (cur % a)
|
|
offs.append(cur)
|
|
cur += size_of(f)
|
|
|
|
# 找 ClassGenEnum 完整子序列匹配
|
|
for i in range(len(gfields) - len(cfields) + 1):
|
|
if gfields[i:i+len(cfields)] == cfields:
|
|
print(f'ClassGenEnum 匹配 @字段{i} 对齐偏移{offs[i]}')
|