[LLVMdev] Complex constant expressions?

Chris Lattner sabre at nondot.org
Tue Jan 22 10:34:09 PST 2008


On Tue, 22 Jan 2008, Talin wrote:
> More questions on the best way to write a compiler using LLVM:

ok

> 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?

It's really up to you, and it may be up to your language spec.  One 
approach would be to generate a series of llvm::ConstantExpr::get* method 
calls, which implicitly constant fold expressions where possible.  For 
example, if you ask for "add 3+7" you'll get 10.  If you ask for "div 15, 
0" you'll get an unfolded constant expr back.

However, if you treat type checking separately from code generation, your 
language may require that you diagnose these sorts of things, and that 
would mean that you have to implement the constant folding logic in  your 
frontend.  This is what clang does, for example.

If you go this route, you can still use the LLVM APInt and APFloat 
classes to do these operations, and maintain the correct precision etc.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list