ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
begin_for_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
49void begin_for_step( Environment * _environment, char * _index, char * _from, char * _to, char * _step ) {
50
51 Variable * index = NULL;
52
53 if ( variable_exists( _environment, _index ) ) {
54 index = variable_retrieve( _environment, _index );
55 } else {
56 index = variable_retrieve_or_define( _environment, _index, VT_SWORD, 0 );
57 }
58
59 outline1("; index = %s", index->realName );
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->type, "(resident to)" );
66 variable_move_naked( _environment, to->name, toResident->name );
67
68 Variable * stepResident = variable_resident( _environment, index->type, "(resident step)" );
69 variable_move( _environment, step->name, stepResident->name );
70
71 Variable * zero = variable_resident( _environment, VT_WORD, "(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;
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 variable_move( _environment, from->name, index->name );
92
93 unsigned char beginFor[MAX_TEMPORARY_STORAGE]; sprintf(beginFor, "%sbf", loop->label );
94 unsigned char endFor[MAX_TEMPORARY_STORAGE]; sprintf(endFor, "%sbis", loop->label );
95 unsigned char forwardFor[MAX_TEMPORARY_STORAGE]; sprintf(forwardFor, "%sfor", loop->label );
96 unsigned char backwardFor[MAX_TEMPORARY_STORAGE]; sprintf(backwardFor, "%sback", loop->label );
97 unsigned char continueFor[MAX_TEMPORARY_STORAGE]; sprintf(continueFor, "%scont", loop->label );
98
99 cpu_label( _environment, beginFor );
100
101 cpu_bvneq( _environment, variable_greater_than( _environment, loop->step->name, zero->name, 0)->realName, forwardFor );
102
103 cpu_jump( _environment, backwardFor );
104
105 cpu_label( _environment, forwardFor );
106
107 cpu_bvneq( _environment, variable_greater_than( _environment, index->name, loop->to->name, 0 )->realName, endFor );
108
109 cpu_jump( _environment, continueFor );
110
111 cpu_label( _environment, backwardFor );
112
113 cpu_bvneq( _environment, variable_less_than( _environment, index->name, loop->to->name, 1 )->realName, endFor );
114
115 cpu_label( _environment, continueFor );
116
117}
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_retrieve(Environment *_environment, char *_name)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _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_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_resident(Environment *_environment, VariableType _type, char *_meaning)
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
void begin_for_step(Environment *_environment, char *_index, char *_from, char *_to, char *_step)
Emit ASM code for FOR ... STEP ... NEXT.
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)
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_WORD
Definition ugbc.h:455
@ VT_SWORD
Definition ugbc.h:457
@ LT_FOR
Definition ugbc.h:1396
struct _Loop Loop
Structure of a single loop.
#define outline1(s, a)
Definition ugbc.h:4253
#define MAKE_LABEL
Definition ugbc.h:3351