-
Notifications
You must be signed in to change notification settings - Fork 0
/
aasm.c
6341 lines (5447 loc) · 249 KB
/
aasm.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
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
/* *** Check mnemonics file; had to regress version due to ftp cock-up *** */
/* AASM - ARM assembler Alpha+ version J. Garside UofM 16/8/12 */
/*
// To do @@@@@@@@
// Symbol table lists
// Can't do ORG top - (end - start) ... difficult (!!), but should be possible
// Would require `virtual' assembler pointer or label values
// Some ELF work May 2004 - output file accepted by ARM's "fromelf" and komodo
// Thumb shift problem fixed Apr.'07
// 10/01/07 Fix of LDRH post index; fix of literal recogniser
// PUSH/POP added to ARM May '07 (Introduced bug removed June '07)
// Reorigining in ELF/DEFS bug found & fixed June '07.
// Data records added 9/7/07
// Oops! LDR= `brain fart' fixed 26/7/07
// Subtle LDR= bug found & believed fixed 21/12/07
// Added Verilog ROM output 30/4/09
// Fixed erroneous 'fed up' reporting 1/5/09
// Added binary IMPORT 2/7/09
// Symbol list bug fix 16/3/10
// ADRL PC trapped and forbidden 17/3/10
// Thumb BLX fixed + 1 improved error report 27/9/11
// C-style character escapes added to immediates (e.g. #'\n') 16/8/12
// Okay/error return value added 16/8/12
// Explicit mnemonic alternatives for shifts added 23/3/15
*/
// To do: ADRL fixed, "MOVX" etc added - some more shakedown tests (?) @@
// ADRL still causing problems :-( 'Length cycle' too great (4) found
// Pass counters increased but needs some attention to find 'real' answer (6/4/11)
//
// Pack literal pool (halfwords) together rather than `in order'
// Dump literals into space in ALIGN where possible
// Not sure LDRSH rd, =nnnn behaviour is correct
// No range check but doesn't alias -1 & FFFF either
// I think range check; ST thinks ban it
// LDMFDEQ as well as LDMEQFD etc. etc. etc.
// Proper shakedown testing (improving)
// Macros
// Conditional assembly
// "mnemonics" file not found if executed via $PATH; improve search
// 'record'/'structure' directive for creating offsets
#include <stdio.h>
#include <string.h> /* For {strcat, strlen, strcpy} */
#include <stdlib.h> /* For {malloc, exit} */
#define TRUE (0 == 0)
#define FALSE (0 != 0)
#define MAX_PASSES 30 /* No of reiterations before giving up */
#define SHRINK_STOP (MAX_PASSES-10) /* First pass where shrinkage forbidden */
#define VERILOG_MAX 0x10000 /* Maximum size of Verilog ROM output */
#define IF_STACK_SIZE 10 /* Maximum nesting of IF clauses */
#define SYM_TAB_HASH_BITS 4
#define SYM_TAB_LIST_COUNT (1 << SYM_TAB_HASH_BITS)
#define SYM_TAB_LIST_MASK (SYM_TAB_LIST_COUNT - 1)
#define SYM_NAME_MAX 32
#define LINE_LENGTH 256
#define SYM_TAB_CASE_FLAG 1 /* Bit mask for case insensitive flag */
#define SYM_TAB_EXPORT_FLAG 2 /* Keep whole table */
#define SYM_REC_DEF_FLAG 0x0100 /* Bit mask for `symbol defined' flag */
#define SYM_REC_EXPORT_FLAG 0x0200 /* Bit mask for `export' flag */
#define SYM_REC_EQU_FLAG 0x0400 /* Indicate `type' as EQU (abs) */
#define SYM_REC_USR_FLAG 0x0800 /* Indicate `type' as DEF (abs) */
#define SYM_REC_EQUATED (SYM_REC_EQU_FLAG | SYM_REC_USR_FLAG) /* Either */
#define SYM_REC_USR_FLAG 0x0800 /* Indicate `type' as DEF (abs) */
#define SYM_REC_THUMB_FLAG 0x1000 /* Indicate label in `Thumb' area */
/* Lowest 8 bits used for pass count */
#define SYM_REC_DATA_FLAG 0x2000 /* Data space offset */
#define ALLOW_ON_FIRST_PASS 0x00010000 /* Bit masks to prevent errors */
#define ALLOW_ON_INTER_PASS 0x00020000 /* occurring when not wanted. */
#define WARNING_ONLY 0x00040000
#define ALL_EXCEPT_LAST_PASS (ALLOW_ON_FIRST_PASS | ALLOW_ON_INTER_PASS)
#define SYM_NO_ERROR 0
#define SYM_ERR_SYNTAX 0x0100
#define SYM_ERR_NO_MNEM 0x0200
#define SYM_ERR_NO_EQU 0x0300
#define SYM_BAD_REG 0x0400
#define SYM_BAD_REG_COMB 0x0500
#define SYM_NO_REGLIST 0x0600
#define SYM_NO_RSQUIGGLE 0x0700
#define SYM_OORANGE (0x0800 | ALL_EXCEPT_LAST_PASS)
#define SYM_ENDLESS_STRING 0x0900
#define SYM_DEF_TWICE 0x0A00
#define SYM_NO_COMMA 0x0B00
#define SYM_NO_TABLE 0x0C00
#define SYM_GARBAGE 0x0D00
#define SYM_ERR_NO_EXPORT (0x0E00 | WARNING_ONLY)
#define SYM_INCONSISTENT 0x0F00
#define SYM_ERR_NO_FILENAME 0x1000
#define SYM_NO_LBR 0x1100
#define SYM_NO_RBR 0x1200
#define SYM_ADDR_MODE_ERR 0x1300
#define SYM_ADDR_MODE_BAD 0x1400
#define SYM_NO_LSQUIGGLE 0x1500
#define SYM_OFFSET_TOO_BIG (0x1600 | ALL_EXCEPT_LAST_PASS)
#define SYM_BAD_COPRO 0x1700
#define SYM_BAD_VARIANT 0x1800
#define SYM_NO_COND 0x1900
#define SYM_BAD_CP_OP 0x1A00
#define SYM_NO_LABELS (0x1B00 | WARNING_ONLY)
#define SYM_DOUBLE_ENTRY 0x1C00
#define SYM_NO_INCLUDE 0x1D00
#define SYM_NO_BANG 0x1E00
#define SYM_MISALIGNED (0x1F00 | ALL_EXCEPT_LAST_PASS)
#define SYM_OORANGE_BRANCH (0x2000 | ALL_EXCEPT_LAST_PASS)
#define SYM_UNALIGNED_BRANCH (0x2100 | ALL_EXCEPT_LAST_PASS)
#define SYM_VAR_INCONSISTENT 0x2200
#define SYM_NO_IDENTIFIER 0x2300
#define SYM_MANY_IFS 0x2400
#define SYM_MANY_FIS 0x2500
#define SYM_LOST_ELSE 0x2600
#define SYM_NO_HASH 0x2700
#define SYM_NO_IMPORT 0x2800
#define SYM_ADRL_PC 0x2900
#define SYM_ERR_NO_SHFT 0x2A00
#define SYM_NO_REG_HASH 0x2B00
#define SYM_ERR_BROKEN 0xFF00 /* TEMP uncommitted @@@ */
/* evaluate return error states */
/*
#define eval_okay 0x0000 // Rationalise @@@
*/
#define eval_okay SYM_NO_ERROR
#define eval_no_operand 0x3000
#define eval_no_operator 0x3100
#define eval_not_closebr 0x3200
#define eval_not_openbr 0x3300
#define eval_mathstack_limit 0x3400
#define eval_no_label (0x3500 | ALLOW_ON_FIRST_PASS)
#define eval_label_undef (0x3600 | ALL_EXCEPT_LAST_PASS)
#define eval_out_of_radix 0x3700
#define eval_div_by_zero (0x3800 | ALL_EXCEPT_LAST_PASS)
#define eval_operand_error 0x3900
#define eval_bad_loc_lab 0x3A00
#define eval_no_label_yet 0x3B00 /* Label not defined `above' (for `IF's)*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/* Literal pool flags */
#define LIT_DEFINED 0x01 /* Set when a literal pool entry has a value */
#define LIT_NO_DUMP 0x02 /* Set when record needn't be planted */
#define LIT_HALF 0x04 /* Set when value is halfword */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/* Expression evaluator */
#define MATHSTACK_SIZE 20
/* Enumerations used for both unary and binary operators */
#define PLUS 0
#define MINUS 1
#define NOT 2
#define MULTIPLY 3
#define DIVIDE 4
#define MODULUS 5
#define CLOSEBR 6
#define LEFT_SHIFT 7
#define RIGHT_SHIFT 8
#define AND 9
#define OR 10
#define XOR 11
#define EQUALS 12
#define NOT_EQUAL 13
#define LOWER_THAN 14 /* Unsigned comparisons */
#define LOWER_EQUAL 15
#define HIGHER_THAN 16
#define HIGHER_EQUAL 17
#define LESS_THAN 18 /* Signed comparisons */
#define LESS_EQUAL 19
#define GREATER_THAN 20
#define GREATER_EQUAL 21
#define LOG 22
#define END 23
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/* List file formatting constants */
#define LIST_LINE_LENGTH 120 /* Total line length */
#define LIST_LINE_ADDRESS 10 /* Address field width */
#define LIST_BYTE_COUNT 4 /* Number of bytes per line */
#define LIST_BYTE_FIELD (LIST_LINE_ADDRESS + 3 * LIST_BYTE_COUNT + 2)
#define LIST_LINE_LIST (LIST_LINE_LENGTH - 1 - LIST_BYTE_FIELD)
#define HEX_LINE_ADDRESS 10
#define HEX_BYTE_COUNT 16
#define HEX_LINE_LENGTH (HEX_LINE_ADDRESS + 3 * HEX_BYTE_COUNT)
#define ELF_TEMP_LENGTH 20
#define ELF_MACHINE 40 /* ARM (?) */
#define ELF_EHSIZE 52 /* Defined in standard */
#define ELF_PHENTSIZE (4 * 8) /* Defined in standard */
#define ELF_SHENTSIZE (4 * 10) /* Defined in standard */
#define ELF_SHN_ABS 0xFFF1 /* Defined in standard */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
#define v3 0xFF
#define v3M 0xFE
#define v4 0xFC
#define v4xM 0xFD
#define v4T 0xF8
#define v4TxM 0xF9
#define v5 0xF0
#define v5xM 0xF1
#define v5T 0xF0
#define v5TxM 0xF1
#define v5TE 0xC0
#define v5TExP 0xE0
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
typedef int boolean;
typedef enum { ARM, THUMB } instr_set;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
typedef enum { TYPE_BYTE, TYPE_HALF, TYPE_WORD, TYPE_CPRO } type_size;
typedef enum { NO_LABEL, MAYBE_SYMBOL, SYMBOL, LOCAL_LABEL } label_type;
typedef enum { ALL, EXPORTED, DEFINED, UNDEFINED } label_category;
typedef enum { ALPHABETIC, VALUE, DEFINITION, FOR_ELF } label_sort;
typedef enum
{
SYM_REC_ADDED, SYM_REC_DEFINED, SYM_REC_REDEFINED,
SYM_REC_UNCHANGED, SYM_REC_ERROR
}
defn_return;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
typedef struct sym_record_name /* Symbol record definition */
{
struct sym_record_name *pNext; /* Pointer to next record */
unsigned int count; /* Number of characters in name */
unsigned int hash;
unsigned int flags;
unsigned int identifier; /* Record identifier, also definition order */
unsigned int elf_section; /* Section number - purely for ELF driver */
int value;
char name[SYM_NAME_MAX]; /* Fixed field for name (quick) */
}
sym_record;
typedef struct /* Symbol table header definition */
{
char *name;
unsigned int symbol_number;
unsigned int flags;
sym_record *pList[SYM_TAB_LIST_COUNT];
}
sym_table;
typedef struct sym_table_item_name /* So we can make lists of symbol tables */
{
sym_table *pTable; /* Pointer to symbol table (or NULL) */
struct sym_table_item_name *pNext; /* Pointer to next record (or NULL) */
}
sym_table_item;
typedef struct local_label_name /* Local label element definition */
{
struct local_label_name *pNext; /* Pointer to next record */
struct local_label_name *pPrev; /* Pointer to previous record */
unsigned int label; /* The value of the label (number) */
unsigned int value; /* The label word value */
unsigned int flags;
}
local_label;
typedef struct own_label_name /* Definition of label on current line */
{
label_type sort; /* What `sort' of label, if any */
struct sym_record_name *symbol; /* Pointer to symbol record, if present */
struct local_label_name *local; /* Pointer to local label, if present */
}
own_label;
typedef struct literal_record_name /* Literal pool element holder definition */
{
struct literal_record_name *pNext; /* Pointer to next record */
unsigned int address; /* The address of the literal word */
unsigned int value; /* The literal word value */
unsigned int flags;
}
literal_record;
typedef struct elf_temp_name
{
struct elf_temp_name *pNext;
boolean continuation;
unsigned int section;
unsigned int address;
unsigned int count;
char data[ELF_TEMP_LENGTH];
}
elf_temp;
typedef struct elf_info_name /* Section info collecting point */
{ /* Just the bits I think need collecting */
struct elf_info_name *pNext;
unsigned int name;
unsigned int address;
unsigned int position;
unsigned int size;
}
elf_info;
typedef struct size_record_name /* Size of variable length operation */
{ /* (form an ordered list) */
struct size_record_name *pNext;
unsigned int size;
}
size_record;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
const SYM_RECORD_SIZE = sizeof(sym_record);
const SYM_TABLE_SIZE = sizeof(sym_table);
const SYM_TABLE_ITEM_SIZE = sizeof(sym_table_item);
const LIT_RECORD_SIZE = sizeof(literal_record);
const LOCAL_LABEL_SIZE = sizeof(local_label);
const ELF_TEMP_SIZE = sizeof(elf_temp);
const ELF_INFO_SIZE = sizeof(elf_info);
const SIZE_RECORD_SIZE = sizeof(size_record);
/*----------------------------------------------------------------------------*/
boolean set_options(int argc, char *argv[]);
boolean input_line(FILE*, char*, unsigned int);
boolean parse_mnemonic_line(char*, sym_table*, sym_table*, sym_table*);
unsigned int parse_source_line(char*, sym_table_item*, sym_table*, int, int,
char**, char*);
void print_error(char*, unsigned int, unsigned int, char*, int);
unsigned int assemble_line(char*, unsigned int, unsigned int,
own_label*, sym_table*, int, int, char**, char*);
int do_literal(instr_set, type_size, int*, boolean, unsigned int*);
unsigned int find_partials(unsigned int, unsigned int*);
unsigned int variable_item_size(int, unsigned int);
int get_thing(char*, unsigned int*, sym_table*);
int get_reg(char*, unsigned int*);
int get_thumb_reg(char*, unsigned int*, unsigned int);
int get_creg(char*, unsigned int*);
int get_copro(char*, unsigned int*);
int get_psr(char*, unsigned int*);
int get_shift(char*, unsigned int*);
int data_op_imm(unsigned int);
unsigned int thumb_pc_load(unsigned int, unsigned int, unsigned int, int, int,
unsigned int*);
void redefine_symbol(char*, sym_record*, sym_table*);
void assemble_redef_label(unsigned int, int, own_label*,
unsigned int*, int, int, int, char*);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
unsigned int evaluate(char*, unsigned int*, int*, sym_table*);
int get_variable(char*, unsigned int*, int*, int*, boolean*, sym_table*);
int get_operator(char*, unsigned int*, int*, int*);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
sym_table *sym_create_table(char*, unsigned int);
int sym_delete_table(sym_table*, boolean);
defn_return sym_define_label(char*, unsigned int, unsigned int,
sym_table*, sym_record**);
int sym_locate_label(char*, unsigned int, sym_table*, sym_record**);
sym_record *sym_find_label_list(char*, sym_table_item*);
sym_record *sym_find_label(char*, sym_table*);
sym_record *sym_create_record(char*, unsigned int, unsigned int, unsigned int);
void sym_delete_record(sym_record*);
int sym_delete_record_list(sym_record**, int);
int sym_add_to_table(sym_table*, sym_record*);
sym_record *sym_find_record(sym_table*, sym_record*);
void sym_string_copy(char*, sym_record*, unsigned int);
char *sym_strtab(sym_record*, unsigned int, unsigned int*);
sym_record *sym_sort_symbols(sym_table*, label_category, label_sort);
unsigned int sym_count_symbols(sym_table*, label_category);
void sym_print_table(sym_table*,label_category,label_sort,int,char*);
void local_label_dump(local_label*, FILE*);
void lit_print_table(literal_record*, FILE*);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
void byte_dump(unsigned int, unsigned int, char*, int);
void literal_dump(int, char*, unsigned int);
FILE *open_output_file(int, char*);
void close_output_file(FILE*, char*, int);
void hex_dump(unsigned int, char);
void hex_dump_flush(void);
void elf_dump(unsigned int, char);
void elf_new_section_maybe(void);
void elf_dump_out(FILE*, sym_table*);
void list_file_out(void);
void list_start_line(unsigned int, int);
void list_mid_line(unsigned int, char*, int);
void list_end_line(char*);
void list_symbols(FILE*, sym_table*);
void list_buffer_init(char*, unsigned int, int);
void list_hex(unsigned int, unsigned int, char*);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
int skip_spc(char*, int);
char *file_path(char*);
char *pathname(char*, char*);
boolean cmp_next_non_space(char*, int*, int, char);
boolean test_eol(char);
unsigned int get_identifier(char*, unsigned int, char*, unsigned int);
boolean alpha_numeric(char);
boolean alphabetic(char);
unsigned char c_char_esc(unsigned char);
int get_num(char*, int*, int*, unsigned int);
int allow_error(unsigned int, boolean, boolean);
/*----------------------------------------------------------------------------*/
/* Global variables */
instr_set instruction_set; /* ARM or Thumb */
char *input_file_name;
char *symbols_file_name;
char *list_file_name;
char *hex_file_name;
char *elf_file_name;
char *verilog_file_name;
FILE *fList, *fHex, *fElf, *fVerilog;
int symbols_stdout, list_stdout, hex_stdout, elf_stdout; /* Booleans */
int verilog_stdout;
label_sort symbols_order;
int list_sym, list_kmd;
int mnemonics_stdout;
char *mnemonics_filename = NULL;
unsigned char Verilog_array[VERILOG_MAX]; /* Assemble Verilog output in array */
unsigned int verilog_mem_size; /* Size of buffer =used= */
unsigned int sym_print_extras; /* <0> set for locals, <1> for literals */
unsigned int arm_variant;
unsigned int assembly_pointer; /* Address at which to plant instruction */
unsigned int def_increment; /* Offset reached from assembly_pointer */
boolean assembly_pointer_defined;
unsigned int entry_address;
boolean entry_address_defined;
unsigned int data_pointer; /* Used for creating record offsets etc. */
unsigned int undefined_count; /* Number of references to undefined variables */
unsigned int defined_count; /* Number of variables defined this pass */
unsigned int redefined_count; /* Number of variables redefined this pass */
unsigned int pass_count; /* Pass number, starts at 0 */
unsigned int pass_errors; /* Errors occurred in this pass */
boolean div_zero_this_pass; /* Indicates /0 in pass, prevents code dump */
boolean dump_code; /* Allow output (FALSE on last pass if error) */
own_label *evaluate_own_label; /* Yuk! @@@@@ */
/* Because evaluate needs to know if there is a local label on -current- line */
literal_record *literal_list; /* Start of the list of literals from "LDR =" */
literal_record *literal_head; /* The next literal record `expected' */
literal_record *literal_tail; /* The last literal record `dumped' */
local_label *loc_lab_list; /* Start of the list of local labels */
local_label *loc_lab_position; /* The current local label position */
size_record *size_record_list; /* Start of list of ADRL (etc.) lengths */
size_record *size_record_current; /* Current record in above list */
unsigned int size_changed_count; /* Number of `instruction' size changes */
boolean if_stack[IF_STACK_SIZE + 1]; /* Used for nesting IF clauses */
int if_SP;
unsigned int list_address;
unsigned int list_byte;
unsigned int list_line_position; /* Pos. in the src line copied to output */
char list_buffer[LIST_LINE_LENGTH];
unsigned int hex_address;
boolean hex_address_defined;
char hex_buffer[HEX_LINE_LENGTH];
int elf_section_valid; /* Flag: true if code dumped in elf_section */
unsigned int elf_section; /* Current elf section number (for labels) */
unsigned int elf_section_old;
boolean elf_new_block;
elf_temp *elf_record_list;
elf_temp *current_elf_record;
sym_table *arch_table; /* Table of possible processor architectures */
sym_table *operator_table;
sym_table *register_table;
sym_table *cregister_table;
sym_table *copro_table;
sym_table *shift_table;
/*----------------------------------------------------------------------------*/
/* Entry point */
int main(int argc, char *argv[])
{
FILE *fMnemonics, *fSource;
char c, line[LINE_LENGTH+1];
sym_table *arm_mnemonic_table, *thumb_mnemonic_table, *directive_table;
sym_table *symbol_table;
sym_table_item *arm_mnemonic_list, *thumb_mnemonic_list; /* Real lists */
int i, *j;
boolean finished, last_pass;
unsigned int error_code;
void code_pass(FILE *fHandle, char *filename)/* Recursion for INCLUDE files */
{
unsigned int line_number;
char *include_file_path; /* Path as far as directory of "filename" */
char *include_name;
FILE *incl_handle;
include_file_path = file_path(filename); /* Path to directory in use */
line_number = 1;
while (!feof(fHandle))
{
include_name = NULL; /* Don't normally return anything */
input_line(fHandle, line, LINE_LENGTH); /* Errors ignored @@@ */
if (instruction_set == THUMB)
error_code = parse_source_line(line, thumb_mnemonic_list, symbol_table,
pass_count, last_pass,
&include_name, include_file_path);
else
error_code = parse_source_line(line, arm_mnemonic_list, symbol_table,
pass_count, last_pass,
&include_name, include_file_path);
/*
printf("Hello Y %08X %s\n", symbol_table->pList[0], line);
*/
if (error_code != eval_okay)
print_error(line, line_number, error_code, filename, last_pass);
else
if (include_name != NULL)
{
char *pInclude;
if (include_name[0] == '/') pInclude = include_name; /* Absolute */
else /* Relative path - create new string */
pInclude = pathname(include_file_path, include_name); /* Add path */
if ((incl_handle = fopen(pInclude, "r")) == NULL)
{
print_error(line, line_number, SYM_NO_INCLUDE, filename, last_pass);
fprintf(stderr, "Can't open \"%s\"\n", include_name);
finished = TRUE;
}
else
{
code_pass(incl_handle, pInclude);
fclose(incl_handle); /* Doesn't leave file locked @@@ */
}
if (pInclude != include_name) free(pInclude); /* If allocated (yuk) */
free(include_name);
}
line_number++; /* Local to file */
}
free(include_file_path);
return;
}
/* Create and initialise a symbol table */
sym_table *build_table(char *table_name, unsigned int flags,
char **sym_names, int *values)
{
sym_table *table;
int i;
sym_record *dummy; /* Don't want returned pointer */
table = sym_create_table(table_name, flags);
for (i = 0; *(sym_names[i]) != '\0'; i++) /* repeat until "" found */
sym_define_label(sym_names[i], values[i], 0, table, &dummy);
return table;
}
arm_mnemonic_list = NULL;
thumb_mnemonic_list = NULL;
symbols_file_name = ""; /* Defaults */
list_file_name = "";
hex_file_name = "";
elf_file_name = "";
verilog_file_name = "";
symbols_stdout = FALSE;
list_stdout = FALSE;
hex_stdout = FALSE;
elf_stdout = FALSE;
verilog_stdout = FALSE;
verilog_mem_size = VERILOG_MAX; /* Default to maximum size */
if (set_options(argc, argv))/* Parse command line and set options accordingly */
{ /* We have a source file name, at least! */
char *pChar, full_name[200]; // Size? @@
/* Set up tables of operators, etc. */
{ /* Architecture names */
char *arch_name[] = { "v3", "v3m", "v4", "v4xm", "v4t", "v4txm",
"v5", "v5xm", "v5t", "v5txm", "v5te","v5texp",
"all", "any", "" };
int arch_value[] = { v3, v3M, v4, v4xM, v4T, v4TxM,
v5, v5xM, v5T, v5TxM, v5TE, v5TExP,
0, 0, -1 };
arch_table = build_table("Architectures", SYM_TAB_CASE_FLAG,
arch_name, arch_value);
}
{ /* Diadic expression operator definitions */
char *op_name[] = { "and", "or", "xor", "eor",
"shl", "lsl", "shr", "lsr",
"div", "mod", "eq", "ne",
"lo", "ls", "hi", "hs",
"lt", "le", "gt", "ge",
"" };
int op_value[] = { AND, OR, XOR, XOR,
LEFT_SHIFT, LEFT_SHIFT, RIGHT_SHIFT, RIGHT_SHIFT,
DIVIDE, MODULUS, EQUALS, NOT_EQUAL,
LOWER_THAN, LOWER_EQUAL, HIGHER_THAN, HIGHER_EQUAL,
LESS_THAN, LESS_EQUAL, GREATER_THAN, GREATER_EQUAL,
-1 };
operator_table = build_table("Operators", SYM_TAB_CASE_FLAG,
op_name, op_value);
}
{ /* Register name definitions */
char *reg_name[] = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"sp", "lr", "pc",
"a1", "a2", "a3", "a4", "v1", "v2", "v3", "v4",
"v5", "sb", "v6", "sl", "v7", "fp", "ip",
"" };
int reg_value[] = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 9, 10, 10, 11, 12,
-1 };
register_table = build_table("Registers", SYM_TAB_CASE_FLAG,
reg_name, reg_value);
}
{ /* Coprocessor register name definitions */
char *creg_name[] = {"cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
"cr8", "cr9","cr10","cr11","cr12","cr13","cr14","cr15",
"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7",
"c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15",""};
int creg_value[] = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, -1};
cregister_table = build_table("Copro_Registers", SYM_TAB_CASE_FLAG,
creg_name, creg_value);
}
{ /* Coprocessor name definitions */
char *copro_name[] = {"p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
"p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
"cp0", "cp1", "cp2", "cp3", "cp4", "cp5", "cp6", "cp7",
"cp8", "cp9","cp10","cp11","cp12","cp13","cp14","cp15",
"" };
int copro_value[] = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
-1 };
copro_table = build_table("Coprocessors", SYM_TAB_CASE_FLAG,
copro_name, copro_value);
}
{ /* Shift definitions */
char *shift_name[] = {"lsl", "asl", "lsr", "asr", "ror", "rrx", ""};
int shift_value[] = { 0, 0, 1, 2, 3, 7, -1};
shift_table = build_table("Shifts",SYM_TAB_CASE_FLAG,shift_name,shift_value);
}
arm_mnemonic_table = sym_create_table("ARM Mnemonics", SYM_TAB_CASE_FLAG);
thumb_mnemonic_table = sym_create_table("Thumb Mnemonics", SYM_TAB_CASE_FLAG);
directive_table = sym_create_table("Directives", SYM_TAB_CASE_FLAG);
if (mnemonics_filename == NULL) {
/* Following is crude hack for test/commissioning purposes. @@@@@ */
realpath(argv[0], full_name); // full path to binary (?) @@
for (pChar = full_name; *pChar != '\0'; pChar++); // find end of string @@
while (*pChar != '/') pChar--; // Cut off last element
pChar[1] = '\0'; // Terminate
strcat(full_name, "mnemonics"); // Then append filename
} else {
// Mnemonics file specified manually, use it instead of trying to find it
strncpy(full_name, mnemonics_filename, 200);
}
if ((fMnemonics = fopen(full_name, "r")) == NULL) /* Read mnemonics */
fprintf(stderr, "Can't open %s\n", "mnemonics");
else
{
while (!feof(fMnemonics))
{
input_line(fMnemonics, line, LINE_LENGTH); /* Errors ignored @@@ */
if (!parse_mnemonic_line(line, arm_mnemonic_table, thumb_mnemonic_table,
directive_table))
fprintf(stderr, "Mnemonic file error\n %s\n", &line[0]);
}
/* no error checks @@@ */
fclose(fMnemonics);
{ /* Make up mnemonic table lists */
sym_table_item *pMnem, *pDir;
pMnem = (sym_table_item*) malloc(SYM_TABLE_ITEM_SIZE); /* ARM defns. */
pDir = (sym_table_item*) malloc(SYM_TABLE_ITEM_SIZE); /* (hand built) */
pMnem->pTable = arm_mnemonic_table;
pMnem->pNext = pDir;
pDir->pTable = directive_table;
pDir->pNext = NULL;
arm_mnemonic_list = pMnem;
pMnem = (sym_table_item*) malloc(SYM_TABLE_ITEM_SIZE); /* Thumb defns. */
pDir = (sym_table_item*) malloc(SYM_TABLE_ITEM_SIZE); /* (hand built) */
pMnem->pTable = thumb_mnemonic_table;
pMnem->pNext = pDir;
pDir->pTable = directive_table;
pDir->pNext = NULL;
thumb_mnemonic_list = pMnem;
}
symbol_table = sym_create_table("Labels", 0);/* Labels are case sensitive */
literal_list = NULL;
loc_lab_list = NULL;
size_record_list = NULL;
pass_count = 0;
finished = FALSE;
last_pass = FALSE;
dump_code = FALSE;
fHex = open_output_file( hex_stdout, hex_file_name); /* Open required */
fList = open_output_file(list_stdout, list_file_name); /* output files */
fElf = open_output_file( elf_stdout, elf_file_name);
fVerilog = open_output_file(verilog_stdout, verilog_file_name);
if ((fList != NULL) && list_kmd) fprintf(fList, "KMD\n"); /* KMD marker */
if ((fSource = fopen(input_file_name, "r")) == NULL) /* Read file in */
{
fprintf(stderr,"Can't open %s\n", input_file_name);
finished = TRUE;
}
else
finished = FALSE;
if (fVerilog != NULL)
{
int i;
for (i = 0; i < verilog_mem_size; i++) Verilog_array[i] = 0;
}
while (!finished)
{
instruction_set = ARM; /* Default */
assembly_pointer = 0; /* Default */
data_pointer = 0;
entry_address = 0;
assembly_pointer_defined = TRUE; /* ??? @@@@ Okay for us! */
entry_address_defined = FALSE;
arm_variant = 0; /* Default to any ARM architecture */
pass_errors = 0;
div_zero_this_pass = FALSE;
hex_address_defined = FALSE;
elf_new_block = TRUE;
undefined_count = 0; /* Reads of undefined variables on this pass */
defined_count = 0; /* Labels newly defined on this pass */
redefined_count = 0; /* Labels with values changed on this pass */
if_SP = 0;
if_stack[0] = TRUE;
elf_section_valid = FALSE; /* No bytes dumped yet */
elf_section = 1;
literal_head = NULL;
literal_tail = NULL;
loc_lab_position = NULL;
size_record_current = size_record_list; /* Go to front of list */
size_changed_count = 0;
rewind(fSource); /* Ensure at start of file */
code_pass(fSource, input_file_name);
/* no error checks @@@ */
if (literal_tail != literal_head) /* Clear the literal pool */
{
char *literals = "Remaining literals";
if (fList != NULL) list_start_line(assembly_pointer, FALSE);
literal_dump(last_pass, literals, 0); /* Much like an instruction */
if (fList != NULL) list_end_line(literals);
}
hex_dump_flush(); /* Ensure buffer is cleared */
// printf("Pass %2d complete. ", pass_count);
// printf("Label changes: defined %3d; ", defined_count);
// printf("values changed %3d; ", redefined_count);
// printf("read while undefined %3d;\n", undefined_count);
//printf("\n");
//printf("Pass %2d complete: %d sizes changed.\n", pass_count, size_changed_count);
if (pass_errors != 0)
{
finished = TRUE;
printf("Pass %2d: terminating due to %d error", pass_count,
pass_errors);
if (pass_errors == 1) printf("\n"); else printf("s\n");
}
else
{
if (last_pass || (pass_count > MAX_PASSES)) finished = TRUE;
else
{
if ((defined_count==0)&&(redefined_count==0)&&(undefined_count==0))
{
last_pass = TRUE; /* One more time ... */
dump_code = !div_zero_this_pass; /* If error don't plant code */
}
pass_count++;
}
}
if (if_SP != 0)
{
printf("Pass completed with IF clause still open; terminating\n");
finished = TRUE;
}
} /* End of WHILE */
if (fSource != NULL) fclose(fSource);
if ((fList != NULL) && list_sym) list_symbols(fList, symbol_table);
/* Symbols into list file */
if (fVerilog != NULL) /* Dump memory image to hex file */
{
int i;
for (i = 0; i < verilog_mem_size; i++)
{
fprintf(fVerilog, "%02X", Verilog_array[i^3]); /* Big endian */
if ((i & 3) == 3) fprintf(fVerilog, "\n");
}
}
close_output_file(fList, list_file_name, pass_errors != 0);
close_output_file(fHex, hex_file_name, pass_errors != 0);
close_output_file(fVerilog, verilog_file_name, pass_errors != 0);
if (fElf != NULL) elf_dump_out(fElf, symbol_table); /* Organise & o/p ELF */
if (pass_count > MAX_PASSES)
{
printf("Couldn't do it ... fed up!\n\n");
printf("Undefined labels:\n");
sym_print_table(symbol_table, UNDEFINED, ALPHABETIC, TRUE, "");
}
else
{
if (symbols_stdout || (symbols_file_name[0] != '\0'))
{
sym_print_table(symbol_table, ALL, symbols_order, symbols_stdout,
symbols_file_name);
if (!symbols_stdout) printf("Symbols in file: %s\n", symbols_file_name);
}
if (pass_errors == 0)
{
if (list_file_name[0]!='\0') printf("List file in: %s\n",list_file_name);
if (hex_file_name[0] !='\0') printf("Hex dump in: %s\n", hex_file_name);
if (elf_file_name[0] !='\0') printf("ELF file in: %s\n", elf_file_name);
if (verilog_file_name[0] !='\0')
{
printf("Verilog file in: %s", verilog_file_name);
printf(" size: %X (decimal %d) words\n", verilog_mem_size,
verilog_mem_size);
}
}
else printf("No output generated.\n"); /* Errors => trash output files */
if (pass_count == 1) printf("\n1 pass performed.\n");
else printf("\nComplete. %d passes performed.\n", pass_count);
}
{ /* Free local label list */
local_label *pTemp;
while ((pTemp = loc_lab_list) != NULL) /* Syntactically grubby! */
{
loc_lab_list = loc_lab_list->pNext;
free(pTemp);
}
}
{ /* Free literal list */
literal_record *pTemp;
while ((pTemp = literal_list) != NULL) /* Syntactically grubby! */
{
literal_list = literal_list->pNext;
free(pTemp);
}
}
{ /* Free size list */
size_record *pTemp;
while ((pTemp = size_record_list) != NULL) /* Syntactically grubby! */
{
size_record_list = size_record_list->pNext; /* Cut out first record */
free(pTemp); /* and delete it */
}
}
{ /* Clear away lists of symbol tables */
sym_table_item *p1, *p2;
p1 = arm_mnemonic_list;
while (p1 != NULL) { p2 = p1; p1 = p1->pNext; free(p2); }
p1 = thumb_mnemonic_list;
while (p1 != NULL) { p2 = p1; p1 = p1->pNext; free(p2); }
}
sym_delete_table( symbol_table, FALSE);
sym_delete_table( directive_table, FALSE);
sym_delete_table( arm_mnemonic_table, FALSE);
sym_delete_table(thumb_mnemonic_table, FALSE);
sym_delete_table( arch_table, FALSE);
sym_delete_table( operator_table, FALSE);
sym_delete_table( register_table, FALSE);
sym_delete_table( cregister_table, FALSE);
sym_delete_table( copro_table, FALSE);
sym_delete_table( shift_table, FALSE);
}
}
else
printf("No input file specified\n");
if (pass_errors == 0) exit(0);
else exit(-1);
}
/*----------------------------------------------------------------------------*/
/* // Allow omission of spaces? @@@@
// Allow filename first ? @@@@*/
boolean set_options(int argc, char *argv[])
{
void file_option(int *std_out, char **filename, char *err_mss)
{
if (argc > 2)
{
if ((argv[1])[0] == '-') *std_out = TRUE;
else { *filename = &(*argv[1]); argc --; argv++; }
}
else printf("%s filename omitted\n", err_mss);
return;
}
boolean okay;
char c;