2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 // should be enough for everyone!
30 #define MAX_INST_COUNT (1000000)
35 //extern void fatal (const char *fmt, ...) __attribute__((__noreturn__)) __attribute__((format(printf, 1, 2)));
36 static __attribute__((__noreturn__
)) __attribute__((format(printf
, 1, 2))) void fatalis (const char *fmt
, ...) {
39 fprintf(stderr
, "AWISH FATAL: ");
41 vfprintf(stderr
, fmt
, ap
);
43 fprintf(stderr
, "\n");
48 #ifndef VM_FATAL_DEFINED
49 static __attribute__((__noreturn__
)) __attribute__((format(printf
, 3, 4))) void vmfatal (int tid
, int pc
, const char *fmt
, ...) {
52 fprintf(stderr
, "VM FATAL (thread #%d, pc=%04x): ", tid
, (unsigned int)pc
);
54 vfprintf(stderr
, fmt
, ap
);
56 fprintf(stderr
, "\n");
62 const char *vmOpNames
[] = {
119 VMRSTCB vmRSTCB
= NULL
;
120 VMMapGetCB vmMapGetCB
= NULL
;
121 VMMapSetCB vmMapSetCB
= NULL
;
128 int *stack
; // internal
129 int *tvars
; // internal
133 int vmDebugTrace
= 0;
134 FILE *vmDebugOutput
= NULL
;
137 static VMThread vmThreads
[VM_MAX_THREADS
];
138 static int vmExecuted
[VM_MAX_THREADS
];
139 static int vmLastThreadId
= 0;
141 unsigned char vmCode
[65536];
143 int vmGVars
[VM_VARS_SIZE
];
146 static void fixLastThreadId (void) {
147 for (; vmLastThreadId
> 0; --vmLastThreadId
) if (vmThreads
[vmLastThreadId
].stack
) break;
151 static int vmFindFreeThread (void) {
152 for (int f
= 0; f
< VM_MAX_THREADS
; ++f
) if (vmThreads
[f
].stack
== NULL
) return f
;
157 static int vmInitThread (VMThread
*trd
) {
161 trd
->stack
= calloc(VM_STACK_SIZE
, sizeof(int));
162 if (trd
->stack
== NULL
) return -1;
163 trd
->tvars
= calloc(VM_VARS_SIZE
, sizeof(int));
164 if (trd
->tvars
== NULL
) { free(trd
->stack
); return -1; }
169 static void vmFreeThread (VMThread
*trd
) {
170 if (trd
->stack
) free(trd
->stack
);
171 if (trd
->tvars
) free(trd
->tvars
);
180 int vmInitialize (void) {
182 memset(vmThreads
, 0, sizeof(vmThreads
));
185 if (vmInitThread(vmThreads
)) return -1; // alas
190 void vmDeinitialize (void) {
191 for (int f
= 0; f
< VM_MAX_THREADS
; ++f
) {
192 if (vmThreads
[f
].stack
) free(vmThreads
[f
].stack
);
193 if (vmThreads
[f
].tvars
) free(vmThreads
[f
].tvars
);
195 memset(vmThreads
, 0, sizeof(vmThreads
));
201 static inline int vmGetByte (int tid
, int opc
, VMThread
*trd
) {
202 if (trd
->pc
< 0 || trd
->pc
>= vmCodeSize
) vmfatal(tid
, opc
, "out of code");
203 return vmCode
[trd
->pc
++];
207 #define STACK_WANT(n) do { if (trd->sp < (n)) vmfatal(tid, opc, "stack underflow"); } while (0)
208 #define STACK_FSPC(n) do { if (trd->sp+(n) > VM_STACK_SIZE) vmfatal(tid, opc, "stack overflow"); } while (0)
210 #define STACK_TOP() (trd->stack[trd->sp-1])
211 #define STACK_POP() (trd->stack[--(trd->sp)])
212 #define STACK_PUSH(n) trd->stack[(trd->sp)++] = (n)
215 #define MATH(op) do { \
219 trd->stack[trd->sp-2] = (trd->stack[trd->sp-2]) op (trd->stack[trd->sp-1]); \
224 trd->stack[trd->sp-1] = (trd->stack[trd->sp-1]) op (argv[0]); \
226 case 2: if (argp[0] != NULL) *(argp[0]) = (argv[0]) op (argv[1]); break; \
227 case 3: if (argp[2] != NULL) *(argp[2]) = (argv[0]) op (argv[1]); break; \
232 #define CHECK_DIV_ZERO \
235 if (trd->sp > 0 && trd->stack[trd->sp-1] == 0) { fprintf(stderr, "VM FATAL: division by zero!\n"); exit(1); } \
238 if (argv[0] == 0) { fprintf(stderr, "VM FATAL: division by zero!\n"); exit(1); } \
241 if (argv[1] == 0) { fprintf(stderr, "VM FATAL: division by zero!\n"); exit(1); } \
246 #define JXX(op) do { \
250 argv[0] = STACK_POP(); \
253 argv[2] = STACK_POP(); \
254 argv[1] = STACK_POP(); \
259 argv[1] = STACK_POP(); \
262 if (argv[1] op argv[2]) trd->pc = argv[0]; \
266 static inline int isBranch (int opcode
) {
280 int vmExecuteOne (int tid
) {
281 VMThread
*trd
= &vmThreads
[tid
];
284 int argv
[3]; // argument values
285 int *argp
[3]; // pointer to vars for each arg
288 // decode instruction
289 opcode
= vmGetByte(tid
, opc
, trd
);
290 argc
= (opcode
>>6)&0x03;
292 argv
[0] = argv
[1] = argv
[2] = 0;
293 argp
[0] = argp
[1] = argp
[2] = NULL
;
295 if (tid
> vmLastThreadId
) vmLastThreadId
= tid
;
296 for (int f
= 0; f
< argc
; ++f
) {
297 int vn
= vmGetByte(tid
, opc
, trd
);
301 argv
[f
] = vmGetByte(tid
, opc
, trd
);
302 argv
[f
] |= vmGetByte(tid
, opc
, trd
)<<8;
303 if (argv
[f
] >= 32768 && (f
!= 0 || !isBranch(opcode
))) argv
[f
] -= 65536;
306 // stack var; <0: from stack top
307 argv
[f
] = vmGetByte(tid
, opc
, trd
);
308 argv
[f
] |= (vmGetByte(tid
, opc
, trd
)<<8);
309 if (argv
[f
] >= 32768) argv
[f
] -= 65536;
310 if (argv
[f
] < 0) argv
[f
] += trd
->sp
;
311 if (argv
[f
] < 0 || argv
[f
] >= VM_STACK_SIZE
) vmfatal(tid
, opc
, "invalid stack offset");
312 argp
[f
] = trd
->stack
+argv
[f
];
315 argp
[f
] = vn
&0x80 ? vmGVars
: trd
->tvars
;
318 argv
[f
] = *(argp
[f
]);
324 if (vmDebugOutput
== NULL
) vmDebugOutput
= stderr
;
326 fprintf(vmDebugOutput
, "(%04d) %04x: ", tid
, (unsigned int)opc
);
327 if (opcode
< sizeof(vmOpNames
)/sizeof(char *)-1) fprintf(vmDebugOutput
, "%s", vmOpNames
[opcode
]); else fprintf(vmDebugOutput
, "BAD");
328 for (int f
= 0; f
< argc
; ++f
) {
329 fputc(' ', vmDebugOutput
);
331 unsigned int ofs
= 0;
333 fputc('[', vmDebugOutput
);
334 if (argp
[f
] >= trd
->tvars
&& argp
[f
] < trd
->tvars
+VM_VARS_SIZE
) {
335 ofs
= (unsigned int)(argp
[f
]-trd
->tvars
);
336 } else if (argp
[f
] >= trd
->stack
&& argp
[f
] < trd
->stack
+VM_VARS_SIZE
) {
337 ofs
= (unsigned int)(argp
[f
]-trd
->stack
);
338 fputc('.', vmDebugOutput
);
339 } else if (argp
[f
] >= vmGVars
&& argp
[f
] < vmGVars
+VM_VARS_SIZE
) {
340 ofs
= (unsigned int)(argp
[f
]-vmGVars
);
341 fputc('@', vmDebugOutput
);
343 fprintf(vmDebugOutput
, "%u]", ofs
);
345 if (f
== 0 && isBranch(opcode
)) {
346 fprintf(vmDebugOutput
, "(0x%04x)", argv
[f
]);
348 fprintf(vmDebugOutput
, "(%d)", argv
[f
]);
351 fputc('\n', vmDebugOutput
);
352 if (vmDebugOutput
!= stderr
&& vmDebugOutput
!= stdout
) fflush(vmDebugOutput
);
357 case VM_ADD
: MATH(+); break;
358 case VM_SUB
: MATH(-); break;
359 case VM_MUL
: MATH(*); break;
368 case VM_BOR
: MATH(|); break;
369 case VM_XOR
: MATH(^); break;
370 case VM_AND
: MATH(&); break;
372 case VM_JEQ
: JXX(==); break;
373 case VM_JNE
: JXX(!=); break;
374 case VM_JLT
: JXX(<); break;
375 case VM_JLE
: JXX(<=); break;
376 case VM_JGT
: JXX(>); break;
377 case VM_JGE
: JXX(>=); break;
382 argv
[0] = STACK_POP();
391 argv
[0] = STACK_POP();
410 case VM_END
: return 1;
411 case VM_BRK
: return -1;
413 case VM_NEW
: { // new thread
414 int xtid
= vmFindFreeThread();
418 argv
[0] = STACK_POP();
421 if (vmInitThread(&vmThreads
[xtid
]) == 0) {
422 vmThreads
[xtid
].pc
= argv
[0];
427 if (xtid
< 0) vmfatal(tid
, opc
, "too many threads");
428 if (xtid
> vmLastThreadId
) vmLastThreadId
= xtid
;
430 if (argp
[1]) *(argp
[1]) = xtid
;
441 argv
[0] = STACK_POP(); // varid
442 argv
[1] = STACK_POP(); // value
445 if (argv
[0] < VM_VARS_SIZE
) vmGVars
[argv
[0]] = argv
[1];
446 } else if (argv
[0] < VM_VARS_SIZE
) {
447 trd
->tvars
[argv
[0]] = argv
[1];
452 argv
[1] = STACK_POP(); // value
455 if (argp
[0]) *(argp
[0]) = argv
[1];
458 int xtid
= argv
[2], vid
= argv
[0];
460 if (xtid
< 0 || xtid
>= VM_MAX_THREADS
|| vmThreads
[xtid
].stack
== NULL
|| vid
< 0 || vid
>= VM_VARS_SIZE
) break;
461 vmThreads
[xtid
].tvars
[vid
] = argv
[1];
467 int xtid
, vid
, pushit
;
493 if (xtid
>= 0 && xtid
< VM_MAX_THREADS
&& vmThreads
[xtid
].stack
!= NULL
&& vid
>= 0 && vid
< VM_VARS_SIZE
) {
494 argv
[0] = vmThreads
[xtid
].tvars
[vid
];
499 if (argp
[2]) *(argp
[2]) = argv
[0];
509 for (int f
= 0; f
< argc
; ++f
) STACK_PUSH(argv
[f
]);
514 trd
->stack
[trd
->sp
] = trd
->stack
[trd
->sp
-1];
525 if (argp
[0] == NULL
&& argv
[0] < 0) {
526 // allocate stack space
529 for (; argc
> 0; --argc
) STACK_PUSH(0);
534 for (int f
= 0; f
< argc
; ++f
) {
538 *(argp
[f
]) = STACK_POP();
539 } else if (argv
[f
] > 0) {
553 argc
= trd
->stack
[trd
->sp
-2];
554 trd
->stack
[trd
->sp
-2] = trd
->stack
[trd
->sp
-1];
555 trd
->stack
[trd
->sp
-1] = argc
;
559 argc
= trd
->stack
[trd
->sp
-1];
560 trd
->stack
[trd
->sp
-1] = argv
[0];
561 if (argp
[0]) *(argp
[0]) = argc
;
564 if (argp
[0]) *(argp
[0]) = argv
[1];
565 if (argp
[1]) *(argp
[1]) = argv
[0];
573 argv
[0] = STACK_POP();
576 if (argv
[0] < 0) argv
[0] += trd
->sp
;
577 if (argv
[0] < 0 || argv
[0] >= trd
->sp
) vmfatal(tid
, opc
, "invalid stack index (%d)", argv
[0]);
578 argv
[2] = trd
->stack
[argv
[0]];
580 if (opcode
== VM_ROL
) {
581 for (int f
= argv
[0]+1; f
< trd
->sp
; ++f
) trd
->stack
[f
-1] = trd
->stack
[f
];
592 if (argp
[1]) *(argp
[1]) = argv
[2];
598 if (argp
[0]) *(argp
[0]) = trd
->sp
;
608 if (argp
[0]) *(argp
[0]) = tid
;
620 argv
[0] = STACK_POP();
622 if (argv
[0] >= 0 && argv
[0] < VM_MAX_THREADS
&& vmThreads
[argv
[0]].stack
!= NULL
) {
625 if (argv
[0] == tid
) return 1;
626 vmFreeThread(&vmThreads
[argv
[0]]);
630 if (argv
[0] == tid
) return -1;
631 vmThreads
[argv
[0]].suspended
= 1;
634 vmThreads
[argv
[0]].suspended
= 0;
643 argv
[0] = STACK_POP();
645 if (argv
[0] >= 0 && argv
[0] < VM_MAX_THREADS
&& vmThreads
[argv
[0]].stack
!= NULL
) {
646 argv
[0] = vmThreads
[argv
[0]].suspended
;
651 if (argp
[1]) *(argp
[1]) = argv
[0];
661 argv
[0] = STACK_POP();
663 if (argv
[0] < 0 || argv
[0] >= vmCodeSize
) vmfatal(tid
, opc
, "invalid address in RXC (%d)", argv
[0]);
666 STACK_PUSH(vmCode
[argv
[0]]);
667 } else if (argp
[1]) {
668 *(argp
[1]) = vmCode
[argv
[0]];
676 argv
[1] = STACK_POP(); // byte
677 argv
[0] = STACK_POP(); // address
681 argv
[1] = STACK_POP(); // byte
684 if (argv
[0] < 0 || argv
[0] >= vmCodeSize
) vmfatal(tid
, opc
, "invalid address in WXC (%d)", argv
[0]);
685 if (argv
[1] < -128 || argv
[1] > 255) vmfatal(tid
, opc
, "invalid value in WXC (%d)", argv
[1]);
686 if (argv
[1] < 0) argv
[1] &= 0xff;
687 vmCode
[argv
[0]] = argv
[1];
694 trd
->pc
= STACK_POP();
697 vmfatal(tid
, opc
, "invalid RET");
698 case 2: // # of locals to pop, # of arguments to pop
699 case 3: // # of locals to pop, # of arguments to pop, retvalue (taken before all pops, pushes after returning)
700 if (argv
[0] < 0) argv
[0] = 0;
701 if (argv
[1] < 0) argv
[1] = 0;
702 STACK_WANT(argv
[0]+argv
[1]+1);
703 trd
->sp
-= argv
[0]; // drop locals
704 trd
->pc
= STACK_POP();
705 trd
->sp
-= argv
[1]; // drop arguments
719 argv
[0] = STACK_POP();
722 vmRSTCB(tid
, opcode
, argc
, argv
, argp
);
723 } else if (argc
== 0) {
735 argv
[1] = STACK_POP(); // y
736 argv
[0] = STACK_POP(); // x
739 if (vmMapGetCB
) argv
[0] = vmMapGetCB(tid
, opcode
==VM_MGF
, argv
[0], argv
[1]); else argv
[0] = 0;
741 if (argp
[2]) *(argp
[2]) = argv
[0];
742 } else if (argc
== 1) {
743 if (argp
[0]) *(argp
[0]) = argv
[0];
754 argv
[2] = STACK_POP(); // tile
755 argv
[1] = STACK_POP(); // y
756 argv
[0] = STACK_POP(); // x
760 argv
[2] = argv
[0]; // tile
761 argv
[1] = STACK_POP(); // y
762 argv
[0] = STACK_POP(); // x
766 argv
[2] = STACK_POP(); // tile
769 if (vmMapSetCB
) vmMapSetCB(tid
, opcode
==VM_MSF
, argv
[0], argv
[1], argv
[2]);
773 vmfatal(tid
, opc
, "invalid opcode (%d)", opcode
);
779 void vmExecuteAll (void) {
782 memset(vmExecuted
, 0, sizeof(vmExecuted
));
785 for (int t
= 0; t
< VM_MAX_THREADS
; ++t
) {
786 if (!vmExecuted
[t
] && vmThreads
[t
].stack
!= NULL
&& !vmThreads
[t
].suspended
) {
791 for (f
= MAX_INST_COUNT
; f
>= 0; --f
) {
792 int res
= vmExecuteOne(t
);
796 //fprintf(stderr, "!!!\n");
801 vmFreeThread(&vmThreads
[t
]);
806 if (f
< 0) vmfatal(t
, vmThreads
[t
].pc
, "too many instructions in frame");
813 // <0: BRK; >0: END; 0: ok
814 // maxinst<0: any number of instructions
815 int vmExecuteBSR (int tid
, int pc
, int maxinst
) {
818 if (tid
< 0 || tid
>= VM_MAX_THREADS
|| vmThreads
[tid
].stack
== NULL
|| vmThreads
[tid
].suspended
) return 2; // END
819 opc
= vmThreads
[tid
].pc
;
820 vmPush(tid
, -666666);
821 vmThreads
[tid
].pc
= pc
;
822 if (maxinst
== 0) maxinst
= MAX_INST_COUNT
;
824 int res
= vmExecuteOne(tid
);
826 if (!vmThreads
[tid
].stack
) return 1;
827 if (vmThreads
[tid
].suspended
) return -2;
828 if (res
> 0) vmFreeThread(&vmThreads
[tid
]);
829 if (res
!= 0) return res
;
830 if (vmThreads
[tid
].pc
== -666666) {
831 vmThreads
[tid
].pc
= opc
;
834 if (maxinst
== 1) vmfatal(tid
, vmThreads
[tid
].pc
, "too many instructions in vmExecuteBSR");
835 if (maxinst
> 0) --maxinst
;
840 int vmIsThreadAlive (int tid
) {
841 if (tid
>= 0 && tid
< VM_MAX_THREADS
) return vmThreads
[tid
].stack
!= NULL
;
846 int vmNewThread (int pc
) {
847 int tid
= vmFindFreeThread();
850 if (vmInitThread(&vmThreads
[tid
]) != 0) return -1;
851 vmThreads
[tid
].pc
= pc
;
852 if (tid
> vmLastThreadId
) vmLastThreadId
= tid
;
858 int vmKillThread (int tid
) {
859 if (vmIsThreadAlive(tid
)) {
860 vmFreeThread(&vmThreads
[tid
]);
868 int vmIsSuspendedThread (int tid
) {
869 if (vmIsThreadAlive(tid
)) return vmThreads
[tid
].suspended
;
874 int vmSuspendThread (int tid
) {
875 if (vmIsThreadAlive(tid
)) { vmThreads
[tid
].suspended
= 1; return 0; }
880 int vmResumeThread (int tid
) {
881 if (vmIsThreadAlive(tid
)) { vmThreads
[tid
].suspended
= 0; return 0; }
886 int vmGetTVar (int tid
, int idx
) {
887 if (idx
>= 0 && idx
< VM_VARS_SIZE
&& vmIsThreadAlive(tid
)) return vmThreads
[tid
].tvars
[idx
];
892 int vmSetTVar (int tid
, int idx
, int value
) {
893 if (idx
>= 0 && idx
< VM_VARS_SIZE
&& vmIsThreadAlive(tid
)) { vmThreads
[tid
].tvars
[idx
] = value
; return 0; }
898 int vmGetSP (int tid
) {
899 if (vmIsThreadAlive(tid
)) return vmThreads
[tid
].sp
;
904 int vmGetPC (int tid
) {
905 if (vmIsThreadAlive(tid
)) return vmThreads
[tid
].pc
;
910 int vmSetPC (int tid
, int pc
) {
911 if (vmIsThreadAlive(tid
) && pc
>= 0 && pc
< vmCodeSize
) { vmThreads
[tid
].pc
= pc
; return 0; }
916 int vmSetSP (int tid
, int value
) {
917 if (vmIsThreadAlive(tid
)) { vmThreads
[tid
].sp
= value
; return 0; }
922 int vmGetStack (int tid
, int idx
) {
923 if (vmIsThreadAlive(tid
)) {
924 if (idx
< 0) idx
+= vmThreads
[tid
].sp
;
925 if (idx
>= 0 && idx
< VM_STACK_SIZE
) return vmThreads
[tid
].stack
[idx
];
931 int vmSetStack (int tid
, int idx
, int value
) {
932 if (vmIsThreadAlive(tid
)) {
933 if (idx
< 0) idx
+= vmThreads
[tid
].sp
;
934 if (idx
>= 0 && idx
< VM_STACK_SIZE
) { vmThreads
[tid
].stack
[idx
] = value
; return 0; }
940 int vmPush (int tid
, int value
) {
941 if (vmIsThreadAlive(tid
) && vmThreads
[tid
].sp
< VM_STACK_SIZE
) {
942 vmThreads
[tid
].stack
[vmThreads
[tid
].sp
++] = value
;
949 int vmPop (int tid
) {
950 if (vmIsThreadAlive(tid
) && vmThreads
[tid
].sp
> 0) return vmThreads
[tid
].stack
[--vmThreads
[tid
].sp
];
955 int vmLoadArgs (int tid
, int argc
, int argv
[], int *argp
[], int aargc
, int aargv
[], int *aargp
[]) {
956 if (!vmIsThreadAlive(tid
)) return -1;
960 if (e
> aargc
) e
= aargc
;
961 for (int f
= 0; f
< e
; ++f
) { argv
[f
] = aargv
[f
]; argp
[f
] = aargp
[f
]; }
962 for (int f
= e
; f
< argc
; ++f
) {
963 if (vmThreads
[tid
].sp
<= 0) return -1;
965 argv
[argc
-f
] = vmThreads
[tid
].stack
[--vmThreads
[tid
].sp
];
972 int vmLastThread (void) {
973 return vmLastThreadId
;
977 static int readBuf (FILE *fl
, void *buf
, int len
) {
978 unsigned char *c
= (unsigned char *)buf
;
983 if (fread(&b
, 1, 1, fl
) != 1) return -1;
991 static int writeBuf (FILE *fl
, const void *buf
, int len
) {
992 const unsigned char *c
= (const unsigned char *)buf
;
995 unsigned char b
= *c
++;
998 if (fwrite(&b
, 1, 1, fl
) != 1) return -1;
1004 static int readDW (FILE *fl
, int *v
) {
1007 for (int f
= 0; f
< 4; ++f
) {
1010 if (fread(&b
, 1, 1, fl
) != 1) return -1;
1016 for (int f
= 0; f
< 4; ++f
) *v
|= t
[f
]<<(8*f
);
1022 static int writeDW (FILE *fl
, int v
) {
1023 for (int f
= 0; f
< 4; ++f
) {
1028 if (fwrite(&b
, 1, 1, fl
) != 1) return -1;
1035 ////////////////////////////////////////////////////////////////////////////////
1036 static VMLabelInfo
*labels
= NULL
;
1039 ////////////////////////////////////////////////////////////////////////////////
1040 void vmFreeLabels (void) {
1041 while (labels
!= NULL
) {
1042 VMLabelInfo
*l
= labels
;
1045 if (l
->name
) free(l
->name
);
1051 void vmFreeLabelsUntilMark (const char *name
) {
1052 while (labels
!= NULL
) {
1053 VMLabelInfo
*l
= labels
;
1054 int isMark
= (l
->type
== LB_MARK
);
1056 if (!(isMark
&& ((name
== NULL
&& l
->name
== NULL
) || (l
->name
!= NULL
&& name
!= NULL
&& strcmp(name
, l
->name
) == 0)))) isMark
= 0;
1058 if (l
->name
) free(l
->name
);
1065 VMLabelInfo
*vmLabelAddMark (const char *name
) {
1066 VMLabelInfo
*l
= malloc(sizeof(VMLabelInfo
));
1068 if (l
== NULL
) fatalis("out of memory");
1071 l
->name
= strdup(name
);
1072 if (l
->name
== NULL
) fatalis("out of memory");
1083 VMLabelInfo
*vmAddLabel (const char *name
, int type
, int value
, int pub
) {
1084 if (name
!= NULL
&& name
[0]) {
1085 VMLabelInfo
*l
= malloc(sizeof(VMLabelInfo
));
1087 if (l
== NULL
) fatalis("out of memory");
1089 l
->name
= strdup(name
);
1090 if (l
->name
== NULL
) fatalis("out of memory");
1105 static VMLabelInfo
*vmAddLabelToEnd (const char *name
, int type
, int value
, int pub
) {
1106 if (name
!= NULL
&& name
[0]) {
1107 VMLabelInfo
*l
= malloc(sizeof(VMLabelInfo
)), *last
= NULL
;
1109 if (l
== NULL
) fatalis("out of memory");
1111 l
->name
= strdup(name
);
1112 if (l
->name
== NULL
) fatalis("out of memory");
1120 if (labels
== NULL
) {
1123 for (last
= labels
; last
->next
!= NULL
; last
= last
->next
) ;
1132 VMLabelInfo
*vmFindLabel (const char *name
) {
1133 if (name
!= NULL
&& name
[0]) {
1134 for (VMLabelInfo
*l
= labels
; l
!= NULL
; l
= l
->next
) {
1135 if (l
->name
&& l
->name
[0] && strcasecmp(l
->name
, name
) == 0) return l
;
1142 int vmFindPC (const char *name
) {
1143 VMLabelInfo
*l
= vmFindLabel(name
);
1145 if (l
== NULL
&& l
->type
!= LB_CODE
) fatalis("no code label: '%s'", name
);
1150 int vmFindVarIndex (const char *name
) {
1151 VMLabelInfo
*l
= vmFindLabel(name
);
1153 if (l
== NULL
|| (l
->type
!= LB_GVAR
&& l
->type
!= LB_TVAR
&& l
->type
!= LB_SVAR
)) fatalis("no var label: '%s'", name
);
1158 int vmFindGVarIndex (const char *name
) {
1159 VMLabelInfo
*l
= vmFindLabel(name
);
1161 if (l
== NULL
|| l
->type
!= LB_GVAR
) fatalis("no global var label: '%s'", name
);
1166 int vmFindTVarIndex (const char *name
) {
1167 VMLabelInfo
*l
= vmFindLabel(name
);
1169 if (l
== NULL
|| l
->type
!= LB_TVAR
) fatalis("no thread var label: '%s'", name
);
1174 int vmFindSVarIndex (const char *name
) {
1175 VMLabelInfo
*l
= vmFindLabel(name
);
1177 if (l
== NULL
|| l
->type
!= LB_SVAR
) fatalis("no thread var label: '%s'", name
);
1182 int vmFindConst (const char *name
) {
1183 VMLabelInfo
*l
= vmFindLabel(name
);
1185 if (l
== NULL
|| l
->type
!= LB_CONST
) fatalis("no const label: '%s'", name
);
1190 VMLabelInfo
*vmFindMark (const char *name
) {
1191 for (VMLabelInfo
*l
= labels
; l
!= NULL
; l
= l
->next
) {
1192 if (l
->type
!= LB_MARK
) continue;
1193 if (name
== NULL
&& l
->name
== NULL
) return l
;
1194 if (name
!= NULL
&& l
->name
!= NULL
&& strcmp(name
, l
->name
) == 0) return l
;
1200 #define XREAD(dest,size,errlabel) do { \
1201 if (pos+(size) > rsz) goto errlabel; \
1202 if ((size) > 0) memcpy((dest), buf+pos, (size)); \
1207 #define XREADB(dest,errlabel) do { \
1208 if (pos+1 > rsz) goto errlabel; \
1209 (dest) = buf[pos]; \
1214 #define XREADW(dest,errlabel) do { \
1215 if (pos+2 > rsz) goto errlabel; \
1216 (dest) = buf[pos+1]; \
1218 (dest) |= buf[pos+0]; \
1224 extern int vmLoadCodeFileFromDump (const void *data
, int datasize
, int pc
) {
1225 int rsz
= datasize
, pos
= 4;
1226 const uint8_t *dta
= (const uint8_t *)data
;
1228 int csize
, lcnt
, rcnt
, elcnt
;
1230 if (rsz
< 4) return -1;
1231 if (memcmp(data
, "AVM1", 4) != 0) goto quitbufonly
;
1233 if (buf
== NULL
) return 1;
1235 for (int f
= 4; f
< rsz
; ++f
) buf
[f
] = dta
[f
]^SECRET
;
1237 XREADW(csize
, quitbufonly
);
1238 XREADW(rcnt
, quitbufonly
); // fixups
1239 XREADW(elcnt
, quitbufonly
); // extern labels
1240 XREADW(lcnt
, quitbufonly
); // labels
1241 if (pc
< 0 || pc
+csize
> 65535) goto quitbufonly
;
1243 /*if (goobers)*/ fprintf(stderr
, "code: %d bytes, %d public labels, %d relocations, %d externs\n", csize
, lcnt
, rcnt
, elcnt
);
1245 XREAD(vmCode
+pc
, csize
, quitbufonly
);
1248 for (int f
= 0; f
< rcnt
; ++f
) {
1249 int rel
, newpc
, val
;
1251 XREADW(rel
, quitbufonly
);
1252 if (rel
< 0 || rel
+1 >= csize
) goto quitbufonly
;
1253 newpc
= pc
+rel
; // fix here
1255 val
= vmCode
[newpc
+1];
1257 val
|= vmCode
[newpc
+0];
1261 vmCode
[newpc
+0] = val
&0xff;
1262 vmCode
[newpc
+1] = (val
>>8)&0xff;
1265 // load extern labels and perform fixups
1266 for (int f
= 0; f
< elcnt
; ++f
) {
1271 XREADB(type
, quitbufonly
);
1272 XREADB(namelen
, quitbufonly
);
1273 XREAD(name
, namelen
, quitbufonly
);
1274 XREADW(rcnt
, quitbufonly
);
1276 if (type
< LB_MIN_TYPE
|| type
> LB_MAX_TYPE
) goto quitbufonly
;
1277 if (namelen
< 1) goto quitbufonly
;
1280 for (int c
= 0; c
< rcnt
; ++c
) {
1284 XREADB(size
, quitbufonly
);
1285 XREADW(xpc
, quitbufonly
);
1287 if (size
!= 1 && size
!= 2) goto quitbufonly
;
1288 if (xpc
< 0 || xpc
+size
> csize
) goto quitbufonly
;
1291 l
= vmFindLabel(name
);
1293 /*if (goobers)*/ fprintf(stderr
, "VM: unknown extern: '%s'\n", name
);
1297 if (l
->type
== LB_GVAR
) val
|= 0x80;
1298 //fprintf(stderr, "%d: [%s]: ofs=%d, size=%d, value=%d (%d)\n", c, l->name, xpc, size, val, vmCode[xpc]);
1299 if (size
== 1 && (val
< -128 || val
> 255)) {
1300 /*if (goobers)*/ fprintf(stderr
, "VM: extern too big: '%s'\n", name
);
1303 vmCode
[xpc
+0] = val
&0xff;
1304 if (size
== 2) vmCode
[xpc
+1] = (val
>>8)&0xff;
1308 vmLabelAddMark(NULL
); // for this code labels
1309 for (int f
= 0; f
< lcnt
; ++f
) {
1310 unsigned char type
, namelen
, b
;
1316 switch (type
&0x7f) {
1324 XREADW(value
, quit
);
1325 if (value
>= 32768) value
-= 65536;
1328 XREADW(value
, quit
);
1329 value
+= pc
; // fixup
1332 fatalis("invalid label type: %d\n", type
);
1336 XREADB(namelen
, quit
);
1337 XREAD(name
, namelen
, quit
);
1340 if (namelen
> 0) vmAddLabel(name
, type
&0x7f, value
, type
&0x80?1:0);
1341 //if (l->type == LB_CODE) printf("%d/%d: [%s] %d : %d\n", f, lcnt, l->name, l->value-pc, l->value);
1347 vmFreeLabelsUntilMark(NULL
);
1354 ////////////////////////////////////////////////////////////////////////////////
1355 int vmSaveState (FILE *fl
) {
1360 if (writeDW(fl
, VM_VARS_SIZE
) != 0) return -1;
1361 //if (writeDW(fl, VM_STACK_SIZE) != 0) return -1;
1362 if (writeDW(fl
, vmLastThreadId
) != 0) return -1;
1363 if (writeDW(fl
, vmCodeSize
) != 0) return -1;
1364 if (writeBuf(fl
, vmCode
, vmCodeSize
) != 0) return -1;
1365 for (int f
= 0; f
< VM_VARS_SIZE
; ++f
) if (writeDW(fl
, vmGVars
[f
]) != 0) return -1;
1366 for (int f
= 0; f
<= vmLastThreadId
; ++f
) {
1367 if (vmThreads
[f
].stack
!= NULL
) {
1368 if (writeDW(fl
, f
) != 0) return -1;
1369 if (writeDW(fl
, vmThreads
[f
].pc
) != 0) return -1;
1370 if (writeDW(fl
, vmThreads
[f
].suspended
) != 0) return -1;
1371 if (writeDW(fl
, vmThreads
[f
].sp
) != 0) return -1;
1372 for (int c
= 0; c
< vmThreads
[f
].sp
; ++c
) if (writeDW(fl
, vmThreads
[f
].stack
[c
]) != 0) return -1;
1373 for (int c
= 0; c
< VM_VARS_SIZE
; ++c
) if (writeDW(fl
, vmThreads
[f
].tvars
[c
]) != 0) return -1;
1375 if (writeDW(fl
, -1) != 0) return -1;
1379 for (VMLabelInfo
*l
= labels
; l
!= NULL
; l
= l
->next
) ++lcnt
;
1380 if (writeDW(fl
, lcnt
) != 0) return -1;
1381 for (VMLabelInfo
*l
= labels
; l
!= NULL
; l
= l
->next
) {
1382 int nlen
= l
->name
!= NULL
? strlen(l
->name
) : -1;
1384 if (writeDW(fl
, l
->type
) != 0) return -1;
1385 if (writeDW(fl
, nlen
) != 0) return -1;
1387 if (writeBuf(fl
, l
->name
, nlen
) != 0) return -1;
1389 if (writeDW(fl
, l
->value
) != 0) return -1;
1390 if (writeDW(fl
, l
->pub
) != 0) return -1;
1397 int vmLoadState (FILE *fl
) {
1401 if (vmInitialize() != 0) goto fail
;
1403 if (readDW(fl
, &v
) != 0 || v
!= VM_VARS_SIZE
) goto fail
;
1404 //if (readDW(fl, &ssz) != 0) goto fail;
1405 if (readDW(fl
, &vmLastThreadId
) != 0 || vmLastThreadId
< 0 || vmLastThreadId
> VM_MAX_THREADS
) goto fail
;
1406 if (readDW(fl
, &vmCodeSize
) != 0 || vmCodeSize
< 1 || vmCodeSize
> 65536) goto fail
;
1407 if (readBuf(fl
, vmCode
, vmCodeSize
) != 0) goto fail
;
1408 for (int f
= 0; f
< VM_VARS_SIZE
; ++f
) if (readDW(fl
, &vmGVars
[f
]) != 0) goto fail
;
1409 for (int f
= 0; f
<= vmLastThreadId
; ++f
) {
1412 if (readDW(fl
, &flag
) != 0) goto fail
;
1413 if (flag
== -1) continue;
1414 if (flag
!= f
) goto fail
;
1415 if (vmInitThread(&vmThreads
[f
]) != 0) goto fail
;
1416 if (readDW(fl
, &vmThreads
[f
].pc
) != 0) goto fail
;
1417 if (readDW(fl
, &vmThreads
[f
].suspended
) != 0) goto fail
;
1418 if (readDW(fl
, &vmThreads
[f
].sp
) != 0) goto fail
;
1419 if (!vmThreads
[f
].suspended
) {
1420 if (vmThreads
[f
].pc
< 0 || vmThreads
[f
].pc
>= vmCodeSize
) goto fail
;
1421 if (vmThreads
[f
].sp
< 0 || vmThreads
[f
].sp
> VM_STACK_SIZE
) goto fail
;
1423 for (int c
= 0; c
< vmThreads
[f
].sp
; ++c
) if (readDW(fl
, &vmThreads
[f
].stack
[c
]) != 0) goto fail
;
1424 for (int c
= 0; c
< VM_VARS_SIZE
; ++c
) if (readDW(fl
, &vmThreads
[f
].tvars
[c
]) != 0) goto fail
;
1427 if (readDW(fl
, &lcnt
) != 0) goto fail
;
1428 for (; lcnt
> 0; --lcnt
) {
1429 int type
, namelen
, value
, pub
;
1432 if (readDW(fl
, &type
) != 0) goto fail
;
1433 if (readDW(fl
, &namelen
) != 0) goto fail
;
1434 if (namelen
> 255) goto fail
;
1436 if (readBuf(fl
, name
, namelen
) != 0) goto fail
;
1439 if (readDW(fl
, &value
) != 0) goto fail
;
1440 if (readDW(fl
, &pub
) != 0) goto fail
;
1441 vmAddLabelToEnd(namelen
>=0 ? name
: NULL
, type
, value
, pub
);