Compare commits
1 Commits
main
...
agent/w092
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68daee7eaf |
@@ -53,7 +53,7 @@ Progress
|
||||
<!-- progress:start -->
|
||||
| Decompiled | Matched | Linked | Data |
|
||||
|:---:|:---:|:---:|:---:|
|
||||
| 99.08% | 86.61% | 60.34% | 90.99% |
|
||||
| 99.09% | 86.61% | 60.34% | 90.99% |
|
||||
|
||||
units 902/1027 complete, functions 12151/12563 matched.
|
||||
|
||||
|
||||
@@ -468,7 +468,7 @@ extern pf_s32 PFPATH_cmpName(const pf_s8* short_name, PFDIR_STR* pattern, pf_u32
|
||||
extern pf_s32 PFPATH_cmpNameUni(const pf_u16* name, PFDIR_STR* pattern);
|
||||
pf_s32 PFDIR_p_mkdir(PF_VOLUME* volume, PF_STR* path, pf_u32 option, PF_DTA* data) {
|
||||
PF_DIR_ENT parent_entry;
|
||||
PF_DIR_ENT entry;
|
||||
pf_u32 index;
|
||||
pf_s8 normalized_name[512];
|
||||
PF_ENT_ITER iter;
|
||||
PFDIR_FFD ffd;
|
||||
@@ -488,8 +488,8 @@ pf_s32 PFDIR_p_mkdir(PF_VOLUME* volume, PF_STR* path, pf_u32 option, PF_DTA* dat
|
||||
pf_u16 filename_length;
|
||||
pf_u16 original_offset;
|
||||
pf_u8 checksum;
|
||||
PF_DIR_ENT entry;
|
||||
pf_u32 cluster;
|
||||
pf_u32 index;
|
||||
pf_u32 lfn_index;
|
||||
pf_u32* next_sector;
|
||||
pf_s32 error;
|
||||
|
||||
@@ -268,10 +268,10 @@ void KPADSetPosParam(s32 chan, f32 x, f32 y) {
|
||||
}
|
||||
|
||||
static void reset_kpad(KPADInside* kpad) {
|
||||
f32 sensorDistance;
|
||||
f32 upperY;
|
||||
f32 distanceValue;
|
||||
f32 zero;
|
||||
f32 upperY;
|
||||
f32 sensorDistance;
|
||||
f32 lowerY;
|
||||
f32 one;
|
||||
f32 negativeOne;
|
||||
|
||||
@@ -562,9 +562,9 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) {
|
||||
s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 offset, u32 flags,
|
||||
WADProcessCallback processCallback) {
|
||||
s32 result = 0;
|
||||
ESTitleMeta* titleMeta;
|
||||
void* firstBuffer = 0;
|
||||
void* secondBuffer = 0;
|
||||
ESHash* sharedContentHashes = 0;
|
||||
ESContentId* installedContentIds = 0;
|
||||
ESContentMeta* matchingContents = 0;
|
||||
u32 importedBytes = 0;
|
||||
u32 contentCount;
|
||||
u32 headerReadSize;
|
||||
@@ -574,11 +574,11 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o
|
||||
WADThreadStack* threadStack = 0;
|
||||
s32 contentFd;
|
||||
s32 titleImportStarted = FALSE;
|
||||
ESTitleMeta* installedTitleMeta = 0;
|
||||
ESContentId* installedContentIds = 0;
|
||||
ESTitleMeta* titleMeta;
|
||||
void* firstBuffer = 0;
|
||||
s32 contentImportStarted = FALSE;
|
||||
ESContentMeta* matchingContents = 0;
|
||||
ESHash* sharedContentHashes = 0;
|
||||
void* secondBuffer = 0;
|
||||
ESTitleMeta* installedTitleMeta = 0;
|
||||
WADStream stream;
|
||||
WADUnpackInfo unpackInfo;
|
||||
WADSaveDataHeader wadHeader ALIGN32;
|
||||
@@ -1177,8 +1177,8 @@ cleanup:
|
||||
|
||||
static s32 WAD_815C1288(WADExportLoopArgs* args) {
|
||||
ESFd fd = args->fd;
|
||||
u32 remaining = args->size;
|
||||
u32 size;
|
||||
u32 remaining = args->size;
|
||||
u32 bufferIndex = 0;
|
||||
WADImportTransfer* transfer = args->transfer;
|
||||
s32 result = 0;
|
||||
|
||||
@@ -871,17 +871,17 @@ int ATERM_8140276C(AtermApRecordSet* currentRecords, AtermApRecordSet* previousR
|
||||
|
||||
int ATERM_81402A24(void) {
|
||||
s32 result = -1;
|
||||
s32 progressInfo[4];
|
||||
AtermApRecordSet* currentRecords = NULL;
|
||||
AtermApRecordSet* previousRecords = NULL;
|
||||
u32 scanBufferBytes;
|
||||
u32 recordIndex;
|
||||
s32 iteration = 0;
|
||||
u8* rawScanBuffer = NULL;
|
||||
u8* scanBuffer;
|
||||
u16* firstDescriptor;
|
||||
u32 recordBytes = gAtermScanLimit * sizeof(AtermApRecord) + sizeof(AtermApRecord) + sizeof(u32);
|
||||
u32 scanBufferBytes;
|
||||
s32 progressInfo[4];
|
||||
u32 scanCount;
|
||||
u32 recordIndex;
|
||||
s32 iteration = 0;
|
||||
AtermApRecordSet* currentRecords = NULL;
|
||||
AtermApRecordSet* previousRecords = NULL;
|
||||
u32 now;
|
||||
char selectedMacText[32];
|
||||
|
||||
|
||||
@@ -930,14 +930,14 @@ void CArGBAOdh::cdj_c_makeHeader(SArCDJ_OdhMaster* master, u32 size) {
|
||||
}
|
||||
|
||||
s32 CArGBAOdh::cdj_c_colorConv(SArCDJ_OdhMaster* master, u8* sourceData, int format) {
|
||||
u16* dimensions;
|
||||
u32 paddedWidth;
|
||||
u16 width;
|
||||
u16 height;
|
||||
u8* workPlane;
|
||||
int sourceStride;
|
||||
u32 paddedWidth;
|
||||
u8* cbPlane;
|
||||
u8* crPlane;
|
||||
u8* cbPlane;
|
||||
u16* dimensions;
|
||||
int rowIndex;
|
||||
int i;
|
||||
u16 dimension;
|
||||
@@ -1358,18 +1358,17 @@ s32 CArGBAOdh::cdj_d_initializeDecompressOdh(SArCDJ_OdhMaster* master, u8* workB
|
||||
}
|
||||
|
||||
s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int workSize) {
|
||||
u32 sourceLimit = srcSize;
|
||||
int blockStride;
|
||||
int workBufferSize = workSize;
|
||||
|
||||
u8 xSampling;
|
||||
u8 coefficientIndex;
|
||||
u16 blockWidth;
|
||||
int blockRowStride;
|
||||
int rowStride;
|
||||
int blockStride;
|
||||
int planeOffset;
|
||||
u32 sourceLimit = srcSize;
|
||||
int statusOrOffset;
|
||||
u16 blockX;
|
||||
int planeOffset;
|
||||
int rowStride;
|
||||
int blockRowSize;
|
||||
int naturalIndex;
|
||||
int requiredBlockEnd;
|
||||
@@ -1392,7 +1391,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blockY << 6) * (u32)master->blocksWide +
|
||||
statusOrOffset = (u32)master->blocksWide * ((u32)master->blockY << 6) +
|
||||
(u32)master->blockX * 8;
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
@@ -1410,7 +1409,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blockY << 6) * (u32)master->blocksWide +
|
||||
(((u32)master->blocksWide << 6) * (u32)master->blocksHigh +
|
||||
((u32)master->blocksHigh * ((u32)master->blocksWide << 6) +
|
||||
(u32)master->blockX * 8);
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
@@ -1429,9 +1428,9 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksWide << 6) * (u32)master->blocksHigh * 2 +
|
||||
statusOrOffset = (u32)master->blocksHigh * ((u32)master->blocksWide << 6) * 2 +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1450,7 +1449,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
planeOffset = (u32)master->blockX * 0x10 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide * 2;
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6) * 2;
|
||||
requiredBlockEnd = planeOffset + blockRowStride + 8;
|
||||
if (requiredBlockEnd > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
@@ -1504,7 +1503,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksHigh * ((u32)master->blocksWide << 7) * 2) +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1523,7 +1522,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
planeOffset = (u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide * 2;
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6) * 2;
|
||||
if (planeOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1556,9 +1555,9 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksWide << 6) * (u32)master->blocksHigh * 2 +
|
||||
statusOrOffset = (u32)master->blocksHigh * ((u32)master->blocksWide << 6) * 2 +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1575,9 +1574,9 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksWide << 6) * (u32)master->blocksHigh * 4 +
|
||||
statusOrOffset = (u32)master->blocksHigh * ((u32)master->blocksWide << 6) * 4 +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1596,7 +1595,7 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
planeOffset = (u32)master->blockX * 0x10 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide * 4;
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6) * 4;
|
||||
requiredBlockEnd = planeOffset + blockRowStride + 8;
|
||||
if (requiredBlockEnd > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
@@ -1663,9 +1662,9 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksWide << 6) * (u32)master->blocksHigh * 4 +
|
||||
statusOrOffset = (u32)master->blocksHigh * ((u32)master->blocksWide << 6) * 4 +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -1682,9 +1681,9 @@ s32 CArGBAOdh::cdj_d_decompressLoop(SArCDJ_OdhMaster* master, int srcSize, int w
|
||||
coefficientIndex = odh_natural_order[naturalIndex];
|
||||
master->coefficients[coefficientIndex] = master->dcCoefficients[naturalIndex];
|
||||
}
|
||||
statusOrOffset = ((u32)master->blocksWide << 6) * (u32)master->blocksHigh * 8 +
|
||||
statusOrOffset = (u32)master->blocksHigh * ((u32)master->blocksWide << 6) * 8 +
|
||||
((u32)master->blockX * 8 +
|
||||
((u32)master->blockY << 6) * (u32)master->blocksWide);
|
||||
(u32)master->blocksWide * ((u32)master->blockY << 6));
|
||||
if (statusOrOffset + blockRowSize + 8 > workBufferSize) {
|
||||
statusOrOffset = -0x7ffffffa;
|
||||
goto finishBlock;
|
||||
@@ -2102,18 +2101,18 @@ void CArGBAOdh::LineDeconv22(u8* dest, u8* y, u8* cb, u8* cr, u16 width, u16 hei
|
||||
}
|
||||
|
||||
s32 CArGBAOdh::huffmanDecoder(u32* coefficientOutput, SArCDJ_HuffmanRequest* request, u16** huffmanTables, int component, u32 sourceLimit) {
|
||||
u16 tableEntry;
|
||||
u32 bitBuffer;
|
||||
u32 tableCount;
|
||||
s32 decodeResult;
|
||||
int bitIndex;
|
||||
s32 maximumBitLength;
|
||||
int maxHuffmanBits;
|
||||
u32 bitBuffer;
|
||||
u8* sourceCursor;
|
||||
u16 tableEntry;
|
||||
s32 valueCategory;
|
||||
u32 tableIndex;
|
||||
u32 magnitudeMask;
|
||||
int bytesConsumed;
|
||||
s32 valueCategory;
|
||||
u8* sourceCursor;
|
||||
u32 decodedSymbol;
|
||||
u32 bitCountAndMask;
|
||||
|
||||
|
||||
194
tools/decomp-assist/declsearch.py
Normal file
194
tools/decomp-assist/declsearch.py
Normal file
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Search local-declaration orders that improve a function's register allocation.
|
||||
|
||||
usage: declsearch.py <unit> <symbol> [--version 43U] [--max-evals N] [--lines START END] [--score-only]
|
||||
|
||||
unit path without extension, e.g. src/system/odh
|
||||
symbol name as in the object, e.g. huffmanCoder__9CArGBAOdhFPUsP21SArCDJ_HuffmanRequest
|
||||
|
||||
MWCC colors registers partly in declaration order, so permuting the leading
|
||||
declaration block of a function can turn a register-only diff into a match.
|
||||
The search hill-climbs over swaps and moves of those lines, rebuilding the
|
||||
object each time, and leaves the best order in the file (the original text is
|
||||
restored on error or Ctrl-C). Run from the worktree root. Set NINJA to the
|
||||
ninja binary if it is not on PATH. --lines takes 1-based inclusive line numbers
|
||||
of the declaration block when the automatic detection picks the wrong lines.
|
||||
"""
|
||||
import argparse
|
||||
import difflib
|
||||
import itertools
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from capstone import CS_ARCH_PPC, CS_MODE_32, CS_MODE_BIG_ENDIAN, Cs
|
||||
from elftools.elf.elffile import ELFFile
|
||||
|
||||
BRANCHES = {"b", "bl", "beq", "bne", "bge", "blt", "ble", "bgt", "bdnz", "bdz", "bc"}
|
||||
DECL = re.compile(r"^\s+(?!return\b|if\b|for\b|while\b|do\b|switch\b|goto\b)"
|
||||
r"(const\s+|volatile\s+|unsigned\s+|signed\s+|struct\s+)*[A-Za-z_]\w*"
|
||||
r"(\s*\*+\s*|\s+)\**\s*[A-Za-z_]\w*(\[[^\]]*\])*(\s*=.*)?;\s*$")
|
||||
|
||||
|
||||
def find_symbol(path, name):
|
||||
with open(path, "rb") as f:
|
||||
elf = ELFFile(f)
|
||||
for s in elf.get_section_by_name(".symtab").iter_symbols():
|
||||
if s.name == name:
|
||||
section = elf.get_section(s["st_shndx"])
|
||||
return section.data()[s["st_value"]:s["st_value"] + s["st_size"]]
|
||||
raise KeyError(f"{name} not in {path}")
|
||||
|
||||
|
||||
def disassemble(code):
|
||||
md = Cs(CS_ARCH_PPC, CS_MODE_32 + CS_MODE_BIG_ENDIAN)
|
||||
md.detail = True
|
||||
out = []
|
||||
for pos in range(0, len(code), 4):
|
||||
insns = list(md.disasm(code[pos:pos + 4], pos))
|
||||
if not insns:
|
||||
out.append((".long", code[pos:pos + 4].hex()))
|
||||
continue
|
||||
insn = insns[0]
|
||||
operand = insn.op_str
|
||||
if insn.mnemonic.rstrip("+-") in BRANCHES and insn.operands:
|
||||
last = insn.operands[-1]
|
||||
if last.type == 2: # immediate: make the target function-relative
|
||||
operand = str(last.imm - pos)
|
||||
out.append((insn.mnemonic, operand))
|
||||
return out
|
||||
|
||||
|
||||
def score(mine, target):
|
||||
"""(structural diff ignoring register names, positional exact diff)."""
|
||||
norm = lambda insns: [(m, re.sub(r"\b[rf]\d+\b", "r", o)) for m, o in insns]
|
||||
sm = difflib.SequenceMatcher(None, norm(mine), norm(target), autojunk=False)
|
||||
structural = sum(max(i2 - i1, j2 - j1) for tag, i1, i2, j1, j2 in sm.get_opcodes() if tag != "equal")
|
||||
exact = sum(1 for x, y in zip(mine, target) if x != y) + abs(len(mine) - len(target))
|
||||
return structural, exact
|
||||
|
||||
|
||||
def source_for(unit):
|
||||
for ext in (".c", ".cpp", ".cp"):
|
||||
if os.path.exists(unit + ext):
|
||||
return unit + ext
|
||||
sys.exit(f"no source file for {unit}")
|
||||
|
||||
|
||||
def function_name(symbol):
|
||||
"""Regex for the definition name: Class::name for member functions, else name."""
|
||||
m = re.match(r"(\w+?)__(Q(\d))?(\d.*)", symbol)
|
||||
if not m:
|
||||
return re.escape(symbol)
|
||||
rest, names = m.group(4), []
|
||||
for _ in range(int(m.group(3) or 1)):
|
||||
length = re.match(r"\d+", rest)
|
||||
if not length:
|
||||
break
|
||||
start = length.end()
|
||||
names.append(rest[start:start + int(length.group())])
|
||||
rest = rest[start + int(length.group()):]
|
||||
if not names or not rest.startswith(("F", "C")):
|
||||
return re.escape(m.group(1))
|
||||
return rf"{names[-1]}::{m.group(1)}"
|
||||
|
||||
|
||||
def declaration_block(lines, symbol):
|
||||
pattern = re.compile(rf"^\s*[A-Za-z_].*\b{function_name(symbol)}\s*\(")
|
||||
for start, line in enumerate(lines):
|
||||
if pattern.match(line) and not line.rstrip().endswith(";"):
|
||||
break
|
||||
else:
|
||||
sys.exit(f"definition of {symbol} not found")
|
||||
body = next(i for i in range(start, len(lines)) if lines[i].rstrip().endswith("{")) + 1
|
||||
end = body
|
||||
while end < len(lines) and DECL.match(lines[end]):
|
||||
end += 1
|
||||
if end - body < 2:
|
||||
sys.exit("fewer than two leading declarations; pass --lines START END")
|
||||
return body, end
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
||||
ap.add_argument("unit")
|
||||
ap.add_argument("symbol")
|
||||
ap.add_argument("--version", default="43U")
|
||||
ap.add_argument("--max-evals", type=int, default=300)
|
||||
ap.add_argument("--lines", type=int, nargs=2, metavar=("START", "END"))
|
||||
ap.add_argument("--score-only", action="store_true")
|
||||
args = ap.parse_args()
|
||||
|
||||
ninja = os.environ.get("NINJA", "ninja")
|
||||
built = f"build/{args.version}/src/{args.unit}.o"
|
||||
target = disassemble(find_symbol(f"build/{args.version}/obj/{args.unit}.o", args.symbol))
|
||||
|
||||
def evaluate():
|
||||
if subprocess.run([ninja, built], capture_output=True).returncode:
|
||||
return (10**6, 10**6)
|
||||
return score(disassemble(find_symbol(built, args.symbol)), target)
|
||||
|
||||
if args.score_only:
|
||||
print("structural %d exact %d" % evaluate())
|
||||
return
|
||||
|
||||
path = source_for(args.unit)
|
||||
with open(path) as f:
|
||||
original = f.read()
|
||||
lines = original.split("\n")
|
||||
if args.lines:
|
||||
body, end = args.lines[0] - 1, args.lines[1]
|
||||
else:
|
||||
body, end = declaration_block(lines, args.symbol)
|
||||
decls = lines[body:end]
|
||||
print("declaration block:", *decls, sep="\n ")
|
||||
|
||||
cache = {}
|
||||
|
||||
def order_score(order):
|
||||
if order not in cache:
|
||||
text = lines[:body] + [decls[i] for i in order] + lines[end:]
|
||||
with open(path, "w") as f:
|
||||
f.write("\n".join(text))
|
||||
cache[order] = evaluate()
|
||||
return cache[order]
|
||||
|
||||
start = best = tuple(range(len(decls)))
|
||||
finished = False
|
||||
try:
|
||||
best_score = order_score(best)
|
||||
print("start", best_score, flush=True)
|
||||
improved = True
|
||||
while improved and best_score != (0, 0) and len(cache) < args.max_evals:
|
||||
improved = False
|
||||
candidates = []
|
||||
for i, j in itertools.combinations(range(len(best)), 2):
|
||||
swapped = list(best)
|
||||
swapped[i], swapped[j] = swapped[j], swapped[i]
|
||||
candidates.append(tuple(swapped))
|
||||
for i, j in itertools.permutations(range(len(best)), 2):
|
||||
moved = list(best)
|
||||
moved.insert(j, moved.pop(i))
|
||||
candidates.append(tuple(moved))
|
||||
for cand in candidates:
|
||||
if len(cache) >= args.max_evals:
|
||||
break
|
||||
s = order_score(cand)
|
||||
if s < best_score:
|
||||
best, best_score, improved = cand, s, True
|
||||
print("improved", s, flush=True)
|
||||
break
|
||||
finished = True
|
||||
finally:
|
||||
keep = finished and best != start
|
||||
with open(path, "w") as f:
|
||||
f.write("\n".join(lines[:body] + [decls[i] for i in best] + lines[end:]) if keep else original)
|
||||
subprocess.run([ninja, built], capture_output=True)
|
||||
print(f"best {cache.get(best)} after {len(cache)} builds;",
|
||||
"kept in source:" if keep else "source restored; best order was:")
|
||||
print(*[decls[i] for i in best], sep="\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user