2022-01-29 08:26:45 -08:00
|
|
|
/** \file stack.h */
|
|
|
|
/*
|
|
|
|
Copyright 2019 Joel Svensson svenssonjoel@yahoo.se
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#ifndef STACK_H_
|
|
|
|
#define STACK_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-02-01 11:40:07 -08:00
|
|
|
#include "lbm_types.h"
|
2022-01-29 08:26:45 -08:00
|
|
|
|
2022-09-04 07:08:58 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-01-29 08:26:45 -08:00
|
|
|
typedef struct {
|
|
|
|
lbm_uint* data;
|
2022-03-25 07:47:05 -07:00
|
|
|
lbm_uint sp;
|
|
|
|
lbm_uint size;
|
|
|
|
lbm_uint max_sp;
|
2022-01-29 08:26:45 -08:00
|
|
|
} lbm_stack_t;
|
|
|
|
|
|
|
|
/** Allocate a stack on the symbols and arrays memory.
|
|
|
|
* lbm_memory_init must have been run before this function or it will fail.
|
|
|
|
* \param s Pointer to an lbm_stack_t to initialize.
|
|
|
|
* \param stack_size Size in 32 bit words of stack to allocate.
|
|
|
|
* \return 1 on success and 0 on failure.
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_stack_allocate(lbm_stack_t *s, lbm_uint stack_size);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Create a stack in a statically allocated array.
|
|
|
|
*
|
|
|
|
* \param s Pointer to an lbm_stack_t to initialize.
|
|
|
|
* \param data Pointer to array of 32 bit words to use as the stack storage.
|
|
|
|
* \param size Size in number of 32 bit words.
|
|
|
|
* \return 1
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_stack_create(lbm_stack_t *s, lbm_uint* data, lbm_uint size);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Free a stack allocated on the lispbm_memory.
|
|
|
|
*
|
|
|
|
* \param s Pointer to lbm_stack_t to free.
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
void lbm_stack_free(lbm_stack_t *s);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Sets the stack SP to 0.
|
|
|
|
*
|
|
|
|
* \param s Stack to clear.
|
|
|
|
* \return 1
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_stack_clear(lbm_stack_t *s);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Get a pointer to the nth element (from the top) of a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack.
|
|
|
|
* \param n Index.
|
|
|
|
* \return Pointer into the stack or NULL.
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
lbm_uint *lbm_get_stack_ptr(lbm_stack_t *s, lbm_uint n);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Drop n elements (from the top) of a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to drop elements from.
|
|
|
|
* \param n Number of elements to drop.
|
|
|
|
* \return 1 on Success and 0 on failure.
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_stack_drop(lbm_stack_t *s, lbm_uint n);
|
2022-05-03 13:58:33 -07:00
|
|
|
|
|
|
|
/** Reserve place for n elements on the stack and
|
|
|
|
* move the stack pointer to the new top.
|
|
|
|
* \param s Stack to reserve values on
|
|
|
|
* \param n Number of values to reserve
|
|
|
|
* \return Pointer into stack position of reserver value 0 or NULL
|
|
|
|
* on failure
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
lbm_uint *lbm_stack_reserve(lbm_stack_t *s, lbm_uint n);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Push an element onto a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to push a value onto.
|
|
|
|
* \param val Value to push to the stack.
|
|
|
|
* \return 1 on success and 0 on failure (stack is full).
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_push(lbm_stack_t *s, lbm_uint val);
|
2022-01-29 08:26:45 -08:00
|
|
|
/** Pop a value from a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to pop a value from.
|
|
|
|
* \param val Pointer to an lbm_value to store the pop:ed value int.
|
|
|
|
* \return 1 on success and 0 on failure (stack is empty).
|
|
|
|
*/
|
2022-09-04 07:08:58 -07:00
|
|
|
int lbm_pop(lbm_stack_t *s, lbm_uint *val);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Check if a stack is empty.
|
|
|
|
*
|
|
|
|
* \param s Stack to check.
|
|
|
|
* \return 1 if stack is empty otherwise 0.
|
|
|
|
*/
|
|
|
|
static inline int lbm_stack_is_empty(lbm_stack_t *s) {
|
|
|
|
if (s->sp == 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Push 2 values to a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to push values onto.
|
|
|
|
* \param val0 Is pushed first.
|
|
|
|
* \param val1 Is pushed last.
|
|
|
|
* \return 1 on success and 0 on failure (stack is full).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_push_2(lbm_stack_t *s, lbm_uint val0, lbm_uint val1);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Push 3 values to a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to push values onto.
|
|
|
|
* \param val0
|
|
|
|
* \param val1
|
|
|
|
* \param val2
|
|
|
|
* \return 1 on success and 0 on failure (stack is full).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_push_3(lbm_stack_t *s, lbm_uint val0, lbm_uint val1, lbm_uint val2);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Push 4 values to a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to push values onto.
|
|
|
|
* \param val0
|
|
|
|
* \param val1
|
|
|
|
* \param val2
|
|
|
|
* \param val3
|
|
|
|
* \return 1 on success and 0 on failure (stack is full).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_push_4(lbm_stack_t *s, lbm_uint val0, lbm_uint val1, lbm_uint val2, lbm_uint val3);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Push 5 values to a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to push values onto.
|
|
|
|
* \param val0
|
|
|
|
* \param val1
|
|
|
|
* \param val2
|
|
|
|
* \param val3
|
|
|
|
* \param val4
|
|
|
|
* \return 1 on success and 0 on failure (stack is full).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_push_5(lbm_stack_t *s, lbm_uint val0, lbm_uint val1, lbm_uint val2, lbm_uint val3, lbm_uint val4);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Pop 2 values from a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to pop values from.
|
|
|
|
* \param r0 Pointer to lbm_value where the first pop:ed value will be stored.
|
|
|
|
* \param r1 Pointer to lbm_value where the seconds pop:ed value will be stored.
|
|
|
|
* \return 1 on success and 0 on failure (stack is empty).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_pop_2(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
|
|
|
/** Pop 3 values from a stack.
|
|
|
|
*
|
|
|
|
* \param s Stack to pop values from.
|
|
|
|
* \param r0
|
|
|
|
* \param r1
|
|
|
|
* \param r2
|
|
|
|
* \return 1 on success and 0 on failure (stack is empty).
|
|
|
|
*/
|
2022-10-17 05:42:17 -07:00
|
|
|
int lbm_pop_3(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1, lbm_uint *r2);
|
2022-01-29 08:26:45 -08:00
|
|
|
|
2022-09-04 07:08:58 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2022-01-29 08:26:45 -08:00
|
|
|
#endif
|