Self-modifying code (and space/time complexity)

Tom Stockfisch tps at chem.ucsd.edu
Sat Jul 16 12:42:37 AEST 1988


In article <33652 at yale-celray.yale.UUCP> lisper-bjorn at CS.YALE.EDU (Bjorn Lisper) writes:
>In article <1744 at vaxb.calgary.UUCP> radford at calgary.UUCP (Radford Neal) writes:
>>There are interesting cases where on-the-fly generation of code seems
>>to be essential to get good asymptotic space and/or time complexity.
>>
>>     for (i = 0; i<L; i++)
>>     { if (c1) p1();
>>       if (c2) p2();
>>       ...
>>       if (cN) pN();
>>     }
>>[SMC solution follows]
>>     start generating code;
>>     if (c1) generate instruction to call p1;
>>     if (c2) generate instruction to call p2;
>>     ...
>>     if (cN) generate instruction to call pN;
>>     for (i = 0; i<L; i++) execute generated instructions;

Wouldn't the following be "nearly" as efficient, and be portable to boot?
At least it has the same asymptotic complexity.

	enum { END, P1, P2, ..., Pn }	code[MAXCODE], *p;

	p = code;
	if (c1)	*p++ =	P1;
	if (c2)	*p++ =	P2;
	...
	if (cn)	*p++ =	Pn;
	*p =	END;

	for ( pixel = 0; pixel++ < NPIXELS )
		for ( p = code; *p != END; )
			switch (*p++)
			{
			case	P1:
				[p1 instructions]
				break;
			case	P2:
				[p2 instructions]
				break;
			...
			case	Pn:
				[pn instructions]
				break;
			default:
				assert(0);
				/*NOTREACHED*/
			}
-- 

|| Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu



More information about the Comp.lang.c mailing list