[Top][All Lists]

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

Re: [Tinycc-devel] Best workaround for VLA's

From: Thomas Preud'homme
Subject: Re: [Tinycc-devel] Best workaround for VLA's
Date: Fri, 8 Apr 2011 14:15:16 +0200
User-agent: KMail/1.13.5 (Linux/2.6.38-1-amd64; KDE/4.4.5; x86_64; ; )

Le vendredi 08 avril 2011 08:32:47, Joe Soroka a écrit :
> On Thu, Apr 7, 2011 at 9:50 PM, Luis Alejandro Muzzachiodi
> <address@hidden> wrote:
> > Until now i was using successfully TCC with a multiplatform library.
> > However in the last time the author begin to use VLA's (and tough i'm
> > trying that not, it's very possible that he keeps and/or increases more
> > your use). So, what's would be the best workaround in TCC for stuff
> > like:
> >      char str[i+15];
> > or
> >      float flt[veclen];
> > etc.?
> > I mean, the simplest option could be the max possible length, sure; but i
> > haven't idea of max size of these arrays ... Having this on mind, what's
> > the best alternative (if exists...) ?.
> I'm planning to push a modified version of Thomas Preud'homme's VLA
> patch very soon that might solve your problem.
> But basically what I've been doing, is just converting those things to
> alloca() calls.
> So,
>     char str[i+15];
>     float flt[veclen];
> becomes
>     char *str = alloca(sizeof(char)*(i+15));
>     float *flt = alloca(sizeof(float)*veclen);
> The problem with that approach is that it doesn't work with
> multidimensional arrays, doesn't work with function parameters, and
> most importantly doesn't work with sizeof.
Actually my patch work with it.

I'm planning to work on it this WE. Currently a code like char str[i][j] is 
converted to something like this :

alloca(i*j*sizeof(char)) and the i, j and alloca return value are kept on the 
stack until we exit the block with the declaration. Also, when all the 
remaining nesting are non VLA, the size of each of these level is not stored 
on the stack (as for regular array).

What I'm doing (the work is already started) is to convert the code to 
something like :

char *alloca_result;
int level_sizes[2] = {i, j};
alloca_result = alloca(sizeof(char *) *  i * j);

For the bound check, if I remember well I managed to make it work by adding 
code but wasn't satisfied as no could should be needed since my VLA patch uses 
alloca and alloca as already bound checking.
> For example, there's no alloca equivalent for multidimensional
> dereferencing: char str[i+15][10];
>     str[0][0] = 123;
> Also, there's no obvious equivalent for something like the VLA example
> in "C in a nutshell":
>     double maximum(int nrows, int ncols, double matrix[nrows][ncols])
> { /* ... */ }
> Finally,
>     float flt[veclen];
>     memset(flt, 0, sizeof(flt));
> doesn't work.
> You can of course just use
>     memset(flt, 0, sizeof(float)*veclen);
> instead, but "veclen" may have changed since the array was allocated,
> say if it was reused for some other purpose in between.
> Also you probably should '#include <alloca.h>' which can be slightly
> annoying.
> ..
> _______________________________________________
> Tinycc-devel mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/tinycc-devel

Attachment: signature.asc
Description: This is a digitally signed message part.

reply via email to

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