Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - language - init.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23022-5b494f78e) Lines: 832 1193 69.7 %
Date: 2018-09-24 05:37:44 Functions: 101 135 74.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*        INITIALIZING THE SYSTEM, ERRORS, STACK MANAGEMENT        */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : /* _GNU_SOURCE is needed before first include to get RUSAGE_THREAD */
      20             : #undef _GNU_SOURCE /* avoid warning */
      21             : #define _GNU_SOURCE
      22             : #include <string.h>
      23             : #if defined(_WIN32) || defined(__CYGWIN32__)
      24             : #  include "../systems/mingw/mingw.h"
      25             : #  include <process.h>
      26             : #endif
      27             : #include "paricfg.h"
      28             : #if defined(STACK_CHECK) && !defined(__EMX__)
      29             : #  include <sys/types.h>
      30             : #  include <sys/time.h>
      31             : #  include <sys/resource.h>
      32             : #endif
      33             : #if defined(HAS_WAITPID) && defined(HAS_SETSID)
      34             : #  include <sys/wait.h>
      35             : #endif
      36             : #ifdef HAS_MMAP
      37             : #  include <sys/mman.h>
      38             : #endif
      39             : #if defined(USE_GETTIMEOFDAY) || defined(USE_GETRUSAGE) || defined(USE_TIMES)
      40             : #  include <sys/time.h>
      41             : #endif
      42             : #if defined(USE_GETRUSAGE)
      43             : #  include <sys/resource.h>
      44             : #endif
      45             : #if defined(USE_FTIME) || defined(USE_FTIMEFORWALLTIME)
      46             : #  include <sys/timeb.h>
      47             : #endif
      48             : #if defined(USE_CLOCK_GETTIME) || defined(USE_TIMES)
      49             : #  include <time.h>
      50             : #endif
      51             : #if defined(USE_TIMES)
      52             : #  include <sys/times.h>
      53             : #endif
      54             : #include "pari.h"
      55             : #include "paripriv.h"
      56             : #include "anal.h"
      57             : 
      58             : const double LOG10_2 = 0.3010299956639812; /* log_10(2) */
      59             : const double LOG2_10 = 3.321928094887362;  /* log_2(10) */
      60             : 
      61             : GEN gnil, gen_0, gen_1, gen_m1, gen_2, gen_m2, ghalf, err_e_STACK;
      62             : 
      63             : static const ulong readonly_constants[] = {
      64             :   evaltyp(t_INT) | _evallg(2),  /* gen_0 */
      65             :   evallgefint(2),
      66             :   evaltyp(t_INT) | _evallg(2),  /* gnil */
      67             :   evallgefint(2),
      68             :   evaltyp(t_INT) | _evallg(3),  /* gen_1 */
      69             :   evalsigne(1) | evallgefint(3),
      70             :   1,
      71             :   evaltyp(t_INT) | _evallg(3),  /* gen_2 */
      72             :   evalsigne(1) | evallgefint(3),
      73             :   2,
      74             :   evaltyp(t_INT) | _evallg(3),  /* gen_m1 */
      75             :   evalsigne(-1) | evallgefint(3),
      76             :   1,
      77             :   evaltyp(t_INT) | _evallg(3),  /* gen_m2 */
      78             :   evalsigne(-1) | evallgefint(3),
      79             :   2,
      80             :   evaltyp(t_ERROR) | _evallg(2), /* err_e_STACK */
      81             :   e_STACK,
      82             :   evaltyp(t_FRAC) | _evallg(3), /* ghalf */
      83             :   (ulong)(readonly_constants+4),
      84             :   (ulong)(readonly_constants+7)
      85             : };
      86             : THREAD GEN bernzone, primetab;
      87             : byteptr diffptr;
      88             : FILE    *pari_outfile, *pari_errfile, *pari_logfile, *pari_infile;
      89             : char    *current_logfile, *current_psfile, *pari_datadir;
      90             : long    gp_colors[c_LAST];
      91             : int     disable_color;
      92             : ulong   DEBUGFILES, DEBUGLEVEL, DEBUGMEM;
      93             : long    DEBUGVAR;
      94             : ulong   pari_mt_nbthreads;
      95             : long    precreal;
      96             : ulong   precdl, logstyle;
      97             : gp_data *GP_DATA;
      98             : 
      99             : entree  **varentries;
     100             : THREAD long *varpriority;
     101             : 
     102             : THREAD pari_sp avma;
     103             : THREAD struct pari_mainstack *pari_mainstack;
     104             : 
     105             : static void ** MODULES;
     106             : static pari_stack s_MODULES;
     107             : const long functions_tblsz = 135; /* size of functions_hash */
     108             : entree **functions_hash, **defaults_hash;
     109             : 
     110             : char *(*cb_pari_fgets_interactive)(char *s, int n, FILE *f);
     111             : int (*cb_pari_get_line_interactive)(const char*, const char*, filtre_t *F);
     112             : void (*cb_pari_quit)(long);
     113             : void (*cb_pari_init_histfile)(void);
     114             : void (*cb_pari_ask_confirm)(const char *);
     115             : int  (*cb_pari_handle_exception)(long);
     116             : int  (*cb_pari_err_handle)(GEN);
     117             : int  (*cb_pari_whatnow)(PariOUT *out, const char *, int);
     118             : void (*cb_pari_sigint)(void);
     119             : void (*cb_pari_pre_recover)(long);
     120             : void (*cb_pari_err_recover)(long);
     121             : int (*cb_pari_break_loop)(int);
     122             : int (*cb_pari_is_interactive)(void);
     123             : void (*cb_pari_start_output)();
     124             : 
     125             : const char * pari_library_path = NULL;
     126             : 
     127             : static THREAD GEN global_err_data;
     128             : THREAD jmp_buf *iferr_env;
     129             : const long CATCH_ALL = -1;
     130             : 
     131             : static void pari_init_timer(void);
     132             : 
     133             : /*********************************************************************/
     134             : /*                                                                   */
     135             : /*                       BLOCKS & CLONES                             */
     136             : /*                                                                   */
     137             : /*********************************************************************/
     138             : /*#define DEBUG*/
     139             : static THREAD long next_block;
     140             : static THREAD GEN cur_block; /* current block in block list */
     141             : #ifdef DEBUG
     142             : static THREAD long NUM;
     143             : #endif
     144             : 
     145             : static void
     146      113259 : pari_init_blocks(void)
     147             : {
     148      113259 :   next_block = 0; cur_block = NULL;
     149             : #ifdef DEBUG
     150             :   NUM = 0;
     151             : #endif
     152      113259 : }
     153             : 
     154             : static void
     155      111651 : pari_close_blocks(void)
     156             : {
     157      111651 :   while (cur_block) killblock(cur_block);
     158      113091 : }
     159             : 
     160             : /* Return x, where:
     161             :  * x[-4]: reference count
     162             :  * x[-3]: adress of next block
     163             :  * x[-2]: adress of preceding block.
     164             :  * x[-1]: number of allocated blocs.
     165             :  * x[0..n-1]: malloc-ed memory. */
     166             : GEN
     167   212369804 : newblock(size_t n)
     168             : {
     169   212369804 :   long *x = (long *) pari_malloc((n + BL_HEAD)*sizeof(long)) + BL_HEAD;
     170             : 
     171   212371199 :   bl_size(x) = n;
     172   212371199 :   bl_refc(x) = 1;
     173   212371199 :   bl_next(x) = NULL;
     174   212371199 :   bl_prev(x) = cur_block;
     175   212371199 :   bl_num(x)  = next_block++;
     176   212371199 :   if (cur_block) bl_next(cur_block) = x;
     177             : #ifdef DEBUG
     178             :   err_printf("+ %ld\n", ++NUM);
     179             : #endif
     180   212371199 :   if (DEBUGMEM > 2)
     181           0 :     err_printf("new block, size %6lu (no %ld): %08lx\n", n, next_block-1, x);
     182   212370987 :   return cur_block = x;
     183             : }
     184             : 
     185             : GEN
     186       50857 : gcloneref(GEN x)
     187             : {
     188       50857 :   if (isclone(x)) { ++bl_refc(x); return x; }
     189       50542 :   else return gclone(x);
     190             : }
     191             : 
     192             : void
     193           0 : gclone_refc(GEN x) { ++bl_refc(x); }
     194             : 
     195             : void
     196   246764081 : gunclone(GEN x)
     197             : {
     198   246764081 :   if (--bl_refc(x) > 0) return;
     199   212366606 :   BLOCK_SIGINT_START;
     200   212369410 :   if (bl_next(x)) bl_prev(bl_next(x)) = bl_prev(x);
     201             :   else
     202             :   {
     203    25876395 :     cur_block = bl_prev(x);
     204    25876395 :     next_block = bl_num(x);
     205             :   }
     206   212369410 :   if (bl_prev(x)) bl_next(bl_prev(x)) = bl_next(x);
     207   212369410 :   if (DEBUGMEM > 2)
     208           0 :     err_printf("killing block (no %ld): %08lx\n", bl_num(x), x);
     209   212370029 :   free((void*)bl_base(x)); /* pari_free not needed: we already block */
     210   212370029 :   BLOCK_SIGINT_END;
     211             : #ifdef DEBUG
     212             :   err_printf("- %ld\n", NUM--);
     213             : #endif
     214             : }
     215             : 
     216             : /* Recursively look for clones in the container and kill them. Then kill
     217             :  * container if clone. SIGINT could be blocked until it returns */
     218             : void
     219  2932170340 : gunclone_deep(GEN x)
     220             : {
     221             :   long i, lx;
     222             :   GEN v;
     223  2932170340 :   if (isclone(x) && bl_refc(x) > 1) { --bl_refc(x); return; }
     224  2878112623 :   BLOCK_SIGINT_START;
     225  2878112624 :   switch(typ(x))
     226             :   {
     227             :     case t_VEC: case t_COL: case t_MAT:
     228   113568027 :       lx = lg(x);
     229   113568027 :       for (i=1;i<lx;i++) gunclone_deep(gel(x,i));
     230   113568027 :       break;
     231             :     case t_LIST:
     232         277 :       v = list_data(x); lx = v? lg(v): 1;
     233         277 :       for (i=1;i<lx;i++) gunclone_deep(gel(v,i));
     234         277 :       if (v) killblock(v);
     235         277 :       break;
     236             :   }
     237  2878112624 :   if (isclone(x)) gunclone(x);
     238  2878112624 :   BLOCK_SIGINT_END;
     239             : }
     240             : 
     241             : int
     242      171380 : pop_entree_block(entree *ep, long loc)
     243             : {
     244      171380 :   GEN x = (GEN)ep->value;
     245      171380 :   if (bl_num(x) < loc) return 0; /* older */
     246         210 :   if (DEBUGMEM>2)
     247           0 :     err_printf("popping %s (block no %ld)\n", ep->name, bl_num(x));
     248         210 :   gunclone_deep(x); return 1;
     249             : }
     250             : 
     251             : /*********************************************************************/
     252             : /*                                                                   */
     253             : /*                       C STACK SIZE CONTROL                        */
     254             : /*                                                                   */
     255             : /*********************************************************************/
     256             : /* Avoid core dump on deep recursion. Adapted Perl code by Dominic Dunlop */
     257             : THREAD void *PARI_stack_limit = NULL;
     258             : 
     259             : #ifdef STACK_CHECK
     260             : 
     261             : #  ifdef __EMX__                                /* Emulate */
     262             : void
     263             : pari_stackcheck_init(void *pari_stack_base)
     264             : {
     265             :   (void) pari_stack_base;
     266             :   if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
     267             :   PARI_stack_limit = get_stack(1./16, 32*1024);
     268             : }
     269             : #  else /* !__EMX__ */
     270             : /* Set PARI_stack_limit to (a little above) the lowest safe address that can be
     271             :  * used on the stack. Leave PARI_stack_limit at its initial value (NULL) to
     272             :  * show no check should be made [init failed]. Assume stack grows downward. */
     273             : void
     274        1552 : pari_stackcheck_init(void *pari_stack_base)
     275             : {
     276             :   struct rlimit rip;
     277             :   ulong size;
     278        1552 :   if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
     279        1552 :   if (getrlimit(RLIMIT_STACK, &rip)) return;
     280        1552 :   size = rip.rlim_cur;
     281        1552 :   if (size == (ulong)RLIM_INFINITY || size > (ulong)pari_stack_base)
     282           0 :     PARI_stack_limit = (void*)(((ulong)pari_stack_base) / 16);
     283             :   else
     284        1552 :     PARI_stack_limit = (void*)((ulong)pari_stack_base - (size/16)*15);
     285             : }
     286             : #  endif /* !__EMX__ */
     287             : 
     288             : #else
     289             : void
     290             : pari_stackcheck_init(void *pari_stack_base)
     291             : {
     292             :   (void) pari_stack_base; PARI_stack_limit = NULL;
     293             : }
     294             : #endif /* STACK_CHECK */
     295             : 
     296             : /*******************************************************************/
     297             : /*                         HEAP TRAVERSAL                          */
     298             : /*******************************************************************/
     299             : struct getheap_t { long n, l; };
     300             : /* x is a block, not necessarily a clone [x[0] may not be set] */
     301             : static void
     302        6762 : f_getheap(GEN x, void *D)
     303             : {
     304        6762 :   struct getheap_t *T = (struct getheap_t*)D;
     305        6762 :   T->n++;
     306        6762 :   T->l += bl_size(x) + BL_HEAD;
     307        6762 : }
     308             : GEN
     309          84 : getheap(void)
     310             : {
     311          84 :   struct getheap_t T = { 0, 0 };
     312          84 :   traverseheap(&f_getheap, &T); return mkvec2s(T.n, T.l);
     313             : }
     314             : 
     315             : void
     316          84 : traverseheap( void(*f)(GEN, void *), void *data )
     317             : {
     318             :   GEN x;
     319          84 :   for (x = cur_block; x; x = bl_prev(x)) f(x, data);
     320          84 : }
     321             : 
     322             : /*********************************************************************/
     323             : /*                          DAEMON / FORK                            */
     324             : /*********************************************************************/
     325             : #if defined(HAS_WAITPID) && defined(HAS_SETSID)
     326             : /* Properly fork a process, detaching from main process group without creating
     327             :  * zombies on exit. Parent returns 1, son returns 0 */
     328             : int
     329          60 : pari_daemon(void)
     330             : {
     331          60 :   pid_t pid = fork();
     332         120 :   switch(pid) {
     333           0 :       case -1: return 1; /* father, fork failed */
     334             :       case 0:
     335          60 :         (void)setsid(); /* son becomes process group leader */
     336          60 :         if (fork()) _exit(0); /* now son exits, also when fork fails */
     337          60 :         break; /* grandson: its father is the son, which exited,
     338             :                 * hence father becomes 'init', that'll take care of it */
     339             :       default: /* father, fork succeeded */
     340          60 :         (void)waitpid(pid,NULL,0); /* wait for son to exit, immediate */
     341          60 :         return 1;
     342             :   }
     343             :   /* grandson */
     344          60 :   return 0;
     345             : }
     346             : #else
     347             : int
     348             : pari_daemon(void)
     349             : {
     350             :   pari_err_IMPL("pari_daemon without waitpid & setsid");
     351             :   return 0;
     352             : }
     353             : #endif
     354             : 
     355             : /*********************************************************************/
     356             : /*                                                                   */
     357             : /*                       SYSTEM INITIALIZATION                       */
     358             : /*                                                                   */
     359             : /*********************************************************************/
     360             : static int try_to_recover = 0;
     361             : THREAD VOLATILE int PARI_SIGINT_block = 0, PARI_SIGINT_pending = 0;
     362             : 
     363             : /*********************************************************************/
     364             : /*                         SIGNAL HANDLERS                           */
     365             : /*********************************************************************/
     366             : static void
     367           0 : dflt_sigint_fun(void) { pari_err(e_MISC, "user interrupt"); }
     368             : 
     369             : #if defined(_WIN32) || defined(__CYGWIN32__)
     370             : int win32ctrlc = 0, win32alrm = 0;
     371             : void
     372             : dowin32ctrlc(void)
     373             : {
     374             :   win32ctrlc = 0;
     375             :   cb_pari_sigint();
     376             : }
     377             : #endif
     378             : 
     379             : static void
     380           0 : pari_handle_SIGINT(void)
     381             : {
     382             : #ifdef _WIN32
     383             :   if (++win32ctrlc >= 5) _exit(3);
     384             : #else
     385           0 :   cb_pari_sigint();
     386             : #endif
     387           0 : }
     388             : 
     389             : typedef void (*pari_sighandler_t)(int);
     390             : 
     391             : pari_sighandler_t
     392       17012 : os_signal(int sig, pari_sighandler_t f)
     393             : {
     394             : #ifdef HAS_SIGACTION
     395             :   struct sigaction sa, oldsa;
     396             : 
     397       17012 :   sa.sa_handler = f;
     398       17012 :   sigemptyset(&sa.sa_mask);
     399       17012 :   sa.sa_flags = SA_NODEFER;
     400             : 
     401       17012 :   if (sigaction(sig, &sa, &oldsa)) return NULL;
     402       17012 :   return oldsa.sa_handler;
     403             : #else
     404             :   return signal(sig,f);
     405             : #endif
     406             : }
     407             : 
     408             : void
     409           0 : pari_sighandler(int sig)
     410             : {
     411             :   const char *msg;
     412             : #ifndef HAS_SIGACTION
     413             :   /*SYSV reset the signal handler in the handler*/
     414             :   (void)os_signal(sig,pari_sighandler);
     415             : #endif
     416           0 :   switch(sig)
     417             :   {
     418             : #ifdef SIGBREAK
     419             :     case SIGBREAK:
     420             :       if (PARI_SIGINT_block==1)
     421             :       {
     422             :         PARI_SIGINT_pending=SIGBREAK;
     423             :         mt_sigint();
     424             :       }
     425             :       else pari_handle_SIGINT();
     426             :       return;
     427             : #endif
     428             : 
     429             : #ifdef SIGINT
     430             :     case SIGINT:
     431           0 :       if (PARI_SIGINT_block==1)
     432             :       {
     433           0 :         PARI_SIGINT_pending=SIGINT;
     434           0 :         mt_sigint();
     435             :       }
     436           0 :       else pari_handle_SIGINT();
     437           0 :       return;
     438             : #endif
     439             : 
     440             : #ifdef SIGSEGV
     441             :     case SIGSEGV:
     442           0 :       msg="PARI/GP (Segmentation Fault)"; break;
     443             : #endif
     444             : #ifdef SIGBUS
     445             :     case SIGBUS:
     446           0 :       msg="PARI/GP (Bus Error)"; break;
     447             : #endif
     448             : #ifdef SIGFPE
     449             :     case SIGFPE:
     450           0 :       msg="PARI/GP (Floating Point Exception)"; break;
     451             : #endif
     452             : 
     453             : #ifdef SIGPIPE
     454             :     case SIGPIPE:
     455             :     {
     456           0 :       pariFILE *f = GP_DATA->pp->file;
     457           0 :       if (f && pari_outfile == f->file)
     458             :       {
     459           0 :         GP_DATA->pp->file = NULL; /* to avoid oo recursion on error */
     460           0 :         pari_outfile = stdout; pari_fclose(f);
     461           0 :         pari_err(e_MISC, "Broken Pipe, resetting file stack...");
     462             :       }
     463             :       return; /* LCOV_EXCL_LINE */
     464             :     }
     465             : #endif
     466             : 
     467           0 :     default: msg="signal handling"; break;
     468             :   }
     469           0 :   pari_err_BUG(msg);
     470             : }
     471             : 
     472             : void
     473        3094 : pari_sig_init(void (*f)(int))
     474             : {
     475             : #ifdef SIGBUS
     476        3094 :   (void)os_signal(SIGBUS,f);
     477             : #endif
     478             : #ifdef SIGFPE
     479        3094 :   (void)os_signal(SIGFPE,f);
     480             : #endif
     481             : #ifdef SIGINT
     482        3094 :   (void)os_signal(SIGINT,f);
     483             : #endif
     484             : #ifdef SIGBREAK
     485             :   (void)os_signal(SIGBREAK,f);
     486             : #endif
     487             : #ifdef SIGPIPE
     488        3094 :   (void)os_signal(SIGPIPE,f);
     489             : #endif
     490             : #ifdef SIGSEGV
     491        3094 :   (void)os_signal(SIGSEGV,f);
     492             : #endif
     493        3094 : }
     494             : 
     495             : /*********************************************************************/
     496             : /*                      STACK AND UNIVERSAL CONSTANTS                */
     497             : /*********************************************************************/
     498             : static void
     499        1552 : init_universal_constants(void)
     500             : {
     501        1552 :   gen_0  = (GEN)readonly_constants;
     502        1552 :   gnil   = (GEN)readonly_constants+2;
     503        1552 :   gen_1  = (GEN)readonly_constants+4;
     504        1552 :   gen_2  = (GEN)readonly_constants+7;
     505        1552 :   gen_m1 = (GEN)readonly_constants+10;
     506        1552 :   gen_m2 = (GEN)readonly_constants+13;
     507        1552 :   err_e_STACK = (GEN)readonly_constants+16;
     508        1552 :   ghalf  = (GEN)readonly_constants+18;
     509        1552 : }
     510             : 
     511             : static void
     512      113495 : pari_init_errcatch(void)
     513             : {
     514      113495 :   iferr_env = NULL;
     515      113495 :   global_err_data = NULL;
     516      113495 : }
     517             : 
     518             : /*********************************************************************/
     519             : /*                           INIT DEFAULTS                           */
     520             : /*********************************************************************/
     521             : void
     522        1552 : pari_init_defaults(void)
     523             : {
     524             :   long i;
     525        1552 :   initout(1);
     526             : 
     527             : #ifdef LONG_IS_64BIT
     528        1334 :   precreal = 128;
     529             : #else
     530         218 :   precreal = 96;
     531             : #endif
     532             : 
     533        1552 :   precdl = 16;
     534        1552 :   DEBUGFILES = DEBUGLEVEL = 0;
     535        1552 :   DEBUGMEM = 1;
     536        1552 :   disable_color = 1;
     537        1552 :   logstyle = logstyle_none;
     538             : 
     539        1552 :   current_psfile = pari_strdup("pari.ps");
     540        1552 :   current_logfile= pari_strdup("pari.log");
     541        1552 :   pari_logfile = NULL;
     542             : 
     543        1552 :   pari_datadir = os_getenv("GP_DATA_DIR");
     544        1552 :   if (!pari_datadir)
     545             :   {
     546             : #if defined(_WIN32) || defined(__CYGWIN32__)
     547             :     if (paricfg_datadir[0]=='@' && paricfg_datadir[1]==0)
     548             :       pari_datadir = win32_datadir();
     549             :     else
     550             : #endif
     551        1552 :       pari_datadir = pari_strdup(paricfg_datadir);
     552             :   }
     553           0 :   else pari_datadir= pari_strdup(pari_datadir);
     554        1552 :   for (i=0; i<c_LAST; i++) gp_colors[i] = c_NONE;
     555        1552 : }
     556             : 
     557             : /*********************************************************************/
     558             : /*                   FUNCTION HASHTABLES, MODULES                    */
     559             : /*********************************************************************/
     560             : extern entree functions_basic[], functions_default[];
     561             : static void
     562        1552 : pari_init_functions(void)
     563             : {
     564        1552 :   pari_stack_init(&s_MODULES, sizeof(*MODULES),(void**)&MODULES);
     565        1552 :   pari_stack_pushp(&s_MODULES,functions_basic);
     566        1552 :   functions_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     567        1552 :   pari_fill_hashtable(functions_hash, functions_basic);
     568        1552 :   defaults_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     569        1552 :   pari_add_defaults_module(functions_default);
     570        1552 : }
     571             : 
     572             : void
     573        1542 : pari_add_module(entree *ep)
     574             : {
     575        1542 :   pari_fill_hashtable(functions_hash, ep);
     576        1542 :   pari_stack_pushp(&s_MODULES, ep);
     577        1542 : }
     578             : 
     579             : void
     580        1552 : pari_add_defaults_module(entree *ep)
     581        1552 : { pari_fill_hashtable(defaults_hash, ep); }
     582             : 
     583             : /*********************************************************************/
     584             : /*                       PARI MAIN STACK                             */
     585             : /*********************************************************************/
     586             : 
     587             : #ifdef HAS_MMAP
     588             : #define PARI_STACK_ALIGN (sysconf(_SC_PAGE_SIZE))
     589             : #ifndef MAP_ANONYMOUS
     590             : #define MAP_ANONYMOUS MAP_ANON
     591             : #endif
     592             : #ifndef MAP_NORESERVE
     593             : #define MAP_NORESERVE 0
     594             : #endif
     595             : static void *
     596      114181 : pari_mainstack_malloc(size_t size)
     597             : {
     598             :   /* Check that the system allows reserving "size" bytes. This is just
     599             :    * a check, we immediately free the memory. */
     600      114181 :   void *b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     601             :                              MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     602      114181 :   if (b == MAP_FAILED) return NULL;
     603      114181 :   munmap(b, size);
     604             : 
     605             :   /* Map again, this time with MAP_NORESERVE. On some operating systems
     606             :    * like Cygwin, this is needed because remapping with PROT_NONE and
     607             :    * MAP_NORESERVE does not work as expected. */
     608      114181 :   b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     609             :                        MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
     610      114181 :   if (b == MAP_FAILED) return NULL;
     611      114181 :   return b;
     612             : }
     613             : 
     614             : static void
     615      114171 : pari_mainstack_mfree(void *s, size_t size)
     616             : {
     617      114171 :   munmap(s, size);
     618      114171 : }
     619             : 
     620             : /* Completely discard the memory mapped between the addresses "from"
     621             :  * and "to" (which must be page-aligned).
     622             :  *
     623             :  * We use mmap() with PROT_NONE, which means that the underlying memory
     624             :  * is freed and that the kernel should not commit memory for it. We
     625             :  * still keep the mapping such that we can change the flags to
     626             :  * PROT_READ|PROT_WRITE later.
     627             :  *
     628             :  * NOTE: remapping with MAP_FIXED and PROT_NONE is not the same as
     629             :  * calling mprotect(..., PROT_NONE) because the latter will keep the
     630             :  * memory committed (this is in particular relevant on Linux with
     631             :  * vm.overcommit = 2). This remains true even when calling
     632             :  * madvise(..., MADV_DONTNEED). */
     633             : static void
     634      193550 : pari_mainstack_mreset(pari_sp from, pari_sp to)
     635             : {
     636      193550 :   size_t s = to - from;
     637             :   void *addr, *res;
     638      193550 :   if (!s) return;
     639             : 
     640           0 :   addr = (void*)from;
     641           0 :   res = mmap(addr, s, PROT_NONE,
     642             :              MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
     643           0 :   if (res != addr) pari_err(e_MEM);
     644             : }
     645             : 
     646             : /* Commit (make available) the virtual memory mapped between the
     647             :  * addresses "from" and "to" (which must be page-aligned).
     648             :  * Return 0 if successful, -1 if failed. */
     649             : static int
     650      193550 : pari_mainstack_mextend(pari_sp from, pari_sp to)
     651             : {
     652      193550 :   size_t s = to - from;
     653      193550 :   return mprotect((void*)from, s, PROT_READ|PROT_WRITE);
     654             : }
     655             : 
     656             : /* Set actual stack size to the given size. This sets st->size and
     657             :  * st->bot. If not enough system memory is available, this can fail.
     658             :  * Return 1 if successful, 0 if failed (in that case, st->size is not
     659             :  * changed) */
     660             : static int
     661      193550 : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     662             : {
     663      193550 :   pari_sp newbot = st->top - size;
     664             :   /* Align newbot to pagesize */
     665      193550 :   pari_sp alignbot = newbot & ~(pari_sp)(PARI_STACK_ALIGN - 1);
     666      193550 :   if (pari_mainstack_mextend(alignbot, st->top))
     667             :   {
     668             :     /* Making the memory available did not work: limit vsize to the
     669             :      * current actual stack size. */
     670           0 :     st->vsize = st->size;
     671           0 :     pari_warn(warnstack, st->vsize);
     672           0 :     return 0;
     673             :   }
     674      193550 :   pari_mainstack_mreset(st->vbot, alignbot);
     675      193550 :   st->bot = newbot;
     676      193550 :   st->size = size;
     677      193550 :   return 1;
     678             : }
     679             : 
     680             : #else
     681             : #define PARI_STACK_ALIGN (0x40UL)
     682             : static void *
     683             : pari_mainstack_malloc(size_t s)
     684             : {
     685             :   return malloc(s); /* NOT pari_malloc, e_MEM would be deadly */
     686             : }
     687             : 
     688             : static void
     689             : pari_mainstack_mfree(void *s, size_t size) { (void) size; free(s); }
     690             : 
     691             : static int
     692             : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     693             : {
     694             :   st->bot = st->top - size;
     695             :   st->size = size;
     696             :   return 1;
     697             : }
     698             : 
     699             : #endif
     700             : 
     701             : static const size_t MIN_STACK = 500032UL;
     702             : static size_t
     703      228352 : fix_size(size_t a)
     704             : {
     705      228352 :   size_t ps = PARI_STACK_ALIGN;
     706      228352 :   size_t b = a & ~(ps - 1); /* Align */
     707      228352 :   if (b < a && b < ~(ps - 1)) b += ps;
     708      228352 :   if (b < MIN_STACK) b = MIN_STACK;
     709      228352 :   return b;
     710             : }
     711             : 
     712             : static void
     713      114181 : pari_mainstack_alloc(int numerr, struct pari_mainstack *st, size_t rsize, size_t vsize)
     714             : {
     715      114181 :   size_t sizemax = vsize ? vsize: rsize, s = fix_size(sizemax);
     716             :   for (;;)
     717             :   {
     718      114181 :     st->vbot = (pari_sp)pari_mainstack_malloc(s);
     719      114181 :     if (st->vbot) break;
     720           0 :     if (s == MIN_STACK) pari_err(e_MEM); /* no way out. Die */
     721           0 :     s = fix_size(s >> 1);
     722           0 :     pari_warn(numerr, s);
     723             :   }
     724      114181 :   st->vsize = vsize ? s: 0;
     725      114181 :   st->rsize = minuu(rsize, s);
     726      114181 :   st->top = st->vbot+s;
     727      114181 :   if (!pari_mainstack_setsize(st, st->rsize))
     728             :   {
     729             :     /* This should never happen since we only decrease the allocated space */
     730           0 :     pari_err(e_MEM);
     731             :   }
     732      114181 :   st->memused = 0;
     733      114181 : }
     734             : 
     735             : static void
     736      114171 : pari_mainstack_free(struct pari_mainstack *st)
     737             : {
     738      114171 :   pari_mainstack_mfree((void*)st->vbot, st->vsize ? st->vsize : fix_size(st->rsize));
     739      114171 :   st->top = st->bot = st->vbot = 0;
     740      114171 :   st->size = st->vsize = 0;
     741      114171 : }
     742             : 
     743             : static void
     744         278 : pari_mainstack_resize(struct pari_mainstack *st, size_t rsize, size_t vsize)
     745             : {
     746         278 :   BLOCK_SIGINT_START;
     747         278 :   pari_mainstack_free(st);
     748         278 :   pari_mainstack_alloc(warnstack, st, rsize, vsize);
     749         278 :   BLOCK_SIGINT_END;
     750         278 : }
     751             : 
     752             : static void
     753      112979 : pari_mainstack_use(struct pari_mainstack *st)
     754             : {
     755      112979 :   pari_mainstack = st;
     756      112979 :   avma = st->top; /* don't use set_avma */
     757      112979 : }
     758             : 
     759             : static void
     760        1552 : paristack_alloc(size_t rsize, size_t vsize)
     761             : {
     762        1552 :   pari_mainstack_alloc(warnstack, pari_mainstack, rsize, vsize);
     763        1552 :   pari_mainstack_use(pari_mainstack);
     764        1552 : }
     765             : 
     766             : void
     767           0 : paristack_setsize(size_t rsize, size_t vsize)
     768             : {
     769           0 :   pari_mainstack_resize(pari_mainstack, rsize, vsize);
     770           0 :   pari_mainstack_use(pari_mainstack);
     771           0 : }
     772             : 
     773             : void
     774           0 : parivstack_resize(ulong newsize)
     775             : {
     776             :   size_t s;
     777           0 :   if (newsize && newsize < pari_mainstack->rsize)
     778           0 :     pari_err_DIM("stack sizes [parisizemax < parisize]");
     779           0 :   if (newsize == pari_mainstack->vsize) return;
     780           0 :   evalstate_reset();
     781           0 :   paristack_setsize(pari_mainstack->rsize, newsize);
     782           0 :   s = pari_mainstack->vsize ? pari_mainstack->vsize : pari_mainstack->rsize;
     783           0 :   if (DEBUGMEM)
     784           0 :     pari_warn(warner,"new maximum stack size = %lu (%.3f Mbytes)",
     785             :               s, s/1048576.);
     786           0 :   pari_init_errcatch();
     787           0 :   cb_pari_err_recover(-1);
     788             : }
     789             : 
     790             : void
     791         284 : paristack_newrsize(ulong newsize)
     792             : {
     793         284 :   size_t s, vsize = pari_mainstack->vsize;
     794         284 :   if (!newsize) newsize = pari_mainstack->rsize << 1;
     795         284 :   if (newsize != pari_mainstack->rsize)
     796         278 :     pari_mainstack_resize(pari_mainstack, newsize, vsize);
     797         284 :   evalstate_reset();
     798         284 :   s = pari_mainstack->rsize;
     799         284 :   if (DEBUGMEM)
     800         284 :     pari_warn(warner,"new stack size = %lu (%.3f Mbytes)", s, s/1048576.);
     801         284 :   pari_init_errcatch();
     802         284 :   cb_pari_err_recover(-1);
     803           0 : }
     804             : 
     805             : void
     806           0 : paristack_resize(ulong newsize)
     807             : {
     808           0 :   long size = pari_mainstack->size;
     809           0 :   if (!newsize)
     810           0 :     newsize = 2 * size;
     811           0 :   newsize = minuu(newsize, pari_mainstack->vsize);
     812           0 :   if (newsize <= pari_mainstack->size) return;
     813           0 :   if (pari_mainstack_setsize(pari_mainstack, newsize))
     814             :   {
     815           0 :     if (DEBUGMEM)
     816           0 :       pari_warn(warner, "increasing stack size to %lu", pari_mainstack->size);
     817             :   }
     818             :   else
     819             :   {
     820           0 :     pari_mainstack_setsize(pari_mainstack, size);
     821           0 :     pari_err(e_STACK);
     822             :   }
     823             : }
     824             : 
     825             : void
     826       79369 : parivstack_reset(void)
     827             : {
     828       79369 :   pari_mainstack_setsize(pari_mainstack, pari_mainstack->rsize);
     829       79369 :   if (avma < pari_mainstack->bot)
     830           0 :     pari_err_BUG("parivstack_reset [avma < bot]");
     831       79369 : }
     832             : 
     833             : /* Enlarge the stack if needed such that the unused portion of the stack
     834             :  * (between bot and avma) is large enough to contain x longs. */
     835             : void
     836           7 : new_chunk_resize(size_t x)
     837             : {
     838           7 :   if (pari_mainstack->vsize==0
     839           0 :     || x > (avma-pari_mainstack->vbot) / sizeof(long)) pari_err(e_STACK);
     840           0 :   while (x > (avma-pari_mainstack->bot) / sizeof(long))
     841           0 :     paristack_resize(0);
     842           0 : }
     843             : 
     844             : /*********************************************************************/
     845             : /*                       PARI THREAD                                 */
     846             : /*********************************************************************/
     847             : 
     848             : /* Initial PARI thread structure t with a stack of size s and virtual size v
     849             :  * and argument arg */
     850             : 
     851             : void
     852           0 : pari_thread_valloc(struct pari_thread *t, size_t s, size_t v, GEN arg)
     853             : {
     854           0 :   pari_mainstack_alloc(warnstackthread, &t->st,s,v);
     855           0 :   t->data = arg;
     856           0 : }
     857             : 
     858             : /* Initial PARI thread structure t with a stack of size s and
     859             :  * argument arg */
     860             : 
     861             : void
     862      112351 : pari_thread_alloc(struct pari_thread *t, size_t s, GEN arg)
     863             : {
     864      112351 :   pari_mainstack_alloc(warnstackthread, &t->st,s,0);
     865      112351 :   t->data = arg;
     866      112351 : }
     867             : 
     868             : void
     869      112351 : pari_thread_free(struct pari_thread *t)
     870             : {
     871      112351 :   pari_mainstack_free(&t->st);
     872      112351 : }
     873             : 
     874             : void
     875      113460 : pari_thread_init(void)
     876             : {
     877      113460 :   pari_init_blocks();
     878      113269 :   pari_init_errcatch();
     879      113270 :   pari_init_rand();
     880      113729 :   pari_init_floats();
     881      113539 :   pari_init_parser();
     882      113817 :   pari_init_compiler();
     883      113782 :   pari_init_evaluator();
     884      113756 :   pari_init_files();
     885      113413 :   pari_thread_init_primetab();
     886      113367 :   pari_thread_init_seadata();
     887      113186 : }
     888             : 
     889             : void
     890       11139 : pari_thread_sync(void)
     891             : {
     892       11139 :   pari_pthread_init_primetab();
     893       11139 :   pari_pthread_init_seadata();
     894       11139 :   pari_pthread_init_varstate();
     895       11139 : }
     896             : 
     897             : void
     898      112981 : pari_thread_close(void)
     899             : {
     900      112981 :   pari_thread_close_files();
     901      111601 :   pari_close_evaluator();
     902      112376 :   pari_close_compiler();
     903      111434 :   pari_close_parser();
     904      112911 :   pari_close_floats();
     905      111894 :   pari_close_blocks();
     906      112891 : }
     907             : 
     908             : GEN
     909      111462 : pari_thread_start(struct pari_thread *t)
     910             : {
     911      111462 :   pari_mainstack_use(&t->st);
     912      111922 :   pari_thread_init();
     913      111696 :   pari_thread_init_varstate();
     914      112101 :   return t->data;
     915             : }
     916             : 
     917             : /*********************************************************************/
     918             : /*                       LIBPARI INIT / CLOSE                        */
     919             : /*********************************************************************/
     920             : 
     921             : static void
     922           0 : pari_exit(void)
     923             : {
     924           0 :   err_printf("  ***   Error in the PARI system. End of program.\n");
     925           0 :   exit(1);
     926             : }
     927             : 
     928             : static void
     929           0 : dflt_err_recover(long errnum) { (void) errnum; pari_exit(); }
     930             : 
     931             : static void
     932           0 : dflt_pari_quit(long err) { (void)err; /*do nothing*/; }
     933             : 
     934             : static int pari_err_display(GEN err);
     935             : 
     936             : /* initialize PARI data. Initialize [new|old]fun to NULL for default set. */
     937             : void
     938        1552 : pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
     939             : {
     940             :   ulong u;
     941             : 
     942        1552 :   pari_mt_nbthreads = 0;
     943        1552 :   cb_pari_quit = dflt_pari_quit;
     944        1552 :   cb_pari_init_histfile = NULL;
     945        1552 :   cb_pari_get_line_interactive = NULL;
     946        1552 :   cb_pari_fgets_interactive = NULL;
     947        1552 :   cb_pari_whatnow = NULL;
     948        1552 :   cb_pari_handle_exception = NULL;
     949        1552 :   cb_pari_err_handle = pari_err_display;
     950        1552 :   cb_pari_pre_recover = NULL;
     951        1552 :   cb_pari_break_loop = NULL;
     952        1552 :   cb_pari_is_interactive = NULL;
     953        1552 :   cb_pari_start_output = NULL;
     954        1552 :   cb_pari_sigint = dflt_sigint_fun;
     955        1552 :   if (init_opts&INIT_JMPm) cb_pari_err_recover = dflt_err_recover;
     956             : 
     957        1552 :   pari_stackcheck_init(&u);
     958        1552 :   pari_init_homedir();
     959        1552 :   if (init_opts&INIT_DFTm) {
     960           0 :     pari_init_defaults();
     961           0 :     GP_DATA = default_gp_data();
     962           0 :     pari_init_paths();
     963             :   }
     964             : 
     965        1552 :   pari_mainstack = (struct pari_mainstack *) malloc(sizeof(*pari_mainstack));
     966        1552 :   paristack_alloc(parisize, 0);
     967        1552 :   init_universal_constants();
     968        1552 :   diffptr = NULL;
     969        1552 :   if (!(init_opts&INIT_noPRIMEm))  pari_init_primes(maxprime);
     970        1552 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_init();
     971        1552 :   pari_init_graphics();
     972        1552 :   pari_init_primetab();
     973        1552 :   pari_init_seadata();
     974        1552 :   pari_thread_init();
     975        1552 :   pari_init_functions();
     976        1552 :   pari_var_init();
     977        1552 :   pari_init_timer();
     978        1552 :   pari_init_buffers();
     979        1552 :   (void)getabstime();
     980        1552 :   try_to_recover = 1;
     981        1552 :   if (!(init_opts&INIT_noIMTm)) pari_mt_init();
     982        1552 :   if ((init_opts&INIT_SIGm)) pari_sig_init(pari_sighandler);
     983        1552 : }
     984             : 
     985             : void
     986           0 : pari_init(size_t parisize, ulong maxprime)
     987           0 : { pari_init_opts(parisize, maxprime, INIT_JMPm | INIT_SIGm | INIT_DFTm); }
     988             : 
     989             : void
     990        1542 : pari_close_opts(ulong init_opts)
     991             : {
     992             :   long i;
     993             : 
     994        1542 :   BLOCK_SIGINT_START;
     995        1542 :   if ((init_opts&INIT_SIGm)) pari_sig_init(SIG_DFL);
     996        1542 :   if (!(init_opts&INIT_noIMTm)) pari_mt_close();
     997             : 
     998        1542 :   pari_var_close(); /* must come before destruction of functions_hash */
     999      209712 :   for (i = 0; i < functions_tblsz; i++)
    1000             :   {
    1001      208170 :     entree *ep = functions_hash[i];
    1002     2230474 :     while (ep) {
    1003     1814134 :       entree *EP = ep->next;
    1004     1814134 :       if (!EpSTATIC(ep)) { freeep(ep); free(ep); }
    1005     1814134 :       ep = EP;
    1006             :     }
    1007             :   }
    1008        1542 :   pari_close_mf();
    1009        1542 :   pari_thread_close();
    1010        1542 :   pari_close_files();
    1011        1542 :   pari_close_homedir();
    1012        1542 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_close();
    1013             : 
    1014        1542 :   free((void*)functions_hash);
    1015        1542 :   free((void*)defaults_hash);
    1016        1542 :   if (diffptr) pari_close_primes();
    1017        1542 :   free(current_logfile);
    1018        1542 :   free(current_psfile);
    1019        1542 :   pari_mainstack_free(pari_mainstack);
    1020        1542 :   free((void*)pari_mainstack);
    1021        1542 :   pari_stack_delete(&s_MODULES);
    1022        1542 :   if (pari_datadir) free(pari_datadir);
    1023        1542 :   if (init_opts&INIT_DFTm)
    1024             :   { /* delete GP_DATA */
    1025        1542 :     pari_close_paths();
    1026        1542 :     if (GP_DATA->hist->v) free((void*)GP_DATA->hist->v);
    1027        1542 :     if (GP_DATA->pp->cmd) free((void*)GP_DATA->pp->cmd);
    1028        1542 :     if (GP_DATA->help) free((void*)GP_DATA->help);
    1029        1542 :     if (GP_DATA->plothsizes) free((void*)GP_DATA->plothsizes);
    1030        1542 :     if (GP_DATA->colormap) pari_free(GP_DATA->colormap);
    1031        1542 :     if (GP_DATA->graphcolors) pari_free(GP_DATA->graphcolors);
    1032        1542 :     free((void*)GP_DATA->prompt);
    1033        1542 :     free((void*)GP_DATA->prompt_cont);
    1034        1542 :     free((void*)GP_DATA->histfile);
    1035             :   }
    1036        1542 :   BLOCK_SIGINT_END;
    1037        1542 : }
    1038             : 
    1039             : void
    1040        1542 : pari_close(void)
    1041        1542 : { pari_close_opts(INIT_JMPm | INIT_SIGm | INIT_DFTm); }
    1042             : 
    1043             : /*******************************************************************/
    1044             : /*                                                                 */
    1045             : /*                         ERROR RECOVERY                          */
    1046             : /*                                                                 */
    1047             : /*******************************************************************/
    1048             : void
    1049       95670 : gp_context_save(struct gp_context* rec)
    1050             : {
    1051       95670 :   rec->prettyp = GP_DATA->fmt->prettyp;
    1052       95670 :   rec->listloc = next_block;
    1053       95670 :   rec->iferr_env = iferr_env;
    1054       95670 :   rec->err_data  = global_err_data;
    1055       95670 :   varstate_save(&rec->var);
    1056       95670 :   evalstate_save(&rec->eval);
    1057       95670 :   parsestate_save(&rec->parse);
    1058       95670 :   filestate_save(&rec->file);
    1059       95670 : }
    1060             : 
    1061             : void
    1062        8889 : gp_context_restore(struct gp_context* rec)
    1063             : {
    1064             :   long i;
    1065             : 
    1066        8889 :   if (!try_to_recover) return;
    1067             :   /* disable gp_context_restore() and SIGINT */
    1068        8889 :   try_to_recover = 0;
    1069        8889 :   BLOCK_SIGINT_START
    1070        8889 :   if (DEBUGMEM>2) err_printf("entering recover(), loc = %ld\n", rec->listloc);
    1071        8889 :   evalstate_restore(&rec->eval);
    1072        8889 :   parsestate_restore(&rec->parse);
    1073        8889 :   filestate_restore(&rec->file);
    1074        8889 :   global_err_data = rec->err_data;
    1075        8889 :   iferr_env = rec->iferr_env;
    1076        8889 :   GP_DATA->fmt->prettyp = rec->prettyp;
    1077             : 
    1078     1208904 :   for (i = 0; i < functions_tblsz; i++)
    1079             :   {
    1080     1200015 :     entree *ep = functions_hash[i];
    1081    13519907 :     while (ep)
    1082             :     {
    1083    11119877 :       entree *EP = ep->next;
    1084    11119877 :       switch(EpVALENCE(ep))
    1085             :       {
    1086             :         case EpVAR:
    1087      195283 :           while (pop_val_if_newer(ep,rec->listloc)) /* empty */;
    1088      195283 :           break;
    1089      622243 :         case EpNEW: break;
    1090             :       }
    1091    11119877 :       ep = EP;
    1092             :     }
    1093             :   }
    1094        8889 :   varstate_restore(&rec->var);
    1095        8889 :   if (DEBUGMEM>2) err_printf("leaving recover()\n");
    1096        8889 :   BLOCK_SIGINT_END
    1097        8889 :   try_to_recover = 1;
    1098             : }
    1099             : 
    1100             : static void
    1101        8821 : err_recover(long numerr)
    1102             : {
    1103        8821 :   if (cb_pari_pre_recover)
    1104        8821 :     cb_pari_pre_recover(numerr);
    1105           0 :   evalstate_reset();
    1106           0 :   killallfiles();
    1107           0 :   pari_init_errcatch();
    1108           0 :   cb_pari_err_recover(numerr);
    1109           0 : }
    1110             : 
    1111             : static void
    1112        9385 : err_init(void)
    1113             : {
    1114             :   /* make sure pari_err msg starts at the beginning of line */
    1115        9385 :   if (!pari_last_was_newline()) pari_putc('\n');
    1116        9385 :   pariOut->flush();
    1117        9385 :   pariErr->flush();
    1118        9385 :   out_term_color(pariErr, c_ERR);
    1119        9385 : }
    1120             : 
    1121             : static void
    1122        9320 : err_init_msg(int user)
    1123             : {
    1124             :   const char *gp_function_name;
    1125        9320 :   out_puts(pariErr, "  *** ");
    1126        9320 :   if (!user && (gp_function_name = closure_func_err()))
    1127        5856 :     out_printf(pariErr, "%s: ", gp_function_name);
    1128             :   else
    1129        3464 :     out_puts(pariErr, "  ");
    1130        9320 : }
    1131             : 
    1132             : void
    1133         543 : pari_warn(int numerr, ...)
    1134             : {
    1135             :   char *ch1;
    1136             :   va_list ap;
    1137             : 
    1138         543 :   va_start(ap,numerr);
    1139             : 
    1140         543 :   err_init();
    1141         543 :   err_init_msg(numerr==warnuser || numerr==warnstack);
    1142         543 :   switch (numerr)
    1143             :   {
    1144             :     case warnuser:
    1145           7 :       out_puts(pariErr, "user warning: ");
    1146           7 :       out_print0(pariErr, NULL, va_arg(ap, GEN), f_RAW);
    1147           7 :       break;
    1148             : 
    1149             :     case warnmem:
    1150           0 :       out_puts(pariErr, "collecting garbage in "); ch1=va_arg(ap, char*);
    1151           0 :       out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
    1152           0 :       break;
    1153             : 
    1154             :     case warner:
    1155         536 :       out_puts(pariErr, "Warning: "); ch1=va_arg(ap, char*);
    1156         536 :       out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
    1157         536 :       break;
    1158             : 
    1159             :     case warnprec:
    1160           0 :       out_vprintf(pariErr, "Warning: increasing prec in %s; new prec = %ld",
    1161             :                       ap);
    1162           0 :       break;
    1163             : 
    1164             :     case warnfile:
    1165           0 :       out_puts(pariErr, "Warning: failed to "),
    1166           0 :       ch1 = va_arg(ap, char*);
    1167           0 :       out_printf(pariErr, "%s: %s", ch1, va_arg(ap, char*));
    1168           0 :       break;
    1169             : 
    1170             :     case warnstack:
    1171             :     case warnstackthread:
    1172             :     {
    1173           0 :       ulong  s = va_arg(ap, ulong);
    1174             :       char buf[128];
    1175           0 :       const char * stk = numerr == warnstackthread
    1176           0 :                          || mt_is_thread() ? "thread": "PARI";
    1177           0 :       sprintf(buf,"Warning: not enough memory, new %s stack %lu", stk, s);
    1178           0 :       out_puts(pariErr,buf);
    1179           0 :       break;
    1180             :     }
    1181             :   }
    1182         543 :   va_end(ap);
    1183         543 :   out_term_color(pariErr, c_NONE);
    1184         543 :   out_putc(pariErr, '\n');
    1185         543 :   pariErr->flush();
    1186         543 : }
    1187             : void
    1188           0 : pari_sigint(const char *time_s)
    1189             : {
    1190           0 :   int recover=0;
    1191           0 :   BLOCK_SIGALRM_START
    1192           0 :   err_init();
    1193           0 :   closure_err(0);
    1194           0 :   err_init_msg(0);
    1195           0 :   out_puts(pariErr, "user interrupt after ");
    1196           0 :   out_puts(pariErr, time_s);
    1197           0 :   out_term_color(pariErr, c_NONE);
    1198           0 :   pariErr->flush();
    1199           0 :   if (cb_pari_handle_exception)
    1200           0 :     recover = cb_pari_handle_exception(-1);
    1201           0 :   if (!recover && !block)
    1202           0 :     PARI_SIGINT_pending = 0;
    1203           0 :   BLOCK_SIGINT_END
    1204           0 :   if (!recover) err_recover(e_MISC);
    1205           0 : }
    1206             : 
    1207             : #define retmkerr2(x,y)\
    1208             :   do { GEN _v = cgetg(3, t_ERROR);\
    1209             :        _v[1] = (x);\
    1210             :        gel(_v,2) = (y); return _v; } while(0)
    1211             : #define retmkerr3(x,y,z)\
    1212             :   do { GEN _v = cgetg(4, t_ERROR);\
    1213             :        _v[1] = (x);\
    1214             :        gel(_v,2) = (y);\
    1215             :        gel(_v,3) = (z); return _v; } while(0)
    1216             : #define retmkerr4(x,y,z,t)\
    1217             :   do { GEN _v = cgetg(5, t_ERROR);\
    1218             :        _v[1] = (x);\
    1219             :        gel(_v,2) = (y);\
    1220             :        gel(_v,3) = (z);\
    1221             :        gel(_v,4) = (t); return _v; } while(0)
    1222             : #define retmkerr5(x,y,z,t,u)\
    1223             :   do { GEN _v = cgetg(6, t_ERROR);\
    1224             :        _v[1] = (x);\
    1225             :        gel(_v,2) = (y);\
    1226             :        gel(_v,3) = (z);\
    1227             :        gel(_v,4) = (t);\
    1228             :        gel(_v,5) = (u); return _v; } while(0)
    1229             : #define retmkerr6(x,y,z,t,u,v)\
    1230             :   do { GEN _v = cgetg(7, t_ERROR);\
    1231             :        _v[1] = (x);\
    1232             :        gel(_v,2) = (y);\
    1233             :        gel(_v,3) = (z);\
    1234             :        gel(_v,4) = (t);\
    1235             :        gel(_v,5) = (u);\
    1236             :        gel(_v,6) = (v); return _v; } while(0)
    1237             : 
    1238             : static GEN
    1239       43520 : pari_err2GEN(long numerr, va_list ap)
    1240             : {
    1241       43520 :   switch ((enum err_list) numerr)
    1242             :   {
    1243             :   case e_SYNTAX:
    1244             :     {
    1245          65 :       const char *msg = va_arg(ap, char*);
    1246          65 :       const char *s = va_arg(ap,char *);
    1247          65 :       const char *entry = va_arg(ap,char *);
    1248          65 :       retmkerr3(numerr,strtoGENstr(msg), mkvecsmall2((long)s,(long)entry));
    1249             :     }
    1250             :   case e_MISC: case e_ALARM:
    1251             :     {
    1252         156 :       const char *ch1 = va_arg(ap, char*);
    1253         156 :       retmkerr2(numerr, gvsprintf(ch1,ap));
    1254             :     }
    1255             :   case e_NOTFUNC:
    1256             :   case e_USER:
    1257        2737 :     retmkerr2(numerr,va_arg(ap, GEN));
    1258             :   case e_FILE:
    1259             :     {
    1260           0 :       const char *f = va_arg(ap, const char*);
    1261           0 :       retmkerr3(numerr, strtoGENstr(f), strtoGENstr(va_arg(ap, char*)));
    1262             :     }
    1263             :   case e_FILEDESC:
    1264             :     {
    1265           0 :       const char *f = va_arg(ap, const char*);
    1266           0 :       retmkerr3(numerr, strtoGENstr(f), stoi(va_arg(ap, long)));
    1267             :     }
    1268             :   case e_OVERFLOW:
    1269             :   case e_IMPL:
    1270             :   case e_DIM:
    1271             :   case e_CONSTPOL:
    1272             :   case e_ROOTS0:
    1273             :   case e_FLAG:
    1274             :   case e_PREC:
    1275             :   case e_BUG:
    1276             :   case e_ARCH:
    1277             :   case e_PACKAGE:
    1278        1342 :     retmkerr2(numerr, strtoGENstr(va_arg(ap, char*)));
    1279             :   case e_MODULUS:
    1280             :   case e_VAR:
    1281             :     {
    1282         952 :       const char *f = va_arg(ap, const char*);
    1283         952 :       GEN x = va_arg(ap, GEN);
    1284         952 :       GEN y = va_arg(ap, GEN);
    1285         952 :       retmkerr4(numerr, strtoGENstr(f), x,y);
    1286             :     }
    1287             :   case e_INV:
    1288             :   case e_IRREDPOL:
    1289             :   case e_PRIME:
    1290             :   case e_SQRTN:
    1291             :   case e_TYPE:
    1292             :     {
    1293       32394 :       const char *f = va_arg(ap, const char*);
    1294       32394 :       GEN x = va_arg(ap, GEN);
    1295       32394 :       retmkerr3(numerr, strtoGENstr(f), x);
    1296             :     }
    1297             :   case e_COPRIME: case e_OP: case e_TYPE2:
    1298             :     {
    1299        3486 :       const char *f = va_arg(ap, const char*);
    1300        3486 :       GEN x = va_arg(ap, GEN);
    1301        3486 :       GEN y = va_arg(ap, GEN);
    1302        3486 :       retmkerr4(numerr,strtoGENstr(f),x,y);
    1303             :     }
    1304             :   case e_COMPONENT:
    1305             :     {
    1306         203 :       const char *f= va_arg(ap, const char *);
    1307         203 :       const char *op = va_arg(ap, const char *);
    1308         203 :       GEN l = va_arg(ap, GEN);
    1309         203 :       GEN x = va_arg(ap, GEN);
    1310         203 :       retmkerr5(numerr,strtoGENstr(f),strtoGENstr(op),l,x);
    1311             :     }
    1312             :   case e_DOMAIN:
    1313             :     {
    1314        2003 :       const char *f = va_arg(ap, const char*);
    1315        2003 :       const char *v = va_arg(ap, const char *);
    1316        2003 :       const char *op = va_arg(ap, const char *);
    1317        2003 :       GEN l = va_arg(ap, GEN);
    1318        2003 :       GEN x = va_arg(ap, GEN);
    1319        2003 :       retmkerr6(numerr,strtoGENstr(f),strtoGENstr(v),strtoGENstr(op),l,x);
    1320             :     }
    1321             :   case e_PRIORITY:
    1322             :     {
    1323         175 :       const char *f = va_arg(ap, const char*);
    1324         175 :       GEN x = va_arg(ap, GEN);
    1325         175 :       const char *op = va_arg(ap, const char *);
    1326         175 :       long v = va_arg(ap, long);
    1327         175 :       retmkerr5(numerr,strtoGENstr(f),x,strtoGENstr(op),stoi(v));
    1328             :     }
    1329             :   case e_MAXPRIME:
    1330           0 :     retmkerr2(numerr, utoi(va_arg(ap, ulong)));
    1331             :   case e_STACK:
    1332           7 :     return err_e_STACK;
    1333             :   case e_STACKTHREAD:
    1334           0 :     retmkerr3(numerr, utoi(va_arg(ap, ulong)), utoi(va_arg(ap, ulong)));
    1335             :   default:
    1336           0 :     return mkerr(numerr);
    1337             :   }
    1338             : }
    1339             : 
    1340             : static char *
    1341        6384 : type_dim(GEN x)
    1342             : {
    1343        6384 :   char *v = stack_malloc(64);
    1344        6384 :   switch(typ(x))
    1345             :   {
    1346             :     case t_MAT:
    1347             :     {
    1348          84 :       long l = lg(x), r = (l == 1)? 1: lgcols(x);
    1349          84 :       sprintf(v, "t_MAT (%ldx%ld)", r-1,l-1);
    1350          84 :       break;
    1351             :     }
    1352             :     case t_COL:
    1353          91 :       sprintf(v, "t_COL (%ld elts)", lg(x)-1);
    1354          91 :       break;
    1355             :     case t_VEC:
    1356         133 :       sprintf(v, "t_VEC (%ld elts)", lg(x)-1);
    1357         133 :       break;
    1358             :     default:
    1359        6076 :       v = (char*)type_name(typ(x));
    1360             :   }
    1361        6384 :   return v;
    1362             : }
    1363             : 
    1364             : static char *
    1365        2359 : gdisplay(GEN x)
    1366             : {
    1367        2359 :   char *s = GENtostr_raw(x);
    1368        2359 :   if (strlen(s) < 1600) return s;
    1369          21 :   if (! GP_DATA->breakloop) return (char*)"(...)";
    1370           0 :   return stack_sprintf("\n  ***  (...) Huge %s omitted; you can access it via dbg_err()", type_name(typ(x)));
    1371             : }
    1372             : 
    1373             : char *
    1374       16099 : pari_err2str(GEN e)
    1375             : {
    1376       16099 :   long numerr = err_get_num(e);
    1377       16099 :   switch ((enum err_list) numerr)
    1378             :   {
    1379             :   case e_ALARM:
    1380           0 :     return pari_sprintf("alarm interrupt after %Ps.",gel(e,2));
    1381             :   case e_MISC:
    1382         154 :     return pari_sprintf("%Ps.",gel(e,2));
    1383             : 
    1384             :   case e_ARCH:
    1385           0 :     return pari_sprintf("sorry, '%Ps' not available on this system.",gel(e,2));
    1386             :   case e_BUG:
    1387          14 :     return pari_sprintf("bug in %Ps, please report.",gel(e,2));
    1388             :   case e_CONSTPOL:
    1389           7 :     return pari_sprintf("constant polynomial in %Ps.", gel(e,2));
    1390             :   case e_COPRIME:
    1391         189 :     return pari_sprintf("elements not coprime in %Ps:\n    %s\n    %s",
    1392         189 :                         gel(e,2), gdisplay(gel(e,3)), gdisplay(gel(e,4)));
    1393             :   case e_DIM:
    1394         487 :     return pari_sprintf("inconsistent dimensions in %Ps.", gel(e,2));
    1395             :   case e_FILE:
    1396           0 :     return pari_sprintf("error opening %Ps: `%Ps'.", gel(e,2), gel(e,3));
    1397             :   case e_FILEDESC:
    1398           0 :     return pari_sprintf("invalid file descriptor in %Ps [%Ps]", gel(e,2), gel(e,3));
    1399             :   case e_FLAG:
    1400          35 :     return pari_sprintf("invalid flag in %Ps.", gel(e,2));
    1401             :   case e_IMPL:
    1402         406 :     return pari_sprintf("sorry, %Ps is not yet implemented.", gel(e,2));
    1403             :   case e_PACKAGE:
    1404           0 :     return pari_sprintf("package %Ps is required, please install it.", gel(e,2));
    1405             :   case e_INV:
    1406         560 :     return pari_sprintf("impossible inverse in %Ps: %s.", gel(e,2),
    1407         560 :                         gdisplay(gel(e,3)));
    1408             :   case e_IRREDPOL:
    1409          56 :     return pari_sprintf("not an irreducible polynomial in %Ps: %s.",
    1410          56 :                         gel(e,2), gdisplay(gel(e,3)));
    1411             :   case e_MAXPRIME:
    1412             :     {
    1413           0 :       const char * msg = "not enough precomputed primes";
    1414           0 :       ulong c = itou(gel(e,2));
    1415           0 :       if (c) return pari_sprintf("%s, need primelimit ~ %lu.",msg, c);
    1416           0 :       else   return pari_strdup(msg);
    1417             :     }
    1418             :   case e_MEM:
    1419           0 :     return pari_strdup("not enough memory");
    1420             :   case e_MODULUS:
    1421             :     {
    1422         749 :       GEN x = gel(e,3), y = gel(e,4);
    1423        1498 :       return pari_sprintf("inconsistent moduli in %Ps: %s != %s",
    1424         749 :                           gel(e,2), gdisplay(x), gdisplay(y));
    1425             :     }
    1426           0 :   case e_NONE: return NULL;
    1427             :   case e_NOTFUNC:
    1428        2723 :     return pari_strdup("not a function in function call");
    1429             :   case e_OP: case e_TYPE2:
    1430             :     {
    1431        3192 :       pari_sp av = avma;
    1432             :       char *v;
    1433        3192 :       const char *f, *op = GSTR(gel(e,2));
    1434        3192 :       const char *what = numerr == e_OP? "inconsistent": "forbidden";
    1435        3192 :       GEN x = gel(e,3);
    1436        3192 :       GEN y = gel(e,4);
    1437        3192 :       switch(*op)
    1438             :       {
    1439          14 :       case '+': f = "addition"; break;
    1440         119 :       case '*': f = "multiplication"; break;
    1441        2394 :       case '/': case '%': case '\\': f = "division"; break;
    1442           0 :       case '=': op = "-->"; f = "assignment"; break;
    1443         665 :       default:  f = op; op = ","; break;
    1444             :       }
    1445        3192 :       v = pari_sprintf("%s %s %s %s %s.", what,f,type_dim(x),op,type_dim(y));
    1446        3192 :       set_avma(av); return v;
    1447             :     }
    1448             :   case e_COMPONENT:
    1449             :     {
    1450         203 :       const char *f= GSTR(gel(e,2));
    1451         203 :       const char *op= GSTR(gel(e,3));
    1452         203 :       GEN l = gel(e,4);
    1453         203 :       if (!*f)
    1454         147 :         return pari_sprintf("non-existent component: index %s %Ps",op,l);
    1455          56 :       return pari_sprintf("non-existent component in %s: index %s %Ps",f,op,l);
    1456             :     }
    1457             :   case e_DOMAIN:
    1458             :     {
    1459        1905 :       const char *f = GSTR(gel(e,2));
    1460        1905 :       const char *v = GSTR(gel(e,3));
    1461        1905 :       const char *op= GSTR(gel(e,4));
    1462        1905 :       GEN l = gel(e,5);
    1463        1905 :       if (!*op)
    1464          28 :         return pari_sprintf("domain error in %s: %s out of range",f,v);
    1465        1877 :       return pari_sprintf("domain error in %s: %s %s %Ps",f,v,op,l);
    1466             :     }
    1467             :   case e_PRIORITY:
    1468             :     {
    1469         126 :       const char *f = GSTR(gel(e,2));
    1470         126 :       long vx = gvar(gel(e,3));
    1471         126 :       const char *op= GSTR(gel(e,4));
    1472         126 :       long v = itos(gel(e,5));
    1473         126 :       return pari_sprintf("incorrect priority in %s: variable %Ps %s %Ps",f,
    1474             :              pol_x(vx), op, pol_x(v));
    1475             :     }
    1476             :   case e_OVERFLOW:
    1477          91 :     return pari_sprintf("overflow in %Ps.", gel(e,2));
    1478             :   case e_PREC:
    1479         210 :     return pari_sprintf("precision too low in %Ps.", gel(e,2));
    1480             :   case e_PRIME:
    1481         140 :     return pari_sprintf("not a prime number in %Ps: %s.",
    1482         140 :                         gel(e,2), gdisplay(gel(e,3)));
    1483             :   case e_ROOTS0:
    1484          49 :     return pari_sprintf("zero polynomial in %Ps.", gel(e,2));
    1485             :   case e_SQRTN:
    1486         154 :     return pari_sprintf("not an n-th power residue in %Ps: %s.",
    1487         154 :                         gel(e,2), gdisplay(gel(e,3)));
    1488             :   case e_STACK:
    1489             :   case e_STACKTHREAD:
    1490             :     {
    1491           7 :       const char *stack = numerr == e_STACK? "PARI": "thread";
    1492           7 :       const char *var = numerr == e_STACK? "parisizemax": "threadsizemax";
    1493           7 :       size_t rsize = numerr == e_STACKTHREAD && GP_DATA->threadsize ?
    1494           7 :                                 GP_DATA->threadsize: pari_mainstack->rsize;
    1495           7 :       size_t vsize = numerr == e_STACK? pari_mainstack->vsize:
    1496           0 :                                         GP_DATA->threadsizemax;
    1497           7 :       char *buf = (char *) pari_malloc(512*sizeof(char));
    1498           7 :       if (vsize)
    1499             :       {
    1500           0 :         sprintf(buf, "the %s stack overflows !\n"
    1501             :             "  current stack size: %lu (%.3f Mbytes)\n"
    1502             :             "  [hint] you can increase '%s' using default()\n",
    1503           0 :             stack, (ulong)vsize, (double)vsize/1048576., var);
    1504             :       }
    1505             :       else
    1506             :       {
    1507           7 :         sprintf(buf, "the %s stack overflows !\n"
    1508             :             "  current stack size: %lu (%.3f Mbytes)\n"
    1509             :             "  [hint] set '%s' to a non-zero value in your GPRC\n",
    1510           7 :             stack, (ulong)rsize, (double)rsize/1048576., var);
    1511             :       }
    1512           7 :       return buf;
    1513             :     }
    1514             :   case e_SYNTAX:
    1515           0 :     return pari_strdup(GSTR(gel(e,2)));
    1516             :   case e_TYPE:
    1517        9452 :     return pari_sprintf("incorrect type in %Ps (%s).",
    1518        9452 :                         gel(e,2), type_name(typ(gel(e,3))));
    1519             :   case e_USER:
    1520          14 :     return pari_sprint0("user error: ", gel(e,2), f_RAW);
    1521             :   case e_VAR:
    1522             :     {
    1523         203 :       GEN x = gel(e,3), y = gel(e,4);
    1524         609 :       return pari_sprintf("inconsistent variables in %Ps, %Ps != %Ps.",
    1525         609 :                           gel(e,2), pol_x(varn(x)), pol_x(varn(y)));
    1526             :     }
    1527             :   }
    1528             :   return NULL; /*LCOV_EXCL_LINE*/
    1529             : }
    1530             : 
    1531             : static int
    1532        8842 : pari_err_display(GEN err)
    1533             : {
    1534        8842 :   long numerr=err_get_num(err);
    1535        8842 :   err_init();
    1536        8842 :   if (numerr==e_SYNTAX)
    1537             :   {
    1538          65 :     const char *msg = GSTR(gel(err,2));
    1539          65 :     const char *s     = (const char *) gmael(err,3,1);
    1540          65 :     const char *entry = (const char *) gmael(err,3,2);
    1541          65 :     print_errcontext(pariErr, msg, s, entry);
    1542             :   }
    1543             :   else
    1544             :   {
    1545             :     char *s;
    1546        8777 :     closure_err(0);
    1547        8777 :     err_init_msg(numerr==e_USER);
    1548        8777 :     s = pari_err2str(err); pariErr->puts(s); pari_free(s);
    1549        8777 :     if (numerr==e_NOTFUNC)
    1550             :     {
    1551        2723 :       GEN fun = gel(err,2);
    1552        2723 :       if (gequalX(fun))
    1553             :       {
    1554        2723 :         entree *ep = varentries[varn(fun)];
    1555        2723 :         const char *t = ep->name;
    1556        2723 :         if (cb_pari_whatnow) cb_pari_whatnow(pariErr,t,1);
    1557             :       }
    1558             :     }
    1559             :   }
    1560        8828 :   out_term_color(pariErr, c_NONE);
    1561        8828 :   pariErr->flush(); return 0;
    1562             : }
    1563             : 
    1564             : void
    1565       43536 : pari_err(int numerr, ...)
    1566             : {
    1567             :   va_list ap;
    1568             :   GEN E;
    1569             : 
    1570       43536 :   va_start(ap,numerr);
    1571             : 
    1572       43536 :   if (numerr)
    1573       43520 :     E = pari_err2GEN(numerr,ap);
    1574             :   else
    1575             :   {
    1576          16 :     E = va_arg(ap,GEN);
    1577          16 :     numerr = err_get_num(E);
    1578             :   }
    1579       43536 :   global_err_data = E;
    1580       43536 :   if (*iferr_env) longjmp(*iferr_env, numerr);
    1581        8844 :   mt_err_recover(numerr);
    1582        8842 :   va_end(ap);
    1583       17670 :   if (cb_pari_err_handle &&
    1584        8842 :       cb_pari_err_handle(E)) return;
    1585       17647 :   if (cb_pari_handle_exception &&
    1586        8826 :       cb_pari_handle_exception(numerr)) return;
    1587        8821 :   err_recover(numerr);
    1588             : }
    1589             : 
    1590             : GEN
    1591       69373 : pari_err_last(void) { return global_err_data; }
    1592             : 
    1593             : const char *
    1594       26947 : numerr_name(long numerr)
    1595             : {
    1596       26947 :   switch ((enum err_list) numerr)
    1597             :   {
    1598           0 :   case e_ALARM:    return "e_ALARM";
    1599           0 :   case e_ARCH:     return "e_ARCH";
    1600           0 :   case e_BUG:      return "e_BUG";
    1601           0 :   case e_COMPONENT: return "e_COMPONENT";
    1602           0 :   case e_CONSTPOL: return "e_CONSTPOL";
    1603           0 :   case e_COPRIME:  return "e_COPRIME";
    1604           0 :   case e_DIM:      return "e_DIM";
    1605          56 :   case e_DOMAIN:   return "e_DOMAIN";
    1606           0 :   case e_FILE:     return "e_FILE";
    1607           0 :   case e_FILEDESC: return "e_FILEDESC";
    1608           7 :   case e_FLAG:     return "e_FLAG";
    1609          28 :   case e_IMPL:     return "e_IMPL";
    1610       19093 :   case e_INV:      return "e_INV";
    1611           0 :   case e_IRREDPOL: return "e_IRREDPOL";
    1612           0 :   case e_MAXPRIME: return "e_MAXPRIME";
    1613           0 :   case e_MEM:      return "e_MEM";
    1614           0 :   case e_MISC:     return "e_MISC";
    1615           0 :   case e_MODULUS:  return "e_MODULUS";
    1616           0 :   case e_NONE:     return "e_NONE";
    1617           0 :   case e_NOTFUNC:  return "e_NOTFUNC";
    1618           0 :   case e_OP:       return "e_OP";
    1619           0 :   case e_OVERFLOW: return "e_OVERFLOW";
    1620           0 :   case e_PACKAGE:  return "e_PACKAGE";
    1621           0 :   case e_PREC:     return "e_PREC";
    1622           0 :   case e_PRIME:    return "e_PRIME";
    1623          49 :   case e_PRIORITY: return "e_PRIORITY";
    1624           0 :   case e_ROOTS0:   return "e_ROOTS0";
    1625           0 :   case e_SQRTN:    return "e_SQRTN";
    1626           0 :   case e_STACK:    return "e_STACK";
    1627           0 :   case e_SYNTAX:   return "e_SYNTAX";
    1628           0 :   case e_STACKTHREAD:   return "e_STACKTHREAD";
    1629           0 :   case e_TYPE2:    return "e_TYPE2";
    1630        7714 :   case e_TYPE:     return "e_TYPE";
    1631           0 :   case e_USER:     return "e_USER";
    1632           0 :   case e_VAR:      return "e_VAR";
    1633             :   }
    1634           0 :   return "invalid error number";
    1635             : }
    1636             : 
    1637             : long
    1638          21 : name_numerr(const char *s)
    1639             : {
    1640          21 :   if (!strcmp(s,"e_ALARM"))    return e_ALARM;
    1641          21 :   if (!strcmp(s,"e_ARCH"))     return e_ARCH;
    1642          21 :   if (!strcmp(s,"e_BUG"))      return e_BUG;
    1643          21 :   if (!strcmp(s,"e_COMPONENT")) return e_COMPONENT;
    1644          21 :   if (!strcmp(s,"e_CONSTPOL")) return e_CONSTPOL;
    1645          21 :   if (!strcmp(s,"e_COPRIME"))  return e_COPRIME;
    1646          21 :   if (!strcmp(s,"e_DIM"))      return e_DIM;
    1647          21 :   if (!strcmp(s,"e_DOMAIN"))   return e_DOMAIN;
    1648          21 :   if (!strcmp(s,"e_FILE"))     return e_FILE;
    1649          21 :   if (!strcmp(s,"e_FILEDESC")) return e_FILEDESC;
    1650          21 :   if (!strcmp(s,"e_FLAG"))     return e_FLAG;
    1651          21 :   if (!strcmp(s,"e_IMPL"))     return e_IMPL;
    1652          21 :   if (!strcmp(s,"e_INV"))      return e_INV;
    1653           0 :   if (!strcmp(s,"e_IRREDPOL")) return e_IRREDPOL;
    1654           0 :   if (!strcmp(s,"e_MAXPRIME")) return e_MAXPRIME;
    1655           0 :   if (!strcmp(s,"e_MEM"))      return e_MEM;
    1656           0 :   if (!strcmp(s,"e_MISC"))     return e_MISC;
    1657           0 :   if (!strcmp(s,"e_MODULUS"))  return e_MODULUS;
    1658           0 :   if (!strcmp(s,"e_NONE"))     return e_NONE;
    1659           0 :   if (!strcmp(s,"e_NOTFUNC"))  return e_NOTFUNC;
    1660           0 :   if (!strcmp(s,"e_OP"))       return e_OP;
    1661           0 :   if (!strcmp(s,"e_OVERFLOW")) return e_OVERFLOW;
    1662           0 :   if (!strcmp(s,"e_PACKAGE"))  return e_PACKAGE;
    1663           0 :   if (!strcmp(s,"e_PREC"))     return e_PREC;
    1664           0 :   if (!strcmp(s,"e_PRIME"))    return e_PRIME;
    1665           0 :   if (!strcmp(s,"e_PRIORITY")) return e_PRIORITY;
    1666           0 :   if (!strcmp(s,"e_ROOTS0"))   return e_ROOTS0;
    1667           0 :   if (!strcmp(s,"e_SQRTN"))    return e_SQRTN;
    1668           0 :   if (!strcmp(s,"e_STACK"))    return e_STACK;
    1669           0 :   if (!strcmp(s,"e_SYNTAX"))   return e_SYNTAX;
    1670           0 :   if (!strcmp(s,"e_TYPE"))     return e_TYPE;
    1671           0 :   if (!strcmp(s,"e_TYPE2"))    return e_TYPE2;
    1672           0 :   if (!strcmp(s,"e_USER"))     return e_USER;
    1673           0 :   if (!strcmp(s,"e_VAR"))      return e_VAR;
    1674           0 :   pari_err(e_MISC,"unknown error name");
    1675             :   return -1; /* LCOV_EXCL_LINE */
    1676             : }
    1677             : 
    1678             : GEN
    1679       26947 : errname(GEN err)
    1680             : {
    1681       26947 :   if (typ(err)!=t_ERROR) pari_err_TYPE("errname",err);
    1682       26947 :   return strtoGENstr(numerr_name(err_get_num(err)));
    1683             : }
    1684             : 
    1685             : /* Try f (trapping error e), recover using r (break_loop, if NULL) */
    1686             : GEN
    1687          21 : trap0(const char *e, GEN r, GEN f)
    1688             : {
    1689          21 :   long numerr = CATCH_ALL;
    1690             :   GEN x;
    1691          21 :   if (!e || !*e) numerr = CATCH_ALL;
    1692          21 :   else numerr = name_numerr(e);
    1693          21 :   if (!f) {
    1694           0 :     pari_warn(warner,"default handlers are no longer supported --> ignored");
    1695           0 :     return gnil;
    1696             :   }
    1697          21 :   x = closure_trapgen(f, numerr);
    1698          14 :   if (x == (GEN)1L) x = r? closure_evalgen(r): gnil;
    1699          14 :   return x;
    1700             : }
    1701             : 
    1702             : /*******************************************************************/
    1703             : /*                                                                */
    1704             : /*                       CLONING & COPY                            */
    1705             : /*                  Replicate an existing GEN                      */
    1706             : /*                                                                 */
    1707             : /*******************************************************************/
    1708             : /* lontyp[tx] = 0 (non recursive type) or number of codewords for type tx */
    1709             : const  long lontyp[] = { 0,0,0,1,1,2,1,2,1,1, 2,2,0,1,1,1,1,1,1,1, 2,0,0,2,2,1 };
    1710             : 
    1711             : static GEN
    1712         697 : list_internal_copy(GEN z, long nmax)
    1713             : {
    1714             :   long i, l;
    1715             :   GEN a;
    1716         697 :   if (!z) return NULL;
    1717         560 :   l = lg(z);
    1718         560 :   a = newblock(nmax+1);
    1719         560 :   for (i = 1; i < l; i++) gel(a,i) = gel(z,i)? gclone(gel(z,i)): gen_0;
    1720         560 :   a[0] = z[0]; return a;
    1721             : }
    1722             : 
    1723             : static void
    1724         697 : listassign(GEN x, GEN y)
    1725             : {
    1726         697 :   long nmax = list_nmax(x);
    1727         697 :   GEN L = list_data(x);
    1728         697 :   if (!nmax && L) nmax = lg(L) + 32; /* not malloc'ed yet */
    1729         697 :   y[1] = evaltyp(list_typ(x))|evallg(nmax);
    1730         697 :   list_data(y) = list_internal_copy(L, nmax);
    1731         697 : }
    1732             : 
    1733             : /* transform a non-malloced list (e.g. from gtolist or gtomap) to a malloced
    1734             :  * list suitable for listput */
    1735             : GEN
    1736           0 : listinit(GEN x)
    1737             : {
    1738           0 :   GEN y = cgetg(3, t_LIST);
    1739           0 :   listassign(x, y); return y;
    1740             : }
    1741             : 
    1742             : /* copy list on the PARI stack */
    1743             : GEN
    1744         207 : listcopy(GEN x)
    1745             : {
    1746         207 :   GEN y = mklist(), L = list_data(x);
    1747         207 :   if (L) list_data(y) = gcopy(L);
    1748         207 :   y[1] = evaltyp(list_typ(x));
    1749         207 :   return y;
    1750             : }
    1751             : 
    1752             : GEN
    1753  3305388303 : gcopy(GEN x)
    1754             : {
    1755  3305388303 :   long tx = typ(x), lx, i;
    1756             :   GEN y;
    1757  3305388303 :   switch(tx)
    1758             :   { /* non recursive types */
    1759  2996893273 :     case t_INT: return signe(x)? icopy(x): gen_0;
    1760             :     case t_REAL:
    1761             :     case t_STR:
    1762   169626161 :     case t_VECSMALL: return leafcopy(x);
    1763             :     /* one more special case */
    1764         207 :     case t_LIST: return listcopy(x);
    1765             :   }
    1766   138868662 :   y = cgetg_copy(x, &lx);
    1767   138868671 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1768   138868671 :   for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1769   138868663 :   return y;
    1770             : }
    1771             : 
    1772             : /* as gcopy, but truncate to the first lx components if recursive type
    1773             :  * [ leaves use their own lg ]. No checks. */
    1774             : GEN
    1775         588 : gcopy_lg(GEN x, long lx)
    1776             : {
    1777         588 :   long tx = typ(x), i;
    1778             :   GEN y;
    1779         588 :   switch(tx)
    1780             :   { /* non recursive types */
    1781           0 :     case t_INT: return signe(x)? icopy(x): gen_0;
    1782             :     case t_REAL:
    1783             :     case t_STR:
    1784           0 :     case t_VECSMALL: return leafcopy(x);
    1785             :     /* one more special case */
    1786           0 :     case t_LIST: return listcopy(x);
    1787             :   }
    1788         588 :   y = cgetg(lx, tx);
    1789         588 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1790         588 :   for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1791         588 :   return y;
    1792             : }
    1793             : 
    1794             : /* cf cgetg_copy: "allocate" (by updating first codeword only) for subsequent
    1795             :  * copy of x, as if avma = *AVMA */
    1796             : INLINE GEN
    1797   349026633 : cgetg_copy_avma(GEN x, long *plx, pari_sp *AVMA) {
    1798             :   GEN z;
    1799   349026633 :   *plx = lg(x);
    1800   349026633 :   z = ((GEN)*AVMA) - *plx;
    1801   349026633 :   z[0] = x[0] & (TYPBITS|LGBITS);
    1802   349026633 :   *AVMA = (pari_sp)z; return z;
    1803             : }
    1804             : INLINE GEN
    1805         133 : cgetlist_avma(pari_sp *AVMA)
    1806             : {
    1807         133 :   GEN y = ((GEN)*AVMA) - 3;
    1808         133 :   y[0] = _evallg(3) | evaltyp(t_LIST);
    1809         133 :   *AVMA = (pari_sp)y; return y;
    1810             : }
    1811             : 
    1812             : /* copy x as if avma = *AVMA, update *AVMA */
    1813             : GEN
    1814  2826245549 : gcopy_avma(GEN x, pari_sp *AVMA)
    1815             : {
    1816  2826245549 :   long i, lx, tx = typ(x);
    1817             :   GEN y;
    1818             : 
    1819  2826245549 :   switch(typ(x))
    1820             :   { /* non recursive types */
    1821             :     case t_INT:
    1822  2690178063 :       if (lgefint(x) == 2) return gen_0;
    1823  2208330449 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1824  2208330444 :       return (GEN)*AVMA;
    1825             :     case t_REAL: case t_STR: case t_VECSMALL:
    1826    29076076 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1827    29076076 :       return (GEN)*AVMA;
    1828             : 
    1829             :     /* one more special case */
    1830             :     case t_LIST:
    1831         133 :       y = cgetlist_avma(AVMA);
    1832         133 :       listassign(x, y); return y;
    1833             : 
    1834             :   }
    1835   106991277 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1836   106991256 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1837   106991256 :   for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), AVMA);
    1838   106991311 :   return y;
    1839             : }
    1840             : 
    1841             : /* [copy_bin/bin_copy:] same as gcopy_avma but use NULL to code an exact 0, and
    1842             :  * make shallow copies of finalized t_LISTs */
    1843             : static GEN
    1844  1135071971 : gcopy_av0(GEN x, pari_sp *AVMA)
    1845             : {
    1846  1135071971 :   long i, lx, tx = typ(x);
    1847             :   GEN y;
    1848             : 
    1849  1135071971 :   switch(tx)
    1850             :   { /* non recursive types */
    1851             :     case t_INT:
    1852   752270709 :       if (!signe(x)) return NULL; /* special marker */
    1853   412184777 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1854   412184762 :       return (GEN)*AVMA;
    1855             :     case t_LIST:
    1856          49 :       if (list_data(x) && !list_nmax(x)) break; /* not finalized, need copy */
    1857             :       /* else finalized: shallow copy */
    1858             :     case t_REAL: case t_STR: case t_VECSMALL:
    1859   140765932 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1860   140765967 :       return (GEN)*AVMA;
    1861             :   }
    1862   242035330 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1863   242035297 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1864   242035297 :   for (; i<lx; i++) gel(y,i) = gcopy_av0(gel(x,i), AVMA);
    1865   242035498 :   return y;
    1866             : }
    1867             : 
    1868             : INLINE GEN
    1869           0 : icopy_avma_canon(GEN x, pari_sp AVMA)
    1870             : {
    1871           0 :   long i, lx = lgefint(x);
    1872           0 :   GEN y = ((GEN)AVMA) - lx;
    1873           0 :   y[0] = evaltyp(t_INT)|evallg(lx); /* kills isclone */
    1874           0 :   y[1] = x[1]; x = int_MSW(x);
    1875           0 :   for (i=2; i<lx; i++, x = int_precW(x)) y[i] = *x;
    1876           0 :   return y;
    1877             : }
    1878             : 
    1879             : /* [copy_bin_canon:] same as gcopy_av0, but copy integers in
    1880             :  * canonical (native kernel) form and make a full copy of t_LISTs */
    1881             : static GEN
    1882           0 : gcopy_av0_canon(GEN x, pari_sp *AVMA)
    1883             : {
    1884           0 :   long i, lx, tx = typ(x);
    1885             :   GEN y;
    1886             : 
    1887           0 :   switch(tx)
    1888             :   { /* non recursive types */
    1889             :     case t_INT:
    1890           0 :       if (!signe(x)) return NULL; /* special marker */
    1891           0 :       *AVMA = (pari_sp)icopy_avma_canon(x, *AVMA);
    1892           0 :       return (GEN)*AVMA;
    1893             :     case t_REAL: case t_STR: case t_VECSMALL:
    1894           0 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1895           0 :       return (GEN)*AVMA;
    1896             : 
    1897             :     /* one more special case */
    1898             :     case t_LIST:
    1899             :     {
    1900           0 :       long t = list_typ(x);
    1901           0 :       GEN y = cgetlist_avma(AVMA), z = list_data(x);
    1902           0 :       if (z) {
    1903           0 :         list_data(y) = gcopy_av0_canon(z, AVMA);
    1904           0 :         y[1] = evaltyp(t)|evallg(lg(z)-1);
    1905             :       } else {
    1906           0 :         list_data(y) = NULL;
    1907           0 :         y[1] = evaltyp(t);
    1908             :       }
    1909           0 :       return y;
    1910             :     }
    1911             :   }
    1912           0 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1913           0 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1914           0 :   for (; i<lx; i++) gel(y,i) = gcopy_av0_canon(gel(x,i), AVMA);
    1915           0 :   return y;
    1916             : }
    1917             : 
    1918             : /* [copy_bin/bin_copy:] size (number of words) required for
    1919             :  * gcopy_av0_canon(x) */
    1920             : static long
    1921           0 : taille0_canon(GEN x)
    1922             : {
    1923           0 :   long i,n,lx, tx = typ(x);
    1924           0 :   switch(tx)
    1925             :   { /* non recursive types */
    1926           0 :     case t_INT: return signe(x)? lgefint(x): 0;
    1927             :     case t_REAL:
    1928             :     case t_STR:
    1929           0 :     case t_VECSMALL: return lg(x);
    1930             : 
    1931             :     /* one more special case */
    1932             :     case t_LIST:
    1933             :     {
    1934           0 :       GEN L = list_data(x);
    1935           0 :       return L? 3 + taille0_canon(L): 3;
    1936             :     }
    1937             :   }
    1938           0 :   n = lx = lg(x);
    1939           0 :   for (i=lontyp[tx]; i<lx; i++) n += taille0_canon(gel(x,i));
    1940           0 :   return n;
    1941             : }
    1942             : 
    1943             : /* [copy_bin/bin_copy:] size (number of words) required for gcopy_av0(x) */
    1944             : static long
    1945  1135073906 : taille0(GEN x)
    1946             : {
    1947  1135073906 :   long i,n,lx, tx = typ(x);
    1948  1135073906 :   switch(tx)
    1949             :   { /* non recursive types */
    1950             :     case t_INT:
    1951   752272215 :       lx = lgefint(x);
    1952   752272215 :       return lx == 2? 0: lx;
    1953             :     case t_LIST:
    1954             :     {
    1955          49 :       GEN L = list_data(x);
    1956          49 :       if (L && !list_nmax(x)) break; /* not finalized, deep copy */
    1957             :     }
    1958             :     /* else finalized: shallow */
    1959             :     case t_REAL:
    1960             :     case t_STR:
    1961             :     case t_VECSMALL:
    1962   140766090 :       return lg(x);
    1963             :   }
    1964   242035601 :   n = lx = lg(x);
    1965   242035601 :   for (i=lontyp[tx]; i<lx; i++) n += taille0(gel(x,i));
    1966   242035633 :   return n;
    1967             : }
    1968             : 
    1969             : static long
    1970  2895704646 : gsizeclone_i(GEN x)
    1971             : {
    1972  2895704646 :   long i,n,lx, tx = typ(x);
    1973  2895704646 :   switch(tx)
    1974             :   { /* non recursive types */
    1975  2690583699 :     case t_INT: lx = lgefint(x); return lx == 2? 0: lx;;
    1976             :     case t_REAL:
    1977             :     case t_STR:
    1978    36353909 :     case t_VECSMALL: return lg(x);
    1979             : 
    1980         697 :     case t_LIST: return 3;
    1981             :     default:
    1982   168766341 :       n = lx = lg(x);
    1983   168766341 :       for (i=lontyp[tx]; i<lx; i++) n += gsizeclone_i(gel(x,i));
    1984   168766335 :       return n;
    1985             :   }
    1986             : }
    1987             : 
    1988             : /* #words needed to clone x; t_LIST is a special case since list_data() is
    1989             :  * malloc'ed later, in list_internal_copy() */
    1990             : static long
    1991   212102303 : gsizeclone(GEN x) { return (typ(x) == t_INT)? lgefint(x): gsizeclone_i(x); }
    1992             : 
    1993             : long
    1994         147 : gsizeword(GEN x)
    1995             : {
    1996             :   GEN L;
    1997         147 :   if (typ(x) != t_LIST) return gsizeclone(x);
    1998             :   /* For t_LIST, return the actual list size, gsizeclone() is always 3 */
    1999           0 :   L = list_data(x);
    2000           0 :   return L? 3 + gsizeclone(L): 3;
    2001             : }
    2002             : long
    2003         147 : gsizebyte(GEN x) { return gsizeword(x) * sizeof(long); }
    2004             : 
    2005             : /* return a clone of x structured as a gcopy */
    2006             : GENbin*
    2007    57043107 : copy_bin(GEN x)
    2008             : {
    2009    57043107 :   long t = taille0(x);
    2010    57043081 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    2011    57043485 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    2012    57043381 :   p->rebase = &shiftaddress;
    2013    57043381 :   p->len = t;
    2014    57043381 :   p->x   = gcopy_av0(x, &AVMA);
    2015    57043225 :   p->base= (GEN)AVMA; return p;
    2016             : }
    2017             : 
    2018             : /* same, writing t_INT in canonical native form */
    2019             : GENbin*
    2020           0 : copy_bin_canon(GEN x)
    2021             : {
    2022           0 :   long t = taille0_canon(x);
    2023           0 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    2024           0 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    2025           0 :   p->rebase = &shiftaddress_canon;
    2026           0 :   p->len = t;
    2027           0 :   p->x   = gcopy_av0_canon(x, &AVMA);
    2028           0 :   p->base= (GEN)AVMA; return p;
    2029             : }
    2030             : 
    2031             : GEN
    2032   212102159 : gclone(GEN x)
    2033             : {
    2034   212102159 :   long i,lx,tx = typ(x), t = gsizeclone(x);
    2035   212102157 :   GEN y = newblock(t);
    2036   212102166 :   switch(tx)
    2037             :   { /* non recursive types */
    2038             :     case t_INT:
    2039   144677321 :       lx = lgefint(x);
    2040   144677321 :       y[0] = evaltyp(t_INT)|evallg(lx);
    2041   144677317 :       for (i=1; i<lx; i++) y[i] = x[i];
    2042   144677317 :       break;
    2043             :     case t_REAL:
    2044             :     case t_STR:
    2045             :     case t_VECSMALL:
    2046     6176201 :       lx = lg(x);
    2047     6176201 :       for (i=0; i<lx; i++) y[i] = x[i];
    2048     6176201 :       break;
    2049             : 
    2050             :     /* one more special case */
    2051             :     case t_LIST:
    2052         564 :       y[0] = evaltyp(t_LIST)|_evallg(3);
    2053         564 :       listassign(x, y);
    2054         564 :       break;
    2055             :     default: {
    2056    61248080 :       pari_sp AVMA = (pari_sp)(y + t);
    2057    61248080 :       lx = lg(x);
    2058    61248080 :       y[0] = x[0];
    2059    61248080 :       if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    2060    61248080 :       for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), &AVMA);
    2061             :     }
    2062             :   }
    2063   212102163 :   setisclone(y); return y;
    2064             : }
    2065             : 
    2066             : void
    2067   794987196 : shiftaddress(GEN x, long dec)
    2068             : {
    2069   794987196 :   long i, lx, tx = typ(x);
    2070   794987196 :   if (is_recursive_t(tx))
    2071             :   {
    2072   242035682 :     if (tx == t_LIST)
    2073             :     {
    2074          49 :       if (!list_data(x) || list_nmax(x)) return; /* empty or finalized */
    2075             :       /* not finalized, update pointers  */
    2076             :     }
    2077   242035640 :     lx = lg(x);
    2078  1320066171 :     for (i=lontyp[tx]; i<lx; i++) {
    2079  1078030535 :       if (!x[i]) gel(x,i) = gen_0;
    2080             :       else
    2081             :       {
    2082   737951200 :         x[i] += dec;
    2083   737951200 :         shiftaddress(gel(x,i), dec);
    2084             :       }
    2085             :     }
    2086             :   }
    2087             : }
    2088             : 
    2089             : void
    2090           0 : shiftaddress_canon(GEN x, long dec)
    2091             : {
    2092           0 :   long i, lx, tx = typ(x);
    2093           0 :   switch(tx)
    2094             :   { /* non recursive types */
    2095             :     case t_INT: {
    2096             :       GEN y;
    2097           0 :       lx = lgefint(x); if (lx <= 3) return;
    2098           0 :       y = x + 2;
    2099           0 :       x = int_MSW(x);  if (x == y) return;
    2100           0 :       while (x > y) { lswap(*x, *y); x = int_precW(x); y++; }
    2101           0 :       break;
    2102             :     }
    2103             :     case t_REAL:
    2104             :     case t_STR:
    2105             :     case t_VECSMALL:
    2106           0 :       break;
    2107             : 
    2108             :     /* one more special case */
    2109             :     case t_LIST: {
    2110           0 :       GEN Lx = list_data(x);
    2111           0 :       if (Lx) {
    2112           0 :         pari_sp av = avma;
    2113           0 :         GEN L = (GEN)((long)Lx+dec);
    2114           0 :         shiftaddress_canon(L, dec);
    2115           0 :         list_data(x) = list_internal_copy(L, lg(L)); set_avma(av);
    2116             :       }
    2117           0 :       break;
    2118             :     }
    2119             :     default:
    2120           0 :       lx = lg(x);
    2121           0 :       for (i=lontyp[tx]; i<lx; i++) {
    2122           0 :         if (!x[i]) gel(x,i) = gen_0;
    2123             :         else
    2124             :         {
    2125           0 :           x[i] += dec;
    2126           0 :           shiftaddress_canon(gel(x,i), dec);
    2127             :         }
    2128             :       }
    2129             :   }
    2130             : }
    2131             : 
    2132             : /********************************************************************/
    2133             : /**                                                                **/
    2134             : /**                INSERT DYNAMIC OBJECT IN STRUCTURE              **/
    2135             : /**                                                                **/
    2136             : /********************************************************************/
    2137             : GEN
    2138          35 : obj_reinit(GEN S)
    2139             : {
    2140          35 :   GEN s, T = leafcopy(S);
    2141          35 :   long a = lg(T)-1;
    2142          35 :   s = gel(T,a);
    2143          35 :   gel(T,a) = zerovec(lg(s)-1);
    2144          35 :   return T;
    2145             : }
    2146             : 
    2147             : GEN
    2148     1208538 : obj_init(long d, long n)
    2149             : {
    2150     1208538 :   GEN S = cgetg(d+2, t_VEC);
    2151     1208538 :   gel(S, d+1) = zerovec(n);
    2152     1208539 :   return S;
    2153             : }
    2154             : /* insert O in S [last position] at position K, return it */
    2155             : GEN
    2156     1126708 : obj_insert(GEN S, long K, GEN O)
    2157     1126708 : { return obj_insert_shallow(S, K, gclone(O)); }
    2158             : /* as obj_insert. WITHOUT cloning (for libpari, when creating a *new* obj
    2159             :  * from an existing one) */
    2160             : GEN
    2161     1130803 : obj_insert_shallow(GEN S, long K, GEN O)
    2162             : {
    2163     1130803 :   GEN o, v = gel(S, lg(S)-1);
    2164     1130803 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_insert", S);
    2165     1130803 :   o = gel(v,K);
    2166     1130803 :   gel(v,K) = O; /*SIGINT: before unclone(o)*/
    2167     1130803 :   if (isclone(o)) gunclone(o);
    2168     1130803 :   return gel(v,K);
    2169             : }
    2170             : 
    2171             : /* Does S [last position] contain data at position K ? Return it, or NULL */
    2172             : GEN
    2173     2615687 : obj_check(GEN S, long K)
    2174             : {
    2175     2615687 :   GEN O, v = gel(S, lg(S)-1);
    2176     2615687 :   if (typ(v) != t_VEC || K >= lg(v)) pari_err_TYPE("obj_check", S);
    2177     2615687 :   O = gel(v,K); return isintzero(O)? NULL: O;
    2178             : }
    2179             : 
    2180             : GEN
    2181      851412 : obj_checkbuild(GEN S, long tag, GEN (*build)(GEN))
    2182             : {
    2183      851412 :   GEN O = obj_check(S, tag);
    2184      851412 :   if (!O)
    2185      658923 :   { pari_sp av = avma; O = obj_insert(S, tag, build(S)); set_avma(av); }
    2186      851405 :   return O;
    2187             : }
    2188             : 
    2189             : GEN
    2190       83448 : obj_checkbuild_prec(GEN S, long tag, GEN (*build)(GEN,long),
    2191             :   long (*pr)(GEN), long prec)
    2192             : {
    2193       83448 :   pari_sp av = avma;
    2194       83448 :   GEN w = obj_check(S, tag);
    2195       83448 :   if (!w || pr(w) < prec) w = obj_insert(S, tag, build(S, prec));
    2196       83448 :   set_avma(av); return gcopy(w);
    2197             : }
    2198             : GEN
    2199       12013 : obj_checkbuild_realprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2200       12013 : { return obj_checkbuild_prec(S,tag,build,gprecision,prec); }
    2201             : GEN
    2202         497 : obj_checkbuild_padicprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2203         497 : { return obj_checkbuild_prec(S,tag,build,padicprec_relative,prec); }
    2204             : 
    2205             : /* Reset S [last position], freeing all clones */
    2206             : void
    2207       11886 : obj_free(GEN S)
    2208             : {
    2209       11886 :   GEN v = gel(S, lg(S)-1);
    2210             :   long i;
    2211       11886 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_free", S);
    2212       65198 :   for (i = 1; i < lg(v); i++)
    2213             :   {
    2214       53312 :     GEN o = gel(v,i);
    2215       53312 :     gel(v,i) = gen_0;
    2216       53312 :     gunclone_deep(o);
    2217             :   }
    2218       11886 : }
    2219             : 
    2220             : /*******************************************************************/
    2221             : /*                                                                 */
    2222             : /*                         STACK MANAGEMENT                        */
    2223             : /*                                                                 */
    2224             : /*******************************************************************/
    2225             : INLINE void
    2226  2120730986 : dec_gerepile(pari_sp *x, pari_sp av0, pari_sp av, pari_sp tetpil, size_t dec)
    2227             : {
    2228  2120730986 :   if (*x < av && *x >= av0)
    2229             :   { /* update address if in stack */
    2230  1737790511 :     if (*x < tetpil) *x += dec;
    2231           0 :     else pari_err_BUG("gerepile, significant pointers lost");
    2232             :   }
    2233  2120730986 : }
    2234             : 
    2235             : void
    2236      186852 : gerepileallsp(pari_sp av, pari_sp tetpil, int n, ...)
    2237             : {
    2238      186852 :   const pari_sp av0 = avma;
    2239      186852 :   const size_t dec = av-tetpil;
    2240             :   int i;
    2241      186852 :   va_list a; va_start(a, n);
    2242      186852 :   (void)gerepile(av,tetpil,NULL);
    2243      186852 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)va_arg(a,GEN*), av0,av,tetpil,dec);
    2244      186852 :   va_end(a);
    2245      186852 : }
    2246             : 
    2247             : /* Takes an array of pointers to GENs, of length n.
    2248             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2249             : void
    2250     5870417 : gerepilemanysp(pari_sp av, pari_sp tetpil, GEN* gptr[], int n)
    2251             : {
    2252     5870417 :   const pari_sp av0 = avma;
    2253     5870417 :   const size_t dec = av-tetpil;
    2254             :   int i;
    2255     5870417 :   (void)gerepile(av,tetpil,NULL);
    2256     5870417 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)gptr[i], av0, av, tetpil, dec);
    2257     5870417 : }
    2258             : 
    2259             : /* Takes an array of GENs (cast to longs), of length n.
    2260             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2261             : void
    2262   131302414 : gerepilecoeffssp(pari_sp av, pari_sp tetpil, long *g, int n)
    2263             : {
    2264   131302414 :   const pari_sp av0 = avma;
    2265   131302414 :   const size_t dec = av-tetpil;
    2266             :   int i;
    2267   131302414 :   (void)gerepile(av,tetpil,NULL);
    2268   131302414 :   for (i=0; i<n; i++,g++) dec_gerepile((pari_sp*)g, av0, av, tetpil, dec);
    2269   131302414 : }
    2270             : 
    2271             : static int
    2272           0 : dochk_gerepileupto(GEN av, GEN x)
    2273             : {
    2274             :   long i,lx,tx;
    2275           0 :   if (!isonstack(x)) return 1;
    2276           0 :   if (x > av)
    2277             :   {
    2278           0 :     pari_warn(warner,"bad object %Ps",x);
    2279           0 :     return 0;
    2280             :   }
    2281           0 :   tx = typ(x);
    2282           0 :   if (! is_recursive_t(tx)) return 1;
    2283             : 
    2284           0 :   lx = lg(x);
    2285           0 :   for (i=lontyp[tx]; i<lx; i++)
    2286           0 :     if (!dochk_gerepileupto(av, gel(x,i)))
    2287             :     {
    2288           0 :       pari_warn(warner,"bad component %ld in object %Ps",i,x);
    2289           0 :       return 0;
    2290             :     }
    2291           0 :   return 1;
    2292             : }
    2293             : /* check that x and all its components are out of stack, or have been
    2294             :  * created after av */
    2295             : int
    2296           0 : chk_gerepileupto(GEN x) { return dochk_gerepileupto(x, x); }
    2297             : 
    2298             : /* print stack between avma & av */
    2299             : void
    2300           0 : dbg_gerepile(pari_sp av)
    2301             : {
    2302           0 :   GEN x = (GEN)avma;
    2303           0 :   while (x < (GEN)av)
    2304             :   {
    2305           0 :     const long tx = typ(x), lx = lg(x);
    2306             :     GEN *a;
    2307             : 
    2308           0 :     pari_printf(" [%ld] %Ps:", x - (GEN)avma, x);
    2309           0 :     if (! is_recursive_t(tx)) { pari_putc('\n'); x += lx; continue; }
    2310           0 :     a = (GEN*)x + lontyp[tx]; x += lx;
    2311           0 :     for (  ; a < (GEN*)x; a++)
    2312             :     {
    2313           0 :       if (*a == gen_0)
    2314           0 :         pari_puts("  gen_0");
    2315           0 :       else if (*a == gen_1)
    2316           0 :         pari_puts("  gen_1");
    2317           0 :       else if (*a == gen_m1)
    2318           0 :         pari_puts("  gen_m1");
    2319           0 :       else if (*a == gen_2)
    2320           0 :         pari_puts("  gen_2");
    2321           0 :       else if (*a == gen_m2)
    2322           0 :         pari_puts("  gen_m2");
    2323           0 :       else if (*a == ghalf)
    2324           0 :         pari_puts("  ghalf");
    2325           0 :       else if (isclone(*a))
    2326           0 :         pari_printf("  %Ps (clone)", *a);
    2327             :       else
    2328           0 :         pari_printf("  %Ps [%ld]", *a, *a - (GEN)avma);
    2329           0 :       if (a+1 < (GEN*)x) pari_putc(',');
    2330             :     }
    2331           0 :     pari_printf("\n");
    2332             :   }
    2333           0 : }
    2334             : void
    2335           0 : dbg_gerepileupto(GEN q)
    2336             : {
    2337           0 :   err_printf("%Ps:\n", q);
    2338           0 :   dbg_gerepile((pari_sp) (q+lg(q)));
    2339           0 : }
    2340             : 
    2341             : GEN
    2342   482428430 : gerepile(pari_sp av, pari_sp tetpil, GEN q)
    2343             : {
    2344   482428430 :   const size_t dec = av - tetpil;
    2345   482428430 :   const pari_sp av0 = avma;
    2346             :   GEN x, a;
    2347             : 
    2348   482428430 :   if (dec == 0) return q;
    2349   405234499 :   if ((long)dec < 0) pari_err(e_MISC,"lbot>ltop in gerepile");
    2350             : 
    2351             :   /* dec_gerepile(&q, av0, av, tetpil, dec), saving 1 comparison */
    2352   405242684 :   if (q >= (GEN)av0 && q < (GEN)tetpil)
    2353   271669355 :     q = (GEN) (((pari_sp)q) + dec);
    2354             : 
    2355   405242684 :   for (x = (GEN)av, a = (GEN)tetpil; a > (GEN)av0; ) *--x = *--a;
    2356   405242684 :   avma = (pari_sp)x;
    2357  2880463710 :   while (x < (GEN)av)
    2358             :   {
    2359  2069981835 :     const long tx = typ(x), lx = lg(x);
    2360             : 
    2361  2069981835 :     if (! is_recursive_t(tx)) { x += lx; continue; }
    2362   482085006 :     a = x + lontyp[tx]; x += lx;
    2363   482085006 :     for (  ; a < x; a++) dec_gerepile((pari_sp*)a, av0, av, tetpil, dec);
    2364             :   }
    2365   405239191 :   return q;
    2366             : }
    2367             : 
    2368             : void
    2369           0 : fill_stack(void)
    2370             : {
    2371           0 :   GEN x = ((GEN)pari_mainstack->bot);
    2372           0 :   while (x < (GEN)avma) *x++ = 0xfefefefeUL;
    2373           0 : }
    2374             : 
    2375             : void
    2376           0 : debug_stack(void)
    2377             : {
    2378           0 :   pari_sp top = pari_mainstack->top, bot = pari_mainstack->bot;
    2379             :   GEN z;
    2380           0 :   err_printf("bot=0x%lx\ttop=0x%lx\tavma=0x%lx\n", bot, top, avma);
    2381           0 :   for (z = ((GEN)top)-1; z >= (GEN)avma; z--)
    2382           0 :     err_printf("%p:\t0x%lx\t%lu\n",z,*z,*z);
    2383           0 : }
    2384             : 
    2385             : void
    2386           0 : setdebugvar(long n) { DEBUGVAR=n; }
    2387             : 
    2388             : long
    2389           0 : getdebugvar(void) { return DEBUGVAR; }
    2390             : 
    2391             : long
    2392           7 : getstack(void) { return pari_mainstack->top-avma; }
    2393             : 
    2394             : /*******************************************************************/
    2395             : /*                                                                 */
    2396             : /*                               timer_delay                             */
    2397             : /*                                                                 */
    2398             : /*******************************************************************/
    2399             : 
    2400             : #if defined(USE_CLOCK_GETTIME)
    2401             : #if defined(_POSIX_THREAD_CPUTIME)
    2402             : static THREAD clockid_t time_type = CLOCK_THREAD_CPUTIME_ID;
    2403             : #else
    2404             : static const THREAD clockid_t time_type = CLOCK_PROCESS_CPUTIME_ID;
    2405             : #endif
    2406             : static void
    2407             : pari_init_timer(void)
    2408             : {
    2409             : #if defined(_POSIX_THREAD_CPUTIME)
    2410             :   time_type = CLOCK_PROCESS_CPUTIME_ID;
    2411             : #endif
    2412             : }
    2413             : 
    2414             : void
    2415             : timer_start(pari_timer *T)
    2416             : {
    2417             :   struct timespec t;
    2418             :   clock_gettime(time_type,&t);
    2419             :   T->us = t.tv_nsec / 1000;
    2420             :   T->s  = t.tv_sec;
    2421             : }
    2422             : #elif defined(USE_GETRUSAGE)
    2423             : #ifdef RUSAGE_THREAD
    2424             : static THREAD int rusage_type = RUSAGE_THREAD;
    2425             : #else
    2426             : static const THREAD int rusage_type = RUSAGE_SELF;
    2427             : #endif /*RUSAGE_THREAD*/
    2428             : static void
    2429        1552 : pari_init_timer(void)
    2430             : {
    2431             : #ifdef RUSAGE_THREAD
    2432        1552 :   rusage_type = RUSAGE_SELF;
    2433             : #endif
    2434        1552 : }
    2435             : 
    2436             : void
    2437      229644 : timer_start(pari_timer *T)
    2438             : {
    2439             :   struct rusage r;
    2440      229644 :   getrusage(rusage_type,&r);
    2441      229646 :   T->us = r.ru_utime.tv_usec;
    2442      229646 :   T->s  = r.ru_utime.tv_sec;
    2443      229646 : }
    2444             : #elif defined(USE_FTIME)
    2445             : 
    2446             : static void
    2447             : pari_init_timer(void) { }
    2448             : 
    2449             : void
    2450             : timer_start(pari_timer *T)
    2451             : {
    2452             :   struct timeb t;
    2453             :   ftime(&t);
    2454             :   T->us = ((long)t.millitm) * 1000;
    2455             :   T->s  = t.time;
    2456             : }
    2457             : 
    2458             : #else
    2459             : 
    2460             : static void
    2461             : _get_time(pari_timer *T, long Ticks, long TickPerSecond)
    2462             : {
    2463             :   T->us = (long) ((Ticks % TickPerSecond) * (1000000. / TickPerSecond));
    2464             :   T->s  = Ticks / TickPerSecond;
    2465             : }
    2466             : 
    2467             : # ifdef USE_TIMES
    2468             : static void
    2469             : pari_init_timer(void) { }
    2470             : 
    2471             : void
    2472             : timer_start(pari_timer *T)
    2473             : {
    2474             : # ifdef _SC_CLK_TCK
    2475             :   long tck = sysconf(_SC_CLK_TCK);
    2476             : # else
    2477             :   long tck = CLK_TCK;
    2478             : # endif
    2479             :   struct tms t; times(&t);
    2480             :   _get_time(T, t.tms_utime, tck);
    2481             : }
    2482             : # elif defined(_WIN32)
    2483             : static void
    2484             : pari_init_timer(void) { }
    2485             : 
    2486             : void
    2487             : timer_start(pari_timer *T)
    2488             : { _get_time(T, win32_timer(), 1000); }
    2489             : # else
    2490             : #  include <time.h>
    2491             : #  ifndef CLOCKS_PER_SEC
    2492             : #   define CLOCKS_PER_SEC 1000000 /* may be false on YOUR system */
    2493             : #  endif
    2494             : static void
    2495             : pari_init_timer(void) { }
    2496             : 
    2497             : void
    2498             : timer_start(pari_timer *T)
    2499             : { _get_time(T, clock(), CLOCKS_PER_SEC); }
    2500             : # endif
    2501             : #endif
    2502             : 
    2503             : static long
    2504       74877 : timer_aux(pari_timer *T, pari_timer *U)
    2505             : {
    2506       74877 :   long s = T->s, us = T->us; timer_start(U);
    2507       74877 :   return 1000 * (U->s - s) + (U->us - us + 500) / 1000;
    2508             : }
    2509             : /* return delay, reset timer */
    2510             : long
    2511       73323 : timer_delay(pari_timer *T) { return timer_aux(T, T); }
    2512             : /* return delay, don't reset timer */
    2513             : long
    2514        1554 : timer_get(pari_timer *T) { pari_timer t; return timer_aux(T, &t); }
    2515             : 
    2516             : static void
    2517           0 : timer_vprintf(pari_timer *T, const char *format, va_list args)
    2518             : {
    2519           0 :   out_puts(pariErr, "Time ");
    2520           0 :   out_vprintf(pariErr, format,args);
    2521           0 :   out_printf(pariErr, ": %ld\n", timer_delay(T));
    2522           0 :   pariErr->flush();
    2523           0 : }
    2524             : void
    2525           0 : timer_printf(pari_timer *T, const char *format, ...)
    2526             : {
    2527           0 :   va_list args; va_start(args, format);
    2528           0 :   timer_vprintf(T, format, args);
    2529           0 :   va_end(args);
    2530           0 : }
    2531             : 
    2532             : long
    2533           0 : timer(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2534             : long
    2535        3050 : gettime(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2536             : 
    2537             : static THREAD pari_timer timer2_T, abstimer_T;
    2538             : long
    2539           0 : timer2(void) {  return timer_delay(&timer2_T);}
    2540             : void
    2541           0 : msgtimer(const char *format, ...)
    2542             : {
    2543           0 :   va_list args; va_start(args, format);
    2544           0 :   timer_vprintf(&timer2_T, format, args);
    2545           0 :   va_end(args);
    2546           0 : }
    2547             : long
    2548        1552 : getabstime(void)  { return timer_get(&abstimer_T);}
    2549             : #if defined(USE_CLOCK_GETTIME) || defined(USE_GETTIMEOFDAY) \
    2550             :  || defined(USE_FTIMEFORWALLTIME)
    2551             : static GEN
    2552           0 : timetoi(ulong s, ulong m)
    2553             : {
    2554           0 :   pari_sp av = avma;
    2555           0 :   GEN r = addiu(muliu(utoi(s), 1000), m);
    2556           0 :   return gerepileuptoint(av, r);
    2557             : }
    2558             : #endif
    2559             : GEN
    2560           0 : getwalltime(void)
    2561             : {
    2562             : #if defined(USE_CLOCK_GETTIME)
    2563             :   struct timespec t;
    2564             :   if (!clock_gettime(CLOCK_REALTIME,&t))
    2565             :     return timetoi(t.tv_sec, (t.tv_nsec + 500000)/1000000);
    2566             : #elif defined(USE_GETTIMEOFDAY)
    2567             :   struct timeval tv;
    2568           0 :   if (!gettimeofday(&tv, NULL))
    2569           0 :     return timetoi(tv.tv_sec, (tv.tv_usec + 500)/1000);
    2570             : #elif defined(USE_FTIMEFORWALLTIME)
    2571             :   struct timeb tp;
    2572             :   ftime(&tp); return timetoi(tp.time, tp.millitm);
    2573             : #endif
    2574           0 :   return utoi(getabstime());
    2575             : }
    2576             : 
    2577             : /*******************************************************************/
    2578             : /*                                                                 */
    2579             : /*                   FUNCTIONS KNOWN TO THE ANALYZER               */
    2580             : /*                                                                 */
    2581             : /*******************************************************************/
    2582             : GEN
    2583           7 : pari_version(void)
    2584             : {
    2585           7 :   const ulong mask = (1UL<<PARI_VERSION_SHIFT) - 1;
    2586           7 :   ulong major, minor, patch, n = paricfg_version_code;
    2587           7 :   patch = n & mask; n >>= PARI_VERSION_SHIFT;
    2588           7 :   minor = n & mask; n >>= PARI_VERSION_SHIFT;
    2589           7 :   major = n;
    2590           7 :   if (*paricfg_vcsversion) {
    2591           7 :     const char *ver = paricfg_vcsversion;
    2592           7 :     const char *s = strchr(ver, '-');
    2593             :     char t[8];
    2594           7 :     const long len = s-ver;
    2595             :     GEN v;
    2596           7 :     if (!s || len > 6) pari_err_BUG("pari_version()"); /* paranoia */
    2597           7 :     memcpy(t, ver, len); t[len] = 0;
    2598           7 :     v = cgetg(6, t_VEC);
    2599           7 :     gel(v,1) = utoi(major);
    2600           7 :     gel(v,2) = utoi(minor);
    2601           7 :     gel(v,3) = utoi(patch);
    2602           7 :     gel(v,4) = stoi( atoi(t) );
    2603           7 :     gel(v,5) = strtoGENstr(s+1);
    2604           7 :     return v;
    2605             :   } else {
    2606           0 :     GEN v = cgetg(4, t_VEC);
    2607           0 :     gel(v,1) = utoi(major);
    2608           0 :     gel(v,2) = utoi(minor);
    2609           0 :     gel(v,3) = utoi(patch);
    2610           0 :     return v;
    2611             :   }
    2612             : }
    2613             : 
    2614             : /* List of GP functions: generated from the description system.
    2615             :  * Format (struct entree) :
    2616             :  *   char *name   : name (under GP).
    2617             :  *   ulong valence: (EpNEW, EpALIAS,EpVAR, EpINSTALL)|EpSTATIC
    2618             :  *   void *value  : For PREDEFINED FUNCTIONS: C function to call.
    2619             :  *                  For USER FUNCTIONS: pointer to defining data (block) =
    2620             :  *                   entree*: NULL, list of entree (arguments), NULL
    2621             :  *                   char*  : function text
    2622             :  *   long menu    : which help section do we belong to
    2623             :  *                   1: Standard monadic or dyadic OPERATORS
    2624             :  *                   2: CONVERSIONS and similar elementary functions
    2625             :  *                   3: functions related to COMBINATORICS
    2626             :  *                   4: TRANSCENDENTAL functions, etc.
    2627             :  *   char *code   : GP prototype, aka Parser Code (see libpari's manual)
    2628             :  *                  if NULL, use valence instead.
    2629             :  *   char *help   : short help text (init to NULL).
    2630             :  *   void *pvalue : push_val history.
    2631             :  *   long arity   : maximum number of arguments.
    2632             :  *   entree *next : next entree (init to NULL, used in hashing code). */
    2633             : #include "init.h"
    2634             : #include "default.h"

Generated by: LCOV version 1.13