-
Notifications
You must be signed in to change notification settings - Fork 1
/
opcas.c
1098 lines (1032 loc) · 35.8 KB
/
opcas.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
/*
opcas.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gf
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "token.h"
#include "pst_tokens.h"
#include "exproper.h"
#include "listctrl.h"
#include "op_class.h"
#define STACK_DEBUG 1
#define OP_DEBUG 2
#define DEBUG 0
#define SCC_BIT 0x00200000
#define SRC1_POS 16
#define DST_POS 22
#define MAXREG 31
#define REGMARK 0xFFE0
#define DISP_MASK (-MIN_DISP | MAX_DISP)
#define DEFNAM(name,numb) {"name",name,numb},
/* next four variables are for compatability with V7.4 Macxx */
unsigned short rel_salign = 2; /* default alignment for rel segments */
unsigned short rel_dalign = 2; /* def algnmnt for data in rel segments */
unsigned short abs_salign = 2; /* default alignment for abs segments */
unsigned short abs_dalign = 2; /* def algnmnt for data in abs segments */
char macxx_name[] = "macas";
char *macxx_target = "ASAP";
char *macxx_descrip = "Cross assembler for the ASAP.";
#if 0
unsigned short macxx_rel_salign = 2; /* default alignment for rel segments */
unsigned short macxx_rel_dalign = 2; /* def algnmnt for data in rel segments */
unsigned short macxx_abs_salign = 2; /* default alignment for abs segments */
unsigned short macxx_abs_dalign = 2; /* def algnmnt for data in abs segments */
#else
unsigned short macxx_salign = 2; /* default alignment segments by LLF */
unsigned short macxx_dalign = 2; /* default alignment data within segment */
#endif
unsigned short macxx_min_dalign = 2;
char macxx_mau = 8; /* # of bits/minimum addressable unit */
char macxx_bytes_mau = 1; /* number of bytes/mau */
char macxx_mau_byte = 1; /* number of mau's in a byte */
char macxx_mau_word = 2; /* number of mau's in a word */
char macxx_mau_long = 4; /* number of mau's in a long */
char macxx_nibbles_byte = 2; /* For the listing output routines */
char macxx_nibbles_word = 4;
char macxx_nibbles_long = 8;
unsigned long macxx_edm_default = ED_LC|ED_GBL|ED_TRUNC; /* default edmask */
unsigned long macxx_lm_default = ~(LIST_ME|LIST_MEB|LIST_MES|LIST_LD|LIST_COD); /* default list mask */
int current_radix = 16; /* default the radix to hex */
char expr_open = '('; /* char that opens an expression */
char expr_close = ')'; /* char that closes an expression */
char expr_escape = '^'; /* char that escapes an expression term */
char macro_arg_open = '<'; /* char that opens a macro argument */
char macro_arg_close = '>'; /* char that closes a macro argument */
char macro_arg_escape = '^'; /* char that escapes a macro argument */
char macro_arg_gensym = '?'; /* char indicating generated symbol for macro */
char macro_arg_genval = '\\'; /* char indicating generated value for macro */
char open_operand = '['; /* char that opens an operand indirection */
char close_operand = ']'; /* char that closes an operand indirection */
int max_opcode_length = 16; /* significant length of opcodes and */
int max_symbol_length = 16; /* symbols */
/* End of processor specific stuff */
int ust_init(void )
{
return 1; /* no custom symbol table stuff for ASAP */
}
static char *am_ptr;
static unsigned short which_stack; /* from whence to finally output */
#define MNBI 0x8000 /* operand must not be indexed */
#define MBI 0x4000 /* operand must be indexed */
#define FIN 0x2000 /* opening @ or ( found */
static void get_ea(unsigned short size);
static void get_dst(void),get_s1(void),get_s2(void);
static int merge_stacks(int a, int b);
void do_opcode(Opcode *opc)
{
int hiword;
int err_cnt = error_count[MSG_ERROR];
EXPR_struct *exp0,*exp1;
which_stack = 0;
EXP0.tag = 'l'; /* opcode is default U32 */
EXP0.tag_len = 1; /* only 1 longword */
EXP1.tag = 'u'; /* operand,if any, is U16 */
EXP0.ptr = 1; /* first stack has opcode (1 element) */
EXP1.ptr = 0; /* assume no other operands */
exprs_stack[2].ptr = 0; /* assume no other operands */
exprs_stack[3].ptr = 0; /* assume no other operands */
exprs_stack[2].tag = 0; /* assume no tags either */
exprs_stack[3].tag = 0;
exprs_stack[2].tag_len = 1; /* but if there is a tag, len = 1 */
exprs_stack[3].tag_len = 1;
exp0 = EXP0SP; /* point to expression stack 0 */
exp1 = EXP1SP; /* point to expression stack 1 */
exp0->expr_code = EXPR_VALUE; /* set the opcode expression */
hiword = opc->op_value; /* stuff in opcode value */
exp0->expr_value = (hiword & 0xFFE0) << 16;
am_ptr = inp_ptr; /* remember where am starts */
switch (opc->op_class & 7)
{
case OPCL_AU :
/* ALU ops:
* OP %i,%j,%k reg.i <- reg.j OP reg.k
* OP %i,%j,k reg.i <- reg.j OP k (constant)
*/
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class AU, value: %04X %s",
opc->op_value, am_ptr);
#endif
get_dst();
if ( *inp_ptr++ != ',' )
{
bad_token(inp_ptr - 1,"Comma expected");
}
get_s1();
if ( *inp_ptr++ != ',' )
{
bad_token(inp_ptr - 1,"Comma expected");
}
get_s2();
break;
case OPCL_LD :
/* load ops (incl LEA):
* Lxx %i,%j[%k] reg.i <- @(reg.j + (reg.k << size))
* Lxx %i,%j[k] reg.i <- @(reg.j + (k << size))
*/
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class LD, value: %04X %s",
opc->op_value, am_ptr);
#endif
get_dst();
if ( *inp_ptr++ != ',' )
{
bad_token(inp_ptr - 1,"Comma expected");
}
get_ea(hiword & 3);
break;
case OPCL_ST :
/* store ops:
* STxx %k,%i[%j] reg.k -> @(reg.i + (reg.j << size))
* STxx %k,%i[j] reg.k -> @(reg.i + (j << size))
* ( same as class LD, separate for debug )
*/
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class ST, value: %04X %s",
opc->op_value, am_ptr);
#endif
js_st:
get_dst();
if ( *inp_ptr++ != ',' )
{
bad_token(inp_ptr - 1,"Comma expected");
}
get_ea(hiword & 3);
break;
case OPCL_BC :
/* conditional branches:
* Bcc where
*/
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class BC, value: %04X %s",
opc->op_value, am_ptr);
#endif
get_offset:
get_token();
if (exprs(1,&EXP1) < 1)
{ /* expression nfg or not present */
exp1->expr_code = EXPR_VALUE; /* make it a 0 */
exp1->expr_value = -4; /* make it a br . */
EXP1.ptr = 1; /* 1 item on stack */
bad_token(inp_ptr - 1,"Expression expected");
}
else
{
exp1 = EXP1SP + EXP1.ptr;
exp1->expr_code = EXPR_SEG;
exp1->expr_value = current_offset + BR_OFF;
(exp1++)->expr_seg = current_section;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = '-';
EXP1.ptr += 2;
EXP1.ptr = compress_expr(&EXP1);
}
EXP1.tag = 'O'; /* flag as branch offset */
exp1 = EXP1SP;
if ( (EXP1.ptr == 1)
&& (exp1->expr_code == EXPR_VALUE)
)
{
/* absolute difference, check range */
if (exp1->expr_value < MIN_DISP ||
exp1->expr_value > MAX_DISP )
{
long toofar;
toofar = exp1->expr_value;
if (toofar > 0)
{
toofar -= MAX_DISP ;
}
else
{
toofar = -toofar + MIN_DISP;
}
sprintf(emsg,"Branch offset 0x%lX byte(s) out of range",toofar);
bad_token((char *)0,emsg);
exp1->expr_value = 0-(BR_OFF);
}
if ((exp1->expr_value&3) != 0)
{
bad_token((char *)0,"Branch to non-long-aligned address");
}
/* absolute difference (possibly cooked), mask and shift */
exp1->expr_value &= (-MIN_DISP|MAX_DISP);
exp1->expr_value >>= 2;
EXP1.ptr = 1;
}
else
{
if (options[QUAL_BOFF])
{
EXP_stk *ep2;
EXPR_struct *exp2;
/* branch offset is expression, add to expression to mask
* and shift down
*/
ep2 = &EXP2; /* clone the expression to stack 2 */
exp2 = EXP2SP;
memcpy(exp2, (char *)EXP1SP, EXP1.ptr*sizeof(EXPR_struct));
exp2 += EXP1.ptr;
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = 0; /* pick's argument; 0=top of stack */
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_PICK; /* dup top of stack */
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = 0x007FFFFFL; /* check for out of range + */
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_TST | (EXPROPER_TST_GT<<8);
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_XCHG; /* save answer, get tos */
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = 0; /* pick's argument; 0=top of stack */
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_PICK; /* dup top of stack */
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = -0x00800000L; /* check for out of range - */
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_TST | (EXPROPER_TST_LT<<8);
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_XCHG; /* save answer, get tos */
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = 3; /* check for unaligned target */
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_AND;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_TST | (EXPROPER_TST_OR<<8);
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_TST | (EXPROPER_TST_OR<<8);
ep2->ptr = exp2 - EXP2SP;
sprintf(emsg, "%s:%d", current_fnd->fn_name_only, current_fnd->fn_line);
ep2->tag = 0;
write_to_tmp(TMP_BOFF, 0, (char *)ep2, 0);
write_to_tmp(TMP_ASTNG, strlen(emsg)+1, emsg, 1);
ep2->ptr = 0; /* done with stack 2 */
}
exp1 = EXP1SP + EXP1.ptr;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = DISP_MASK;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_AND;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = 2;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_SHR;
EXP1.ptr = exp1 - EXP1SP;
}
/* now merge with opcode from stack 0 */
if ( (which_stack = merge_stacks(1,0)) )
EXP0.ptr = 0;
else
EXP1.ptr = 0;
break;
case OPCL_BS :
/* branch to subroutine:
* Bsr %link,where
*/
/* get link register, then merge with Bcc */
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class BS, value: %04X %s",
opc->op_value, am_ptr);
#endif
get_dst();
if ( *inp_ptr++ != ',' )
{
bad_token(inp_ptr - 1,"Comma expected");
}
goto get_offset;
case OPCL_JS :
/* Jump to subroutine
* Jsr %link,where
*/
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class JS, value: %04X %s",
opc->op_value, am_ptr);
#endif
goto js_st;
break;
case OPCL_PS :
/* putps, getps */
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class PS, value: %04X %s",
opc->op_value, am_ptr);
#endif
if ( hiword == (OP_PUTPS << 11) )
{
/* PUTPS, get src2 */
get_s2();
}
else
{
/* GETPS, get dst */
get_dst();
}
f1_eatit();
break;
case OPCL_IL :
/* illegal and sysill */
#if DEBUG & OP_DEBUG
fprintf(stderr,"got class IL, value: %04X %s",
opc->op_value, am_ptr);
#endif
f1_eatit();
break;
} /* -- switch(class) */
if (err_cnt != error_count[MSG_ERROR])
{ /* if any errors detected... */
f1_eatit(); /* ...eat to EOL */
}
#if DEBUG & STACK_DEBUG
fprintf(stderr,"First shot, which_stack: %d\n",which_stack);
dump_stacks();
if ( which_stack = merge_stacks(1,0) )
{
/* merge ended up in stack 1 */
EXP0.ptr = 0;
fputs("stack 1 fiddle clearing stack 0\n",stderr);
}
else
{
/* merge ended up in stack 0 */
EXP1.ptr = 0;
fputs("stack 1 fiddle clearing stack 1\n",stderr);
} /* end of stack[1] fiddling */
#else
which_stack = merge_stacks(1,0);
#endif
#if DEBUG & STACK_DEBUG
fprintf(stderr,"After stack[1] fiddling, which_stack: %d\n",which_stack);
dump_stacks();
if ( exprs_stack[3].ptr != 0 )
{
/* something on stack 3 */
if ( (uval = merge_stacks(3,which_stack)) == 3 )
{
/* merge ended up in stack 3 */
exprs_stack[which_stack].ptr = 0;
fprintf(stderr,"stack 3 fiddle clearing stack %d\n",which_stack);
which_stack = 3;
}
else
{
/* merge ended up in other stack */
exprs_stack[3].ptr = 0;
fputs("stack 3 fiddle clearing stack 3\n",stderr);
}
} /* end of stack[3] fiddling */
#else
which_stack = merge_stacks(3,which_stack);
#endif
#if DEBUG & STACK_DEBUG
fprintf(stderr,"After stack[3] fiddling, which_stack: %d\n",which_stack);
dump_stacks();
if ( exprs_stack[2].ptr )
{
/* something on stack 3 */
if ( (uval = merge_stacks(2,which_stack)) == 3 )
{
/* merge ended up in stack 2 */
exprs_stack[which_stack].ptr = 0;
fprintf(stderr,"stack 2 fiddle clearing stack %d\n",which_stack);
which_stack = 2;
}
else
{
/* merge ended up in other stack*/
exprs_stack[2].ptr = 0;
fputs("stack 2 fiddle clearing stack 2\n",stderr);
}
} /* end of stack 2 fiddling */
fprintf(stderr,"After stack[2] fiddling, which_stack: %d\n",which_stack);
dump_stacks();
#else
which_stack = merge_stacks(2,which_stack);
#endif
exprs_stack[which_stack].ptr = compress_expr(&exprs_stack[which_stack]);
#if DEBUG & STACK_DEBUG
fprintf(stderr,"After compress(), which_stack: %d\n",which_stack);
dump_stacks();
#endif
exprs_stack[which_stack].tag = ( edmask & ED_M68 ) ? 'L' : 'l';
if (list_bin) compress_expr_psuedo(&exprs_stack[which_stack]);
p1o_long(&exprs_stack[which_stack]); /* output inst */
} /* -- opc_as() */
static void get_ea(unsigned short size)
{
long val;
EXPR_struct *exp1,*exp2;
exp1 = EXP1SP;
get_token();
if ( exprs(1,&EXP1) < 1)
{ /* bad or no expression */
/* NOP the instruction by clearing Scc, leaving rdst = %0 */
EXP0SP->expr_value &= ~SCC_BIT;
EXP1.ptr = 0;
bad_token(inp_ptr - 1,"Register expression expected");
return;
}
else if (EXP1.register_reference)
{
if ((EXP1.ptr == 1)&&(exp1->expr_code == EXPR_VALUE))
{
/* absolute register # */
if ( (val = exp1->expr_value) < 0 || val > MAXREG )
{
bad_token(inp_ptr - 1,"Invalid Register expression");
exp1->expr_value = 0;
}
exp1->expr_value = val << SRC1_POS;
}
else
{
/* non-absolute register expression */
exp1 = EXP1SP + EXP1.ptr;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = MAXREG;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_AND;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = SRC1_POS;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_SHL;
EXP1.ptr += 4;
exp1 = EXP1SP;
} /* end register expression */
EXP1.tag = 'R'; /* let common code know */
} /* end register reference */
else if ( (EXP1.ptr == 1)
&& (exp1->expr_code == EXPR_VALUE)
&& !exp1->expr_value
)
{
/* constant 0 is accepted as %0 */
EXP1.tag = 'R'; /* let common code know */
}
else if ( *inp_ptr == open_operand )
{
/* he got the syntax right but muffed the semantics */
EXP1.ptr = 0;
bad_token(inp_ptr - 1,"expression must resolve to Register or 0");
return;
}
else
{
/* special shorthand hack for xxx fred => xxx %0[fred] */
EXP1.tag = 'u';
if ( size )
{
exp1 = EXP1SP + EXP1.ptr;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = size;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_SHR;
EXP1.ptr += 2;
}
EXP1.ptr = compress_expr(&EXP1);
return;
}
if ( *inp_ptr++ != open_operand )
{
sprintf(emsg,"%c expected",open_operand);
bad_token(inp_ptr - 1,emsg);
}
exp2 = EXP2SP;
get_token();
if ( exprs(1,&EXP2) < 1 )
{
/* partially NOP the instruction by clearing Scc */
EXP0SP->expr_value &= ~SCC_BIT;
bad_token(inp_ptr - 1,"Register or expression expected");
return;
}
else if ( EXP2.register_reference )
{
if ((EXP2.ptr == 1)&&(exp2->expr_code == EXPR_VALUE))
{
/* absolute register # */
if ( (val = exp2->expr_value) < 0 || val > MAXREG )
{
bad_token(inp_ptr - 1,"Invalid Register expression");
exp2->expr_value = 0;
}
else exp2->expr_value |= REGMARK;
}
else
{
/* a register valued expression, build expression
* to clip and position it.
*/
exp2 = EXP2SP + EXP2.ptr;
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = MAXREG;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_AND;
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = REGMARK;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_OR;
EXP2.ptr += 4;
}
EXP2.tag = 'R'; /* let common code know */
}
else
{
EXP2.tag = 'u';
if ( size )
{
exp2 = EXP2SP + EXP2.ptr;
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = size;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_SHR;
EXP2.ptr += 2;
EXP2.ptr = compress_expr(&EXP2);
}
}
if ( *inp_ptr++ != close_operand )
{
sprintf(emsg,"%c expected",close_operand);
bad_token(inp_ptr - 1,emsg);
}
else
{
while ( *inp_ptr && (*inp_ptr == ' ' || *inp_ptr == '\t'))
{
++inp_ptr;
}
}
}
static void get_s1( void )
{
long val;
EXPR_struct *exp1;
get_token();
exp1 = EXP1SP;
if ( exprs(1,&EXP1) < 1 )
{
/* partially NOP the instruction by clearing Scc */
EXP0SP->expr_value &= ~SCC_BIT;
bad_token(inp_ptr - 1,"Register expression expected");
return;
}
else if ( EXP1.register_reference )
{
if ((EXP1.ptr == 1)&&(exp1->expr_code == EXPR_VALUE))
{
/* absolute register # */
if ( (val = exp1->expr_value) < 0 || val > MAXREG )
{
bad_token(inp_ptr - 1,"Invalid Register expression");
exp1->expr_value = 0;
}
exp1->expr_value <<= SRC1_POS;
}
else
{
/* a register valued expression, build expression
* to clip and position it.
*/
exp1 = EXP1SP + EXP1.ptr;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = MAXREG;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_AND;
exp1->expr_code = EXPR_VALUE;
(exp1++)->expr_value = SRC1_POS;
exp1->expr_code = EXPR_OPER;
(exp1++)->expr_value = EXPROPER_SHL;
EXP1.ptr += 4;
}
EXP1.tag = 'R'; /* let common code know */
}
else if ((EXP1.ptr == 1)
&& exp1->expr_code == EXPR_VALUE
&& !exp1->expr_value
)
{
/* constant 0 is accepted as %0 */
EXP1.tag = 'R'; /* let common code know */
}
else
{
bad_token(inp_ptr - 1,"Expression must resolve to Register or 0");
EXP1.ptr = 0;
}
}
static void get_s2( void )
{
long val;
EXPR_struct *exp2;
get_token();
exp2 = EXP2SP;
if ( exprs(1,&EXP2) < 1 )
{
/* partially NOP the instruction by clearing Scc */
EXP0SP->expr_value &= ~SCC_BIT;
bad_token(inp_ptr - 1,"Register or expression expected");
return;
}
else if ( EXP2.register_reference )
{
if ((EXP2.ptr == 1)&&(exp2->expr_code == EXPR_VALUE))
{
/* absolute register # */
if ( (val = exp2->expr_value) < 0 || val > MAXREG )
{
bad_token(inp_ptr - 1,"Invalid Register expression");
exp2->expr_value = 0;
}
else exp2->expr_value |= REGMARK;
}
else
{
/* a register valued expression, build expression
* to clip and position it.
*/
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = MAXREG;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_AND;
exp2->expr_code = EXPR_VALUE;
(exp2++)->expr_value = REGMARK;
exp2->expr_code = EXPR_OPER;
(exp2++)->expr_value = EXPROPER_OR;
EXP2.ptr += 4;
}
EXP2.tag = 'R'; /* let common code know */
}
else
{
EXP2.tag = 'u';
}
}
static void get_dst( void )
{
long val;
EXPR_struct *exp3;
get_token();
exp3 = EXP3SP;
if ( exprs(1,&EXP3) < 1 )
{
/* partially NOP the instruction by clearing Scc & leaving dst=R0*/
EXP0SP->expr_value &= ~SCC_BIT;
bad_token(inp_ptr - 1,"Register expression expected");
EXP3.ptr = 0;
}
else if ( EXP3.register_reference )
{
if ((EXP3.ptr == 1)&&(exp3->expr_code == EXPR_VALUE))
{
/* absolute register # */
if ( (val = exp3->expr_value) < 0 || val > MAXREG )
{
bad_token(inp_ptr - 1,"Invalid Register expression");
exp3->expr_value = 0;
}
exp3->expr_value <<= DST_POS;
}
else
{
/* a register valued expression, build expression
* to clip and position it.
*/
exp3->expr_code = EXPR_VALUE;
(exp3++)->expr_value = MAXREG;
exp3->expr_code = EXPR_OPER;
(exp3++)->expr_value = EXPROPER_AND;
exp3->expr_code = EXPR_VALUE;
(exp3++)->expr_value = DST_POS;
exp3->expr_code = EXPR_OPER;
(exp3++)->expr_value = EXPROPER_SHL;
EXP3.ptr += 4;
}
EXP3.tag = 'R'; /* let common code know */
}
else if ( EXP3.ptr == 1
&& exp3->expr_code == EXPR_VALUE
&& !exp3->expr_value
)
{
/* constant 0 is accepted as %0 */
EXP3.tag = 'R'; /* let common code know */
}
else
{
bad_token(inp_ptr - 1,"Expression must resolve to Register or 0");
EXP3.ptr = 0;
}
}
/* merge_stacks()
* merges two expression stacks, by copying the shorter to the longer
* and adding an 'or' operator. Returns the number of the resulting stack
* and sets its stack ptr correctly. There is a slight preference for
* returning first.
*/
static int merge_stacks(int first, int second)
{
short flen,slen;
int final;
EXP_stk *ep1,*ep2;
EXPR_struct *esp1,*esp2;
ep1 = &exprs_stack[first];
ep2 = &exprs_stack[second];
esp1 = ep1->stack;
esp2 = ep2->stack;
/* if either is empty, return the other... */
if ( (slen = ep2->ptr) == 0) return first;
if ( (flen = ep1->ptr) == 0) return second;
if ( flen < slen )
{
/* first is shorter, copy it to second */
memcpy((char *)(esp2+ep2->ptr),(char *)esp1,
flen * sizeof(EXPR_struct));
final = second;
ep1->ptr = 0; /* first stack is now empty */
ep1 = ep2; /* set final pointer to second stack */
esp1 = esp2;
}
else
{
/* second is shorter, copy it to first */
memcpy((char *)(esp1+ep1->ptr),(char *)esp2,
slen * sizeof(EXPR_struct));
final = first;
ep2->ptr = 0; /* second stack is now empty */
}
flen += slen; /* my own constant subexpression elimination */
esp1 = ep1->stack + flen; /* compute new end of stack */
ep1->ptr = flen+1; /* set new size of final stack */
esp1->expr_code = EXPR_OPER; /* glue on an "or" to merge the items */
esp1->expr_value = EXPROPER_OR;
return final;
}
#if STACK_DEBUG
void dump_stack(int stacknum)
{
int tag,last;
EXP_stk *eps;
EXPR_struct *curr,*top;
eps = &exprs_stack[stacknum];
tag = eps->tag;
fprintf(stderr,"\nStack %d Tag '%c' (0x%02X) depth %d\n",
stacknum,
(isgraph(tag) ? tag : '.'),
tag,
last = eps->ptr);
curr = eps->stack;
top = eps->stack+last;
for ( ; curr < top ; ++curr )
{
switch (tag = curr->expr_code)
{
case EXPR_VALUE :
fprintf(stderr," 0x%lX\n",curr->expr_value);
break;
case EXPR_OPER :
tag = curr->expr_value;
fprintf(stderr," %c (0x%X)\n",(isgraph(tag) ? tag : '.'),tag);
break;
case EXPR_SEG :
fprintf(stderr," %s + 0x%lX\n",
(curr->expt.expt_seg)->seg_string,curr->expr_value);
break;
case EXPR_SYM :
fprintf(stderr,"`%s'\n",
(curr->expt.expt_sym)->ss_string);
break;
default :
fprintf(stderr,"?? code: 0x%X value: 0x%lX\n",tag,curr->expr_value);
} /* end case */
} /* end for */
} /* end routine dump_stack */
void dump_stacks( void )
{
int i;
for ( i = 0 ; i < EXPR_MAXSTACKS ; ++i )
{
if ( exprs_stack[i].ptr ) dump_stack(i);
}
}
#endif
#define NTYPE_REG (1) /* expression resolves to register */
#define NTYPE_S2 (2) /* expression can always be used in src2 slot */
#define NTYPE_AS2 (4) /* expression can be used in Arithmetic src2 slot */
#define NTYPE_LS2 (8) /* expression can be used in Logical src2 slot */
/* the next two (LEA and SHF) are not yet implemented */
#define NTYPE_LEA (0x10)/* expression can be built with LEA */
#define NTYPE_SHF (0x20)/* expression can be built one shift */
#define NTYPE_ABS (0x40)/* expression is absolute */
#define NTYPE_SYM (0x80)/* expression is symbol_ref, bit 16-23 have section */
long op_ntype( Opcode *opc )
{
long val,type;
EXPR_struct *exp2;
get_token();
exp2 = EXP2SP;
type = 0;
if ( exprs(1,&EXP2) < 1 )
{
/* no expression */
bad_token(inp_ptr - 1,"Register or expression expected");
}
else if ( EXP2.register_reference )
{
return(NTYPE_REG | NTYPE_S2 | NTYPE_AS2 | NTYPE_LS2);
}
else if ( (EXP2.ptr == 1) && (exp2->expr_code == EXPR_VALUE))
{
/* an absolute value, classify further */
type = NTYPE_ABS;
if ( (val = exp2->expr_value) > 0 && ( val < 0xFFE0 ) )
{
type |= (NTYPE_S2 | NTYPE_AS2 | NTYPE_LS2);
}
if ( (val < 0) && (-val < 0xFFE0) ) type |= NTYPE_AS2;
if ( (val < 0) && (~val < 0xFFE0) ) type |= NTYPE_LS2;
}
else if ( (EXP2.ptr == 1) && (exp2->expr_code == EXPR_SYM))
{
type = ((exp2->expt.expt_sym)->ssp_up.ssp_seg)->seg_ident;
type = (type << 16) | NTYPE_SYM;
}
f1_eatit();
return type;
}
static SS_struct *literal_pool_sym;
static SEG_struct *literal_pool_ptr;
static long literal_pool_register = 26<<SRC1_POS;
int op_using( Opcode *opc )
{
EXPR_struct *exp3;
SEG_struct *segp;
SS_struct *sym=0;
exp3 = EXP3SP;
get_dst();
if (EXP3.ptr == 1 && exp3->expr_code == EXPR_VALUE && EXP3.tag == 'R' && exp3->expr_value != 0)
{
if (*inp_ptr++ != ',')
{
bad_token(inp_ptr-1, "Comma expected here");
f1_eatit();
return 0;
}
get_token();
if (token_type != TOKEN_strng)
{
bad_token(tkn_ptr, "Expected a section name here");
f1_eatit();
return 0;
}
sym = sym_lookup(token_pool, SYM_INSERT_IF_NOT_FOUND);
if ((new_symbol&SYM_ADD) != 0)
{
sym->ss_fnd = current_fnd;
sym->ss_line = current_fnd->fn_line;
sym->flg_global = 1;
sym->ss_string = token_pool;
token_pool_size -= token_value+1;
token_pool += token_value+1;
}
segp = find_segment(sym->ss_string, seg_list, seg_list_index);
if (segp == 0)
{
segp = get_seg_mem(&sym, sym->ss_string);
segp->flg_literal = 1;
segp->seg_salign = macxx_salign;
segp->seg_dalign = macxx_dalign;
segp->seg_maxlen = 256*1024-(32*4);
}
literal_pool_sym = sym;
literal_pool_ptr = segp;
literal_pool_register = exp3->expr_value>>(DST_POS-SRC1_POS);
return 0;
}
bad_token(tkn_ptr, "First argument must a register > 0");
return 0;
}
int op_ldlit( Opcode *opc )
{
int which_stack;
EXPR_struct *exp0, *exp1, *exp;
#if 0
EXPR_struct *exp2;
#endif
EXP0.tag = 'l'; /* opcode is default U32 */
EXP0.tag_len = 1; /* only 1 longword */
EXP0.ptr = 1; /* first stack has opcode (1 element) */
exp0 = EXP0SP; /* point to expression stack 0 */
exp1 = EXP1SP; /* point to expression stack 1 */
#if 0
exp2 = EXP2SP; /* point to expression stack 2 */
#endif
exp0->expr_code = EXPR_VALUE; /* set the opcode expression */
exp0->expr_value = 0x80000000L; /* opcode for LD */
EXP1.ptr = 0; /* assume no other operands */
EXP2.ptr = 0; /* assume no other operands */
EXP3.ptr = 0; /* assume no other operands */
EXP2.tag = 0; /* assume no tags either */