-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.c
186 lines (162 loc) · 4.4 KB
/
prelude.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#define MAX_RECURSION 1024
#define MAX_HEAP (64*1024*1024)
#define MAX_STACK (64*1024*1024)
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#define DATA_START UINT64_C(0x2000000000000000)
#define STACK_START UINT64_C(0x1000000000000000)
#define HEAP_START UINT64_C(0x0000000000000000)
#define IO_ADDR UINT64_C(0xffffffffffffffff)
struct registers
{
uint64_t a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
};
struct registers call_stack_regs[MAX_RECURSION];
void *call_stack_returns[MAX_RECURSION];
size_t call_stack_i = 0;
struct registers regs;
uint8_t *stack, *heap;
%%FILLDATA%%
uint64_t cycles = 0;
void init()
{
memset(®s, 0, sizeof(regs));
regs.z = UINT64_C(0x1000000000000000);
stack = (uint8_t *)malloc(MAX_STACK);
heap = (uint8_t *)malloc(MAX_HEAP);
}
void halt(int a)
{
fprintf(stderr, "Ran for %"PRIu64" cycles\n", cycles);
exit(a);
}
static inline void mult64to128(uint64_t op1, uint64_t op2, uint64_t *hi, uint64_t *lo)
{
uint64_t u1 = (op1 & 0xffffffff);
uint64_t v1 = (op2 & 0xffffffff);
uint64_t t = (u1 * v1);
uint64_t w3 = (t & 0xffffffff);
uint64_t k = (t >> 32);
op1 >>= 32;
t = (op1 * v1) + k;
k = (t & 0xffffffff);
uint64_t w1 = (t >> 32);
op2 >>= 32;
t = (u1 * op2) + k;
k = (t >> 32);
*hi = (op1 * op2) + w1 + k;
*lo = (t << 32) + w3;
}
static inline void store(uint64_t address, uint64_t value, int width)
{
if (address == IO_ADDR)
{
if (width != 8)
{
fprintf(stderr, "May only use lw/sw for stdin/stdout");
exit(1);
}
putchar((char)(value & 0xff));
fflush(stdout);
return;
}
uint8_t *storep;
if (unlikely(address >= DATA_START))
{
fprintf(stderr, "Attempt to store in read-only data section");
exit(1);
}
else if (address >= STACK_START)
{
address -= STACK_START;
if (unlikely(address+width >= MAX_STACK))
{
fprintf(stderr, "Store outside of stack");
exit(1);
}
storep = &stack[address];
}
else
{
if (unlikely(address+width >= MAX_HEAP))
{
fprintf(stderr, "Store outside of heap");
exit(1);
}
storep = &heap[address];
}
switch (width) {
case 8: *(storep+7) = (uint8_t)(value >> 56);
*(storep+6) = (uint8_t)(value >> 48);
*(storep+5) = (uint8_t)(value >> 40);
*(storep+4) = (uint8_t)(value >> 32);
case 4: *(storep+3) = (uint8_t)(value >> 24);
*(storep+2) = (uint8_t)(value >> 16);
case 2: *(storep+1) = (uint8_t)(value >> 8);
case 1: *(storep ) = (uint8_t)(value );
}
}
static inline uint64_t load(uint64_t address, int width)
{
if (address == IO_ADDR)
{
if (width != 8)
{
fprintf(stderr, "May only use lw/sw for stdin/stdout");
exit(1);
}
return getchar();
}
uint8_t *loadp;
if (address >= DATA_START)
{
address -= DATA_START;
if (unlikely(address+width >= DATA_LEN))
{
fprintf(stderr, "Load outside of data");
exit(1);
}
loadp = &data[address];
}
else if (address >= STACK_START)
{
address -= STACK_START;
if (unlikely(address+width >= MAX_STACK))
{
fprintf(stderr, "Load outside of stack");
exit(1);
}
loadp = &stack[address];
}
else
{
if (unlikely(address+width >= MAX_HEAP))
{
fprintf(stderr, "Load outside of heap");
exit(1);
}
loadp = &heap[address];
}
uint64_t result = 0;
switch (width) {
case 8: result += (uint64_t)(*(loadp+7)) << 56;
result += (uint64_t)(*(loadp+6)) << 48;
result += (uint64_t)(*(loadp+5)) << 40;
result += (uint64_t)(*(loadp+4)) << 32;
case 4: result += (uint64_t)(*(loadp+3)) << 24;
result += (uint64_t)(*(loadp+2)) << 16;
case 2: result += (uint64_t)(*(loadp+1)) << 8;
case 1: result += (uint64_t)(*(loadp )) ;
}
return result;
}