ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
begin_for_mt_step.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
49/* <usermanual>
50@keyword FOR...NEXT
51@target all
52</usermanual> */
53void begin_for_mt_step( Environment * _environment, char * _index, char * _from, char * _to, char * _step ) {
54
55 Variable * index = variable_retrieve( _environment, _index );
56
57 if ( index->type != VT_TARRAY ) {
58 CRITICAL_NOT_ARRAY( index->name );
59 }
60
61 Variable * from = variable_retrieve( _environment, _from );
62 Variable * to = variable_retrieve( _environment, _to );
63 Variable * step = variable_retrieve( _environment, _step );
64
65 Variable * toResident = variable_resident( _environment, to->arrayType, "(resident to)" );
66 variable_move_naked( _environment, to->name, toResident->name );
67
68 Variable * stepResident = variable_resident( _environment, index->arrayType, "(resident step)" );
69 variable_move( _environment, step->name, stepResident->name );
70
71 Variable * zero = variable_resident( _environment, index->arrayType, "(zero)" );
72 variable_store( _environment, zero->name, 0 );
73
75
76 Loop * loop = malloc( sizeof( Loop ) );
77 memset( loop, 0, sizeof( Loop ) );
78 loop->label = strdup( label );
79 loop->type = LT_FOR_MT;
80 loop->next = _environment->loops;
81 loop->index = index;
82 loop->index->locked = 1;
83 loop->step = stepResident;
84 loop->step->locked = 1;
85 loop->to = toResident;
86 loop->to->locked = 1;
87 loop->zero = zero;
88 loop->zero->locked = 1;
89 _environment->loops = loop;
90
91 parser_array_init( _environment );
92 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
93 Variable * array = variable_retrieve( _environment, index->name );
94 if ( array->type != VT_TARRAY ) {
95 CRITICAL_NOT_ARRAY( index->name );
96 }
97 variable_move_array( _environment, index->name, from->name );
98 parser_array_cleanup( _environment );
99
100 unsigned char beginFor[MAX_TEMPORARY_STORAGE]; sprintf(beginFor, "%sbf", loop->label );
101 unsigned char endFor[MAX_TEMPORARY_STORAGE]; sprintf(endFor, "%sbis", loop->label );
102 unsigned char forwardFor[MAX_TEMPORARY_STORAGE]; sprintf(forwardFor, "%sfor", loop->label );
103 unsigned char backwardFor[MAX_TEMPORARY_STORAGE]; sprintf(backwardFor, "%sback", loop->label );
104 unsigned char continueFor[MAX_TEMPORARY_STORAGE]; sprintf(continueFor, "%scont", loop->label );
105
106 cpu_label( _environment, beginFor );
107
108 parser_array_init( _environment );
109 parser_array_index_symbolic( _environment, "PROTOTHREADCT" );
110 array = variable_retrieve_or_define( _environment, index->name, VT_TARRAY, 0 );
111 if ( array->type != VT_TARRAY ) {
112 CRITICAL_NOT_ARRAY( index->name );
113 }
114 Variable * value = variable_move_from_array( _environment, index->name );
115 parser_array_cleanup( _environment );
116
117 cpu_bvneq( _environment, variable_greater_than( _environment, loop->step->name, zero->name, 0)->realName, forwardFor );
118
119 cpu_jump( _environment, backwardFor );
120
121 cpu_label( _environment, forwardFor );
122
123 cpu_bvneq( _environment, variable_greater_than( _environment, value->name, variable_add( _environment, loop->to->name, loop->step->name )->name, 0 )->realName, endFor );
124
125 cpu_jump( _environment, continueFor );
126
127 cpu_label( _environment, backwardFor );
128
129 cpu_bvneq( _environment, variable_less_than( _environment, value->name, variable_add( _environment, loop->to->name, loop->step->name )->name, 1 )->realName, endFor );
130
131 cpu_label( _environment, continueFor );
132
133}
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
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_add(Environment *_environment, char *_source, char *_destination)
Add two variable and return the sum of them.
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)
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_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_move_naked(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable without conversion.
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_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
void begin_for_mt_step(Environment *_environment, char *_index, char *_from, char *_to, char *_step)
Emit ASM code for FOR ... STEP ... NEXT (in thread).
Loop * loops
Definition ugbc.h:2669
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
VariableType arrayType
Definition ugbc.h:1125
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 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
struct _Loop Loop
Structure of a single loop.
#define MAKE_LABEL
Definition ugbc.h:3351