[LLVMdev] Complex constant expressions?

Talin viridia at gmail.com
Tue Jan 22 09:00:27 PST 2008


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?

The disavantage of generating LLVM code for a constant expression are
several, including in particular the handling of errors - if the
constant expression gets a fatal error, you'd prefer not to crash the
compiler. Also, in a cross-compilation environment you'd have to
generate the constant expression for the host platform, rather than
for the target platform.

On the other hand, writing an interpreter means duplicating a lot of
the functionality that's already in LLVM. For example, consider just
the problem of float to int conversions:

   char B[ (int)3.0 ];

Generating code for this is relatively simple; Converting
arbitrary-sized APFloats to arbitrary-sized APInts isn't quite as
easy.

Similarly, the mathematical operators directly supported by APFloat
are only a subset of the math operators supported by the LLVM
instructions.

-- 
-- Talin



More information about the llvm-dev mailing list