diff --git a/Makefile b/Makefile index 3fb2e7c..498ab38 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -pedantic -g -DUNIX_HOST -DDEBUG_HEAP +CFLAGS=-Wall -pedantic -g -DUNIX_HOST LIBS=#-lm TARGET = picoc diff --git a/expression.c b/expression.c index f36bcf1..56c445a 100644 --- a/expression.c +++ b/expression.c @@ -258,11 +258,15 @@ void ExpressionStackPushLValue(struct ParseState *Parser, struct ExpressionStack void ExpressionStackPushDereference(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *DereferenceValue) { struct Value *DerefVal; + struct Value *ValueLoc; int Offset; struct ValueType *DerefType; int DerefIsLValue; void *DerefDataLoc = VariableDereferencePointer(Parser, DereferenceValue, &DerefVal, &Offset, &DerefType, &DerefIsLValue); - struct Value *ValueLoc = VariableAllocValueFromExistingData(Parser, DerefType, (union AnyValue *)DerefDataLoc, DerefIsLValue, DerefVal); + if (DerefDataLoc == NULL) + ProgramFail(Parser, "NULL pointer dereference"); + + ValueLoc = VariableAllocValueFromExistingData(Parser, DerefType, (union AnyValue *)DerefDataLoc, DerefIsLValue, DerefVal); ExpressionStackPushValueNode(Parser, StackTop, ValueLoc); } @@ -372,6 +376,7 @@ void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue) { struct Value *Result; + union AnyValue *ValPtr; if (Parser->Mode != RunModeRun) { @@ -387,8 +392,9 @@ void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack if (!TopValue->IsLValue) ProgramFail(Parser, "can't get the address of this"); + ValPtr = TopValue->Val; Result = VariableAllocValueFromType(Parser, TypeGetMatching(Parser, TopValue->Typ, TypePointer, 0, StrEmpty), FALSE, NULL, FALSE); - Result->Val->NativePointer = TopValue->Val; + Result->Val->NativePointer = (void *)ValPtr; ExpressionStackPushValueNode(Parser, StackTop, Result); break; @@ -762,7 +768,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * TopValue = TopStackNode->Val; /* pop the value and then the prefix operator - assume they'll still be there until we're done */ - HeapPopStack(TopOperatorNode, sizeof(struct ExpressionStack)*2 + sizeof(struct Value) + TypeStackSizeValue(TopValue)); + HeapPopStack(NULL, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue)); + HeapPopStack(TopOperatorNode, sizeof(struct ExpressionStack)); *StackTop = TopOperatorNode->Next; /* do the prefix operation */ @@ -775,7 +782,8 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * TopValue = TopStackNode->Next->Val; /* pop the postfix operator and then the value - assume they'll still be there until we're done */ - HeapPopStack(TopValue, sizeof(struct ExpressionStack)*2 + sizeof(struct Value) + TypeStackSizeValue(TopValue)); + HeapPopStack(NULL, sizeof(struct ExpressionStack)); + HeapPopStack(TopValue, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue)); *StackTop = TopStackNode->Next->Next; /* do the postfix operation */ @@ -791,7 +799,9 @@ void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack * BottomValue = TopOperatorNode->Next->Val; /* pop a value, the operator and another value - assume they'll still be there until we're done */ - HeapPopStack(BottomValue, sizeof(struct ExpressionStack)*3 + sizeof(struct Value)*2 + TypeStackSizeValue(TopValue) + TypeStackSizeValue(BottomValue)); + HeapPopStack(NULL, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue)); + HeapPopStack(NULL, sizeof(struct ExpressionStack)); + HeapPopStack(BottomValue, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(BottomValue)); *StackTop = TopOperatorNode->Next->Next; /* do the infix operation */ diff --git a/heap.c b/heap.c index 5b907b7..5658908 100644 --- a/heap.c +++ b/heap.c @@ -86,7 +86,7 @@ int HeapPopStack(void *Addr, int Size) printf("HeapPopStack(0x%lx, %ld) back to 0x%lx\n", (unsigned long)Addr, (unsigned long)MEM_ALIGN(Size), (unsigned long)HeapStackTop - ToLose); #endif HeapStackTop = (void *)((char *)HeapStackTop - ToLose); - assert(HeapStackTop == Addr); + assert(Addr == NULL || HeapStackTop == Addr); return TRUE; } @@ -99,7 +99,7 @@ void HeapPushStackFrame() #endif *(void **)HeapStackTop = StackFrame; StackFrame = HeapStackTop; - HeapStackTop = (void *)((char *)HeapStackTop + sizeof(void *)); + HeapStackTop = (void *)((char *)HeapStackTop + MEM_ALIGN(sizeof(void *))); } /* pop the current stack frame, freeing all memory in the frame. can return NULL */ @@ -219,7 +219,7 @@ void HeapFreeMem(void *Mem) int Bucket = MemNode->Size >> 2; #ifdef DEBUG_HEAP - printf("HeapFreeMem(%lx)\n", (unsigned long)Mem); + printf("HeapFreeMem(0x%lx)\n", (unsigned long)Mem); #endif assert((unsigned long)Mem >= (unsigned long)&HeapMemory[0] && (unsigned char *)Mem - &HeapMemory[0] < HEAP_SIZE); assert(MemNode->Size < HEAP_SIZE && MemNode->Size > 0); diff --git a/type.c b/type.c index 84eef58..ea87491 100644 --- a/type.c +++ b/type.c @@ -73,7 +73,7 @@ int TypeStackSizeValue(struct Value *Val) int TypeSizeValue(struct Value *Val) { if (Val->Typ->Base == TypeChar || Val->Typ->Base == TypeShort) - return sizeof(int); /* allow some extra room for type extension to int */ + return sizeof(void *); /* allow some extra room for type extension */ else if (Val->Typ->Base != TypeArray) return Val->Typ->Sizeof; else @@ -84,7 +84,7 @@ int TypeSizeValue(struct Value *Val) int TypeSize(struct ValueType *Typ, int ArraySize, int Compact) { if ( (Typ->Base == TypeChar || Typ->Base == TypeShort) && !Compact) - return sizeof(int); /* allow some extra room for type extension to int */ + return sizeof(void *); /* allow some extra room for type extension */ else if (Typ->Base != TypeArray) return Typ->Sizeof; else