forked from zsaleeba/picoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.c
477 lines (394 loc) · 17.7 KB
/
variable.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* picoc variable storage. This provides ways of defining and accessing
* variables */
#include "interpreter.h"
/* maximum size of a value to temporarily copy while we create a variable */
#define MAX_TMP_COPY_BUF 256
/* initialise the variable system */
void VariableInit(Picoc *pc)
{
TableInitTable(&(pc->GlobalTable), &(pc->GlobalHashTable)[0], GLOBAL_TABLE_SIZE, TRUE);
TableInitTable(&pc->StringLiteralTable, &pc->StringLiteralHashTable[0], STRING_LITERAL_TABLE_SIZE, TRUE);
pc->TopStackFrame = NULL;
}
/* deallocate the contents of a variable */
void VariableFree(Picoc *pc, struct Value *Val)
{
if (Val->ValOnHeap || Val->AnyValOnHeap)
{
/* free function bodies */
if (Val->Typ == &pc->FunctionType && Val->Val->FuncDef.Intrinsic == NULL && Val->Val->FuncDef.Body.Pos != NULL)
HeapFreeMem(pc, (void *)Val->Val->FuncDef.Body.Pos);
/* free macro bodies */
if (Val->Typ == &pc->MacroType)
HeapFreeMem(pc, (void *)Val->Val->MacroDef.Body.Pos);
/* free the AnyValue */
if (Val->AnyValOnHeap)
HeapFreeMem(pc, Val->Val);
}
/* free the value */
if (Val->ValOnHeap)
HeapFreeMem(pc, Val);
}
/* deallocate the global table and the string literal table */
void VariableTableCleanup(Picoc *pc, struct Table *HashTable)
{
struct TableEntry *Entry;
struct TableEntry *NextEntry;
int Count;
for (Count = 0; Count < HashTable->Size; Count++)
{
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry)
{
NextEntry = Entry->Next;
VariableFree(pc, Entry->p.v.Val);
/* free the hash table entry */
HeapFreeMem(pc, Entry);
}
}
}
void VariableCleanup(Picoc *pc)
{
VariableTableCleanup(pc, &pc->GlobalTable);
VariableTableCleanup(pc, &pc->StringLiteralTable);
}
/* allocate some memory, either on the heap or the stack and check if we've run out */
void *VariableAlloc(Picoc *pc, struct ParseState *Parser, int Size, int OnHeap)
{
void *NewValue;
if (OnHeap)
NewValue = HeapCallocMem(pc, Size);
else
NewValue = HeapAllocStack(pc, Size);
if (NewValue == NULL)
ProgramFail(Parser, "out of memory");
#ifdef DEBUG_HEAP
if (!OnHeap)
printf("pushing %d at 0x%lx\n", Size, (unsigned long)NewValue);
#endif
return NewValue;
}
/* allocate a value either on the heap or the stack using space dependent on what type we want */
struct Value *VariableAllocValueAndData(Picoc *pc, struct ParseState *Parser, int DataSize, int IsLValue, struct Value *LValueFrom, int OnHeap)
{
struct Value *NewValue = (struct Value *) VariableAlloc(pc, Parser, MEM_ALIGN(sizeof(struct Value)) + DataSize, OnHeap);
NewValue->Val = (union AnyValue *)((char *)NewValue + MEM_ALIGN(sizeof(struct Value)));
NewValue->ValOnHeap = (char) OnHeap;
NewValue->AnyValOnHeap = FALSE;
NewValue->ValOnStack = (char) !OnHeap;
NewValue->IsLValue = (char) IsLValue;
NewValue->LValueFrom = LValueFrom;
if (Parser)
NewValue->ScopeID = Parser->ScopeID;
NewValue->OutOfScope = 0;
return NewValue;
}
/* allocate a value given its type */
struct Value *VariableAllocValueFromType(Picoc *pc, struct ParseState *Parser, struct ValueType *Typ, int IsLValue, struct Value *LValueFrom, int OnHeap)
{
int Size = TypeSize(Typ, Typ->ArraySize, FALSE);
struct Value *NewValue = VariableAllocValueAndData(pc, Parser, Size, IsLValue, LValueFrom, OnHeap);
assert(Size >= 0 || Typ == &pc->VoidType);
NewValue->Typ = Typ;
return NewValue;
}
/* allocate a value either on the heap or the stack and copy its value. handles overlapping data */
struct Value *VariableAllocValueAndCopy(Picoc *pc, struct ParseState *Parser, struct Value *FromValue, int OnHeap)
{
struct ValueType *DType = FromValue->Typ;
struct Value *NewValue;
char TmpBuf[MAX_TMP_COPY_BUF];
int CopySize = TypeSizeValue(FromValue, TRUE);
assert(CopySize <= MAX_TMP_COPY_BUF);
memcpy((void *)&TmpBuf[0], (void *)FromValue->Val, CopySize);
NewValue = VariableAllocValueAndData(pc, Parser, CopySize, FromValue->IsLValue, FromValue->LValueFrom, OnHeap);
NewValue->Typ = DType;
memcpy((void *)NewValue->Val, (void *)&TmpBuf[0], CopySize);
return NewValue;
}
/* allocate a value either on the heap or the stack from an existing AnyValue and type */
struct Value *VariableAllocValueFromExistingData(struct ParseState *Parser, struct ValueType *Typ, union AnyValue *FromValue, int IsLValue, struct Value *LValueFrom)
{
struct Value *NewValue = (struct Value *) VariableAlloc(Parser->pc, Parser, sizeof(struct Value), FALSE);
NewValue->Typ = Typ;
NewValue->Val = FromValue;
NewValue->ValOnHeap = FALSE;
NewValue->AnyValOnHeap = FALSE;
NewValue->ValOnStack = FALSE;
NewValue->IsLValue = (char) IsLValue;
NewValue->LValueFrom = LValueFrom;
return NewValue;
}
/* allocate a value either on the heap or the stack from an existing Value, sharing the value */
struct Value *VariableAllocValueShared(struct ParseState *Parser, struct Value *FromValue)
{
return VariableAllocValueFromExistingData(Parser, FromValue->Typ, FromValue->Val, FromValue->IsLValue, FromValue->IsLValue ? FromValue : NULL);
}
/* reallocate a variable so its data has a new size */
void VariableRealloc(struct ParseState *Parser, struct Value *FromValue, int NewSize)
{
if (FromValue->AnyValOnHeap)
HeapFreeMem(Parser->pc, FromValue->Val);
FromValue->Val = (union AnyValue *) VariableAlloc(Parser->pc, Parser, NewSize, TRUE);
FromValue->AnyValOnHeap = TRUE;
}
int VariableScopeBegin(struct ParseState * Parser, int* OldScopeID)
{
struct TableEntry *Entry;
struct TableEntry *NextEntry;
Picoc * pc = Parser->pc;
int Count;
#ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0;
#endif
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
if (Parser->ScopeID == -1) return -1;
/* XXX dumb hash, let's hope for no collisions... */
*OldScopeID = Parser->ScopeID;
Parser->ScopeID = (int)(intptr_t)(Parser->SourceText) * ((int)(intptr_t)(Parser->Pos) / sizeof(char*));
/* or maybe a more human-readable hash for debugging? */
/* Parser->ScopeID = Parser->Line * 0x10000 + Parser->CharacterPos; */
for (Count = 0; Count < HashTable->Size; Count++)
{
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry)
{
NextEntry = Entry->Next;
if (Entry->p.v.Val->ScopeID == Parser->ScopeID && Entry->p.v.Val->OutOfScope)
{
Entry->p.v.Val->OutOfScope = FALSE;
Entry->p.v.Key = (char*)((intptr_t)Entry->p.v.Key & ~1);
#ifdef VAR_SCOPE_DEBUG
if (!FirstPrint) { PRINT_SOURCE_POS; }
FirstPrint = 1;
printf(">>> back into scope: %s %x %d\n", Entry->p.v.Key, Entry->p.v.Val->ScopeID, Entry->p.v.Val->Val->Integer);
#endif
}
}
}
return Parser->ScopeID;
}
void VariableScopeEnd(struct ParseState * Parser, int ScopeID, int PrevScopeID)
{
struct TableEntry *Entry;
struct TableEntry *NextEntry;
Picoc * pc = Parser->pc;
int Count;
#ifdef VAR_SCOPE_DEBUG
int FirstPrint = 0;
#endif
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
if (ScopeID == -1) return;
for (Count = 0; Count < HashTable->Size; Count++)
{
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = NextEntry)
{
NextEntry = Entry->Next;
if (Entry->p.v.Val->ScopeID == ScopeID && !Entry->p.v.Val->OutOfScope)
{
#ifdef VAR_SCOPE_DEBUG
if (!FirstPrint) { PRINT_SOURCE_POS; }
FirstPrint = 1;
printf(">>> out of scope: %s %x %d\n", Entry->p.v.Key, Entry->p.v.Val->ScopeID, Entry->p.v.Val->Val->Integer);
#endif
Entry->p.v.Val->OutOfScope = TRUE;
Entry->p.v.Key = (char*)((intptr_t)Entry->p.v.Key | 1); /* alter the key so it won't be found by normal searches */
}
}
}
Parser->ScopeID = PrevScopeID;
}
int VariableDefinedAndOutOfScope(Picoc * pc, const char* Ident)
{
struct TableEntry *Entry;
int Count;
struct Table * HashTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
for (Count = 0; Count < HashTable->Size; Count++)
{
for (Entry = HashTable->HashTable[Count]; Entry != NULL; Entry = Entry->Next)
{
if (Entry->p.v.Val->OutOfScope && (char*)((intptr_t)Entry->p.v.Key & ~1) == Ident)
return TRUE;
}
}
return FALSE;
}
/* define a variable. Ident must be registered */
struct Value *VariableDefine(Picoc *pc, struct ParseState *Parser, char *Ident, struct Value *InitValue, struct ValueType *Typ, int MakeWritable)
{
struct Value * AssignValue;
struct Table * currentTable = (pc->TopStackFrame == NULL) ? &(pc->GlobalTable) : &(pc->TopStackFrame)->LocalTable;
int ScopeID = Parser ? Parser->ScopeID : -1;
#ifdef VAR_SCOPE_DEBUG
if (Parser) fprintf(stderr, "def %s %x (%s:%d:%d)\n", Ident, ScopeID, Parser->FileName, Parser->Line, Parser->CharacterPos);
#endif
if (InitValue != NULL)
AssignValue = VariableAllocValueAndCopy(pc, Parser, InitValue, pc->TopStackFrame == NULL);
else
AssignValue = VariableAllocValueFromType(pc, Parser, Typ, MakeWritable, NULL, pc->TopStackFrame == NULL);
AssignValue->IsLValue = (char) MakeWritable;
AssignValue->ScopeID = ScopeID;
AssignValue->OutOfScope = FALSE;
if (!TableSet(pc, currentTable, Ident, AssignValue, Parser ? ((char *)Parser->FileName) : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0))
ProgramFail(Parser, "'%s' is already defined", Ident);
return AssignValue;
}
/* define a variable. Ident must be registered. If it's a redefinition from the same declaration don't throw an error */
struct Value *VariableDefineButIgnoreIdentical(struct ParseState *Parser, char *Ident, struct ValueType *Typ, int IsStatic, int *FirstVisit)
{
Picoc *pc = Parser->pc;
struct Value *ExistingValue;
const char *DeclFileName;
int DeclLine;
int DeclColumn;
/* is the type a forward declaration? */
if (TypeIsForwardDeclared(Parser, Typ))
ProgramFail(Parser, "type '%t' isn't defined", Typ);
if (IsStatic)
{
char MangledName[LINEBUFFER_MAX];
char *MNPos = &MangledName[0];
char *MNEnd = &MangledName[LINEBUFFER_MAX-1];
const char *RegisteredMangledName;
/* make the mangled static name (avoiding using sprintf() to minimise library impact) */
memset((void *)&MangledName, '\0', sizeof(MangledName));
*MNPos++ = '/';
strncpy(MNPos, (char *)Parser->FileName, MNEnd - MNPos);
MNPos += strlen(MNPos);
if (pc->TopStackFrame != NULL)
{
/* we're inside a function */
if (MNEnd - MNPos > 0) *MNPos++ = '/';
strncpy(MNPos, (char *)pc->TopStackFrame->FuncName, MNEnd - MNPos);
MNPos += strlen(MNPos);
}
if (MNEnd - MNPos > 0) *MNPos++ = '/';
strncpy(MNPos, Ident, MNEnd - MNPos);
RegisteredMangledName = TableStrRegister(pc, MangledName);
/* is this static already defined? */
if (!TableGet(&pc->GlobalTable, RegisteredMangledName, &ExistingValue, &DeclFileName, &DeclLine, &DeclColumn))
{
/* define the mangled-named static variable store in the global scope */
ExistingValue = VariableAllocValueFromType(Parser->pc, Parser, Typ, TRUE, NULL, TRUE);
TableSet(pc, &pc->GlobalTable, (char *)RegisteredMangledName, ExistingValue, (char *)Parser->FileName, Parser->Line, Parser->CharacterPos);
*FirstVisit = TRUE;
}
/* static variable exists in the global scope - now make a mirroring variable in our own scope with the short name */
VariableDefinePlatformVar(Parser->pc, Parser, Ident, ExistingValue->Typ, ExistingValue->Val, TRUE);
return ExistingValue;
}
else
{
if (Parser->Line != 0 && TableGet((pc->TopStackFrame == NULL) ? &pc->GlobalTable : &pc->TopStackFrame->LocalTable, Ident, &ExistingValue, &DeclFileName, &DeclLine, &DeclColumn)
&& DeclFileName == Parser->FileName && DeclLine == Parser->Line && DeclColumn == Parser->CharacterPos)
return ExistingValue;
else
return VariableDefine(Parser->pc, Parser, Ident, NULL, Typ, TRUE);
}
}
/* check if a variable with a given name is defined. Ident must be registered */
int VariableDefined(Picoc *pc, const char *Ident)
{
struct Value *FoundValue;
if (pc->TopStackFrame == NULL || !TableGet(&pc->TopStackFrame->LocalTable, Ident, &FoundValue, NULL, NULL, NULL))
{
if (!TableGet(&pc->GlobalTable, Ident, &FoundValue, NULL, NULL, NULL))
return FALSE;
}
return TRUE;
}
/* get the value of a variable. must be defined. Ident must be registered */
void VariableGet(Picoc *pc, struct ParseState *Parser, const char *Ident, struct Value **LVal)
{
if (pc->TopStackFrame == NULL || !TableGet(&pc->TopStackFrame->LocalTable, Ident, LVal, NULL, NULL, NULL))
{
if (!TableGet(&pc->GlobalTable, Ident, LVal, NULL, NULL, NULL))
{
if (VariableDefinedAndOutOfScope(pc, Ident))
ProgramFail(Parser, "'%s' is out of scope", Ident);
else
ProgramFail(Parser, "'%s' is undefined", Ident);
}
}
}
/* define a global variable shared with a platform global. Ident will be registered */
void VariableDefinePlatformVar(Picoc *pc, struct ParseState *Parser, char *Ident, struct ValueType *Typ, union AnyValue *FromValue, int IsWritable)
{
struct Value *SomeValue = VariableAllocValueAndData(pc, NULL, 0, IsWritable, NULL, TRUE);
SomeValue->Typ = Typ;
SomeValue->Val = FromValue;
if (!TableSet(pc, (pc->TopStackFrame == NULL) ? &pc->GlobalTable : &pc->TopStackFrame->LocalTable, TableStrRegister(pc, Ident), SomeValue, Parser ? Parser->FileName : NULL, Parser ? Parser->Line : 0, Parser ? Parser->CharacterPos : 0))
ProgramFail(Parser, "'%s' is already defined", Ident);
}
/* free and/or pop the top value off the stack. Var must be the top value on the stack! */
void VariableStackPop(struct ParseState *Parser, struct Value *Var)
{
int Success;
#ifdef DEBUG_HEAP
if (Var->ValOnStack)
printf("popping %ld at 0x%lx\n", (unsigned long)(sizeof(struct Value) + TypeSizeValue(Var, FALSE)), (unsigned long)Var);
#endif
if (Var->ValOnHeap)
{
if (Var->Val != NULL)
HeapFreeMem(Parser->pc, Var->Val);
Success = HeapPopStack(Parser->pc, Var, sizeof(struct Value)); /* free from heap */
}
else if (Var->ValOnStack)
Success = HeapPopStack(Parser->pc, Var, sizeof(struct Value) + TypeSizeValue(Var, FALSE)); /* free from stack */
else
Success = HeapPopStack(Parser->pc, Var, sizeof(struct Value)); /* value isn't our problem */
if (!Success)
ProgramFail(Parser, "stack underrun");
}
/* add a stack frame when doing a function call */
void VariableStackFrameAdd(struct ParseState *Parser, const char *FuncName, int NumParams)
{
struct StackFrame *NewFrame;
HeapPushStackFrame(Parser->pc);
NewFrame = (struct StackFrame *) HeapAllocStack(Parser->pc, sizeof(struct StackFrame) + sizeof(struct Value *) * NumParams);
if (NewFrame == NULL)
ProgramFail(Parser, "out of memory");
ParserCopy(&NewFrame->ReturnParser, Parser);
NewFrame->FuncName = FuncName;
NewFrame->Parameter = (struct Value **) ((NumParams > 0) ? ((void *)((char *)NewFrame + sizeof(struct StackFrame))) : NULL);
TableInitTable(&NewFrame->LocalTable, &NewFrame->LocalHashTable[0], LOCAL_TABLE_SIZE, FALSE);
NewFrame->PreviousStackFrame = Parser->pc->TopStackFrame;
Parser->pc->TopStackFrame = NewFrame;
}
/* remove a stack frame */
void VariableStackFramePop(struct ParseState *Parser)
{
if (Parser->pc->TopStackFrame == NULL)
ProgramFail(Parser, "stack is empty - can't go back");
ParserCopy(Parser, &Parser->pc->TopStackFrame->ReturnParser);
Parser->pc->TopStackFrame = Parser->pc->TopStackFrame->PreviousStackFrame;
HeapPopStackFrame(Parser->pc);
}
/* get a string literal. assumes that Ident is already registered. NULL if not found */
struct Value *VariableStringLiteralGet(Picoc *pc, char *Ident)
{
struct Value *LVal = NULL;
if (TableGet(&pc->StringLiteralTable, Ident, &LVal, NULL, NULL, NULL))
return LVal;
else
return NULL;
}
/* define a string literal. assumes that Ident is already registered */
void VariableStringLiteralDefine(Picoc *pc, char *Ident, struct Value *Val)
{
TableSet(pc, &pc->StringLiteralTable, Ident, Val, NULL, 0, 0);
}
/* check a pointer for validity and dereference it for use */
void *VariableDereferencePointer(struct ParseState *Parser, struct Value *PointerValue, struct Value **DerefVal, int *DerefOffset, struct ValueType **DerefType, int *DerefIsLValue)
{
if (PointerValue->Typ->Base != TypePointer)
ProgramFail(Parser, "attempt to deference a non-pointer");
if (DerefVal != NULL)
*DerefVal = NULL;
if (DerefType != NULL)
*DerefType = PointerValue->Typ->FromType;
if (DerefOffset != NULL)
*DerefOffset = 0;
if (DerefIsLValue != NULL)
*DerefIsLValue = TRUE;
return PointerValue->Val->Pointer;
}