aboutsummaryrefslogtreecommitdiff
path: root/gas/config/rl78-parse.y
blob: 81a98ec915388358a64044e547b0d4de87d364fb (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
/* rl78-parse.y  Renesas RL78 parser
   Copyright (C) 2011-2018 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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, or (at your option)
   any later version.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
%{

#include "as.h"
#include "safe-ctype.h"
#include "rl78-defs.h"

static int rl78_lex (void);

/* Ok, here are the rules for using these macros...

   B*() is used to specify the base opcode bytes.  Fields to be filled
        in later, leave zero.  Call this first.

   F() and FE() are used to fill in fields within the base opcode bytes.  You MUST
        call B*() before any F() or FE().

   [UN]*O*(), PC*() appends operands to the end of the opcode.  You
        must call P() and B*() before any of these, so that the fixups
        have the right byte location.
        O = signed, UO = unsigned, NO = negated, PC = pcrel

   IMM() adds an immediate and fills in the field for it.
   NIMM() same, but negates the immediate.
   NBIMM() same, but negates the immediate, for sbb.
   DSP() adds a displacement, and fills in the field for it.

   Note that order is significant for the O, IMM, and DSP macros, as
   they append their data to the operand buffer in the order that you
   call them.

   Use "disp" for displacements whenever possible; this handles the
   "0" case properly.  */

#define B1(b1)             rl78_base1 (b1)
#define B2(b1, b2)         rl78_base2 (b1, b2)
#define B3(b1, b2, b3)     rl78_base3 (b1, b2, b3)
#define B4(b1, b2, b3, b4) rl78_base4 (b1, b2, b3, b4)

/* POS is bits from the MSB of the first byte to the LSB of the last byte.  */
#define F(val,pos,sz)      rl78_field (val, pos, sz)
#define FE(exp,pos,sz)	   rl78_field (exp_val (exp), pos, sz);

#define O1(v)              rl78_op (v, 1, RL78REL_DATA)
#define O2(v)              rl78_op (v, 2, RL78REL_DATA)
#define O3(v)              rl78_op (v, 3, RL78REL_DATA)
#define O4(v)              rl78_op (v, 4, RL78REL_DATA)

#define PC1(v)             rl78_op (v, 1, RL78REL_PCREL)
#define PC2(v)             rl78_op (v, 2, RL78REL_PCREL)
#define PC3(v)             rl78_op (v, 3, RL78REL_PCREL)

#define IMM(v,pos)	   F (immediate (v, RL78REL_SIGNED, pos), pos, 2); \
			   if (v.X_op != O_constant && v.X_op != O_big) rl78_linkrelax_imm (pos)
#define NIMM(v,pos)	   F (immediate (v, RL78REL_NEGATIVE, pos), pos, 2)
#define NBIMM(v,pos)	   F (immediate (v, RL78REL_NEGATIVE_BORROW, pos), pos, 2)
#define DSP(v,pos,msz)	   if (!v.X_md) rl78_relax (RL78_RELAX_DISP, pos); \
			   else rl78_linkrelax_dsp (pos); \
			   F (displacement (v, msz), pos, 2)

#define id24(a,b2,b3)	   B3 (0xfb+a, b2, b3)

static int         expr_is_sfr (expressionS);
static int         expr_is_saddr (expressionS);
static int         expr_is_word_aligned (expressionS);
static int         exp_val (expressionS exp);

static int    need_flag = 0;
static int    rl78_in_brackets = 0;
static int    rl78_last_token = 0;
static char * rl78_init_start;
static char * rl78_last_exp_start = 0;
static int    rl78_bit_insn = 0;

#define YYDEBUG 1
#define YYERROR_VERBOSE 1

#define NOT_SADDR  rl78_error ("Expression not 0xFFE20 to 0xFFF1F")
#define SA(e) if (!expr_is_saddr (e)) NOT_SADDR;

#define SET_SA(e) e.X_md = BFD_RELOC_RL78_SADDR

#define NOT_SFR  rl78_error ("Expression not 0xFFF00 to 0xFFFFF")
#define SFR(e) if (!expr_is_sfr (e)) NOT_SFR;

#define NOT_SFR_OR_SADDR  rl78_error ("Expression not 0xFFE20 to 0xFFFFF")

#define NOT_ES if (rl78_has_prefix()) rl78_error ("ES: prefix not allowed here");

#define WA(x) if (!expr_is_word_aligned (x)) rl78_error ("Expression not word-aligned");

#define ISA_G10(s) if (!rl78_isa_g10()) rl78_error (s " is only supported on the G10")
#define ISA_G13(s) if (!rl78_isa_g13()) rl78_error (s " is only supported on the G13")
#define ISA_G14(s) if (!rl78_isa_g14()) rl78_error (s " is only supported on the G14")

static void check_expr_is_bit_index (expressionS);
#define Bit(e) check_expr_is_bit_index (e);

/* Returns TRUE (non-zero) if the expression is a constant in the
   given range.  */
static int check_expr_is_const (expressionS, int vmin, int vmax);

/* Convert a "regb" value to a "reg_xbc" value.  Error if other
   registers are passed.  Needed to avoid reduce-reduce conflicts.  */
static int
reg_xbc (int reg)
{
  switch (reg)
    {
      case 0: /* X */
        return 0x10;
      case 3: /* B */
        return 0x20;
      case 2: /* C */
        return 0x30;
      default:
        rl78_error ("Only X, B, or C allowed here");
	return 0;
    }
}

%}

%name-prefix="rl78_"

%union {
  int regno;
  expressionS exp;
}

%type <regno> regb regb_na regw regw_na FLAG sfr
%type <regno> A X B C D E H L AX BC DE HL
%type <exp> EXPR

%type <regno> addsub addsubw andor1 bt_bf setclr1 oneclrb oneclrw
%type <regno> incdec incdecw

%token A X B C D E H L AX BC DE HL
%token SPL SPH PSW CS ES PMC MEM
%token FLAG SP CY
%token RB0 RB1 RB2 RB3

%token EXPR UNKNOWN_OPCODE IS_OPCODE

%token DOT_S DOT_B DOT_W DOT_L DOT_A DOT_UB DOT_UW

%token ADD ADDC ADDW AND_ AND1
/* BC is also a register pair */
%token BF BH BNC BNH BNZ BR BRK BRK1 BT BTCLR BZ
%token CALL CALLT CLR1 CLRB CLRW CMP CMP0 CMPS CMPW
%token DEC DECW DI DIVHU DIVWU
%token EI
%token HALT
%token INC INCW
%token MACH MACHU MOV MOV1 MOVS MOVW MULH MULHU MULU
%token NOP NOT1
%token ONEB ONEW OR OR1
%token POP PUSH
%token RET RETI RETB ROL ROLC ROLWC ROR RORC
%token SAR SARW SEL SET1 SHL SHLW SHR SHRW
%token   SKC SKH SKNC SKNH SKNZ SKZ STOP SUB SUBC SUBW
%token XCH XCHW XOR XOR1

