/* hitomi1.c - Don Yang (uguu.org) Optimize. Well, this is only _one_ preprocessing step, but actually took me many hours to do it (didn't occur to me to split this step to separate files until too late). It was mainly taking hitomi0.c and replace as many if/else with ?: as possible, working from the leaf of the tree up. The effect of preprocessing the leaves is that many branches can be merged to one single expression, etc, etc, until most if/else disappeared. By the time I am done, it sure looks very different from hitomi0.c! 10/21/01 */ #include #include static char *bytecode = ";O`WQIuxyp`QMGoJDQlp@>kpplZ>hZxWorS;KTfZoEuRUT[byFH[ciGl^QUGkN=DQB]R?G_DG?w]xpdf>YoEciiw@f]u;[um;JxRU;RXTibgoWJGCsRPpl?v>QXtKkJWjSHW>kyH[ubiSq>Ozo`>Oz;w]p@u>OzKgi?cXrXrXReCuXrypkBoygzM>yep_<]=Oau[_um;jxRUtImjoMmOB[ORJrw=mBGWn^fFA[ff^ZljeOfp`>OepZCQU;LMFA;EV`WAsxFH;?gJXxg_;`Sh<wy`QI?w^Qecv[xfbf[z;CqqUl;KXT;CsBFHkypN@Xr>eFNQH[pDOzKkylE?wNRecv;UXkBCqM@pXoBORAsxFHJk>ypD;SZuuoJ<;ORAsdzM>CENXT]Bho?]R?GcfiW;s@zuKgoGjv>Sefb>;jsxff>lMnwiR?G[diiSzuaJwc`axfFQQXXqfWjMxNNRJxLafon>lUNdbBd]wmLExwScbrU;WXl=WXXwNq>Uy;aumX_ZdTy?;NLefrNZu;", *p, *String[] = { "Decoded", "Encoded", "Empty file" }; enum { exec_while = 575, exec_if, exec_frame, end_bytecode }; enum { state_exec, state_endbegin, state_exit, state_getlabel, state_jmplabel, state_ignlabel }; int ip, *sp, i, j, Memory[10][512], Register[14], ExecState, ExecArg, pBytecode[7000], Constant[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, -2, -3, 256 }; /****************************************************************** GetBit */ static int GetBit() { return pBytecode[ip++]; } /* GetBit() */ /******************************************************************** main */ int main(int argc, char **argv) { FILE *infile, *outfile; if( argc > 2 && (infile = fopen(argv[1], "rb")) && (outfile = fopen(argv[2], "wb+")) ) { sp = pBytecode; for(p = bytecode; *p; p++) { if( *p > 58 ) for(i = 0; i < 6; *(sp++) = ((*p - 59) & (1 << i++)) ? 1 : 0); } sp = Memory[9]; for(ip = ExecState = 0; ExecState - state_exit;) { if( GetBit() ) { /* 1 */ if( GetBit() ) { /* 11 */ if( GetBit() ) { /* 111. */ if( ExecState ) { ip++; } else { if( GetBit() ) { i = Register[*sp]; *sp = i; } else { sp--; } } } else { /* 110 */ if( GetBit() ) { /* 1101.. */ if( ExecState ) { ip += 2; } else { if( GetBit() ) { if( GetBit() ) { i = Register[*sp]++; *sp = i; } else { sp[-1] -= *sp; sp--; } } else { sp--; *sp = GetBit() ? *sp + sp[1] : *sp < sp[1] ? 1 : 0; } } } else { /* 1100 */ if( GetBit() ) { /* 11001 */ if( GetBit() ) { /* 110011 -> sget */ if( !ExecState ) { for(i = -1; sp[i] - exec_frame; i--); i = sp[*sp + i]; *sp = i; } } else { /* 110010 -> mput */ if( !ExecState ) { sp -= 2; Memory[sp[1]][sp[2]] = *sp; } } } else { /* 11000 -> mget */ if( !ExecState ) { i = Memory[sp[-1]][*sp]; *(--sp) = i; } } } } } else { /* 10 */ if( GetBit() ) { /* 101 */ if( GetBit() ) { /* 1011 */ if( GetBit() ) { /* 10111 -> end */ if( ExecState ) { if( ExecState == state_endbegin ) { if( !--ExecArg ) { ExecState = state_exec; sp -= (*sp - exec_while) ? 1 : 2; } } } else { sp--; ip = (sp[1] - exec_while) ? ip : *(sp++); } } else { /* 10110 -> begin */ ExecArg += ExecState ? ExecState - state_endbegin ? 0 : 1 : *(sp--) ? 0 : (ExecState = 1) - ExecArg; } } else { /* 1010 -> rput */ if( !ExecState ) { sp--; Register[sp[1]] = *sp; } } } else { /* 100 */ if( GetBit() ) { /* 1001..... */ j = GetBit() ? ( ExecState ? ip++ : (i = GetBit() ? exec_if : (*(++sp) = ip) ? exec_while : 0) | 1 ? *(++sp) = i : 0 ) : ( GetBit() ? ( GetBit() ? ( ip = GetBit() ? GetBit() ? ExecState ? ip : (sp -= 2)[1] : 0 : (ExecState = ExecState ? ExecState : state_exit) ? ip : ip ) : ( ExecState = ExecState ? ExecState - state_jmplabel ? ExecState : state_ignlabel : state_getlabel ) ) : ( GetBit() ? GetBit() ? ExecState ? 0 : fseek(infile, *(sp--), 0) : ExecState ? 0 : (*(++sp) = fgetc(infile)) : ExecState ? 0 : fputc(*(sp--) < 0 ? -sp[1] : sp[1], outfile) ) ); } else { /* 1000 */ if( GetBit() ) { /* 10001 */ if( GetBit() ) { /* 100011 -> otherwise */ ExecState = ExecState ? (ExecState - state_endbegin || ExecArg - 1) ? ExecState : state_exec : (ExecArg = 1); } else { /* 100010.. */ j = pBytecode[ip] + pBytecode[ip] + pBytecode[ip + 1]; ip += 2; i = ExecState - state_getlabel ? ( ExecState = ExecState - state_jmplabel ? ExecState - state_ignlabel ? ExecState : state_jmplabel : ExecArg - j ? ExecState : state_exec ) : ( (*(++sp) = ip) && (*(++sp) = exec_frame) && (ExecState = state_jmplabel) && (ExecArg = j) ); } } else { /* 10000.. */ i = ExecState ? (ip += 2) : sp-- ? ( *sp = GetBit() ? GetBit() ? (*sp & sp[1]) : (*sp >> sp[1]) : GetBit() ? (*sp << sp[1]) : (*sp | sp[1]) ) : 0; } } } } } else { /* 0 -> data */ for(i = j = 0; i < 4; j |= GetBit() << i++); if( !ExecState ) *(++sp) = Constant[j]; } } puts(String[*sp]); fclose(infile); fclose(outfile); } else { puts("Error"); } return 0; } /* main() */