aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/utils/beam_makeops
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2017-11-03 10:51:36 +0100
committerBjörn Gustavsson <[email protected]>2017-11-06 15:43:49 +0100
commit6808aa38b1845bf4bc92ed1cbf6005911e95385a (patch)
tree99ca33114edec0b4dd755526414ad5bfac20dd16 /erts/emulator/utils/beam_makeops
parent83b87392042cb138c3761ad009e30784fb551b76 (diff)
downloadotp-6808aa38b1845bf4bc92ed1cbf6005911e95385a.tar.gz
otp-6808aa38b1845bf4bc92ed1cbf6005911e95385a.tar.bz2
otp-6808aa38b1845bf4bc92ed1cbf6005911e95385a.zip
Add the built-in macro $IF() to handle conditionals
Add an $IF() macro to conditionally expand macros. Use it like this: $IF(Expression, IfTrue, IfFalse) Expression is a Perl expression that can be evaulated at macro expansion time. If the expression evaluates to 0, the result will be IfFalse, otherwise IfTrue.
Diffstat (limited to 'erts/emulator/utils/beam_makeops')
-rwxr-xr-xerts/emulator/utils/beam_makeops13
1 files changed, 11 insertions, 2 deletions
diff --git a/erts/emulator/utils/beam_makeops b/erts/emulator/utils/beam_makeops
index 452e070482..dc4da0cca0 100755
--- a/erts/emulator/utils/beam_makeops
+++ b/erts/emulator/utils/beam_makeops
@@ -309,7 +309,8 @@ if ($wordsize == 64) {
my %predef_macros =
(IS_PACKED => ['Expr'],
OPERAND_POSITION => ['Expr'],
-);
+ IF => ['Expr','IfTrue','IfFalse'],
+ );
foreach my $name (keys %predef_macros) {
my @args = @{$predef_macros{$name}};
my $body = join(':', map { '$' . $_ } @args);
@@ -1659,9 +1660,17 @@ sub expand_macro {
}
} elsif ($name eq 'IS_PACKED') {
$body = ($body =~ /^I\[\d+\]$/) ? 0 : 1;
+ } elsif ($name eq 'IF') {
+ my $expr = $new_bindings{Expr};
+ my $bool = eval $expr;
+ if ($@ ne '') {
+ &error("bad expression '$expr' in \$IF()");
+ }
+ my $part = $bool ? 'IfTrue' : 'IfFalse';
+ $body = $new_bindings{$part};
}
- # Wrap body if needed and return resul.t
+ # Wrap body if needed and return result.
$body = "do {\n$body\n} while (0)"
if needs_do_wrapper($body);
($body,$rest);