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 */
|
2009-01-04 23:28:54 -05:00
|
|
|
#define GLOBAL_TABLE_SIZE 397 /* global variable table */
|
|
|
|
#define FUNCTION_STORE_MAX 200 /* maximum number of used-defined functions and macros */
|
2008-12-21 21:29:19 -05:00
|
|
|
#define STACK_MAX 10 /* maximum function call stack depth */
|
2008-12-23 21:30:29 -05:00
|
|
|
#define PARAMETER_MAX 10 /* maximum number of parameters to a function */
|
2008-12-21 21:29:19 -05:00
|
|
|
#define LOCAL_TABLE_SIZE 11 /* maximum number of local variables */
|
2009-01-23 06:34:12 -05:00
|
|
|
#define STRUCT_TABLE_SIZE 11 /* maximum number of struct/union members */
|
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
|
|
|
|
|
|
|
/* 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
|
|
|
};
|
|
|
|
|
|
|
|
/* string type so we can use source file strings */
|
|
|
|
typedef struct _Str
|
|
|
|
{
|
|
|
|
int Len;
|
|
|
|
const char *Str;
|
|
|
|
} Str;
|
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
/* lexer state - so we can lex nested files */
|
|
|
|
struct LexState
|
|
|
|
{
|
|
|
|
int Line;
|
|
|
|
const char *Pos;
|
|
|
|
const char *End;
|
|
|
|
const Str *FileName;
|
|
|
|
};
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
/* function definition - really just where it is in the source file */
|
|
|
|
struct FuncDef
|
|
|
|
{
|
|
|
|
Str Source;
|
|
|
|
Str FileName;
|
|
|
|
int StartLine;
|
|
|
|
};
|
|
|
|
|
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) */
|
|
|
|
};
|
|
|
|
|
|
|
|
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-01-23 22:15:02 -05:00
|
|
|
Str 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
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2008-10-14 07:46:42 -04:00
|
|
|
Str String;
|
2009-01-20 19:52:42 -05:00
|
|
|
struct ArrayValue Array;
|
|
|
|
struct PointerValue Pointer;
|
2009-01-23 06:34:12 -05:00
|
|
|
struct LexState Lexer;
|
|
|
|
struct ValueType *Typ;
|
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
|
|
|
|
{
|
|
|
|
Str 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-01-29 06:10:46 -05:00
|
|
|
struct LexState ReturnLex; /* how we got here */
|
|
|
|
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;
|
2009-01-24 21:09:44 -05:00
|
|
|
extern Str StrEmpty;
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* str.c */
|
|
|
|
void StrToC(char *Dest, int DestSize, const Str *Source);
|
|
|
|
void StrFromC(Str *Dest, const char *Source);
|
|
|
|
int StrEqual(const Str *Str1, const Str *Str2);
|
|
|
|
int StrEqualC(const Str *Str1, const char *Str2);
|
2008-10-13 06:53:25 -04:00
|
|
|
void StrPrintf(const char *Format, ...);
|
2008-10-14 07:18:43 -04:00
|
|
|
void vStrPrintf(const char *Format, va_list Args);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* picoc.c */
|
|
|
|
void Fail(const char *Message, ...);
|
2008-11-21 21:56:08 -05:00
|
|
|
void ProgramFail(struct LexState *Lexer, const char *Message, ...);
|
2008-10-12 20:53:28 -04:00
|
|
|
void ScanFile(const Str *FileName);
|
|
|
|
|
|
|
|
/* table.c */
|
2009-01-29 17:26:04 -05:00
|
|
|
void TableInit(struct Table *Tbl, struct TableEntry **HashTable, int Size, int OnHeap);
|
2008-12-20 22:13:25 -05:00
|
|
|
int TableSet(struct Table *Tbl, const Str *Key, struct Value *Val);
|
2008-12-18 19:22:52 -05:00
|
|
|
int TableGet(struct Table *Tbl, const Str *Key, struct Value **Val);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* lex.c */
|
2008-10-13 06:53:25 -04:00
|
|
|
void LexInit(struct LexState *Lexer, const Str *Source, const Str *FileName, int Line);
|
2009-01-26 03:57:32 -05:00
|
|
|
enum LexToken LexGetToken(struct LexState *Lexer, struct Value **Value);
|
2008-12-22 06:52:31 -05:00
|
|
|
enum LexToken LexGetPlainToken(struct LexState *Lexer);
|
2009-01-26 03:57:32 -05:00
|
|
|
enum LexToken LexPeekToken(struct LexState *Lexer, struct Value **Value);
|
2008-12-22 06:52:31 -05:00
|
|
|
enum LexToken LexPeekPlainToken(struct LexState *Lexer);
|
2009-01-03 23:08:49 -05:00
|
|
|
void LexToEndOfLine(struct LexState *Lexer);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
/* parse.c */
|
|
|
|
void ParseInit(void);
|
2009-01-26 03:57:32 -05:00
|
|
|
int ParseExpression(struct LexState *Lexer, struct Value **Result, int ResultOnHeap, int RunIt);
|
|
|
|
int ParseIntExpression(struct LexState *Lexer, int RunIt);
|
2008-12-20 05:24:34 -05:00
|
|
|
void Parse(const Str *FileName, const Str *Source, int RunIt);
|
2009-01-23 06:34:12 -05:00
|
|
|
|
|
|
|
/* type.c */
|
|
|
|
void TypeInit();
|
|
|
|
int TypeSizeof(struct ValueType *Typ);
|
2009-01-23 22:15:02 -05:00
|
|
|
void TypeParse(struct LexState *Lexer, struct ValueType **Typ, Str *Identifier);
|
2008-12-26 23:36:45 -05:00
|
|
|
|
|
|
|
/* intrinsic.c */
|
|
|
|
void IntrinsicInit(struct Table *GlobalTable);
|
|
|
|
void IntrinsicGetLexer(struct LexState *Lexer, int IntrinsicId);
|
2009-01-21 04:02:05 -05:00
|
|
|
void IntrinsicCall(struct LexState *Lexer, 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-01-26 03:57:32 -05:00
|
|
|
void *VariableAlloc(struct LexState *Lexer, int Size, int OnHeap);
|
|
|
|
void VariableStackPop(struct LexState *Lexer, struct Value *Var);
|
|
|
|
struct Value *VariableAllocValueAndData(struct LexState *Lexer, int DataSize, int OnHeap);
|
|
|
|
struct Value *VariableAllocValueAndCopy(struct LexState *Lexer, struct Value *FromValue, int OnHeap);
|
|
|
|
struct Value *VariableAllocValueFromType(struct LexState *Lexer, struct ValueType *Typ, int OnHeap);
|
2009-01-23 06:34:12 -05:00
|
|
|
void VariableDefine(struct LexState *Lexer, const Str *Ident, struct Value *InitValue);
|
|
|
|
int VariableDefined(Str *Ident);
|
2009-01-26 03:57:32 -05:00
|
|
|
void VariableGet(struct LexState *Lexer, Str *Ident, struct Value **LVal);
|
2009-01-23 06:34:12 -05:00
|
|
|
void VariableStackFrameAdd(struct LexState *Lexer);
|
2009-01-26 03:57:32 -05:00
|
|
|
void VariableStackFramePop(struct LexState *Lexer);
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2009-01-23 06:34:12 -05:00
|
|
|
#endif /* PICOC_H */
|