%%
/* ====================================================================== */

statement :

	  UNKNOWN_OPCODE
	  { as_bad (_("Unknown opcode: %s"), rl78_init_start); }

/* The opcodes are listed in approximately alphabetical order.  */

/* For reference:

  sfr  = special function register - symbol, 0xFFF00 to 0xFFFFF
  sfrp = special function register - symbol, 0xFFF00 to 0xFFFFE, even only
  saddr  = 0xFFE20 to 0xFFF1F
  saddrp = 0xFFE20 to 0xFFF1E, even only

  addr20 = 0x00000 to 0xFFFFF
  addr16 = 0x00000 to 0x0FFFF, even only for 16-bit ops
  addr5  = 0x00000 to 0x000BE, even only
*/

/* ---------------------------------------------------------------------- */

/* addsub is ADD, ADDC, SUB, SUBC, AND, OR, XOR, and parts of CMP.  */

	| addsub A ',' '#' EXPR
	  { B1 (0x0c|$1); O1 ($5); }

	| addsub EXPR {SA($2)} ',' '#' EXPR
	  { B1 (0x0a|$1); SET_SA ($2); O1 ($2); O1 ($6); }

	| addsub A ',' A
	  { B2 (0x61, 0x01|$1); }

	| addsub A ',' regb_na
	  { B2 (0x61, 0x08|$1); F ($4, 13, 3); }

	| addsub regb_na ',' A
	  { B2 (0x61, 0x00|$1); F ($2, 13, 3); }

	| addsub A ',' EXPR {SA($4)}
	  { B1 (0x0b|$1); SET_SA ($4); O1 ($4); }

	| addsub A ',' opt_es '!' EXPR
	  { B1 (0x0f|$1); O2 ($6); rl78_linkrelax_addr16 (); }

	| addsub A ',' opt_es '[' HL ']'
	  { B1 (0x0d|$1); }

	| addsub A ',' opt_es '[' HL '+' EXPR ']'
	  { B1 (0x0e|$1); O1 ($8); }

	| addsub A ',' opt_es '[' HL '+' B ']'
	  { B2 (0x61, 0x80|$1); }

	| addsub A ',' opt_es '[' HL '+' C ']'
	  { B2 (0x61, 0x82|$1); }

	| addsub opt_es '!' EXPR ',' '#' EXPR
	  { if ($1 != 0x40)
	      { rl78_error ("Only CMP takes these operands"); }
	    else
	      { B1 (0x00|$1); O2 ($4); O1 ($7); rl78_linkrelax_addr16 (); }
	  }

/* ---------------------------------------------------------------------- */

	| addsubw AX ',' '#' EXPR
	  { B1 (0x04|$1); O2 ($5); }

	| addsubw AX ',' regw
	  { B1 (0x01|$1); F ($4, 5, 2); }

	| addsubw AX ',' EXPR {SA($4)}
	  { B1 (0x06|$1); SET_SA ($4); O1 ($4); }

	| addsubw AX ',' opt_es '!' EXPR
	  { B1 (0x02|$1); O2 ($6); rl78_linkrelax_addr16 (); }

	| addsubw AX ',' opt_es '[' HL '+' EXPR ']'
	  { B2 (0x61, 0x09|$1); O1 ($8); }

	| addsubw AX ',' opt_es '[' HL ']'
	  { B3 (0x61, 0x09|$1, 0); }

	| addsubw SP ',' '#' EXPR
	  { B1 ($1 ? 0x20 : 0x10); O1 ($5);
	    if ($1 == 0x40)
	      rl78_error ("CMPW SP,#imm not allowed");
	  }

