ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
end_for.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
49void end_for_identifier( Environment * _environment, char * _index ) {
50
51 Variable * index = variable_retrieve( _environment, _index );
52
53 Loop * loop = _environment->loops;
54
55 if ( ! loop ) {
57 }
58
59 if ( loop->type != LT_FOR && loop->type != LT_FOR_MT ) {
61 }
62
63 if ( strcmp( loop->index->name, index->name ) ) {
65 }
66
67 end_for( _environment );
68
69}
70
71void end_for( Environment * _environment ) {
72
73 Loop * loop = _environment->loops;
74
75 if ( ! loop ) {
77 }
78
79 if ( loop->type != LT_FOR && loop->type != LT_FOR_MT ) {
81 }
82
83 unsigned char beginForFromPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForFromPrepare, "%sprepfrom", loop->label );
84 unsigned char beginForToPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForToPrepare, "%sprepto", loop->label );
85 unsigned char beginForStepPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForStepPrepare, "%sprepstep", loop->label );
86
87 unsigned char assignStep[MAX_TEMPORARY_STORAGE]; sprintf(assignStep, "%sas", loop->label );
88
89 Variable * step = loop->stepResident;
90
91 unsigned char beginFor[MAX_TEMPORARY_STORAGE]; sprintf(beginFor, "%sbf", loop->label );
92 unsigned char endFor[MAX_TEMPORARY_STORAGE]; sprintf(endFor, "%sbis", loop->label );
93 unsigned char beginForPrepare[MAX_TEMPORARY_STORAGE]; sprintf(beginForPrepare, "%sprep", loop->label );
94
95 if ( !loop->statical ) {
96 cpu_call( _environment, beginForFromPrepare );
97 cpu_call( _environment, beginForToPrepare );
98 cpu_call( _environment, beginForStepPrepare );
99 }
100
101 if ( loop->type == LT_FOR ) {
102
103 if ( step->initializedByConstant ) {
104 variable_add_inplace( _environment, loop->index->name, step->value );
105 } else {
106 variable_add_inplace_vars( _environment, loop->index->name, step->name );
107 }
108
109 if ( !VT_SIGNED( loop->index->type ) ) {
110 variable_compare_and_branch_const( _environment, loop->index->name, 0, endFor, 1 );
111 }
112
113 if ( loop->from->initializedByConstant ) {
114 Variable * isLastStep = variable_compare_const( _environment, loop->index->name, loop->from->value );
115 cpu_bvneq( _environment, isLastStep->realName, endFor );
116 } else {
117 Variable * isLastStep = variable_compare( _environment, loop->index->name, loop->fromResident->name );
118 cpu_bvneq( _environment, isLastStep->realName, endFor );
119 }
120
121 } else {
122 parser_array_init( _environment );
123 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
124 Variable * array = variable_retrieve( _environment, loop->index->name );
125 if ( array->type != VT_TARRAY ) {
126 CRITICAL_NOT_ARRAY( loop->index->name );
127 }
128 Variable * value = variable_move_from_array( _environment, loop->index->name );
129 parser_array_cleanup( _environment );
130
131 parser_array_init( _environment );
132 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
133 Variable * stepValue = variable_move_from_array( _environment, loop->stepResident->name );
134 parser_array_cleanup( _environment );
135
136 parser_array_init( _environment );
137 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
138 Variable * fromValue = variable_move_from_array( _environment, loop->fromResident->name );
139 parser_array_cleanup( _environment );
140
141 variable_add_inplace_vars( _environment, value->name, stepValue->name );
142
143 parser_array_init( _environment );
144 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
145 array = variable_retrieve( _environment, loop->index->name );
146 if ( array->type != VT_TARRAY ) {
147 CRITICAL_NOT_ARRAY( loop->index->name );
148 }
149 variable_move_array( _environment, loop->index->name, value->name );
150 parser_array_cleanup( _environment );
151
152 if ( !VT_SIGNED( value->type ) ) {
153 variable_compare_and_branch_const( _environment, value->name, 0, endFor, 1 );
154 }
155
156 Variable * isLastStep = variable_compare( _environment, value->name, fromValue->name );
157 cpu_bvneq( _environment, isLastStep->realName, endFor );
158
159 }
160
161 if ( _environment->procedureName && _environment->protothread && ! _environment->protothreadForbid ) {
162 yield( _environment );
163 }
164
165 cpu_jump( _environment, beginFor );
166
167 cpu_label( _environment, endFor );
168
169 if ( loop->to ) {
170 loop->to->locked = 0;
171 }
172 if ( loop->from ) {
173 loop->from->locked = 0;
174 }
175 if ( loop->step ) {
176 loop->step->locked = 0;
177 }
178 if ( loop->zero ) {
179 loop->zero->locked = 0;
180 }
181 if ( loop->toResident ) {
182 loop->toResident->locked = 0;
183 }
184 if ( loop->fromResident ) {
185 loop->fromResident->locked = 0;
186 }
187 if ( loop->stepResident ) {
188 loop->stepResident->locked = 0;
189 }
190
191 _environment->loops = _environment->loops->next;
192
193};
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
Variable * variable_retrieve(Environment *_environment, char *_name)
void variable_add_inplace(Environment *_environment, char *_source, int _destination)
void parser_array_index_symbolic(Environment *_environment, char *_index)
void parser_array_cleanup(Environment *_environment)
void variable_move_array(Environment *_environment, char *_array, char *_value)
void variable_compare_and_branch_const(Environment *_environment, char *_source, int _destination, char *_name, int _positive)
Variable * variable_move_from_array(Environment *_environment, char *_array)
void variable_add_inplace_vars(Environment *_environment, char *_source, char *_destination)
Add two variable and return the sum of them on the first.
void parser_array_init(Environment *_environment)
Variable * variable_compare(Environment *_environment, char *_source, char *_destination)
Compare two variable and return the result of comparation.
Variable * variable_compare_const(Environment *_environment, char *_source, int _destination)
Compare two variable and return the result of comparation.
void end_for_identifier(Environment *_environment, char *_index)
Emit ASM code for ... NEXT.
Definition end_for.c:49
void end_for(Environment *_environment)
Definition end_for.c:71
int protothread
Definition ugbc.h:2830
char * procedureName
Definition ugbc.h:2775
int protothreadForbid
Definition ugbc.h:2845
Loop * loops
Definition ugbc.h:2669
struct _Loop * next
Definition ugbc.h:1452
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
int value
Definition ugbc.h:1025
int initializedByConstant
Definition ugbc.h:1036
char * realName
Definition ugbc.h:982
#define CRITICAL_WRONG_NEXT_INDEX(v)
Definition ugbc.h:3699
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
void loop(Environment *_environment, char *_label)
#define VT_SIGNED(t)
Definition ugbc.h:618
#define CRITICAL_NOT_ARRAY(v)
Definition ugbc.h:3480
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
@ LT_FOR
Definition ugbc.h:1396
#define CRITICAL_NEXT_WITHOUT_FOR()
Definition ugbc.h:3594
struct _Loop Loop
Structure of a single loop.
void yield(Environment *_environment)
Emit code for YIELD.
Definition yield.c:63