68 lines
1.4 KiB
C
68 lines
1.4 KiB
C
#ifndef STACKSLOT
|
|
#define STACKSLOT
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "objref.c"
|
|
#include "heap.h"
|
|
|
|
typedef int Object;
|
|
|
|
|
|
typedef struct {
|
|
bool isObjRef;
|
|
union {
|
|
ObjRef objRef;
|
|
int number;
|
|
} u;
|
|
} StackSlot;
|
|
|
|
ObjRef getIntObj(int val) {
|
|
ObjRef intObject;
|
|
unsigned int objSize = sizeof(ObjRef) + sizeof(int);
|
|
if ((intObject = my_malloc(objSize)) == NULL) {
|
|
perror("malloc");
|
|
}
|
|
*(int *) intObject->data = val;
|
|
intObject->size = objSize;
|
|
return intObject;
|
|
}
|
|
|
|
int getValFromIntObj(ObjRef iref) {
|
|
if (iref == NULL) perror("ObjRef is null");
|
|
return *(int *) iref->data;
|
|
}
|
|
|
|
int getIntValfromStackSlot(StackSlot s) {
|
|
if (s.isObjRef) {
|
|
return *(int *) s.u.objRef->data;
|
|
}
|
|
return s.u.number;
|
|
}
|
|
|
|
void setValIntObj(ObjRef iref, int val) {
|
|
if (iref == NULL) perror("ObjRef is null");
|
|
iref->size = sizeof(int)+sizeof(ObjRef);
|
|
*(int *) iref->data = val;
|
|
}
|
|
|
|
StackSlot stackSlotWithObjRef(ObjRef val) {
|
|
StackSlot *stackSlot;
|
|
stackSlot = my_malloc(sizeof(StackSlot));
|
|
if(stackSlot == NULL) perror("malloc");
|
|
stackSlot->isObjRef = true;
|
|
stackSlot->u.objRef = val;
|
|
return *stackSlot;
|
|
}
|
|
|
|
StackSlot stackSlotWitchNumber(int val) {
|
|
StackSlot *stackSlot;
|
|
stackSlot = my_malloc(sizeof(StackSlot));
|
|
if(stackSlot == NULL) perror("malloc");
|
|
stackSlot->isObjRef = false;
|
|
stackSlot->u.number = val;
|
|
return *stackSlot;
|
|
}
|
|
|
|
#endif
|