/* ---------------------------------------------------------------------- */

	| andor1 CY ',' sfr '.' EXPR {Bit($6)}
	  { B3 (0x71, 0x08|$1, $4); FE ($6, 9, 3); }

	| andor1 CY ',' EXPR '.' EXPR {Bit($6)}
	  { if (expr_is_sfr ($4))
	      { B2 (0x71, 0x08|$1); FE ($6, 9, 3); O1 ($4); }
	    else if (expr_is_saddr ($4))
	      { B2 (0x71, 0x00|$1); FE ($6, 9, 3); SET_SA ($4); O1 ($4); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| andor1 CY ',' A '.' EXPR {Bit($6)}
	  { B2 (0x71, 0x88|$1);  FE ($6, 9, 3); }

	| andor1 CY ',' opt_es '[' HL ']' '.' EXPR {Bit($9)}
	  { B2 (0x71, 0x80|$1);  FE ($9, 9, 3); }

/* ---------------------------------------------------------------------- */

	| BC '$' EXPR
	  { B1 (0xdc); PC1 ($3); rl78_linkrelax_branch (); }

	| BNC '$' EXPR
	  { B1 (0xde); PC1 ($3); rl78_linkrelax_branch (); }

	| BZ '$' EXPR
	  { B1 (0xdd); PC1 ($3); rl78_linkrelax_branch (); }

	| BNZ '$' EXPR
	  { B1 (0xdf); PC1 ($3); rl78_linkrelax_branch (); }

	| BH '$' EXPR
	  { B2 (0x61, 0xc3); PC1 ($3); rl78_linkrelax_branch (); }

	| BNH '$' EXPR
	  { B2 (0x61, 0xd3); PC1 ($3); rl78_linkrelax_branch (); }

/* ---------------------------------------------------------------------- */

	| bt_bf sfr '.' EXPR ',' '$' EXPR
	  { B3 (0x31, 0x80|$1, $2); FE ($4, 9, 3); PC1 ($7); }

	| bt_bf EXPR '.' EXPR ',' '$' EXPR
	  { if (expr_is_sfr ($2))
	      { B2 (0x31, 0x80|$1); FE ($4, 9, 3); O1 ($2); PC1 ($7); }
	    else if (expr_is_saddr ($2))
	      { B2 (0x31, 0x00|$1); FE ($4, 9, 3); SET_SA ($2); O1 ($2); PC1 ($7); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| bt_bf A '.' EXPR ',' '$' EXPR
	  { B2 (0x31, 0x01|$1); FE ($4, 9, 3); PC1 ($7); }

	| bt_bf opt_es '[' HL ']' '.' EXPR ',' '$' EXPR
	  { B2 (0x31, 0x81|$1); FE ($7, 9, 3); PC1 ($10); }

/* ---------------------------------------------------------------------- */

	| BR AX
	  { B2 (0x61, 0xcb); }

	| BR '$' EXPR
	  { B1 (0xef); PC1 ($3); rl78_linkrelax_branch (); }

	| BR '$' '!' EXPR
	  { B1 (0xee); PC2 ($4); rl78_linkrelax_branch (); }

	| BR '!' EXPR
	  { B1 (0xed); O2 ($3); rl78_linkrelax_branch (); }

	| BR '!' '!' EXPR
	  { B1 (0xec); O3 ($4); rl78_linkrelax_branch (); }

/* ---------------------------------------------------------------------- */

	| BRK
	  { B2 (0x61, 0xcc); }

	| BRK1
	  { B1 (0xff); }

/* ---------------------------------------------------------------------- */

	| CALL regw
	  { B2 (0x61, 0xca); F ($2, 10, 2); }

	| CALL '$' '!' EXPR
	  { B1 (0xfe); PC2 ($4); }

	| CALL '!' EXPR
	  { B1 (0xfd); O2 ($3); }

	| CALL '!' '!' EXPR
	  { B1 (0xfc); O3 ($4); rl78_linkrelax_branch (); }

	| CALLT '[' EXPR ']'
	  { if ($3.X_op != O_constant)
	      rl78_error ("CALLT requires a numeric address");
	    else
	      {
	        int i = $3.X_add_number;
		if (i < 0x80 || i > 0xbe)
		  rl78_error ("CALLT address not 0x80..0xbe");
		else if (i & 1)
		  rl78_error ("CALLT address not even");
		else
		  {
		    B2 (0x61, 0x84);
	    	    F ((i >> 1) & 7, 9, 3);
	    	    F ((i >> 4) & 7, 14, 2);
		  }
	      }
	  }

/* ---------------------------------------------------------------------- */

	| setclr1 CY
	  { B2 (0x71, $1 ? 0x88 : 0x80); }

	| setclr1 sfr '.' EXPR
	  { B3 (0x71, 0x0a|$1, $2); FE ($4, 9, 3); }

	| setclr1 EXPR '.' EXPR
	  { if (expr_is_sfr ($2))
	      { B2 (0x71, 0x0a|$1); FE ($4, 9, 3); O1 ($2); }
	    else if (expr_is_saddr ($2))
	      { B2 (0x71, 0x02|$1); FE ($4, 9, 3); SET_SA ($2); O1 ($2); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| setclr1 A '.' EXPR
	  { B2 (0x71, 0x8a|$1);  FE ($4, 9, 3); }

	| setclr1 opt_es '!' EXPR '.' EXPR
	  { B2 (0x71, 0x00+$1*0x08); FE ($6, 9, 3); O2 ($4); rl78_linkrelax_addr16 (); }

	| setclr1 opt_es '[' HL ']' '.' EXPR
	  { B2 (0x71, 0x82|$1); FE ($7, 9, 3); }

/* ---------------------------------------------------------------------- */

	| oneclrb A
	  { B1 (0xe1|$1); }
	| oneclrb X
	  { B1 (0xe0|$1); }
	| oneclrb B
	  { B1 (0xe3|$1); }
	| oneclrb C
	  { B1 (0xe2|$1); }

	| oneclrb EXPR {SA($2)}
	  { B1 (0xe4|$1); SET_SA ($2); O1 ($2); }

	| oneclrb opt_es '!' EXPR
	  { B1 (0xe5|$1); O2 ($4); rl78_linkrelax_addr16 (); }

/* ---------------------------------------------------------------------- */

	| oneclrw AX
	  { B1 (0xe6|$1); }
	| oneclrw BC
	  { B1 (0xe7|$1); }

/* ---------------------------------------------------------------------- */

	| CMP0 A
	  { B1 (0xd1); }

	| CMP0 X
	  { B1 (0xd0); }

	| CMP0 B
	  { B1 (0xd3); }

	| CMP0 C
	  { B1 (0xd2); }

	| CMP0 EXPR {SA($2)}
	  { B1 (0xd4); SET_SA ($2); O1 ($2); }

	| CMP0 opt_es '!' EXPR
	  { B1 (0xd5); O2 ($4); rl78_linkrelax_addr16 (); }

/* ---------------------------------------------------------------------- */

	| CMPS X ',' opt_es '[' HL '+' EXPR ']'
	  { B2 (0x61, 0xde); O1 ($8); }

/* ---------------------------------------------------------------------- */

	| incdec regb
	  { B1 (0x80|$1); F ($2, 5, 3); }

	| incdec EXPR {SA($2)}
	  { B1 (0xa4|$1); SET_SA ($2); O1 ($2); }
	| incdec '!' EXPR
	  { B1 (0xa0|$1); O2 ($3); rl78_linkrelax_addr16 (); }
	| incdec ES ':' '!' EXPR
	  { B2 (0x11, 0xa0|$1); O2 ($5); }
	| incdec '[' HL '+' EXPR ']'
	  { B2 (0x61, 0x59+$1); O1 ($5); }
	| incdec ES ':' '[' HL '+' EXPR ']'
	  { B3 (0x11, 0x61, 0x59+$1); O1 ($7); }

/* ---------------------------------------------------------------------- */

	| incdecw regw
	  { B1 (0xa1|$1); F ($2, 5, 2); }

	| incdecw EXPR {SA($2)}
	  { B1 (0xa6|$1); SET_SA ($2); O1 ($2); }

	| incdecw opt_es '!' EXPR
	  { B1 (0xa2|$1); O2 ($4); rl78_linkrelax_addr16 (); }

	| incdecw opt_es '[' HL '+' EXPR ']'
	  { B2 (0x61, 0x79+$1); O1 ($6); }

/* ---------------------------------------------------------------------- */

	| DI
	  { B3 (0x71, 0x7b, 0xfa); }

	| EI
	  { B3 (0x71, 0x7a, 0xfa); }

/* ---------------------------------------------------------------------- */

	| MULHU { ISA_G14 ("MULHU"); }
	  { B3 (0xce, 0xfb, 0x01); }

	| MULH { ISA_G14 ("MULH"); }
	  { B3 (0xce, 0xfb, 0x02); }

	| MULU X
	  { B1 (0xd6); }

	| DIVHU { ISA_G14 ("DIVHU"); }
	  { B3 (0xce, 0xfb, 0x03); }

/* Note that the DIVWU encoding was changed from [0xce,0xfb,0x04] to
   [0xce,0xfb,0x0b].  Different versions of the Software Manual exist
   with the same version number, but varying encodings.  The version
   here matches the hardware.  */

	| DIVWU { ISA_G14 ("DIVWU"); }
	  { B3 (0xce, 0xfb, 0x0b); }

	| MACHU { ISA_G14 ("MACHU"); }
	  { B3 (0xce, 0xfb, 0x05); }

	| MACH { ISA_G14 ("MACH"); }
	  { B3 (0xce, 0xfb, 0x06); }

/* ---------------------------------------------------------------------- */

	| HALT
	  { B2 (0x61, 0xed); }

/* ---------------------------------------------------------------------- */
/* Note that opt_es is included even when it's not an option, to avoid
   shift/reduce conflicts.  The NOT_ES macro produces an error if ES:
   is given by the user.  */

	| MOV A ',' '#' EXPR
	  { B1 (0x51); O1 ($5); }
	| MOV regb_na ',' '#' EXPR
	  { B1 (0x50); F($2, 5, 3); O1 ($5); }

	| MOV sfr ',' '#' EXPR
	  { if ($2 != 0xfd)
	      { B2 (0xce, $2); O1 ($5); }
	    else
	      { B1 (0x41); O1 ($5); }
	  }

	| MOV opt_es EXPR ',' '#' EXPR  {NOT_ES}
	  { if (expr_is_sfr ($3))
	      { B1 (0xce); O1 ($3); O1 ($6); }
	    else if (expr_is_saddr ($3))
	      { B1 (0xcd); SET_SA ($3); O1 ($3); O1 ($6); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOV '!' EXPR ',' '#' EXPR
	  { B1 (0xcf); O2 ($3); O1 ($6); rl78_linkrelax_addr16 (); }

	| MOV ES ':' '!' EXPR ',' '#' EXPR
	  { B2 (0x11, 0xcf); O2 ($5); O1 ($8); }

	| MOV regb_na ',' A
	  { B1 (0x70); F ($2, 5, 3); }

	| MOV A ',' regb_na
	  { B1 (0x60); F ($4, 5, 3); }

	| MOV opt_es EXPR ',' A  {NOT_ES}
	  { if (expr_is_sfr ($3))
	      { B1 (0x9e); O1 ($3); }
	    else if (expr_is_saddr ($3))
	      { B1 (0x9d); SET_SA ($3); O1 ($3); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOV A ',' opt_es '!' EXPR
	  { B1 (0x8f); O2 ($6); rl78_linkrelax_addr16 (); }

	| MOV '!' EXPR ',' A
	  { B1 (0x9f); O2 ($3); rl78_linkrelax_addr16 (); }

	| MOV ES ':' '!' EXPR ',' A
	  { B2 (0x11, 0x9f); O2 ($5); }

	| MOV regb_na ',' opt_es '!' EXPR
	  { B1 (0xc9|reg_xbc($2)); O2 ($6); rl78_linkrelax_addr16 (); }

	| MOV A ',' opt_es EXPR  {NOT_ES}
	  { if (expr_is_saddr ($5))
	      { B1 (0x8d); SET_SA ($5); O1 ($5); }
	    else if (expr_is_sfr ($5))
	      { B1 (0x8e); O1 ($5); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOV regb_na ',' opt_es EXPR {SA($5)} {NOT_ES}
	  { B1 (0xc8|reg_xbc($2)); SET_SA ($5); O1 ($5); }

	| MOV A ',' sfr
	  { B2 (0x8e, $4); }

	| MOV sfr ',' regb
	  { if ($4 != 1)
	      rl78_error ("Only A allowed here");
	    else
	      { B2 (0x9e, $2); }
	  }

	| MOV sfr ',' opt_es EXPR {SA($5)} {NOT_ES}
	  { if ($2 != 0xfd)
	      rl78_error ("Only ES allowed here");
	    else
	      { B2 (0x61, 0xb8); SET_SA ($5); O1 ($5); }
	  }

	| MOV A ',' opt_es '[' DE ']'
	  { B1 (0x89); }

	| MOV opt_es '[' DE ']' ',' A
	  { B1 (0x99); }

	| MOV opt_es '[' DE '+' EXPR ']' ',' '#' EXPR
	  { B1 (0xca); O1 ($6); O1 ($10); }

	| MOV A ',' opt_es '[' DE '+' EXPR ']'
	  { B1 (0x8a); O1 ($8); }

	| MOV opt_es '[' DE '+' EXPR ']' ',' A
	  { B1 (0x9a); O1 ($6); }

	| MOV A ',' opt_es '[' HL ']'
	  { B1 (0x8b); }

	| MOV opt_es '[' HL ']' ',' A
	  { B1 (0x9b); }

	| MOV opt_es '[' HL '+' EXPR ']' ',' '#' EXPR
	  { B1 (0xcc); O1 ($6); O1 ($10); }

	| MOV A ',' opt_es '[' HL '+' EXPR ']'
	  { B1 (0x8c); O1 ($8); }

	| MOV opt_es '[' HL '+' EXPR ']' ',' A
	  { B1 (0x9c); O1 ($6); }

	| MOV A ',' opt_es '[' HL '+' B ']'
	  { B2 (0x61, 0xc9); }

	| MOV opt_es '[' HL '+' B ']' ',' A
	  { B2 (0x61, 0xd9); }

	| MOV A ',' opt_es '[' HL '+' C ']'
	  { B2 (0x61, 0xe9); }

	| MOV opt_es '[' HL '+' C ']' ',' A
	  { B2 (0x61, 0xf9); }

	| MOV opt_es EXPR '[' B ']' ',' '#' EXPR
	  { B1 (0x19); O2 ($3); O1 ($9); }

	| MOV A ',' opt_es EXPR '[' B ']'
	  { B1 (0x09); O2 ($5); }

	| MOV opt_es EXPR '[' B ']' ',' A
	  { B1 (0x18); O2 ($3); }

	| MOV opt_es EXPR '[' C ']' ',' '#' EXPR
	  { B1 (0x38); O2 ($3); O1 ($9); }

	| MOV A ',' opt_es EXPR '[' C ']'
	  { B1 (0x29); O2 ($5); }

	| MOV opt_es EXPR '[' C ']' ',' A
	  { B1 (0x28); O2 ($3); }

	| MOV opt_es EXPR '[' BC ']' ',' '#' EXPR
	  { B1 (0x39); O2 ($3); O1 ($9); }

	| MOV opt_es '[' BC ']' ',' '#' EXPR
	  { B3 (0x39, 0, 0); O1 ($8); }

	| MOV A ',' opt_es EXPR '[' BC ']'
	  { B1 (0x49); O2 ($5); }

	| MOV A ',' opt_es '[' BC ']'
	  { B3 (0x49, 0, 0); }

	| MOV opt_es EXPR '[' BC ']' ',' A
	  { B1 (0x48); O2 ($3); }

	| MOV opt_es '[' BC ']' ',' A
	  { B3 (0x48, 0, 0); }

	| MOV opt_es '[' SP '+' EXPR ']' ',' '#' EXPR  {NOT_ES}
	  { B1 (0xc8); O1 ($6); O1 ($10); }

	| MOV opt_es '[' SP ']' ',' '#' EXPR  {NOT_ES}
	  { B2 (0xc8, 0); O1 ($8); }

	| MOV A ',' opt_es '[' SP '+' EXPR ']'  {NOT_ES}
	  { B1 (0x88); O1 ($8); }

	| MOV A ',' opt_es '[' SP ']'  {NOT_ES}
	  { B2 (0x88, 0); }

	| MOV opt_es '[' SP '+' EXPR ']' ',' A  {NOT_ES}
	  { B1 (0x98); O1 ($6); }

	| MOV opt_es '[' SP ']' ',' A  {NOT_ES}
	  { B2 (0x98, 0); }

/* ---------------------------------------------------------------------- */

	| mov1 CY ',' EXPR '.' EXPR
	  { if (expr_is_saddr ($4))
	      { B2 (0x71, 0x04); FE ($6, 9, 3); SET_SA ($4); O1 ($4); }
	    else if (expr_is_sfr ($4))
	      { B2 (0x71, 0x0c); FE ($6, 9, 3); O1 ($4); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| mov1 CY ',' A '.' EXPR
	  { B2 (0x71, 0x8c); FE ($6, 9, 3); }

	| mov1 CY ',' sfr '.' EXPR
	  { B3 (0x71, 0x0c, $4); FE ($6, 9, 3); }

	| mov1 CY ',' opt_es '[' HL ']' '.' EXPR
	  { B2 (0x71, 0x84); FE ($9, 9, 3); }

	| mov1 EXPR '.' EXPR ',' CY
	  { if (expr_is_saddr ($2))
	      { B2 (0x71, 0x01); FE ($4, 9, 3); SET_SA ($2); O1 ($2); }
	    else if (expr_is_sfr ($2))
	      { B2 (0x71, 0x09); FE ($4, 9, 3); O1 ($2); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| mov1 A '.' EXPR ',' CY
	  { B2 (0x71, 0x89); FE ($4, 9, 3); }

	| mov1 sfr '.' EXPR ',' CY
	  { B3 (0x71, 0x09, $2); FE ($4, 9, 3); }

	| mov1 opt_es '[' HL ']' '.' EXPR ',' CY
	  { B2 (0x71, 0x81); FE ($7, 9, 3); }

/* ---------------------------------------------------------------------- */

	| MOVS opt_es '[' HL '+' EXPR ']' ',' X
	  { B2 (0x61, 0xce); O1 ($6); }

/* ---------------------------------------------------------------------- */

	| MOVW AX ',' '#' EXPR
	  { B1 (0x30); O2 ($5); }

	| MOVW regw_na ',' '#' EXPR
	  { B1 (0x30); F ($2, 5, 2); O2 ($5); }

	| MOVW opt_es EXPR ',' '#' EXPR {NOT_ES}
	  { if (expr_is_saddr ($3))
	      { B1 (0xc9); SET_SA ($3); O1 ($3); O2 ($6); }
	    else if (expr_is_sfr ($3))
	      { B1 (0xcb); O1 ($3); O2 ($6); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOVW AX ',' opt_es EXPR {NOT_ES}
	  { if (expr_is_saddr ($5))
	      { B1 (0xad); SET_SA ($5); O1 ($5); WA($5); }
	    else if (expr_is_sfr ($5))
	      { B1 (0xae); O1 ($5); WA($5); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOVW opt_es EXPR ',' AX {NOT_ES}
	  { if (expr_is_saddr ($3))
	      { B1 (0xbd); SET_SA ($3); O1 ($3); WA($3); }
	    else if (expr_is_sfr ($3))
	      { B1 (0xbe); O1 ($3); WA($3); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

	| MOVW AX ',' regw_na
	  { B1 (0x11); F ($4, 5, 2); }

	| MOVW regw_na ',' AX
	  { B1 (0x10); F ($2, 5, 2); }

	| MOVW AX ',' opt_es '!' EXPR
	  { B1 (0xaf); O2 ($6); WA($6); rl78_linkrelax_addr16 (); }

	| MOVW opt_es '!' EXPR ',' AX
	  { B1 (0xbf); O2 ($4); WA($4); rl78_linkrelax_addr16 (); }

	| MOVW AX ',' opt_es '[' DE ']'
	  { B1 (0xa9); }

	| MOVW opt_es '[' DE ']' ',' AX
	  { B1 (0xb9); }

	| MOVW AX ',' opt_es '[' DE '+' EXPR ']'
	  { B1 (0xaa); O1 ($8); }

	| MOVW opt_es '[' DE '+' EXPR ']' ',' AX
	  { B1 (0xba); O1 ($6); }

	| MOVW AX ',' opt_es '[' HL ']'
	  { B1 (0xab); }

	| MOVW opt_es '[' HL ']' ',' AX
	  { B1 (0xbb); }

	| MOVW AX ',' opt_es '[' HL '+' EXPR ']'
	  { B1 (0xac); O1 ($8); }

	| MOVW opt_es '[' HL '+' EXPR ']' ',' AX
	  { B1 (0xbc); O1 ($6); }

	| MOVW AX ',' opt_es EXPR '[' B ']'
	  { B1 (0x59); O2 ($5); }

	| MOVW opt_es EXPR '[' B ']' ',' AX
	  { B1 (0x58); O2 ($3); }

	| MOVW AX ',' opt_es EXPR '[' C ']'
	  { B1 (0x69); O2 ($5); }

	| MOVW opt_es EXPR '[' C ']' ',' AX
	  { B1 (0x68); O2 ($3); }

	| MOVW AX ',' opt_es EXPR '[' BC ']'
	  { B1 (0x79); O2 ($5); }

	| MOVW AX ',' opt_es '[' BC ']'
	  { B3 (0x79, 0, 0); }

	| MOVW opt_es EXPR '[' BC ']' ',' AX
	  { B1 (0x78); O2 ($3); }

	| MOVW opt_es '[' BC ']' ',' AX
	  { B3 (0x78, 0, 0); }

	| MOVW AX ',' opt_es '[' SP '+' EXPR ']' {NOT_ES}
	  { B1 (0xa8); O1 ($8);  WA($8);}

	| MOVW AX ',' opt_es '[' SP ']' {NOT_ES}
	  { B2 (0xa8, 0); }

	| MOVW opt_es '[' SP '+' EXPR ']' ',' AX {NOT_ES}
	  { B1 (0xb8); O1 ($6); WA($6); }

	| MOVW opt_es '[' SP ']' ',' AX {NOT_ES}
	  { B2 (0xb8, 0); }

	| MOVW regw_na ',' EXPR {SA($4)}
	  { B1 (0xca); F ($2, 2, 2); SET_SA ($4); O1 ($4); WA($4); }

	| MOVW regw_na ',' opt_es '!' EXPR
	  { B1 (0xcb); F ($2, 2, 2); O2 ($6); WA($6); rl78_linkrelax_addr16 (); }

	| MOVW SP ',' '#' EXPR
	  { B2 (0xcb, 0xf8); O2 ($5); }

	| MOVW SP ',' AX
	  { B2 (0xbe, 0xf8); }

	| MOVW AX ',' SP
	  { B2 (0xae, 0xf8); }

	| MOVW regw_na ',' SP
	  { B3 (0xcb, 0xf8, 0xff); F ($2, 2, 2); }

/* ---------------------------------------------------------------------- */

	| NOP
	  { B1 (0x00); }

/* ---------------------------------------------------------------------- */

	| NOT1 CY
	  { B2 (0x71, 0xc0); }

/* ---------------------------------------------------------------------- */

	| POP regw
	  { B1 (0xc0); F ($2, 5, 2); }

	| POP PSW
	  { B2 (0x61, 0xcd); };

	| PUSH regw
	  { B1 (0xc1); F ($2, 5, 2); }

	| PUSH PSW
	  { B2 (0x61, 0xdd); };

/* ---------------------------------------------------------------------- */

	| RET
	  { B1 (0xd7); }

	| RETI
	  { B2 (0x61, 0xfc); }

	| RETB
	  { B2 (0x61, 0xec); }

/* ---------------------------------------------------------------------- */

	| ROL A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xeb); }
	  }

	| ROLC A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xdc); }
	  }

	| ROLWC AX ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xee); }
	  }

	| ROLWC BC ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xfe); }
	  }

	| ROR A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xdb); }
	  }

	| RORC A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 1))
	      { B2 (0x61, 0xfb);}
	  }

/* ---------------------------------------------------------------------- */

	| SAR A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 7))
	      { B2 (0x31, 0x0b); FE ($4, 9, 3); }
	  }

	| SARW AX ',' EXPR
	  { if (check_expr_is_const ($4, 1, 15))
	      { B2 (0x31, 0x0f); FE ($4, 8, 4); }
	  }

/* ---------------------------------------------------------------------- */

	| SEL RB0
	  { B2 (0x61, 0xcf); }

	| SEL RB1
	  { B2 (0x61, 0xdf); }

	| SEL RB2
	  { B2 (0x61, 0xef); }

	| SEL RB3
	  { B2 (0x61, 0xff); }

/* ---------------------------------------------------------------------- */

	| SHL A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 7))
	      { B2 (0x31, 0x09); FE ($4, 9, 3); }
	  }

	| SHL B ',' EXPR
	  { if (check_expr_is_const ($4, 1, 7))
	      { B2 (0x31, 0x08); FE ($4, 9, 3); }
	  }

	| SHL C ',' EXPR
	  { if (check_expr_is_const ($4, 1, 7))
	      { B2 (0x31, 0x07); FE ($4, 9, 3); }
	  }

	| SHLW AX ',' EXPR
	  { if (check_expr_is_const ($4, 1, 15))
	      { B2 (0x31, 0x0d); FE ($4, 8, 4); }
	  }

	| SHLW BC ',' EXPR
	  { if (check_expr_is_const ($4, 1, 15))
	      { B2 (0x31, 0x0c); FE ($4, 8, 4); }
	  }

