-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmmixlib.ch
More file actions
2820 lines (2491 loc) · 65 KB
/
mmixlib.ch
File metadata and controls
2820 lines (2491 loc) · 65 KB
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% This is the change file that extracts from mmix-sim.w
% the various .c files that make up the mmix library
Types and preprocessor macros go into libtype.h
@x
@* Basics. To get started, we define a type that provides semantic sugar.
@y
@ @(libtype.h@>=
@h
#define _LIBTYPE_H_
@<Preprocessor macros@>@;
@<Type declarations@>@;
@* Basics. To get started, we define a type that provides semantic sugar.
@z
@x
@ @<Sub...@>=
void print_hex @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@ @(libprint.c@>=
#include <stdio.h>
#include <setjmp.h>
#include "libconfig.h"
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
#include "libarith.h"
#include "libimport.h"
void print_hex @,@,@[ARGS((octa))@];@+@t}\6{@>
@z
The external definitions of mmix-arith
should also go into a header file.
@x
@<Sub...@>=
extern octa zero_octa; /* |zero_octa.h=zero_octa.l=0| */
@y
@(libarith.h@>=
extern octa zero_octa; /* |zero_octa.h=zero_octa.l=0| */
@z
Fatal errors need to be handled but error messages moves to libconfig.h
@x
@d panic(m) {@+fprintf(stderr,"Panic: %s!\n",m);@+exit(-2);@+}
@y
@d panic(m) {@+MMIX_ERROR("Panic: %s!\n",m);@+longjmp(mmix_exit,-2);@+}
@z
@x
@<Sub...@>=
void print_int @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@(libprint.c@>=
void print_int @,@,@[ARGS((octa))@];@+@t}\6{@>
@z
The tet field of the mem_tetra may be eliminated.
@x
tetra tet; /* the tetrabyte of simulated memory */
@y
#ifdef MMIX_MEM_TET
tetra tet; /* the tetrabyte of simulated memory */
#endif
@z
@x
@<Sub...@>=
mem_node* new_mem @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libmem.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include "libconfig.h"
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
#include "libimport.h"
mem_node* new_mem @,@,@[ARGS((void))@];@+@t}\6{@>
@z
We distinguish between different levels of initialization:
persistent data (initialized once when we load the library),
then data that needs to be initialized each time we start the
simulator,
and finally data that is initialized each time we boot the machine.
@x
@<Initialize...@>=
mem_root=new_mem();
mem_root->loc.h=0x40000000;
last_mem=mem_root;
@y
@<Set up persistent data@>=
mem_root=new_mem();
mem_root->loc.h=0x40000000;
last_mem=mem_root;
@z
Handling the simulation of memory goes into its own file.
@x
@<Sub...@>=
mem_tetra* mem_find @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@(libmem.c@>=
mem_tetra* mem_find @,@,@[ARGS((octa))@];@+@t}\6{@>
@z
Defining the macro mm conflicts with other macros when using MS Visual C
@x
@d mm 0x98 /* the escape code of \.{mmo} format */
@y
@d mmo_esc 0x98 /* the escape code of \.{mmo} format */
@z
Loading the object file will go into its own file.
@x
@<Initialize everything@>=
mmo_file=fopen(mmo_file_name,"rb");
@y
@<Load object file@>=
mmo_file=fopen(mmo_file_name,"rb");
@z
How to print errors is defined in libconfig.h and
exit needs to be replaced by a longjmp.
@x
fprintf(stderr,"Can't open the object file %s or %s!\n",
@.Can't open...@>
mmo_file_name,alt_name);
exit(-3);
@y
MMIX_ERROR("Can't open the object file %s!\n",mmo_file_name);
longjmp(mmix_exit,-3);
@z
these global varaibles go into libload
@x
@ @<Glob...@>=
FILE *mmo_file; /* the input file */
int postamble; /* have we encountered |lop_post|? */
int byte_count; /* index of the next-to-be-read byte */
byte buf[4]; /* the most recently read bytes */
int yzbytes; /* the two least significant bytes */
int delta; /* difference for relative fixup */
tetra tet; /* |buf| bytes packed big-endianwise */
@y
@ @<Load globals@>=
static FILE *mmo_file; /* the input file */
static int postamble; /* have we encountered |lop_post|? */
static int byte_count; /* index of the next-to-be-read byte */
static byte buf[4]; /* the most recently read bytes */
static int yzbytes; /* the two least significant bytes */
static int delta; /* difference for relative fixup */
static tetra tet; /* |buf| bytes packed big-endianwise */@z
@z
MMIX_ERROR again.
@x
fprintf(stderr,"Bad object file! (Try running MMOtype.)\n");
@.Bad object file@>
exit(-4);
@y
MMIX_ERROR("%s","Bad object file! (Try running MMOtype.)\n");
longjmp(mmix_exit,-4);
@z
The next function goes into libload.
@x
@<Sub...@>=
void read_tet @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libload.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "libconfig.h"
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
#include "libarith.h"
#include "libname.h"
#include "libimport.h"
@<Load globals@>
@<Loading subroutines@>@;
@ @<Loading subroutines@>=
void read_tet @,@,@[ARGS((void))@];@+@t}\6{@>
@z
Also to libload.
@x
@ @<Sub...@>=
byte read_byte @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@ @<Loading subroutines@>=
byte read_byte @,@,@[ARGS((void))@];@+@t}\6{@>
@z
Replace mm by mmo_esc.
@x
if (buf[0]!=mm || buf[1]!=lop_pre) mmo_err;
@y
if (buf[0]!=mmo_esc || buf[1]!=lop_pre) mmo_err;
@z
And again.
@x
loop:@+if (buf[0]==mm) switch (buf[1]) {
@y
loop:@+if (buf[0]==mmo_esc) switch (buf[1]) {
@z
The macro mmo_load becomes a function,
which uses MMIX_LDT and MMIX_STT as defined in libconfig.h.
line 940
@x
@d mmo_load(loc,val) ll=mem_find(loc), ll->tet^=val
@y
@d mmo_load(loc,val) ll=load_mem_tetra(loc,val)
@(libload.c@>=
static mem_tetra *load_mem_tetra(octa loc, tetra val)
{ octa x;
mem_tetra *ll=mem_find(loc);
MMIX_LDT(x,loc);
x.l = x.l^val;
if (!MMIX_STT(x,loc))
panic("Unable to store mmo file to RAM");
return ll;
}
@ This function is used next.
@z
Before we increment the line number,
we reset the frequency and possibly record file, line, and loc.
@x
ll->file_no=cur_file;
ll->line_no=cur_line;
cur_line++;
@y
#ifdef MMIX_LOAD_LINE_LOC
MMIX_LOAD_LINE_LOC(cur_file,cur_line,cur_loc);
#else
ll->file_no=cur_file;
ll->line_no=cur_line;
#endif
ll->freq=0;
cur_line++;
@z
We add a variable for the current frequency.
@x
int cur_line; /* the current position in |cur_file|, if nonzero */
@y
int cur_line; /* the current position in |cur_file|, if nonzero */
tetra cur_freq; /* the current frequency, if nonzero */
@z
The next lines of code complete loading the object file.
@x
@ @<Initialize...@>=
cur_loc.h=cur_loc.l=0;
@y
@ @<Load object file@>=
cur_loc.h=cur_loc.l=0;
@z
@x
cur_line=0;
@y
cur_freq=cur_line=0;
@z
mmo files use local file numbers.
As we might load multiple files,
we have to map the file number stored in
the mmo file as the ybyte to the
filenumbers used inside the library.
Default conversion functions are defined
in libname.c.
First the case of known files.
@x
case lop_file:@+if (file_info[ybyte].name) {
if (zbyte) mmo_err;
cur_file=ybyte;
@y
case lop_file:
if (ybyte2file[ybyte]>=0) {
if (zbyte) mmo_err;
cur_file=ybyte2file[ybyte];
@z
Now we handle new files.
@x
}@+else {
if (!zbyte) mmo_err;
file_info[ybyte].name=(char*)calloc(4*zbyte+1,1);
if (!file_info[ybyte].name) {
fprintf(stderr,"No room to store the file name!\n");@+exit(-5);
@.No room...@>
}
cur_file=ybyte;
for (j=zbyte,p=file_info[ybyte].name; j>0; j--,p+=4) {
read_tet();
*p=buf[0];@+*(p+1)=buf[1];@+*(p+2)=buf[2];@+*(p+3)=buf[3];
}
}
@y
}@+else {
if (!zbyte) mmo_err;
if (4*zbyte+1>FILENAME_MAX) mmo_err;
for (j=zbyte,p=filename; j>0; j--,p+=4) {
read_tet();
*p=buf[0];@+*(p+1)=buf[1];@+*(p+2)=buf[2];@+*(p+3)=buf[3];
}
cur_file=filename2file(filename);
ybyte2file[ybyte]=cur_file;
}
@z
mm was replaced by mmo_esc.
@x
if (buf[0]==mm) {
@y
if (buf[0]==mmo_esc) {
@z
We load the postamble into the beginning
of segment~3, also known as \.{Stack\_Segment}).
The stack segment is set up to be used with an unsave instruction.
On the stack, we have, the local registers (argc and argv) and the
value of rL, then the global
registers and the special registers rB, rD, rE, rH, rJ, rM, rR, rP, rW, rX, rY, and rZ,
followed by rG and rA packed into eight byte.
@x
@<Load the postamble@>=
aux.h=0x60000000;@+ aux.l=0x18;
ll=mem_find(aux);
(ll-1)->tet=2; /* this will ultimately set |rL=2| */
(ll-5)->tet=argc; /* and $\$0=|argc|$ */
(ll-4)->tet=0x40000000;
(ll-3)->tet=0x8; /* and $\$1=\.{Pool\_Segment}+8$ */
G=zbyte;@+ L=0;@+ O=0;
for (j=G+G;j<256+256;j++,ll++,aux.l+=4) read_tet(), ll->tet=tet;
inst_ptr.h=(ll-2)->tet, inst_ptr.l=(ll-1)->tet; /* \.{Main} */
(ll+2*12)->tet=G<<24;
g[255]=incr(aux,12*8); /* we will \.{UNSAVE} from here, to get going */
@y
@<Load the postamble@>=
{ octa x;
aux.h=0x60000000;
x.h=0;@+x.l=0;@+aux.l=0x00;
if (!MMIX_STO(x,aux)) /* $\$0=|argc|$ */
panic("Unable to store mmo file to RAM");
x.h=0x40000000;@+x.l=0x8;@+aux.l=0x08;
if (!MMIX_STO(x,aux)) /* and $\$1=\.{Pool\_Segment}+8$ */
panic("Unable to store mmo file to RAM");
x.h=0;@+x.l=2;@+aux.l=0x10;
if (!MMIX_STO(x,aux)) /* this will ultimately set |rL=2| */
panic("Unable to store mmo file to RAM");
G=zbyte;@+ L=0;@+ O=0;
aux.l=0x18;
for (j=G;j<256;j++,aux.l+=8)
{ read_tet(); x.h=tet;
read_tet(), x.l=tet;
if (!MMIX_STO(x,aux))
panic("Unable to store mmo file to RAM");
}
aux=incr(aux,12*8); /* we can |UNSAVE| from here, to get going */
#ifdef MMIX_BOOT
loc.h=0x80000000; loc.l=0;
g[rWW] = x; /* last octa stored is address of \.{Main} */
g[rBB] = aux;
g[rXX].h = 0; g[rXX].l = ((tetra)UNSAVE<<24)+255; /* \.{UNSAVE} \$255 */
rzz = 1;
#else
loc.h=0x80000000; loc.l=0;
inst_ptr = x; /* last octa stored is address of \.{Main} */
g[255] = aux;
g[rXX].h = 0; g[rXX].l = ((tetra)UNSAVE<<24)+255; /* \.{UNSAVE} \$255 */
rzz = 1;
/* RESUME 0 will not work for x = 0
inst_ptr = x;
g[255] = aux;
rzz = 0; pretend \.{RESUME} 0 */
#endif
x.h=G<<24; x.l=0 /* rA */;
if (!MMIX_STO(x,aux))
panic("Unable to store mmo file to RAM");
G=g[rG].l; /* restore G to rG because it was changed above */
}
@z
The source line buffer is allocated once.
@x
@<Initialize...@>=
if (buf_size<72) buf_size=72;
@y
@<Set up persistent data@>=
if (buf_size<=0) buf_size=256;
@z
The display of source lines gets its own file.
@x
@<Sub...@>=
void make_map @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libshowline.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <setjmp.h>
#include "libconfig.h"
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
#include "libimport.h"
void make_map @,@,@[ARGS((void))@];@+@t}\6{@>
@z
these include files are needed only in libshowline.c
@x
@<Preprocessor macros@>=
@y
@<Showline macros@>=
@z
@x
@<Sub...@>=
void print_line @,@,@[ARGS((int))@];@+@t}\6{@>
void print_line(k)
int k;
@y
@(libshowline.c@>=
void print_line(int k)
@z
@x
@ @<Preprocessor macros@>=
@y
@ @<Showline macros@>=
@z
@x
@<Sub...@>=
void show_line @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libshowline.c@>=
void show_line @,@,@[ARGS((void))@];@+@t}\6{@>
@z
Printing the profile has its own file.
@x
@<Sub...@>=
void print_freqs @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@y
@(libprofile.c@>=
#include <stdio.h>
#include <setjmp.h>
#include "libconfig.h"
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
#include "libarith.h"
#include "libimport.h"
void print_freqs @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@z
We might need ll as a local variable here.
@x
octa cur_loc;
@y
octa cur_loc;
MMIX_LOCAL_LL
@z
we use MMIX_FETCH.
@x
loc_implied: printf("%10d. %08x%08x: %08x (%s)\n",
p->dat[j].freq, cur_loc.h, cur_loc.l, p->dat[j].tet,
info[p->dat[j].tet>>24].name);
@y
loc_implied:
{ tetra inst;
MMIX_FETCH(inst,cur_loc);
printf("%10d. %08x%08x: %08x (%s)\n",
p->dat[j].freq, cur_loc.h, cur_loc.l, inst,
info[inst>>24].name);
}
@z
For the mmixlib, we split performing the
instruction in three parts:
resuming,fetching, and executing.
here we do only the resuming.
@x
@<Perform one instruction@>=
{
if (resuming) loc=incr(inst_ptr,-4), inst=g[rX].l;
else @<Fetch the next instruction@>;
@y
@<Perform one instruction@>=
{
if (resuming)
{ loc=incr(inst_ptr,-4), inst=g[rzz?rXX:rX].l;
if (rzz==0) /* RESUME 0 */
{ if ((loc.h&sign_bit) != (inst_ptr.h&sign_bit))
{ resuming = false;
goto protection_violation;
}
@<Check for security violation@>
}
}
@z
This restriction is no longer necessary.
@x
if (loc.h>=0x20000000) goto privileged_inst;
@y
@z
The next two lines are not a propper part of
performing an instruction and move to a separate
function respectively to the main loop.
@x
@<Trace the current instruction, if requested@>;
if (resuming && op!=RESUME) resuming=false;
@y
@z
@x
int rop; /* ropcode of a resumed instruction */
@y
int rop; /* ropcode of a resumed instruction */
int rzz; /* Z field of a resumed instruction */
@z
We turn |breakpoint| into an int to provide more information on the
kind of breakpoint. Instead of setting |breakpoint| to |true|, we
(usually) store the respective bit of the |bkpt| field in the |mem_tetra|
that caused the break. In case of a SWYM instruction, we set it to
the YZ value shifted by 8 bit to the left.
In all other cases, we set the |trace_bit|.
@x
bool breakpoint; /* should we pause after the current instruction? */
bool tracing; /* should we trace the current instruction? */
@y
int breakpoint=0; /* what caused the pause after the current instruction? */
bool tracing; /* should we trace the current instruction? */
@z
@x
bool interacting; /* are we in interactive mode? */
@y
bool interacting=false; /* are we in interactive mode? */
#ifdef MMIX_TRAP
bool show_operating_system = false; /* do we show negative addresses */
#endif
@z
We make some more variables global.
@x
@ @<Local...@>=
register mmix_opcode op; /* operation code of the current instruction */
register int xx,yy,zz,yz; /* operand fields of the current instruction */
register tetra f; /* properties of the current |op| */
@y
@ @<Glob...@>=
mmix_opcode op; /* operation code of the current instruction */
int xx,yy,zz,yz; /* operand fields of the current instruction */
tetra f; /* properties of the current |op| */
@ @<Local...@>=
@z
and p is no longer needed in performing an instruction.
@x
register char *p; /* current place in a string */
@y
@z
loading the instruction is postponed
@x
inst=ll->tet;
@y
@z
When we hit an execute breakpoint, we set the exec bit in |breakpoint|.
We keep the current frequency for tracing.
@x
ll->freq++;
if (ll->bkpt&exec_bit) breakpoint=true;
tracing=breakpoint||(ll->bkpt&trace_bit)||(ll->freq<=trace_threshold);
@y
cur_freq=++(ll->freq);
if (ll->bkpt&exec_bit) breakpoint|=exec_bit;
if ((ll->bkpt&trace_bit)||(ll->freq<=trace_threshold)) breakpoint|=trace_bit;
tracing=(breakpoint!=0);
@z
now before incrementing the instruction pointer we load the instruction.
@x
inst_ptr=incr(inst_ptr,4);
@y
@<Check for security violation@>
inst=0; /* default TRAP 0,Halt,0 */
if(!MMIX_FETCH(inst,loc))
goto page_fault;
inst_ptr=incr(inst_ptr,4);
if ((inst_ptr.h&sign_bit) && !(loc.h&sign_bit))
goto protection_violation;
@z
We change how to display certain instructions.
@x
{"RESUME",0x00,0,0,5,"{%#b} -> %#z"},@|
@y
{"RESUME",0x00,0,0,5,"{%#b}, $255 = %x, -> %#z"},@|
@z
@x
{"SYNC",0x01,0,0,1,""},@|
{"SWYM",0x00,0,0,1,""},@|
@y
{"SYNC",0x01,0,0,1,"%z"},@|
{"SWYM",0x01,0,0,1,"%r"},@|
@z
L,G, and O are made global.
@x
@ @<Local...@>=
register int G,L,O; /* accessible copies of key registers */
@y
@ @<Glob...@>=
int G=255,L=0,O=0; /* accessible copies of key registers */
@z
Some initialization needs to be done once others at each reboot.
@x
@<Initialize...@>=
g[rK]=neg_one;
g[rN].h=(VERSION<<24)+(SUBVERSION<<16)+(SUBSUBVERSION<<8);
g[rN].l=ABSTIME; /* see comment and warning above */
g[rT].h=0x80000005;
g[rTT].h=0x80000006;
g[rV].h=0x369c2004;
if (lring_size<256) lring_size=256;
lring_mask=lring_size-1;
if (lring_size&lring_mask)
panic("The number of local registers must be a power of 2");
@.The number of local...@>
l=(octa*)calloc(lring_size,sizeof(octa));
if (!l) panic("No room for the local registers");
@.No room...@>
cur_round=ROUND_NEAR;
@y
@<Set up persistent data@>=
if (lring_size<256) lring_size=256;
lring_mask=lring_size-1;
if (lring_size&lring_mask)
panic("The number of local registers must be a power of 2");
l=(octa*)calloc(lring_size,sizeof(octa));
if (!l) panic("No room for the local registers");
@ @<Initialize...@>=
sclock.l=sclock.h=0;
profile_started=false;
halted=false;
stdin_buf_start=stdin_buf_end=stdin_buf;
good_guesses=bad_guesses=0;
profiling=false;
interrupt=false;
@ @<Boot the machine@>=
memset(l,0,lring_size*sizeof(octa));
memset(g,0,sizeof(g));
L=O=S=0;
G=g[rG].l=255;
#ifdef MMIX_BOOT
g[rK] = zero_octa;
#else
g[rK] = neg_one;
#endif
g[rN].h=(VERSION<<24)+(SUBVERSION<<16)+(SUBSUBVERSION<<8);
g[rN].l=ABSTIME; /* see comment and warning above */
g[rT].h=0x80000000;g[rT].l=0x00000000;
g[rTT].h=0x80000000;g[rTT].l=0x00000000;
g[rV].h=0x12340D00;
g[rV].l=0x00002000;
cur_round=ROUND_NEAR;
@z
@x
if (((S-O-L)&lring_mask)==0) stack_store();
@y
if (((S-O-L)&lring_mask)==0) stack_store(l[S&lring_mask]);
@z
@x
@d test_store_bkpt(ll) if ((ll)->bkpt&write_bit) breakpoint=tracing=true
@y
@d test_store_bkpt(ll) if ((ll)->bkpt&write_bit) breakpoint|=write_bit,tracing=true
@z
stack_store must implement the rC register.
@x
@<Sub...@>=
void stack_store @,@,@[ARGS((void))@];@+@t}\6{@>
void stack_store()
{
register mem_tetra *ll=mem_find(g[rS]);
register int k=S&lring_mask;
ll->tet=l[k].h;@+test_store_bkpt(ll);
(ll+1)->tet=l[k].l;@+test_store_bkpt(ll+1);
if (stack_tracing) {
tracing=true;
if (cur_line) show_line();
printf(" M8[#%08x%08x]=l[%d]=#%08x%08x, rS+=8\n",
g[rS].h,g[rS].l,k,l[k].h,l[k].l);
}
g[rS]=incr(g[rS],8), S++;
}
@y
@<Stack store@>=
void stack_store @,@,@[ARGS((octa))@];@+@t}\6{@>
void stack_store(x)
octa x;
{ unsigned int pw_bit, new_pw_bit;
mem_tetra *ll;
pw_bit=g[rQ].h&PW_BIT;
new_pw_bit=new_Q.h&PW_BIT;
if(!MMIX_STO(x,g[rS])) /* implementing the rC register */
{ /* set CP_BIT */
g[rQ].l |= CP_BIT;
new_Q.l |= CP_BIT;
if (g[rC].l&0x02) /* Write bit */
{ int s;
octa address, base, offset,mask;
mask.h=mask.l=0xFFFFFFFF;
g[rQ].h &=~PW_BIT; /* restore PW_BIT */
new_Q.h &=~PW_BIT;
g[rQ].h |= pw_bit;
new_Q.h |= new_pw_bit;
s = (g[rV].h>>8)&0xFF; /* extract the page size from rV */
mask = shift_left(mask,s);
offset.h = g[rS].h&~mask.h,offset.l = g[rS].l&~mask.l;
mask.h &= 0x0000FFFF; /* reduce mask to 48 bits */
base.h = g[rC].h&mask.h,base.l = g[rC].l&mask.l;
address.h=base.h|offset.h,address.l=base.l|offset.l;
MMIX_STO(x,address);
}
}
ll=mem_find(g[rS]);
test_store_bkpt(ll);
test_store_bkpt(ll+1);
mmix_stack_trace(" M8[#%08x%08x]=#%08x%08x, rS+=8\n",
g[rS].h,g[rS].l,x.h,x.l);
g[rS]=incr(g[rS],8), S++;
}
@ @<Sub...@>=
@<Stack store@>@;
@z
@x
@d test_load_bkpt(ll) if ((ll)->bkpt&read_bit) breakpoint=tracing=true
@y
@d test_load_bkpt(ll) if ((ll)->bkpt&read_bit) breakpoint|=read_bit,tracing=true
@z
Same with stack load.
@x
@<Sub...@>=
void stack_load @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@<Sub...@>=
@<Stack load@>@;
@ @<Stack load@>=
void stack_load @,@,@[ARGS((void))@];@+@t}\6{@>
@z
@x
l[k].h=ll->tet;@+test_load_bkpt(ll);
l[k].l=(ll+1)->tet;@+test_load_bkpt(ll+1);
@y
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
MMIX_LDO(l[k],g[rS]);
@z
showing lines is part of main.
@x
if (stack_tracing) {
tracing=true;
if (cur_line) show_line();
printf(" rS-=8, l[%d]=M8[#%08x%08x]=#%08x%08x\n",
k,g[rS].h,g[rS].l,l[k].h,l[k].l);
}
@y
mmix_stack_trace(" rS-=8, l[%d]=M8[#%08x%08x]=#%08x%08x\n",
k,g[rS].h,g[rS].l,l[k].h,l[k].l);
@z
@x
@<Sub...@>=
int register_truth @,@,@[ARGS((octa,mmix_opcode))@];@+@t}\6{@>
@y
@<Sub...@>=
@<Register truth@>@;
@ @<Register truth@>=
int register_truth @,@,@[ARGS((octa,mmix_opcode))@];@+@t}\6{@>
@z
Make sure we do not branch from a positive to a negative address.
@x
inst_ptr=z;
@y
if ((z.h&sign_bit) && !(loc.h&sign_bit))
goto protection_violation;
inst_ptr=z;
@z
@x
if (g[rI].l<=2 && g[rI].l && g[rI].h==0) tracing=breakpoint=true;
@y
if (g[rI].l<=2 && g[rI].l && g[rI].h==0)
g[rQ].l |= IN_BIT, new_Q.l |= IN_BIT;
@z
Loading is supposed to use the functions from libconfig
@x
case LDB: case LDBI: case LDBU: case LDBUI:@/
i=56;@+j=(w.l&0x3)<<3; goto fin_ld;
case LDW: case LDWI: case LDWU: case LDWUI:@/
i=48;@+j=(w.l&0x2)<<3; goto fin_ld;
case LDT: case LDTI: case LDTU: case LDTUI:@/
i=32;@+j=0;@+ goto fin_ld;
case LDHT: case LDHTI: i=j=0;
fin_ld: ll=mem_find(w);@+test_load_bkpt(ll);
x.h=ll->tet;
x=shift_right(shift_left(x,j),i,op&0x2);
check_ld:@+if (w.h&sign_bit) goto privileged_inst;
@y
case LDB: case LDBI: case LDBU: case LDBUI:@/
i=56;@+j=56;@+if(!MMIX_LDB(x,w)) goto page_fault; goto fin_ld;
case LDW: case LDWI: case LDWU: case LDWUI:@/
i=48;@+j=48;@+if(!MMIX_LDW(x,w)) goto page_fault; goto fin_ld;
case LDT: case LDTI: case LDTU: case LDTUI:@/
i=32;@+j=32;@+if(!MMIX_LDT(x,w)) goto page_fault; goto fin_ld;
case LDHT: case LDHTI: i=j=0;@+if(!MMIX_LDT(x,w)) goto page_fault;
x.h=x.l;x.l=0;
fin_ld: ll=mem_find(w);@+test_load_bkpt(ll);
if ((op&0x2)==0) x=shift_right(shift_left(x,i),i,op&0x2);
check_ld:@+ if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
goto store_x;
page_fault:
if ((g[rK].h & g[rQ].h) != 0 || (g[rK].l & g[rQ].l) != 0)
{ x.h=0, x.l=inst;
y = w;
z = zero_octa;
@<Initiate a trap interrupt@>
inst_ptr=y=g[rTT];
}
break;
@z
@x
case LDO: case LDOI: case LDOU: case LDOUI: case LDUNC: case LDUNCI:
w.l&=-8;@+ ll=mem_find(w);
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
x.h=ll->tet;@+ x.l=(ll+1)->tet;
goto check_ld;
case LDSF: case LDSFI: ll=mem_find(w);@+test_load_bkpt(ll);
x=load_sf(ll->tet);@+ goto check_ld;
@y
case LDO: case LDOI: case LDOU: case LDOUI:
w.l&=-8;@+ ll=mem_find(w);
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
if (!MMIX_LDO(x,w)) goto page_fault;
goto check_ld;
case LDUNC: case LDUNCI:
w.l&=-8;@+ ll=mem_find(w);
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
if (!MMIX_LDO_UNCACHED(x,w)) goto page_fault;
goto check_ld;
case LDSF: case LDSFI: ll=mem_find(w);@+test_load_bkpt(ll);
if (!MMIX_LDT(x,w)) goto page_fault;
x=load_sf(x.l);@+ goto check_ld;
@z
Same for storing.
@x
case STB: case STBI: case STBU: case STBUI:@/
i=56;@+j=(w.l&0x3)<<3; goto fin_pst;
@y
case STB: case STBI: case STBU: case STBUI:@/
if ((op&0x2)==0) {
a=shift_right(shift_left(b,56),56,0);
if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
}
if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
if (!MMIX_STB(b,w)) goto page_fault;
goto fin_st;
@z
@x
case STW: case STWI: case STWU: case STWUI:@/
i=48;@+j=(w.l&0x2)<<3; goto fin_pst;
@y
case STW: case STWI: case STWU: case STWUI:@/
if ((op&0x2)==0) {
a=shift_right(shift_left(b,48),48,0);
if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
}
if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
if (!MMIX_STW(b,w)) goto page_fault;
goto fin_st;
@z
@x
case STT: case STTI: case STTU: case STTUI:@/