njvm/stackslot.c
nilspolek ba9d0b77b9 gc
2024-01-28 17:56:12 +01:00

68 lines
1.4 KiB
C

#ifndef STACKSLOT
#define STACKSLOT
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "objref.c"
#include "njvm.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 = alloc(objSize)) == NULL) {
perror("alloc");
}
*(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 = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = true;
stackSlot->u.objRef = val;
return *stackSlot;
}
StackSlot stackSlotWitchNumber(int val) {
StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = false;
stackSlot->u.number = val;
return *stackSlot;
}
#endif