agssim: make last command repeatable with enter
[rofl0r-agsutils.git] / agssim.c
blob72373b100dcaa8d4a47f0cc1ea06a3d40e91a7f5
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <assert.h>
7 #include "ags_cpu.h"
8 #include "version.h"
9 #define ADS ":::AGSSim " VERSION " by rofl0r:::"
11 enum RegisterAccess {
12 RA_NONE = 0,
13 RA_READ = 1 << 0,
14 RA_WRITE = 1 << 1,
15 RA_READWRITE = 1 << 2,
18 struct regaccess_info {
19 /* enum RegisterAccess */ unsigned char ra_reg1;
20 /* enum RegisterAccess */ unsigned char ra_reg2;
21 } __attribute__((packed));
23 static const struct regaccess_info regaccess_info[] = {
24 [0] = {RA_NONE, RA_NONE},
25 [SCMD_ADD] = {RA_READWRITE, RA_NONE},
26 [SCMD_SUB] = {RA_READWRITE, RA_NONE},
27 [SCMD_REGTOREG] = {RA_READ, RA_WRITE},
28 [SCMD_WRITELIT] = {RA_NONE, RA_NONE}, // TODO
29 [SCMD_RET] = {RA_NONE, RA_NONE},
30 [SCMD_LITTOREG] = {RA_WRITE, RA_NONE},
31 [SCMD_MEMREAD] = {RA_WRITE, RA_NONE},
32 [SCMD_MEMWRITE] = {RA_READ, RA_NONE},
33 [SCMD_MULREG] = {RA_READWRITE, RA_READ},
34 [SCMD_DIVREG] = {RA_READWRITE, RA_READ},
35 [SCMD_ADDREG] = {RA_READWRITE, RA_READ},
36 [SCMD_SUBREG] = {RA_READWRITE, RA_READ},
37 [SCMD_BITAND] = {RA_READWRITE, RA_READ},
38 [SCMD_BITOR] = {RA_READWRITE, RA_READ},
39 [SCMD_ISEQUAL] = {RA_READWRITE, RA_READ},
40 [SCMD_NOTEQUAL] = {RA_READWRITE, RA_READ},
41 [SCMD_GREATER] = {RA_READWRITE, RA_READ},
42 [SCMD_LESSTHAN] = {RA_READWRITE, RA_READ},
43 [SCMD_GTE] = {RA_READWRITE, RA_READ},
44 [SCMD_LTE] = {RA_READWRITE, RA_READ},
45 [SCMD_AND] = {RA_READWRITE, RA_READ}, /*logical*/
46 [SCMD_OR] = {RA_READWRITE, RA_READ},
47 [SCMD_CALL] = {RA_READ, RA_NONE},
48 [SCMD_MEMREADB] = {RA_WRITE, RA_NONE},
49 [SCMD_MEMREADW] = {RA_WRITE, RA_NONE},
50 [SCMD_MEMWRITEB] = {RA_READ, RA_NONE},
51 [SCMD_MEMWRITEW] = {RA_READ, RA_NONE},
52 [SCMD_JZ] = {RA_READ, RA_NONE},
53 [SCMD_PUSHREG] = {RA_READ, RA_NONE},
54 [SCMD_POPREG] = {RA_WRITE, RA_NONE},
55 [SCMD_JMP] = {RA_READ, RA_NONE},
56 [SCMD_MUL] = {RA_READWRITE, RA_NONE},
57 [SCMD_CALLEXT] = {RA_READ, RA_NONE},
58 [SCMD_PUSHREAL] = {RA_READ, RA_NONE},
59 [SCMD_SUBREALSTACK] = {RA_READ, RA_NONE},
60 [SCMD_LINENUM] = {RA_NONE, RA_NONE},
61 [SCMD_CALLAS] = {RA_READ, RA_NONE},
62 [SCMD_THISBASE] = {RA_NONE, RA_NONE},
63 [SCMD_NUMFUNCARGS] = {RA_NONE, RA_NONE},
64 [SCMD_MODREG] = {RA_READWRITE, RA_READ},
65 [SCMD_XORREG] = {RA_READWRITE, RA_READ},
66 [SCMD_NOTREG] = {RA_READWRITE, RA_READ},
67 [SCMD_SHIFTLEFT] = {RA_READWRITE, RA_READ},
68 [SCMD_SHIFTRIGHT] = {RA_READWRITE, RA_READ},
69 [SCMD_CALLOBJ] = {RA_READ, RA_NONE},
70 [SCMD_CHECKBOUNDS] = {RA_READ, RA_NONE},
71 [SCMD_MEMWRITEPTR] = {RA_NONE, RA_NONE}, //TODO
72 [SCMD_MEMREADPTR] = {RA_NONE, RA_NONE}, //TODO
73 [SCMD_MEMZEROPTR] = {RA_NONE, RA_NONE},
74 [SCMD_MEMINITPTR] = {RA_NONE, RA_NONE}, //TODO
75 [SCMD_LOADSPOFFS] = {RA_NONE, RA_NONE},
76 [SCMD_CHECKNULL] = {RA_NONE, RA_NONE},
77 [SCMD_FADD] = {RA_READWRITE, RA_NONE},
78 [SCMD_FSUB] = {RA_READWRITE, RA_NONE},
79 [SCMD_FMULREG] = {RA_READWRITE, RA_READ},
80 [SCMD_FDIVREG] = {RA_READWRITE, RA_READ},
81 [SCMD_FADDREG] = {RA_READWRITE, RA_READ},
82 [SCMD_FSUBREG] = {RA_READWRITE, RA_READ},
83 [SCMD_FGREATER] = {RA_READWRITE, RA_READ},
84 [SCMD_FLESSTHAN] = {RA_READWRITE, RA_READ},
85 [SCMD_FGTE] = {RA_READWRITE, RA_READ},
86 [SCMD_FLTE] = {RA_READWRITE, RA_READ},
87 [SCMD_ZEROMEMORY] = {RA_NONE, RA_NONE},
88 [SCMD_CREATESTRING] = {RA_NONE, RA_NONE}, //TODO
89 [SCMD_STRINGSEQUAL] = {RA_READWRITE, RA_READ},
90 [SCMD_STRINGSNOTEQ] = {RA_READWRITE, RA_READ},
91 [SCMD_CHECKNULLREG] = {RA_NONE, RA_NONE}, //TODO
92 [SCMD_LOOPCHECKOFF] = {RA_NONE, RA_NONE},
93 [SCMD_MEMZEROPTRND] = {RA_NONE, RA_NONE},
94 [SCMD_JNZ] = {RA_NONE, RA_NONE},
95 [SCMD_DYNAMICBOUNDS] = {RA_NONE, RA_NONE}, //TODO
96 [SCMD_NEWARRAY] = {RA_NONE, RA_NONE}, //TODO
99 #ifndef MAX
100 #define MAX(a, b) ((a) > (b) ? (a) : (b))
101 #define MIN(a, b) ((a) < (b) ? (a) : (b))
102 #endif
104 /* TODO: move duplicate code from Assembler.c into separate TU */
105 static int get_reg(char* regname) {
106 int i = AR_NULL + 1;
107 for(; i < AR_MAX; i++)
108 if(strcmp(regnames[i], regname) == 0)
109 return i;
110 return AR_NULL;
113 static size_t mnemolen[SCMD_MAX];
114 static int mnemolen_initdone = 0;
116 static void init_mnemolen(void) {
117 size_t i = 0;
118 for(; i< SCMD_MAX; i++)
119 mnemolen[i] = strlen(opcodes[i].mnemonic);
120 mnemolen_initdone = 1;
123 static unsigned find_insn(char* sym) {
124 if(!mnemolen_initdone) init_mnemolen();
125 size_t i = 0, l = strlen(sym);
126 for(; i< SCMD_MAX; i++)
127 if(l == mnemolen[i] && memcmp(sym, opcodes[i].mnemonic, l) == 0)
128 return i;
129 return 0;
132 #include "StringEscape.h"
133 /* expects a pointer to the first char after a opening " in a string,
134 * converts the string into convbuf, and returns the length of that string */
135 static size_t get_length_and_convert(char* x, char* end, char* convbuf, size_t convbuflen) {
136 size_t result = 0;
137 char* e = x + strlen(x);
138 assert(e > x && e < end && *e == 0);
139 e--;
140 while(isspace(*e)) e--;
141 if(*e != '"') return (size_t) -1;
142 *e = 0;
143 result = unescape(x, convbuf, convbuflen);
144 return result;
147 /* sets lets char in arg to 0, and advances pointer till the next argstart */
148 static char* finalize_arg(char **p, char* pend, char* convbuf, size_t convbuflen) {
149 if(**p == '"') {
150 convbuf[0] = '"';
151 size_t l= get_length_and_convert(*p + 1, pend, convbuf+1, convbuflen - 1);
152 if(l == (size_t) -1) return 0;
153 convbuf[l+1] = '"';
154 convbuf[l+2] = 0;
155 *p = 0; /* make it crash if its accessed again, since a string should always be the last arg */
156 return convbuf;
157 } else {
158 char* ret = *p;
159 while(*p < pend && **p != ',' && !isspace(**p)) (*p)++;
160 assert(*p < pend);
161 **p = 0; (*p)++;
162 while(*p < pend && isspace(**p)) (*p)++;
163 assert(*p < pend);
164 return ret;
169 static struct text_segment {
170 int *code;
171 size_t len;
172 size_t capa;
173 } text;
175 enum RegisterUsage {
176 RU_NONE = 0,
177 RU_READ = 1 << 0,
178 RU_WRITE = 1 << 1,
179 RU_WRITE_AFTER_READ = 1 << 2,
182 static struct rval {
183 union {
184 int i;
185 float f;
187 enum RegisterUsage ru;
188 } registers[AR_MAX];
190 static unsigned char stack_mem[1000*4];
191 #define memory stack_mem
193 static int canread(int index, int cnt) {
194 return index >= 0 && index+cnt < sizeof(memory)/sizeof(memory[0]);
197 static void grow_text(size_t req) {
198 if(text.len + req > text.capa) {
199 text.code = realloc(text.code, (text.capa+1024)*sizeof(int));
200 text.capa += 1024;
204 static void append_code(int *code, size_t cnt) {
205 grow_text(cnt);
206 size_t i;
207 for(i = 0; i < cnt; i++) {
208 text.code[text.len++] = code[i];
212 static void vm_init() {
213 size_t i;
214 /* initialize registers to an easily recognisable junk value */
215 for(i = AR_NULL + 1; i < AR_MAX; i++) {
216 registers[i].i = 2222222222;
217 registers[i].ru = RU_NONE;
219 registers[AR_SP].i = 0;
220 registers[AR_NULL].i = 0;
221 /* set up EIP so vm_state() doesn't crash */
222 grow_text(16);
225 static inline int consume_int(int **eip) {
226 *eip = *eip+1;
227 return **eip;
230 static void change_reg_usage(int regno, enum RegisterAccess ra) {
231 enum RegisterUsage ru = registers[regno].ru;
232 switch(ra) {
233 case RA_READ:
234 if(ru == RU_NONE || ru == RU_READ) ru = RU_READ;
235 else if(ru == RU_WRITE);
236 else if(ru == RU_WRITE_AFTER_READ);
237 break;
238 case RA_WRITE:
239 if(ru == RU_NONE || ru == RU_WRITE) ru = RU_WRITE;
240 else if(ru == RU_READ) ru = RU_WRITE_AFTER_READ;
241 else if(ru == RU_WRITE_AFTER_READ);
242 break;
243 case RA_READWRITE:
244 if(ru == RU_NONE || ru == RU_READ) ru = RU_WRITE_AFTER_READ;
245 else if(ru == RU_WRITE);
246 else if(ru == RU_WRITE_AFTER_READ);
247 break;
249 registers[regno].ru = ru;
252 static void vm_update_register_usage(int *eip) {
253 const struct regaccess_info *ri = &regaccess_info[*eip];
254 if(ri->ra_reg1) change_reg_usage(eip[1], ri->ra_reg1);
255 if(ri->ra_reg2) change_reg_usage(eip[2], ri->ra_reg2);
258 static void write_mem1(int off, int val) {
259 unsigned char *m = (void*) memory;
260 m[off] = val&0xff;
262 static void write_mem2(int off, int val) {
263 unsigned short *m = (void*) memory;
264 m[off/2] = val&0xffff;
266 static void write_mem(int off, int val) {
267 int *m = (void*) memory;
268 m[off/4] = val;
271 static int read_mem(int off) {
272 int *m = (void*) memory;
273 return m[off/4];
276 #define CODE_INT(X) eip[X]
277 #define CODE_FLOAT(X) ((float*)eip)[X]
278 #define REGI(X) registers[CODE_INT(X)].i
279 #define REGF(X) registers[CODE_INT(X)].f
281 static void vm_step() {
282 /* we use register AR_NULL as instruction pointer */
283 int *eip = &text.code[registers[AR_NULL].i];
284 int eip_inc = 1 + opcodes[*eip].argcount;
285 int tmp;
286 vm_update_register_usage(eip);
288 switch(*eip) {
289 case SCMD_ADD:
290 REGI(1) += CODE_INT(2);
291 break;
292 case SCMD_SUB:
293 REGI(1) -= CODE_INT(2);
294 break;
295 case SCMD_REGTOREG:
296 REGI(2) = REGI(1);
297 break;
298 case SCMD_LITTOREG:
299 REGI(1) = CODE_INT(2);
300 break;
301 case SCMD_MULREG:
302 REGI(1) *= REGI(2);
303 break;
304 case SCMD_DIVREG:
305 REGI(1) /= REGI(2);
306 break;
307 case SCMD_ADDREG:
308 REGI(1) += REGI(2);
309 break;
310 case SCMD_SUBREG:
311 REGI(1) -= REGI(2);
312 break;
313 case SCMD_BITAND:
314 REGI(1) &= REGI(2);
315 break;
316 case SCMD_BITOR:
317 REGI(1) &= REGI(2);
318 break;
319 case SCMD_ISEQUAL:
320 REGI(1) = !!(REGI(1) == REGI(2));
321 break;
322 case SCMD_NOTEQUAL:
323 REGI(1) = !!(REGI(1) != REGI(2));
324 break;
325 case SCMD_GREATER:
326 REGI(1) = !!(REGI(1) > REGI(2));
327 break;
328 case SCMD_LESSTHAN:
329 REGI(1) = !!(REGI(1) < REGI(2));
330 break;
331 case SCMD_GTE:
332 REGI(1) = !!(REGI(1) >= REGI(2));
333 break;
334 case SCMD_LTE:
335 REGI(1) = !!(REGI(1) <= REGI(2));
336 break;
337 case SCMD_AND:
338 REGI(1) = !!(REGI(1) && REGI(2));
339 break;
340 case SCMD_OR:
341 REGI(1) = !!(REGI(1) || REGI(2));
342 break;
343 case SCMD_LOADSPOFFS:
344 registers[AR_MAR].i = registers[AR_SP].i - CODE_INT(1);
345 break;
346 case SCMD_PUSHREG:
347 write_mem(registers[AR_SP].i, REGI(1));
348 registers[AR_SP].i += 4;
349 break;
350 case SCMD_POPREG:
351 registers[AR_SP].i -= 4;
352 REGI(1) = read_mem(registers[AR_SP].i);
353 break;
354 case SCMD_MUL:
355 REGI(1) *= CODE_INT(2);
356 break;
357 case SCMD_THISBASE:
358 case SCMD_LINENUM:
359 break;
360 case SCMD_MODREG:
361 REGI(1) %= REGI(2);
362 break;
363 case SCMD_XORREG:
364 REGI(1) ^= REGI(2);
365 break;
366 case SCMD_NOTREG:
367 REGI(1) = !REGI(2);
368 break;
369 case SCMD_SHIFTLEFT:
370 REGI(1) <<= REGI(2);
371 break;
372 case SCMD_SHIFTRIGHT:
373 REGI(1) >>= REGI(2);
374 break;
375 case SCMD_FADD:
376 REGF(1) += CODE_FLOAT(2);
377 break;
378 case SCMD_FSUB:
379 REGF(1) -= CODE_FLOAT(2);
380 break;
381 case SCMD_FMULREG:
382 REGF(1) *= REGF(2);
383 break;
384 case SCMD_FDIVREG:
385 REGF(1) /= REGF(2);
386 break;
387 case SCMD_FADDREG:
388 REGF(1) += REGF(2);
389 break;
390 case SCMD_FSUBREG:
391 REGF(1) -= REGF(2);
392 break;
393 case SCMD_FGREATER:
394 REGI(1) = !!(REGF(1) > REGF(2));
395 break;
396 case SCMD_FLESSTHAN:
397 REGI(1) = !!(REGF(1) < REGF(2));
398 break;
399 case SCMD_FGTE:
400 REGI(1) = !!(REGF(1) >= REGF(2));
401 break;
402 case SCMD_FLTE:
403 REGI(1) = !!(REGF(1) <= REGF(2));
404 break;
405 case SCMD_MEMWRITE:
406 tmp = 4;
407 goto mwrite;
408 case SCMD_MEMWRITEW:
409 tmp = 2;
410 goto mwrite;
411 case SCMD_MEMWRITEB:
412 tmp = 1;
413 mwrite:
414 if(canread(registers[AR_MAR].i, tmp)) {
415 switch(tmp) {
416 case 4: write_mem(registers[AR_MAR].i, REGI(1)); break;
417 case 2: write_mem2(registers[AR_MAR].i, REGI(1)); break;
418 case 1: write_mem1(registers[AR_MAR].i, REGI(1)); break;
420 } else {
421 dprintf(2, "info: caught OOB memwrite\n");
423 break;
424 case SCMD_MEMREAD:
425 tmp = 4;
426 goto mread;
427 case SCMD_MEMREADW:
428 tmp = 2;
429 goto mread;
430 case SCMD_MEMREADB:
431 tmp = 1;
432 mread:
433 if(canread(registers[AR_MAR].i, tmp)) {
434 int val = memory[registers[AR_MAR].i];
435 switch(tmp) {
436 case 4: REGI(1) = val; break;
437 case 2: REGI(1) = val & 0xffff; break;
438 case 1: REGI(1) = val & 0xff; break;
440 } else {
441 dprintf(2, "info: caught OOB memread\n");
443 break;
444 case SCMD_NEWARRAY:
445 case SCMD_DYNAMICBOUNDS:
446 case SCMD_JNZ:
447 case SCMD_MEMZEROPTRND:
448 case SCMD_LOOPCHECKOFF:
449 case SCMD_CHECKNULLREG:
450 case SCMD_STRINGSNOTEQ:
451 case SCMD_STRINGSEQUAL:
452 case SCMD_CREATESTRING:
453 case SCMD_ZEROMEMORY:
454 case SCMD_CHECKNULL:
455 case SCMD_MEMINITPTR:
456 case SCMD_MEMZEROPTR:
457 case SCMD_MEMREADPTR:
458 case SCMD_MEMWRITEPTR:
459 case SCMD_CHECKBOUNDS:
460 case SCMD_CALLOBJ:
461 case SCMD_NUMFUNCARGS:
462 case SCMD_CALLAS:
463 case SCMD_SUBREALSTACK:
464 case SCMD_PUSHREAL:
465 case SCMD_CALLEXT:
466 case SCMD_JMP:
467 case SCMD_JZ:
468 case SCMD_CALL:
469 case SCMD_WRITELIT:
470 case SCMD_RET:
471 default:
472 dprintf(2, "info: %s not implemented yet\n", opcodes[*eip].mnemonic);
474 size_t i, l = opcodes[*eip].argcount;
475 for(i = 0; i < l; i++) ++(*eip);
477 break;
479 registers[AR_NULL].i += eip_inc;
482 static inline char *int_to_str(int value, char* out) {
483 sprintf(out, "%d", value);
484 return out;
487 static void vm_state() {
488 static const char ru_strings[][3] = {
489 [RU_NONE] = {0},
490 [RU_READ] = {'R', 0},
491 [RU_WRITE] = {'W', 0},
492 [RU_WRITE_AFTER_READ] = {'R', 'W', 0},
494 size_t i;
495 for(i=0; i< AR_MAX; i++)
496 printf("%s: %2s %d\n", i == 0 ? "eip" : regnames[i], ru_strings[registers[i].ru], registers[i].i);
498 for( i = MIN(registers[AR_SP].i+2*4, sizeof(stack_mem)/4);
499 i >= MAX(registers[AR_SP].i-2*4, 0);
500 i-=4) {
501 printf("SL %s %3zu %d\n", i == registers[AR_SP].i ? ">" : " ", i, read_mem(i));
502 if(i == 0) break;
505 int *eip = &text.code[registers[AR_NULL].i];
506 char arg1buf[32], arg2buf[32];
507 const char *arg1 = opcodes[*eip].argcount == 0 ? "" : \
508 (opcodes[*eip].regcount > 0 ? regnames[eip[1]] : int_to_str(eip[1], arg1buf));
509 const char *arg2 = opcodes[*eip].argcount < 2 ? "" : \
510 (opcodes[*eip].regcount > 1 ? regnames[eip[2]] : int_to_str(eip[2], arg2buf));
511 printf(" > %s %s %s\n", opcodes[*eip].mnemonic, arg1, arg2);
514 void vm_run(void) {
515 while(1) {
516 int *eip = &text.code[registers[AR_NULL].i];
517 if(!*eip) break;
518 vm_step();
522 static int usage(int fd, char *a0) {
523 dprintf(fd,
524 "%s - simple ags vm simulator\n"
525 "implements the ALU and a small stack\n"
526 "useful to examine how a chunk of code modifies VM state\n"
527 "not implemented: memory access apart from stack, jumps, functions\n"
528 "supply the assembly code via stdin, then type one of the following\n"
529 "commands:\n"
530 "!i - reset VM state and IP\n"
531 "!s - single-step\n"
532 "!r - run\n"
533 , a0);
534 return 1;
537 static int lastcommand;
538 enum UserCommand {
539 UC_STEP = 1,
540 UC_RUN,
541 UC_INIT,
542 UC_QUIT,
543 UC_HELP,
545 static void execute_user_command_i(int uc) {
546 switch(uc) {
547 case UC_STEP: vm_step(); break;
548 case UC_RUN : vm_run(); break;
549 case UC_INIT: vm_init(); break;
550 case UC_QUIT: exit(0); break;
551 case UC_HELP: usage(1, "agssim"); break;
553 lastcommand = uc;
554 vm_state();
556 static void execute_user_command(char *cmd) {
557 int uc = 0;
558 if(0) ;
559 else if(!strcmp(cmd, "s")) uc = UC_STEP;
560 else if(!strcmp(cmd, "r")) uc = UC_RUN;
561 else if(!strcmp(cmd, "i")) uc = UC_INIT;
562 else if(!strcmp(cmd, "q")) uc = UC_QUIT;
563 else if(!strcmp(cmd, "h")) uc = UC_HELP;
564 else {
565 dprintf(2, "unknown command\n");
566 return;
568 execute_user_command_i(uc);
571 int main(int argc, char** argv) {
572 if(argc != 1) return usage(2, argv[0]);
573 char buf[1024], *sym;
574 char convbuf[sizeof(buf)]; /* to convert escaped string into non-escaped version */
575 int lineno = 0;
576 vm_init();
577 printf(ADS " - type !h for help\n");
578 while(fgets(buf, sizeof buf, stdin)) {
579 int code[3];
580 size_t pos = 0;
581 lineno++;
582 char* p = buf, *pend = buf + sizeof buf;
583 if(*p == '\n' && lastcommand) {
584 execute_user_command_i(lastcommand);
585 continue;
587 if(*p == '#' || *p == ';') continue;
588 if(*p == '!') {
589 char *n = strchr(p, '\n');
590 if(n) *n = 0;
591 execute_user_command(p+1);
592 continue;
594 while(isspace(*p) && p < pend) p++;
595 assert(p < pend);
596 if(!*p) continue;
597 char* sym = p;
598 while(!isspace(*p) && p < pend) p++;
599 *p = 0; p++;
600 size_t l = strlen(sym);
601 if(l > 1 && sym[l-1] == ':') {
602 // functionstart or label
603 sym[l-1] = 0;
604 // we currently ignore that
605 continue;
607 unsigned instr = find_insn(sym);
608 if(!instr) {
609 dprintf(2, "line %zu: error: unknown instruction '%s'\n", lineno, sym);
610 continue;
612 code[pos++] = instr;
613 size_t arg;
614 for(arg = 0; arg < opcodes[instr].argcount; arg++) {
615 sym = finalize_arg(&p, pend, convbuf, sizeof(convbuf));
616 if(sym == 0) {
617 dprintf(2, "line %zu: error: expected \"\n", lineno);
618 goto loop_footer;
620 int value = 0;
621 if(arg < opcodes[instr].regcount) {
622 value=get_reg(sym);
623 if(instr == SCMD_REGTOREG) {
624 /* fix reversed order of arguments */
625 int dst = value;
626 sym = p;
627 while(p < pend && *p != ',' && !isspace(*p)) p++;
628 assert(p < pend);
629 *p = 0;
630 value=get_reg(sym);
631 code[pos++] = value;
632 code[pos++] = dst;
633 break;
635 } else {
636 switch(instr) {
637 case SCMD_LITTOREG:
638 /* immediate can be function name, string,
639 * variable name, stack fixup, or numeric value */
640 if(sym[0] == '"') {
641 dprintf(2, "error: string handling not implemented\n");
642 goto loop_footer;
643 } else if(sym[0] == '@') {
644 dprintf(2, "error: global variable handling not implemented\n");
645 goto loop_footer;
646 } else if(sym[0] == '.') {
647 if(memcmp(sym+1, "stack", 5)) {
648 dprintf(2, "error: expected stack\n");
649 goto loop_footer;;
651 dprintf(2, "error: stack fixup not implemented\n");
652 goto loop_footer;
653 } else if(isdigit(sym[0]) || sym[0] == '-') {
654 if(sym[0] == '-') assert(isdigit(sym[1]));
655 value = atoi(sym);
656 } else {
657 dprintf(2, "error: function refs not implemented yet\n");
658 goto loop_footer;
660 break;
662 case SCMD_JMP: case SCMD_JZ: case SCMD_JNZ:
663 add_label_ref(a, sym, pos);
664 break;
666 default:
667 value = atoi(sym);
670 code[pos++] = value;
672 append_code(code, pos);
673 loop_footer: ;