2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2012-02-04 13:50:25 +00:00
|
|
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2021-12-28 18:18:17 +00:00
|
|
|
/* Note: Various places expect the allocated memory to be zeroed. */
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline void * allocBytes(size_t n)
|
|
|
|
{
|
|
|
|
void * p;
|
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
p = GC_MALLOC(n);
|
|
|
|
#else
|
|
|
|
p = calloc(n, 1);
|
|
|
|
#endif
|
|
|
|
if (!p) throw std::bad_alloc();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
Value * EvalState::allocValue()
|
|
|
|
{
|
2022-01-05 00:48:26 +00:00
|
|
|
#if HAVE_BOEHMGC
|
2021-12-28 18:18:17 +00:00
|
|
|
/* We use the boehm batch allocator to speed up allocations of Values (of which there are many).
|
|
|
|
GC_malloc_many returns a linked list of objects of the given size, where the first word
|
|
|
|
of each object is also the pointer to the next object in the list. This also means that we
|
|
|
|
have to explicitly clear the first word of every object we take. */
|
|
|
|
if (!*valueAllocCache) {
|
|
|
|
*valueAllocCache = GC_malloc_many(sizeof(Value));
|
|
|
|
if (!*valueAllocCache) throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GC_NEXT is a convenience macro for accessing the first word of an object.
|
|
|
|
Take the first list item, advance the list to the next item, and clear the next pointer. */
|
|
|
|
void * p = *valueAllocCache;
|
|
|
|
*valueAllocCache = GC_NEXT(p);
|
|
|
|
GC_NEXT(p) = nullptr;
|
2022-01-05 00:48:26 +00:00
|
|
|
#else
|
|
|
|
void * p = allocBytes(sizeof(Value));
|
|
|
|
#endif
|
2021-12-28 18:18:17 +00:00
|
|
|
|
|
|
|
nrValues++;
|
|
|
|
return (Value *) p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
Env & EvalState::allocEnv(size_t size)
|
|
|
|
{
|
|
|
|
nrEnvs++;
|
|
|
|
nrValuesInEnvs += size;
|
|
|
|
|
|
|
|
Env * env;
|
|
|
|
|
2022-01-05 00:48:26 +00:00
|
|
|
#if HAVE_BOEHMGC
|
|
|
|
if (size == 1) {
|
2021-12-28 18:18:17 +00:00
|
|
|
/* see allocValue for explanations. */
|
|
|
|
if (!*env1AllocCache) {
|
|
|
|
*env1AllocCache = GC_malloc_many(sizeof(Env) + sizeof(Value *));
|
|
|
|
if (!*env1AllocCache) throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void * p = *env1AllocCache;
|
|
|
|
*env1AllocCache = GC_NEXT(p);
|
|
|
|
GC_NEXT(p) = nullptr;
|
|
|
|
env = (Env *) p;
|
2022-01-05 00:48:26 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *));
|
2021-12-28 18:18:17 +00:00
|
|
|
|
|
|
|
env->type = Env::Plain;
|
|
|
|
|
|
|
|
/* We assume that env->values has been cleared by the allocator; maybeThunk() and lookupVar fromWith expect this. */
|
|
|
|
|
|
|
|
return *env;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2022-03-04 18:31:59 +00:00
|
|
|
void EvalState::forceValue(Value & v, const PosIdx pos)
|
2022-02-03 23:31:33 +00:00
|
|
|
{
|
|
|
|
forceValue(v, [&]() { return pos; });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Callable>
|
|
|
|
void EvalState::forceValue(Value & v, Callable getPos)
|
2012-02-04 13:50:25 +00:00
|
|
|
{
|
2020-12-12 01:15:11 +00:00
|
|
|
if (v.isThunk()) {
|
2013-03-14 16:21:13 +00:00
|
|
|
Env * env = v.thunk.env;
|
|
|
|
Expr * expr = v.thunk.expr;
|
2012-02-04 13:50:25 +00:00
|
|
|
try {
|
2020-12-18 13:38:49 +00:00
|
|
|
v.mkBlackhole();
|
2012-02-04 13:50:25 +00:00
|
|
|
//checkInterrupt();
|
2013-03-14 16:21:13 +00:00
|
|
|
expr->eval(*this, *env, v);
|
2017-06-20 10:11:22 +00:00
|
|
|
} catch (...) {
|
2020-12-18 13:38:49 +00:00
|
|
|
v.mkThunk(env, expr);
|
2012-02-04 13:50:25 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 01:15:11 +00:00
|
|
|
else if (v.isApp())
|
2014-04-04 15:53:52 +00:00
|
|
|
callFunction(*v.app.left, *v.app.right, v, noPos);
|
2020-12-12 01:15:11 +00:00
|
|
|
else if (v.isBlackhole())
|
2023-01-19 12:23:04 +00:00
|
|
|
error("infinite recursion encountered").atPos(getPos()).template debugThrow<EvalError>();
|
2012-02-04 13:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 12:23:04 +00:00
|
|
|
inline void EvalState::forceAttrs(Value & v, const PosIdx pos, std::string_view errorCtx)
|
2014-04-04 17:11:40 +00:00
|
|
|
{
|
2023-01-19 12:23:04 +00:00
|
|
|
forceAttrs(v, [&]() { return pos; }, errorCtx);
|
2022-02-03 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Callable>
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 12:23:04 +00:00
|
|
|
inline void EvalState::forceAttrs(Value & v, Callable getPos, std::string_view errorCtx)
|
2022-02-03 23:31:33 +00:00
|
|
|
{
|
2023-01-19 12:23:04 +00:00
|
|
|
forceValue(v, noPos);
|
|
|
|
if (v.type() != nAttrs) {
|
|
|
|
PosIdx pos = getPos();
|
|
|
|
error("value is %1% while a set was expected", showType(v)).withTrace(pos, errorCtx).debugThrow<TypeError>();
|
|
|
|
}
|
2014-04-04 17:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-28 18:18:17 +00:00
|
|
|
[[gnu::always_inline]]
|
2023-01-19 12:23:04 +00:00
|
|
|
inline void EvalState::forceList(Value & v, const PosIdx pos, std::string_view errorCtx)
|
2014-04-04 17:05:36 +00:00
|
|
|
{
|
2023-01-19 12:23:04 +00:00
|
|
|
forceValue(v, noPos);
|
|
|
|
if (!v.isList()) {
|
|
|
|
error("value is %1% while a list was expected", showType(v)).withTrace(pos, errorCtx).debugThrow<TypeError>();
|
|
|
|
}
|
2014-04-04 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:58:19 +00:00
|
|
|
|
2012-02-04 13:50:25 +00:00
|
|
|
}
|