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 21059-cbe0d6a) Lines: 878 1183 74.2 %
Date: 2017-09-22 06:24:58 Functions: 108 134 80.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*        INITIALIZING THE SYSTEM, ERRORS, STACK MANAGEMENT        */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : /* _GNU_SOURCE is needed before first include to get RUSAGE_THREAD */
      20             : #undef _GNU_SOURCE /* avoid warning */
      21             : #define _GNU_SOURCE
      22             : #include <string.h>
      23             : #if defined(_WIN32) || defined(__CYGWIN32__)
      24             : #  include "../systems/mingw/mingw.h"
      25             : #  include <process.h>
      26             : #endif
      27             : #include "paricfg.h"
      28             : #if defined(STACK_CHECK) && !defined(__EMX__)
      29             : #  include <sys/types.h>
      30             : #  include <sys/time.h>
      31             : #  include <sys/resource.h>
      32             : #endif
      33             : #if defined(HAS_WAITPID) && defined(HAS_SETSID)
      34             : #  include <sys/wait.h>
      35             : #endif
      36             : #ifdef HAS_MMAP
      37             : #  include <sys/mman.h>
      38             : #endif
      39             : #if defined(USE_GETTIMEOFDAY) || defined(USE_GETRUSAGE) || defined(USE_TIMES)
      40             : #  include <sys/time.h>
      41             : #endif
      42             : #if defined(USE_GETRUSAGE)
      43             : #  include <sys/resource.h>
      44             : #endif
      45             : #if defined(USE_FTIME) || defined(USE_FTIMEFORWALLTIME)
      46             : #  include <sys/timeb.h>
      47             : #endif
      48             : #if defined(USE_CLOCK_GETTIME) || defined(USE_TIMES)
      49             : #  include <time.h>
      50             : #endif
      51             : #if defined(USE_TIMES)
      52             : #  include <sys/times.h>
      53             : #endif
      54             : #include "pari.h"
      55             : #include "paripriv.h"
      56             : #include "anal.h"
      57             : 
      58             : const double 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       65430 : pari_init_blocks(void)
     152             : {
     153       65430 :   next_block = 0; cur_block = NULL;
     154             : #ifdef DEBUG
     155             :   NUM = 0;
     156             : #endif
     157       65430 : }
     158             : 
     159             : static void
     160       65824 : pari_close_blocks(void)
     161             : {
     162       65824 :   while (cur_block) killblock(cur_block);
     163       66157 : }
     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   181179501 : newblock(size_t n)
     173             : {
     174   181179501 :   long *x = (long *) pari_malloc((n + BL_HEAD)*sizeof(long)) + BL_HEAD;
     175             : 
     176   181181896 :   bl_refc(x) = 1;
     177   181181896 :   bl_next(x) = NULL;
     178   181181896 :   bl_prev(x) = cur_block;
     179   181181896 :   bl_num(x)  = next_block++;
     180   181181896 :   if (cur_block) bl_next(cur_block) = x;
     181             : #ifdef DEBUG
     182             :   err_printf("+ %ld\n", ++NUM);
     183             : #endif
     184   181181896 :   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   181181659 :   return cur_block = x;
     191             : }
     192             : 
     193             : GEN
     194       50489 : gcloneref(GEN x)
     195             : {
     196       50489 :   if (isclone(x)) { ++bl_refc(x); return x; }
     197       50489 :   else return gclone(x);
     198             : }
     199             : 
     200             : void
     201           0 : gclone_refc(GEN x) { ++bl_refc(x); }
     202             : 
     203             : void
     204   223549406 : gunclone(GEN x)
     205             : {
     206   447099272 :   if (--bl_refc(x) > 0) return;
     207   181181700 :   BLOCK_SIGINT_START;
     208   181182404 :   if (bl_next(x)) bl_prev(bl_next(x)) = bl_prev(x);
     209             :   else
     210             :   {
     211    24792953 :     cur_block = bl_prev(x);
     212    24792953 :     next_block = bl_num(x);
     213             :   }
     214   181182404 :   if (bl_prev(x)) bl_next(bl_prev(x)) = bl_next(x);
     215   181182404 :   if (DEBUGMEM > 2)
     216           0 :     err_printf("killing block (no %ld): %08lx\n", bl_num(x), x);
     217   181182578 :   free((void*)bl_base(x)); /* pari_free not needed: we already block */
     218   181182578 :   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  2715384708 : gunclone_deep(GEN x)
     228             : {
     229             :   long i, lx;
     230             :   GEN v;
     231  5430769416 :   if (isclone(x) && bl_refc(x) > 1) { --bl_refc(x); return; }
     232  2679774455 :   BLOCK_SIGINT_START;
     233  2679774455 :   switch(typ(x))
     234             :   {
     235             :     case t_VEC: case t_COL: case t_MAT:
     236    77740015 :       lx = lg(x);
     237    77740015 :       for (i=1;i<lx;i++) gunclone_deep(gel(x,i));
     238    77740015 :       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  2679774455 :   if (isclone(x)) gunclone(x);
     246  2679774455 :   BLOCK_SIGINT_END;
     247             : }
     248             : 
     249             : int
     250      191834 : pop_entree_block(entree *ep, long loc)
     251             : {
     252      191834 :   GEN x = (GEN)ep->value;
     253      191834 :   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        1503 : pari_stackcheck_init(void *pari_stack_base)
     283             : {
     284             :   struct rlimit rip;
     285             :   ulong size;
     286        1503 :   if (!pari_stack_base) { PARI_stack_limit = NULL; return; }
     287        1503 :   if (getrlimit(RLIMIT_STACK, &rip)) return;
     288        1503 :   size = rip.rlim_cur;
     289        1503 :   if (size == (ulong)RLIM_INFINITY || size > (ulong)pari_stack_base)
     290           0 :     PARI_stack_limit = (void*)(((ulong)pari_stack_base) / 16);
     291             :   else
     292        1503 :     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       16521 : os_signal(int sig, pari_sighandler_t f)
     401             : {
     402             : #ifdef HAS_SIGACTION
     403             :   struct sigaction sa, oldsa;
     404             : 
     405       16521 :   sa.sa_handler = f;
     406       16521 :   sigemptyset(&sa.sa_mask);
     407       16521 :   sa.sa_flags = SA_NODEFER;
     408             : 
     409       16521 :   if (sigaction(sig, &sa, &oldsa)) return NULL;
     410       16521 :   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        3004 : pari_sig_init(void (*f)(int))
     482             : {
     483             : #ifdef SIGBUS
     484        3004 :   (void)os_signal(SIGBUS,f);
     485             : #endif
     486             : #ifdef SIGFPE
     487        3004 :   (void)os_signal(SIGFPE,f);
     488             : #endif
     489             : #ifdef SIGINT
     490        3004 :   (void)os_signal(SIGINT,f);
     491             : #endif
     492             : #ifdef SIGBREAK
     493             :   (void)os_signal(SIGBREAK,f);
     494             : #endif
     495             : #ifdef SIGPIPE
     496        3004 :   (void)os_signal(SIGPIPE,f);
     497             : #endif
     498             : #ifdef SIGSEGV
     499        3004 :   (void)os_signal(SIGSEGV,f);
     500             : #endif
     501        3004 : }
     502             : 
     503             : /*********************************************************************/
     504             : /*                      STACK AND UNIVERSAL CONSTANTS                */
     505             : /*********************************************************************/
     506             : static void
     507        1503 : init_universal_constants(void)
     508             : {
     509        1503 :   gen_0  = (GEN)readonly_constants;
     510        1503 :   gnil   = (GEN)readonly_constants+2;
     511        1503 :   gen_1  = (GEN)readonly_constants+4;
     512        1503 :   gen_2  = (GEN)readonly_constants+7;
     513        1503 :   gen_m1 = (GEN)readonly_constants+10;
     514        1503 :   gen_m2 = (GEN)readonly_constants+13;
     515        1503 :   ghalf  = (GEN)readonly_constants+16;
     516        1503 :   err_e_STACK = (GEN)readonly_err_STACK;
     517        1503 : }
     518             : 
     519             : static void
     520       65607 : pari_init_errcatch(void)
     521             : {
     522       65607 :   iferr_env = NULL;
     523       65607 :   global_err_data = NULL;
     524       65607 : }
     525             : 
     526             : /*********************************************************************/
     527             : /*                           INIT DEFAULTS                           */
     528             : /*********************************************************************/
     529             : void
     530        1503 : pari_init_defaults(void)
     531             : {
     532             :   long i;
     533        1503 :   initout(1);
     534             : 
     535             : #ifdef LONG_IS_64BIT
     536        1296 :   precreal = 128;
     537             : #else
     538         207 :   precreal = 96;
     539             : #endif
     540             : 
     541        1503 :   precdl = 16;
     542        1503 :   DEBUGFILES = DEBUGLEVEL = DEBUGMEM = 0;
     543        1503 :   disable_color = 1;
     544        1503 :   logstyle = logstyle_none;
     545             : 
     546        1503 :   current_psfile = pari_strdup("pari.ps");
     547        1503 :   current_logfile= pari_strdup("pari.log");
     548        1503 :   pari_logfile = NULL;
     549             : 
     550        1503 :   pari_datadir = os_getenv("GP_DATA_DIR");
     551        1503 :   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        1503 :       pari_datadir = pari_strdup(paricfg_datadir);
     559             :   }
     560           0 :   else pari_datadir= pari_strdup(pari_datadir);
     561        1503 :   for (i=0; i<c_LAST; i++) gp_colors[i] = c_NONE;
     562        1503 :   colormap = NULL; pari_graphcolors = NULL;
     563        1503 : }
     564             : 
     565             : /*********************************************************************/
     566             : /*                   FUNCTION HASHTABLES, MODULES                    */
     567             : /*********************************************************************/
     568             : extern entree functions_basic[], functions_default[];
     569             : static void
     570        1503 : pari_init_functions(void)
     571             : {
     572        1503 :   pari_stack_init(&s_MODULES, sizeof(*MODULES),(void**)&MODULES);
     573        1503 :   pari_stack_pushp(&s_MODULES,functions_basic);
     574        1503 :   functions_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     575        1503 :   pari_fill_hashtable(functions_hash, functions_basic);
     576        1503 :   defaults_hash = (entree**) pari_calloc(sizeof(entree*)*functions_tblsz);
     577        1503 :   pari_add_defaults_module(functions_default);
     578        1503 : }
     579             : 
     580             : void
     581        1501 : pari_add_module(entree *ep)
     582             : {
     583        1501 :   pari_fill_hashtable(functions_hash, ep);
     584        1501 :   pari_stack_pushp(&s_MODULES, ep);
     585        1501 : }
     586             : 
     587             : void
     588        1503 : pari_add_defaults_module(entree *ep)
     589        1503 : { 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       66552 : 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       66552 :   void *b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     609             :                              MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     610       66552 :   if (b == MAP_FAILED) return NULL;
     611       66552 :   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       66552 :   b = mmap(NULL, size, PROT_READ|PROT_WRITE,
     617             :                        MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0);
     618       66552 :   if (b == MAP_FAILED) return NULL;
     619       66552 :   return b;
     620             : }
     621             : 
     622             : static void
     623       66550 : pari_mainstack_mfree(void *s, size_t size)
     624             : {
     625       66550 :   munmap(s, size);
     626       66550 : }
     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      133788 : pari_mainstack_mreset(pari_sp from, pari_sp to)
     643             : {
     644      133788 :   size_t s = to - from;
     645             :   void *addr, *res;
     646      267576 :   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      133788 : pari_mainstack_mextend(pari_sp from, pari_sp to)
     659             : {
     660      133788 :   size_t s = to - from;
     661      133788 :   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      133788 : pari_mainstack_setsize(struct pari_mainstack *st, size_t size)
     670             : {
     671      133788 :   pari_sp newbot = st->top - size;
     672             :   /* Align newbot to pagesize */
     673      133788 :   pari_sp alignbot = newbot & ~(pari_sp)(PARI_STACK_ALIGN - 1);
     674      133788 :   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      133788 :   pari_mainstack_mreset(st->vbot, alignbot);
     683      133788 :   st->bot = newbot;
     684      133788 :   st->size = size;
     685      133788 :   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      133102 : fix_size(size_t a)
     712             : {
     713      133102 :   size_t ps = PARI_STACK_ALIGN;
     714      133102 :   size_t b = a & ~(ps - 1); /* Align */
     715      133102 :   if (b < a && b < ~(ps - 1)) b += ps;
     716      133102 :   if (b < MIN_STACK) b = MIN_STACK;
     717      133102 :   return b;
     718             : }
     719             : 
     720             : static void
     721       66552 : pari_mainstack_alloc(struct pari_mainstack *st, size_t rsize, size_t vsize)
     722             : {
     723       66552 :   size_t sizemax = vsize ? vsize: rsize, s = fix_size(sizemax);
     724             :   for (;;)
     725             :   {
     726       66552 :     st->vbot = (pari_sp)pari_mainstack_malloc(s);
     727       66552 :     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       66552 :   st->vsize = vsize ? s: 0;
     733       66552 :   st->rsize = minuu(rsize, s);
     734       66552 :   st->top = st->vbot+s;
     735       66552 :   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       66552 :   st->memused = 0;
     741       66552 : }
     742             : 
     743             : static void
     744       66550 : pari_mainstack_free(struct pari_mainstack *st)
     745             : {
     746       66550 :   pari_mainstack_mfree((void*)st->vbot, st->vsize ? st->vsize : fix_size(st->rsize));
     747       66550 :   st->top = st->bot = st->vbot = 0;
     748       66550 :   st->size = st->vsize = 0;
     749       66550 : }
     750             : 
     751             : static void
     752         222 : pari_mainstack_resize(struct pari_mainstack *st, size_t rsize, size_t vsize)
     753             : {
     754         222 :   BLOCK_SIGINT_START;
     755         222 :   pari_mainstack_free(st);
     756         222 :   pari_mainstack_alloc(st, rsize, vsize);
     757         222 :   BLOCK_SIGINT_END;
     758         222 : }
     759             : 
     760             : static void
     761       65453 : pari_mainstack_use(struct pari_mainstack *st)
     762             : {
     763       65453 :   pari_mainstack = st;
     764       65453 :   avma = st->top;
     765       65453 : }
     766             : 
     767             : static void
     768        1503 : paristack_alloc(size_t rsize, size_t vsize)
     769             : {
     770        1503 :   pari_mainstack_alloc(pari_mainstack, rsize, vsize);
     771        1503 :   pari_mainstack_use(pari_mainstack);
     772        1503 : }
     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         228 : paristack_newrsize(ulong newsize)
     798             : {
     799         228 :   size_t s, vsize = pari_mainstack->vsize;
     800         228 :   if (!newsize) newsize = pari_mainstack->rsize << 1;
     801         228 :   if (newsize != pari_mainstack->rsize)
     802         222 :     pari_mainstack_resize(pari_mainstack, newsize, vsize);
     803         228 :   evalstate_reset();
     804         228 :   s = pari_mainstack->rsize;
     805         228 :   pari_warn(warner,"new stack size = %lu (%.3f Mbytes)", s, s/1048576.);
     806         228 :   pari_init_errcatch();
     807         228 :   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       67236 : parivstack_reset(void)
     829             : {
     830       67236 :   pari_mainstack_setsize(pari_mainstack, pari_mainstack->rsize);
     831       67236 :   if (avma < pari_mainstack->bot)
     832           0 :     pari_err_BUG("parivstack_reset [avma < bot]");
     833       67236 : }
     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       64827 : pari_thread_alloc(struct pari_thread *t, size_t s, GEN arg)
     865             : {
     866       64827 :   pari_mainstack_alloc(&t->st,s,0);
     867       64827 :   t->data = arg;
     868       64827 : }
     869             : 
     870             : void
     871       64827 : pari_thread_free(struct pari_thread *t)
     872             : {
     873       64827 :   pari_mainstack_free(&t->st);
     874       64827 : }
     875             : 
     876             : void
     877       66006 : pari_thread_init(void)
     878             : {
     879       66006 :   pari_init_blocks();
     880       65522 :   pari_init_errcatch();
     881       65372 :   pari_init_rand();
     882       65880 :   pari_init_floats();
     883       65807 :   pari_init_parser();
     884       66209 :   pari_init_compiler();
     885       66077 :   pari_init_evaluator();
     886       65699 :   pari_init_files();
     887       65818 :   pari_thread_init_primetab();
     888       65635 :   pari_thread_init_seadata();
     889       65463 : }
     890             : 
     891             : void
     892        6635 : pari_thread_sync(void)
     893             : {
     894        6635 :   pari_pthread_init_primetab();
     895        6635 :   pari_pthread_init_seadata();
     896        6635 :   pari_pthread_init_varstate();
     897        6635 : }
     898             : 
     899             : void
     900       66134 : pari_thread_close(void)
     901             : {
     902       66134 :   pari_thread_close_files();
     903       66155 :   pari_close_evaluator();
     904       66158 :   pari_close_compiler();
     905       66072 :   pari_close_parser();
     906       65960 :   pari_close_floats();
     907       65939 :   pari_close_blocks();
     908       65645 : }
     909             : 
     910             : GEN
     911       63931 : pari_thread_start(struct pari_thread *t)
     912             : {
     913       63931 :   pari_mainstack_use(&t->st);
     914       64584 :   pari_thread_init();
     915       63969 :   pari_thread_init_varstate();
     916       64797 :   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        1503 : pari_init_opts(size_t parisize, ulong maxprime, ulong init_opts)
     941             : {
     942             :   ulong u;
     943             : 
     944        1503 :   pari_mt_nbthreads = 0;
     945        1503 :   cb_pari_quit = dflt_pari_quit;
     946        1503 :   cb_pari_init_histfile = NULL;
     947        1503 :   cb_pari_get_line_interactive = NULL;
     948        1503 :   cb_pari_fgets_interactive = NULL;
     949        1503 :   cb_pari_whatnow = NULL;
     950        1503 :   cb_pari_handle_exception = NULL;
     951        1503 :   cb_pari_err_handle = pari_err_display;
     952        1503 :   cb_pari_pre_recover = NULL;
     953        1503 :   cb_pari_break_loop = NULL;
     954        1503 :   cb_pari_is_interactive = NULL;
     955        1503 :   cb_pari_start_output = NULL;
     956        1503 :   cb_pari_sigint = dflt_sigint_fun;
     957        1503 :   if (init_opts&INIT_JMPm) cb_pari_err_recover = dflt_err_recover;
     958             : 
     959        1503 :   pari_stackcheck_init(&u);
     960        1503 :   pari_init_homedir();
     961        1503 :   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        1503 :   pari_mainstack = (struct pari_mainstack *) malloc(sizeof(*pari_mainstack));
     968        1503 :   paristack_alloc(parisize, 0);
     969        1503 :   init_universal_constants();
     970        1503 :   diffptr = NULL;
     971        1503 :   if (!(init_opts&INIT_noPRIMEm))  pari_init_primes(maxprime);
     972        1503 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_init();
     973        1503 :   pari_init_graphics();
     974        1503 :   pari_init_primetab();
     975        1503 :   pari_init_seadata();
     976        1503 :   pari_thread_init();
     977        1503 :   pari_init_functions();
     978        1503 :   pari_var_init();
     979        1503 :   pari_init_timer();
     980        1503 :   pari_init_buffers();
     981        1503 :   (void)getabstime();
     982        1503 :   try_to_recover = 1;
     983        1503 :   if (!(init_opts&INIT_noIMTm)) pari_mt_init();
     984        1503 :   if ((init_opts&INIT_SIGm)) pari_sig_init(pari_sighandler);
     985        1503 : }
     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        1501 : pari_close_opts(ulong init_opts)
     993             : {
     994             :   long i;
     995             : 
     996        1501 :   BLOCK_SIGINT_START;
     997        1501 :   if ((init_opts&INIT_SIGm)) pari_sig_init(SIG_DFL);
     998        1501 :   if (!(init_opts&INIT_noIMTm)) pari_mt_close();
     999             : 
    1000      204136 :   for (i = 0; i < functions_tblsz; i++)
    1001             :   {
    1002      202635 :     entree *ep = functions_hash[i];
    1003     2080658 :     while (ep) {
    1004     1675388 :       entree *EP = ep->next;
    1005     1675388 :       if (!EpSTATIC(ep)) { freeep(ep); free(ep); }
    1006     1675388 :       ep = EP;
    1007             :     }
    1008             :   }
    1009        1501 :   pari_var_close();
    1010        1501 :   pari_close_mf();
    1011        1501 :   pari_thread_close();
    1012        1501 :   pari_close_files();
    1013        1501 :   pari_close_homedir();
    1014        1501 :   if (!(init_opts&INIT_noINTGMPm)) pari_kernel_close();
    1015             : 
    1016        1501 :   free((void*)functions_hash);
    1017        1501 :   free((void*)defaults_hash);
    1018        1501 :   if (diffptr) pari_close_primes();
    1019        1501 :   free(current_logfile);
    1020        1501 :   free(current_psfile);
    1021        1501 :   pari_mainstack_free(pari_mainstack);
    1022        1501 :   free((void*)pari_mainstack);
    1023        1501 :   pari_stack_delete(&s_MODULES);
    1024        1501 :   if (pari_datadir) free(pari_datadir);
    1025        1501 :   if (init_opts&INIT_DFTm)
    1026             :   { /* delete GP_DATA */
    1027        1501 :     pari_close_paths();
    1028        1501 :     if (GP_DATA->hist->v) free((void*)GP_DATA->hist->v);
    1029        1501 :     if (GP_DATA->pp->cmd) free((void*)GP_DATA->pp->cmd);
    1030        1501 :     if (GP_DATA->help) free((void*)GP_DATA->help);
    1031        1501 :     free((void*)GP_DATA->prompt);
    1032        1501 :     free((void*)GP_DATA->prompt_cont);
    1033        1501 :     free((void*)GP_DATA->histfile);
    1034             :   }
    1035        1501 :   BLOCK_SIGINT_END;
    1036        1501 : }
    1037             : 
    1038             : void
    1039        1501 : pari_close(void)
    1040        1501 : { pari_close_opts(INIT_JMPm | INIT_SIGm | INIT_DFTm); }
    1041             : 
    1042             : /*******************************************************************/
    1043             : /*                                                                 */
    1044             : /*                         ERROR RECOVERY                          */
    1045             : /*                                                                 */
    1046             : /*******************************************************************/
    1047             : void
    1048       81630 : gp_context_save(struct gp_context* rec)
    1049             : {
    1050       81630 :   rec->file = pari_last_tmp_file();
    1051       81630 :   if (DEBUGFILES>1)
    1052           0 :     err_printf("gp_context_save: %s\n", rec->file ? rec->file->name: "NULL");
    1053       81630 :   rec->prettyp = GP_DATA->fmt->prettyp;
    1054       81630 :   rec->listloc = next_block;
    1055       81630 :   rec->iferr_env = iferr_env;
    1056       81630 :   rec->err_data  = global_err_data;
    1057       81630 :   varstate_save(&rec->var);
    1058       81630 :   evalstate_save(&rec->eval);
    1059       81630 :   parsestate_save(&rec->parse);
    1060       81630 : }
    1061             : 
    1062             : void
    1063        7909 : gp_context_restore(struct gp_context* rec)
    1064             : {
    1065             :   long i;
    1066             : 
    1067       15818 :   if (!try_to_recover) return;
    1068             :   /* disable gp_context_restore() and SIGINT */
    1069        7909 :   try_to_recover = 0;
    1070        7909 :   BLOCK_SIGINT_START
    1071        7909 :   if (DEBUGMEM>2) err_printf("entering recover(), loc = %ld\n", rec->listloc);
    1072        7909 :   evalstate_restore(&rec->eval);
    1073        7909 :   parsestate_restore(&rec->parse);
    1074        7909 :   filestate_restore(rec->file);
    1075        7909 :   global_err_data = rec->err_data;
    1076        7909 :   iferr_env = rec->iferr_env;
    1077        7909 :   GP_DATA->fmt->prettyp = rec->prettyp;
    1078             : 
    1079     1075624 :   for (i = 0; i < functions_tblsz; i++)
    1080             :   {
    1081     1067715 :     entree *ep = functions_hash[i];
    1082    11679638 :     while (ep)
    1083             :     {
    1084     9544208 :       entree *EP = ep->next;
    1085     9544208 :       switch(EpVALENCE(ep))
    1086             :       {
    1087             :         case EpVAR:
    1088      211824 :           while (pop_val_if_newer(ep,rec->listloc)) /* empty */;
    1089      211824 :           break;
    1090      632484 :         case EpNEW: break;
    1091             :       }
    1092     9544208 :       ep = EP;
    1093             :     }
    1094             :   }
    1095        7909 :   varstate_restore(&rec->var);
    1096        7909 :   if (DEBUGMEM>2) err_printf("leaving recover()\n");
    1097        7909 :   BLOCK_SIGINT_END
    1098        7909 :   try_to_recover = 1;
    1099             : }
    1100             : 
    1101             : static void
    1102        7839 : err_recover(long numerr)
    1103             : {
    1104        7839 :   if (cb_pari_pre_recover)
    1105        7839 :     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        8340 : err_init(void)
    1114             : {
    1115             :   /* make sure pari_err msg starts at the beginning of line */
    1116        8340 :   if (!pari_last_was_newline()) pari_putc('\n');
    1117        8340 :   pariOut->flush();
    1118        8340 :   pariErr->flush();
    1119        8340 :   out_term_color(pariErr, c_ERR);
    1120        8340 : }
    1121             : 
    1122             : static void
    1123        8305 : err_init_msg(int user)
    1124             : {
    1125             :   const char *gp_function_name;
    1126        8305 :   out_puts(pariErr, "  *** ");
    1127        8305 :   if (!user && (gp_function_name = closure_func_err()))
    1128        4904 :     out_printf(pariErr, "%s: ", gp_function_name);
    1129             :   else
    1130        3401 :     out_puts(pariErr, "  ");
    1131        8305 : }
    1132             : 
    1133             : void
    1134         480 : pari_warn(int numerr, ...)
    1135             : {
    1136             :   char *ch1;
    1137             :   va_list ap;
    1138             : 
    1139         480 :   va_start(ap,numerr);
    1140             : 
    1141         480 :   err_init();
    1142         480 :   err_init_msg(numerr==warnuser || numerr==warnstack);
    1143         480 :   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         473 :       out_puts(pariErr, "Warning: "); ch1=va_arg(ap, char*);
    1157         473 :       out_vprintf(pariErr, ch1,ap); out_putc(pariErr, '.');
    1158         473 :       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         480 :   va_end(ap);
    1182         480 :   out_term_color(pariErr, c_NONE);
    1183         480 :   out_putc(pariErr, '\n');
    1184         480 :   pariErr->flush();
    1185         480 : }
    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       22755 : pari_err2GEN(long numerr, va_list ap)
    1239             : {
    1240       22755 :   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         118 :       const char *ch1 = va_arg(ap, char*);
    1252         118 :       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        1203 :     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       12242 :       const char *f = va_arg(ap, const char*);
    1288       12242 :       GEN x = va_arg(ap, GEN);
    1289       12242 :       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        1688 :       const char *f = va_arg(ap, const char*);
    1309        1688 :       const char *v = va_arg(ap, const char *);
    1310        1688 :       const char *op = va_arg(ap, const char *);
    1311        1688 :       GEN l = va_arg(ap, GEN);
    1312        1688 :       GEN x = va_arg(ap, GEN);
    1313        1688 :       retmkerr6(numerr,strtoGENstr(f),strtoGENstr(v),strtoGENstr(op),l,x);
    1314             :     }
    1315             :   case e_PRIORITY:
    1316             :     {
    1317         147 :       const char *f = va_arg(ap, const char*);
    1318         147 :       GEN x = va_arg(ap, GEN);
    1319         147 :       const char *op = va_arg(ap, const char *);
    1320         147 :       long v = va_arg(ap, long);
    1321         147 :       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        2268 : gdisplay(GEN x)
    1360             : {
    1361        2268 :   char *s = GENtostr_raw(x);
    1362        2268 :   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       14937 : pari_err2str(GEN e)
    1369             : {
    1370       14937 :   long numerr = err_get_num(e);
    1371       14937 :   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         112 :     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         445 :     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         329 :     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         518 :     return pari_sprintf("impossible inverse in %Ps: %s.", gel(e,2),
    1399         518 :                         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        1590 :       const char *f = GSTR(gel(e,2));
    1452        1590 :       const char *v = GSTR(gel(e,3));
    1453        1590 :       const char *op= GSTR(gel(e,4));
    1454        1590 :       GEN l = gel(e,5);
    1455        1590 :       if (!*op)
    1456          28 :         return pari_sprintf("domain error in %s: %s out of range",f,v);
    1457        1562 :       return pari_sprintf("domain error in %s: %s %s %Ps",f,v,op,l);
    1458             :     }
    1459             :   case e_PRIORITY:
    1460             :     {
    1461          98 :       const char *f = GSTR(gel(e,2));
    1462          98 :       long vx = gvar(gel(e,3));
    1463          98 :       const char *op= GSTR(gel(e,4));
    1464          98 :       long v = itos(gel(e,5));
    1465          98 :       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          91 :     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        8430 :     return pari_sprintf("incorrect type in %Ps (%s).",
    1510        8430 :                         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        7860 : pari_err_display(GEN err)
    1525             : {
    1526        7860 :   long numerr=err_get_num(err);
    1527        7860 :   err_init();
    1528        7860 :   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        7825 :     char *s = pari_err2str(err);
    1538        7825 :     closure_err(0);
    1539        7825 :     err_init_msg(numerr==e_USER);
    1540        7825 :     pariErr->puts(s);
    1541        7825 :     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        7811 :     pari_free(s);
    1552             :   }
    1553        7846 :   out_term_color(pariErr, c_NONE);
    1554        7846 :   pariErr->flush(); return 0;
    1555             : }
    1556             : 
    1557             : void
    1558       22771 : pari_err(int numerr, ...)
    1559             : {
    1560             :   va_list ap;
    1561             :   GEN E;
    1562             : 
    1563       22771 :   va_start(ap,numerr);
    1564             : 
    1565       22771 :   if (numerr)
    1566       22755 :     E = pari_err2GEN(numerr,ap);
    1567             :   else
    1568             :   {
    1569          16 :     E = va_arg(ap,GEN);
    1570          16 :     numerr = err_get_num(E);
    1571             :   }
    1572       22771 :   global_err_data = E;
    1573       22771 :   if (*iferr_env) longjmp(*iferr_env, numerr);
    1574        7862 :   mt_err_recover(numerr);
    1575        7860 :   va_end(ap);
    1576       15706 :   if (cb_pari_err_handle &&
    1577        7860 :       cb_pari_err_handle(E)) return;
    1578       15685 :   if (cb_pari_handle_exception &&
    1579        7846 :       cb_pari_handle_exception(numerr)) return;
    1580        7839 :   err_recover(numerr);
    1581             : }
    1582             : 
    1583             : GEN
    1584       29810 : 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         215 : listcopy(GEN x)
    1727             : {
    1728         215 :   GEN y = mklist(), L = list_data(x);
    1729         215 :   if (L) list_data(y) = gcopy(L);
    1730         215 :   y[1] = evaltyp(list_typ(x));
    1731         215 :   return y;
    1732             : }
    1733             : 
    1734             : GEN
    1735  3382634690 : gcopy(GEN x)
    1736             : {
    1737  3382634690 :   long tx = typ(x), lx, i;
    1738             :   GEN y;
    1739  3382634690 :   switch(tx)
    1740             :   { /* non recursive types */
    1741  3040016564 :     case t_INT: return signe(x)? icopy(x): gen_0;
    1742             :     case t_REAL:
    1743             :     case t_STR:
    1744   190042069 :     case t_VECSMALL: return leafcopy(x);
    1745             :     /* one more special case */
    1746         215 :     case t_LIST: return listcopy(x);
    1747             :   }
    1748   152575842 :   y = cgetg_copy(x, &lx);
    1749   152575845 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1750   152575845 :   for (; i<lx; i++) gel(y,i) = gcopy(gel(x,i));
    1751   152575842 :   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   269897503 : cgetg_copy_avma(GEN x, long *plx, pari_sp *AVMA) {
    1780             :   GEN z;
    1781   269897503 :   *plx = lg(x);
    1782   269897503 :   z = ((GEN)*AVMA) - *plx;
    1783   269897503 :   z[0] = x[0] & (TYPBITS|LGBITS);
    1784   269897503 :   *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  2685470603 : gcopy_avma(GEN x, pari_sp *AVMA)
    1797             : {
    1798  2685470603 :   long i, lx, tx = typ(x);
    1799             :   GEN y;
    1800             : 
    1801  2685470603 :   switch(typ(x))
    1802             :   { /* non recursive types */
    1803             :     case t_INT:
    1804  2584774338 :       if (lgefint(x) == 2) return gen_0;
    1805  2122314109 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1806  2122314130 :       return (GEN)*AVMA;
    1807             :     case t_REAL: case t_STR: case t_VECSMALL:
    1808    23554941 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1809    23554941 :       return (GEN)*AVMA;
    1810             : 
    1811             :     /* one more special case */
    1812             :     case t_LIST:
    1813         133 :       y = cgetlist_avma(AVMA);
    1814         133 :       listassign(x, y); return y;
    1815             : 
    1816             :   }
    1817    77141191 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1818    77141203 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1819    77141203 :   for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), AVMA);
    1820    77141206 :   return y;
    1821             : }
    1822             : 
    1823             : /* [copy_bin/bin_copy:] same as gcopy_avma but use NULL to code an exact 0, and
    1824             :  * make shallow copies of finalized t_LISTs */
    1825             : static GEN
    1826  1033587289 : gcopy_av0(GEN x, pari_sp *AVMA)
    1827             : {
    1828  1033587289 :   long i, lx, tx = typ(x);
    1829             :   GEN y;
    1830             : 
    1831  1033587289 :   switch(tx)
    1832             :   { /* non recursive types */
    1833             :     case t_INT:
    1834   714355149 :       if (!signe(x)) return NULL; /* special marker */
    1835   354138883 :       *AVMA = (pari_sp)icopy_avma(x, *AVMA);
    1836   354138853 :       return (GEN)*AVMA;
    1837             :     case t_LIST:
    1838          49 :       if (list_data(x) && !list_nmax(x)) break; /* not finalized, need copy */
    1839             :       /* else finalized: shallow copy */
    1840             :     case t_REAL: case t_STR: case t_VECSMALL:
    1841   126475953 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1842   126475980 :       return (GEN)*AVMA;
    1843             :   }
    1844   192756187 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1845   192756194 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1846   192756194 :   for (; i<lx; i++) gel(y,i) = gcopy_av0(gel(x,i), AVMA);
    1847   192756330 :   return y;
    1848             : }
    1849             : 
    1850             : INLINE GEN
    1851          12 : icopy_avma_canon(GEN x, pari_sp AVMA)
    1852             : {
    1853          12 :   long i, lx = lgefint(x);
    1854          12 :   GEN y = ((GEN)AVMA) - lx;
    1855          12 :   y[0] = evaltyp(t_INT)|evallg(lx); /* kills isclone */
    1856          12 :   y[1] = x[1]; x = int_MSW(x);
    1857          12 :   for (i=2; i<lx; i++, x = int_precW(x)) y[i] = *x;
    1858          12 :   return y;
    1859             : }
    1860             : 
    1861             : /* [copy_bin_canon:] same as gcopy_av0, but copy integers in
    1862             :  * canonical (native kernel) form and make a full copy of t_LISTs */
    1863             : static GEN
    1864          28 : gcopy_av0_canon(GEN x, pari_sp *AVMA)
    1865             : {
    1866          28 :   long i, lx, tx = typ(x);
    1867             :   GEN y;
    1868             : 
    1869          28 :   switch(tx)
    1870             :   { /* non recursive types */
    1871             :     case t_INT:
    1872          20 :       if (!signe(x)) return NULL; /* special marker */
    1873          12 :       *AVMA = (pari_sp)icopy_avma_canon(x, *AVMA);
    1874          12 :       return (GEN)*AVMA;
    1875             :     case t_REAL: case t_STR: case t_VECSMALL:
    1876           0 :       *AVMA = (pari_sp)leafcopy_avma(x, *AVMA);
    1877           0 :       return (GEN)*AVMA;
    1878             : 
    1879             :     /* one more special case */
    1880             :     case t_LIST:
    1881             :     {
    1882           0 :       long t = list_typ(x);
    1883           0 :       GEN y = cgetlist_avma(AVMA), z = list_data(x);
    1884           0 :       if (z) {
    1885           0 :         list_data(y) = gcopy_av0_canon(z, AVMA);
    1886           0 :         y[1] = evaltyp(t)|evallg(lg(z)-1);
    1887             :       } else {
    1888           0 :         list_data(y) = NULL;
    1889           0 :         y[1] = evaltyp(t);
    1890             :       }
    1891           0 :       return y;
    1892             :     }
    1893             :   }
    1894           8 :   y = cgetg_copy_avma(x, &lx, AVMA);
    1895           8 :   if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    1896           8 :   for (; i<lx; i++) gel(y,i) = gcopy_av0_canon(gel(x,i), AVMA);
    1897           8 :   return y;
    1898             : }
    1899             : 
    1900             : /* [copy_bin/bin_copy:] size (number of words) required for
    1901             :  * gcopy_av0_canon(x) */
    1902             : static long
    1903          28 : taille0_canon(GEN x)
    1904             : {
    1905          28 :   long i,n,lx, tx = typ(x);
    1906          28 :   switch(tx)
    1907             :   { /* non recursive types */
    1908          20 :     case t_INT: return signe(x)? lgefint(x): 0;
    1909             :     case t_REAL:
    1910             :     case t_STR:
    1911           0 :     case t_VECSMALL: return lg(x);
    1912             : 
    1913             :     /* one more special case */
    1914             :     case t_LIST:
    1915             :     {
    1916           0 :       GEN L = list_data(x);
    1917           0 :       return L? 3 + taille0_canon(L): 3;
    1918             :     }
    1919             :   }
    1920           8 :   n = lx = lg(x);
    1921           8 :   for (i=lontyp[tx]; i<lx; i++) n += taille0_canon(gel(x,i));
    1922           8 :   return n;
    1923             : }
    1924             : 
    1925             : /* [copy_bin/bin_copy:] size (number of words) required for gcopy_av0(x) */
    1926             : static long
    1927  1033587867 : taille0(GEN x)
    1928             : {
    1929  1033587867 :   long i,n,lx, tx = typ(x);
    1930  1033587867 :   switch(tx)
    1931             :   { /* non recursive types */
    1932             :     case t_INT:
    1933   714355453 :       lx = lgefint(x);
    1934   714355453 :       return lx == 2? 0: lx;
    1935             :     case t_LIST:
    1936             :     {
    1937          49 :       GEN L = list_data(x);
    1938          49 :       if (L && !list_nmax(x)) break; /* not finalized, deep copy */
    1939             :     }
    1940             :     /* else finalized: shallow */
    1941             :     case t_REAL:
    1942             :     case t_STR:
    1943             :     case t_VECSMALL:
    1944   126476021 :       return lg(x);
    1945             :   }
    1946   192756393 :   n = lx = lg(x);
    1947   192756393 :   for (i=lontyp[tx]; i<lx; i++) n += taille0(gel(x,i));
    1948   192756343 :   return n;
    1949             : }
    1950             : 
    1951             : static long
    1952  2743275110 : gsizeclone_i(GEN x)
    1953             : {
    1954  2743275110 :   long i,n,lx, tx = typ(x);
    1955  2743275110 :   switch(tx)
    1956             :   { /* non recursive types */
    1957  2584949710 :     case t_INT: lx = lgefint(x); return lx == 2? 0: lx;;
    1958             :     case t_REAL:
    1959             :     case t_STR:
    1960    29479863 :     case t_VECSMALL: return lg(x);
    1961             : 
    1962         705 :     case t_LIST: return 3;
    1963             :     default:
    1964   128844832 :       n = lx = lg(x);
    1965   128844832 :       for (i=lontyp[tx]; i<lx; i++) n += gsizeclone_i(gel(x,i));
    1966   128844842 :       return n;
    1967             :   }
    1968             : }
    1969             : 
    1970             : /* #words needed to clone x; t_LIST is a special case since list_data() is
    1971             :  * malloc'ed later, in list_internal_copy() */
    1972             : static long
    1973   181013188 : gsizeclone(GEN x) { return (typ(x) == t_INT)? lgefint(x): gsizeclone_i(x); }
    1974             : 
    1975             : long
    1976        6811 : gsizeword(GEN x)
    1977             : {
    1978             :   GEN L;
    1979        6811 :   if (typ(x) != t_LIST) return gsizeclone(x);
    1980             :   /* For t_LIST, return the actual list size, gsizeclone() is always 3 */
    1981         147 :   L = list_data(x);
    1982         147 :   return L? 3 + gsizeclone(L): 3;
    1983             : }
    1984             : long
    1985         112 : gsizebyte(GEN x) { return gsizeword(x) * sizeof(long); }
    1986             : 
    1987             : /* return a clone of x structured as a gcopy */
    1988             : GENbin*
    1989    45293915 : copy_bin(GEN x)
    1990             : {
    1991    45293915 :   long t = taille0(x);
    1992    45293878 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    1993    45294002 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    1994    45293985 :   p->rebase = &shiftaddress;
    1995    45293985 :   p->len = t;
    1996    45293985 :   p->x   = gcopy_av0(x, &AVMA);
    1997    45293929 :   p->base= (GEN)AVMA; return p;
    1998             : }
    1999             : 
    2000             : /* same, writing t_INT in canonical native form */
    2001             : GENbin*
    2002          12 : copy_bin_canon(GEN x)
    2003             : {
    2004          12 :   long t = taille0_canon(x);
    2005          12 :   GENbin *p = (GENbin*)pari_malloc(sizeof(GENbin) + t*sizeof(long));
    2006          12 :   pari_sp AVMA = (pari_sp)(GENbinbase(p) + t);
    2007          12 :   p->rebase = &shiftaddress_canon;
    2008          12 :   p->len = t;
    2009          12 :   p->x   = gcopy_av0_canon(x, &AVMA);
    2010          12 :   p->base= (GEN)AVMA; return p;
    2011             : }
    2012             : 
    2013             : GEN
    2014   181006398 : gclone(GEN x)
    2015             : {
    2016   181006398 :   long i,lx,tx = typ(x), t = gsizeclone(x);
    2017   181006399 :   GEN y = newblock(t);
    2018   181006403 :   switch(tx)
    2019             :   { /* non recursive types */
    2020             :     case t_INT:
    2021   123494522 :       lx = lgefint(x);
    2022   123494522 :       y[0] = evaltyp(t_INT)|evallg(lx);
    2023   123494522 :       for (i=1; i<lx; i++) y[i] = x[i];
    2024   123494522 :       break;
    2025             :     case t_REAL:
    2026             :     case t_STR:
    2027             :     case t_VECSMALL:
    2028     5915559 :       lx = lg(x);
    2029     5915559 :       for (i=0; i<lx; i++) y[i] = x[i];
    2030     5915559 :       break;
    2031             : 
    2032             :     /* one more special case */
    2033             :     case t_LIST:
    2034         565 :       y[0] = evaltyp(t_LIST)|_evallg(3);
    2035         565 :       listassign(x, y);
    2036         565 :       break;
    2037             :     default: {
    2038    51595757 :       pari_sp AVMA = (pari_sp)(y + t);
    2039    51595757 :       lx = lg(x);
    2040    51595757 :       y[0] = x[0];
    2041    51595757 :       if (lontyp[tx] == 1) i = 1; else { y[1] = x[1]; i = 2; }
    2042    51595757 :       for (; i<lx; i++) gel(y,i) = gcopy_avma(gel(x,i), &AVMA);
    2043             :     }
    2044             :   }
    2045   181006399 :   setisclone(y); return y;
    2046             : }
    2047             : 
    2048             : void
    2049   673371191 : shiftaddress(GEN x, long dec)
    2050             : {
    2051   673371191 :   long i, lx, tx = typ(x);
    2052   673371191 :   if (is_recursive_t(tx))
    2053             :   {
    2054   192756385 :     if (tx == t_LIST)
    2055             :     {
    2056   673371223 :       if (!list_data(x) || list_nmax(x)) return; /* empty or finalized */
    2057             :       /* not finalized, update pointers  */
    2058             :     }
    2059   192756343 :     lx = lg(x);
    2060  1181050129 :     for (i=lontyp[tx]; i<lx; i++) {
    2061   988293754 :       if (!x[i]) gel(x,i) = gen_0;
    2062             :       else
    2063             :       {
    2064   628084076 :         x[i] += dec;
    2065   628084076 :         shiftaddress(gel(x,i), dec);
    2066             :       }
    2067             :     }
    2068             :   }
    2069             : }
    2070             : 
    2071             : void
    2072          20 : shiftaddress_canon(GEN x, long dec)
    2073             : {
    2074          20 :   long i, lx, tx = typ(x);
    2075          20 :   switch(tx)
    2076             :   { /* non recursive types */
    2077             :     case t_INT: {
    2078             :       GEN y;
    2079          12 :       lx = lgefint(x); if (lx <= 3) return;
    2080           0 :       y = x + 2;
    2081           0 :       x = int_MSW(x);  if (x == y) return;
    2082           0 :       while (x > y) { lswap(*x, *y); x = int_precW(x); y++; }
    2083           0 :       break;
    2084             :     }
    2085             :     case t_REAL:
    2086             :     case t_STR:
    2087             :     case t_VECSMALL:
    2088           0 :       break;
    2089             : 
    2090             :     /* one more special case */
    2091             :     case t_LIST: {
    2092           0 :       GEN Lx = list_data(x);
    2093           0 :       if (Lx) {
    2094           0 :         pari_sp av = avma;
    2095           0 :         GEN L = (GEN)((long)Lx+dec);
    2096           0 :         shiftaddress_canon(L, dec);
    2097           0 :         list_data(x) = list_internal_copy(L, lg(L)); avma = av;
    2098             :       }
    2099             :     }
    2100             :     default:
    2101           8 :       lx = lg(x);
    2102          24 :       for (i=lontyp[tx]; i<lx; i++) {
    2103          16 :         if (!x[i]) gel(x,i) = gen_0;
    2104             :         else
    2105             :         {
    2106           8 :           x[i] += dec;
    2107           8 :           shiftaddress_canon(gel(x,i), dec);
    2108             :         }
    2109             :       }
    2110             :   }
    2111             : }
    2112             : 
    2113             : /********************************************************************/
    2114             : /**                                                                **/
    2115             : /**                INSERT DYNAMIC OBJECT IN STRUCTURE              **/
    2116             : /**                                                                **/
    2117             : /********************************************************************/
    2118             : GEN
    2119          28 : obj_reinit(GEN S)
    2120             : {
    2121          28 :   GEN s, T = leafcopy(S);
    2122          28 :   long a = lg(T)-1;
    2123          28 :   s = gel(T,a);
    2124          28 :   gel(T,a) = zerovec(lg(s)-1);
    2125          28 :   return T;
    2126             : }
    2127             : 
    2128             : GEN
    2129     1848584 : obj_init(long d, long n)
    2130             : {
    2131     1848584 :   GEN S = cgetg(d+2, t_VEC);
    2132     1848584 :   gel(S, d+1) = zerovec(n);
    2133     1848585 :   return S;
    2134             : }
    2135             : /* insert O in S [last position] at position K, return it */
    2136             : GEN
    2137     1100327 : obj_insert(GEN S, long K, GEN O)
    2138     1100327 : { return obj_insert_shallow(S, K, gclone(O)); }
    2139             : /* as obj_insert. WITHOUT cloning (for libpari, when creating a *new* obj
    2140             :  * from an existing one) */
    2141             : GEN
    2142     1104177 : obj_insert_shallow(GEN S, long K, GEN O)
    2143             : {
    2144     1104177 :   GEN o, v = gel(S, lg(S)-1);
    2145     1104177 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_insert", S);
    2146     1104177 :   o = gel(v,K);
    2147     1104177 :   gel(v,K) = O; /*SIGINT: before unclone(o)*/
    2148     1104177 :   if (isclone(o)) gunclone(o);
    2149     1104177 :   return gel(v,K);
    2150             : }
    2151             : 
    2152             : /* Does S [last position] contain data at position K ? Return it, or NULL */
    2153             : GEN
    2154     2427725 : obj_check(GEN S, long K)
    2155             : {
    2156     2427725 :   GEN O, v = gel(S, lg(S)-1);
    2157     2427725 :   if (typ(v) != t_VEC || K >= lg(v)) pari_err_TYPE("obj_check", S);
    2158     2427725 :   O = gel(v,K); return isintzero(O)? NULL: O;
    2159             : }
    2160             : 
    2161             : GEN
    2162      742025 : obj_checkbuild(GEN S, long tag, GEN (*build)(GEN))
    2163             : {
    2164      742025 :   GEN O = obj_check(S, tag);
    2165      742025 :   if (!O)
    2166      634537 :   { pari_sp av = avma; O = obj_insert(S, tag, build(S)); avma = av; }
    2167      742025 :   return O;
    2168             : }
    2169             : 
    2170             : GEN
    2171       35372 : obj_checkbuild_prec(GEN S, long tag, GEN (*build)(GEN,long),
    2172             :   long (*pr)(GEN), long prec)
    2173             : {
    2174       35372 :   pari_sp av = avma;
    2175       35372 :   GEN w = obj_check(S, tag);
    2176       35372 :   if (!w || pr(w) < prec) w = obj_insert(S, tag, build(S, prec));
    2177       35372 :   avma = av; return gcopy(w);
    2178             : }
    2179             : GEN
    2180       11243 : obj_checkbuild_realprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2181       11243 : { return obj_checkbuild_prec(S,tag,build,gprecision,prec); }
    2182             : GEN
    2183         455 : obj_checkbuild_padicprec(GEN S, long tag, GEN (*build)(GEN,long), long prec)
    2184         455 : { return obj_checkbuild_prec(S,tag,build,padicprec_relative,prec); }
    2185             : 
    2186             : /* Reset S [last position], freeing all clones */
    2187             : void
    2188        9149 : obj_free(GEN S)
    2189             : {
    2190        9149 :   GEN v = gel(S, lg(S)-1);
    2191             :   long i;
    2192        9149 :   if (typ(v) != t_VEC) pari_err_TYPE("obj_free", S);
    2193       48601 :   for (i = 1; i < lg(v); i++)
    2194             :   {
    2195       39452 :     GEN o = gel(v,i);
    2196       39452 :     if (isclone(o)) gunclone(o);
    2197       39452 :     gel(v,i) = gen_0;
    2198             :   }
    2199        9149 : }
    2200             : 
    2201             : /*******************************************************************/
    2202             : /*                                                                 */
    2203             : /*                         STACK MANAGEMENT                        */
    2204             : /*                                                                 */
    2205             : /*******************************************************************/
    2206             : INLINE void
    2207  2131263008 : dec_gerepile(pari_sp *x, pari_sp av0, pari_sp av, pari_sp tetpil, size_t dec)
    2208             : {
    2209  2131263008 :   if (*x < av && *x >= av0)
    2210             :   { /* update address if in stack */
    2211  1725773046 :     if (*x < tetpil) *x += dec;
    2212           0 :     else pari_err_BUG("gerepile, significant pointers lost");
    2213             :   }
    2214  2131263008 : }
    2215             : 
    2216             : void
    2217      189367 : gerepileallsp(pari_sp av, pari_sp tetpil, int n, ...)
    2218             : {
    2219      189367 :   const pari_sp av0 = avma;
    2220      189367 :   const size_t dec = av-tetpil;
    2221             :   int i;
    2222      189367 :   va_list a; va_start(a, n);
    2223      189367 :   (void)gerepile(av,tetpil,NULL);
    2224      189367 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)va_arg(a,GEN*), av0,av,tetpil,dec);
    2225      189367 :   va_end(a);
    2226      189367 : }
    2227             : 
    2228             : /* Takes an array of pointers to GENs, of length n.
    2229             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2230             : void
    2231     5449723 : gerepilemanysp(pari_sp av, pari_sp tetpil, GEN* gptr[], int n)
    2232             : {
    2233     5449723 :   const pari_sp av0 = avma;
    2234     5449723 :   const size_t dec = av-tetpil;
    2235             :   int i;
    2236     5449723 :   (void)gerepile(av,tetpil,NULL);
    2237     5449723 :   for (i=0; i<n; i++) dec_gerepile((pari_sp*)gptr[i], av0, av, tetpil, dec);
    2238     5449723 : }
    2239             : 
    2240             : /* Takes an array of GENs (cast to longs), of length n.
    2241             :  * Cleans up the stack between av and tetpil, updating those GENs. */
    2242             : void
    2243   154793650 : gerepilecoeffssp(pari_sp av, pari_sp tetpil, long *g, int n)
    2244             : {
    2245   154793650 :   const pari_sp av0 = avma;
    2246   154793650 :   const size_t dec = av-tetpil;
    2247             :   int i;
    2248   154793650 :   (void)gerepile(av,tetpil,NULL);
    2249   154793650 :   for (i=0; i<n; i++,g++) dec_gerepile((pari_sp*)g, av0, av, tetpil, dec);
    2250   154793650 : }
    2251             : 
    2252             : static int
    2253           0 : dochk_gerepileupto(GEN av, GEN x)
    2254             : {
    2255             :   long i,lx,tx;
    2256           0 :   if (!isonstack(x)) return 1;
    2257           0 :   if (x > av)
    2258             :   {
    2259           0 :     pari_warn(warner,"bad object %Ps",x);
    2260           0 :     return 0;
    2261             :   }
    2262           0 :   tx = typ(x);
    2263           0 :   if (! is_recursive_t(tx)) return 1;
    2264             : 
    2265           0 :   lx = lg(x);
    2266           0 :   for (i=lontyp[tx]; i<lx; i++)
    2267           0 :     if (!dochk_gerepileupto(av, gel(x,i)))
    2268             :     {
    2269           0 :       pari_warn(warner,"bad component %ld in object %Ps",i,x);
    2270           0 :       return 0;
    2271             :     }
    2272           0 :   return 1;
    2273             : }
    2274             : /* check that x and all its components are out of stack, or have been
    2275             :  * created after av */
    2276             : int
    2277           0 : chk_gerepileupto(GEN x) { return dochk_gerepileupto(x, x); }
    2278             : 
    2279             : /* print stack between avma & av */
    2280             : void
    2281           0 : dbg_gerepile(pari_sp av)
    2282             : {
    2283           0 :   GEN x = (GEN)avma;
    2284           0 :   while (x < (GEN)av)
    2285             :   {
    2286           0 :     const long tx = typ(x), lx = lg(x);
    2287             :     GEN *a;
    2288             : 
    2289           0 :     pari_printf(" [%ld] %Ps:", x - (GEN)avma, x);
    2290           0 :     if (! is_recursive_t(tx)) { pari_putc('\n'); x += lx; continue; }
    2291           0 :     a = (GEN*)x + lontyp[tx]; x += lx;
    2292           0 :     for (  ; a < (GEN*)x; a++)
    2293             :     {
    2294           0 :       if (*a == gen_0)
    2295           0 :         pari_puts("  gen_0");
    2296           0 :       else if (*a == gen_1)
    2297           0 :         pari_puts("  gen_1");
    2298           0 :       else if (*a == gen_m1)
    2299           0 :         pari_puts("  gen_m1");
    2300           0 :       else if (*a == gen_2)
    2301           0 :         pari_puts("  gen_2");
    2302           0 :       else if (*a == gen_m2)
    2303           0 :         pari_puts("  gen_m2");
    2304           0 :       else if (*a == ghalf)
    2305           0 :         pari_puts("  ghalf");
    2306           0 :       else if (isclone(*a))
    2307           0 :         pari_printf("  %Ps (clone)", *a);
    2308             :       else
    2309           0 :         pari_printf("  %Ps [%ld]", *a, *a - (GEN)avma);
    2310           0 :       if (a+1 < (GEN*)x) pari_putc(',');
    2311             :     }
    2312           0 :     pari_printf("\n");
    2313             :   }
    2314           0 : }
    2315             : void
    2316           0 : dbg_gerepileupto(GEN q)
    2317             : {
    2318           0 :   err_printf("%Ps:\n", q);
    2319           0 :   dbg_gerepile((pari_sp) (q+lg(q)));
    2320           0 : }
    2321             : 
    2322             : GEN
    2323   474249624 : gerepile(pari_sp av, pari_sp tetpil, GEN q)
    2324             : {
    2325   474249624 :   const size_t dec = av - tetpil;
    2326   474249624 :   const pari_sp av0 = avma;
    2327             :   GEN x, a;
    2328             : 
    2329   474249624 :   if (dec == 0) return q;
    2330   402175623 :   if ((long)dec < 0) pari_err(e_MISC,"lbot>ltop in gerepile");
    2331             : 
    2332             :   /* dec_gerepile(&q, av0, av, tetpil, dec), saving 1 comparison */
    2333   402167778 :   if (q >= (GEN)av0 && q < (GEN)tetpil)
    2334   245475043 :     q = (GEN) (((pari_sp)q) + dec);
    2335             : 
    2336   402167778 :   for (x = (GEN)av, a = (GEN)tetpil; a > (GEN)av0; ) *--x = *--a;
    2337   402167778 :   avma = (pari_sp)x;
    2338  2897589117 :   while (x < (GEN)av)
    2339             :   {
    2340  2093244524 :     const long tx = typ(x), lx = lg(x);
    2341             : 
    2342  2093244524 :     if (! is_recursive_t(tx)) { x += lx; continue; }
    2343   451269798 :     a = x + lontyp[tx]; x += lx;
    2344   451269798 :     for (  ; a < x; a++) dec_gerepile((pari_sp*)a, av0, av, tetpil, dec);
    2345             :   }
    2346   402176815 :   return q;
    2347             : }
    2348             : 
    2349             : void
    2350           0 : fill_stack(void)
    2351             : {
    2352           0 :   GEN x = ((GEN)pari_mainstack->bot);
    2353           0 :   while (x < (GEN)avma) *x++ = 0xfefefefeUL;
    2354           0 : }
    2355             : 
    2356             : void
    2357           0 : debug_stack(void)
    2358             : {
    2359           0 :   pari_sp top = pari_mainstack->top, bot = pari_mainstack->bot;
    2360             :   GEN z;
    2361           0 :   err_printf("bot=0x%lx\ttop=0x%lx\tavma=0x%lx\n", bot, top, avma);
    2362           0 :   for (z = ((GEN)top)-1; z >= (GEN)avma; z--)
    2363           0 :     err_printf("%p:\t0x%lx\t%lu\n",z,*z,*z);
    2364           0 : }
    2365             : 
    2366             : void
    2367           0 : setdebugvar(long n) { DEBUGVAR=n; }
    2368             : 
    2369             : long
    2370           0 : getdebugvar(void) { return DEBUGVAR; }
    2371             : 
    2372             : long
    2373           7 : getstack(void) { return pari_mainstack->top-avma; }
    2374             : 
    2375             : /*******************************************************************/
    2376             : /*                                                                 */
    2377             : /*                               timer_delay                             */
    2378             : /*                                                                 */
    2379             : /*******************************************************************/
    2380             : 
    2381             : #if defined(USE_CLOCK_GETTIME)
    2382             : #if defined(_POSIX_THREAD_CPUTIME)
    2383             : static THREAD clockid_t time_type = CLOCK_THREAD_CPUTIME_ID;
    2384             : #else
    2385             : static const THREAD clockid_t time_type = CLOCK_PROCESS_CPUTIME_ID;
    2386             : #endif
    2387             : static void
    2388             : pari_init_timer(void)
    2389             : {
    2390             : #if defined(_POSIX_THREAD_CPUTIME)
    2391             :   time_type = CLOCK_PROCESS_CPUTIME_ID;
    2392             : #endif
    2393             : }
    2394             : 
    2395             : void
    2396             : timer_start(pari_timer *T)
    2397             : {
    2398             :   struct timespec t;
    2399             :   clock_gettime(time_type,&t);
    2400             :   T->us = t.tv_nsec / 1000;
    2401             :   T->s  = t.tv_sec;
    2402             : }
    2403             : #elif defined(USE_GETRUSAGE)
    2404             : #ifdef RUSAGE_THREAD
    2405             : static THREAD int rusage_type = RUSAGE_THREAD;
    2406             : #else
    2407             : static const THREAD int rusage_type = RUSAGE_SELF;
    2408             : #endif /*RUSAGE_THREAD*/
    2409             : static void
    2410        1503 : pari_init_timer(void)
    2411             : {
    2412             : #ifdef RUSAGE_THREAD
    2413        1503 :   rusage_type = RUSAGE_SELF;
    2414             : #endif
    2415        1503 : }
    2416             : 
    2417             : void
    2418      493945 : timer_start(pari_timer *T)
    2419             : {
    2420             :   struct rusage r;
    2421      493945 :   getrusage(rusage_type,&r);
    2422      493945 :   T->us = r.ru_utime.tv_usec;
    2423      493945 :   T->s  = r.ru_utime.tv_sec;
    2424      493945 : }
    2425             : #elif defined(USE_FTIME)
    2426             : 
    2427             : static void
    2428             : pari_init_timer(void) { }
    2429             : 
    2430             : void
    2431             : timer_start(pari_timer *T)
    2432             : {
    2433             :   struct timeb t;
    2434             :   ftime(&t);
    2435             :   T->us = ((long)t.millitm) * 1000;
    2436             :   T->s  = t.time;
    2437             : }
    2438             : 
    2439             : #else
    2440             : 
    2441             : static void
    2442             : _get_time(pari_timer *T, long Ticks, long TickPerSecond)
    2443             : {
    2444             :   T->us = (long) ((Ticks % TickPerSecond) * (1000000. / TickPerSecond));
    2445             :   T->s  = Ticks / TickPerSecond;
    2446             : }
    2447             : 
    2448             : # ifdef USE_TIMES
    2449             : static void
    2450             : pari_init_timer(void) { }
    2451             : 
    2452             : void
    2453             : timer_start(pari_timer *T)
    2454             : {
    2455             : # ifdef _SC_CLK_TCK
    2456             :   long tck = sysconf(_SC_CLK_TCK);
    2457             : # else
    2458             :   long tck = CLK_TCK;
    2459             : # endif
    2460             :   struct tms t; times(&t);
    2461             :   _get_time(T, t.tms_utime, tck);
    2462             : }
    2463             : # elif defined(_WIN32)
    2464             : static void
    2465             : pari_init_timer(void) { }
    2466             : 
    2467             : void
    2468             : timer_start(pari_timer *T)
    2469             : { _get_time(T, win32_timer(), 1000); }
    2470             : # else
    2471             : #  include <time.h>
    2472             : #  ifndef CLOCKS_PER_SEC
    2473             : #   define CLOCKS_PER_SEC 1000000 /* may be false on YOUR system */
    2474             : #  endif
    2475             : static void
    2476             : pari_init_timer(void) { }
    2477             : 
    2478             : void
    2479             : timer_start(pari_timer *T)
    2480             : { _get_time(T, clock(), CLOCKS_PER_SEC); }
    2481             : # endif
    2482             : #endif
    2483             : 
    2484             : static long
    2485       63645 : timer_aux(pari_timer *T, pari_timer *U)
    2486             : {
    2487       63645 :   long s = T->s, us = T->us; timer_start(U);
    2488       63645 :   return 1000 * (U->s - s) + (U->us - us + 500) / 1000;
    2489             : }
    2490             : /* return delay, reset timer */
    2491             : long
    2492       62124 : timer_delay(pari_timer *T) { return timer_aux(T, T); }
    2493             : /* return delay, don't reset timer */
    2494             : long
    2495        1521 : timer_get(pari_timer *T) { pari_timer t; return timer_aux(T, &t); }
    2496             : 
    2497             : static void
    2498           0 : timer_vprintf(pari_timer *T, const char *format, va_list args)
    2499             : {
    2500           0 :   out_puts(pariErr, "Time ");
    2501           0 :   out_vprintf(pariErr, format,args);
    2502           0 :   out_printf(pariErr, ": %ld\n", timer_delay(T));
    2503           0 :   pariErr->flush();
    2504           0 : }
    2505             : void
    2506           0 : timer_printf(pari_timer *T, const char *format, ...)
    2507             : {
    2508           0 :   va_list args; va_start(args, format);
    2509           0 :   timer_vprintf(T, format, args);
    2510           0 :   va_end(args);
    2511           0 : }
    2512             : 
    2513             : long
    2514           0 : timer(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2515             : long
    2516        2948 : gettime(void)  { static THREAD pari_timer T; return timer_delay(&T);}
    2517             : 
    2518             : static THREAD pari_timer timer2_T, abstimer_T;
    2519             : long
    2520           0 : timer2(void) {  return timer_delay(&timer2_T);}
    2521             : void
    2522           0 : msgtimer(const char *format, ...)
    2523             : {
    2524           0 :   va_list args; va_start(args, format);
    2525           0 :   timer_vprintf(&timer2_T, format, args);
    2526           0 :   va_end(args);
    2527           0 : }
    2528             : long
    2529        1515 : getabstime(void)  { return timer_get(&abstimer_T);}
    2530             : #if defined(USE_CLOCK_GETTIME) || defined(USE_GETTIMEOFDAY) \
    2531             :  || defined(USE_FTIMEFORWALLTIME)
    2532             : static GEN
    2533           8 : timetoi(ulong s, ulong m)
    2534             : {
    2535           8 :   pari_sp av = avma;
    2536           8 :   GEN r = addiu(muliu(utoi(s), 1000), m);
    2537           8 :   return gerepileuptoint(av, r);
    2538             : }
    2539             : #endif
    2540             : GEN
    2541           8 : getwalltime(void)
    2542             : {
    2543             : #if defined(USE_CLOCK_GETTIME)
    2544             :   struct timespec t;
    2545             :   if (!clock_gettime(CLOCK_REALTIME,&t))
    2546             :     return timetoi(t.tv_sec, (t.tv_nsec + 500000)/1000000);
    2547             : #elif defined(USE_GETTIMEOFDAY)
    2548             :   struct timeval tv;
    2549           8 :   if (!gettimeofday(&tv, NULL))
    2550           8 :     return timetoi(tv.tv_sec, (tv.tv_usec + 500)/1000);
    2551             : #elif defined(USE_FTIMEFORWALLTIME)
    2552             :   struct timeb tp;
    2553             :   ftime(&tp); return timetoi(tp.time, tp.millitm);
    2554             : #endif
    2555           0 :   return utoi(getabstime());
    2556             : }
    2557             : 
    2558             : /*******************************************************************/
    2559             : /*                                                                 */
    2560             : /*                   FUNCTIONS KNOWN TO THE ANALYZER               */
    2561             : /*                                                                 */
    2562             : /*******************************************************************/
    2563             : GEN
    2564           7 : pari_version(void)
    2565             : {
    2566           7 :   const ulong mask = (1UL<<PARI_VERSION_SHIFT) - 1;
    2567           7 :   ulong major, minor, patch, n = paricfg_version_code;
    2568           7 :   patch = n & mask; n >>= PARI_VERSION_SHIFT;
    2569           7 :   minor = n & mask; n >>= PARI_VERSION_SHIFT;
    2570           7 :   major = n;
    2571           7 :   if (*paricfg_vcsversion) {
    2572           7 :     const char *ver = paricfg_vcsversion;
    2573           7 :     const char *s = strchr(ver, '-');
    2574             :     char t[8];
    2575           7 :     const long len = s-ver;
    2576             :     GEN v;
    2577           7 :     if (!s || len > 6) pari_err_BUG("pari_version()"); /* paranoia */
    2578           7 :     memcpy(t, ver, len); t[len] = 0;
    2579           7 :     v = cgetg(6, t_VEC);
    2580           7 :     gel(v,1) = utoi(major);
    2581           7 :     gel(v,2) = utoi(minor);
    2582           7 :     gel(v,3) = utoi(patch);
    2583           7 :     gel(v,4) = stoi( atoi(t) );
    2584           7 :     gel(v,5) = strtoGENstr(s+1);
    2585           7 :     return v;
    2586             :   } else {
    2587           0 :     GEN v = cgetg(4, t_VEC);
    2588           0 :     gel(v,1) = utoi(major);
    2589           0 :     gel(v,2) = utoi(minor);
    2590           0 :     gel(v,3) = utoi(patch);
    2591           0 :     return v;
    2592             :   }
    2593             : }
    2594             : 
    2595             : /* List of GP functions: generated from the description system.
    2596             :  * Format (struct entree) :
    2597             :  *   char *name   : name (under GP).
    2598             :  *   ulong valence: (EpNEW, EpALIAS,EpVAR, EpINSTALL)|EpSTATIC
    2599             :  *   void *value  : For PREDEFINED FUNCTIONS: C function to call.
    2600             :  *                  For USER FUNCTIONS: pointer to defining data (block) =
    2601             :  *                   entree*: NULL, list of entree (arguments), NULL
    2602             :  *                   char*  : function text
    2603             :  *   long menu    : which help section do we belong to
    2604             :  *                   1: Standard monadic or dyadic OPERATORS
    2605             :  *                   2: CONVERSIONS and similar elementary functions
    2606             :  *                   3: functions related to COMBINATORICS
    2607             :  *                   4: TRANSCENDENTAL functions, etc.
    2608             :  *   char *code   : GP prototype, aka Parser Code (see libpari's manual)
    2609             :  *                  if NULL, use valence instead.
    2610             :  *   char *help   : short help text (init to NULL).
    2611             :  *   void *pvalue : push_val history.
    2612             :  *   long arity   : maximum number of arguments.
    2613             :  *   entree *next : next entree (init to NULL, used in hashing code). */
    2614             : #include "init.h"
    2615             : #include "default.h"

Generated by: LCOV version 1.11