/* ---------------------------------------------------------------------- */

	| SHR A ',' EXPR
	  { if (check_expr_is_const ($4, 1, 7))
	      { B2 (0x31, 0x0a); FE ($4, 9, 3); }
	  }

	| SHRW AX ',' EXPR
	  { if (check_expr_is_const ($4, 1, 15))
	      { B2 (0x31, 0x0e); FE ($4, 8, 4); }
	  }

/* ---------------------------------------------------------------------- */

	| SKC
	  { B2 (0x61, 0xc8); rl78_relax (RL78_RELAX_BRANCH, 0); }

	| SKH
	  { B2 (0x61, 0xe3); rl78_relax (RL78_RELAX_BRANCH, 0); }

	| SKNC
	  { B2 (0x61, 0xd8); rl78_relax (RL78_RELAX_BRANCH, 0); }

	| SKNH
	  { B2 (0x61, 0xf3); rl78_relax (RL78_RELAX_BRANCH, 0); }

	| SKNZ
	  { B2 (0x61, 0xf8); rl78_relax (RL78_RELAX_BRANCH, 0); }

	| SKZ
	  { B2 (0x61, 0xe8); rl78_relax (RL78_RELAX_BRANCH, 0); }

/* ---------------------------------------------------------------------- */

	| STOP
	  { B2 (0x61, 0xfd); }

