| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2019, 2021, 2024 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <lbm_memory.h> | ||
| 19 | #include <lbm_types.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #include "stack.h" | ||
| 23 | #include "print.h" | ||
| 24 | |||
| 25 | #define STACK_UNUSED_BYTE 0x55 | ||
| 26 | #ifndef LBM64 | ||
| 27 | #define STACK_UNUSED_WORD 0x55555555 | ||
| 28 | #else | ||
| 29 | #define STACK_UNUSED_WORD 0x5555555555555555 | ||
| 30 | #endif | ||
| 31 | |||
| 32 | 69970 | int lbm_stack_allocate(lbm_stack_t *s, lbm_uint stack_size) { | |
| 33 | 69970 | int r = 0; | |
| 34 | 69970 | s->data = lbm_memory_allocate(stack_size); | |
| 35 |
2/2✓ Branch 0 taken 69801 times.
✓ Branch 1 taken 169 times.
|
69970 | if (s->data) { |
| 36 | 69801 | memset(s->data, STACK_UNUSED_BYTE, stack_size * sizeof(lbm_uint)); | |
| 37 | 69801 | s->sp = 0; | |
| 38 | 69801 | s->size = stack_size; | |
| 39 | 69801 | r = 1; | |
| 40 | } | ||
| 41 | 69970 | return r; | |
| 42 | } | ||
| 43 | |||
| 44 | 133143 | int lbm_stack_create(lbm_stack_t *s, lbm_uint* data, lbm_uint stack_size) { | |
| 45 | 133143 | s->data = data; | |
| 46 | 133143 | memset(s->data, STACK_UNUSED_BYTE, stack_size * sizeof(lbm_uint)); | |
| 47 | 133143 | s->sp = 0; | |
| 48 | 133143 | s->size = stack_size; | |
| 49 | 133143 | return 1; | |
| 50 | } | ||
| 51 | |||
| 52 | 9 | lbm_uint lbm_get_max_stack(lbm_stack_t *s) { | |
| 53 | 9 | lbm_uint unused = 0; | |
| 54 |
2/2✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 6 times.
|
1331 | for (int i = (int)s->size-1 ; i >= 0; i --) { |
| 55 |
2/2✓ Branch 0 taken 1322 times.
✓ Branch 1 taken 3 times.
|
1325 | if (s->data[i] == STACK_UNUSED_WORD) { |
| 56 | 1322 | unused ++; | |
| 57 | } else { | ||
| 58 | 3 | break; | |
| 59 | } | ||
| 60 | } | ||
| 61 | 9 | return s->size - unused; | |
| 62 | } | ||
| 63 | |||
| 64 | 69052 | void lbm_stack_free(lbm_stack_t *s) { | |
| 65 |
2/2✓ Branch 0 taken 69051 times.
✓ Branch 1 taken 1 times.
|
69052 | if (s->data) { |
| 66 | 69051 | lbm_memory_free(s->data); | |
| 67 | } | ||
| 68 | 69052 | } | |
| 69 | |||
| 70 | 1003186 | void lbm_stack_clear(lbm_stack_t *s) { | |
| 71 | 1003186 | s->sp = 0; | |
| 72 | 1003186 | } | |
| 73 | |||
| 74 | 1608797787 | int lbm_stack_drop(lbm_stack_t *s, lbm_uint n) { | |
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1608797786 times.
|
1608797787 | if (n > s->sp) return 0; |
| 77 | |||
| 78 | 1608797786 | s->sp -= n; | |
| 79 | 1608797786 | return 1; | |
| 80 | } | ||
| 81 | |||
| 82 | 98280313 | int lbm_push(lbm_stack_t *s, lbm_uint val) { | |
| 83 | 98280313 | int res = 1; | |
| 84 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 98280310 times.
|
98280313 | if (s->sp == s->size) { |
| 85 | 3 | return 0; | |
| 86 | } | ||
| 87 | 98280310 | s->data[s->sp++] = val; | |
| 88 | 98280310 | return res; | |
| 89 | } | ||
| 90 | |||
| 91 | 127017040 | int lbm_pop(lbm_stack_t *s, lbm_uint *val) { | |
| 92 | 127017040 | s->sp--; | |
| 93 | 127017040 | *val = s->data[s->sp]; | |
| 94 | 127017040 | return 1; | |
| 95 | } | ||
| 96 | |||
| 97 | 1602 | int lbm_pop_2(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1) { | |
| 98 | 1602 | s->sp--; | |
| 99 | 1602 | *r0 = s->data[s->sp--]; | |
| 100 | 1602 | *r1 = s->data[s->sp]; | |
| 101 | 1602 | return 1; | |
| 102 | } | ||
| 103 | |||
| 104 | 1 | int lbm_pop_3(lbm_stack_t *s, lbm_uint *r0, lbm_uint *r1, lbm_uint *r2) { | |
| 105 | 1 | s->sp--; | |
| 106 | 1 | *r0 = s->data[s->sp--]; | |
| 107 | 1 | *r1 = s->data[s->sp--]; | |
| 108 | 1 | *r2 = s->data[s->sp]; | |
| 109 | 1 | return 1; | |
| 110 | } | ||
| 111 | |||
| 112 |