/*
 * variable.c
 *
 * Variable manipulation routines
 *
 */

#include "ztypes.h"
#include "variable.h"
#include "operand.h"

/*
 * load
 *
 * Load and store a variable value.
 *
 */

void z_load(int variable)
{
    store_operand(load_variable(variable));

}/* load */

#if 0
/*
 * push
 *
 * Push a value onto the stack
 *
 */

void z_push(unsigned value)
{
    *--sp = (zword_t) value;

}/* push */
#endif

/*
 * pull
 *
 * Pop a variable from the stack.
 *
 */

void z_pull(int argc, unsigned *argv)
{
    if (h_type != V6)
        /* Pull a variable from the stack */
    	z_store(argv[0], (zword_t) *sp++);
    else
    {
        if (argc==0)
            /* Return the top of the stack */
            store_operand(*sp++);
        else
        {
            /* Return the top of a user stack */
            int stack, free_slots;

            stack=argv[0];
            free_slots=get_word(stack)+1;
            set_word(stack, free_slots);
            store_operand(get_word(stack+free_slots*2));
        }
    }
}/* pull */

/*
 * inc
 *
 * Increment a variable.
 *
 */

void z_inc(int variable)
{
    z_store(variable, (zword_t) (load_variable(variable) + 1));

}/* inc */

/*
 * dec
 *
 * Decrement a variable.
 *
 */

void z_dec(int variable)
{
    z_store(variable, (zword_t) (load_variable(variable) - 1));

}/* dec */

/*
 * inc_chk
 *
 * Increment a variable and then check its value against a target.
 *
 */

void z_inc_chk(int variable, short target)
{
    short value;

    value = (short) load_variable(variable);
    z_store(variable, (zword_t) ++value);
    conditional_jump(value > target);

}/* inc_chk */

/*
 * dec_chk
 *
 * Decrement a variable and then check its value against a target.
 *
 */

void z_dec_chk(int variable, short target)
{
    short value;

    value = (short) load_variable(variable);
    z_store(variable, (zword_t) --value);
    conditional_jump(value < target);

}/* dec_chk */
