lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


David Given wrote:
[...]
> For reference: my 'application' is a shell script that compiles an
> embedded Lua interpreter and runs a Lua program with it. Works very
> nicely. I'm currently crunching the C side of things with cobfusc, and
> LuaSrcDiet crunches the Lua side of things as well as is effective
> (there's no point trying to get that any smaller since the C side is so
> large; I'm now looking for a better C cruncher).

I just thought you'd be interested to know that having now *written* a
better C cruncher, I've managed to squash the entire interpreter source
code into a single 154kB source file, which is 40% of the original 389kB
source.

Admittedly, it's 154kB of this gibberish:

n;o0*h;i4a(L,F2(ra));h=i1(ra);bc=v7(i);if(V_(i)==B6)n=(bc&(P4-1))+1;else{n=L->X-ra-1;L->X=L->ci->X;}bc&=~(P4-1);for(;n>0;n--)p1a(i8(L,h,bc+n),ra+n);break;}case
I3a:{S4(L,ra);break;}case
K9:{E_*p;z2*ncl;int
nup,j;p=cl->p->p[v7(i)];nup=p->e5;ncl=D8(L,nup,&cl->g);ncl->l.p=p;for(j=0;j<nup;j++,pc++){if(V_(*pc)==J5)ncl->l.P1a[j]=cl->P1a[a2(*pc)];else{H(V_(*pc)==S8);ncl->l.P1a[j]=H2a(L,k_+a2(*pc));}}x0a(ra,ncl);z1(L);break;}}}}

Yes, that is actually *valid* C source.

I've done this by renaming all the non-C-specific symbols in the source.
(Did you know that there's over 6.5kB of 'lua_State' strings *alone*?) I
think I might be able to get it a bit smaller by also renaming C
keywords and Posix reserved symbols, but doing that would involve having
to #define them back to their real values so that the compiler can
understand them, and that may not be worth it.

I partially avoided the how-do-I-generate-unused-short-symbol-names
problem by making symbols using the [letter][number][letter or number]
template, as you can see above; there are very few symbols that clash
with those (the only ones I've found are the Bessel functions in the GNU
libm). However, I'm still not entirely sure that the resulting source is
truly portable and need to do some tests. It does at least pass my unit
tests on Linux.

The crunching process involves a manual stage where I have to go through
a big list of all the tokens used by the program and remove all the ones
that the cruncher's not allowed to touch (like 'break' or 'stat'), and
it's currently a rather messy collection of small tools in Lua, but if
anyone wants something like this, look at the 'build' script here:

http://primemover.svn.sourceforge.net/viewvc/primemover/pm/

I can now fit the entire Prime Mover executable, which is 5000 lines of
Lua, a patched Lua interpreter source and lposix, into under 64kB of
bzipped data. That's not bad...

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "Wizards get cranky, / Dark days dawn, / Riders smell manky, / The
│ road goes on. / Omens are lowering, / Elves go West; / The Shire needs
│ scouring, / You may as well quest." - John M. Ford

Attachment: signature.asc
Description: OpenPGP digital signature