ms_fetch.c 21.6 KB
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
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */


	/*
	 *  ms_fetch.c  -  Handle instruction fetching for the MXS simulator
	 *
	 *	Jim Bennett
	 *	1994, 1995
	 */

#ifdef MIPSY_MXS
#include <sys/types.h>
#endif
#include <stdlib.h>
#include "ms.h"
#ifdef MIPSY_MXS
#include "cpu_state.h"
#include "mipsy.h"
#include "annotations.h"
#include "cp0.h"
#include "pcache.h"
#endif

/* Global variable set if there is an annotation on this instruction. */
extern int mxs_annotation;

static int ms_rename (struct s_cpu_state *st, INST *ip, THREAD *th,
				int this_inst, int store_inst);


	/*
	 *  ms_fetch  -  Fetch instructions from the text segment and add
	 *		 them to the instruction window
	 */

void ms_fetch (struct s_cpu_state *st)
	{
	INST	*ip;
	THREAD	*th;
	BrTREE	*br;
	int	this_inst, fetches, store_inst;
	int	nfetch;			/* Number of successful fetches	*/
#ifdef MIPSY_MXS
	uint	inst;
#endif


	AddToStat (ST_FETCHES_TRIED, FETCH_WIDTH);
	if (st->exception_pending)
	    {	/* Pending exception stalls all threads	*/
		AddToStat (ST_FETCH_EXPEND, FETCH_WIDTH)
		return;
	    }
	nfetch = 0;
#if MAX_ACT_THREADS == 1
	for (fetches=0;fetches<FETCH_WIDTH;fetches++)
#else
	for (th = st->active_thread, fetches=0;
				fetches<FETCH_WIDTH;
				fetches++, th = th->active_thread)
#endif 
		{
			/* If instruction window is full,	*/
			/* then proceed to next unit.		*/
		if (st->iwin_ninst >= IWIN_SIZE)
			{
			AddToStat (ST_FETCH_IW_FULL, FETCH_WIDTH-fetches)
			break;
			}
		this_inst = st->iwin_nextfree;
		if (this_inst < 0)
			{
			AddToStat (ST_FETCH_RB_FULL, FETCH_WIDTH-fetches)
			break;
			}

			/* If there are no active unstalled threads,	*/
			/* then proceed to next unit.			*/
#if MAX_ACT_THREADS == 1
		th = st->active_thread;
		if (!th) {
		    AddToStat (ST_FETCH_STALL_OTH,FETCH_WIDTH-fetches)
		    AddToStat (ST_FETCH_STALL,FETCH_WIDTH-fetches)
		    break;
		}
		if (th->stall_fetch) {
		    if (th->stall_branch) {
			if (th->stall_thread) {
			  AddToStat (ST_FETCH_STALLTHREAD,FETCH_WIDTH-fetches);
			} else {
			  AddToStat (ST_FETCH_STALLBRANCH,FETCH_WIDTH-fetches);
			}
		    } else if (th->stall_fpc) {
			AddToStat (ST_FETCH_STALLFPC,FETCH_WIDTH-fetches);
		    } else if (th->stall_icache) {
			AddToStat (ST_FETCH_STALLICACHE,FETCH_WIDTH-fetches);
		    } else if (th->stall_itlbmiss) {
			AddToStat (ST_FETCH_STALLITLB,FETCH_WIDTH-fetches);
		    } else  if (th->stall_except) {
			AddToStat (ST_FETCH_STALLEXCEPT,FETCH_WIDTH-fetches);
		    } else  if (th->stall_sys) {
			AddToStat (ST_FETCH_STALLSYS,FETCH_WIDTH-fetches);
		    } else  if (th->stall_cp0) {
			AddToStat (ST_FETCH_STALLCP0,FETCH_WIDTH-fetches);
		    } else  if (th->stall_sc) {
			AddToStat (ST_FETCH_STALLSC,FETCH_WIDTH-fetches);
                    } else {
			AddToStat (ST_FETCH_STALL_OTH,FETCH_WIDTH-fetches);
                    }
		    AddToStat (ST_FETCH_STALL,FETCH_WIDTH-fetches)

#ifndef MIPSY_MXS
#ifdef DEBUG_CHECKS
			if (((th->thread_st & TH_SPEC) == 0) &&
			    (th->stall_icache || th->stall_except) )
				{
				fprintf (stderr,
					"Fetch unit permanently stalled\r\n");
				fprintf (stderr,
					"Cycle #% 9d%0.5d\r\n",
					st->work_ticks, st->work_cycle);
				ms_break (st, NULL, "ERR");
				}
#endif
#endif

		    break;
		}
#else
		while ((th) && th->stall_fetch)
			th = th->active_thread;
		if (th == NULL)
			{
			th = st->active_thread;
			while ((th) && th->stall_fetch)
				th = th->active_thread;
			if (th == NULL)
				{
		    		AddToStat (ST_FETCH_STALL_OTH,
						FETCH_WIDTH-fetches)
				AddToStat (ST_FETCH_STALL,
						FETCH_WIDTH-fetches);
				break;
				}
			}
#endif
		ip = &st->iwin [this_inst];
#ifdef MIPSY_MXS
			/* Fetch the instruction to this thread	*/
		{
		PA	paddr;
		int	ret, dum1, dum2, bdoor;
		uint    tlbFlavor = TLB_IFETCH|TLB_READING;
		CPUState *P = (CPUState *) (st->mipsyPtr);

		if ((th->pc & 3) == 0) { 
                      void *bdoorAddr = 0;

		    ret = TranslateVirtual(P, th->pc, &paddr, &tlbFlavor, &bdoorAddr);
		} else {
		    RECORD_EXCEPTION(P, EXC_RADE, E_VEC,th->pc, 
                             P->CP0[C0_TLBHI], P->CP0[C0_CTXT],
                             P->CP0[C0_XCTXT]);  
		    ret = FAILURE;

		}
		if (ret != SUCCESS)
			{
			th->except = GetLastException(st);
			th->stall_itlbmiss = 1;
			UpdateStallFetch (th);
			IncStat (ST_FETCH_STALLITLB);
			IncStat (ST_FETCH_STALL);
			continue;
			}
                bdoor = (tlbFlavor & TLB_BDOOR);
		if (!bdoor) {

		    st->ms_action = ACT_UNSTALL_ICACHE;
		    P->PC = th->pc;
		    ret = ReadICache(P->myNum, th->pc, paddr, &inst);
		    if (ret != SUCCESS)
			{
			    if (ret == FAILURE) {
				/* Cache wouldn't accept req - stall for a cycle. */
                                IncStat (ST_FETCH_CACHEFAILURE);
				IncStat (ST_FETCH_STALLICACHE);
				IncStat (ST_FETCH_STALL);
				continue;
			    }
			    th->stall_icache = 1;
                            th->icache_stall_reason = MxsClassifyMiss(P->myNum,
                                                 th->pc, paddr, TRUE);
			    UpdateStallFetch (th);
			    IncStat (ST_FETCH_STALLICACHE);
			    IncStat (ST_FETCH_STALL);
			    continue;
			}
		}
		if (!bdoor)  {
                   compile_inst (inst, th->pc, ip, &dum1, &dum2);
		} else {
		   ip->op = OPBDOOR;
		   ip->r1 = ip->r2 = ip->r3 = -1;
		   ip->imm = paddr;
		}
	     }
#else
			/* If the PC of this thread is invalid, then	*/
			/* stall the thread.				*/
		if (th->pc == OUTSIDE_ADDR)
			{
			th->stall_icache = 1;
			UpdateStallFetch (th);
			IncStat (ST_FETCH_STALL_INV);
			IncStat (ST_FETCH_STALL);
			continue;
			}

			/* Fetch the instruction to this thread	*/
		*ip = pmap.ctbuf[th->pc];
#endif

#ifdef PRINT_INST
	{
	int t, flagp;

	if (enable_iprint && ((st->work_cycle % iprint_frequency) == 0))
		{
		printf ("% 7d%0.5d @0x%8.8x: ",
			st->work_ticks, st->work_cycle, th->debugpc);
		print_inst (stdout, ip);

		flagp = 0;
		if (ip->r2 > 0)
			{
			t = th->regnames [ip->r2];
			printf (" [");
			print_reg_contents (stdout, ip->r2, &st->regs[t]);
			flagp = 1;
			}
		if (ip->r3 > 0)
			{
			t = th->regnames [ip->r3];
			if (flagp)
				printf (", ");
			else
				printf (" [");
			print_reg_contents (stdout, ip->r3, &st->regs[t]);
			flagp = 1;
			}

		if (flagp)
			printf ("]\r\n");
		else
			printf ("\r\n");

#ifdef PRINT_IREG
		for (i=0; i<32; i+=8)
			printf ( "%8x %8x %8x %8x %8x %8x %8x %8x\r\n",
				Ireg(i), Ireg(i+1), Ireg(i+2), Ireg(i+3),
				Ireg(i+4), Ireg(i+5), Ireg(i+6), Ireg(i+7));
		printf ("\r\n");
#endif
		}
	}
#endif

		st->iwin_br_node [this_inst] = th->branch_node;
#ifdef BREAKPOINT
		if (th->debugpc == brkpt) ms_break (st, ip, "PC BRK");
		if (ip->op == opcbrk) ms_break (st, ip, "OP BRK");
#endif

			/* If it is a branch likely inst, we need to	*/
			/* ensure we can process it now.		*/
			/* NOTE:  This means that branch likely	inst's	*/
			/* won't work in a non-speculative model.	*/
			/* I.e. THREAD_WIDTH must be at least two.	*/
		if (is_likely (ip->op) && (st->nthreads >= THREAD_WIDTH))
			{
			IncStat (ST_FETCH_THREAD);
			continue;
			}

			/* Convert illegal instruction sequence to	*/
			/* illegal instruction, for exception handling	*/
		if (th->branch_dly && (is_branch (ip->op) || is_call (ip->op))) 
			{
			ip->op = OPILL;
			ip->r1 = ip->r2 = ip->r3 = -1;
			}

			/* Rename logical registers to use real		*/
			/* (physical) registers.			*/
		store_inst = is_store (ip->op);
		if (ms_rename (st, ip, th, this_inst, store_inst) < 0)
			{
			IncStat (ST_FETCH_NAME);
			continue;
			}

			/* Rename succeeded, so it is OK to update the	*/
			/* machine state now.				*/
		ms_grad_enqueue (st, this_inst);
		st->iwin_pc [this_inst] = th->pc;
		st->iwin_flags [this_inst] |= IWIN_BUSY;

		if (mxs_annotation)
			st->iwin_flags [this_inst] |= IWIN_ANNOTATED;
		if (th->thread_st & TH_SPEC)
			st->iwin_flags [this_inst] |= IWIN_SPEC;
		st->iwin_nextfree = st->iwin_freelist [this_inst];

		th->pc = th->pc + PC_INC;
		th->debugpc += 4;

			/* Add instruction to list for this thread	*/
		br = &st->branch_tree [th->branch_node];
		if (br->iwin_tail_th >= 0)
			{
			st->iwin_thread [br->iwin_tail_th] = this_inst;
			st->iwin_bthread [this_inst] = br->iwin_tail_th;
			st->iwin_thread [this_inst] = -1;
			br->iwin_tail_th = this_inst;
			}
		else
			{
			br->iwin_head_th = this_inst;
			br->iwin_tail_th = this_inst;
			st->iwin_bthread [this_inst] = -1;
			st->iwin_thread [this_inst] = -1;
			}

			/* Add load/store instructions to load/store chain */
		if (is_ldst (ip->op))
		    {
		    if (store_inst) st->iwin_flags [this_inst] |= IWIN_STORE;
		    ms_ldst_enqueue (st, this_inst);

#ifdef ONE_PHASE_LS
			/* Loads and stores must wait if there is a	*/
			/* preceeding store.				*/
		    if (st->iwin_nstores > 0)
			st->iwin_flags [this_inst] |= IWIN_LDST_DEP;

			/* A store must wait if there are any		*/
			/* preceeding loads or stores.			*/
		    if (store_inst)
			{
			if (this_inst != st->iwin_head_ldst)
				st->iwin_flags [this_inst] |= IWIN_LDST_DEP;
			st->iwin_nstores++;
			}
#endif

		    }
			/* Add this instruction to the tail	*/
			/* of the priority chain in the window	*/
		nfetch++;
		ms_pri_enqueue (st, this_inst);

			/* If we've just fetched some kind of branch,	*/
			/* then stop fetching until the branch is	*/
			/* resolved.					*/
		if (is_branch (ip->op) || is_call (ip->op))
			{
			if (th->branch_dly)
				{
				fprintf (stderr,
				  "No branches allowed in branch shadow!!\r\n");
				ms_break (st, NULL, "ERR");
				}

				/* IWIN_BRDLY is used to hold off issue	*/
				/* of this inst until branch delay	*/
				/* slots have been fetched.		*/
			st->iwin_flags [this_inst] |= IWIN_BRDLY + IWIN_BRANCH;
			th->branch_inum = this_inst;
			th->branch_dly = BRANCH_SLOTS + 1;
			th->returnpc = th->pc + (BRANCH_SLOTS*PC_INC);

				/* Handle branch likely instructions	*/
			if (is_likely (ip->op))
				{
				th->stall_branch = 1;
				UpdateStallFetch (th);
				ms_branch (st, th, 1);
				}
			}
		else
			/* If it is a system call or a write to the FP	*/
			/* control register, stall fetching until all	*/
			/* prior instructions are executed.		*/
		if (is_sys (ip->op) || is_fp_ctl (ip->op) || is_sc(ip->op))
			{
                        if (is_sc (ip->op)) 
                            th->stall_sc = 1;
			else if (is_sys(ip->op)) {
                            if (ip->op == OPCP0) 
				th->stall_cp0 = 1;
                            else 
				th->stall_sys = 1;
                        }  else 
			    th->stall_fpc = 1;
                        
			UpdateStallFetch (th);
			if (!is_sc(ip->op))
			    st->iwin_flags [this_inst] |= IWIN_FLUSH;
			}

			/* If there is branch processing in progress	*/
			/* for this thread, check if we have finished	*/
			/* fetching the branch delay instructions.	*/
			/* If so, then OK to process the branch.	*/
			/* In the case of the branch likely, the branch	*/
			/* has already been processed, so just update	*/
			/* the PC to the branch target.			*/

		if (th->branch_dly)
			{
			th->branch_dly--;
			if (th->branch_dly == 0)
			    {
			    if (th->branch_likely)
				{
				th->pc = th->branch_likely_pc;
				th->branch_likely = 0;
				}
			    else
				{
				th->stall_branch = 1;
				UpdateStallFetch (th);
				ms_branch (st, th, 0);
				}
			    }
			}

		CheckInstAvail (st, this_inst);
		if (ip->op == OPILL) {
		    /* Current off the fetching for threads that encounter an
                     * illegal instruction. 
		     */
		    th->stall_except = 1;
		    UpdateStallFetch (th);
		}
		}
	AddToStat (ST_FETCHES, nfetch);
	}


	/*
	 *  ms_rename  -  Rename logical registers to use real (physical)
	 *		  registers.  Note that register 0 is never remapped,
	 *		  and that registers are allocated in pairs.
	 *
	 *	Returns -1 if rename fails (out of free registers, or
	 *	special register still in use)
	 *
	 *	If a source register has the REG_ERROR bit set, this indicates
	 *	that the writer of the register caused a fault, so stall
	 *	this thread until the problem is dealt with.
	 */

