forked from shattered/macro11
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rept_irpc.c
406 lines (315 loc) · 9.68 KB
/
rept_irpc.c
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
#define REPT_IRPC__C
/*
.REPT
.IRPC streams
*/
#include <stdlib.h>
#include <string.h>
#include "rept_irpc.h" /* my own definitions */
#include "util.h"
#include "assemble_aux.h"
#include "parse.h"
#include "listing.h"
#include "macros.h"
#include "assemble_globals.h"
/* *** implement REPT_STREAM */
typedef struct rept_stream {
BUFFER_STREAM bstr;
int count; /* The current repeat countdown */
int savecond; /* conditional stack level at time of
expansion */
} REPT_STREAM;
/* rept_stream_getline gets a line from a repeat stream. At the end of
each count, the coutdown is decreated and the stream is reset to
its beginning. */
char *rept_stream_getline(
STREAM *str)
{
REPT_STREAM *rstr = (REPT_STREAM *) str;
char *cp;
for (;;) {
if (rstr->count <= 0)
return NULL;
if ((cp = buffer_stream_getline(str)) != NULL)
return cp;
buffer_stream_rewind(str);
rstr->count--;
}
}
/* rept_stream_delete unwinds nested conditionals like .MEXIT does. */
void rept_stream_delete(
STREAM *str)
{
REPT_STREAM *rstr = (REPT_STREAM *) str;
pop_cond(rstr->savecond); /* complete unterminated
conditionals */
buffer_stream_delete(&rstr->bstr.stream);
}
/* The VTBL */
STREAM_VTBL rept_stream_vtbl = {
rept_stream_delete, rept_stream_getline, buffer_stream_rewind
};
/* expand_rept is called when a .REPT is encountered in the input. */
STREAM *expand_rept(
STACK *stack,
char *cp)
{
EX_TREE *value;
BUFFER *gb;
REPT_STREAM *rstr;
int levelmod;
value = parse_expr(cp, 0);
if (value->type != EX_LIT) {
report(stack->top, ".REPT value must be constant\n");
free_tree(value);
return NULL;
}
/*
* Reading the next lines-to-be-repeated overwrites the line buffer
* that the caller is using. So for junk-at-end-of-line checking we
* need to do it here.
*/
check_eol(stack, value->cp);
list_value(stack->top, value->data.lit);
gb = new_buffer();
levelmod = 0;
if (!list_md) {
list_level--;
levelmod = 1;
}
read_body(stack, gb, NULL, FALSE);
list_level += levelmod;
rstr = memcheck(malloc(sizeof(REPT_STREAM))); {
char *name = memcheck(malloc(strlen(stack->top->name) + 32));
sprintf(name, "%s:%d->.REPT", stack->top->name, stack->top->line);
buffer_stream_construct(&rstr->bstr, gb, name);
free(name);
}
rstr->count = value->data.lit;
rstr->bstr.stream.vtbl = &rept_stream_vtbl;
rstr->savecond = last_cond;
buffer_free(gb);
free_tree(value);
return &rstr->bstr.stream;
}
/* *** implement IRP_STREAM */
typedef struct irp_stream {
BUFFER_STREAM bstr;
char *label; /* The substitution label */
char *items; /* The substitution items (in source code
format) */
int offset; /* Current offset into "items" */
BUFFER *body; /* Original body */
int savecond; /* Saved conditional level */
} IRP_STREAM;
/* irp_stream_getline expands the IRP as the stream is read. */
/* Each time an iteration is exhausted, the next iteration is
generated. */
char *irp_stream_getline(
STREAM *str)
{
IRP_STREAM *istr = (IRP_STREAM *) str;
char *cp;
BUFFER *buf;
ARG *arg;
for (;;) {
if ((cp = buffer_stream_getline(str)) != NULL)
return cp;
cp = istr->items + istr->offset;
if (!*cp)
return NULL; /* No more items. EOF. */
arg = new_arg();
arg->next = NULL;
arg->locsym = 0;
arg->label = istr->label;
arg->value = getstring_macarg(str, cp, &cp);
cp = skipdelim(cp);
istr->offset = (int) (cp - istr->items);
buf = subst_args(istr->body, arg);
free(arg->value);
free(arg);
buffer_stream_set_buffer(&istr->bstr, buf);
buffer_free(buf);
}
}
/* irp_stream_delete - also pops the conditional stack */
void irp_stream_delete(
STREAM *str)
{
IRP_STREAM *istr = (IRP_STREAM *) str;
pop_cond(istr->savecond); /* complete unterminated
conditionals */
buffer_free(istr->body);
free(istr->items);
free(istr->label);
buffer_stream_delete(str);
}
STREAM_VTBL irp_stream_vtbl = {
irp_stream_delete, irp_stream_getline, buffer_stream_rewind
};
/* We occasionally see .IRP with the formal name in angle brackets. I
* have no idea why, but it appears to be legal. So allow that. Not
* sure if it should be allowed elsewhere, e.g., in .MACRO. For now,
* don't. */
static char *get_irp_sym (char *cp, char **endcp, int *islocal)
{
char *ret = NULL;
cp = skipwhite(cp);
if (*cp == '<') {
ret = get_symbol (cp + 1, &cp, islocal);
if (*cp++ != '>') {
*endcp = cp;
return NULL;
}
}
else {
ret = get_symbol (cp, &cp, islocal);
}
*endcp = cp;
return ret;
}
/* expand_irp is called when a .IRP is encountered in the input. */
STREAM *expand_irp(
STACK *stack,
char *cp)
{
char *label,
*items;
BUFFER *gb;
int levelmod = 0;
IRP_STREAM *str;
label = get_irp_sym(cp, &cp, NULL);
if (!label) {
report(stack->top, "Illegal .IRP syntax\n");
return NULL;
}
cp = skipdelim(cp);
items = getstring(cp, &cp);
if (!items) {
report(stack->top, "Illegal .IRP syntax\n");
free(label);
return NULL;
}
check_eol(stack, cp);
gb = new_buffer();
levelmod = 0;
if (!list_md) {
list_level--;
levelmod++;
}
read_body(stack, gb, NULL, FALSE);
list_level += levelmod;
str = memcheck(malloc(sizeof(IRP_STREAM))); {
char *name = memcheck(malloc(strlen(stack->top->name) + 32));
sprintf(name, "%s:%d->.IRP", stack->top->name, stack->top->line);
buffer_stream_construct(&str->bstr, NULL, name);
free(name);
}
str->bstr.stream.vtbl = &irp_stream_vtbl;
str->body = gb;
str->items = items;
str->offset = 0;
str->label = label;
str->savecond = last_cond;
return &str->bstr.stream;
}
/* *** implement IRPC_STREAM */
typedef struct irpc_stream {
BUFFER_STREAM bstr;
char *label; /* The substitution label */
char *items; /* The substitution items (in source code
format) */
int offset; /* Current offset in "items" */
BUFFER *body; /* Original body */
int savecond; /* conditional stack at invocation */
} IRPC_STREAM;
/* irpc_stream_getline - same comments apply as with irp_stream_getline, but
the substitution is character-by-character */
char *irpc_stream_getline(
STREAM *str)
{
IRPC_STREAM *istr = (IRPC_STREAM *) str;
char *cp;
BUFFER *buf;
ARG *arg;
for (;;) {
if ((cp = buffer_stream_getline(str)) != NULL)
return cp;
cp = istr->items + istr->offset;
if (!*cp)
return NULL; /* No more items. EOF. */
arg = new_arg();
arg->next = NULL;
arg->locsym = 0;
arg->label = istr->label;
arg->value = memcheck(malloc(2));
arg->value[0] = *cp++;
arg->value[1] = 0;
istr->offset = (int) (cp - istr->items);
buf = subst_args(istr->body, arg);
free(arg->value);
free(arg);
buffer_stream_set_buffer(&istr->bstr, buf);
buffer_free(buf);
}
}
/* irpc_stream_delete - also pops contidionals */
void irpc_stream_delete(
STREAM *str)
{
IRPC_STREAM *istr = (IRPC_STREAM *) str;
pop_cond(istr->savecond); /* complete unterminated
conditionals */
buffer_free(istr->body);
free(istr->items);
free(istr->label);
buffer_stream_delete(str);
}
STREAM_VTBL irpc_stream_vtbl = {
irpc_stream_delete, irpc_stream_getline, buffer_stream_rewind
};
/* expand_irpc - called when .IRPC is encountered in the input */
STREAM *expand_irpc(
STACK *stack,
char *cp)
{
char *label,
*items;
BUFFER *gb;
int levelmod = 0;
IRPC_STREAM *str;
label = get_irp_sym(cp, &cp, NULL);
if (!label) {
report(stack->top, "Illegal .IRPC syntax\n");
return NULL;
}
cp = skipdelim(cp);
items = getstring(cp, &cp);
if (!items) {
report(stack->top, "Illegal .IRPC syntax\n");
free(label);
return NULL;
}
check_eol(stack, cp);
gb = new_buffer();
levelmod = 0;
if (!list_md) {
list_level--;
levelmod++;
}
read_body(stack, gb, NULL, FALSE);
list_level += levelmod;
str = memcheck(malloc(sizeof(IRPC_STREAM))); {
char *name = memcheck(malloc(strlen(stack->top->name) + 32));
sprintf(name, "%s:%d->.IRPC", stack->top->name, stack->top->line);
buffer_stream_construct(&str->bstr, NULL, name);
free(name);
}
str->bstr.stream.vtbl = &irpc_stream_vtbl;
str->body = gb;
str->items = items;
str->offset = 0;
str->label = label;
str->savecond = last_cond;
return &str->bstr.stream;
}