Arcane C hacks?

KW Heuer kwh at bentley.UUCP
Thu Feb 27 02:14:41 AEST 1986


[ bu-cs!bzs (Barry Shein) ]
>> >declare a table something like:   int (*funtable[MAXFNS])() ;
>> >and just malloc the storage for the generated code.  ...  It should be
>> >quite portable (code generator aside) and is legal C.

[ bentley!kwh (Karl Heuer) ]
>> Well, some compilers will dislike the attempt to cast a (char *) into
>> a (int (*)()) ; in fact I think some will call it an outright error

[ houxf!jph (Jack Harkins) ]
>Huh???  This line of code DOES NOT cast a char * into an int....
>I have programs that use pointers to functions, some that run on 3B2/5's.

Sorry, you seem to have lost the context.  The original poster wanted to
malloc space for the CODE ITSELF, not the pointer table; i.e. do something
like
	funtable[0] = (int (*)())malloc(codesize);
and this line _does_ cast a (char *) (which is what malloc() returns)
into a function pointer.  (Actually a more likely sequence is
	char *s = malloc(codesize);
	s[0] = CLRW;  s[1] = R0;
	funtable[0] = (int (*)())s;
	n = (*funtable[0])();
or something like that.)

[ bentley!kwh (Karl Heuer) ]
>> because all programs are pure -- you can't goto/call data space, nor
>> can you read from the instruction stream.  Some sort of chastity belt
>> in the hardware, I think.

[ houxf!jph (Jack Harkins) ]
>Whazat??
>
>MOST(not all) programs are pure in this environment, yes.  But that has
>nothing to do with being able to use a pointer to a function.  The code
>that is executed is actually in the shared text region; it is only the
>pointer to the function that is in the data area.

When I said "all programs are pure", I meant that on the 3b2 it is _not_
_possible_ to write an impure program (as far as I can determine).  The
code fragment above can be made to work on a VAX (even without "ld -N"),
but on the 3b2 it dies with a bus error.  I hope I've cleared this up.

To any would-be flamers: the alignment is appropriate; the bus error
occurs on the CALL instruction; don't flame me about what I'm "clearly"
doing wrong unless you can demonstrate a way to do it right.  On a 3b2.
I've already checked things pretty carefully, including the source code
in the kernel.



More information about the Comp.lang.c mailing list