[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: Fred Weigel
Subject: Re: [Tinycc-devel] Proposal for handling alloca(). Anyone see a problem with it?
Date: Tue, 08 May 2007 08:04:27 -0400

alloca() is usually implemented on architectures that have separate
base-pointer and stack-pointer. The base pointer is used to provide a
consistent base for addressing the local frame on the stack. The stack
pointer can grow down any time (eg. hardware interrupts, or complex
expressions being stacked as parameters).

Since this is the case on the x86 (SP and BP), and since BP is restored
from its local frame, and SP adjusted to that, the correct
implementation of alloca() (well, AN arguably correct implementation) is
to simply return SP, and decrement it by the request amount, rounded up
to the basic stack alignment.

But, you may be using alloca() in a parameter sub-expression! (at which
point later earlier parameters can no longer be correctly retrieved).

Which is the problem you are thinking about...

I suggest that on the each occurrence of alloca() in an expression that
is a parameter, generate a subtract from SP. Generate a copy of the
total parameter bytes (to date) to just before the new SP.

Note that attempting a malloc()/free() will not work: unless you can
guarantee the longjmp implementation (and carry information on each
parameter). It is still tricky: consider a setjmp() before the alloca()
function is invoked. So, all jmpbuf need to be chained, and since *they*
can be dynamically allocated, including alloca(), well, its tricky...

My solution is simple, and will work.

reply via email to

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