[M3devel] a "need" for multi-pass m3cg (or different order of calls)

Hendrik Boom hendrik at topoi.pooq.com
Sat Sep 8 02:36:19 CEST 2012


On Fri, Sep 07, 2012 at 04:10:19PM -0400, Antony Hosking wrote:
> Sorry, I should have read more closely.  You are right.
> Yes, I do believe that when generating C code you will need multiple 
> passes to accumulate stuff to be dumped out together.
> For example, I assume that to handle nested functions that you will 
> construct a local "frame" struct for the function, containing a 
> pointer to it’s outer scope frame struct plus all the local variables 
> having "up_level==true".  There is no other way to do static chains 
> portably in C.  A pointer to this frame struct will serve as the 
> static link to inner-scope functions.

This is why I (once I had machines with gigabytes of RAM) devised the 
following data structure in RAM:

It's a string with insertion points.  Initially, it's empty, with one 
insertion point.  At any insertion point, anytine, you can insert a 
string or an insertion point,  either before or after the insertion point.

Thus at any place in code generation where you might have to accumulate 
declarations while you go on generating code that uses them elsewhere, 
you set up two insertion points -- one where the declarations go, and 
one where the other code goes.

When you're done, you just write it all out in sequence.

It eats storage, but is very simple to use.

In the old days, about four decades ago, I was thinking of bulding such 
a data structure on disk.  Instead I generated code with a lot of 
macro-time GOTO statemtents to rearrange the code as needed.

Using RAM is so much easier.

-- hendrik

> 
> 
> On Sep 6, 2012, at 10:23 PM, Jay K <jay.krell at cornell.edu> wrote:
> 
> > I'm not really suggesting any change to any existing code..
> >  
> > 
> > Though, the frontend, I gather, is already multi-pass,
> > so it might be nice if it did things in an order more
> > convenient for backends.
> > e.g. it'd be nice if all the import_procedure calls came in before any begin_procedure;
> > as it stands, many come in early, and then more come in in the middle of code.
> >  
> > 
> > I'm motivating that my C/C++ generating backend could
> > benefit from internally having multiple passes.
> >  
> > 
> > Which might be convenient to implement via a general
> > mechanism for combining multiple "partial" passes.
> >  
> >  
> > Such mechanism would naturally "temporarily persist"
> > a faithful m3cg representation in memory.
> > Very similar to the existing temporary binary files
> > we feed to parse.c, but without all the encoding for
> > compactness. i.e. an array of records
> >  
> >  
> > I guess I'll still try to ignore this matter for now.
> > I'm getting by with one pass that generates invalid C++ but valid C.
> >  
> >  
> > Possibly I can provide a "nicely reordering m3cg"
> > that buffers it all up and then plays it back
> > in a slightly better order, that is more amenable
> > to a simple implementation, e.g. again getting
> > all the import_procedure calls in before any begin_procedure.

You could use a mechanism like mine.  I could send you my C code for it 
if you like.  Or you could write the inport procedure calls out to one 
file and the begin_procedures to another and subsequently concatenate 
them.

-- hendrik



More information about the M3devel mailing list