-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbarebones.c
executable file
·346 lines (288 loc) · 7.01 KB
/
barebones.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
// Bare Bones interpreter
// $Id: barebones.c 14 2008-02-19 19:34:41Z eric $
// Copyright 2008 Eric Smith <[email protected]>
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation. Note that I am not
// granting permission to redistribute or modify this program under the
// terms of any other version of the General Public License.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program (in the file "COPYING"); if not, see
// <http://www.gnu.org/licenses/>.
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "barebones.h"
#include "parser.tab.h"
struct var_t
{
struct var_t *next;
char *name;
bool init;
uintmax_t val;
};
int stmt_line; // line number of currently executing statement, for
// error reporting purposes
bool init_to_zero = true; // if false, reference to uninitialized variables
// (other than in a clear statement) will result
// in a run time error
void usage (FILE *f)
{
fprintf (f, "%s [options] [initializers...] src-file:\n", progname);
fprintf (f, "options:\n");
fprintf (f, " -u report uninitialized variables\n");
fprintf (f, " -O optimize\n");
fprintf (f, "initializers:\n");
fprintf (f, " var=value e.g. X=37\n");
}
void error (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, "error on line %d: ", stmt_line);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, "\n");
exit (2);
}
// linked list of variables
// unsorted because it is assumed that there will only be a small number
// of variables, so the cost of looking them up by a linear search of a
// list at "compile time" will be negligible.
var_t *var_head = NULL;
var_t *find_var (char *name)
{
var_t *var;
// linear search for named variable
for (var = var_head; var; var = var->next)
{
if (strcasecmp (name, var->name) == 0)
return var;
}
// if not found, create
var = alloc (sizeof (var_t));
var->name = strdup (name);
if (init_to_zero)
{
var->val = 0;
var->init = true;
}
else
{
var->init = false;
}
var->next = var_head;
var_head = var;
return var;
}
void set_var (var_t *var, uintmax_t val)
{
var->val = val;
var->init = true;
}
static void define_var (char *s)
{
var_t *var;
char *p = strchr (s, '=');
char *name = newstrn (s, p - s);
intmax_t val;
p++;
val = a_to_intmax (p);
if (val < 0)
fatal (2, "negative values are not permitted");
var = find_var (name);
set_var (var, val);
}
void print_vars (bool show_uninitialized)
{
var_t *var;
for (var = var_head; var; var = var->next)
{
if ((! var->init) && (! show_uninitialized))
continue;
printf ("%s: ", var->name);
if (var->init)
printf ("%" PRIuMAX, var->val);
else
printf ("uninitialized");
printf ("\n");
}
}
void check_var_init (var_t *var)
{
if (! var->init)
error ("unitialized variable %s", var->name);
}
void execute_stmt (stmt_t *stmt)
{
stmt_line = stmt->line;
switch (stmt->type)
{
case CLEAR_STMT:
stmt->var->val = 0;
stmt->var->init = true;
break;
case INCR_STMT:
check_var_init (stmt->var);
if (stmt->var->val == UINTMAX_MAX)
fatal (2, "overflow");
stmt->var->val++;
break;
case DECR_STMT:
check_var_init (stmt->var);
if (stmt->var->val)
stmt->var->val--;
break;
case WHILE_STMT:
check_var_init (stmt->var);
while (stmt->var->val)
execute_stmt_list (stmt->stmt_list);
break;
case COPY_STMT:
check_var_init (stmt->var);
stmt->dest->val = stmt->var->val;
stmt->dest->init = true;
break;
case ADD_CLEAR_STMT:
check_var_init (stmt->var);
check_var_init (stmt->dest);
stmt->dest->val += stmt->var->val;
stmt->var->val = 0;
break;
}
}
void execute_stmt_list (stmt_t *list)
{
while (list)
{
execute_stmt (list);
list = list->next;
}
}
stmt_t *new_stmt (stmt_type_t type, var_t *var)
{
stmt_t *stmt = alloc (sizeof (stmt_t));
stmt->type = type;
stmt->var = var;
return stmt;
}
void add_stmt_to_list (stmt_t *head, stmt_t *stmt)
{
if (head->tail)
head->tail->next = stmt;
else
head->next = stmt;
head->tail = stmt;
}
extern FILE *yyin;
extern int yydebug;
int yyparse (void);
bool parse_prog (char *fn)
{
int status;
yyin = fopen (fn, "r");
if (! yyin)
fatal (1, "can't read program\n");
#if 0
yydebug = 1;
#endif
status = yyparse ();
fclose (yyin);
return status == 0;
}
void optimize_stmt_list (stmt_t *list);
void optimize_while_stmt (stmt_t *stmt)
{
bool match = false;
var_t *loop_var = stmt->var;
stmt_t *first_stmt = stmt->stmt_list;
stmt_t *second_stmt = first_stmt->next;
var_t *src;
var_t *dest;
if (first_stmt && second_stmt && ! second_stmt->next)
{
if ((first_stmt->type == INCR_STMT) &&
(first_stmt->var != loop_var) &&
(second_stmt->type == DECR_STMT) &&
(second_stmt->var == loop_var))
{
match = true;
src = second_stmt->var;
dest = first_stmt->var;
}
else if ((second_stmt->type == INCR_STMT) &&
(second_stmt->var != loop_var) &&
(first_stmt->type == DECR_STMT) &&
(first_stmt->var == loop_var))
{
match = true;
src = first_stmt->var;
dest = second_stmt->var;
}
}
if (match)
{
stmt->type = ADD_CLEAR_STMT;
stmt->var = src;
stmt->dest = dest;
}
else
optimize_stmt_list (stmt->stmt_list);
}
void optimize_stmt_list (stmt_t *list)
{
while (list)
{
if (list->type == WHILE_STMT)
optimize_while_stmt (list);
list = list->next;
}
}
stmt_t *main_prog = NULL;
int main (int argc, char *argv [])
{
bool opt_flag = false;
progname = argv [0];
while (--argc)
{
argv++;
if (argv [0][0] == '-')
{
if (strcmp (argv [0], "-u") == 0)
init_to_zero = false;
else if (strcmp (argv [0], "-O") == 0)
opt_flag = true;
else
fatal (1, "unrecognized option '%s'", argv [0]);
}
else if (strchr (argv [0], '='))
define_var (argv [0]);
else if (main_prog)
fatal (1, "only one program may be specified");
else if (! parse_prog (argv [0]))
fatal (2, "parse failed");
else
{
; // everything OK!
}
}
if (! main_prog)
fatal (1, "no program found");
if (opt_flag)
optimize_stmt_list (main_prog);
printf ("initial values of variables:\n");
print_vars (false);
execute_stmt_list (main_prog);
printf ("final values of variables:\n");
print_vars (true);
exit (0);
}