diff --git a/NEWS.md b/NEWS.md index 7adabb8cd6b8..36952fdaf582 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,1423 +1,1432 @@ # News +## 6.2.2 + +This is a production release that fixes a bug. + +The bug was that if an array element was used as a parameter, and then a later +parameter had the same name as the array whose element was used, `bc` would grab +the element from the new array parameter, not the actual element from before the +function call. + ## 6.2.1 This is a production release with one bug fix for a memory bug in history. ## 6.2.0 This is a production release with a new feature and a few bug fixes. The bug fixes include: * A crash when `bc` and `dc` are built using editline, but history is not activated. * A missing local in the `uint*()` family of functions in the extended math library. * A failure to clear the tail call list in `dc` on error. * A crash when attempting to swap characters in command-line history when no characters exist. * `SIGWINCH` was activated even when history was not. The new feature is that stack traces are now given for runtime errors. In debug mode, the C source file and line of errors are given as well. ## 6.1.1 This is a production release that fixes a build issue with predefined builds and generated tests. ## 6.1.0 This is a production release that fixes a discrepancy from the `bc` standard, a couple of memory bugs, and adds new features. The discrepancy from the `bc` standard was with regards to the behavior of the `quit` command. This `bc` used to quit whenever it encountered `quit` during parsing, even if it was parsing a full file. Now, `bc` only quits when encountering `quit` *after* it has executed all executable statements up to that point. This behavior is slightly different from GNU `bc`, but users will only notice the difference if they put `quit` on the same line as other statements. The first memory bug could be reproduced by assigning a string to a non-local variable in a function, then redefining the function with use of the same non-local variable, which would still refer to a string in the previous version of the function. The second memory bug was caused by passing an array argument to the `asciify()` built-in function. In certain cases, that was wrongly allowed, and the interpreter just assumed everything was correct and accessed memory. Now that arrays are allowed as arguments (see below), this is not an issue. The first feature was the addition of the `is_number()` built-in function (`u` in `dc`) that returns 1 if the runtime argument is a number and 0 otherwise. The second feature was the addition of the `is_string()` built-in function (`t` in `dc`) that returns 1 if the runtime argument is a string and 0 otherwise. These features were added because I realized that type-checking is necessary now that strings can be assigned to variables in `bc` and because they've always been assignable to variables in `dc`. The last added feature is the ability of the `asciify()` built-in function in `bc` to convert a full array of numbers into a string. This means that character-by-character printing will not be necessary, and more strings than just single-character ones will be able to be created. ## 6.0.4 This is a production release that most users will not need to upgrade to. This fixes a build bug for `bcl` only on OpenBSD. Users that do not need `bcl` or have not run into build errors with `bcl` do ***NOT*** need to upgrade. ## 6.0.3 This is a production release that fixes a build bug for cross-compilation. Users that do not need cross-compilation do ***NOT*** need to upgrade. ## 6.0.2 This is a production release that fixes two bugs: * The `-l` option overrode the `-S` option. * A double-free and crash when sending a `SIGINT` while executing expressions given on the command-line. ## 6.0.1 This is a production release that fixes memory bugs and memory leaks in `bcl`. Users that do not use `bcl` (use only `bc` and/or `dc`) do ***NOT*** need to upgrade. These happened because I was unaware that the `bcl` test was not hooked into the Valgrind test infrastructure. Then, when I ran the release script, which tests everything under Valgrind (or so I thought), it caught nothing, and I thought it was safe. But it was not. Nevertheless, I have now run it under Valgrind and fixed all of the memory bugs (caused by not using `memset()` where I should have but previously didn't have to) and memory leaks. ## 6.0.0 This is a production release that fixes an oversight in the `bc` parser (that sometimes caused the wrong error message) and adds a feature for compatibility with the BSD `bc` and `dc`: turning off digit clamping when parsing numbers. The default for clamping can be set during the build (see the [build manual][13]), it can be set with the `BC_DIGIT_CLAMP` and `DC_DIGIT_CLAMP` environment variables, and it can be set with the `-c` and `-C` command-line options. Turning off clamping was also added to the `bcl` library. In addition, signal handling was removed from the `bcl` library in order to add the capability for multi-threading. This required a major version bump. I apologize to all library users (I don't know of any), but signals and threads do not play well together. To help with building, a convenience option (`-p`) to `configure.sh` was added to build a `bc` and `dc` that is by default compatible with either the BSD `bc` and `dc` or the GNU `bc` and `dc`. ## 5.3.3 This is a production release that fixes a build problem in the FreeBSD base system. All other users do **NOT** need to upgrade. ## 5.3.2 This is a production release that fixes prompt bugs with editline and readline where the `BC_PROMPT` environment variable was not being respected. This also fixes editline and readline output on `EOF`. ## 5.3.1 This is a production release that fixes a build problem in the FreeBSD base system, as well as a problem in the `en_US` locale. If you don't have problems with either, you do not need to upgrade. ## 5.3.0 This is a production release that adds features and has a few bug fixes. First, support for editline and readline history has been added. To use editline, pass `-e` to `configure.sh`, and to use readline, pass `-r`. Second, history support for Windows has been fixed and re-enabled. Third, command-line options to set `scale`, `ibase`, `obase`, and `seed` were added. This was requested long ago, and I originally disagreed with the idea. Fourth, the manuals had typos and were missing information. That has been fixed. Fifth, the manuals received different formatting to be more readable as manpages. ## 5.2.5 This is a production release that fixes this `bc`'s behavior on `^D` to match GNU `bc`. ## 5.2.4 This is a production release that fixes two bugs in history: * Without prompt, the cursor could not be placed on the first character in a line. * Home and End key handling in `tmux` was fixed. Any users that do not care about these improvements do not need to upgrade. ## 5.2.3 This is a production release that fixes one bug, a parse error when passing a file to `bc` using `-f` if that file had a multiline comment or string in it. ## 5.2.2 This is a production release that fixes one bug, a segmentation fault if `argv[0]` equals `NULL`. This is not a critical bug; there will be no vulnerability as far as I can tell. There is no need to update if you do not wish to. ## 5.2.1 This is a production release that fixes two parse bugs when in POSIX standard mode. One of these bugs was due to a quirk of the POSIX grammar, and the other was because `bc` was too strict. ## 5.2.0 This is a production release that adds a new feature, fixes some bugs, and adds out-of-source builds and a `pkg-config` file for `bcl`. The new feature is the ability to turn off exiting on expressions. It is also possible to set the default using `configure.sh`. This behavior used to exist with the `BC_EXPR_EXIT` environment variable, which is now used again. Bugs fixed include: * Some possible race conditions with error handling. * Install and uninstall targets for `bcl` did not work. ## 5.1.1 This is a production release that completes a bug fix from `5.1.0`. The bug exists in all versions of `bc`. The bug was that `if` statements without `else` statements would not be handled correctly at the end of files or right before a function definition. ## 5.1.0 This is a production release with some fixes and new features. * Fixed a bug where an `if` statement without an `else` before defining a function caused an error. * Fixed a bug with the `bc` banner and `-q`. * Fixed a bug on Windows where files were not read correctly. * Added a command-line flag (`-z`) to make `bc` and `dc` print leading zeroes on numbers `-1 < x < 1`. * Added four functions to `lib2.bc` (`plz()`, `plznl()`, `pnlz()`, and `pnlznl()`) to allow printing numbers with or without leading zeros, despite the use of `-z` or not. * Added builtin functions to query global state like line length, global stacks, and leading zeroes. * Added a command-line flag (`-L`) to disable wrapping when printing numbers. * Improved builds on Windows. ## 5.0.2 This is a production release with one fix for a flaky test. If you have not experienced problems with the test suite, you do ***NOT*** need to upgrade. The test was one that tested whether `bc` fails gracefully when it can't allocate memory. Unfortunately, there are cases when Linux and FreeBSD lie and pretend to allocate the memory. The reason they do this is because a lot of programs don't use all of the memory they allocate, so those OS's usually get away with it. However, this `bc` uses all of the memory it allocates (at least at page granularity), so when it tries to use the memory, FreeBSD and Linux kill it. This only happens sometimes, however. Other times (on my machine), they do, in fact, refuse the request. So I changed the test to not test for that because I think the graceful failure code won't really change much. ## 5.0.1 This is a production release with two fixes: * Fix for the build on Mac OSX. * Fix for the build on Android. Users that do not use those platforms do ***NOT*** need to update. ## 5.0.0 This is a major production release with several changes: * Added support for OpenBSD's `pledge()` and `unveil()`. * Fixed print bug where a backslash newline combo was printed even if only one digit was left, something I blindly copied from GNU `bc`, like a fool. * Fixed bugs in the manuals. * Fixed a possible multiplication overflow in power. * Temporary numbers are garbage collected if allocation fails, and the allocation is retried. This is to make `bc` and `dc` more resilient to running out of memory. * Limited the number of temporary numbers and made the space for them static so that allocating more space for them cannot fail. * Allowed integers with non-zero `scale` to be used with power, places, and shift operators. * Added greatest common divisor and least common multiple to `lib2.bc`. * Added `SIGQUIT` handling to history. * Added a command to `dc` (`y`) to get the length of register stacks. * Fixed multi-digit bugs in `lib2.bc`. * Removed the no prompt build option. * Created settings that builders can set defaults for and users can set their preferences for. This includes the `bc` banner, resetting on `SIGINT`, TTY mode, and prompt. * Added history support to Windows. * Fixed bugs with the handling of register names in `dc`. * Fixed bugs with multi-line comments and strings in both calculators. * Added a new error type and message for `dc` when register stacks don't have enough items. * Optimized string allocation. * Made `bc` and `dc` UTF-8 capable. * Fixed a bug with `void` functions. * Fixed a misspelled symbol in `bcl`. This is technically a breaking change, which requires this to be `5.0.0`. * Added the ability for users to get the copyright banner back. * Added the ability for users to have `bc` and `dc` quit on `SIGINT`. * Added the ability for users to disable prompt and TTY mode by environment variables. * Added the ability for users to redefine keywords. This is another reason this is `5.0.0`. * Added `dc`'s modular exponentiation and divmod to `bc`. * Added the ability to assign strings to variables and array elements and pass them to functions in `bc`. * Added `dc`'s asciify command and stream printing to `bc`. * Added a command to `dc` (`Y`) to get the length of an array. * Added a command to `dc` (`,`) to get the depth of the execution stack. * Added bitwise and, or, xor, left shift, right shift, reverse, left rotate, right rotate, and mod functions to `lib2.bc`. * Added the functions `s2u(x)` and `s2un(x,n)`, to `lib2.bc`. ## 4.0.2 This is a production release that fixes two bugs: 1. If no files are used and the first statement on `stdin` is invalid, `scale` would not be set to `20` even if `-l` was used. 2. When using history, `bc` failed to respond properly to `SIGSTOP` and `SIGTSTP`. ## 4.0.1 This is a production release that only adds one thing: flushing output when it is printed with a print statement. ## 4.0.0 This is a production release with many fixes, a new command-line option, and a big surprise: * A bug was fixed in `dc`'s `P` command where the item on the stack was *not* popped. * Various bugs in the manuals have been fixed. * A known bug was fixed where history did not interact well with prompts printed by user code without newlines. * A new command-line option, `-R` and `--no-read-prompt` was added to disable just the prompt when using `read()` (`bc`) or `?` (`dc`). * And finally, **official support for Windows was added**. The last item is why this is a major version bump. Currently, only one set of build options (extra math and prompt enabled, history and NLS/locale support disabled, both calculators enabled) is supported on Windows. However, both debug and release builds are supported. In addition, Windows builds are supported for the the library (`bcl`). For more details about how to build on Windows, see the [README][5] or the [build manual][13]. ## 3.3.4 This is a production release that fixes a small bug. The bug was that output was not flushed before a `read()` call, so prompts without a newline on the end were not flushed before the `read()` call. This is such a tiny bug that users only need to upgrade if they are affected. ## 3.3.3 This is a production release with one tweak and fixes for manuals. The tweak is that `length(0)` returns `1` instead of `0`. In `3.3.1`, I changed it so `length(0.x)`, where `x` could be any number of digits, returned the `scale`, but `length(0)` still returned `0` because I believe that `0` has `0` significant digits. After request of FreeBSD and considering the arguments of a mathematician, compatibility with other `bc`'s, and the expectations of users, I decided to make the change. The fixes for manuals fixed a bug where `--` was rendered as `-`. ## 3.3.2 This is a production release that fixes a divide-by-zero bug in `root()` in the [extended math library][16]. All previous versions with `root()` have the bug. ## 3.3.1 This is a production release that fixes a bug. The bug was in the reporting of number length when the value was 0. ## 3.3.0 This is a production release that changes one behavior and fixes documentation bugs. The changed behavior is the treatment of `-e` and `-f` when given through `BC_ENV_ARGS` or `DC_ENV_ARGS`. Now `bc` and `dc` do not exit when those options (or their equivalents) are given through those environment variables. However, `bc` and `dc` still exit when they or their equivalents are given on the command-line. ## 3.2.7 This is a production release that removes a small non-portable shell operation in `configure.sh`. This problem was only noticed on OpenBSD, not FreeBSD or Linux. Non-OpenBSD users do ***NOT*** need to upgrade, although NetBSD users may also need to upgrade. ## 3.2.6 This is a production release that fixes the build on FreeBSD. There was a syntax error in `configure.sh` that the Linux shell did not catch, and FreeBSD depends on the existence of `tests/all.sh`. All users that already upgraded to `3.2.5` should update to this release, with my apologies for the poor release of `3.2.5`. Other users should skip `3.2.5` in favor of this version. ## 3.2.5 This is a production release that fixes several bugs and adds a couple small things. The two most important bugs were bugs that causes `dc` to access memory out-of-bounds (crash in debug builds). This was found by upgrading to `afl++` from `afl`. Both were caused by a failure to distinguish between the same two cases. Another bug was the failure to put all of the licenses in the `LICENSE.md` file. Third, some warnings by `scan-build` were found and eliminated. This needed one big change: `bc` and `dc` now bail out as fast as possible on fatal errors instead of unwinding the stack. Fourth, the pseudo-random number now attempts to seed itself with `/dev/random` if `/dev/urandom` fails. Finally, this release has a few quality-of-life changes to the build system. The usage should not change at all; the only thing that changed was making sure the `Makefile.in` was written to rebuild properly when headers changed and to not rebuild when not necessary. ## 3.2.4 This is a production release that fixes a warning on `gcc` 6 or older, which does not have an attribute that is used. Users do ***NOT*** need to upgrade if they don't use `gcc` 6 or older. ## 3.2.3 This is a production release that fixes a bug in `gen/strgen.sh`. I recently changed `gen/strgen.c`, but I did not change `gen/strgen.sh`. Users that do not use `gen/strgen.sh` do not need to upgrade. ## 3.2.2 This is a production release that fixes a portability bug in `configure.sh`. The bug was using the GNU `find` extension `-wholename`. ## 3.2.1 This is a production release that has one fix for `bcl(3)`. It is technically not a bug fix since the behavior is undefined, but the `BclNumber`s that `bcl_divmod()` returns will be set to `BCL_ERROR_INVALID_NUM` if there is an error. Previously, they were not set. ## 3.2.0 This is a production release that has one bug fix and a major addition. The bug fix was a missing `auto` variable in the bessel `j()` function in the math library. The major addition is a way to build a version of `bc`'s math code as a library. This is done with the `-a` option to `configure.sh`. The API for the library can be read in `./manuals/bcl.3.md` or `man bcl` once the library is installed with `make install`. This library was requested by developers before I even finished version 1.0, but I could not figure out how to do it until now. If the library has API breaking changes, the major version of `bc` will be incremented. ## 3.1.6 This is a production release that fixes a new warning from Clang 12 for FreeBSD and also removes some possible undefined behavior found by UBSan that compilers did not seem to take advantage of. Users do ***NOT*** need to upgrade, if they do not want to. ## 3.1.5 This is a production release that fixes the Chinese locales (which caused `bc` to crash) and a crash caused by `bc` executing code when it should not have been able to. ***ALL USERS SHOULD UPGRADE.*** ## 3.1.4 This is a production release that fixes one bug, changes two behaviors, and removes one environment variable. The bug is like the one in the last release except it applies if files are being executed. I also made the fix more general. The behavior that was changed is that `bc` now exits when given `-e`, `-f`, `--expression` or `--file`. However, if the last one of those is `-f-` (using `stdin` as the file), `bc` does not exit. If `-f-` exists and is not the last of the `-e` and `-f` options (and equivalents), `bc` gives a fatal error and exits. Next, I removed the `BC_EXPR_EXIT` and `DC_EXPR_EXIT` environment variables since their use is not needed with the behavior change. Finally, I made it so `bc` does not print the header, though the `-q` and `--quiet` options were kept for compatibility with GNU `bc`. ## 3.1.3 This is a production release that fixes one minor bug: if `bc` was invoked like the following, it would error: ``` echo "if (1 < 3) 1" | bc ``` Unless users run into this bug, they do not need to upgrade, but it is suggested that they do. ## 3.1.2 This is a production release that adds a way to install *all* locales. Users do ***NOT*** need to upgrade. For package maintainers wishing to make use of the change, just pass `-l` to `configure.sh`. ## 3.1.1 This is a production release that adds two Spanish locales. Users do ***NOT*** need to upgrade, unless they want those locales. ## 3.1.0 This is a production release that adjusts one behavior, fixes eight bugs, and improves manpages for FreeBSD. Because this release fixes bugs, **users and package maintainers should update to this version as soon as possible**. The behavior that was adjusted was how code from the `-e` and `-f` arguments (and equivalents) were executed. They used to be executed as one big chunk, but in this release, they are now executed line-by-line. The first bug fix in how output to `stdout` was handled in `SIGINT`. If a `SIGINT` came in, the `stdout` buffer was not correctly flushed. In fact, a clean-up function was not getting called. This release fixes that bug. The second bug is in how `dc` handled input from `stdin`. This affected `bc` as well since it was a mishandling of the `stdin` buffer. The third fixed bug was that `bc` and `dc` could `abort()` (in debug mode) when receiving a `SIGTERM`. This one was a race condition with pushing and popping items onto and out of vectors. The fourth bug fixed was that `bc` could leave extra items on the stack and thus, not properly clean up some memory. (The memory would still get `free()`'ed, but it would not be `free()`'ed when it could have been.) The next two bugs were bugs in `bc`'s parser that caused crashes when executing the resulting code. The last two bugs were crashes in `dc` that resulted from mishandling of strings. The manpage improvement was done by switching from [ronn][20] to [Pandoc][21] to generate manpages. Pandoc generates much cleaner manpages and doesn't leave blank lines where they shouldn't be. ## 3.0.3 This is a production release that adds one new feature: specific manpages. Before this release, `bc` and `dc` only used one manpage each that referred to various build options. This release changes it so there is one manpage set per relevant build type. Each manual only has information about its particular build, and `configure.sh` selects the correct set for install. ## 3.0.2 This is a production release that adds `utf8` locale symlinks and removes an unused `auto` variable from the `ceil()` function in the [extended math library][16]. Users do ***NOT*** need to update unless they want the locales. ## 3.0.1 This is a production release with two small changes. Users do ***NOT*** need to upgrade to this release; however, if they haven't upgraded to `3.0.0` yet, it may be worthwhile to upgrade to this release. The first change is fixing a compiler warning on FreeBSD with strict warnings on. The second change is to make the new implementation of `ceil()` in `lib2.bc` much more efficient. ## 3.0.0 *Notes for package maintainers:* *First, the `2.7.0` release series saw a change in the option parsing. This made me change one error message and add a few others. The error message that was changed removed one format specifier. This means that `printf()` will seqfault on old locale files. Unfortunately, `bc` cannot use any locale files except the global ones that are already installed, so it will use the previous ones while running tests during install. **If `bc` segfaults while running arg tests when updating, it is because the global locale files have not been replaced. Make sure to either prevent the test suite from running on update or remove the old locale files before updating.** (Removing the locale files can be done with `make uninstall` or by running the [`locale_uninstall.sh`][22] script.) Once this is done, `bc` should install without problems.* *Second, **the option to build without signal support has been removed**. See below for the reasons why.* This is a production release with some small bug fixes, a few improvements, three major bug fixes, and a complete redesign of `bc`'s error and signal handling. **Users and package maintainers should update to this version as soon as possible.** The first major bug fix was in how `bc` executed files. Previously, a whole file was parsed before it was executed, but if a function is defined *after* code, especially if the function definition was actually a redefinition, and the code before the definition referred to the previous function, this `bc` would replace the function before executing any code. The fix was to make sure that all code that existed before a function definition was executed. The second major bug fix was in `bc`'s `lib2.bc`. The `ceil()` function had a bug where a `0` in the decimal place after the truncation position, caused it to output the wrong numbers if there was any non-zero digit after. The third major bug is that when passing parameters to functions, if an expression included an array (not an array element) as a parameter, it was accepted, when it should have been rejected. It is now correctly rejected. Beyond that, this `bc` got several improvements that both sped it up, improved the handling of signals, and improved the error handling. First, the requirements for `bc` were pushed back to POSIX 2008. `bc` uses one function, `strdup()`, which is not in POSIX 2001, and it is in the X/Open System Interfaces group 2001. It is, however, in POSIX 2008, and since POSIX 2008 is old enough to be supported anywhere that I care, that should be the requirement. Second, the BcVm global variable was put into `bss`. This actually slightly reduces the size of the executable from a massive code shrink, and it will stop `bc` from allocating a large set of memory when `bc` starts. Third, the default Karatsuba length was updated from 64 to 32 after making the optimization changes below, since 32 is going to be better than 64 after the changes. Fourth, Spanish translations were added. Fifth, the interpreter received a speedup to make performance on non-math-heavy scripts more competitive with GNU `bc`. While improvements did, in fact, get it much closer (see the [benchmarks][19]), it isn't quite there. There were several things done to speed up the interpreter: First, several small inefficiencies were removed. These inefficiencies included calling the function `bc_vec_pop(v)` twice instead of calling `bc_vec_npop(v, 2)`. They also included an extra function call for checking the size of the stack and checking the size of the stack more than once on several operations. Second, since the current `bc` function is the one that stores constants and strings, the program caches pointers to the current function's vectors of constants and strings to prevent needing to grab the current function in order to grab a constant or a string. Third, `bc` tries to reuse `BcNum`'s (the internal representation of arbitary-precision numbers). If a `BcNum` has the default capacity of `BC_NUM_DEF_SIZE` (32 on 64-bit and 16 on 32-bit) when it is freed, it is added to a list of available `BcNum`'s. And then, when a `BcNum` is allocated with a capacity of `BC_NUM_DEF_SIZE` and any `BcNum`'s exist on the list of reusable ones, one of those ones is grabbed instead. In order to support these changes, the `BC_NUM_DEF_SIZE` was changed. It used to be 16 bytes on all systems, but it was changed to more closely align with the minimum allocation size on Linux, which is either 32 bytes (64-bit musl), 24 bytes (64-bit glibc), 16 bytes (32-bit musl), or 12 bytes (32-bit glibc). Since these are the minimum allocation sizes, these are the sizes that would be allocated anyway, making it worth it to just use the whole space, so the value of `BC_NUM_DEF_SIZE` on 64-bit systems was changed to 32 bytes. On top of that, at least on 64-bit, `BC_NUM_DEF_SIZE` supports numbers with either 72 integer digits or 45 integer digits and 27 fractional digits. This should be more than enough for most cases since `bc`'s default `scale` values are 0 or 20, meaning that, by default, it has at most 20 fractional digits. And 45 integer digits are *a lot*; it's enough to calculate the amount of mass in the Milky Way galaxy in kilograms. Also, 72 digits is enough to calculate the diameter of the universe in Planck lengths. (For 32-bit, these numbers are either 32 integer digits or 12 integer digits and 20 fractional digits. These are also quite big, and going much bigger on a 32-bit system seems a little pointless since 12 digits is just under a trillion and 20 fractional digits is still enough for about any use since `10^-20` light years is just under a millimeter.) All of this together means that for ordinary uses, and even uses in scientific work, the default number size will be all that is needed, which means that nearly all, if not all, numbers will be reused, relieving pressure on the system allocator. I did several experiments to find the changes that had the most impact, especially with regard to reusing `BcNum`'s. One was putting `BcNum`'s into buckets according to their capacity in powers of 2 up to 512. That performed worse than `bc` did in `2.7.2`. Another was putting any `BcNum` on the reuse list that had a capacity of `BC_NUM_DEF_SIZE * 2` and reusing them for `BcNum`'s that requested `BC_NUM_DEF_SIZE`. This did reduce the amount of time spent, but it also spent a lot of time in the system allocator for an unknown reason. (When using `strace`, a bunch more `brk` calls showed up.) Just reusing `BcNum`'s that had exactly `BC_NUM_DEF_SIZE` capacity spent the smallest amount of time in both user and system time. This makes sense, especially with the changes to make `BC_NUM_DEF_SIZE` bigger on 64-bit systems, since the vast majority of numbers will only ever use numbers with a size less than or equal to `BC_NUM_DEF_SIZE`. Last of all, `bc`'s signal handling underwent a complete redesign. (This is the reason that this version is `3.0.0` and not `2.8.0`.) The change was to move from a polling approach to signal handling to an interrupt-based approach. Previously, every single loop condition had a check for signals. I suspect that this could be expensive when in tight loops. Now, the signal handler just uses `longjmp()` (actually `siglongjmp()`) to start an unwinding of the stack until it is stopped or the stack is unwound to `main()`, which just returns. If `bc` is currently executing code that cannot be safely interrupted (according to POSIX), then signals are "locked." The signal handler checks if the lock is taken, and if it is, it just sets the status to indicate that a signal arrived. Later, when the signal lock is released, the status is checked to see if a signal came in. If so, the stack unwinding starts. This design eliminates polling in favor of maintaining a stack of `jmp_buf`'s. This has its own performance implications, but it gives better interaction. And the cost of pushing and popping a `jmp_buf` in a function is paid at most twice. Most functions do not pay that price, and most of the rest only pay it once. (There are only some 3 functions in `bc` that push and pop a `jmp_buf` twice.) As a side effect of this change, I had to eliminate the use of `stdio.h` in `bc` because `stdio` does not play nice with signals and `longjmp()`. I implemented custom I/O buffer code that takes a fraction of the size. This means that static builds will be smaller, but non-static builds will be bigger, though they will have less linking time. This change is also good because my history implementation was already bypassing `stdio` for good reasons, and unifying the architecture was a win. Another reason for this change is that my `bc` should *always* behave correctly in the presence of signals like `SIGINT`, `SIGTERM`, and `SIGQUIT`. With the addition of my own I/O buffering, I needed to also make sure that the buffers were correctly flushed even when such signals happened. For this reason, I **removed the option to build without signal support**. As a nice side effect of this change, the error handling code could be changed to take advantage of the stack unwinding that signals used. This means that signals and error handling use the same code paths, which means that the stack unwinding is well-tested. (Errors are tested heavily in the test suite.) It also means that functions do not need to return a status code that ***every*** caller needs to check. This eliminated over 100 branches that simply checked return codes and then passed that return code up the stack if necessary. The code bloat savings from this is at least 1700 bytes on `x86_64`, *before* taking into account the extra code from removing `stdio.h`. ## 2.7.2 This is a production release with one major bug fix. The `length()` built-in function can take either a number or an array. If it takes an array, it returns the length of the array. Arrays can be passed by reference. The bug is that the `length()` function would not properly dereference arrays that were references. This is a bug that affects all users. **ALL USERS SHOULD UPDATE `bc`**. ## 2.7.1 This is a production release with fixes for new locales and fixes for compiler warnings on FreeBSD. ## 2.7.0 This is a production release with a bug fix for Linux, new translations, and new features. Bug fixes: * Option parsing in `BC_ENV_ARGS` was broken on Linux in 2.6.1 because `glibc`'s `getopt_long()` is broken. To get around that, and to support long options on every platform, an adapted version of [`optparse`][17] was added. Now, `bc` does not even use `getopt()`. * Parsing `BC_ENV_ARGS` with quotes now works. It isn't the smartest, but it does the job if there are spaces in file names. The following new languages are supported: * Dutch * Polish * Russian * Japanes * Simplified Chinese All of these translations were generated using [DeepL][18], so improvements are welcome. There is only one new feature: **`bc` now has a built-in pseudo-random number generator** (PRNG). The PRNG is seeded, making it useful for applications where `/dev/urandom` does not work because output needs to be reproducible. However, it also uses `/dev/urandom` to seed itself by default, so it will start with a good seed by default. It also outputs 32 bits on 32-bit platforms and 64 bits on 64-bit platforms, far better than the 15 bits of C's `rand()` and `bash`'s `$RANDOM`. In addition, the PRNG can take a bound, and when it gets a bound, it automatically adjusts to remove bias. It can also generate numbers of arbitrary size. (As of the time of release, the largest pseudo-random number generated by this `bc` was generated with a bound of `2^(2^20)`.) ***IMPORTANT: read the [`bc` manual][9] and the [`dc` manual][10] to find out exactly what guarantees the PRNG provides. The underlying implementation is not guaranteed to stay the same, but the guarantees that it provides are guaranteed to stay the same regardless of the implementation.*** On top of that, four functions were added to `bc`'s [extended math library][16] to make using the PRNG easier: * `frand(p)`: Generates a number between `[0,1)` to `p` decimal places. * `ifrand(i, p)`: Generates an integer with bound `i` and adds it to `frand(p)`. * `srand(x)`: Randomizes the sign of `x`. In other words, it flips the sign of `x` with probability `0.5`. * `brand()`: Returns a random boolean value (either `0` or `1`). ## 2.6.1 This is a production release with a bug fix for FreeBSD. The bug was that when `bc` was built without long options, it would give a fatal error on every run. This was caused by a mishandling of `optind`. ## 2.6.0 This release is a production release ***with no bugfixes***. If you do not want to upgrade, you don't have to. No source code changed; the only thing that changed was `lib2.bc`. This release adds one function to the [extended math library][16]: `p(x, y)`, which calculates `x` to the power of `y`, whether or not `y` is an integer. (The `^` operator can only accept integer powers.) This release also includes a couple of small tweaks to the [extended math library][16], mostly to fix returning numbers with too high of `scale`. ## 2.5.3 This release is a production release which addresses inconsistencies in the Portuguese locales. No `bc` code was changed. The issues were that the ISO files used different naming, and also that the files that should have been symlinks were not. I did not catch that because GitHub rendered them the exact same way. ## 2.5.2 This release is a production release. No code was changed, but the build system was changed to allow `CFLAGS` to be given to `CC`, like this: ``` CC="gcc -O3 -march=native" ./configure.sh ``` If this happens, the flags are automatically put into `CFLAGS`, and the compiler is set appropriately. In the example above this means that `CC` will be "gcc" and `CFLAGS` will be "-O3 -march=native". This behavior was added to conform to GNU autotools practices. ## 2.5.1 This is a production release which addresses portability concerns discovered in the `bc` build system. No `bc` code was changed. * Support for Solaris SPARC and AIX were added. * Minor documentations edits were performed. * An option for `configure.sh` was added to disable long options if `getopt_long()` is missing. ## 2.5.0 This is a production release with new translations. No code changed. The translations were contributed by [bugcrazy][15], and they are for Portuguese, both Portugal and Brazil locales. ## 2.4.0 This is a production release primarily aimed at improving `dc`. * A couple of copy and paste errors in the [`dc` manual][10] were fixed. * `dc` startup was optimized by making sure it didn't have to set up `bc`-only things. * The `bc` `&&` and `||` operators were made available to `dc` through the `M` and `m` commands, respectively. * `dc` macros were changed to be tail call-optimized. The last item, tail call optimization, means that if the last thing in a macro is a call to another macro, then the old macro is popped before executing the new macro. This change was made to stop `dc` from consuming more and more memory as macros are executed in a loop. The `q` and `Q` commands still respect the "hidden" macros by way of recording how many macros were removed by tail call optimization. ## 2.3.2 This is a production release meant to fix warnings in the Gentoo `ebuild` by making it possible to disable binary stripping. Other users do *not* need to upgrade. ## 2.3.1 This is a production release. It fixes a bug that caused `-1000000000 < -1` to return `0`. This only happened with negative numbers and only if the value on the left was more negative by a certain amount. That said, this bug *is* a bad bug, and needs to be fixed. **ALL USERS SHOULD UPDATE `bc`**. ## 2.3.0 This is a production release with changes to the build system. ## 2.2.0 This release is a production release. It only has new features and performance improvements. 1. The performance of `sqrt(x)` was improved. 2. The new function `root(x, n)` was added to the extended math library to calculate `n`th roots. 3. The new function `cbrt(x)` was added to the extended math library to calculate cube roots. ## 2.1.3 This is a non-critical release; it just changes the build system, and in non-breaking ways: 1. Linked locale files were changed to link to their sources with a relative link. 2. A bug in `configure.sh` that caused long option parsing to fail under `bash` was fixed. ## 2.1.2 This release is not a critical release. 1. A few codes were added to history. 2. Multiplication was optimized a bit more. 3. Addition and subtraction were both optimized a bit more. ## 2.1.1 This release contains a fix for the test suite made for Linux from Scratch: now the test suite prints `pass` when a test is passed. Other than that, there is no change in this release, so distros and other users do not need to upgrade. ## 2.1.0 This release is a production release. The following bugs were fixed: 1. A `dc` bug that caused stack mishandling was fixed. 2. A warning on OpenBSD was fixed. 3. Bugs in `ctrl+arrow` operations in history were fixed. 4. The ability to paste multiple lines in history was added. 5. A `bc` bug, mishandling of array arguments to functions, was fixed. 6. A crash caused by freeing the wrong pointer was fixed. 7. A `dc` bug where strings, in a rare case, were mishandled in parsing was fixed. In addition, the following changes were made: 1. Division was slightly optimized. 2. An option was added to the build to disable printing of prompts. 3. The special case of empty arguments is now handled. This is to prevent errors in scripts that end up passing empty arguments. 4. A harmless bug was fixed. This bug was that, with the pop instructions (mostly) removed (see below), `bc` would leave extra values on its stack for `void` functions and in a few other cases. These extra items would not affect anything put on the stack and would not cause any sort of crash or even buggy behavior, but they would cause `bc` to take more memory than it needed. On top of the above changes, the following optimizations were added: 1. The need for pop instructions in `bc` was removed. 2. Extra tests on every iteration of the interpreter loop were removed. 3. Updating function and code pointers on every iteration of the interpreter loop was changed to only updating them when necessary. 4. Extra assignments to pointers were removed. Altogether, these changes sped up the interpreter by around 2x. ***NOTE***: This is the last release with new features because this `bc` is now considered complete. From now on, only bug fixes and new translations will be added to this `bc`. ## 2.0.3 This is a production, bug-fix release. Two bugs were fixed in this release: 1. A rare and subtle signal handling bug was fixed. 2. A misbehavior on `0` to a negative power was fixed. The last bug bears some mentioning. When I originally wrote power, I did not thoroughly check its error cases; instead, I had it check if the first number was `0` and then if so, just return `0`. However, `0` to a negative power means that `1` will be divided by `0`, which is an error. I caught this, but only after I stopped being cocky. You see, sometime later, I had noticed that GNU `bc` returned an error, correctly, but I thought it was wrong simply because that's not what my `bc` did. I saw it again later and had a double take. I checked for real, finally, and found out that my `bc` was wrong all along. That was bad on me. But the bug was easy to fix, so it is fixed now. There are two other things in this release: 1. Subtraction was optimized by [Stefan Eßer][14]. 2. Division was also optimized, also by Stefan Eßer. ## 2.0.2 This release contains a fix for a possible overflow in the signal handling. I would be surprised if any users ran into it because it would only happen after 2 billion (`2^31-1`) `SIGINT`'s, but I saw it and had to fix it. ## 2.0.1 This release contains very few things that will apply to any users. 1. A slight bug in `dc`'s interactive mode was fixed. 2. A bug in the test suite that was only triggered on NetBSD was fixed. 3. **The `-P`/`--no-prompt` option** was added for users that do not want a prompt. 4. A `make check` target was added as an alias for `make test`. 5. `dc` got its own read prompt: `?> `. ## 2.0.0 This release is a production release. This release is also a little different from previous releases. From here on out, I do not plan on adding any more features to this `bc`; I believe that it is complete. However, there may be bug fix releases in the future, if I or any others manage to find bugs. This release has only a few new features: 1. `atan2(y, x)` was added to the extended math library as both `a2(y, x)` and `atan2(y, x)`. 2. Locales were fixed. 3. A **POSIX shell-compatible script was added as an alternative to compiling `gen/strgen.c`** on a host machine. More details about making the choice between the two can be found by running `./configure.sh --help` or reading the [build manual][13]. 4. Multiplication was optimized by using **diagonal multiplication**, rather than straight brute force. 5. The `locale_install.sh` script was fixed. 6. `dc` was given the ability to **use the environment variable `DC_ENV_ARGS`**. 7. `dc` was also given the ability to **use the `-i` or `--interactive`** options. 8. Printing the prompt was fixed so that it did not print when it shouldn't. 9. Signal handling was fixed. 10. **Handling of `SIGTERM` and `SIGQUIT`** was fixed. 11. The **built-in functions `maxibase()`, `maxobase()`, and `maxscale()`** (the commands `T`, `U`, `V` in `dc`, respectively) were added to allow scripts to query for the max allowable values of those globals. 12. Some incompatibilities with POSIX were fixed. In addition, this release is `2.0.0` for a big reason: the internal format for numbers changed. They used to be a `char` array. Now, they are an array of larger integers, packing more decimal digits into each integer. This has delivered ***HUGE*** performance improvements, especially for multiplication, division, and power. This `bc` should now be the fastest `bc` available, but I may be wrong. ## 1.2.8 This release contains a fix for a harmless bug (it is harmless in that it still works, but it just copies extra data) in the [`locale_install.sh`][12] script. ## 1.2.7 This version contains fixes for the build on Arch Linux. ## 1.2.6 This release removes the use of `local` in shell scripts because it's not POSIX shell-compatible, and also updates a man page that should have been updated a long time ago but was missed. ## 1.2.5 This release contains some missing locale `*.msg` files. ## 1.2.4 This release contains a few bug fixes and new French translations. ## 1.2.3 This release contains a fix for a bug: use of uninitialized data. Such data was only used when outputting an error message, but I am striving for perfection. As Michelangelo said, "Trifles make perfection, and perfection is no trifle." ## 1.2.2 This release contains fixes for OpenBSD. ## 1.2.1 This release contains bug fixes for some rare bugs. ## 1.2.0 This is a production release. There have been several changes since `1.1.0`: 1. The build system had some changes. 2. Locale support has been added. (Patches welcome for translations.) 3. **The ability to turn `ibase`, `obase`, and `scale` into stacks** was added with the `-g` command-line option. (See the [`bc` manual][9] for more details.) 4. Support for compiling on Mac OSX out of the box was added. 5. The extended math library got `t(x)`, `ceil(x)`, and some aliases. 6. The extended math library also got `r2d(x)` (for converting from radians to degrees) and `d2r(x)` (for converting from degrees to radians). This is to allow using degrees with the standard library. 7. Both calculators now accept numbers in **scientific notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 8. Both calculators can **output in either scientific or engineering notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 9. Some inefficiencies were removed. 10. Some bugs were fixed. 11. Some bugs in the extended library were fixed. 12. Some defects from [Coverity Scan][11] were fixed. ## 1.1.4 This release contains a fix to the build system that allows it to build on older versions of `glibc`. ## 1.1.3 This release contains a fix for a bug in the test suite where `bc` tests and `dc` tests could not be run in parallel. ## 1.1.2 This release has a fix for a history bug; the down arrow did not work. ## 1.1.1 This release fixes a bug in the `1.1.0` build system. The source is exactly the same. The bug that was fixed was a failure to install if no `EXECSUFFIX` was used. ## 1.1.0 This is a production release. However, many new features were added since `1.0`. 1. **The build system has been changed** to use a custom, POSIX shell-compatible configure script ([`configure.sh`][6]) to generate a POSIX make-compatible `Makefile`, which means that `bc` and `dc` now build out of the box on any POSIX-compatible system. 2. Out-of-memory and output errors now cause the `bc` to report the error, clean up, and die, rather than just reporting and trying to continue. 3. **Strings and constants are now garbage collected** when possible. 4. Signal handling and checking has been made more simple and more thorough. 5. `BcGlobals` was refactored into `BcVm` and `BcVm` was made global. Some procedure names were changed to reflect its difference to everything else. 6. Addition got a speed improvement. 7. Some common code for addition and multiplication was refactored into its own procedure. 8. A bug was removed where `dc` could have been selected, but the internal `#define` that returned `true` for a query about `dc` would not have returned `true`. 9. Useless calls to `bc_num_zero()` were removed. 10. **History support was added.** The history support is based off of a [UTF-8 aware fork][7] of [`linenoise`][8], which has been customized with `bc`'s own data structures and signal handling. 11. Generating C source from the math library now removes tabs from the library, shrinking the size of the executable. 12. The math library was shrunk. 13. Error handling and reporting was improved. 14. Reallocations were reduced by giving access to the request size for each operation. 15. **`abs()` (`b` command for `dc`) was added as a builtin.** 16. Both calculators were tested on FreeBSD. 17. Many obscure parse bugs were fixed. 18. Markdown and man page manuals were added, and the man pages are installed by `make install`. 19. Executable size was reduced, though the added features probably made the executable end up bigger. 20. **GNU-style array references were added as a supported feature.** 21. Allocations were reduced. 22. **New operators were added**: `$` (`$` for `dc`), `@` (`@` for `dc`), `@=`, `<<` (`H` for `dc`), `<<=`, `>>` (`h` for `dc`), and `>>=`. See the [`bc` manual][9] and the [`dc` manual][10] for more details. 23. **An extended math library was added.** This library contains code that makes it so I can replace my desktop calculator with this `bc`. See the [`bc` manual][3] for more details. 24. Support for all capital letters as numbers was added. 25. **Support for GNU-style void functions was added.** 26. A bug fix for improper handling of function parameters was added. 27. Precedence for the or (`||`) operator was changed to match GNU `bc`. 28. `dc` was given an explicit negation command. 29. `dc` was changed to be able to handle strings in arrays. ## 1.1 Release Candidate 3 This release is the eighth release candidate for 1.1, though it is the third release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 2 This release is the seventh release candidate for 1.1, though it is the second release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 5 This release is the sixth release candidate for 1.1, though it is the fifth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 4 This release is the fifth release candidate for 1.1, though it is the fourth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 3 This release is the fourth release candidate for 1.1, though it is the third release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 2 This release is the third release candidate for 1.1, though it is the second release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 1 This release is the second release candidate for 1.1, though it is meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 1 This is the first release candidate for 1.1. The new code has not been tested as thoroughly as it should for release. ## 1.0 This is the first non-beta release. `bc` is ready for production use. As such, a lot has changed since 0.5. 1. `dc` has been added. It has been tested even more thoroughly than `bc` was for `0.5`. It does not have the `!` command, and for security reasons, it never will, so it is complete. 2. `bc` has been more thoroughly tested. An entire section of the test suite (for both programs) has been added to test for errors. 3. A prompt (`>>> `) has been added for interactive mode, making it easier to see inputs and outputs. 4. Interrupt handling has been improved, including elimination of race conditions (as much as possible). 5. MinGW and [Windows Subsystem for Linux][1] support has been added (see [xstatic][2] for binaries). 6. Memory leaks and errors have been eliminated (as far as ASan and Valgrind can tell). 7. Crashes have been eliminated (as far as [afl][3] can tell). 8. Karatsuba multiplication was added (and thoroughly) tested, speeding up multiplication and power by orders of magnitude. 9. Performance was further enhanced by using a "divmod" function to reduce redundant divisions and by removing superfluous `memset()` calls. 10. To switch between Karatsuba and `O(n^2)` multiplication, the config variable `BC_NUM_KARATSUBA_LEN` was added. It is set to a sane default, but the optimal number can be found with [`karatsuba.py`][4] (requires Python 3) and then configured through `make`. 11. The random math test generator script was changed to Python 3 and improved. `bc` and `dc` have together been run through 30+ million random tests. 12. All known math bugs have been fixed, including out of control memory allocations in `sine` and `cosine` (that was actually a parse bug), certain cases of infinite loop on square root, and slight inaccuracies (as much as possible; see the [README][5]) in transcendental functions. 13. Parsing has been fixed as much as possible. 14. Test coverage was improved to 94.8%. The only paths not covered are ones that happen when `malloc()` or `realloc()` fails. 15. An extension to get the length of an array was added. 16. The boolean not (`!`) had its precedence change to match negation. 17. Data input was hardened. 18. `bc` was made fully compliant with POSIX when the `-s` flag is used or `POSIXLY_CORRECT` is defined. 19. Error handling was improved. 20. `bc` now checks that files it is given are not directories. ## 1.0 Release Candidate 7 This is the seventh release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 6. ## 1.0 Release Candidate 6 This is the sixth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 5. ## 1.0 Release Candidate 5 This is the fifth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 4. ## 1.0 Release Candidate 4 This is the fourth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 3. ## 1.0 Release Candidate 3 This is the third release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 2. ## 1.0 Release Candidate 2 This is the second release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 1. ## 1.0 Release Candidate 1 This is the first Release Candidate for 1.0. `bc` is complete, with `dc`, but it is not tested. ## 0.5 This beta release completes more features, but it is still not complete nor tested as thoroughly as necessary. ## 0.4.1 This beta release fixes a few bugs in 0.4. ## 0.4 This is a beta release. It does not have the complete set of features, and it is not thoroughly tested. [1]: https://docs.microsoft.com/en-us/windows/wsl/install-win10 [2]: https://pkg.musl.cc/bc/ [3]: http://lcamtuf.coredump.cx/afl/ [4]: ./scripts/karatsuba.py [5]: ./README.md [6]: ./configure.sh [7]: https://github.com/rain-1/linenoise-mob [8]: https://github.com/antirez/linenoise [9]: ./manuals/bc/A.1.md [10]: ./manuals/dc/A.1.md [11]: https://scan.coverity.com/projects/gavinhoward-bc [12]: ./scripts/locale_install.sh [13]: ./manuals/build.md [14]: https://github.com/stesser [15]: https://github.com/bugcrazy [16]: ./manuals/bc/A.1.md#extended-library [17]: https://github.com/skeeto/optparse [18]: https://www.deepl.com/translator [19]: ./manuals/benchmarks.md [20]: https://github.com/apjanke/ronn-ng [21]: https://pandoc.org/ [22]: ./scripts/locale_uninstall.sh diff --git a/include/lang.h b/include/lang.h index 4ad6df88f5ed..2d9776532249 100644 --- a/include/lang.h +++ b/include/lang.h @@ -1,707 +1,713 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * Definitions for program data. * */ #ifndef BC_LANG_H #define BC_LANG_H #include // These have to come first to silence a warning on BC_C11 below. #include #include #include #if BC_C11 #include #endif // BC_C11 /// The instructions for bytecode. typedef enum BcInst { #if BC_ENABLED /// Postfix increment and decrement. Prefix are translated into /// BC_INST_ONE with either BC_INST_ASSIGN_PLUS or BC_INST_ASSIGN_MINUS. BC_INST_INC = 0, BC_INST_DEC, #endif // BC_ENABLED /// Unary negation. BC_INST_NEG, /// Boolean not. BC_INST_BOOL_NOT, #if BC_ENABLE_EXTRA_MATH /// Truncation operator. BC_INST_TRUNC, #endif // BC_ENABLE_EXTRA_MATH /// These should be self-explanatory. BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, #if BC_ENABLE_EXTRA_MATH /// Places operator. BC_INST_PLACES, /// Shift operators. BC_INST_LSHIFT, BC_INST_RSHIFT, #endif // BC_ENABLE_EXTRA_MATH /// Comparison operators. BC_INST_REL_EQ, BC_INST_REL_LE, BC_INST_REL_GE, BC_INST_REL_NE, BC_INST_REL_LT, BC_INST_REL_GT, /// Boolean or and and. BC_INST_BOOL_OR, BC_INST_BOOL_AND, #if BC_ENABLED /// Same as the normal operators, but assigment. So ^=, *=, /=, etc. BC_INST_ASSIGN_POWER, BC_INST_ASSIGN_MULTIPLY, BC_INST_ASSIGN_DIVIDE, BC_INST_ASSIGN_MODULUS, BC_INST_ASSIGN_PLUS, BC_INST_ASSIGN_MINUS, #if BC_ENABLE_EXTRA_MATH /// Places and shift assignment operators. BC_INST_ASSIGN_PLACES, BC_INST_ASSIGN_LSHIFT, BC_INST_ASSIGN_RSHIFT, #endif // BC_ENABLE_EXTRA_MATH /// Normal assignment. BC_INST_ASSIGN, /// bc and dc detect when the value from an assignment is not necessary. /// For example, a plain assignment statement means the value is never used. /// In those cases, we can get lots of performance back by not even creating /// a copy at all. In fact, it saves a copy, a push onto the results stack, /// a pop from the results stack, and a free. Definitely worth it to detect. BC_INST_ASSIGN_POWER_NO_VAL, BC_INST_ASSIGN_MULTIPLY_NO_VAL, BC_INST_ASSIGN_DIVIDE_NO_VAL, BC_INST_ASSIGN_MODULUS_NO_VAL, BC_INST_ASSIGN_PLUS_NO_VAL, BC_INST_ASSIGN_MINUS_NO_VAL, #if BC_ENABLE_EXTRA_MATH /// Same as above. BC_INST_ASSIGN_PLACES_NO_VAL, BC_INST_ASSIGN_LSHIFT_NO_VAL, BC_INST_ASSIGN_RSHIFT_NO_VAL, #endif // BC_ENABLE_EXTRA_MATH #endif // BC_ENABLED /// Normal assignment that pushes no value on the stack. BC_INST_ASSIGN_NO_VAL, /// Push a constant onto the results stack. BC_INST_NUM, /// Push a variable onto the results stack. BC_INST_VAR, /// Push an array element onto the results stack. BC_INST_ARRAY_ELEM, /// Push an array onto the results stack. This is different from pushing an /// array *element* onto the results stack; it pushes a reference to the /// whole array. This is needed in bc for function arguments that are /// arrays. It is also needed for returning the length of an array. BC_INST_ARRAY, /// Push a zero or a one onto the stack. These are special cased because it /// does help performance, particularly for one since inc/dec operators /// use it. BC_INST_ZERO, BC_INST_ONE, #if BC_ENABLED /// Push the last printed value onto the stack. BC_INST_LAST, #endif // BC_ENABLED /// Push the value of any of the globals onto the stack. BC_INST_IBASE, BC_INST_OBASE, BC_INST_SCALE, #if BC_ENABLE_EXTRA_MATH /// Push the value of the seed global onto the stack. BC_INST_SEED, #endif // BC_ENABLE_EXTRA_MATH /// These are builtin functions. BC_INST_LENGTH, BC_INST_SCALE_FUNC, BC_INST_SQRT, BC_INST_ABS, BC_INST_IS_NUMBER, BC_INST_IS_STRING, #if BC_ENABLE_EXTRA_MATH /// Another builtin function. BC_INST_IRAND, #endif // BC_ENABLE_EXTRA_MATH /// Asciify. BC_INST_ASCIIFY, /// Another builtin function. BC_INST_READ, #if BC_ENABLE_EXTRA_MATH /// Another builtin function. BC_INST_RAND, #endif // BC_ENABLE_EXTRA_MATH /// Return the max for the various globals. BC_INST_MAXIBASE, BC_INST_MAXOBASE, BC_INST_MAXSCALE, #if BC_ENABLE_EXTRA_MATH /// Return the max value returned by rand(). BC_INST_MAXRAND, #endif // BC_ENABLE_EXTRA_MATH /// bc line_length() builtin function. BC_INST_LINE_LENGTH, #if BC_ENABLED /// bc global_stacks() builtin function. BC_INST_GLOBAL_STACKS, #endif // BC_ENABLED /// bc leading_zero() builtin function. BC_INST_LEADING_ZERO, /// This is slightly misnamed versus BC_INST_PRINT_POP. Well, it is in bc. /// dc uses this instruction to print, but not pop. That's valid in dc. /// However, in bc, it is *never* valid to print without popping. In bc, /// BC_INST_PRINT_POP is used to indicate when a string should be printed /// because of a print statement or whether it should be printed raw. The /// reason for this is because a print statement handles escaped characters. /// So BC_INST_PRINT_POP is for printing a string from a print statement, /// BC_INST_PRINT_STR is for printing a string by itself. /// /// In dc, BC_INST_PRINT_POP prints and pops, and BC_INST_PRINT just prints. /// /// Oh, and BC_INST_STR pushes a string onto the results stack. BC_INST_PRINT, BC_INST_PRINT_POP, BC_INST_STR, #if BC_ENABLED BC_INST_PRINT_STR, /// Jumps unconditionally. BC_INST_JUMP, /// Jumps if the top of the results stack is zero (condition failed). It /// turns out that we only want to jump when conditions fail to "skip" code. BC_INST_JUMP_ZERO, /// Call a function. BC_INST_CALL, /// Return the top of the stack to the caller. BC_INST_RET, /// Return 0 to the caller. BC_INST_RET0, /// Special return instruction for void functions. BC_INST_RET_VOID, /// Special halt instruction. BC_INST_HALT, #endif // BC_ENABLED /// Pop an item off of the results stack. BC_INST_POP, /// Swaps the top two items on the results stack. BC_INST_SWAP, /// Modular exponentiation. BC_INST_MODEXP, /// Do divide and modulus at the same time. BC_INST_DIVMOD, /// Turns a number into a string and prints it. BC_INST_PRINT_STREAM, #if DC_ENABLED /// dc's return; it pops an executing string off of the stack. BC_INST_POP_EXEC, /// Unconditionally execute a string. BC_INST_EXECUTE, /// Conditionally execute a string. BC_INST_EXEC_COND, /// Prints each item on the results stack, separated by newlines. BC_INST_PRINT_STACK, /// Pops everything off of the results stack. BC_INST_CLEAR_STACK, /// Pushes the current length of a register stack onto the results stack. BC_INST_REG_STACK_LEN, /// Pushes the current length of the results stack onto the results stack. BC_INST_STACK_LEN, /// Pushes a copy of the item on the top of the results stack onto the /// results stack. BC_INST_DUPLICATE, /// Copies the value in a register and pushes the copy onto the results /// stack. BC_INST_LOAD, /// Pops an item off of a register stack and pushes it onto the results /// stack. BC_INST_PUSH_VAR, /// Pops an item off of the results stack and pushes it onto a register's /// stack. BC_INST_PUSH_TO_VAR, /// Quit. BC_INST_QUIT, /// Quit executing some number of strings. BC_INST_NQUIT, /// Push the depth of the execution stack onto the stack. BC_INST_EXEC_STACK_LEN, #endif // DC_ENABLED /// Invalid instruction. BC_INST_INVALID, } BcInst; #if BC_C11 _Static_assert(BC_INST_INVALID <= UCHAR_MAX, "Too many instructions to fit into an unsigned char"); #endif // BC_C11 /// Used by maps to identify where items are in the array. typedef struct BcId { /// The name of the item. char* name; /// The index into the array where the item is. size_t idx; } BcId; /// The location of a var, array, or array element. typedef struct BcLoc { /// The index of the var or array. size_t loc; + /// The index of the array or variable in the array stack. This is to + /// prevent a bug with getting the wrong array element or variable after a + /// function call. See the tests/bc/scripts/array.bc test for the array + /// case; the variable case is in various variable tests. + size_t stack_idx; + /// The index of the array element. Only used for array elements. size_t idx; } BcLoc; /// An entry for a constant. typedef struct BcConst { /// The original string as parsed from the source code. char* val; /// The last base that the constant was parsed in. BcBigDig base; /// The parsed constant. BcNum num; } BcConst; /// A function. This is also used in dc, not just bc. The reason is that strings /// are executed in dc, and they are converted to functions in order to be /// executed. typedef struct BcFunc { /// The bytecode instructions. BcVec code; #if BC_ENABLED /// The labels. This is a vector of indices. The index is the index into /// the bytecode vector where the label is. BcVec labels; /// The autos for the function. The first items are the parameters, and the /// arguments to the parameters must match the types in this vector. BcVec autos; /// The number of parameters the function takes. size_t nparams; #endif // BC_ENABLED /// The function's name. const char* name; #if BC_ENABLED /// True if the function is a void function. bool voidfn; #endif // BC_ENABLED } BcFunc; /// Types of results that can be pushed onto the results stack. typedef enum BcResultType { /// Result is a variable. BC_RESULT_VAR, /// Result is an array element. BC_RESULT_ARRAY_ELEM, /// Result is an array. This is only allowed for function arguments or /// returning the length of the array. BC_RESULT_ARRAY, /// Result is a string. BC_RESULT_STR, /// Result is a temporary. This is used for the result of almost all /// expressions. BC_RESULT_TEMP, /// Special casing the two below gave performance improvements. /// Result is a 0. BC_RESULT_ZERO, /// Result is a 1. Useful for inc/dec operators. BC_RESULT_ONE, #if BC_ENABLED /// Result is the special "last" variable. BC_RESULT_LAST, /// Result is the return value of a void function. BC_RESULT_VOID, #endif // BC_ENABLED /// Result is the value of ibase. BC_RESULT_IBASE, /// Result is the value of obase. BC_RESULT_OBASE, /// Result is the value of scale. BC_RESULT_SCALE, #if BC_ENABLE_EXTRA_MATH /// Result is the value of seed. BC_RESULT_SEED, #endif // BC_ENABLE_EXTRA_MATH } BcResultType; /// A union to store data for various result types. typedef union BcResultData { /// A number. Strings are stored here too; they are numbers with /// cap == 0 && num == NULL. The string's index into the strings vector is /// stored in the scale field. But this is only used for strings stored in /// variables. BcNum n; /// A vector. BcVec v; /// A variable, array, or array element reference. This could also be a /// string if a string is not stored in a variable (dc only). BcLoc loc; } BcResultData; /// A tagged union for results. typedef struct BcResult { /// The tag. The type of the result. BcResultType t; /// The data. The data for the result. BcResultData d; } BcResult; /// An instruction pointer. This is how bc knows where in the bytecode vector, /// and which function, the current execution is. typedef struct BcInstPtr { /// The index of the currently executing function in the fns vector. size_t func; /// The index into the bytecode vector of the *next* instruction. size_t idx; /// The length of the results vector when this function started executing. /// This is mostly used for bc where functions should not affect the results /// of their callers. size_t len; } BcInstPtr; /// Types of identifiers. typedef enum BcType { /// Variable. BC_TYPE_VAR, /// Array. BC_TYPE_ARRAY, #if BC_ENABLED /// Array reference. BC_TYPE_REF, #endif // BC_ENABLED } BcType; #if BC_ENABLED /// An auto variable in bc. typedef struct BcAuto { /// The index of the variable in the vars or arrs vectors. size_t idx; /// The type of the variable. BcType type; } BcAuto; #endif // BC_ENABLED /// Forward declaration. struct BcProgram; /** * Initializes a function. * @param f The function to initialize. * @param name The name of the function. The string is assumed to be owned by * some other entity. */ void bc_func_init(BcFunc* f, const char* name); /** * Inserts an auto into the function. * @param f The function to insert into. * @param p The program. This is to search for the variable or array name. * @param name The name of the auto to insert. * @param type The type of the auto. * @param line The line in the source code where the insert happened. This is * solely for error reporting. */ void bc_func_insert(BcFunc* f, struct BcProgram* p, char* name, BcType type, size_t line); /** * Resets a function in preparation for it to be reused. This can happen in bc * because it is a dynamic language and functions can be redefined. * @param f The functio to reset. */ void bc_func_reset(BcFunc* f); #ifndef NDEBUG /** * Frees a function. This is a destructor. This is only used in debug builds * because all functions are freed at exit. We free them in debug builds to * check for memory leaks. * @param func The function to free as a void pointer. */ void bc_func_free(void* func); #endif // NDEBUG /** * Initializes an array, which is the array type in bc and dc source code. Since * variables and arrays are both arrays (see the development manual, * manuals/development.md#execution, for more information), the @a nums * parameter tells bc whether to initialize an array of numbers or an array of * arrays of numbers. If the latter, it does a recursive call with nums set to * true. * @param a The array to initialize. * @param nums True if the array should be for numbers, false if it should be * for vectors. */ void bc_array_init(BcVec* a, bool nums); /** * Copies an array to another array. This is used to do pass arrays to functions * that do not take references to arrays. The arrays are passed entirely by * value, which means that they need to be copied. * @param d The destination array. * @param s The source array. */ void bc_array_copy(BcVec* d, const BcVec* s); /** * Frees a string stored in a function. This is a destructor. * @param string The string to free as a void pointer. */ void bc_string_free(void* string); /** * Frees a constant stored in a function. This is a destructor. * @param constant The constant to free as a void pointer. */ void bc_const_free(void* constant); /** * Clears a result. It sets the type to BC_RESULT_TEMP and clears the union by * clearing the BcNum in the union. This is to ensure that bc does not use * uninitialized data. * @param r The result to clear. */ void bc_result_clear(BcResult* r); /** * Copies a result into another. This is done for things like duplicating the * top of the results stack or copying the result of an assignment to put back * on the results stack. * @param d The destination result. * @param src The source result. */ void bc_result_copy(BcResult* d, BcResult* src); /** * Frees a result. This is a destructor. * @param result The result to free as a void pointer. */ void bc_result_free(void* result); /** * Expands an array to @a len. This can happen because in bc, you do not have to * explicitly initialize elements of an array. If you access an element that is * not initialized, the array is expanded to fit it, and all missing elements * are initialized to 0 if they are numbers, or arrays with one element of 0. * This function does that expansion. * @param a The array to expand. * @param len The length to expand to. */ void bc_array_expand(BcVec* a, size_t len); #if BC_ENABLED /** * Returns non-zero if the bytecode instruction i is an assignment instruction. * @param i The instruction to test. * @return Non-zero if i is an assignment instruction, zero otherwise. */ #define BC_INST_IS_ASSIGN(i) \ ((i) == BC_INST_ASSIGN || (i) == BC_INST_ASSIGN_NO_VAL) /** * Returns true if the bytecode instruction @a i requires the value to be * returned for use. * @param i The instruction to test. * @return True if @a i requires the value to be returned for use, false * otherwise. */ #define BC_INST_USE_VAL(i) ((i) <= BC_INST_ASSIGN) #else // BC_ENABLED /** * Returns non-zero if the bytecode instruction i is an assignment instruction. * @param i The instruction to test. * @return Non-zero if i is an assignment instruction, zero otherwise. */ #define BC_INST_IS_ASSIGN(i) ((i) == BC_INST_ASSIGN_NO_VAL) /** * Returns true if the bytecode instruction @a i requires the value to be * returned for use. * @param i The instruction to test. * @return True if @a i requires the value to be returned for use, false * otherwise. */ #define BC_INST_USE_VAL(i) (false) #endif // BC_ENABLED #if BC_DEBUG_CODE /// Reference to string names for all of the instructions. For debugging. extern const char* bc_inst_names[]; #endif // BC_DEBUG_CODE /// References to the names of the main and read functions. extern const char bc_func_main[]; extern const char bc_func_read[]; #endif // BC_LANG_H diff --git a/include/program.h b/include/program.h index 3acd5157b127..3a3ea6c9dab7 100644 --- a/include/program.h +++ b/include/program.h @@ -1,1071 +1,1062 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * Definitions for bc programs. * */ #ifndef BC_PROGRAM_H #define BC_PROGRAM_H #include #include #include #include #include #include #include /// The index of ibase in the globals array. #define BC_PROG_GLOBALS_IBASE (0) /// The index of obase in the globals array. #define BC_PROG_GLOBALS_OBASE (1) /// The index of scale in the globals array. #define BC_PROG_GLOBALS_SCALE (2) #if BC_ENABLE_EXTRA_MATH /// The index of the rand max in the maxes array. #define BC_PROG_MAX_RAND (3) #endif // BC_ENABLE_EXTRA_MATH /// The length of the globals array. #define BC_PROG_GLOBALS_LEN (3 + BC_ENABLE_EXTRA_MATH) typedef struct BcProgram { /// The array of globals values. BcBigDig globals[BC_PROG_GLOBALS_LEN]; #if BC_ENABLED /// The array of globals stacks. BcVec globals_v[BC_PROG_GLOBALS_LEN]; #endif // BC_ENABLED #if BC_ENABLE_EXTRA_MATH /// The pseudo-random number generator. BcRNG rng; #endif // BC_ENABLE_EXTRA_MATH /// The results stack. BcVec results; /// The execution stack. BcVec stack; /// The constants encountered in the program. They are global to the program /// to prevent bad accesses when functions that used non-auto variables are /// replaced. BcVec consts; /// The map of constants to go with consts. BcVec const_map; /// The strings encountered in the program. They are global to the program /// to prevent bad accesses when functions that used non-auto variables are /// replaced. BcVec strs; /// The map of strings to go with strs. BcVec str_map; /// The array of functions. BcVec fns; /// The map of functions to go with fns. BcVec fn_map; /// The array of variables. BcVec vars; /// The map of variables to go with vars. BcVec var_map; /// The array of arrays. BcVec arrs; /// The map of arrays to go with arrs. BcVec arr_map; #if DC_ENABLED /// A vector of tail calls. These are just integers, which are the number of /// tail calls that have been executed for each function (string) on the /// stack for dc. This is to prevent dc from constantly growing memory use /// because of pushing more and more string executions on the stack. BcVec tail_calls; #endif // DC_ENABLED /// A BcNum that has the proper base for asciify. BcNum strmb; // A BcNum to run asciify. This is to prevent GCC longjmp() clobbering // warnings. BcNum asciify; #if BC_ENABLED /// The last printed value for bc. BcNum last; #endif // BC_ENABLED // The BcDig array for strmb. This uses BC_NUM_LONG_LOG10 because it is used // in bc_num_ulong2num(), which attempts to realloc, unless it is big // enough. This is big enough. BcDig strmb_num[BC_NUM_BIGDIG_LOG10]; } BcProgram; /** * Returns true if the stack @a s has at least @a n items, false otherwise. * @param s The stack to check. * @param n The number of items the stack must have. * @return True if @a s has at least @a n items, false otherwise. */ #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) (n))) /** * Get a pointer to the top value in a global value stack. * @param v The global value stack. * @return A pointer to the top value in @a v. */ #define BC_PROG_GLOBAL_PTR(v) (bc_vec_top(v)) /** * Get the top value in a global value stack. * @param v The global value stack. * @return The top value in @a v. */ #define BC_PROG_GLOBAL(v) (*((BcBigDig*) BC_PROG_GLOBAL_PTR(v))) /** * Returns the current value of ibase. * @param p The program. * @return The current ibase. */ #define BC_PROG_IBASE(p) ((p)->globals[BC_PROG_GLOBALS_IBASE]) /** * Returns the current value of obase. * @param p The program. * @return The current obase. */ #define BC_PROG_OBASE(p) ((p)->globals[BC_PROG_GLOBALS_OBASE]) /** * Returns the current value of scale. * @param p The program. * @return The current scale. */ #define BC_PROG_SCALE(p) ((p)->globals[BC_PROG_GLOBALS_SCALE]) /// The index for the main function in the functions array.// #define BC_PROG_MAIN (0) /// The index for the read function in the functions array. #define BC_PROG_READ (1) /** * Retires (completes the execution of) an instruction. Some instructions * require special retirement, but most can use this. This basically pops the * operands while preserving the result (which we assumed was pushed before the * actual operation). * @param p The program. * @param nres The number of results returned by the instruction. * @param nops The number of operands used by the instruction. */ #define bc_program_retire(p, nres, nops) \ (bc_vec_npopAt(&(p)->results, (nops), (p)->results.len - (nres + nops))) #if DC_ENABLED /// A constant that tells how many functions are required in dc. #define BC_PROG_REQ_FUNCS (2) #if !BC_ENABLED -/// This define disappears the parameter last because for dc only, last is -/// always true. -#define bc_program_copyToVar(p, name, t, last) \ - bc_program_copyToVar_impl(p, name, t) - /// Returns true if the calculator should pop after printing. #define BC_PROGRAM_POP(pop) (pop) #else // !BC_ENABLED -// This is here to quiet a compiler warning. -#define bc_program_copyToVar(p, name, t, last) \ - bc_program_copyToVar_impl(p, name, t, last) - /// Returns true if the calculator should pop after printing. #define BC_PROGRAM_POP(pop) (BC_IS_BC || (pop)) #endif // !BC_ENABLED // This is here to satisfy a clang warning about recursive macros. #define bc_program_pushVar(p, code, bgn, pop, copy) \ bc_program_pushVar_impl(p, code, bgn, pop, copy) #else // DC_ENABLED // This define disappears pop and copy because for bc, 'pop' and 'copy' are // always false. #define bc_program_pushVar(p, code, bgn, pop, copy) \ bc_program_pushVar_impl(p, code, bgn) /// Returns true if the calculator should pop after printing. #define BC_PROGRAM_POP(pop) (BC_IS_BC) // In debug mode, we want bc to check the stack, but otherwise, we don't because // the bc language implicitly mandates that the stack should always have enough // items. #ifdef NDEBUG #define BC_PROG_NO_STACK_CHECK #endif // NDEBUG #endif // DC_ENABLED /** * Returns true if the BcNum @a n is acting as a string. * @param n The BcNum to test. * @return True if @a n is acting as a string, false otherwise. */ #define BC_PROG_STR(n) ((n)->num == NULL && !(n)->cap) #if BC_ENABLED /** * Returns true if the result @a r and @a n is a number. * @param r The result. * @param n The number corresponding to the result. * @return True if the result holds a number, false otherwise. */ #define BC_PROG_NUM(r, n) \ ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) #else // BC_ENABLED /** * Returns true if the result @a r and @a n is a number. * @param r The result. * @param n The number corresponding to the result. * @return True if the result holds a number, false otherwise. */ #define BC_PROG_NUM(r, n) ((r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) #endif // BC_ENABLED /** * This is a function type for unary operations. Currently, these include * boolean not, negation, and truncation with extra math. * @param r The BcResult to store the result into. * @param n The parameter to the unary operation. */ typedef void (*BcProgramUnary)(BcResult* r, BcNum* n); /** * Initializes the BcProgram. * @param p The program to initialize. */ void bc_program_init(BcProgram* p); #ifndef NDEBUG /** * Frees a BcProgram. This is only used in debug builds because a BcProgram is * only freed on program exit, and we don't care about freeing resources on * exit. * @param p The program to initialize. */ void bc_program_free(BcProgram* p); #endif // NDEBUG /** * Prints a stack trace of the bc functions or dc strings currently executing. * @param p The program. */ void bc_program_printStackTrace(BcProgram* p); #if BC_DEBUG_CODE #if BC_ENABLED && DC_ENABLED /** * Prints the bytecode in a function. This is a debug-only function. * @param p The program. */ void bc_program_code(const BcProgram* p); /** * Prints an instruction. This is a debug-only function. * @param p The program. * @param code The bytecode array. * @param bgn A pointer to the current index. It is also updated to the next * index. */ void bc_program_printInst(const BcProgram* p, const char* code, size_t* restrict bgn); /** * Prints the stack. This is a debug-only function. * @param p The program. */ void bc_program_printStackDebug(BcProgram* p); #endif // BC_ENABLED && DC_ENABLED #endif // BC_DEBUG_CODE /** * Returns the index of the variable or array in their respective arrays. * @param p The program. * @param name The name of the variable or array. * @param var True if the search should be for a variable, false for an array. * @return The index of the variable or array in the correct array. */ size_t bc_program_search(BcProgram* p, const char* name, bool var); /** * Adds a string to the program and returns the string's index in the program. * @param p The program. * @param str The string to add. * @return The string's index in the program. */ size_t bc_program_addString(BcProgram* p, const char* str); /** * Inserts a function into the program and returns the index of the function in * the fns array. * @param p The program. * @param name The name of the function. * @return The index of the function after insertion. */ size_t bc_program_insertFunc(BcProgram* p, const char* name); /** * Resets a program, usually because of resetting after an error. * @param p The program to reset. */ void bc_program_reset(BcProgram* p); /** * Executes bc or dc code in the BcProgram. * @param p The program. */ void bc_program_exec(BcProgram* p); /** * Negates a copy of a BcNum. This is a BcProgramUnary function. * @param r The BcResult to store the result into. * @param n The parameter to the unary operation. */ void bc_program_negate(BcResult* r, BcNum* n); /** * Returns a boolean not of a BcNum. This is a BcProgramUnary function. * @param r The BcResult to store the result into. * @param n The parameter to the unary operation. */ void bc_program_not(BcResult* r, BcNum* n); #if BC_ENABLE_EXTRA_MATH /** * Truncates a copy of a BcNum. This is a BcProgramUnary function. * @param r The BcResult to store the result into. * @param n The parameter to the unary operation. */ void bc_program_trunc(BcResult* r, BcNum* n); /** * Assigns a value to the seed builtin variable. * @param p The program. * @param val The value to assign to the seed. */ void bc_program_assignSeed(BcProgram* p, BcNum* val); #endif // BC_ENABLE_EXTRA_MATH /** * Assigns a value to a builtin value that is not seed. * @param p The program. * @param scale True if the builtin is scale. * @param obase True if the builtin is obase. This cannot be true at the same * time @a scale is. * @param val The value to assign to the builtin. */ void bc_program_assignBuiltin(BcProgram* p, bool scale, bool obase, BcBigDig val); /// A reference to an array of binary operator functions. extern const BcNumBinaryOp bc_program_ops[]; /// A reference to an array of binary operator allocation request functions. extern const BcNumBinaryOpReq bc_program_opReqs[]; /// A reference to an array of unary operator functions. extern const BcProgramUnary bc_program_unarys[]; /// A reference to a filename for command-line expressions. extern const char bc_program_exprs_name[]; /// A reference to a filename for stdin. extern const char bc_program_stdin_name[]; /// A reference to the ready message printed on SIGINT. extern const char bc_program_ready_msg[]; /// A reference to the length of the ready message. extern const size_t bc_program_ready_msg_len; /// A reference to an array of escape characters for the print statement. extern const char bc_program_esc_chars[]; /// A reference to an array of the characters corresponding to the escape /// characters in bc_program_esc_chars. extern const char bc_program_esc_seqs[]; #if BC_HAS_COMPUTED_GOTO #if BC_DEBUG_CODE // clang-format off #define BC_PROG_JUMP(inst, code, ip) \ do \ { \ inst = (uchar) (code)[(ip)->idx++]; \ bc_file_printf(&vm->ferr, "inst: %s\n", bc_inst_names[inst]); \ bc_file_flush(&vm->ferr, bc_flush_none); \ goto *bc_program_inst_lbls[inst]; \ } \ while (0) // clang-format on #else // BC_DEBUG_CODE // clang-format off #define BC_PROG_JUMP(inst, code, ip) \ do \ { \ inst = (uchar) (code)[(ip)->idx++]; \ goto *bc_program_inst_lbls[inst]; \ } \ while (0) // clang-format on #endif // BC_DEBUG_CODE #define BC_PROG_DIRECT_JUMP(l) goto lbl_##l; #define BC_PROG_LBL(l) lbl_##l #define BC_PROG_FALLTHROUGH #if BC_C11 #define BC_PROG_LBLS_SIZE (sizeof(bc_program_inst_lbls) / sizeof(void*)) #define BC_PROG_LBLS_ASSERT \ _Static_assert(BC_PROG_LBLS_SIZE == BC_INST_INVALID + 1, \ "bc_program_inst_lbls[] mismatches the instructions") #else // BC_C11 #define BC_PROG_LBLS_ASSERT #endif // BC_C11 #if BC_ENABLED #if DC_ENABLED #if BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_INC, \ &&lbl_BC_INST_DEC, \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_TRUNC, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_PLACES, \ &&lbl_BC_INST_LSHIFT, \ &&lbl_BC_INST_RSHIFT, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_POWER, \ &&lbl_BC_INST_ASSIGN_MULTIPLY, \ &&lbl_BC_INST_ASSIGN_DIVIDE, \ &&lbl_BC_INST_ASSIGN_MODULUS, \ &&lbl_BC_INST_ASSIGN_PLUS, \ &&lbl_BC_INST_ASSIGN_MINUS, \ &&lbl_BC_INST_ASSIGN_PLACES, \ &&lbl_BC_INST_ASSIGN_LSHIFT, \ &&lbl_BC_INST_ASSIGN_RSHIFT, \ &&lbl_BC_INST_ASSIGN, \ &&lbl_BC_INST_ASSIGN_POWER_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MULTIPLY_NO_VAL, \ &&lbl_BC_INST_ASSIGN_DIVIDE_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MODULUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MINUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLACES_NO_VAL, \ &&lbl_BC_INST_ASSIGN_LSHIFT_NO_VAL, \ &&lbl_BC_INST_ASSIGN_RSHIFT_NO_VAL, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_LAST, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_SEED, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_RAND, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_MAXRAND, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_GLOBAL_STACKS, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_PRINT_STR, \ &&lbl_BC_INST_JUMP, \ &&lbl_BC_INST_JUMP_ZERO, \ &&lbl_BC_INST_CALL, \ &&lbl_BC_INST_RET, \ &&lbl_BC_INST_RET0, \ &&lbl_BC_INST_RET_VOID, \ &&lbl_BC_INST_HALT, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ &&lbl_BC_INST_PRINT_STACK, \ &&lbl_BC_INST_CLEAR_STACK, \ &&lbl_BC_INST_REG_STACK_LEN, \ &&lbl_BC_INST_STACK_LEN, \ &&lbl_BC_INST_DUPLICATE, \ &&lbl_BC_INST_LOAD, \ &&lbl_BC_INST_PUSH_VAR, \ &&lbl_BC_INST_PUSH_TO_VAR, \ &&lbl_BC_INST_QUIT, \ &&lbl_BC_INST_NQUIT, \ &&lbl_BC_INST_EXEC_STACK_LEN, \ &&lbl_BC_INST_INVALID, \ } #else // BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_INC, \ &&lbl_BC_INST_DEC, \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_POWER, \ &&lbl_BC_INST_ASSIGN_MULTIPLY, \ &&lbl_BC_INST_ASSIGN_DIVIDE, \ &&lbl_BC_INST_ASSIGN_MODULUS, \ &&lbl_BC_INST_ASSIGN_PLUS, \ &&lbl_BC_INST_ASSIGN_MINUS, \ &&lbl_BC_INST_ASSIGN, \ &&lbl_BC_INST_ASSIGN_POWER_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MULTIPLY_NO_VAL, \ &&lbl_BC_INST_ASSIGN_DIVIDE_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MODULUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MINUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_LAST, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_GLOBAL_STACKS, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_PRINT_STR, \ &&lbl_BC_INST_JUMP, \ &&lbl_BC_INST_JUMP_ZERO, \ &&lbl_BC_INST_CALL, \ &&lbl_BC_INST_RET, \ &&lbl_BC_INST_RET0, \ &&lbl_BC_INST_RET_VOID, \ &&lbl_BC_INST_HALT, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ &&lbl_BC_INST_PRINT_STACK, \ &&lbl_BC_INST_CLEAR_STACK, \ &&lbl_BC_INST_REG_STACK_LEN, \ &&lbl_BC_INST_STACK_LEN, \ &&lbl_BC_INST_DUPLICATE, \ &&lbl_BC_INST_LOAD, \ &&lbl_BC_INST_PUSH_VAR, \ &&lbl_BC_INST_PUSH_TO_VAR, \ &&lbl_BC_INST_QUIT, \ &&lbl_BC_INST_NQUIT, \ &&lbl_BC_INST_EXEC_STACK_LEN, \ &&lbl_BC_INST_INVALID, \ } #endif // BC_ENABLE_EXTRA_MATH #else // DC_ENABLED #if BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_INC, \ &&lbl_BC_INST_DEC, \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_TRUNC, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_PLACES, \ &&lbl_BC_INST_LSHIFT, \ &&lbl_BC_INST_RSHIFT, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_POWER, \ &&lbl_BC_INST_ASSIGN_MULTIPLY, \ &&lbl_BC_INST_ASSIGN_DIVIDE, \ &&lbl_BC_INST_ASSIGN_MODULUS, \ &&lbl_BC_INST_ASSIGN_PLUS, \ &&lbl_BC_INST_ASSIGN_MINUS, \ &&lbl_BC_INST_ASSIGN_PLACES, \ &&lbl_BC_INST_ASSIGN_LSHIFT, \ &&lbl_BC_INST_ASSIGN_RSHIFT, \ &&lbl_BC_INST_ASSIGN, \ &&lbl_BC_INST_ASSIGN_POWER_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MULTIPLY_NO_VAL, \ &&lbl_BC_INST_ASSIGN_DIVIDE_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MODULUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MINUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLACES_NO_VAL, \ &&lbl_BC_INST_ASSIGN_LSHIFT_NO_VAL, \ &&lbl_BC_INST_ASSIGN_RSHIFT_NO_VAL, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_LAST, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_SEED, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_RAND, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_MAXRAND, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_GLOBAL_STACKS, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_PRINT_STR, \ &&lbl_BC_INST_JUMP, \ &&lbl_BC_INST_JUMP_ZERO, \ &&lbl_BC_INST_CALL, \ &&lbl_BC_INST_RET, \ &&lbl_BC_INST_RET0, \ &&lbl_BC_INST_RET_VOID, \ &&lbl_BC_INST_HALT, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_INVALID, \ } #else // BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_INC, \ &&lbl_BC_INST_DEC, \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_POWER, \ &&lbl_BC_INST_ASSIGN_MULTIPLY, \ &&lbl_BC_INST_ASSIGN_DIVIDE, \ &&lbl_BC_INST_ASSIGN_MODULUS, \ &&lbl_BC_INST_ASSIGN_PLUS, \ &&lbl_BC_INST_ASSIGN_MINUS, \ &&lbl_BC_INST_ASSIGN, \ &&lbl_BC_INST_ASSIGN_POWER_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MULTIPLY_NO_VAL, \ &&lbl_BC_INST_ASSIGN_DIVIDE_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MODULUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_PLUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_MINUS_NO_VAL, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_LAST, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_GLOBAL_STACKS, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_PRINT_STR, \ &&lbl_BC_INST_JUMP, \ &&lbl_BC_INST_JUMP_ZERO, \ &&lbl_BC_INST_CALL, \ &&lbl_BC_INST_RET, \ &&lbl_BC_INST_RET0, \ &&lbl_BC_INST_RET_VOID, \ &&lbl_BC_INST_HALT, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_INVALID, \ } #endif // BC_ENABLE_EXTRA_MATH #endif // DC_ENABLED #else // BC_ENABLED #if BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_TRUNC, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_PLACES, \ &&lbl_BC_INST_LSHIFT, \ &&lbl_BC_INST_RSHIFT, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_SEED, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_IRAND, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_RAND, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_MAXRAND, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ &&lbl_BC_INST_PRINT_STACK, \ &&lbl_BC_INST_CLEAR_STACK, \ &&lbl_BC_INST_REG_STACK_LEN, \ &&lbl_BC_INST_STACK_LEN, \ &&lbl_BC_INST_DUPLICATE, \ &&lbl_BC_INST_LOAD, \ &&lbl_BC_INST_PUSH_VAR, \ &&lbl_BC_INST_PUSH_TO_VAR, \ &&lbl_BC_INST_QUIT, \ &&lbl_BC_INST_NQUIT, \ &&lbl_BC_INST_EXEC_STACK_LEN, \ &&lbl_BC_INST_INVALID, \ } #else // BC_ENABLE_EXTRA_MATH #define BC_PROG_LBLS \ static const void* const bc_program_inst_lbls[] = { \ &&lbl_BC_INST_NEG, \ &&lbl_BC_INST_BOOL_NOT, \ &&lbl_BC_INST_POWER, \ &&lbl_BC_INST_MULTIPLY, \ &&lbl_BC_INST_DIVIDE, \ &&lbl_BC_INST_MODULUS, \ &&lbl_BC_INST_PLUS, \ &&lbl_BC_INST_MINUS, \ &&lbl_BC_INST_REL_EQ, \ &&lbl_BC_INST_REL_LE, \ &&lbl_BC_INST_REL_GE, \ &&lbl_BC_INST_REL_NE, \ &&lbl_BC_INST_REL_LT, \ &&lbl_BC_INST_REL_GT, \ &&lbl_BC_INST_BOOL_OR, \ &&lbl_BC_INST_BOOL_AND, \ &&lbl_BC_INST_ASSIGN_NO_VAL, \ &&lbl_BC_INST_NUM, \ &&lbl_BC_INST_VAR, \ &&lbl_BC_INST_ARRAY_ELEM, \ &&lbl_BC_INST_ARRAY, \ &&lbl_BC_INST_ZERO, \ &&lbl_BC_INST_ONE, \ &&lbl_BC_INST_IBASE, \ &&lbl_BC_INST_OBASE, \ &&lbl_BC_INST_SCALE, \ &&lbl_BC_INST_LENGTH, \ &&lbl_BC_INST_SCALE_FUNC, \ &&lbl_BC_INST_SQRT, \ &&lbl_BC_INST_ABS, \ &&lbl_BC_INST_IS_NUMBER, \ &&lbl_BC_INST_IS_STRING, \ &&lbl_BC_INST_ASCIIFY, \ &&lbl_BC_INST_READ, \ &&lbl_BC_INST_MAXIBASE, \ &&lbl_BC_INST_MAXOBASE, \ &&lbl_BC_INST_MAXSCALE, \ &&lbl_BC_INST_LINE_LENGTH, \ &&lbl_BC_INST_LEADING_ZERO, \ &&lbl_BC_INST_PRINT, \ &&lbl_BC_INST_PRINT_POP, \ &&lbl_BC_INST_STR, \ &&lbl_BC_INST_POP, \ &&lbl_BC_INST_SWAP, \ &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ &&lbl_BC_INST_PRINT_STACK, \ &&lbl_BC_INST_CLEAR_STACK, \ &&lbl_BC_INST_REG_STACK_LEN, \ &&lbl_BC_INST_STACK_LEN, \ &&lbl_BC_INST_DUPLICATE, \ &&lbl_BC_INST_LOAD, \ &&lbl_BC_INST_PUSH_VAR, \ &&lbl_BC_INST_PUSH_TO_VAR, \ &&lbl_BC_INST_QUIT, \ &&lbl_BC_INST_NQUIT, \ &&lbl_BC_INST_EXEC_STACK_LEN, \ &&lbl_BC_INST_INVALID, \ } #endif // BC_ENABLE_EXTRA_MATH #endif // BC_ENABLED #else // BC_HAS_COMPUTED_GOTO #define BC_PROG_JUMP(inst, code, ip) break #define BC_PROG_DIRECT_JUMP(l) #define BC_PROG_LBL(l) case l #define BC_PROG_FALLTHROUGH BC_FALLTHROUGH #define BC_PROG_LBLS #endif // BC_HAS_COMPUTED_GOTO #endif // BC_PROGRAM_H diff --git a/include/version.h b/include/version.h index a9abf5be6c67..e1b72de90eef 100644 --- a/include/version.h +++ b/include/version.h @@ -1,42 +1,42 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The version of bc. * */ #ifndef BC_VERSION_H #define BC_VERSION_H /// The current version. -#define VERSION 6.2.1 +#define VERSION 6.2.2 #endif // BC_VERSION_H diff --git a/src/program.c b/src/program.c index 5424b715a29b..7e2ba8178b61 100644 --- a/src/program.c +++ b/src/program.c @@ -1,3840 +1,3817 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * Code to execute bc programs. * */ #include #include #include #include #include #include #include #include #include #include /** * Does a type check for something that expects a number. * @param r The result that will be checked. * @param n The result's number. */ static inline void bc_program_type_num(BcResult* r, BcNum* n) { #if BC_ENABLED // This should have already been taken care of. assert(r->t != BC_RESULT_VOID); #endif // BC_ENABLED if (BC_ERR(!BC_PROG_NUM(r, n))) bc_err(BC_ERR_EXEC_TYPE); } #if BC_ENABLED /** * Does a type check. * @param r The result to check. * @param t The type that the result should be. */ static void bc_program_type_match(BcResult* r, BcType t) { if (BC_ERR((r->t != BC_RESULT_ARRAY) != (!t))) bc_err(BC_ERR_EXEC_TYPE); } #endif // BC_ENABLED /** * Pulls an index out of a bytecode vector and updates the index into the vector * to point to the spot after the index. For more details on bytecode indices, * see the development manual (manuals/development.md#bytecode-indices). * @param code The bytecode vector. * @param bgn An in/out parameter; the index into the vector that will be * updated. * @return The index at @a bgn in the bytecode vector. */ static size_t bc_program_index(const char* restrict code, size_t* restrict bgn) { uchar amt = (uchar) code[(*bgn)++], i = 0; size_t res = 0; for (; i < amt; ++i, ++(*bgn)) { size_t temp = ((size_t) ((int) (uchar) code[*bgn]) & UCHAR_MAX); res |= (temp << (i * CHAR_BIT)); } return res; } /** * Returns a string from a result and its number. * @param p The program. * @param n The number tied to the result. * @return The string corresponding to the result and number. */ static inline char* bc_program_string(BcProgram* p, const BcNum* n) { return *((char**) bc_vec_item(&p->strs, n->scale)); } #if BC_ENABLED /** * Prepares the globals for a function call. This is only called when global * stacks are on because it pushes a copy of the current globals onto each of * their respective stacks. * @param p The program. */ static void bc_program_prepGlobals(BcProgram* p) { size_t i; for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { bc_vec_push(p->globals_v + i, p->globals + i); } #if BC_ENABLE_EXTRA_MATH bc_rand_push(&p->rng); #endif // BC_ENABLE_EXTRA_MATH } /** * Pops globals stacks on returning from a function, or in the case of reset, * pops all but one item on each global stack. * @param p The program. * @param reset True if all but one item on each stack should be popped, false * otherwise. */ static void bc_program_popGlobals(BcProgram* p, bool reset) { size_t i; BC_SIG_ASSERT_LOCKED; for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { BcVec* v = p->globals_v + i; bc_vec_npop(v, reset ? v->len - 1 : 1); p->globals[i] = BC_PROG_GLOBAL(v); } #if BC_ENABLE_EXTRA_MATH bc_rand_pop(&p->rng, reset); #endif // BC_ENABLE_EXTRA_MATH } /** * Derefeneces an array reference and returns a pointer to the real array. * @param p The program. * @param vec The reference vector. * @return A pointer to the desired array. */ static BcVec* bc_program_dereference(const BcProgram* p, BcVec* vec) { BcVec* v; size_t vidx, nidx, i = 0; // We want to be sure we have a reference vector. assert(vec->size == sizeof(uchar)); // Get the index of the vector in arrs, then the index of the original // referenced vector. vidx = bc_program_index(vec->v, &i); nidx = bc_program_index(vec->v, &i); v = bc_vec_item(bc_vec_item(&p->arrs, vidx), nidx); // We want to be sure we do *not* have a reference vector. assert(v->size != sizeof(uchar)); return v; } #endif // BC_ENABLED /** * Creates a BcNum from a BcBigDig and pushes onto the results stack. This is a * convenience function. * @param p The program. * @param dig The BcBigDig to push onto the results stack. * @param type The type that the pushed result should be. */ static void bc_program_pushBigdig(BcProgram* p, BcBigDig dig, BcResultType type) { BcResult res; res.t = type; BC_SIG_LOCK; bc_num_createFromBigdig(&res.d.n, dig); bc_vec_push(&p->results, &res); BC_SIG_UNLOCK; } size_t bc_program_addString(BcProgram* p, const char* str) { size_t idx; BC_SIG_ASSERT_LOCKED; if (bc_map_insert(&p->str_map, str, p->strs.len, &idx)) { char** str_ptr; BcId* id = bc_vec_item(&p->str_map, idx); // Get the index. idx = id->idx; // Push an empty string on the proper vector. str_ptr = bc_vec_pushEmpty(&p->strs); // We reuse the string in the ID (allocated by bc_map_insert()), because // why not? *str_ptr = id->name; } else { BcId* id = bc_vec_item(&p->str_map, idx); idx = id->idx; } return idx; } size_t bc_program_search(BcProgram* p, const char* name, bool var) { BcVec* v; BcVec* map; size_t i; BC_SIG_ASSERT_LOCKED; // Grab the right vector and map. v = var ? &p->vars : &p->arrs; map = var ? &p->var_map : &p->arr_map; // We do an insert because the variable might not exist yet. This is because // the parser calls this function. If the insert succeeds, we create a stack // for the variable/array. But regardless, bc_map_insert() gives us the // index of the item in i. if (bc_map_insert(map, name, v->len, &i)) { BcVec* temp = bc_vec_pushEmpty(v); bc_array_init(temp, var); } return ((BcId*) bc_vec_item(map, i))->idx; } /** * Returns the correct variable or array stack for the type. * @param p The program. * @param idx The index of the variable or array in the variable or array * vector. * @param type The type of vector to return. * @return A pointer to the variable or array stack. */ static inline BcVec* bc_program_vec(const BcProgram* p, size_t idx, BcType type) { const BcVec* v = (type == BC_TYPE_VAR) ? &p->vars : &p->arrs; return bc_vec_item(v, idx); } /** * Returns a pointer to the BcNum corresponding to the result. There is one * case, however, where this returns a pointer to a BcVec: if the type of the * result is array. In that case, the pointer is casted to a pointer to BcNum, * but is never used. The function that calls this expecting an array casts the * pointer back. This function is called a lot and needs to be as fast as * possible. * @param p The program. * @param r The result whose number will be returned. * @return The BcNum corresponding to the result. */ static BcNum* bc_program_num(BcProgram* p, BcResult* r) { BcNum* n; #ifdef _WIN32 // Windows made it an error to not initialize this, so shut it up. // I don't want to do this on other platforms because this procedure // is one of the most heavily-used, and eliminating the initialization // is a performance win. n = NULL; #endif // _WIN32 switch (r->t) { case BC_RESULT_STR: case BC_RESULT_TEMP: case BC_RESULT_IBASE: case BC_RESULT_SCALE: case BC_RESULT_OBASE: #if BC_ENABLE_EXTRA_MATH case BC_RESULT_SEED: #endif // BC_ENABLE_EXTRA_MATH { n = &r->d.n; break; } case BC_RESULT_VAR: case BC_RESULT_ARRAY: case BC_RESULT_ARRAY_ELEM: { BcVec* v; BcType type = (r->t == BC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY; // Get the correct variable or array vector. v = bc_program_vec(p, r->d.loc.loc, type); // Surprisingly enough, the hard case is *not* returning an array; // it's returning an array element. This is because we have to dig // deeper to get *to* the element. That's what the code inside this // if statement does. if (r->t == BC_RESULT_ARRAY_ELEM) { size_t idx = r->d.loc.idx; - v = bc_vec_top(v); + v = bc_vec_item(v, r->d.loc.stack_idx); #if BC_ENABLED // If this is true, we have a reference vector, so dereference // it. The reason we don't need to worry about it for returning // a straight array is because we only care about references // when we access elements of an array that is a reference. That // is this code, so in essence, this line takes care of arrays // as well. if (v->size == sizeof(uchar)) v = bc_program_dereference(p, v); #endif // BC_ENABLED // We want to be sure we got a valid array of numbers. assert(v->size == sizeof(BcNum)); // The bc spec says that if an element is accessed that does not // exist, it should be preinitialized to 0. Well, if we access // an element *way* out there, we have to preinitialize all // elements between the current last element and the actual // accessed element. if (v->len <= idx) { BC_SIG_LOCK; bc_array_expand(v, bc_vm_growSize(idx, 1)); BC_SIG_UNLOCK; } n = bc_vec_item(v, idx); } // This is either a number (for a var) or an array (for an array). - // Because bc_vec_top() returns a void*, we don't need to cast. - else n = bc_vec_top(v); + // Because bc_vec_top() and bc_vec_item() return a void*, we don't + // need to cast. + else + { +#if BC_ENABLED + if (BC_IS_BC) + { + n = bc_vec_item(v, r->d.loc.stack_idx); + } + else +#endif // BC_ENABLED + { + n = bc_vec_top(v); + } + } break; } case BC_RESULT_ZERO: { n = &vm->zero; break; } case BC_RESULT_ONE: { n = &vm->one; break; } #if BC_ENABLED // We should never get here; this is taken care of earlier because a // result is expected. case BC_RESULT_VOID: #ifndef NDEBUG { abort(); // Fallthrough } #endif // NDEBUG case BC_RESULT_LAST: { n = &p->last; break; } #endif // BC_ENABLED #if BC_GCC // This is here in GCC to quiet the "maybe-uninitialized" warning. default: { abort(); } #endif // BC_GCC } return n; } /** * Prepares an operand for use. * @param p The program. * @param r An out parameter; this is set to the pointer to the result that * we care about. * @param n An out parameter; this is set to the pointer to the number that * we care about. * @param idx The index of the result from the top of the results stack. */ static void bc_program_operand(BcProgram* p, BcResult** r, BcNum** n, size_t idx) { *r = bc_vec_item_rev(&p->results, idx); #if BC_ENABLED if (BC_ERR((*r)->t == BC_RESULT_VOID)) bc_err(BC_ERR_EXEC_VOID_VAL); #endif // BC_ENABLED *n = bc_program_num(p, *r); } /** * Prepares the operands of a binary operator. * @param p The program. * @param l An out parameter; this is set to the pointer to the result for * the left operand. * @param ln An out parameter; this is set to the pointer to the number for * the left operand. * @param r An out parameter; this is set to the pointer to the result for * the right operand. * @param rn An out parameter; this is set to the pointer to the number for * the right operand. * @param idx The starting index where the operands are in the results stack, * starting from the top. */ static void bc_program_binPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r, BcNum** rn, size_t idx) { BcResultType lt; assert(p != NULL && l != NULL && ln != NULL && r != NULL && rn != NULL); #ifndef BC_PROG_NO_STACK_CHECK // Check the stack for dc. if (BC_IS_DC) { if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 2))) { bc_err(BC_ERR_EXEC_STACK); } } #endif // BC_PROG_NO_STACK_CHECK assert(BC_PROG_STACK(&p->results, idx + 2)); // Get the operands. bc_program_operand(p, l, ln, idx + 1); bc_program_operand(p, r, rn, idx); lt = (*l)->t; #if BC_ENABLED // bc_program_operand() checked these for us. assert(lt != BC_RESULT_VOID && (*r)->t != BC_RESULT_VOID); #endif // BC_ENABLED // We run this again under these conditions in case any vector has been // reallocated out from under the BcNums or arrays we had. In other words, // this is to fix pointer invalidation. if (lt == (*r)->t && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { *ln = bc_program_num(p, *l); } if (BC_ERR(lt == BC_RESULT_STR)) bc_err(BC_ERR_EXEC_TYPE); } /** * Prepares the operands of a binary operator and type checks them. This is * separate from bc_program_binPrep() because some places want this, others want * bc_program_binPrep(). * @param p The program. * @param l An out parameter; this is set to the pointer to the result for * the left operand. * @param ln An out parameter; this is set to the pointer to the number for * the left operand. * @param r An out parameter; this is set to the pointer to the result for * the right operand. * @param rn An out parameter; this is set to the pointer to the number for * the right operand. * @param idx The starting index where the operands are in the results stack, * starting from the top. */ static void bc_program_binOpPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r, BcNum** rn, size_t idx) { bc_program_binPrep(p, l, ln, r, rn, idx); bc_program_type_num(*l, *ln); bc_program_type_num(*r, *rn); } /** * Prepares the operands of an assignment operator. * @param p The program. * @param l An out parameter; this is set to the pointer to the result for the * left operand. * @param ln An out parameter; this is set to the pointer to the number for the * left operand. * @param r An out parameter; this is set to the pointer to the result for the * right operand. * @param rn An out parameter; this is set to the pointer to the number for the * right operand. */ static void bc_program_assignPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r, BcNum** rn) { BcResultType lt, min; bool good; // This is the min non-allowable result type. dc allows strings. min = BC_RESULT_TEMP - ((unsigned int) (BC_IS_BC)); // Prepare the operands. bc_program_binPrep(p, l, ln, r, rn, 0); lt = (*l)->t; // Typecheck the left. if (BC_ERR(lt >= min && lt <= BC_RESULT_ONE)) bc_err(BC_ERR_EXEC_TYPE); // Strings can be assigned to variables. We are already good if we are // assigning a string. good = ((*r)->t == BC_RESULT_STR && lt <= BC_RESULT_ARRAY_ELEM); assert(BC_PROG_STR(*rn) || (*r)->t != BC_RESULT_STR); // If not, type check for a number. if (!good) bc_program_type_num(*r, *rn); } /** * Prepares a single operand and type checks it. This is separate from * bc_program_operand() because different places want one or the other. * @param p The program. * @param r An out parameter; this is set to the pointer to the result that * we care about. * @param n An out parameter; this is set to the pointer to the number that * we care about. * @param idx The index of the result from the top of the results stack. */ static void bc_program_prep(BcProgram* p, BcResult** r, BcNum** n, size_t idx) { assert(p != NULL && r != NULL && n != NULL); #ifndef BC_PROG_NO_STACK_CHECK // Check the stack for dc. if (BC_IS_DC) { if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1))) { bc_err(BC_ERR_EXEC_STACK); } } #endif // BC_PROG_NO_STACK_CHECK assert(BC_PROG_STACK(&p->results, idx + 1)); bc_program_operand(p, r, n, idx); // dc does not allow strings in this case. bc_program_type_num(*r, *n); } /** * Prepares and returns a clean result for the result of an operation. * @param p The program. * @return A clean result. */ static BcResult* bc_program_prepResult(BcProgram* p) { BcResult* res = bc_vec_pushEmpty(&p->results); bc_result_clear(res); return res; } /** * Prepares a constant for use. This parses the constant into a number and then * pushes that number onto the results stack. * @param p The program. * @param code The bytecode vector that we will pull the index of the constant * from. * @param bgn An in/out parameter; marks the start of the index in the * bytecode vector and will be updated to point to after the index. */ static void bc_program_const(BcProgram* p, const char* code, size_t* bgn) { // I lied. I actually push the result first. I can do this because the // result will be popped on error. I also get the constant itself. BcResult* r = bc_program_prepResult(p); BcConst* c = bc_vec_item(&p->consts, bc_program_index(code, bgn)); BcBigDig base = BC_PROG_IBASE(p); // Only reparse if the base changed. if (c->base != base) { // Allocate if we haven't yet. if (c->num.num == NULL) { // The plus 1 is in case of overflow with lack of clamping. size_t len = strlen(c->val) + (BC_DIGIT_CLAMP == 0); BC_SIG_LOCK; bc_num_init(&c->num, BC_NUM_RDX(len)); BC_SIG_UNLOCK; } // We need to zero an already existing number. else bc_num_zero(&c->num); // bc_num_parse() should only do operations that cannot fail. bc_num_parse(&c->num, c->val, base); c->base = base; } BC_SIG_LOCK; bc_num_createCopy(&r->d.n, &c->num); BC_SIG_UNLOCK; } /** * Executes a binary operator operation. * @param p The program. * @param inst The instruction corresponding to the binary operator to execute. */ static void bc_program_op(BcProgram* p, uchar inst) { BcResult* opd1; BcResult* opd2; BcResult* res; BcNum* n1; BcNum* n2; size_t idx = inst - BC_INST_POWER; res = bc_program_prepResult(p); bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 1); BC_SIG_LOCK; // Initialize the number with enough space, using the correct // BcNumBinaryOpReq function. This looks weird because it is executing an // item of an array. Rest assured that item is a function. bc_num_init(&res->d.n, bc_program_opReqs[idx](n1, n2, BC_PROG_SCALE(p))); BC_SIG_UNLOCK; assert(BC_NUM_RDX_VALID(n1)); assert(BC_NUM_RDX_VALID(n2)); // Run the operation. This also executes an item of an array. bc_program_ops[idx](n1, n2, &res->d.n, BC_PROG_SCALE(p)); bc_program_retire(p, 1, 2); } /** * Executes a read() or ? command. * @param p The program. */ static void bc_program_read(BcProgram* p) { BcStatus s; BcInstPtr ip; size_t i; const char* file; BcMode mode; BcFunc* f = bc_vec_item(&p->fns, BC_PROG_READ); // If we are already executing a read, that is an error. So look for a read // and barf. for (i = 0; i < p->stack.len; ++i) { BcInstPtr* ip_ptr = bc_vec_item(&p->stack, i); if (ip_ptr->func == BC_PROG_READ) bc_err(BC_ERR_EXEC_REC_READ); } BC_SIG_LOCK; // Save the filename because we are going to overwrite it. file = vm->file; mode = vm->mode; // It is a parse error if there needs to be more than one line, so we unset // this to tell the lexer to not request more. We set it back later. vm->mode = BC_MODE_FILE; if (!BC_PARSE_IS_INITED(&vm->read_prs, p)) { // We need to parse, but we don't want to use the existing parser // because it has state it needs to keep. (It could have a partial parse // state.) So we create a new parser. This parser is in the BcVm struct // so that it is not local, which means that a longjmp() could change // it. bc_parse_init(&vm->read_prs, p, BC_PROG_READ); // We need a separate input buffer; that's why it is also in the BcVm // struct. bc_vec_init(&vm->read_buf, sizeof(char), BC_DTOR_NONE); } // This needs to be updated because the parser could have been used // somewhere else else bc_parse_updateFunc(&vm->read_prs, BC_PROG_READ); BC_SETJMP_LOCKED(vm, exec_err); BC_SIG_UNLOCK; // Set up the lexer and the read function. bc_lex_file(&vm->read_prs.l, bc_program_stdin_name); bc_vec_popAll(&f->code); // Read a line. if (!BC_R) s = bc_read_line(&vm->read_buf, ""); else s = bc_read_line(&vm->read_buf, BC_VM_READ_PROMPT); // We should *not* have run into EOF. if (s == BC_STATUS_EOF) bc_err(BC_ERR_EXEC_READ_EXPR); // Parse *one* expression, so mode should not be stdin. bc_parse_text(&vm->read_prs, vm->read_buf.v, BC_MODE_FILE); BC_SIG_LOCK; vm->expr(&vm->read_prs, BC_PARSE_NOREAD | BC_PARSE_NEEDVAL); BC_SIG_UNLOCK; // We *must* have a valid expression. A semicolon cannot end an expression, // although EOF can. if (BC_ERR(vm->read_prs.l.t != BC_LEX_NLINE && vm->read_prs.l.t != BC_LEX_EOF)) { bc_err(BC_ERR_EXEC_READ_EXPR); } #if BC_ENABLED // Push on the globals stack if necessary. if (BC_G) bc_program_prepGlobals(p); #endif // BC_ENABLED // Set up a new BcInstPtr. ip.func = BC_PROG_READ; ip.idx = 0; ip.len = p->results.len; // Update this pointer, just in case. f = bc_vec_item(&p->fns, BC_PROG_READ); // We want a return instruction to simplify things. bc_vec_pushByte(&f->code, vm->read_ret); // This lock is here to make sure dc's tail calls are the same length. BC_SIG_LOCK; bc_vec_push(&p->stack, &ip); #if DC_ENABLED // We need a new tail call entry for dc. if (BC_IS_DC) { size_t temp = 0; bc_vec_push(&p->tail_calls, &temp); } #endif // DC_ENABLED exec_err: BC_SIG_MAYLOCK; vm->mode = (uchar) mode; vm->file = file; BC_LONGJMP_CONT(vm); } #if BC_ENABLE_EXTRA_MATH /** * Execute a rand(). * @param p The program. */ static void bc_program_rand(BcProgram* p) { BcRand rand = bc_rand_int(&p->rng); bc_program_pushBigdig(p, (BcBigDig) rand, BC_RESULT_TEMP); #ifndef NDEBUG // This is just to ensure that the generated number is correct. I also use // braces because I declare every local at the top of the scope. { BcResult* r = bc_vec_top(&p->results); assert(BC_NUM_RDX_VALID_NP(r->d.n)); } #endif // NDEBUG } #endif // BC_ENABLE_EXTRA_MATH /** * Prints a series of characters, without escapes. * @param str The string (series of characters). */ static void bc_program_printChars(const char* str) { const char* nl; size_t len = vm->nchars + strlen(str); sig_atomic_t lock; BC_SIG_TRYLOCK(lock); bc_file_puts(&vm->fout, bc_flush_save, str); // We need to update the number of characters, so we find the last newline // and set the characters accordingly. nl = strrchr(str, '\n'); if (nl != NULL) len = strlen(nl + 1); vm->nchars = len > UINT16_MAX ? UINT16_MAX : (uint16_t) len; BC_SIG_TRYUNLOCK(lock); } /** * Prints a string with escapes. * @param str The string. */ static void bc_program_printString(const char* restrict str) { size_t i, len = strlen(str); #if DC_ENABLED // This is to ensure a nul byte is printed for dc's stream operation. if (!len && BC_IS_DC) { bc_vm_putchar('\0', bc_flush_save); return; } #endif // DC_ENABLED // Loop over the characters, processing escapes and printing the rest. for (i = 0; i < len; ++i) { int c = str[i]; // If we have an escape... if (c == '\\' && i != len - 1) { const char* ptr; // Get the escape character and its companion. c = str[++i]; ptr = strchr(bc_program_esc_chars, c); // If we have a companion character... if (ptr != NULL) { // We need to specially handle a newline. if (c == 'n') { BC_SIG_LOCK; vm->nchars = UINT16_MAX; BC_SIG_UNLOCK; } // Grab the actual character. c = bc_program_esc_seqs[(size_t) (ptr - bc_program_esc_chars)]; } else { // Just print the backslash if there is no companion character. // The following character will be printed later after the outer // if statement. bc_vm_putchar('\\', bc_flush_save); } } bc_vm_putchar(c, bc_flush_save); } } /** * Executes a print. This function handles all printing except streaming. * @param p The program. * @param inst The instruction for the type of print we are doing. * @param idx The index of the result that we are printing. */ static void bc_program_print(BcProgram* p, uchar inst, size_t idx) { BcResult* r; char* str; BcNum* n; bool pop = (inst != BC_INST_PRINT); assert(p != NULL); #ifndef BC_PROG_NO_STACK_CHECK if (BC_IS_DC) { if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1))) { bc_err(BC_ERR_EXEC_STACK); } } #endif // BC_PROG_NO_STACK_CHECK assert(BC_PROG_STACK(&p->results, idx + 1)); r = bc_vec_item_rev(&p->results, idx); #if BC_ENABLED // If we have a void value, that's not necessarily an error. It is if pop is // true because that means that we are executing a print statement, but // attempting to do a print on a lone void value is allowed because that's // exactly how we want void values used. if (r->t == BC_RESULT_VOID) { if (BC_ERR(pop)) bc_err(BC_ERR_EXEC_VOID_VAL); bc_vec_pop(&p->results); return; } #endif // BC_ENABLED n = bc_program_num(p, r); // If we have a number... if (BC_PROG_NUM(r, n)) { #if BC_ENABLED assert(inst != BC_INST_PRINT_STR); #endif // BC_ENABLED // Print the number. bc_num_print(n, BC_PROG_OBASE(p), !pop); #if BC_ENABLED // Need to store the number in last. if (BC_IS_BC) bc_num_copy(&p->last, n); #endif // BC_ENABLED } else { // We want to flush any stuff in the stdout buffer first. bc_file_flush(&vm->fout, bc_flush_save); str = bc_program_string(p, n); #if BC_ENABLED if (inst == BC_INST_PRINT_STR) bc_program_printChars(str); else #endif // BC_ENABLED { bc_program_printString(str); // Need to print a newline only in this case. if (inst == BC_INST_PRINT) bc_vm_putchar('\n', bc_flush_err); } } // bc always pops. This macro makes sure that happens. if (BC_PROGRAM_POP(pop)) bc_vec_pop(&p->results); } void bc_program_negate(BcResult* r, BcNum* n) { bc_num_copy(&r->d.n, n); if (BC_NUM_NONZERO(&r->d.n)) BC_NUM_NEG_TGL_NP(r->d.n); } void bc_program_not(BcResult* r, BcNum* n) { if (!bc_num_cmpZero(n)) bc_num_one(&r->d.n); } #if BC_ENABLE_EXTRA_MATH void bc_program_trunc(BcResult* r, BcNum* n) { bc_num_copy(&r->d.n, n); bc_num_truncate(&r->d.n, n->scale); } #endif // BC_ENABLE_EXTRA_MATH /** * Runs a unary operation. * @param p The program. * @param inst The unary operation. */ static void bc_program_unary(BcProgram* p, uchar inst) { BcResult* res; BcResult* ptr; BcNum* num; res = bc_program_prepResult(p); bc_program_prep(p, &ptr, &num, 1); BC_SIG_LOCK; bc_num_init(&res->d.n, num->len); BC_SIG_UNLOCK; // This calls a function that is in an array. bc_program_unarys[inst - BC_INST_NEG](res, num); bc_program_retire(p, 1, 1); } /** * Executes a logical operator. * @param p The program. * @param inst The operator. */ static void bc_program_logical(BcProgram* p, uchar inst) { BcResult* opd1; BcResult* opd2; BcResult* res; BcNum* n1; BcNum* n2; bool cond = 0; ssize_t cmp; res = bc_program_prepResult(p); // All logical operators (except boolean not, which is taken care of by // bc_program_unary()), are binary operators. bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 1); // Boolean and and or are not short circuiting. This is why; they can be // implemented much easier this way. if (inst == BC_INST_BOOL_AND) { cond = (bc_num_cmpZero(n1) && bc_num_cmpZero(n2)); } else if (inst == BC_INST_BOOL_OR) { cond = (bc_num_cmpZero(n1) || bc_num_cmpZero(n2)); } else { // We have a relational operator, so do a comparison. cmp = bc_num_cmp(n1, n2); switch (inst) { case BC_INST_REL_EQ: { cond = (cmp == 0); break; } case BC_INST_REL_LE: { cond = (cmp <= 0); break; } case BC_INST_REL_GE: { cond = (cmp >= 0); break; } case BC_INST_REL_NE: { cond = (cmp != 0); break; } case BC_INST_REL_LT: { cond = (cmp < 0); break; } case BC_INST_REL_GT: { cond = (cmp > 0); break; } #ifndef NDEBUG default: { // There is a bug if we get here. abort(); } #endif // NDEBUG } } BC_SIG_LOCK; bc_num_init(&res->d.n, BC_NUM_DEF_SIZE); BC_SIG_UNLOCK; if (cond) bc_num_one(&res->d.n); bc_program_retire(p, 1, 2); } /** * Assigns a string to a variable. * @param p The program. * @param num The location of the string as a BcNum. * @param v The stack for the variable. * @param push Whether to push the string or not. To push means to move the * string from the results stack and push it onto the variable * stack. */ static void bc_program_assignStr(BcProgram* p, BcNum* num, BcVec* v, bool push) { BcNum* n; assert(BC_PROG_STACK(&p->results, 1 + !push)); assert(num != NULL && num->num == NULL && num->cap == 0); // If we are not pushing onto the variable stack, we need to replace the // top of the variable stack. if (!push) bc_vec_pop(v); bc_vec_npop(&p->results, 1 + !push); n = bc_vec_pushEmpty(v); // We can just copy because the num should not have allocated anything. // NOLINTNEXTLINE memcpy(n, num, sizeof(BcNum)); } /** * Copies a value to a variable. This is used for storing in dc as well as to * set function parameters to arguments in bc. - * @param p The program. - * @param idx The index of the variable or array to copy to. - * @param t The type to copy to. This could be a variable or an array. - * @param last Whether to grab the last item on the variable stack or not (for - * bc function parameters). This is important because if a new - * value has been pushed to the variable already, we need to grab - * the value pushed before. This happens when you have a parameter - * named something like "x", and a variable "x" is passed to - * another parameter. + * @param p The program. + * @param idx The index of the variable or array to copy to. + * @param t The type to copy to. This could be a variable or an array. */ static void -bc_program_copyToVar(BcProgram* p, size_t idx, BcType t, bool last) +bc_program_copyToVar(BcProgram* p, size_t idx, BcType t) { BcResult *ptr = NULL, r; BcVec* vec; BcNum* n = NULL; bool var = (t == BC_TYPE_VAR); #if DC_ENABLED // Check the stack for dc. if (BC_IS_DC) { if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK); } #endif assert(BC_PROG_STACK(&p->results, 1)); bc_program_operand(p, &ptr, &n, 0); #if BC_ENABLED // Get the variable for a bc function call. if (BC_IS_BC) { // Type match the result. bc_program_type_match(ptr, t); - - // Get the variable or array, taking care to get the real item. We take - // care of last with arrays later. - if (!last && var) - { - n = bc_vec_item_rev(bc_program_vec(p, ptr->d.loc.loc, t), 1); - } } #endif // BC_ENABLED vec = bc_program_vec(p, idx, t); // We can shortcut in dc if it's assigning a string by using // bc_program_assignStr(). if (ptr->t == BC_RESULT_STR) { assert(BC_PROG_STR(n)); if (BC_ERR(!var)) bc_err(BC_ERR_EXEC_TYPE); bc_program_assignStr(p, n, vec, true); return; } BC_SIG_LOCK; // Just create and copy for a normal variable. if (var) { if (BC_PROG_STR(n)) { // NOLINTNEXTLINE memcpy(&r.d.n, n, sizeof(BcNum)); } else bc_num_createCopy(&r.d.n, n); } else { // If we get here, we are handling an array. This is one place we need // to cast the number from bc_program_num() to a vector. BcVec* v = (BcVec*) n; BcVec* rv = &r.d.v; #if BC_ENABLED if (BC_IS_BC) { - BcVec* parent; bool ref, ref_size; - // We need to figure out if the parameter is a reference or not and - // construct the reference vector, if necessary. So this gets the - // parent stack for the array. - parent = bc_program_vec(p, ptr->d.loc.loc, t); - assert(parent != NULL); - - // This takes care of last for arrays. Mostly. - if (!last) v = bc_vec_item_rev(parent, !last); - assert(v != NULL); - // True if we are using a reference. ref = (v->size == sizeof(BcNum) && t == BC_TYPE_REF); // True if we already have a reference vector. This is slightly // (okay, a lot; it just doesn't look that way) different from // above. The above means that we need to construct a reference // vector, whereas this means that we have one and we might have to // *dereference* it. ref_size = (v->size == sizeof(uchar)); // If we *should* have a reference. if (ref || (ref_size && t == BC_TYPE_REF)) { // Create a new reference vector. bc_vec_init(rv, sizeof(uchar), BC_DTOR_NONE); // If this is true, then we need to construct a reference. if (ref) { - assert(parent->len >= (size_t) (!last + 1)); - // Make sure the pointer was not invalidated. vec = bc_program_vec(p, idx, t); // Push the indices onto the reference vector. This takes // care of last; it ensures the reference goes to the right // place. bc_vec_pushIndex(rv, ptr->d.loc.loc); - bc_vec_pushIndex(rv, parent->len - !last - 1); + bc_vec_pushIndex(rv, ptr->d.loc.stack_idx); } // If we get here, we are copying a ref to a ref. Just push a // copy of all of the bytes. else bc_vec_npush(rv, v->len * sizeof(uchar), v->v); // Push the reference vector onto the array stack and pop the // source. bc_vec_push(vec, &r.d); bc_vec_pop(&p->results); // We need to return early to avoid executing code that we must // not touch. BC_SIG_UNLOCK; return; } // If we get here, we have a reference, but we need an array, so // dereference the array. else if (ref_size && t != BC_TYPE_REF) { v = bc_program_dereference(p, v); } } #endif // BC_ENABLED // If we get here, we need to copy the array because in bc, all // arguments are passed by value. Yes, this is expensive. bc_array_init(rv, true); bc_array_copy(rv, v); } // Push the vector onto the array stack and pop the source. bc_vec_push(vec, &r.d); bc_vec_pop(&p->results); BC_SIG_UNLOCK; } void bc_program_assignBuiltin(BcProgram* p, bool scale, bool obase, BcBigDig val) { BcBigDig* ptr_t; BcBigDig max, min; #if BC_ENABLED BcVec* v; BcBigDig* ptr; #endif // BC_ENABLED assert(!scale || !obase); // Scale needs handling separate from ibase and obase. if (scale) { // Set the min and max. min = 0; max = vm->maxes[BC_PROG_GLOBALS_SCALE]; #if BC_ENABLED // Get a pointer to the stack. v = p->globals_v + BC_PROG_GLOBALS_SCALE; #endif // BC_ENABLED // Get a pointer to the current value. ptr_t = p->globals + BC_PROG_GLOBALS_SCALE; } else { // Set the min and max. min = BC_NUM_MIN_BASE; if (BC_ENABLE_EXTRA_MATH && obase && (BC_IS_DC || !BC_IS_POSIX)) { min = 0; } max = vm->maxes[obase + BC_PROG_GLOBALS_IBASE]; #if BC_ENABLED // Get a pointer to the stack. v = p->globals_v + BC_PROG_GLOBALS_IBASE + obase; #endif // BC_ENABLED // Get a pointer to the current value. ptr_t = p->globals + BC_PROG_GLOBALS_IBASE + obase; } // Check for error. if (BC_ERR(val > max || val < min)) { BcErr e; // This grabs the right error. if (scale) e = BC_ERR_EXEC_SCALE; else if (obase) e = BC_ERR_EXEC_OBASE; else e = BC_ERR_EXEC_IBASE; bc_verr(e, min, max); } #if BC_ENABLED // Set the top of the stack. ptr = bc_vec_top(v); *ptr = val; #endif // BC_ENABLED // Set the actual global variable. *ptr_t = val; } #if BC_ENABLE_EXTRA_MATH void bc_program_assignSeed(BcProgram* p, BcNum* val) { bc_num_rng(val, &p->rng); } #endif // BC_ENABLE_EXTRA_MATH /** * Executes an assignment operator. * @param p The program. * @param inst The assignment operator to execute. */ static void bc_program_assign(BcProgram* p, uchar inst) { // The local use_val is true when the assigned value needs to be copied. BcResult* left; BcResult* right; BcResult res; BcNum* l; BcNum* r; bool ob, sc, use_val = BC_INST_USE_VAL(inst); bc_program_assignPrep(p, &left, &l, &right, &r); // Assigning to a string should be impossible simply because of the parse. assert(left->t != BC_RESULT_STR); // If we are assigning a string... if (right->t == BC_RESULT_STR) { assert(BC_PROG_STR(r)); #if BC_ENABLED if (inst != BC_INST_ASSIGN && inst != BC_INST_ASSIGN_NO_VAL) { bc_err(BC_ERR_EXEC_TYPE); } #endif // BC_ENABLED // If we are assigning to an array element... if (left->t == BC_RESULT_ARRAY_ELEM) { BC_SIG_LOCK; // We need to free the number and clear it. bc_num_free(l); // NOLINTNEXTLINE memcpy(l, r, sizeof(BcNum)); // Now we can pop the results. bc_vec_npop(&p->results, 2); BC_SIG_UNLOCK; } else { // If we get here, we are assigning to a variable, which we can use // bc_program_assignStr() for. BcVec* v = bc_program_vec(p, left->d.loc.loc, BC_TYPE_VAR); bc_program_assignStr(p, r, v, false); } #if BC_ENABLED // If this is true, the value is going to be used again, so we want to // push a temporary with the string. if (inst == BC_INST_ASSIGN) { res.t = BC_RESULT_STR; // NOLINTNEXTLINE memcpy(&res.d.n, r, sizeof(BcNum)); bc_vec_push(&p->results, &res); } #endif // BC_ENABLED // By using bc_program_assignStr(), we short-circuited this, so return. return; } // If we have a normal assignment operator, not a math one... if (BC_INST_IS_ASSIGN(inst)) { // Assigning to a variable that has a string here is fine because there // is no math done on it. // BC_RESULT_TEMP, BC_RESULT_IBASE, BC_RESULT_OBASE, BC_RESULT_SCALE, // and BC_RESULT_SEED all have temporary copies. Because that's the // case, we can free the left and just move the value over. We set the // type of right to BC_RESULT_ZERO in order to prevent it from being // freed. We also don't have to worry about BC_RESULT_STR because it's // take care of above. if (right->t == BC_RESULT_TEMP || right->t >= BC_RESULT_IBASE) { BC_SIG_LOCK; bc_num_free(l); // NOLINTNEXTLINE memcpy(l, r, sizeof(BcNum)); right->t = BC_RESULT_ZERO; BC_SIG_UNLOCK; } // Copy over. else bc_num_copy(l, r); } #if BC_ENABLED else { // If we get here, we are doing a math assignment (+=, -=, etc.). So // we need to prepare for a binary operator. BcBigDig scale = BC_PROG_SCALE(p); // At this point, the left side could still be a string because it could // be a variable that has the string. If that's the case, we have a type // error. if (BC_PROG_STR(l)) bc_err(BC_ERR_EXEC_TYPE); // Get the right type of assignment operator, whether val is used or // NO_VAL for performance. if (!use_val) { inst -= (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER); } assert(BC_NUM_RDX_VALID(l)); assert(BC_NUM_RDX_VALID(r)); // Run the actual operation. We do not need worry about reallocating l // because bc_num_binary() does that behind the scenes for us. bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, scale); } #endif // BC_ENABLED ob = (left->t == BC_RESULT_OBASE); sc = (left->t == BC_RESULT_SCALE); // The globals need special handling, especially the non-seed ones. The // first part of the if statement handles them. if (ob || sc || left->t == BC_RESULT_IBASE) { // Get the actual value. BcBigDig val = bc_num_bigdig(l); bc_program_assignBuiltin(p, sc, ob, val); } #if BC_ENABLE_EXTRA_MATH // To assign to steed, let bc_num_rng() do its magic. else if (left->t == BC_RESULT_SEED) bc_program_assignSeed(p, l); #endif // BC_ENABLE_EXTRA_MATH BC_SIG_LOCK; // If we needed to use the value, then we need to copy it. Otherwise, we can // pop indiscriminately. Oh, and the copy should be a BC_RESULT_TEMP. if (use_val) { bc_num_createCopy(&res.d.n, l); res.t = BC_RESULT_TEMP; bc_vec_npop(&p->results, 2); bc_vec_push(&p->results, &res); } else bc_vec_npop(&p->results, 2); BC_SIG_UNLOCK; } /** * Pushes a variable's value onto the results stack. * @param p The program. * @param code The bytecode vector to pull the variable's index out of. * @param bgn An in/out parameter; the start of the index in the bytecode * vector, and will be updated to point after the index on return. * @param pop True if the variable's value should be popped off its stack. * This is only used in dc. * @param copy True if the variable's value should be copied to the results * stack. This is only used in dc. */ static void bc_program_pushVar(BcProgram* p, const char* restrict code, size_t* restrict bgn, bool pop, bool copy) { BcResult r; size_t idx = bc_program_index(code, bgn); + BcVec* v; // Set the result appropriately. r.t = BC_RESULT_VAR; r.d.loc.loc = idx; + // Get the stack for the variable. This is used in both bc and dc. + v = bc_program_vec(p, idx, BC_TYPE_VAR); + r.d.loc.stack_idx = v->len - 1; + #if DC_ENABLED // If this condition is true, then we have the hard case, where we have to // adjust dc registers. if (BC_IS_DC && (pop || copy)) { - // Get the stack for the variable and the number at the top. - BcVec* v = bc_program_vec(p, idx, BC_TYPE_VAR); + // Get the number at the top at the top of the stack. BcNum* num = bc_vec_top(v); // Ensure there are enough elements on the stack. if (BC_ERR(!BC_PROG_STACK(v, 2 - copy))) { const char* name = bc_map_name(&p->var_map, idx); bc_verr(BC_ERR_EXEC_STACK_REGISTER, name); } assert(BC_PROG_STACK(v, 2 - copy)); // If the top of the stack is actually a number... if (!BC_PROG_STR(num)) { BC_SIG_LOCK; // Create a copy to go onto the results stack as appropriate. r.t = BC_RESULT_TEMP; bc_num_createCopy(&r.d.n, num); // If we are not actually copying, we need to do a replace, so pop. if (!copy) bc_vec_pop(v); bc_vec_push(&p->results, &r); BC_SIG_UNLOCK; return; } else { // Set the string result. We can just memcpy because all of the // fields in the num should be cleared. // NOLINTNEXTLINE memcpy(&r.d.n, num, sizeof(BcNum)); r.t = BC_RESULT_STR; } // If we are not actually copying, we need to do a replace, so pop. if (!copy) bc_vec_pop(v); } #endif // DC_ENABLED bc_vec_push(&p->results, &r); } /** * Pushes an array or an array element onto the results stack. * @param p The program. * @param code The bytecode vector to pull the variable's index out of. * @param bgn An in/out parameter; the start of the index in the bytecode * vector, and will be updated to point after the index on return. * @param inst The instruction; whether to push an array or an array element. */ static void bc_program_pushArray(BcProgram* p, const char* restrict code, size_t* restrict bgn, uchar inst) { BcResult r; BcResult* operand; BcNum* num; BcBigDig temp; + BcVec* v; // Get the index of the array. r.d.loc.loc = bc_program_index(code, bgn); + // We need the array to get its length. + v = bc_program_vec(p, r.d.loc.loc, BC_TYPE_ARRAY); + assert(v != NULL); + + r.d.loc.stack_idx = v->len - 1; + // Doing an array is easy; just set the result type and finish. if (inst == BC_INST_ARRAY) { r.t = BC_RESULT_ARRAY; bc_vec_push(&p->results, &r); return; } // Grab the top element of the results stack for the array index. bc_program_prep(p, &operand, &num, 0); temp = bc_num_bigdig(num); // Set the result. r.t = BC_RESULT_ARRAY_ELEM; r.d.loc.idx = (size_t) temp; BC_SIG_LOCK; // Pop the index and push the element. bc_vec_pop(&p->results); bc_vec_push(&p->results, &r); BC_SIG_UNLOCK; } #if BC_ENABLED /** * Executes an increment or decrement operator. This only handles postfix * inc/dec because the parser translates prefix inc/dec into an assignment where * the value is used. * @param p The program. * @param inst The instruction; whether to do an increment or decrement. */ static void bc_program_incdec(BcProgram* p, uchar inst) { BcResult *ptr, res, copy; BcNum* num; uchar inst2; bc_program_prep(p, &ptr, &num, 0); BC_SIG_LOCK; // We need a copy from *before* the operation. copy.t = BC_RESULT_TEMP; bc_num_createCopy(©.d.n, num); BC_SETJMP_LOCKED(vm, exit); BC_SIG_UNLOCK; // Create the proper assignment. res.t = BC_RESULT_ONE; inst2 = BC_INST_ASSIGN_PLUS_NO_VAL + (inst & 0x01); bc_vec_push(&p->results, &res); bc_program_assign(p, inst2); BC_SIG_LOCK; bc_vec_push(&p->results, ©); BC_UNSETJMP(vm); BC_SIG_UNLOCK; // No need to free the copy here because we pushed it onto the stack. return; exit: BC_SIG_MAYLOCK; bc_num_free(©.d.n); BC_LONGJMP_CONT(vm); } /** * Executes a function call for bc. * @param p The program. * @param code The bytecode vector to pull the number of arguments and the * function index out of. * @param bgn An in/out parameter; the start of the indices in the bytecode * vector, and will be updated to point after the indices on * return. */ static void bc_program_call(BcProgram* p, const char* restrict code, size_t* restrict bgn) { BcInstPtr ip; size_t i, nargs; BcFunc* f; BcVec* v; BcAuto* a; BcResult* arg; // Pull the number of arguments out of the bytecode vector. nargs = bc_program_index(code, bgn); // Set up instruction pointer. ip.idx = 0; ip.func = bc_program_index(code, bgn); f = bc_vec_item(&p->fns, ip.func); // Error checking. if (BC_ERR(!f->code.len)) bc_verr(BC_ERR_EXEC_UNDEF_FUNC, f->name); if (BC_ERR(nargs != f->nparams)) { bc_verr(BC_ERR_EXEC_PARAMS, f->nparams, nargs); } // Set the length of the results stack. We discount the argument, of course. ip.len = p->results.len - nargs; assert(BC_PROG_STACK(&p->results, nargs)); // Prepare the globals' stacks. if (BC_G) bc_program_prepGlobals(p); // Push the arguments onto the stacks of their respective parameters. for (i = 0; i < nargs; ++i) { - size_t j; - bool last = true; - arg = bc_vec_top(&p->results); if (BC_ERR(arg->t == BC_RESULT_VOID)) bc_err(BC_ERR_EXEC_VOID_VAL); // Get the corresponding parameter. a = bc_vec_item(&f->autos, nargs - 1 - i); - // If I have already pushed to a var, I need to make sure I - // get the previous version, not the already pushed one. This condition - // must be true for that to even be possible. - if (arg->t == BC_RESULT_VAR || arg->t == BC_RESULT_ARRAY) - { - // Loop through all of the previous parameters. - for (j = 0; j < i && last; ++j) - { - BcAuto* aptr = bc_vec_item(&f->autos, nargs - 1 - j); - - // This condition is true if there is a previous parameter with - // the same name *and* type because variables and arrays do not - // interfere with each other. - last = (arg->d.loc.loc != aptr->idx || - (!aptr->type) != (arg->t == BC_RESULT_VAR)); - } - } - // Actually push the value onto the parameter's stack. - bc_program_copyToVar(p, a->idx, a->type, last); + bc_program_copyToVar(p, a->idx, a->type); } BC_SIG_LOCK; // Push zeroes onto the stacks of the auto variables. for (; i < f->autos.len; ++i) { // Get the auto and its stack. a = bc_vec_item(&f->autos, i); v = bc_program_vec(p, a->idx, a->type); // If a variable, just push a 0; otherwise, push an array. if (a->type == BC_TYPE_VAR) { BcNum* n = bc_vec_pushEmpty(v); bc_num_init(n, BC_NUM_DEF_SIZE); } else { BcVec* v2; assert(a->type == BC_TYPE_ARRAY); v2 = bc_vec_pushEmpty(v); bc_array_init(v2, true); } } // Push the instruction pointer onto the execution stack. bc_vec_push(&p->stack, &ip); BC_SIG_UNLOCK; } /** * Executes a return instruction. * @param p The program. * @param inst The return instruction. bc can return void, and we need to know * if it is. */ static void bc_program_return(BcProgram* p, uchar inst) { BcResult* res; BcFunc* f; BcInstPtr* ip; size_t i, nresults; // Get the instruction pointer. ip = bc_vec_top(&p->stack); // Get the difference between the actual number of results and the number of // results the caller expects. nresults = p->results.len - ip->len; // If this isn't true, there was a missing call somewhere. assert(BC_PROG_STACK(&p->stack, 2)); // If this isn't true, the parser screwed by giving us no value when we // expected one, or giving us a value when we expected none. assert(BC_PROG_STACK(&p->results, ip->len + (inst == BC_INST_RET))); // Get the function we are returning from. f = bc_vec_item(&p->fns, ip->func); res = bc_program_prepResult(p); // If we are returning normally... if (inst == BC_INST_RET) { BcNum* num; BcResult* operand; // Prepare and copy the return value. bc_program_operand(p, &operand, &num, 1); if (BC_PROG_STR(num)) { // We need to set this because otherwise, it will be a // BC_RESULT_TEMP, and BC_RESULT_TEMP needs an actual number to make // it easier to do type checking. res->t = BC_RESULT_STR; // NOLINTNEXTLINE memcpy(&res->d.n, num, sizeof(BcNum)); } else { BC_SIG_LOCK; bc_num_createCopy(&res->d.n, num); } } // Void is easy; set the result. else if (inst == BC_INST_RET_VOID) res->t = BC_RESULT_VOID; else { BC_SIG_LOCK; // If we get here, the instruction is for returning a zero, so do that. bc_num_init(&res->d.n, BC_NUM_DEF_SIZE); } BC_SIG_MAYUNLOCK; // We need to pop items off of the stacks of arguments and autos as well. for (i = 0; i < f->autos.len; ++i) { BcAuto* a = bc_vec_item(&f->autos, i); BcVec* v = bc_program_vec(p, a->idx, a->type); bc_vec_pop(v); } BC_SIG_LOCK; // When we retire, pop all of the unused results. bc_program_retire(p, 1, nresults); // Pop the globals, if necessary. if (BC_G) bc_program_popGlobals(p, false); // Pop the stack. This is what causes the function to actually "return." bc_vec_pop(&p->stack); BC_SIG_UNLOCK; } #endif // BC_ENABLED /** * Executes a builtin function. * @param p The program. * @param inst The builtin to execute. */ static void bc_program_builtin(BcProgram* p, uchar inst) { BcResult* opd; BcResult* res; BcNum* num; bool len = (inst == BC_INST_LENGTH); // Ensure we have a valid builtin. #if BC_ENABLE_EXTRA_MATH assert(inst >= BC_INST_LENGTH && inst <= BC_INST_IRAND); #else // BC_ENABLE_EXTRA_MATH assert(inst >= BC_INST_LENGTH && inst <= BC_INST_IS_STRING); #endif // BC_ENABLE_EXTRA_MATH #ifndef BC_PROG_NO_STACK_CHECK // Check stack for dc. if (BC_IS_DC && BC_ERR(!BC_PROG_STACK(&p->results, 1))) { bc_err(BC_ERR_EXEC_STACK); } #endif // BC_PROG_NO_STACK_CHECK assert(BC_PROG_STACK(&p->results, 1)); res = bc_program_prepResult(p); bc_program_operand(p, &opd, &num, 1); assert(num != NULL); // We need to ensure that strings and arrays aren't passed to most builtins. // The scale function can take strings in dc. if (!len && (inst != BC_INST_SCALE_FUNC || BC_IS_BC) && inst != BC_INST_IS_NUMBER && inst != BC_INST_IS_STRING) { bc_program_type_num(opd, num); } // Square root is easy. if (inst == BC_INST_SQRT) bc_num_sqrt(num, &res->d.n, BC_PROG_SCALE(p)); // Absolute value is easy. else if (inst == BC_INST_ABS) { BC_SIG_LOCK; bc_num_createCopy(&res->d.n, num); BC_SIG_UNLOCK; BC_NUM_NEG_CLR_NP(res->d.n); } // Testing for number or string is easy. else if (inst == BC_INST_IS_NUMBER || inst == BC_INST_IS_STRING) { bool cond; bool is_str; BC_SIG_LOCK; bc_num_init(&res->d.n, BC_NUM_DEF_SIZE); BC_SIG_UNLOCK; // Test if the number is a string. is_str = BC_PROG_STR(num); // This confusing condition simply means that the instruction must be // true if is_str is, or it must be false if is_str is. Otherwise, the // returned value is false (0). cond = ((inst == BC_INST_IS_STRING) == is_str); if (cond) bc_num_one(&res->d.n); } #if BC_ENABLE_EXTRA_MATH // irand() is easy. else if (inst == BC_INST_IRAND) { BC_SIG_LOCK; bc_num_init(&res->d.n, num->len - BC_NUM_RDX_VAL(num)); BC_SIG_UNLOCK; bc_num_irand(num, &res->d.n, &p->rng); } #endif // BC_ENABLE_EXTRA_MATH // Everything else is...not easy. else { BcBigDig val = 0; // Well, scale() is easy, but length() is not. if (len) { // If we are bc and we have an array... if (opd->t == BC_RESULT_ARRAY) { // Yes, this is one place where we need to cast the number from // bc_program_num() to a vector. BcVec* v = (BcVec*) num; // XXX: If this is changed, you should also change the similar // code in bc_program_asciify(). #if BC_ENABLED // Dereference the array, if necessary. if (BC_IS_BC && v->size == sizeof(uchar)) { v = bc_program_dereference(p, v); } #endif // BC_ENABLED assert(v->size == sizeof(BcNum)); val = (BcBigDig) v->len; } else { // If the item is a string... if (!BC_PROG_NUM(opd, num)) { char* str; // Get the string, then get the length. str = bc_program_string(p, num); val = (BcBigDig) strlen(str); } else { // Calculate the length of the number. val = (BcBigDig) bc_num_len(num); } } } // Like I said; scale() is actually easy. It just also needs the integer // conversion that length() does. else if (BC_IS_BC || BC_PROG_NUM(opd, num)) { val = (BcBigDig) bc_num_scale(num); } BC_SIG_LOCK; // Create the result. bc_num_createFromBigdig(&res->d.n, val); BC_SIG_UNLOCK; } bc_program_retire(p, 1, 1); } /** * Executes a divmod. * @param p The program. */ static void bc_program_divmod(BcProgram* p) { BcResult* opd1; BcResult* opd2; BcResult* res; BcResult* res2; BcNum* n1; BcNum* n2; size_t req; // We grow first to avoid pointer invalidation. bc_vec_grow(&p->results, 2); // We don't need to update the pointer because // the capacity is enough due to the line above. res2 = bc_program_prepResult(p); res = bc_program_prepResult(p); // Prepare the operands. bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 2); req = bc_num_mulReq(n1, n2, BC_PROG_SCALE(p)); BC_SIG_LOCK; // Initialize the results. bc_num_init(&res->d.n, req); bc_num_init(&res2->d.n, req); BC_SIG_UNLOCK; // Execute. bc_num_divmod(n1, n2, &res2->d.n, &res->d.n, BC_PROG_SCALE(p)); bc_program_retire(p, 2, 2); } /** * Executes modular exponentiation. * @param p The program. */ static void bc_program_modexp(BcProgram* p) { BcResult* r1; BcResult* r2; BcResult* r3; BcResult* res; BcNum* n1; BcNum* n2; BcNum* n3; #if DC_ENABLED // Check the stack. if (BC_IS_DC && BC_ERR(!BC_PROG_STACK(&p->results, 3))) { bc_err(BC_ERR_EXEC_STACK); } #endif // DC_ENABLED assert(BC_PROG_STACK(&p->results, 3)); res = bc_program_prepResult(p); // Get the first operand and typecheck. bc_program_operand(p, &r1, &n1, 3); bc_program_type_num(r1, n1); // Get the last two operands. bc_program_binOpPrep(p, &r2, &n2, &r3, &n3, 1); // Make sure that the values have their pointers updated, if necessary. // Only array elements are possible because this is dc. if (r1->t == BC_RESULT_ARRAY_ELEM && (r1->t == r2->t || r1->t == r3->t)) { n1 = bc_program_num(p, r1); } BC_SIG_LOCK; bc_num_init(&res->d.n, n3->len); BC_SIG_UNLOCK; bc_num_modexp(n1, n2, n3, &res->d.n); bc_program_retire(p, 1, 3); } /** * Asciifies a number for dc. This is a helper for bc_program_asciify(). * @param p The program. * @param n The number to asciify. */ static uchar bc_program_asciifyNum(BcProgram* p, BcNum* n) { bc_num_copy(&p->asciify, n); // We want to clear the scale and sign for easy mod later. bc_num_truncate(&p->asciify, p->asciify.scale); BC_NUM_NEG_CLR(&p->asciify); // This is guaranteed to not have a divide by 0 // because strmb is equal to 256. bc_num_mod(&p->asciify, &p->strmb, &p->asciify, 0); // This is also guaranteed to not error because num is in the range // [0, UCHAR_MAX], which is definitely in range for a BcBigDig. And // it is not negative. return (uchar) bc_num_bigdig2(&p->asciify); } /** * Executes the "asciify" command in bc and dc. * @param p The program. */ static void bc_program_asciify(BcProgram* p) { BcResult *r, res; BcNum* n; uchar c; size_t idx; #if BC_ENABLED // This is in the outer scope because it has to be freed after a jump. char* temp_str; #endif // BC_ENABLED // Check the stack. if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK); assert(BC_PROG_STACK(&p->results, 1)); // Get the top of the results stack. bc_program_operand(p, &r, &n, 0); assert(n != NULL); assert(BC_IS_BC || r->t != BC_RESULT_ARRAY); #if BC_ENABLED // Handle arrays in bc specially. if (r->t == BC_RESULT_ARRAY) { // Yes, this is one place where we need to cast the number from // bc_program_num() to a vector. BcVec* v = (BcVec*) n; size_t i; // XXX: If this is changed, you should also change the similar code in // bc_program_builtin(). // Dereference the array, if necessary. if (v->size == sizeof(uchar)) { v = bc_program_dereference(p, v); } assert(v->size == sizeof(BcNum)); // Allocate the string and set the jump for it. BC_SIG_LOCK; temp_str = bc_vm_malloc(v->len + 1); BC_SETJMP_LOCKED(vm, exit); BC_SIG_UNLOCK; // Convert the array. for (i = 0; i < v->len; ++i) { BcNum* num = (BcNum*) bc_vec_item(v, i); if (BC_PROG_STR(num)) { temp_str[i] = (bc_program_string(p, num))[0]; } else { temp_str[i] = (char) bc_program_asciifyNum(p, num); } } temp_str[v->len] = '\0'; // Store the string in the slab and map, and free the temp string. BC_SIG_LOCK; idx = bc_program_addString(p, temp_str); free(temp_str); BC_UNSETJMP(vm); BC_SIG_UNLOCK; } else #endif // BC_ENABLED { char str[2]; char* str2; // Asciify. if (BC_PROG_NUM(r, n)) c = bc_program_asciifyNum(p, n); else { // Get the string itself, then the first character. str2 = bc_program_string(p, n); c = (uchar) str2[0]; } // Fill the resulting string. str[0] = (char) c; str[1] = '\0'; // Add the string to the data structures. BC_SIG_LOCK; idx = bc_program_addString(p, str); BC_SIG_UNLOCK; } // Set the result res.t = BC_RESULT_STR; bc_num_clear(&res.d.n); res.d.n.scale = idx; // Pop and push. bc_vec_pop(&p->results); bc_vec_push(&p->results, &res); return; #if BC_ENABLED exit: free(temp_str); #endif // BC_ENABLED } /** * Streams a number or a string to stdout. * @param p The program. */ static void bc_program_printStream(BcProgram* p) { BcResult* r; BcNum* n; // Check the stack. if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK); assert(BC_PROG_STACK(&p->results, 1)); // Get the top of the results stack. bc_program_operand(p, &r, &n, 0); assert(n != NULL); // Stream appropriately. if (BC_PROG_NUM(r, n)) bc_num_stream(n); else bc_program_printChars(bc_program_string(p, n)); // Pop the operand. bc_vec_pop(&p->results); } #if DC_ENABLED /** * Gets the length of a register in dc and pushes it onto the results stack. * @param p The program. * @param code The bytecode vector to pull the register's index out of. * @param bgn An in/out parameter; the start of the index in the bytecode * vector, and will be updated to point after the index on return. */ static void bc_program_regStackLen(BcProgram* p, const char* restrict code, size_t* restrict bgn) { size_t idx = bc_program_index(code, bgn); BcVec* v = bc_program_vec(p, idx, BC_TYPE_VAR); bc_program_pushBigdig(p, (BcBigDig) v->len, BC_RESULT_TEMP); } /** * Pushes the length of the results stack onto the results stack. * @param p The program. */ static void bc_program_stackLen(BcProgram* p) { bc_program_pushBigdig(p, (BcBigDig) p->results.len, BC_RESULT_TEMP); } /** * Pops a certain number of elements off the execution stack. * @param p The program. * @param inst The instruction to tell us how many. There is one to pop up to * 2, and one to pop the amount equal to the number at the top of * the results stack. */ static void bc_program_nquit(BcProgram* p, uchar inst) { BcResult* opnd; BcNum* num; BcBigDig val; size_t i; // Ensure that the tail calls stack is correct. assert(p->stack.len == p->tail_calls.len); // Get the number of executions to pop. if (inst == BC_INST_QUIT) val = 2; else { bc_program_prep(p, &opnd, &num, 0); val = bc_num_bigdig(num); bc_vec_pop(&p->results); } // Loop over the tail call stack and adjust the quit value appropriately. for (i = 0; val && i < p->tail_calls.len; ++i) { // Get the number of tail calls for this one. size_t calls = *((size_t*) bc_vec_item_rev(&p->tail_calls, i)) + 1; // Adjust the value. if (calls >= val) val = 0; else val -= (BcBigDig) calls; } // If we don't have enough executions, just quit. if (i == p->stack.len) { vm->status = BC_STATUS_QUIT; BC_JMP; } else { // We can always pop the last item we reached on the tail call stack // because these are for tail calls. That means that any executions that // we would not have quit in that position on the stack would have quit // anyway. BC_SIG_LOCK; bc_vec_npop(&p->stack, i); bc_vec_npop(&p->tail_calls, i); BC_SIG_UNLOCK; } } /** * Pushes the depth of the execution stack onto the stack. * @param p The program. */ static void bc_program_execStackLen(BcProgram* p) { size_t i, amt, len = p->tail_calls.len; amt = len; for (i = 0; i < len; ++i) { amt += *((size_t*) bc_vec_item(&p->tail_calls, i)); } bc_program_pushBigdig(p, (BcBigDig) amt, BC_RESULT_TEMP); } /** * * @param p The program. * @param code The bytecode vector to pull the register's index out of. * @param bgn An in/out parameter; the start of the index in the bytecode * vector, and will be updated to point after the index on return. * @param cond True if the execution is conditional, false otherwise. * @param len The number of bytes in the bytecode vector. */ static void bc_program_execStr(BcProgram* p, const char* restrict code, size_t* restrict bgn, bool cond, size_t len) { BcResult* r; char* str; BcFunc* f; BcInstPtr ip; size_t fidx; BcNum* n; assert(p->stack.len == p->tail_calls.len); // Check the stack. if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK); assert(BC_PROG_STACK(&p->results, 1)); // Get the operand. bc_program_operand(p, &r, &n, 0); // If execution is conditional... if (cond) { bool exec; size_t then_idx; // These are volatile to quiet warnings on GCC about clobbering with // longjmp(). volatile size_t else_idx; volatile size_t idx; // Get the index of the "then" var and "else" var. then_idx = bc_program_index(code, bgn); else_idx = bc_program_index(code, bgn); // Figure out if we should execute. exec = (r->d.n.len != 0); idx = exec ? then_idx : else_idx; BC_SIG_LOCK; BC_SETJMP_LOCKED(vm, exit); // If we are supposed to execute, execute. If else_idx == SIZE_MAX, that // means there was no else clause, so if execute is false and else does // not exist, we don't execute. The goto skips all of the setup for the // execution. if (exec || (else_idx != SIZE_MAX)) { n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR)); } else goto exit; if (BC_ERR(!BC_PROG_STR(n))) bc_err(BC_ERR_EXEC_TYPE); BC_UNSETJMP(vm); BC_SIG_UNLOCK; } else { // In non-conditional situations, only the top of stack can be executed, // and in those cases, variables are not allowed to be "on the stack"; // they are only put on the stack to be assigned to. assert(r->t != BC_RESULT_VAR); if (r->t != BC_RESULT_STR) return; } assert(BC_PROG_STR(n)); // Get the string. str = bc_program_string(p, n); // Get the function index and function. BC_SIG_LOCK; fidx = bc_program_insertFunc(p, str); BC_SIG_UNLOCK; f = bc_vec_item(&p->fns, fidx); // If the function has not been parsed yet... if (!f->code.len) { BC_SIG_LOCK; if (!BC_PARSE_IS_INITED(&vm->read_prs, p)) { bc_parse_init(&vm->read_prs, p, fidx); // Initialize this too because bc_vm_shutdown() expects them to be // initialized togther. bc_vec_init(&vm->read_buf, sizeof(char), BC_DTOR_NONE); } // This needs to be updated because the parser could have been used // somewhere else else bc_parse_updateFunc(&vm->read_prs, fidx); bc_lex_file(&vm->read_prs.l, vm->file); BC_SETJMP_LOCKED(vm, err); BC_SIG_UNLOCK; // Parse. Only one expression is needed, so stdin isn't used. bc_parse_text(&vm->read_prs, str, BC_MODE_FILE); BC_SIG_LOCK; vm->expr(&vm->read_prs, BC_PARSE_NOCALL); BC_UNSETJMP(vm); // We can just assert this here because // dc should parse everything until EOF. assert(vm->read_prs.l.t == BC_LEX_EOF); BC_SIG_UNLOCK; } // Set the instruction pointer. ip.idx = 0; ip.len = p->results.len; ip.func = fidx; BC_SIG_LOCK; // Pop the operand. bc_vec_pop(&p->results); // Tail call processing. This condition means that there is more on the // execution stack, and we are at the end of the bytecode vector, and the // last instruction is just a BC_INST_POP_EXEC, which would return. if (p->stack.len > 1 && *bgn == len - 1 && code[*bgn] == BC_INST_POP_EXEC) { size_t* call_ptr = bc_vec_top(&p->tail_calls); // Add one to the tail call. *call_ptr += 1; // Pop the execution stack before pushing the new instruction pointer // on. bc_vec_pop(&p->stack); } // If not a tail call, just push a new one. else bc_vec_push(&p->tail_calls, &ip.idx); // Push the new function onto the execution stack and return. bc_vec_push(&p->stack, &ip); BC_SIG_UNLOCK; return; err: BC_SIG_MAYLOCK; f = bc_vec_item(&p->fns, fidx); // Make sure to erase the bytecode vector so dc knows it is not parsed. bc_vec_popAll(&f->code); exit: bc_vec_pop(&p->results); BC_LONGJMP_CONT(vm); } /** * Prints every item on the results stack, one per line. * @param p The program. */ static void bc_program_printStack(BcProgram* p) { size_t idx; for (idx = 0; idx < p->results.len; ++idx) { bc_program_print(p, BC_INST_PRINT, idx); } } #endif // DC_ENABLED /** * Pushes the value of a global onto the results stack. * @param p The program. * @param inst Which global to push, as an instruction. */ static void bc_program_pushGlobal(BcProgram* p, uchar inst) { BcResultType t; // Make sure the instruction is valid. assert(inst >= BC_INST_IBASE && inst <= BC_INST_SCALE); // Push the global. t = inst - BC_INST_IBASE + BC_RESULT_IBASE; bc_program_pushBigdig(p, p->globals[inst - BC_INST_IBASE], t); } /** * Pushes the value of a global setting onto the stack. * @param p The program. * @param inst Which global setting to push, as an instruction. */ static void bc_program_globalSetting(BcProgram* p, uchar inst) { BcBigDig val; // Make sure the instruction is valid. assert(inst >= BC_INST_LINE_LENGTH && inst <= BC_INST_LEADING_ZERO); if (inst == BC_INST_LINE_LENGTH) val = (BcBigDig) vm->line_len; #if BC_ENABLED else if (inst == BC_INST_GLOBAL_STACKS) val = (BC_G != 0); #endif // BC_ENABLED else val = (BC_Z != 0); // Push the global. bc_program_pushBigdig(p, val, BC_RESULT_TEMP); } #if BC_ENABLE_EXTRA_MATH /** * Pushes the value of seed on the stack. * @param p The program. */ static void bc_program_pushSeed(BcProgram* p) { BcResult* res; res = bc_program_prepResult(p); res->t = BC_RESULT_SEED; BC_SIG_LOCK; // We need 2*BC_RAND_NUM_SIZE because of the size of the state. bc_num_init(&res->d.n, 2 * BC_RAND_NUM_SIZE); BC_SIG_UNLOCK; bc_num_createFromRNG(&res->d.n, &p->rng); } #endif // BC_ENABLE_EXTRA_MATH /** * Adds a function to the fns array. The function's ID must have already been * inserted into the map. * @param p The program. * @param id_ptr The ID of the function as inserted into the map. */ static void bc_program_addFunc(BcProgram* p, BcId* id_ptr) { BcFunc* f; BC_SIG_ASSERT_LOCKED; // Push and init. f = bc_vec_pushEmpty(&p->fns); bc_func_init(f, id_ptr->name); } size_t bc_program_insertFunc(BcProgram* p, const char* name) { BcId* id_ptr; bool new; size_t idx; BC_SIG_ASSERT_LOCKED; assert(p != NULL && name != NULL); // Insert into the map and get the resulting ID. new = bc_map_insert(&p->fn_map, name, p->fns.len, &idx); id_ptr = (BcId*) bc_vec_item(&p->fn_map, idx); idx = id_ptr->idx; // If the function is new... if (new) { // Add the function to the fns array. bc_program_addFunc(p, id_ptr); } #if BC_ENABLED // bc has to reset the function because it's about to be redefined. else if (BC_IS_BC) { BcFunc* func = bc_vec_item(&p->fns, idx); bc_func_reset(func); } #endif // BC_ENABLED return idx; } #ifndef NDEBUG void bc_program_free(BcProgram* p) { #if BC_ENABLED size_t i; #endif // BC_ENABLED BC_SIG_ASSERT_LOCKED; assert(p != NULL); #if BC_ENABLED // Free the globals stacks. for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { bc_vec_free(p->globals_v + i); } #endif // BC_ENABLED bc_vec_free(&p->fns); bc_vec_free(&p->fn_map); bc_vec_free(&p->vars); bc_vec_free(&p->var_map); bc_vec_free(&p->arrs); bc_vec_free(&p->arr_map); bc_vec_free(&p->results); bc_vec_free(&p->stack); bc_vec_free(&p->consts); bc_vec_free(&p->const_map); bc_vec_free(&p->strs); bc_vec_free(&p->str_map); bc_num_free(&p->asciify); #if BC_ENABLED if (BC_IS_BC) bc_num_free(&p->last); #endif // BC_ENABLED #if BC_ENABLE_EXTRA_MATH bc_rand_free(&p->rng); #endif // BC_ENABLE_EXTRA_MATH #if DC_ENABLED if (BC_IS_DC) bc_vec_free(&p->tail_calls); #endif // DC_ENABLED } #endif // NDEBUG void bc_program_init(BcProgram* p) { BcInstPtr ip; size_t i; BC_SIG_ASSERT_LOCKED; assert(p != NULL); // We want this clear. // NOLINTNEXTLINE memset(&ip, 0, sizeof(BcInstPtr)); // Setup the globals stacks and the current values. for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i) { BcBigDig val = i == BC_PROG_GLOBALS_SCALE ? 0 : BC_BASE; #if BC_ENABLED bc_vec_init(p->globals_v + i, sizeof(BcBigDig), BC_DTOR_NONE); bc_vec_push(p->globals_v + i, &val); #endif // BC_ENABLED p->globals[i] = val; } #if DC_ENABLED // dc-only setup. if (BC_IS_DC) { bc_vec_init(&p->tail_calls, sizeof(size_t), BC_DTOR_NONE); // We want an item for the main function on the tail call stack. i = 0; bc_vec_push(&p->tail_calls, &i); } #endif // DC_ENABLED bc_num_setup(&p->strmb, p->strmb_num, BC_NUM_BIGDIG_LOG10); bc_num_bigdig2num(&p->strmb, BC_NUM_STREAM_BASE); bc_num_init(&p->asciify, BC_NUM_DEF_SIZE); #if BC_ENABLE_EXTRA_MATH // We need to initialize srand() just in case /dev/urandom and /dev/random // are not available. srand((unsigned int) time(NULL)); bc_rand_init(&p->rng); #endif // BC_ENABLE_EXTRA_MATH #if BC_ENABLED if (BC_IS_BC) bc_num_init(&p->last, BC_NUM_DEF_SIZE); #endif // BC_ENABLED #ifndef NDEBUG bc_vec_init(&p->fns, sizeof(BcFunc), BC_DTOR_FUNC); #else // NDEBUG bc_vec_init(&p->fns, sizeof(BcFunc), BC_DTOR_NONE); #endif // NDEBUG bc_map_init(&p->fn_map); bc_program_insertFunc(p, bc_func_main); bc_program_insertFunc(p, bc_func_read); bc_vec_init(&p->vars, sizeof(BcVec), BC_DTOR_VEC); bc_map_init(&p->var_map); bc_vec_init(&p->arrs, sizeof(BcVec), BC_DTOR_VEC); bc_map_init(&p->arr_map); bc_vec_init(&p->results, sizeof(BcResult), BC_DTOR_RESULT); // Push the first instruction pointer onto the execution stack. bc_vec_init(&p->stack, sizeof(BcInstPtr), BC_DTOR_NONE); bc_vec_push(&p->stack, &ip); bc_vec_init(&p->consts, sizeof(BcConst), BC_DTOR_CONST); bc_map_init(&p->const_map); bc_vec_init(&p->strs, sizeof(char*), BC_DTOR_NONE); bc_map_init(&p->str_map); } void bc_program_printStackTrace(BcProgram* p) { size_t i, max_digits; max_digits = bc_vm_numDigits(p->stack.len - 1); for (i = 0; i < p->stack.len; ++i) { BcInstPtr* ip = bc_vec_item_rev(&p->stack, i); BcFunc* f = bc_vec_item(&p->fns, ip->func); size_t j, digits; digits = bc_vm_numDigits(i); bc_file_puts(&vm->ferr, bc_flush_none, " "); for (j = 0; j < max_digits - digits; ++j) { bc_file_putchar(&vm->ferr, bc_flush_none, ' '); } bc_file_printf(&vm->ferr, "%zu: %s", i, f->name); #if BC_ENABLED if (BC_IS_BC && ip->func != BC_PROG_MAIN && ip->func != BC_PROG_READ) { bc_file_puts(&vm->ferr, bc_flush_none, "()"); } #endif // BC_ENABLED bc_file_putchar(&vm->ferr, bc_flush_none, '\n'); } } void bc_program_reset(BcProgram* p) { BcFunc* f; BcInstPtr* ip; BC_SIG_ASSERT_LOCKED; // Pop all but the last execution and all results. bc_vec_npop(&p->stack, p->stack.len - 1); bc_vec_popAll(&p->results); #if DC_ENABLED // We need to pop tail calls too. if (BC_IS_DC) bc_vec_npop(&p->tail_calls, p->tail_calls.len - 1); #endif // DC_ENABLED #if BC_ENABLED // Clear the globals' stacks. if (BC_G) bc_program_popGlobals(p, true); #endif // BC_ENABLED // Clear the bytecode vector of the main function. f = bc_vec_item(&p->fns, BC_PROG_MAIN); bc_vec_npop(&f->code, f->code.len); // Reset the instruction pointer. ip = bc_vec_top(&p->stack); // NOLINTNEXTLINE memset(ip, 0, sizeof(BcInstPtr)); if (BC_SIG_INTERRUPT(vm)) { // Write the ready message for a signal. bc_file_printf(&vm->fout, "%s", bc_program_ready_msg); bc_file_flush(&vm->fout, bc_flush_err); } // Clear the signal. vm->sig = 0; } void bc_program_exec(BcProgram* p) { size_t idx; BcResult r; BcResult* ptr; BcInstPtr* ip; BcFunc* func; char* code; bool cond = false; uchar inst; #if BC_ENABLED BcNum* num; #endif // BC_ENABLED #if !BC_HAS_COMPUTED_GOTO #ifndef NDEBUG size_t jmp_bufs_len; #endif // NDEBUG #endif // !BC_HAS_COMPUTED_GOTO #if BC_HAS_COMPUTED_GOTO #if BC_GCC #pragma GCC diagnostic ignored "-Wpedantic" #endif // BC_GCC #if BC_CLANG #pragma clang diagnostic ignored "-Wgnu-label-as-value" #endif // BC_CLANG BC_PROG_LBLS; BC_PROG_LBLS_ASSERT; #if BC_CLANG #pragma clang diagnostic warning "-Wgnu-label-as-value" #endif // BC_CLANG #if BC_GCC #pragma GCC diagnostic warning "-Wpedantic" #endif // BC_GCC // BC_INST_INVALID is a marker for the end so that we don't have to have an // execution loop. func = (BcFunc*) bc_vec_item(&p->fns, BC_PROG_MAIN); bc_vec_pushByte(&func->code, BC_INST_INVALID); #endif // BC_HAS_COMPUTED_GOTO BC_SETJMP(vm, end); ip = bc_vec_top(&p->stack); func = (BcFunc*) bc_vec_item(&p->fns, ip->func); code = func->code.v; #if !BC_HAS_COMPUTED_GOTO #ifndef NDEBUG jmp_bufs_len = vm->jmp_bufs.len; #endif // NDEBUG // This loop is the heart of the execution engine. It *is* the engine. For // computed goto, it is ignored. while (ip->idx < func->code.len) #endif // !BC_HAS_COMPUTED_GOTO { BC_SIG_ASSERT_NOT_LOCKED; #if BC_HAS_COMPUTED_GOTO #if BC_GCC #pragma GCC diagnostic ignored "-Wpedantic" #endif // BC_GCC #if BC_CLANG #pragma clang diagnostic ignored "-Wgnu-label-as-value" #endif // BC_CLANG BC_PROG_JUMP(inst, code, ip); #else // BC_HAS_COMPUTED_GOTO // Get the next instruction and increment the index. inst = (uchar) code[(ip->idx)++]; #endif // BC_HAS_COMPUTED_GOTO #if BC_DEBUG_CODE bc_file_printf(&vm->ferr, "inst: %s\n", bc_inst_names[inst]); bc_file_flush(&vm->ferr, bc_flush_none); #endif // BC_DEBUG_CODE #if !BC_HAS_COMPUTED_GOTO switch (inst) #endif // !BC_HAS_COMPUTED_GOTO { #if BC_ENABLED // This just sets up the condition for the unconditional jump below, // which checks the condition, if necessary. // clang-format off BC_PROG_LBL(BC_INST_JUMP_ZERO): // clang-format on { bc_program_prep(p, &ptr, &num, 0); cond = !bc_num_cmpZero(num); bc_vec_pop(&p->results); BC_PROG_DIRECT_JUMP(BC_INST_JUMP) } // Fallthrough. BC_PROG_FALLTHROUGH // clang-format off BC_PROG_LBL(BC_INST_JUMP): // clang-format on { idx = bc_program_index(code, &ip->idx); // If a jump is required... if (inst == BC_INST_JUMP || cond) { // Get the address to jump to. size_t* addr = bc_vec_item(&func->labels, idx); // If this fails, then the parser failed to set up the // labels correctly. assert(*addr != SIZE_MAX); // Set the new address. ip->idx = *addr; } BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_CALL): // clang-format on { assert(BC_IS_BC); bc_program_call(p, code, &ip->idx); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_INC): BC_PROG_LBL(BC_INST_DEC): // clang-format on { bc_program_incdec(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_HALT): // clang-format on { vm->status = BC_STATUS_QUIT; // Just jump out. The jump series will take care of everything. BC_JMP; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_RET): BC_PROG_LBL(BC_INST_RET0): BC_PROG_LBL(BC_INST_RET_VOID): // clang-format on { bc_program_return(p, inst); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } #endif // BC_ENABLED // clang-format off BC_PROG_LBL(BC_INST_BOOL_OR): BC_PROG_LBL(BC_INST_BOOL_AND): BC_PROG_LBL(BC_INST_REL_EQ): BC_PROG_LBL(BC_INST_REL_LE): BC_PROG_LBL(BC_INST_REL_GE): BC_PROG_LBL(BC_INST_REL_NE): BC_PROG_LBL(BC_INST_REL_LT): BC_PROG_LBL(BC_INST_REL_GT): // clang-format on { bc_program_logical(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_READ): // clang-format on { // We want to flush output before // this in case there is a prompt. bc_file_flush(&vm->fout, bc_flush_save); bc_program_read(p); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } #if BC_ENABLE_EXTRA_MATH // clang-format off BC_PROG_LBL(BC_INST_RAND): // clang-format on { bc_program_rand(p); BC_PROG_JUMP(inst, code, ip); } #endif // BC_ENABLE_EXTRA_MATH // clang-format off BC_PROG_LBL(BC_INST_MAXIBASE): BC_PROG_LBL(BC_INST_MAXOBASE): BC_PROG_LBL(BC_INST_MAXSCALE): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_MAXRAND): #endif // BC_ENABLE_EXTRA_MATH // clang-format on { BcBigDig dig = vm->maxes[inst - BC_INST_MAXIBASE]; bc_program_pushBigdig(p, dig, BC_RESULT_TEMP); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_LINE_LENGTH): #if BC_ENABLED BC_PROG_LBL(BC_INST_GLOBAL_STACKS): #endif // BC_ENABLED BC_PROG_LBL(BC_INST_LEADING_ZERO): // clang-format on { bc_program_globalSetting(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_VAR): // clang-format on { bc_program_pushVar(p, code, &ip->idx, false, false); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_ARRAY_ELEM): BC_PROG_LBL(BC_INST_ARRAY): // clang-format on { bc_program_pushArray(p, code, &ip->idx, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_IBASE): BC_PROG_LBL(BC_INST_SCALE): BC_PROG_LBL(BC_INST_OBASE): // clang-format on { bc_program_pushGlobal(p, inst); BC_PROG_JUMP(inst, code, ip); } #if BC_ENABLE_EXTRA_MATH // clang-format off BC_PROG_LBL(BC_INST_SEED): // clang-format on { bc_program_pushSeed(p); BC_PROG_JUMP(inst, code, ip); } #endif // BC_ENABLE_EXTRA_MATH // clang-format off BC_PROG_LBL(BC_INST_LENGTH): BC_PROG_LBL(BC_INST_SCALE_FUNC): BC_PROG_LBL(BC_INST_SQRT): BC_PROG_LBL(BC_INST_ABS): BC_PROG_LBL(BC_INST_IS_NUMBER): BC_PROG_LBL(BC_INST_IS_STRING): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_IRAND): #endif // BC_ENABLE_EXTRA_MATH // clang-format on { bc_program_builtin(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_ASCIIFY): // clang-format on { bc_program_asciify(p); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_NUM): // clang-format on { bc_program_const(p, code, &ip->idx); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_ZERO): BC_PROG_LBL(BC_INST_ONE): #if BC_ENABLED BC_PROG_LBL(BC_INST_LAST): #endif // BC_ENABLED // clang-format on { r.t = BC_RESULT_ZERO + (inst - BC_INST_ZERO); bc_vec_push(&p->results, &r); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_PRINT): BC_PROG_LBL(BC_INST_PRINT_POP): #if BC_ENABLED BC_PROG_LBL(BC_INST_PRINT_STR): #endif // BC_ENABLED // clang-format on { bc_program_print(p, inst, 0); // We want to flush right away to save the output for history, // if history must preserve it when taking input. bc_file_flush(&vm->fout, bc_flush_save); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_STR): // clang-format on { // Set up the result and push. r.t = BC_RESULT_STR; bc_num_clear(&r.d.n); r.d.n.scale = bc_program_index(code, &ip->idx); bc_vec_push(&p->results, &r); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_POWER): BC_PROG_LBL(BC_INST_MULTIPLY): BC_PROG_LBL(BC_INST_DIVIDE): BC_PROG_LBL(BC_INST_MODULUS): BC_PROG_LBL(BC_INST_PLUS): BC_PROG_LBL(BC_INST_MINUS): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_PLACES): BC_PROG_LBL(BC_INST_LSHIFT): BC_PROG_LBL(BC_INST_RSHIFT): #endif // BC_ENABLE_EXTRA_MATH // clang-format on { bc_program_op(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_NEG): BC_PROG_LBL(BC_INST_BOOL_NOT): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_TRUNC): #endif // BC_ENABLE_EXTRA_MATH // clang-format on { bc_program_unary(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off #if BC_ENABLED BC_PROG_LBL(BC_INST_ASSIGN_POWER): BC_PROG_LBL(BC_INST_ASSIGN_MULTIPLY): BC_PROG_LBL(BC_INST_ASSIGN_DIVIDE): BC_PROG_LBL(BC_INST_ASSIGN_MODULUS): BC_PROG_LBL(BC_INST_ASSIGN_PLUS): BC_PROG_LBL(BC_INST_ASSIGN_MINUS): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_ASSIGN_PLACES): BC_PROG_LBL(BC_INST_ASSIGN_LSHIFT): BC_PROG_LBL(BC_INST_ASSIGN_RSHIFT): #endif // BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_ASSIGN): BC_PROG_LBL(BC_INST_ASSIGN_POWER_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_MULTIPLY_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_DIVIDE_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_MODULUS_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_PLUS_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_MINUS_NO_VAL): #if BC_ENABLE_EXTRA_MATH BC_PROG_LBL(BC_INST_ASSIGN_PLACES_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_LSHIFT_NO_VAL): BC_PROG_LBL(BC_INST_ASSIGN_RSHIFT_NO_VAL): #endif // BC_ENABLE_EXTRA_MATH #endif // BC_ENABLED BC_PROG_LBL(BC_INST_ASSIGN_NO_VAL): // clang-format on { bc_program_assign(p, inst); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_POP): // clang-format on { #ifndef BC_PROG_NO_STACK_CHECK // dc must do a stack check, but bc does not. if (BC_IS_DC) { if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) { bc_err(BC_ERR_EXEC_STACK); } } #endif // BC_PROG_NO_STACK_CHECK assert(BC_PROG_STACK(&p->results, 1)); bc_vec_pop(&p->results); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_SWAP): // clang-format on { BcResult* ptr2; // Check the stack. if (BC_ERR(!BC_PROG_STACK(&p->results, 2))) { bc_err(BC_ERR_EXEC_STACK); } assert(BC_PROG_STACK(&p->results, 2)); // Get the two items. ptr = bc_vec_item_rev(&p->results, 0); ptr2 = bc_vec_item_rev(&p->results, 1); // Swap. It's just easiest to do it this way. // NOLINTNEXTLINE memcpy(&r, ptr, sizeof(BcResult)); // NOLINTNEXTLINE memcpy(ptr, ptr2, sizeof(BcResult)); // NOLINTNEXTLINE memcpy(ptr2, &r, sizeof(BcResult)); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_MODEXP): // clang-format on { bc_program_modexp(p); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_DIVMOD): // clang-format on { bc_program_divmod(p); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_PRINT_STREAM): // clang-format on { bc_program_printStream(p); BC_PROG_JUMP(inst, code, ip); } #if DC_ENABLED // clang-format off BC_PROG_LBL(BC_INST_POP_EXEC): // clang-format on { // If this fails, the dc parser got something wrong. assert(BC_PROG_STACK(&p->stack, 2)); // Pop the execution stack and tail call stack. bc_vec_pop(&p->stack); bc_vec_pop(&p->tail_calls); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_EXECUTE): BC_PROG_LBL(BC_INST_EXEC_COND): // clang-format on { cond = (inst == BC_INST_EXEC_COND); bc_program_execStr(p, code, &ip->idx, cond, func->code.len); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_PRINT_STACK): // clang-format on { bc_program_printStack(p); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_CLEAR_STACK): // clang-format on { bc_vec_popAll(&p->results); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_REG_STACK_LEN): // clang-format on { bc_program_regStackLen(p, code, &ip->idx); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_STACK_LEN): // clang-format on { bc_program_stackLen(p); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_DUPLICATE): // clang-format on { // Check the stack. if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) { bc_err(BC_ERR_EXEC_STACK); } assert(BC_PROG_STACK(&p->results, 1)); // Get the top of the stack. ptr = bc_vec_top(&p->results); BC_SIG_LOCK; // Copy and push. bc_result_copy(&r, ptr); bc_vec_push(&p->results, &r); BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_LOAD): BC_PROG_LBL(BC_INST_PUSH_VAR): // clang-format on { bool copy = (inst == BC_INST_LOAD); bc_program_pushVar(p, code, &ip->idx, true, copy); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_PUSH_TO_VAR): // clang-format on { idx = bc_program_index(code, &ip->idx); - bc_program_copyToVar(p, idx, BC_TYPE_VAR, true); + bc_program_copyToVar(p, idx, BC_TYPE_VAR); BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_QUIT): BC_PROG_LBL(BC_INST_NQUIT): // clang-format on { bc_program_nquit(p, inst); // Because we changed the execution stack and where we are // executing, we have to update all of this. BC_SIG_LOCK; ip = bc_vec_top(&p->stack); func = bc_vec_item(&p->fns, ip->func); code = func->code.v; BC_SIG_UNLOCK; BC_PROG_JUMP(inst, code, ip); } // clang-format off BC_PROG_LBL(BC_INST_EXEC_STACK_LEN): // clang-format on { bc_program_execStackLen(p); BC_PROG_JUMP(inst, code, ip); } #endif // DC_ENABLED #if BC_HAS_COMPUTED_GOTO // clang-format off BC_PROG_LBL(BC_INST_INVALID): // clang-format on { goto end; } #else // BC_HAS_COMPUTED_GOTO default: { BC_UNREACHABLE #if !defined(NDEBUG) && !BC_CLANG abort(); #endif // !defined(NDEBUG) && !BC_CLANG } #endif // BC_HAS_COMPUTED_GOTO } #if BC_HAS_COMPUTED_GOTO #if BC_CLANG #pragma clang diagnostic warning "-Wgnu-label-as-value" #endif // BC_CLANG #if BC_GCC #pragma GCC diagnostic warning "-Wpedantic" #endif // BC_GCC #else // BC_HAS_COMPUTED_GOTO #ifndef NDEBUG // This is to allow me to use a debugger to see the last instruction, // which will point to which function was the problem. But it's also a // good smoke test for error handling changes. assert(jmp_bufs_len == vm->jmp_bufs.len); #endif // NDEBUG #endif // BC_HAS_COMPUTED_GOTO } end: BC_SIG_MAYLOCK; // This is here just to print a stack trace on interrupts. This is for // finding infinite loops. if (BC_SIG_INTERRUPT(vm)) { BcStatus s; bc_file_putchar(&vm->ferr, bc_flush_none, '\n'); bc_program_printStackTrace(p); s = bc_file_flushErr(&vm->ferr, bc_flush_err); if (BC_ERR(s != BC_STATUS_SUCCESS && vm->status == BC_STATUS_SUCCESS)) { vm->status = (sig_atomic_t) s; } } BC_LONGJMP_CONT(vm); } #if BC_DEBUG_CODE #if BC_ENABLED && DC_ENABLED void bc_program_printStackDebug(BcProgram* p) { bc_file_puts(&vm->fout, bc_flush_err, "-------------- Stack ----------\n"); bc_program_printStack(p); bc_file_puts(&vm->fout, bc_flush_err, "-------------- Stack End ------\n"); } static void bc_program_printIndex(const char* restrict code, size_t* restrict bgn) { uchar byte, i, bytes = (uchar) code[(*bgn)++]; ulong val = 0; for (byte = 1, i = 0; byte && i < bytes; ++i) { byte = (uchar) code[(*bgn)++]; if (byte) val |= ((ulong) byte) << (CHAR_BIT * i); } bc_vm_printf(" (%lu) ", val); } static void bc_program_printStr(const BcProgram* p, const char* restrict code, size_t* restrict bgn) { size_t idx = bc_program_index(code, bgn); char* s; s = *((char**) bc_vec_item(p->strs, idx)); bc_vm_printf(" (\"%s\") ", s); } void bc_program_printInst(const BcProgram* p, const char* restrict code, size_t* restrict bgn) { uchar inst = (uchar) code[(*bgn)++]; bc_vm_printf("Inst[%zu]: %s [%lu]; ", *bgn - 1, bc_inst_names[inst], (unsigned long) inst); if (inst == BC_INST_VAR || inst == BC_INST_ARRAY_ELEM || inst == BC_INST_ARRAY) { bc_program_printIndex(code, bgn); } else if (inst == BC_INST_STR) bc_program_printStr(p, code, bgn); else if (inst == BC_INST_NUM) { size_t idx = bc_program_index(code, bgn); BcConst* c = bc_vec_item(p->consts, idx); bc_vm_printf("(%s)", c->val); } else if (inst == BC_INST_CALL || (inst > BC_INST_STR && inst <= BC_INST_JUMP_ZERO)) { bc_program_printIndex(code, bgn); if (inst == BC_INST_CALL) bc_program_printIndex(code, bgn); } bc_vm_putchar('\n', bc_flush_err); } void bc_program_code(const BcProgram* p) { BcFunc* f; char* code; BcInstPtr ip; size_t i; for (i = 0; i < p->fns.len; ++i) { ip.idx = ip.len = 0; ip.func = i; f = bc_vec_item(&p->fns, ip.func); code = f->code.v; bc_vm_printf("func[%zu]:\n", ip.func); while (ip.idx < f->code.len) { bc_program_printInst(p, code, &ip.idx); } bc_file_puts(&vm->fout, bc_flush_err, "\n\n"); } } #endif // BC_ENABLED && DC_ENABLED #endif // BC_DEBUG_CODE diff --git a/tests/bc/scripts/all.txt b/tests/bc/scripts/all.txt index b4a178783796..e2d2aa320c6f 100644 --- a/tests/bc/scripts/all.txt +++ b/tests/bc/scripts/all.txt @@ -1,19 +1,20 @@ multiply.bc divide.bc subtract.bc add.bc print.bc parse.bc array.bc +array2.bc atan.bc bessel.bc functions.bc globals.bc len.bc rand.bc references.bc screen.bc strings2.bc ifs.bc ifs2.bc afl1.bc diff --git a/tests/bc/scripts/array2.bc b/tests/bc/scripts/array2.bc new file mode 100644 index 000000000000..34d88c3e276b --- /dev/null +++ b/tests/bc/scripts/array2.bc @@ -0,0 +1,20 @@ +#! /usr/bin/bc -q + +define z(x, a[]) { + return x + a[1] +} + +define y(x, *b[]) { + return x + b[1] +} + +a[0] = 5 +a[1] = 6 + +b[0] = 8 +b[1] = 7 + +z(a[0], b[]) +y(b[0], a[]) + +halt diff --git a/tests/bc/scripts/array2.txt b/tests/bc/scripts/array2.txt new file mode 100644 index 000000000000..76dcb035f907 --- /dev/null +++ b/tests/bc/scripts/array2.txt @@ -0,0 +1,2 @@ +12 +14