2009-01-23 06:34:12 -05:00
|
|
|
#include "picoc.h"
|
|
|
|
|
|
|
|
/* some basic types */
|
|
|
|
struct ValueType UberType;
|
|
|
|
struct ValueType IntType;
|
|
|
|
struct ValueType CharType;
|
2009-02-20 21:35:52 -05:00
|
|
|
struct ValueType WordType;
|
2009-02-23 19:21:17 -05:00
|
|
|
#ifndef NO_FP
|
2009-01-23 06:34:12 -05:00
|
|
|
struct ValueType FPType;
|
2009-02-23 19:21:17 -05:00
|
|
|
#endif
|
2009-01-23 06:34:12 -05:00
|
|
|
struct ValueType VoidType;
|
|
|
|
struct ValueType FunctionType;
|
|
|
|
struct ValueType MacroType;
|
|
|
|
struct ValueType EnumType;
|
|
|
|
struct ValueType Type_Type;
|
2009-02-20 21:35:52 -05:00
|
|
|
struct ValueType *CharPtrType;
|
|
|
|
struct ValueType *CharArrayType;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* add a new type to the set of types we know about */
|
2009-02-02 00:27:15 -05:00
|
|
|
struct ValueType *TypeAdd(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier, int Sizeof)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-02 00:27:15 -05:00
|
|
|
struct ValueType *NewType = VariableAlloc(Parser, sizeof(struct ValueType), TRUE);
|
2009-01-23 06:34:12 -05:00
|
|
|
NewType->Base = Base;
|
|
|
|
NewType->ArraySize = ArraySize;
|
|
|
|
NewType->Sizeof = Sizeof;
|
2009-02-02 00:27:15 -05:00
|
|
|
NewType->Identifier = Identifier;
|
2009-01-23 06:34:12 -05:00
|
|
|
NewType->Members = NULL;
|
|
|
|
NewType->FromType = ParentType;
|
|
|
|
NewType->DerivedTypeList = NULL;
|
|
|
|
NewType->Next = ParentType->DerivedTypeList;
|
|
|
|
ParentType->DerivedTypeList = NewType;
|
|
|
|
|
|
|
|
return NewType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* given a parent type, get a matching derived type and make one if necessary */
|
2009-02-02 00:27:15 -05:00
|
|
|
struct ValueType *TypeGetMatching(struct ParseState *Parser, struct ValueType *ParentType, enum BaseType Base, int ArraySize, const char *Identifier)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
|
|
|
int Sizeof;
|
|
|
|
struct ValueType *ThisType = ParentType->DerivedTypeList;
|
2009-02-02 00:27:15 -05:00
|
|
|
while (ThisType != NULL && (ThisType->Base != Base || ThisType->ArraySize != ArraySize || ThisType->Identifier != Identifier))
|
2009-01-23 06:34:12 -05:00
|
|
|
ThisType = ThisType->Next;
|
|
|
|
|
|
|
|
if (ThisType != NULL)
|
|
|
|
return ThisType;
|
|
|
|
|
|
|
|
switch (Base)
|
|
|
|
{
|
|
|
|
case TypePointer: Sizeof = sizeof(struct PointerValue); break;
|
2009-02-23 21:28:35 -05:00
|
|
|
case TypeArray: Sizeof = sizeof(struct ArrayValue) + ArraySize * ParentType->Sizeof; break;
|
2009-01-23 06:34:12 -05:00
|
|
|
case TypeEnum: Sizeof = sizeof(int); break;
|
|
|
|
default: Sizeof = 0; break; /* structs and unions will get bigger when we add members to them */
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
return TypeAdd(Parser, ParentType, Base, ArraySize, Identifier, Sizeof);
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
2009-03-02 17:13:47 -05:00
|
|
|
/* stack space used by a value */
|
|
|
|
int TypeStackSizeValue(struct Value *Val)
|
|
|
|
{
|
|
|
|
if (Val->ValOnStack)
|
|
|
|
return TypeSizeValue(Val); // XXX - doesn't handle passing system-memory arrays by value correctly
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-20 21:35:52 -05:00
|
|
|
/* memory used by a value */
|
|
|
|
int TypeSizeValue(struct Value *Val)
|
|
|
|
{
|
|
|
|
if (Val->Typ->Base != TypeArray)
|
|
|
|
return Val->Typ->Sizeof;
|
|
|
|
else
|
2009-02-23 19:08:11 -05:00
|
|
|
return sizeof(struct ArrayValue) + Val->Typ->FromType->Sizeof * Val->Val->Array.Size;
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* memory used by a variable given its type and array size */
|
|
|
|
int TypeSize(struct ValueType *Typ, int ArraySize)
|
|
|
|
{
|
|
|
|
if (Typ->Base != TypeArray)
|
|
|
|
return Typ->Sizeof;
|
|
|
|
else
|
2009-02-23 21:28:35 -05:00
|
|
|
return sizeof(struct ArrayValue) + Typ->FromType->Sizeof * ArraySize;
|
2009-02-20 21:35:52 -05:00
|
|
|
}
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* add a base type */
|
|
|
|
void TypeAddBaseType(struct ValueType *TypeNode, enum BaseType Base, int Sizeof)
|
|
|
|
{
|
|
|
|
TypeNode->Base = Base;
|
|
|
|
TypeNode->ArraySize = 0;
|
|
|
|
TypeNode->Sizeof = Sizeof;
|
2009-01-24 21:09:44 -05:00
|
|
|
TypeNode->Identifier = StrEmpty;
|
2009-01-23 06:34:12 -05:00
|
|
|
TypeNode->Members = NULL;
|
|
|
|
TypeNode->FromType = NULL;
|
|
|
|
TypeNode->DerivedTypeList = NULL;
|
2009-01-23 22:15:02 -05:00
|
|
|
TypeNode->Next = UberType.DerivedTypeList;
|
|
|
|
UberType.DerivedTypeList = TypeNode;
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialise the type system */
|
|
|
|
void TypeInit()
|
|
|
|
{
|
|
|
|
UberType.DerivedTypeList = NULL;
|
|
|
|
TypeAddBaseType(&IntType, TypeInt, sizeof(int));
|
2009-02-23 19:21:17 -05:00
|
|
|
#ifndef NO_FP
|
2009-01-23 06:34:12 -05:00
|
|
|
TypeAddBaseType(&FPType, TypeFP, sizeof(double));
|
2009-02-23 19:21:17 -05:00
|
|
|
#endif
|
2009-01-23 06:34:12 -05:00
|
|
|
TypeAddBaseType(&VoidType, TypeVoid, 0);
|
|
|
|
TypeAddBaseType(&FunctionType, TypeFunction, sizeof(int));
|
|
|
|
TypeAddBaseType(&MacroType, TypeMacro, sizeof(int));
|
|
|
|
TypeAddBaseType(&Type_Type, TypeType, sizeof(struct ValueType *));
|
2009-02-20 21:35:52 -05:00
|
|
|
TypeAddBaseType(&CharType, TypeChar, sizeof(char));
|
2009-02-21 07:02:15 -05:00
|
|
|
CharPtrType = TypeAdd(NULL, &CharType, TypePointer, 0, StrEmpty, sizeof(struct PointerValue));
|
2009-02-20 21:35:52 -05:00
|
|
|
CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char));
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a struct or union declaration */
|
2009-02-02 00:27:15 -05:00
|
|
|
void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsStruct)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-01-26 03:57:32 -05:00
|
|
|
struct Value *LexValue;
|
2009-01-23 06:34:12 -05:00
|
|
|
struct ValueType *MemberType;
|
2009-02-20 21:35:52 -05:00
|
|
|
char *MemberIdentifier;
|
2009-01-23 06:34:12 -05:00
|
|
|
struct Value *MemberValue;
|
2009-02-10 03:42:09 -05:00
|
|
|
enum LexToken Token;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-01-29 06:10:46 -05:00
|
|
|
if (TopStackFrame != NULL)
|
2009-02-02 00:27:15 -05:00
|
|
|
ProgramFail(Parser, "struct/union definitions can only be globals");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-10 03:42:09 -05:00
|
|
|
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
|
|
|
|
ProgramFail(Parser, "struct/union name required");
|
|
|
|
|
2009-02-20 21:35:52 -05:00
|
|
|
*Typ = TypeGetMatching(Parser, &UberType, IsStruct ? TypeStruct : TypeUnion, 0, LexValue->Val->Identifier);
|
2009-02-10 03:42:09 -05:00
|
|
|
|
|
|
|
Token = LexGetToken(Parser, NULL, FALSE);
|
|
|
|
if (Token != TokenLeftBrace)
|
|
|
|
{ /* use the already defined structure */
|
|
|
|
if ((*Typ)->Members == NULL)
|
2009-02-20 21:35:52 -05:00
|
|
|
ProgramFail(Parser, "structure '%s' isn't defined", LexValue->Val->Identifier);
|
2009-02-10 03:42:09 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LexGetToken(Parser, NULL, TRUE);
|
2009-02-02 00:27:15 -05:00
|
|
|
(*Typ)->Members = VariableAlloc(Parser, sizeof(struct Table) + STRUCT_TABLE_SIZE * sizeof(struct TableEntry), TRUE);
|
2009-01-24 21:09:44 -05:00
|
|
|
(*Typ)->Members->HashTable = (void *)(*Typ)->Members + sizeof(struct Table);
|
2009-02-20 21:35:52 -05:00
|
|
|
TableInitTable((*Typ)->Members, (void *)(*Typ)->Members + sizeof(struct Table), STRUCT_TABLE_SIZE, TRUE);
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
do {
|
2009-02-02 00:27:15 -05:00
|
|
|
TypeParse(Parser, &MemberType, &MemberIdentifier);
|
2009-02-10 03:42:09 -05:00
|
|
|
if (MemberType == NULL || MemberIdentifier == NULL)
|
|
|
|
ProgramFail(Parser, "invalid type in struct");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-26 04:56:22 -05:00
|
|
|
MemberValue = VariableAllocValueAndData(Parser, sizeof(int), FALSE, NULL, TRUE);
|
2009-01-24 21:09:44 -05:00
|
|
|
MemberValue->Typ = MemberType;
|
2009-01-23 06:34:12 -05:00
|
|
|
if (IsStruct)
|
|
|
|
{ /* allocate this member's location in the struct */
|
2009-01-24 21:09:44 -05:00
|
|
|
MemberValue->Val->Integer = (*Typ)->Sizeof;
|
2009-01-23 06:34:12 -05:00
|
|
|
(*Typ)->Sizeof += MemberValue->Typ->Sizeof;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* union members always start at 0, make sure it's big enough to hold the largest member */
|
2009-01-24 21:09:44 -05:00
|
|
|
MemberValue->Val->Integer = 0;
|
2009-01-23 06:34:12 -05:00
|
|
|
if (MemberValue->Typ->Sizeof > (*Typ)->Sizeof)
|
|
|
|
(*Typ)->Sizeof = MemberValue->Typ->Sizeof;
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
if (!TableSet((*Typ)->Members, MemberIdentifier, MemberValue))
|
2009-02-02 06:08:36 -05:00
|
|
|
ProgramFail(Parser, "member '%s' already defined", &MemberIdentifier);
|
2009-02-10 03:42:09 -05:00
|
|
|
|
|
|
|
if (LexGetToken(Parser, NULL, TRUE) != TokenSemicolon)
|
|
|
|
ProgramFail(Parser, "semicolon expected");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
} while (LexGetToken(Parser, NULL, FALSE) != TokenRightBrace);
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
LexGetToken(Parser, NULL, TRUE);
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
|
2009-03-09 18:36:01 -04:00
|
|
|
/* parse an enum declaration */
|
|
|
|
void TypeParseEnum(struct ParseState *Parser, struct ValueType **Typ)
|
|
|
|
{
|
|
|
|
struct Value *LexValue;
|
|
|
|
struct Value InitValue;
|
|
|
|
enum LexToken Token;
|
|
|
|
struct ValueType *EnumType;
|
|
|
|
int EnumValue = 0;
|
|
|
|
char *EnumIdentifier;
|
|
|
|
|
|
|
|
if (TopStackFrame != NULL)
|
|
|
|
ProgramFail(Parser, "enum definitions can only be globals");
|
|
|
|
|
|
|
|
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
|
|
|
|
ProgramFail(Parser, "enum name required");
|
|
|
|
|
|
|
|
*Typ = &IntType;
|
|
|
|
EnumType = TypeGetMatching(Parser, &UberType, TypeEnum, 0, LexValue->Val->Identifier);
|
|
|
|
Token = LexGetToken(Parser, NULL, FALSE);
|
|
|
|
if (Token != TokenLeftBrace)
|
|
|
|
{ /* use the already defined enum */
|
|
|
|
if ((*Typ)->Members == NULL)
|
|
|
|
ProgramFail(Parser, "enum '%s' isn't defined", LexValue->Val->Identifier);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LexGetToken(Parser, NULL, TRUE);
|
|
|
|
(*Typ)->Members = &GlobalTable;
|
|
|
|
memset(&InitValue, '\0', sizeof(struct Value));
|
|
|
|
InitValue.Typ = &IntType;
|
|
|
|
InitValue.Val = (union AnyValue *)&EnumValue;
|
|
|
|
do {
|
|
|
|
if (LexGetToken(Parser, &LexValue, TRUE) != TokenIdentifier)
|
|
|
|
ProgramFail(Parser, "identifier expected");
|
|
|
|
|
|
|
|
EnumIdentifier = LexValue->Val->Identifier;
|
|
|
|
if (LexGetToken(Parser, NULL, FALSE) == TokenAssign)
|
|
|
|
{
|
|
|
|
LexGetToken(Parser, NULL, TRUE);
|
|
|
|
EnumValue = ExpressionParseInt(Parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableDefine(Parser, EnumIdentifier, &InitValue);
|
|
|
|
|
|
|
|
Token = LexGetToken(Parser, NULL, TRUE);
|
|
|
|
if (Token != TokenComma && Token != TokenRightBrace)
|
|
|
|
ProgramFail(Parser, "comma expected");
|
|
|
|
|
|
|
|
EnumValue++;
|
|
|
|
|
|
|
|
} while (Token == TokenComma);
|
|
|
|
}
|
|
|
|
|
2009-03-06 01:00:51 -05:00
|
|
|
/* parse a type - just the basic type */
|
|
|
|
int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ)
|
|
|
|
{
|
|
|
|
struct ParseState Before = *Parser;
|
|
|
|
enum LexToken Token = LexGetToken(Parser, NULL, TRUE);
|
|
|
|
*Typ = NULL;
|
|
|
|
|
|
|
|
switch (Token)
|
|
|
|
{
|
|
|
|
case TokenIntType: case TokenLongType: case TokenShortType: *Typ = &IntType; break;
|
|
|
|
case TokenCharType: *Typ = &CharType; break;
|
|
|
|
#ifndef NO_FP
|
|
|
|
case TokenFloatType: case TokenDoubleType: *Typ = &FPType; break;
|
|
|
|
#endif
|
|
|
|
case TokenVoidType: *Typ = &VoidType; break;
|
|
|
|
|
|
|
|
case TokenStructType: case TokenUnionType:
|
|
|
|
if (*Typ != NULL)
|
|
|
|
ProgramFail(Parser, "bad type declaration");
|
|
|
|
|
|
|
|
TypeParseStruct(Parser, Typ, Token == TokenStructType);
|
|
|
|
break;
|
2009-03-09 18:36:01 -04:00
|
|
|
|
|
|
|
case TokenEnumType:
|
|
|
|
if (*Typ != NULL)
|
|
|
|
ProgramFail(Parser, "bad type declaration");
|
|
|
|
|
|
|
|
TypeParseEnum(Parser, Typ);
|
|
|
|
break;
|
2009-03-06 01:00:51 -05:00
|
|
|
|
|
|
|
default: *Parser = Before; return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a type - the part which is repeated with each identifier in a declaration list */
|
|
|
|
void TypeParseIdentPart(struct ParseState *Parser, struct ValueType *BasicTyp, struct ValueType **Typ, char **Identifier)
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-02 00:27:15 -05:00
|
|
|
struct ParseState Before;
|
2009-01-24 21:09:44 -05:00
|
|
|
enum LexToken Token;
|
2009-01-26 03:57:32 -05:00
|
|
|
struct Value *LexValue;
|
2009-01-23 06:34:12 -05:00
|
|
|
int Done = FALSE;
|
2009-03-06 01:00:51 -05:00
|
|
|
*Typ = BasicTyp;
|
2009-02-02 00:27:15 -05:00
|
|
|
*Identifier = StrEmpty;
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
while (!Done)
|
|
|
|
{
|
2009-02-02 00:27:15 -05:00
|
|
|
Before = *Parser;
|
|
|
|
Token = LexGetToken(Parser, &LexValue, TRUE);
|
2009-01-23 06:34:12 -05:00
|
|
|
switch (Token)
|
|
|
|
{
|
|
|
|
case TokenOpenBracket:
|
|
|
|
if (*Typ != NULL)
|
2009-02-02 00:27:15 -05:00
|
|
|
ProgramFail(Parser, "bad type declaration");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
TypeParse(Parser, Typ, Identifier);
|
|
|
|
if (LexGetToken(Parser, NULL, TRUE) != TokenCloseBracket)
|
|
|
|
ProgramFail(Parser, "')' expected");
|
2009-01-23 06:34:12 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokenAsterisk:
|
|
|
|
if (*Typ == NULL)
|
2009-02-02 00:27:15 -05:00
|
|
|
ProgramFail(Parser, "bad type declaration");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
*Typ = TypeGetMatching(Parser, *Typ, TypePointer, 0, StrEmpty);
|
2009-01-23 06:34:12 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokenIdentifier:
|
2009-02-02 19:46:02 -05:00
|
|
|
if (*Typ == NULL || *Identifier != StrEmpty)
|
2009-02-02 00:27:15 -05:00
|
|
|
ProgramFail(Parser, "bad type declaration");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-20 21:35:52 -05:00
|
|
|
*Identifier = LexValue->Val->Identifier;
|
2009-01-23 06:34:12 -05:00
|
|
|
Done = TRUE;
|
|
|
|
break;
|
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
default: *Parser = Before; Done = TRUE; break;
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*Typ == NULL)
|
2009-02-02 00:27:15 -05:00
|
|
|
ProgramFail(Parser, "bad type declaration");
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
if (*Identifier != StrEmpty)
|
2009-01-23 06:34:12 -05:00
|
|
|
{ /* parse stuff after the identifier */
|
|
|
|
Done = FALSE;
|
|
|
|
while (!Done)
|
|
|
|
{
|
2009-02-02 00:27:15 -05:00
|
|
|
Before = *Parser;
|
|
|
|
switch (LexGetToken(Parser, NULL, TRUE))
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-01-24 21:09:44 -05:00
|
|
|
case TokenLeftSquareBracket:
|
2009-01-23 22:15:02 -05:00
|
|
|
{
|
2009-02-18 03:19:06 -05:00
|
|
|
enum RunMode OldMode = Parser->Mode;
|
|
|
|
Parser->Mode = RunModeRun;
|
2009-03-08 03:56:28 -04:00
|
|
|
int ArraySize = ExpressionParseInt(Parser);
|
2009-02-18 03:19:06 -05:00
|
|
|
Parser->Mode = OldMode;
|
2009-01-26 03:57:32 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
if (LexGetToken(Parser, NULL, TRUE) != TokenRightSquareBracket)
|
|
|
|
ProgramFail(Parser, "']' expected");
|
2009-01-23 22:15:02 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
*Typ = TypeGetMatching(Parser, *Typ, TypeArray, ArraySize, StrEmpty);
|
2009-01-23 22:15:02 -05:00
|
|
|
}
|
2009-01-23 06:34:12 -05:00
|
|
|
break;
|
|
|
|
|
2009-02-03 05:39:48 -05:00
|
|
|
// case TokenOpenBracket:
|
|
|
|
// break; // XXX - finish this
|
2009-01-23 06:34:12 -05:00
|
|
|
|
2009-02-02 00:27:15 -05:00
|
|
|
default: *Parser = Before; Done = TRUE; break;
|
2009-01-23 06:34:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-06 01:00:51 -05:00
|
|
|
|
|
|
|
/* parse a type - a complete declaration including identifier */
|
|
|
|
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, char **Identifier)
|
|
|
|
{
|
|
|
|
struct ValueType *BasicType;
|
|
|
|
|
|
|
|
TypeParseFront(Parser, &BasicType);
|
|
|
|
TypeParseIdentPart(Parser, BasicType, Typ, Identifier);
|
|
|
|
}
|
|
|
|
|