ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
declare_procedure.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
47/* <usermanual>
48@keyword DECLARE PROCEDURE
49
50@english
51
52The ''DECLARE PROCEDURE'' is a way to define and call external procedures written in machine language
53and to insert assembly into the sources. The procedures defined in this way can be called as if they
54were an integral part of the language. Once the procedure has been declared, it will be sufficient
55to invoke it with one of the following syntaxes:
56
57 '''CALL test'''
58 '''PROC test'''
59 '''test[]'''
60
61as if it were any ugBASIC procedure. If desired, parameters can be added. For each it is obviously
62necessary to indicate how the value will be passed to the function written in machine language.
63
64The ugBASIC compiler allows you to declare functions and procedures that are “system”.
65It means that the machine code resides in a ROM, preloaded at run time and therefore
66already made available to any program that knows how to call it.
67
68However, since ugBASIC makes available all memory space allowed by the hardware, it is possible that
69the ROMs have been disabled or otherwise made unreachable. Indicating that you want to call a procedure
70or a system function, ugBASIC will take care of re-enabling the ROM before executing the request,
71deactivating it on exit.
72
73To declare a procedure or function to be system, simply use the ''SYSTEM'' keyword.
74
75@italian
76
77Il ''DECLARE PROCEDURE'' è un modo per definire e chiamare procedure esterne scritte in linguaggio macchina
78e per inserire assembly nei sorgenti. Le procedure così definite possono essere richiamate come se
79fossero parte integrante del linguaggio. Una volta dichiarata la procedura sarà sufficiente invocarla
80con una delle seguenti sintassi:
81
82 '''CALL test'''
83 '''PROC test'''
84 '''test[]'''
85
86come se fosse una procedura ugBASIC. Se lo si desidera, è possibile aggiungere parametri. Per ognuno
87è ovviamente necessario indicare come verrà passato il valore alla funzione scritta in linguaggio macchina.
88
89Il compilatore ugBASIC consente di dichiarare funzioni e procedure che sono “di sistema”.
90Vuol dire che il codice macchina risiede in una ROM, precaricata in fase di esecuzione e
91quindi già resa disponibile a qualsiasi programma che sappia chiamarla.
92
93Tuttavia, poiché ugBASIC mette a disposizione tutto lo spazio di memoria consentito dall'hardware, è
94possibile che le ROM siano state disabilitate o comunque rese irraggiungibili. Indicando che si vuole
95chiamare una procedura o una funzione di sistema, ugBASIC si occuperà di riabilitare la ROM prima di
96eseguire la richiesta, disattivandola all'uscita.
97
98Per dichiarare una procedura o una funzione come sistema, utilizzare semplicemente la parola chiave ''SYSTEM''.
99
100@syntax DECLARE [SYSTEM] PROCEDURE name AT address [ ( par [, par [, ... ] ] ) ] [ ON targets ]
101@syntax par : name AS type ON register
102@syntax par : name AS type ON STACK(width)
103@syntax width: BYTE | WORD | DWORD
104@syntax targets : name of targets, separated by comma (,)
105
106@example DECLARE SYSTEM PROCEDURE test ON CPC, ZX
107
108@usedInExample extern_example_11.bas
109
110@alias DECLARE PROC
111@seeAlso DECLARE FUNCTION
112
113@target all
114</usermanual> */
115
116/* <usermanual>
117@keyword DECLARE PROC
118
119@english
120
121@italian
122
123@syntax DECLARE [SYSTEM] PROC name AT address [ ( par [, par [, ... ] ] ) ] [ ON targets ]
124@syntax par : name AS type ON register
125@syntax par : name AS type ON STACK(width)
126@syntax width: BYTE | WORD | DWORD
127@syntax targets : name of targets, separated by comma (,)
128
129@example DECLARE SYSTEM PROC test ON CPC, ZX
130
131@usedInExample extern_example_11.bas
132
133@alias DECLARE PROCEDURE
134@seeAlso DECLARE FUNCTION
135
136@target all
137</usermanual> */
138
139/* <usermanual>
140@keyword DECLARE FUNCTION
141
142@english
143
144The ''DECLARE FUNCTION'' is a way to define and call external procedures written in machine language
145and to insert assembly into the sources. The procedures defined in this way can be called as if they
146were an integral part of the language, and any result value can be retrieved by calling it
147as a function:
148
149 '''value = test[]'''
150
151@italian
152
153La ''DECLARE FUNCTION'' è un modo per definire e chiamare procedure esterne scritte in linguaggio
154macchina e per inserire assembly nelle sorgenti. Le procedure definite in questo modo possono essere
155chiamate come se fossero parte integrante del linguaggio e qualsiasi valore di risultato può essere
156recuperato chiamandolo come una funzione:
157
158'''value = test[]'''
159
160@syntax DECLARE [SYSTEM] FUNCTION name AT address [ ( par1 [, par2 [, ... ] ] ) ] RETURN ret [ ON targets ]
161@syntax par : name AS type ON register
162@syntax par : name AS type ON STACK(width)
163@syntax ret : register AS type | STACK(width) AS type
164@syntax width: BYTE | WORD | DWORD
165@syntax targets : name of targets, separated by comma (,)
166
167@example DECLARE SYSTEM FUNCTION test ON CPC, ZX
168
169@usedInExample extern_example_11.bas
170
171@seeAlso DECLARE PROCEDURE
172
173@target all
174</usermanual> */
175void declare_procedure( Environment * _environment, char * _name, int _address, int _system ) {
176
177 if ( _environment->emptyProcedure ) {
178 return;
179 }
180
181 if ( _environment->procedureName ) {
183 }
184
185 Procedure * procedure = malloc( sizeof( Procedure ) );
186 memset( procedure, 0, sizeof( Procedure ) );
187 procedure->name = strdup( _name );
188 procedure->realName = strdup( _name );
189 procedure->declared = 1;
190 procedure->address = _address;
191 procedure->system = _system;
192
193 int i = 0;
194 procedure->parameters = _environment->parameters;
195 for( i=0; i<_environment->parameters; ++i ) {
196 procedure->parametersEach[i] = _environment->parametersEach[i];
197 procedure->parametersAsmioEach[i] = _environment->parametersAsmioEach[i];
198 procedure->parametersTypeEach[i] = _environment->parametersTypeEach[i];
199 }
200
201 procedure->returns = _environment->returns;
202 for( i=0; i<_environment->returns; ++i ) {
203 procedure->returnsEach[i] = _environment->returnsEach[i];
204 procedure->returnsAsmioEach[i] = _environment->returnsAsmioEach[i];
205 procedure->returnsTypeEach[i] = _environment->returnsTypeEach[i];
206 }
207
208 procedure->next = _environment->procedures;
209
210 _environment->procedures = procedure;
211
212}
void declare_procedure(Environment *_environment, char *_name, int _address, int _system)
Emit code for DECLARE PROC ....
char * parametersEach[MAX_PARAMETERS]
Definition ugbc.h:2790
int returnsAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:2820
VariableType parametersTypeEach[MAX_PARAMETERS]
Definition ugbc.h:2800
int parameters
Definition ugbc.h:2785
int returns
Definition ugbc.h:2810
char * procedureName
Definition ugbc.h:2775
VariableType returnsTypeEach[MAX_PARAMETERS]
Definition ugbc.h:2825
Procedure * procedures
Definition ugbc.h:2621
int emptyProcedure
Definition ugbc.h:2932
int parametersAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:2795
char * returnsEach[MAX_PARAMETERS]
Definition ugbc.h:2815
char * parametersEach[MAX_PARAMETERS]
Definition ugbc.h:1269
int system
Definition ugbc.h:1299
struct _Procedure * next
Definition ugbc.h:1327
int address
Definition ugbc.h:1304
char * name
Definition ugbc.h:1256
int returnsAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:1319
VariableType parametersTypeEach[MAX_PARAMETERS]
Definition ugbc.h:1284
int parameters
Definition ugbc.h:1264
int returns
Definition ugbc.h:1309
VariableType returnsTypeEach[MAX_PARAMETERS]
Definition ugbc.h:1324
int declared
Definition ugbc.h:1294
int parametersAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:1274
char * returnsEach[MAX_PARAMETERS]
Definition ugbc.h:1314
char * realName
Definition ugbc.h:1259
void * malloc(YYSIZE_T)
struct _Environment Environment
Structure of compilation environment.
#define CRITICAL_DECLARE_PROC_NESTED_UNSUPPORTED(v)
Definition ugbc.h:3627
struct _Procedure Procedure