ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
_cleanups.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
50void gameloop_cleanup( Environment * _environment ) {
51
52 if ( _environment->hasGameLoop ) {
53 end_gameloop( _environment );
54 }
55
56}
57
58void end_compilation( Environment * _environment ) {
59
60 gameloop_cleanup( _environment );
61
62 if ( _environment->anyProtothread ) {
63 begin_do_loop( _environment );
64 run_parallel( _environment );
65 end_do_loop( _environment );
66 }
67
68 if ( _environment->finalReturn ) {
69 cpu_return( _environment );
70 } else {
71 halt( _environment );
72 }
73
74 Label * first = _environment->referredLabels;
75 while( first ) {
76 if ( first->name ) {
77 if ( !label_exists_named( _environment, first->name ) ) {
79 }
80 } else {
81 if ( !label_exists_numeric( _environment, first->number ) ) {
83 }
84 }
85 first = first->next;
86 }
87
88 if ( _environment->procedureName ) {
90 }
91
92 if ( _environment->conditionals ) {
93
94 switch( _environment->conditionals->type ) {
95 case CT_IF:
97 case CT_SELECT_CASE:
99 }
100
101 }
102
103 if ( _environment->loops ) {
104
105 switch( _environment->loops->type ) {
106 case LT_DO:
108 case LT_WHILE:
110 case LT_REPEAT:
112 case LT_FOR:
113 case LT_FOR_MT:
115 case LT_GAMELOOP:
117 }
118
119 }
120
121 if ( _environment->readDataUsed ) {
122 if ( !_environment->dataSegment ) {
124 }
125 }
126
127 finalize_text_variables( _environment );
128
129 int j=0;
130 for( j=0; j<MAX_TEMPORARY_STORAGE; ++j ) {
131 if ( _environment->deferredEmbedded[j] ) {
132 int size = _environment->deferredEmbeddedSize[j];
133 char * parsed = malloc( (8*size) + 1 );
134 memset( parsed, 0, (8*size) + 1 );
135 char * line = strtok( _environment->deferredEmbedded[j], "\x0a" );
136 while( line ) {
137 _environment->embedResult.line = line;
138 _environment->embedResult.conditional = 0;
139 _environment->embedResult.lineCount = 0;
140 embed_scan_string( line );
141 embedparse(_environment);
142 if ( ! _environment->embedResult.conditional ) {
143 int i;
144 for( i=0; i<_environment->embedResult.current; ++i ) {
145 if ( _environment->embedResult.excluded[i] )
146 break;
147 }
148 if ( i>= _environment->embedResult.current ) {
149 if ( _environment->embedResult.lineCount ) {
150 int j=0;
151 for( j=0; j<_environment->embedResult.lineCount; ++j ) {
152 strcat( parsed, _environment->embedResult.lines[j] );
153 strcat( parsed, "\x0a" );
154 ((Environment *)_environment)->producedAssemblyLines += assemblyLineIsAComment( _environment->embedResult.lines[j] ) ? 0 : 1;
155 }
156 } else {
157 strcat( parsed, line );
158 strcat( parsed, "\x0a" );
159 ((Environment *)_environment)->producedAssemblyLines += assemblyLineIsAComment( line ) ? 0 : 1;
160 }
161 }
162 }
163 line = strtok( NULL, "\x0a" );
164 }
165 free( _environment->deferredEmbedded[j] );
166 buffered_fwrite( _environment, parsed, strlen( parsed )-1, 1, ((Environment *)_environment)->asmFile );
167 free( parsed );
168 buffered_fputs( _environment,"\n", ((Environment *)_environment)->asmFile );
169 }
170 }
171
172 target_prepare_finalization( _environment );
173
174 bank_cleanup( _environment );
175 every_cleanup( _environment );
176 variable_cleanup( _environment );
177 dstring_cleanup( _environment );
178
179 target_finalization( _environment );
180
181 if ( _environment->configurationFileName ) {
182 linker_setup( _environment );
183 linker_cleanup( _environment );
184 fclose(_environment->configurationFile);
185 }
186
187 if ( _environment->debuggerLabelsFile ) {
188 fclose(_environment->debuggerLabelsFile);
189 }
190
191 buffered_output( _environment, _environment->asmFile );
192
193 fclose(_environment->asmFile);
194
195}
196
void cpu_return(Environment *_environment)
Definition 6309.c:4030
void gameloop_cleanup(Environment *_environment)
Emit source and configuration lines for game loops.
Definition _cleanups.c:50
void end_compilation(Environment *_environment)
Definition _cleanups.c:58
void dstring_cleanup(Environment *_environment)
Definition _dstring.c:41
int assemblyLineIsAComment(char *_buffer)
size_t buffered_fwrite(Environment *_environment, void *_data, size_t _size, size_t _count, FILE *_stream)
int buffered_fputs(Environment *_environment, const char *_string, FILE *_stream)
int label_exists_named(Environment *_environment, char *_label)
int label_exists_numeric(Environment *_environment, int _label)
void buffered_output(Environment *_environment, FILE *_stream)
void target_finalization(Environment *_environment)
Definition _cleanup.c:45
void target_prepare_finalization(Environment *_environment)
Definition _cleanup.c:41
void every_cleanup(Environment *_environment)
Definition _every.c:41
void linker_setup(Environment *_environment)
Emit tail of linker's configuration file lines.
Definition _linker.c:48
void linker_cleanup(Environment *_environment)
Emit tail of linker's configuration file lines.
Definition _linker.c:133
int size
Definition _optimizer.c:678
void variable_cleanup(Environment *_environment)
Emit source and configuration lines for variables.
Definition _var.c:559
void bank_cleanup(Environment *_environment)
Emit source and configuration lines for banks.
Definition bank.c:52
void end_gameloop(Environment *_environment)
Emit ASM code for END GAMELOOP.
void begin_do_loop(Environment *_environment)
Emit ASM code for DO ....
void finalize_text_variables(Environment *_environment)
Definition _init.c:169
void end_do_loop(Environment *_environment)
Emit ASM code for ... LOOP.
Definition end_do_loop.c:49
void halt(Environment *_environment)
Emit ASM code for HALT.
Definition halt.c:74
void run_parallel(Environment *_environment)
Emit code for RUN PARALLEL.
ConditionalType type
Definition ugbc.h:1363
int conditional
Definition ugbc.h:2121
char * line
Definition ugbc.h:2118
int current
Definition ugbc.h:2119
char * lines[MAX_TEMPORARY_STORAGE]
Definition ugbc.h:2126
int excluded[MAX_NESTED_ARRAYS]
Definition ugbc.h:2120
int lineCount
Definition ugbc.h:2127
Conditional * conditionals
Definition ugbc.h:2664
int deferredEmbeddedSize[MAX_TEMPORARY_STORAGE]
Definition ugbc.h:3095
int anyProtothread
Definition ugbc.h:2835
FILE * configurationFile
Definition ugbc.h:3314
FILE * asmFile
Definition ugbc.h:3309
char * configurationFileName
Definition ugbc.h:2295
Label * referredLabels
Definition ugbc.h:2550
DataSegment * dataSegment
Definition ugbc.h:2568
FILE * debuggerLabelsFile
Definition ugbc.h:3319
int hasGameLoop
Definition ugbc.h:2646
int finalReturn
Definition ugbc.h:2462
char * procedureName
Definition ugbc.h:2775
char * deferredEmbedded[MAX_TEMPORARY_STORAGE]
Definition ugbc.h:3093
EmbedResult embedResult
Definition ugbc.h:2447
Loop * loops
Definition ugbc.h:2669
int readDataUsed
Definition ugbc.h:2578
struct _Label * next
Definition ugbc.h:848
char * name
Definition ugbc.h:842
int number
Definition ugbc.h:845
LoopType type
Definition ugbc.h:1420
void * malloc(YYSIZE_T)
void free(void *)
int embedparse(void *_environment)
#define CRITICAL_MISSING_LABEL_NAMED(v)
Definition ugbc.h:3734
int embed_scan_string(const char *)
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define CRITICAL_FOR_WITHOUT_NEXT()
Definition ugbc.h:3671
#define CRITICAL_MISSING_LABEL_NUMBER(v)
Definition ugbc.h:3735
@ CT_IF
Definition ugbc.h:1336
@ CT_SELECT_CASE
Definition ugbc.h:1348
#define CRITICAL_READ_WITHOUT_DATA(v)
Definition ugbc.h:3677
struct _Environment Environment
Structure of compilation environment.
@ LT_GAMELOOP
Definition ugbc.h:1402
@ LT_REPEAT
Definition ugbc.h:1393
@ LT_WHILE
Definition ugbc.h:1390
@ LT_FOR_MT
Definition ugbc.h:1399
@ LT_DO
Definition ugbc.h:1387
@ LT_FOR
Definition ugbc.h:1396
#define CRITICAL_IF_WITHOUT_ENDIF()
Definition ugbc.h:3666
#define CRITICAL_DO_WITHOUT_LOOP()
Definition ugbc.h:3668
#define CRITICAL_REPEAT_WITHOUT_UNTIL()
Definition ugbc.h:3670
#define CRITICAL_SELECT_CASE_WITHOUT_ENDSELECT()
Definition ugbc.h:3667
#define CRITICAL_BEGIN_GAMELOOP_WITHOUT_END_GAMELOOP()
Definition ugbc.h:3672
struct _Label Label
Structure of a single label.
#define CRITICAL_WHILE_WITHOUT_WEND()
Definition ugbc.h:3669
#define CRITICAL_MISSING_END_PROC(p)
Definition ugbc.h:3799