aboutsummaryrefslogtreecommitdiff
path: root/sys/contrib/ia64/libuwx/src/uwx_uinfo.c
blob: bf9530dd990135ca1a5e1496679c63dbb3a797b9 (plain) (blame)
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
/*
Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "uwx_env.h"
#include "uwx_uinfo.h"
#include "uwx_utable.h"
#include "uwx_scoreboard.h"
#include "uwx_bstream.h"
#include "uwx_trace.h"
#include "uwx_swap.h"

int uwx_count_ones(unsigned int mask);

/*
 *  uwx_uinfo.c
 *
 *  This file contains the routines for reading and decoding
 *  the unwind information block. 
 *
 *  The main entry point, uwx_decode_uinfo(), is given a pointer
 *  to an unwind table entry and a pointer (passed by reference)
 *  to be filled in with a pointer to an update vector. It will
 *  read and decode the unwind descriptors contained in the
 *  unwind information block, then build the register state array,
 *  which describes the actions necessary to step from the current
 *  frame to the previous one.
 */

#define COPYIN_UINFO_4(dest, src) \
    (env->remote? \
	(*env->copyin)(UWX_COPYIN_UINFO, (dest), (src), \
						WORDSZ, env->cb_token) : \
	(*(uint32_t *)(dest) = *(uint32_t *)(src), WORDSZ) )

#define COPYIN_UINFO_8(dest, src) \
    (env->remote? \
	(*env->copyin)(UWX_COPYIN_UINFO, (dest), (src), \
						DWORDSZ, env->cb_token) : \
	(*(uint64_t *)(dest) = *(uint64_t *)(src), DWORDSZ) )


/* uwx_default_rstate: Returns the default register state for a leaf routine */

int uwx_default_rstate(struct uwx_env *env, uint64_t **rstatep)
{
    struct uwx_scoreboard *sb;

    sb = uwx_init_scoreboards(env);
    *rstatep = sb->rstate;
    return UWX_OK;
}


/* uwx_decode_uinfo: Decodes unwind info region */

int uwx_decode_uinfo(
    struct uwx_env *env,
    struct uwx_utable_entry *uentry,
    uint64_t **rstatep)
{
    uint64_t uinfohdr;
    unsigned int ulen;
    int len;
    struct uwx_bstream bstream;
    struct uwx_scoreboard *scoreboard;
    int ip_slot;
    int cur_slot;
    int status;
    struct uwx_rhdr rhdr;

    /* Remember the offset from the start of the function */
    /* to the current IP. This helps the client find */
    /* the symbolic information. */

    env->function_offset = env->context.special[UWX_REG_IP] -
						uentry->code_start;

    /* Read the unwind info header using the copyin callback. */
    /* (If we're reading a 32-bit unwind table, we need to */
    /* read the header as two 32-bit pieces to preserve the */
    /* guarantee that we always call copyin for aligned */
    /* 4-byte or 8-byte chunks.) */
    /* Then compute the length of the unwind descriptor */
    /* region and initialize a byte stream to read it. */

    if (uentry->unwind_flags & UNWIND_TBL_32BIT) {
	len = COPYIN_UINFO_4((char *)&uinfohdr, uentry->unwind_info);
	len += COPYIN_UINFO_4((char *)&uinfohdr + WORDSZ,
					uentry->unwind_info + WORDSZ);
	}
    else
	len = COPYIN_UINFO_8((char *)&uinfohdr, uentry->unwind_info);
    if (len != DWORDSZ)
	return UWX_ERR_COPYIN_UINFO;
    if (env->byte_swap)
	uwx_swap8(&uinfohdr);
    if (uentry->unwind_flags & UNWIND_TBL_32BIT)
	ulen = UNW_LENGTH(uinfohdr) * WORDSZ;
    else
	ulen = UNW_LENGTH(uinfohdr) * DWORDSZ;
    uwx_init_bstream(&bstream, env,
		uentry->unwind_info + DWORDSZ, ulen, UWX_COPYIN_UINFO);

    TRACE_R_UIB(uentry, ulen)

    /* Create an initial scoreboard for tracking the unwind state. */

    scoreboard = uwx_init_scoreboards(env);

    /* Prepare to read and decode the unwind regions described */
    /* by the unwind info block. Find the target "ip" slot */
    /* relative to the beginning of the region. The lower 4 bits */
    /* of the actual IP encode the slot number within a bundle. */

    cur_slot = 0;
    ip_slot = (int) ((env->context.special[UWX_REG_IP] & ~0x0fLL)
							- uentry->code_start)
		/ BUNDLESZ * SLOTSPERBUNDLE
		+ (unsigned int) (env->context.special[UWX_REG_IP] & 0x0f);

    /* Loop over the regions in the unwind info block. */

    for (;;) {

	/* Decode the next region header. */
	/* We have an error if we reach the end of the info block, */
	/* since we should have found our target ip slot by then. */
	/* We also have an error if the next byte isn't a region */
	/* header record. */

	status = uwx_decode_rhdr(env, &bstream, &rhdr);
	if (status != UWX_OK)
	    return status;

	/* If a prologue region, get a new scoreboard, pushing */
	/* the previous one onto the prologue stack. Then read */
	/* and decode the prologue region records. */

	if (rhdr.is_prologue) {
	    scoreboard = uwx_new_scoreboard(env, scoreboard);
	    if (scoreboard == 0)
		return UWX_ERR_NOMEM;
	    status = uwx_decode_prologue(env, &bstream,
					    scoreboard, &rhdr, ip_slot);
	}

	/* If a body region, read and decode the body region */
	/* records. If the body has an epilogue count, */
	/* uwx_decode_body will note that in the region header */
	/* record for use at the bottom of the loop. */

	else {
	    status = uwx_decode_body(env, &bstream, scoreboard, &rhdr, ip_slot);
	}

	if (status != UWX_OK)
	    return status;

	TRACE_R_DUMP_SB(scoreboard, rhdr, cur_slot, ip_slot)

	/* If the target ip slot is within this region, we're done. */
	/* Return the scoreboard's register state array. */

	if (ip_slot < rhdr.rlen) {
	    *rstatep = scoreboard->rstate;
	    return UWX_OK;
	}

	/* Otherwise, update the current ip slot, pop the */
	/* scoreboard stack based on the epilogue count, */
	/* and loop back around for the next region. */

	cur_slot += rhdr.rlen;
	ip_slot -= rhdr.rlen;
	if (rhdr.ecount > 0) {
	    scoreboard = uwx_pop_scoreboards(env, scoreboard, rhdr.ecount);
	    if (scoreboard == 0)
		return UWX_ERR_PROLOG_UF;
	}
    }
    /*NOTREACHED*/
}


