malloc() and free() implemented.

git-svn-id: http://picoc.googlecode.com/svn/trunk@346 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-10-25 20:50:56 +00:00
parent c6d483ee6f
commit 24a601271a
6 changed files with 36 additions and 5 deletions

View file

@ -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 }
};

View file

@ -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 */

View file

@ -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[];

View file

@ -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');

View file

@ -1,7 +1,6 @@
1
8
64
-1
1
14
16

5
type.c
View file

@ -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