-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit-x8664.c
1180 lines (992 loc) · 41.2 KB
/
jit-x8664.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
/*
* SDyn: JIT for x86_64. Architecture conventions are described below.
*
* Copyright (c) 2015, 2019 Gregor Richards
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* On x86_64:
* All JIT code conforms to the Unix calling convention.
*
* By the Unix calling convention, the first four arguments go in RDI, RSI,
* RDX, RCX, the return goes in RAX, RSP is the stack pointer and RBP is the
* frame pointer. We use ONLY these registers. RSP must be 16-byte aligned.
*
* RDI is used as the second (collected pointer) stack. RDI will never be
* overwritten by a JIT function, but MAY be overwritten by a normal function,
* so calls to normal functions must save RDI on the conventional stack (RSP)
* and restore it. All functions are guaranteed to preserve RSP and RBP.
* Normal functions intended to be called by the JIT take the pointer stack as
* their first argument, and so the JIT is never responsible for communicating
* it to the GC.
*
* JIT functions themselves take RDI as the pointer stack, RSI as the number
* of arguments, and RDX as the argument array. All arguments must be boxed,
* and thus RDX is frequently (but not necessarily) a region within the
* pointer stack as well. If RSI is 0, RDX may be 0. JIT functions must
* restore RDI to its former value before returning to the caller.
*
* When a JIT function initializes, its conventional stack space is not
* initialized (i.e., it's garbage), but its pointer stack space must be, and
* is initialized to many pointers to sdyn_undefined.
*
* 0(RDI) and 8(RDI) are reserved for temporary collected pointer use.
* -16(RBP) and -8(RBP) are reserved for temporary non-collected or
* non-pointer use. -8(RBP) is generally used to store RDI during calls to
* non-JIT functions, and should be avoided in other cases so there is no
* accidental overlap. Arguments begin at 16(RDI), and storage begins at
* 16+x(RDI), where x is the maximum number of arguments times the word size
* (8).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "sdyn/intrinsics.h"
#include "sdyn/nodes.h"
#include "sdyn/value.h"
BUFFER(size_t, size_t);
/* utility function to create a pointer that's GC'd */
static void **createPointer()
{
void **ret = malloc(sizeof(void *));
if (ret == NULL) {
perror("malloc");
abort();
}
*ret = NULL;
GGC_PUSH_1(*ret);
GGC_GLOBALIZE();
return ret;
}
/* compile IR into a native function */
sdyn_native_function_t sdyn_compile(SDyn_IRNodeArray ir)
{
SDyn_IRNode node = NULL, unode = NULL, onode = NULL;
sdyn_native_function_t ret = NULL;
struct Buffer_uchar buf;
struct Buffer_size_t returns;
struct SJA_X8664_Operand left, right, third, target;
int leftType, rightType, thirdType, targetType;
size_t i, uidx, lastArg, unsuppCount;
long imm;
INIT_BUFFER(buf);
INIT_BUFFER(returns);
/* macros to write pseudo-assembly lines:
* Cn(opcode, operands) for n-ary assembly instructions
* CF(opcode, label) for forwards-referencing jumps
* L(label) to define the label for CF jumps
* IMM64 to load an immediate value of type size_t
* IMM64P to load an immediate pointer value */
#define C3(x, o1, o2, o3) sja_compile(OP3(x, o1, o2, o3), &buf, NULL)
#define C2(x, o1, o2) sja_compile(OP2(x, o1, o2), &buf, NULL)
#define C1(x, o1) sja_compile(OP1(x, o1), &buf, NULL)
#define C0(x) sja_compile(OP0(x), &buf, NULL)
#define CF(x, frel) sja_compile(OP0(x), &buf, &(frel))
#define IMM64(o1, v) do { \
size_t imm64 = (v); \
if (imm64 < 0x100000000L) { \
C2(MOV, o1, IMM(imm64)); \
} else if (imm64 & 0x80000000L) { \
C2(MOV, o1, IMM((~imm64)>>32)); \
C2(SHL, o1, IMM(32)); \
C2(XOR, o1, IMM(imm64&0xFFFFFFFFL)); \
} else { \
C2(MOV, o1, IMM(imm64>>32)); \
C2(SHL, o1, IMM(32)); \
C2(OR, o1, IMM(imm64&0xFFFFFFFFL)); \
} \
} while(0)
#define IMM64P(o1, v) IMM64(o1, (size_t) (void *) (v))
#define L(frel) sja_patchFrel(&buf, (frel))
GGC_PUSH_4(ir, node, unode, onode);
/* for debugging sake, don't fail on unsupported operations until the end */
unsuppCount = 0;
lastArg = 0;
for (i = 0; i < ir->length; i++) {
node = GGC_RAP(ir, i);
unode = node;
/* find our desired targetType by looking for the unified IR node. Our
* own rtype SHOULD be identical, but the unified target is the
* canonical one. */
uidx = i;
while (GGC_RD(unode, uidx) != uidx) {
uidx = GGC_RD(unode, uidx);
unode = GGC_RAP(ir, uidx);
}
targetType = GGC_RD(unode, rtype);
/* macro to load an operand (left, right, third) into a register */
#define LOADOP(opa, defreg) do { \
if (GGC_RD(node, opa)) { \
uidx = GGC_RD(node, opa); \
onode = GGC_RAP(ir, uidx); \
while (GGC_RD(onode, uidx) != uidx) { \
uidx = GGC_RD(onode, uidx); \
onode = GGC_RAP(ir, uidx); \
} \
opa ## Type = GGC_RD(onode, rtype); \
if (GGC_RD(onode, stype) == SDYN_STORAGE_PSTK) { \
opa = defreg; \
C2(MOV, defreg, MEM(8, RDI, 0, RNONE, GGC_RD(onode, addr) * 8 + 16)); \
} else if (GGC_RD(onode, stype) == SDYN_STORAGE_STK) { \
opa = defreg; \
C2(MOV, defreg, MEM(8, RSP, 0, RNONE, GGC_RD(onode, addr) * 8)); \
} \
} \
} while(0)
/* macro to perform a call, saving our pointer stack (see architecture notes at the beginning of this file */
#define JCALL(what) do { \
C2(MOV, MEM(8, RBP, 0, RNONE, -8), RDI); \
C1(CALL, what); \
C2(MOV, RDI, MEM(8, RBP, 0, RNONE, -8)); \
} while(0)
/* macro to box a value of any type */
#define BOX(type, targ, reg) do { \
switch (type) { \
case SDYN_TYPE_UNDEFINED: \
IMM64P(RAX, &sdyn_undefined); \
C2(MOV, targ, MEM(8, RAX, 0, RNONE, 0)); \
break; \
\
case SDYN_TYPE_BOOL: \
C2(MOV, RSI, reg); \
IMM64P(RAX, sdyn_boxBool); \
JCALL(RAX); \
C2(MOV, targ, RAX); \
break; \
\
case SDYN_TYPE_INT: \
C2(MOV, RSI, reg); \
IMM64P(RAX, sdyn_boxInt); \
JCALL(RAX); \
C2(MOV, targ, RAX); \
break; \
\
default: \
C2(MOV, targ, reg); \
} \
} while(0)
/* choose our target based on the storage type */
switch (GGC_RD(node, stype)) {
case SDYN_STORAGE_STK:
target = MEM(8, RSP, 0, RNONE, GGC_RD(node, addr)*8);
break;
case SDYN_STORAGE_ASTK:
case SDYN_STORAGE_PSTK:
target = MEM(8, RDI, 0, RNONE, GGC_RD(node, addr)*8 + 16);
break;
default:
target = RAX;
}
switch (GGC_RD(node, op)) {
case SDYN_NODE_ALLOCA:
imm = GGC_RD(node, imm) + 2; /* 2 extra slots for temporaries */
/* must align stack to 16 by Unix calling conventions */
if ((imm % 2) != 0) imm++;
/* 8 bytes per word */
imm *= 8;
/* standard entry code */
C1(PUSH, RBP);
C2(MOV, RBP, RSP);
C2(SUB, RSP, IMM(imm));
break;
case SDYN_NODE_PALLOCA:
{
size_t j;
imm = GGC_RD(node, imm) * 8 + 16; /* two extra words for temporaries */
/* explicitly assign sdyn_undefined to all new slots, so all
* pointers are valid */
C2(SUB, RDI, IMM(imm));
IMM64P(RAX, &sdyn_undefined);
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 0));
for (j = 0; j < imm; j += 8)
C2(MOV, MEM(8, RDI, 0, RNONE, j), RAX);
break;
}
case SDYN_NODE_POPA:
imm = GGC_RD(node, imm) + 2;
/* must align stack to 16 */
if ((imm % 2) != 0) imm++;
imm *= 8;
C2(ADD, RSP, IMM(imm));
C1(POP, RBP);
C0(RET);
break;
case SDYN_NODE_PPOPA:
{
size_t j;
/* since PPOPA ends the function body, we use this time to fix
* up all the forward references */
for (j = 0; j < returns.bufused; j++)
sja_patchFrel(&buf, returns.buf[j]);
imm = GGC_RD(node, imm) * 8 + 16;
C2(ADD, RDI, IMM(imm));
break;
}
case SDYN_NODE_IF:
{
/* if the condition is false, we will jump to the else clause */
size_t ifelse;
LOADOP(left, RAX);
/* we may need to coerce it */
if (leftType == SDYN_TYPE_BOXED_BOOL) {
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 8));
leftType = SDYN_TYPE_BOOL;
}
if (leftType != SDYN_TYPE_BOOL) {
BOX(leftType, RSI, RAX);
IMM64P(RAX, sdyn_toBoolean);
JCALL(RAX);
}
C2(CMP, RAX, IMM(0));
CF(JEF, ifelse);
GGC_WD(node, imm, ifelse);
break;
}
case SDYN_NODE_IFELSE:
{
/* first off, jump out of the if */
size_t ifelse, ifend;
CF(JMPF, ifend);
GGC_WD(node, imm, ifend);
/* now make the jump in for the else */
ifelse = GGC_RD(node, left);
onode = GGC_RAP(ir, ifelse);
ifelse = GGC_RD(onode, imm);
L(ifelse);
break;
}
case SDYN_NODE_IFEND:
{
/* make the jump out of the if */
size_t ifend;
ifend = GGC_RD(node, left);
onode = GGC_RAP(ir, ifend);
ifend = GGC_RD(onode, imm);
L(ifend);
break;
}
case SDYN_NODE_WHILE:
{
/* we need to keep track of the program counter at the start of
* the while loop, but there is no generated machine code for
* it. We just save the PC into the IR node's imm field, which
* is otherwise unused */
size_t wstart;
wstart = buf.bufused;
GGC_WD(node, imm, wstart);
break;
}
case SDYN_NODE_WCOND:
{
size_t wcond;
/* first get it to a bool */
LOADOP(left, RAX);
if (leftType < SDYN_TYPE_FIRST_BOXED &&
leftType != SDYN_TYPE_BOOL) {
/* wrong unboxed type! */
BOX(leftType, RAX, left);
leftType = SDYN_TYPE_BOXED;
}
if (leftType >= SDYN_TYPE_FIRST_BOXED) {
/* boolify it */
C2(MOV, RSI, RAX);
IMM64P(RAX, sdyn_toBoolean);
JCALL(RAX);
}
/* now it's ready to check */
C2(TEST, RAX, RAX);
CF(JEF, wcond);
/* we don't know where to jump to yet, so we save the label in the imm field */
GGC_WD(node, imm, wcond);
break;
}
case SDYN_NODE_WEND:
{
size_t wstart, wcond;
/* get our wstart and wcond program counters */
wstart = GGC_RD(node, left);
onode = GGC_RAP(ir, wstart);
wstart = GGC_RD(onode, imm);
wcond = GGC_RD(node, right);
onode = GGC_RAP(ir, wcond);
wcond = GGC_RD(onode, imm);
/* just jump back to the beginning */
C1(JMPR, RREL(wstart));
/* then provide the jumping-forward point from the condition */
L(wcond);
break;
}
case SDYN_NODE_PARAM:
{
size_t nonExist;
/* it is not necessary to provide exactly the right number of
* arguments. The number of arguments provided is in RSI. So,
* we check whether enough arguments were provided, and if so,
* load in an argument value. Because PALLOCA defaults
* everything to undefined, we don't have to do anything if
* insufficient arguments were provided. */
C2(CMP, RSI, IMM(GGC_RD(node, imm)));
CF(JLEF, nonExist); /* argument not provided */
C2(MOV, RAX, MEM(8, RDX, 0, RNONE, GGC_RD(node, imm)*8)); /* get it from RDX */
C2(MOV, target, RAX);
L(nonExist);
break;
}
case SDYN_NODE_INTRINSICCALL:
/* just get the address of the intrinsic and call it */
C2(MOV, RSI, IMM(lastArg + 1));
C2(LEA, RDX, MEM(8, RDI, 0, RNONE, 16));
IMM64P(RAX, sdyn_getIntrinsic((SDyn_String) GGC_RP(node, immp)));
JCALL(RAX);
C2(MOV, target, RAX);
break;
case SDYN_NODE_CALL:
/* left is the function to call, args are handled in ARG nodes */
LOADOP(left, RAX);
BOX(leftType, RSI, left);
/* save the hopefully-function in GC'd space */
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RSI);
/* assert that it's a function */
IMM64P(RAX, sdyn_assertFunction);
JCALL(RAX);
/* reload it from GC'd space (in case it's moved) */
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
/* pass in the number of arguments */
C2(MOV, RDX, IMM(lastArg + 1));
/* ARG loads to RDI+16, so just provide that address as the base for arguments */
C2(LEA, RCX, MEM(8, RDI, 0, RNONE, 16));
/* then call sdyn_call */
IMM64P(RAX, sdyn_call);
JCALL(RAX);
C2(MOV, target, RAX);
break;
case SDYN_NODE_ASSIGN:
/* assignments don't really exist in IR, so this is just a move, possibly boxing */
LOADOP(left, RAX);
if (targetType >= SDYN_TYPE_FIRST_BOXED)
BOX(leftType, RAX, left);
C2(MOV, target, RAX);
break;
case SDYN_NODE_MEMBER:
{
SDyn_String *gstring;
LOADOP(left, RAX);
BOX(leftType, RSI, left);
if (leftType != SDYN_TYPE_OBJECT) {
/* coerce it */
IMM64P(RAX, sdyn_toObject);
JCALL(RAX);
C2(MOV, RSI, RAX);
}
/* put the string member name somewhere to load at runtime */
gstring = (SDyn_String *) createPointer();
*gstring = GGC_RP(node, immp);
IMM64P(RDX, gstring);
C2(MOV, RDX, MEM(8, RDX, 0, RNONE, 0));
/* get everything into place and call */
IMM64P(RAX, sdyn_getObjectMember);
JCALL(RAX);
C2(MOV, target, RAX);
break;
}
case SDYN_NODE_ASSIGNMEMBER:
{
SDyn_String *gstring;
LOADOP(left, RAX);
BOX(leftType, RSI, left);
if (leftType != SDYN_TYPE_OBJECT) {
/* coerce it */
IMM64P(RAX, sdyn_toObject);
JCALL(RAX);
C2(MOV, RSI, RAX);
}
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RSI);
LOADOP(right, RAX);
BOX(rightType, RCX, right);
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
/* make the string globally accessible */
gstring = (SDyn_String *) createPointer();
*gstring = GGC_RP(node, immp);
IMM64P(RDX, gstring);
C2(MOV, RDX, MEM(8, RDX, 0, RNONE, 0));
/* get everything into place and call */
IMM64P(RAX, sdyn_setObjectMember);
JCALL(RAX);
LOADOP(right, RAX);
C2(MOV, target, RAX);
break;
}
case SDYN_NODE_INDEX:
/* left is the object to access */
LOADOP(left, RAX);
BOX(leftType, RSI, left);
/* save it in GC'd space */
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RSI);
/* right is the "index", which will be coerced to a string */
LOADOP(right, RAX);
BOX(rightType, RSI, right);
IMM64P(RAX, sdyn_toString);
JCALL(RAX);
C2(MOV, RDX, RAX);
/* reload the object */
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
/* then simply sdyn_getObjectMember to access */
IMM64P(RAX, sdyn_getObjectMember);
JCALL(RAX);
C2(MOV, target, RAX);
break;
case SDYN_NODE_ASSIGNINDEX:
/* (similar to above, but with a value) */
LOADOP(left, RAX);
BOX(leftType, RSI, left);
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RSI);
LOADOP(right, RAX);
BOX(rightType, RSI, right);
IMM64P(RAX, sdyn_toString);
JCALL(RAX);
C2(MOV, MEM(8, RDI, 0, RNONE, 8), RAX);
LOADOP(third, RCX);
BOX(thirdType, RCX, third);
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
C2(MOV, RDX, MEM(8, RDI, 0, RNONE, 8));
IMM64P(RAX, sdyn_setObjectMember);
JCALL(RAX);
LOADOP(third, RAX);
C2(MOV, target, RAX);
break;
case SDYN_NODE_SPECULATE:
LOADOP(left, RSI);
/* we'll store our label address in imm. Set it to 0 for non-label cases */
GGC_WD(node, imm, 0);
/* first off, this is very silly if our input type is already right */
if (targetType == leftType) {
C2(MOV, target, RSI);
break;
}
/* or if they can't possibly match */
if (leftType != SDYN_TYPE_BOXED) {
if ((leftType == SDYN_TYPE_BOXED_UNDEFINED) && (targetType == SDYN_TYPE_UNDEFINED)) {
/* no unboxing required for undefined */
} else if (((leftType == SDYN_TYPE_BOXED_BOOL) && (targetType == SDYN_TYPE_BOOL)) ||
((leftType == SDYN_TYPE_BOXED_INT) && (targetType == SDYN_TYPE_INT))) {
/* unbox the value */
C2(MOV, target, MEM(8, RSI, 0, RNONE, 8));
} else if ((leftType == SDYN_TYPE_UNDEFINED) && (targetType == SDYN_TYPE_BOXED_UNDEFINED)) {
/* box the undefined value */
IMM64P(RAX, &sdyn_undefined);
C2(MOV, target, MEM(8, RAX, 0, RNONE, 0));
} else if ((leftType == SDYN_TYPE_BOOL) && (targetType == SDYN_TYPE_BOXED_BOOL)) {
/* box the bool */
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
C2(MOV, target, RAX);
} else if ((leftType == SDYN_TYPE_INT) && (targetType == SDYN_TYPE_BOXED_INT)) {
/* box the int */
IMM64P(RAX, sdyn_boxInt);
JCALL(RAX);
C2(MOV, target, RAX);
} else {
/* speculation can never succeed */
size_t fail;
CF(JMPF, fail);
GGC_WD(node, imm, fail);
}
break;
}
/* our input is something boxed; check its type. The type tag is buried like so:
* struct Value {
* struct Descriptor *d;
* ...
* };
* struct Descriptor {
* struct Descriptor *descriptorDescriptor;
* struct Tag *tag;
* ...
* };
* struct Tag {
* struct Descriptor *tagDescriptor;
* long tag;
* };
*/
C2(MOV, RAX, MEM(8, RSI, 0, RNONE, 0)); /* get the descriptor */
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 8)); /* get the tag box */
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 8)); /* get the tag */
/* now we check if the tag is what we expect */
{
size_t expected = 0;
switch (targetType) {
case SDYN_TYPE_UNDEFINED:
expected = SDYN_TYPE_BOXED_UNDEFINED;
break;
case SDYN_TYPE_BOOL:
expected = SDYN_TYPE_BOXED_BOOL;
break;
case SDYN_TYPE_INT:
expected = SDYN_TYPE_BOXED_INT;
break;
default:
expected = targetType;
}
C2(CMP, RAX, IMM(expected));
}
/* if it's not, jump to the failed speculation */
{
size_t fail;
CF(JNEF, fail);
GGC_WD(node, imm, fail);
}
break;
case SDYN_NODE_SPECULATE_FAIL:
{
/* our speculation failed. This is the label target for the associated SPECULATE */
size_t fail;
fail = GGC_RD(node, left);
onode = GGC_RAP(ir, fail);
fail = GGC_RD(onode, imm);
L(fail);
break;
}
/* 0-ary: */
case SDYN_NODE_TOP:
IMM64P(RAX, &sdyn_globalObject);
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 0));
C2(MOV, target, RAX);
break;
case SDYN_NODE_NIL:
IMM64P(target, &sdyn_undefined);
C2(MOV, RAX, MEM(8, target, 0, RNONE, 0));
C2(MOV, target, RAX);
break;
case SDYN_NODE_NUM:
C2(MOV, target, IMM(GGC_RD(node, imm)));
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
C2(MOV, RSI, target);
IMM64P(RAX, &sdyn_boxInt);
JCALL(RAX);
C2(MOV, target, RAX);
}
break;
case SDYN_NODE_STR:
{
SDyn_String *gstring;
/* make the string globally accessible */
gstring = (SDyn_String *) createPointer();
*gstring = GGC_RP(node, immp);
*gstring = sdyn_unquote(*gstring);
/* then simply load it */
IMM64P(RAX, gstring);
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 0));
C2(MOV, target, RAX);
break;
}
case SDYN_NODE_FALSE:
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
IMM64P(RAX, &sdyn_false);
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 0));
C2(MOV, target, RAX);
} else {
IMM64(target, 0);
}
break;
case SDYN_NODE_TRUE:
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
IMM64P(RAX, &sdyn_true);
C2(MOV, RAX, MEM(8, RAX, 0, RNONE, 0));
C2(MOV, target, RAX);
} else {
IMM64(target, 1);
}
break;
case SDYN_NODE_OBJ:
IMM64P(RAX, sdyn_newObject);
JCALL(RAX);
C2(MOV, target, RAX);
break;
/* Unary: */
case SDYN_NODE_ARG:
lastArg = GGC_RD(node, imm);
/* arguments must be boxed */
LOADOP(left, RAX);
BOX(leftType, RAX, left);
C2(MOV, target, RAX);
break;
case SDYN_NODE_RETURN:
/* returns must be boxed */
LOADOP(left, RAX);
BOX(leftType, RAX, left);
/* jump to the return address */
while (BUFFER_SPACE(returns) < 1) EXPAND_BUFFER(returns);
CF(JMPF, *BUFFER_END(returns));
returns.bufused++;
break;
/* (boolean) -> boolean */
case SDYN_NODE_NOT:
LOADOP(left, RSI);
/* do we need to unbox? */
if (leftType == SDYN_TYPE_BOXED_BOOL) {
C2(MOV, RSI, MEM(8, RSI, 0, RNONE, 8));
leftType = SDYN_TYPE_BOOL;
}
/* do we need to coerce? */
if (leftType != SDYN_TYPE_BOOL) {
IMM64P(RAX, sdyn_toBoolean);
JCALL(RAX);
C2(MOV, RSI, RAX);
}
/* invert */
C2(XOR, RSI, IMM(1));
/* and possibly box */
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
C2(MOV, target, RAX);
} else {
C2(MOV, target, RSI);
}
break;
case SDYN_NODE_TYPEOF:
LOADOP(left, RAX);
BOX(leftType, RSI, left);
/* just count on sdyn_typeof */
IMM64P(RAX, sdyn_typeof);
JCALL(RAX);
C2(MOV, target, RAX);
break;
/* Binary: */
/* (*, *) -> boolean */
case SDYN_NODE_EQ:
case SDYN_NODE_NE:
{
LOADOP(left, RSI);
LOADOP(right, RDX);
if (leftType != rightType) {
/* we can unbox numbers to get compatible types */
if (leftType == SDYN_TYPE_INT && rightType == SDYN_TYPE_BOXED_INT) {
C2(MOV, right, MEM(8, right, 0, RNONE, 8));
rightType = SDYN_TYPE_INT;
} else if (leftType == SDYN_TYPE_BOXED_INT && rightType == SDYN_TYPE_INT) {
C2(MOV, left, MEM(8, left, 0, RNONE, 8));
leftType = SDYN_TYPE_INT;
} else if (leftType == SDYN_TYPE_BOXED_INT && rightType == SDYN_TYPE_BOXED_INT) {
C2(MOV, left, MEM(8, left, 0, RNONE, 8));
C2(MOV, right, MEM(8, right, 0, RNONE, 8));
leftType = rightType = SDYN_TYPE_INT;
}
}
/* we always put our result in RAX, for later moving */
if (leftType == rightType) {
/* if the types are the same, we only need to do a
* sophisticated equality comparison if they're both
* strings or if we only know they're both boxed */
if (leftType == SDYN_TYPE_STRING || leftType == SDYN_TYPE_BOXED) {
/* oh well, just use sdyn_equal */
IMM64P(RAX, sdyn_equal);
JCALL(RAX);
} else {
size_t eq;
/* comparison is direct */
C2(MOV, RAX, IMM(1));
C2(CMP, left, right);
CF(JEF, eq);
C2(MOV, RAX, IMM(0));
L(eq);
}
} else {
/* types aren't the same, so just box and go */
BOX(leftType, RSI, RSI);
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RSI);
LOADOP(right, RDX);
BOX(rightType, RDX, RDX);
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
IMM64P(RAX, sdyn_equal);
JCALL(RAX);
}
if (GGC_RD(node, op) == SDYN_NODE_NE) {
/* invert our result */
C2(XOR, RAX, IMM(1));
}
/* possibly box it */
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
C2(MOV, RSI, RAX);
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
}
C2(MOV, target, RAX);
break;
}
/* (number, number) -> boolean */
case SDYN_NODE_LT:
case SDYN_NODE_GT:
case SDYN_NODE_LE:
case SDYN_NODE_GE:
{
struct SJA_X8664_Operand intLeft;
size_t after;
/* get both operands as numbers */
intLeft = MEM(8, RBP, 0, RNONE, -16);
LOADOP(left, RAX);
switch (leftType) {
case SDYN_TYPE_BOXED_INT:
C2(MOV, RAX, MEM(8, left, 0, RNONE, 8));
C2(MOV, intLeft, RAX);
break;
case SDYN_TYPE_INT:
C2(MOV, intLeft, left);
break;
default:
if (leftType < SDYN_TYPE_FIRST_BOXED) {
BOX(leftType, RSI, left);
} else {
C2(MOV, RSI, left);
}
IMM64P(RAX, sdyn_toNumber);
JCALL(RAX);
C2(MOV, intLeft, RAX);
}
LOADOP(right, RDX);
switch (rightType) {
case SDYN_TYPE_BOXED_INT:
C2(MOV, RDX, MEM(8, right, 0, RNONE, 8));
break;
case SDYN_TYPE_INT:
C2(MOV, RDX, right);
break;
default:
if (rightType < SDYN_TYPE_FIRST_BOXED) {
BOX(rightType, RSI, right);
} else {
C2(MOV, RSI, right);
}
IMM64P(RAX, sdyn_toNumber);
JCALL(RAX);
C2(MOV, RDX, RAX);
}
C2(MOV, RSI, intLeft);
/* load true */
C2(MOV, RAX, IMM(1));
/* now compare them */
C2(CMP, RSI, RDX);
/* do the appropriate jump */
switch (GGC_RD(node, op)) {
case SDYN_NODE_LT: CF(JLF, after); break;
case SDYN_NODE_GT: CF(JGF, after); break;
case SDYN_NODE_LE: CF(JLEF, after); break;
case SDYN_NODE_GE: CF(JGEF, after); break;
}
/* load false */
C2(MOV, RAX, IMM(0));
/* and return */
L(after);
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
C2(MOV, RSI, RAX);
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
}
C2(MOV, target, RAX);
break;
}
/* the infamous add, the type of which is:
* (number, number) -> number
* (not number, number) -> string
* (number, not number) -> string
* (not number, not number) -> string */
case SDYN_NODE_ADD:
{
/* this is an infinitely complicated melange of type nonsense.
* We always store our result here in RAX, then just move it to
* target at the end. */
LOADOP(left, RAX);
LOADOP(right, RDX);
if (leftType == rightType) {
/* "easier" case: They're at least the same type */
switch (leftType) {
case SDYN_TYPE_UNDEFINED:
/* let the generic case handle it */
IMM64P(RSI, &sdyn_undefined);
C2(MOV, RSI, MEM(8, RSI, 0, RNONE, 0));
C2(MOV, RDX, RSI);
IMM64P(RAX, sdyn_add);
JCALL(RAX);
break;
case SDYN_TYPE_BOOL:
/* box them and then go to the generic case */
C2(MOV, RSI, left);
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
C2(MOV, MEM(8, RDI, 0, RNONE, 0), RAX); /* remember boxed left */
C2(MOV, RSI, right);
IMM64P(RAX, sdyn_boxBool);
JCALL(RAX);
/* put them in the argument slots */
C2(MOV, RDX, RAX);
C2(MOV, RSI, MEM(8, RDI, 0, RNONE, 0));
/* and add */
IMM64P(RAX, sdyn_add);
JCALL(RAX);
break;
case SDYN_TYPE_INT:
if (targetType >= SDYN_TYPE_FIRST_BOXED) {
/* may as well box now */
C2(MOV, RSI, left);
C2(ADD, RSI, right);
IMM64P(RAX, sdyn_boxInt);