/* ---------------------------------------------------------------------- */

	| XCH A ',' regb_na
	  { if ($4 == 0) /* X */
	      { B1 (0x08); }
	    else
	      { B2 (0x61, 0x88); F ($4, 13, 3); }
	  }

	| XCH A ',' opt_es '!' EXPR
	  { B2 (0x61, 0xaa); O2 ($6); rl78_linkrelax_addr16 (); }

	| XCH A ',' opt_es '[' DE ']'
	  { B2 (0x61, 0xae); }

	| XCH A ',' opt_es '[' DE '+' EXPR ']'
	  { B2 (0x61, 0xaf); O1 ($8); }

	| XCH A ',' opt_es '[' HL ']'
	  { B2 (0x61, 0xac); }

	| XCH A ',' opt_es '[' HL '+' EXPR ']'
	  { B2 (0x61, 0xad); O1 ($8); }

	| XCH A ',' opt_es '[' HL '+' B ']'
	  { B2 (0x61, 0xb9); }

	| XCH A ',' opt_es '[' HL '+' C ']'
	  { B2 (0x61, 0xa9); }

	| XCH A ',' EXPR
	  { if (expr_is_sfr ($4))
	      { B2 (0x61, 0xab); O1 ($4); }
	    else if (expr_is_saddr ($4))
	      { B2 (0x61, 0xa8); SET_SA ($4); O1 ($4); }
	    else
	      NOT_SFR_OR_SADDR;
	  }