/* uwx_decode_rhdr: Decodes a region header record */

int uwx_decode_rhdr(
    struct uwx_env *env,
    struct uwx_bstream *bstream,
    struct uwx_rhdr *rhdr)
{
    int b0;
    int b1;
    uint64_t val;
    int status;

    /* Get the first byte of the next descriptor record. */
    b0 = uwx_get_byte(bstream);
    if (b0 < 0)
	return UWX_ERR_NOUDESC;

    /* Initialize region header record. */

    rhdr->is_prologue = 0;
    rhdr->rlen = 0;
    rhdr->mask = 0;
    rhdr->grsave = 0;
    rhdr->ecount = 0;

    /* Format R1 */

    if (b0 < 0x40) {
	if ((b0 & 0x20) == 0) {
	    TRACE_I_DECODE_RHDR_1("(R1) prologue", b0)
	    rhdr->is_prologue = 1;
	}
	else {
	    TRACE_I_DECODE_RHDR_1("(R1) body", b0)
	}
	rhdr->rlen = b0 & 0x1f;
    }

    /* Format R2 */

    else if (b0 < 0x60) {
	b1 = uwx_get_byte(bstream);
	if (b1 < 0)
	    return UWX_ERR_BADUDESC;
	status = uwx_get_uleb128(bstream, &val);
	if (status != 0)
	    return UWX_ERR_BADUDESC;
	TRACE_I_DECODE_RHDR_2L("(R2) prologue_gr", b0, b1, val)
	rhdr->is_prologue = 1;
	rhdr->rlen = (unsigned int) val;
	rhdr->mask = ((b0 & 0x07) << 1) | (b1 >> 7);
	rhdr->grsave = b1 & 0x7f;
    }

    /* Format R3 */

    else if (b0 < 0x80) {
	status = uwx_get_uleb128(bstream, &val);
	if (status != 0)
	    return UWX_ERR_BADUDESC;
	if ((b0 & 0x03) == 0) {
	    TRACE_I_DECODE_RHDR_1L("(R3) prologue", b0, val)
	    rhdr->is_prologue = 1;
	}
	else {
	    TRACE_I_DECODE_RHDR_1L("(R3) body", b0, val)
	}
	rhdr->rlen = (unsigned int) val;
    }

    /* Otherwise, not a region header record. */

    else {
	TRACE_I_DECODE_RHDR_1("(?)", b0)
	return UWX_ERR_BADUDESC;
    }

    return UWX_OK;
}


/* uwx_decode_prologue: Decodes a prologue region */

