-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrm.c
3047 lines (2497 loc) · 72.7 KB
/
nrm.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
/////////////////////////////////// NRM ////////////////////////////////////////
///////////////////////////// NEW ARM ASSEMBLER ////////////////////////////////
////////////////////////// NANCY'S ARM ASSEMBLER ///////////////////////////////
/*
Public Domain (CC0) ARM Assembler.
Free as in actual freedom.
License: https://creativecommons.org/publicdomain/zero/1.0/
GOALS:
- ObjAsm compatibility
- Portability (across 32-bit CPUs)
- Simplicity
- Speed (for JIT use)
TODO:
- ROUT and local labels, which are useful for macros
- handle |labels| and |variables|
- Consult ObjAsm if S bit should be generated with CMP, CMN, TST and TEQ
also check what CMPS generates with ObjAsm
- STM/LDM can't have PC as a base or have empty {}
- STM/LDM after Arch >= 7 doesn't allow writeback into the register that appear in {}
- replace `strtol` with proper readers
- consult ObjAsm on if label without anything else on a line should
be passed to a macro or EQU style directives
- rest of the {VARS}
NOTES:
- With 2-pass assembler there are two approaches:
- 1st pass generates the code, while also storing label positions.
2nd pass patches it.
- 1st pass goes over the code, calculating label positions.
2nd pass generates actual code.
- APCS versions:
https://ext.3dodev.com/3DO/Portfolio_2.5/OnLineDoc/DevDocs/tktfldr/atsfldr/4atsb.html#XREF35590
*/
//////////////////////////// NRM CONFIGURATION /////////////////////////////////
//uncomment to create a standalone assembler
#define NRM_STANDALONE
//ObjAsm set it to 256
#define NRM_MAX_MACRO_DEPTH 512
//////////////////////////// STANDARD INCLUDES /////////////////////////////////
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
/////////////////////////// OBSCURE INCLUDES ///////////////////////////////////
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
//NOTE: SH tables by default expect user to do strdup
#define arrdup(xs) ((xs) ? \
(void*)((uint8_t*)memcpy(malloc(sizeof(*stbds_header(xs)) \
+ stbds_header(xs)->capacity*sizeof(xs[0])) \
, stbds_header(xs) \
,(sizeof(*stbds_header(xs)) \
+ stbds_header(xs)->capacity*sizeof(xs[0])) \
)+sizeof(*stbds_header(xs))) \
: 0)
#define alen(a) arrlen(a)
#define aput(a,v) arrput(a,v)
#define apop(a) arrpop(a)
#define alast(a) arrlast(a)
#define adup(a) arrdup(a)
//NOTE: the bellow macro doesn't add trailing 0
#define aputs(a,b) for(char *p_ = b; *p_; p_++) aput(a,*p_)
////////////////////////// GENERIC DECLARATIONS ////////////////////////////////
#define U8 uint8_t
#define U16 uint16_t
#define U32 uint32_t
#define S8 int8_t
#define S16 int16_t
#define S32 int
#define F32 float
#define F64 double
#define U64 uint64_t
#define S64 int64_t
#define PUTW(p,w) do { \
(p)[0] = (w) &0xFF; \
(p)[1] = ((w)>> 8)&0xFF; \
(p)[2] = ((w)>>16)&0xFF; \
(p)[3] = (w)>>24; \
} while (0)
#define INTRODUCE(type) struct type; typedef struct type type;
INTRODUCE(nrm_t)
#define TCTX nrm_t
#define CTX TCTX *this
#define C (*this)
#define FLM (&this->flm)
/////////////////////////////// UTILITIES //////////////////////////////////////
static int nrm_dbg = 0;
//file end
#define ISFLE(x) ((x) == FLE)
#define ISWS(x) ((x) == ' ' || (x) == '\t')
#define SKPWS(p) while (ISWS(nx)) rd()
//locally defined terminating characater
#define ISTC(x) ((x) == tc)
static void upcase(char *d, char *s) {
while ((*d++ = toupper(*s++)));
}
static char *_hidden_cat_(char *a0, ...) {
char *a;
va_list ap;
int size = strlen(a0);
va_start(ap, a0);
for (;;) {
a = va_arg(ap, char*);
if (!a) break;
size += strlen(a);
}
va_end(ap);
size += 1;
char *o = malloc(size);
char *p = o;
a = a0;
while (*a) *p++ = *a++;
va_start(ap, a0);
for (;;) {
a = va_arg(ap, char*);
if (!a) break;
while (*a) *p++ = *a++;
}
va_end(ap);
*p++ = 0;
return o;
}
#define cat(...) _hidden_cat_(__VA_ARGS__, 0)
////////////////////////// MEMORY MANAGEMENT ///////////////////////////////////
///////////////////////////
//local heap, allows evading malloc where possible
typedef struct {
char *p;
char *e;
} lhp__t;
#define LHP_RESET() do {lhp_->p = lhpd_;} while (0)
#define LHP(TSZ) \
char lhpd_[TSZ]; lhp__t lhpb_; \
lhp__t *lhp_ = &lhpb_; \
lhp_->p = lhpd_; \
lhp_->e = lhpd_ + TSZ;
#define LHPI lhp__t *lhp_
#define LHPA lhp_
#define LHPTR lhp_->p
#define LHPUT(v) do {if (lhp_->p == lhp_->e) fatal("line is too long."); \
*lhp_->p++ = (v); } while (0)
#define LHPUTS(s) do { for (char *p = (s); *p; *p++) LHPUT(*p); } while (0)
//read characters by calling rd(), while tst(nx) is true
//assumes hp points to a temporary heap buffer and he points to its end
#define RDS(dst,tst) do { \
char *hp_ = lhp_->p; \
char *he_ = lhp_->e; \
dst = hp_; \
while (tst(nx)) { \
if (nx == FLE) fatal("unexpected EOF"); \
if (hp_ == he_) fatal("line is too long."); \
*hp_++ = rd(); \
} \
*hp_++ = 0; \
lhp_->p = hp_; \
} while (0)
#define SKP(tst) do { \
while (tst(nx)) { \
if (nx == FLE) fatal("unexpected EOF"); \
rd(); \
} \
} while (0)
////////////////////////////////// URL /////////////////////////////////////////
typedef struct {
char *dir;
char *name;
char *ext;
} url_t;
static url_t *url_parts(char *path) {
int bufsz = strlen(path)+3;
url_t *fn = malloc(sizeof(url_t)+bufsz);
char *r = (char*)(fn+1);
char *dir;
char *name;
char *ext;
char *p, *q;
int l;
if ((p = strrchr(path, '/'))) {
l = p-path + 1;
dir = r;
strncpy(dir, path, l);
dir[l] = 0;
p++;
r += l+1;
} else {
p = path;
dir = r;
*r++ = 0;
}
if ((q = strrchr(p, '.'))) {
l = q-p;
name = r;
strncpy(name, p, l);
name[l] = 0;
q++;
r += l+1;
ext = r;
l = strlen(q);
strcpy(ext, q);
} else {
ext = r;
*r++ = 0;
name = r;
strcpy(name, p);
}
fn->dir = dir;
fn->name = name;
fn->ext = ext;
return fn;
}
/////////////////////// FILE MANAGER DECLARATIONS //////////////////////////////
/*
Macro processors work by repeatedly rewriting the input file.
They do that top-to-bottom, left-to-right.
This file manager flm_t facilitates that.
The idea is to keep flp_t.val first item on the context pointer.
That way nx will result into a single pointer dereference.
rdF() does some row/column house-keeping for the user code.
*/
//File end
#define FLE -1
/////////////////////////
// mfl_t.desc flags
//file's data array is owned by us and have to be freed
#define MFL_OWNED 0x001
//macro buffer
#define MFL_MACRO 0x002
//has an associated filesystem
#define MFL_FS 0x004
//the file won't be freed when nref hits 0
#define MFL_RETAIN 0x008
//root file
#define MFL_ROOT 0x010
INTRODUCE(flm_t)
INTRODUCE(flp_t)
INTRODUCE(mfl_t)
INTRODUCE(flmp_t)
struct mfl_t { //memory held file
U32 desc; //file description
U8 *base; //start of the data
U32 size; //length of the file
char *name; //name of the file
url_t *url; //path to the source
int nrefs; //number of references to this file
//if it gets 0, we we can free it.
mfl_t *ovrd; //this entry overrides another in the file table
flm_t *flm; //associated file manager
};
struct flp_t { //file pointer
int val; //current value
U8 *ptr; //current location inside the file
U8 *end; //end of the file
U8 *line; //start of the current line (used to calculate column)
U32 row; //line counter
mfl_t *file; //the file handle
//flp_t *prev; //OBSOLETE: for reading files which include other files
//use nrm_t->fstack instead,
//since one file can be opened several times
U8 *shd; // shadow copy
};
static mfl_t *new_mfl(char *name, U8 *base, U32 size) {
mfl_t *f = malloc(sizeof(mfl_t));
f->name = strdup(name);
f->base = base;
f->size = size;
f->nrefs = 0;
f->desc = 0;
f->url = 0;
f->ovrd = 0;
return f;
}
static void del_mfl(mfl_t *f) {
if (f->url) free(f->url);
free(f->name);
if (f->desc&MFL_OWNED) arrfree(f->base);
free(f);
}
static void flm_location(flm_t *this, flmp_t *p);
static void flm_include(flm_t *this, char *name, U8 *base,U32 size, U32 flags);
static void flm_conclude(flm_t *this);
static int flp_next(flp_t *f) { return f->ptr==f->end ? FLE : *f->ptr; }
static int flp_read(flp_t *f) {
int ch = f->val;
if (f->shd) *f->shd++ = ch; //does a shadow copy
if (++f->ptr < f->end) {
if (ch == '\n') { f->row++; f->line = f->ptr; }
f->val = *f->ptr;
} else { //file end
if (f->ptr-1 != f->end) flm_conclude(f->file->flm);
else --f->ptr;
}
return ch;
}
static int flp_nextI(flp_t *f, int i) {
if (f->ptr+i < f->end) return f->ptr[i];
U8 *r = 0;
int j;
for (j = i; j > 0; j--) {
int v = flp_read(f);
if (v == FLE) break;
aput(r, v);
}
if (j != i) flm_include(f->file->flm, "<peek>", r, alen(r), MFL_OWNED);
return j ? FLE : f->ptr[i];
}
static flp_t *new_flp(mfl_t *f) {
flp_t *fp = malloc(sizeof(flp_t));
fp->file = f;
fp->ptr = f->base;
fp->end = f->base + f->size;
fp->line = fp->ptr;
fp->row = 0;
fp->shd = 0;
fp->val = flp_next(fp);
return fp;
}
static void del_flp(flp_t *fp) {
free(fp);
}
static void mfl_ref(mfl_t *f) {f->nrefs++;}
static void mfl_unref(mfl_t *f) {
if (!(--f->nrefs || (f->desc&MFL_RETAIN))) del_mfl(f);
}
struct flm_t { //file manager
flp_t flp; //current file
TCTX *ctx; //context/parent object, using filemanager
struct { char *key; mfl_t *value; } *ft; //file table
//a stack of currently processed files (beside the flp)
flp_t **fstack;
flp_t *tflp_stack; //used for temporary redefining input
char **paths; //include directories
int row; // global row
};
struct flmp_t {
int row;
int col;
char *macroname;
char *filename;
};
#define cflp (&FLM->flp)
//next value peek
#define nx cflp->val
//next ith value peek
#define ith(i) flp_nextI(cflp, i)
//read char
#define rd() flp_read(cflp)
//FIXME: the below macros will fail with windowed files
//these are for temporary redefining input
#define cflp_push(tflp) \
aput(FLM->tflp_stack, *cflp); \
cflp = *tflp;
#define cflp_pop() cflp = apop(FLM->tflp_stack)
#define cflp_shd(s) do { cflp->shd = s; } while (0)
//FIXME: handle unicode
#define FL_COL(f) ((f)->ptr - (f)->line)
#define cflp_saved() FLM->fstack[alen(FLM->fstack)-1]
#define cflp_save() do {*cflp_saved() = *cflp;} while (0)
#define cflp_load() do {*cflp = *cflp_saved();} while (0)
#define FLLOC(R,C,N,F) int R = (F)->row; int C = FL_COL(F); \
char *N = (F)->file->name;
//////////////////////////// SYMBOL TABLE //////////////////////////////////////
//////////////////
// SYMBOL TYPES
//none
#define VAL_NON 0x00
//register
#define VAL_REG 0x01
//macro
#define VAL_MACRO 0x02
//macro constant
#define VAL_EQU 0x04
//macro constant
#define VAL_LBL 0x05
#define VAL_LOGIC 0x06
#define VAL_ARITH 0x07
#define VAL_STRING 0x08
#define VAL_SCPNFO 0x09
//ObjAsm apparently uses AREA as a synonym for namespace
typedef struct {
char *name;
} area_t;
typedef struct { //scope info
int up;
int id;
int lv;
} scpnfo_t;
typedef union { //note that ARM word is 32bit, compared to x86's 16bit
int i;
U8 b; //byte
U16 h; //half-word
U32 w;
U32 d; //dword
U64 q; //qword
S8 sb; //byte
S16 sh; //half-word
S32 sw;
S32 sd; //dword
S64 sq; //qword
char *s;
void *v;
scpnfo_t si;
} val_t;
struct sym_t;
typedef struct sym_t sym_t;
struct sym_t {
char *name; //index inside the name table
U32 desc; //description of the symbol
val_t v; //value of the symbol
int row;
sym_t *next; //allows several symbols to share a name inside a scope
int rout; //rout this symbol belongs to
};
typedef struct { char *key; sym_t *value; } *scope_t;
#define SYM_TYPE(s) ((s)->desc&0xff)
////////////////////// CONDITION CODES /////////////////////////////////////////
// Equal / Zero: Z == 1
#define NRM_EQ 0x0
// Not Equal / Not Zero: Z = 0
#define NRM_NE 0x1
//HS/CS - Carry Set / Unsigned Higher or Same: C == 1
#define NRM_CS 0x2
//LO/CC - Carry Clear / Unsigned Lower: C == 0
#define NRM_CC 0x3
// Minus / Negative: N==1
#define NRM_MI 0x4
// Plus / Positive or Zero: N==0
#define NRM_PL 0x5
// Overflow Set: V==1
#define NRM_VS 0x6
// Overflow Clear: V==0
#define NRM_VC 0x7
// Unsigned Higher: C == 1 && Z == 0
#define NRM_HI 0x8
// Unsigned Lower or Same: C == 0 || Z == 1
#define NRM_LS 0x9
// Greater or Equal: N == V
#define NRM_GE 0xA
// Less Than: N != V
#define NRM_LT 0xB
// Greater Than: Z == 0 && N == V
#define NRM_GT 0xC
// Less or Equal: Z == 1 || N != V
#define NRM_LE 0xD
// Always (unconditional)
#define NRM_AL 0xE
// Never (unconditionally false)
#define NRM_NV 0xF
//////////////////////////// OPERATION CODES ///////////////////////////////////
/////////////////////
// Legend:
// Rd - destionation
// Rn - 1st operand
// Rm - 2nd operand
//Rd = Rn & Rm
#define NRM_AND (0x0<<21)
//Rd = Rn ^ Rm
#define NRM_EOR (0x1<<21)
//Rd = Rn - Rm
#define NRM_SUB (0x2<<21)
//Rd = Rm - Rn
#define NRM_RSB (0x3<<21)
//Rd = Rn + Rm
#define NRM_ADD (0x4<<21)
//Rd = Rn + Rm + C
#define NRM_ADC (0x5<<21)
//Rd = Rn - Rm - C
#define NRM_SBC (0x6<<21)
//Rd = Rm - Rn - C
#define NRM_RSC (0x7<<21)
//NZCV <- Rn & Rm
//S should be always 1
#define NRM_TST (0x8<<21)
//NZCV <- Rn ^ Rm
//S should be always 1
#define NRM_TEQ (0x9<<21)
//NZCV <- Rn - Rm
//S should be always 1
#define NRM_CMP (0xA<<21)
//NZCV <- Rn + Rm (compare negated)
//S should be always 1
#define NRM_CMN (0xB<<21)
//Rd = Rn | Rm
#define NRM_ORR (0xC<<21)
//Rd = Rm (Rn is ignored)
#define NRM_MOV (0xD<<21)
//Rd = Rn & ~Rm (bit clear)
#define NRM_BIC (0xE<<21)
//Rd = ~Rm (Rn is ignored)
#define NRM_MVN (0xF<<21)
//store
#define NRM_STR (0x2<<25)
//load
#define NRM_LDR ((0x2<<25)|NRM_STLD_LD)
//store
#define NRM_STM (0x4<<25)
//load
#define NRM_LDM ((0x4<<25)|NRM_STLD_LD)
//branch
#define NRM_B (0x5<<25)
//branch and link
#define NRM_BL ((0x5<<25)|(1<<24))
//software interrupt
#define NRM_SWI ((0x7<<25)|(1<<24))
#define NRM_MAX_SWI_INDEX 0xFFFFFF
//for S suffixed opcodes
#define NRM_S (1<<20)
#define NRM_L (1<<24)
//immediate operand
#define NRM_I (1<<25)
//load flag for ST/LD opcode
#define NRM_STLD_LD (1<<20)
//save incremented offset back after ST/LD
#define NRM_WRITEBACK (1<<21)
//preicrement Rn (base register), before STR/LDR
#define NRM_PRE (1<<24)
//increment up
#define NRM_UP (1<<23)
#define NRM_STLD_BYTE (1<<22)
#define NRM_STLD_TRANS (NRM_WRITEBACK|1)
//that is for exiting service mode
#define NRM_LOAD_PSR_FORCE_USER_MODE (1<<22)
#define NRM_NONE 0xFFFFFFFF
#define NRM_ERROR 0xFFFFFFFE
#define NRM_EOF 0xFFFFFFFD
//data processing register
#define NRM_CLS_DPR 0x0
//data processing immediate
#define NRM_CLS_DPI 0x1
//load/store immediate offset
#define NRM_CLS_STLDI 0x2
//load/store register offset
#define NRM_CLS_STLDR 0x3
//load/store multiple
#define NRM_CLS_STLDM 0x4
//branch
#define NRM_CLS_B_BL 0x5
#define NRM_CLS_SWI 0x7
#define NRM_CND(dsc) ((dsc)>>28)
#define NRM_OPC(dsc) ((dsc)&(0xF<<21))
#define NRM_IS_CTDP(x) ((x)==NRM_CMP || (x)==NRM_CMN || \
(x)==NRM_TST || (x)==NRM_TEQ)
#define NRM_IS_MOV(x) ((x)==NRM_MOV || (x)==NRM_MVN)
//instruction class
#define NRM_CLS(dsc) (((dsc)>>25)&7)
#define NRM_IS_DP(x) (NRM_CLS(x) == NRM_CLS_DPR || NRM_CLS(x) == NRM_CLS_DPI)
#define NRM_IS_STLD(x) \
(NRM_CLS(x) == NRM_CLS_STLDI || NRM_CLS(x) == NRM_CLS_STLDR)
#define NRM_IS_BRANCH(x) (NRM_CLS(x) == NRM_CLS_B_BL)
#define NRM_IS_STLDM(x) (NRM_CLS(x) == NRM_CLS_STLDM)
/////////////////////////// BIT MANIPULATION ///////////////////////////////////
static U32 nrm_ror(U32 x, U32 n) { //32bit rotate right
return (x >> n) | (x << (32 - n));
}
static U32 nrm_rol(U32 x, U32 n) { //32bit rotate left
return (x << n) | (x >> (32 - n));
}
#define NRM_BAD_IMM 0xFFFFFFFF
U32 nrm_enc_imm(U32 imm) { //encode immediate as a RORed value
U32 v = imm;
for (U32 s = 0; s < 31; s += 2) {
v = nrm_rol(imm, s);
if (v < 256) {
//printf("v=%d s=%d\n", v, s);
return v | (s<<7) | NRM_I;
}
}
return NRM_BAD_IMM;
}
U32 nrm_dec_imm(U32 opcode) {//decode immediate given ARM opcode
U32 v = opcode & 0xff;
U32 r = ((opcode >> 8) & 0xf) << 1;
return nrm_ror(v, r);
}
//////////////////////////// GLOBAL DATA ///////////////////////////////////////
typedef struct {
char *key;
int32_t value;
} ki_t; //key to integer value
typedef struct {
char *key;
U32 value;
} ku_t; //key to integer value
static ki_t base_regs[] = {
{ "r0", 0},{ "r1", 1},{ "r2", 2},{ "r3", 3},
{ "r4", 4},{ "r5", 5},{ "r6", 6},{ "r7", 7},
{ "r8", 8},{ "r9", 9},{"r10",10},{"r11",11},
{"r12",12},{"r13",13},{"r14",14},{"r15",15},
{ "R0", 0},{ "R1", 1},{ "R2", 2},{ "R3", 3},
{ "R4", 4},{ "R5", 5},{ "R6", 6},{ "R7", 7},
{ "R8", 8},{ "R9", 9},{"R10",10},{"R11",11},
{"R12",12},{"R13",13},{"R14",14},{"R15",15},
{ "sp",13},{ "SP",13},{ "lr",14},{ "LR",14},
{ "pc",15},{ "PC",15},
{0,0}
};
static ki_t apcs_regs[] = { //RISC OS C ABI
//objasm v3.27 apparently only includes the lower case names
{"a1", 0}, {"a2", 1}, {"a3", 2}, {"a4", 3},
{"v1", 4}, {"v2", 5}, {"v3", 6}, {"v4", 7},
{"v8", 8}, {"v6", 9}, {"sl",10}, {"fp",11},
{"ip",12}, {"sp",13}, {"lr",14}, {"pc",15},
{0,0}
};
static ku_t cnds[] = {
{"EQ", NRM_EQ}, {"NE", NRM_NE}, {"CS", NRM_CS}, {"CC", NRM_CC},
{"MI", NRM_MI}, {"PL", NRM_PL}, {"VS", NRM_VS}, {"VC", NRM_VC},
{"HI", NRM_HI}, {"LS", NRM_LS}, {"GE", NRM_GE}, {"LT", NRM_LT},
{"GT", NRM_GT}, {"LE", NRM_LE}, {"AL", NRM_AL}, {"NV", NRM_NV},
{"HS", NRM_CS}, {"LO", NRM_CC},
{0,0}
};
static ku_t opcs[] = {
{"AND", NRM_AND}, {"EOR", NRM_EOR}, {"SUB", NRM_SUB}, {"RSB", NRM_RSB},
{"ADD", NRM_ADD}, {"ADC", NRM_ADC}, {"SBC", NRM_SBC}, {"RSC", NRM_RSC},
{"TST", NRM_TST|NRM_S}, {"TEQ", NRM_TEQ|NRM_S},
{"CMP", NRM_CMP|NRM_S}, {"CMN", NRM_CMN|NRM_S},
{"ORR", NRM_ORR}, {"MOV", NRM_MOV}, {"BIC", NRM_BIC}, {"MVN", NRM_MVN},
{"LDR", NRM_LDR}, {"STR", NRM_STR},
{"LDM", NRM_LDM}, {"STM", NRM_STM},
{"B" , NRM_B }, {"BL" , NRM_BL },
{"SWI", NRM_SWI},
{0,0}
};
#define NRM_S_LSL 0
#define NRM_S_LSR 1
#define NRM_S_ASR 2
#define NRM_S_ROR 3
#define NRM_S_RRX 4
#define NRM_S_NIL 5
static ku_t sfts[] = {
{"LSL", NRM_S_LSL}, {"ASL", NRM_S_LSL},
{"LSR", NRM_S_LSR}, {"ASR", NRM_S_ASR},
{"ROR", NRM_S_ROR}, {"RRX", NRM_S_RRX},
{0,0}
};
enum { NRM_D_FIRST_DRC=0,
NRM_D_ASSERT,
NRM_D_OPT, NRM_D_TTL, NRM_D_SUBT,
NRM_D_ORG, NRM_D_LTORG, NRM_D_AREA, NRM_D_ENTRY, NRM_D_ROUT,
NRM_D_END, NRM_D_KEEP, NRM_D_GET,
NRM_D_IMPORT, NRM_D_EXPORT,
NRM_D_EQU,
NRM_D_GBLA, NRM_D_GBLL, NRM_D_GBLS,
NRM_D_LCLA, NRM_D_LCLL, NRM_D_LCLS,
NRM_D_SETA, NRM_D_SETL, NRM_D_SETS,
NRM_D_RN, NRM_D_FN, NRM_D_CP, NRM_D_CN,
NRM_D_DCB, NRM_D_DCW, NRM_D_DCD, NRM_D_DCZ, NRM_D_DCFS, NRM_D_DCFD,
NRM_D_ALIGN, NRM_D_SMORG, NRM_D_RESERVE,
NRM_D_NOFP, NRM_D_RLIST,
NRM_D_IF, NRM_D_ELSE, NRM_D_ENDIF,
NRM_D_WHILE, NRM_D_WEND,
NRM_D_MACRO, NRM_D_MEND, NRM_D_MEXIT,
NRM_D_LAST_DRC
};
static ku_t dcts[] = { //assembler directives
{"ASSERT", NRM_D_ASSERT},{"!", NRM_D_ASSERT},
{"OPT", NRM_D_OPT}, {"TTL", NRM_D_TTL}, {"SUBT", NRM_D_SUBT},
{"ORG", NRM_D_ORG}, //{"ABS", NRM_D_ORG}, /*in ObjAsm ABS is synonym for ORG*/
{"LTORG", NRM_D_LTORG},
{"AREA", NRM_D_AREA}, {"ENTRY", NRM_D_ENTRY}, {"ROUT", NRM_D_ROUT},
{"END", NRM_D_END}, {"KEEP", NRM_D_KEEP},
{"GET", NRM_D_GET}, {"INCLUDE", NRM_D_GET},
{"IMPORT", NRM_D_IMPORT}, {"EXPORT", NRM_D_EXPORT},
{"*", NRM_D_EQU}, {"EQU", NRM_D_EQU},
{"GBLA", NRM_D_GBLA}, {"GBLL", NRM_D_GBLL}, {"GBLS", NRM_D_GBLS},
{"LCLA", NRM_D_LCLA}, {"LCLL", NRM_D_LCLL}, {"LCLS", NRM_D_LCLS},
{"SETA", NRM_D_SETA}, {"SETL", NRM_D_SETL}, {"SETS", NRM_D_SETS},
{"RN", NRM_D_RN}, {"FN", NRM_D_FN}, {"CP", NRM_D_CP},{"CN", NRM_D_CN},
{"DCB", NRM_D_DCB}, {"DCW", NRM_D_DCW}, {"DCD", NRM_D_DCD},
{"=", NRM_D_DCB}, {"&", NRM_D_DCD}, {"%", NRM_D_DCZ},
{"DCFS", NRM_D_DCB}, {"DCFD", NRM_D_DCFD},
{"ALIGN", NRM_D_ALIGN}, {"^", NRM_D_SMORG}, {"#", NRM_D_RESERVE},
{"NOFP", NRM_D_NOFP}, {"RLIST", NRM_D_NOFP},
{"IF", NRM_D_IF}, {"ELSE", NRM_D_ELSE}, {"ENDIF", NRM_D_ENDIF},
{"[", NRM_D_IF}, {"|", NRM_D_ELSE}, {"]", NRM_D_ENDIF},
{"WHILE", NRM_D_WHILE}, {"WEND", NRM_D_WEND},
{"MACRO", NRM_D_MACRO}, {"MEND", NRM_D_MEND}, {"MEXIT", NRM_D_MEXIT},
{0,0}
};
#define NRM_BV_FALSE 0x01
#define NRM_BV_TRUE 0x02
#define NRM_BV_PC 0x03
#define NRM_BV_VAR 0x04
#define NRM_BV_OPT 0x05
#define NRM_BV_CONFIG 0x06
#define NRM_BV_ENDIAN 0x07
#define NRM_BV_INVALID 0x08
static ku_t blts[] = { //builtin variables
{"{FALSE}", NRM_BV_FALSE}, {"{TRUE}", NRM_BV_TRUE},
{"{PC}", NRM_BV_PC}, {".", NRM_BV_PC},
{"{VAR}", NRM_BV_VAR}, {"@", NRM_BV_VAR},
{"{OPT}", NRM_BV_OPT},
{"{CONFIG}", NRM_BV_CONFIG}, {"{ENDIAN}", NRM_BV_ENDIAN},
{0,0}
};
static int nrm_globals_ready = 0;
#define NRM_BAD_OPCODE 0xFFFFFFFF
static ku_t *opcm = NULL; // opcode mnemonic map
static ku_t *sftm = NULL; // shift operations map
static ku_t *cndm = NULL; // condition mnemonic map
static ku_t *dctm = NULL; // directive map
static ku_t *bltm = NULL; // builtin variable map
static void nrm_init_globals() {
shdefault(opcm,NRM_BAD_OPCODE);
for (ku_t *o = opcs; o->key; o++) shput(opcm, o->key, o->value);
shdefault(cndm,NRM_BAD_OPCODE);
for (ku_t *o = cnds; o->key; o++) shput(cndm, o->key, o->value<<28);
shdefault(sftm,NRM_S_NIL);
for (ku_t *o = sfts; o->key; o++) shput(sftm, o->key, o->value);
for (ku_t *o = dcts; o->key; o++) shput(dctm, o->key, o->value);
for (ku_t *o = blts; o->key; o++) shput(bltm, o->key, o->value);
nrm_globals_ready = 1;
}
////////////////////////////// NRM STATE ///////////////////////////////////////
// Options for the nrm_init
typedef struct {
//ARGUMENTS
char **paths; //paths to search for files
U32 flags;
void *user; //user provided handle