/* ---------------------------------------------------------------------- */

	| XCHW AX ',' regw_na
	  { B1 (0x31); F ($4, 5, 2); }

/* ---------------------------------------------------------------------- */

	; /* end of statement */

/* ---------------------------------------------------------------------- */

opt_es	: /* nothing */
	| ES ':'
	  { rl78_prefix (0x11); }
	;

regb	: X { $$ = 0; }
	| A { $$ = 1; }
	| C { $$ = 2; }
	| B { $$ = 3; }
	| E { $$ = 4; }
	| D { $$ = 5; }
	| L { $$ = 6; }
	| H { $$ = 7; }
	;

regb_na	: X { $$ = 0; }
	| C { $$ = 2; }
	| B { $$ = 3; }
	| E { $$ = 4; }
	| D { $$ = 5; }
	| L { $$ = 6; }
	| H { $$ = 7; }
	;

regw	: AX { $$ = 0; }
	| BC { $$ = 1; }
	| DE { $$ = 2; }
	| HL { $$ = 3; }
	;

regw_na	: BC { $$ = 1; }
	| DE { $$ = 2; }
	| HL { $$ = 3; }
	;

sfr	: SPL { $$ = 0xf8; }
	| SPH { $$ = 0xf9; }
	| PSW { $$ = 0xfa; }
	| CS  { $$ = 0xfc; }
	| ES  { $$ = 0xfd; }
	| PMC { $$ = 0xfe; }
	| MEM { $$ = 0xff; }
	;

