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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// clang-format off
// COMPILE TIME FLAGS EXPLANATION
// - NO_MMAP Prevents usage of mmap+hugepages, which is only supported on linux
// - NO_ASM Prevents usage of inline assembly, useful for non-x86 platforms
// - NO_INTRINSICS Disables compiler intrinsics
// - BAD_CLOCK Use if your system clock does not support nanoseconds. Uses microseconds instead.
// - OLD_CLANG Use if your compiler version is not very up-to-date.
// - SPEED_IMPROVEMENT Almost doubles clock rate for the VM with a few optimizations.
// - EXTENDED_INSTRUCTION_SET Adds a few instructions which decrease token throughput for ir2. The high-level IR emitter will not work otherwise.
// - TIME_SLICE_SUPPORT Adds support for time slices, a way for the emulation layer to manage how long a pu2 program runs for. Only supported in the speed optimized version.
// clang-format on
/* -- BEGIN COMPILE TIME FLAGS -- */
// #define PU2_NO_MMAP
// #define PU2_NO_ASM
// #define PU2_NO_INTRINSICS
// #define PU2_BAD_CLOCK
// #define PU2_OLD_CLANG
#define PU2_SPEED_IMPROVEMENT
#define PU2_EXTENDED_INSTRUCTION_SET
#define PU2_TIME_SLICE_SUPPORT
/* -- END COMPILE TIME FLAGS --*/
#ifdef PU2_NO_ASM
#warning Enabling this flag will invalidate all clock speed and cycle count measurements (thus, your CPU clock rate will be reported as 0MHz)
#endif
#define _GNU_SOURCE
#include <ctype.h>
#include <limits.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#ifndef PU2_NO_MMAP
#include <sys/mman.h>
#endif
#pragma clang diagnostic ignored "-Wparentheses"
#pragma clang diagnostic ignored "-Wreturn-type"
#pragma clang diagnostic ignored "-Wempty-body"
// pu2!
// maybe compacter? idk
#define A 8
// change this for error reporting on unknown instructions!
// examples:
// #define A __builtin_trap()
// ^ does SIGTRAP
// #define A my_custom_cpu_error_trap()
// ^ calls my_custom_cpu_error_trap()
#define S (1 << 25)
#define J while
#define H !--c
#ifndef PU2_SPEED_IMPROVEMENT
#ifdef PU2_TIME_SLICE_SUPPORT
#error Time slices are not supported in the basic, codegolfed versions. Please remove this flag before compilation.
#endif
#ifndef PU2_EXTENDED_INSTRUCTION_SET
// clang-format off
m[S],c,v,a,b,u;pu2(){J(c=m[m[7]++],v=m[m[7]++],c){H?a=v:H?a=m[b]:H?u?m[a]=b:0:H?v=a,a=b,b=v:H?a+=b:H?a-=b:H?a&=b:H?a=!a:H?u=v:H?u=a:H?a=u:H?u=b:H?b=u:A;}}
// clang-format on
#else
// clang-format off
m[S],c,v,a,b,u;pu2(){J(c=m[m[7]++],v=m[m[7]++],c){H?a=v:H?a=m[b]:H?u?m[a]=b:0:H?v=a,a=b,b=v:H?a+=b:H?a-=b:H?a&=b:H?a=!a:H?u=v:H?u=a:H?a=u:H?u=b:H?b=u:H?m[0]+=v*2:H?m[v]=m[a]:H?a=m[v]:H?m[a]=v:H?m[a]=m[v]:A;}}
// clang-format on
#endif
#else
#include <stdio.h>
#undef A
#define A \
({ \
printf("ERROR: Invalid instruction!\n"); \
exit(1); \
})
// not implemented yet, was lazy
// bake(int start, int end) {}
#ifndef PU2_NO_INTRINSICS
#define unlikely(JJ) __builtin_expect((JJ), 0)
#define likely(JJ) __builtin_expect((JJ), 1)
#else
#define unlikely(JJ) (JJ)
#define likely(JJ) (JJ)
#endif
// __attribute__((aligned(64))) int *m;
typedef struct {
int c;
int v;
} vminst;
const int hugepage_size = 2 * 1024 * 1024;
#ifndef PU2_NO_ASM
// unused
void set_affinity(int cpu) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) != 0) { perror("sched_setaffinity failed"); }
}
#endif
int *init_pu2() {
// set_affinity(0);
int *m;
#ifndef PU2_NO_MMAP
const int S_val = S * sizeof(int);
int page_count = (S_val / hugepage_size) + 1;
if (page_count < 1) page_count = 1;
const int page_size = page_count * hugepage_size;
m = mmap(
NULL,
page_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGE_2MB | MAP_HUGETLB,
-1,
0);
if (m == MAP_FAILED) {
perror("[warn] couldn't allocate pu2 memory");
printf(
" ( hint: enable hugepages in your kernel settings\n"
" `echo %i | sudo tee /proc/sys/vm/nr_hugepages` )",
page_count);
exit(1);
}
#else
m = calloc(S, sizeof(int));
if (m == NULL) {
perror("[error] failed to allocate pu2 memory");
exit(1);
}
#endif
// size_t i;
// for (i = 0; i < page_count; i++) ((char *) m)[ i * hugepage_size ] = 0;
return m;
}
#define asm(...) __asm__(__VA_ARGS__)
__attribute__((hot, aligned(64)))
#ifdef PU2_TIME_SLICE_SUPPORT
pu2(int __time_slice, int *m, int mem_size)
#else
pu2(int *m, int mem_size)
#endif
{
#ifdef PU2_TIME_SLICE_SUPPORT
TIME_SLICE_AGAIN:;
int time_slice = __time_slice != -1 ?: INT_MAX;
#endif
int a = 0, b = 0, c = 0, u = 0;
vminst *M2 = (vminst *) m;
vminst *M = &M2[ m[ 7 ] >> 1 ];
vminst *M_END = M2 + mem_size;
m[ 8 ] = mem_size;
int s1 = 0, s2 = 0, s3 = 0;
static const __attribute__((aligned(64))) void *PRECOMPUTED_TABLE[]
= { &&PU2_END, &&C_ONE, &&C_TWO, &&C_THREE, &&C_FOUR,
&&C_FIVE, &&C_SIX, &&C_SEVEN, &&C_EIGHT, &&C_NINE,
&&C_TEN, &&C_ELEVEN, &&C_TWELVE, &&C_THIRTEEN, &&CB_FOURTEEN,
&&CB_FIFTEEN, &&CB_SIXTEEN, &&CB_SEVENTEEN, &&CB_EIGHTEEN, [32] = 0 };
static vminst inst __asm__("r21");
#ifdef PU2_TIME_SLICE_SUPPORT
static int slice_i;
for (slice_i = 0; slice_i < time_slice; slice_i++)
#else
while (1)
#endif
{
PU2_START:
if (unlikely(M2 >= M_END)) return 0;
inst = *M++;
// printf("running [%li] [%i] [%i]\n", M - M2, inst.c, inst.v);
// if (unlikely(inst.c > 18)) A;
#ifndef PU2_NO_INTRINSICS
__builtin_prefetch(M + 1, 0, 3);
#endif
goto *PRECOMPUTED_TABLE[ inst.c ];
// C_ZERO:
// goto PU2_END;
C_ONE:
#ifndef PU2_NO_ASM
asm("mov %1, %0" : "=r"(a) : "r"(inst.v));
#else
a = inst.v;
#endif
goto PU2_START;
C_TWO:
#ifndef PU2_NO_ASM
asm("mov %1, %0" : "=r"(a) : "r"(m[b]));
#else
a = m[ b ];
#endif
goto PU2_START;
C_THREE:
// if (unlikely(a == 7)) printf("conditional jmp %i (%i) [a=%i b=%i u=%i]\n", M - M2, u, a,
// b, u);
if (likely(u)) {
if (unlikely(a == 7)) M = M2 + (b >> 1);
m[ a ] = b;
}
goto PU2_START;
C_FOUR:
#ifndef PU2_NO_ASM
asm("xchg %0, %1" : "+r"(a), "+r"(b));
#else
s1 = a;
a = b;
b = s1;
#endif
goto PU2_START;
C_FIVE:
a += b;
goto PU2_START;
C_SIX:
a -= b;
goto PU2_START;
C_SEVEN:
a &= b;
goto PU2_START;
C_EIGHT:
a = !a;
goto PU2_START;
C_NINE:
u = inst.v;
goto PU2_START;
C_TEN:
u = a;
goto PU2_START;
C_ELEVEN:
a = u;
goto PU2_START;
C_TWELVE:
u = b;
goto PU2_START;
C_THIRTEEN:
b = u;
goto PU2_START;
CB_FOURTEEN:
// nop
// this instruction allows for essentially embedding data inside a program, as pu2 will skip
// right over it
M += inst.v;
goto PU2_START;
CB_FIFTEEN:
// if (unlikely(inst.v == 7)) M = M2 + (m[a] >> 1);
m[ inst.v ] = a;
// asm("mov %1, %0" : "=r"(m[inst.v]) : "r"(a));
goto PU2_START;
CB_SIXTEEN:
// asm("mov %1, %0" : "=r"(a) : "r"(m[inst.v]));
a = m[ inst.v ];
goto PU2_START;
CB_SEVENTEEN: // beware, only instruction 17 (cb3) actually updates pc on an assignment to m[7]!
if (unlikely(a == 7)) M = M2 + (inst.v >> 1);
// asm("mov %1, %0" : "=r"(m[a]) : "r"(inst.v));
m[ a ] = inst.v;
goto PU2_START;
CB_EIGHTEEN:
// if (unlikely(a == 7)) M = M2 + (m[inst.v] >> 1);
// if (unlikely(inst.v == a)) goto PU2_START;
m[ a ] = m[ inst.v ];
// asm("mov %1, %0" : "=r"(m[a]) : "r"(m[inst.v]));
goto PU2_START;
}
#ifdef PU2_TIME_SLICE_SUPPORT
// this is when time slice ends
if (__time_slice == -1) goto TIME_SLICE_AGAIN;
#endif
PU2_END:
// normal halt
m[ 7 ] = (M - M2) << 1;
// return remaining time
return __time_slice - slice_i;
}
#endif
// there's just memory. no, really
// ok actually there's two registers called a and b
// 0 HLT: halt
// 1 IMM: a = imm
// 2 LOD: a = m[b]
// 3 STR: m[a] = b (only happens if acc != 0)
// 4 SWP: swap a and b
// 5 ADD: a += b
// 6 SUB: a -= b
// 7 AND: a &= b
// 8 NOT: a = !a
// 9 ACC: u = imm
// 10 A2U: u = a
// 11 U2A: a = u
// 12 B2U: u = b
// 13 U2B: b = u
// pu2 optimized for speed implements a few new instructions for speed,
// and has a "bake" mode which will preprocess the memory and perform the required modifications.
// 14 NOP: pc += imm
// 15 CB1: m[imm] = a
// 16 CB2: a = m[imm]
// 17 CB3: m[a] = imm
// 18 CB4: m[a] = m[imm]
// NOTE: Memory indexes less than 1024 should be considered as temporary/compute memory, and
// programs should be written at index 1024 and upwards.
//
// NOTE: Memory index 7 is the program
// counter, in which the current instruction is read from m[m[7]] at each cycle
// -------------------------
// yeehaw baby it's time for as2
// E is the error code loc. zero it before calling as2
// Y is macro storage. extend as needed
// set O for output before running. keep in mind, it gets modified whilst as2 is running.
// therefore, i recommend you store a copy somewhere!
// clang-format off
char*C="hltimmlodstrswpaddsubandnotacca2uu2ab2uu2bnopcb1cb2cb3",*Y[1<<10],*E,*L;c,i;*K;as2(char*I){J(c=*I++){if(c==35){J(*++I-10);c=*I++;}L=C;(!isspace(c)?c-39?c-125?c-123?c-64?({i=0,I--;J(i++,*L)strncmp(L,I,3)?L+=3:({I+=3;*K++=i-1;*K++=(i-2&&i-9)?0:strtol(I,&I,10);goto Q;});E=I;return 1;Q:;}):as2(Y[strtol(I,&I,10)]):({Y[strtol(I,&I,10)]=I+1;J(*I++-125);}):({break;}):({*K++=1;*K++=*I++;}):0);}}
// clang-format on
// #undef S
#undef H
#undef J
#undef E
|