2008-10-12 20:53:28 -04:00
|
|
|
#ifndef PICOC_H
|
|
|
|
#define PICOC_H
|
|
|
|
|
2008-10-14 07:18:43 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* configurable options */
|
2009-01-20 19:52:42 -05:00
|
|
|
#define HEAP_SIZE 2048 /* space for the heap and the stack */
|
2008-12-20 20:36:09 -05:00
|
|
|
#define LARGE_INT_POWER_OF_TEN 1000000000 /* the largest power of ten which fits in an int on this architecture */
|
2009-01-20 19:52:42 -05:00
|
|
|
#define ARCH_ALIGN_WORDSIZE sizeof(int) /* memory alignment boundary on this architecture */
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
#define GLOBAL_TABLE_SIZE 397 /* global variable table */
|
|
|
|
#define STRING_TABLE_SIZE 97 /* shared string table size */
|
|
|
|
#define PARAMETER_MAX 10 /* maximum number of parameters to a function */
|
|
|
|
#define LINEBUFFER_MAX 256 /* maximum number of characters on a line */
|
|
|
|
#define LOCAL_TABLE_SIZE 11 /* size of local variable table (can expand) */
|
|
|
|
#define STRUCT_TABLE_SIZE 11 /* size of struct/union member table (can expand) */
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* handy definitions */
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
#define NULL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
#define min(x,y) (((x)<(y))?(x):(y))
|
|
|
|
#endif
|
|
|
|
|
2009-01-20 19:52:42 -05:00
|
|
|
#define MEM_ALIGN(x) (((x) + ARCH_ALIGN_WORDSIZE - 1) & ~(ARCH_ALIGN_WORDSIZE-1))
|
|
|
|
|
2008-12-26 22:04:58 -05:00
|
|
|
#define LOG10E 0.43429448190325182765
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024
|
|
|
|
#endif
|
|
|
|
|
2009-01-21 04:02:05 -05:00
|
|
|
#define ISVALUETYPE(t) (((t)->Base == TypeInt) || ((t)->Base == TypeFP) || ((t)->Base == TypeString))
|
2009-01-03 23:08:49 -05:00
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
struct Table;
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* lexical tokens */
|
|
|
|
enum LexToken
|
|
|
|
{
|
2009-01-23 22:15:02 -05:00
|
|
|
TokenNone, TokenEOF, TokenEndOfLine,
|
|
|
|
TokenIdentifier, TokenIntegerConstant, TokenFPConstant, TokenStringConstant, TokenCharacterConstant,
|
|
|
|
TokenOpenBracket, TokenCloseBracket,
|
|
|
|
TokenAssign, TokenPlus, TokenMinus, TokenAsterisk, TokenSlash,
|
|
|
|
TokenEquality, TokenLessThan, TokenGreaterThan, TokenLessEqual, TokenGreaterEqual,
|
|
|
|
TokenSemicolon, TokenComma, TokenDot,
|
|
|
|
TokenArrow, TokenAmpersand,
|
|
|
|
TokenLeftBrace, TokenRightBrace,
|
2009-01-24 21:09:44 -05:00
|
|
|
TokenLeftSquareBracket, TokenRightSquareBracket,
|
2009-01-23 22:15:02 -05:00
|
|
|
TokenLogicalAnd, TokenLogicalOr, TokenArithmeticOr, TokenArithmeticExor, TokenUnaryExor, TokenUnaryNot,
|
|
|
|
TokenAddAssign, TokenSubtractAssign,
|
|
|
|
TokenIncrement, TokenDecrement,
|
|
|
|
TokenIntType, TokenCharType, TokenFloatType, TokenDoubleType, TokenVoidType, TokenEnumType,
|
|
|
|
TokenLongType, TokenSignedType, TokenShortType, TokenStructType, TokenUnionType, TokenUnsignedType, TokenTypedef,
|
|
|
|
TokenDo, TokenElse, TokenFor, TokenIf, TokenWhile, TokenBreak, TokenSwitch, TokenCase, TokenDefault, TokenReturn,
|
|
|
|
TokenHashDefine, TokenHashInclude
|
2008-10-12 20:53:28 -04:00
|
|
|
};
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
/* parser state - has all this detail so we can parse nested files */
|
|
|
|
struct ParseState
|
2009-01-23 06:34:12 -05:00
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
const void *Pos;
|
|
|
|
const void *End;
|
2009-01-23 06:34:12 -05:00
|
|
|
int Line;
|
2009-02-01 06:31:18 -05:00
|
|
|
const char *FileName;
|
2008-10-12 20:53:28 -04:00
|
|
|
};
|
|
|
|
|
2008-12-18 19:22:52 -05:00
|
|
|
/* values */
|
2009-01-20 19:52:42 -05:00
|
|
|
enum BaseType
|
|
|
|
{
|
|
|
|
TypeVoid, /* no type */
|
|
|
|
TypeInt, /* integer */
|
|
|
|
TypeFP, /* floating point */
|
|
|
|
TypeChar, /* a single character - acts like an integer except in machine memory access */
|
|
|
|
TypeString, /* a constant source text string with C string emulation */
|
|
|
|
TypeFunction, /* a function */
|
|
|
|
TypeMacro, /* a macro */
|
|
|
|
TypePointer, /* a pointer */
|
|
|
|
TypeArray, /* an array of a sub-type */
|
2009-01-23 06:34:12 -05:00
|
|
|
TypeStruct, /* aggregate type */
|
|
|
|
TypeUnion, /* merged type */
|
|
|
|
TypeEnum, /* enumated integer type */
|
2009-01-20 19:52:42 -05:00
|
|
|
TypeType /* a type (eg. typedef) */
|
|
|
|
};
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
/* data type */
|
2009-01-20 19:52:42 -05:00
|
|
|
struct ValueType
|
|
|
|
{
|
|
|
|
enum BaseType Base; /* what kind of type this is */
|
2009-01-23 06:34:12 -05:00
|
|
|
int ArraySize; /* the size of an array type */
|
|
|
|
int Sizeof; /* the storage required */
|
2009-02-01 06:31:18 -05:00
|
|
|
const char *Identifier; /* the name of a struct or union */
|
2009-01-23 06:34:12 -05:00
|
|
|
struct ValueType *FromType; /* the type we're derived from (or NULL) */
|
|
|
|
struct ValueType *DerivedTypeList; /* first in a list of types derived from this one */
|
|
|
|
struct ValueType *Next; /* next item in the derived type list */
|
|
|
|
struct Table *Members; /* members of a struct, union or enum */
|
2009-01-20 19:52:42 -05:00
|
|
|
};
|
|
|
|
|
2009-02-01 06:31:18 -05:00
|
|
|
/* function definition */
|
|
|
|
struct FuncDef
|
|
|
|
{
|
|
|
|
struct ValueType *ReturnType; /* the return value type */
|
|
|
|
int NumParams; /* the number of parameters */
|
|
|
|
struct Typ *ParamType; /* array of parameter types */
|
|
|
|
const char **ParamName; /* array of parameter names */
|
|
|
|
void (*Intrinsic)(); /* intrinsic call address or NULL */
|
|
|
|
struct ParseState Body; /* lexical tokens of the function body if not intrinsic */
|
|
|
|
};
|
|
|
|
|
2009-01-20 19:52:42 -05:00
|
|
|
struct ArrayValue
|
|
|
|
{
|
|
|
|
unsigned int Size; /* the number of elements in the array */
|
|
|
|
void *Data; /* pointer to the array data */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PointerValue
|
2008-11-21 21:56:08 -05:00
|
|
|
{
|
2009-01-20 19:52:42 -05:00
|
|
|
struct Value *Segment; /* array or basic value which this points to, NULL for machine memory access */
|
|
|
|
union s {
|
|
|
|
unsigned int Offset; /* index into an array */
|
|
|
|
void *Memory; /* machine memory pointer for raw memory access */
|
|
|
|
} Data;
|
2008-11-21 21:56:08 -05:00
|
|
|
};
|
|
|
|
|
2008-10-14 07:46:42 -04:00
|
|
|
union AnyValue
|
|
|
|
{
|
2009-01-21 04:02:05 -05:00
|
|
|
unsigned char Character;
|
|
|
|
short ShortInteger;
|
|
|
|
int Integer;
|
|
|
|
double FP;
|
2009-02-01 06:31:18 -05:00
|
|
|
char *String;
|
2009-01-20 19:52:42 -05:00
|
|
|
struct ArrayValue Array;
|
|
|
|
struct PointerValue Pointer;
|
2009-02-01 06:31:18 -05:00
|
|
|
struct ParseState Parser;
|
2009-01-23 06:34:12 -05:00
|
|
|
struct ValueType *Typ;
|
2009-02-01 06:31:18 -05:00
|
|
|
struct FuncDef FuncDef;
|
2008-10-14 07:46:42 -04:00
|
|
|
};
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2008-11-21 21:56:08 -05:00
|
|
|
struct Value
|
|
|
|
{
|
2009-01-21 04:02:05 -05:00
|
|
|
struct ValueType *Typ;
|
2009-01-20 19:52:42 -05:00
|
|
|
union AnyValue *Val;
|
2009-01-26 03:57:32 -05:00
|
|
|
char ValOnHeap;
|
|
|
|
char ValOnStack;
|
2008-11-21 21:56:08 -05:00
|
|
|
};
|
|
|
|
|
2008-12-18 19:22:52 -05:00
|
|
|
/* hash table data structure */
|
|
|
|
struct TableEntry
|
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
const char *Key;
|
2009-01-21 04:02:05 -05:00
|
|
|
struct Value *Val;
|
2009-01-29 17:26:04 -05:00
|
|
|
struct TableEntry *Next;
|
2008-12-18 19:22:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Table
|
|
|
|
{
|
|
|
|
short Size;
|
2009-01-29 17:26:04 -05:00
|
|
|
short OnHeap;
|
|
|
|
struct TableEntry **HashTable;
|
2008-12-18 19:22:52 -05:00
|
|
|
};
|
|
|
|
|
2008-12-21 21:29:19 -05:00
|
|
|
/* stack frame for function calls */
|
|
|
|
struct StackFrame
|
|
|
|
{
|
2009-02-01 06:31:18 -05:00
|
|
|
struct ParseState ReturnParser; /* how we got here */
|
2009-01-29 06:10:46 -05:00
|
|
|
struct Table LocalTable; /* the local variables and parameters */
|
2009-01-29 17:26:04 -05:00
|
|
|
struct TableEntry *LocalHashTable[LOCAL_TABLE_SIZE];
|
2009-01-29 06:10:46 -05:00
|
|
|
struct StackFrame *PreviousStackFrame; /* the next lower stack frame */
|
2008-12-21 21:29:19 -05:00
|
|
|
};
|
|
|
|
|
2008-12-26 23:36:45 -05:00
|
|
|
/* globals */
|
2009-01-23 06:34:12 -05:00
|
|
|
extern struct Table GlobalTable;
|
2009-01-24 21:09:44 -05:00
|
|
|
extern struct Value *Parameter[PARAMETER_MAX];
|
2009-01-03 23:08:49 -05:00
|
|
|
extern int ParameterUsed;
|
2009-01-29 06:10:46 -05:00
|
|
|
extern struct StackFrame *TopStackFrame;
|
2009-01-24 21:09:44 -05:00
|
|
|
extern struct Value *ReturnValue;
|
2009-01-23 06:34:12 -05:00
|
|
|
extern struct ValueType IntType;
|
|
|
|
extern struct ValueType CharType;
|
|
|
|
extern struct ValueType StringType;
|
|
|
|
extern struct ValueType FPType;
|
2009-01-21 04:02:05 -05:00
|
|
|
extern struct ValueType VoidType;
|
|
|
|
extern struct ValueType FunctionType;
|
2009-01-23 06:34:12 -05:00
|
|
|
extern struct ValueType MacroType;
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* picoc.c */
|
2009-02-01 06:31:18 -05:00
|
|
|
void ProgramFail(struct ParseState *Parser, const char *Message, ...);
|
|
|
|
void ScanFile(const char *FileName);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* table.c */
|
2009-01-29 17:26:04 -05:00
|
|
|
void TableInit(struct Table *Tbl, struct TableEntry **HashTable, int Size, int OnHeap);
|
2009-02-01 06:31:18 -05:00
|
|
|
int TableSet(struct Table *Tbl, const char *Key, struct Value *Val);
|
|
|
|
int TableGet(struct Table *Tbl, const char *Key, struct Value **Val);
|
|
|
|
const char *TableSetKey(struct Table *Tbl, const char *Ident, int IdentLen);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* lex.c */
|
2009-02-01 06:31:18 -05:00
|
|
|
void LexInit(struct ParseState *Parser, const char *Source, int SourceLen, const char *FileName, int Line);
|
|
|
|
enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int IncPos);
|
|
|
|
void LexToEndOfLine(struct ParseState *Parser);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* parse.c */
|
|
|
|
void ParseInit(void);
|
2009-02-01 06:31:18 -05:00
|
|
|
int ParseExpression(struct ParseState *Parser, struct Value **Result, int ResultOnHeap, int RunIt);
|
|
|
|
int ParseIntExpression(struct ParseState *Parser, int RunIt);
|
|
|
|
int ParseStatement(struct ParseState *Parser, int RunIt);
|
|
|
|
void Parse(const char *FileName, const char *Source, int SourceLen, int RunIt);
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
/* type.c */
|
|
|
|
void TypeInit();
|
|
|
|
int TypeSizeof(struct ValueType *Typ);
|
2009-02-01 06:31:18 -05:00
|
|
|
void TypeParse(struct ParseState *Parser, struct ValueType **Typ, const char **Identifier);
|
2008-12-26 23:36:45 -05:00
|
|
|
|
|
|
|
/* intrinsic.c */
|
|
|
|
void IntrinsicInit(struct Table *GlobalTable);
|
2009-02-01 06:31:18 -05:00
|
|
|
void IntrinsicGetLexer(struct ParseState *Parser, int IntrinsicId);
|
|
|
|
void IntrinsicCall(struct ParseState *Parser, struct Value *Result, struct ValueType *ReturnType, int IntrinsicId);
|
2009-01-20 19:52:42 -05:00
|
|
|
|
|
|
|
/* heap.c */
|
|
|
|
void HeapInit();
|
|
|
|
void *HeapAllocStack(int Size);
|
2009-01-26 03:57:32 -05:00
|
|
|
int HeapPopStack(void *Addr, int Size);
|
2009-01-20 19:52:42 -05:00
|
|
|
void HeapPushStackFrame();
|
|
|
|
int HeapPopStackFrame();
|
|
|
|
void *HeapAlloc(int Size);
|
|
|
|
void HeapFree(void *Mem);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* variable.c */
|
|
|
|
void VariableInit();
|
2009-02-01 06:31:18 -05:00
|
|
|
void *VariableAlloc(struct ParseState *Parser, int Size, int OnHeap);
|
|
|
|
void VariableStackPop(struct ParseState *Parser, struct Value *Var);
|
|
|
|
struct Value *VariableAllocValueAndData(struct ParseState *Parser, int DataSize, int OnHeap);
|
|
|
|
struct Value *VariableAllocValueAndCopy(struct ParseState *Parser, struct Value *FromValue, int OnHeap);
|
|
|
|
struct Value *VariableAllocValueFromType(struct ParseState *Parser, struct ValueType *Typ, int OnHeap);
|
|
|
|
void VariableDefine(struct ParseState *Parser, const char *Ident, struct Value *InitValue);
|
|
|
|
int VariableDefined(const char *Ident);
|
|
|
|
void VariableGet(struct ParseState *Parser, const char *Ident, struct Value **LVal);
|
|
|
|
void VariableStackFrameAdd(struct ParseState *Parser);
|
|
|
|
void VariableStackFramePop(struct ParseState *Parser);
|
|
|
|
|
|
|
|
/* str.c */
|
|
|
|
void StrInit();
|
|
|
|
const char *StrRegister(const char *Str);
|
|
|
|
const char *StrRegister2(const char *Str, int Len);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
#endif /* PICOC_H */
|