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

Generated by: LCOV version 1.11