LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_instruction.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 481 629 76.5 %
Date: 2024-10-07 21:21:43 Functions: 42 50 84.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * (author)  Author M. Kersten
      15             :  * For documentation see website
      16             :  */
      17             : #include "monetdb_config.h"
      18             : #include "mal_instruction.h"
      19             : #include "mal_function.h"             /* for getPC() */
      20             : #include "mal_utils.h"
      21             : #include "mal_exception.h"
      22             : #include "mal_private.h"
      23             : 
      24             : /* to avoid memory fragmentation stmt and var blocks are allocated in chunks */
      25             : #define MALCHUNK 256
      26             : 
      27             : /* If we encounter an error it can be left behind in the MalBlk
      28             :  * for the upper layers to abandon the track
      29             :  */
      30             : void
      31           0 : addMalException(MalBlkPtr mb, str msg)
      32             : {
      33           0 :         if (msg == NULL)
      34             :                 return;
      35           0 :         if (mb->errors) {
      36           0 :                 mb->errors = concatErrors(mb->errors, msg);
      37             :         } else {
      38           0 :                 mb->errors = dupError(msg);
      39             :         }
      40             : }
      41             : 
      42             : Symbol
      43     3406834 : newSymbol(const char *nme, int kind)
      44             : {
      45     3406834 :         Symbol cur;
      46             : 
      47     3406834 :         assert(kind == COMMANDsymbol || kind == PATTERNsymbol || kind == FUNCTIONsymbol);
      48     3406834 :         if (nme == NULL)
      49             :                 return NULL;
      50     3406834 :         cur = (Symbol) GDKzalloc(sizeof(SymRecord));
      51     3406834 :         if (cur == NULL)
      52             :                 return NULL;
      53     3406834 :         cur->name = putName(nme);
      54     3406834 :         if (cur->name == NULL) {
      55           0 :                 GDKfree(cur);
      56           0 :                 return NULL;
      57             :         }
      58     3406834 :         cur->kind = kind;
      59     3406834 :         cur->peer = NULL;
      60     3406834 :         if (kind == FUNCTIONsymbol) {
      61       50415 :                 cur->def = newMalBlk(STMT_INCREMENT);
      62       50415 :                 if (cur->def == NULL) {
      63           0 :                         GDKfree(cur);
      64           0 :                         return NULL;
      65             :                 }
      66             :         }
      67             :         return cur;
      68             : }
      69             : 
      70             : void
      71     3386821 : freeSymbol(Symbol s)
      72             : {
      73     3386821 :         if (s == NULL)
      74             :                 return;
      75     3386821 :         if (s->def) {
      76       50414 :                 freeMalBlk(s->def);
      77       50414 :                 s->def = NULL;
      78     3336407 :         } else if (s->allocated && s->func) {
      79     1633125 :                 GDKfree(s->func->comment);
      80     1633125 :                 GDKfree((char*)s->func->cname);
      81     1633125 :                 GDKfree(s->func->args);
      82     1633125 :                 GDKfree(s->func);
      83             :         }
      84     3386821 :         GDKfree(s);
      85             : }
      86             : 
      87             : void
      88      198315 : freeSymbolList(Symbol s)
      89             : {
      90      198315 :         Symbol t = s;
      91             : 
      92     3584622 :         while (s) {
      93     3386307 :                 t = s->peer;
      94     3386307 :                 s->peer = NULL;
      95     3386307 :                 freeSymbol(s);
      96     3386307 :                 s = t;
      97             :         }
      98      198315 : }
      99             : 
     100             : int
     101     4020476 : newMalBlkStmt(MalBlkPtr mb, int maxstmts)
     102             : {
     103     4020476 :         InstrPtr *p;
     104     4020476 :         maxstmts = maxstmts % MALCHUNK == 0 ? maxstmts : ((maxstmts / MALCHUNK) + 1) * MALCHUNK;
     105             : 
     106     4020476 :         p = (InstrPtr *) GDKzalloc(sizeof(InstrPtr) * maxstmts);
     107     4021632 :         if (p == NULL)
     108             :                 return -1;
     109     4021632 :         mb->stmt = p;
     110     4021632 :         mb->stop = 0;
     111     4021632 :         mb->ssize = maxstmts;
     112     4021632 :         return 0;
     113             : }
     114             : 
     115             : MalBlkPtr
     116       62800 : newMalBlk(int elements)
     117             : {
     118       62800 :         MalBlkPtr mb;
     119       62800 :         VarRecord *v;
     120             : 
     121       62800 :         mb = (MalBlkPtr) GDKmalloc(sizeof(MalBlkRecord));
     122       62802 :         if (mb == NULL)
     123             :                 return NULL;
     124             : 
     125             :         /* each MAL instruction implies at least one variable
     126             :          * we reserve some extra for constants */
     127       62802 :         assert(elements >= 0);
     128       62802 :         elements += 8;
     129       62802 :         if (elements % MALCHUNK != 0)
     130       62806 :                 elements = (elements / MALCHUNK + 1) * MALCHUNK;
     131       62802 :         v = (VarRecord *) GDKzalloc(sizeof(VarRecord) * elements);
     132       62808 :         if (v == NULL) {
     133           0 :                 GDKfree(mb);
     134           0 :                 return NULL;
     135             :         }
     136       62808 :         *mb = (MalBlkRecord) {
     137             :                 .var = v,
     138             :                 .vsize = elements,
     139             :                 .maxarg = MAXARG,               /* the minimum for each instruction */
     140             :                 .workers = ATOMIC_VAR_INIT(1),
     141             :         };
     142       62808 :         if (newMalBlkStmt(mb, elements) < 0) {
     143           0 :                 GDKfree(mb->var);
     144           0 :                 GDKfree(mb);
     145           0 :                 return NULL;
     146             :         }
     147             :         return mb;
     148             : }
     149             : 
     150             : int
     151       71786 : resizeMalBlk(MalBlkPtr mb, int elements)
     152             : {
     153       71786 :         int i;
     154       71786 :         assert(elements >= 0);
     155       71786 :         if (elements % MALCHUNK != 0)
     156       10457 :                 elements = (elements / MALCHUNK + 1) * MALCHUNK;
     157             : 
     158       71786 :         if (elements > mb->ssize) {
     159       61329 :                 InstrPtr *ostmt = mb->stmt;
     160       61329 :                 mb->stmt = GDKrealloc(mb->stmt, elements * sizeof(InstrPtr));
     161       61329 :                 if (mb->stmt) {
     162    15645647 :                         for (i = mb->ssize; i < elements; i++)
     163    15584318 :                                 mb->stmt[i] = 0;
     164       61329 :                         mb->ssize = elements;
     165             :                 } else {
     166           0 :                         mb->stmt = ostmt;    /* reinstate old pointer */
     167           0 :                         mb->errors = createMalException(mb, 0, TYPE,
     168             :                                                                                         SQLSTATE(HY013) MAL_MALLOC_FAIL);
     169           0 :                         return -1;
     170             :                 }
     171             :         }
     172             :         return 0;
     173             : }
     174             : 
     175             : /* For a MAL session we have to keep the variables around
     176             :  * and only need to reset the instruction pointer
     177             :  */
     178             : void
     179      584159 : resetMalTypes(MalBlkPtr mb, int stop)
     180             : {
     181      584159 :         int i;
     182             : 
     183    28193067 :         for (i = 0; i < stop; i++)
     184    27608908 :                 mb->stmt[i]->typeresolved = false;
     185      584159 :         mb->stop = stop;
     186      584159 :         mb->errors = NULL;
     187      584159 : }
     188             : 
     189             : /* For SQL operations we have to cleanup variables and trim the space
     190             :  * A portion is retained for the next query */
     191             : void
     192      569668 : resetMalBlk(MalBlkPtr mb)
     193             : {
     194      569668 :         int i;
     195      569668 :         InstrPtr *new;
     196      569668 :         VarRecord *vnew;
     197             : 
     198   153678203 :         for (i = 1/*MALCHUNK*/; i < mb->ssize; i++) {
     199   153108488 :                 freeInstruction(mb->stmt[i]);
     200   153108535 :                 mb->stmt[i] = NULL;
     201             :         }
     202      569715 :         if (mb->ssize != MALCHUNK) {
     203       16945 :                 new = GDKrealloc(mb->stmt, sizeof(InstrPtr) * MALCHUNK);
     204       16945 :                 if (new == NULL) {
     205             :                         /* the only place to return an error signal at this stage. */
     206             :                         /* The Client context should be passed around more deeply */
     207           0 :                         mb->errors = createMalException(mb, 0, TYPE,
     208             :                                                                                         SQLSTATE(HY013) MAL_MALLOC_FAIL);
     209           0 :                         return;
     210             :                 }
     211       16945 :                 mb->stmt = new;
     212       16945 :                 mb->ssize = MALCHUNK;
     213             :         }
     214             :         /* Reuse the initial function statement */
     215      569715 :         mb->stop = 1;
     216             : 
     217    64211580 :         for (i = 0; i < mb->vtop; i++) {
     218    63641950 :                 if (mb->var[i].name)
     219      353315 :                         GDKfree(mb->var[i].name);
     220    63641802 :                 mb->var[i].name = NULL;
     221    63641802 :                 if (isVarConstant(mb, i))
     222    18200526 :                         VALclear(&getVarConstant(mb, i));
     223             :         }
     224             : 
     225      569630 :         if (mb->vsize != MALCHUNK) {
     226       25769 :                 vnew = GDKrealloc(mb->var, sizeof(VarRecord) * MALCHUNK);
     227       25769 :                 if (vnew == NULL) {
     228             :                         /* the only place to return an error signal at this stage. */
     229             :                         /* The Client context should be passed around more deeply */
     230           0 :                         mb->errors = createMalException(mb, 0, TYPE,
     231             :                                                                                         SQLSTATE(HY013) MAL_MALLOC_FAIL);
     232           0 :                         return;
     233             :                 }
     234       25769 :                 mb->var = vnew;
     235       25769 :                 mb->vsize = MALCHUNK;
     236             :         }
     237      569630 :         mb->vtop = 0;
     238             : }
     239             : 
     240             : 
     241             : /* The freeMalBlk code is quite defensive. It is used to localize an
     242             :  * illegal reuse of a MAL blk. */
     243             : void
     244       63027 : freeMalBlk(MalBlkPtr mb)
     245             : {
     246       63027 :         int i;
     247             : 
     248    16296075 :         for (i = 0; i < mb->ssize; i++)
     249    16233041 :                 if (mb->stmt[i]) {
     250      177316 :                         freeInstruction(mb->stmt[i]);
     251      177323 :                         mb->stmt[i] = NULL;
     252             :                 }
     253       63034 :         mb->stop = 0;
     254      349165 :         for (i = 0; i < mb->vtop; i++) {
     255      286130 :                 if (mb->var[i].name)
     256       10309 :                         GDKfree(mb->var[i].name);
     257      286131 :                 mb->var[i].name = NULL;
     258      286131 :                 if (isVarConstant(mb, i))
     259       61079 :                         VALclear(&getVarConstant(mb, i));
     260             :         }
     261       63035 :         mb->vtop = 0;
     262       63035 :         GDKfree(mb->stmt);
     263       63035 :         mb->stmt = 0;
     264       63035 :         GDKfree(mb->var);
     265       63035 :         mb->var = 0;
     266             : 
     267       63035 :         mb->binding[0] = 0;
     268       63035 :         mb->tag = 0;
     269       63035 :         mb->memory = 0;
     270       63035 :         if (mb->help)
     271           0 :                 GDKfree(mb->help);
     272       63035 :         mb->help = 0;
     273       63035 :         mb->inlineProp = 0;
     274       63035 :         mb->unsafeProp = 0;
     275       63035 :         freeException(mb->errors);
     276       63033 :         GDKfree(mb);
     277       63035 : }
     278             : 
     279             : /* The routine below should assure that all referenced structures are
     280             :  * private. The copying is memory conservative. */
     281             : MalBlkPtr
     282         228 : copyMalBlk(MalBlkPtr old)
     283             : {
     284         228 :         MalBlkPtr mb;
     285         228 :         int i;
     286             : 
     287         228 :         mb = (MalBlkPtr) GDKzalloc(sizeof(MalBlkRecord));
     288         228 :         if (mb == NULL)
     289             :                 return NULL;
     290             : 
     291         228 :         mb->var = (VarRecord *) GDKzalloc(sizeof(VarRecord) * old->vsize);
     292         228 :         if (mb->var == NULL) {
     293           0 :                 GDKfree(mb);
     294           0 :                 return NULL;
     295             :         }
     296             : 
     297         228 :         mb->vsize = old->vsize;
     298             : 
     299             :         /* copy all variable records */
     300       11664 :         for (i = 0; i < old->vtop; i++) {
     301       11436 :                 mb->var[i] = old->var[i];
     302       11436 :                 if (mb->var[i].name) {
     303         613 :                         mb->var[i].name = GDKstrdup(mb->var[i].name);
     304         613 :                         if (!mb->var[i].name)
     305           0 :                                 goto bailout;
     306             :                 }
     307       11436 :                 if (VALcopy(&(mb->var[i].value), &(old->var[i].value)) == NULL) {
     308           0 :                         mb->vtop = i;
     309           0 :                         goto bailout;
     310             :                 }
     311             :         }
     312         228 :         mb->vtop = old->vtop;
     313             : 
     314         228 :         mb->stmt = (InstrPtr *) GDKzalloc(sizeof(InstrPtr) * old->ssize);
     315         228 :         if (mb->stmt == NULL) {
     316           0 :                 goto bailout;
     317             :         }
     318             : 
     319         228 :         mb->ssize = old->ssize;
     320         228 :         assert(old->stop < old->ssize);
     321       10887 :         for (i = 0; i < old->stop; i++) {
     322       10659 :                 mb->stmt[i] = copyInstruction(old->stmt[i]);
     323       10659 :                 if (mb->stmt[i] == NULL) {
     324           0 :                         mb->stop = i;
     325           0 :                         goto bailout;
     326             :                 }
     327             :         }
     328         228 :         mb->stop = old->stop;
     329         228 :         if (old->help && (mb->help = GDKstrdup(old->help)) == NULL) {
     330           0 :                 goto bailout;
     331             :         }
     332             : 
     333         228 :         strcpy_len(mb->binding, old->binding, sizeof(mb->binding));
     334         228 :         mb->errors = old->errors ? GDKstrdup(old->errors) : 0;
     335         228 :         mb->tag = old->tag;
     336         228 :         mb->runtime = old->runtime;
     337         228 :         mb->calls = old->calls;
     338         228 :         mb->optimize = old->optimize;
     339         228 :         mb->maxarg = old->maxarg;
     340         228 :         mb->inlineProp = old->inlineProp;
     341         228 :         mb->unsafeProp = old->unsafeProp;
     342         228 :         return mb;
     343             : 
     344             :   bailout:
     345           0 :         for (i = 0; i < old->stop; i++)
     346           0 :                 freeInstruction(mb->stmt[i]);
     347           0 :         for (i = 0; i < old->vtop; i++) {
     348           0 :                 if (mb->var[i].name)
     349           0 :                         GDKfree(mb->var[i].name);
     350           0 :                 VALclear(&mb->var[i].value);
     351             :         }
     352           0 :         GDKfree(mb->var);
     353           0 :         GDKfree(mb->stmt);
     354           0 :         GDKfree(mb);
     355           0 :         return NULL;
     356             : }
     357             : 
     358             : /* The MAL records should be managed from a pool to
     359             :  * avoid repeated alloc/free and reduce probability of
     360             :  * memory fragmentation. (todo)
     361             :  * The complicating factor is their variable size,
     362             :  * which leads to growing records as a result of pushArguments
     363             :  * Allocation of an instruction should always succeed.
     364             :  */
     365             : InstrPtr
     366    33774535 : newInstructionArgs(MalBlkPtr mb, const char *modnme, const char *fcnnme,
     367             :                                    int args)
     368             : {
     369    33774535 :         InstrPtr p;
     370             : 
     371    33774535 :         if (mb && mb->errors)
     372             :                 return NULL;
     373    33774520 :         if (args <= 0)
     374             :                 args = 1;
     375    33774520 :         p = GDKmalloc(args * sizeof(p->argv[0]) + offsetof(InstrRecord, argv));
     376    33778904 :         if (p == NULL) {
     377           0 :                 if (mb)
     378           0 :                         mb->errors = createMalException(mb, 0, TYPE,
     379             :                                                                                         SQLSTATE(HY013) MAL_MALLOC_FAIL);
     380           0 :                 return NULL;
     381             :         }
     382    33778904 :         *p = (InstrRecord) {
     383             :                 .maxarg = args,
     384             :                 .typeresolved = false,
     385             :                 .modname = modnme,
     386             :                 .fcnname = fcnnme,
     387             :                 .argc = 1,
     388             :                 .retc = 1,
     389             :                 /* Flow of control instructions are always marked as an assignment
     390             :                  * with modifier */
     391             :                 .token = ASSIGNsymbol,
     392             :         };
     393    33778904 :         memset(p->argv, 0, args * sizeof(p->argv[0]));
     394    33778904 :         p->argv[0] = -1;
     395    33778904 :         return p;
     396             : }
     397             : 
     398             : InstrPtr
     399     4367905 : newInstruction(MalBlkPtr mb, const char *modnme, const char *fcnnme)
     400             : {
     401     4367905 :         return newInstructionArgs(mb, modnme, fcnnme, MAXARG);
     402             : }
     403             : 
     404             : InstrPtr
     405    23407380 : copyInstructionArgs(const InstrRecord *p, int args)
     406             : {
     407    23407380 :         if (args < p->maxarg)
     408             :                 args = p->maxarg;
     409    23407380 :         InstrPtr new = (InstrPtr) GDKmalloc(offsetof(InstrRecord, argv) +
     410             :                                                                                 args * sizeof(p->argv[0]));
     411    23407815 :         if (new == NULL)
     412             :                 return new;
     413    23407815 :         memcpy(new, p,
     414    23407815 :                    offsetof(InstrRecord, argv) + p->maxarg * sizeof(p->argv[0]));
     415    23407815 :         if (args > p->maxarg)
     416     3311059 :                 memset(new->argv + p->maxarg, 0,
     417     3311059 :                            (args - p->maxarg) * sizeof(new->argv[0]));
     418    23407815 :         new->typeresolved = false;
     419    23407815 :         new->maxarg = args;
     420    23407815 :         return new;
     421             : }
     422             : 
     423             : InstrPtr
     424    16411097 : copyInstruction(const InstrRecord *p)
     425             : {
     426    16411097 :         return copyInstructionArgs(p, p->maxarg);
     427             : }
     428             : 
     429             : void
     430      639959 : clrFunction(InstrPtr p)
     431             : {
     432      639959 :         p->token = ASSIGNsymbol;
     433      639959 :         p->fcn = 0;
     434      639959 :         p->blk = 0;
     435      639959 :         p->typeresolved = false;
     436      639959 :         setModuleId(p, NULL);
     437      639984 :         setFunctionId(p, NULL);
     438      640055 : }
     439             : 
     440             : void
     441           0 : clrInstruction(InstrPtr p)
     442             : {
     443           0 :         clrFunction(p);
     444           0 :         memset(p, 0, offsetof(InstrRecord, argv) + p->maxarg * sizeof(p->argv[0]));
     445           0 : }
     446             : 
     447             : void
     448   180245562 : freeInstruction(InstrPtr p)
     449             : {
     450   180245562 :         GDKfree(p);
     451   180149931 : }
     452             : 
     453             : /* Query optimizers walk their way through a MAL program block. They
     454             :  * require some primitives to move instructions around and to remove
     455             :  * superfluous instructions. The removal is based on the assumption
     456             :  * that indeed the instruction belonged to the block. */
     457             : void
     458           0 : removeInstruction(MalBlkPtr mb, InstrPtr p)
     459             : {
     460           0 :         int i;
     461           0 :         for (i = 0; i < mb->stop - 1; i++)
     462           0 :                 if (mb->stmt[i] == p)
     463             :                         break;
     464           0 :         if (i == mb->stop)
     465             :                 return;
     466           0 :         for (; i < mb->stop - 1; i++)
     467           0 :                 mb->stmt[i] = mb->stmt[i + 1];
     468           0 :         mb->stmt[i] = 0;
     469           0 :         mb->stop--;
     470           0 :         assert(i == mb->stop);               /* move statement after stop */
     471           0 :         mb->stmt[i] = p;
     472             : }
     473             : 
     474             : void
     475           0 : removeInstructionBlock(MalBlkPtr mb, int pc, int cnt)
     476             : {
     477           0 :         int i;
     478           0 :         InstrPtr p;
     479           0 :         for (i = pc; i < pc + cnt; i++) {
     480           0 :                 p = getInstrPtr(mb, i);
     481           0 :                 freeInstruction(p);
     482           0 :                 mb->stmt[i] = NULL;
     483           0 :         } for (i = pc; i < mb->stop - cnt; i++)
     484           0 :                 mb->stmt[i] = mb->stmt[i + cnt];
     485           0 :         mb->stop -= cnt;
     486           0 :         for (; i < mb->stop; i++)
     487             :                 mb->stmt[i] = 0;
     488           0 : }
     489             : 
     490             : void
     491           0 : moveInstruction(MalBlkPtr mb, int pc, int target)
     492             : {
     493           0 :         InstrPtr p;
     494           0 :         int i;
     495           0 :         p = getInstrPtr(mb, pc);
     496           0 :         if (pc > target) {
     497           0 :                 for (i = pc; i > target; i--)
     498           0 :                         mb->stmt[i] = mb->stmt[i - 1];
     499           0 :                 mb->stmt[i] = p;
     500             :         } else {
     501           0 :                 for (i = target; i > pc; i--)
     502           0 :                         mb->stmt[i] = mb->stmt[i - 1];
     503           0 :                 mb->stmt[i] = p;
     504             :         }
     505           0 : }
     506             : 
     507             : /* Beware that the first argument of a signature is reserved for the
     508             :  * function return type , which should be equal to the destination
     509             :  * variable type.
     510             :  */
     511             : int
     512      664725 : findVariable(MalBlkPtr mb, const char *name)
     513             : {
     514      664725 :         int i;
     515      664725 :         if (name == NULL)
     516             :                 return -1;
     517     5954381 :         for (i = mb->vtop - 1; i >= 0; i--)
     518     5638630 :                 if (mb->var[i].name && idcmp(name, mb->var[i].name) == 0)
     519      348974 :                         return i;
     520             :         return -1;
     521             : }
     522             : 
     523             : /* The second version of findVariable assumes you have not yet
     524             :  * allocated a private structure. This is particularly useful during
     525             :  * parsing, because most variables are already defined. This way we
     526             :  * safe GDKmalloc/GDKfree. */
     527             : int
     528       54207 : findVariableLength(MalBlkPtr mb, const char *name, int len)
     529             : {
     530       54207 :         int i;
     531     2532975 :         for (i = mb->vtop - 1; i >= 0; i--) {
     532     2490057 :                 const char *s = mb->var[i].name;
     533     2490057 :                 if (s && strncmp(name, s, len) == 0 && s[len] == 0)
     534       11289 :                         return i;
     535             :         }
     536             :         return -1;
     537             : }
     538             : 
     539             : str
     540         174 : getArgDefault(MalBlkPtr mb, InstrPtr p, int idx)
     541             : {
     542         174 :         ValPtr v = &getVarConstant(mb, getArg(p, idx));
     543         174 :         if (v->vtype == TYPE_str)
     544         174 :                 return v->val.sval;
     545             :         return NULL;
     546             : }
     547             : 
     548             : /* Beware, the symbol table structure assumes that it is relatively
     549             :  * cheap to perform a linear search to a variable or constant. */
     550             : static int
     551    63687551 : makeVarSpace(MalBlkPtr mb)
     552             : {
     553    63687551 :         if (mb->vtop >= mb->vsize) {
     554       94887 :                 VarRecord *new;
     555       94887 :                 int s = (mb->vtop / MALCHUNK + 1) * MALCHUNK;
     556       94887 :                 new = (VarRecord *) GDKrealloc(mb->var, s * sizeof(VarRecord));
     557       94887 :                 if (new == NULL) {
     558             :                         /* the only place to return an error signal at this stage. */
     559             :                         /* The Client context should be passed around more deeply */
     560           0 :                         mb->errors = createMalException(mb, 0, TYPE, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     561           0 :                         return -1;
     562             :                 }
     563       94887 :                 memset(new + mb->vsize, 0, (s - mb->vsize) * sizeof(VarRecord));
     564       94887 :                 mb->vsize = s;
     565       94887 :                 mb->var = new;
     566             :         }
     567             :         return 0;
     568             : }
     569             : 
     570             : /* create and initialize a variable record*/
     571             : void
     572    63690586 : setVariableType(MalBlkPtr mb, const int n, malType type)
     573             : {
     574    63690586 :         assert(n >= 0 && n < mb->vtop);
     575    63690586 :         setVarType(mb, n, type);
     576    63690586 :         setRowCnt(mb, n, 0);
     577    63690586 :         clrVarFixed(mb, n);
     578    63690586 :         clrVarUsed(mb, n);
     579    63690586 :         clrVarInit(mb, n);
     580    63690586 :         clrVarDisabled(mb, n);
     581    63690586 :         clrVarConstant(mb, n);
     582    63690586 :         clrVarCleanup(mb, n);
     583    63690586 : }
     584             : 
     585             : char *
     586       26056 : getVarNameIntoBuffer(MalBlkPtr mb, int idx, char *buf)
     587             : {
     588       26056 :         char *s = mb->var[idx].name;
     589       26056 :         if (getVarKind(mb, idx) == 0)
     590           0 :                 setVarKind(mb, idx, REFMARKER);
     591       26056 :         if (s == NULL) {
     592       25917 :                 (void) snprintf(buf, IDLENGTH, "%c_%d", getVarKind(mb, idx), idx);
     593             :         } else {
     594         139 :                 (void) snprintf(buf, IDLENGTH, "%s", s);
     595             :         }
     596       26056 :         return buf;
     597             : }
     598             : 
     599             : int
     600    63685973 : newVariable(MalBlkPtr mb, const char *name, size_t len, malType type)
     601             : {
     602    63685973 :         int n;
     603    63685973 :         int kind = REFMARKER;
     604    63685973 :         if (mb->errors)
     605             :                 return -1;
     606    63685973 :         if (len >= IDLENGTH) {
     607           1 :                 mb->errors = createMalException(mb, 0, TYPE, "newVariable: id too long");
     608           1 :                 return -1;
     609             :         }
     610    63685972 :         if (makeVarSpace(mb)) {         /* no space for a new variable */
     611             :                 return -1;
     612             :         }
     613    63688099 :         n = mb->vtop;
     614    63688099 :         mb->var[n].name = NULL;
     615    63688099 :         if (name && len > 0) {
     616      363003 :                 char *nme = GDKmalloc(len+1);
     617      363014 :                 if (!nme) {
     618           0 :                         mb->errors = createMalException(mb, 0, TYPE, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     619           0 :                         return -1;
     620             :                 }
     621      363014 :                 mb->var[n].name = nme;
     622     3026771 :                 for (size_t i = 0; i < len; i++)
     623     2663757 :                         nme[i] = name[i];
     624      363014 :                 nme[len] = 0;
     625      363014 :                 kind = nme[0];
     626             :         }
     627    63688110 :         mb->vtop++;
     628    63688110 :         setVarKind(mb, n, kind);
     629    63688110 :         setVariableType(mb, n, type);
     630    63688110 :         return n;
     631             : }
     632             : 
     633             : /* Simplified cloning. */
     634             : int
     635           0 : cloneVariable(MalBlkPtr tm, MalBlkPtr mb, int x)
     636             : {
     637           0 :         int res;
     638           0 :         if (isVarConstant(mb, x))
     639           0 :                 res = cpyConstant(tm, getVar(mb, x));
     640             :         else {
     641           0 :                 res = newTmpVariable(tm, getVarType(mb, x));
     642           0 :                 if (mb->var[x].name)
     643           0 :                         tm->var[x].name = GDKstrdup(mb->var[x].name);
     644             :         }
     645           0 :         if (res < 0)
     646             :                 return res;
     647           0 :         if (isVarFixed(mb, x))
     648           0 :                 setVarFixed(tm, res);
     649           0 :         if (isVarUsed(mb, x))
     650           0 :                 setVarUsed(tm, res);
     651           0 :         if (isVarInit(mb, x))
     652           0 :                 setVarInit(tm, res);
     653           0 :         if (isVarDisabled(mb, x))
     654           0 :                 setVarDisabled(tm, res);
     655           0 :         if (isVarCleanup(mb, x))
     656           0 :                 setVarCleanup(tm, res);
     657           0 :         getVarSTC(tm, x) = getVarSTC(mb, x);
     658           0 :         setVarKind(tm, x, getVarKind(mb, x));
     659           0 :         return res;
     660             : }
     661             : 
     662             : int
     663    63324753 : newTmpVariable(MalBlkPtr mb, malType type)
     664             : {
     665    63324753 :         return newVariable(mb, 0, 0, type);
     666             : }
     667             : 
     668             : int
     669         274 : newTypeVariable(MalBlkPtr mb, malType type)
     670             : {
     671         274 :         int n, i;
     672        1284 :         for (i = 0; i < mb->vtop; i++)
     673        1053 :                 if (isVarTypedef(mb, i) && getVarType(mb, i) == type)
     674             :                         break;
     675         274 :         if (i < mb->vtop)
     676             :                 return i;
     677         231 :         n = newTmpVariable(mb, type);
     678         231 :         if (n >= 0)
     679         231 :                 setVarTypedef(mb, n);
     680             :         return n;
     681             : }
     682             : 
     683             : void
     684       72286 : clearVariable(MalBlkPtr mb, int varid)
     685             : {
     686       72286 :         VarPtr v;
     687       72286 :         v = getVar(mb, varid);
     688       72286 :         if (isVarConstant(mb, varid) || isVarDisabled(mb, varid))
     689       27807 :                 VALclear(&v->value);
     690       72286 :         if (v->name)
     691           0 :                 GDKfree(v->name);
     692       72286 :         v->name = NULL;
     693       72286 :         v->type = 0;
     694       72286 :         v->constant = 0;
     695       72286 :         v->typevar = 0;
     696       72286 :         v->fixedtype = 0;
     697       72286 :         v->cleanup = 0;
     698       72286 :         v->initialized = 0;
     699       72286 :         v->used = 0;
     700       72286 :         v->rowcnt = 0;
     701       72286 :         v->eolife = 0;
     702       72286 :         v->stc = 0;
     703       72286 : }
     704             : 
     705             : void
     706          81 : freeVariable(MalBlkPtr mb, int varid)
     707             : {
     708          81 :         clearVariable(mb, varid);
     709          81 : }
     710             : 
     711             : /* A special action is to reduce the variable space by removing all
     712             :  * that do not contribute.
     713             :  * All temporary variables are renamed in the process to trim the varid.
     714             :  */
     715             : void
     716           3 : trimMalVariables_(MalBlkPtr mb, MalStkPtr glb)
     717             : {
     718           3 :         int *alias, cnt = 0, i, j;
     719           3 :         InstrPtr q;
     720           3 :         if (mb->vtop == 0)
     721             :                 return;
     722           3 :         alias = (int *) GDKzalloc(mb->vtop * sizeof(int));
     723           3 :         if (alias == NULL)
     724             :                 return;                                 /* forget it if we run out of memory *//* build the alias table */
     725         674 :         for (i = 0; i < mb->vtop; i++) {
     726         671 :                 if (isVarUsed(mb, i) == 0) {
     727          81 :                         if (glb && i < glb->stktop && isVarConstant(mb, i))
     728           0 :                                 VALclear(&glb->stk[i]);
     729          81 :                         freeVariable(mb, i);
     730          81 :                         continue;
     731             :                 }
     732         590 :                 if (i > cnt) {                       /* remap temporary variables */
     733         530 :                         VarRecord t = mb->var[cnt];
     734         530 :                         mb->var[cnt] = mb->var[i];
     735         530 :                         mb->var[i] = t;
     736             :                 }                                               /* valgrind finds a leak when we move these variable record * pointers around. */
     737         590 :                 alias[i] = cnt;
     738         590 :                 if (glb && i < glb->stktop && i != cnt) {
     739           0 :                         glb->stk[cnt] = glb->stk[i];
     740           0 :                         VALempty(&glb->stk[i]);
     741             :                 }
     742         590 :                 cnt++;
     743             :         }                                                       /* remap all variable references to their new position. */
     744           3 :         if (cnt < mb->vtop) {
     745         385 :                 for (i = 0; i < mb->stop; i++) {
     746         382 :                         q = getInstrPtr(mb, i);
     747        2271 :                         for (j = 0; j < q->argc; j++) {
     748        1889 :                                 getArg(q, j) = alias[getArg(q, j)];
     749             :                         }
     750             :                 }
     751           3 :                 mb->vtop = cnt;
     752             :         }
     753           3 :         GDKfree(alias);
     754             : }
     755             : 
     756             : void
     757           3 : trimMalVariables(MalBlkPtr mb, MalStkPtr stk)
     758             : {
     759           3 :         int i, j;
     760           3 :         InstrPtr q;                                     /* reset the use bit for all non-signature arguments */
     761         674 :         for (i = 0; i < mb->vtop; i++)
     762         671 :                 clrVarUsed(mb, i);              /* build the use table */
     763         385 :         for (i = 0; i < mb->stop; i++) {
     764         382 :                 q = getInstrPtr(mb, i);
     765        2271 :                 for (j = 0; j < q->argc; j++)
     766        1889 :                         setVarUsed(mb, getArg(q, j));
     767             :         }
     768           3 :         trimMalVariables_(mb, stk);
     769           3 : }
     770             : 
     771             : /* MAL constants
     772             :  * Constants are stored in the symbol table and referenced by a
     773             :  * variable identifier. This means that per MAL instruction, we may
     774             :  * end up with MAXARG entries in the symbol table. This may lead to
     775             :  * long searches for variables. An optimization strategy deployed in
     776             :  * the current implementation is to look around for a similar
     777             :  * (constant) definition and to reuse its identifier. This avoids an
     778             :  * exploding symbol table with a lot of temporary variables (as in
     779             :  * tst400cHuge)
     780             :  *
     781             :  * But then the question becomes how far to search? Searching through
     782             :  * all variables is only useful when the list remains short or when
     783             :  * the constant-variable-name is easily derivable from its literal
     784             :  * value and a hash-based index leads you quickly to it.
     785             :  *
     786             :  * For the time being, we use a MAL system parameter, MAL_VAR_WINDOW,
     787             :  * to indicate the number of symbol table entries to consider. Setting
     788             :  * it to >= MAXARG will at least capture repeated use of a constant
     789             :  * within a single function call or repeated use within a small block
     790             :  * of code.
     791             :  *
     792             :  * The final step is to prepare a GDK value record, from which the
     793             :  * internal representation can be obtained during MAL interpretation.
     794             :  *
     795             :  * The constant values are linked together to improve searching
     796             :  * them. This start of the constant list is kept in the MalBlk.
     797             :  *
     798             :  * Conversion of a constant to another type is limited to well-known
     799             :  * coercion rules. Errors are reported and the nil value is set. */
     800             : 
     801             : /* Converts the constant in vr to the MAL type type.  Conversion is
     802             :  * done in the vr struct. */
     803             : str
     804      278330 : convertConstant(int type, ValPtr vr)
     805             : {
     806      278330 :         if (type > GDKatomcnt)
     807           0 :                 throw(SYNTAX, "convertConstant", "type index out of bound");
     808      278330 :         if (vr->vtype == type)
     809             :                 return MAL_SUCCEED;
     810      278327 :         if (isaBatType(type)) { /* BAT variables can only be set to nil */
     811           0 :                 if (vr->vtype != TYPE_void)
     812           0 :                         throw(SYNTAX, "convertConstant", "BAT conversion error");
     813           0 :                 VALclear(vr);
     814           0 :                 vr->vtype = getBatType(type);
     815           0 :                 vr->bat = true;
     816           0 :                 vr->val.bval = bat_nil;
     817           0 :                 return MAL_SUCCEED;
     818             :         }
     819      278327 :         if (type == TYPE_ptr) {         /* all coercions should be avoided to protect against memory probing */
     820          32 :                 if (vr->vtype == TYPE_void) {
     821          32 :                         VALclear(vr);
     822          32 :                         vr->vtype = type;
     823          32 :                         vr->val.pval = NULL;
     824          32 :                         return MAL_SUCCEED;
     825             :                 }
     826           0 :                 if (vr->vtype != type)
     827           0 :                         throw(SYNTAX, "convertConstant", "pointer conversion error");
     828             :                 return MAL_SUCCEED;
     829             :         }
     830      278295 :         if (type == TYPE_any) {
     831             : #ifndef DEBUG_MAL_INSTR
     832             :                 assert(0);
     833             : #endif
     834           0 :                 throw(SYNTAX, "convertConstant", "missing type");
     835             :         }
     836      278295 :         if (VALconvert(type, vr) == NULL) {
     837           3 :                 if (vr->vtype == TYPE_str)
     838           0 :                         throw(SYNTAX, "convertConstant", "parse error in '%s'", vr->val.sval);
     839           3 :                 throw(SYNTAX, "convertConstant", "coercion failed");
     840             :         }
     841             :         return MAL_SUCCEED;
     842             : }
     843             : 
     844             : int
     845    51235861 : fndConstant(MalBlkPtr mb, const ValRecord *cst, int depth)
     846             : {
     847    51235861 :         int i, k;
     848    51235861 :         const void *p;                          /* pointers never match */
     849    51235861 :         if (ATOMstorage(cst->vtype) == TYPE_ptr)
     850             :                 return -1;
     851    51079688 :         p = VALptr(cst);
     852    51079688 :         k = mb->vtop - depth;
     853    51079688 :         if (k < 0)
     854             :                 k = 0;
     855   579935369 :         for (i = k; i < mb->vtop - 1; i++) {
     856   560268858 :                 VarPtr v = getVar(mb, i);
     857   560268858 :                 if (v->constant) {
     858   196024569 :                         if (v && v->type == cst->vtype &&
     859   113718200 :                                         v->value.len == cst->len &&
     860   187300341 :                                         isaBatType(v->type) == cst->bat &&
     861    93655600 :                                         ATOMcmp(cst->vtype, VALptr(&v->value), p) == 0)
     862    31407921 :                                 return i;
     863             :                 }
     864             :         }
     865             :         return -1;
     866             : }
     867             : 
     868             : int
     869        3277 : cpyConstant(MalBlkPtr mb, VarPtr vr)
     870             : {
     871        3277 :         int i;
     872        3277 :         ValRecord cst;
     873        3277 :         if (VALcopy(&cst, &vr->value) == NULL)
     874             :                 return -1;
     875        3277 :         i = defConstant(mb, vr->type, &cst);
     876        3277 :         if (i < 0)
     877             :                 return -1;
     878             :         return i;
     879             : }
     880             : 
     881             : int
     882    46071773 : defConstant(MalBlkPtr mb, int type, ValPtr cst)
     883             : {
     884    46071773 :         int k;
     885    46071773 :         str msg;
     886             : 
     887    46071773 :         assert(!isaBatType(type) || cst->bat);
     888    46071773 :         cst->bat = false;
     889    46071773 :         if (isaBatType(type)) {
     890      464652 :                 if (cst->vtype == TYPE_void) {
     891      464651 :                         cst->vtype = getBatType(type);
     892      464651 :                         cst->bat = true;
     893      464651 :                         cst->val.bval = bat_nil;
     894             :                 } else {
     895           1 :                         mb->errors = createMalException(mb, 0, TYPE, "BAT coercion error");
     896           1 :                         VALclear(cst);  // it could contain allocated space
     897           1 :                         return -1;
     898             :                 }
     899    45607121 :         } else if (cst->vtype != type && !isPolyType(type)) {
     900        1919 :                 int otype = cst->vtype;
     901        1919 :                 assert(type != TYPE_any);       /* help Coverity */
     902        1919 :                 msg = convertConstant(getBatType(type), cst);
     903        1920 :                 if (msg) {
     904           3 :                         str ft, tt;                     /* free old value */
     905           3 :                         ft = getTypeName(otype);
     906           3 :                         tt = getTypeName(type);
     907           3 :                         if (ft && tt)
     908           3 :                                 mb->errors = createMalException(mb, 0, TYPE,
     909             :                                                                                                 "constant coercion error from %s to %s",
     910             :                                                                                                 ft, tt);
     911             :                         else
     912           0 :                                 mb->errors = createMalException(mb, 0, TYPE,
     913             :                                                                                                 "constant coercion error");
     914           3 :                         GDKfree(ft);
     915           3 :                         GDKfree(tt);
     916           3 :                         freeException(msg);
     917           3 :                         VALclear(cst);          /* it could contain allocated space */
     918           3 :                         return -1;
     919             :                 } else {
     920        1917 :                         assert(cst->vtype == type);
     921             :                 }
     922             :         }
     923    46071770 :         if (cst->vtype != TYPE_any) {
     924    46072409 :                 k = fndConstant(mb, cst, MAL_VAR_WINDOW);
     925    46072831 :                 if (k >= 0) {                                /* protect against leaks coming from constant reuse */
     926    27819500 :                         VALclear(cst);
     927    27819500 :                         return k;
     928             :                 }
     929             :         }
     930    18252692 :         k = newTmpVariable(mb, type);
     931    18294024 :         if (k < 0) {
     932           0 :                 VALclear(cst);
     933           0 :                 return -1;
     934             :         }
     935    18294024 :         setVarConstant(mb, k);
     936    18294024 :         setVarFixed(mb, k);
     937    18294024 :         if (type >= 0 && type < GDKatomcnt && ATOMextern(type))
     938     4691228 :                 setVarCleanup(mb, k);
     939             :         else
     940    13602796 :                 clrVarCleanup(mb, k);   /* if cst is external, we give its allocated buffer away, so clear * it to avoid confusion */
     941    18294024 :         getVarConstant(mb, k) = *cst;
     942    18294024 :         VALempty(cst);
     943    18294024 :         return k;
     944             : }
     945             : 
     946             : /* Argument handling
     947             :  * The number of arguments for procedures is currently
     948             :  * limited. Furthermore, we should assure that no variable is
     949             :  * referenced before being assigned. Failure to obey should mark the
     950             :  * instruction as type-error. */
     951             : static InstrPtr
     952         319 : extendInstruction(MalBlkPtr mb, InstrPtr p)
     953             : {
     954         319 :         InstrPtr pn = p;
     955         319 :         if (p->argc == p->maxarg) {
     956         319 :                 int space = p->maxarg * sizeof(p->argv[0]) + offsetof(InstrRecord, argv);
     957         319 :                 pn = (InstrPtr) GDKrealloc(p, space + MAXARG * sizeof(p->argv[0]));
     958         319 :                 if (pn == NULL) {               /* In the exceptional case we can not allocate more space * then we show an exception, mark the block as erroneous * and leave the instruction as is. */
     959           0 :                         mb->errors = createMalException(mb, 0, TYPE,
     960             :                                                                                         SQLSTATE(HY013) MAL_MALLOC_FAIL);
     961           0 :                         return p;
     962             :                 }
     963         319 :                 memset(((char *) pn) + space, 0, MAXARG * sizeof(pn->argv[0]));
     964         319 :                 pn->maxarg += MAXARG;
     965             :         }
     966             :         return pn;
     967             : }
     968             : 
     969             : InstrPtr
     970   171357324 : pushArgument(MalBlkPtr mb, InstrPtr p, int varid)
     971             : {
     972   171357324 :         if (p == NULL || mb->errors)
     973             :                 return p;
     974   171357324 :         if (varid < 0) {                     /* leave everything as is in this exceptional programming error */
     975           0 :                 mb->errors = createMalException(mb, 0, TYPE, "improper variable id");
     976           0 :                 return p;
     977             :         }
     978   171357324 :         if (p->argc == p->maxarg) {
     979             : #ifndef NDEBUG
     980        1011 :                 for (int i = 0; i < mb->stop; i++)
     981         692 :                         assert(mb->stmt[i] != p);
     982             : #endif
     983         319 :                 p = extendInstruction(mb, p);
     984         319 :                 if (mb->errors)
     985             :                         return p;
     986             :         }                                                       /* protect against the case that the instruction is malloced in isolation */
     987   171357324 :         if (mb->maxarg < p->maxarg)
     988       13849 :                 mb->maxarg = p->maxarg;
     989   171357324 :         p->argv[p->argc++] = varid;
     990   171357324 :         return p;
     991             : }
     992             : 
     993             : InstrPtr
     994      843085 : setArgument(MalBlkPtr mb, InstrPtr p, int idx, int varid)
     995             : {
     996      843085 :         int i;
     997      843085 :         if (p == NULL || mb->errors)
     998             :                 return p;
     999      843089 :         p = pushArgument(mb, p, varid); /* make space */
    1000      847042 :         for (i = p->argc - 1; i > idx; i--)
    1001        3952 :                 getArg(p, i) = getArg(p, i - 1);
    1002      843090 :         getArg(p, i) = varid;
    1003      843090 :         return p;
    1004             : }
    1005             : 
    1006             : InstrPtr
    1007      844022 : pushReturn(MalBlkPtr mb, InstrPtr p, int varid)
    1008             : {
    1009      844022 :         if (p == NULL || mb->errors)
    1010             :                 return p;
    1011      844022 :         if (p->retc == 1 && p->argv[0] == -1) {
    1012         931 :                 p->argv[0] = varid;
    1013         931 :                 return p;
    1014             :         }
    1015      843091 :         p = setArgument(mb, p, p->retc, varid);
    1016      843093 :         p->retc++;
    1017      843093 :         return p;
    1018             : }
    1019             : 
    1020             : /* Store the information of a destination variable in the signature
    1021             :  * structure of each instruction. This code is largely equivalent to
    1022             :  * pushArgument, but it is more efficient in searching and collecting
    1023             :  * the information.
    1024             :  * TODO */
    1025             : /* swallows name argument */
    1026             : InstrPtr
    1027        5397 : pushArgumentId(MalBlkPtr mb, InstrPtr p, const char *name)
    1028             : {
    1029        5397 :         int v;
    1030        5397 :         if (p == NULL || mb->errors)
    1031             :                 return p;
    1032        5397 :         v = findVariable(mb, name);
    1033        5399 :         if (v < 0) {
    1034         382 :                 size_t namelen = strlen(name);
    1035         382 :                 if ((v = newVariable(mb, name, namelen, getAtomIndex(name, namelen, TYPE_any))) < 0) {
    1036             :                         /* set the MAL block to erroneous and simply return without
    1037             :                          * doing anything */
    1038             :                         /* mb->errors already set */
    1039             :                         return p;
    1040             :                 }
    1041             :         }
    1042        5398 :         return pushArgument(mb, p, v);
    1043             : }
    1044             : 
    1045             : /* The alternative is to remove arguments from an instruction
    1046             :  * record. This is typically part of instruction constructions. */
    1047             : void
    1048     1468209 : delArgument(InstrPtr p, int idx)
    1049             : {
    1050     1468209 :         int i;
    1051     2190834 :         for (i = idx; i < p->argc - 1; i++)
    1052      722625 :                 p->argv[i] = p->argv[i + 1];
    1053     1468209 :         p->argc--;
    1054     1468209 :         if (idx < p->retc)
    1055      171109 :                 p->retc--;
    1056     1468209 : }
    1057             : 
    1058             : void
    1059       32955 : setArgType(MalBlkPtr mb, InstrPtr p, int i, int tpe)
    1060             : {
    1061       32955 :         assert(p->argv[i] < mb->vsize);
    1062       32955 :         setVarType(mb, getArg(p, i), tpe);
    1063       32955 : }
    1064             : 
    1065             : void
    1066           0 : setReturnArgument(InstrPtr p, int i)
    1067             : {
    1068           0 :         setDestVar(p, i);
    1069           0 : }
    1070             : 
    1071             : malType
    1072           0 : destinationType(MalBlkPtr mb, InstrPtr p)
    1073             : {
    1074           0 :         if (p->argc > 0)
    1075           0 :                 return getVarType(mb, getDestVar(p));
    1076             :         return TYPE_any;
    1077             : }
    1078             : 
    1079             : /* For polymorphic instructions we should keep around the maximal
    1080             :  * index to later allocate sufficient space for type resolutions maps.
    1081             :  * Beware, that we should only consider the instruction polymorphic if
    1082             :  * it has a positive index or belongs to the signature.
    1083             :  * BATs can only have a polymorphic type at the tail.
    1084             :  */
    1085             : inline void
    1086        9277 : setPolymorphic(InstrPtr p, int tpe, int force /* just any isn't polymorphic */)
    1087             : {
    1088        9277 :         int any = isAnyExpression(tpe) || tpe == TYPE_any, index = 0;
    1089        9277 :         if ((force == FALSE && tpe == TYPE_any) || !any)
    1090             :                 return;
    1091          34 :         if (getTypeIndex(tpe) > 0)
    1092             :                 index = getTypeIndex(tpe);
    1093          34 :         if (any && (index + 1) >= p->polymorphic)
    1094          31 :                 p->polymorphic = index + 1;
    1095             : }
    1096             : 
    1097             : /* Instructions are simply appended to a MAL block. It should always succeed.
    1098             :  * The assumption is to push it when you are completely done with its preparation.
    1099             :  */
    1100             : void
    1101   286222788 : pushInstruction(MalBlkPtr mb, InstrPtr p)
    1102             : {
    1103   286222788 :         int i;
    1104   286222788 :         InstrPtr q;
    1105   286222788 :         if (p == NULL)
    1106             :                 return;
    1107   286222788 :         if (mb->stop + 1 >= mb->ssize) {
    1108       61329 :                 int s = (mb->ssize / MALCHUNK + 1) * MALCHUNK;
    1109       61329 :                 if (resizeMalBlk(mb, s) < 0) {
    1110             :                         /* we are now left with the situation that the new
    1111             :                          * instruction is dangling.  The hack is to take an
    1112             :                          * instruction out of the block that is likely not
    1113             :                          * referenced independently.  The last resort is to take the
    1114             :                          * first, which should always be there.  This assumes that
    1115             :                          * no references are kept elsewhere to the statement. */
    1116           0 :                         assert(mb->errors != NULL);
    1117           0 :                         for (i = 1; i < mb->stop; i++) {
    1118           0 :                                 q = getInstrPtr(mb, i);
    1119           0 :                                 if (q->token == REMsymbol) {
    1120           0 :                                         freeInstruction(q);
    1121           0 :                                         mb->stmt[i] = p;
    1122           0 :                                         return;
    1123             :                                 }
    1124             :                         }
    1125           0 :                         freeInstruction(getInstrPtr(mb, 0));
    1126           0 :                         mb->stmt[0] = p;
    1127           0 :                         return;
    1128             :                 }
    1129             :         }
    1130   286222788 :         if (mb->stmt[mb->stop])
    1131       19107 :                 freeInstruction(mb->stmt[mb->stop]);
    1132   286237095 :         p->pc = mb->stop;
    1133   286237095 :         mb->stmt[mb->stop++] = p;
    1134             : }

Generated by: LCOV version 1.14