1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
include <tbset.h>
include "trs.h"
#* HISTORY *
#* B.Simon 02-Jan-98 original
# TRSGENCODE -- Generate pseudocode from binary tree
procedure trsgencode (tp, root, pcode)
pointer tp # i: table descriptor
int root # i: root node of binary tree
pointer pcode # u: pseudocode structure
#--
int nrow
bool trshasrow()
int tbpsta()
pointer trsoptimize(), rst_create()
errchk trshasrow, trsputcode, trsoptimze
begin
nrow = tbpsta (tp, TBL_NROWS)
if (trshasrow (root)) {
TRS_ROWS(pcode) = trsoptimize (root, nrow)
} else {
TRS_ROWS(pcode) = rst_create (1, nrow)
}
call trsputcode (root, pcode)
call trsputjump (root, pcode)
end
# TRSHASROW -- Does code contains a row expression that can be optimized?
bool procedure trshasrow (root)
pointer root # i: root of binary tree
#--
bool result, hasrow
pointer node, child
bool trs_over_tree()
pointer trs_first_tree(), trs_next_tree()
errchk trs_xcg_tree
begin
# Expressions without row ranges cannot be optimized. Also
# expressions with YNOT outside of YRANGE cannot be optimized.
# However, if the YNOT operates on a single range, the order
# of the YRANGE and YNOT can be flipped
result = true
hasrow = false
node = trs_first_tree (root)
while (node != NULL) {
if (TREE_OPER(node) == YRANGE && TREE_RIGHT(node) == NULL) {
hasrow = true
} else if (TREE_OPER(node) == YNOT) {
# If a YNOT is found outside a YRANGE controlling a row,
# it is not optimizable unless the two can be swapped
child = TREE_LEFT(node)
if (TREE_OPER(child) == YRANGE) {
# YNOT and YRANGE can be swapped, so do it
call trs_xcg_tree (child)
} else if (trs_over_tree (node)) {
# Can't be swapped and over row range,
# so not optimizable
result = false
}
}
node = trs_next_tree (node)
}
# No row range, so not optimizable
if (! hasrow)
result = false
return (result)
end
# TRSOPTIMIZE -- Optimize an expression by evaluting its row ranges
pointer procedure trsoptimize (root, nrow)
pointer root # i: root of binary tree
int nrow # i: number of rows in table
#--
int top, istack, nstack
pointer sp, eval, node, prev, set
bool trs_under_tree()
pointer trs_first_tree(), trs_next_tree()
errchk trsroweval, trs_snip_tree
begin
# Allocate arrays used in traversing binary tree
call smark (sp)
call salloc (eval, MAXDEPTH, TY_INT)
# Traverse the binary tree, looking for row expressions
# when one is found, evaluate it and remove it from the tree
top = 0
node = trs_first_tree (root)
while(node != NULL) {
# Evaluate row expressions
if (trs_under_tree (node))
call trsroweval (TREE_OPER(node), -TREE_LEFT(node),
-TREE_RIGHT(node), nrow, Memi[eval],
top)
prev = node
node = trs_next_tree (node)
# After complete evaluation of the row expression
# snip it out of the binary tree. If both branches
# of a logical have been snipped, also snip it out
# of the tree. Don't have to worry about YNOT as it
# was already buried beneath YRANGE in trshasrow
if (TREE_OPER(prev) == YRANGE && TREE_RIGHT(prev) == NULL) {
call trs_snip_tree (prev)
} else if ((TREE_OPER(prev) == YAND || TREE_OPER(prev) == YOR) &&
(TREE_RIGHT(prev) == NULL && TREE_LEFT(prev) == NULL)) {
call trs_snip_tree (prev)
}
}
# If there is more than one row expression, they are
# combined with ands
nstack = top - 1
do istack = 1, nstack
call trsroweval (YAND, NULL, NULL, nrow, Memi[eval], top)
# Return the row set evaluated
set = Memi[eval]
call sfree (sp)
return (set)
end
# TRSPUTCODE -- Convert binary tree into pseudocode instructions
procedure trsputcode (root, pcode)
pointer root # i: root of binary tree
pointer pcode # u: pseudocode structure
#--
int icode, oper
pointer codebuf, node, col, loval, hival
string noroom "Table row selection expression too complex"
pointer trs_first_tree(), trs_next_tree(), trs_col_tree()
begin
icode = 0
codebuf = TRS_CODE(pcode)
node = trs_first_tree (root)
while (node != NULL) {
oper = TREE_OPER(node)
if ((oper == YAND || oper == YOR) &&
(TREE_LEFT(node) == NULL ||
TREE_RIGHT(node) == NULL)) {
# Skip encoding if one branch of a logical
# has been snipped
TREE_INST(node) = ERR
} else {
# Check for buffer overflow
if (icode + SZ_INSTR >= SZ_BUFFER)
call error (1, noroom)
# Set instruction field in tree
TREE_INST(node) = icode
# Retrieve column value
if (YLOGICAL(oper))
col = NULL
else
col = trs_col_tree (node)
# Retrieve field values
call trsvalue (node, loval, hival)
# Add instruction to code buffer
Memi[codebuf+icode+OCODE] = oper
Memi[codebuf+icode+OCOLUMN] = col
Memi[codebuf+icode+OTJUMP] = NULL
Memi[codebuf+icode+OFJUMP] = NULL
Memi[codebuf+icode+OLOVAL] = loval
Memi[codebuf+icode+OHIVAL] = hival
# Increment code buffer index
icode = icode + SZ_INSTR
}
node = trs_next_tree (node)
}
end
# TRSPUTJUMP -- Add jumps to pseudocode
procedure trsputjump (root, pcode)
pointer root # i: root of binary tree
pointer pcode # u: pseudocode structure
#--
int icode, inst
pointer codebuf, node, jump, child
pointer trs_first_tree(), trs_next_tree()
begin
codebuf = TRS_CODE(pcode)
node = trs_first_tree (root)
while (node != NULL) {
if (TREE_INST(node) != ERR) {
inst = TREE_OPER(node)
jump = TREE_INST(node)
child = TREE_LEFT(node)
if (child > 0) {
icode = TREE_INST(child)
if (inst == YOR)
Memi[codebuf+icode+OTJUMP] = jump
if (inst == YAND)
Memi[codebuf+icode+OFJUMP] = jump
}
}
node = trs_next_tree (node)
}
end
# TRSROWEVAL -- Evaluate an operation in a row expression
procedure trsroweval (code, loval, hival, nrow, eval, top)
int code # i: pseudocode instruction
pointer loval # i: low end of range
pointer hival # i: high end of range
int nrow # i: number of rows in table
pointer eval[MAXDEPTH] # u: stack of pending results
int top # u: index to top of stack
#--
int narg, iarg, lo, hi
string ovflow "trs_roweval: stack overflow"
string badcode "trs_roweval: bad instruction"
pointer rst_create(), rst_and(), rst_or(), rst_not()
begin
if (top == MAXDEPTH)
call error (1, ovflow)
switch (code) {
case YRANGE: # range operation, really a no-op
narg = 0
case YAND: # logical and
narg = 2
top = top + 1
eval[top] = rst_and (eval[top-1], eval[top-2])
case YOR: # logical or
narg = 2
top = top + 1
eval[top] = rst_or (eval[top-1], eval[top-2])
case YNOT: # logical not
narg = 1
top = top + 1
eval[top] = rst_not (nrow, eval[top-1])
case YEQN: # numerical equality test
narg = 0
top = top + 1
lo = max (1, int(Memd[loval]))
eval[top] = rst_create (lo, lo)
case YLEN: # numeric less than or equal check
narg = 0
top = top + 1
lo = max (1, int(Memd[loval]))
eval[top] = rst_create (1, lo)
case YINN: # numeric inclusion check
narg = 0
top = top + 1
lo = min (Memd[loval], Memd[hival])
hi = max (Memd[loval], Memd[hival])
lo = max (1, lo)
hi = min (nrow, hi)
eval[top] = rst_create (lo, hi)
case YGEN: # numeric greater than or equal check
narg = 0
top = top + 1
hi = min (nrow, int(Memd[loval]))
eval[top] = rst_create (hi, nrow)
default:
call error (1, badcode)
}
# Free used stack elements
if (narg > 0) {
do iarg = 1, narg
call rst_free (eval[top-iarg])
eval[top-narg] = eval[top]
top = top - narg
}
end
# TRSVALUE -- Extract field values from a node of a binary tree
procedure trsvalue (node, loval, hival)
pointer node # i: binary tree node
pointer loval # o: smaller of the two values
pointer hival # o: larger of the two values
#--
bool strgt()
begin
if (TREE_RIGHT(node) == NULL) {
# Duplicate left value if right value is NULL
loval = -TREE_LEFT(node)
hival = -TREE_LEFT(node)
} else {
# Flip high and low values if out of order
if (TREE_OPER(node) == YINN) {
if (Memd[-TREE_RIGHT(node)] >
Memd[-TREE_LEFT(node)]) {
loval = -TREE_LEFT(node)
hival = -TREE_RIGHT(node)
} else {
loval = -TREE_RIGHT(node)
hival = -TREE_LEFT(node)
}
} else if (TREE_OPER(node) == YINS) {
if (strgt (Memc[-TREE_RIGHT(node)],
Memc[-TREE_LEFT(node)])) {
loval = -TREE_LEFT(node)
hival = -TREE_RIGHT(node)
} else {
loval = -TREE_RIGHT(node)
hival = -TREE_LEFT(node)
}
}
}
# Set values to null if the value is actually a node address
if (loval < 0)
loval = NULL
if (hival < 0)
hival = NULL
end
|