Clean only required registers

This commit is contained in:
Devine Lu Linvega 2024-04-24 15:46:27 -07:00
parent 5ab8e10a6b
commit bd6a194e24
1 changed files with 11 additions and 8 deletions

View File

@ -2,7 +2,7 @@
typedef struct { typedef struct {
unsigned int id, refs, ptr; unsigned int id, refs, ptr;
char *a, *b, key[0x10]; char *a, *b, reg[0x10];
} Rule; } Rule;
static int flip, quiet, cycles = 0x10000; static int flip, quiet, cycles = 0x10000;
@ -88,7 +88,7 @@ write_rule(Rule *r, char *s, int create)
{ {
while((*dst_++ = *s++)) while((*dst_++ = *s++))
; ;
*dst_++ = 0; *dst_ = 0;
if((flip = !flip)) if((flip = !flip))
src_ = bank_b, dst_ = bank_a; src_ = bank_b, dst_ = bank_a;
else else
@ -105,7 +105,10 @@ write_rule(Rule *r, char *s, int create)
static int static int
apply_rule(Rule *r, char *s) apply_rule(Rule *r, char *s)
{ {
char c, *a = r->a, *regs[0x08] = {NULL}, *reg; unsigned int i;
char c, *a = r->a, *regs[0x08], *reg;
/* phase: clean registers */
for(i = 0; i < r->ptr; i++) regs[i] = NULL;
/* phase: match rule */ /* phase: match rule */
while((c = *a)) { while((c = *a)) {
if(c == '?') { if(c == '?') {
@ -133,7 +136,7 @@ apply_rule(Rule *r, char *s)
unsigned int id = *b - '0'; unsigned int id = *b - '0';
if(id < 9 && (reg = regs[id])) { if(id < 9 && (reg = regs[id])) {
b++; b++;
if(!write_reg(r->key[id], reg) && dst_ != origin) dst_--; if(!write_reg(r->reg[id], reg) && dst_ != origin) dst_--;
} else } else
*dst_++ = c; *dst_++ = c;
} else } else
@ -153,7 +156,7 @@ find_register(Rule *r, char reg)
{ {
int i; int i;
for(i = 0; i < (int)r->ptr; i++) for(i = 0; i < (int)r->ptr; i++)
if(r->key[i] == reg) return i; if(r->reg[i] == reg) return i;
return -1; return -1;
} }
@ -163,7 +166,7 @@ compile_rule(Rule *r, int id, char *src)
char c, *cap; char c, *cap;
int wrapped, reg; int wrapped, reg;
r->id = id, r->ptr = 0, r->a = &empty, r->b = &empty; r->id = id, r->ptr = 0, r->a = &empty, r->b = &empty;
/* phase: left */ /* phase: compile left */
while((c = *src) && c == ' ') src++; while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src; if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->a = dict_, cap = walk(src), wrapped = c == '('; r->a = dict_, cap = walk(src), wrapped = c == '(';
@ -173,12 +176,12 @@ compile_rule(Rule *r, int id, char *src)
if(c == '?') { if(c == '?') {
reg = find_register(r, *src); reg = find_register(r, *src);
if(reg == -1 && *src != '(') if(reg == -1 && *src != '(')
r->key[r->ptr] = *src, reg = r->ptr++; r->reg[r->ptr] = *src, reg = r->ptr++;
*dict_++ = '0' + reg; *dict_++ = '0' + reg;
} }
} }
src += wrapped, *dict_++ = 0; src += wrapped, *dict_++ = 0;
/* phase: right */ /* phase: compile right */
while((c = *src) && c == ' ') src++; while((c = *src) && c == ' ') src++;
if(c == ')' || (c == '<' && src[1] == '>')) return src; if(c == ')' || (c == '<' && src[1] == '>')) return src;
r->b = dict_, cap = walk(src), wrapped = c == '('; r->b = dict_, cap = walk(src), wrapped = c == '(';