-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decoder.c
More file actions
1691 lines (1606 loc) · 39.2 KB
/
encode_decoder.c
File metadata and controls
1691 lines (1606 loc) · 39.2 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
/*
* encode_decoder
* ==============
*
* Program to process an input stream of Assembly Language
* definitions and output C/C++ source code providing a
* mechanism to decode those instructions.
*
* Usage:
* encode_decoder < input_stream > output_stream
* or
* encode_decoder input_file[.suffix]
*/
#include <stdio.h>
/*
* The input stream has the following format definition.
*
* o Only data following an open brace (to the end of the line
* or a close brace) is considered 'data'. Everything
* else is considered a comment.
*
* o The character immediately following the open brace
* provides the type of the data being supplied in
* the rest of the data item.
*
* Record Commands:
*
* Z Provide the number of bits which define the size of an
* instruction (typically 8 or 16).
*
* {Z 8}
*
* Only one per instruction set; must be set before first
* instruction definition.
*
* I Provide details of an instruction definition. This is
* a series of binary numbers, each of S bits long separated
* by white space. The final word, not a binary number and
* not needing to be S bits long, is the name of the instruction.
* This is not case sensitive, and is effectively passed through
* to the output without interpretation.
*
* Where bits of the instruction are arguments to the instruction
* (and are therefore not actually part of the instruction) they
* should be marked with a period or letter.
*
* {I 00000000 NOP}
* or
* {I 0101.... ADD}
*
* F Provide formatting details for outputting instruction names.
* In the absence of a percent symbol this is taken as a prefix,
* where a percent symbol is provided then this is where the
* instruction name will be inserted in the output.
*
* {F opcode_%_func}
*
* When no F record has been defined then each record has only
* a single result value (being the instruction named in the I record).
*
* If multiple F records are provided then a corresponding number
* of result values are created with the decoding data structure.
*
* T Provide the name of the array type, defaults "decoder_t".
*
* S Define the scope of the table, defaults to 'static'.
*
* N Provide the name of the array (of the above type), defaults
* "decoder".
*
* L Language selection:
*
* {L C} Select C
* or
* {L C++} Select C++
* {L CPP}
*
* Given the ability of the pre-processor to output text to
* the target files ahead of the decoding data, it is required
* to make this the first record at head of the input data.
*
* E provide the name of a routine to be placed into the decoding
* tree in the event that decoding does not reach a formal
* instruction.
*
* If not defined then decoding stops at the nearest valid opcode
* and the output table includes details of where the ambiguity is.
*
* {E illegal_inst}
*
* W Define the maximum number of words required to determine a
* target instruction.
*
* If this is specified as 1 then the output table will NOT
* include a word index (assuming it always be 0).
*
* {W 1} No index output
* or
* {W 2} Index output
*
* [space]
* [tab] Content of the record is passed through to the output "as is"
* before the content of the table is generated.
*
* [Underscore]
* _ Content of the record is passed through to the output "as is"
* AFTER the content of the table is generated.
*
* H Content is passed into the header file with the file name
* either based on the input file (with '.h' applied) or simply
* also sent to stdout with the other output.
*
* Block Commands:
*
* These are Record Commands which are used to bracket a number
* of lines for pass through to one of the header files, the start
* of the source file or the tail of the source file.
*
* BS Used to indicate following line belong at the Start of the
* source file.
*
* BE Used to indicate following line belong at the End of the
* source file.
*
* BH Used to indicate following lines belong in the header file.
*
* BC Used to indicate following lines are only comments and are
* to be skipped.
*
* B Used to indicate the end of a block of lines.
*
* The initial implementation of the block commands had a simple "you're
* either in a block, or you're not" mechanism. Starting a new block
* with one of BS, BE, BH or BC ended the previous block and started the
* next.
*
* This has been modified (as well as BF becoming simply B) and now blocks
* can be nested. Consequently for *every* block starting command there
* must now be a corresponding block ending command.
*
* This has been done to simplify the mechanism through which header files,
* source files and block comments are handled. I believe.
*/
/*
* Provide constant names for the characters above that have specific
* meanings.
*/
#define BEGIN_RECORD '{'
#define END_RECORD '}'
#define SIZE_RECORD 'Z'
#define INSTRUCTION_RECORD 'I'
#define FORMAT_RECORD 'F'
#define TYPE_RECORD 'T'
#define SCOPE_RECORD 'S'
#define NAME_RECORD 'N'
#define LANGUAGE_RECORD 'L'
#define ERROR_RECORD 'E'
#define WORDS_RECORD 'W'
#define HEADER_RECORD 'H'
#define BLOCK_RECORD 'B'
#define BLOCK_START 'S'
#define BLOCK_END 'E'
#define BLOCK_HEADER 'H'
#define BLOCK_COMMENT 'C'
#define INSERT_HERE '%'
#define ONE_BIT '1'
#define ZERO_BIT '0'
#define ARGUMENT_BIT '.'
#define PERIOD '.'
#define SPACE ' '
#define TAB '\t'
#define UNDERSCORE '_'
#define NL '\n'
#define EOS '\0'
#define ESCAPE_SYMBOL '\\'
#define QUESTION '?'
/*
* defines that shape the output of unmatched bits.
*/
#define PLACE_PATTERN 0x88888888
#define PLACE_MARK '+'
#define PLACE_GAP '-'
#define PLACE_VARIABLE '?'
/*
* Define simplistic memory allocation routines
*/
#include <malloc.h>
#define NEW(t) ((t *)malloc(sizeof(t)))
#define FREE(p) free(( void *)(p))
/*
* String ops.
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define DUP(s) strdup(s)
/*
* Define an internal type used to handle the bits bits.
*/
#include <stdint.h>
typedef uint32_t word;
/*
* Define a logical value.
*/
typedef int bool;
#define TRUE (0==0)
#define FALSE (0==1)
/*
* Define the maximum number of words an instruction can
* be composed from.
*/
#define MAX_CODES 16
/*
* Define the largest input line we are happy to handle.
*/
#define MAX_BUFFER 256
/*
* Define the maximum number of formats which the program
* will handle.
*/
#define MAX_FORMATS 8
/*
* Define the data structure used to capture a single instruction
*/
#define INSTRUCTION struct instruction
INSTRUCTION {
/*
* Source line number.
*/
int line;
/*
* Capture the input data describing the instruction.
*/
char *name, /* Given instruction name */
*comment; /* Additional commentary provided */
int elements; /* Number of words forming the instruction */
word opcode[ MAX_CODES ], /* The instruction construction. */
mask[ MAX_CODES ]; /* Indicate those bits which are instruction */
char *description[ MAX_CODES ]; /* Copy of the original bit description */
int matches; /* How many versions of this instruction? */
word unmatched[ MAX_CODES ]; /* The mask giving the bits that are undefined. */
/*
* We will daisy chain the records together
* as a flexible mechanism for keeping them
* together.
*/
INSTRUCTION *next;
};
/*
* This is the data structure used to capture the decoding tree.
*/
#define NODE struct node
NODE {
/*
* These are the output table index numbers once
* initial processing has been completed.
*/
int index;
/*
* Provide mechanism to link multiple
* instructions into a data structure
* that can decode instructions.
*/
bool leaf; /* True if this is a leaf node (and */
/* therefore specifies the instruction */
/* below). */
/*
* If leaf TRUE...
*/
INSTRUCTION *decoded;
/*
* If leaf FALSE...
*
* then the follow data is used to distinguish between the
* two branches from this node.
*/
int op_word, /* Which instruction word are we comparing? */
op_bit; /* Which bit in that word? */
NODE *zero,
*one;
};
/*
* Define a record to hold those lines of data which need
* to be output AFTER the table has been created.
*/
#define FINISH struct finish
FINISH {
int line;
char *data;
FINISH *next;
};
/************************************************
* *
* GLOBAL VARIABLES DEFINED HERE *
* *
************************************************/
/*
* Define the output streams for the program result.
*/
static char *input_source_file = NULL,
*output_base_name = NULL,
*output_source_name = NULL,
*output_header_name = NULL;
static FILE *output_source = NULL,
*output_header = NULL;
/*
* Define word size and enabled flag.
*/
static bool word_size_set = FALSE;
static int word_size = 0;
/*
* Define the maximum number of words required
* to identify an instruction uniquwly.
*/
static bool maximum_words_set = FALSE;
static int maximum_words = MAX_CODES;
/*
* Define the output formatting; a in front
* b afterwards.
*/
static int output_formats = 0;
static char *output_format_a[ MAX_FORMATS ],
*output_format_b[ MAX_FORMATS ];
/*
* Define the comment output formatting.
*/
static char *output_comment_a = NULL,
*output_comment_b = NULL;
/*
* Define the name of the error routine
*/
static char *error_handler = NULL;
/*
* Output data type record and decode name.
*/
static char *data_type = NULL;
static char *data_scope = NULL;
static char *data_name = NULL;
/*
* The tail/finish data to be output after
* the table.
*/
static FINISH *finish_data = NULL,
**finish_data_tail = &( finish_data );
/*
* All the instructions can be found here.
*/
static INSTRUCTION *instructions = NULL,
**instruction_tail = &( instructions );
/*
* The decode tree is found here
*/
static NODE *tree = NULL;
/*
* Instruction DROP count.
*/
static int dropped = 0;
/*
* Enumeration with variable for tracking where the last line of output went
*/
static enum {
UNSPECIFIED_TARGET,
SOURCE_TARGET,
HEADER_TARGET
} output_target = UNSPECIFIED_TARGET;
/*
* Enumeration with variable for tracking the block mode in force.
*/
enum block_modes {
LINE_MODE,
START_MODE,
END_MODE,
HEADER_MODE,
COMMENT_MODE
};
#define BLOCK_STACK struct block_stack
BLOCK_STACK {
enum block_modes mode;
int line;
BLOCK_STACK *prev;
};
static BLOCK_STACK *root_block_stack = NULL,
*spare_block_stack = NULL;
void push_mode( enum block_modes mode, int line ) {
BLOCK_STACK *ptr;
if(( ptr = spare_block_stack )) {
spare_block_stack = ptr->prev;
}
else {
ptr = NEW( BLOCK_STACK );
}
ptr->mode = mode;
ptr->line = line;
ptr->prev = root_block_stack;
root_block_stack = ptr;
}
int pop_mode( void ) {
BLOCK_STACK *ptr;
if(( ptr = root_block_stack )) {
root_block_stack = ptr->prev;
ptr->prev = spare_block_stack;
spare_block_stack = ptr;
return( TRUE );
}
return( FALSE );
}
/************************************************
* *
* IMPLEMENTATION ROUTINES *
* *
************************************************/
/*
* Is a character a visible one?
*/
static bool isvisible( int c ) {
return(( c > SPACE )&&( c < 127 ));
}
/*
* Is a character a valid part of an opcode?
*/
static bool isopcode( int c ) {
return(( c == ZERO_BIT )||( c == ONE_BIT )||( c == ARGUMENT_BIT )||( isalpha( c )));
}
/*
* Output filename construction
*/
static char *strcatdup( char *a, const char *b ) {
int l = strlen( a ) + strlen( b ) + 1;
char *r = (char *)malloc( l );
strcpy( r, a );
return( strcat( r, b ));
}
/*
* Process a line of input.
*/
static bool process( int line, char *input, char *comment ) {
char record;
switch(( record = *input++ )) {
case SIZE_RECORD: {
int i;
/*
* S nnn Provide number of bits per word of instruction
*/
output_target = UNSPECIFIED_TARGET;
i = atoi( input );
if(( i <= 0 )||( i > ( sizeof( word ) << 3 ))) {
fprintf( stderr, "Line %d: Invalid word size %d.\n", line, i );
return( FALSE );
}
if( word_size_set ) {
fprintf( stderr, "Line %d: Cannot reset word size.\n", line );
return( FALSE );
}
word_size = i;
word_size_set = TRUE;
break;
}
case WORDS_RECORD: {
int i;
/*
* W nnn Provide maximum number of words required to
* identify an instruction.
*/
output_target = UNSPECIFIED_TARGET;
i = atoi( input );
if(( i <= 0 )||( i > MAX_CODES )) {
fprintf( stderr, "Line %d: Invalid number of words %d.\n", line, i );
return( FALSE );
}
if( maximum_words_set ) {
fprintf( stderr, "Line %d: Cannot reset number of words.\n", line );
return( FALSE );
}
maximum_words = i;
maximum_words_set = TRUE;
break;
}
case FORMAT_RECORD: {
char *p, *q, *r;
/*
* F ffff[%ffff]
*/
output_target = UNSPECIFIED_TARGET;
if( output_formats >= MAX_FORMATS ) {
fprintf( stderr, "Line %d: Too many output formats specified (maximum is %d).\n", line, MAX_FORMATS );
return( FALSE );
}
/*
* Strip spaces...
*/
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No output format found.\n" );
return( FALSE );
}
if(( p = strchr( input, INSERT_HERE ))) {
*p++ = EOS;
output_format_a[ output_formats ] = DUP( input );
output_format_b[ output_formats ] = DUP( p );
}
else {
output_format_a[ output_formats ] = DUP( input );
output_format_b[ output_formats ] = "";
}
output_formats++;
break;
}
case LANGUAGE_RECORD: {
char *p, *q, *r;
/*
* L language
*/
output_target = UNSPECIFIED_TARGET;
if(( output_comment_a != NULL )||( output_comment_b != NULL )) {
fprintf( stderr, "Output comment already specified.\n" );
return( FALSE );
}
/*
* Strip spaces...
*/
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No language found.\n" );
return( FALSE );
}
/*
* We have to remember to set up the output files, but only
* if the base name is non-NULL.
*/
if( strcasecmp( input, "c" ) == 0 ) {
/*
* C Language
*/
output_comment_a = "/*";
output_comment_b = "*/";
if( output_base_name ) {
output_header_name = strcatdup( output_base_name, ".h" );
output_source_name = strcatdup( output_base_name, ".c" );
if(( output_header = fopen( output_header_name, "w" )) == NULL ) {
fprintf( stderr, "Unable to open header file '%s'.\n", output_header_name );
return( FALSE );
}
if(( output_source = fopen( output_source_name, "w" )) == NULL ) {
fprintf( stderr, "Unable to open header file '%s'.\n", output_source_name );
return( FALSE );
}
}
}
else {
if(( strcasecmp( input, "c++" ) == 0 )||( strcasecmp( input, "cpp" ) == 0 )) {
/*
* C++ Language
*/
output_comment_a = "//";
output_comment_b = "";
if( output_base_name ) {
output_header_name = strcatdup( output_base_name, ".h" );
output_source_name = strcatdup( output_base_name, ".cpp" );
if(( output_header = fopen( output_header_name, "w" )) == NULL ) {
fprintf( stderr, "Unable to open header file '%s' for write.\n", output_header_name );
return( FALSE );
}
if(( output_source = fopen( output_source_name, "w" )) == NULL ) {
fprintf( stderr, "Unable to open source file '%s' for write.\n", output_source_name );
return( FALSE );
}
}
}
else {
fprintf( stderr, "Unrecognised language '%s'.\n", input );
return( FALSE );
}
}
break;
}
case TYPE_RECORD: {
char *p, *q, *r;
/*
* Strip spaces...
*/
output_target = UNSPECIFIED_TARGET;
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No data type found.\n" );
return( FALSE );
}
if( data_type ) {
fprintf( stderr, "Data type already set.\n" );
}
data_type = DUP( input );
break;
}
case SCOPE_RECORD: {
char *p, *q, *r;
/*
* Strip spaces...
*/
output_target = UNSPECIFIED_TARGET;
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No scope found.\n" );
return( FALSE );
}
if( data_scope ) {
fprintf( stderr, "Scope already set.\n" );
}
data_scope = DUP( input );
break;
}
case NAME_RECORD: {
char *p, *q, *r;
/*
* Strip spaces...
*/
output_target = UNSPECIFIED_TARGET;
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No data name found.\n" );
return( FALSE );
}
if( data_name ) {
fprintf( stderr, "Data name already set.\n" );
}
data_name = DUP( input );
break;
}
case ERROR_RECORD: {
char *p, *q, *r;
/*
* Strip spaces...
*/
output_target = UNSPECIFIED_TARGET;
p = input;
while( *p != EOS ) {
if( isvisible( *p )) {
p++;
}
else {
/*
* Roll out the white space.
*/
q = p;
r = p+1;
while(( *q++ = *r++ ));
}
}
if( *input == EOS ) {
fprintf( stderr, "No error handler found.\n" );
return( FALSE );
}
if( error_handler ) {
fprintf( stderr, "Error handler already set.\n" );
}
error_handler = DUP( input );
break;
}
case SPACE:
case TAB: {
/*
* Pass through "as is".
*/
if( output_target != SOURCE_TARGET ) {
output_target = SOURCE_TARGET;
fprintf( output_source, "#line %d \"%s\"\n", line, input_source_file );
}
fprintf( output_source, "%s\n", input );
break;
}
case EOS: {
/*
* Special case of "pass through": a
* record start symbol as the last
* character in a line.
*/
if( output_target != SOURCE_TARGET ) {
output_target = SOURCE_TARGET;
fprintf( output_source, "#line %d \"%s\"\n", line, input_source_file );
}
fprintf( output_source, "\n" );
break;
}
case UNDERSCORE: {
/*
* Add more data to the end of the file.
*/
output_target = UNSPECIFIED_TARGET;
FINISH *ptr = NEW( FINISH );
ptr->line = line;
ptr->data = DUP( input );
ptr->next = NULL;
*finish_data_tail = ptr;
finish_data_tail = &( ptr->next );
break;
}
case HEADER_RECORD: {
/*
* Pass through "as is".
*/
if( output_target != HEADER_TARGET ) {
output_target = HEADER_TARGET;
fprintf( output_header, "#line %d \"%s\"\n", line, input_source_file );
}
fprintf( output_header, "%s\n", input );
break;
}
case INSTRUCTION_RECORD: {
INSTRUCTION *p;
/*
* Start with new empty record, and link it in.
*/
output_target = UNSPECIFIED_TARGET;
p = NEW( INSTRUCTION );
p->line = line;
p->name = NULL;
p->comment = DUP( comment );
p->elements = 0;
for( int i = 0; i < MAX_CODES; p->opcode[ i++ ] = 0 );
for( int i = 0; i < MAX_CODES; p->mask[ i++ ] = 0 );
for( int i = 0; i < MAX_CODES; p->description[ i++ ] = NULL );
for( int i = 0; i < MAX_CODES; p->unmatched[ i++ ] = 0 );
p->matches = 0;
p->next = NULL;
*instruction_tail = p;
instruction_tail = &( p->next );
/*
* Fill in the record; start by breaking the input up into
* space separated units (but still ignoring initial spaces).
*/
while(( *input )&&( !isvisible( *input ))) input += 1;
while( *input ) {
char *e;
int count;
/*
* Find end of this word..
*/
for( e = input; isvisible( *e ); e++ );
/*
* Mark end of word as long as it is not the
* end of the input.
*/
if( *e ) {
*e++ = EOS;
while(( *e )&&( !isvisible( *e ))) e++;
}
/*
* Instruction data or instruction name? Count
* instruction characters first...
*/
count = 0;
for( char *l = input; *l; l++ ) if( isopcode( *l )) count++;
/*
* If count is same as string length and this is
* not the last word in the record, then this should
* be an opcode word.
*/
if(( count == strlen( input ))&&( *e != EOS )) {
int i;
if( count != word_size ) {
fprintf( stderr, "Found instruction size %d (word size is %d).\n", count, word_size );
return( FALSE );
}
/*
* Can we add this to the instruction record?
*/
if( p->elements >= MAX_CODES ) {
fprintf( stderr, "Found instruction too big (maximum %d words).\n", MAX_CODES );
return( FALSE );
}
/*
* Fill in instruction data.
*/
i = p->elements++;
for( char *l = input; *l; l++ ) {
switch( *l ) {
case ONE_BIT: {
p->opcode[ i ] = ( p->opcode[ i ] << 1 ) | 1;
p->mask[ i ] = ( p->mask[ i ] << 1 ) | 1;
break;
}
case ZERO_BIT: {
p->opcode[ i ] = ( p->opcode[ i ] << 1 ) | 0;
p->mask[ i ] = ( p->mask[ i ] << 1 ) | 1;
break;
}
default: {
p->opcode[ i ] = ( p->opcode[ i ] << 1 ) | 0;
p->mask[ i ] = ( p->mask[ i ] << 1 ) | 0;
break;
}
}
}
p->description[ i ] = DUP( input );
}
else {
/*
* This should be the last word on the line (ie *e should point
* to the EOS character.
*/
if( *e ) {
fprintf( stderr, "Instruction name '%s' not last word on line\n", input );
return( FALSE );
}
/*
* There SHOULD be some identifier verification at this point.
*
* .. eventually.
*/
p->name = DUP( input );
}
/*
* Move input to, the start of the next text of interest.
*/
input = e;
}
break;
}
default: {
output_target = UNSPECIFIED_TARGET;
if( isvisible( record )) {
fprintf( stderr, "Invalid record identifier '%c'.\n", record );
}
else {
fprintf( stderr, "Invalid record identifier ascii code %d.\n", (int)record );
}
return( FALSE );
}
}
return( TRUE );
}
/*
* Recursive tree building routine.
*
* Create tree using a binary division mechanism
*/
static NODE *insert( word *mask, INSTRUCTION *list, int count ) {
int w,
b,
d;
bool v;
word t;
NODE *here;
INSTRUCTION *ptr,
*ones,
*zeros;
int c0,
c1;
/*
* Deal with up front cases first.
*/
if( count == 0 ) {
/* Error Leaf node time! */
here = NEW( NODE );
here->index = 0;
here->leaf = TRUE;
here->decoded = NULL;
here->op_word = 0;
here->op_bit = 0;
here->zero = NULL;
here->one = NULL;
return( here );
}
if( count == 1 ) {
/*
* Brief duplication check: Getting here should mean that
* the AND result of the mask value passed in and the mask
* value of the instruction results in 0. If there are n
* bits still set then that instruction is accessible through
* 2^n alternative opcodes.
*/
count = 0;
for( int i = 0; i < MAX_CODES; i++ ) {
list->unmatched[ i ] = t = mask[ i ] & list->mask[ i ];
for( int j = 0; j < word_size; j++ ) {
if( t & 1 ) count++;