njvm/stackslot.c
2024-01-27 10:25:27 +01:00

67 lines
1.4 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef STACKSLOT
#define STACKSLOT
typedef int Object;
typedef struct ObjRef{
bool brokenHeart;
struct ObjRef *forward_pointer;
unsigned int size;
unsigned char data[1];
} *ObjRef;
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 = malloc(objSize)) == NULL) {
perror("malloc");
}
*(int *) intObject->data = val;
intObject->size = sizeof(int);
return intObject;
}
int getValFromIntObj(ObjRef iref) {
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) {
iref->size = sizeof(int);
*(int *) iref->data = val;
}
StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot));
stackSlot->isObjRef = true;
stackSlot->u.objRef = val;
return *stackSlot;
}
StackSlot stackSlotWitchNumber(int val) {
StackSlot *stackSlot;
stackSlot = malloc(sizeof(StackSlot));
stackSlot->isObjRef = false;
stackSlot->u.number = val;
return *stackSlot;
}
#endif