static Value mathAbs(Context *ctx, const List<Value>& args) { if (args.getCount() != 1) { ctx->throwException(createException(ExcType::ValueError, "abs takes only one argument.")); } switch (args[0].type) { case ValueType::Float: { return createFloat(std::abs(args[0].f)); } case ValueType::Int: { return createInt(std::llabs(args[0].i)); } default: { ctx->throwException(createException(ExcType::TypeError, "Value is not convertable to float.")); } } SDL_assert_release(false); return createInt(0); }
Engine::Engine() : nextTypeID(LONG_LONG_MIN) { globalVars.set("__classify", createNativeFunction(createClass)); globalVars.set("__methodify", createNativeFunction(createMethod)); globalVars.set("abs", createNativeFunction(mathAbs)); globalVars.set("exp", createNativeFunction(mathFunc1<std::exp>::f)); globalVars.set("exp2", createNativeFunction(mathFunc1<std::exp2>::f)); globalVars.set("expm1", createNativeFunction(mathFunc1<std::expm1>::f)); globalVars.set("log", createNativeFunction(mathFunc1<std::log>::f)); globalVars.set("log10", createNativeFunction(mathFunc1<std::log10>::f)); globalVars.set("log2", createNativeFunction(mathFunc1<std::log2>::f)); globalVars.set("log1p", createNativeFunction(mathFunc1<std::log1p>::f)); globalVars.set("pow", createNativeFunction(mathFunc2<std::pow>::f)); globalVars.set("sqrt", createNativeFunction(mathFunc1<std::sqrt>::f)); globalVars.set("cbrt", createNativeFunction(mathFunc1<std::cbrt>::f)); globalVars.set("hypot", createNativeFunction(mathFunc2<std::hypot>::f)); globalVars.set("sin", createNativeFunction(mathFunc1<std::sin>::f)); globalVars.set("cos", createNativeFunction(mathFunc1<std::cos>::f)); globalVars.set("tan", createNativeFunction(mathFunc1<std::tan>::f)); globalVars.set("asin", createNativeFunction(mathFunc1<std::asin>::f)); globalVars.set("acos", createNativeFunction(mathFunc1<std::acos>::f)); globalVars.set("atan", createNativeFunction(mathFunc1<std::atan>::f)); globalVars.set("atan2", createNativeFunction(mathFunc2<std::atan2>::f)); globalVars.set("sinh", createNativeFunction(mathFunc1<std::sinh>::f)); globalVars.set("cosh", createNativeFunction(mathFunc1<std::cosh>::f)); globalVars.set("tanh", createNativeFunction(mathFunc1<std::tanh>::f)); globalVars.set("asinh", createNativeFunction(mathFunc1<std::asinh>::f)); globalVars.set("acosh", createNativeFunction(mathFunc1<std::acosh>::f)); globalVars.set("atanh", createNativeFunction(mathFunc1<std::atanh>::f)); globalVars.set("erf", createNativeFunction(mathFunc1<std::erf>::f)); globalVars.set("erfc", createNativeFunction(mathFunc1<std::erfc>::f)); globalVars.set("lgamma", createNativeFunction(mathFunc1<std::lgamma>::f)); globalVars.set("ceil", createNativeFunction(mathFunc1<std::ceil>::f)); globalVars.set("floor", createNativeFunction(mathFunc1<std::floor>::f)); globalVars.set("trunc", createNativeFunction(mathFunc1<std::trunc>::f)); globalVars.set("round", createNativeFunction(mathFunc1<std::round>::f)); globalVars.set("max", createNativeFunction(mathFunc2<mathMax>::f)); globalVars.set("min", createNativeFunction(mathFunc2<mathMin>::f)); globalVars.set("HUGE_VAL", createFloat(HUGE_VAL)); globalVars.set("INF", createFloat(INFINITY)); globalVars.set("NAN", createFloat(NAN)); globalVars.set("PI", createFloat(M_PI)); globalVars.set("isNil", createNativeFunction(isNil)); globalVars.set("print", createNativeFunction(print)); }
static Value f(Context *ctx, const List<Value>& args) { if (args.getCount() != 2) { ctx->throwException(createException(ExcType::ValueError, "Function takes only one argument.")); } return createFloat(F(asNumber(ctx, args[0]), asNumber(ctx, args[1]))); }
Domain * Constructors::create(const llvm::Type &type) const { CANAL_ASSERT_MSG(!type.isVoidTy(), "Cannot create value of type Void."); if (type.isIntegerTy()) { llvm::IntegerType &integerType = checkedCast<llvm::IntegerType>(type); return createInteger(integerType.getBitWidth()); } if (type.isFloatingPointTy()) { const llvm::fltSemantics &semantics = Float::Utils::getSemantics(type); return createFloat(semantics); } if (type.isPointerTy()) { const llvm::PointerType &pointerType = checkedCast<llvm::PointerType>(type); return createPointer(pointerType); } if (type.isArrayTy() || type.isVectorTy()) { const llvm::SequentialType &stype = checkedCast<llvm::SequentialType>(type); return createArray(stype); } if (type.isStructTy()) { const llvm::StructType &structType = checkedCast<llvm::StructType>(type); std::vector<Domain*> members; for (unsigned i = 0; i < structType.getNumElements(); i ++) members.push_back(create(*structType.getElementType(i))); return createStructure(structType, members); } CANAL_DIE_MSG("Unsupported llvm::Type::TypeID: " << type.getTypeID()); }
void PropertyManager::addAttribute(wxPropertyGridManager *pg, std::unique_ptr<Attribute> &a) { switch (a->getType()) { case Enums::ENUM: createEnum(pg, a); break; case Enums::BOOL: createBool(pg, a); break; case Enums::BVEC4: createBVec4(pg, a); break; case Enums::INT: createInt(pg, a); break; case Enums::IVEC3: createIVec3(pg, a); break; case Enums::UINT: createUInt(pg, a); break; case Enums::UIVEC2: createUIVec2(pg, a); break; case Enums::UIVEC3: createUIVec3(pg, a); break; case Enums::FLOAT: createFloat(pg, a); break; case Enums::VEC2: createVec2(pg, a); break; case Enums::VEC3: createVec3(pg, a); break; case Enums::VEC4: createVec4(pg, a); break; case Enums::MAT3: createMat3(pg, a); break; case Enums::MAT4: createMat4(pg, a); break; case Enums::STRING: createString(pg, a); break; default: assert(false && "Missing datatype in property manager"); } }
Domain * Constructors::create(const llvm::Constant &value, const llvm::Value &place, const State *state) const { if (llvm::isa<llvm::UndefValue>(value)) return create(*value.getType()); if (llvm::isa<llvm::ConstantInt>(value)) { const llvm::ConstantInt &intValue = checkedCast<llvm::ConstantInt>(value); const llvm::APInt &i = intValue.getValue(); return createInteger(i); } if (llvm::isa<llvm::ConstantPointerNull>(value)) { const llvm::ConstantPointerNull &nullValue = checkedCast<llvm::ConstantPointerNull>(value); const llvm::PointerType &pointerType = *nullValue.getType(); Domain *constPointer = createPointer(pointerType); constPointer->setZero(&place); return constPointer; } if (llvm::isa<llvm::ConstantExpr>(value)) { const llvm::ConstantExpr &exprValue = checkedCast<llvm::ConstantExpr>(value); return createConstantExpr(exprValue, place, state); } if (llvm::isa<llvm::ConstantFP>(value)) { const llvm::ConstantFP &fp = checkedCast<llvm::ConstantFP>(value); const llvm::APFloat &number = fp.getValueAPF(); return createFloat(number); } if (llvm::isa<llvm::ConstantStruct>(value)) { const llvm::ConstantStruct &structValue = checkedCast<llvm::ConstantStruct>(value); uint64_t elementCount = structValue.getType()->getNumElements(); std::vector<Domain*> members; for (uint64_t i = 0; i < elementCount; ++i) { members.push_back(create(*structValue.getOperand(i), *structValue.getOperand(i), state)); } return createStructure(*structValue.getType(), members); } if (llvm::isa<llvm::ConstantVector>(value)) { const llvm::ConstantVector &vectorValue = checkedCast<llvm::ConstantVector>(value); // VectorType::getNumElements returns unsigned int. unsigned elementCount = vectorValue.getType()->getNumElements(); std::vector<Domain*> values; for (unsigned i = 0; i < elementCount; ++i) { values.push_back(create(*vectorValue.getOperand(i), *vectorValue.getOperand(i), state)); } return createArray(*vectorValue.getType(), values); } if (llvm::isa<llvm::ConstantArray>(value)) { const llvm::ConstantArray &arrayValue = checkedCast<llvm::ConstantArray>(value); // ArrayType::getNumElements returns uint64_t. uint64_t elementCount = arrayValue.getType()->getNumElements(); std::vector<Domain*> values; for (uint64_t i = 0; i < elementCount; ++i) { values.push_back(create(*arrayValue.getOperand(i), *arrayValue.getOperand(i), state)); } return createArray(*arrayValue.getType(), values); } #if (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 1) || LLVM_VERSION_MAJOR > 3 // llvm::isa<llvm::ConstantDataSequential> returns false for an // llvm::ConstantDataArray/Vector instance at least on on LLVM // 3.1. if (llvm::isa<llvm::ConstantDataVector>(value) || llvm::isa<llvm::ConstantDataArray>(value)) { const llvm::ConstantDataSequential &sequentialValue = checkedCast<llvm::ConstantDataSequential>(value); unsigned elementCount = sequentialValue.getNumElements(); std::vector<Domain*> values; for (unsigned i = 0; i < elementCount; ++i) { values.push_back(create(*sequentialValue.getElementAsConstant(i), place, state)); } return createArray(*sequentialValue.getType(), values); } #endif if (llvm::isa<llvm::ConstantAggregateZero>(value)) { const llvm::Type *type = value.getType(); Domain *result = Constructors::create(*type); result->setZero(&place); return result; } if (llvm::isa<llvm::Function>(value)) { const llvm::Function &functionValue = checkedCast<llvm::Function>(value); Domain *constPointer; constPointer = createPointer(*llvm::PointerType::getUnqual( functionValue.getFunctionType())); Pointer::Utils::addTarget(*constPointer, Pointer::Target::Function, &place, &value, std::vector<Domain*>(), NULL); return constPointer; } CANAL_NOT_IMPLEMENTED(); }