-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.cpp
executable file
·885 lines (700 loc) · 31.3 KB
/
context.cpp
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
#include "context.h"
#include "jumpTable.h"
#include "nativeCode.h"
#include <algorithm>
#include <type_traits>
#include <istream>
#include <ostream>
#include <stdexcept>
#include <boost/assert.hpp>
using namespace std;
namespace
{
/*
* When native code returns it is expected to put one of these values into
* eax to indicate the reason of the return.
*/
namespace nativeCodeReturnValue
{
enum value
{
/*
* ebx: error code
* 0 - normal termination
* 1 - invalid operator
* 2 - execution beyond array size
*
* ecx: additional value only valid for the following ebx values
* 1 - ecx is a value of the plater that was not decoded
*/
halt = 1,
/*
* ebx: size of the new array
*/
allocation = 2,
/*
* ebx: an index of an array to abandon
*/
abandonment = 3,
/*
* ebx: value to output (should be [0, 255])
*/
output = 4,
/*
* Upon return into the native code:
* eax should contain a character code that was read [0, 255]
* or ~0 for EOF
*/
input = 5,
/*
* ebx: an index of an array to be copied into array 0
* ecx: an index to set the execution finger to
*/
loadProgram = 6,
/*
* This code is returned when execution hits a "recompile stub".
* Array 0 native code should be regenerated and execution should be
* restarted from the same platter.
*
* Recompile stubs are inserted by array amendment operations that
* amend array 0 itself. This way we are trying to avoid extra
* recompilations if array 0 is modified in more than one spot
* before the modified code is actually executed.
*
* Execution finger position is calculated by doing a search in the
* jumpTable for the nativeCode before recompilation. It is not
* very efficient but reduces stub size. As stub may replace any
* other operator its size is effectively the minimum size for
* native code blocks generated for other operators. As
* recompilation of a generated native code should not happen often
* this seems like a reasonable optimization.
*/
recompile = 7,
};
};
namespace haltReturnCodes
{
enum value
{
normalTermination = 0,
invalidOperator = 1,
outOfBoundExecution = 2,
};
};
}
/*
* === context ===
*/
context::context(memoryManager & mm, istream & is, ostream & os, ::array * zeroArray)
: _mm(mm)
, _is(is)
, _os(os)
, _array0Source(0)
, _minEmptyArrayIndex(1)
{
if (!zeroArray)
throw invalid_argument("zeroArray should not be a null pointer");
_arrays.push_back(zeroArray);
}
#pragma warning( push )
/*
* C4731: frame pointer register 'ebp' modified by inline assembly code
*
* This is intentional. ebp is modified to be used by the generated native code
* but then it should be restored to the original value.
*
* With vc10 this warning seems to work only at a function level.
*/
#pragma warning( disable: 4731 )
void context::run() throw(exceptions::invalidArrayIndex,
exceptions::invalidOperatorFormat)
{
::array * array0 = _arrays[0];
generateNativeCode(*array0);
size_t eaxValue = 0;
void * registers = &_registers[0];
void * arrays = &_arrays[0];
class jumpTable * jumpTable = array0->jumpTable();
void * resumeAt = array0->nativeCode()->begin();
size_t newFingerPosition;
size_t returnCode; /* eax */
while (true)
{
size_t value1; /* ebx */
size_t value2; /* ecx */
__asm
{
pushad
mov eax, eaxValue
mov ecx, resumeAt
mov esi, registers
mov edi, arrays
mov ebx, jumpTable
mov ebp, ebx
call ecx
pop edx
mov ebp, [esp + 8]
mov returnCode, eax
mov value1, ebx
mov value2, ecx
mov resumeAt, edx
popad
}
switch (returnCode)
{
case nativeCodeReturnValue::halt:
switch (value1)
{
case haltReturnCodes::normalTermination:
break;
case haltReturnCodes::invalidOperator:
_os << endl
<< "Invalid operator: "
"0x" << hex << uppercase << value2 << endl;
break;
case haltReturnCodes::outOfBoundExecution:
_os << endl
<< "Execution beyound array length" << endl;
break;
default:
_os << endl
<< "Unexpected halt code: "
"0x" << hex << uppercase << value1 << endl;
}
return;
case nativeCodeReturnValue::allocation:
eaxValue = allocation(value1);
arrays = &_arrays[0];
break;
case nativeCodeReturnValue::abandonment:
abandonment(value1);
arrays = &_arrays[0];
break;
case nativeCodeReturnValue::output:
output(static_cast<unsigned char>(value1));
break;
case nativeCodeReturnValue::input:
eaxValue = input();
break;
case nativeCodeReturnValue::loadProgram:
newFingerPosition = value2;
loadProgram(value1);
array0 = _arrays[0];
if (newFingerPosition >= array0->size())
throw exceptions::invalidArrayIndex
(L"loadProgram index out of range", newFingerPosition);
jumpTable = array0->jumpTable();
resumeAt = jumpTable->address(newFingerPosition);
break;
case nativeCodeReturnValue::recompile:
newFingerPosition = fingerPositionFor(resumeAt);
generateNativeCode(*array0);
jumpTable = array0->jumpTable();
resumeAt = jumpTable->address(newFingerPosition);
break;
default:
_os << endl
<< "Unexpected native code return: "
"0x" << hex << uppercase << returnCode << endl;
return;
}
}
}
#pragma warning( pop )
size_t context::fingerPositionFor(void * returnAddress)
{
::array & array0 = *_arrays[0];
BOOST_ASSERT(array0.size() > 0);
void ** begin = array0.jumpTable()->begin();
void ** end = begin + array0.size() - 1;
void ** platter = lower_bound(begin, end, returnAddress);
/*
* As return address should never be the very first byte of an array native
* code we should never get begin as a result.
*/
BOOST_ASSERT(platter > begin);
return platter - begin - 1;
}
/*
* --- Code generation helper macros ---
*
* Used by codeFor and codeForOOBStub.
*
* Expect to, curr and size to be in scope.
*/
/*
* STR is expected to be a litteral string constant and thus include a trailing
* zero bytes that is not copied in the output.
*/
#define EMIT_BYTES(STR) \
if (to) \
{ \
memcpy(curr, STR, sizeof(STR) - 1); \
curr += sizeof(STR) - 1; \
} \
size += sizeof(STR) - 1; \
/* */
#define EMIT_REGISTER_AS_BYTE_DISP(REG) \
if (to) \
{ \
*curr = static_cast<char>(REG * sizeof(unsigned int)); \
++curr; \
} \
++size; \
/* */
#define EMIT_BYTE(VALUE) \
if (to) \
{ \
*reinterpret_cast<unsigned char *>(curr) = VALUE; \
++curr; \
} \
++size; \
/* */
#define EMIT_WORD(VALUE) \
if (to) \
{ \
*reinterpret_cast<unsigned int *>(curr) = VALUE; \
curr += sizeof(unsigned int); \
} \
size += sizeof(unsigned int); \
/* */
size_t context::codeFor(const platter & p, char * to)
{
/*
* Native code assumes:
*
* ESI - pointer to the registers array [8 32-bit values]
* EDI - pointer to the collection of array pointers
* EBP - jump table first entry address
*/
unsigned int A, B, C, value;
size_t size = 0;
char * curr = to;
size_t jmpSource = 0;
/*
* Any instruction should be compiled into at least this many bytes so that
* it can always be overwritten by a recompile stub in case the code in the
* array 0 will decide to modify itself.
*/
const size_t recompileStubSize = 7;
platter::operator_::value op;
try
{
op = p.decode(A, B, C, value);
}
catch (const exceptions::invalidOperatorFormat & /* ex */)
{
static_assert(nativeCodeReturnValue::halt == 1,
"halt value is encoded below. If it changes "
"the value below should be updated.");
static_assert(haltReturnCodes::invalidOperator == 1,
"invalidOperator value is encoded below. If it changes "
"the value below should be update.");
/* eax: nativeCodeReturnValue::halt */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x01" /* mov al, imm8 */
/* imm8: nativeCodeReturnValue::halt */
/* ecx: 1 - invalid operator */
"\x31\xDB" /* xor ebx, ebx */
"\xB3\x01" /* mov bl, imm8 */
/* imm8: haltReturnCodes::invalidOperator */
/* edx: Invalid platter value */
"\xB9"); /* mov ecx, imm32 */
EMIT_WORD(p);
/* return */
EMIT_BYTES("\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
BOOST_ASSERT(size >= recompileStubSize);
return size;
}
switch (op)
{
case platter::operator_::conditionalMove:
/* ecx: C */
EMIT_BYTES("\x8B\x4E"); /* mov ecx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* if (C != 0) { */
EMIT_BYTES("\xE3\x06"); /* jcxz rel8 (6) */
jmpSource = size;
/* eax: B */
EMIT_BYTES("\x8B\x46"); /* mov eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* A = B */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
/* } */
BOOST_ASSERT(jmpSource + 6 == size);
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::arrayIndex:
/* ebx: B */
EMIT_BYTES("\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* eax: array[B] */
EMIT_BYTES("\x8B\x04\x9F"); /* mov eax, [edi + ebx * 4] */
/* ecx: C */
EMIT_BYTES("\x8B\x4E"); /* mov ecx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* eax: array[B]->platters()[C] */
EMIT_BYTES("\x8B\x44\x88");
/* mov eax, [eax + ecx * 4 + disp8] */
/* [eax + ecx * 4 + <first platter>] */
EMIT_BYTE(::array::_plattersOffset); /* <first platter> */
BOOST_ASSERT(::array::_plattersOffset < 256);
/* A = array[B]->platters()[C] */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::arrayAmendment:
/* ecx: A */
EMIT_BYTES("\x8B\x4E"); /* mov ecx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
/* ebx: B */
EMIT_BYTES("\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* edx: C */
EMIT_BYTES("\x8B\x56"); /* mov edx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* eax: array[A] */
EMIT_BYTES("\x8B\x04\x8F"); /* mov eax, [edi + ecx * 4] */
/* array[A]->_flags |= dirty */
EMIT_BYTES("\x83\x88"); /* or [eax + disp32], imm8 */
EMIT_WORD(static_cast<unsigned int>(offsetof(::array, _flags)));
/* [eax + array::_flags] */
EMIT_BYTE(static_cast<unsigned char>(::array::flag::dirty));
/* imm8: array::flag::dirty */
/* array[A]->platters()[B] = C */
EMIT_BYTES("\x89\x54\x98");
/* mov [eax + ebx * 4 + disp8], edx */
/* [eax + ebx * 4 + <first platter>] */
EMIT_BYTE(::array::_plattersOffset); /* <first platter> */
BOOST_ASSERT(::array::_plattersOffset < 256);
/* if (A == 0) { */
EMIT_BYTES("\x83\xF9\x00"); /* cmp ecx, imm8 (0) */
EMIT_BYTES("\x75\x13"); /* jnz rel8: 19 */
jmpSource = size;
/* eax: jumpTable[B] */
EMIT_BYTES("\x8B\x44\x9D\x00");
/* mov eax, [ebp + ebx * 4 + disp8(0)] */
/*
* *eax = asm {
* xor eax, eax
* mov al, imm8
* ... nativeCodeReturnValue::recompile
* pop edx
* call edx
* }
*/
static_assert(nativeCodeReturnValue::recompile == 7,
"recompile is encoded in the code below. If it "
"value changes code below should be updated.");
/*
* 31 C0 xor eax, eax
* B0 07 mov al, imm8 - B0+ al(0)
* nativeCodeReturnValue::recompile
* 5A pop edx
* FF D2 (near abs) call edx
*/
EMIT_BYTES("\xC7\x00\x31\xC0\xB0\x00"
/* mov [eax], imm32 (0x31C0B000) */
"\xC7\x40\x03\x07\x5A\xFF\xD2"
/* mov [eax + disp8(3)], imm32 (0x075AFFD2) */
/* jmp rel8 (0) */
"\xEB\x00");
/* } */
BOOST_ASSERT(jmpSource + 19 == size);
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::addition:
/* eax: B */
EMIT_BYTES("\x8B\x46"); /* mov eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* eax: B + C */
EMIT_BYTES("\x03\x46"); /* add eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* A = B + C */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::multiplication:
/* eax: B */
EMIT_BYTES("\x8B\x46"); /* mov eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* eax: B * C */
EMIT_BYTES("\xF7\x66"); /* mul edx:eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* A = B * C */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::division:
/* eax: B */
EMIT_BYTES("\x8B\x46"); /* mov eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* edx: 0 */
EMIT_BYTES("\x31\xD2"); /* xor edx, edx */
/* eax: B / C */
EMIT_BYTES("\xF7\x76"); /* div edx:eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* A = B / C */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::notAnd:
/* eax: B */
EMIT_BYTES("\x8B\x46"); /* mov eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* eax: B & C */
EMIT_BYTES("\x23\x46"); /* and eax, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* eax: ~(B & C) */
EMIT_BYTES("\xF7\xD0"); /* not eax */
/* A = ~(B & C) */
EMIT_BYTES("\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::halt:
static_assert(nativeCodeReturnValue::halt == 1,
"halt value is encoded below. If it changes "
"the value below should be updated.");
static_assert(haltReturnCodes::normalTermination == 0,
"normalTermination value is encoded below. If it "
"changes the value below should be update.");
/* eax: nativeCodeReturnValue::halt */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x01" /* mov al, imm8 */
/* imm8: nativeCodeReturnValue::halt */
/* ebx: 0 - normal termination */
"\x31\xDB" /* xor ebx, ebx */
/* return */
"\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::allocation:
static_assert(nativeCodeReturnValue::allocation == 2,
"allocation value is encoded below. If it "
"changes the value below should be updated.");
/* eax: nativeCodeReturnValue::allocation */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x02" /* mov al, imm8 */
/* imm8: nativeCodeReturnValue::allocation */
/* ebx: C */
"\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* return */
EMIT_BYTES("\x5A" /* pop edx */
"\xFF\xD2" /* call edx */
/* B: eax (new array index) */
"\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::abandonment:
static_assert(nativeCodeReturnValue::abandonment == 3,
"abandonment value is encoded below. If it "
"changes the value below should be updated.");
/* eax: nativeCodeReturnValue::abandonment */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x03" /* mov el, imm8 */
/* imm8: nativeCodeReturnValue::abandonment */
/* ebx: C */
"\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* return */
EMIT_BYTES("\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::output:
static_assert(nativeCodeReturnValue::output == 4,
"output value is encoded below. If it "
"changes the value below should be updated.");
/* eax: nativeCodeReturnValue::output */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x04" /* mov el, imm8 */
/* imm8: nativeCodeReturnValue::output */
/* ebx: C */
"\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* return */
EMIT_BYTES("\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::input:
static_assert(nativeCodeReturnValue::input == 5,
"input value is encoded below. If it "
"changes the value below should be updated.");
/* eax: nativeCodeReturnValue::input */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x05" /* mov al, imm8 */
/* imm8: nativeCodeReturnValue::input */
"\x5A" /* pop edx */
"\xFF\xD2" /* call edx */
/* C = <input char> */
"\x89\x46"); /* mov [esi + disp8], eax */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::loadProgram:
static_assert(nativeCodeReturnValue::loadProgram == 6,
"loadProgram value is encoded below. If it "
"changes the value below should be updated.");
/* ebx: B */
EMIT_BYTES("\x8B\x5E"); /* mov ebx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(B); /* [esi + B] */
/* ecx: C */
EMIT_BYTES("\x8B\x4E"); /* mov ecx, [esi + disp8] */
EMIT_REGISTER_AS_BYTE_DISP(C); /* [esi + C] */
/* if (B == 0) { */
EMIT_BYTES("\x83\xFB\x00"); /* cmp ebx, imm8 (0) */
EMIT_BYTES("\x75\x06"); /* jnz rel8: 6 */
jmpSource = size;
/* eax: jumpTable[C] */
EMIT_BYTES("\x8B\x44\x8D\x00"
/* mov eax, [ebp + ecx * 4 + disp8(0)] */
/* jmp eax */
"\xFF\xE0"); /* jmp eax */
/* } */
BOOST_ASSERT(jmpSource + 6 == size);
/* eax: nativeCodeReturnValue::loadProgram */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x06" /* mov el, imm8 */
/* imm8: nativeCodeReturnValue::loadProgram */
/* return */
"\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
BOOST_ASSERT(size >= recompileStubSize);
break;
case platter::operator_::orthography:
/* A = value */
EMIT_BYTES("\xC7\x46"); /* mov [esi + disp8], imm32 */
EMIT_REGISTER_AS_BYTE_DISP(A); /* [esi + A] */
EMIT_WORD(value); /* value */
BOOST_ASSERT(size >= recompileStubSize);
break;
default:
throw logic_error("Unexpected operator");
}
return size;
}
size_t context::codeForOOBStub(char * to)
{
size_t size = 0;
char * curr = to;
static_assert(nativeCodeReturnValue::halt == 1,
"halt value is encoded below. If it changes "
"the value below should be updated.");
static_assert(haltReturnCodes::outOfBoundExecution == 2,
"outOfBoundExecution value is encoded below. If it "
"changes the value below should be update.");
/* eax: nativeCodeReturnValue::halt */
EMIT_BYTES("\x31\xC0" /* xor eax, eax */
"\xB0\x01" /* mov al, imm8 */
/* imm8: nativeCodeReturnValue::halt */
/* ebx: 2 - out of bound execution */
"\x31\xDB" /* xor ebx, ebx */
"\xB3\x02" /* mov bl, imm8 */
/* imm8: haltReturnCodes::outOfBoundExecution */
/* return */
"\x5A" /* pop edx */
"\xFF\xD2"); /* call edx */
return size;
}
#undef EMIT_BYTES
#undef EMIT_REGISTER_AS_BYTE_DISP
#undef EMIT_BYTE
#undef EMIT_WORD
void context::generateNativeCode(::array & a)
{
if (a._nativeCode)
{
a._nativeCode->destroy(_mm);
a._nativeCode = nullptr;
}
a.dirty(false);
/* Precalculate native code size */
size_t nativeCodeSize = 0;
for (size_t i = 0, len = a.size(); i < len; ++i)
nativeCodeSize += codeFor(a[i], nullptr);
/* Stub to prevent execution beyond array length */
nativeCodeSize += codeForOOBStub(nullptr);
a._nativeCode = nativeCode::create(_mm, nativeCodeSize, a.size());
char * nativeCode = a._nativeCode->begin();
void ** jumpTable = a._nativeCode->jumpTable()->begin();
for (size_t i = 0, len = a.size(); i < len; ++i)
{
*jumpTable++ = nativeCode;
nativeCode += codeFor(a[i], nativeCode);
}
nativeCode += codeForOOBStub(nativeCode);
}
size_t context::allocation(size_t size)
{
/* Find next available index. */
while (_minEmptyArrayIndex < _arrays.size() && _arrays[_minEmptyArrayIndex])
++_minEmptyArrayIndex;
if (_minEmptyArrayIndex == _arrays.size())
{
_arrays.push_back(nullptr);
_minEmptyArrayIndex = _arrays.size() - 1;
}
_arrays[_minEmptyArrayIndex] = ::array::create(_mm, size);
return _minEmptyArrayIndex;
}
void context::abandonment(size_t index)
{
if (index >= _arrays.size())
throw exceptions::invalidArrayIndex
(L"Attempt to an abandon an unallocated array", index);
if (_array0Source == index)
_array0Source = 0;
_arrays[index]->destroy(_mm);
_arrays[index] = 0;
if (index < _minEmptyArrayIndex)
_minEmptyArrayIndex = index;
}
void context::output(unsigned char v)
{
_os << static_cast<char>(v);
if (v == '\n')
_os << flush;
}
unsigned int context::input()
{
istream::int_type v = _is.get();
return v == istream::traits_type::eof() ? ~static_cast<unsigned int>(0) : v;
}
void context::loadProgram(size_t index)
{
if (index == 0)
throw exceptions::invalidArrayIndex(L"Can not load array 0", 0);
if (index >= _arrays.size()
|| _arrays[index] == nullptr)
throw exceptions::invalidArrayIndex
(L"Attempt to load an array that is not allocated", index);
::array * array0 = _arrays[0];
if (!array0->dirty() && _array0Source != 0)
{
::array * oldSource = _arrays[_array0Source];
if (!oldSource->dirty())
{
oldSource->_nativeCode = array0->_nativeCode;
array0->_nativeCode = nullptr;
}
}
array0->destroy(_mm);
::array * source = _arrays[index];
if (source->dirty() || !source->_nativeCode)
generateNativeCode(*source);
array0 = _arrays[0] = source->clone(_mm);
array0->_nativeCode = source->_nativeCode;
source->_nativeCode = nullptr;
_array0Source = index;
}