-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathqb_native_compiler.c
1449 lines (1346 loc) · 48.8 KB
/
qb_native_compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Chung Leong <cleong@cal.berkeley.edu> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "qb.h"
#if NATIVE_COMPILE_ENABLED
ZEND_ATTRIBUTE_FORMAT(printf, 2, 3)
static void qb_printf(qb_native_compiler_context *cxt, const char *format, ...) {
USE_TSRM
va_list args;
va_start(args, format);
vfprintf(cxt->write_stream, format, args);
va_end(args);
if(cxt->print_source) {
char *buffer;
int len;
va_start(args, format);
len = vspprintf(&buffer, 0, format, args);
php_write(buffer, len TSRMLS_CC);
efree(buffer);
}
}
static void qb_print(qb_native_compiler_context *cxt, const char *s) {
USE_TSRM
uint32_t len = (uint32_t) strlen(s);
if(cxt->print_source) {
php_write((char *) s, len TSRMLS_CC);
}
fwrite(s, 1, len, cxt->write_stream);
}
static void qb_print_macros(qb_native_compiler_context *cxt) {
qb_print(cxt, "#define NAN "STRING(NAN)"\n");
qb_print(cxt, "#define INF "STRING(INFINITY)"\n");
qb_print(cxt, "#define restrict __restrict\n");
qb_print(cxt, "#define NULL "STRING(NULL)"\n");
qb_print(cxt, "#define M_PI "STRING(M_PI)"\n");
qb_print(cxt, "#define MAX_DIMENSION "STRING(MAX_DIMENSION)"\n");
#ifdef FAST_FLOAT_TO_INT
qb_print(cxt, "#define FAST_FLOAT_TO_INT\n");
#endif
#ifdef QB_LITTLE_ENDIAN
qb_print(cxt, "#define QB_LITTLE_ENDIAN\n");
#else
qb_print(cxt, "#define QB_BIG_ENDIAN\n");
#endif
qb_print(cxt, "#define TSRMLS_C "STRING(TSRMLS_C)"\n");
qb_print(cxt, "#define TSRMLS_CC "STRING(TSRMLS_CC)"\n");
qb_print(cxt, "#define TSRMLS_D "STRING(TSRMLS_D)"\n");
qb_print(cxt, "#define TSRMLS_DC "STRING(TSRMLS_DC)"\n");
qb_print(cxt, "#define PRId8 "STRING(PRId8)"\n");
qb_print(cxt, "#define PRId16 "STRING(PRId16)"\n");
qb_print(cxt, "#define PRId32 "STRING(PRId32)"\n");
qb_print(cxt, "#define PRId64 "STRING(PRId64)"\n");
qb_print(cxt, "#define PRIu8 "STRING(PRIu8)"\n");
qb_print(cxt, "#define PRIu16 "STRING(PRIu16)"\n");
qb_print(cxt, "#define PRIu32 "STRING(PRIu32)"\n");
qb_print(cxt, "#define PRIu64 "STRING(PRIu64)"\n");
qb_print(cxt, "#define NO_RETURN "STRING(NO_RETURN)"\n");
qb_print(cxt, "#define EXPECTED(c) "STRING(EXPECTED(c))"\n");
qb_print(cxt, "#define UNEXPECTED(c) "STRING(UNEXPECTED(c))"\n");
#if defined(ZEND_DEBUG) && defined(_MSC_VER)
qb_print(cxt, "#define zend_always_inline __forceinline\n");
#else
qb_print(cxt, "#define zend_always_inline "STRING(zend_always_inline)"\n");
#endif
qb_print(cxt, "#define SWAP_BE_I16(v) "STRING(SWAP_BE_I16(v))"\n");
qb_print(cxt, "#define SWAP_BE_I32(v) "STRING(SWAP_BE_I32(v))"\n");
qb_print(cxt, "#define SWAP_BE_I64(v) "STRING(SWAP_BE_I64(v))"\n");
qb_print(cxt, "#define SWAP_LE_I16(v) "STRING(SWAP_LE_I16(v))"\n");
qb_print(cxt, "#define SWAP_LE_I32(v) "STRING(SWAP_LE_I32(v))"\n");
qb_print(cxt, "#define SWAP_LE_I64(v) "STRING(SWAP_LE_I64(v))"\n");
qb_print(cxt, "#define TRUE "STRING(TRUE)"\n");
qb_print(cxt, "#define FALSE "STRING(FALSE)"\n");
#if defined(HAVE_COMPLEX_H)
qb_print(cxt, "#define cmult(x, y) ((x) * (y))\n");
qb_print(cxt, "#define cmultf(x, y) ((x) * (y))\n");
qb_print(cxt, "#define cdiv(x, y) ((x) / (y))\n");
qb_print(cxt, "#define cdivf(x, y) ((x) / (y))\n");
#endif
#ifdef __GNUC__
#ifndef __builtin_bswap16
qb_print(cxt, "#define __builtin_bswap16(n) ((n >> 8) | (n << 8))\n");
#endif
#endif
}
static void qb_print_typedefs(qb_native_compiler_context *cxt) {
#ifdef __GNUC__
qb_print(cxt, STRING(typedef __SIZE_TYPE__ size_t;)"\n");
#endif
#ifdef _MSC_VER
#ifdef _WIN64
qb_print(cxt, "typedef unsigned __int64 size_t;\n");
#else
qb_print(cxt, "typedef unsigned int size_t;\n");
#endif
#endif
qb_print(cxt, "typedef char int8_t;\n");
qb_print(cxt, "typedef unsigned char uint8_t;\n");
qb_print(cxt, "typedef short int16_t;\n");
qb_print(cxt, "typedef unsigned short uint16_t;\n");
qb_print(cxt, "typedef int int32_t;\n");
qb_print(cxt, "typedef unsigned int uint32_t;\n");
#if SIZEOF_LONG == 8
qb_print(cxt, "typedef long int64_t;\n");
qb_print(cxt, "typedef unsigned long uint64_t;\n");
#else
qb_print(cxt, "typedef long long int64_t;\n");
qb_print(cxt, "typedef unsigned long long uint64_t;\n");
#endif
#if SIZEOF_PTRDIFF_T == 8
qb_print(cxt, "typedef int64_t intptr_t;\n");
qb_print(cxt, "typedef uint64_t uintptr_t;\n");
#else
qb_print(cxt, "typedef int32_t intptr_t;\n");
qb_print(cxt, "typedef uint32_t uintptr_t;\n");
#endif
qb_print(cxt, "typedef float float32_t;\n");
qb_print(cxt, "typedef double float64_t;\n");
#if defined(HAVE_COMPLEX_H)
qb_print(cxt, "typedef float "STRING(complex)" cfloat32_t;\n");
qb_print(cxt, "typedef double "STRING(complex)" cfloat64_t;\n");
#else
qb_print(cxt, "typedef struct cfloat32_t cfloat32_t;\n");
qb_print(cxt, "typedef struct cfloat64_t cfloat64_t;\n");
qb_print(cxt, "\
struct cfloat32_t {\
float r;\
float i;\
};\
\n");
qb_print(cxt, "struct cfloat64_t {\
double r;\
double i;\
};\
\n");
#endif
qb_print(cxt, "\n");
qb_print(cxt, "typedef struct qb_address\tqb_address;\n");
qb_print(cxt, "typedef struct qb_variable\tqb_variable;\n");
qb_print(cxt, "typedef struct zend_op_array\tzend_op_array;\n");
qb_print(cxt, "typedef struct zval\tzval;\n");
qb_print(cxt, "typedef struct php_stream\tphp_stream;\n");
qb_print(cxt, "typedef struct qb_index_alias_scheme\tqb_index_alias_scheme;\n");
qb_print(cxt, "typedef struct qb_memory_segment\tqb_memory_segment;\n");
qb_print(cxt, "typedef struct qb_storage\tqb_storage;\n");
qb_print(cxt, "typedef struct qb_function\tqb_function;\n");
qb_print(cxt, "typedef struct qb_interpreter_context\tqb_interpreter_context;\n");
qb_print(cxt, "typedef struct qb_external_symbol\tqb_external_symbol;\n");
qb_print(cxt, "typedef enum qb_vm_exit_type\tqb_vm_exit_type;\n");
qb_print(cxt, "\
enum qb_vm_exit_type {\
QB_VM_RETURN = 0,\
QB_VM_ERROR,\
QB_VM_TIMEOUT,\
QB_VM_FORK,\
QB_VM_SPOON,\
QB_VM_EXCEPTION,\
QB_VM_YIELD,\
};\
\n");
qb_print(cxt, "\
struct qb_memory_segment {\
int8_t *memory;\
uint32_t flags;\
uint32_t byte_count;\
uint32_t current_allocation;\
php_stream *stream;\
qb_memory_segment *imported_segment;\
qb_memory_segment *next_dependent;\
uintptr_t **references;\
uint32_t reference_count;\
};\
\n");
qb_print(cxt, "\
struct qb_storage {\
uint32_t size;\
qb_memory_segment *segments;\
uint32_t segment_count;\
uint32_t flags;\
};\
\n");
qb_print(cxt, "\
struct qb_external_symbol {\
long hash_value;\
const char *name;\
void *address;\
};\
\n");
qb_print(cxt, "\
struct qb_function {\
int8_t *instructions;\
int8_t *instruction_start;\
uint64_t instruction_crc64;\
uint32_t instruction_length;\
uint16_t *instruction_opcodes;\
uint32_t instruction_opcode_count;\
uint32_t flags;\
qb_variable *return_variable;\
qb_variable *return_key_variable;\
qb_variable *sent_variable;\
qb_variable **variables;\
uint32_t variable_count;\
uint32_t argument_count;\
qb_storage *local_storage;\
const char *name;\
uint32_t line_id;\
void *native_proc;\
uintptr_t instruction_base_address;\
uintptr_t local_storage_base_address;\
zend_op_array *zend_op_array;\
qb_function *next_reentrance_copy;\
qb_function *next_forked_copy;\
volatile int32_t in_use;\
};\
\n");
qb_print(cxt, "\
struct qb_interpreter_context {\
qb_function *function;\
int8_t *instruction_pointer;\
qb_interpreter_context *caller_context;\
uint32_t thread_count;\
uint32_t fork_id;\
uint32_t fork_count;\
uint32_t *argument_indices;\
uint32_t argument_count;\
uint32_t result_index;\
uint32_t line_id;\
uint32_t call_depth;\
void *send_target;\
qb_vm_exit_type exit_type;\
int32_t exit_status_code;\
int32_t exception_encountered;\
uint32_t debug_flags;\
volatile unsigned char *windows_timed_out_pointer;\
int floating_point_precision;\
void ***tsrm_ls;\
};\
\n");
#if ZEND_DEBUG
qb_print(cxt, "\
typedef struct qb_native_proc_record {\
uint64_t crc64;\
void *proc;\
} qb_native_proc_record;\
\n");
#endif
qb_print(cxt, "\n");
}
static void qb_print_prototypes(qb_native_compiler_context *cxt) {
uint32_t i, j, k;
int32_t *prototype_indices;
uint32_t prototype_count = cxt->pool->function_prototype_count;
int32_t *required;
qb_op *qop;
// see which functions are required
required = alloca(sizeof(int32_t) * prototype_count);
memset(required, 0, sizeof(int32_t) * prototype_count);
for(i = 0; i < cxt->compiler_context_count; i++) {
qb_compiler_context *compiler_cxt = cxt->compiler_contexts[i];
if(!compiler_cxt->compiled_function->native_proc && (compiler_cxt->function_flags & QB_FUNCTION_NATIVE_IF_POSSIBLE)) {
// go through all ops and see what functions they use
for(j = 0; j < compiler_cxt->op_count; j++) {
qop = compiler_cxt->ops[j];
prototype_indices = cxt->op_function_usages[qop->opcode];
for(k = 0; prototype_indices[k] != 0xFFFFFFFF; k++) {
uint32_t index = prototype_indices[k];
required[index] = TRUE;
}
}
}
}
// print prototypes, maintaining correct order
for(i = 0; i < prototype_count; i++) {
if(required[i]) {
const char *prototype = cxt->function_prototypes[i];
qb_native_symbol *symbol = &global_native_symbols[i];
if(symbol->flags & QB_NATIVE_SYMBOL_UNUSED) {
continue;
}
#ifdef _WIN64
if(!(symbol->flags & QB_NATIVE_SYMBOL_INLINE_FUNCTION)) {
if(symbol->flags & QB_NATIVE_SYMBOL_INTRINSIC_FUNCTION) {
// redefine it to something else so we can declare it
qb_printf(cxt, "#define %s intrinsic_%s\n", symbol->name, symbol->name);
}
// add __declspec so function looks like a something frm a DLL
// otherwise we might be able to reach it due to use of 32-bit pointers
qb_print(cxt, "__declspec(dllimport)\n");
} else if(symbol->flags & QB_NATIVE_SYMBOL_INTRINSIC_FUNCTION) {
continue;
}
#endif
qb_print(cxt, prototype);
qb_print(cxt, "\n");
}
}
qb_print(cxt, "\n");
}
static char * qb_get_buffer(qb_native_compiler_context *cxt) {
if(cxt->string_buffer_index == 16) {
cxt->string_buffer_index = 0;
}
return cxt->string_buffers[cxt->string_buffer_index++];
}
static qb_address * qb_get_root_address(qb_native_compiler_context *cxt, qb_address *address) {
while(address->source_address) {
address = address->source_address;
}
return address;
}
static int32_t qb_cast_required(qb_native_compiler_context *cxt, qb_address *address) {
qb_address *base_address = qb_get_root_address(cxt, address);
return (base_address->type != address->type);
}
static int32_t qb_offset_required(qb_native_compiler_context *cxt, qb_address *address) {
if(address->array_index_address != cxt->zero_address) {
return TRUE;
} else {
qb_address *base_address = qb_get_root_address(cxt, address);
return (base_address->segment_offset != address->segment_offset);
}
}
static qb_access_method qb_get_scalar_access_method(qb_native_compiler_context *cxt, qb_address *address) {
switch(address->segment_selector) {
case QB_SELECTOR_CONSTANT_ARRAY:
case QB_SELECTOR_CONSTANT_SCALAR: return QB_SCALAR_LITERAL;
case QB_SELECTOR_CLASS_SCALAR:
case QB_SELECTOR_OBJECT_SCALAR:
case QB_SELECTOR_GLOBAL_SCALAR:
case QB_SELECTOR_STATIC_SCALAR:
case QB_SELECTOR_SHARED_SCALAR: return QB_SCALAR_POINTER;
case QB_SELECTOR_LOCAL_SCALAR:
case QB_SELECTOR_TEMPORARY_SCALAR: {
if(address->flags & QB_ADDRESS_DIMENSION) {
return QB_SCALAR_POINTER;
} else {
return QB_SCALAR_LOCAL_VARIABLE;
}
}
case INVALID_INDEX: {
return QB_ARRAY_UNUSED;
}
default: return QB_SCALAR_ELEMENT;
}
}
static qb_access_method qb_get_array_access_method(qb_native_compiler_context *cxt, qb_address *address) {
switch(address->segment_selector) {
case QB_SELECTOR_CONSTANT_SCALAR:
case QB_SELECTOR_CLASS_SCALAR:
case QB_SELECTOR_OBJECT_SCALAR:
case QB_SELECTOR_GLOBAL_SCALAR:
case QB_SELECTOR_STATIC_SCALAR:
case QB_SELECTOR_SHARED_SCALAR: return QB_SCALAR_POINTER;
case QB_SELECTOR_LOCAL_SCALAR:
case QB_SELECTOR_TEMPORARY_SCALAR: {
if(address->flags & QB_ADDRESS_DIMENSION) {
return QB_SCALAR_POINTER;
} else {
return QB_SCALAR_LOCAL_VARIABLE;
}
}
case QB_SELECTOR_CONSTANT_ARRAY:
case QB_SELECTOR_CLASS_ARRAY:
case QB_SELECTOR_OBJECT_ARRAY:
case QB_SELECTOR_GLOBAL_ARRAY:
case QB_SELECTOR_STATIC_ARRAY:
case QB_SELECTOR_SHARED_ARRAY:
case QB_SELECTOR_LOCAL_ARRAY:
case QB_SELECTOR_TEMPORARY_ARRAY: {
if(qb_offset_required(cxt, address)) {
return QB_ARRAY_SLICE;
} else {
return QB_ARRAY_POINTER;
}
}
case INVALID_INDEX: {
return QB_ARRAY_UNUSED;
}
default: {
if(qb_offset_required(cxt, address)) {
return QB_ARRAY_SLICE;
} else {
return QB_ARRAY_POINTER_POINTER;
}
}
}
}
static const char * qb_get_pointer(qb_native_compiler_context *cxt, qb_address *address);
static const char * qb_get_scalar(qb_native_compiler_context *cxt, qb_address *address) {
char *buffer = qb_get_buffer(cxt);
const char *ctype = qb_cast_required(cxt, address) ? type_cnames[address->type] : NULL;
qb_access_method method = qb_get_scalar_access_method(cxt, address);
switch(method) {
case QB_SCALAR_LITERAL: {
// scalar is a constant
switch(address->type) {
case QB_TYPE_S08: snprintf(buffer, 128, "%" PRId8, VALUE(S08, address)); break;
case QB_TYPE_U08: snprintf(buffer, 128, "%" PRIu8"U", VALUE(U08, address)); break;
case QB_TYPE_S16: snprintf(buffer, 128, "%" PRId16, VALUE(S16, address)); break;
case QB_TYPE_U16: snprintf(buffer, 128, "%" PRIu16"U", VALUE(U16, address)); break;
case QB_TYPE_S32: snprintf(buffer, 128, "%" PRId32, VALUE(S32, address)); break;
case QB_TYPE_U32: snprintf(buffer, 128, "%" PRIu32"U", VALUE(U32, address)); break;
case QB_TYPE_S64: snprintf(buffer, 128, "%" PRId64"LL", VALUE(S64, address)); break;
case QB_TYPE_U64: snprintf(buffer, 128, "%" PRIu64"ULL", VALUE(U64, address)); break;
case QB_TYPE_F32: {
float32_t number = VALUE(F32, address);
if(isnan(number)) {
snprintf(buffer, 128, "NAN");
} else if(zend_finite(number)) {
snprintf(buffer, 128, "%.11ff", number);
} else {
if(number > 0) {
snprintf(buffer, 128, "INF");
} else {
snprintf(buffer, 128, "-INF");
}
}
} break;
case QB_TYPE_F64: {
float64_t number = VALUE(F64, address);
if(isnan(number)) {
snprintf(buffer, 128, "NAN");
} else if(zend_finite(number)) {
snprintf(buffer, 128, "%.17f", number);
} else {
if(number > 0) {
snprintf(buffer, 128, "INF");
} else {
snprintf(buffer, 128, "-INF");
}
}
} break;
default: break;
}
} break;
case QB_SCALAR_POINTER: {
// scalar is referenced by pointer
if(ctype) {
snprintf(buffer, 128, "((%s) (*var_ptr_%u_%u))", ctype, address->segment_selector, address->segment_offset);
} else {
snprintf(buffer, 128, "(*var_ptr_%u_%u)", address->segment_selector, address->segment_offset);
}
} break;
case QB_SCALAR_LOCAL_VARIABLE: {
// scalar is a local variable
if(ctype) {
snprintf(buffer, 128, "((%s) var_%u_%u)", ctype, address->segment_selector, address->segment_offset);
} else {
snprintf(buffer, 128, "var_%u_%u", address->segment_selector, address->segment_offset);
}
} break;
case QB_SCALAR_ELEMENT: {
// array elements
qb_address *base_address = qb_get_root_address(cxt, address);
const char *container = qb_get_pointer(cxt, base_address);
if(address->array_index_address == cxt->zero_address) {
// referenced by a fixed index
uint32_t index = ELEMENT_COUNT(address->segment_offset - base_address->segment_offset, address->type);
if(ctype) {
snprintf(buffer, 128, "((%s) %s[%u])", ctype, container, index);
} else {
snprintf(buffer, 128, "(%s[%u])", container, index);
}
} else {
// variable index
const char *index = qb_get_scalar(cxt, address->array_index_address);
if(ctype) {
snprintf(buffer, 128, "((%s) %s[%s])", ctype, container, index);
} else {
snprintf(buffer, 128, "(%s[%s])", container, index);
}
}
} break;
default: {
} break;
}
return buffer;
}
static const char * qb_get_pointer(qb_native_compiler_context *cxt, qb_address *address) {
char *buffer = qb_get_buffer(cxt);
const char *ctype = qb_cast_required(cxt, address) ? type_cnames[address->type] : NULL;
qb_access_method method = qb_get_array_access_method(cxt, address);
switch(method) {
case QB_SCALAR_POINTER: {
if(ctype) {
snprintf(buffer, 128, "((%s *) var_ptr_%u_%u)", ctype, address->segment_selector, address->segment_offset);
} else {
snprintf(buffer, 128, "var_ptr_%u_%u", address->segment_selector, address->segment_offset);
}
} break;
case QB_SCALAR_LOCAL_VARIABLE: {
if(ctype) {
snprintf(buffer, 128, "((%s *) &var_%u_%u)", ctype, address->segment_selector, address->segment_offset);
} else {
snprintf(buffer, 128, "&var_%u_%u", address->segment_selector, address->segment_offset);
}
} break;
case QB_ARRAY_POINTER: {
// fixed-length array
if(ctype) {
snprintf(buffer, 128, "((%s *) var_ptr_%u_%u)", ctype, address->segment_selector, address->segment_offset);
} else {
snprintf(buffer, 128, "var_ptr_%u_%u", address->segment_selector, address->segment_offset);
}
} break;
case QB_ARRAY_POINTER_POINTER: {
// a variable-length array--dereference the pointer to pointer
if(ctype) {
snprintf(buffer, 128, "((%s *) (*var_ptr_ptr_%u))", ctype, address->segment_selector);
} else {
snprintf(buffer, 128, "(*var_ptr_ptr_%u)", address->segment_selector);
}
} break;
case QB_ARRAY_SLICE: {
qb_address *base_address = qb_get_root_address(cxt, address);
const char *container = qb_get_pointer(cxt, base_address);
if(address->array_index_address == cxt->zero_address) {
uint32_t offset = ELEMENT_COUNT(address->segment_offset - base_address->segment_offset, address->type);
if(ctype) {
snprintf(buffer, 128, "(((%s *) %s) + %u)", ctype, container, offset);
} else {
snprintf(buffer, 128, "(%s + %u)", container, offset);
}
} else {
const char *offset = qb_get_scalar(cxt, address->array_index_address);
if(ctype) {
snprintf(buffer, 128, "(((%s *) %s) + %s)", ctype, container, offset);
} else {
snprintf(buffer, 128, "(%s + %s)", container, offset);
}
}
} break;
default: {
} break;
}
return buffer;
}
static const char * qb_get_jump_label(qb_native_compiler_context *cxt, uint32_t jump_target_index) {
char *buffer = qb_get_buffer(cxt);
snprintf(buffer, 128, "L%04d", jump_target_index);
return buffer;
}
static const char * qb_get_op_action(qb_native_compiler_context *cxt, uint32_t opcode) {
return cxt->op_actions[opcode];
}
extern const char compressed_table_op_names[];
static const char * qb_get_op_name(qb_native_compiler_context *cxt, uint32_t opcode) {
if(!cxt->pool->op_names) {
// decompress the opname table
qb_uncompress_table(compressed_table_op_names, (void ***) &cxt->pool->op_names, &cxt->pool->op_name_count, 0);
}
if(cxt->pool->op_names && opcode < cxt->pool->op_name_count) {
return cxt->pool->op_names[opcode];
}
return "?";
}
static void qb_copy_local_scalar_to_storage(qb_native_compiler_context *cxt, qb_address *address) {
qb_access_method method = qb_get_scalar_access_method(cxt, address);
if(method == QB_SCALAR_LOCAL_VARIABLE) {
const char *c_type = type_cnames[address->type];
qb_printf(cxt, "*((%s *) (storage->segments[%u].memory + %u)) = var_%u_%u;\n", c_type, address->segment_selector, address->segment_offset, address->segment_selector, address->segment_offset);
}
}
static void qb_copy_local_variables_to_storage(qb_native_compiler_context *cxt, qb_op *qop) {
uint32_t i, j;
for(i = 0; i < qop->operand_count; i++) {
qb_operand *operand = &qop->operands[i];
if(operand->type == QB_OPERAND_ADDRESS) {
qb_address *address = operand->address;
switch(address->mode) {
case QB_ADDRESS_MODE_SCA: {
qb_copy_local_scalar_to_storage(cxt, address);
} break;
case QB_ADDRESS_MODE_ELE: {
qb_copy_local_scalar_to_storage(cxt, address);
qb_copy_local_scalar_to_storage(cxt, address->array_index_address);
} break;
case QB_ADDRESS_MODE_ARR: {
qb_copy_local_scalar_to_storage(cxt, address);
qb_copy_local_scalar_to_storage(cxt, address->array_index_address);
for(j = 0; j < address->dimension_count; j++) {
qb_copy_local_scalar_to_storage(cxt, address->array_size_addresses[j]);
if(j != address->dimension_count - 1) {
qb_copy_local_scalar_to_storage(cxt, address->dimension_addresses[j]);
}
}
} break;
default: {
} break;
}
}
}
}
static void qb_copy_local_arguments_to_storage(qb_native_compiler_context *cxt, qb_op *qop) {
uint32_t *var_indices = ARRAY(U32, qop->operands[1].address);
uint32_t arg_count = ARRAY_SIZE(qop->operands[1].address);
uint32_t i;
for(i = 0; i < arg_count; i++) {
qb_variable *qvar = cxt->variables[var_indices[i]];
if(qvar->address) {
switch(qvar->address->mode) {
case QB_ADDRESS_MODE_SCA: {
qb_copy_local_scalar_to_storage(cxt, qvar->address);
} break;
default: {
} break;
}
}
}
}
static void qb_copy_all_local_variables_to_storage(qb_native_compiler_context *cxt) {
uint32_t i;
for(i = 0; i < cxt->writable_scalar_count; i++) {
qb_address *address = cxt->writable_scalars[i];
qb_copy_local_scalar_to_storage(cxt, address);
}
}
static void qb_copy_local_scalar_from_storage(qb_native_compiler_context *cxt, qb_address *address) {
qb_access_method method = qb_get_scalar_access_method(cxt, address);
if(method == QB_SCALAR_LOCAL_VARIABLE) {
const char *c_type = type_cnames[address->type];
qb_printf(cxt, "var_%u_%u = *((%s *) (storage->segments[%u].memory + %u));\n", address->segment_selector, address->segment_offset, c_type, address->segment_selector, address->segment_offset);
}
}
static void qb_copy_local_variables_from_storage(qb_native_compiler_context *cxt, qb_op *qop) {
uint32_t i;
for(i = 0; i < qop->operand_count; i++) {
qb_operand *operand = &qop->operands[i];
if(operand->type == QB_OPERAND_ADDRESS) {
if(qb_is_operand_write_target(qop->opcode, i)) {
qb_address *address = operand->address;
switch(address->mode) {
case QB_ADDRESS_MODE_SCA: {
qb_copy_local_scalar_from_storage(cxt, address);
} break;
case QB_ADDRESS_MODE_ELE: {
qb_copy_local_scalar_from_storage(cxt, address);
qb_copy_local_scalar_from_storage(cxt, address->array_index_address);
} break;
case QB_ADDRESS_MODE_ARR: {
qb_copy_local_scalar_from_storage(cxt, address);
qb_copy_local_scalar_from_storage(cxt, address->array_index_address);
} break;
default: {
} break;
}
}
}
}
}
static void qb_copy_local_arguments_from_storage(qb_native_compiler_context *cxt, qb_op *qop) {
USE_TSRM
uint32_t symbol_index = VALUE(U32, qop->operands[0].address);
qb_external_symbol *symbol = &QB_G(external_symbols)[symbol_index];
zend_function *zfunc = symbol->pointer;
qb_function *qfunc = qb_get_compiled_function(zfunc);
uint32_t *var_indices = ARRAY(U32, qop->operands[1].address);
uint32_t arg_count = ARRAY_SIZE(qop->operands[1].address);
uint32_t retval_index = VALUE(U32, qop->operands[2].address);
uint32_t i;
for(i = 0; i < arg_count; i++) {
int32_t by_ref;
if(qfunc) {
if(i < qfunc->argument_count ) {
qb_variable *arg = qfunc->variables[i];
by_ref = (arg->flags & QB_VARIABLE_BY_REF);
} else {
by_ref = FALSE;
}
} else {
if(zfunc->common.arg_info && i < zfunc->common.num_args) {
by_ref = zfunc->common.arg_info[i].pass_by_reference;
} else {
by_ref = TRUE;
}
}
if(by_ref) {
qb_variable *qvar = cxt->variables[var_indices[i]];
qb_address *address = qvar->address;
switch(address->mode) {
case QB_ADDRESS_MODE_SCA: {
qb_copy_local_scalar_from_storage(cxt, address);
} break;
default: {
} break;
}
}
}
if(retval_index != INVALID_INDEX) {
qb_variable *qvar = cxt->variables[retval_index];
qb_address *address = qvar->address;
switch(address->mode) {
case QB_ADDRESS_MODE_SCA: {
qb_copy_local_scalar_from_storage(cxt, address);
} break;
default: {
} break;
}
}
}
static void qb_print_op(qb_native_compiler_context *cxt, qb_op *qop, uint32_t qop_index) {
if(qop->flags & QB_OP_JUMP_TARGET) {
const char *label = qb_get_jump_label(cxt, qop_index);
qb_printf(cxt, "%s:\n", label);
}
if(qop->opcode != QB_NOP) {
const char *name;
const char *action;
uint32_t i;
if(cxt->print_source || TRUE) {
name = qb_get_op_name(cxt, qop->opcode);
qb_printf(cxt, "// %s (line #%u)\n", name, LINE_NUMBER(qop->line_id));
}
// define the operands
if(qop->flags & QB_OP_NEED_LINE_IDENTIFIER) {
qb_printf(cxt, "#define line_id %u\n", qop->line_id);
}
for(i = 0; i < qop->operand_count; i++) {
qb_operand *operand = &qop->operands[i];
qb_address *address = operand->address;
char name[8];
if(i == qop->operand_count - 1 && qb_is_operand_write_target(qop->opcode, i)) {
sprintf(name, "res");
} else {
sprintf(name, "op%u", i + 1);
}
if(operand->type == QB_OPERAND_ADDRESS) {
switch(address->mode) {
case QB_ADDRESS_MODE_SCA:
case QB_ADDRESS_MODE_ELE: {
const char *scalar = qb_get_scalar(cxt, address);
qb_printf(cxt, "#define %s %s\n", name, scalar);
} break;
case QB_ADDRESS_MODE_ARR: {
if(address->array_size_address != cxt->zero_address) {
const char *pointer = qb_get_pointer(cxt, address);
const char *count = qb_get_scalar(cxt, address->array_size_address);
const char *count_pointer = qb_get_pointer(cxt, address->array_size_address);
if(qb_is_operand_complex(qop->opcode, i)) {
char *buffer = qb_get_buffer(cxt);
snprintf(buffer, 128, "((c%s *) (%s))", type_cnames[operand->address->type], pointer);
pointer = buffer;
}
qb_printf(cxt, "#define %s_ptr %s\n", name, pointer);
qb_printf(cxt, "#define %s_count %s\n", name, count);
qb_printf(cxt, "#define %s_count_ptr %s\n", name, count_pointer);
} else {
qb_printf(cxt, "#define %s_ptr NULL\n", name);
qb_printf(cxt, "#define %s_count 0U\n", name);
}
} break;
default: {
} break;
}
} else if(operand->type == QB_OPERAND_SEGMENT_SELECTOR) {
qb_printf(cxt, "#define %s %u\n", name, address->segment_selector);
} else if(operand->type == QB_OPERAND_ELEMENT_SIZE) {
qb_printf(cxt, "#define %s %u\n", name, BYTE_COUNT(1, address->type));
} else if(operand->type == QB_OPERAND_NUMBER) {
qb_printf(cxt, "#define %s %u\n", name, operand->number);
}
}
qb_print(cxt, "\n");
if(qop->flags & QB_OP_NEED_INSTRUCTION_STRUCT) {
qb_copy_local_variables_to_storage(cxt, qop);
qb_printf(cxt, "ip = cxt->function->instructions + %u;\n", qop->instruction_offset);
qb_print(cxt, "\n");
} else if(qop->opcode == QB_RET) {
if(cxt->compiled_function->return_variable->address) {
if(IS_SCALAR(cxt->compiled_function->return_variable->address)) {
qb_copy_local_scalar_to_storage(cxt, cxt->compiled_function->return_variable->address);
qb_print(cxt, "\n");
}
}
} else if(qop->opcode == QB_FCALL_U32_U32_U32) {
qb_copy_local_arguments_to_storage(cxt, qop);
qb_print(cxt, "\n");
} else if(qop->opcode == QB_END_STATIC || qop->opcode == QB_RESUME || qop->opcode == QB_SPOON) {
qb_printf(cxt, "ip = cxt->function->instructions + %u;\n", qop->instruction_offset);
} else if(qop->opcode == QB_INTR || qop->opcode == QB_FORK_U32) {
qb_printf(cxt, "ip = cxt->function->instructions + %u;\n", qop->instruction_offset);
qb_copy_all_local_variables_to_storage(cxt);
}
// print code that actually performs the action
action = qb_get_op_action(cxt, qop->opcode);
if(action) {
qb_print(cxt, action);
qb_print(cxt, "\n");
}
if(qop->flags & QB_OP_NEED_INSTRUCTION_STRUCT) {
qb_copy_local_variables_from_storage(cxt, qop);
qb_print(cxt, "\n");
} else if(qop->opcode == QB_FCALL_U32_U32_U32) {
qb_copy_local_arguments_from_storage(cxt, qop);
qb_print(cxt, "\n");
} else if(qop->opcode == QB_END_STATIC || qop->opcode == QB_INTR || qop->opcode == QB_RESUME || qop->opcode == QB_SPOON) {
if(!(cxt->ops[qop_index + 1]->flags & QB_OP_JUMP_TARGET)) {
const char *jump_target = qb_get_jump_label(cxt, qop_index + 1);
qb_printf(cxt, "%s:\n", jump_target);
}
}
#ifdef ZEND_WIN32
if(qop->flags & (QB_OP_BRANCH | QB_OP_JUMP)) {
// check for timed-out condition on any backward jump
uint32_t j;
for(j = 0; j < qop->jump_target_count; j++) {
if(qop->jump_target_indices[j] <= qop_index) {
qb_print(cxt, "if(*cxt->windows_timed_out_pointer) {\n");
qb_print(cxt, "cxt->exit_type = QB_VM_TIMEOUT;\n");
qb_print(cxt, "return;\n");
qb_print(cxt, "}\n");
break;
}
}
}
#endif
if(qop->flags & QB_OP_BRANCH) {
if(qop->jump_target_indices[1] == qop_index + 1) {
const char *jump_target = qb_get_jump_label(cxt, qop->jump_target_indices[0]);
qb_printf(cxt, "if(condition) goto %s;\n", jump_target);
} else if(qop->jump_target_indices[0] == qop_index + 1) {
const char *jump_target = qb_get_jump_label(cxt, qop->jump_target_indices[1]);
qb_printf(cxt, "if(!condition) goto %s;\n", jump_target);
} else {
const char *jump_target1 = qb_get_jump_label(cxt, qop->jump_target_indices[0]);
const char *jump_target2 = qb_get_jump_label(cxt, qop->jump_target_indices[1]);
qb_printf(cxt, "if(condition) goto %s; else goto %s;\n", jump_target1, jump_target2);
}
qb_print(cxt, "\n");
} else if(qop->flags & QB_OP_JUMP) {
const char *jump_target = qb_get_jump_label(cxt, qop->jump_target_indices[0]);
qb_printf(cxt, "goto %s;\n", jump_target);
qb_print(cxt, "\n");
} else if(qop->flags & QB_OP_BRANCH_TABLE) {
#ifdef __GNUC__
// use computed goto
qb_printf(cxt, "static void *branch_table_%u[%u] = {", qop_index, qop->jump_target_count);
for(i = 0; i < qop->jump_target_count; i++) {
uint32_t jump_target_index = qop->jump_target_indices[i];
const char *jump_target = qb_get_jump_label(cxt, jump_target_index);
qb_printf(cxt, "&&%s,\n", jump_target);
}
qb_print(cxt, "};\n");
qb_printf(cxt, "goto *branch_table_%u[offset];\n", qop_index);
#else
// create a switch statement for MSVC
uint32_t case_count = qop->jump_target_count - 1;
uint32_t default_jump_target_index = qop->jump_target_indices[case_count];
const char *default_jump_target = qb_get_jump_label(cxt, default_jump_target_index);
qb_print(cxt, "switch(offset) {\n");
for(i = 0; i < case_count; i++) {
uint32_t jump_target_index = qop->jump_target_indices[i];
if(jump_target_index != default_jump_target_index) {
uint32_t next_jump_target_index = qop->jump_target_indices[i + 1];
if(jump_target_index == next_jump_target_index) {
// fall through to the next case
qb_printf(cxt, "case %u:\n", i);
} else {
const char *jump_target = qb_get_jump_label(cxt, jump_target_index);
qb_printf(cxt, "case %u: goto %s;\n", i, jump_target);
}
}
}
qb_printf(cxt, "default: goto %s;\n", default_jump_target);
qb_print(cxt, "}\n");
#endif
}
#ifdef ZEND_DEBUG
if(qop->flags & QB_OP_NEED_LINE_IDENTIFIER) {
qb_printf(cxt, "#undef line_id\n");
}
for(i = 0; i < qop->operand_count; i++) {
qb_operand *operand = &qop->operands[i];
qb_address *address = operand->address;
char name[8];
if(i == qop->operand_count - 1 && qb_is_operand_write_target(qop->opcode, i)) {
sprintf(name, "res");
} else {
sprintf(name, "op%u", i + 1);
}
if(operand->type == QB_OPERAND_ADDRESS) {
switch(address->mode) {
case QB_ADDRESS_MODE_SCA:
case QB_ADDRESS_MODE_ELE: {
qb_printf(cxt, "#undef %s\n", name);
} break;
case QB_ADDRESS_MODE_ARR: {
if(address->array_size_address != cxt->zero_address) {
qb_printf(cxt, "#undef %s_ptr\n", name);
qb_printf(cxt, "#undef %s_count\n", name);
qb_printf(cxt, "#undef %s_count_ptr\n", name);
} else {
qb_printf(cxt, "#undef %s_ptr\n", name);
qb_printf(cxt, "#undef %s_count\n", name);
}
} break;
default: {
} break;
}
} else if(operand->type == QB_OPERAND_SEGMENT_SELECTOR) {
qb_printf(cxt, "#undef %s\n", name);
} else if(operand->type == QB_OPERAND_ELEMENT_SIZE) {
qb_printf(cxt, "#undef %s\n", name);
} else if(operand->type == QB_OPERAND_NUMBER) {
qb_printf(cxt, "#undef %s\n", name);
}
}
qb_print(cxt, "\n");
#endif
}
}
static void qb_print_local_variables(qb_native_compiler_context *cxt) {
uint32_t i, j, k;
int32_t *constant_as_pointer;
ALLOCA_FLAG(use_heap);
qb_print(cxt, "int8_t *ip;\n");
qb_print(cxt, "int condition;\n");
qb_print(cxt, "unsigned int offset;\n");
qb_print(cxt, "qb_storage *storage = cxt->function->local_storage;\n");
// create variables for writable scalars
for(i = 0; i < cxt->writable_scalar_count; i++) {
qb_address *address = cxt->writable_scalars[i];
const char *c_type = type_cnames[address->type];
qb_access_method method = qb_get_scalar_access_method(cxt, address);