/* ---------------------------------------------------------------------- */
/* Shortcuts for groups of opcodes with common encodings.                 */

addsub	: ADD  { $$ = 0x00; }
	| ADDC { $$ = 0x10; }
	| SUB  { $$ = 0x20; }
	| SUBC { $$ = 0x30; }
	| CMP  { $$ = 0x40; }
	| AND_ { $$ = 0x50; }
	| OR   { $$ = 0x60; }
	| XOR  { $$ = 0x70; }
	;

addsubw	: ADDW  { $$ = 0x00; }
	| SUBW  { $$ = 0x20; }
	| CMPW  { $$ = 0x40; }
	;

andor1	: AND1 { $$ = 0x05; rl78_bit_insn = 1; }
	| OR1  { $$ = 0x06; rl78_bit_insn = 1; }
	| XOR1 { $$ = 0x07; rl78_bit_insn = 1; }
	;

bt_bf	: BT { $$ = 0x02;    rl78_bit_insn = 1; rl78_linkrelax_branch (); }
	| BF { $$ = 0x04;    rl78_bit_insn = 1; rl78_linkrelax_branch (); }
	| BTCLR { $$ = 0x00; rl78_bit_insn = 1; }
	;

setclr1	: SET1 { $$ = 0; rl78_bit_insn = 1; }
	| CLR1 { $$ = 1; rl78_bit_insn = 1; }
	;

oneclrb	: ONEB { $$ = 0x00; }
	| CLRB { $$ = 0x10; }
	;

oneclrw	: ONEW { $$ = 0x00; }
	| CLRW { $$ = 0x10; }
	;

incdec	: INC { $$ = 0x00; }
	| DEC { $$ = 0x10; }
	;

incdecw	: INCW { $$ = 0x00; }
	| DECW { $$ = 0x10; }
	;

mov1	: MOV1 { rl78_bit_insn = 1; }
	;

%%
/* ====================================================================== */

static struct
{
  const char * string;
  int          token;
  int          val;
}
token_table[] =
{
  { "r0", X, 0 },
  { "r1", A, 1 },
  { "r2", C, 2 },
  { "r3", B, 3 },
  { "r4", E, 4 },
  { "r5", D, 5 },
  { "r6", L, 6 },
  { "r7", H, 7 },
  { "x", X, 0 },
  { "a", A, 1 },
  { "c", C, 2 },
  { "b", B, 3 },
  { "e", E, 4 },
  { "d", D, 5 },
  { "l", L, 6 },
  { "h", H, 7 },

  { "rp0", AX, 0 },
  { "rp1", BC, 1 },
  { "rp2", DE, 2 },
  { "rp3", HL, 3 },
  { "ax", AX, 0 },
  { "bc", BC, 1 },
  { "de", DE, 2 },
  { "hl", HL, 3 },

  { "RB0", RB0, 0 },
  { "RB1", RB1, 1 },
  { "RB2", RB2, 2 },
  { "RB3", RB3, 3 },

  { "sp", SP, 0 },
  { "cy", CY, 0 },

  { "spl", SPL, 0xf8 },
  { "sph", SPH, 0xf9 },
  { "psw", PSW, 0xfa },
  { "cs", CS, 0xfc },
  { "es", ES, 0xfd },
  { "pmc", PMC, 0xfe },
  { "mem", MEM, 0xff },

  { ".s", DOT_S, 0 },
  { ".b", DOT_B, 0 },
  { ".w", DOT_W, 0 },
  { ".l", DOT_L, 0 },
  { ".a", DOT_A , 0},
  { ".ub", DOT_UB, 0 },
  { ".uw", DOT_UW , 0},

  { "c", FLAG, 0 },
  { "z", FLAG, 1 },
  { "s", FLAG, 2 },
  { "o", FLAG, 3 },
  { "i", FLAG, 8 },
  { "u", FLAG, 9 },

#define OPC(x) { #x, x, IS_OPCODE }

  OPC(ADD),
  OPC(ADDC),
  OPC(ADDW),
  { "and", AND_, IS_OPCODE },
  OPC(AND1),
  OPC(BC),
  OPC(BF),
  OPC(BH),
  OPC(BNC),
  OPC(BNH),
  OPC(BNZ),
  OPC(BR),
  OPC(BRK),
  OPC(BRK1),
  OPC(BT),
  OPC(BTCLR),
  OPC(BZ),
  OPC(CALL),
  OPC(CALLT),
  OPC(CLR1),
  OPC(CLRB),
  OPC(CLRW),
  OPC(CMP),
  OPC(CMP0),
  OPC(CMPS),
  OPC(CMPW),
  OPC(DEC),
  OPC(DECW),
  OPC(DI),
  OPC(DIVHU),
  OPC(DIVWU),
  OPC(EI),
  OPC(HALT),
  OPC(INC),
  OPC(INCW),
  OPC(MACH),
  OPC(MACHU),
  OPC(MOV),
  OPC(MOV1),
  OPC(MOVS),
  OPC(MOVW),
  OPC(MULH),
  OPC(MULHU),
  OPC(MULU),
  OPC(NOP),
  OPC(NOT1),
  OPC(ONEB),
  OPC(ONEW),
  OPC(OR),
  OPC(OR1),
  OPC(POP),
  OPC(PUSH),
  OPC(RET),
  OPC(RETI),
  OPC(RETB),
  OPC(ROL),
  OPC(ROLC),
  OPC(ROLWC),
  OPC(ROR),
  OPC(RORC),
  OPC(SAR),
  OPC(SARW),
  OPC(SEL),
  OPC(SET1),
  OPC(SHL),
  OPC(SHLW),
  OPC(SHR),
  OPC(SHRW),
  OPC(SKC),
  OPC(SKH),
  OPC(SKNC),
  OPC(SKNH),
  OPC(SKNZ),
  OPC(SKZ),
  OPC(STOP),
  OPC(SUB),
  OPC(SUBC),
  OPC(SUBW),
  OPC(XCH),
  OPC(XCHW),
  OPC(XOR),
  OPC(XOR1),
};

