-
Notifications
You must be signed in to change notification settings - Fork 5
/
vars.c
executable file
·336 lines (311 loc) · 9.35 KB
/
vars.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/***************************************************************************
* NESHLA: The Nintendo Entertainment System High Level Assembler
* Copyright (C) 2003,2004,2005 Brian Provinciano, http://www.bripro.com
*
* This program is free software.
* You may use this code for anything you wish.
* It comes with no warranty.
***************************************************************************/
/******************************************************************************/
#pragma hdrstop
#include "compiler.h"
#include "functions.h"
/******************************************************************************
* Handles variables
******************************************************************************/
#pragma package(smart_init)
/******************************************************************************/
VAR *vars,*curVar,*typedefs;
/******************************************************************************/
VARCAST varcasts[] = {
{"",
VARCAST_NULL, VARSIZE_NULL},
{"byte",
VARCAST_BYTE, VARSIZE_BYTE},
{"char",
VARCAST_CHAR, VARSIZE_BYTE},
{"bool",
VARCAST_BOOL, VARSIZE_BYTE},
{"word",
VARCAST_WORD, VARSIZE_WORD},
{"pointer",
VARCAST_POINTER,VARSIZE_WORD},
{"struct",
VARCAST_STRUCT, VARSIZE_NULL},
};
/******************************************************************************/
void FreeVars(VAR **pvar)
{
VAR *var=*pvar,*next;
if(pvar) {
while(var) {
next = var->prev;
FreeVars(&var->childVars);
ssFree(var->label);
ssFree(var);
var = next;
}
*pvar = NULL;
}
}
/******************************************************************************/
VAR *AddVariable(char *label, U16 cast, VAR *castvar, U16 flags, S32 offset)
{
VAR *newvar;
if(IsLabelUsed(label,curVar?curVar->childVars:vars))
label[0]='\0';
newvar =
(castvar)?
(VAR*)CloneVar(castvar,curVar,flags):
(VAR*)ssCalloc(sizeof(VAR));
if(curVar) { // in a struct
if(curVar->childVars)
curVar->childVars->next = newvar;
newvar->prev = curVar->childVars;
curVar->childVars = newvar;
} else {
if(flags&VARFLAG_TYPECAST) {
if(typedefs)
typedefs->next = newvar;
newvar->prev = typedefs;
typedefs = newvar;
} else {
if(vars)
vars->next = newvar;
newvar->prev = vars;
vars = newvar;
}
}
newvar->parent = curVar;
newvar->next = NULL;
if(!castvar) {
newvar->childVars = NULL;
newvar->size = varcasts[cast].size;
newvar->arraySize = 0;
newvar->cast = cast;
newvar->flags = flags;
if(newvar->size==VARSIZE_WORD)
newvar->flags |= VARFLAG_16BIT;
} else {
newvar->flags |= VARFLAG_DEFCASTED;
}
newvar->label = strdup(label);
//if(newvar->parent)
// newvar->flags |= newvar->parent->flags&(VARFLAG_TYPECAST);
newvar->bank = curBank;
if(!(flags&VARFLAG_TYPECAST)) {
if(offset==-1) {
if(!curBank) {
error(ERR_VARNOBANK);
newvar->offset = 0;
} else {
newvar->offset = -1;
switch(curBank->type) {
case BANKTYPE_RAM:
break;
case BANKTYPE_ROM:
newvar->flags |= VARFLAG_ROM;
break;
case BANKTYPE_CHR:
newvar->flags |= VARFLAG_ROM;
break;
}
}
} else {
SetVarOffsets(newvar,offset,FALSE);
newvar->offset = offset;
}
} else {
newvar->flags |= VARFLAG_TYPECAST;
if(offset==-1)
newvar->offset = 0;//SetVarOffsets(newvar,0,FALSE);
else
newvar->offset = offset;
}
return newvar;
}
/******************************************************************************/
void SetVarOffsets(VAR *var, S32 offset, BOOL SCAN)
{ /*
if(!SCAN && var)
offset += VarSize(var);
while(var) {
SetVarOffsets(var->childVars, offset, TRUE);
var->offset = (offset-=VarSize(var));
if(!SCAN) break;
var = var->prev;
} */
if(SCAN && var)
var = FindFirstVariable(var);
while(var) {
var->offset = offset;
SetVarOffsets(var->childVars, offset, TRUE);
offset+=VarSize(var);
if(!SCAN) break;
var = var->next;
}
}
/******************************************************************************/
VAR *CloneVar(VAR *var, VAR *parent,U16 flags)
{
VAR *prev=NULL,*start=NULL,*next;
while(var) {
var = memclone(var,sizeof(VAR));
if(!start) start = var;
next = var->prev;
var->childVars = CloneVar(var->childVars, var, flags);
var->parent = parent;
var->flags &= ~(VARFLAG_TYPECAST);
var->flags |= flags;
var->prev = NULL;
var->next = prev;
var->label = strdup(var->label);
if(prev)
prev->prev = var;
prev = var;
if(!parent)
break;
var = next;
}
return start;
}
/******************************************************************************/
void SetCurVar(VAR* var)
{
curVar = var;
}
/******************************************************************************/
BOOL CheckoutCurVar()
{
if(curVar) {
error(ERR_DECNOTINSTRUCT);
return TRUE;
}
return FALSE;
}
/******************************************************************************/
VAR* ReleaseCurVar()
{
if(curVar)
curVar = curVar->parent;
return curVar;
}
/******************************************************************************/
VAR* FindVariable(VAR *var, char *label)
{
while(var && STRCMP(var->label,label))
var = var->prev;
return var;
}
/******************************************************************************/
VAR *FindFirstVariable(VAR *var)
{
while(var && var->prev)
var = var->prev;
return var;
}
/******************************************************************************/
int IsVarCast(char *label,VAR **castvar)
{
int i;
S16 type;
S32 offset;
for(i=1;i<VARCAST_TOTAL;i++)
if(!STRCMP(varcasts[i].label,label))
return i;
if(castvar && (*castvar=FindVariable(typedefs, label))!=NULL) {
return (*castvar)->cast;
}
return 0;
}
/******************************************************************************/
S32 VarSize(VAR *var)
{
return ( var->arraySize? var->arraySize*var->size : var->size );
}
/******************************************************************************/
S32 PonderArray(VAR *var)
{
S32 offset=0,num;
PeekNextWord();
if(szTemp[0]=='[') {
if(!var->arraySize)
error(ERR_VARNOTARRAY,var->label);
GetNextWord();
if(!CompileImmediateInteger(0, &num, -1, 0)) {
error(ERR_INVALIDARRAYINDEX,var->label);
} else {
if(num<0||num>=var->arraySize) {
warning(WRN_ARRAYINDEXOUTOFBOUNDS,var->label,num,var->arraySize-1);
}
offset += (num*var->size);
}
if(GetNextWord()[0]!=']') {
error(ERR_ARRAYENDEXP,szTemp);
}
PeekNextWord();
}
return offset;
}
/******************************************************************************/
VAR *PonderVariable(VAR *var, S32 *_offset)
{
VAR *newvar;
FUNC *func;
S32 offset,n;
n = var->offset;
//if(var->offset==-1)
offset = 0; /*
else {
offset = var->offset;
if(!STRCMP(var->label,"title_text_writemap"))
offset=offset; */
offset += PonderArray(var);
while(var->childVars && szTemp[0]=='.') {
GetNextWord();
USE_DEFS = FALSE;
if( (newvar=FindVariable(var->childVars, GetNextWord())) == NULL ) {
error(ERR_NOTMEMBEROFSTRUCT, szTemp,var->label);
USE_DEFS = TRUE;
return NULL;
}
USE_DEFS = TRUE;
var = newvar;
offset += PonderArray(var);
} /*
} */
*_offset = n==-1?n:offset+var->offset;
// to catch vars shared between funcs/interrupts
if( cfg.msg.warning.level >= 2 &&
curFunction &&
!(var->flags&(VARFLAG_FIXED|VARFLAG_SHARED))// &&
//(activeOpcode == opcodeSta || activeOpcode == opcodeStx || activeOpcode == opcodeSty)
) {
switch((func=FindFirstCurFunc())->type) {
case FUNCTYPE_INTERRUPT:
if(FUNCFLAG_GETTYPE(func->flags) != INTTYPE_START)
var->flags |= VARFLAG_INTCALLED;
break;
case FUNCTYPE_FUNCTION:
var->flags |= VARFLAG_FUNCCALLED;
break;
}
}
return var;
}
/******************************************************************************/
void CheckVariableCalls()
{
VAR *var = vars;
while(var && var->prev) {
if((var->flags &
(VARFLAG_FUNCCALLED|VARFLAG_INTCALLED|VARFLAG_FIXED))
== (VARFLAG_FUNCCALLED|VARFLAG_INTCALLED)
) {
warning(WRN_VARINTFUNC,
var->label);
}
var = var->prev;
}
}
/******************************************************************************/