diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | llvm.cpp (renamed from tuplr_llvm.cpp) | 21 | ||||
-rw-r--r-- | tuplr.hpp | 8 | ||||
-rw-r--r-- | typing.cpp | 22 |
4 files changed, 31 insertions, 22 deletions
@@ -4,7 +4,7 @@ LLVM_LDFLAGS=`llvm-config --ldflags --libs core jit native` CXXFLAGS=-O0 -g -Wall -Wextra -Wno-unused-parameter $(LLVM_CXXFLAGS) LDFLAGS=$(LLVM_LDFLAGS) -lm -tuplr: tuplr.o typing.o tuplr_llvm.o +tuplr: tuplr.o typing.o llvm.o g++ -o $@ $^ $(LDFLAGS) %.o: %.cpp tuplr.hpp diff --git a/tuplr_llvm.cpp b/llvm.cpp index 5ff570e..0b32656 100644 --- a/tuplr_llvm.cpp +++ b/llvm.cpp @@ -212,6 +212,11 @@ compileFunction(CEnv& cenv, const std::string& name, CType retT, const ASTTuple& return f; } + +/*************************************************************************** + * AST Code Generation * + ***************************************************************************/ + CValue ASTSymbol::compile(CEnv& cenv) { @@ -332,22 +337,18 @@ ASTIf::compile(CEnv& cenv) BasicBlock* mergeBB = BasicBlock::Create("endif"); BasicBlock* nextBB = NULL; Branches branches; - ostringstream ss; for (size_t i = 1; i < size() - 1; i += 2) { - Value* condV = LLVal(cenv.compile(at(i))); - - ss.str(""); ss << "then" << ((i + 1) / 2); - BasicBlock* thenBB = BasicBlock::Create(ss.str()); + Value* condV = LLVal(cenv.compile(at(i))); + BasicBlock* thenBB = BasicBlock::Create((format("then%1%") % ((i+1)/2)).str()); - ss.str(""); ss << "else" << ((i + 1) / 2); - nextBB = BasicBlock::Create(ss.str()); + nextBB = BasicBlock::Create((format("else%1%") % ((i+1)/2)).str()); cenv.engine.builder.CreateCondBr(condV, thenBB, nextBB); // Emit then block for this condition parent->getBasicBlockList().push_back(thenBB); cenv.engine.builder.SetInsertPoint(thenBB); - Value* thenV = LLVal(cenv.compile(at(i + 1))); + Value* thenV = LLVal(cenv.compile(at(i+1))); cenv.engine.builder.CreateBr(mergeBB); branches.push_back(make_pair(thenV, cenv.engine.builder.GetInsertBlock())); @@ -355,7 +356,7 @@ ASTIf::compile(CEnv& cenv) cenv.engine.builder.SetInsertPoint(nextBB); } - // Emit else block + // Emit final else block cenv.engine.builder.SetInsertPoint(nextBB); Value* elseV = LLVal(cenv.compile(at(size() - 1))); cenv.engine.builder.CreateBr(mergeBB); @@ -366,7 +367,7 @@ ASTIf::compile(CEnv& cenv) cenv.engine.builder.SetInsertPoint(mergeBB); PHINode* pn = cenv.engine.builder.CreatePHI(LLType(cenv.tenv.type(this)->type()), "ifval"); - for (Branches::iterator i = branches.begin(); i != branches.end(); ++i) + FOREACH(Branches::iterator, i, branches) pn->addIncoming(i->first, i->second); return pn; @@ -151,7 +151,13 @@ struct ASTTuple : public AST, public vector<AST*> { FOREACH(iterator, t, *this) (*t)->lift(cenv); } - bool contains(AST* child) const; + bool contains(AST* child) const { + if (*this == *child) return true; + FOREACH(const_iterator, p, *this) + if (**p == *child || (*p)->contains(child)) + return true; + return false; + } void constrain(TEnv& tenv) const; CValue compile(CEnv& cenv) { throw Error("tuple compiled"); } }; @@ -16,6 +16,12 @@ */ #include "tuplr.hpp" + + +/*************************************************************************** + * AST Type Contraints * + ***************************************************************************/ + void ASTTuple::constrain(TEnv& tenv) const { @@ -107,6 +113,11 @@ ASTCdrCall::constrain(TEnv& tenv) const tenv.constrain(this, ct); } + +/*************************************************************************** + * Type Inferencing/Substitution * + ***************************************************************************/ + static void substitute(ASTTuple* tup, AST* from, AST* to) { @@ -118,16 +129,6 @@ substitute(ASTTuple* tup, AST* from, AST* to) substitute(dynamic_cast<ASTTuple*>(tup->at(i)), from, to); } -bool -ASTTuple::contains(AST* child) const -{ - if (*this == *child) return true; - FOREACH(const_iterator, p, *this) - if (**p == *child || (*p)->contains(child)) - return true; - return false; -} - TSubst compose(const TSubst& delta, const TSubst& gamma) // TAPL 22.1.1 { @@ -196,3 +197,4 @@ TEnv::apply(const TSubst& substs) else substitute(t->second, s->first, s->second); } + |