#define NUM_TOKENS (sizeof (token_table) / sizeof (token_table[0]))

void
rl78_lex_init (char * beginning, char * ending)
{
  rl78_init_start = beginning;
  rl78_lex_start = beginning;
  rl78_lex_end = ending;
  rl78_in_brackets = 0;
  rl78_last_token = 0;

  rl78_bit_insn = 0;

  setbuf (stdout, 0);
}

/* Return a pointer to the '.' in a bit index expression (like
   foo.5), or NULL if none is found.  */
static char *
find_bit_index (char *tok)
{
  char *last_dot = NULL;
  char *last_digit = NULL;
  while (*tok && *tok != ',')
    {
      if (*tok == '.')
	{
	  last_dot = tok;
	  last_digit = NULL;
	}
      else if (*tok >= '0' && *tok <= '7'
	       && last_dot != NULL
	       && last_digit == NULL)
	{
	  last_digit = tok;
	}
      else if (ISSPACE (*tok))
	{
	  /* skip */
	}
      else
	{
	  last_dot = NULL;
	  last_digit = NULL;
	}
      tok ++;
    }
  if (last_dot != NULL
      && last_digit != NULL)
    return last_dot;
  return NULL;
}

static int
rl78_lex (void)
{
  /*unsigned int ci;*/
  char * save_input_pointer;
  char * bit = NULL;

  while (ISSPACE (*rl78_lex_start)
	 && rl78_lex_start != rl78_lex_end)
    rl78_lex_start ++;

  rl78_last_exp_start = rl78_lex_start;

  if (rl78_lex_start == rl78_lex_end)
    return 0;

  if (ISALPHA (*rl78_lex_start)
      || (*rl78_lex_start == '.' && ISALPHA (rl78_lex_start[1])))
    {
      unsigned int i;
      char * e;
      char save;

      for (e = rl78_lex_start + 1;
	   e < rl78_lex_end && ISALNUM (*e);
	   e ++)
	;
      save = *e;
      *e = 0;

      for (i = 0; i < NUM_TOKENS; i++)
	if (strcasecmp (rl78_lex_start, token_table[i].string) == 0
	    && !(token_table[i].val == IS_OPCODE && rl78_last_token != 0)
	    && !(token_table[i].token == FLAG && !need_flag))
	  {
	    rl78_lval.regno = token_table[i].val;
	    *e = save;
	    rl78_lex_start = e;
	    rl78_last_token = token_table[i].token;
	    return token_table[i].token;
	  }
      *e = save;
    }

  if (rl78_last_token == 0)
    {
      rl78_last_token = UNKNOWN_OPCODE;
      return UNKNOWN_OPCODE;
    }

  if (rl78_last_token == UNKNOWN_OPCODE)
    return 0;

  if (*rl78_lex_start == '[')
    rl78_in_brackets = 1;
  if (*rl78_lex_start == ']')
    rl78_in_brackets = 0;

  /* '.' is funny - the syntax includes it for bitfields, but only for
      bitfields.  We check for it specially so we can allow labels
      with '.' in them.  */

  if (rl78_bit_insn
      && *rl78_lex_start == '.'
      && find_bit_index (rl78_lex_start) == rl78_lex_start)
    {
      rl78_last_token = *rl78_lex_start;
      return *rl78_lex_start ++;
    }

  if ((rl78_in_brackets && *rl78_lex_start == '+')
      || strchr ("[],#!$:", *rl78_lex_start))
    {
      rl78_last_token = *rl78_lex_start;
      return *rl78_lex_start ++;
    }

  /* Again, '.' is funny.  Look for '.<digit>' at the end of the line
     or before a comma, which is a bitfield, not an expression.  */

  if (rl78_bit_insn)
    {
      bit = find_bit_index (rl78_lex_start);
      if (bit)
	*bit = 0;
      else
	bit = NULL;
    }

  save_input_pointer = input_line_pointer;
  input_line_pointer = rl78_lex_start;
  rl78_lval.exp.X_md = 0;
  expression (&rl78_lval.exp);

  if (bit)
    *bit = '.';

  rl78_lex_start = input_line_pointer;
  input_line_pointer = save_input_pointer;
  rl78_last_token = EXPR;
  return EXPR;
}

int
rl78_error (const char * str)
{
  int len;

  len = rl78_last_exp_start - rl78_init_start;

  as_bad ("%s", rl78_init_start);
  as_bad ("%*s^ %s", len, "", str);
  return 0;
}

static int
expr_is_sfr (expressionS exp)
{
  unsigned long v;

  if (exp.X_op != O_constant)
    return 0;

  v = exp.X_add_number;
  if (0xFFF00 <= v && v <= 0xFFFFF)
    return 1;
  return 0;
}

static int
expr_is_saddr (expressionS exp)
{
  unsigned long v;

  if (exp.X_op != O_constant)
    return 1;

  v = exp.X_add_number;
  if (0xFFE20 <= v && v <= 0xFFF1F)
    return 1;
  return 0;
}

static int
expr_is_word_aligned (expressionS exp)
{
  unsigned long v;

  if (exp.X_op != O_constant)
    return 1;

  v = exp.X_add_number;
  if (v & 1)
    return 0;
  return 1;

}

static void
check_expr_is_bit_index (expressionS exp)
{
  int val;

  if (exp.X_op != O_constant)
    {
      rl78_error (_("bit index must be a constant"));
      return;
    }
  val = exp.X_add_number;

  if (val < 0 || val > 7)
    rl78_error (_("rtsd size must be 0..7"));
}

static int
exp_val (expressionS exp)
{
  if (exp.X_op != O_constant)
  {
    rl78_error (_("constant expected"));
    return 0;
  }
  return exp.X_add_number;
}

static int
check_expr_is_const (expressionS e, int vmin, int vmax)
{
  static char buf[100];
  if (e.X_op != O_constant
      || e.X_add_number < vmin
      || e.X_add_number > vmax)
    {
      if (vmin == vmax)
	sprintf (buf, "%d expected here", vmin);
      else
	sprintf (buf, "%d..%d expected here", vmin, vmax);
      rl78_error(buf);
      return 0;
    }
  return 1;
}