BartC wrote:
> "Les Cargill" <> wrote in message
> news:k5ajp5$c73$...
>> BartC wrote:
>
>>> It's been a struggle, as the two languages don't match exactly, neither
>>> is C anywhere near the 'portable assembler' that everyone says it is.
>>
>> Dunno - 'C' makes a pretty good target language, but you have to Think
>> Stupid.
>
> I can't; C insists on very strict type-checking, so I have to double guess
> what C expects the type of any expression to be, and compare that with the
> original types, and any coercions, of the source language.
>
> Then you may have to apply extra coercions which work differently on
> l-values, &-terms and *-terms. And there's the quirk where A usually means
> the value of A, *unless* it's an array then it means &A[0] (in the source
> language, arrays have values (as well as having arbitrary lower bounds for
> good measure!)).
>
So that's not *NEARLY* stupid enough

By "Think Stupid", I mean
vastly reduce the number of degrees of freedom for the generated code.
> C is fussy about type-matching pointers (so T* and U* don't match); the
> source language doesn't care.
>
Generally, I store (void *) pointers in a table, then have a column in
the table that suggests a type.
> C also does pointer arithmetic using object-sized offsets, while the source
> language uses byte offsets! Generating dumb ASM code is actually far
> easier.
> (What's much harder is generating efficient, optimised ASM.)
>
Could be, then. I mean no disrespect, but it sounds like you're
doing it the Hard Way, which is ... hard.
> (There are many other issues: for example C is case-sensitive, the source
> language isn't; trying to import a C function such as fopen() into the
> source language, without also importing the FILE* type; words that are
> identifiers in one language but reserved words in the other, etc etc.)
>
>
>>> I can't make use of half the language (most of the control statements
>>> are out for example), and the output is terrible-looking, completely
>>> unstructured C.
>
>> The best way to generate 'C' is to
>> generate tables that hand-coded 'C' then operates on.
>> Tables can include callbacks...
>
> My starting point is intermediate code, a '3-address-code' kind of
> representation. At this point everything is already linearised and
> unstructured, as the usual target is assembly code. You then translate each
> instruction into a simple C statement, typically an assignment of the
> form X
> = Y op Z;
>
> It sounds easy; it isn't!
>
*Shrug?*
struct Thing[] = {
{ (void *)&X, (void *)&Y, "op", (void *)&Z },
....
};
void eval(Thing *t)
{
....
}
....
eval(&Thing[0]);
....
--
Les Cargill