static int ms_rename (struct s_cpu_state *st, INST *ip, THREAD *th,
				int this_inst, int store_inst)
	{
	int	lreg_ix, reg_ix, dest_reg;
	REGSTAT	*rs;
	int	old_reg_ix, old_lreg, old_half_def;
	int	halfreg, evenreg, preg, halfpreg;
	int	owner;

#ifdef PRECISE
	st->iwin_lr1 [this_inst] = ip->r1;
	st->iwin_lr2 [this_inst] = ip->r2;
	st->iwin_lr3 [this_inst] = ip->r3;
#endif

	if (ip->r2 > 0)
		{
		lreg_ix = ip->r2 >> 1;
		if (lreg_ix != (ip->r1 >> 1))
			th->half_def[lreg_ix] &= ~REGNAME_MASK;
		ip->r2 = th->regnames [ip->r2];
		reg_ix = ip->r2 >> 1;
		if (st->reg_rstat[reg_ix].reg_status & REG_ERROR)
			{
			th->stall_except = 1;
			UpdateStallFetch (th);
			IncStat (ST_REG_ERROR);
			return (-1);
			}
		}
	if (ip->r3 > 0)
		{
		lreg_ix = ip->r3 >> 1;
		if (lreg_ix != (ip->r1 >> 1))
			th->half_def[lreg_ix] &= ~REGNAME_MASK;
		ip->r3 = th->regnames [ip->r3];
		reg_ix = ip->r3 >> 1;
		if (st->reg_rstat[reg_ix].reg_status & REG_ERROR)
			{
			th->stall_except = 1;
			UpdateStallFetch (th);
			IncStat (ST_REG_ERROR);
			return (-1);
			}
		}

		/* For destination register renaming, need a	*/
		/* new register name.  Find a free register,	*/
		/* set owner to be this instruction, and set	*/
		/* status = mapped + busy + in window.		*/
	old_reg_ix = -1;

	if (ip->r1 > 0)
		{
		reg_ix = st->reg_nextfree;
		if (reg_ix < 0)
			{
			IncStat (ST_REG_BUSY);
			return (-1);	/* Out of free reg's	*/
			}
		dest_reg = reg_ix << 1;
		halfreg = ip->r1 >> 1;

/*************************************************************************
	At this point, the fetch is guaranteed to complete except for the
	case of a special register, or a double precision register that
	has been claimed by another thread, which might still be in use.
	In the other cases, it is safe to update the machine state.
*************************************************************************/

			/* Handle integer reg renaming	*/
		if ((ip->r1 < FPREG) || (ip->r1 >= MAX_FP))
			{
			old_reg_ix = th->regnames[ip->r1] >> 1;
			old_lreg = ip->r1;
			old_half_def = 0;
			th->regnames[ip->r1] = dest_reg;
			AcquireRegMap (&st->reg_rstat[reg_ix], th, ip->r1);

			st->reg_owner [dest_reg] = this_inst;
			st->reg_rstat[reg_ix].reg_status |=
						REG_BUSY + REG_IN_WIN;
			st->reg_excuse [reg_ix] = ST_NOT_ISSUED;
			st->new_excuse [reg_ix] = ST_NOT_ISSUED;
			st->reg_nextfree = st->reg_freelist [reg_ix];
			st->iwin_flags [this_inst] |= IWIN_DEFINE;
#ifdef BREAKPOINT
			if (ip->r1 == trace_reg)
				trace_preg = dest_reg;
#endif
			ip->r1 = dest_reg;
			}
		else
			/* Handle FP reg renaming, keep	*/
			/* track of odd/even register	*/
			/* pairs.			*/
		if ((ip->r1 < FPCTL) || (ip->r1 >= TOT_REG))
		    {
		    if ((ip->op == OPCVTDW) || (ip->op == OPCVTDS) ||
			((ip->op >= OPFADDD) && (ip->op <= OPFFLOORD)) )
			{	/* These instructions update both	*/
				/* halves of an FP register pair.	*/
			old_reg_ix = th->regnames[ip->r1] >> 1;
			old_lreg = ip->r1;
			old_half_def = th->half_def[halfreg];
			th->regnames[ip->r1]   = dest_reg;
			th->regnames[ip->r1+1] = dest_reg+1;
			th->half_def[halfreg] = 0;
			AcquireRegMap (&st->reg_rstat[reg_ix], th, ip->r1);

			st->reg_owner [dest_reg] = this_inst;
			st->reg_owner [dest_reg+1] = this_inst;
			st->reg_rstat[reg_ix].reg_status |=
						REG_BUSY + REG_IN_WIN;
			st->reg_excuse [reg_ix] = ST_NOT_ISSUED;
			st->new_excuse [reg_ix] = ST_NOT_ISSUED;
			st->reg_nextfree = st->reg_freelist [reg_ix];
			st->iwin_flags [this_inst] |= IWIN_DEFINE;
#ifdef BREAKPOINT
			if (ip->r1 == trace_reg)
				trace_preg = dest_reg;
#endif
			ip->r1 = dest_reg;
			}
		    else
			{	/* Otherwise, are updating a half pair	*/
			int	old_owner;

			evenreg = halfreg << 1;
			halfpreg = (th->regnames[evenreg]) >> 1;

			if ((th->half_def[halfreg]) &&
			    ((th->half_def[halfreg] & REGNAME_MASK)
					== (ip->r1^0x01)) )
			    {

			/* Check if another thread has claimed	*/
			/* this register, and if not then stake	*/
			/* our claim.				*/

			    rs = &st->reg_rstat[halfpreg];
			    if (rs->reg_status & REG_CLAIMED)
				{
				IncStat (ST_REG_CLAIMED);
				return (-1);
				}
			    rs->reg_status |= REG_CLAIMED;
			    rs->reg_nclaims = 1;

			/* If there is a danger that this register	*/
			/* might be referenced by a non-descendant	*/
			/* thread, set the control flag, so that the	*/
			/* write won't complete until the instruction	*/
			/* is no longer speculative.			*/

			    preg = th->regnames[ip->r1];
			    old_owner = st->reg_owner [preg];
			    if (((rs->reg_status & REG_IN_WIN) == 0) ||
			     (st->iwin_br_node [old_owner] != th->branch_node))
				st->iwin_flags [this_inst] |= IWIN_CTL;

			/* The first half of the register def	*/
			/* is still valid, so complete the def	*/

			    th->half_def[halfreg] |= REGNAME_CLAIM;
			    th->half_def[halfreg] &= ~REGNAME_MASK;
			    st->reg_owner [preg] = this_inst;
#ifdef BREAKPOINT
			if (ip->r1 == trace_reg)
				trace_preg = preg;
#endif
			    ip->r1 = preg;

			/* If it is still in the window, set	*/
			/* register doubly mapped, and point	*/
			/* from first definition to this one.	*/

			    if (rs->reg_status & REG_IN_WIN)
				{
				rs->reg_status |= REG_DMAP;

				st->reg_otherhalf2 [halfpreg] = -1;
				if ((preg & (~0x01)) == (ip->r2 & (~0x01)))
					st->reg_otherhalf2 [halfpreg] =
							this_inst;

				st->reg_otherhalf3 [halfpreg] = -1;
				if ((preg & (~0x01)) == (ip->r3 & (~0x01)))
					st->reg_otherhalf3 [halfpreg] =
							this_inst;
				}
			    else
				{

			/* If the other half is no longer around, then	*/
			/* set this instruction as the register owner.	*/

				st->reg_owner [preg ^ 0x01] = this_inst;
				rs->reg_status |= REG_BUSY + REG_IN_WIN;
				st->reg_excuse [halfpreg] = ST_NOT_ISSUED;
				st->new_excuse [halfpreg] = ST_NOT_ISSUED;
				}
			    }
			else
			    {

			/* Otherwise, treat this as the start	*/
			/* of a new (half) definition.		*/

			    old_reg_ix = th->regnames[evenreg] >> 1;
			    old_lreg = evenreg;
			    old_half_def = th->half_def[halfreg];
			    th->half_def[halfreg] = ip->r1;
			    th->regnames[evenreg]  = dest_reg;
			    th->regnames[evenreg+1]= dest_reg+1;
			    st->regs[dest_reg]   = 0;
			    st->regs[dest_reg+1] = 0;
			    AcquireRegMap (&st->reg_rstat[reg_ix], th, ip->r1);

			    st->reg_owner [dest_reg] = this_inst;
			    if (ip->r1 & 0x01)
				{
				dest_reg++;
				st->reg_owner [dest_reg] = this_inst;
				}
			    st->reg_rstat[reg_ix].reg_status |=
						REG_BUSY + REG_IN_WIN;
			    st->reg_excuse [reg_ix] = ST_NOT_ISSUED;
			    st->new_excuse [reg_ix] = ST_NOT_ISSUED;
			    st->reg_nextfree = st->reg_freelist [reg_ix];
			    st->iwin_flags [this_inst] |= IWIN_DEFINE;
#ifdef BREAKPOINT
			if (ip->r1 == trace_reg)
				trace_preg = dest_reg;
#endif
			    ip->r1 = dest_reg;
			    }
			}
         }
		else
			/* For special registers, keep new	*/
			/* writer out until the previous	*/
			/* instance is out of the system.	*/
			{
			evenreg = halfreg << 1;
			preg = th->regnames[evenreg];
			halfpreg = preg >> 1;
			if (st->reg_rstat[halfpreg].reg_status & REG_BUSY)
				{
				IncStat (ST_REG_SPEC);
				return (-1);
				}

			st->iwin_flags [this_inst] |= IWIN_CTL;
			st->reg_owner [preg] = this_inst;
			if (ip->r1 & 0x01)
				st->reg_owner [preg+1] = this_inst;
			st->reg_rstat[halfpreg].reg_status |=
						REG_BUSY + REG_IN_WIN;
			st->reg_excuse [halfpreg] = ST_NOT_ISSUED;
			st->new_excuse [halfpreg] = ST_NOT_ISSUED;
			ip->r1 = (ip->r1 & 0x01 ? preg+1 : preg);
			}
     }

/****************************************************************************
	Except as noted above, updates to machine state should occur after
	this point.  Previously, the fetch could be aborted.  Now we know
	the fetch succeeds, so we can update the machine state accordingly.
****************************************************************************/

		/* During renaming, check for dependency on previous	*/
		/* instruction in window, increment reference count,	*/
		/* and add this inst to dependency list.		*/
	halfpreg = ip->r1 >> 1;
	if (ip->r2 > 0)
		{
		reg_ix = ip->r2 >> 1;
		rs = &st->reg_rstat [reg_ix];
		rs->reg_ref++;
#ifdef DEBUG_CHECKS
		if (!(rs->reg_status & (REG_BUSY | REG_MAPPED)))
			ms_break (st, ip, "CONERR");
#endif
		if (rs->reg_status & REG_IN_WIN)
		    {
		    if (reg_ix == halfpreg)
			{
			if (rs->reg_status & REG_DMAP)
			    {
			    st->iwin_flags [this_inst] |= IWIN_DEP2;
			    owner = st->reg_owner [ip->r1 ^ 0x01];
			    st->iwin_dep2 [owner] [st->iwin_index2[owner]++] = 
					this_inst;
			    }
			}
		    else
			{
			st->iwin_flags [this_inst] |= IWIN_DEP2;
			owner = st->reg_owner [reg_ix << 1];
			st->iwin_dep2 [owner] [st->iwin_index2[owner]++] = 
					this_inst;
			}
		    }
		}

	if (ip->r3 > 0)
		{
		reg_ix = ip->r3 >> 1;
		rs = &st->reg_rstat [reg_ix];
		rs->reg_ref++;
#ifdef DEBUG_CHECKS
		if (!(rs->reg_status & (REG_BUSY | REG_MAPPED)))
			ms_break (st, ip, "CONERR");
#endif
#ifdef ONE_PHASE_LS
		if (rs->reg_status & REG_IN_WIN)
#else
			/* For stores, don't worry about R3, the load	*/
			/* store buffer will handle that.		*/
		if ((rs->reg_status & REG_IN_WIN) && (!store_inst))
#endif
		    {
		    if (reg_ix == halfpreg)
			{
			if (rs->reg_status & REG_DMAP)
			    {
			    st->iwin_flags [this_inst] |= IWIN_DEP3;
			    owner = st->reg_owner [ip->r1 ^ 0x01];
			    st->iwin_dep3 [owner] [st->iwin_index3[owner]++] = 
					this_inst;
			    }
			}
		    else
			{
			st->iwin_flags [this_inst] |= IWIN_DEP3;
			owner = st->reg_owner [reg_ix << 1];
			st->iwin_dep3 [owner] [st->iwin_index3[owner]++] = 
					this_inst;
			}
		    }
     }

	if (old_reg_ix >= 0)
		{
		int	sav_half_def;

		sav_half_def = th->half_def[old_lreg>>1];
		th->half_def[old_lreg>>1] = old_half_def;
		ReleaseRegMap (st, th, old_reg_ix, old_lreg);
		th->half_def[old_lreg>>1] = sav_half_def;
		}

	return (0);
	}