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) M. Kersten
15 : * For documentation see website
16 : */
17 : #include "monetdb_config.h"
18 : #include "mal_function.h"
19 : #include "mal_resolve.h" /* for isPolymorphic() & chkProgram() */
20 : #include "mal_interpreter.h" /* for showErrors() */
21 : #include "mal_listing.h"
22 : #include "mal_namespace.h"
23 : #include "mal_private.h"
24 :
25 : Symbol
26 50420 : newFunctionArgs(const char *mod, const char *nme, int kind, int args)
27 : {
28 50420 : Symbol s;
29 :
30 50420 : if (mod == NULL || nme == NULL)
31 : return NULL;
32 :
33 50420 : s = newSymbol(nme, kind);
34 50422 : if (s == NULL)
35 : return NULL;
36 :
37 50422 : if (kind == FUNCTIONsymbol) {
38 50407 : int varid = newVariable(s->def, nme, strlen(nme), TYPE_any);
39 50405 : if (varid < 0) {
40 0 : freeSymbol(s);
41 0 : return NULL;
42 : }
43 :
44 50405 : if (args > 0) {
45 50394 : InstrPtr p = newInstructionArgs(NULL, mod, nme, args);
46 50390 : if (p == NULL) {
47 0 : freeSymbol(s);
48 0 : return NULL;
49 : }
50 50390 : p->token = kind;
51 50390 : p->barrier = 0;
52 50390 : setDestVar(p, varid);
53 50390 : pushInstruction(s->def, p);
54 50390 : if (s->def->errors) {
55 0 : freeSymbol(s);
56 0 : return NULL;
57 : }
58 : }
59 : }
60 : return s;
61 : }
62 :
63 : Symbol
64 49228 : newFunction(const char *mod, const char *nme, int kind)
65 : {
66 49228 : return newFunctionArgs(mod, nme, kind, MAXARG);
67 : }
68 :
69 : int
70 155327 : getPC(MalBlkPtr mb, InstrPtr p)
71 : {
72 155327 : int i;
73 1792661 : for (i = 0; i < mb->stop; i++)
74 1792679 : if (getInstrPtr(mb, i) == p)
75 155345 : return i;
76 : return -1;
77 : }
78 :
79 : /*
80 : * Checking the control flow structure is done by a single pass over the
81 : * MAL program after the program has been type-checked.
82 : * It should inspect all BARRIER and CATCH blocks for proper structure.
83 : * If the flow is correct and not dependent on an undefined typed instruction
84 : * we avoid doing this check any further.
85 : */
86 : #define DEPTH 128
87 :
88 : str
89 3131470 : chkFlow(MalBlkPtr mb)
90 : {
91 3131470 : int i, j, k, v, lastInstruction;
92 3131470 : int pc[DEPTH];
93 3131470 : int var[DEPTH];
94 3131470 : InstrPtr stmt[DEPTH];
95 3131470 : int btop = 0;
96 3131470 : int endseen = 0, retseen = 0;
97 3131470 : InstrPtr p, sig;
98 3131470 : str msg = MAL_SUCCEED;
99 3131470 : char name[IDLENGTH] = { 0 };
100 :
101 3131470 : if (mb->errors != MAL_SUCCEED)
102 : return mb->errors;
103 3133302 : sig = getInstrPtr(mb, 0);
104 3133302 : lastInstruction = mb->stop - 1;
105 197496165 : for (i = 0; i < mb->stop; i++) {
106 194362879 : p = getInstrPtr(mb, i);
107 : /* we have to keep track on the maximal arguments/block
108 : because it is needed by the interpreter */
109 194362879 : switch (p->barrier) {
110 248025 : case BARRIERsymbol:
111 : case CATCHsymbol:
112 248025 : if (btop == DEPTH)
113 0 : throw(MAL, "chkFlow", "%s.%s Too many nested MAL blocks",
114 : getModuleId(sig), getFunctionId(sig));
115 248025 : pc[btop] = i;
116 248025 : v = var[btop] = getDestVar(p);
117 248025 : stmt[btop] = p;
118 :
119 248834 : for (j = btop - 1; j >= 0; j--)
120 809 : if (v == var[j])
121 0 : throw(MAL, "chkFlow",
122 : "%s.%s recursive %s[%d] shields %s[%d]",
123 : getModuleId(sig), getFunctionId(sig), getVarNameIntoBuffer(mb, v, name),
124 0 : pc[j], getFcnName(mb), pc[i]);
125 :
126 248025 : btop++;
127 248025 : break;
128 248167 : case EXITsymbol:
129 248167 : v = getDestVar(p);
130 248167 : if (btop > 0 && var[btop - 1] != v)
131 3 : throw(MAL, "chkFlow",
132 : "%s.%s exit-label '%s' doesnot match '%s'",
133 : getModuleId(sig), getFunctionId(sig), getVarNameIntoBuffer(mb, v, name),
134 : getVarNameIntoBuffer(mb, var[btop - 1], name));
135 248164 : if (btop == 0)
136 0 : throw(MAL, "chkFlow",
137 : "%s.%s exit-label '%s' without begin-label",
138 : getModuleId(sig), getFunctionId(sig), getVarNameIntoBuffer(mb, v, name));
139 : /* search the matching block */
140 248164 : for (j = btop - 1; j >= 0; j--)
141 248131 : if (var[j] == v)
142 : break;
143 248164 : if (j >= 0)
144 : btop = j;
145 : else
146 0 : btop--;
147 :
148 : /* retrofit LEAVE/REDO instructions */
149 248164 : stmt[btop]->jump = i;
150 23187939 : for (k = pc[btop]; k < i; k++) {
151 22939775 : InstrPtr p1 = getInstrPtr(mb, k);
152 22939775 : if (getDestVar(p1) == v) {
153 : /* handle assignments with leave/redo option */
154 249758 : if (p1->barrier == LEAVEsymbol)
155 125 : p1->jump = i;
156 249758 : if (p1->barrier == REDOsymbol)
157 1697 : p1->jump = pc[btop] + 1;
158 : }
159 : }
160 : break;
161 1836 : case LEAVEsymbol:
162 : case REDOsymbol:
163 1836 : v = getDestVar(p);
164 1845 : for (j = btop - 1; j >= 0; j--)
165 1837 : if (var[j] == v)
166 : break;
167 : if (j < 0) {
168 8 : str nme = getVarNameIntoBuffer(mb, v, name);
169 8 : throw(MAL, "chkFlow", "%s.%s label '%s' not in guarded block",
170 : getModuleId(sig), getFunctionId(sig), nme);
171 : }
172 : break;
173 3153 : case RETURNsymbol: {
174 3153 : InstrPtr ps = getInstrPtr(mb, 0);
175 3153 : int e;
176 3153 : if (ps->retc != p->retc) {
177 2 : throw(MAL, "chkFlow", "%s.%s invalid return target!",
178 : getModuleId(sig), getFunctionId(sig));
179 3151 : } else if (ps->typeresolved)
180 20850 : for (e = 0; e < p->retc; e++) {
181 17707 : if (resolvedType(getArgType(mb, ps, e), getArgType(mb, p, e)) < 0) {
182 0 : str tpname = getTypeName(getArgType(mb, p, e));
183 0 : msg = createException(MAL,
184 : "%s.%s RETURN type mismatch at type '%s'\n",
185 0 : getModuleId(p) ? getModuleId(p) :
186 : "",
187 0 : getFunctionId(p) ?
188 : getFunctionId(p) : "", tpname);
189 0 : GDKfree(tpname);
190 0 : return msg;
191 : }
192 : }
193 : //if (btop == 0)
194 : retseen = 1;
195 : break;
196 : }
197 : case RAISEsymbol:
198 : endseen = 1;
199 : break;
200 : case ENDsymbol:
201 : endseen = 1;
202 : break;
203 193861079 : default:
204 193861079 : if (isaSignature(p)) {
205 28206251 : if (p->token == REMsymbol) {
206 : /* do nothing */
207 3130899 : } else if (i) {
208 0 : str l = instruction2str(mb, 0, p, TRUE);
209 0 : msg = createException(MAL, "%s.%s signature misplaced\n!%s",
210 : getModuleId(p), getFunctionId(p), l);
211 0 : GDKfree(l);
212 0 : return msg;
213 : }
214 : }
215 : }
216 : }
217 :
218 3133286 : if (lastInstruction < mb->stop - 1)
219 0 : throw(MAL, "chkFlow", "%s.%s instructions after END", getModuleId(sig),
220 : getFunctionId(sig));
221 :
222 3133286 : if (endseen && btop > 0)
223 0 : throw(MAL, "chkFlow", "barrier '%s' without exit in %s[%d]",
224 0 : getVarNameIntoBuffer(mb, var[btop - 1], name), getFcnName(mb), i);
225 3133286 : p = getInstrPtr(mb, 0);
226 3133286 : if (!isaSignature(p))
227 0 : throw(MAL, "chkFlow", "%s.%s signature missing", getModuleId(sig),
228 : getFunctionId(sig));
229 3133286 : if (retseen == 0) {
230 3130830 : if (getArgType(mb, p, 0) != TYPE_void && (p->token == FUNCTIONsymbol))
231 0 : throw(MAL, "chkFlow", "%s.%s RETURN missing", getModuleId(sig),
232 : getFunctionId(sig));
233 : }
234 : return MAL_SUCCEED;
235 : }
236 :
237 : /*
238 : * A code may contain temporary names for marking barrier blocks.
239 : * Since they are introduced by the compiler, the parser should locate
240 : * them itself when encountering the LEAVE,EXIT,REDO.
241 : * The starting position is mostly the last statement entered.
242 : * Purposely, the nameless envelops searches the name of the last
243 : * unclosed block. All others are ignored.
244 : */
245 : int
246 4 : getBarrierEnvelop(MalBlkPtr mb)
247 : {
248 4 : int pc;
249 4 : InstrPtr p;
250 10 : for (pc = mb->stop - 2; pc >= 0; pc--) {
251 9 : p = getInstrPtr(mb, pc);
252 9 : if (blockExit(p)) {
253 1 : int l = p->argv[0];
254 4 : for (; pc >= 0; pc--) {
255 4 : p = getInstrPtr(mb, pc);
256 4 : if (blockStart(p) && p->argv[0] == l)
257 : break;
258 : }
259 1 : continue;
260 : }
261 8 : if (blockStart(p))
262 3 : return p->argv[0];
263 : }
264 1 : return newTmpVariable(mb, TYPE_any);
265 : }
266 :
267 : static void
268 14 : replaceTypeVar(MalBlkPtr mb, InstrPtr p, int v, malType t)
269 : {
270 83 : for (int j = 0; j < mb->stop; j++) {
271 69 : p = getInstrPtr(mb, j);
272 69 : if (p->polymorphic) {
273 94 : for (int i = 0; i < p->argc; i++) {
274 68 : int x = getArgType(mb, p, i);
275 68 : if (isPolymorphic(x) && getTypeIndex(x) == v) {
276 33 : if (isaBatType(x)) {
277 17 : int tail = newBatType(t);
278 17 : setArgType(mb, p, i, tail);
279 : } else {
280 16 : setArgType(mb, p, i, t);
281 : }
282 : }
283 : }
284 : }
285 : }
286 14 : }
287 :
288 : /* insert a symbol into the symbol table just before the symbol
289 : * "before". */
290 : static void
291 11 : insertSymbolBefore(Module scope, Symbol prg, Symbol before)
292 : {
293 11 : int t;
294 11 : Symbol s;
295 :
296 11 : assert(strcmp(prg->name, before->name) == 0);
297 11 : t = getSymbolIndex(prg->name);
298 11 : assert(scope->space != NULL);
299 11 : assert(scope->space[t] != NULL);
300 11 : s = scope->space[t];
301 11 : prg->skip = before->skip;
302 11 : prg->peer = before;
303 11 : if (s == before) {
304 9 : scope->space[t] = prg;
305 : } else {
306 3 : for (;;) {
307 3 : assert(s != NULL);
308 3 : if (s->skip == before) {
309 0 : s->skip = prg;
310 : }
311 3 : if (s->peer == before) {
312 2 : s->peer = prg;
313 2 : break;
314 : }
315 : s = s->peer;
316 : }
317 : }
318 11 : }
319 :
320 : /*
321 : * Upon cloning a function we should remove all the polymorphic flags.
322 : * Otherwise we may end up with a recursive clone.
323 : */
324 : Symbol
325 11 : cloneFunction(Module scope, Symbol proc, MalBlkPtr mb, InstrPtr p)
326 : {
327 11 : Symbol new;
328 11 : int i, v;
329 11 : InstrPtr pp;
330 11 : str msg = MAL_SUCCEED;
331 :
332 11 : new = newFunctionArgs(scope->name, proc->name, proc->kind, -1);
333 11 : if (new == NULL) {
334 : return NULL;
335 : }
336 11 : freeMalBlk(new->def);
337 11 : if ((new->def = copyMalBlk(proc->def)) == NULL) {
338 0 : freeSymbol(new);
339 0 : return NULL;
340 : }
341 : /* now change the definition of the original proc */
342 : /* check for errors after fixation , TODO */
343 11 : pp = getSignature(new);
344 43 : for (i = 0; i < pp->argc; i++)
345 32 : if (isPolymorphic(v = getArgType(new->def, pp, i))) {
346 14 : int t = getArgType(mb, p, i);
347 :
348 14 : if (v == TYPE_any) {
349 : assert(0);
350 : replaceTypeVar(new->def, pp, v, t);
351 : }
352 14 : if (isaBatType(v)) {
353 14 : if (getTypeIndex(v))
354 14 : replaceTypeVar(new->def, pp, getTypeIndex(v), getBatType(t));
355 : } else
356 0 : replaceTypeVar(new->def, pp, getTypeIndex(v), t);
357 : }
358 : /* include the function at the proper place in the scope */
359 11 : insertSymbolBefore(scope, new, proc);
360 : /* clear polymorphic and type to force analysis */
361 76 : for (i = 0; i < new->def->stop; i++) {
362 54 : pp = getInstrPtr(new->def, i);
363 54 : pp->typeresolved = false;
364 54 : pp->polymorphic = 0;
365 : }
366 : /* clear type fixations */
367 85 : for (i = 0; i < new->def->vtop; i++)
368 74 : clrVarFixed(new->def, i);
369 :
370 :
371 : /* check for errors after fixation , TODO */
372 : /* beware, we should now ignore any cloning */
373 11 : if (proc->def->errors == 0) {
374 11 : msg = chkProgram(scope, new->def);
375 11 : if (msg)
376 3 : mb->errors = msg;
377 8 : else if (new->def->errors) {
378 0 : assert(mb->errors == NULL);
379 0 : mb->errors = new->def->errors;
380 0 : mb->errors = createMalException(mb, 0, TYPE, "Error in cloned function");
381 0 : new->def->errors = 0;
382 : }
383 : }
384 :
385 : return new;
386 : }
387 :
388 : /*
389 : * For commands we do not have to clone the routine. We merely have to
390 : * assure that the type-constraints are obeyed. The resulting type
391 : * is returned.
392 : */
393 : void
394 0 : debugFunction(stream *fd, MalBlkPtr mb, MalStkPtr stk, int flg, int first,
395 : int step)
396 : {
397 0 : int i, j;
398 0 : str ps;
399 0 : InstrPtr p;
400 :
401 0 : if (mb == NULL) {
402 0 : mnstr_printf(fd, "# function definition missing\n");
403 0 : return;
404 : }
405 0 : if (flg == 0 || step < 0 || first < 0)
406 : return;
407 :
408 0 : if (mb->errors)
409 0 : mnstr_printf(fd, "#errors seen: %s\n", mb->errors);
410 0 : for (i = first; i < first + step && i < mb->stop; i++) {
411 0 : ps = instruction2str(mb, stk, (p = getInstrPtr(mb, i)), flg);
412 0 : if (ps) {
413 0 : if (p->token == REMsymbol)
414 0 : mnstr_printf(fd, "%-40s\n", ps);
415 : else {
416 0 : mnstr_printf(fd, "%-40s\t#[%d] %s ", ps, i,
417 0 : (p->blk ? p->blk->binding : ""));
418 0 : if (flg & LIST_MAL_FLOW) {
419 0 : for (j = 0; j < p->retc; j++)
420 0 : mnstr_printf(fd, "%d ", getArg(p, j));
421 0 : if (p->argc - p->retc > 0)
422 0 : mnstr_printf(fd, "<- ");
423 0 : for (; j < p->argc; j++)
424 0 : mnstr_printf(fd, "%d ", getArg(p, j));
425 : }
426 0 : mnstr_printf(fd, "\n");
427 : }
428 0 : GDKfree(ps);
429 : } else
430 0 : mnstr_printf(fd, "#failed instruction2str()\n");
431 : }
432 : }
433 :
434 : void
435 67 : listFunction(stream *fd, MalBlkPtr mb, MalStkPtr stk, int flg, int first,
436 : int size)
437 : {
438 67 : int i;
439 67 : int sample = 256;
440 :
441 67 : if (mb == NULL) {
442 0 : mnstr_printf(fd, "# function definition missing\n");
443 0 : return;
444 : }
445 67 : if (flg == 0)
446 : return;
447 :
448 67 : assert(size >= 0);
449 67 : assert(first >= 0 && first < mb->stop);
450 67 : if (flg & LIST_MAL_MAPI) {
451 63 : size_t len = 0;
452 63 : str ps;
453 63 : mnstr_printf(fd, "&1 0 %d 1 %d\n", /* type id rows columns tuples */
454 : mb->stop, mb->stop);
455 63 : mnstr_printf(fd, "%% .explain # table_name\n");
456 63 : mnstr_printf(fd, "%% mal # name\n");
457 63 : mnstr_printf(fd, "%% clob # type\n");
458 3030 : for (i = first; i < first + size && i < mb->stop && sample-- > 0; i++) {
459 2904 : ps = instruction2str(mb, stk, getInstrPtr(mb, i), flg);
460 2904 : if (ps) {
461 2904 : size_t l = strlen(ps);
462 2904 : if (l > len)
463 : len = l;
464 2904 : GDKfree(ps);
465 : } else
466 0 : mnstr_printf(fd, "#failed instruction2str()\n");
467 : }
468 63 : mnstr_printf(fd, "%% %zu # length\n", len);
469 : }
470 3011 : for (i = first; i < first + size && i < mb->stop; i++)
471 2944 : printInstruction(fd, mb, stk, getInstrPtr(mb, i), flg);
472 : }
473 :
474 :
475 : void
476 67 : printFunction(stream *fd, MalBlkPtr mb, MalStkPtr stk, int flg)
477 : {
478 67 : int i, j;
479 67 : InstrPtr p;
480 :
481 :
482 : // Set the used bits properly
483 6224 : for (i = 0; i < mb->vtop; i++)
484 6157 : clrVarUsed(mb, i);
485 :
486 :
487 3011 : for (i = 0; i < mb->stop; i++) {
488 2944 : p = getInstrPtr(mb, i);
489 9509 : for (j = p->retc; j < p->argc; j++)
490 6565 : setVarUsed(mb, getArg(p, j));
491 2944 : if (p->barrier)
492 24 : for (j = 0; j < p->retc; j++)
493 12 : setVarUsed(mb, getArg(p, j));
494 : }
495 67 : listFunction(fd, mb, stk, flg, 0, mb->stop);
496 67 : }
497 :
498 : void
499 0 : traceFunction(component_t comp, MalBlkPtr mb, MalStkPtr stk, int flg)
500 : {
501 0 : int i, j;
502 0 : InstrPtr p;
503 : // Set the used bits properly
504 0 : for (i = 0; i < mb->vtop; i++)
505 0 : clrVarUsed(mb, i);
506 0 : for (i = 0; i < mb->stop; i++) {
507 0 : p = getInstrPtr(mb, i);
508 0 : for (j = p->retc; j < p->argc; j++)
509 0 : setVarUsed(mb, getArg(p, j));
510 0 : if (p->barrier)
511 0 : for (j = 0; j < p->retc; j++)
512 0 : setVarUsed(mb, getArg(p, j));
513 : }
514 0 : for (i = 0; i < mb->stop; i++)
515 0 : traceInstruction(comp, mb, stk, getInstrPtr(mb, i), flg);
516 0 : }
517 :
518 : /* initialize the static scope boundaries for all variables */
519 : void
520 2564894 : setVariableScope(MalBlkPtr mb)
521 : {
522 2564894 : int pc, k, depth = 0, dflow = -1;
523 2564894 : InstrPtr p;
524 :
525 : /* reset the scope admin */
526 276713409 : for (k = 0; k < mb->vtop; k++)
527 274148515 : if (isVarConstant(mb, k)) {
528 74929099 : setVarScope(mb, k, 0);
529 74929099 : setVarDeclared(mb, k, 0);
530 74929099 : setVarUpdated(mb, k, 0);
531 74929099 : setVarEolife(mb, k, mb->stop);
532 : } else {
533 199219416 : setVarScope(mb, k, 0);
534 199219416 : setVarDeclared(mb, k, 0);
535 199219416 : setVarUpdated(mb, k, 0);
536 199219416 : setVarEolife(mb, k, 0);
537 : }
538 :
539 142531871 : for (pc = 0; pc < mb->stop; pc++) {
540 139966977 : p = getInstrPtr(mb, pc);
541 :
542 139966977 : if (blockStart(p)) {
543 447249 : if (getModuleId(p) && getFunctionId(p)
544 443720 : && strcmp(getModuleId(p), "language") == 0
545 441378 : && strcmp(getFunctionId(p), "dataflow") == 0) {
546 441417 : if (dflow != -1)
547 0 : addMalException(mb,
548 : "setLifeSpan nested dataflow blocks not allowed");
549 : dflow = depth;
550 : } else
551 5832 : depth++;
552 : }
553 :
554 622157996 : for (k = 0; k < p->argc; k++) {
555 482191019 : int v = getArg(p, k);
556 482191019 : if (isVarConstant(mb, v) && getVarUpdated(mb, v) == 0)
557 68388493 : setVarUpdated(mb, v, pc);
558 :
559 482191019 : if (getVarDeclared(mb, v) == 0) {
560 210911118 : setVarDeclared(mb, v, pc);
561 210911118 : setVarScope(mb, v, depth);
562 : }
563 482191019 : if (k < p->retc)
564 143092913 : setVarUpdated(mb, v, pc);
565 482191019 : if (getVarScope(mb, v) == depth)
566 482147273 : setVarEolife(mb, v, pc);
567 :
568 482191019 : if (k >= p->retc && getVarScope(mb, v) < depth)
569 26959 : setVarEolife(mb, v, -1);
570 : }
571 : /*
572 : * At a block exit we can finalize all variables defined within that block.
573 : * This does not hold for dataflow blocks. They merely direct the execution
574 : * thread, not the syntactic scope.
575 : */
576 139966977 : if (blockExit(p)) {
577 131969208 : for (k = 0; k < mb->vtop; k++)
578 131522018 : if (getVarEolife(mb, k) == 0 && getVarScope(mb, k) == depth)
579 45428948 : setVarEolife(mb, k, pc);
580 86093070 : else if (getVarEolife(mb, k) == -1)
581 10785 : setVarEolife(mb, k, pc);
582 :
583 447190 : if (dflow == depth)
584 : dflow = -1;
585 : else
586 5799 : depth--;
587 : }
588 139966977 : if (blockReturn(p)) {
589 25235 : for (k = 0; k < p->argc; k++)
590 22978 : setVarEolife(mb, getArg(p, k), pc);
591 : }
592 : }
593 276882852 : for (k = 0; k < mb->vtop; k++)
594 274317958 : if (getVarEolife(mb, k) == 0)
595 24548971 : setVarEolife(mb, k, mb->stop - 1);
596 2564894 : }
597 :
598 : int
599 0 : isLoopBarrier(MalBlkPtr mb, int pc)
600 : {
601 0 : InstrPtr p;
602 0 : int varid;
603 0 : p = getInstrPtr(mb, pc);
604 0 : if (p->barrier != BARRIERsymbol)
605 : return 0;
606 0 : varid = getDestVar(p);
607 0 : for (pc++; pc < mb->stop; pc++) {
608 0 : p = getInstrPtr(mb, pc);
609 0 : if (p->barrier == REDOsymbol && getDestVar(p) == varid)
610 : return 1;
611 0 : if (p->barrier == EXITsymbol && getDestVar(p) == varid)
612 : break;
613 : }
614 : return 0;
615 : }
616 :
617 : int
618 0 : getBlockBegin(MalBlkPtr mb, int pc)
619 : {
620 0 : InstrPtr p;
621 0 : int varid = 0, i;
622 :
623 0 : for (i = pc; i < mb->stop; i++) {
624 0 : p = getInstrPtr(mb, i);
625 0 : if (p->barrier == EXITsymbol) {
626 0 : varid = getDestVar(p);
627 0 : break;
628 : }
629 : }
630 0 : if (i == mb->stop)
631 : return 0;
632 :
633 0 : for (; pc > 0; pc--) {
634 0 : p = getInstrPtr(mb, pc);
635 0 : if ((p->barrier == BARRIERsymbol || p->barrier == CATCHsymbol) &&
636 0 : getDestVar(p) == varid)
637 0 : return pc;
638 : }
639 : return 0;
640 : }
641 :
642 : int
643 0 : getBlockExit(MalBlkPtr mb, int pc)
644 : {
645 0 : InstrPtr p;
646 0 : int varid;
647 0 : p = getInstrPtr(mb, pc);
648 0 : if (p->barrier != BARRIERsymbol && p->barrier != CATCHsymbol)
649 : return 0;
650 0 : varid = getDestVar(p);
651 0 : for (pc++; pc < mb->stop; pc++) {
652 0 : p = getInstrPtr(mb, pc);
653 0 : if (p->barrier == EXITsymbol && getDestVar(p) == varid)
654 0 : return pc;
655 : }
656 : return 0;
657 : }
658 :
659 : /*
660 : * Variable declaration
661 : * Variables are implicitly declared upon first use.
662 : * This feature may become a source of runtime errors and
663 : * complicates the analyse during optimization.
664 : * Therefore, in line with the flow of control check,
665 : * we make sure that all variables are properly initialized
666 : * before being used. Since barrier blocks may be skipped at
667 : * runtime, they actually introduce a separate scope.
668 : * Variables declared within a block may not be used outside it.
669 : * Variables can only be declared once.
670 : *
671 : * In many situation chkFlow and chkDeclarations should be called
672 : * together. Moreover, an erroneous chkFlow most likely implies
673 : * errors in the declarations as well.
674 : *
675 : * Since in interactive mode each statement is handled separately,
676 : * we have to remember the scope assigned to a variable.
677 : */
678 : void
679 0 : clrDeclarations(MalBlkPtr mb)
680 : {
681 0 : int i;
682 0 : for (i = 0; i < mb->vtop; i++) {
683 0 : clrVarInit(mb, i);
684 0 : clrVarUsed(mb, i);
685 0 : clrVarDisabled(mb, i);
686 : }
687 0 : }
688 :
689 : str
690 3134107 : chkDeclarations(MalBlkPtr mb)
691 : {
692 3134107 : int pc, i, k, l;
693 3134107 : InstrPtr p, sig;
694 3134107 : short blks[MAXDEPTH], top = 0, blkId = 1;
695 3134107 : int dflow = -1;
696 3134107 : str msg = MAL_SUCCEED;
697 3134107 : char name[IDLENGTH] = { 0 };
698 :
699 3134107 : if (mb->errors)
700 0 : return GDKstrdup(mb->errors);
701 3134107 : blks[top] = blkId;
702 :
703 : /* initialize the scope */
704 378832824 : for (i = 0; i < mb->vtop; i++)
705 375698717 : setVarScope(mb, i, 0);
706 :
707 : /* all signature variables are declared at outer level */
708 3134107 : sig = getInstrPtr(mb, 0);
709 6286257 : for (k = 0; k < sig->argc; k++)
710 3152150 : setVarScope(mb, getArg(sig, k), blkId);
711 :
712 194217872 : for (pc = 1; pc < mb->stop; pc++) {
713 191083770 : p = getInstrPtr(mb, pc);
714 191083770 : if (p->token == REMsymbol)
715 25078204 : continue;
716 : /* check correct use of the arguments */
717 659346222 : for (k = p->retc; k < p->argc; k++) {
718 493340661 : l = getArg(p, k);
719 493340661 : if (l < 0)
720 0 : throw(MAL, "chkFlow",
721 : "%s.%s Non-declared variable: pc=%d, var= %d",
722 : getModuleId(sig), getFunctionId(sig), pc, k);
723 493340661 : setVarUsed(mb, l);
724 493340661 : if (getVarScope(mb, l) == 0) {
725 : /*
726 : * The problem created here is that only variables are
727 : * recognized that are declared through instructions.
728 : * For interactive code, and code that is based on a global
729 : * stack this is insufficient. In those cases, the variable
730 : * can be defined in a previous execution.
731 : * We have to recognize if the declaration takes place
732 : * in the context of a global stack.
733 : */
734 197180145 : if (p->barrier == CATCHsymbol) {
735 0 : setVarScope(mb, l, blks[0]);
736 197180145 : } else if (!(isVarConstant(mb, l) || isVarTypedef(mb, l))
737 11284 : && !isVarInit(mb, l)) {
738 5 : throw(MAL, "chkFlow",
739 : "%s.%s '%s' may not be used before being initialized",
740 : getModuleId(sig), getFunctionId(sig), getVarNameIntoBuffer(mb, l, name));
741 : }
742 296160516 : } else if (!isVarInit(mb, l)) {
743 : /* is the block still active ? */
744 8117 : for (i = 0; i <= top; i++)
745 8116 : if (blks[i] == getVarScope(mb, l))
746 : break;
747 8117 : if (i > top || blks[i] != getVarScope(mb, l))
748 0 : throw(MAL, "chkFlow", "%s.%s '%s' used outside scope",
749 : getModuleId(sig), getFunctionId(sig), getVarNameIntoBuffer(mb, l, name));
750 : }
751 493340656 : if (blockCntrl(p) || blockStart(p))
752 329164 : setVarInit(mb, l);
753 : }
754 : /* define variables */
755 340786511 : for (k = 0; k < p->retc; k++) {
756 174780950 : l = getArg(p, k);
757 174780950 : if (isVarInit(mb, l) && getVarScope(mb, l) == 0) {
758 : /* first time we see this variable and it is already
759 : * initialized: assume it exists globally */
760 133834442 : setVarScope(mb, l, blks[0]);
761 : }
762 174780950 : setVarInit(mb, l);
763 174780950 : if (getVarScope(mb, l) == 0) {
764 : /* variable has not been defined yet */
765 : /* exceptions are always declared at level 1 */
766 40704440 : if (p->barrier == CATCHsymbol)
767 195 : setVarScope(mb, l, blks[0]);
768 : else
769 40704245 : setVarScope(mb, l, blks[top]);
770 : }
771 174780950 : if (blockCntrl(p) || blockStart(p))
772 270538 : setVarUsed(mb, l);
773 : }
774 166005561 : if (p->barrier && msg == MAL_SUCCEED) {
775 501912 : if (blockStart(p)) {
776 248045 : if (top == MAXDEPTH - 2)
777 0 : throw(MAL, "chkFlow",
778 : "%s.%s too deeply nested MAL program",
779 : getModuleId(sig), getFunctionId(sig));
780 248045 : blkId++;
781 248045 : if (getModuleId(p) && getFunctionId(p)
782 243380 : && strcmp(getModuleId(p), "language") == 0
783 241857 : && strcmp(getFunctionId(p), "dataflow") == 0) {
784 241877 : if (dflow != -1)
785 0 : throw(MAL, "chkFlow",
786 : "%s.%s setLifeSpan nested dataflow blocks not allowed",
787 : getModuleId(sig), getFunctionId(sig));
788 241877 : dflow = blkId;
789 : }
790 248045 : blks[++top] = blkId;
791 : }
792 501912 : if (blockExit(p) && top > 0) {
793 248159 : if (dflow == blkId) {
794 : dflow = -1;
795 : } else
796 : /*
797 : * At the end of the block we should reset the status of all variables
798 : * defined within the block. For, the block could have been skipped
799 : * leading to uninitialized variables.
800 : */
801 3488418 : for (l = 0; l < mb->vtop; l++)
802 3482173 : if (getVarScope(mb, l) == blks[top]) {
803 59991 : setVarScope(mb, l, 0);
804 59991 : clrVarInit(mb, l);
805 : }
806 248159 : top--;
807 : }
808 : }
809 : }
810 : return msg;
811 : }
|