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

Generated by: LCOV version 1.13