From 8a6a14e1f5006543f63117fb1f0a81a0b1024ebd Mon Sep 17 00:00:00 2001 From: volth Date: Sat, 12 May 2018 16:50:39 +0000 Subject: [PATCH 1/5] add `mod' and bitwise builtins --- src/libexpr/parser.y | 7 ++++++ src/libexpr/primops.cc | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index eee48887d..6beb28868 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -338,6 +338,13 @@ expr_op | expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__sub")), $1), $3); } | expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mul")), $1), $3); } | expr_op '/' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__div")), $1), $3); } + | expr_op '%' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mod")), $1), $3); } + | expr_op '&' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_and")), $1), $3); } + | expr_op '|' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_or")), $1), $3); } + | expr_op '^' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_xor")), $1), $3); } + | expr_op '<<' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_shl")), $1), $3); } + | expr_op '>>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_unsigned_shr")), $1), $3); } + | expr_op '>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_signed_shr")), $1), $3); } | expr_op CONCAT expr_op { $$ = new ExprOpConcatLists(CUR_POS, $1, $3); } | expr_app ; diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 57dc7bd12..fa17f06b4 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,6 +1676,52 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } +static void prim_mod(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + NixFloat f2 = state.forceFloat(*args[1], pos); + if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos); + + if (args[0]->type == tFloat || args[1]->type == tFloat) { + mkFloat(v, state.forceFloat(*args[0], pos) % state.forceFloat(*args[1], pos)); + } else { + NixInt i1 = state.forceInt(*args[0], pos); + NixInt i2 = state.forceInt(*args[1], pos); + /* Avoid division overflow as it might raise SIGFPE. */ + if (i1 == std::numeric_limits::min() && i2 == -1) + throw EvalError(format("overflow in integer division, at %1%") % pos); + mkInt(v, i1 % i2); + } +} + +static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); +} + +static void prim_bin_or(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) | state.forceInt(*args[1], pos)); +} + +static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); +} + +static void prim_bin_shl(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) << (state.forceInt(*args[1], pos) & 63)); +} + +static void prim_bin_unsigned_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, (unsigned long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); +} + +static void prim_bin_signed_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, (signed long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); +} static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v) { @@ -2221,6 +2267,13 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); + addPrimOp("__mod", 2, prim_mod); + addPrimOp("__bin_and", 2, prim_bin_and); + addPrimOp("__bin_or", 2, prim_bin_or); + addPrimOp("__bin_xor", 2, prim_bin_xor); + addPrimOp("__bin_shl", 2, prim_bin_shl); + addPrimOp("__bin_unsigned_shr", 2, prim_bin_unsigned_shr); + addPrimOp("__bin_signed_shr", 2, prim_bin_signed_shr); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation From 49b7cf1813d71b2c9d09f7c69408061774c01c51 Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 06:55:24 +0000 Subject: [PATCH 2/5] add `mod' and bitwise builtins: remove infix functions --- src/libexpr/parser.y | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 6beb28868..eee48887d 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -338,13 +338,6 @@ expr_op | expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__sub")), $1), $3); } | expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mul")), $1), $3); } | expr_op '/' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__div")), $1), $3); } - | expr_op '%' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mod")), $1), $3); } - | expr_op '&' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_and")), $1), $3); } - | expr_op '|' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_or")), $1), $3); } - | expr_op '^' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_xor")), $1), $3); } - | expr_op '<<' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_shl")), $1), $3); } - | expr_op '>>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_unsigned_shr")), $1), $3); } - | expr_op '>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_signed_shr")), $1), $3); } | expr_op CONCAT expr_op { $$ = new ExprOpConcatLists(CUR_POS, $1, $3); } | expr_app ; From f3c090f91ce5e188151d887e3b5806239f5e6c0a Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 06:57:11 +0000 Subject: [PATCH 3/5] add `mod' and bitwise builtins: remove `mod' and shifts --- src/libexpr/primops.cc | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index fa17f06b4..8514e804f 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,23 +1676,6 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } -static void prim_mod(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - NixFloat f2 = state.forceFloat(*args[1], pos); - if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos); - - if (args[0]->type == tFloat || args[1]->type == tFloat) { - mkFloat(v, state.forceFloat(*args[0], pos) % state.forceFloat(*args[1], pos)); - } else { - NixInt i1 = state.forceInt(*args[0], pos); - NixInt i2 = state.forceInt(*args[1], pos); - /* Avoid division overflow as it might raise SIGFPE. */ - if (i1 == std::numeric_limits::min() && i2 == -1) - throw EvalError(format("overflow in integer division, at %1%") % pos); - mkInt(v, i1 % i2); - } -} - static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); @@ -1708,21 +1691,6 @@ static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Val mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); } -static void prim_bin_shl(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, state.forceInt(*args[0], pos) << (state.forceInt(*args[1], pos) & 63)); -} - -static void prim_bin_unsigned_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, (unsigned long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); -} - -static void prim_bin_signed_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, (signed long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); -} - static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v) { state.forceValue(*args[0]); @@ -2267,13 +2235,9 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); - addPrimOp("__mod", 2, prim_mod); addPrimOp("__bin_and", 2, prim_bin_and); addPrimOp("__bin_or", 2, prim_bin_or); addPrimOp("__bin_xor", 2, prim_bin_xor); - addPrimOp("__bin_shl", 2, prim_bin_shl); - addPrimOp("__bin_unsigned_shr", 2, prim_bin_unsigned_shr); - addPrimOp("__bin_signed_shr", 2, prim_bin_signed_shr); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation From 6cc28c0589a0072e94128976ddbf8b2fbfd9f496 Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 10:52:19 +0000 Subject: [PATCH 4/5] add `mod' and bitwise builtins: camel-case function names --- src/libexpr/primops.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 8514e804f..9dab8ecb0 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,17 +1676,17 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } -static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitAnd(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); } -static void prim_bin_or(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitOr(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) | state.forceInt(*args[1], pos)); } -static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitXor(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); } @@ -2235,9 +2235,9 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); - addPrimOp("__bin_and", 2, prim_bin_and); - addPrimOp("__bin_or", 2, prim_bin_or); - addPrimOp("__bin_xor", 2, prim_bin_xor); + addPrimOp("__bitAnd", 2, prim_bitAnd); + addPrimOp("__bitOr", 2, prim_bitOr); + addPrimOp("__bitXor", 2, prim_bitXor); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation From 88c1ea30e461092db713e58fe7521c5c43c52212 Mon Sep 17 00:00:00 2001 From: volth Date: Thu, 24 May 2018 12:48:48 +0000 Subject: [PATCH 5/5] add docs and tests --- doc/manual/expressions/builtins.xml | 30 +++++++++++++++++++++++++++++ tests/lang/eval-okay-arithmetic.exp | 2 +- tests/lang/eval-okay-arithmetic.nix | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/doc/manual/expressions/builtins.xml b/doc/manual/expressions/builtins.xml index ac1fe7e2f..5489fab00 100644 --- a/doc/manual/expressions/builtins.xml +++ b/doc/manual/expressions/builtins.xml @@ -92,6 +92,36 @@ available as builtins.derivation. + builtins.bitAnd + e1 e2 + + Return the bitwise AND of the integers + e1 and + e2. + + + + + builtins.bitOr + e1 e2 + + Return the bitwise OR of the integers + e1 and + e2. + + + + + builtins.bitXor + e1 e2 + + Return the bitwise XOR of the integers + e1 and + e2. + + + + builtins The set builtins contains all diff --git a/tests/lang/eval-okay-arithmetic.exp b/tests/lang/eval-okay-arithmetic.exp index b195055b7..5c54d10b7 100644 --- a/tests/lang/eval-okay-arithmetic.exp +++ b/tests/lang/eval-okay-arithmetic.exp @@ -1 +1 @@ -2188 +2216 diff --git a/tests/lang/eval-okay-arithmetic.nix b/tests/lang/eval-okay-arithmetic.nix index bbbbc4691..7e9e6a0b6 100644 --- a/tests/lang/eval-okay-arithmetic.nix +++ b/tests/lang/eval-okay-arithmetic.nix @@ -26,6 +26,10 @@ let { (56088 / 123 / 2) (3 + 4 * const 5 0 - 6 / id 2) + (builtins.bitAnd 12 10) # 0b1100 & 0b1010 = 8 + (builtins.bitOr 12 10) # 0b1100 | 0b1010 = 14 + (builtins.bitXor 12 10) # 0b1100 ^ 0b1010 = 6 + (if 3 < 7 then 1 else err) (if 7 < 3 then err else 1) (if 3 < 3 then err else 1)