996
|
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
2 *
|
|
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
4 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation, either version 3 of the License, or
|
|
9 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
10 * proxy who can decide which future versions of the GNU General Public
|
|
11 * License can be used.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * @file
|
|
24 * Definition and functions for of instruction pointer and instruction
|
|
25 * pointer list.
|
|
26 */
|
|
27
|
|
28 #ifndef FUNGE_HAD_SRC_IP_H
|
|
29 #define FUNGE_HAD_SRC_IP_H
|
|
30
|
|
31 #include "global.h"
|
|
32
|
|
33 #include <sys/types.h>
|
|
34 #include <stdint.h>
|
|
35
|
|
36 #include "stack.h"
|
|
37 #include "vector.h"
|
|
38 #include "funge-space/funge-space.h"
|
|
39 #include "fingerprints/manager.h"
|
|
40
|
|
41 /// IP mode: code.
|
|
42 #define ipmCODE 0x0
|
|
43 /// IP mode: string.
|
|
44 #define ipmSTRING 0x1
|
|
45 /// Type of the ipMode entry.
|
|
46 typedef uint_fast8_t ipMode;
|
|
47
|
|
48 /// This is for size of opcode array.
|
|
49 #define FINGEROPCODECOUNT 26
|
|
50
|
|
51 /// Instruction pointer.
|
|
52 /// @note
|
|
53 /// Fields of the style fingerXXXX* are for fingerprint per-IP data.
|
|
54 /// Please avoid such fields when possible.
|
|
55 typedef struct s_instructionPointer {
|
|
56 funge_stack * stack; ///< Pointer to top stack.
|
|
57 funge_vector position; ///< Current position.
|
|
58 funge_vector delta; ///< Current delta.
|
|
59 funge_vector storageOffset; ///< The storage offset for current IP.
|
|
60 ipMode mode; ///< String or code mode.
|
|
61 // "Full" bool for very often checked flags.
|
|
62 bool needMove; ///< Should ip_forward be called at end of main loop. Is reset to true each time.
|
|
63 bool stringLastWasSpace; ///< Used in string mode for SGML style spaces.
|
|
64 // These are more uncommon flags, and will be turned into bitfields
|
|
65 // should that save space at some point (doesn't currently).
|
|
66 bool fingerSUBRisRelative; ///< Data for fingerprint SUBR.
|
|
67 funge_cell ID; ///< The ID of this IP.
|
|
68 funge_stackstack * stackstack; ///< The stack stack.
|
|
69 fungeOpcodeStack fingerOpcodes[FINGEROPCODECOUNT]; ///< Array of fingerprint opcodes.
|
|
70 void * fingerHRTItimestamp; ///< Data for fingerprint HRTI.
|
|
71 /// We don't know what type here.
|
|
72 } instructionPointer;
|
|
73 #define CF_INSTRUCTIONPOINTER_DEFINED
|
|
74
|
|
75 #ifdef CONCURRENT_FUNGE
|
|
76 /// Instruction pointer list. For concurrent Funge.
|
|
77 typedef struct s_ipList {
|
|
78 size_t size; /**< Total size */
|
|
79 size_t top; /**< Top valid one */
|
|
80 size_t highestID; /**< Currently highest ID, they are unique. */
|
|
81 /**
|
|
82 * This array is slightly complex for speed reasons.
|
|
83 * Main loop must iterate over it *backwards*, this allow easy splitting of last ip.
|
|
84 */
|
|
85 instructionPointer ips[];
|
|
86 } ipList;
|
|
87 #endif
|
|
88
|
|
89 #ifndef CONCURRENT_FUNGE
|
|
90 /**
|
|
91 * Create a new instruction pointer.
|
|
92 */
|
|
93 FUNGE_ATTR_MALLOC FUNGE_ATTR_WARN_UNUSED FUNGE_ATTR_FAST
|
|
94 instructionPointer * ip_create(void);
|
|
95 #endif
|
|
96
|
|
97 #if !defined(CONCURRENT_FUNGE) && !defined(NDEBUG)
|
|
98 /**
|
|
99 * Free an instruction pointer.
|
|
100 */
|
|
101 FUNGE_ATTR_FAST
|
|
102 void ip_free(instructionPointer * restrict ip);
|
|
103 #endif
|
|
104
|
|
105
|
|
106 /**
|
|
107 * Move the IP forwards one step.
|
|
108 * @param m_ip Instruction pointer to operate on.
|
|
109 */
|
|
110 #define ip_forward(m_ip) \
|
|
111 do { \
|
|
112 (m_ip)->position.x += (m_ip)->delta.x; \
|
|
113 (m_ip)->position.y += (m_ip)->delta.y; \
|
|
114 fungespace_wrap(&((m_ip)->position), &((m_ip)->delta)); \
|
|
115 } while(0)
|
|
116 /**
|
|
117 * Move the IP backwards one step.
|
|
118 * @param m_ip Instruction pointer to operate on.
|
|
119 */
|
|
120 #define ip_backward(m_ip) \
|
|
121 do { \
|
|
122 (m_ip)->position.x -= (m_ip)->delta.x; \
|
|
123 (m_ip)->position.y -= (m_ip)->delta.y; \
|
|
124 fungespace_wrap(&((m_ip)->position), &((m_ip)->delta)); \
|
|
125 } while(0)
|
|
126
|
|
127 /**
|
|
128 * Mirror IP direction.
|
|
129 */
|
|
130 #define ip_reverse(m_ip) \
|
|
131 do { \
|
|
132 (m_ip)->delta.x *= -1; \
|
|
133 (m_ip)->delta.y *= -1; \
|
|
134 } while(0)
|
|
135 // I don't like the do { ... } while(0) hack at all..
|
|
136 // but it is needed.
|
|
137
|
|
138 /// Turn the IP left as [ would do.
|
|
139 #define ip_turn_left(m_ip) \
|
|
140 do { \
|
|
141 (m_ip)->delta = (funge_vector) { (m_ip)->delta.y, -(m_ip)->delta.x }; \
|
|
142 } while(0)
|
|
143 /// Turn the IP right as ] would do.
|
|
144 #define ip_turn_right(m_ip) \
|
|
145 do { \
|
|
146 (m_ip)->delta = (funge_vector) { -(m_ip)->delta.y, (m_ip)->delta.x }; \
|
|
147 } while(0)
|
|
148
|
|
149 /// Set delta of an IP to a new vector.
|
|
150 FUNGE_ATTR_NONNULL FUNGE_ATTR_FAST
|
|
151 void ip_set_delta(instructionPointer * restrict ip, const funge_vector * restrict delta);
|
|
152 /// Set position of an IP to a new vector. Will wrap if needed (based on current delta).
|
|
153 FUNGE_ATTR_NONNULL FUNGE_ATTR_FAST
|
|
154 void ip_set_position(instructionPointer * restrict ip, const funge_vector * restrict position);
|
|
155
|
|
156 // To make things simpler.
|
|
157 /// Set IP delta to west.
|
|
158 #define ip_go_west(m_ip) do { (m_ip)->delta = (funge_vector) {-1, 0}; } while(0)
|
|
159 /// Set IP delta to east.
|
|
160 #define ip_go_east(m_ip) do { (m_ip)->delta = (funge_vector) {1, 0}; } while(0)
|
|
161 /// Set IP delta to north.
|
|
162 #define ip_go_north(m_ip) do { (m_ip)->delta = (funge_vector) {0, -1}; } while(0)
|
|
163 /// Set IP delta to south.
|
|
164 #define ip_go_south(m_ip) do { (m_ip)->delta = (funge_vector) {0, 1}; } while(0)
|
|
165
|
|
166 #ifdef CONCURRENT_FUNGE
|
|
167 /**
|
|
168 * Create a new IP list with the single default IP in it.
|
|
169 * @warning Should only be called from internal setup code.
|
|
170 */
|
|
171 FUNGE_ATTR_MALLOC FUNGE_ATTR_WARN_UNUSED FUNGE_ATTR_FAST
|
|
172 ipList* iplist_create(void);
|
|
173
|
|
174 #ifndef NDEBUG
|
|
175 /**
|
|
176 * Free an IP list.
|
|
177 * @warning Should only be called from internal tear-down code.
|
|
178 * @param me ipList to free.
|
|
179 */
|
|
180 FUNGE_ATTR_FAST
|
|
181 void iplist_free(ipList* me);
|
|
182 #endif
|
|
183
|
|
184 /**
|
|
185 * Add a new IP, one place before current one.
|
|
186 * @param me ipList to operate on.
|
|
187 * @param index What entry in the list to duplicate.
|
|
188 * @return Returns the index of next to execute as that may have changed after
|
|
189 * this call. A value of -1 = failed to create IP.
|
|
190 * @note This function calls functions which may exit with an OOM error on out
|
|
191 * of memory. As well as functions returning error value.
|
|
192 */
|
|
193 FUNGE_ATTR_NONNULL FUNGE_ATTR_WARN_UNUSED FUNGE_ATTR_FAST
|
|
194 ssize_t iplist_duplicate_ip(ipList** me, size_t index);
|
|
195
|
|
196 /**
|
|
197 * Terminate an ip.
|
|
198 * @param me ipList to operate on.
|
|
199 * @param index What entry in the list to terminate.
|
|
200 * @return Returns index of next to execute as that may have changed after this call.
|
|
201 */
|
|
202 FUNGE_ATTR_NONNULL FUNGE_ATTR_WARN_UNUSED FUNGE_ATTR_FAST
|
|
203 ssize_t iplist_terminate_ip(ipList** me, size_t index);
|
|
204 #endif
|
|
205
|
|
206
|
|
207 #endif
|