diff options
Diffstat (limited to 'contrib/bc/include/program.h')
-rw-r--r-- | contrib/bc/include/program.h | 314 |
1 files changed, 149 insertions, 165 deletions
diff --git a/contrib/bc/include/program.h b/contrib/bc/include/program.h index 1a87aa612c90..1df753afad22 100644 --- a/contrib/bc/include/program.h +++ b/contrib/bc/include/program.h @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2018-2021 Gavin D. Howard and contributors. + * 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: @@ -69,8 +69,10 @@ 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 @@ -85,11 +87,21 @@ typedef struct BcProgram /// The execution stack. BcVec stack; - /// A pointer to the current function's constants. - BcVec* consts; + /// 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; - /// A pointer to the current function's strings. - BcVec* strs; + /// 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; @@ -122,6 +134,10 @@ typedef struct BcProgram /// 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. @@ -204,25 +220,36 @@ typedef struct BcProgram #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(p, name, t) +/// Returns true if the calculator should pop after printing. +#define BC_PROGRAM_POP(pop) (pop) + +#else // !BC_ENABLED + +/// 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. +// 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(p, code, bgn) + 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 +#ifdef BC_DEBUG #define BC_PROG_NO_STACK_CHECK -#endif // NDEBUG +#endif // BC_DEBUG #endif // DC_ENABLED @@ -271,7 +298,7 @@ typedef void (*BcProgramUnary)(BcResult* r, BcNum* n); void bc_program_init(BcProgram* p); -#ifndef NDEBUG +#if BC_DEBUG /** * Frees a BcProgram. This is only used in debug builds because a BcProgram is @@ -282,7 +309,14 @@ bc_program_init(BcProgram* p); void bc_program_free(BcProgram* p); -#endif // NDEBUG +#endif // BC_DEBUG + +/** + * 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 @@ -317,22 +351,22 @@ bc_program_printStackDebug(BcProgram* p); /** * Returns the index of the variable or array in their respective arrays. - * @param p The program. - * @param id The BcId 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. + * @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* id, bool var); +bc_program_search(BcProgram* p, const char* name, bool var); /** - * Adds a string to a function and returns the string's index in the function. - * @param p The program. - * @param str The string to add. - * @param fidx The index of the function to add to. + * 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, size_t fidx); +bc_program_addString(BcProgram* p, const char* str); /** * Inserts a function into the program and returns the index of the function in @@ -438,14 +472,14 @@ extern const char bc_program_esc_seqs[]; #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]; \ - } \ +#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 @@ -545,6 +579,8 @@ extern const char bc_program_esc_seqs[]; &&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, \ @@ -572,6 +608,7 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ + &&lbl_BC_INST_EXTENDED_REGISTERS, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ @@ -639,6 +676,8 @@ extern const char bc_program_esc_seqs[]; &&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, \ @@ -663,6 +702,7 @@ extern const char bc_program_esc_seqs[]; &&lbl_BC_INST_MODEXP, \ &&lbl_BC_INST_DIVMOD, \ &&lbl_BC_INST_PRINT_STREAM, \ + &&lbl_BC_INST_EXTENDED_REGISTERS, \ &&lbl_BC_INST_POP_EXEC, \ &&lbl_BC_INST_EXECUTE, \ &&lbl_BC_INST_EXEC_COND, \ @@ -745,6 +785,8 @@ extern const char bc_program_esc_seqs[]; &&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, \ @@ -825,6 +867,8 @@ extern const char bc_program_esc_seqs[]; &&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, \ @@ -860,142 +904,82 @@ extern const char bc_program_esc_seqs[]; #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_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, \ +#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_EXTENDED_REGISTERS, \ + &&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_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, \ +#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_EXTENDED_REGISTERS, \ + &&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 |