1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#ifndef _MAL_INSTR_H
10#define _MAL_INSTR_H
11
12#include "mal_type.h"
13#include "mal_stack.h"
14#include "mal_namespace.h"
15
16#define isaSignature(P) ((P)->token >=COMMANDsymbol)
17
18#ifdef HAVE_SYS_TIMES_H
19# include <sys/times.h>
20#endif
21
22#define DEBUG_MAL_INSTR
23/* #define DEBUG_REDUCE */
24#define MAXARG 8 /* was 4 BEWARE the code depends on this knowledge, where? */
25#define STMT_INCREMENT 4
26#define MAL_VAR_WINDOW 32
27#define MAXLISTING (64*1024)
28
29/* Allocation of space assumes a rather exotic number of
30 * arguments. Access to module and function name are cast in macros to
31 * prepare for separate name space management. */
32#define getModuleId(P) (P)->modname
33#define setModuleId(P,S) (P)->modname= S
34#define setModuleScope(P,S) do {(P)->modname= (S)==NULL?NULL: (S)->name;} while (0)
35
36#define getFunctionId(P) (P)->fcnname
37#define setFunctionId(P,S) (P)->fcnname= S
38#define garbageControl(P) ((P)->gc & GARBAGECONTROL)
39
40#define getInstrPtr(M,I) (M)->stmt[I]
41#define getSignature(S) getInstrPtr((S)->def,0)
42#define isMain(M) ((getInstrPtr(M,0))->fcnname== putName("main",4))
43#define getFcnName(M) getFunctionId(getInstrPtr(M,0))
44#define getArgCount(M) getInstrPtr(M,0)->argc
45#define getModName(M) getModuleId(getInstrPtr(M,0))
46#define getPrgSize(M) (M)->stop
47
48#define getVar(M,I) (&(M)->var[I])
49#define getVarType(M,I) ((M)->var[I].type)
50#define getVarName(M,I) ((M)->var[I].id)
51#define getVarGDKType(M,I) getGDKType((M)->var[I].type)
52#define setVarType(M,I,V) (M)->var[I].type = V
53
54#define clrVarFixed(M,I) ((M)->var[I].fixedtype = 0)
55#define setVarFixed(M,I) ((M)->var[I].fixedtype =1)
56#define isVarFixed(M,I) ((M)->var[I].fixedtype)
57
58#define clrVarCleanup(M,I) ((M)->var[I].cleanup = 0)
59#define setVarCleanup(M,I) ((M)->var[I].cleanup = 1)
60#define isVarCleanup(M,I) ((M)->var[I].cleanup )
61
62#define isTmpVar(M,I) (*getVarName(M,I) == REFMARKER && *(getVarName(M,I)+1) == TMPMARKER)
63
64#define clrVarUsed(M,I) ((M)->var[I].used = 0)
65#define setVarUsed(M,I) ((M)->var[I].used = 1)
66#define isVarUsed(M,I) ((M)->var[I].used)
67
68#define clrVarDisabled(M,I) ((M)->var[I].disabled= 0 )
69#define setVarDisabled(M,I) ((M)->var[I].disabled = 1)
70#define isVarDisabled(M,I) ((M)->var[I].disabled)
71
72#define clrVarInit(M,I) ((M)->var[I].initialized = 0)
73#define setVarInit(M,I) ((M)->var[I].initialized = 1)
74#define isVarInit(M,I) ((M)->var[I].initialized)
75
76#define clrVarTypedef(M,I) ((M)->var[I].typevar = 0)
77#define setVarTypedef(M,I) ((M)->var[I].typevar = 1)
78#define isVarTypedef(M,I) ((M)->var[I].typevar)
79
80#define clrVarUDFtype(M,I) ((M)->var[I].udftype = 0)
81#define setVarUDFtype(M,I) ((M)->var[I].udftype = 1)
82#define isVarUDFtype(M,I) ((M)->var[I].udftype)
83
84#define clrVarConstant(M,I) ((M)->var[I].constant = 0)
85#define setVarConstant(M,I) ((M)->var[I].constant = 1)
86#define isVarConstant(M,I) ((M)->var[I].constant)
87
88#define setVarDeclared(M,I,X) ((M)->var[I].declared = X )
89#define getVarDeclared(M,I) ((M)->var[I].declared)
90
91#define setVarUpdated(M,I,X) ((M)->var[I].updated = X )
92#define getVarUpdated(M,I) ((M)->var[I].updated)
93
94#define setVarEolife(M,I,X) ((M)->var[I].eolife = X )
95#define getVarEolife(M,I) ((M)->var[I].eolife)
96
97#define setVarWorker(M,I,S) ((M)->var[I].worker = S)
98#define getVarWorker(M,I) ((M)->var[I].worker)
99
100#define setVarScope(M,I,S) ((M)->var[I].depth = S)
101#define getVarScope(M,I) ((M)->var[I].depth)
102
103#define clrVarCList(M,I) ((M)->var[I].id[0]= REFMARKER)
104#define setVarCList(M,I) ((M)->var[I].id[0]= REFMARKERC)
105#define isVarCList(M,I) ((M)->var[I].id[0] == REFMARKERC)
106
107#define getVarConstant(M,I) ((M)->var[I].value)
108#define getVarValue(M,I) VALget(&(M)->var[I].value)
109
110#define setRowCnt(M,I,C) (M)->var[I].rowcnt = C
111#define getRowCnt(M,I) ((M)->var[I].rowcnt)
112
113#define setMitosisPartition(P,C) (P)->mitosis = C
114#define getMitosisPartition(P) ((P)->mitosis)
115
116#define getVarSTC(M,I) ((M)->var[I].stc)
117
118#define getDestVar(P) (P)->argv[0]
119#define setDestVar(P,X) (P)->argv[0] =X
120#define setDestType(M,P,V) setVarType((M),getDestVar(P),V)
121#define getDestType(M,P) destinationType((M),(P))
122#define getArg(P,I) (P)->argv[I]
123#define setArg(P,I,R) (P)->argv[I]= R
124#define getArgName(M,P,I) getVarName((M),(P)->argv[I])
125#define getArgType(M,P,I) getVarType((M),(P)->argv[I])
126#define getArgGDKType(M,P,I) getVarGDKType((M),(P)->argv[I])
127#define getGDKType(T) ( T <= TYPE_str ? T : (T == TYPE_any ? TYPE_void : findGDKtype(T)))
128
129mal_export void addMalException(MalBlkPtr mb, str msg);
130mal_export void mal_instruction_reset(void);
131mal_export InstrPtr newInstruction(MalBlkPtr mb, str modnme, str fcnnme);
132mal_export InstrPtr newInstructionArgs(MalBlkPtr mb, str modnme, str fcnnme, int args);
133mal_export InstrPtr copyInstruction(InstrPtr p);
134mal_export void oldmoveInstruction(InstrPtr dst, InstrPtr src);
135mal_export void clrInstruction(InstrPtr p);
136mal_export void freeInstruction(InstrPtr p);
137mal_export void clrFunction(InstrPtr p);
138mal_export Symbol newSymbol(str nme, int kind);
139mal_export void freeSymbol(Symbol s);
140mal_export void freeSymbolList(Symbol s);
141mal_export void printSignature(stream *fd, Symbol s, int flg);
142
143mal_export MalBlkPtr newMalBlk(int elements);
144mal_export void resetMalBlk(MalBlkPtr mb, int stop);
145mal_export void resetMalBlkAndFreeInstructions(MalBlkPtr mb, int stop);
146mal_export int newMalBlkStmt(MalBlkPtr mb, int elements);
147mal_export int resizeMalBlk(MalBlkPtr mb, int elements);
148mal_export int prepareMalBlk(MalBlkPtr mb, str s);
149mal_export void freeMalBlk(MalBlkPtr mb);
150mal_export MalBlkPtr copyMalBlk(MalBlkPtr mb);
151mal_export void addtoMalBlkHistory(MalBlkPtr mb);
152mal_export MalBlkPtr getMalBlkHistory(MalBlkPtr mb, int idx);
153mal_export MalBlkPtr getMalBlkOptimized(MalBlkPtr mb, str name);
154mal_export void trimMalVariables(MalBlkPtr mb, MalStkPtr stk);
155mal_export void trimMalVariables_(MalBlkPtr mb, MalStkPtr glb);
156mal_export void moveInstruction(MalBlkPtr mb, int pc, int target);
157mal_export void removeInstruction(MalBlkPtr mb, InstrPtr p);
158mal_export void removeInstructionBlock(MalBlkPtr mb, int pc, int cnt);
159mal_export str operatorName(int i);
160
161mal_export int findVariable(MalBlkPtr mb, const char *name);
162mal_export int findVariableLength(MalBlkPtr mb, str name, int len);
163mal_export malType getType(MalBlkPtr mb, str nme);
164mal_export str getArgDefault(MalBlkPtr mb, InstrPtr p, int idx);
165mal_export int newVariable(MalBlkPtr mb, const char *name, size_t len, malType type);
166mal_export int cloneVariable(MalBlkPtr dst, MalBlkPtr src, int varid);
167/* generate a new variable name based on a pattern with 1 %d argument
168 * -- not used, but this is how to do it */
169/* #define renameVariable(mb, id, pattern, newid) snprintf(getVarName(mb,id),IDLENGTH,pattern,newid) */
170mal_export int newTmpVariable(MalBlkPtr mb, malType type);
171mal_export int newTypeVariable(MalBlkPtr mb, malType type);
172mal_export void freeVariable(MalBlkPtr mb, int varid);
173mal_export void clearVariable(MalBlkPtr mb, int varid);
174mal_export int cpyConstant(MalBlkPtr mb, VarPtr vr);
175mal_export int defConstant(MalBlkPtr mb, int type, ValPtr cst);
176mal_export int fndConstant(MalBlkPtr mb, const ValRecord *cst, int depth);
177mal_export str convertConstant(malType type, ValPtr vr);
178
179mal_export void pushInstruction(MalBlkPtr mb, InstrPtr p);
180mal_export InstrPtr pushArgument(MalBlkPtr mb, InstrPtr p, int varid);
181mal_export InstrPtr setArgument(MalBlkPtr mb, InstrPtr p, int idx, int varid);
182mal_export InstrPtr pushReturn(MalBlkPtr mb, InstrPtr p, int varid);
183mal_export InstrPtr pushArgumentId(MalBlkPtr mb, InstrPtr p, const char *name);
184mal_export void delArgument(InstrPtr p, int varid);
185mal_export void setArgType(MalBlkPtr mb, InstrPtr p, int i, int tpe);
186mal_export void setReturnArgument(InstrPtr p, int varid);
187mal_export malType destinationType(MalBlkPtr mb, InstrPtr p);
188mal_export void setPolymorphic(InstrPtr p, int tpe, int force);
189/* Utility macros to inspect an instruction */
190#define functionStart(X) ((X)->token == FUNCTIONsymbol || \
191 (X)->token == COMMANDsymbol || \
192 (X)->token == FACTORYsymbol )
193#define patternStart(X) ((X)->token == PATTERNsymbol)
194#define functionExit(X) ((X)->token == ENDsymbol)
195
196#define blockStart(X) ((X)->barrier && (((X)->barrier == BARRIERsymbol || \
197 (X)->barrier == CATCHsymbol )))
198#define blockExit(X) ((X)->barrier == EXITsymbol)
199#define blockReturn(X) ((X)->barrier == RETURNsymbol)
200#define blockCntrl(X) ( (X)->barrier== LEAVEsymbol || \
201 (X)->barrier== REDOsymbol || (X)->barrier== RETURNsymbol )
202#define isLinearFlow(X) (!(blockStart(X) || blockExit(X) || \
203 (X)->barrier== LEAVEsymbol || (X)->barrier== REDOsymbol ))
204
205mal_export void strBeforeCall(ValPtr v, ValPtr bak);
206mal_export void strAfterCall(ValPtr v, ValPtr bak);
207mal_export void batBeforeCall(ValPtr v, ValPtr bak);
208mal_export void batAfterCall(ValPtr v, ValPtr bak);
209#endif /* _MAL_INSTR_H */
210