+ CORE_ADDR func_addr, func_end, addr, stop;
+ CORE_ADDR stack_size;
+ int imm_size;
+ unsigned char buf[4];
+ int status, movm_args = 0;
+ char *name;
+
+ /* Use the PC in the frame if it's provided to look up the
+ start of this function. */
+ pc = (fi ? fi->pc : pc);
+
+ /* Find the start of this function. */
+ status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
+
+ /* Do nothing if we couldn't find the start of this function or if we're
+ stopped at the first instruction in the prologue. */
+ if (status == 0)
+ {
+ return pc;
+ }
+
+ /* If we're in start, then give up. */
+ if (strcmp (name, "start") == 0)
+ {
+ if (fi != NULL)
+ fi->extra_info->status = NO_MORE_FRAMES;
+ return pc;
+ }
+
+ /* At the start of a function our frame is in the stack pointer. */
+ if (fi)
+ fi->extra_info->status = MY_FRAME_IN_SP;
+
+ /* Get the next two bytes into buf, we need two because rets is a two
+ byte insn and the first isn't enough to uniquely identify it. */
+ status = read_memory_nobpt (pc, buf, 2);
+ if (status != 0)
+ return pc;
+
+ /* If we're physically on an "rets" instruction, then our frame has
+ already been deallocated. Note this can also be true for retf
+ and ret if they specify a size of zero.
+
+ In this case fi->frame is bogus, we need to fix it. */
+ if (fi && buf[0] == 0xf0 && buf[1] == 0xfc)
+ {
+ if (fi->next == NULL)
+ fi->frame = read_sp ();
+ return fi->pc;
+ }
+
+ /* Similarly if we're stopped on the first insn of a prologue as our
+ frame hasn't been allocated yet. */
+ if (fi && fi->pc == func_addr)
+ {
+ if (fi->next == NULL)
+ fi->frame = read_sp ();
+ return fi->pc;
+ }
+
+ /* Figure out where to stop scanning. */
+ stop = fi ? fi->pc : func_end;
+
+ /* Don't walk off the end of the function. */
+ stop = stop > func_end ? func_end : stop;
+
+ /* Start scanning on the first instruction of this function. */
+ addr = func_addr;
+
+ /* Suck in two bytes. */
+ status = read_memory_nobpt (addr, buf, 2);
+ if (status != 0)
+ {
+ fix_frame_pointer (fi, 0);
+ return addr;
+ }
+
+ /* First see if this insn sets the stack pointer from a register; if
+ so, it's probably the initialization of the stack pointer in _start,
+ so mark this as the bottom-most frame. */
+ if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
+ {
+ if (fi)
+ fi->extra_info->status = NO_MORE_FRAMES;
+ return addr;
+ }
+
+ /* Now look for movm [regs],sp, which saves the callee saved registers.
+
+ At this time we don't know if fi->frame is valid, so we only note
+ that we encountered a movm instruction. Later, we'll set the entries
+ in fsr.regs as needed. */
+ if (buf[0] == 0xcf)
+ {
+ /* Extract the register list for the movm instruction. */
+ status = read_memory_nobpt (addr + 1, buf, 1);
+ movm_args = *buf;
+
+ addr += 2;
+
+ /* Quit now if we're beyond the stop point. */
+ if (addr >= stop)
+ {
+ /* Fix fi->frame since it's bogus at this point. */
+ if (fi && fi->next == NULL)
+ fi->frame = read_sp ();
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+
+ /* Get the next two bytes so the prologue scan can continue. */
+ status = read_memory_nobpt (addr, buf, 2);
+ if (status != 0)
+ {
+ /* Fix fi->frame since it's bogus at this point. */
+ if (fi && fi->next == NULL)
+ fi->frame = read_sp ();
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+ }
+
+ /* Now see if we set up a frame pointer via "mov sp,a3" */
+ if (buf[0] == 0x3f)
+ {
+ addr += 1;
+
+ /* The frame pointer is now valid. */
+ if (fi)
+ {
+ fi->extra_info->status |= MY_FRAME_IN_FP;
+ fi->extra_info->status &= ~MY_FRAME_IN_SP;
+ }
+
+ /* Quit now if we're beyond the stop point. */
+ if (addr >= stop)
+ {
+ /* Fix fi->frame if it's bogus at this point. */
+ fix_frame_pointer (fi, 0);
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+
+ /* Get two more bytes so scanning can continue. */
+ status = read_memory_nobpt (addr, buf, 2);
+ if (status != 0)
+ {
+ /* Fix fi->frame if it's bogus at this point. */
+ fix_frame_pointer (fi, 0);
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+ }
+
+ /* Next we should allocate the local frame. No more prologue insns
+ are found after allocating the local frame.
+
+ Search for add imm8,sp (0xf8feXX)
+ or add imm16,sp (0xfafeXXXX)
+ or add imm32,sp (0xfcfeXXXXXXXX).
+
+ If none of the above was found, then this prologue has no
+ additional stack. */
+
+ status = read_memory_nobpt (addr, buf, 2);
+ if (status != 0)
+ {
+ /* Fix fi->frame if it's bogus at this point. */
+ fix_frame_pointer (fi, 0);
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+
+ imm_size = 0;
+ if (buf[0] == 0xf8 && buf[1] == 0xfe)
+ imm_size = 1;
+ else if (buf[0] == 0xfa && buf[1] == 0xfe)
+ imm_size = 2;
+ else if (buf[0] == 0xfc && buf[1] == 0xfe)
+ imm_size = 4;
+
+ if (imm_size != 0)
+ {
+ /* Suck in imm_size more bytes, they'll hold the size of the
+ current frame. */
+ status = read_memory_nobpt (addr + 2, buf, imm_size);
+ if (status != 0)
+ {
+ /* Fix fi->frame if it's bogus at this point. */
+ fix_frame_pointer (fi, 0);
+
+ /* Note if/where callee saved registers were saved. */
+ set_movm_offsets (fi, movm_args);
+ return addr;
+ }
+
+ /* Note the size of the stack in the frame info structure. */
+ stack_size = extract_signed_integer (buf, imm_size);
+ if (fi)
+ fi->extra_info->stack_size = stack_size;
+
+ /* We just consumed 2 + imm_size bytes. */
+ addr += 2 + imm_size;