int uwx_decode_prologue(
    struct uwx_env *env,
    struct uwx_bstream *bstream,
    struct uwx_scoreboard *scoreboard,
    struct uwx_rhdr *rhdr,
    int ip_slot)
{
    int status;
    int reg;
    int mask;
    int b0;
    int b1;
    int b2;
    int b3;
    int r;
    int t;
    int i;
    uint64_t parm1;
    uint64_t parm2;
    uint64_t newrstate[NSBREG];
    int tspill[NSBREG];
    int priunat_mem_rstate;
    int t_priunat_mem;
    unsigned int gr_mem_mask;
    unsigned int br_mem_mask;
    unsigned int fr_mem_mask;
    unsigned int gr_gr_mask;
    unsigned int br_gr_mask;
    int ngr;
    int nbr;
    int nfr;
    unsigned int spill_base;
    unsigned int gr_base;
    unsigned int br_base;
    unsigned int fr_base;

    /* Initialize an array of register states from the current */
    /* scoreboard, along with a parallel array of spill times. */
    /* We use this as a temporary scoreboard, then update the */
    /* real scoreboard at the end of the procedure. */
    /* We initialize the spill time to (rhdr.rlen - 1) so that */
    /* spills without a "when" descriptor will take effect */
    /* at the end of the prologue region. */
    /* (Boundary condition: all actions in a zero-length prologue */
    /* will appear to have happened in the instruction slot */
    /* immediately preceding the prologue.) */

    for (i = 0; i < env->nsbreg; i++) {
	newrstate[i] = scoreboard->rstate[i];
	tspill[i] = rhdr->rlen - 1;
    }
    priunat_mem_rstate = UWX_DISP_NONE;
    t_priunat_mem = rhdr->rlen - 1;

    fr_mem_mask = 0;
    gr_mem_mask = 0;
    br_mem_mask = 0;
    gr_gr_mask = 0;
    br_gr_mask = 0;
    nfr = 0;
    ngr = 0;
    nbr = 0;
    spill_base = 0;

    /* If prologue_gr header record supplied mask and grsave, */
    /* record these in the scoreboard. */

    reg = rhdr->grsave;
    mask = rhdr->mask;
    if (mask & 0x8) {
	newrstate[SBREG_RP] = UWX_DISP_REG(UWX_REG_GR(reg));
	reg++;
    }
    if (mask & 0x4) {
	newrstate[SBREG_PFS] = UWX_DISP_REG(UWX_REG_GR(reg));
	reg++;
    }
    if (mask & 0x2) {
	newrstate[SBREG_PSP] = UWX_DISP_REG(UWX_REG_GR(reg));
	reg++;
    }
    if (mask & 0x1) {
	newrstate[SBREG_PREDS] = UWX_DISP_REG(UWX_REG_GR(reg));
	reg++;
    }

    /* Read prologue descriptor records until */
    /* we hit another region header. */

    for (;;) {

	b0 = uwx_get_byte(bstream);

	if (b0 < 0x80) {
	    /* Return the last byte read to the byte stream, since it's */
	    /* really the first byte of the next region header record. */
	    if (b0 >= 0)
		(void) uwx_unget_byte(bstream, b0);
	    break;
	}

	switch ((b0 & 0x70) >> 4) {

	    case 0:			/* 1000 xxxx */
	    case 1:			/* 1001 xxxx */
		/* Format P1 (br_mem) */
		TRACE_I_DECODE_PROLOGUE_1("(P1) br_mem", b0)
		br_mem_mask = b0 & 0x1f;
		break;

	    case 2:			/* 1010 xxxx */
		/* Format P2 (br_gr) */
		b1 = uwx_get_byte(bstream);
		if (b1 < 0)
		    return UWX_ERR_BADUDESC;
		TRACE_I_DECODE_PROLOGUE_2("(P2) br_gr", b0, b1)
		mask = ((b0 & 0x0f) << 1) | (b1 >> 7);
		reg = b1 & 0x7f;
		br_gr_mask = mask;
		for (i = 0; i < NSB_BR && mask != 0; i++) {
		    if (mask & 0x01) {
			newrstate[SBREG_BR + i] = UWX_DISP_REG(UWX_REG_GR(reg));
			reg++;
		    }
		    mask = mask >> 1;
		}
		break;

	    case 3:			/* 1011 xxxx */
		/* Format P3 */
		if (b0 < 0xb8) {
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    r = ((b0 & 0x3) << 1) | (b1 >> 7);
		    reg = b1 & 0x7f;
		    switch (r) {
			case 0:		/* psp_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) psp_gr", b0, b1)
			    newrstate[SBREG_PSP] = UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 1:		/* rp_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) rp_gr", b0, b1)
			    newrstate[SBREG_RP] = UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 2:		/* pfs_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) pfs_gr", b0, b1)
			    newrstate[SBREG_PFS] = UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 3:		/* preds_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) preds_gr", b0, b1)
			    newrstate[SBREG_PREDS] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 4:		/* unat_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) unat_gr", b0, b1)
			    newrstate[SBREG_UNAT] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 5:		/* lc_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) lc_gr", b0, b1)
			    newrstate[SBREG_LC] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 6:		/* rp_br */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) rp_br", b0, b1)
			    scoreboard->rstate[SBREG_RP] =
					UWX_DISP_REG(UWX_REG_BR(reg));
			    break;
			case 7:		/* rnat_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) rnat_gr", b0, b1)
			    newrstate[SBREG_RNAT] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 8:		/* bsp_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) bsp_gr", b0, b1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 9:		/* bspstore_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) bspstore_gr", b0, b1)
			    /* Don't track BSPSTORE yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 10:	/* fpsr_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) fpsr_gr", b0, b1)
			    newrstate[SBREG_FPSR] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			case 11:	/* priunat_gr */
			    TRACE_I_DECODE_PROLOGUE_2("(P3) priunat_gr", b0, b1)
			    newrstate[SBREG_PRIUNAT] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    break;
			default:
			    TRACE_I_DECODE_PROLOGUE_2("(P3) ??", b0, b1)
			    return UWX_ERR_BADUDESC;
		    }
		}

		/* Format P4 (spill_mask) */
		else if (b0 == 0xb8) {
		    TRACE_I_DECODE_PROLOGUE_1("(P4) spill_mask", b0)
		    /* The spill_mask descriptor is followed by */
		    /* an imask field whose length is determined */
		    /* by the region length: there are two mask */
		    /* bits per instruction slot in the region. */
		    /* We decode these bits two at a time, counting */
		    /* the number of FRs, GRs, and BRs that are */
		    /* saved up to the slot of interest. Other */
		    /* descriptors describe which sets of these */
		    /* registers are spilled, and we put those */
		    /* two pieces of information together at the */
		    /* end of the main loop. */
		    t = 0;
		    while (t < rhdr->rlen) {
			b1 = uwx_get_byte(bstream);
			if (b1 < 0)
			    return UWX_ERR_BADUDESC;
			for (i = 0; i < 4 && (t + i) < ip_slot; i++) {
			    switch (b1 & 0xc0) {
				case 0x00: break;
				case 0x40: nfr++; break;
				case 0x80: ngr++; break;
				case 0xc0: nbr++; break;
			    }
			    b1 = b1 << 2;
			}
			t += 4;
		    }
		}

		/* Format P5 (frgr_mem) */
		else if (b0 == 0xb9) {
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    b3 = uwx_get_byte(bstream);
		    if (b3 < 0)
			return UWX_ERR_BADUDESC;
		    TRACE_I_DECODE_PROLOGUE_4("(P5) frgr_mem", b0, b1, b2, b3)
		    gr_mem_mask = b1 >> 4;
		    fr_mem_mask = ((b1 & 0x0f) << 16) | (b2 << 8) | b3;
		}

		/* Invalid descriptor record */
		else {
		    TRACE_I_DECODE_PROLOGUE_1("(?)", b0)
		    return UWX_ERR_BADUDESC;
		}

		break;

	    case 4:			/* 1100 xxxx */
		/* Format P6 (fr_mem) */
		TRACE_I_DECODE_PROLOGUE_1("(P6) fr_mem", b0)
		fr_mem_mask = b0 & 0x0f;
		break;

	    case 5:			/* 1101 xxxx */
		/* Format P6 (gr_mem) */
		TRACE_I_DECODE_PROLOGUE_1("(P6) gr_mem", b0)
		gr_mem_mask = b0 & 0x0f;
		break;

	    case 6:			/* 1110 xxxx */
		/* Format P7 */
		r = b0 & 0xf;
		status = uwx_get_uleb128(bstream, &parm1);
		if (status != 0)
		    return UWX_ERR_BADUDESC;
		switch (r) {
		    case 0:		/* mem_stack_f */
			status = uwx_get_uleb128(bstream, &parm2);
			if (status != 0)
			    return UWX_ERR_BADUDESC;
			TRACE_I_DECODE_PROLOGUE_1LL("(P7) mem_stack_f", b0, parm1, parm2)
			newrstate[SBREG_PSP] = UWX_DISP_SPPLUS(parm2 * 16);
			tspill[SBREG_PSP] = (int) parm1;
			break;
		    case 1:		/* mem_stack_v */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) mem_stack_v", b0, parm1)
			tspill[SBREG_PSP] = (int) parm1;
			break;
		    case 2:		/* spill_base */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) spill_base", b0, parm1)
			spill_base = 4 * (unsigned int) parm1;
			break;
		    case 3:		/* psp_sprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) psp_sprel", b0, parm1)
			newrstate[SBREG_PSP] = UWX_DISP_SPREL(parm1 * 4);
			break;
		    case 4:		/* rp_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) rp_when", b0, parm1)
			tspill[SBREG_RP] = (int) parm1;
			break;
		    case 5:		/* rp_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) rp_psprel", b0, parm1)
			newrstate[SBREG_RP] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		    case 6:		/* pfs_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) pfs_when", b0, parm1)
			tspill[SBREG_PFS] = (int) parm1;
			break;
		    case 7:		/* pfs_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) pfs_psprel", b0, parm1)
			newrstate[SBREG_PFS] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		    case 8:		/* preds_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) preds_when", b0, parm1)
			tspill[SBREG_PREDS] = (int) parm1;
			break;
		    case 9:		/* preds_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) preds_psprel", b0, parm1)
			newrstate[SBREG_PREDS] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		    case 10:	/* lc_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) lc_when", b0, parm1)
			tspill[SBREG_LC] = (int) parm1;
			break;
		    case 11:	/* lc_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) lc_psprel", b0, parm1)
			newrstate[SBREG_LC] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		    case 12:	/* unat_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) unat_when", b0, parm1)
			tspill[SBREG_UNAT] = (int) parm1;
			break;
		    case 13:	/* unat_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) unat_psprel", b0, parm1)
			newrstate[SBREG_UNAT] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		    case 14:	/* fpsr_when */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) fpsr_when", b0, parm1)
			tspill[SBREG_FPSR] = (int) parm1;
			break;
		    case 15:	/* fpsr_psprel */
			TRACE_I_DECODE_PROLOGUE_1L("(P7) fpsr_psprel", b0, parm1)
			newrstate[SBREG_FPSR] = UWX_DISP_PSPREL(parm1 * 4);
			break;
		}
		break;

	    case 7:			/* 1111 xxxx */
		/* Format P8 */
		if (b0 == 0xf0) {
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    status = uwx_get_uleb128(bstream, &parm1);
		    if (status != 0)
			return UWX_ERR_BADUDESC;
		    switch (b1) {
			case 1:		/* rp_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) rp_sprel", b0, b1, parm1)
			    newrstate[SBREG_RP] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 2:		/* pfs_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) pfs_sprel", b0, b1, parm1)
			    newrstate[SBREG_PFS] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 3:		/* preds_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) preds_sprel", b0, b1, parm1)
			    newrstate[SBREG_PREDS] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 4:		/* lc_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) lc_sprel", b0, b1, parm1)
			    newrstate[SBREG_LC] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 5:		/* unat_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) unat_sprel", b0, b1, parm1)
			    newrstate[SBREG_UNAT] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 6:		/* fpsr_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) fpsr_sprel", b0, b1, parm1)
			    newrstate[SBREG_FPSR] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 7:		/* bsp_when */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bsp_when", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 8:		/* bsp_psprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bsp_psprel", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 9:		/* bsp_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bsp_sprel", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 10:	/* bspstore_when */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bspstore_when", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 11:	/* bspstore_psprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bspstore_psprel", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 12:	/* bspstore_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) bspstore_sprel", b0, b1, parm1)
			    /* Don't track BSP yet */
			    return UWX_ERR_CANTUNWIND;
			    break;
			case 13:	/* rnat_when */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) rnat_when", b0, b1, parm1)
			    tspill[SBREG_RNAT] = (int) parm1;
			    break;
			case 14:	/* rnat_psprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) rnat_psprel", b0, b1, parm1)
			    newrstate[SBREG_RNAT] = UWX_DISP_PSPREL(parm1 * 4);
			    break;
			case 15:	/* rnat_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) rnat_sprel", b0, b1, parm1)
			    newrstate[SBREG_RNAT] = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 16:	/* priunat_when_gr */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) priunat_when_gr", b0, b1, parm1)
			    tspill[SBREG_PRIUNAT] = (int) parm1;
			    break;
			case 17:	/* priunat_psprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) priunat_psprel", b0, b1, parm1)
			    priunat_mem_rstate = UWX_DISP_PSPREL(parm1 * 4);
			    break;
			case 18:	/* priunat_sprel */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) priunat_sprel", b0, b1, parm1)
			    priunat_mem_rstate = UWX_DISP_SPREL(parm1 * 4);
			    break;
			case 19:	/* priunat_when_mem */
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) priunat_when_mem", b0, b1, parm1)
			    t_priunat_mem = (int) parm1;
			    break;
			default:
			    TRACE_I_DECODE_PROLOGUE_2L("(P8) ??", b0, b1, parm1)
			    return UWX_ERR_BADUDESC;
		    }
		}

		/* Format P9 (gr_gr) */
		else if (b0 == 0xf1) {
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    TRACE_I_DECODE_PROLOGUE_3("(P9) gr_gr", b0, b1, b2)
		    mask = b1 & 0x0f;
		    reg = b2 & 0x7f;
		    gr_gr_mask = mask;
		    for (i = 0; i < NSB_GR && mask != 0; i++) {
			if (mask & 0x01) {
			    newrstate[SBREG_GR + i] =
					UWX_DISP_REG(UWX_REG_GR(reg));
			    reg++;
			}
			mask = mask >> 1;
		    }
		}

		/* Format X1 */
		else if (b0 == 0xf9) {
		    TRACE_I_DECODE_PROLOGUE_1("(X1)", b0)
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    /* Don't support X-format descriptors yet */
		    return UWX_ERR_CANTUNWIND;
		}

		/* Format X2 */
		else if (b0 == 0xfa) {
		    TRACE_I_DECODE_PROLOGUE_1("(X2)", b0)
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    /* Don't support X-format descriptors yet */
		    return UWX_ERR_CANTUNWIND;
		}

		/* Format X3 */
		else if (b0 == 0xfb) {
		    TRACE_I_DECODE_PROLOGUE_1("(X3)", b0)
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    /* Don't support X-format descriptors yet */
		    return UWX_ERR_CANTUNWIND;
		}

		/* Format X4 */
		else if (b0 == 0xfc) {
		    TRACE_I_DECODE_PROLOGUE_1("(X4)", b0)
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    b3 = uwx_get_byte(bstream);
		    if (b3 < 0)
			return UWX_ERR_BADUDESC;
		    /* Don't support X-format descriptors yet */
		    return UWX_ERR_CANTUNWIND;
		}

		/* Format P10 */
		else if (b0 == 0xff) {
		    b1 = uwx_get_byte(bstream);
		    if (b1 < 0)
			return UWX_ERR_BADUDESC;
		    b2 = uwx_get_byte(bstream);
		    if (b2 < 0)
			return UWX_ERR_BADUDESC;
		    TRACE_I_DECODE_PROLOGUE_3("(P10) abi", b0, b1, b2)
		    env->abi_context = (b1 << 8) | b2;
		    return UWX_ABI_FRAME;
		}

		/* Invalid descriptor record */
		else {
		    TRACE_I_DECODE_PROLOGUE_1("(?)", b0)
		    return UWX_ERR_BADUDESC;
		}
		break;
	}
    }

    /* Process the masks of spilled GRs, FRs, and BRs to */
    /* determine when and where each register was saved. */

    fr_base = spill_base + 16 * uwx_count_ones(fr_mem_mask);
    br_base = fr_base + 8 * uwx_count_ones(br_mem_mask);
    gr_base = br_base + 8 * uwx_count_ones(gr_mem_mask);
    TRACE_I_DECODE_PROLOGUE_SPILL_BASE(spill_base)
    TRACE_I_DECODE_PROLOGUE_MASKS(gr_mem_mask, gr_gr_mask)
    TRACE_I_DECODE_PROLOGUE_NSPILL(ngr)
    for (i = 0; ngr > 0 && i <= NSB_GR; i++) {
	if (gr_mem_mask & 1) {
	    newrstate[SBREG_GR + i] = UWX_DISP_PSPREL(gr_base);
	    tspill[SBREG_GR + i] = 0;
	    gr_base -= 8;
	    ngr--;
	}
	else if (gr_gr_mask & 1) {
	    tspill[SBREG_GR + i] = 0;
	    ngr--;
	}
	gr_gr_mask = gr_gr_mask >> 1;
	gr_mem_mask = gr_mem_mask >> 1;
    }
    for (i = 0; nbr > 0 && i <= NSB_BR; i++) {
	if (br_mem_mask & 1) {
	    newrstate[SBREG_BR + i] = UWX_DISP_PSPREL(br_base);
	    tspill[SBREG_BR + i] = 0;
	    br_base -= 8;
	    nbr--;
	}
	else if (br_gr_mask & 1) {
	    tspill[SBREG_BR + i] = 0;
	    nbr--;
	}
	br_gr_mask = br_gr_mask >> 1;
	br_mem_mask = br_mem_mask >> 1;
    }
    for (i = 0; nfr > 0 && i <= NSB_FR; i++) {
	if (fr_mem_mask & 1) {
	    newrstate[SBREG_FR + i] = UWX_DISP_PSPREL(fr_base);
	    tspill[SBREG_FR + i] = 0;
	    fr_base -= 16;
	    nfr--;
	}
	fr_mem_mask = fr_mem_mask >> 1;
    }

    /* Update the scoreboard. */

    for (i = 0; i < env->nsbreg; i++) {
	if (ip_slot >= rhdr->rlen || ip_slot > tspill[i])
	    scoreboard->rstate[i] = newrstate[i];
    }
    if (priunat_mem_rstate != UWX_DISP_NONE && ip_slot > t_priunat_mem)
	scoreboard->rstate[SBREG_PRIUNAT] = priunat_mem_rstate;

    return UWX_OK;
}

