aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/gen-idecode.c
blob: 87d76a060291515835057c924194bb24d98f67e1 (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
/*  This file is part of the program psim.

    Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>

    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 2 of the License, or
    (at your option) any later version.

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */

#include "misc.h"
#include "lf.h"
#include "table.h"

#include "filter.h"

#include "ld-decode.h"
#include "ld-cache.h"
#include "ld-insn.h"

#include "igen.h"

#include "gen-idecode.h"
#include "gen-icache.h"
#include "gen-semantics.h"



static void
lf_print_opcodes(lf *file,
		 insn_table *table)
{
  if (table != NULL) {
    while (1) {
      ASSERT(table->opcode != NULL);
      lf_printf(file, "_%d_%d",
		table->opcode->first,
		table->opcode->last);
      if (table->parent == NULL) break;
      lf_printf(file, "__%d", table->opcode_nr);
      table = table->parent;
    }
  }
}

/****************************************************************/


static void
lf_print_table_name(lf *file,
		    insn_table *table)
{
  lf_printf(file, "idecode_table");
  lf_print_opcodes(file, table);
}



static void
print_idecode_table(lf *file,
		    insn_table *entry,
		    const char *result)
{
  lf_printf(file, "/* prime the search */\n");
  lf_printf(file, "idecode_table_entry *table = ");
  lf_print_table_name(file, entry);
  lf_printf(file, ";\n");
  lf_printf(file, "int opcode = EXTRACTED32(instruction, %d, %d);\n",
	    i2target(hi_bit_nr, entry->opcode->first),
	    i2target(hi_bit_nr, entry->opcode->last));
  lf_printf(file, "idecode_table_entry *table_entry = table + opcode;\n");

  lf_printf(file, "\n");
  lf_printf(file, "/* iterate until a leaf */\n");
  lf_printf(file, "while (1) {\n");
  lf_printf(file, "  signed shift = table_entry->shift;\n");
  lf_printf(file, "if (shift == function_entry) break;\n");
  lf_printf(file, "  if (shift >= 0) {\n");
  lf_printf(file, "    table = ((idecode_table_entry*)\n");
  lf_printf(file, "             table_entry->function_or_table);\n");
  lf_printf(file, "    opcode = ((instruction & table_entry->mask)\n");
  lf_printf(file, "              >> shift);\n");
  lf_printf(file, "    table_entry = table + opcode;\n");
  lf_printf(file, "  }\n");
  lf_printf(file, "  else {\n");
  lf_printf(file, "    /* must be a boolean */\n");
  lf_printf(file, "    ASSERT(table_entry->shift == boolean_entry);\n");
  lf_printf(file, "    opcode = ((instruction & table_entry->mask)\n");
  lf_printf(file, "              != table_entry->value);\n");
  lf_printf(file, "    table = ((idecode_table_entry*)\n");
  lf_printf(file, "             table_entry->function_or_table);\n");
  lf_printf(file, "    table_entry = table + opcode;\n");
  lf_printf(file, "  }\n");
  lf_printf(file, "}\n");

  lf_printf(file, "\n");
  lf_printf(file, "/* call the leaf code */\n");
  if ((code & generate_jumps)) {
    lf_printf(file, "goto *table_entry->function_or_table;\n");
  }
  else {
    lf_printf(file, "%s ", result);
    if ((code & generate_with_icache)) {
      lf_printf(file, "(((idecode_icache*)table_entry->function_or_table)\n");
      lf_printf(file, "  (%s));\n", ICACHE_FUNCTION_ACTUAL);
    }
    else {
      lf_printf(file, "((idecode_semantic*)table_entry->function_or_table)\n");
      lf_printf(file, "  (%s);\n", SEMANTIC_FUNCTION_ACTUAL);
    }
  }
}


static void
print_idecode_table_start(insn_table *table,
			  lf *file,
			  void *data,
			  int depth)
{
  ASSERT(depth == 0);
  /* start of the table */
  if (table->opcode_rule->gen == array_gen) {
    lf_printf(file, "\n");
    lf_printf(file, "static idecode_table_entry ");
    lf_print_table_name(file, table);
    lf_printf(file, "[] = {\n");
  }
}

static void
print_idecode_table_leaf(insn_table *entry,
			 lf *file,
			 void *data,
			 insn *instruction,
			 int depth)
{
  ASSERT(entry->parent != NULL);
  ASSERT(depth == 0);

  /* add an entry to the table */
  if (entry->parent->opcode_rule->gen == array_gen) {
    lf_printf(file, "  /*%d*/ { ", entry->opcode_nr);
    if (entry->opcode == NULL) {
      /* table leaf entry */
      lf_printf(file, "function_entry, 0, 0, ");
      if ((code & generate_jumps))
	lf_printf(file, "&&");
      print_function_name(file,
			  entry->insns->file_entry->fields[insn_name],
			  entry->expanded_bits,
			  ((code & generate_with_icache)
			   ? function_name_prefix_icache
			   : function_name_prefix_semantics));
    }
    else if (entry->opcode_rule->gen == switch_gen
	     || entry->opcode_rule->gen == goto_switch_gen
	     || entry->opcode_rule->gen == padded_switch_gen) {
      /* table calling switch statement */
      lf_printf(file, "function_entry, 0, 0, ");
      if ((code & generate_jumps))
	lf_printf(file, "&&");
      lf_print_table_name(file, entry);
    }
    else if (entry->opcode->is_boolean) {
      /* table `calling' boolean table */
      lf_printf(file, "boolean_entry, ");
      lf_printf(file, "MASK32(%d, %d), ",
		i2target(hi_bit_nr, entry->opcode->first),
		i2target(hi_bit_nr, entry->opcode->last));
      lf_printf(file, "INSERTED32(%d, %d, %d), ",
		entry->opcode->boolean_constant,
		i2target(hi_bit_nr, entry->opcode->first),
		i2target(hi_bit_nr, entry->opcode->last));
      lf_print_table_name(file, entry);
    }
    else {
      /* table `calling' another table */
      lf_printf(file, "%d, ", insn_bit_size - entry->opcode->last - 1);
      lf_printf(file, "MASK32(%d,%d), ",
		i2target(hi_bit_nr, entry->opcode->first),
		i2target(hi_bit_nr, entry->opcode->last));
      lf_printf(file, "0, ");
      lf_print_table_name(file, entry);
    }
    lf_printf(file, " },\n");
  }
}

static void
print_idecode_table_end(insn_table *table,
			lf *file,
			void *data,
			int depth)
{
  ASSERT(depth == 0);
  if (table->opcode_rule->gen == array_gen) {
    lf_printf(file, "};\n");
  }
}

static void
print_idecode_table_padding(insn_table *table,
			    lf *file,
			    void *data,
			    int depth,
			    int opcode_nr)
{
  ASSERT(depth == 0);
  if (table->opcode_rule->gen == array_gen) {
    lf_printf(file, "  /*%d*/ { function_entry, 0, 0, ", opcode_nr);
    if ((code & generate_jumps))
      lf_printf(file, "&&");
    lf_printf(file, "%s_illegal },\n",
	      ((code & generate_with_icache) ? "icache" : "semantic"));
  }
}


/****************************************************************/


static void
print_goto_switch_name(lf *file,
		       insn_table *entry)
{
  lf_printf(file, "case_");
  if (entry->opcode == NULL)
    print_function_name(file,
			entry->insns->file_entry->fields[insn_name],
			entry->expanded_bits,
			((code & generate_with_icache)
			 ? function_name_prefix_icache
			 : function_name_prefix_semantics));
  else
    lf_print_table_name(file, entry);
}

static void
print_goto_switch_table_leaf(insn_table *entry,
			     lf *file,
			     void *data,
			     insn *instruction,
			     int depth)
{
  ASSERT(entry->parent != NULL);
  ASSERT(depth == 0);
  ASSERT(entry->parent->opcode_rule->gen == goto_switch_gen);
  ASSERT(entry->parent->opcode);

  lf_printf(file, "&&");
  print_goto_switch_name(file, entry);
  lf_printf(file, ",\n");
}

static void
print_goto_switch_table_padding(insn_table *table,
				lf *file,
				void *data,
				int depth,
				int opcode_nr)
{
  ASSERT(depth == 0);
  ASSERT(table->opcode_rule->gen == goto_switch_gen);

  lf_printf(file, "&&illegal_");
  lf_print_table_name(file, table);
  lf_printf(file, ",\n");
}

static void
print_goto_switch_break(lf *file,
			insn_table *entry)
{
  lf_printf(file, "goto break_");
  lf_print_table_name(file, entry->parent);
  lf_printf(file, ";\n");
}


static void
print_goto_switch_table(lf *file,
			insn_table *table)
{
  lf_printf(file, "const static void *");
  lf_print_table_name(file, table);
  lf_printf(file, "[] = {\n");
  lf_indent(file, +2);
  insn_table_traverse_tree(table,
			   file, NULL/*data*/,
			   0,
			   NULL/*start*/,
			   print_goto_switch_table_leaf,
			   NULL/*end*/,
			   print_goto_switch_table_padding);
  lf_indent(file, -2);
  lf_printf(file, "};\n");
}


void print_idecode_switch
(lf *file, 
 insn_table *table,
 const char *result);

static void
idecode_switch_start(insn_table *table,
		     lf *file,
		     void *data,
		     int depth)
{
  /* const char *result = data; */
  ASSERT(depth == 0);
  ASSERT(table->opcode_rule->gen == switch_gen
	 || table->opcode_rule->gen == goto_switch_gen
	 || table->opcode_rule->gen == padded_switch_gen);

  if (table->opcode->is_boolean
      || table->opcode_rule->gen == switch_gen
	 || table->opcode_rule->gen == padded_switch_gen) {
    lf_printf(file, "switch (EXTRACTED32(instruction, %d, %d)) {\n",
	      i2target(hi_bit_nr, table->opcode->first),
	      i2target(hi_bit_nr, table->opcode->last));
  }
  else if (table->opcode_rule->gen == goto_switch_gen) {
    if (table->parent != NULL
	&& (table->parent->opcode_rule->gen == switch_gen
	    || table->parent->opcode_rule->gen == goto_switch_gen
	    || table->parent->opcode_rule->gen == padded_switch_gen)) {
      lf_printf(file, "{\n");
      lf_indent(file, +2);
    }
    print_goto_switch_table(file, table);
    lf_printf(file, "ASSERT(EXTRACTED32(instruction, %d, %d)\n",
	      i2target(hi_bit_nr, table->opcode->first),
	      i2target(hi_bit_nr, table->opcode->last));
    lf_printf(file, "       < (sizeof(");
    lf_print_table_name(file, table);
    lf_printf(file, ") / sizeof(void*)));\n");
    lf_printf(file, "goto *");
    lf_print_table_name(file, table);
    lf_printf(file, "[EXTRACTED32(instruction, %d, %d)];\n",
	      i2target(hi_bit_nr, table->opcode->first),
	      i2target(hi_bit_nr, table->opcode->last));
  }
  else {
    ASSERT("bad switch" == NULL);
  }
}


static void
idecode_switch_leaf(insn_table *entry,
		    lf *file,
		    void *data,
		    insn *instruction,
		    int depth)
{
  const char *result = data;
  ASSERT(entry->parent != NULL);
  ASSERT(depth == 0);
  ASSERT(entry->parent->opcode_rule->gen == switch_gen
	 || entry->parent->opcode_rule->gen == goto_switch_gen
	 || entry->parent->opcode_rule->gen == padded_switch_gen);
  ASSERT(entry->parent->opcode);

  if (entry->parent->opcode->is_boolean
      && entry->opcode_nr == 0) {
    /* boolean false target */
    lf_printf(file, "case %d:\n", entry->parent->opcode->boolean_constant);
  }
  else if (entry->parent->opcode->is_boolean
	   && entry->opcode_nr != 0) {
    /* boolean true case */
    lf_printf(file, "default:\n");
  }
  else if (entry->parent->opcode_rule->gen == switch_gen
	   || entry->parent->opcode_rule->gen == padded_switch_gen) {
    /* normal goto */
    lf_printf(file, "case %d:\n", entry->opcode_nr);
  }
  else if (entry->parent->opcode_rule->gen == goto_switch_gen) {
    /* lf_indent(file, -1); */
    print_goto_switch_name(file, entry);
    lf_printf(file, ":\n");
    /* lf_indent(file, +1); */
  }
  else {
    ASSERT("bad switch" == NULL);
  }
  lf_indent(file, +2);
  {
    if (entry->opcode == NULL) {
      /* switch calling leaf */
      if ((code & generate_jumps))
	lf_printf(file, "goto ");
      if ((code & generate_calls))
	lf_printf(file, "%s ", result);
      print_function_name(file,
			  entry->insns->file_entry->fields[insn_name],
			  entry->expanded_bits,
			  ((code & generate_with_icache)
			   ? function_name_prefix_icache
			   : function_name_prefix_semantics));
      if ((code & generate_calls))
	lf_printf(file, "(%s)", SEMANTIC_FUNCTION_ACTUAL);
      lf_printf(file, ";\n");
    }
    else if (entry->opcode_rule->gen == switch_gen
	     || entry->opcode_rule->gen == goto_switch_gen
	     || entry->opcode_rule->gen == padded_switch_gen) {
      /* switch calling switch */
      print_idecode_switch(file, entry, result);
    }
    else {
      /* switch looking up a table */
      lf_printf(file, "{\n");
      lf_indent(file, -2);
      print_idecode_table(file, entry, result);
      lf_indent(file, -2);
      lf_printf(file, "}\n");
    }
    if (entry->parent->opcode->is_boolean
	|| entry->parent->opcode_rule->gen == switch_gen
	|| entry->parent->opcode_rule->gen == padded_switch_gen) {
      lf_printf(file, "break;\n");
    }
    else if (entry->parent->opcode_rule->gen == goto_switch_gen) {
      print_goto_switch_break(file, entry);
    }
    else {
      ASSERT("bad switch" == NULL);
    }
  }
  lf_indent(file, -2);
}


static void
print_idecode_switch_illegal(lf *file,
			     const char *result)
{
  lf_indent(file, +2);
  print_idecode_illegal(file, result);
  lf_printf(file, "break;\n");
  lf_indent(file, -2);
}

static void
idecode_switch_end(insn_table *table,
		   lf *file,
		   void *data,
		   int depth)
{
  const char *result = data;
  ASSERT(depth == 0);
  ASSERT(table->opcode_rule->gen == switch_gen
	 || table->opcode_rule->gen == goto_switch_gen
	 || table->opcode_rule->gen == padded_switch_gen);
  ASSERT(table->opcode);

  if (table->opcode->is_boolean) {
    lf_printf(file, "}\n");
  }
  else if (table->opcode_rule->gen == switch_gen
	   || table->opcode_rule->gen == padded_switch_gen) {
    lf_printf(file, "default:\n");
    switch (table->opcode_rule->gen) {
    case switch_gen:
      print_idecode_switch_illegal(file, result);
      break;
    case padded_switch_gen:
      lf_printf(file, "  error(\"Internal error - bad switch generated\\n\");\n");
      lf_printf(file, "  break;\n");
      break;
    default:
      ASSERT("bad switch" == NULL);
    }
    lf_printf(file, "}\n");
  }
  else if (table->opcode_rule->gen == goto_switch_gen) {
    lf_printf(file, "illegal_");
    lf_print_table_name(file, table);
    lf_printf(file, ":\n");
    print_idecode_illegal(file, result);
    lf_printf(file, "break_");
    lf_print_table_name(file, table);
    lf_printf(file, ":;\n");
    if (table->parent != NULL
	&& (table->parent->opcode_rule->gen == switch_gen
	    || table->parent->opcode_rule->gen == goto_switch_gen
	    || table->parent->opcode_rule->gen == padded_switch_gen)) {
      lf_indent(file, -2);
      lf_printf(file, "}\n");
    }
  }
  else {
    ASSERT("bad switch" == NULL);
  }
}

static void
idecode_switch_padding(insn_table *table,
		       lf *file,
		       void *data,
		       int depth,
		       int opcode_nr)
{
  const char *result = data;
  ASSERT(depth == 0);
  ASSERT(table->opcode_rule->gen == switch_gen
	 || table->opcode_rule->gen == goto_switch_gen
	 || table->opcode_rule->gen == padded_switch_gen);

  switch (table->opcode_rule->gen) {
  case switch_gen:
    break;
  case padded_switch_gen:
    lf_printf(file, "case %d:\n", opcode_nr);
    print_idecode_switch_illegal(file, result);
    break;
  case goto_switch_gen:
    /* no padding needed */
    break;
  default:
    ASSERT("bad switch" != NULL);
  }
}


void
print_idecode_switch(lf *file, 
		     insn_table *table,
		     const char *result)
{
  insn_table_traverse_tree(table,
			   file, (void*)result,
			   0,
			   idecode_switch_start,
			   idecode_switch_leaf,
			   idecode_switch_end,
			   idecode_switch_padding);
}


static void
print_idecode_switch_function_header(lf *file,
				     insn_table *table,
				     int is_function_definition)
{
  lf_printf(file, "\n");
  if ((code & generate_calls)) {
    lf_printf(file, "static ");
    if ((code & generate_with_icache))
      lf_printf(file, "idecode_semantic *");
    else
      lf_printf(file, "unsigned_word");
    if (is_function_definition)
      lf_printf(file, "\n");
    else
      lf_printf(file, " ");
    lf_print_table_name(file, table);
    lf_printf(file, "\n(%s)", ICACHE_FUNCTION_FORMAL);
    if (!is_function_definition)
      lf_printf(file, ";");
    lf_printf(file, "\n");
  }
  if ((code & generate_jumps) && is_function_definition) {
    lf_indent(file, -1);
    lf_print_table_name(file, table);
    lf_printf(file, ":\n");
    lf_indent(file, +1);
  }
}


static void
idecode_declare_if_switch(insn_table *table,
			  lf *file,
			  void *data,
			  int depth)
{
  if ((table->opcode_rule->gen == switch_gen
       || table->opcode_rule->gen == goto_switch_gen
       || table->opcode_rule->gen == padded_switch_gen)
      && table->parent != NULL /* don't declare the top one yet */
      && table->parent->opcode_rule->gen == array_gen) {
    print_idecode_switch_function_header(file,
					 table,
					 0/*isnt function definition*/);
  }
}


static void
idecode_expand_if_switch(insn_table *table,
			 lf *file,
			 void *data,
			 int depth)
{
  if ((table->opcode_rule->gen == switch_gen
       || table->opcode_rule->gen == goto_switch_gen
       || table->opcode_rule->gen == padded_switch_gen)
      && table->parent != NULL /* don't expand the top one yet */
      && table->parent->opcode_rule->gen == array_gen) {
    print_idecode_switch_function_header(file,
					    table,
					    1/*is function definition*/);
    if ((code & generate_calls)) {
      lf_printf(file, "{\n");
      lf_indent(file, +2);
    }
    print_idecode_switch(file, table, "return");
    if ((code & generate_calls)) {
      lf_indent(file, -2);
      lf_printf(file, "}\n");
    }
  }
}


/****************************************************************/


static void
print_idecode_lookups(lf *file,
		      insn_table *table,
		      cache_table *cache_rules)
{
  int depth;

  /* output switch function declarations where needed by tables */
  insn_table_traverse_tree(table,
			   file, NULL,
			   1,
			   idecode_declare_if_switch, /* START */
			   NULL, NULL, NULL);
  
  /* output tables where needed */
  for (depth = insn_table_depth(table);
       depth > 0;
       depth--) {
    insn_table_traverse_tree(table,
			     file, NULL,
			     1-depth,
			     print_idecode_table_start,
			     print_idecode_table_leaf,
			     print_idecode_table_end,
			     print_idecode_table_padding);
  }
  
  /* output switch functions where needed */
  insn_table_traverse_tree(table,
			   file, NULL,
			   1,
			   idecode_expand_if_switch, /* START */
			   NULL, NULL, NULL);
}


static void
print_idecode_body(lf *file,
		   insn_table *table,
		   const char *result)
{
  if (table->opcode_rule->gen == switch_gen
      || table->opcode_rule->gen == goto_switch_gen
      || table->opcode_rule->gen == padded_switch_gen)
    print_idecode_switch(file, table, result);
  else
    print_idecode_table(file, table, result);
}


/****************************************************************/


static void
print_run_until_stop_body(lf *file,
			  insn_table *table,
			  int can_stop)
{
  /* Output the function to execute real code:

     Unfortunatly, there are multiple cases to consider vis:

     <icache> X <smp> X <events> X <keep-running-flag> X ...

     Consequently this function is written in multiple different ways */

  lf_putstr(file, "{\n");
  lf_indent(file, +2);
  lf_putstr(file, "jmp_buf halt;\n");
  lf_putstr(file, "jmp_buf restart;\n");
  if (!generate_smp) {
    lf_putstr(file, "cpu *processor = NULL;\n");
    lf_putstr(file, "unsigned_word cia = -1;\n");
  }
  lf_putstr(file, "int last_cpu;\n");
  if (generate_smp) {
    lf_putstr(file, "int current_cpu;\n");
  }

  if ((code & generate_with_icache)) {
    lf_putstr(file, "int cpu_nr;\n");
    lf_putstr(file, "\n");
    lf_putstr(file, "/* flush the icache of a possible break insn */\n");
    lf_putstr(file, "for (cpu_nr = 0; cpu_nr < nr_cpus; cpu_nr++)\n");
    lf_putstr(file, "  cpu_flush_icache(processors[cpu_nr]);\n");
  }

  lf_putstr(file, "\n");
  lf_putstr(file, "/* set the halt target initially */\n");
  lf_putstr(file, "psim_set_halt_and_restart(system, &halt, NULL);\n");
  lf_putstr(file, "if (setjmp(halt))\n");
  lf_putstr(file, "  return;\n");

  lf_putstr(file, "\n");
  lf_putstr(file, "/* where were we before the halt? */\n");
  lf_putstr(file, "last_cpu = psim_last_cpu(system);\n");

  lf_putstr(file, "\n");
  lf_putstr(file, "/* check for need to force event processing first */\n");
  lf_putstr(file, "if (WITH_EVENTS) {\n");
  lf_putstr(file, "  if (last_cpu == nr_cpus) {\n");
  lf_putstr(file, "    /* halted during event processing */\n");
  lf_putstr(file, "    event_queue_process(events);\n");
  lf_putstr(file, "    last_cpu = -1;\n");
  lf_putstr(file, "  }\n");
  lf_putstr(file, "  else if (last_cpu == nr_cpus - 1) {\n");
  lf_putstr(file, "    /* last cpu did halt */\n");
  lf_putstr(file, "    if (event_queue_tick(events)) {\n");
  lf_putstr(file, "      event_queue_process(events);\n");
  lf_putstr(file, "    }\n");
  lf_putstr(file, "    last_cpu = -1;\n");
  lf_putstr(file, "  }\n");
  lf_putstr(file, "}\n");
  lf_putstr(file, "else {\n");
  lf_putstr(file, " if (last_cpu == nr_cpus - 1)\n");
  lf_putstr(file, "   /* cpu zero is next */\n");
  lf_putstr(file, "   last_cpu = -1;\n");
  lf_putstr(file, "}\n");

  lf_putstr(file, "\n");
  lf_putstr(file, "/* have ensured that the event queue can not be first */\n");
  lf_putstr(file, "ASSERT(last_cpu >= -1 && last_cpu < nr_cpus - 1);\n");

  if (!generate_smp) {

    lf_putstr(file, "
/* CASE 1: NO SMP (with or with out instruction cache).

   In this case, we can take advantage of the fact that the current
   instruction address does not need to be returned to the cpu object
   after every execution of an instruction.  Instead it only needs to
   be saved when either A. the main loop exits or B. A cpu-halt or
   cpu-restart call forces the loop to be re-enered.  The later
   functions always save the current cpu instruction address.

   Two subcases also exist that with and that without an instruction
   cache.

   An additional complexity is the need to ensure that a 1:1 ratio
   is maintained between the execution of an instruction and the
   incrementing of the simulation clock */");

    lf_putstr(file, "\n");

    lf_putstr(file, "\n");
    lf_putstr(file, "/* now add restart target as ready to run */\n");
    lf_putstr(file, "psim_set_halt_and_restart(system, &halt, &restart);\n");
    lf_putstr(file, "if (setjmp(restart)) {\n");
    lf_putstr(file, "  if (WITH_EVENTS) {\n");
    lf_putstr(file, "    /* when restart, cpu must have been last, clock next */\n");
    lf_putstr(file, "    if (event_queue_tick(events)) {\n");
    lf_putstr(file, "      event_queue_process(events);\n");
    lf_putstr(file, "    }\n");
    lf_putstr(file, "  }\n");
    lf_putstr(file, "}\n");

    lf_putstr(file, "\n");
    lf_putstr(file, "/* prime the main loop */\n");
    lf_putstr(file, "processor = processors[0];\n");
    lf_putstr(file, "cia = cpu_get_program_counter(processor);\n");

    lf_putstr(file, "\n");
    lf_putstr(file, "while (1) {\n");
    lf_indent(file, +2);

    if (!(code & generate_with_icache)) {
      lf_putstr(file, "instruction_word instruction =\n");
      lf_putstr(file, "  vm_instruction_map_read(cpu_instruction_map(processor), processor, cia);\n");
      lf_putstr(file, "\n");
      print_idecode_body(file, table, "cia =");;
    }

    if ((code & generate_with_icache)) {
      lf_putstr(file, "idecode_cache *cache_entry =\n");
      lf_putstr(file, "  cpu_icache_entry(processor, cia);\n");
      lf_putstr(file, "if (cache_entry->address == cia) {\n");
      lf_putstr(file, "  /* cache hit */\n");
      lf_putstr(file, "  idecode_semantic *const semantic = cache_entry->semantic;\n");
      lf_putstr(file, "  cia = semantic(processor, cache_entry, cia);\n");
      /* tail */
      if (can_stop) {
	lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
	lf_putstr(file, "  cpu_halt(processor, cia, was_continuing, 0/*ignore*/);\n");
      }
      lf_putstr(file, "}\n");
      lf_putstr(file, "else {\n");
      lf_putstr(file, "  /* cache miss */\n");
      if (!(code & generate_with_semantic_icache)) {
	lf_indent(file, +2);
	lf_putstr(file, "idecode_semantic *semantic;\n");
	lf_indent(file, -2);
      }
      lf_putstr(file, "  instruction_word instruction =\n");
      lf_putstr(file, "    vm_instruction_map_read(cpu_instruction_map(processor), processor, cia);\n");
      lf_putstr(file, "  if (WITH_MON != 0)\n");
      lf_putstr(file, "    mon_event(mon_event_icache_miss, processor, cia);\n");
      if ((code & generate_with_semantic_icache)) {
	lf_putstr(file, "{\n");
	lf_indent(file, +2);
	print_idecode_body(file, table, "cia =");
	lf_indent(file, -2);
	lf_putstr(file, "}\n");
      }
      else {
	print_idecode_body(file, table, "semantic =");
	lf_putstr(file, "  cia = semantic(processor, cache_entry, cia);\n");
      }
      lf_putstr(file, "}\n");
    }

    /* events */
    lf_putstr(file, "\n");
    lf_putstr(file, "/* process any events */\n");
    lf_putstr(file, "if (WITH_EVENTS) {\n");
    lf_putstr(file, "  if (event_queue_tick(events)) {\n");
    lf_putstr(file, "    cpu_set_program_counter(processor, cia);\n");
    lf_putstr(file, "    event_queue_process(events);\n");
    lf_putstr(file, "    cia = cpu_get_program_counter(processor);\n");
    lf_putstr(file, "  }\n");
    lf_putstr(file, "}\n");

    /* tail */
    if (can_stop) {
      lf_putstr(file, "\n");
      lf_putstr(file, "/* abort if necessary */\n");
      lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
      lf_putstr(file, "  cpu_halt(processor, cia, was_continuing, 0/*not important*/);\n");
    }

    lf_indent(file, -2);
    lf_putstr(file, "}\n");
  }
    
  if (generate_smp) {

    lf_putstr(file, "
/* CASE 2: SMP (With or without ICACHE)

   The complexity here comes from needing to correctly restart the
   system when it is aborted.  In particular if cpu0 requests a
   restart, the next cpu is still cpu1.  Cpu0 being restarted after
   all the other CPU's and the event queue have been processed */");

    lf_putstr(file, "\n");

    lf_putstr(file, "\n");
    lf_putstr(file, "/* now establish the restart target */\n");
    lf_putstr(file, "psim_set_halt_and_restart(system, &halt, &restart);\n");
    lf_putstr(file, "if (setjmp(restart)) {\n");
    lf_putstr(file, "  current_cpu = psim_last_cpu(system);\n");
    lf_putstr(file, "  ASSERT(current_cpu >= 0 && current_cpu < nr_cpus);\n");
    lf_putstr(file, "}\n");
    lf_putstr(file, "else {\n");
    lf_putstr(file, "  current_cpu = last_cpu;\n");
    lf_putstr(file, "  ASSERT(current_cpu >= -1 && current_cpu < nr_cpus);\n");
    lf_putstr(file, "}\n");
    

    lf_putstr(file, "\n");
    lf_putstr(file, "while (1) {\n");
    lf_indent(file, +2);

    lf_putstr(file, "\n");
    lf_putstr(file, "if (WITH_EVENTS) {\n");
    lf_putstr(file, "  current_cpu += 1;\n");
    lf_putstr(file, "  if (current_cpu == nr_cpus) {\n");
    lf_putstr(file, "    if (event_queue_tick(events)) {\n");
    lf_putstr(file, "      event_queue_process(events);\n");
    lf_putstr(file, "    }\n");
    lf_putstr(file, "    current_cpu = 0;\n");
    lf_putstr(file, "  }\n");
    lf_putstr(file, "}\n");
    lf_putstr(file, "else {\n");
    lf_putstr(file, "  current_cpu = (current_cpu + 1) % nr_cpus;\n");
    lf_putstr(file, "}\n");

    lf_putstr(file, "\n");
    lf_putstr(file, "{\n");
    lf_indent(file, +2);
    lf_putstr(file, "cpu *processor = processors[current_cpu];\n");
    lf_putstr(file, "unsigned_word cia =\n");
    lf_putstr(file, "  cpu_get_program_counter(processor);\n");

    if (!(code & generate_with_icache)) {
      lf_putstr(file, "instruction_word instruction =\n");
      lf_putstr(file, "  vm_instruction_map_read(cpu_instruction_map(processor), processor, cia);\n");
      print_idecode_body(file, table, "cia =");
      if (can_stop) {
	lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
	lf_putstr(file, "  cpu_halt(processor, cia, was_continuing, 0/*ignore*/);\n");
      }
      lf_putstr(file, "cpu_set_program_counter(processor, cia);\n");
    }

    if ((code & generate_with_icache)) {
      lf_putstr(file, "idecode_cache *cache_entry =\n");
      lf_putstr(file, "  cpu_icache_entry(processor, cia);\n");
      lf_putstr(file, "\n");
      lf_putstr(file, "if (cache_entry->address == cia) {\n");
      {
	lf_indent(file, +2);
	lf_putstr(file, "\n");
	lf_putstr(file, "/* cache hit */\n");
	lf_putstr(file, "idecode_semantic *semantic = cache_entry->semantic;\n");
	lf_putstr(file, "cia = semantic(processor, cache_entry, cia);\n");
	/* tail */
	if (can_stop) {
	  lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
	  lf_putstr(file, "  cpu_halt(processor, cia, was_continuing, 0/*ignore-signal*/);\n");
	}
	lf_putstr(file, "cpu_set_program_counter(processor, cia);\n");
	lf_putstr(file, "\n");
	lf_indent(file, -2);
      }
      lf_putstr(file, "}\n");
      lf_putstr(file, "else {\n");
      {
	lf_indent(file, +2);
	lf_putstr(file, "\n");
	lf_putstr(file, "/* cache miss */\n");
	if (!(code & generate_with_semantic_icache)) {
	  lf_putstr(file, "idecode_semantic *semantic;\n");
	}
	lf_putstr(file, "instruction_word instruction =\n");
	lf_putstr(file, "  vm_instruction_map_read(cpu_instruction_map(processor), processor, cia);\n");
	lf_putstr(file, "if (WITH_MON != 0)\n");
	lf_putstr(file, "  mon_event(mon_event_icache_miss, processors[current_cpu], cia);\n");
	if ((code & generate_with_semantic_icache)) {
	  lf_putstr(file, "{\n");
	  lf_indent(file, +2);
	  print_idecode_body(file, table, "cia =");
	  lf_indent(file, -2);
	  lf_putstr(file, "}\n");
	}
	else {
	  print_idecode_body(file, table, "semantic = ");
	  lf_putstr(file, "cia = semantic(processor, cache_entry, cia);\n");
	}
	/* tail */
	if (can_stop) {
	  lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
	  lf_putstr(file, "  cpu_halt(processor, cia, was_continuing, 0/*ignore-signal*/);\n");
	}
	lf_putstr(file, "cpu_set_program_counter(processor, cia);\n");
	lf_putstr(file, "\n");
	lf_indent(file, -2);
      }
      lf_putstr(file, "}\n");
    }

    /* close */
    lf_indent(file, -2);
    lf_putstr(file, "}\n");

    /* tail */
    lf_indent(file, -2);
    lf_putstr(file, "}\n");
  }


  lf_indent(file, -2);
  lf_putstr(file, "}\n");
}


/****************************************************************/

static void
print_jump(lf *file,
	   int is_tail)
{
  if (is_tail) {
    lf_putstr(file, "if (keep_running != NULL && !*keep_running)\n");
    lf_putstr(file, "  cpu_halt(processor, nia, was_continuing, 0/*na*/);\n");
  }
  
  if (!generate_smp) {
    lf_putstr(file, "if (WITH_EVENTS) {\n");
    lf_putstr(file, "  if (event_queue_tick(events)) {\n");
    lf_putstr(file, "    cpu_set_program_counter(processor, nia);\n");
    lf_putstr(file, "    event_queue_process(events);\n");
    lf_putstr(file, "    nia = cpu_get_program_counter(processor);\n");
    lf_putstr(file, "  }\n");
    lf_putstr(file, "}\n");
  }

  if (generate_smp) {
    if (is_tail)
      lf_putstr(file, "cpu_set_program_counter(processor, nia);\n");
    lf_putstr(file, "if (WITH_EVENTS) {\n");
    lf_putstr(file, "  current_cpu += 1;\n");
    lf_putstr(file, "  if (current_cpu >= nr_cpus) {\n");
    lf_putstr(file, "    if (event_queue_tick(events)) {\n");
    lf_putstr(file, "      event_queue_process(events);\n");
    lf_putstr(file, "    }\n");
    lf_putstr(file, "    current_cpu = 0;\n");
    lf_putstr(file, "  }\n");
    lf_putstr(file, "}\n");
    lf_putstr(file, "else {\n");
    lf_putstr(file, "  current_cpu = (current_cpu + 1) % nr_cpus;\n");
    lf_putstr(file, "}\n");
    lf_putstr(file, "processor = processors[current_cpu];\n");
    lf_putstr(file, "nia = cpu_get_program_counter(processor);\n");
  }

  if ((code & generate_with_icache)) {
    lf_putstr(file, "cache_entry = cpu_icache_entry(processor, nia);\n");
    lf_putstr(file, "if (cache_entry->address == nia) {\n");
    lf_putstr(file, "  /* cache hit */\n");
    lf_putstr(file, "  goto *cache_entry->semantic;\n");
    lf_putstr(file, "}\n");
    if (is_tail) {
      lf_putstr(file, "goto cache_miss;\n");
    }
  }

  if (!(code & generate_with_icache) && is_tail) {
    lf_printf(file, "goto idecode;\n");
  }

}





static void
print_jump_insn(lf *file,
		insn *instruction,
		insn_bits *expanded_bits,
		opcode_field *opcodes,
		cache_table *cache_rules)
{

  /* what we are for the moment */
  lf_printf(file, "\n");
  print_my_defines(file, expanded_bits, instruction->file_entry);

  /* output the icache entry */
  if ((code & generate_with_icache)) {
    lf_printf(file, "\n");
    lf_indent(file, -1);
    print_function_name(file,
			instruction->file_entry->fields[insn_name],
			expanded_bits,
			function_name_prefix_icache);
    lf_printf(file, ":\n");
    lf_indent(file, +1);
    lf_printf(file, "{\n");
    lf_indent(file, +2);
    lf_putstr(file, "const unsigned_word cia = nia;\n");
    print_itrace(file, instruction->file_entry, 1/*putting-value-in-cache*/);
    print_idecode_validate(file, instruction, opcodes);
    lf_printf(file, "\n");
    lf_printf(file, "{\n");
    lf_indent(file, +2);
    print_icache_body(file,
		      instruction,
		      expanded_bits,
		      cache_rules,
		      0, /*use_defines*/
		      put_values_in_icache);
    lf_printf(file, "cache_entry->address = nia;\n");
    lf_printf(file, "cache_entry->semantic = &&");
    print_function_name(file,
			instruction->file_entry->fields[insn_name],
			expanded_bits,
			function_name_prefix_semantics);
    lf_printf(file, ";\n");
    if ((code & generate_with_semantic_icache)) {
      print_semantic_body(file,
			  instruction,
			  expanded_bits,
			  opcodes);
      print_jump(file, 1/*is-tail*/);
    }
    else {
      lf_printf(file, "/* goto ");
      print_function_name(file,
			  instruction->file_entry->fields[insn_name],
			  expanded_bits,
			  function_name_prefix_semantics);
      lf_printf(file, "; */\n");
    }
    lf_indent(file, -2);
    lf_putstr(file, "}\n");
    lf_indent(file, -2);
    lf_printf(file, "}\n");
  }

  /* print the semantics */
  lf_printf(file, "\n");
  lf_indent(file, -1);
  print_function_name(file,
		      instruction->file_entry->fields[insn_name],
		      expanded_bits,
		      function_name_prefix_semantics);
  lf_printf(file, ":\n");
  lf_indent(file, +1);
  lf_printf(file, "{\n");
  lf_indent(file, +2);
  lf_putstr(file, "const unsigned_word cia = nia;\n");
  print_icache_body(file,
		    instruction,
		    expanded_bits,
		    cache_rules,
		    ((code & generate_with_direct_access)
		     ? define_variables
		     : declare_variables),
		    ((code & generate_with_icache)
		     ? get_values_from_icache
		     : do_not_use_icache));
  print_semantic_body(file,
		      instruction,
		      expanded_bits,
		      opcodes);
  if (code & generate_with_direct_access)
    print_icache_body(file,
		      instruction,
		      expanded_bits,
		      cache_rules,
		      undef_variables,
		      ((code & generate_with_icache)
		       ? get_values_from_icache
		       : do_not_use_icache));
  print_jump(file, 1/*is tail*/);
  lf_indent(file, -2);
  lf_printf(file, "}\n");
}

static void
print_jump_definition(insn_table *entry,
		      lf *file,
		      void *data,
		      insn *instruction,
		      int depth)
{
  cache_table *cache_rules = (cache_table*)data;
  if (generate_expanded_instructions) {
    ASSERT(entry->nr_insn == 1
	   && entry->opcode == NULL
	   && entry->parent != NULL
	   && entry->parent->opcode != NULL);
    ASSERT(entry->nr_insn == 1
	   && entry->opcode == NULL
	   && entry->parent != NULL
	   && entry->parent->opcode != NULL
	   && entry->parent->opcode_rule != NULL);
    print_jump_insn(file,
		    entry->insns,
		    entry->expanded_bits,
		    entry->opcode,
		    cache_rules);
  }
  else {
    print_jump_insn(file,
		    instruction,
		    NULL,
		    NULL,
		    cache_rules);
  }
}


static void
print_jump_internal_function(insn_table *table,
			     lf *file,
			     void *data,
			     table_entry *function)
{
  if (it_is("internal", function->fields[insn_flags])) {
    lf_printf(file, "\n");
    table_entry_print_cpp_line_nr(file, function);
    lf_indent(file, -1);
    print_function_name(file,
			function->fields[insn_name],
			NULL,
			((code & generate_with_icache)
			 ? function_name_prefix_icache
			 : function_name_prefix_semantics));
    lf_printf(file, ":\n");
    lf_indent(file, +1);
    lf_printf(file, "{\n");
    lf_indent(file, +2);
    lf_printf(file, "const unsigned_word cia = nia;\n");
    lf_print__c_code(file, function->annex);
    lf_print__internal_reference(file);
    lf_printf(file, "error(\"Internal function must longjump\\n\");\n");
    lf_indent(file, -2);
    lf_printf(file, "}\n");
  }
}

static void
print_jump_until_stop_body(lf *file,
			   insn_table *table,
			   cache_table *cache_rules,
			   int can_stop)
{
  lf_printf(file, "{\n");
  lf_indent(file, +2);
  if (!can_stop)
    lf_printf(file, "int *keep_running = NULL;\n");
  lf_putstr(file, "jmp_buf halt;\n");
  lf_putstr(file, "jmp_buf restart;\n");
  lf_putstr(file, "cpu *processor = NULL;\n");
  lf_putstr(file, "unsigned_word nia = -1;\n");
  lf_putstr(file, "instruction_word instruction = 0;\n");
  if ((code & generate_with_icache)) {
    lf_putstr(file, "idecode_cache *cache_entry = NULL;\n");
  }
  if (generate_smp) {
    lf_putstr(file, "int current_cpu = -1;\n");
  }

  /* all the switches and tables - they know about jumping */
  print_idecode_lookups(file, table, cache_rules);
 
  /* start the simulation up */
  if ((code & generate_with_icache)) {
    lf_putstr(file, "\n");
    lf_putstr(file, "{\n");
    lf_putstr(file, "  int cpu_nr;\n");
    lf_putstr(file, "  for (cpu_nr = 0; cpu_nr < nr_cpus; cpu_nr++)\n");
    lf_putstr(file, "    cpu_flush_icache(processors[cpu_nr]);\n");
    lf_putstr(file, "}\n");
  }

  lf_putstr(file, "\n");
  lf_putstr(file, "psim_set_halt_and_restart(system, &halt, &restart);\n");

  lf_putstr(file, "\n");
  lf_putstr(file, "if (setjmp(halt))\n");
  lf_putstr(file, "  return;\n");

  lf_putstr(file, "\n");
  lf_putstr(file, "setjmp(restart);\n");

  lf_putstr(file, "\n");
  if (!generate_smp) {
    lf_putstr(file, "processor = processors[0];\n");
    lf_putstr(file, "nia = cpu_get_program_counter(processor);\n");
  }
  else {
    lf_putstr(file, "current_cpu = psim_last_cpu(system);\n");
  }

  if (!(code & generate_with_icache)) {
    lf_printf(file, "\n");
    lf_indent(file, -1);
    lf_printf(file, "idecode:\n");
    lf_indent(file, +1);
  }

  print_jump(file, 0/*is_tail*/);

  if ((code & generate_with_icache)) {
    lf_indent(file, -1);
    lf_printf(file, "cache_miss:\n");
    lf_indent(file, +1);
  }

  lf_putstr(file, "instruction\n");
  lf_putstr(file, "  = vm_instruction_map_read(cpu_instruction_map(processor),\n");
  lf_putstr(file, "                            processor, nia);\n");
  print_idecode_body(file, table, "/*IGORE*/");

  /* print out a table of all the internals functions */
  insn_table_traverse_function(table,
			       file, NULL,
			       print_jump_internal_function);

 /* print out a table of all the instructions */
  if (generate_expanded_instructions)
    insn_table_traverse_tree(table,
			     file, cache_rules,
			     1,
			     NULL, /* start */
			     print_jump_definition, /* leaf */
			     NULL, /* end */
			     NULL); /* padding */
  else
    insn_table_traverse_insn(table,
			     file, cache_rules,
			     print_jump_definition);
  lf_indent(file, -2);
  lf_printf(file, "}\n");
}


/****************************************************************/



static void
print_idecode_floating_point_unavailable(lf *file)
{
  if ((code & generate_jumps))
    lf_printf(file, "goto %s_floating_point_unavailable;\n", (code & generate_with_icache) ? "icache" : "semantic");
  else if ((code & generate_with_icache))
    lf_printf(file, "return icache_floating_point_unavailable(%s);\n",
	      ICACHE_FUNCTION_ACTUAL);
  else
    lf_printf(file, "return semantic_floating_point_unavailable(%s);\n",
	      SEMANTIC_FUNCTION_ACTUAL);
}


/* Output code to do any final checks on the decoded instruction.
   This includes things like verifying any on decoded fields have the
   correct value and checking that (for floating point) floating point
   hardware isn't disabled */

void
print_idecode_validate(lf *file,
		       insn *instruction,
		       opcode_field *opcodes)
{
  /* Validate: unchecked instruction fields

     If any constant fields in the instruction were not checked by the
     idecode tables, output code to check that they have the correct
     value here */
  { 
    unsigned check_mask = 0;
    unsigned check_val = 0;
    insn_field *field;
    opcode_field *opcode;

    /* form check_mask/check_val containing what needs to be checked
       in the instruction */
    for (field = instruction->fields->first;
	 field->first < insn_bit_size;
	 field = field->next) {

      check_mask <<= field->width;
      check_val <<= field->width;

      /* is it a constant that could need validating? */
      if (!field->is_int && !field->is_slash)
	continue;

      /* has it been checked by a table? */
      for (opcode = opcodes; opcode != NULL; opcode = opcode->parent) {
	if (field->first >= opcode->first
	    && field->last <= opcode->last)
	  break;
      }
      if (opcode != NULL)
	continue;

      check_mask |= (1 << field->width)-1;
      check_val |= field->val_int;
    }

    /* if any bits not checked by opcode tables, output code to check them */
    if (check_mask) {
      lf_printf(file, "\n");
      lf_printf(file, "/* validate: %s */\n",
		instruction->file_entry->fields[insn_format]);
      lf_printf(file, "if (WITH_RESERVED_BITS && (instruction & 0x%x) != 0x%x)\n",
		check_mask, check_val);
      lf_indent(file, +2);
      print_idecode_illegal(file, "return");
      lf_indent(file, -2);
    }
  }

  /* Validate floating point hardware

     If the simulator is being built with out floating point hardware
     (different to it being disabled in the MSR) then floating point
     instructions are invalid */
  {
    if (it_is("f", instruction->file_entry->fields[insn_flags])) {
      lf_printf(file, "\n");
      lf_printf(file, "/* Validate: FP hardware exists */\n");
      lf_printf(file, "if (CURRENT_FLOATING_POINT != HARD_FLOATING_POINT)\n");
      lf_indent(file, +2);
      print_idecode_illegal(file, "return");
      lf_indent(file, -2);
    }
  }

  /* Validate: Floating Point available

     If floating point is not available, we enter a floating point
     unavailable interrupt into the cache instead of the instruction
     proper.

     The PowerPC spec requires a CSI after MSR[FP] is changed and when
     ever a CSI occures we flush the instruction cache. */

  {
    if (it_is("f", instruction->file_entry->fields[insn_flags])) {
      lf_printf(file, "\n");
      lf_printf(file, "/* Validate: FP available according to MSR[FP] */\n");
      lf_printf(file, "if (!IS_FP_AVAILABLE(processor))\n");
      lf_indent(file, +2);
      print_idecode_floating_point_unavailable(file);
      lf_indent(file, -2);
    }
  }
}


/****************************************************************/


static void
print_idecode_run_function_header(lf *file,
				  int can_stop,
				  int is_definition)
{
  int indent;
  lf_printf(file, "\n");
  lf_print_function_type(file, "void", "PSIM_INLINE_IDECODE", (is_definition ? " " : "\n"));
  indent = lf_putstr(file, (can_stop ? "idecode_run_until_stop" : "idecode_run"));
  if (is_definition)
    lf_putstr(file, "\n");
  else
    lf_indent(file, +indent);
  lf_putstr(file, "(psim *system,\n");
  if (can_stop)
    lf_putstr(file, " volatile int *keep_running,\n");
  lf_printf(file, " event_queue *events,\n");
  lf_putstr(file, " cpu *const processors[],\n");
  lf_putstr(file, " const int nr_cpus)");
  if (is_definition)
    lf_putstr(file, ";");
  else
    lf_indent(file, -indent);
  lf_putstr(file, "\n");
}


void
gen_idecode_h(lf *file,
	      insn_table *table,
	      cache_table *cache_rules)
{
  lf_printf(file, "/* The idecode_*.h functions shall move to support */\n");
  lf_printf(file, "#include \"idecode_expression.h\"\n");
  lf_printf(file, "#include \"idecode_fields.h\"\n");
  lf_printf(file, "#include \"idecode_branch.h\"\n");
  lf_printf(file, "\n");
  print_icache_struct(table, cache_rules, file);
  lf_printf(file, "\n");
  lf_printf(file, "#define WITH_IDECODE_SMP %d\n", generate_smp);
  lf_printf(file, "\n");
  print_idecode_run_function_header(file, 0/*can stop*/, 1/*is definition*/);
  print_idecode_run_function_header(file, 1/*can stop*/, 1/*is definition*/);
}


void
gen_idecode_c(lf *file,
	      insn_table *table,
	      cache_table *cache_rules)
{
  /* the intro */
  lf_printf(file, "#include \"inline.c\"\n");
  lf_printf(file, "\n");
  lf_printf(file, "#include \"cpu.h\"\n");
  lf_printf(file, "#include \"idecode.h\"\n");
  lf_printf(file, "#include \"semantics.h\"\n");
  lf_printf(file, "#include \"icache.h\"\n");
  lf_printf(file, "#include \"support.h\"\n");
  lf_printf(file, "\n");
  lf_printf(file, "#include <setjmp.h>\n");
  lf_printf(file, "\n");
  lf_printf(file, "enum {\n");
  lf_printf(file, "  /* greater or equal to zero => table */\n");
  lf_printf(file, "  function_entry = -1,\n");
  lf_printf(file, "  boolean_entry = -2,\n");
  lf_printf(file, "};\n");
  lf_printf(file, "\n");
  lf_printf(file, "typedef struct _idecode_table_entry {\n");
  lf_printf(file, "  int shift;\n");
  lf_printf(file, "  instruction_word mask;\n");
  lf_printf(file, "  instruction_word value;");
  lf_printf(file, "  void *function_or_table;\n");
  lf_printf(file, "} idecode_table_entry;\n");
  lf_printf(file, "\n");
  lf_printf(file, "\n");

  if ((code & generate_calls)) {

    print_idecode_lookups(file, table, cache_rules);

    /* output the main idecode routine */
    print_idecode_run_function_header(file, 0/*can stop*/, 0/*is definition*/);
    print_run_until_stop_body(file, table, 0/* have stop argument */);

    print_idecode_run_function_header(file, 1/*can stop*/, 0/*is definition*/);
    print_run_until_stop_body(file, table, 1/* no stop argument */);

  }
  else if ((code & generate_jumps)) {

    print_idecode_run_function_header(file, 0/*can stop*/, 0/*is definition*/);
    print_jump_until_stop_body(file, table, cache_rules, 0 /* have stop argument */);

    print_idecode_run_function_header(file, 1/*can stop*/, 0/*is definition*/);
    print_jump_until_stop_body(file, table, cache_rules, 1/* have stop argument */);

  }
  else {
    error("Something is wrong!\n");
  }
}