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
|
/* This file is part of the Project Athena Zephyr Notification System.
* It is one of the source files comprising zwgc, the Zephyr WindowGram
* client.
*
* Created by: Marc Horowitz <marc@athena.mit.edu>
*
* $Id$
*
* Copyright (c) 1989 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#include <sysdep.h>
#if (!defined(lint) && !defined(SABER))
static const char rcsid_node_c[] = "$Id$";
#endif
#include <zephyr/mit-copyright.h>
#include "new_memory.h"
#include "node.h"
/****************************************************************************/
/* */
/* Internal node construction & destruction functions: */
/* */
/****************************************************************************/
/*
* NODE_BATCH_SIZE - the number of nodes to malloc at once to save overhead:
*/
#define NODE_BATCH_SIZE 100
/*
* The nodes we have malloced are kept in a linked list of bunches of
* NODE_BATCH_SIZE nodes. Nodes points to the first bunch on the list
* and current_bunch to the last. All nodes from the first one in the first
* bunch to the last_node_in_current_bunch_used'th one in the last bunch
* are in use. The others have not been used yet.
*/
static struct _bunch_of_nodes {
struct _bunch_of_nodes *next_bunch;
Node nodes[NODE_BATCH_SIZE];
} *nodes = NULL;
static struct _bunch_of_nodes *current_bunch = NULL;
static int last_node_in_current_bunch_used = -1;
/*
* Internal Routine:
*
* Node *node_create(int opcode)
* Effects: Creates a node with opcode opcode and returns a pointer
* to it. The next pointer of the returned node is NULL.
* If the opcode is STRING_CONSTANT_OPCODE the caller must
* ensure that the string_constant field points to a valid
* string on the heap when node_DestroyAllNodes is called.
*/
static Node *
node_create(int opcode)
{
Node *result;
if (!nodes) {
/*
* Handle special case where no nodes allocated yet:
*/
current_bunch = nodes = (struct _bunch_of_nodes *)
malloc(sizeof(struct _bunch_of_nodes));
nodes->next_bunch = NULL;
last_node_in_current_bunch_used = -1;
}
/*
* If all nodes allocated so far in use, allocate another
* bunch of NODE_BATCH_SIZE nodes:
*/
if (last_node_in_current_bunch_used == NODE_BATCH_SIZE-1) {
current_bunch->next_bunch = (struct _bunch_of_nodes *)
malloc(sizeof(struct _bunch_of_nodes));
current_bunch = current_bunch->next_bunch;
current_bunch->next_bunch = NULL;
last_node_in_current_bunch_used = -1;
}
/*
* Get next not already used node & ready it for use:
*/
last_node_in_current_bunch_used++;
result = &(current_bunch->nodes[last_node_in_current_bunch_used]);
result->opcode = opcode;
result->next = NULL;
return(result);
}
/*
*
*/
void
node_DestroyAllNodes(void)
{
struct _bunch_of_nodes *next_bunch;
int i, last_node_used_in_this_bunch;
while (nodes) {
next_bunch = nodes->next_bunch;
last_node_used_in_this_bunch = next_bunch ?
NODE_BATCH_SIZE-1 : last_node_in_current_bunch_used;
for (i=0; i<=last_node_used_in_this_bunch; i++) {
if (nodes->nodes[i].opcode==STRING_CONSTANT_OPCODE)
free(nodes->nodes[i].d.string_constant);
else if (nodes->nodes[i].opcode==VARREF_OPCODE)
free(nodes->nodes[i].d.string_constant);
else if (nodes->nodes[i].opcode==VARNAME_OPCODE)
free(nodes->nodes[i].d.string_constant);
}
free(nodes);
nodes = next_bunch;
}
current_bunch = nodes;
}
/****************************************************************************/
/* */
/* Node construction functions: */
/* */
/****************************************************************************/
Node *
node_create_string_constant(int opcode,
string text)
{
Node *n;
n = node_create(opcode);
n->d.string_constant = text;
return(n);
}
Node *
node_create_noary(int opcode)
{
Node *n;
n = node_create(opcode);
return(n);
}
Node *
node_create_unary(int opcode,
Node *arg)
{
Node *n;
n = node_create(opcode);
n->d.nodes.first = arg;
return(n);
}
Node *
node_create_binary(int opcode,
Node *first_arg,
Node *second_arg)
{
Node *n;
n = node_create(opcode);
n->d.nodes.first = first_arg;
n->d.nodes.second = second_arg;
return(n);
}
/****************************************************************************/
/* */
/* Node utility functions: */
/* */
/****************************************************************************/
/*
* Node *reverse_list_of_nodes(Node *list)
* Modifies: the nodes on the linked list list
* Effects: Reverses the linked list list and returns it.
* This is done by modifing the next pointers of the
* list elements to point to the previous node & returning
* the address of the (previously) last node.
*/
Node *
reverse_list_of_nodes(Node *list)
{
Node *next_node;
Node *head = NULL;
while (list) {
next_node = list->next;
/*
* Add the node list to the beginning of linked list head:
*/
list->next = head;
head = list;
list = next_node;
}
return(head);
}
/****************************************************************************/
/* */
/* Node display functions: */
/* */
/****************************************************************************/
#ifdef DEBUG
static void
print_stuff(Node *node,
string format_string)
{
char c;
for (c=(*(format_string++)); c; c=(*(format_string++))) {
if (c!='%') {
putchar(c);
continue;
}
c=(*(format_string++));
if (!c) {
format_string--;
continue;
}
if (c=='s')
printf("%s", node->d.string_constant);
else if (c=='1')
node_display(node->d.nodes.first);
else if (c=='2')
node_display(node->d.nodes.second);
else
putchar(c);
}
}
static string how_to_print[] = {
"\"%s\"", /* constant string */
"$%s", /* varref */
"%s", /* varname */
"!%1",
"( %1 + %2 )",
"( %1 and %2 )",
"( %1 or %2 )",
"( %1 == %2 )",
"( %1 != %2 )",
"( %1 =~ %2 )",
"( %1 !~ %2 )",
"buffer()",
"substitute(%1)",
"protect(%1)",
"verbatim(%1)",
"stylestrip(%1)",
"getenv(%1)",
"upcase(%1)",
"downcase(%1)",
"zvar(%1)",
"get(%1)",
"lany(%1, %2)",
"rany(%1, %2)",
"lbreak(%1, %2)",
"rbreak(%1, %2)",
"lspan(%1, %2)",
"rspan(%1, %2)",
"noop\n",
"set %1 = %2\n",
"fields %1\n",
"print %1\n",
"clearbuf\n",
"appendport %1 %2\n",
"execport %1 %2\n",
"inputport %1 %2\n",
"outputport %1 %2\n",
"put %1 %2\n",
"closeinput %1\n",
"closeoutput %1\n",
"closeport %1\n",
"exec %1 %2\n",
"%1endif\n",
"case %1\n%2endcase\n",
"while %1 do\n%2endwhile\n",
"break\n",
"exit\n",
"if %1 then\n%2",
"elseif %1 then\n%2",
"else\n%2",
"match %1\n%2",
"default\n%2" };
void node_display(Node *node)
{
int opcode = LAST_EXPR_OPCODE + 1;
for (; node; node=node->next) {
if (opcode<=LAST_EXPR_OPCODE)
printf(" ");
opcode = node->opcode;
if (opcode>=0 && opcode<NUMBER_OF_OPCODES)
print_stuff(node, how_to_print[opcode]);
else
printf("[opcode %d]", opcode);
}
}
#endif
|