#include "forth.h"
/* Stack stuff (both data and return stacks). */

PRIMITIVE ("pop", Fpop, Spop, NON_IMMEDIATE, ( n -- ) )
{
  POP;
}


PRIMITIVE ("dup", Fdup, Sdup, NON_IMMEDIATE, ( n -- n n ) )
{
  PUSH (sp[0]);
}


PRIMITIVE ("over", Fover, Sover, NON_IMMEDIATE, ( n1 n2 -- n1 n2 n1 ) )
{
  PUSH (sp[2]);
}


PRIMITIVE ("swap", Fswap, Sswap, NON_IMMEDIATE, ( n1 n2 -- n2 n1 ) )
{
  /* We use the space beyond the stack for temp storage. */
  sp[-2] = sp[2];
  sp[2]  = sp[0];
  sp[0]  = sp[-2];
}


PRIMITIVE ("rot", Frot, Srot, NON_IMMEDIATE, ( n1 n2 n3 -- n2 n3 n1 ) )
{
  /* We use the space beyond the stack for temp storage. */
  sp[-2] = sp[4];
  sp[4]  = sp[2];
  sp[2]  = sp[0];
  sp[0]  = sp[-2];
}


PRIMITIVE (">r", Fto_r, Sto_r, NON_IMMEDIATE, ( n -- ) )
{
  /* Pop top and push it onto return stack. */
  RPUSH ((funcptr *) POP);
}


PRIMITIVE ("r>", Fr_from, Sr_from, NON_IMMEDIATE, ( -- n ) )
{
  /* Pop return stack and push it onto data stack. */
  PUSH ((long) RPOP);
}


PRIMITIVE ("r@", Fr_fetch, Sr_fetch, NON_IMMEDIATE, ( -- n ) )
{
  /* Copy top of return stack and push that onto data stack. */
  PUSH ((long) *rp);
}


PRIMITIVE ("print_stack", Fprint_stack, Sprint_stack, NON_IMMEDIATE, ( n -- ) )
{
  /* Print the contents of the data stack, to the depth specified by
   * N on top of the stack.
   */

  long n = POP;
  PRINT_STACK (n);
}


PRIMITIVE ("print_rstack", Fprint_rstack, Sprint_rstack,
           NON_IMMEDIATE, ( n -- ) )
{
  /* Print the contents of the data stack, to the depth specified by
   * N on top of the stack.
   */

  long n = POP;
  PRINT_RSTACK (n);
}


