mmixallib.ch 11.1 KB
Newer Older
1
2
3
4
5
6
7
This file has been written by Felix Albrecht and was modified by Martin Ruckert.

We start by adding a preprocessor definition to disable warnings
about deprecated functions, like strcpy or sprintf, and 
include mmixlib.h at the start of the sybroutines 
to declare the extern library interface.

8
9
10
11
12
We prepare for the elimination
of the exit() function, which must be replaced by a return from
the subroutine.


13
14
15
16
17
18
19
@x
@<Preprocessor definitions@>=
@y
@<Sub...@>=
#include "mmixlib.h"

@ @<Global...@>=
20
21
#include <setjmp.h>
jmp_buf mmixal_exit;
22
23

@ @<Preprocessor definitions@>=
24
#ifdef WIN32
25
#pragma warning(disable : 4996)
26
#endif
27
28
29
30
31
32
33
#define _MMIXAL_
@z

We change error messages that handle errors in the command line 
(there is none). Errors are no longer written to stderr.

@x
34
35
36
    fprintf(stderr,
       "(say `-b <number>' to increase the length of my input buffer)\n");
@y
37
    MMXIAL_LINE_TRUNCATED
38
39
@z

40
The filenames in mmixal are considered local.
41
42
43
44

@x
Char *filename[257];
@y
45
static Char *filename[257];
46
47
@z

48
49
The error reporting goes into a separate file, because we might want
to change it.
50
51

@x
52
53
54
55
56
57
58
59
@d err(m) {@+report_error(m);@+if (m[0]!='*') goto bypass;@+}
@d derr(m,p) {@+sprintf(err_buf,m,p);
   report_error(err_buf);@+if (err_buf[0]!='*') goto bypass;@+}
@d dderr(m,p,q) {@+sprintf(err_buf,m,p,q);
   report_error(err_buf);@+if (err_buf[0]!='*') goto bypass;@+}
@d panic(m) {@+sprintf(err_buf,"!%s",m);@+report_error(err_buf);@+}
@d dpanic(m,p) {@+err_buf[0]='!';@+sprintf(err_buf+1,m,p);@+
                                          report_error(err_buf);@+}
60
@y
61
62
63
64
65
66
67
68
@d err(m) {@+report_error(m,filename[cur_file],line_no);@+if (m[0]!='*') goto bypass;@+}
@d derr(m,p) {@+sprintf(err_buf,m,p);
   report_error(err_buf,filename[cur_file],line_no);@+if (err_buf[0]!='*') goto bypass;@+}
@d dderr(m,p,q) {@+sprintf(err_buf,m,p,q);
   report_error(err_buf,filename[cur_file],line_no);@+if (err_buf[0]!='*') goto bypass;@+}
@d panic(m) {@+sprintf(err_buf,"!%s",m);@+report_error(err_buf,filename[cur_file],line_no);@+}
@d dpanic(m,p) {@+err_buf[0]='!';@+sprintf(err_buf+1,m,p);@+
                                          report_error(err_buf,filename[cur_file],line_no);@+}
69
70
71
@z


