-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual.hpp
1366 lines (1255 loc) · 38 KB
/
virtual.hpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _NAN_VIRTUAL_IMPL
#define _NAN_VIRTUAL_IMPL
#include <stack>
#include <stdlib.h>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <fcntl.h>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#endif
#include "mewall.h"
#include "mewmath.hpp"
#include "dlllib.hpp"
#pragma pack(push, 1)
// todo
// stack offset as argument wheve
namespace Virtual {
using byte = mew::byte;
struct VirtualMachine;
typedef void(*VM_Processor)(VirtualMachine&, byte*);
enum Instruction: byte {
Instruction_NONE = 0,
Instruction_LDLL,
Instruction_CALL,
Instruction_PUSH,
Instruction_POP,
Instruction_RPOP,
Instruction_ADD,
Instruction_SUB,
Instruction_MUL,
Instruction_DIV,
Instruction_INC,
Instruction_DEC,
Instruction_XOR,
Instruction_OR,
Instruction_NOT,
Instruction_AND,
Instruction_LS, // left shift
Instruction_RS, // right shift
Instruction_NUM, // arg type | number
Instruction_INT, // arg type | int
Instruction_FLT, // arg type | float
Instruction_DBL, // arg type | double
Instruction_UINT, // arg type | uint
Instruction_BYTE, // arg type | char
Instruction_MEM, // arg type | memory
Instruction_REG, // arg type | memory
Instruction_HEAP, // arg type | heap begin
Instruction_ST, // arg type | stack top
Instruction_JMP,
Instruction_RET,
Instruction_EXIT,
Instruction_TEST,
Instruction_JE,
Instruction_JEL,
Instruction_JEM,
Instruction_JNE,
Instruction_JL,
Instruction_JM,
Instruction_MOV, // replace head data from stack to memory
Instruction_SWAP, // replace head data from stack to memory
Instruction_MSET,
Instruction_SWST, // set used stream
Instruction_WRITE, // write to used stream
Instruction_READ, // read used stream
Instruction_OPEN, // open file as destinator
Instruction_PUTC,
Instruction_PUTI,
Instruction_PUTS,
Instruction_GETCH,
Instruction_MOVRDI,
};
#define VIRTUAL_VERSION (Instruction_PUTS*100)+0x34
struct FuncInfo {
uint idx;
const char* name;
uint calloffset;
};
struct CodeManifest {
// std::map<const char*, FuncInfo> procs;
};
struct Code {
size_t capacity;
Instruction* playground;
size_t data_size = 0;
byte* data = nullptr;
mew::stack<FuncInfo, 8U> procs;
};
struct CodeExtended {
CodeManifest* manifest;
Code* code;
};
struct CodeManifestMarker {
bool has_linked_files: 1;
};
#pragma region FILE
// stoped development
void Code_SaveFromFile(const Code& code, const std::filesystem::path& path) {
std::ofstream file(path, std::ios::out | std::ios::binary);
MewAssert(file.is_open());
file.seekp(std::ios::beg);
/* version */
uint vv = (uint)VIRTUAL_VERSION;
file.write((const char*)(&vv), sizeof(uint));
/* lib procs */
uint code_proc_size = code.procs.size();
file.write((const char*)(&code_proc_size), sizeof(uint));
for (int i = 0; i < code_proc_size; i++) {
FuncInfo& info = code.procs.at((size_t)i);
file.write((const char*)(&info.idx), sizeof(uint));
uint name_size = (uint)strlen(info.name);
file.write((const char*)(&name_size), sizeof(uint));
file.write(info.name, name_size);
file.write((const char*)(&info.calloffset), sizeof(uint));
}
/* code */
file << code.capacity;
for (int i = 0; i < code.capacity; i++) {
file << ((byte*)code.playground)[i];
}
/* data */
file << code.data_size;
for (int i = 0; i < code.data_size; i++) {
file << ((byte*)code.data)[i];
}
file.close();
}
void Code_SaveFromFile(const Code& code, const char* path) {
std::filesystem::path __path(path);
if (!__path.is_absolute()) {
__path = std::filesystem::absolute(__path.lexically_normal());
}
Code_SaveFromFile(code, __path);
}
Code* Code_LoadFromFile(const std::filesystem::path& path) {
std::ifstream file(path, std::ios::in | std::ios::binary);
MewAssert(file.is_open());
// file.seekg(std::ios::beg);
file >> std::noskipws;
/* version */
int file_version = mew::readInt4Bytes(file);
if (file_version != VIRTUAL_VERSION) {
MewWarn("file version not support (%i != %i)", file_version, VIRTUAL_VERSION);
return nullptr;
}
/** MANIFEST */
Code* code = new Code();
code->capacity = mew::readInt4Bytes(file);
code->data_size = mew::readInt4Bytes(file);
// CodeManifestMarker manifest_marker = static_cast<CodeManifestMarker>(mew::readInt4Bytes(file));
// todo data needs size for [da]
/* code */
code->playground = new Instruction[code->capacity];
for (int i = 0; i < code->capacity; i++) {
file >> ((byte*)code->playground)[i];
}
/* data */
code->data = new byte[code->data_size];
for (int i = 0; i < code->data_size; i++) {
file >> ((byte*)code->data)[i];
}
// if (manifest_marker.has_linked_files) {
// }
file.close();
return code;
}
Code* Code_LoadFromFile(const char* path) {
std::filesystem::path __path(path);
if (!__path.is_absolute()) {
__path = std::filesystem::absolute(__path.lexically_normal());
}
return Code_LoadFromFile(__path);
}
enum VM_Status: byte {
VM_Status_Panding = 0,
VM_Status_Execute = 1 << 1,
VM_Status_Ret = 1 << 2,
};
enum VM_TestStatus: byte {
VM_TestStatus_Skip = 0,
VM_TestStatus_Equal = 1 << 1,
VM_TestStatus_Less = 1 << 2,
VM_TestStatus_More = 1 << 3,
VM_TestStatus_EqualMore = VM_TestStatus_Equal | VM_TestStatus_More,
VM_TestStatus_EqualLess = VM_TestStatus_Equal | VM_TestStatus_Less,
};
#pragma region VM
enum VM_flags {
None = 0,
HeapLockExecute = 1 << 1,
};
template<size_t size>
struct VM_Register {
byte data[size];
};
enum struct VM_RegType: byte {
None, R, RX, DX, FX
};
#pragma pack(push, 4)
struct VirtualMachine {
VM_Register<4> _r[5]; // 4*5(20)
VM_Register<4> _fx[5]; // 4*5(20)
VM_Register<8> _rx[5]; // 8*5(40)
VM_Register<8> _dx[5]; // 8*5(40)
size_t capacity; // 8byte
FILE *r_stream; // 8byte
byte *memory, *heap,
*begin, *end; // 4x8byte(24byte)
struct TestStatus {
byte skip : 1 = 0;
byte equal : 1 = 0;
byte less : 1 = 0;
byte more : 1 = 0;
} test; // 1byte
VM_Status status; // 1byte
struct Flags {
byte heap_lock_execute: 1 = 1;
} flags; // 1byte
byte _pad0[1];
mew::stack<uint, 8U> stack; // 24byte
size_t rdi = 0;
mew::stack<byte *, 8U> begin_stack; // 24byte
mew::stack<mew::_dll_hinstance, 8U> hdlls; // 24byte
mew::stack<mew::_dll_farproc, 8U> hprocs; // 24byte
mew::stack<FuncInfo, 8U> procs; // 24byte
byte* getRegister(VM_RegType rt, byte idx, size_t* size = nullptr) {
MewUserAssert(idx < 5, "undefined register idx");
switch (rt) {
case VM_RegType::R:
if (!size) {*size = 4;}
return this->_r[idx].data;
case VM_RegType::RX:
if (!size) {*size = 8;}
return this->_rx[idx].data;
case VM_RegType::FX:
if (!size) {*size = 4;}
return this->_fx[idx].data;
case VM_RegType::DX:
if (!size) {*size = 8;}
return this->_dx[idx].data;
default: return nullptr;
}
}
}; // 300byte
#pragma pack(pop)
void a() {
sizeof(VirtualMachine);
}
#ifndef VM_ALLOC_ALIGN
#define VM_ALLOC_ALIGN 512
#endif
#ifndef VM_MINHEAP_ALIGN
#define VM_MINHEAP_ALIGN 128
#endif
#ifndef VM_CODE_ALIGN
#define VM_CODE_ALIGN 8
#endif
#define __VM_ALIGN(_val, _align) (((int)((_val) / (_align)) + 1) * (_align))
void Alloc(VirtualMachine& vm) {
if (vm.memory != nullptr) {
free(vm.memory);
}
vm.memory = new byte[VM_ALLOC_ALIGN];
memset(vm.memory, Instruction_NONE, VM_ALLOC_ALIGN);
vm.capacity = VM_ALLOC_ALIGN;
}
void Alloc(VirtualMachine& vm, Code& code) {
size_t size = __VM_ALIGN(code.capacity+code.data_size, VM_ALLOC_ALIGN);
if ((size - code.capacity - code.data_size) <= 0) {
size += VM_MINHEAP_ALIGN;
}
vm.memory = new byte[size];
memset(vm.memory, Instruction_NONE, size);
vm.capacity = size;
}
uint DeclareProccessor(VirtualMachine& vm, VM_Processor proc) {
MewNotImpl();
// vm.procs.push_back(proc);
// return vm.procs.size()-1;
}
void LoadMemory(VirtualMachine& vm, Code& code) {
vm.memory = (byte*)code.playground;
vm.procs = code.procs;
/* load extern functions */
for (int i = 0; i < code.procs.size(); ++i) {
FuncInfo& info = code.procs.at(i);
mew::_dll_farproc proc = mew::GetFunction(vm.hdlls.at((size_t)info.idx), info.name);
vm.hprocs.pushIfNotExists(proc);
}
}
void VM_ManualPush(VirtualMachine& vm, uint x) {
vm.stack.push(x);
}
void VM_Push(VirtualMachine& vm, byte head_byte, uint number) {
switch (head_byte) {
case 0:
case Instruction_FLT:
case Instruction_NUM: {
vm.stack.push(number);
} break;
case Instruction_MEM: {
MewUserAssert(vm.heap+number < vm.end, "out of memory");
byte* pointer = vm.heap+number;
uint x; memcpy(&x, pointer, sizeof(x));
vm.stack.push(x, vm.rdi);
} break;
case Instruction_REG: {
MewUserAssert(vm.heap+number < vm.end, "out of memory");
vm.stack.push(number, vm.rdi);
} break;
case Instruction_ST: {
MewUserAssert(vm.stack.has(number), "out of stack");
vm.stack.push(vm.stack.at((int)number), vm.rdi);
} break;
default: MewNot(); break;
}
}
byte* VM_GetReg(VirtualMachine& vm, size_t* size = nullptr) {
byte rtype = *vm.begin++;
byte ridx = *vm.begin++;
return vm.getRegister((Virtual::VM_RegType)rtype, ridx);
}
void VM_Push(VirtualMachine& vm) {
byte head_byte = *vm.begin++;
switch (head_byte) {
case 0:
case Instruction_FLT:
case Instruction_NUM: {
uint number = 0;
memcpy(&number, vm.begin, sizeof(number));
vm.stack.push(number);
vm.begin += sizeof(number);
} break;
case Instruction_MEM: {
uint number = 0;
memcpy(&number, vm.begin, sizeof(number));
MewUserAssert(vm.heap+number < vm.end, "out of memory");
byte* pointer = vm.heap+number;
uint x; memcpy(&x, pointer, sizeof(x));
vm.stack.push(x, vm.rdi);
vm.begin += sizeof(number);
} break;
case Instruction_REG: {
size_t size;
byte* reg = VM_GetReg(vm, &size);
MewUserAssert(reg != nullptr, "invalid register");
vm.stack.push((uint)*reg, vm.rdi);
if (size == 8) {
vm.stack.push((uint)*(reg+sizeof(uint)), vm.rdi);
}
} break;
case Instruction_ST: {
int number = 0;
memcpy(&number, vm.begin, sizeof(number));
MewUserAssert(vm.stack.has(number), "out of stack");
vm.stack.push(vm.stack.at(number), vm.rdi);
vm.begin += sizeof(number);
} break;
default: MewNot(); break;
}
}
void VM_Pop(VirtualMachine& vm) {
MewAssert(!vm.stack.empty());
vm.stack.pop();
}
void VM_RPop(VirtualMachine& vm) {
MewAssert(!vm.stack.empty());
size_t size;
byte* reg = VM_GetReg(vm, &size);
if (size == 4) {
uint value = vm.stack.pop();
memcpy(reg, &value, sizeof(uint));
} else
if (size == 8) {
long long value = vm.stack.npop<long long>();
memcpy(reg, &value, sizeof(value));
}
}
void VM_StackTop(VirtualMachine& vm, byte type, uint* x, byte** mem = nullptr) {
switch (type) {
case 0:
case Instruction_FLT:
case Instruction_NUM: {
MewUserAssert(!vm.stack.empty(), "stack is empty");
uint _top = vm.stack.top(vm.rdi);
memmove(x, &_top, sizeof(_top));
} break;
case Instruction_MEM: {
MewUserAssert(!vm.stack.empty(), "stack is empty");
uint _top = vm.stack.top(vm.rdi);
uint offset = _top;
MewUserAssert(vm.heap+offset < vm.end, "out of memory");
byte* pointer = vm.heap+offset;
if (mem != nullptr) {
*mem = pointer;
}
memmove(x, pointer, sizeof(*x));
} break;
default: MewNot(); break;
}
// if (vm.rdi == 0) {
// vm.stack.pop();
// }
}
void VM_ManualCall(VirtualMachine& vm, int offset) {
MewUserAssert(MEW_IN_RANGE(vm.memory, vm.end, vm.begin+offset),
"out of memory");
vm.begin_stack.push(vm.begin);
vm.begin += offset;
}
void VM_Call(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
VM_ManualCall(vm, offset);
}
void VM_MathBase(VirtualMachine& vm, uint* x, uint* y, byte** mem = nullptr) {
byte type_x = *vm.begin++;
byte type_y = *vm.begin++;
vm.rdi += sizeof(uint);
VM_StackTop(vm, type_x, x, mem);
vm.rdi -= sizeof(uint);
VM_StackTop(vm, type_y, y);
}
int VM_GetOffset(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int)); vm.begin += sizeof(int);
return offset/4;
}
#pragma region VM_ARG
typedef struct {
VM_RegType type;
byte idx;
} VM_REG_INFO;
class VM_ARG {
public:
VM_ARG() {}
byte* data;
byte* data2;
byte type;
int& getInt() {
return (int&)(*this->data);
}
lli& getLong() {
return (lli&)(*this->data);
}
float& getFloat() {
return (float&)(*this->data);
}
double& getDouble() {
return (double&)(*this->data);
}
byte getByte() {
return (byte)(*this->data);
}
static void do_math(VM_ARG& a, mew::asgio fn, bool depr_float = false) {
switch (a.type) {
case Instruction_ST: mew::gen_asgio(fn, a.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_asgio(fn, a.getInt()); break;
case VM_RegType::RX: mew::gen_asgio(fn, a.getLong()); break;
case VM_RegType::FX: if (!depr_float) mew::gen_asgio(fn, a.getFloat()); break;
case VM_RegType::DX: if (!depr_float) mew::gen_asgio(fn, a.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
break;
}
default: MewUserAssert(false, "undefined arg type");
}
}
static void do_math(VM_ARG& a, VM_ARG& b, mew::adgio fn, bool depr_float = false) {
switch (a.type) {
case Instruction_ST: {
switch (b.type) {
case Instruction_ST: mew::gen_adgio(fn, a.getInt(), b.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_adgio(fn, a.getInt(), b.getInt()); break;
case VM_RegType::RX: mew::gen_adgio(fn, a.getInt(), b.getLong()); break;
case VM_RegType::FX: if (!depr_float) mew::gen_adgio(fn, a.getInt(), b.getFloat()); break;
case VM_RegType::DX: if (!depr_float) mew::gen_adgio(fn, a.getInt(), b.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
case Instruction_NUM: {
mew::gen_adgio(fn, a.getInt(), b.getInt());
} break;
}
} break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: {
switch (b.type) {
case Instruction_ST: mew::gen_adgio(fn, a.getInt(), b.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_adgio(fn, a.getInt(), b.getInt()); break;
case VM_RegType::RX: mew::gen_adgio(fn, a.getInt(), b.getLong()); break;
case VM_RegType::FX: if (!depr_float) mew::gen_adgio(fn, a.getInt(), b.getFloat()); break;
case VM_RegType::DX: if (!depr_float) mew::gen_adgio(fn, a.getInt(), b.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
case Instruction_NUM: {
mew::gen_adgio(fn, a.getInt(), b.getInt());
} break;
}
} break;
case VM_RegType::RX: {
switch (b.type) {
case Instruction_ST: mew::gen_adgio(fn, a.getLong(), b.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_adgio(fn, a.getLong(), b.getInt()); break;
case VM_RegType::RX: mew::gen_adgio(fn, a.getLong(), b.getLong()); break;
case VM_RegType::FX: if (!depr_float) mew::gen_adgio(fn, a.getLong(), b.getFloat()); break;
case VM_RegType::DX: if (!depr_float) mew::gen_adgio(fn, a.getLong(), b.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
case Instruction_NUM: {
mew::gen_adgio(fn, a.getLong(), b.getInt());
} break;
}
} break;
case VM_RegType::FX: {
if (depr_float) break;
switch (b.type) {
case Instruction_ST: mew::gen_adgio(fn, a.getFloat(), b.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_adgio(fn, a.getFloat(), b.getInt()); break;
case VM_RegType::RX: mew::gen_adgio(fn, a.getFloat(), b.getLong()); break;
case VM_RegType::FX: mew::gen_adgio(fn, a.getFloat(), b.getFloat()); break;
case VM_RegType::DX: mew::gen_adgio(fn, a.getFloat(), b.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
case Instruction_NUM: {
mew::gen_adgio(fn, a.getFloat(), b.getInt());
} break;
}
} break;
case VM_RegType::DX: {
if (depr_float) break;
switch (b.type) {
case Instruction_ST: mew::gen_adgio(fn, a.getDouble(), b.getInt()); break;
case Instruction_REG: {
VM_REG_INFO* ri = (VM_REG_INFO*)a.data2;
switch (ri->type) {
case VM_RegType::R: mew::gen_adgio(fn, a.getDouble(), b.getInt()); break;
case VM_RegType::RX: mew::gen_adgio(fn, a.getDouble(), b.getLong()); break;
case VM_RegType::FX: mew::gen_adgio(fn, a.getDouble(), b.getFloat()); break;
case VM_RegType::DX: mew::gen_adgio(fn, a.getDouble(), b.getDouble()); break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
case Instruction_NUM: {
mew::gen_adgio(fn, a.getDouble(), b.getInt());
} break;
}
} break;
default: MewUserAssert(false, "undefined reg type");
}
} break;
default: MewUserAssert(false, "undefined arg type");
}
}
VM_ARG& operator++() {
do_math(*this, mew::aginc);
return *this;
}
VM_ARG& operator--() {
do_math(*this, mew::agdec);
return *this;
}
static void mov(VM_ARG& a, VM_ARG& b) {
do_math(a, b, mew::agmov);
}
static void swap(VM_ARG& a, VM_ARG& b) {
do_math(a, b, mew::agswap);
}
};
VM_ARG& operator+(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agadd);
return a;
}
VM_ARG& operator-(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agsub);
return a;
}
VM_ARG& operator/(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agdiv);
return a;
}
VM_ARG& operator*(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agmul);
return a;
}
VM_ARG& operator>>(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agrs, true);
return a;
}
VM_ARG& operator<<(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agls, true);
return a;
}
VM_ARG& operator^(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agxor, true);
return a;
}
VM_ARG& operator~(VM_ARG& a) {
VM_ARG::do_math(a, mew::agnot, true);
return a;
}
VM_ARG& operator|(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agor, true);
return a;
}
VM_ARG& operator&(VM_ARG& a, VM_ARG& b) {
VM_ARG::do_math(a, b, mew::agand, true);
return a;
}
VM_ARG VM_GetArg(VirtualMachine& vm) {
byte type = *vm.begin++;
switch (type) {
case Instruction_ST: {
int offset;
memcpy(&offset, vm.begin, sizeof(int)); vm.begin += sizeof(int);
VM_ARG arg;
arg.data = vm.stack.rat(-offset);
arg.type = type;
return arg;
};
case Instruction_REG: {
byte rtype = *vm.begin++;
byte ridx = *vm.begin++;
size_t size;
VM_ARG arg;
arg.data = vm.getRegister((VM_RegType)rtype, ridx, &size);
VM_REG_INFO* ri = new VM_REG_INFO();
ri->idx = ridx;
ri->type = (VM_RegType)rtype;
arg.data2 = (byte*)ri;
arg.type = type;
return arg;
}
case Instruction_NUM: {
int num;
memcpy(&num, vm.begin, sizeof(int)); vm.begin += sizeof(int);
VM_ARG arg;
arg.data = (byte*)num;
arg.type = type;
return arg;
}
default: MewUserAssert(false, "undefined arg type");
}
}
#pragma endregion VM_ARG
void VM_MovRDI(VirtualMachine& vm) {
int offset = VM_GetOffset(vm);
vm.rdi = offset;
}
void VM_Add(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a + b;
}
void VM_Sub(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a - b;
}
void VM_Mul(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a * b;
}
void VM_Div(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a / b;
}
void VM_Inc(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
++a;
}
void VM_Dec(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
--a;
}
void VM_Xor(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a ^ b;
}
void VM_Or(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a | b;
}
void VM_Not(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
~a;
}
void VM_And(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a & b;
}
void VM_LS(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a << b;
}
void VM_RS(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
a >> b;
}
void VM_ManualJmp(VirtualMachine& vm, int offset) {
MewUserAssert(MEW_IN_RANGE(vm.memory, vm.end, vm.begin+offset),
"out of memory");
// vm.begin_stack.push(vm.begin);
vm.begin += offset;
}
void VM_Jmp(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int)); vm.begin += sizeof(int);
MewUserAssert(MEW_IN_RANGE(vm.memory, vm.end, vm.begin+offset),
"out of memory");
// vm.begin_stack.push(vm.begin);
vm.begin += offset;
}
void VM_Ret(VirtualMachine& vm) {
if (vm.begin_stack.empty()) {
vm.status = VM_Status_Ret; return;
}
byte* begin = vm.begin_stack.top();
vm.begin = begin;
vm.begin_stack.pop();
}
void VM_Test(VirtualMachine& vm) {
int x, y;
vm.test = {0};
VM_MathBase(vm, (uint*)&x, (uint*)&y);
int result = memcmp(&x, &y, sizeof(x));
if (result > 0) {
vm.test.more = 1;
} else if (result < 0) {
vm.test.less = 1;
} else {
vm.test.equal = 1;
}
}
void VM_JE(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (vm.test.equal) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_JEL(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (vm.test.equal || vm.test.less) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_JEM(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (vm.test.equal || vm.test.more) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_JL(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (vm.test.less) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_JM(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (vm.test.more) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_JNE(VirtualMachine& vm) {
int offset;
memcpy(&offset, vm.begin, sizeof(int));
if (!vm.test.equal) {
VM_ManualJmp(vm, offset);
} else {
vm.begin += sizeof(int);
}
}
void VM_Mov(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
VM_ARG::mov(a, b);
}
void VM_Swap(VirtualMachine& vm) {
auto a = VM_GetArg(vm);
auto b = VM_GetArg(vm);
VM_ARG::swap(a, b);
}
void VM_MSet(VirtualMachine& vm) {
uint x; /* start */
uint y; /* size */
uint z; /* value */
memcpy(&x, vm.begin, sizeof(x)); vm.begin += sizeof(x);
memcpy(&y, vm.begin, sizeof(y)); vm.begin += sizeof(y);
memcpy(&z, vm.begin, sizeof(z)); vm.begin += sizeof(z);
MewUserAssert(vm.heap+x < vm.end, "out of memory");
MewUserAssert(vm.heap+x+y < vm.end, "out of memory");
memset(vm.heap+x, z, y);
}
void VM_Putc(VirtualMachine& vm) {
wchar_t long_char;
memcpy(&long_char, vm.begin, sizeof(wchar_t)); vm.begin+=sizeof(wchar_t);
putwchar(long_char);
}
void VM_Puti(VirtualMachine& vm) {
uint offset;
memcpy(&offset, vm.begin, sizeof(uint)); vm.begin+=sizeof(uint);
vm.rdi += offset;
int x; VM_StackTop(vm, *vm.begin++, (uint*)&x);
vm.rdi -= offset;
char str[12] = {0};
mew::_itoa10(x, str);
fputs(str, stdout);
}
void VM_Puts(VirtualMachine& vm) {
uint offset;
memcpy(&offset, vm.begin, sizeof(uint)); vm.begin+=sizeof(uint);
MewUserAssert(vm.heap+offset < vm.end, "out of memory");
byte* pointer = vm.heap+offset;
char* begin = (char*)pointer;
while (*(begin) != 0) {
putchar(*(begin++));
// putwchar(*(begin++));
}
}
void VM_Getch(VirtualMachine& vm) {
int& a = VM_GetArg(vm).getInt();
a = mew::wait_char();
}
void VM_Open(VirtualMachine& vm) {
uint offset;
memcpy(&offset, vm.begin, sizeof(uint)); vm.begin+=sizeof(uint);
MewUserAssert(vm.heap+offset < vm.end, "out of memory");
byte* pointer = vm.heap+offset;
char* begin = (char*)pointer;
int flags;
memcpy(&flags, vm.begin, sizeof(int)); vm.begin+=sizeof(int);
int dest = open(begin, flags);
VM_ManualPush(vm, dest);
}
void VM_Swst(VirtualMachine& vm) {
int idx;
bool use_stack;
memcpy(&use_stack, vm.begin++, sizeof(use_stack));
if (use_stack) {
VM_StackTop(vm, *vm.begin++, (uint*)&idx);
} else {
memcpy(&idx, vm.begin, sizeof(idx)); vm.begin+=sizeof(idx);
}
vm.r_stream = fdopen(idx, "r+");
}
void VM_Write(VirtualMachine& vm) {
uint offset;
memcpy(&offset, vm.begin, sizeof(uint)); vm.begin+=sizeof(uint);
MewUserAssert(vm.heap+offset < vm.end, "out of memory");
byte* pointer = vm.heap+offset;
fputs((char*)pointer, vm.r_stream);
}
void VM_Read(VirtualMachine& vm) {
uint offset;
memcpy(&offset, vm.begin, sizeof(uint)); vm.begin+=sizeof(uint);
MewUserAssert(vm.heap+offset < vm.end, "out of memory");
byte* pointer = vm.heap+offset;
short int chunk_size;
memcpy(&chunk_size, vm.begin, sizeof(chunk_size)); vm.begin+=sizeof(chunk_size);
MewUserAssert(vm.heap+offset+(chunk_size*2) < vm.end, "out of memory (chunk too big)");
fgets((char*)pointer, chunk_size, vm.r_stream);
}
void RunLine(VirtualMachine& vm) {
byte head_byte = *vm.begin++;
// printf("[%i]\n", head_byte);