-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtime.c
79 lines (70 loc) · 1.36 KB
/
runtime.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#define num_shift 2
#define num_mask 0b11
#define num_tag 0b00
#define bool_shift 7
#define bool_mask 0b1111111
#define bool_tag 0b0011111
#define heap_mask 0b111
#define pair_tag 0b010
#define fn_tag 0b110
extern int64_t entry(void *heap);
void error()
{
printf("ERROR");
exit(1);
}
uint64_t read_num()
{
int r = 0;
scanf("%d", &r);
return r << num_shift;
}
void print_newline()
{
printf("\n");
}
void print_value(uint64_t value)
{
if ((value & num_mask) == num_tag)
{
int64_t ivalue = (int64_t)value;
printf("%" PRIi64, ivalue >> num_shift);
}
else if ((value & bool_mask) == bool_tag)
{
if (value >> bool_shift)
{
printf("true");
}
else
{
printf("false");
}
}
else if ((value & heap_mask) == pair_tag)
{
uint64_t v1 = *(uint64_t *)(value - pair_tag);
uint64_t v2 = *(uint64_t *)(value - pair_tag + 8);
printf("(pair ");
print_value(v1);
printf(" ");
print_value(v2);
printf(")");
}
else if ((value & heap_mask) == fn_tag)
{
printf("<function>");
}
else
{
printf("BAD VALUE %" PRIu64, value);
}
}
int main(int argc, char **argv)
{
entry((void *)malloc(4096));
return 0;
}