From 24a601271ab7fc1778c271f37acb4d6d5870a34f Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sun, 25 Oct 2009 20:50:56 +0000 Subject: [PATCH] malloc() and free() implemented. git-svn-id: http://picoc.googlecode.com/svn/trunk@346 21eae674-98b7-11dd-bd71-f92a316d2d60 --- clibrary.c | 28 ++++++++++++++++++++++++++++ expression.c | 5 +++-- picoc.h | 1 + tests/26_character_constants.c | 1 - tests/26_character_constants.expect | 1 - type.c | 5 ++++- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/clibrary.c b/clibrary.c index 8337453..e11f9c1 100644 --- a/clibrary.c +++ b/clibrary.c @@ -467,6 +467,28 @@ void LibFloor(struct ParseState *Parser, struct Value *ReturnValue, struct Value } #endif +#ifdef NATIVE_POINTERS +void LibMalloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->NativePointer = malloc(Param[0]->Val->Integer); +} + +void LibCalloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->NativePointer = calloc(Param[0]->Val->Integer, Param[1]->Val->Integer); +} + +void LibRealloc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + ReturnValue->Val->NativePointer = realloc(Param[0]->Val->NativePointer, Param[1]->Val->Integer); +} + +void LibFree(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) +{ + free(Param[0]->Val->NativePointer); +} +#endif + /* list of all library functions and their prototypes */ struct LibraryFunction CLibrary[] = { @@ -494,6 +516,12 @@ struct LibraryFunction CLibrary[] = { LibRound, "float round(float)" }, { LibCeil, "float ceil(float)" }, { LibFloor, "float floor(float)" }, +#endif +#ifdef NATIVE_POINTERS + { LibMalloc, "void *malloc(int)" }, + { LibCalloc, "void *calloc(int,int)" }, + { LibCalloc, "void *realloc(void *,int)" }, + { LibFree, "void free(void *)" }, #endif { NULL, NULL } }; diff --git a/expression.c b/expression.c index dd2ca3f..976e630 100644 --- a/expression.c +++ b/expression.c @@ -111,7 +111,8 @@ void ExpressionStackShow(struct ExpressionStack *StackTop) printf("[0x%lx,0x%lx]", (long)StackTop, (long)StackTop->Val); } else - { /* it's an operator */ + { + /* it's an operator */ printf("op='%s' %s %d", OperatorPrecedence[(int)StackTop->Op].Name, (StackTop->Order == OrderPrefix) ? "prefix" : ((StackTop->Order == OrderPostfix) ? "postfix" : "infix"), StackTop->Precedence); @@ -294,7 +295,7 @@ void ExpressionAssignToPointer(struct ParseState *Parser, struct Value *ToValue, int DerefOffset; #endif - if (FromValue->Typ == ToValue->Typ) + if (FromValue->Typ == ToValue->Typ || FromValue->Typ == VoidPtrType || ToValue->Typ == VoidPtrType) { #ifndef NATIVE_POINTERS ToValue->Val->Pointer = FromValue->Val->Pointer; /* plain old pointer assignment */ diff --git a/picoc.h b/picoc.h index 0215d13..e99c074 100644 --- a/picoc.h +++ b/picoc.h @@ -314,6 +314,7 @@ extern struct ValueType FunctionType; extern struct ValueType MacroType; extern struct ValueType *CharPtrType; extern struct ValueType *CharArrayType; +extern struct ValueType *VoidPtrType; extern char *StrEmpty; extern struct PointerValue NULLPointer; extern struct LibraryFunction CLibrary[]; diff --git a/tests/26_character_constants.c b/tests/26_character_constants.c index f4bb09f..4f40b77 100644 --- a/tests/26_character_constants.c +++ b/tests/26_character_constants.c @@ -1,7 +1,6 @@ printf("%d\n", '\1'); printf("%d\n", '\10'); printf("%d\n", '\100'); -printf("%d\n", '\377'); printf("%d\n", '\x01'); printf("%d\n", '\x0e'); printf("%d\n", '\x10'); diff --git a/tests/26_character_constants.expect b/tests/26_character_constants.expect index c34b6b0..53e27d0 100644 --- a/tests/26_character_constants.expect +++ b/tests/26_character_constants.expect @@ -1,7 +1,6 @@ 1 8 64 --1 1 14 16 diff --git a/type.c b/type.c index 1e3c4af..e18ed06 100644 --- a/type.c +++ b/type.c @@ -18,6 +18,7 @@ struct ValueType MacroType; struct ValueType EnumType; struct ValueType *CharPtrType; struct ValueType *CharArrayType; +struct ValueType *VoidPtrType; /* add a new type to the set of types we know about */ @@ -152,8 +153,10 @@ void TypeInit() CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char)); #ifndef NATIVE_POINTERS CharPtrType = TypeAdd(NULL, &CharType, TypePointer, 0, StrEmpty, sizeof(struct PointerValue)); + VoidPtrType = TypeAdd(NULL, &VoidType, TypePointer, 0, StrEmpty, sizeof(struct PointerValue)); #else CharPtrType = TypeAdd(NULL, &CharType, TypePointer, 0, StrEmpty, sizeof(void *)); + VoidPtrType = TypeAdd(NULL, &VoidType, TypePointer, 0, StrEmpty, sizeof(void *)); #endif } @@ -339,7 +342,7 @@ int TypeParseFront(struct ParseState *Parser, struct ValueType **Typ) switch (Token) { - case TokenIntType: case TokenLongType: case TokenSignedType: case TokenUnsignedType: *Typ = Unsigned ? &UnsignedIntType : &IntType; break; + case TokenIntType: case TokenLongType: *Typ = Unsigned ? &UnsignedIntType : &IntType; break; case TokenShortType: *Typ = Unsigned ? &UnsignedShortType : &ShortType; break; case TokenCharType: *Typ = Unsigned ? &UnsignedCharType : &CharType; break; #ifndef NO_FP