summaryrefslogtreecommitdiffstats
path: root/bytecode/value.c
blob: 1b18a964c275edc2ee40d4985c85d727ef2a05e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdbool.h>
#include <stdint.h>

#include "panic.h"

#include "value.h"

bool is_int(value v)
{
	return v & 0x1;
}

int64_t to_int(value v)
{
	if (!is_int(v)) {
		panicf("value 0x%lx is not an int!\n", v);
	}
	return (int64_t) v >> 1;
}

value from_int(int64_t i)
{
	return i << 1 | 0x1;
}

bool is_pointer(value v)
{
	return v && !(v & 0x7);
}

value *to_pointer(value v)
{
	if (!is_pointer(v)) {
		panicf("value 0x%lx is not a pointer!\n", v);
	}
	return (value *) v;
}

value from_pointer(value *p)
{
	return (value) p;
}