-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyan.c
287 lines (254 loc) · 6.4 KB
/
nyan.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
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
#include <malloc.h>
#include <stdio.h>
#include <wchar.h>
#include <fcntl.h>
#include <stdint.h>
#include "nyan.h"
#include "mouse.h"
#include "util.h"
struct jump_pair {
int jz;
int jnz;
};
void set_jump_pair(struct nyan_s *nyan, struct jump_pair *jump_pairs, int *jplen) {
int stack[nyan->len];
int stack_idx = 0;
for (int i = 0; i < nyan->len; i++) {
switch (nyan->commands[i]) {
case JUMP_ZERO:
stack[stack_idx] = i;
stack_idx++;
break;
case JUMP_NON_ZERO:
stack_idx--;
jump_pairs[*jplen].jz = stack[stack_idx];
jump_pairs[*jplen].jnz = i;
(*jplen)++;
break;
default:
// do nothing
break;
}
}
if (stack_idx > 0) {
printf("Error: unmatched [ found\n");
}
else if (stack_idx < 0) {
printf("Error: unmatched ] found\n");
}
}
int get_matching_jz(struct jump_pair* jp, int jnz, int jplen) {
for (int i = 0; i < jplen; i++) {
if (jp[i].jnz == jnz) {
return jp[i].jz;
}
}
return -1;
}
int get_matching_jnz(struct jump_pair* jp, int jz, int jplen) {
for (int i = 0; i < jplen; i++) {
if (jp[i].jz == jz) {
return jp[i].jnz;
}
}
return -1;
}
int get_matching_y(struct mouse* mice, int len, int x) {
for (int i = 0; i < len; i++) {
if (mice[i].x == x) {
return mice[i].y;
}
}
return -1;
}
int compile_raw_nyan(struct nyan_s nyan, Assembly* assem, char* name);
int compile_nyan(struct nyan_s nyan, Assembly* assem) {
asm_init(assem);
asm_add(assem, "section .data");
asm_add(assem, "memory: times 0x100000 db 0");
asm_add(assem, "modules: times 0x100000 db 0");
asm_add(assem, "section .text");
// make modules as functions
for (int i = 0; i < nyan.mice_len; i++) {
struct mouse m = nyan.mice[i];
asm_addf(assem, "func_%d:", m.x);
asm_addf(assem, "add r8, %d", 0x1000 * (m.x + 1));
Assembly* assm_mouse = malloc(sizeof(Assembly));
asm_init(assm_mouse);
struct nyan_s nyan_mouse = parse_nyan(m.f);
char* funcname = malloc(sizeof(char) * 10);
sprintf(funcname, "func_%d", m.x);
compile_raw_nyan(nyan_mouse, assm_mouse, funcname);
asm_add(assem, assm_mouse->data);
asm_addf(assem, "sub r8, %d", 0x1000 * m.x);
free(assm_mouse);
}
asm_add(assem, "global _start");
asm_add(assem, "_start:");
asm_add(assem, "mov r8, memory");
asm_add(assem, "mov rdx, _start");
for (int i = 0; i < nyan.mice_len; i++) {
struct mouse m = nyan.mice[i];
asm_addf(assem, "mov qword [modules+%d], func_%d", m.x, m.x);
}
compile_raw_nyan(nyan, assem, "main");
asm_add(assem, "mov rax, 60");
asm_add(assem, "mov rdi, 0");
asm_add(assem, "syscall");
return 0;
}
int compile_raw_nyan(struct nyan_s nyan, Assembly* assem, char* name) {
asm_add(assem, "push r8");
asm_add(assem, "push r9");
asm_add(assem, "mov r9, 0");
struct jump_pair *jump_pairs = malloc(sizeof(struct jump_pair) * nyan.len);
int jplen = 0;
set_jump_pair(&nyan, jump_pairs, &jplen);
for (int i = 0; i < nyan.len; i++) {
switch (nyan.commands[i]) {
case POINTER_ADD:
asm_add(assem, "inc r8");
break;
case POINTER_SUB:
asm_add(assem, "dec r8");
break;
case VALUE_ADD:
asm_add(assem, "inc qword [r8]");
break;
case VALUE_SUB:
asm_add(assem, "dec qword [r8]");
break;
case DEBUG_PRINT:
// TODO: print number
case PRINT:
asm_add(assem, "mov rax, 1");
asm_add(assem, "mov rdi, 1");
asm_add(assem, "mov rsi, r8");
asm_add(assem, "mov rdx, 1");
asm_add(assem, "syscall");
break;
case JUMP_ZERO: {
// jump point
asm_addf(assem, ".jz%s%d:", name, i);
asm_add(assem, "cmp qword [r8], 0");
// get pair jump point
int jnz = get_matching_jnz(jump_pairs, i, jplen);
asm_addf(assem, "je .jnz%s%d", name, jnz);
break;
}
case JUMP_NON_ZERO:
asm_addf(assem, ".jnz%s%d:", name, i);
asm_add(assem, "cmp byte qword [r8], 0");
int jz = get_matching_jz(jump_pairs, i, jplen);
asm_addf(assem, "jne .jz%s%d", name, jz);
break;
case MODULE_POINTER_ADD:
asm_add(assem, "inc r9");
break;
case MODULE_POINTER_SUB:
asm_add(assem, "dec r9");
break;
case MODULE_RETREIVE:
// find module in modules and call it
asm_add(assem, "mov rbx, modules");
asm_add(assem, "mov rax, qword [rbx+r9]");
asm_add(assem, "call rax");
// mov data in rax to data in r8
asm_add(assem, "mov qword [r8], rax");
// mov rax to r8
break;
case MODULE_RETURN: {
// mov data in ptr to rax
asm_add(assem, "mov rax, qword [r8]");
// return
asm_add(assem, "pop r9");
asm_add(assem, "pop r8");
asm_add(assem, "ret");
break;
}
}
}
return 0;
}
// create a function that returns nyan struct
struct nyan_s parse_nyan(char* filename) {
wchar_t* code = NULL;
size_t len = 0;
wread_file(&code, &len, filename);
char* mbuffer = NULL;
size_t msize;
char* mousename = remove_nyan_ext(filename);
strcat(mousename, ".mouse");
read_file(&mbuffer, &msize, mousename);
int mlen;
struct mouse* mice = parse_mouse(mbuffer, &mlen);
struct nyan_s nyan;
nyan.commands = calloc(len, sizeof(unsigned int*));
for (int i = 0; i < len; i++) {
nyan.commands[i] = IGNORE;
}
nyan.len = len;
nyan.mice = mice;
nyan.mice_len = mlen;
// for (int i = 0; i < nyan.mice_len; i++) {
// int x;
// int y;
// memcpy(&x, &nyan.mice[i]->x, sizeof(int));
// memcpy(&y, &nyan.mice[i]->y, sizeof(int));
// printf("NYMouse %d: %d, %d, %s\n", i, x, y, nyan.mice[i]->f);
// }
for (int i = 0; i < len; i++) {
switch (code[i]) {
case L'?':
nyan.commands[i] = POINTER_ADD;
break;
case L'!':
nyan.commands[i] = POINTER_SUB;
break;
case L'냥':
nyan.commands[i] = VALUE_ADD;
break;
case L'냐':
nyan.commands[i] = VALUE_SUB;
break;
case L'뀨':
nyan.commands[i] = DEBUG_PRINT;
break;
case L'.':
nyan.commands[i] = PRINT;
break;
case L'~':
nyan.commands[i] = JUMP_ZERO;
break;
case L'-':
nyan.commands[i] = JUMP_NON_ZERO;
break;
case L'먕':
nyan.commands[i] = MODULE_POINTER_ADD;
break;
case L'먀':
nyan.commands[i] = MODULE_POINTER_SUB;
break;
case L':':
nyan.commands[i] = MODULE_RETREIVE;
break;
case L';':
nyan.commands[i] = MODULE_RETURN;
break;
case L' ':
nyan.commands[i] = IGNORE;
// ignore whitespace
break;
case L'\n':
nyan.commands[i] = IGNORE;
// ignore newlines
break;
default:
wprintf(L"Unknown command: %lc\n", code[i]);
}
}
free(mbuffer);
free(mousename);
free(code);
return nyan;
}