ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
begin_for_mt.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41void begin_for_prepare_mt( Environment * _environment, char * _index ) {
42
44
45 Loop * loop = malloc( sizeof( Loop ) );
46 memset( loop, 0, sizeof( Loop ) );
47 loop->label = strdup( label );
48 loop->type = LT_FOR_MT;
49 loop->next = _environment->loops;
50 _environment->loops = loop;
51
52 unsigned char beginForPrepareAfter[MAX_TEMPORARY_STORAGE]; sprintf(beginForPrepareAfter, "%sprepa", label );
53
54 cpu_jump( _environment, beginForPrepareAfter );
55
56 Variable * index = NULL;
57 if ( variable_exists( _environment, _index ) ) {
58 index = variable_retrieve( _environment, _index );
59 } else {
60 index = variable_retrieve_or_define( _environment, _index, _environment->defaultVariableType, 0 );
61 }
62
63 loop->index = index;
64
65}
66
67void begin_for_from_prepare_mt( Environment * _environment ) {
68
69 Loop * loop = _environment->loops;
70
71 unsigned char beginForFromPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForFromPrepare, "%sprepfrom", loop->label );
72
73 cpu_label( _environment, beginForFromPrepare );
74
75}
76
77void begin_for_from_assign_mt( Environment * _environment, char * _from ) {
78
79 Loop * loop = _environment->loops;
80
81 Variable * from = variable_retrieve( _environment, _from );
82 Variable * fromResident = variable_resident( _environment, VT_TARRAY, "(from)" );
83 _environment->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
84 _environment->arrayDimensions = 1;
85 variable_array_type( _environment, fromResident->name, loop->index->arrayType );
86
87 parser_array_init( _environment );
88 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
89 variable_move_array( _environment, fromResident->name, from->name );
90 parser_array_cleanup( _environment );
91
92 cpu_return( _environment );
93
94 loop->from = from;
95 loop->from->locked = 1;
96 loop->fromResident = fromResident;
97 loop->fromResident->locked = 1;
98
99 loop->to = NULL;
100
101}
102
103void begin_for_to_prepare_mt( Environment * _environment ) {
104
105 Loop * loop = _environment->loops;
106
107 unsigned char beginForToPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForToPrepare, "%sprepto", loop->label );
108
109 cpu_label( _environment, beginForToPrepare );
110
111}
112
113void begin_for_to_assign_mt( Environment * _environment, char * _to ) {
114
115 Loop * loop = _environment->loops;
116 Variable * to = variable_retrieve( _environment, _to );
117
118 Variable * toResident = variable_resident( _environment, VT_TARRAY, "(to)" );
119 _environment->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
120 _environment->arrayDimensions = 1;
121 variable_array_type( _environment, toResident->name, loop->index->arrayType );
122
123 parser_array_init( _environment );
124 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
125 variable_move_array( _environment, toResident->name, to->name );
126 parser_array_cleanup( _environment );
127
128 cpu_return( _environment );
129
130 loop->to = to;
131 loop->to->locked = 1;
132 loop->toResident = toResident;
133 loop->toResident->locked = 1;
134
135}
136
138
139 Loop * loop = _environment->loops;
140
141 unsigned char beginForStepPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForStepPrepare, "%sprepstep", loop->label );
142
143 cpu_label( _environment, beginForStepPrepare );
144
145}
146
147void begin_for_step_assign_mt( Environment * _environment, char * _step ) {
148
149 Loop * loop = _environment->loops;
150 Variable * step = NULL;
151
152 Variable * from = loop->from;
153 Variable * to = loop->to;
154
155 // Calculate the maximum rappresentable size for the index, based on from and to.
156 int maxType = VT_MAX_BITWIDTH_TYPE( from->type, to->type );
157
158 Variable * stepResident = NULL;
159 if ( _step ) {
160 step = variable_retrieve( _environment, _step );
161 if ( VT_SIGNED( from->type ) || VT_SIGNED( to->type ) || VT_SIGNED( step->type ) ) {
162 maxType = VT_SIGN( maxType );
163 }
164
165 // In this version, the step is given
166 stepResident = variable_resident( _environment, VT_TARRAY, "(to)" );
167 _environment->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
168 _environment->arrayDimensions = 1;
169 variable_array_type( _environment, stepResident->name, step->type );
170
171 parser_array_init( _environment );
172 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
173 variable_move_array( _environment, stepResident->name, step->name );
174 parser_array_cleanup( _environment );
175
176 if ( step ) {
177 loop->step = step;
178 loop->step->locked = 1;
179 }
180 } else {
181
182 if ( VT_SIGNED( from->type ) || VT_SIGNED( to->type ) ) {
183 maxType = VT_SIGN( maxType );
184 }
185 // In this version, the step is not given - by default, step = 1
186 step = variable_temporary( _environment, _environment->defaultVariableType, "(step 1)" );
187 variable_store( _environment, step->name, 1 );
188
189 stepResident = variable_resident( _environment, VT_TARRAY, "(to)" );
190 _environment->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
191 _environment->arrayDimensions = 1;
192 variable_array_type( _environment, stepResident->name, _environment->defaultVariableType );
193
194 parser_array_init( _environment );
195 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
196 variable_move_array( _environment, stepResident->name, step->name );
197 parser_array_cleanup( _environment );
198 }
199
200 loop->stepResident = stepResident;
201 loop->stepResident->locked = 1;
202
203 cpu_return( _environment );
204
205 if ( step ) {
206 loop->step = step;
207 loop->step->locked = 1;
208 }
209 loop->stepResident = stepResident;
210 loop->stepResident->locked = 1;
211
212}
213
214void begin_for_identifier_mt( Environment * _environment, char * _index ) {
215
216 Loop * loop = _environment->loops;
217
218 unsigned char beginForPrepareAfter[MAX_TEMPORARY_STORAGE]; sprintf(beginForPrepareAfter, "%sprepa", loop->label );
219 unsigned char beginForFromPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForFromPrepare, "%sprepfrom", loop->label );
220 unsigned char beginForToPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForToPrepare, "%sprepto", loop->label );
221 unsigned char beginForStepPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForStepPrepare, "%sprepstep", loop->label );
222
223 cpu_label( _environment, beginForPrepareAfter );
224
225 cpu_call( _environment, beginForFromPrepare );
226 cpu_call( _environment, beginForToPrepare );
227 cpu_call( _environment, beginForStepPrepare );
228
229 Variable * index = variable_retrieve( _environment, _index );
230
231 loop->index = index;
232
233 Variable * from = loop->fromResident;
234 Variable * to = loop->toResident;
235 Variable * step = loop->stepResident;
236
237 unsigned char beginFor[MAX_TEMPORARY_STORAGE]; sprintf(beginFor, "%sbf", loop->label );
238 unsigned char backwardFor[MAX_TEMPORARY_STORAGE]; sprintf(backwardFor, "%sback", loop->label );
239 unsigned char forwardFor[MAX_TEMPORARY_STORAGE]; sprintf(forwardFor, "%sforw", loop->label );
240 unsigned char continueFor[MAX_TEMPORARY_STORAGE]; sprintf(continueFor, "%scont", loop->label );
241 unsigned char endFor[MAX_TEMPORARY_STORAGE]; sprintf(endFor, "%sbis", loop->label );
242
243 Variable * isLastStep;
244
245 parser_array_init( _environment );
246 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
247 Variable * fromValue = variable_move_from_array( _environment, loop->fromResident->name );
248 parser_array_cleanup( _environment );
249
250 parser_array_init( _environment );
251 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
252 variable_move_array( _environment, index->name, fromValue->name );
253 parser_array_cleanup( _environment );
254
255 cpu_label( _environment, beginFor );
256
257 parser_array_init( _environment );
258 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
259 Variable * indexValue = variable_move_from_array( _environment, loop->index->name );
260 parser_array_cleanup( _environment );
261
262 parser_array_init( _environment );
263 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
264 Variable * stepValue = variable_move_from_array( _environment, loop->stepResident->name );
265 parser_array_cleanup( _environment );
266
267 parser_array_init( _environment );
268 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
269 fromValue = variable_move_from_array( _environment, loop->fromResident->name );
270 parser_array_cleanup( _environment );
271
272 parser_array_init( _environment );
273 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
274 Variable * toValue = variable_move_from_array( _environment, loop->toResident->name );
275 parser_array_cleanup( _environment );
276
277 cpu_bvneq( _environment, variable_greater_than_const( _environment, stepValue->name, 0, 0)->realName, forwardFor );
278
279 cpu_jump( _environment, backwardFor );
280
281 cpu_label( _environment, forwardFor );
282
283 // Finish the loop if the index is less than lower bound.
284 isLastStep = variable_less_than( _environment, indexValue->name, fromValue->name, 0 );
285 cpu_bvneq( _environment, isLastStep->realName, endFor );
286
287 // Finish the loop if the index is less than upper bound.
288 isLastStep = variable_greater_than( _environment, indexValue->name, toValue->name, 0 );
289 cpu_bvneq( _environment, isLastStep->realName, endFor );
290
291 cpu_jump( _environment, continueFor );
292
293 cpu_label( _environment, backwardFor );
294
295 // Finish the loop if the index is less than lower bound.
296 isLastStep = variable_greater_than( _environment, indexValue->name, fromValue->name, 0 );
297 cpu_bvneq( _environment, isLastStep->realName, endFor );
298
299 // Finish the loop if the index is less than upper bound.
300 isLastStep = variable_less_than( _environment, indexValue->name, toValue->name, 0 );
301 cpu_bvneq( _environment, isLastStep->realName, endFor );
302
303 cpu_label( _environment, continueFor );
304
305}
306
308
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_call(Environment *_environment, char *_label)
Definition 6309.c:3755
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_bvneq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:345
void cpu_return(Environment *_environment)
Definition 6309.c:4030
Variable * variable_retrieve(Environment *_environment, char *_name)
void parser_array_index_symbolic(Environment *_environment, char *_index)
void parser_array_cleanup(Environment *_environment)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
void variable_move_array(Environment *_environment, char *_array, char *_value)
int variable_exists(Environment *_environment, char *_name)
Variable * variable_less_than(Environment *_environment, char *_source, char *_destination, int _equal)
Compare two variable and return the result of comparation.
Variable * variable_greater_than(Environment *_environment, char *_source, char *_destination, int _equal)
Compare two variable and return the result of comparation.
Variable * variable_array_type(Environment *_environment, char *_name, VariableType _type)
Variable * variable_move_from_array(Environment *_environment, char *_array)
Variable * variable_resident(Environment *_environment, VariableType _type, char *_meaning)
void parser_array_init(Environment *_environment)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * variable_greater_than_const(Environment *_environment, char *_source, int _destination, int _equal)
void begin_for_from_assign_mt(Environment *_environment, char *_from)
void begin_for_from_prepare_mt(Environment *_environment)
void begin_for_step_assign_mt(Environment *_environment, char *_step)
void begin_for_prepare_mt(Environment *_environment, char *_index)
void begin_for_step_prepare_mt(Environment *_environment)
void begin_for_identifier_mt(Environment *_environment, char *_index)
void begin_for_to_assign_mt(Environment *_environment, char *_to)
void begin_for_to_prepare_mt(Environment *_environment)
VariableType defaultVariableType
Definition ugbc.h:2956
int arrayDimensions
Definition ugbc.h:2718
int arrayDimensionsEach[MAX_ARRAY_DIMENSIONS]
Definition ugbc.h:2723
ProtothreadConfig protothreadConfig
Definition ugbc.h:2430
Loop * loops
Definition ugbc.h:2669
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
void * malloc(YYSIZE_T)
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
void loop(Environment *_environment, char *_label)
#define VT_SIGNED(t)
Definition ugbc.h:618
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_TARRAY
Definition ugbc.h:480
@ LT_FOR_MT
Definition ugbc.h:1399
#define VT_MAX_BITWIDTH_TYPE(a, b)
Definition ugbc.h:606
struct _Loop Loop
Structure of a single loop.
#define VT_SIGN(t)
Definition ugbc.h:633
#define MAKE_LABEL
Definition ugbc.h:3351