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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
/* -*-c-*- -------------- mix_parser.c :
* Implementation of the functions declared in mix_parser.h and
* xmix_parser.h
* ------------------------------------------------------------------
* Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "mix.h"
#include "mix_code_file.h"
#include "xmix_parser.h"
/* The flex-generated scanner, according to file mix_parser.l */
extern mix_parser_err_t
mix_flex_scan (mix_parser_t *parser);
/*------------ mixparser.h functions -------------------------------------*/
/* error messages */
static const gchar * const ERR_MESSAGE_[] = {
N_("successful compilation"),
N_("file not yet compiled"),
N_("internal error"),
N_("unable to open MIX source file"),
N_("unable to open MIX output file"),
N_("unexpected end of file"),
N_("invalid location field"),
N_("duplicated symbol"),
N_("symbol too long"),
N_("missing operator field"),
N_("unexpected location symbol"),
N_("invalid address field"),
N_("invalid index field"),
N_("invalid f-specification"),
N_("invalid operation field"),
N_("invalid expression"),
N_("undefined symbol"),
N_("mismatched parenthesis"),
N_("unexpected f-specfication"),
N_("missing symbol name"),
N_("symbol is an instruction name"),
N_("failed write access to code file"),
N_("operand of ALF pseudo instruction has less than 5 chars"),
N_("operand of ALF pseudo instruction has more than 5 chars")
};
static const guint NO_OF_MESSAGES_ = sizeof(ERR_MESSAGE_)/sizeof (gchar*);
const gchar *
mix_parser_err_string (mix_parser_err_t error)
{
return (error < NO_OF_MESSAGES_) ? _(ERR_MESSAGE_[error]) : NULL;
}
guint
mix_parser_err_count (const mix_parser_t *parser)
{
return (parser) ? parser->err_count : 0;
}
guint
mix_parser_warning_count (const mix_parser_t *parser)
{
return (parser) ? parser->warn_count : 0;
}
const gchar *
mix_parser_src_file_base_name (const mix_parser_t *parser)
{
return (parser) ? mix_file_base_name (parser->in_file) : NULL;
}
const gchar *
mix_parser_src_file_extension (const mix_parser_t *parser)
{
return (parser) ? mix_file_extension (parser->in_file) : NULL;
}
/* Create/destroy a mix_parser */
/* compare function for the table of ins */
static gint
compare_shorts_ (gconstpointer s1, gconstpointer s2)
{
mix_short_t a = (mix_short_t)GPOINTER_TO_UINT(s1);
mix_short_t b = (mix_short_t)GPOINTER_TO_UINT (s2);
if ( mix_short_sign (a) == mix_short_sign (b) )
return mix_short_magnitude (a) - mix_short_magnitude (b);
else if ( mix_short_magnitude (a) == 0 && mix_short_magnitude (b) == 0 )
return 0;
else if ( mix_short_is_positive (a) )
return 1;
return -1;
}
mix_parser_t *
mix_parser_new (const gchar *in_file)
{
mix_parser_t *result;
mix_file_t *f = mix_file_new_with_def_ext (in_file, mix_io_READ,
MIX_SRC_DEFEXT);
if ( f == NULL ) return NULL;
result = g_new (mix_parser_t, 1);
if ( result == NULL )
{
g_warning (_("No system resources"));
mix_file_delete (f);
return NULL;
}
result->symbol_table = mix_symbol_table_new ();
result->ls_table = mix_symbol_table_new ();
result->cur_ls = 0;
result->future_refs = g_hash_table_new (g_str_hash, g_str_equal);
result->ins_table = g_tree_new (compare_shorts_);
if ( result->symbol_table == NULL || result->future_refs == NULL
|| result->ins_table == NULL || result->ls_table == NULL )
{
mix_symbol_table_delete (result->symbol_table);
mix_symbol_table_delete (result->ls_table);
g_hash_table_destroy (result->future_refs);
g_tree_destroy (result->ins_table);
mix_file_delete (f);
g_free (result);
g_warning (_("No system resources"));
return NULL;
}
result->in_file = f;
result->loc_count = MIX_SHORT_ZERO;
result->status = MIX_PERR_NOCOMP;
result->err_line = 0;
result->err_count = 0;
result->warn_count = 0;
return result;
}
static void
delete_list_vals_ (gpointer key, gpointer value, gpointer data)
{
g_free (key);
g_slist_free ((GSList*)value);
}
static int
delete_tree_vals_ (gpointer key, gpointer value, gpointer data)
{
g_free (value);
return FALSE;
}
void
mix_parser_delete (mix_parser_t *parser)
{
g_return_if_fail (parser != NULL);
/* clear the GSList values of future_refs and its keys */
g_hash_table_foreach (parser->future_refs, delete_list_vals_, NULL);
/* clear the ins_node_'s of the ins tree */
g_tree_traverse (parser->ins_table, delete_tree_vals_, G_IN_ORDER, NULL);
/* destroy the tree and hash tables */
g_tree_destroy (parser->ins_table);
mix_symbol_table_delete (parser->symbol_table);
mix_symbol_table_delete (parser->ls_table);
g_hash_table_destroy (parser->future_refs);
mix_file_delete (parser->in_file);
g_free (parser);
}
/* Compile a mix source file */
static gboolean
undef_warning_ (gpointer symbol, gpointer value, gpointer data)
{
mix_parser_log_error ((mix_parser_t *)data, MIX_PERR_UNDEF_SYM, 1,
(const gchar *)symbol, TRUE);
/* move the symbol to the symbol table */
mix_symbol_table_insert_static (((mix_parser_t *)data)->symbol_table,
symbol, MIX_WORD_ZERO);
return TRUE;
}
static void
update_future_refs_value_ (mix_parser_t *parser, const gchar *name,
mix_short_t value)
{
GSList *list = NULL;
gpointer key;
g_assert (parser != NULL && name != NULL);
if ( g_hash_table_lookup_extended (parser->future_refs, name, &key,
(gpointer *)&list) )
{
GSList *tmp = list;
ins_node_ *node;
while ( tmp != NULL )
{
node =
(ins_node_ *)g_tree_lookup (parser->ins_table,tmp->data);
g_assert (node);
if (mix_get_ins_address (node->ins) == 1) {
value = mix_short_negative (value);
node->ins = mix_word_set_field (node->ins,
MIX_WORD_ZERO,
mix_fspec_new (1,2));
}
mix_word_add_address (node->ins, value);
g_tree_insert (parser->ins_table, tmp->data, (gpointer)node);
tmp = g_slist_next (tmp);
}
g_hash_table_remove (parser->future_refs, name);
g_free (key);
g_slist_free (list);
}
}
#define update_future_refs_(parser,name) \
update_future_refs_value_(parser, name, (parser)->loc_count);
static void
update_ls_ (gpointer symbol, gpointer value, gpointer parser)
{ /* add an instruction on current location and update refs to it */
mix_ins_t ins;
mix_word_t w = (mix_word_t) GPOINTER_TO_UINT (value);
mix_parser_t *par = (mix_parser_t *) parser;
mix_word_to_ins_uncheck (w, ins);
mix_parser_add_ins (par, &ins, 0);
update_future_refs_ (par, (const gchar *)symbol);
par->loc_count++;
}
mix_parser_err_t
mix_parser_compile (mix_parser_t *parser)
{
g_return_val_if_fail (parser != NULL, MIX_PERR_INTERNAL);
g_return_val_if_fail (parser->in_file != NULL, MIX_PERR_NOIN);
g_return_val_if_fail (parser->symbol_table != NULL, MIX_PERR_INTERNAL);
g_return_val_if_fail (parser->future_refs != NULL, MIX_PERR_INTERNAL);
g_return_val_if_fail (parser->ins_table != NULL, MIX_PERR_INTERNAL);
parser->status = mix_flex_scan (parser);
if ( parser->status == MIX_PERR_OK )
{
mix_symbol_table_foreach (parser->ls_table, update_ls_,
(gpointer)parser);
g_hash_table_foreach_remove (parser->future_refs,
undef_warning_, (gpointer)parser);
}
return parser->status;
}
/* Write a compiled source to a code file */
struct write_code_context_
{
mix_code_file_t *file;
mix_parser_t *parser;
};
static gint
write_code_ (gpointer address, gpointer ins_node, gpointer context)
{
mix_ins_desc_t desc;
struct write_code_context_ *cntx = (struct write_code_context_ *)context;
desc.ins = ((ins_node_ *)ins_node)->ins;
desc.lineno = ((ins_node_ *)ins_node)->lineno;
desc.address = (mix_address_t)GPOINTER_TO_UINT (address);
if ( mix_code_file_write_ins (cntx->file, &desc) )
return FALSE;
else
{
cntx->parser->status = MIX_PERR_NOWRITE;
return TRUE;
}
}
mix_parser_err_t
mix_parser_write_code (mix_parser_t *parser, const gchar *code_file,
gboolean debug)
{
struct write_code_context_ context;
const gchar *cfname = (code_file) ?
code_file : mix_file_base_name (parser->in_file);
gchar *dir;
gchar *source_path;
g_return_val_if_fail (parser != NULL, MIX_PERR_INTERNAL);
if (parser->status != MIX_PERR_OK ) return parser->status;
context.parser = parser;
dir = g_get_current_dir ();
source_path = g_strconcat (dir, G_DIR_SEPARATOR_S,
mix_file_base_name (parser->in_file), NULL);
context.file = mix_code_file_new_write (cfname, parser->start, source_path,
debug, parser->symbol_table);
g_free (dir);
g_free (source_path);
if (context.file == NULL) return MIX_PERR_NOOUT;
g_tree_traverse (parser->ins_table, write_code_,
G_IN_ORDER, (gpointer)&context);
mix_code_file_delete (context.file);
return parser->status;
}
/* Produce a listing file summarising the compilation */
static gint
write_listing_ (gpointer address, gpointer ins, gpointer context)
{
guint k;
FILE *file = (FILE *)context;
ins_node_ *ins_node = (ins_node_ *)ins;
mix_ins_t instruct;
gchar *instext = NULL;
mix_ins_id_t id = mix_word_to_ins (ins_node->ins, &instruct);
if (id != mix_INVALID_INS)
instext = mix_ins_to_string (&instruct);
fprintf (file, "%d\t%s ", GPOINTER_TO_INT (address),
mix_word_is_negative (ins_node->ins)? "-":"+");
for ( k = 1; k < 6; ++k )
fprintf (file, "%02d ", mix_word_get_byte (ins_node->ins, k));
fprintf (file, _("\t%s\t(line %d)\n"), instext? instext:"--DATA--",
ins_node->lineno);
if (instext) g_free (instext);
return FALSE;
}
mix_parser_err_t
mix_parser_write_listing (mix_parser_t *parser, const gchar *list_file)
{
FILE *file;
mix_file_t *mfile;
const gchar *name;
g_return_val_if_fail (parser != NULL, MIX_PERR_INTERNAL);
if (parser->status != MIX_PERR_OK ) return parser->status;
name = (list_file) ? list_file : mix_file_base_name (parser->in_file);
mfile = mix_file_new_with_def_ext (name, mix_io_WRITE, MIX_LIST_DEFEXT);
if ( mfile == NULL ) return MIX_PERR_NOOUT;
file = mix_file_to_FILE (mfile);
fprintf (file, _("*** %s%s: compilation summary ***\n\n"),
mix_file_base_name (parser->in_file),
mix_file_extension (parser->in_file));
fputs ( _("*** Address / Compiled word / Symbolic rep / Source file line\n"),
file);
g_tree_traverse (parser->ins_table, write_listing_,
G_IN_ORDER, (gpointer)file);
fprintf (file, _("\n*** Start address: %d\n"),
mix_short_magnitude (parser->start));
fprintf (file, _("\n*** Symbol table\n"));
mix_symbol_table_print (parser->symbol_table, MIX_SYM_ROWS, file);
fprintf (file, _("\n*** End of summary ***\n"));
mix_file_delete (mfile);
return parser->status;
}
/* load a virtual machine's memory with the contents of a compiled file */
static gint
load_vm_ (gpointer address, gpointer ins, gpointer vm)
{
mix_vm_set_addr_contents ((mix_vm_t*)vm,
(mix_address_t)GPOINTER_TO_UINT (address),
((ins_node_ *)ins)->ins);
return FALSE;
}
mix_parser_err_t
mix_parser_load_vm (const mix_parser_t *parser, mix_vm_t *vm)
{
g_return_val_if_fail (parser != NULL, MIX_PERR_INTERNAL);
g_return_val_if_fail (vm != NULL, MIX_PERR_INTERNAL);
g_return_val_if_fail (parser->status == MIX_PERR_OK, parser->status);
mix_vm_reset (vm);
g_tree_traverse (parser->ins_table, load_vm_, G_IN_ORDER, (gpointer)vm);
mix_vm_set_start_addr (vm, parser->start);
return parser->status;
}
/*------------ xmiparser.h functions -------------------------------------*/
/* functions to manipulate mix_parser_t during compilation */
/* symbol table */
/* Define a new symbol with value equal to the current loc_count
* and update future refs to this symbol
*/
mix_parser_err_t
mix_parser_define_symbol_here (mix_parser_t *parser, const gchar *name)
{
mix_word_t value = mix_short_to_word_fast (parser->loc_count);
return mix_parser_define_symbol_value (parser, name, value);
}
mix_parser_err_t
mix_parser_define_symbol_value (mix_parser_t *parser, const gchar *name,
mix_word_t value)
{
g_assert (parser != NULL && name != NULL);
switch (mix_symbol_table_add (parser->symbol_table, name, value))
{
case MIX_SYM_OK:
if ( parser->status == MIX_PERR_NOCOMP )
update_future_refs_value_ (parser, name, value);
return MIX_PERR_OK;
case MIX_SYM_LONG: return MIX_PERR_LONG_SYMBOL;
case MIX_SYM_DUP: return MIX_PERR_DUP_SYMBOL;
default: return MIX_PERR_INTERNAL;
}
}
/* Obtain the value of a symbol */
void
mix_parser_set_future_ref (mix_parser_t *parser, const gchar *name)
{
const gchar *nname = name;
GSList *list;
g_assert (parser != NULL && name != NULL);
if ( parser->status == MIX_PERR_NOCOMP )
{
list = g_hash_table_lookup (parser->future_refs, name);
if ( list == NULL ) nname = g_strdup (name);
list = g_slist_prepend (list, (gpointer)((guint)parser->loc_count));
g_hash_table_insert (parser->future_refs, (gpointer)nname, list);
}
}
/* Redefine the value of a local symbol as the current loc_count */
void
mix_parser_manage_local_symbol (mix_parser_t *parser, const gchar *name,
mix_short_t value)
{
gchar ref[3];
ref[2] = 0;
g_assert (parser != NULL && name != NULL);
g_assert (strlen(name) == 2);
switch (name[1])
{
case 'f': case 'F':
mix_parser_set_future_ref (parser, name);
break;
case 'h': case 'H':
ref[0] = name[0];
ref[1] = 'F';
if ( parser->status == MIX_PERR_NOCOMP )
update_future_refs_value_ (parser, ref, value);
ref[1] = 'B';
mix_symbol_table_insert (parser->symbol_table, ref,
mix_short_to_word_fast (value));
break;
default:
return;
}
}
/* Literal strings symbols */
void
mix_parser_define_ls (mix_parser_t *parser, mix_word_t value)
{
gchar *name = g_strdup_printf ("%05d", parser->cur_ls++);
mix_symbol_table_add (parser->ls_table, name, value);
mix_parser_set_future_ref (parser, name);
g_free (name);
}
/* Compilation */
void
mix_parser_add_ins (mix_parser_t *parser, const mix_ins_t *new_ins,
guint lineno)
{
g_assert (parser != NULL && new_ins != NULL);
mix_parser_add_raw (parser, mix_ins_to_word_uncheck (*new_ins), lineno);
}
void
mix_parser_add_raw (mix_parser_t *parser, mix_word_t word, guint lineno)
{
g_assert (parser != NULL);
if ( parser->status == MIX_PERR_NOCOMP || parser->status == MIX_PERR_OK )
{
ins_node_ *node = g_new (ins_node_, 1);
if ( node == NULL ) {
g_warning (_("Unable to allocate memory"));
return;
}
node->ins = word;
node->lineno = lineno;
g_tree_insert (parser->ins_table, (gpointer)((guint)parser->loc_count),
(gpointer)node);
}
}
/* Error handling */
void
mix_parser_log_error (mix_parser_t *parser, mix_parser_err_t error,
gint lineno, const gchar *comment, gboolean warn)
{
g_assert (parser != NULL);
if ( warn )
parser->warn_count += 1;
else
{
parser->err_count += 1;
parser->err_line = lineno;
parser->status = error;
}
fprintf (stderr, "%s%s:%d: %s: %s",
mix_file_base_name (parser->in_file),
mix_file_extension (parser->in_file),
lineno, warn ? _("warning"):_("error"), _(ERR_MESSAGE_[error]));
if (comment != NULL)
fprintf (stderr, ": %s\n", comment);
else
fputs ("\n", stderr);
}
|