[Top][All Lists]

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

Re: [Tinycc-devel] Proposal for handling alloca(). Anyone see a problem

From: Daniel Glöckner
Subject: Re: [Tinycc-devel] Proposal for handling alloca(). Anyone see a problem with it?
Date: Mon, 7 May 2007 22:35:26 +0200
User-agent: Mutt/

On Mon, May 07, 2007 at 03:28:51PM -0400, David A. Wheeler wrote:
> Wheeler mentioned this as a problem:
> > >   a( b(4+c()), alloca(x*g()), h()+n());
> > > In this example, a naive alloca implementation would be in the
> > > middle of computing this expression, with some intermediate values
> > > on the stack, and the alloca() call would then screw up the stack
> > > DURING the expression calculation... causing the whole expression
> > > to miscalculate.
> Daniel Glöckner <address@hidden>
> > Are you shure this can happen with tinycc?
> > AFAIR all parameters are evaluated before gfunc_call is called to put
> > them on the stack.
> I believe that's only true for each function call taken individually.
> But when calls are nested, you can't make an (outer) call unless you've
> computed all its (inner) parameters, and if a call to alloca() is in
> the parameters, it'll usually subvert the evaluation process.

> Here's my understanding of what would happen in the example above

Ok, and here is my understanding of what would happen:
 * tinycc generates anonymous variables p1, p2, t in the stack frame
 * tinycc generates asm code that works like:
   - call c
   - add 4 to returned value
   - push result on stack
   - call b
   - store result in p1
   - call g
   - multiply by x
   - push result on stack
   - call alloca
   - store result in p2
   - call h
   - store result in t
   - call n
   - add t to returned value
   - push result on stack
   - push p2 on stack
   - push p1 on stack
   - call a

> * The code calculates b(4+c()), and places that result on the stack.

The result is not immediately placed on the real stack.
It stays in a register and is moved to an anonymous variable when the
register is needed. This happens for all parameters.

> Now tinycc needs to generate the code that calls a().
> But wait!  The parameters of a() are no longer adjacent on the stack

They are adjacent on the vtop stack which contains references to the
registers and variables that contain the parameters.
gfunc_call now pushes all (already evaluated) parameters on the stack
and generates a call.

The drawback of this behaviour is that tinycc generates dozens of
temporary variables in the stack frame which are never reused.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]