[LLVMdev] Complex constant expressions?

me22 me22.ca at gmail.com
Tue Jan 22 20:15:01 PST 2008

On 22/01/2008, Talin <viridia at gmail.com> wrote:
> More questions on the best way to write a compiler using LLVM:
> Lets say I have a struct definition that looks like this:
>    const int imageSize = 77;
>    struct A {
>       char B[align(imageSize)];
>    }
> ...where 'imageSize' is some small inline function that rounds up to a
> power of two or something. (A common requirement for textures on 3d
> graphics cards.)
> Now, clearly the compiler needs to evaluate the array size expression
> at compile time, since the size of the array needs to be known up
> front. My question is, would it generally be better to generate LLVM
> code for the expression and run it in the compiler, or would you
> basically have to create an interpreter within your compiler that is
> capable of executing your AST?

One thing that jumped to mind was "Hey, I can write that as a
metafunction!" using C++, so you might be able to find hints in the
way that templates are handled.

On a similar note, there's a paper on Generalized Constant Expressions
( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf )
proposed for the next C++ revision, so you might be able to find other
people doing the same thing.  (Though IIRC that proposal doesn't allow
recursion or mutation of values, which drastically simplifies things.)

Sed quis custodiet ipsos custodes?

More information about the llvm-dev mailing list