diff --git a/cpp/src/analysis/typehints.cpp b/cpp/src/analysis/typehints.cpp index 5c9c1e7..f504d2d 100644 --- a/cpp/src/analysis/typehints.cpp +++ b/cpp/src/analysis/typehints.cpp @@ -1,68 +1,68 @@ // // Created by pgess on 24/03/2020. // #include "analysis/typehints.h" #include "analysis/predefinedanns.h" #include "analysis/utils.h" +#include "aux/expressions.h" namespace xreate{namespace typehints{ namespace impl { template inline Hint getHint(const Expression& e, const Hint& def, unsigned annId, const ExpandedType& hintT){ - std::list hintsL; + const std::list& hintsL = getAnnotations(e); auto predefined = analysis::PredefinedAnns::instance(); - for(const auto& tag: e.tags){hintsL.push_back(tag.second);} const Expression& hintActual = analysis::findAnnById(annId, hintT, hintsL); if (!hintActual.isValid()){ return def; } return parse(hintActual); } } template<> IntBits parse(const Expression& e){ return {(unsigned) e.operands.at(0).getValueDouble()}; } template<> ArrayHint parse(const Expression& e){ return {(unsigned) e.operands.at(0).getValueDouble()}; } template<> FlyHint parse(const Expression& e){ return {e.operands.at(0)}; } template<> IntBits find(const Expression& e, const IntBits& def){ auto predefined = analysis::PredefinedAnns::instance(); return impl::getHint(e, def, (unsigned) analysis::PredefinedAnns::IntHints::SIZE, ExpandedType(predefined.hintsIntT) ); } template<> ArrayHint find(const Expression& e, const ArrayHint& def){ auto predefined = analysis::PredefinedAnns::instance(); return impl::getHint(e, def, (unsigned) analysis::PredefinedAnns::ContHints::ARRAY, ExpandedType(predefined.hintsContT) ); } template<> FlyHint find(const Expression& e, const FlyHint& def){ auto predefined = analysis::PredefinedAnns::instance(); return impl::getHint(e, def, (unsigned) analysis::PredefinedAnns::ContHints::FLY, ExpandedType(predefined.hintsContT)); } }} \ No newline at end of file diff --git a/cpp/src/aux/expressions.cpp b/cpp/src/aux/expressions.cpp index 67428a6..aae6182 100644 --- a/cpp/src/aux/expressions.cpp +++ b/cpp/src/aux/expressions.cpp @@ -1,60 +1,67 @@ #include "expressions.h" namespace xreate{ Expression getVariantData(const Expression &variantE, ExpandedType variantT){ Expression result(Operator::LIST, {}); TypeAnnotation variantDataT(variantT->__operands.at(variantE.getValueDouble())); if(!variantDataT.isValid()) return Expression(); assert(variantDataT.__operator == TypeOperator::RECORD); result.operands = variantE.operands; result.bindings = variantDataT.fields; result.type = variantDataT; return result; } ListDictionary reprListAsDict(const Expression &e){ ListDictionary result; assert(e.__state == Expression::COMPOUND); switch(e.op){ case Operator::LIST:{ Expression keyE; int lastPos = 0; for(size_t i = 0; i < e.operands.size(); ++i){ const Expression &valueE = e.operands.at(i); const std::string &keyS = e.bindings.at(i); if(keyS.empty()){ keyE = Expression(Atom(lastPos++)); } else{ keyE = Expression(Atom(std::string(keyS))); } result.emplace(keyE, valueE); } break; } case Operator::LIST_INDEX:{ auto opI = e.operands.cbegin(); while(opI != e.operands.cend()){ const Expression &keysListE = *(opI++); const Expression &valueE = *(opI++); assert(keysListE.operands.size() == 1); result.emplace(keysListE.operands.at(0), valueE); } break; } default: assert(false); } return result; } +std::list +getAnnotations(const Expression& e){ + std::list result; + for(const auto& tag: e.tags){result.push_back(tag.second);} + + return result; +} } \ No newline at end of file diff --git a/cpp/src/aux/expressions.h b/cpp/src/aux/expressions.h index 4be22b8..1d33ea8 100644 --- a/cpp/src/aux/expressions.h +++ b/cpp/src/aux/expressions.h @@ -1,15 +1,16 @@ // // Created by pgess on 31/01/2020. // #ifndef XREATE_EXPRESSIONS_H #define XREATE_EXPRESSIONS_H #include "ast.h" namespace xreate{ typedef std::map ListDictionary; Expression getVariantData(const Expression& variantE, ExpandedType variantT); ListDictionary reprListAsDict(const Expression& e); + std::list getAnnotations(const Expression& e); } #endif //XREATE_EXPRESSIONS_H diff --git a/cpp/src/compilation/containers.cpp b/cpp/src/compilation/containers.cpp index c6e7140..c5f5438 100644 --- a/cpp/src/compilation/containers.cpp +++ b/cpp/src/compilation/containers.cpp @@ -1,236 +1,287 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: containers.cpp * Author: pgess */ /** * \file compilation/containers.h * \brief Containers compilation support. See [Containers](/d/concepts/containers/) in the Xreate's documentation. */ #include "compilation/containers.h" #include "compilation/targetinterpretation.h" #include "aux/expressions.h" #include "compilation/containers/arrays.h" #include "compilation/lambdas.h" #include "analysis/predefinedanns.h" #include "analysis/utils.h" using namespace std; namespace xreate { namespace containers{ ImplementationType IContainersIR::getImplementation(const Expression& aggrE, AST* ast){ auto manPredefined = analysis::PredefinedAnns::instance(); const Expression& hintE = analysis::findAnnByType(aggrE, ExpandedType(manPredefined.hintsContT), ast); assert(hintE.isValid()); return (ImplementationType ) hintE.getValueDouble(); } IContainersIR * IContainersIR::create(const Expression &aggrE, const TypeAnnotation &expectedT, const compilation::Context &context){ ExpandedType aggrT = context.pass->man->root->getType(aggrE, expectedT); Expression aggr2E; if (aggrE.__state == Expression::IDENT && !aggrE.tags.size()){ Symbol aggrS = Attachments::get(aggrE); aggr2E = CodeScope::getDefinition(aggrS); } else { aggr2E = aggrE; } switch(aggr2E.op){ case Operator::LIST:{ typehints::ArrayHint aggrHint = typehints::find( - aggr2E, typehints::ArrayHint{aggr2E.operands.size()} + aggr2E, typehints::ArrayHint{aggr2E.operands.size()} ); return new ArrayIR(aggrT, aggrHint, context); } default: typehints::ArrayHint aggrHint = typehints::find( - aggr2E, typehints::ArrayHint{0} + aggr2E, typehints::ArrayHint{0} ); assert(aggrHint.size != 0); return new ArrayIR(aggrT, aggrHint, context); } assert(false); return nullptr; } +llvm::Type* +IContainersIR::getRawType(const Expression& aggrE, const ExpandedType& aggrT, LLVMLayer* llvm){ + auto manPredefined = analysis::PredefinedAnns::instance(); + const Expression& hintE = analysis::findAnnByType(aggrE, ExpandedType(manPredefined.hintsContT), llvm->ast); + assert(hintE.isValid()); + + return getRawTypeByHint(hintE, aggrT, llvm); +} + +llvm::Type* +IContainersIR::getRawTypeByHint(const Expression& hintE, const ExpandedType& aggrT, LLVMLayer* llvm) { + ImplementationType hintImpl = (ImplementationType ) hintE.getValueDouble(); + switch (hintImpl){ + case SOLID: { + typehints::ArrayHint hint = typehints::parse(hintE); + return ArrayIR::getRawType(aggrT, hint, llvm); + } + + case ON_THE_FLY:{ + typehints::FlyHint hint = typehints::parse(hintE); + return FlyIR::getRawType(aggrT, hint, llvm); + } + + default: break; + } + + assert(false); + return nullptr; +} + llvm::Value * RecordIR::init(llvm::StructType *tyAggr){ return llvm::UndefValue::get(tyAggr); } llvm::Value* RecordIR::init(std::forward_list fields){ std::vector fieldsVec(fields.begin(), fields.end()); llvm::ArrayRef fieldsArr(fieldsVec); llvm::StructType* resultTR = llvm::StructType::get(__context.pass->man->llvm->llvmContext, fieldsArr, false); return init(resultTR); } llvm::Value * RecordIR::update(llvm::Value *aggrRaw, const ExpandedType &aggrT, const Expression &updE){ interpretation::InterpretationScope *scopeI12n = __context.pass->targetInterpretation->transformContext(__context); TypesHelper helper(__context.pass->man->llvm); const auto &fields = helper.getRecordFields(aggrT); std::map indexFields; for(size_t i = 0, size = fields.size(); i < size; ++i){ indexFields.emplace(fields[i], i); } for(const auto &entry: reprListAsDict(updE)){ unsigned keyId; std::string keyHint; const Expression keyE = scopeI12n->process(entry.first); switch(keyE.__state){ case Expression::STRING: keyId = indexFields.at(keyE.getValueString()); keyHint = keyE.getValueString(); break; case Expression::NUMBER: keyId = keyE.getValueDouble(); keyHint = aggrT->fields.at(keyId); break; default: assert(false); break; } const TypeAnnotation &valueT = aggrT->__operands.at(keyId); llvm::Value *valueRaw = __context.scope->process(entry.second, keyHint, valueT); aggrRaw = __context.pass->man->llvm->irBuilder.CreateInsertValue( aggrRaw, valueRaw, keyId); } return aggrRaw; } llvm::Value* FlyIR::create(llvm::Value* sourceRaw, CodeScope* body, const std::string& hintAlias){ RecordIR recordIR(__context); compilation::LambdaIR lambdaIR(__context.pass); llvm::Function* fnTransform = lambdaIR.compile(body, hintAlias); llvm::Value* resultRaw = recordIR.init({ sourceRaw->getType(), fnTransform->getFunctionType()->getPointerTo() }); resultRaw = __context.pass->man->llvm->irBuilder.CreateInsertValue( resultRaw, sourceRaw, 0); resultRaw = __context.pass->man->llvm->irBuilder.CreateInsertValue( resultRaw, fnTransform, 1); return resultRaw; } +llvm::Type* +FlyIR::getRawType(const ExpandedType& aggrT, const typehints::FlyHint& hint, LLVMLayer* llvm){ + assert(aggrT->__operator == TypeOperator::ARRAY); + TypesHelper types(llvm); + + llvm::Type* sourceTRaw = IContainersIR::getRawTypeByHint(hint.hintSrc, aggrT, llvm); + llvm::Type* elTRaw = llvm->toLLVMType(ExpandedType(aggrT->__operands.at(0))); + llvm::Type* resultTRaw = types.getPreferredIntTy(); + + llvm::Type* fnTnsfTRaw = llvm::FunctionType::get(resultTRaw, llvm::ArrayRef(elTRaw), false); + std::vector fieldsVec = { + sourceTRaw, + fnTnsfTRaw->getPointerTo() + }; + llvm::ArrayRef fieldsArr(fieldsVec); + llvm::StructType* resultTR = llvm::StructType::get(llvm->llvmContext, fieldsArr, false); + + return resultTR; +} + llvm::Value* FlyIR::getTransformFn(llvm::Value* aggrRaw){ LLVMLayer* llvm = __context.pass->man->llvm; llvm::Value* fnRaw = llvm->irBuilder.CreateExtractValue(aggrRaw, llvm::ArrayRef{1}); return fnRaw; } -llvm::Value* FlyIR::getSourceAggr(llvm::Value* aggrRaw){ +llvm::Value* +FlyIR::getSourceAggr(llvm::Value* aggrRaw){ LLVMLayer* llvm = __context.pass->man->llvm; return llvm->irBuilder.CreateExtractValue(aggrRaw, llvm::ArrayRef{0}); } llvm::Value* FlyIR::operatorMap(const Expression& expr, const std::string& hintAlias){ const Expression& sourceE = expr.getOperands().at(0); llvm::Value* sourceRaw = __context.scope->process(sourceE); CodeScope* loopS = expr.blocks.front(); return create(sourceRaw, loopS, hintAlias); } FlyIR::FlyIR(typehints::FlyHint hint, compilation::Context context) : __hint(hint), __context(context){} IFwdIteratorIR* IFwdIteratorIR::createByHint(const Expression& hintE, const ExpandedType& aggrT, const compilation::Context& context){ ImplementationType hintType = (ImplementationType) hintE.getValueDouble(); switch(hintType){ case SOLID:{ ArrayIR compiler(aggrT, typehints::parse(hintE), context); return new FwdIteratorIR(compiler); } case ON_THE_FLY:{ return new FwdIteratorIR(typehints::parse(hintE), aggrT, context); } default: break; } assert(false); return nullptr; } IFwdIteratorIR* IFwdIteratorIR::create(const Expression& aggrE, const ExpandedType& aggrT, const compilation::Context& context){ auto manPredefined = analysis::PredefinedAnns::instance(); const Expression& hintE = analysis::findAnnByType( aggrE, ExpandedType(manPredefined.hintsContT), context.pass->man->root ); assert(hintE.isValid()); return createByHint(hintE, aggrT, context); } llvm::Value* FwdIteratorIR::begin() { std::unique_ptr itSrcIR(IFwdIteratorIR::createByHint(__hint.hintSrc, __aggrT, __context)); return itSrcIR->begin(); } llvm::Value* FwdIteratorIR::end() { std::unique_ptr itSrcIR(IFwdIteratorIR::createByHint(__hint.hintSrc, __aggrT, __context)); return itSrcIR->end(); } llvm::Value* FwdIteratorIR::advance(llvm::Value* idxRaw, const std::string& hintAlias){ std::unique_ptr itSrcIR(IFwdIteratorIR::createByHint(__hint.hintSrc, __aggrT, __context)); return itSrcIR->advance(idxRaw, hintAlias); } llvm::Value* FwdIteratorIR::get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintAlias){ std::unique_ptr srcIterIR(IFwdIteratorIR::createByHint(__hint.hintSrc, __aggrT, __context)); FlyIR compilerFly(__hint, __context); llvm::Value* aggrSrcRaw = compilerFly.getSourceAggr(aggrRaw); llvm::Value* valueSrcRaw = srcIterIR->get(aggrSrcRaw, idxRaw); FlyIR flyIR(__hint, __context); llvm::Value* fnTnsfRaw = flyIR.getTransformFn(aggrRaw); llvm::Type* fnTnsfTRawRaw = llvm::cast(fnTnsfRaw->getType())->getElementType(); llvm::FunctionType* fnTnsfTRaw = llvm::cast(fnTnsfTRawRaw); compilation::BruteFnInvocation fnTnsfInvoc(fnTnsfRaw, fnTnsfTRaw, __context.pass->man->llvm); return fnTnsfInvoc({valueSrcRaw}, hintAlias); } }} //end of xreate::containers diff --git a/cpp/src/compilation/containers.h b/cpp/src/compilation/containers.h index 8c953f3..6ec9f12 100644 --- a/cpp/src/compilation/containers.h +++ b/cpp/src/compilation/containers.h @@ -1,108 +1,111 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: containers.h * Author: pgess */ #ifndef CODEINSTRUCTIONS_H #define CODEINSTRUCTIONS_H #include "ast.h" #include "llvmlayer.h" #include "pass/compilepass.h" #include "compilation/control.h" #include "query/containers.h" #include "analysis/typehints.h" namespace xreate { namespace containers { class IFwdIteratorIR; class IContainersIR{ public: static IContainersIR *create( const Expression &aggrE, const TypeAnnotation &expectedT, const compilation::Context &context); static ImplementationType getImplementation(const Expression& aggrE, AST* ast); - + static llvm::Type* getRawType(const Expression& aggrE, const ExpandedType& aggrT, LLVMLayer* llvm); + static llvm::Type* getRawTypeByHint(const Expression& hintE, const ExpandedType& aggrT, LLVMLayer* llvm); virtual llvm::Value *init(const std::string &hintAlias = "") = 0; virtual llvm::Value *update(llvm::Value *aggrRaw, const Expression &updE, const std::string &hintAlias) = 0; virtual IFwdIteratorIR* getFwdIterator() = 0; + virtual ~IContainersIR(){} }; class RecordIR{ public: RecordIR(const compilation::Context& context): __context(context){} llvm::Value* init(llvm::StructType* tyAggr); llvm::Value* init(std::forward_list fields); llvm::Value* update(llvm::Value* aggrRaw, const ExpandedType& aggrT, const Expression& updE); private: compilation::Context __context; }; class FlyIR{ public: FlyIR(typehints::FlyHint hint, compilation::Context context); llvm::Value* create(llvm::Value* sourceRaw, CodeScope* body, const std::string& hintAlias); llvm::Value* getSourceAggr(llvm::Value* aggrRaw); llvm::Value* getTransformFn(llvm::Value* aggrRaw); llvm::Value* operatorMap(const Expression& expr, const std::string& hintAlias); + static llvm::Type *getRawType(const ExpandedType& aggrT, const typehints::FlyHint& hint, LLVMLayer* llvm); private: typehints::FlyHint __hint; compilation::Context __context; }; /** * \brief A factory to create a concrete iterator based on the solution provided by xreate::containers::Query * \sa xreate::containers::Query */ class IFwdIteratorIR{ public : virtual ~IFwdIteratorIR(){}; virtual llvm::Value* begin() = 0; virtual llvm::Value* end() = 0; virtual llvm::Value* get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintAlias="") = 0; virtual llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") = 0; static IFwdIteratorIR* create(const Expression& aggrE, const ExpandedType& aggrT, const compilation::Context& context); static IFwdIteratorIR* createByHint(const Expression& hintE, const ExpandedType& aggrT, const compilation::Context& context); }; template class FwdIteratorIR; /** \brief The lazy container implementation. * * Represents computation on the fly. * \sa xreate::containers::IFwdIteratorIR, \sa xreate::containers::Query */ template<> class FwdIteratorIR : public IFwdIteratorIR { public: FwdIteratorIR(typehints::FlyHint hint, const ExpandedType &aggrT, compilation::Context context) : __aggrT(aggrT), __hint(hint), __context(context){} virtual llvm::Value* begin() override; virtual llvm::Value* end() override; virtual llvm::Value* get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintAlias="") override; virtual llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") override; private: ExpandedType __aggrT; typehints::FlyHint __hint; compilation::Context __context; }; }} #endif //CODEINSTRUCTIONS_H diff --git a/cpp/src/compilation/containers/arrays.cpp b/cpp/src/compilation/containers/arrays.cpp index d57ff02..78f2abc 100644 --- a/cpp/src/compilation/containers/arrays.cpp +++ b/cpp/src/compilation/containers/arrays.cpp @@ -1,169 +1,167 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: arrays.cpp * Author: pgess * * Created in March 2020. */ #include "compilation/containers/arrays.h" #include "aux/expressions.h" using namespace std; using namespace llvm; namespace xreate { namespace containers{ -llvm::ArrayType* -ArrayIR::getRawT(){ - assert(__aggrT->__operator == TypeOperator::ARRAY); - assert(__aggrT->__operands.size() == 1); +llvm::PointerType* +ArrayIR::getRawType(const ExpandedType& aggrT, const typehints::ArrayHint& hint, LLVMLayer* llvm){ + assert(aggrT->__operator == TypeOperator::ARRAY); + assert(aggrT->__operands.size() == 1); + llvm::Type* elRawT = llvm->toLLVMType(ExpandedType(aggrT->__operands.at(0))); - LLVMLayer* llvm = __context.pass->man->llvm; - llvm::Type* elRawT = llvm->toLLVMType(ExpandedType(__aggrT->__operands.at(0))); - - return llvm::ArrayType::get(elRawT, __hints.size); + return llvm::ArrayType::get(elRawT, hint.size)->getPointerTo(); } llvm::Value* ArrayIR::init(const string& hintAlias){ LLVMLayer* llvm = __context.pass->man->llvm; TypesHelper helper(llvm); - llvm::ArrayType* aggrRawT = getRawT(); + llvm::PointerType* aggrRawT = getRawType(__aggrT, __hint, __context.pass->man->llvm); //llvm::Value* aggrLengthRaw = ConstantInt::get(helper.getPreferredIntTy(), aggrInfo.size); - llvm::Value* aggrRaw = llvm->irBuilder.CreateAlloca(aggrRawT, nullptr, hintAlias); + llvm::Value* aggrRaw = llvm->irBuilder.CreateAlloca(aggrRawT->getElementType(), nullptr, hintAlias); return aggrRaw; } llvm::Value* ArrayIR::update(llvm::Value* aggrRaw, const Expression& updE, const std::string& hintAlias) { LLVMLayer* llvm = __context.pass->man->llvm; TypesHelper helper(llvm); llvm::IntegerType* intT = helper.getPreferredIntTy(); llvm::Value* idxZeroRaw = ConstantInt::get(intT, 0); - llvm::ArrayType* aggrRawT = getRawT(); + llvm::PointerType* aggrRawT = getRawType(__aggrT, __hint, __context.pass->man->llvm); const TypeAnnotation& elT = __aggrT->__operands.at(0); //llvm::Type* elTRaw = llvm->toLLVMType(ExpandedType(aggrT->__operands.at(0))); for (const auto& entry: reprListAsDict(updE)){ llvm::Value* keyRaw = __context.scope->process(entry.first); llvm::Value* elRaw = __context.scope->process(entry.second, "", elT); llvm::Value* elLoc = llvm->irBuilder.CreateGEP( - aggrRawT, aggrRaw, ArrayRef(std::vector{idxZeroRaw, keyRaw})); + aggrRawT->getElementType(), aggrRaw, ArrayRef(std::vector{idxZeroRaw, keyRaw})); llvm->irBuilder.CreateStore(elRaw, elLoc) ; } return aggrRaw; } llvm::Value* ArrayIR::get(llvm::Value* aggrRaw, std::vector idxL, const std::string& hintAlias) { LLVMLayer* llvm = __context.pass->man->llvm; TypesHelper helper(llvm); llvm::IntegerType* intT = helper.getPreferredIntTy(); llvm::Value* zeroRaw = ConstantInt::get(intT, 0); idxL.insert(idxL.begin(), zeroRaw); llvm::Value *pEl = llvm->irBuilder.CreateGEP(aggrRaw, llvm::ArrayRef(idxL)); return llvm->irBuilder.CreateLoad(pEl, hintAlias); } llvm::Value* ArrayIR::operatorMap(const Expression& expr, const std::string& hintAlias) { assert(false); return nullptr; //EXPAND_CONTEXT UNUSED(scope); // //initializationcompileListAsSolidArray // Symbol symbolIn = Attachments::get(expr.getOperands()[0]); // // ImplementationRec implIn = containers::Query::queryImplementation(symbolIn).extract(); // impl of input list // size_t size = implIn.size; // CodeScope* scopeLoop = expr.blocks.front(); // std::string varEl = scopeLoop->__bindings[0]; // // IFwdIteratorIR* it = IFwdIteratorIR::create(context, symbolIn); // llvm::Value *rangeFrom = it->begin(); // llvm::Value *rangeTo = it->end(); // // //definitions // ArrayType* tyNumArray = nullptr; //(ArrayType*) (llvm->toLLVMType(ExpandedType(TypeAnnotation(tag_array, TypePrimitive::Int, size)))); // llvm::IRBuilder<> &builder = llvm->irBuilder; // // llvm::BasicBlock *blockLoop = llvm::BasicBlock::Create(llvm->llvmContext, "loop", function->raw); // llvm::BasicBlock *blockBeforeLoop = builder.GetInsertBlock(); // llvm::BasicBlock *blockAfterLoop = llvm::BasicBlock::Create(llvm->llvmContext, "postloop", function->raw); // Value* dataOut = llvm->irBuilder.CreateAlloca(tyNumArray, ConstantInt::get(tyNum, size), NAME("map")); // // // * initial check // Value* condBefore = builder.CreateICmpSLE(rangeFrom, rangeTo); // builder.CreateCondBr(condBefore, blockLoop, blockAfterLoop); // // // create PHI: // builder.SetInsertPoint(blockLoop); // llvm::PHINode *stateLoop = builder.CreatePHI(tyNum, 2, "mapIt"); // stateLoop->addIncoming(rangeFrom, blockBeforeLoop); // // // loop body: // Value* elIn = it->get(stateLoop, varEl); // compilation::IBruteScope* scopeLoopUnit = function->getScopeUnit(scopeLoop); // scopeLoopUnit->bindArg(elIn, move(varEl)); // Value* elOut = scopeLoopUnit->compile(); // Value *pElOut = builder.CreateGEP(dataOut, ArrayRef(std::vector{ConstantInt::get(tyNum, 0), stateLoop})); // builder.CreateStore(elOut, pElOut); // // //next iteration preparing // Value *stateLoopNext = builder.CreateAdd(stateLoop, llvm::ConstantInt::get(tyNum, 1)); // stateLoop->addIncoming(stateLoopNext, builder.GetInsertBlock()); // // //next iteration checks: // Value* condAfter = builder.CreateICmpSLE(stateLoopNext, rangeTo); // builder.CreateCondBr(condAfter, blockLoop, blockAfterLoop); // // //finalization: // builder.SetInsertPoint(blockAfterLoop); // // return dataOut; } IFwdIteratorIR* ArrayIR::getFwdIterator(){ return new FwdIteratorIR(*this); } llvm::Value * FwdIteratorIR::begin(){ TypesHelper helper(__compiler.__context.pass->man->llvm); llvm::IntegerType* intT = helper.getPreferredIntTy(); return llvm::ConstantInt::get(intT, 0); } llvm::Value * FwdIteratorIR::end(){ TypesHelper helper(__compiler.__context.pass->man->llvm); llvm::IntegerType* intT = helper.getPreferredIntTy(); - size_t size = __compiler.__hints.size; + size_t size = __compiler.__hint.size; return llvm::ConstantInt::get(intT, size); } llvm::Value * FwdIteratorIR::get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintAlias){ return __compiler.get(aggrRaw, {idxRaw}, hintAlias); } llvm::Value * FwdIteratorIR::advance(llvm::Value *idxRaw, const std::string &hintAlias){ LLVMLayer* llvm = __compiler.__context.pass->man->llvm; TypesHelper helper(llvm); llvm::IntegerType* intT = helper.getPreferredIntTy(); llvm::Value* cnstOneRaw = llvm::ConstantInt::get(intT, 1); return llvm->irBuilder.CreateAdd(idxRaw, cnstOneRaw, hintAlias); } }} //xreate::containers \ No newline at end of file diff --git a/cpp/src/compilation/containers/arrays.h b/cpp/src/compilation/containers/arrays.h index 8951d9d..5b378be 100644 --- a/cpp/src/compilation/containers/arrays.h +++ b/cpp/src/compilation/containers/arrays.h @@ -1,63 +1,63 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: arrays.h * Author: pgess * * Created in March 2020. */ -ifndef XREATE_ARRAYSIR_H +#ifndef XREATE_ARRAYSIR_H #define XREATE_ARRAYSIR_H #include "compilation/containers.h" namespace xreate { namespace containers{ class IFwdIteratorIR; /** \brief The contiguous container implementation. * * Represents contiguous in memory(array) implementation. * \sa xreate::containers::IFwdIteratorIR, \sa xreate::containers::Query */ class ArrayIR : public IContainersIR{ friend class FwdIteratorIR; public: ArrayIR(const ExpandedType &aggrT, const typehints::ArrayHint &hints, const compilation::Context &context) - : __context(context), __aggrT(aggrT), __hints(hints){} + : __context(context), __aggrT(aggrT), __hint(hints){} virtual llvm::Value *init(const std::string &hintAlias = "") override; virtual llvm::Value *update(llvm::Value *aggrRaw, const Expression &updE, const std::string &hintAlias) override; virtual IFwdIteratorIR* getFwdIterator() override; llvm::Value *get(llvm::Value *aggrRaw, std::vector idxL, const std::string &hintAlias); - llvm::ArrayType *getRawT(); + static llvm::PointerType *getRawType(const ExpandedType& aggrT, const typehints::ArrayHint& hint, LLVMLayer* llvm); /** \brief `loop map` statement compilation*/ llvm::Value* operatorMap(const Expression& expr, const std::string& hintAlias); private: compilation::Context __context; ExpandedType __aggrT; - typehints::ArrayHint __hints; + typehints::ArrayHint __hint; }; template<> class FwdIteratorIR: public IFwdIteratorIR { public: FwdIteratorIR(const ArrayIR& arraysIR): __compiler(arraysIR) {}; virtual llvm::Value* begin() override; virtual llvm::Value* end() override; virtual llvm::Value* get(llvm::Value* aggrRaw, llvm::Value *idxRaw, const std::string &hintAlias="") override; virtual llvm::Value* advance(llvm::Value* idxRaw, const std::string& hintAlias="") override; private: ArrayIR __compiler; }; }} // xreate::containers #endif //XREATE_ARRAYSIR_H diff --git a/cpp/src/compilation/decorators.h b/cpp/src/compilation/decorators.h index 87d4934..422d7cd 100644 --- a/cpp/src/compilation/decorators.h +++ b/cpp/src/compilation/decorators.h @@ -1,237 +1,237 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: scopedecorators.h * Author: pgess * * Created on February 24, 2017, 11:35 AM */ /** * \file scopedecorators.h * \brief Basic code block compilation xreate::compilation::IBruteScope decorators */ #ifndef SCOPEDECORATORS_H #define SCOPEDECORATORS_H #include "ast.h" #include "compilation/transformations.h" #include "analysis/typeinference.h" #include "compilation/demand.h" #include "compilation/polymorph.h" #include "compilation/targetinterpretation.h" #ifndef XREATE_CONFIG_MIN #include "compilation/versions.h" #include "compilation/polymorph.h" #endif #include namespace xreate { class CompilePass; namespace compilation { class IBruteScope; class IBruteFunction; /**\brief Provides caching ability for code scope compilation * \extends xreate::compilation::IBruteScope */ template class CachedScopeDecorator: public Parent{ typedef CachedScopeDecorator SELF; public: CachedScopeDecorator(const CodeScope* const codeScope, IBruteFunction* f, CompilePass* compilePass): Parent(codeScope, f, compilePass){} Symbol bindArg(llvm::Value* value, std::string&& alias) { //ensure existence of an alias assert(Parent::scope->__identifiers.count(alias)); //memorize new value for an alias ScopedSymbol id{Parent::scope->__identifiers.at(alias), versions::VERSION_NONE}; __rawVars[id] = value; return Symbol{id, Parent::scope}; } void bindArg(llvm::Value* value, const ScopedSymbol& s) { __rawVars[s] = value; } llvm::Value* compile(const std::string& aliasBlock="") override{ if (__rawVars.count(ScopedSymbol::RetSymbol)){ return __rawVars[ScopedSymbol::RetSymbol]; } return Parent::compile(aliasBlock); } llvm::Value* processSymbol(const Symbol& s, std::string hintRetVar) override{ const CodeScope* scope = s.scope; SELF* self = dynamic_cast(Parent::function->getScopeUnit(scope)); if (self->__rawVars.count(s.identifier)){ return self->__rawVars[s.identifier]; } //Declaration could be overriden /* Expression declaration = CodeScope::getDefinition(s, true); if (!declaration.isDefined()){ assert(__declarationsOverriden.count(s.identifier)); declaration = __declarationsOverriden[s.identifier]; } else { (false); //in case of binding there should be raws provided. } } */ llvm::Value* resultRaw = Parent::processSymbol(s, hintRetVar); self->__rawVars.emplace(s.identifier, resultRaw); return resultRaw; } void overrideDeclarations(std::list> bindings){ reset(); for (auto entry: bindings){ SELF* self = dynamic_cast(Parent::function->getScopeUnit(entry.first.scope)); assert(self == this); self->__declarationsOverriden.emplace(entry.first.identifier, entry.second); } } void registerChildScope(std::shared_ptr scope){ __childScopes.push_back(scope); } void reset(){ __rawVars.clear(); __declarationsOverriden.clear(); __childScopes.clear(); } private: std::unordered_map __declarationsOverriden; std::unordered_map __rawVars; std::list> __childScopes; }; /** \brief Provides automatic type conversion * \extends xreate::compilation::IBruteScope */ template class TypeConversionScopeDecorator: public Parent { public: TypeConversionScopeDecorator(const CodeScope* const codeScope, IBruteFunction* f, CompilePass* compilePass): Parent(codeScope, f, compilePass){} llvm::Value* process(const Expression& expr, const std::string& hintVarDecl="", const TypeAnnotation& expectedT = TypeAnnotation()) override { llvm::Value* resultR = Parent::process(expr, hintVarDecl, expectedT); if(!expr.type.isValid()) { return resultR; } ExpandedType exprT = Parent::pass->man->root->getType(expr); - llvm::Type* exprTR = Parent::pass->man->llvm->toLLVMType(exprT); + llvm::Type* exprTR = Parent::pass->man->llvm->toLLVMType(exprT, expr); return typeinference::doAutomaticTypeConversion(resultR, exprTR, Parent::pass->man->llvm->irBuilder); } }; #ifndef XREATE_CONFIG_MIN /**\brief The default code scope compilation implementation * \extends xreate::compilation::IBruteScope */ typedef CachedScopeDecorator< TypeConversionScopeDecorator< latex::LatexBruteScopeDecorator< polymorph::PolymorphBruteScopeDecorator< compilation::TransformationsScopeDecorator< interpretation::InterpretationScopeDecorator< versions::VersionsScopeDecorator< compilation::BasicBruteScope >>>>>>> DefaultCodeScopeUnit; } //end of compilation namespace struct CachedScopeDecoratorTag; struct VersionsScopeDecoratorTag; template<> struct DecoratorsDict{ typedef compilation::CachedScopeDecorator< compilation::TypeConversionScopeDecorator< latex::LatexBruteScopeDecorator< polymorph::PolymorphBruteScopeDecorator< compilation::TransformationsScopeDecorator< interpretation::InterpretationScopeDecorator< versions::VersionsScopeDecorator< compilation::BasicBruteScope >>>>>>> result; }; template<> struct DecoratorsDict{ typedef versions::VersionsScopeDecorator< compilation::BasicBruteScope > result; }; #else /**\brief The default code scope compilation implementation * \extends xreate::compilation::IBruteScope */ typedef CachedScopeDecorator< TypeConversionScopeDecorator< interpretation::InterpretationScopeDecorator< demand::DemandBruteScopeDecorator< polymorph::PolymorphBruteScopeDecorator< compilation::BasicBruteScope >>>>> DefaultCodeScopeUnit; } //end of compilation namespacef struct CachedScopeDecoratorTag; template<> struct DecoratorsDict{ typedef compilation::CachedScopeDecorator< compilation::TypeConversionScopeDecorator< interpretation::InterpretationScopeDecorator< demand::DemandBruteScopeDecorator< polymorph::PolymorphBruteScopeDecorator< compilation::BasicBruteScope >>>>> result; }; typedef demand::DemandBruteFnDecorator< //polymorph::PolymorphBruteFnDecorator< compilation::BasicBruteFunction > BruteFunctionDefault; #endif } //end of xreate #endif /* SCOPEDECORATORS_H */ diff --git a/cpp/src/compilation/targetinterpretation.cpp b/cpp/src/compilation/targetinterpretation.cpp index 02d4b9d..6b9c084 100644 --- a/cpp/src/compilation/targetinterpretation.cpp +++ b/cpp/src/compilation/targetinterpretation.cpp @@ -1,648 +1,645 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * File: targetinterpretation.cpp * Author: pgess * * Created on June 29, 2016, 6:45 PM */ /** * \file targetinterpretation.h * \brief Interpretation support. See more details on [Interpretation](/d/concepts/interpretation/) */ #include "compilation/targetinterpretation.h" #include "pass/interpretationpass.h" #include "analysis/typeinference.h" #include "llvmlayer.h" #include "compilation/decorators.h" #include "compilation/i12ninst.h" #include "compilation/intrinsics.h" #include #include #include using namespace std; using namespace xreate::compilation; namespace xreate{ namespace interpretation{ const Expression EXPRESSION_FALSE = Expression(Atom(0)); const Expression EXPRESSION_TRUE = Expression(Atom(1)); CodeScope* InterpretationScope::processOperatorIf(const Expression& expression) { const Expression& exprCondition = process(expression.getOperands()[0]); if (exprCondition == EXPRESSION_TRUE) { return expression.blocks.front(); } return expression.blocks.back(); } CodeScope* InterpretationScope::processOperatorSwitch(const Expression& expression) { const Expression& exprCondition = process(expression.operands[0]); bool flagHasDefault = expression.operands[1].op == Operator::CASE_DEFAULT; //TODO check that one and only one case variant is appropriate for (size_t size = expression.operands.size(), i = flagHasDefault ? 2 : 1; i < size; ++i) { const Expression& exprCase = process(expression.operands[i]); if (function->getScope((const CodeScope*) exprCase.blocks.front())->processScope() == exprCondition) { return exprCase.blocks.back(); } } if (flagHasDefault) { const Expression& exprCaseDefault = expression.operands[1]; return exprCaseDefault.blocks.front(); } assert(false && "Switch has no appropriate variant"); return nullptr; } CodeScope* InterpretationScope::processOperatorSwitchVariant(const Expression& expression) { const ExpandedType& conditionT = function->__pass->man->root->getType(expression.operands.at(0)); const Expression& conditionE = process(expression.operands.at(0)); assert(conditionE.op == Operator::VARIANT); const string& aliasS = expression.bindings.front(); unsigned caseExpectedId = (int) conditionE.getValueDouble(); auto itFoundValue = std::find_if(++expression.operands.begin(), expression.operands.end(), [caseExpectedId](const auto& caseActualE){ return (unsigned) caseActualE.getValueDouble() == caseExpectedId; }); assert(itFoundValue != expression.operands.end()); int caseScopeId = itFoundValue - expression.operands.begin() - 1; auto caseScopeRef = expression.blocks.begin(); std::advance(caseScopeRef, caseScopeId); InterpretationScope* scopeI12n = function->getScope(*caseScopeRef); if(conditionE.operands.size()) { Expression valueE(Operator::LIST, {}); valueE.operands = conditionE.operands; valueE.bindings = conditionT->__operands.at(caseExpectedId).fields; scopeI12n->overrideBindings({ {valueE, aliasS} }); }; return *caseScopeRef; } llvm::Value* InterpretationScope::processLate(const InterpretationOperator& op, const Expression& expression, const Context& context, const std::string& hintAlias) { switch(op) { case IF_INTERPRET_CONDITION: { CodeScope* scopeResult = processOperatorIf(expression); llvm::Value* result = context.function->getScopeUnit(scopeResult)->compile(); return result; } case SWITCH_INTERPRET_CONDITION: { CodeScope* scopeResult = processOperatorSwitch(expression); llvm::Value* result = context.function->getScopeUnit(scopeResult)->compile(); return result; } case SWITCH_VARIANT: { CodeScope* scopeResult = processOperatorSwitchVariant(expression); const Expression& condCrudeE = expression.operands.at(0); const Expression& condE = process(condCrudeE); const string identCondition = expression.bindings.front(); auto scopeCompilation = Decorators::getInterface(context.function->getScopeUnit(scopeResult)); if(condE.operands.size()) { //override value Symbol symbCondition{ScopedSymbol{scopeResult->__identifiers.at(identCondition), versions::VERSION_NONE}, scopeResult}; scopeCompilation->overrideDeclarations({ {symbCondition, Expression(condE.operands.at(0))}} ); //set correct type for binding: const ExpandedType& typeVariant = function->__pass->man->root->getType(condCrudeE); int conditionIndex = condE.getValueDouble(); ScopedSymbol symbolInternal = scopeResult->getSymbol(identCondition); scopeResult->__declarations[symbolInternal].bindType(typeVariant->__operands.at(conditionIndex)); } llvm::Value* result = context.function->getScopeUnit(scopeResult)->compile(); return result; } case SWITCH_LATE: { return nullptr; // latereasoning::LateReasoningCompiler compiler(dynamic_cast(this->function), context); // return compiler.processSwitchLateStatement(expression, ""); } case FOLD_INTERPRET_INPUT: { //initialization const Expression& containerE = process(expression.getOperands().at(0)); const TypeAnnotation& accumT = expression.type; assert(containerE.op == Operator::LIST); CodeScope* bodyScope = expression.blocks.front(); const string& elAlias = expression.bindings[0]; Symbol elS{ScopedSymbol{bodyScope->__identifiers.at(elAlias), versions::VERSION_NONE}, bodyScope}; const std::string& accumAlias = expression.bindings[1]; llvm::Value* accumRaw = context.scope->process(expression.getOperands().at(1), accumAlias, accumT); InterpretationScope* bodyI12n = function->getScope(bodyScope); auto bodyBrute = Decorators::getInterface(context.function->getScopeUnit(bodyScope)); const std::vector& containerVec = containerE.getOperands(); for(size_t i = 0; i < containerVec.size(); ++i) { const Expression& elE = containerVec[i]; bodyI12n->overrideBindings({ {elE, elAlias} }); bodyBrute->overrideDeclarations({ {elS, elE} }); //resets bodyBrute bodyBrute->bindArg(accumRaw, string(accumAlias)); - accumRaw = bodyBrute->compile(); - accumRaw->getType()->print(llvm::outs()); - llvm::outs() << "\n\n"; } return accumRaw; } // case FOLD_INF_INTERPRET_INOUT: // { // } //TODO refactor as InterpretationCallStatement class case CALL_INTERPRET_PARTIAL: { const std::string &calleeName = expression.getValueString(); IBruteScope* scopeUnitSelf = context.scope; ManagedFnPtr callee = this->function->__pass->man->root->findFunction(calleeName); const I12nFunctionSpec& calleeData = FunctionInterpretationHelper::getSignature(callee); std::vector argsActual; PIFnSignature sig; sig.declaration = callee; for(size_t no = 0, size = expression.operands.size(); no < size; ++no) { const Expression& op = expression.operands[no]; if (calleeData.signature.at(no) == INTR_ONLY) { sig.bindings.push_back(process(op)); continue; } argsActual.push_back(scopeUnitSelf->process(op)); } TargetInterpretation* man = dynamic_cast (this->function->__pass); PIFunction* pifunction = man->getFunction(move(sig)); llvm::Function* raw = pifunction->compile(); boost::scoped_ptr statement(new BruteFnInvocation(raw, man->pass->man->llvm)); return (*statement)(move(argsActual)); } case QUERY_LATE: { return nullptr; // return IntrinsicQueryInstruction( // dynamic_cast(this->function)) // .processLate(expression, context); } default: break; } assert(false && "Unknown late interpretation operator"); return nullptr; } llvm::Value* InterpretationScope::compile(const Expression& expression, const Context& context, const std::string& hintAlias) { const InterpretationData& data = Attachments::get(expression); if (data.op != InterpretationOperator::NONE) { return processLate(data.op, expression, context, hintAlias); } Expression result = process(expression); return context.scope->process(result, hintAlias); } Expression InterpretationScope::process(const Expression& expression) { #ifndef NDEBUG if (expression.tags.count("bpoint")) { std::raise(SIGINT); } #endif PassManager* man = function->__pass->man; switch (expression.__state) { case Expression::INVALID: assert(false); case Expression::NUMBER: case Expression::STRING: return expression; case Expression::IDENT: { Symbol s = Attachments::get(expression); return Parent::processSymbol(s); } case Expression::COMPOUND: break; default: assert(false); } switch (expression.op) { case Operator::EQU: { const Expression& left = process(expression.operands[0]); const Expression& right = process(expression.operands[1]); if (left == right) return EXPRESSION_TRUE; return EXPRESSION_FALSE; } case Operator::NE: { const Expression& left = process(expression.operands[0]); const Expression& right = process(expression.operands[1]); if (left == right) return EXPRESSION_FALSE; return EXPRESSION_TRUE; } case Operator::LOGIC_AND: { assert(expression.operands.size() == 1); return process (expression.operands[0]); } // case Operator::LOGIC_OR: case Operator::CALL: { const std::string &fnName = expression.getValueString(); ManagedFnPtr fnAst = man->root->findFunction(fnName); InterpretationFunction* fnUnit = this->function->__pass->getFunction(fnAst); vector args; args.reserve(expression.getOperands().size()); for(size_t i = 0, size = expression.getOperands().size(); i < size; ++i) { args.push_back(process(expression.getOperands()[i])); } return fnUnit->process(args); } case Operator::CALL_INTRINSIC: { const Expression& opCallIntrCrude = expression; vector argsActual; argsActual.reserve(opCallIntrCrude.getOperands().size()); for(const auto& op: opCallIntrCrude.getOperands()) { argsActual.push_back(process(op)); } Expression opCallIntr(Operator::CALL_INTRINSIC, {}); opCallIntr.setValueDouble(opCallIntrCrude.getValueDouble()); opCallIntr.operands = argsActual; compilation::IntrinsicCompiler compiler(man); return compiler.interpret(opCallIntr); } case Operator::QUERY: { return Expression(); // return IntrinsicQueryInstruction(dynamic_cast(this->function)) // .process(expression); } case Operator::QUERY_LATE: { assert(false && "Can't be interpretated"); return Expression(); } case Operator::IF: { CodeScope* scopeResult = processOperatorIf(expression); return function->getScope(scopeResult)->processScope(); } case Operator::SWITCH: { CodeScope* scopeResult = processOperatorSwitch(expression); return function->getScope(scopeResult)->processScope(); } case Operator::SWITCH_VARIANT: { CodeScope* scopeResult = processOperatorSwitchVariant(expression); return function->getScope(scopeResult)->processScope(); } case Operator::VARIANT: { Expression result{Operator::VARIANT, {}}; result.setValueDouble(expression.getValueDouble()); for(const Expression& op: expression.operands){ result.operands.push_back(process(op)); } return result; } case Operator::INDEX: { Expression aggrE = process(expression.operands[0]); for (size_t keyId = 1; keyId < expression.operands.size(); ++keyId) { const Expression& keyE = process(expression.operands[keyId]); if (keyE.__state == Expression::STRING) { const string& fieldExpectedS = keyE.getValueString(); unsigned fieldId; for(fieldId = 0; fieldId < aggrE.bindings.size(); ++fieldId){ if (aggrE.bindings.at(fieldId) == fieldExpectedS){break;} } assert(fieldId < aggrE.bindings.size()); aggrE = Expression(aggrE.operands.at(fieldId)); continue; } if (keyE.__state == Expression::NUMBER) { int opId = keyE.getValueDouble(); aggrE = Expression(aggrE.operands.at(opId)); continue; } assert(false && "Inappropriate key"); } return aggrE; } case Operator::FOLD: { const Expression& exprInput = process(expression.getOperands()[0]); const Expression& exprInit = process(expression.getOperands()[1]); const std::string& argEl = expression.bindings[0]; const std::string& argAccum = expression.bindings[1]; InterpretationScope* body = function->getScope(expression.blocks.front()); Expression accum = exprInit; for(size_t size = exprInput.getOperands().size(), i = 0; i < size; ++i) { body->overrideBindings({ {exprInput.getOperands()[i], argEl}, {accum, argAccum} }); accum = body->processScope(); } return accum; } case Operator::LIST: case Operator::LIST_RANGE: { Expression result(expression.op,{}); result.operands.resize(expression.operands.size()); result.bindings = expression.bindings; int keyId = 0; for(const Expression& opCurrent : expression.operands) { result.operands[keyId++] = process(opCurrent); } return result; } // case Operator::MAP: { // break; // } default: break; } return expression; } InterpretationFunction* TargetInterpretation::getFunction(IBruteFunction* unit) { if (__dictFunctionsByUnit.count(unit)) { return __dictFunctionsByUnit.at(unit); } InterpretationFunction* f = new InterpretationFunction(unit->getASTFn(), this); __dictFunctionsByUnit.emplace(unit, f); assert(__functions.emplace(unit->getASTFn().id(), f).second); return f; } PIFunction* TargetInterpretation::getFunction(PIFnSignature&& sig) { auto f = __pifunctions.find(sig); if (f != __pifunctions.end()) { return f->second; } PIFunction* result = new PIFunction(PIFnSignature(sig), __pifunctions.size(), this); __pifunctions.emplace(move(sig), result); assert(__dictFunctionsByUnit.emplace(result->fnRaw, result).second); return result; } InterpretationScope* TargetInterpretation::transformContext(const Context& c) { return this->getFunction(c.function)->getScope(c.scope->scope); } llvm::Value* TargetInterpretation::compile(const Expression& expression, const Context& ctx, const std::string& hintAlias) { return transformContext(ctx)->compile(expression, ctx, hintAlias); } InterpretationFunction::InterpretationFunction(const ManagedFnPtr& function, Target* target) : Function(function, target) { } Expression InterpretationFunction::process(const std::vector& args) { InterpretationScope* body = getScope(__function->__entry); list> bindings; for(size_t i = 0, size = args.size(); i < size; ++i) { bindings.push_back(make_pair(args.at(i), body->scope->__bindings.at(i))); } body->overrideBindings(bindings); return body->processScope(); } // Partial function interpretation typedef BasicBruteFunction BruteFunction; class PIBruteFunction : public BruteFunction{ public: PIBruteFunction(ManagedFnPtr f, std::set&& arguments, size_t id, CompilePass* p) : BruteFunction(f, p), argumentsActual(move(arguments)), __id(id) { } protected: std::vector prepareSignature() override { LLVMLayer* llvm = BruteFunction::pass->man->llvm; AST* ast = BruteFunction::pass->man->root; CodeScope* entry = IBruteFunction::__entry; std::vector signature; for(size_t no : argumentsActual) { VNameId argId = entry->__identifiers.at(entry->__bindings.at(no)); ScopedSymbol arg{argId, versions::VERSION_NONE}; signature.push_back(llvm->toLLVMType(ast->expandType(entry->__declarations.at(arg).type))); } return signature; } llvm::Function::arg_iterator prepareBindings() override{ CodeScope* entry = IBruteFunction::__entry; IBruteScope* entryCompilation = BruteFunction::getScopeUnit(entry); llvm::Function::arg_iterator fargsI = BruteFunction::raw->arg_begin(); for(size_t no : argumentsActual) { ScopedSymbol arg{entry->__identifiers.at(entry->__bindings.at(no)), versions::VERSION_NONE}; entryCompilation->bindArg(&*fargsI, arg); fargsI->setName(entry->__bindings.at(no)); ++fargsI; } return fargsI; } virtual std::string prepareName() override { return BruteFunction::prepareName() + "_" + std::to_string(__id); } private: std::set argumentsActual; size_t __id; } ; PIFunction::PIFunction(PIFnSignature&& sig, size_t id, TargetInterpretation* target) : InterpretationFunction(sig.declaration, target), instance(move(sig)) { const I12nFunctionSpec& functionData = FunctionInterpretationHelper::getSignature(instance.declaration); std::set argumentsActual; for (size_t no = 0, size = functionData.signature.size(); no < size; ++no) { if (functionData.signature.at(no) != INTR_ONLY) { argumentsActual.insert(no); } } fnRaw = new PIBruteFunction(instance.declaration, move(argumentsActual), id, target->pass); CodeScope* entry = instance.declaration->__entry; auto entryUnit = Decorators::getInterface<>(fnRaw->getEntry()); InterpretationScope* entryIntrp = InterpretationFunction::getScope(entry); list> bindingsPartial; list> declsPartial; for(size_t no = 0, sigNo = 0, size = entry->__bindings.size(); no < size; ++no) { if(functionData.signature.at(no) == INTR_ONLY) { bindingsPartial.push_back({instance.bindings[sigNo], entry->__bindings[no]}); VNameId argId = entry->__identifiers.at(entry->__bindings[no]); Symbol argSymbol{ScopedSymbol {argId, versions::VERSION_NONE}, entry}; declsPartial.push_back({argSymbol, instance.bindings[sigNo]}); ++sigNo; } } entryIntrp->overrideBindings(bindingsPartial); entryUnit->overrideDeclarations(declsPartial); } llvm::Function* PIFunction::compile() { llvm::Function* raw = fnRaw->compile(); return raw; } bool operator<(const PIFnSignature& lhs, const PIFnSignature& rhs) { if (lhs.declaration.id() != rhs.declaration.id()) { return lhs.declaration.id() < rhs.declaration.id(); } return lhs.bindings < rhs.bindings; } bool operator<(const PIFnSignature& lhs, PIFunction * const rhs) { return lhs < rhs->instance; } bool operator<(PIFunction * const lhs, const PIFnSignature& rhs) { return lhs->instance < rhs; } } } /** \class xreate::interpretation::InterpretationFunction * * Holds list of xreate::interpretation::InterpretationScope 's focused on interpretation of individual code scopes * * There is particulat subclass PIFunction intended to represent partially interpreted functions. *\sa TargetInterpretation, [Interpretation Concept](/d/concepts/interpretation/) */ /** \class xreate::interpretation::TargetInterpretation * * TargetInterpretation is executed during compilation and is intended to preprocess eligible for interpretation parts of a source code. * * Keeps a list of InterpretationFunction / PIFunction that represent interpretation for an individual functions. * * There is \ref InterpretationScopeDecorator that embeds interpretation to an overall compilation process. * \sa InterpretationPass, compilation::Target, [Interpretation Concept](/d/concepts/interpretation/) * */ diff --git a/cpp/src/llvmlayer.cpp b/cpp/src/llvmlayer.cpp index ac179d8..1474b50 100644 --- a/cpp/src/llvmlayer.cpp +++ b/cpp/src/llvmlayer.cpp @@ -1,283 +1,295 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * llvmlayer.cpp * * Author: pgess */ /** * \file llvmlayer.h * \brief Bytecode generation */ #include "ast.h" #include "llvmlayer.h" #include "analysis/typehints.h" #ifdef XREATE_ENABLE_EXTERN #include "ExternLayer.h" #endif +#include +#include "llvm/IR/Module.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/PassManager.h" +#include "llvm/IR/CallingConv.h" +#include "llvm/IR/Verifier.h" +#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/MCJIT.h" #include "llvm/Support/TargetSelect.h" #include #include + using namespace llvm; using namespace xreate; using namespace xreate::typehints; using namespace std; LLVMLayer::LLVMLayer(AST *root) : llvmContext(), irBuilder(llvmContext), ast(root), module(new llvm::Module(root->getModuleName(), llvmContext)){ llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); llvm::EngineBuilder builder; TargetMachine *target = builder.selectTarget(); module->setDataLayout(target->createDataLayout()); #ifdef XREATE_ENABLE_EXTERN layerExtern = new ExternLayer(this); layerExtern->init(root); #endif } void * LLVMLayer::getFunctionPointer(llvm::Function *function){ uint64_t entryAddr = jit->getFunctionAddress(function->getName().str()); return (void *) entryAddr; } void LLVMLayer::initJit(){ std::string ErrStr; llvm::EngineBuilder builder(std::unique_ptr(module.release())); jit.reset(builder .setEngineKind(llvm::EngineKind::JIT) + .setTargetOptions(optsTarget) + .setOptLevel(optsLevel) .setErrorStr(&ErrStr) .setVerifyModules(true) .create() ); } void LLVMLayer::print(){ llvm::PassManager PM; PM.addPass(llvm::PrintModulePass(llvm::outs(), "banner")); llvm::AnalysisManager aman; PM.run(*module.get(), aman); } void LLVMLayer::moveToGarbage(void *o){ __garbage.push_back(o); } llvm::Type* -LLVMLayer::toLLVMType(const ExpandedType &ty) const{ +LLVMLayer::toLLVMType(const ExpandedType &ty, const Expression& expr){ TypeAnnotation t = ty.get(); switch(t.__operator){ case TypeOperator::ARRAY:{ - //see ArrayIR::getRawT() - return nullptr; + if (expr.tags.size() == 0) return nullptr; //TODO shouldn't be invalid + + return containers::IContainersIR::getRawType(expr, ty, this); } case TypeOperator::RECORD:{ std::vector packVec; packVec.reserve(t.__operands.size()); std::transform(t.__operands.begin(), t.__operands.end(), std::inserter(packVec, packVec.end()), [this](const TypeAnnotation &t){ return toLLVMType(ExpandedType(TypeAnnotation(t))); }); llvm::ArrayRef packArr(packVec); return llvm::StructType::get(llvmContext, packArr, false); }; case TypeOperator::REF:{ TypeAnnotation tyRef = t.__operands.at(0); assert(tyRef.__operator == TypeOperator::ALIAS); llvm::StructType *tyOpaqRaw = llvm::StructType::create(llvmContext, tyRef.__valueCustom); llvm::PointerType *tyRefRaw = llvm::PointerType::get(tyOpaqRaw, 0); return tyRefRaw; }; case TypeOperator::ALIAS:{ #ifdef XREATE_ENABLE_EXTERN //Look in extern types clang::QualType qt = layerExtern->lookupType(t.__valueCustom); return layerExtern->toLLVMType(qt); #else assert(false); #endif }; //DEBT omit ID field in case of single variant. case TypeOperator::VARIANT:{ /* Variant Type Layout: * { * id :: i8, Holds stored variant id * storage:: type of biggest variant * } */ uint64_t sizeStorage = 0; llvm::Type *typStorageRaw = llvm::Type::getVoidTy(llvmContext); for(const TypeAnnotation &subtype : t.__operands){ llvm::Type *subtypeRaw = toLLVMType(ExpandedType(subtype)); if(subtypeRaw->isVoidTy()) continue; uint64_t sizeSubtype = module->getDataLayout().getTypeStoreSize(subtypeRaw); if(sizeSubtype > sizeStorage){ sizeStorage = sizeSubtype; typStorageRaw = subtypeRaw; } } std::vector layout; layout.push_back(llvm::Type::getInt8Ty(llvmContext)); //id const bool flagHoldsData = sizeStorage > 0; if(flagHoldsData){ layout.push_back(typStorageRaw); //storage } return llvm::StructType::get(llvmContext, llvm::ArrayRef(layout)); } case TypeOperator::NONE:{ switch(t.__value){ case TypePrimitive::Bool: return llvm::Type::getInt1Ty(llvmContext); case TypePrimitive::I8: return llvm::Type::getInt8Ty(llvmContext); case TypePrimitive::I32: return llvm::Type::getInt32Ty(llvmContext); case TypePrimitive::I64: return llvm::Type::getInt64Ty(llvmContext); case TypePrimitive::Int: { // IntBits hintSize; // if (existsSize(hintSize)){ // return llvm::IntegerType::getIntNTy(llvmContext, hintSize.n); // } TypesHelper helper(this); return helper.getPreferredIntTy(); } case TypePrimitive::Float: return llvm::Type::getDoubleTy(llvmContext); case TypePrimitive::String: return llvm::Type::getInt8PtrTy(llvmContext); case TypePrimitive::Invalid: return llvm::Type::getVoidTy(llvmContext); default: assert(false); } } default: assert(false); } assert(false); return nullptr; } bool TypesHelper::isRecordT(const ExpandedType &ty){ const TypeAnnotation &t = ty.get(); if(t.__operator == TypeOperator::RECORD){ return true; } if(t.__operator != TypeOperator::ALIAS){ return false; } #ifdef XREATE_ENABLE_EXTERN clang::QualType tqual = llvm->layerExtern->lookupType(t.__valueCustom); const clang::Type * raw = tqual.getTypePtr(); // TODO skip ALL the pointers until non-pointer type found if (raw->isStructureType()) return true; if (!raw->isAnyPointerType()) return false; clang::QualType pointee = raw->getPointeeType(); return pointee->isStructureType(); #else assert(false); return false; #endif } bool TypesHelper::isArrayT(const Expanded& ty){ const TypeAnnotation &t = ty.get(); if(t.__operator == TypeOperator::ARRAY){ return true; } return false; } bool TypesHelper::isPointerT(const ExpandedType &ty){ if(ty.get().__operator != TypeOperator::ALIAS) return false; #ifdef XREATE_ENABLE_EXTERN clang::QualType qt = llvm->layerExtern->lookupType(ty.get().__valueCustom); return llvm->layerExtern->isPointer(qt); #else assert(false); return false; #endif } bool TypesHelper::isIntegerT(const Expanded& ty){ return (ty->__operator == TypeOperator::NONE) && ((ty->__value == TypePrimitive::Bool) || (ty->__value == TypePrimitive::I8) || (ty->__value == TypePrimitive::I32) || (ty->__value == TypePrimitive::I64) || (ty->__value == TypePrimitive::Int)); } std::vector TypesHelper::getRecordFields(const ExpandedType &t){ #ifdef XREATE_ENABLE_EXTERN return (t.get().__operator == TypeOperator::RECORD) ? t.get().fields : llvm->layerExtern->getStructFields( llvm->layerExtern->lookupType(t.get().__valueCustom)); #else assert(t.get().__operator == TypeOperator::RECORD); return t.get().fields; #endif } llvm::IntegerType * TypesHelper::getPreferredIntTy() const{ unsigned sizePreferred = llvm->module->getDataLayout().getLargestLegalIntTypeSizeInBits(); return llvm::IntegerType::getIntNTy(llvm->llvmContext, sizePreferred); } \ No newline at end of file diff --git a/cpp/src/llvmlayer.h b/cpp/src/llvmlayer.h index 410cb25..7ea03b3 100644 --- a/cpp/src/llvmlayer.h +++ b/cpp/src/llvmlayer.h @@ -1,71 +1,70 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * llvmlayer.h * * Author: pgess */ #ifndef LLVMLAYER_H #define LLVMLAYER_H -#include "llvm/IR/Module.h" -#include "llvm/IR/Function.h" -#include "llvm/IR/PassManager.h" -#include "llvm/IR/CallingConv.h" -#include "llvm/IR/Verifier.h" -#include "llvm/IR/IRPrintingPasses.h" -#include "llvm/IR/IRBuilder.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/IR/LLVMContext.h" -#include "llvm/ExecutionEngine/ExecutionEngine.h" +#include "ast.h" #include "utils.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/Target/TargetOptions.h" + +namespace llvm { + class ExecutionEngine; +} + namespace xreate { -class AST; class ExternLayer; -class TypeAnnotation; /** \brief A wrapper over LLVM toolchain to generate and execute bytecode */ class LLVMLayer { public: LLVMLayer(AST* rootAST); mutable llvm::LLVMContext llvmContext; llvm::IRBuilder<> irBuilder; AST *ast = 0; ExternLayer *layerExtern =0; std::unique_ptr module; std::unique_ptr jit; + llvm::TargetOptions optsTarget; + llvm::CodeGenOpt::Level optsLevel = llvm::CodeGenOpt::None; void moveToGarbage(void *o); - llvm::Type* toLLVMType(const Expanded& ty) const; + llvm::Type* toLLVMType(const Expanded& ty, const Expression& expr = Expression()); void print(); void* getFunctionPointer(llvm::Function* function); void initJit(); private: llvm::Type* toLLVMType(const Expanded& ty, std::map& conjunctions) const; std::vector __garbage; }; class TypesHelper { public: bool isArrayT(const Expanded& ty); bool isRecordT(const Expanded& ty); bool isPointerT(const Expanded& ty); bool isIntegerT(const Expanded& ty); llvm::IntegerType* getPreferredIntTy() const; std::vector getRecordFields(const Expanded& t); TypesHelper(const LLVMLayer* llvmlayer): llvm(llvmlayer){} private: const LLVMLayer* llvm; }; } #endif // LLVMLAYER_H diff --git a/cpp/src/pass/compilepass.cpp b/cpp/src/pass/compilepass.cpp index e3a6fc4..012b38c 100644 --- a/cpp/src/pass/compilepass.cpp +++ b/cpp/src/pass/compilepass.cpp @@ -1,879 +1,886 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Author: pgess * * compilepass.cpp */ /** * \file compilepass.h * \brief Main compilation routine. See \ref xreate::CompilePass */ #include "compilepass.h" #include "transcendlayer.h" #include "ast.h" #include "llvmlayer.h" #include "compilation/decorators.h" #include "compilation/pointers.h" #include "analysis/typeinference.h" #include "compilation/control.h" #include "compilation/demand.h" #include "analysis/resources.h" #ifdef XREATE_ENABLE_EXTERN #include "ExternLayer.h" #endif #include "compilation/containers.h" #include "compilation/containers/arrays.h" #ifndef XREATE_CONFIG_MIN #include "query/containers.h" #include "pass/versionspass.h" #include "compilation/targetinterpretation.h" #endif #include #include using namespace std; using namespace llvm; using namespace xreate::typehints; using namespace xreate::containers; namespace xreate{ namespace compilation{ #define DEFAULT(x) (hintAlias.empty()? x: hintAlias) std::string BasicBruteFunction::prepareName() { AST* ast = IBruteFunction::pass->man->root; string name = ast->getFnSpecializations(__function->__name).size() > 1 ? __function->__name + std::to_string(__function.id()) : __function->__name; return name; } std::vector BasicBruteFunction::prepareSignature() { CodeScope* entry = __function->__entry; return getScopeSignature(entry); } llvm::Type* BasicBruteFunction::prepareResult() { LLVMLayer* llvm = IBruteFunction::pass->man->llvm; AST* ast = IBruteFunction::pass->man->root; CodeScope* entry = __function->__entry; return llvm->toLLVMType(ast->expandType(entry->__declarations.at(ScopedSymbol::RetSymbol).type)); } llvm::Function::arg_iterator BasicBruteFunction::prepareBindings() { CodeScope* entry = __function->__entry; IBruteScope* entryCompilation = IBruteFunction::getScopeUnit(entry); llvm::Function::arg_iterator fargsI = IBruteFunction::raw->arg_begin(); for (std::string &arg : entry->__bindings) { ScopedSymbol argid{entry->__identifiers[arg], versions::VERSION_NONE}; entryCompilation->bindArg(&*fargsI, argid); fargsI->setName(arg); ++fargsI; } return fargsI; } void BasicBruteFunction::applyAttributes(){} IBruteScope::IBruteScope(const CodeScope * const codeScope, IBruteFunction* f, CompilePass* compilePass) : pass(compilePass), function(f), scope(codeScope), lastBlockRaw(nullptr) { } llvm::Value* BruteFnInvocation::operator()(std::vector&& args, const std::string& hintDecl) { if (__calleeTy) { auto argsFormalT = __calleeTy->params(); size_t sizeArgsF = __calleeTy->getNumParams(); assert(args.size() >= sizeArgsF); assert(__calleeTy->isVarArg() || args.size() == sizeArgsF); auto argFormalT = argsFormalT.begin(); for(size_t argId = 0; argId < args.size(); ++argId){ if(argFormalT != argsFormalT.end()){ args[argId] = typeinference::doAutomaticTypeConversion( args.at(argId), *argFormalT, llvm->irBuilder); ++argFormalT; } } } //Do not name function call that returns Void. std::string hintName = (!__calleeTy->getReturnType()->isVoidTy()) ? hintDecl : ""; return llvm->irBuilder.CreateCall(__calleeTy, __callee, args, hintName); } llvm::Value* HiddenArgsFnInvocation::operator() (std::vector&& args, const std::string& hintDecl) { args.insert(args.end(), __args.begin(), __args.end()); return __parent->operator ()(std::move(args), hintDecl); } class CallStatementInline : public IFnInvocation{ public: CallStatementInline(IBruteFunction* caller, IBruteFunction* callee, LLVMLayer* l) : __caller(caller), __callee(callee), llvm(l) { } llvm::Value* operator()(std::vector&& args, const std::string& hintDecl) { return nullptr; } private: IBruteFunction* __caller; IBruteFunction* __callee; LLVMLayer* llvm; bool isInline() { // Symbol ret = Symbol{0, function->__entry}; // bool flagOnTheFly = SymbolAttachments::get(ret, false); //TODO consider inlining return false; } } ; BasicBruteScope::BasicBruteScope(const CodeScope * const codeScope, IBruteFunction* f, CompilePass* compilePass) : IBruteScope(codeScope, f, compilePass) { } llvm::Value* BasicBruteScope::processSymbol(const Symbol& s, std::string hintRetVar) { Expression declaration = CodeScope::getDefinition(s); const CodeScope* scopeExternal = s.scope; IBruteScope* scopeBruteExternal = IBruteScope::function->getScopeUnit(scopeExternal); assert(scopeBruteExternal->lastBlockRaw); llvm::Value* resultRaw; llvm::BasicBlock* blockOwn = pass->man->llvm->irBuilder.GetInsertBlock(); if (scopeBruteExternal->lastBlockRaw == blockOwn) { resultRaw = scopeBruteExternal->process(declaration, hintRetVar); scopeBruteExternal->lastBlockRaw = lastBlockRaw = pass->man->llvm->irBuilder.GetInsertBlock(); } else { pass->man->llvm->irBuilder.SetInsertPoint(scopeBruteExternal->lastBlockRaw); resultRaw = scopeBruteExternal->processSymbol(s, hintRetVar); pass->man->llvm->irBuilder.SetInsertPoint(blockOwn); } return resultRaw; } IFnInvocation* BasicBruteScope::findFunction(const Expression& opCall) { const std::string& calleeName = opCall.getValueString(); LLVMLayer* llvm = pass->man->llvm; const std::list& specializations = pass->man->root->getFnSpecializations(calleeName); #ifdef XREATE_ENABLE_EXTERN //if no specializations registered - check external function if (specializations.size() == 0) { llvm::Function* external = llvm->layerExtern->lookupFunction(calleeName); llvm::outs() << "Debug/External function: " << calleeName; external->getType()->print(llvm::outs(), true); llvm::outs() << "\n"; return new BruteFnInvocation(external, llvm); } #endif //There should be only one specialization without any valid guards at this point return new BruteFnInvocation(pass->getFunctionUnit( pass->man->root->findFunction(calleeName))->compile(), llvm); } //DISABLEDFEATURE transformations // if (pass->transformations->isAcceptable(expr)){ // return pass->transformations->transform(expr, result, ctx); // } llvm::Value* BasicBruteScope::process(const Expression& expr, const std::string& hintAlias, const TypeAnnotation& expectedT) { llvm::Value *leftRaw; llvm::Value *rightRaw; LLVMLayer& l = *pass->man->llvm; Context ctx{this, function, pass}; xreate::compilation::ControlIR controlIR = xreate::compilation::ControlIR({this, function, pass}); switch (expr.op) { case Operator::ADD: case Operator::SUB: case Operator::MUL: case Operator::MOD: case Operator::DIV: case Operator::EQU: case Operator::LSS: case Operator::GTR: case Operator::NE: case Operator::LSE: case Operator::GTE: assert(expr.__state == Expression::COMPOUND); assert(expr.operands.size() == 2); leftRaw = process(expr.operands.at(0)); rightRaw = process(expr.operands.at(1)); break; default:; } switch (expr.op) { case Operator::AND: { assert(expr.operands.size()); - llvm::Value* resultRaw = process(expr.operands[0]); + llvm::Value* resultRaw = process(expr.operands.at(0)); + if (expr.operands.size() == 1) return resultRaw; + for(size_t i=1; i< expr.operands.size()-1; ++i){ resultRaw = l.irBuilder.CreateAnd(resultRaw, process(expr.operands.at(i))); } return l.irBuilder.CreateAnd(resultRaw, process(expr.operands.at(expr.operands.size()-1)), hintAlias); } case Operator::OR: { assert(expr.operands.size()); - llvm::Value* resultRaw = process(expr.operands[0]); + llvm::Value* resultRaw = process(expr.operands.at(0)); + if (expr.operands.size() == 1) return resultRaw; + for(size_t i=1; i< expr.operands.size()-1; ++i){ resultRaw = l.irBuilder.CreateOr(resultRaw, process(expr.operands.at(i))); } return l.irBuilder.CreateOr(resultRaw, process(expr.operands.at(expr.operands.size()-1)), hintAlias); } case Operator::ADD: { return l.irBuilder.CreateAdd(leftRaw, rightRaw, DEFAULT("addv")); } case Operator::SUB: return l.irBuilder.CreateSub(leftRaw, rightRaw, DEFAULT("tmp_sub")); break; case Operator::MUL: return l.irBuilder.CreateMul(leftRaw, rightRaw, DEFAULT("tmp_mul")); break; case Operator::DIV: if (leftRaw->getType()->isIntegerTy()) return l.irBuilder.CreateSDiv(leftRaw, rightRaw, DEFAULT("tmp_div")); if (leftRaw->getType()->isFloatingPointTy()) return l.irBuilder.CreateFDiv(leftRaw, rightRaw, DEFAULT("tmp_div")); break; case Operator::MOD:{ return l.irBuilder.CreateSRem(leftRaw, rightRaw, hintAlias); } case Operator::EQU: { if (leftRaw->getType()->isIntegerTy()) return l.irBuilder.CreateICmpEQ(leftRaw, rightRaw, DEFAULT("tmp_equ")); if (leftRaw->getType()->isFloatingPointTy()) return l.irBuilder.CreateFCmpOEQ(leftRaw, rightRaw, DEFAULT("tmp_equ")); const ExpandedType& leftT = pass->man->root->getType(expr.operands[0]); const ExpandedType& rightT = pass->man->root->getType(expr.operands[1]); if(leftT->__operator == TypeOperator::VARIANT && rightT->__operator == TypeOperator::VARIANT){ llvm::Type* selectorT = llvm::cast(leftRaw->getType())->getElementType(0); llvm::Value* leftUnwapped = typeinference::doAutomaticTypeConversion(leftRaw, selectorT, l.irBuilder); llvm::Value* rightUnwapped = typeinference::doAutomaticTypeConversion(rightRaw, selectorT, l.irBuilder); return l.irBuilder.CreateICmpEQ(leftUnwapped, rightUnwapped, DEFAULT("tmp_equ")); } break; } case Operator::NE: return l.irBuilder.CreateICmpNE(leftRaw, rightRaw, DEFAULT("tmp_ne")); break; case Operator::LSS: return l.irBuilder.CreateICmpSLT(leftRaw, rightRaw, DEFAULT("tmp_lss")); break; case Operator::LSE: return l.irBuilder.CreateICmpSLE(leftRaw, rightRaw, DEFAULT("tmp_lse")); break; case Operator::GTR: return l.irBuilder.CreateICmpSGT(leftRaw, rightRaw, DEFAULT("tmp_gtr")); break; case Operator::GTE: return l.irBuilder.CreateICmpSGE(leftRaw, rightRaw, DEFAULT("tmp_gte")); break; case Operator::NEG: { leftRaw = process(expr.operands[0]); ExpandedType leftTy = pass->man->root->getType(expr.operands[0]); if (leftTy->__value == TypePrimitive::Bool){ return l.irBuilder.CreateNot(leftRaw, hintAlias); } else { return l.irBuilder.CreateNeg(leftRaw, hintAlias); } break; } case Operator::CALL: { assert(expr.__state == Expression::COMPOUND); shared_ptr callee(findFunction(expr)); const std::string& nameCallee = expr.getValueString(); //prepare arguments std::vector args; args.reserve(expr.operands.size()); std::transform(expr.operands.begin(), expr.operands.end(), std::inserter(args, args.end()), [this](const Expression & operand) { return process(operand); } ); return (*callee)(move(args), DEFAULT("res_" + nameCallee)); } case Operator::IF: { return controlIR.compileIf(expr, DEFAULT("tmp_if")); } case Operator::SWITCH: { return controlIR.compileSwitch(expr, DEFAULT("tmp_switch")); } case Operator::LOGIC_AND: { assert(expr.operands.size() == 1); return process(expr.operands[0]); } case Operator::LIST: //init record or array { ExpandedType exprT = l.ast->getType(expr, expectedT); TypesHelper helper(pass->man->llvm); enum {RECORD, ARRAY} kind; if (helper.isArrayT(exprT)){ kind = ARRAY; } else if (helper.isRecordT(exprT)){ kind = RECORD; } else { assert(false && "Inapproriate type"); } #ifdef XREATE_ENABLE_EXTERN if (exprT->__operator == TypeOperator::ALIAS){ if (l.layerExtern->isArrayType(exprT->__valueCustom)){ flagIsArray = true; break; } if (l.layerExtern->isRecordType(exprT->__valueCustom)){ flagIsArray = false; break; } assert(false && "Inapproriate external type"); } #endif switch(kind){ case RECORD:{ const std::vector fieldsFormal = helper.getRecordFields(exprT); containers::RecordIR irRecords(ctx); llvm::StructType *recordTRaw = llvm::cast(l.toLLVMType(exprT)); llvm::Value *resultRaw = irRecords.init(recordTRaw); return irRecords.update(resultRaw, exprT, expr); } case ARRAY: { std::unique_ptr containerIR( containers::IContainersIR::create(expr, expectedT, ctx)); llvm::Value* aggrRaw = containerIR->init(hintAlias); return containerIR->update(aggrRaw, expr, hintAlias); } } break; }; case Operator::LIST_RANGE: { assert(false); //no compilation phase for a range list // return InstructionList(this).compileConstantArray(expr, l, hintRetVar); }; case Operator::MAP: { assert(expr.blocks.size()); containers::ImplementationType implType = containers::IContainersIR::getImplementation(expr, pass->man->root); switch(implType){ case containers::ImplementationType::SOLID: { ExpandedType exprT = pass->man->root->getType(expr, expectedT); ArrayHint hint = find(expr, ArrayHint{}); containers::ArrayIR compiler(exprT, hint, ctx); return compiler.operatorMap(expr, DEFAULT("map")); } case containers::ImplementationType::ON_THE_FLY:{ FlyHint hint = find(expr, {}); containers::FlyIR compiler(hint, ctx); return compiler.operatorMap(expr, DEFAULT("map")); } default: break; } assert(false && "Operator MAP does not support this container impl"); return nullptr; }; case Operator::FOLD: { return controlIR.compileFold(expr, DEFAULT("fold")); }; case Operator::FOLD_INF: { return controlIR.compileFoldInf(expr, DEFAULT("fold")); }; case Operator::INDEX: { assert(expr.operands.size() > 1); const Expression& aggrE = expr.operands[0]; const ExpandedType& aggrT = pass->man->root->getType(aggrE); llvm::Value* aggrRaw = process(aggrE); switch (aggrT->__operator) { case TypeOperator::RECORD: { list fieldsList; for(auto opIt = ++expr.operands.begin(); opIt!=expr.operands.end(); ++opIt){ fieldsList.push_back(getIndexStr(*opIt)); } return controlIR.compileStructIndex(aggrRaw, aggrT, fieldsList); }; case TypeOperator::ARRAY: { std::vector indexes; std::transform(++expr.operands.begin(), expr.operands.end(), std::inserter(indexes, indexes.end()), [this] (const Expression & op) { return process(op); } ); std::unique_ptr containersIR( containers::IContainersIR::create(aggrE, expectedT, ctx) ); containers::ArrayIR* arraysIR = static_cast(containersIR.get()); return arraysIR->get(aggrRaw, indexes, hintAlias); }; default: assert(false); } }; case Operator::CALL_INTRINSIC: { // const std::string op = expr.getValueString(); // // if (op == "copy") { // llvm::Value* result = process(expr.getOperands().at(0)); // // auto decoratorVersions = Decorators::getInterface(this); // llvm::Value* storage = decoratorVersions->processIntrinsicInit(result->getType()); // decoratorVersions->processIntrinsicCopy(result, storage); // // return l.irBuilder.CreateLoad(storage, hintAlias); // } assert(false && "undefined intrinsic"); } case Operator::QUERY: case Operator::QUERY_LATE: { assert(false && "Should be processed by interpretation"); } case Operator::VARIANT: { const ExpandedType& typResult = pass->man->root->getType(expr); llvm::Type* typResultRaw = l.toLLVMType(typResult); llvm::Type* typIdRaw = llvm::cast(typResultRaw)->getElementType(0); uint64_t id = expr.getValueDouble(); llvm::Value* resultRaw = llvm::UndefValue::get(typResultRaw); resultRaw = l.irBuilder.CreateInsertValue(resultRaw, llvm::ConstantInt::get(typIdRaw, id), llvm::ArrayRef({0})); const ExpandedType& typVariant = ExpandedType(typResult->__operands.at(id)); llvm::Type* typVariantRaw = l.toLLVMType(typVariant); llvm::Value* variantRaw = llvm::UndefValue::get(typVariantRaw); assert(expr.operands.size() == typVariant->__operands.size() && "Wrong variant arguments count"); if (!typVariant->__operands.size()) return resultRaw; for (unsigned int fieldId = 0; fieldId < expr.operands.size(); ++fieldId) { const ExpandedType& typField = ExpandedType(typVariant->__operands.at(fieldId)); Attachments::put(expr.operands.at(fieldId), typField); llvm::Value* fieldRaw = process(expr.operands.at(fieldId)); assert(fieldRaw); variantRaw = l.irBuilder.CreateInsertValue(variantRaw, fieldRaw, llvm::ArrayRef({fieldId})); } llvm::Type* typStorageRaw = llvm::cast(typResultRaw)->getElementType(1); llvm::Value* addrAsStorage = l.irBuilder.CreateAlloca(typStorageRaw); llvm::Value* addrAsVariant = l.irBuilder.CreateBitOrPointerCast(addrAsStorage, typVariantRaw->getPointerTo()); l.irBuilder.CreateStore(variantRaw, addrAsVariant); llvm::Value* storageRaw = l.irBuilder.CreateLoad(typStorageRaw, addrAsStorage); resultRaw = l.irBuilder.CreateInsertValue(resultRaw, storageRaw, llvm::ArrayRef({1})); return resultRaw; } case Operator::SWITCH_VARIANT: { return controlIR.compileSwitchVariant(expr, DEFAULT("tmpswitch")); } case Operator::SWITCH_LATE: { assert(false && "Instruction's compilation should've been redirected to interpretation"); return nullptr; } case Operator::SEQUENCE: { return controlIR.compileSequence(expr); } case Operator::UNDEF: { llvm::Type* typExprUndef = l.toLLVMType(pass->man->root->getType(expr, expectedT)); return llvm::UndefValue::get(typExprUndef); } case Operator::UPDATE: { TypesHelper helper(pass->man->llvm); containers::RecordIR irRecords(ctx); const Expression& aggrE = expr.operands.at(0); const Expression& updE = expr.operands.at(1); const ExpandedType& aggrT = pass->man->root->getType(aggrE); llvm::Value* aggrRaw = process(aggrE); if (helper.isRecordT(aggrT)){ return irRecords.update(aggrRaw, aggrT, updE); } if (helper.isArrayT(aggrT)){ if (updE.op == Operator::LIST_INDEX){ std::unique_ptr containersIR( containers::IContainersIR::create(aggrE, TypeAnnotation(), ctx )); return containersIR->update(aggrRaw, updE, hintAlias); } } assert(false); return nullptr; } case Operator::INVALID: assert(expr.__state != Expression::COMPOUND); switch (expr.__state) { case Expression::IDENT: { Symbol s = Attachments::get(expr); return processSymbol(s, expr.getValueString()); } case Expression::NUMBER: { llvm::Type* typConst = l.toLLVMType(pass->man->root->getType(expr, expectedT)); int literal = expr.getValueDouble(); if (typConst->isFloatingPointTy()) return llvm::ConstantFP::get(typConst, literal); if (typConst->isIntegerTy()) return llvm::ConstantInt::get(typConst, literal); assert(false && "Can't compile literal"); } case Expression::STRING: { return controlIR.compileConstantStringAsPChar(expr.getValueString(), DEFAULT("tmp_str")); }; default: { break; } }; break; default: break; } assert(false && "Can't compile expression"); return 0; } llvm::Value* BasicBruteScope::compile(const std::string& hintBlockDecl) { LLVMLayer* llvm = pass->man->llvm; if (!hintBlockDecl.empty()) { llvm::BasicBlock *block = llvm::BasicBlock::Create(llvm->llvmContext, hintBlockDecl, function->raw); pass->man->llvm->irBuilder.SetInsertPoint(block); } lastBlockRaw = pass->man->llvm->irBuilder.GetInsertBlock(); Symbol symbScope = Symbol{ScopedSymbol::RetSymbol, scope}; return processSymbol(symbScope); } IBruteScope::~IBruteScope() { } IBruteFunction::~IBruteFunction() { } llvm::Function* IBruteFunction::compile() { if (raw != nullptr) return raw; LLVMLayer* llvm = pass->man->llvm; llvm::IRBuilder<>& builder = llvm->irBuilder; string&& functionName = prepareName(); std::vector&& types = prepareSignature(); llvm::Type* expectedResultType = prepareResult(); llvm::FunctionType *ft = llvm::FunctionType::get(expectedResultType, types, false); raw = llvm::cast(llvm->module->getOrInsertFunction(functionName, ft)); prepareBindings(); applyAttributes(); const std::string& blockName = "entry"; llvm::BasicBlock* blockCurrent = builder.GetInsertBlock(); llvm::Value* result = getScopeUnit(__entry)->compile(blockName); assert(result); //SECTIONTAG types/convert function ret value builder.CreateRet(typeinference::doAutomaticTypeConversion(result, expectedResultType, llvm->irBuilder)); if (blockCurrent) { builder.SetInsertPoint(blockCurrent); } llvm->moveToGarbage(ft); return raw; } IBruteScope* IBruteFunction::getScopeUnit(const CodeScope * const scope) { if (__scopes.count(scope)) { auto result = __scopes.at(scope).lock(); if (result) { return result.get(); } } std::shared_ptr unit(pass->buildCodeScopeUnit(scope, this)); if (scope->__parent != nullptr) { auto parentUnit = Decorators::getInterface(getScopeUnit(scope->__parent)); parentUnit->registerChildScope(unit); } else { __orphanedScopes.push_back(unit); } if (!__scopes.emplace(scope, unit).second) { __scopes[scope] = unit; } return unit.get(); } IBruteScope* IBruteFunction::getScopeUnit(ManagedScpPtr scope) { return getScopeUnit(&*scope); } IBruteScope* IBruteFunction::getEntry() { return getScopeUnit(__entry); } std::vector IBruteFunction::getScopeSignature(CodeScope* scope){ LLVMLayer* llvm = IBruteFunction::pass->man->llvm; AST* ast = IBruteFunction::pass->man->root; std::vector result; std::transform(scope->__bindings.begin(), scope->__bindings.end(), std::inserter(result, result.end()), - [llvm, ast, scope](const std::string & arg)->llvm::Type* { - assert(scope->__identifiers.count(arg)); + [llvm, ast, scope](const std::string & argAlias)->llvm::Type* { + assert(scope->__identifiers.count(argAlias)); + + ScopedSymbol argS{scope->__identifiers.at(argAlias), versions::VERSION_NONE}; + const Expression& argE = scope->__declarations.at(argS); + const ExpandedType& argT = ast->expandType(argE.type); - ScopedSymbol argid{scope->__identifiers.at(arg), versions::VERSION_NONE}; - return llvm->toLLVMType(ast->expandType(scope->__declarations.at(argid).type)); + return llvm->toLLVMType(argT, argE); }); return result; } template<> compilation::IBruteFunction* CompilePassCustomDecorators ::buildFunctionUnit(const ManagedFnPtr& function) { return new BruteFunctionDefault(function, this); } template<> compilation::IBruteScope* CompilePassCustomDecorators ::buildCodeScopeUnit(const CodeScope * const scope, IBruteFunction* function) { return new DefaultCodeScopeUnit(scope, function, this); } std::string BasicBruteScope::getIndexStr(const Expression& index){ switch(index.__state){ //named struct field case Expression::STRING: return index.getValueString(); break; //anonymous struct field case Expression::NUMBER: return to_string((int) index.getValueDouble()); break; default: assert(false && "Wrong index for a struct"); } return ""; } } // end of compilation compilation::IBruteFunction* CompilePass::getFunctionUnit(const ManagedFnPtr& function) { unsigned int id = function.id(); if (!functions.count(id)) { compilation::IBruteFunction* unit = buildFunctionUnit(function); functions.emplace(id, unit); return unit; } return functions.at(id); } void CompilePass::prepare(){ //Initialization: #ifndef XREATE_CONFIG_MIN #endif managerTransformations = new xreate::compilation::TransformationsManager(); targetInterpretation = new interpretation::TargetInterpretation(man, this); } void CompilePass::run() { prepare(); //Determine entry function: StaticModel model = man->transcend->query(analysis::FN_ENTRY_PREDICATE); assert(model.size() && "Error: No entry function found"); assert(model.size() == 1 && "Error: Ambiguous entry function"); string nameMain = std::get<0>(TranscendLayer::parse(model.begin()->second)); compilation::IBruteFunction* unitMain = getFunctionUnit(man->root->findFunction(nameMain)); //Compilation itself: entry = unitMain->compile(); } llvm::Function* CompilePass::getEntryFunction() { assert(entry); return entry; } void CompilePass::prepareQueries(TranscendLayer* transcend) { #ifndef XREATE_CONFIG_MIN transcend->registerQuery(new latex::LatexQuery(), QueryId::LatexQuery); #endif transcend->registerQuery(new containers::Query(), QueryId::ContainersQuery); transcend->registerQuery(new demand::DemandQuery(), QueryId::DemandQuery); transcend->registerQuery(new polymorph::PolymorphQuery(), QueryId::PolymorphQuery); } } //end of namespace xreate /** * \class xreate::CompilePass * \brief The owner of the compilation process. Performs fundamental compilation activities along with the xreate::compilation's routines * * xreate::CompilePass traverses over xreate::AST tree and produces executable code. * The pass performs compilation using the following data sources: * - %Attachments: the data gathered by the previous passes. See \ref xreate::Attachments. * - Transcend solutions accessible via queries. See \ref xreate::IQuery, \ref xreate::TranscendLayer. * * The pass generates a bytecode by employing \ref xreate::LLVMLayer(wrapper over LLVM toolchain). * Many compilation activities are delegated to more specific routines. Most notable delegated compilation aspects are: * - Containers support. See \ref xreate::containers. * - Latex compilation. See \ref xreate::latex. * - Interpretation support. See \ref xreate::interpretation. * - Loop saturation support. See \ref xreate::compilation::TransformationsScopeDecorator. * - External code interaction support. See \ref xreate::ExternLayer (wrapper over Clang library). * * \section adaptability_sect Adaptability * xreate::CompilePass's behaviour can be adapted in several ways: * - %Function Decorators to alter function-level compilation. See \ref xreate::compilation::IBruteFunction * - Code Block Decorators to alter code block level compilation. See \ref xreate::compilation::ICodeScopeUnit. * Default functionality defined by \ref xreate::compilation::DefaultCodeScopeUnit * - Targets to allow more versitile extensions. * Currently only xreate::interpretation::TargetInterpretation use Targets infrastructure. See \ref xreate::compilation::Target. * - Altering %function invocation. See \ref xreate::compilation::IFnInvocation. * * Clients are free to construct a compiler instantiation with the desired decorators by using \ref xreate::compilation::CompilePassCustomDecorators. * As a handy alias, `CompilePassCustomDecorators` constructs the default compiler. * */ diff --git a/cpp/tests/compilation.cpp b/cpp/tests/compilation.cpp index 367e8de..34b7216 100644 --- a/cpp/tests/compilation.cpp +++ b/cpp/tests/compilation.cpp @@ -1,304 +1,331 @@ /* Any copyright is dedicated to the Public Domain. * http://creativecommons.org/publicdomain/zero/1.0/ * * compilation.cpp * * Created on: - * Author: pgess */ #include "xreatemanager.h" #include "supplemental/basics.h" #include "llvmlayer.h" #include "pass/compilepass.h" #include "compilation/lambdas.h" #include "gtest/gtest.h" using namespace xreate; using namespace xreate::compilation; using namespace std; //DEBT implement no pkgconfig ways to link libs //TOTEST FunctionUnit::compileInline TEST(Compilation, functionEntry1){ std::unique_ptr program(XreateManager::prepare( "func1 = function(a:: int):: int {a+8} \ func2 = function::int; entry {12 + func1(4)} \ ")); void* entryPtr = program->run(); int (*entry)() = (int (*)())(intptr_t)entryPtr; int answer = entry(); ASSERT_EQ(24, answer); } TEST(Compilation, full_IFStatementWithVariantType){ XreateManager* man = XreateManager::prepare( "Color = type variant {RED, BLUE, GREEN}.\n" "\n" " main = function(x::int):: bool; entry {\n" " color = if (x == 0 )::Color {RED()} else {BLUE()}.\n" " if (color == BLUE())::bool {true} else {false}\n" " }" ); bool (*main)(int) = (bool (*)(int)) man->run(); ASSERT_FALSE(main(0)); ASSERT_TRUE(main(1)); } TEST(Compilation, full_Variant1){ XreateManager* man = XreateManager::prepare(R"Code( global = type predicate { entry } Command= type variant{ Add(x::int, y::int), Dec(x::int) }. main = function::Command; entry() { Dec(2) ::Command } )Code"); void (*main)() = (void (*)()) man->run(); } TEST(Compilation, full_SwitchVariant1){ XreateManager* man = XreateManager::prepare(R"Code( Command= type variant{ Add(x::int, y::int), Dec(x::int) }. main = function::int; entry { command = Add(3, 5):: Command. switch variant(command)::int case(Add){command["x"] + command["y"]} case(Dec){command["x"]} } )Code"); int (*mainFn)() = (int (*)()) man->run(); int result = mainFn(); ASSERT_EQ(8, result); } TEST(Compilation, full_SwitchVariantNoArguments2){ XreateManager* man = XreateManager::prepare(R"Code( Command= type variant{Add, Dec}. main = function::int; entry { command = Dec():: Command. switch variant(command)::int case(Add){0} case(Dec){1} } )Code"); int (*mainFn)() = (int (*)()) man->run(); int result = mainFn(); ASSERT_EQ(1, result); } TEST(Compilation, full_SwitchVariantMixedArguments3){ XreateManager* man = XreateManager::prepare(R"Code( Command= type variant{ Add(x::int, y::int), Dec }. main = function(arg::int):: int; entry { command = if (arg > 0)::Command {Dec()} else {Add(1, 2)}. switch variant(command)::int case(Add){0} case(Dec){1} } )Code"); int (*mainFn)(int) = (int (*)(int)) man->run(); int result = mainFn(5); ASSERT_EQ(1, result); } TEST(Compilation, full_StructUpdate){ XreateManager* man = XreateManager::prepare( R"Code( Rec = type { a :: int, b:: int }. test= function:: int; entry { a = {a = 18, b = 20}:: Rec. b = a + {a = 11}:: Rec. b["a"] } )Code"); int (*main)() = (int (*)()) man->run(); int result = main(); ASSERT_EQ(11, result); } TEST(Compilation, AnonymousStruct_init_index){ std::string code = R"Code( main = function:: int; entry { x = {10, 15} :: {int, int}. x[1] } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); int (*main)() = (int (*)()) man->run(); EXPECT_EQ(15, main()); } TEST(Compilation, AnonymousStruct_init_update){ std::string code = R"Code( main = function:: int; entry { x = {10, 15} :: {int, int}. y = x + {6}:: {int, int}. y[0] } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); int (*main)() = (int (*)()) man->run(); EXPECT_EQ(6, main()); } TEST(Compilation, BugIncorrectScopes1){ std::string code = R"Code( init = function:: int {10} main = function(cmd:: int):: int; entry { x = init():: int. if(cmd > 0):: int { x + 1 } else { x } } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); int (*mainFn)(int) = (int (*)(int)) man->run(); EXPECT_EQ(11, mainFn(1)); } TEST(Compilation, Sequence1){ std::string code = R"Code( interface(extern-c){ libbsd = library:: pkgconfig("libbsd"). include { libbsd = {"bsd/stdlib.h", "string.h"} }. } start = function:: i32; entry { seq { nameNew = "TestingSequence":: string. setprogname(nameNew) } {strlen(getprogname())}::i32 } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); int (*startFn)() = (int (*)()) man->run(); int nameNewLen = startFn(); ASSERT_EQ(15, nameNewLen); } TEST(Compilation, BoolInstructions1){ std::string code = R"Code( test = function (a:: bool, b:: bool):: bool; entry { -a } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); Fn2Args startFn = (Fn2Args) man->run(); } TEST(Compilation, StructIndex1){ std::string code = R"Code( Anns = type predicate { entry() } test = function:: int; entry() { x = {a = ({b = 3}::{b:: int})}:: {a:: {b:: int}}. 2 + x["a", "b"] + x["a"]["b"] } )Code"; std::unique_ptr man(XreateManager::prepare(move(code))); FnNoArgs startFn = (FnNoArgs) man->run(); int result = startFn(); ASSERT_EQ(2, result); } TEST(Compilation, PreferredInt1){ std::unique_ptr man(XreateManager::prepare("")); TypesHelper utils(man->llvm); int bitwidth = utils.getPreferredIntTy()->getBitWidth(); ASSERT_EQ(64, bitwidth); } TEST(Compilation, PredPredicates1){ string code = R"( my-fn = function:: int; entry() {0} )"; auto man = XreateManager::prepare(move(code)); FnNoArgs startFn = (FnNoArgs) man->run(); int result = startFn(); ASSERT_EQ(0, result); } typedef intmax_t (*FnI_I)(intmax_t); TEST(Compilation, Lambda1){ string code = R"( myfn = function:: int { a = [1..5]:: [int]. loop map(a->x:: int):: [int] { x + 10:: int} } )"; auto man = details::tier1::XreateManager::prepare(move(code)); LLVMLayer* llvm = man->llvm; man->analyse(); std::unique_ptr compiler(new compilation::CompilePassCustomDecorators<>(man)); compiler->prepare(); LambdaIR compilerLambda(compiler.get()); CodeScope* scopeLoop = man->root->findFunction("myfn")->getEntryScope()->getBody().blocks.front(); auto fnRaw = compilerLambda.compile(scopeLoop, "loop"); llvm->initJit(); FnI_I fn = (FnI_I)llvm->getFunctionPointer(fnRaw); ASSERT_EQ(20, fn(10)); +} + +struct Tuple3 {intmax_t a; intmax_t b; intmax_t c; }; +typedef Tuple3 (*FnTuple3)(); + +intmax_t fn_BUG_Triple(FnTuple3 callee){ + Tuple3 result = callee(); + return result.a+ result.b + result.c; +} + +TEST(Compilation, BUG_Triple){ + std::unique_ptr man(XreateManager::prepare(R"( +Tuple2 = type {int, int}. +Tuple3 = type {int, int, int}. +Tuple4 = type {int, int, int, int}. + +main = function:: Tuple3; entry() +{ + {1, 2, 3} +} +)")); + FnTuple3 mainFn = (FnTuple3) man->run(); + intmax_t result = fn_BUG_Triple(mainFn); + + ASSERT_EQ(6, result); +// ASSERT_EQ(2, result.b); +// ASSERT_EQ(3, result.c); } \ No newline at end of file diff --git a/cpp/tests/containers.cpp b/cpp/tests/containers.cpp index 8f3074a..949aa9f 100644 --- a/cpp/tests/containers.cpp +++ b/cpp/tests/containers.cpp @@ -1,321 +1,325 @@ /* Any copyright is dedicated to the Public Domain. * http://creativecommons.org/publicdomain/zero/1.0/ * * containers.cpp * * Created on: Jun 9, 2015 * Author: pgess */ #include "xreatemanager.h" #include "query/containers.h" #include "main/Parser.h" +#include "pass/compilepass.h" +#include "llvmlayer.h" #include "supplemental/docutils.h" #include "supplemental/basics.h" #include "gtest/gtest.h" using namespace std; using namespace xreate::grammar::main; using namespace xreate::containers; using namespace xreate; struct Tuple2 {intmax_t a; intmax_t b;}; typedef Tuple2 (*FnTuple2)(); -struct Tuple3 {intmax_t a; intmax_t b; intmax_t c; }; -typedef Tuple3 (*FnTuple3)(); - struct Tuple4 {intmax_t a; intmax_t b; intmax_t c; intmax_t d;}; typedef Tuple4 (*FnTuple4)(); TEST(Containers, RecInitByList1){ string code = R"( Rec = type {x:: int, y:: int}. test = function(a:: int, b::int):: Rec; entry() { {x = a + b, y = 2} } )"; auto man = XreateManager::prepare(move(code)); man->run(); } TEST(Containers, RecInitByList2){ string code = R"( Rec = type {x:: int, y:: int}. test = function(a:: int, b::int):: Rec; entry() { {a + b, y = 2} } )"; auto man = XreateManager::prepare(move(code)); man->run(); } TEST(Containers, RecUpdateByList1){ string code = R"( Rec = type {x:: int, y:: int}. test = function(a:: int, b::int):: Rec; entry() { r = {0, y = 2}:: Rec. r : {a + b} } )"; auto man = XreateManager::prepare(move(code)); man->run(); } TEST(Containers, RecUpdateByListIndex1){ string code = R"( Rec = type {x:: int, y:: int}. test = function(a:: int, b::int):: int; entry() { r1 = undef:: Rec. r2 = r1 : {[1] = b, [0] = a}:: Rec. r2["x"] } )"; auto man = XreateManager::prepare(move(code)); Fn2Args program = (Fn2Args) man->run(); ASSERT_EQ(10, program(10, 11)); } TEST(Containers, RecUpdateInLoop1){ FILE* code = fopen("scripts/containers/RecUpdateInLoop1.xreate", "r"); assert(code != nullptr); auto man = XreateManager::prepare(code); Fn1Args program = (Fn1Args) man->run(); ASSERT_EQ(11, program(10)); } TEST(Containers, ArrayInit1){ XreateManager* man = XreateManager::prepare( R"Code( main = function(x:: int):: int; entry() { a = {1, 2, 3}:: [int]. a[x] } )Code"); void* mainPtr = man->run(); Fn1Args main = (Fn1Args) mainPtr; ASSERT_EQ(2, main(1)); delete man; } TEST(Containers, ArrayUpdate1){ XreateManager* man = XreateManager::prepare(R"( main = function(x::int):: int; entry() { a = {1, 2, 3}:: [int]; csize(5). b = a : {[1] = x}:: [int]; csize(5). b[1] } )"); void* mainPtr = man->run(); Fn1Args main = (Fn1Args) mainPtr; ASSERT_EQ(2, main(2)); delete man; } TEST(Containers, FlyMap1){ std::unique_ptr man(XreateManager::prepare(R"( main = function:: int; entry() { x = {1, 2, 3, 4}:: [int]. y = loop map(x->el::int)::[int]; fly(csize(4)) {2 * el:: int }. loop fold((y::[int]; fly(csize(4)))->el:: int, 0->sum):: int {sum + el}-20 } )")); FnNoArgs mainFn = (FnNoArgs) man->run(); intmax_t valueMain = mainFn(); ASSERT_EQ(0, valueMain); } -intmax_t fn_BUG_Triple(FnTuple3 callee){ - Tuple3 result = callee(); - return result.a+ result.b + result.c; -} -TEST(Containers, BUG_Triple){ - std::unique_ptr man(XreateManager::prepare(R"( -Tuple2 = type {int, int}. -Tuple3 = type {int, int, int}. -Tuple4 = type {int, int, int, int}. +TEST(Containers, ArrayArg1){ + FILE* code = fopen("scripts/containers/containers-tests.xreate", "r"); + assert(code != nullptr); -main = function:: Tuple3; entry() -{ - {1, 2, 3} -} -)")); - FnTuple3 mainFn = (FnTuple3) man->run(); - intmax_t result = fn_BUG_Triple(mainFn); + auto man = details::tier1::XreateManager::prepare(code); + LLVMLayer* llvm = man->llvm; + man->analyse(); - ASSERT_EQ(6, result); -// ASSERT_EQ(2, result.b); -// ASSERT_EQ(3, result.c); + std::unique_ptr compiler(new compilation::CompilePassCustomDecorators<>(man)); + compiler->prepare(); + llvm::Function* fnMainRaw = compiler->getFunctionUnit(man->root->findFunction("fn-ArrayArg1"))->compile(); + llvm->print(); + llvm->initJit(); + + FnNoArgs mainFn = (FnNoArgs) llvm->getFunctionPointer(fnMainRaw); + ASSERT_EQ(1, mainFn()); } -TEST(Containers, ArrayArg1){ - FILE* code = fopen("scripts/containers/array_arg_1.xreate", "r"); +TEST(Containers, FlyArg1){ + FILE* code = fopen("scripts/containers/containers-tests.xreate", "r"); assert(code != nullptr); - std::unique_ptr man (XreateManager::prepare(code)); - FnNoArgs mainFn = (FnNoArgs) man->run(); - ASSERT_EQ(0, mainFn()); -} + auto man = details::tier1::XreateManager::prepare(code); + LLVMLayer* llvm = man->llvm; + man->analyse(); + std::unique_ptr compiler(new compilation::CompilePassCustomDecorators<>(man)); + compiler->prepare(); + llvm::Function* fnTestedRaw = compiler->getFunctionUnit(man->root->findFunction("fn-FlyArg1"))->compile(); + llvm->print(); + + llvm->optsLevel = llvm::CodeGenOpt::Aggressive; + llvm->initJit(); + + FnNoArgs fnTested = (FnNoArgs) llvm->getFunctionPointer(fnTestedRaw); + ASSERT_EQ(8, fnTested()); +} //TEST(Containers, ListAsArray2){ // XreateManager* man = XreateManager::prepare( // //R"Code( // // CONTAINERS // import raw("scripts/dfa/ast-attachments.lp"). // import raw("scripts/containers/containers.lp"). // // main = function:: int;entry { // a= {1, 2, 3}:: [int]. // b= loop map(a->el:: int):: [int]{ // 2 * el // }. // // sum = loop fold(b->el:: int, 0->acc):: int { // acc + el // }. // // sum // } //)Code"); // // void* mainPtr = man->run(); // FnNoArgs main = (FnNoArgs) mainPtr; // ASSERT_EQ(12, main()); // // delete man; //} // //TEST(Containers, Doc_RecField1){ // string code_Variants1 = getDocumentationExampleById("documentation/Syntax/syntax.xml", "RecField1"); // XreateManager::prepare(move(code_Variants1)); // // ASSERT_TRUE(true); //} // //TEST(Containers, Doc_RecUpdate1){ // string code_Variants1 = getDocumentationExampleById("documentation/Syntax/syntax.xml", "RecUpdate1"); // XreateManager::prepare(move(code_Variants1)); // // ASSERT_TRUE(true); //} // //TEST(Containers, ContanierLinkedList1){ // FILE* input = fopen("scripts/containers/Containers_Implementation_LinkedList1.xreate","r"); // assert(input != nullptr); // // Scanner scanner(input); // Parser parser(&scanner); // parser.Parse(); // // AST* ast = parser.root->finalize(); // CodeScope* body = ast->findFunction("test")->getEntryScope(); // const Symbol symb_chilrenRaw{body->getSymbol("childrenRaw"), body}; // // containers::ImplementationLinkedList iLL(symb_chilrenRaw); // // ASSERT_EQ(true, static_cast(iLL)); // ASSERT_EQ("next", iLL.fieldPointer); // // Implementation impl = Implementation::create(symb_chilrenRaw); // ASSERT_NO_FATAL_FAILURE(impl.extract()); // // ImplementationRec recOnthefly = impl.extract(); // ASSERT_EQ(symb_chilrenRaw, recOnthefly.source); //} // //TEST(Containers, Implementation_LinkedListFull){ // FILE* input = fopen("scripts/containers/Containers_Implementation_LinkedList1.xreate","r"); // assert(input != nullptr); // // std::unique_ptr program(XreateManager::prepare(input)); // void* mainPtr = program->run(); // int (*main)() = (int (*)())(intptr_t)mainPtr; // // intmax_t answer = main(); // ASSERT_EQ(17, answer); // // fclose(input); //} // //TEST(Containers, Doc_Intr_1){ // string example = R"Code( // import raw("scripts/containers/containers.lp"). // // test = function:: int; entry // { // // x // } // )Code"; // string body = getDocumentationExampleById("documentation/Concepts/containers.xml", "Intr_1"); // replace(example, "", body); // // XreateManager* xreate = XreateManager::prepare(move(example)); // FnNoArgs program = (FnNoArgs) xreate->run(); // // intmax_t result = program(); // ASSERT_EQ(1, result); //} // //TEST(Containers, Doc_OpAccessSeq_1){ // string example = getDocumentationExampleById("documentation/Concepts/containers.xml", "OpAccessSeq_1"); // XreateManager* xreate = XreateManager::prepare(move(example)); // FnNoArgs program = (FnNoArgs) xreate->run(); // // intmax_t result = program(); // ASSERT_EQ(15, result); //} // //TEST(Containers, Doc_OpAccessRand_1){ // string example = getDocumentationExampleById("documentation/Concepts/containers.xml", "OpAccessRand_1"); // XreateManager* xreate = XreateManager::prepare(move(example)); // FnNoArgs program = (FnNoArgs) xreate->run(); // // intmax_t result = program(); // ASSERT_EQ(2, result); //} // //TEST(Containers, Doc_ASTAttach_1){ // string example = getDocumentationExampleById("documentation/Concepts/containers.xml", "ASTAttach_1"); // string outputExpected = "containers_impl(s(1,-2,0),onthefly)"; // XreateManager* xreate = XreateManager::prepare(move(example)); // // testing::internal::CaptureStdout(); // xreate->run(); // std::string outputActual = testing::internal::GetCapturedStdout(); // // ASSERT_NE(std::string::npos, outputActual.find(outputExpected)); //} // //TEST(Containers, IntrinsicArrInit1){ // XreateManager* man = XreateManager::prepare( // //R"Code( //FnAnns = type predicate { // entry //} // //main = function(x:: int):: int; entry() { // a{0} = intrinsic array_init(16):: [int]. // a{1} = a{0} + {15: 12} //} //)Code"); //}