From da47da35f268416e1131fa4e107e32fa6a667ae4 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 17 Aug 2008 13:09:08 +0200 Subject: LLVM compiler: Implement IF and support self-evaluating forms. --- MLKLLVMCompiler.mm | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/MLKLLVMCompiler.mm b/MLKLLVMCompiler.mm index 7767d48..81aa764 100644 --- a/MLKLLVMCompiler.mm +++ b/MLKLLVMCompiler.mm @@ -18,6 +18,7 @@ #import "MLKLLVMCompiler.h" #import "globals.h" +#import "util.h" #import #import @@ -606,9 +607,61 @@ static Constant @implementation MLKQuoteForm (MLKLLVMCompilation) -(Value *) processForLLVM { + // FIXME: When to release _quotedData? At the same time the code is + // released, probably... + LRETAIN (_quotedData); return builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, (uint64_t)_quotedData, false), PointerTy); } @end + + +@implementation MLKSelfEvaluatingForm (MLKLLVMCompilation) +-(Value *) processForLLVM +{ + // FIXME: When to release _form? At the same time the code is + // released, probably... + LRETAIN (_form); + return builder.CreateIntToPtr (ConstantInt::get(Type::Int64Ty, + (uint64_t)_form, + false), + PointerTy); +} +@end + + +@implementation MLKIfForm (MLKLLVMCompilation) +-(Value *) processForLLVM +{ + Function *function = builder.GetInsertBlock()->getParent(); + BasicBlock *thenBlock = BasicBlock::Create ("if_then", function); + BasicBlock *elseBlock = BasicBlock::Create ("if_else"); + BasicBlock *joinBlock = BasicBlock::Create ("if_join"); + + Value *thenValue, *elseValue; + + Value *test = builder.CreateICmpNE ([_conditionForm processForLLVM], + ConstantPointerNull::get (PointerTy)); + builder.CreateCondBr (test, thenBlock, elseBlock); + + builder.SetInsertPoint (thenBlock); + thenValue = [_consequentForm processForLLVM]; + builder.CreateBr (joinBlock); + + builder.SetInsertPoint (elseBlock); + function->getBasicBlockList().push_back (elseBlock); + elseValue = [_alternativeForm processForLLVM]; + builder.CreateBr (joinBlock); + + builder.SetInsertPoint (joinBlock); + function->getBasicBlockList().push_back (joinBlock); + + PHINode *value = builder.CreatePHI (PointerTy, "if_result"); + value->addIncoming (thenValue, thenBlock); + value->addIncoming (elseValue, elseBlock); + + return value; +} +@end -- cgit v1.2.3