2009-02-10 03:42:09 -05:00
|
|
|
#ifdef DEBUG_HEAP
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
2009-01-23 06:34:12 -05:00
|
|
|
#include <string.h>
|
2009-02-20 21:35:52 -05:00
|
|
|
#include <assert.h>
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
#include "picoc.h"
|
|
|
|
|
|
|
|
/* the table of global definitions */
|
|
|
|
struct Table GlobalTable;
|
2009-01-29 17:26:04 -05:00
|
|
|
struct TableEntry *GlobalHashTable[GLOBAL_TABLE_SIZE];
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
/* the stack */
|
2009-01-29 06:10:46 -05:00
|
|
|
struct StackFrame *TopStackFrame = NULL;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* initialise the variable system */
|
|
|
|
void VariableInit()
|
|
|
|
{
|
2009-02-20 21:35:52 -05:00
|
|
|
TableInitTable(&GlobalTable, &GlobalHashTable[0], GLOBAL_TABLE_SIZE, TRUE);
|
2009-01-29 06:10:46 -05:00
|
|
|
TopStackFrame = NULL;
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate some memory, either on the heap or the stack and check if we've run out */
|
2009-02-01 06:31:18 -05:00
|
|
|
void *VariableAlloc(struct ParseState *Parser, int Size, int OnHeap)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
|
|
|
void *NewValue;
|
|
|
|
|
2009-01-26 03:57:32 -05:00
|
|
|
if (OnHeap)
|
2009-01-23 06:34:12 -05:00
|
|
|
NewValue = HeapAlloc(Size);
|
|
|
|
else
|
|
|
|
NewValue = HeapAllocStack(Size);
|
|
|
|
|
|
|
|
if (NewValue == NULL)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(Parser, "out of memory");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-03 19:17:30 -05:00
|
|
|
#ifdef DEBUG_HEAP
|
|
|
|
if (!OnHeap)
|
|
|
|
printf("pushing %d at 0x%lx\n", Size, (unsigned long)NewValue);
|
|
|
|
#endif
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
return NewValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a value either on the heap or the stack using space dependent on what type we want */
|
2009-02-15 00:52:03 -05:00
|
|
|
struct Value *VariableAllocValueAndData(struct ParseState *Parser, int DataSize, int IsLValue, int OnHeap)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-03 05:39:48 -05:00
|
|
|
struct Value *NewValue = VariableAlloc(Parser, sizeof(struct Value) + DataSize, OnHeap);
|
2009-01-23 06:34:12 -05:00
|
|
|
NewValue->Val = (union AnyValue *)((void *)NewValue + sizeof(struct Value));
|
2009-01-26 03:57:32 -05:00
|
|
|
NewValue->ValOnHeap = OnHeap;
|
|
|
|
NewValue->ValOnStack = !OnHeap;
|
2009-02-15 00:52:03 -05:00
|
|
|
NewValue->IsLValue = IsLValue;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
return NewValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a value given its type */
|
2009-02-19 18:29:35 -05:00
|
|
|
struct Value *VariableAllocValueFromType(struct ParseState *Parser, struct ValueType *Typ, int IsLValue)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-20 21:35:52 -05:00
|
|
|
int Size = TypeSize(Typ, Typ->ArraySize);
|
|
|
|
struct Value *NewValue = VariableAllocValueAndData(Parser, Size, IsLValue, FALSE);
|
|
|
|
assert(Size > 0 || Typ == &VoidType);
|
2009-01-23 06:34:12 -05:00
|
|
|
NewValue->Typ = Typ;
|
|
|
|
return NewValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a value either on the heap or the stack and copy its value */
|
2009-02-01 06:31:18 -05:00
|
|
|
struct Value *VariableAllocValueAndCopy(struct ParseState *Parser, struct Value *FromValue, int OnHeap)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-20 21:35:52 -05:00
|
|
|
int CopySize = TypeSizeValue(FromValue);
|
|
|
|
struct Value *NewValue = VariableAllocValueAndData(Parser, CopySize, FromValue->IsLValue, OnHeap);
|
2009-01-23 06:34:12 -05:00
|
|
|
NewValue->Typ = FromValue->Typ;
|
2009-02-20 21:35:52 -05:00
|
|
|
memcpy(NewValue->Val, FromValue->Val, CopySize);
|
2009-01-23 06:34:12 -05:00
|
|
|
return NewValue;
|
|
|
|
}
|
|
|
|
|
2009-02-09 06:40:56 -05:00
|
|
|
/* allocate a value either on the heap or the stack from an existing AnyValue and type */
|
2009-02-19 18:29:35 -05:00
|
|
|
struct Value *VariableAllocValueFromExistingData(struct ParseState *Parser, struct ValueType *Typ, union AnyValue *FromValue, int IsLValue)
|
2009-02-09 06:40:56 -05:00
|
|
|
{
|
2009-02-19 18:29:35 -05:00
|
|
|
struct Value *NewValue = VariableAlloc(Parser, sizeof(struct Value), FALSE);
|
2009-02-09 06:40:56 -05:00
|
|
|
NewValue->Typ = Typ;
|
|
|
|
NewValue->Val = FromValue;
|
|
|
|
NewValue->ValOnHeap = FALSE;
|
|
|
|
NewValue->ValOnStack = FALSE;
|
2009-02-15 00:52:03 -05:00
|
|
|
NewValue->IsLValue = IsLValue;
|
2009-02-09 06:40:56 -05:00
|
|
|
|
|
|
|
return NewValue;
|
|
|
|
}
|
|
|
|
|
2009-02-11 01:47:36 -05:00
|
|
|
/* allocate a value either on the heap or the stack from an existing Value, sharing the value */
|
2009-02-19 18:29:35 -05:00
|
|
|
struct Value *VariableAllocValueShared(struct ParseState *Parser, struct Value *FromValue)
|
2009-02-11 01:47:36 -05:00
|
|
|
{
|
2009-02-19 18:29:35 -05:00
|
|
|
return VariableAllocValueFromExistingData(Parser, FromValue->Typ, FromValue->Val, FromValue->IsLValue);
|
2009-02-11 01:47:36 -05:00
|
|
|
}
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* define a variable */
|
2009-02-20 21:35:52 -05:00
|
|
|
void VariableDefine(struct ParseState *Parser, char *Ident, struct Value *InitValue)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
if (!TableSet((TopStackFrame == NULL) ? &GlobalTable : &TopStackFrame->LocalTable, Ident, VariableAllocValueAndCopy(Parser, InitValue, TopStackFrame == NULL)))
|
2009-02-02 06:08:36 -05:00
|
|
|
ProgramFail(Parser, "'%s' is already defined", Ident);
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if a variable with a given name is defined */
|
2009-02-01 06:31:18 -05:00
|
|
|
int VariableDefined(const char *Ident)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
|
|
|
struct Value *FoundValue;
|
|
|
|
|
2009-01-29 06:10:46 -05:00
|
|
|
if (TopStackFrame == NULL || !TableGet(&TopStackFrame->LocalTable, Ident, &FoundValue))
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
|
|
|
if (!TableGet(&GlobalTable, Ident, &FoundValue))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the value of a variable. must be defined */
|
2009-02-01 06:31:18 -05:00
|
|
|
void VariableGet(struct ParseState *Parser, const char *Ident, struct Value **LVal)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-01-29 06:10:46 -05:00
|
|
|
if (TopStackFrame == NULL || !TableGet(&TopStackFrame->LocalTable, Ident, LVal))
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
|
|
|
if (!TableGet(&GlobalTable, Ident, LVal))
|
2009-02-02 06:08:36 -05:00
|
|
|
ProgramFail(Parser, "'%s' is undefined", Ident);
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
2009-01-26 03:57:32 -05:00
|
|
|
}
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-01-26 03:57:32 -05:00
|
|
|
/* free and/or pop the top value off the stack. Var must be the top value on the stack! */
|
2009-02-01 06:31:18 -05:00
|
|
|
void VariableStackPop(struct ParseState *Parser, struct Value *Var)
|
2009-01-26 03:57:32 -05:00
|
|
|
{
|
|
|
|
int Success;
|
|
|
|
|
2009-02-03 19:17:30 -05:00
|
|
|
#ifdef DEBUG_HEAP
|
|
|
|
if (Var->ValOnStack)
|
2009-02-20 21:35:52 -05:00
|
|
|
printf("popping %d at 0x%lx\n", sizeof(struct Value) + VariableSizeValue(Var), (unsigned long)Var);
|
2009-02-03 19:17:30 -05:00
|
|
|
#endif
|
|
|
|
|
2009-01-26 03:57:32 -05:00
|
|
|
if (Var->ValOnHeap)
|
2009-02-15 00:52:03 -05:00
|
|
|
{
|
2009-01-26 03:57:32 -05:00
|
|
|
HeapFree(Var->Val);
|
|
|
|
Success = HeapPopStack(Var, sizeof(struct Value)); /* free from heap */
|
|
|
|
}
|
|
|
|
else if (Var->ValOnStack)
|
2009-02-20 21:35:52 -05:00
|
|
|
Success = HeapPopStack(Var, sizeof(struct Value) + TypeSizeValue(Var)); /* free from stack */
|
2009-01-26 03:57:32 -05:00
|
|
|
else
|
|
|
|
Success = HeapPopStack(Var, sizeof(struct Value)); /* value isn't our problem */
|
|
|
|
|
|
|
|
if (!Success)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(Parser, "stack underrun");
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a stack frame when doing a function call */
|
2009-02-19 18:29:35 -05:00
|
|
|
void VariableStackFrameAdd(struct ParseState *Parser, int NumParams)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-01-29 06:10:46 -05:00
|
|
|
struct StackFrame *NewFrame;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-01-29 06:10:46 -05:00
|
|
|
HeapPushStackFrame();
|
2009-02-19 18:29:35 -05:00
|
|
|
NewFrame = HeapAllocStack(sizeof(struct StackFrame) + sizeof(struct Value *) * NumParams);
|
2009-02-20 21:35:52 -05:00
|
|
|
if (NewFrame == NULL)
|
|
|
|
ProgramFail(Parser, "out of memory");
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
NewFrame->ReturnParser = *Parser;
|
2009-02-19 18:29:35 -05:00
|
|
|
NewFrame->Parameter = (NumParams > 0) ? ((void *)NewFrame + sizeof(struct StackFrame)) : NULL;
|
2009-02-20 21:35:52 -05:00
|
|
|
TableInitTable(&NewFrame->LocalTable, &NewFrame->LocalHashTable[0], LOCAL_TABLE_SIZE, FALSE);
|
2009-01-29 06:10:46 -05:00
|
|
|
NewFrame->PreviousStackFrame = TopStackFrame;
|
|
|
|
TopStackFrame = NewFrame;
|
2009-01-26 03:57:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a stack frame */
|
2009-02-01 06:31:18 -05:00
|
|
|
void VariableStackFramePop(struct ParseState *Parser)
|
2009-01-26 03:57:32 -05:00
|
|
|
{
|
2009-01-29 06:10:46 -05:00
|
|
|
if (TopStackFrame == NULL)
|
2009-02-01 06:31:18 -05:00
|
|
|
ProgramFail(Parser, "stack is empty - can't go back");
|
2009-01-26 03:57:32 -05:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
*Parser = TopStackFrame->ReturnParser;
|
2009-02-03 19:17:30 -05:00
|
|
|
TopStackFrame = TopStackFrame->PreviousStackFrame;
|
2009-01-26 03:57:32 -05:00
|
|
|
HeapPopStackFrame();
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|