int uwx_count_ones(unsigned int mask)
{
    mask = (mask & 0x55555555) + ((mask & 0xaaaaaaaa) >> 1);
    mask = (mask & 0x33333333) + ((mask & 0xcccccccc) >> 2);
    mask = (mask & 0x0f0f0f0f) + ((mask & 0xf0f0f0f0) >> 4);
    mask = (mask & 0x00ff00ff) + ((mask & 0xff00ff00) >> 8);
    return (mask & 0x0000ffff) + ((mask & 0xffff0000) >> 16);
}

/* uwx_decode_body: Decodes a body region */

int uwx_decode_body(
    struct uwx_env *env,
    struct uwx_bstream *bstream,
    struct uwx_scoreboard *scoreboard,
    struct uwx_rhdr *rhdr,
    int ip_slot)
{
    int status;
    int b0;
    int b1;
    int b2;
    int b3;
    int label;
    int ecount;
    int i;
    uint64_t parm1;
    uint64_t parm2;
    uint64_t newrstate[NSBREG];
    int tspill[NSBREG];
    int t_sp_restore;

    /* Initialize an array of register states from the current */
    /* scoreboard, along with a parallel array of spill times. */
    /* We use this as a temporary scoreboard, then update the */
    /* real scoreboard at the end of the procedure. */
    /* We initialize the spill time to (rhdr.rlen - 1) so that */
    /* spills without a "when" descriptor will take effect */
    /* at the end of the prologue region. */
    /* (Boundary condition: all actions in a zero-length prologue */
    /* will appear to have happened in the instruction slot */
    /* immediately preceding the prologue.) */

    for (i = 0; i < env->nsbreg; i++) {
	newrstate[i] = scoreboard->rstate[i];
	tspill[i] = rhdr->rlen - 1;
    }
    t_sp_restore = rhdr->rlen - 1;

    /* Read body descriptor records until */
    /* we hit another region header. */

    for (;;) {

	b0 = uwx_get_byte(bstream);

	if (b0 < 0x80) {
	    /* Return the last byte read to the byte stream, since it's */
	    /* really the first byte of the next region header record. */
	    if (b0 >= 0)
		(void) uwx_unget_byte(bstream, b0);
	    break;
	}

	/* Format B1 (label_state) */
	if (b0 < 0xa0) {
	    TRACE_I_DECODE_BODY_1("(B1) label_state", b0)
	    label = b0 & 0x1f;
	    status = uwx_label_scoreboard(env, scoreboard, label);
	    if (status != UWX_OK)
		return (status);
	}

	/* Format B1 (copy_state)  */
	else if (b0 < 0xc0) {
	    TRACE_I_DECODE_BODY_1("(B1) copy_state", b0)
	    label = b0 & 0x1f;
	    status = uwx_copy_scoreboard(env, scoreboard, label);
	    if (status != UWX_OK)
		return (status);
	    for (i = 0; i < env->nsbreg; i++) {
		newrstate[i] = scoreboard->rstate[i];
		tspill[i] = rhdr->rlen;
	    }
	}

	/* Format B2 (epilogue) */
	else if (b0 < 0xe0) {
	    ecount = b0 & 0x1f;
	    status = uwx_get_uleb128(bstream, &parm1);
	    if (status != 0)
		return UWX_ERR_BADUDESC;
	    TRACE_I_DECODE_BODY_1L("(B2) epilogue", b0, parm1)
	    rhdr->ecount = ecount + 1;
	    t_sp_restore = rhdr->rlen - (unsigned int) parm1;
	}

	/* Format B3 (epilogue) */
	else if (b0 == 0xe0) {
	    status = uwx_get_uleb128(bstream, &parm1);
	    if (status != 0)
		return UWX_ERR_BADUDESC;
	    status = uwx_get_uleb128(bstream, &parm2);
	    if (status != 0)
		return UWX_ERR_BADUDESC;
	    TRACE_I_DECODE_BODY_1LL("(B3) epilogue", b0, parm1, parm2)
	    t_sp_restore = rhdr->rlen - (unsigned int) parm1;
	    rhdr->ecount = (unsigned int) parm2 + 1;
	}

	/* Format B4 (label_state) */
	else if (b0 == 0xf0) {
	    status = uwx_get_uleb128(bstream, &parm1);
	    if (status != 0)
		return UWX_ERR_BADUDESC;
	    TRACE_I_DECODE_BODY_1L("(B4) label_state", b0, parm1)
	    label = (int) parm1;
	    status = uwx_label_scoreboard(env, scoreboard, label);
	    if (status != UWX_OK)
		return (status);
	}

	/* Format B4 (copy_state) */
	else if (b0 == 0xf8) {
	    status = uwx_get_uleb128(bstream, &parm1);
	    if (status != 0)
		return UWX_ERR_BADUDESC;
	    TRACE_I_DECODE_BODY_1L("(B4) copy_state", b0, parm1)
	    label = (int) parm1;
	    status = uwx_copy_scoreboard(env, scoreboard, label);
	    if (status != UWX_OK)
		return (status);
	    for (i = 0; i < env->nsbreg; i++) {
		newrstate[i] = scoreboard->rstate[i];
		tspill[i] = rhdr->rlen;
	    }
	}

	/* Format X1 */
	else if (b0 == 0xf9) {
	    TRACE_I_DECODE_BODY_1("(X1)", b0)
	    b1 = uwx_get_byte(bstream);
	    if (b1 < 0)
		return UWX_ERR_BADUDESC;
	    /* Don't support X-format descriptors yet */
	    return UWX_ERR_CANTUNWIND;
	}

	/* Format X2 */
	else if (b0 == 0xfa) {
	    TRACE_I_DECODE_BODY_1("(X2)", b0)
	    b1 = uwx_get_byte(bstream);
	    if (b1 < 0)
		return UWX_ERR_BADUDESC;
	    b2 = uwx_get_byte(bstream);
	    if (b2 < 0)
		return UWX_ERR_BADUDESC;
	    /* Don't support X-format descriptors yet */
	    return UWX_ERR_CANTUNWIND;
	}

	/* Format X3 */
	else if (b0 == 0xfb) {
	    TRACE_I_DECODE_BODY_1("(X3)", b0)
	    b1 = uwx_get_byte(bstream);
	    if (b1 < 0)
		return UWX_ERR_BADUDESC;
	    b2 = uwx_get_byte(bstream);
	    if (b2 < 0)
		return UWX_ERR_BADUDESC;
	    /* Don't support X-format descriptors yet */
	    return UWX_ERR_CANTUNWIND;
	}

	/* Format X4 */
	else if (b0 == 0xfc) {
	    TRACE_I_DECODE_BODY_1("(X4)", b0)
	    b1 = uwx_get_byte(bstream);
	    if (b1 < 0)
		return UWX_ERR_BADUDESC;
	    b2 = uwx_get_byte(bstream);
	    if (b2 < 0)
		return UWX_ERR_BADUDESC;
	    b3 = uwx_get_byte(bstream);
	    if (b3 < 0)
		return UWX_ERR_BADUDESC;
	    /* Don't support X-format descriptors yet */
	    return UWX_ERR_CANTUNWIND;
	}

	/* Invalid descriptor record */
	else {
	    TRACE_I_DECODE_BODY_1("(?)", b0)
	    return UWX_ERR_BADUDESC;
	}
    }

    /* Update the scoreboard. */

    for (i = 0; i < env->nsbreg; i++) {
	if (ip_slot > tspill[i])
	    scoreboard->rstate[i] = newrstate[i];
    }

    /* If we've passed the point in the epilogue where sp */
    /* is restored, update the scoreboard entry for PSP */
    /* and reset any entries for registers saved in memory. */

    if (ip_slot > t_sp_restore) {
	scoreboard->rstate[SBREG_PSP] = UWX_DISP_SPPLUS(0);
	for (i = 0; i < env->nsbreg; i++) {
	    if (UWX_GET_DISP_CODE(scoreboard->rstate[i]) == UWX_DISP_SPREL(0) ||
		UWX_GET_DISP_CODE(scoreboard->rstate[i]) == UWX_DISP_PSPREL(0))
		scoreboard->rstate[i] = UWX_DISP_NONE;
	}
    }

    return UWX_OK;
}