72
@x
73
74
75
76
77
78
@<Sub...@>=
void report_error @,@,@[ARGS((char*))@];@+@t}\6{@>
void report_error(message)
  char *message;
{
  if (!filename[cur_file]) filename[cur_file]="(nofile)";
79
80
81
82
83
84
85
86
87
88
89
  if (message[0]=='*')
    fprintf(stderr,"\"%s\", line %d warning: %s\n",
                 filename[cur_file],line_no,message+1);
  else if (message[0]=='!')
    fprintf(stderr,"\"%s\", line %d fatal error: %s\n",
                 filename[cur_file],line_no,message+1);
  else {
    fprintf(stderr,"\"%s\", line %d: %s!\n",
                 filename[cur_file],line_no,message);
    err_count++;
  }
90
91
92
93
94
95
96
97
  if (listing_file) {
    if (!line_listed) flush_listing_line("****************** ");
    if (message[0]=='*') fprintf(listing_file,
            "************ warning: %s\n",message+1);
    else if (message[0]=='!') fprintf(listing_file,
            "******** fatal error: %s!\n",message+1);
    else fprintf(listing_file,
            "********** error: %s!\n",message);
98
99
  }
  if (message[0]=='!') exit(-2);
100
}
101
@y
102
103
104
105
@(libalerror.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
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
extern int err_count;
extern FILE *listing_file;
extern void flush_listing_line(char*line);
typedef enum{false,true}bool;
extern bool line_listed;
extern jmp_buf mmixal_exit;

void report_error(char *message,char *filename,int line_no)
{
  if (!filename) filename="(nofile)";
  if (message[0]=='*')
    fprintf(stderr,"\"%s\", line %d warning: %s\n",
                 filename,line_no,message+1);
  else if (message[0]=='!')
    fprintf(stderr,"\"%s\", line %d fatal error: %s\n",
                 filename,line_no,message+1);
  else {
    fprintf(stderr,"\"%s\", line %d: %s!\n",
                 filename,line_no,message);
    err_count++;
  }
  if (listing_file) {
    if (!line_listed) flush_listing_line("****************** ");
    if (message[0]=='*') fprintf(listing_file,
            "************ warning: %s\n",message+1);
    else if (message[0]=='!') fprintf(listing_file,
            "******** fatal error: %s!\n",message+1);
    else fprintf(listing_file,
            "********** error: %s!\n",message);
  }
  if (message[0]=='!') longjmp(mmixal_exit,-2);
}
139
140
141
142
143
144
145
@z

In the assemble subroutine, we have the
opportunity to gain some information about
the association of lines and locations.

@x
146
  for (j=0;j<k;j++) {
147
@y
148
 MMIXAL_LINE_LOC(cur_file, line_no, cur_loc);
149
150
151
152
153
154
 for (j=0;j<k;j++) {
@z

We allocate tree nodes node by node so that we can free them again.

@x
155
156
157
158
159
160
161
162
  register trie_node *t=next_trie_node;
  if (t==last_trie_node) {
    t=(trie_node*)calloc(1000,sizeof(trie_node));
    if (!t) panic("Capacity exceeded: Out of trie memory");
@.Capacity exceeded...@>
    last_trie_node=t+1000;
  }
  next_trie_node=t+1;
163
164
  return t;
@y
165
166
  trie_node *t=(trie_node*)calloc(1,sizeof(trie_node));
  if (!t) panic("Capacity exceeded: Out of trie memory");
167
168
169
170
171
172
  return t;
@z

We need extra information in the symbol table.

@x
173
typedef struct sym_tab_struct {
174
@y
175
176
177
typedef struct sym_tab_struct {
  int file_no;
  int line_no;
178
179
180
181
182
@z

We allocat single sym nodes to be able to free them.

@x
183
184
185
186
187
188
189
190
191
192
193
194
    p=next_sym_node;
    if (p==last_sym_node) {
      p=(sym_node*)calloc(1000,sizeof(sym_node));
      if (!p) panic("Capacity exceeded: Out of symbol memory");
@.Capacity exceeded...@>
      last_sym_node=p+1000;
    }
    next_sym_node=p+1;
@y
    p=(sym_node*)calloc(1,sizeof(sym_node));
    if (!p) panic("Capacity exceeded: Out of symbol memory");
@z   
195
196
197
198
199
200
201
202
203
204
205
206

The special names are already defined in mmix-sim.w

@x
Char *special_name[32]={"rB","rD","rE","rH","rJ","rM","rR","rBB",
 "rC","rN","rO","rS","rI","rT","rTT","rK","rQ","rU","rV","rG","rL",
 "rA","rF","rP","rW","rX","rY","rZ","rWW","rXX","rYY","rZZ"};
@y
extern Char *special_name[32];
@z

Errors generated at the end of the assembly should use the
207
normal error reporting function.
208
209

@x
210
211
212
  fprintf(stderr,"undefined symbol: %s\n",sym_buf+1);
@.undefined symbol@>
  err_count++;
213
@y
214
  sprintf(err_buf,"undefined symbol: %s",sym_buf+1);
215
  report_error(err_buf,filename[cur_file],line_no);
216
217
218
219
220
@z

when a sym_node becomes DEFINED, we record file and line.

@x
221
222
223
  @<Find the symbol table node, |pp|@>;
@y
  @<Find the symbol table node, |pp|@>;
224
  pp->file_no=MMIX_FILE_NO(cur_file);
225
  pp->line_no=line_no;  
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
@z




Some global variables need to be static to
avoid name conflicts in the library.

@x
tetra z,y,x,yz,xyz; /* pieces for assembly */
@y
static tetra z,y,x,yz,xyz; /* pieces for assembly */
@z

The main() program becomes mmixal().

@x
243
244
245
246
247
248
249
250
251
252
253
254
@c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
@#
@<Preprocessor definitions@>@;
@<Type definitions@>@;
@<Global variables@>@;
@<Subroutines@>@;
@#
255
256
257
int main(argc,argv)
  int argc;@+
  char *argv[];
258
259
260
{
  register int j,k; /* all-purpose integers */
  @<Local variables@>;
261
  @<Process the command line@>;
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  @<Initialize everything@>;
  while(1) {
    @<Get the next line of input text, or |break| if the input has ended@>;
    while(1) {
      @<Process the next \MMIXAL\ instruction or comment@>;
      if (!*buf_ptr) break;
    }
    if (listing_file) {
      if (listing_bits) listing_clear();
      else if (!line_listed) flush_listing_line("                   ");
    }
  }
  @<Finish the assembly@>;
}
276
@y
277
278
279
280
281
282
@c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
283
#include <setjmp.h>
284
#include "libconfig.h"
285
286
287
@#
@<Preprocessor definitions@>@;
@<Type definitions@>@;
288
289
290
291
292
extern int expanding;
extern int buf_size;
extern char*src_file_name;
extern char obj_file_name[FILENAME_MAX+1];
extern char listing_name[FILENAME_MAX+1];
293
extern jmp_buf mmixal_exit;
294
295
296

extern void report_error(char * message, char *filename, int line_no);
extern int mmixal(char *mms_name, char *mmo_name, char *mml_name, int x_option, int b_option);
297
298
299
300
301
@#
int main(argc,argv)
  int argc;@+
  char *argv[];
{
302
303
  register int j; /* all-purpose integers */

304
  @<Process the command line@>;
305
  return mmixal(src_file_name, obj_file_name, listing_name, expanding, buf_size);
306
307
308
309
310
311
312
}

@ @(libmmixal.c@>=
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
313
#include <time.h> 
314
#include <setjmp.h>
315
#include "libconfig.h"
316
317
318
319
320
@#
@h
@<Preprocessor definitions@>@;
@<Type definitions@>@;
@<Global variables@>@;
321
322

extern void report_error(char * message, char *filename, int line_no);
323
extern jmp_buf mmixal_exit;
324

325
@<Subroutines@>@;
326

327
328
329
330
331
332
@#
int mmixal(char *mms_name, char *mmo_name, char *mml_name, int x_option, int b_option)
{
  register int j,k; /* all-purpose integers */
  @<Local variables@>;
  /* instead of processing the commandline */
333
  err_count=setjmp(mmixal_exit);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  if (err_count!=0){
   prune(trie_root);
   goto clean_up;
  }
  if (mms_name==NULL)
    panic("No input file name");
  src_file_name= mms_name;
  if (mmo_name==NULL)
    obj_file_name[0]=0;
  else
    strncpy(obj_file_name,mmo_name,FILENAME_MAX);
  if (mml_name==NULL)
    listing_name[0]=0;
  else
    strncpy(listing_name,mml_name,FILENAME_MAX);
  expanding = x_option;
  buf_size = b_option;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
    cur_file=0;
  line_no=0;
  long_warning_given=0;
  cur_loc.h=cur_loc.l=0;
  listing_loc.h=listing_loc.l=0;
  spec_mode= 0;
  spec_mode_loc= 0;
  mmo_ptr=0;
  err_count=0;
  serial_number=0;
  filename_count=0;
  for (j=0;j<10;j++)
  { forward_local[j].link=0;
    backward_local[j].link=0;
  }
  greg= 255;
367
  lreg= 32;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  @<Initialize everything@>;
  while(1) {
    @<Get the next line of input text, or |break| if the input has ended@>;
    while(1) {
      @<Process the next \MMIXAL\ instruction or comment@>;
      if (!*buf_ptr) break;
    }
    if (listing_file) {
      if (listing_bits) listing_clear();
      else if (!line_listed) flush_listing_line("                   ");
    }
  }
  @<Finish the assembly@>;
}
382
383
@z

384
385
386
387
388
389
390
We end with return or longjmp instead of exit.

@x
  exit(-1);
@y
  longjmp(mmixal_exit,-1);
@z
391
392

@x
393
394
395
396
397
398
399
if (err_count) {
  if (err_count>1) fprintf(stderr,"(%d errors were found.)\n",err_count);
  else fprintf(stderr,"(One error was found.)\n");
}
exit(err_count);
@y
  if (err_count>0){
400
401
402
    if (err_count>1) sprintf(err_buf,"(%d errors were found.)",err_count);
    else sprintf(err_buf,"(One error was found.)");
    report_error(err_buf,filename[cur_file],line_no);
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
  }
clean_up:
  if (listing_file!=NULL) 
  { fclose(listing_file);
    listing_file=NULL;
  }
  if (obj_file!=NULL) 
  { fclose(obj_file);
    obj_file=NULL;
  }
  if (src_file!=NULL) 
  { fclose(src_file);
    src_file=NULL;
  }
  cur_file=0;
  line_no=0;
  long_warning_given=0;
  cur_loc.h=cur_loc.l=0;
  listing_loc.h=listing_loc.l=0;
  spec_mode= 0;
  spec_mode_loc= 0;
  serial_number=0;
  free(buffer); buffer=NULL;
  free(lab_field); lab_field=NULL;
  free(op_field); op_field=NULL;
  free(operand_list); operand_list=NULL;
  free(err_buf);err_buf=NULL;
  free(op_stack); op_stack=NULL;
  free(val_stack);val_stack=NULL;
  filename[0]=NULL;
  filename_passed[0]=0;
  for (j=1;j<filename_count;j++)
  { free(filename[j]);
    filename[j]=NULL;
437
    filename_passed[j]=0;
438
439
440
  }
  filename_count=0;
  
441
442
443
return err_count;
@z

444

445
446
447
448
To report undefined local sysmbols we use the
usual error reporting.

@x
449
450
  err_count++,fprintf(stderr,"undefined local symbol %dF\n",j);
@y
451
452
  { sprintf(err_buf,"undefined local symbol %dF",j);
    report_error(err_buf,filename[cur_file],line_no);
453
454
  }
@z