aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/jmp.cpp
blob: 6a3b46166db3bd090ca04074641f3ec210fce572 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
#include <stdio.h>
#include <string.h>
#include <string>
#define XBYAK_NO_OP_NAMES
#include <xbyak/xbyak.h>
#include <cybozu/inttype.hpp>
#include <cybozu/test.hpp>

using namespace Xbyak;

void putNop(Xbyak::CodeGenerator *gen, int n)
{
	for (int i = 0; i < n; i++) {
		gen->nop();
	}
}

void diff(const std::string& a, const std::string& b)
{
	if (a == b) return;
	if (a.size() != b.size()) printf("size diff %d %d\n", (int)a.size(), (int)b.size());
	for (size_t i = 0; i < (std::min)(a.size(), b.size()); i++) {
		if (a[i] != b[i]) {
			printf("diff %d(%04x) %02x %02x\n", (int)i, (int)i, (unsigned char)a[i], (unsigned char)b[i]);
		}
	}
}

void dump(const std::string& m)
{
	printf("size=%d\n     ", (int)m.size());
	for (int i = 0; i < 16; i++) {
		printf("%02x ", i);
	}
	printf("\n     ");
	for (int i = 0; i < 16; i++) {
		printf("---");
	}
	printf("\n");
	for (size_t i = 0; i < m.size(); i++) {
		if ((i % 16) == 0) printf("%04x ", (int)(i / 16));
		printf("%02x ", (unsigned char)m[i]);
		if ((i % 16) == 15) putchar('\n');
	}
	putchar('\n');
}

CYBOZU_TEST_AUTO(test1)
{
	struct TestJmp : public Xbyak::CodeGenerator {
	/*
	     4                                  X0:
	     5 00000004 EBFE                    jmp short X0
	     6
	     7                                  X1:
	     8 00000006 <res 00000001>          dummyX1 resb 1
	     9 00000007 EBFD                    jmp short X1
	    10
	    11                                  X126:
	    12 00000009 <res 0000007E>          dummyX126 resb 126
	    13 00000087 EB80                    jmp short X126
	    14
	    15                                  X127:
	    16 00000089 <res 0000007F>          dummyX127 resb 127
	    17 00000108 E97CFFFFFF              jmp near X127
	    18
	    19 0000010D EB00                    jmp short Y0
	    20                                  Y0:
	    21
	    22 0000010F EB01                    jmp short Y1
	    23 00000111 <res 00000001>          dummyY1 resb 1
	    24                                  Y1:
	    25
	    26 00000112 EB7F                    jmp short Y127
	    27 00000114 <res 0000007F>          dummyY127 resb 127
	    28                                  Y127:
	    29
	    30 00000193 E980000000              jmp near Y128
	    31 00000198 <res 00000080>          dummyY128 resb 128
	    32                                  Y128:
	*/
		TestJmp(int offset, bool isBack, bool isShort, bool useNewLabel)
		{
			if (useNewLabel) {
				Label label;
				if (isBack) {
					L(label);
					putNop(this, offset);
					jmp(label);
				} else {
					if (isShort) {
						jmp(label);
					} else {
						jmp(label, T_NEAR);
					}
					putNop(this, offset);
					L(label);
				}
			} else {
				if (isBack) {
					L("@@");
					putNop(this, offset);
					jmp("@b");
				} else {
					if (isShort) {
						jmp("@f");
					} else {
						jmp("@f", T_NEAR);
					}
					putNop(this, offset);
					L("@@");
				}
			}
		}
	};
	static const struct Tbl {
		int offset;
		bool isBack;
		bool isShort;
		uint8 result[6];
		int size;
	} tbl[] = {
		{ 0, true, true, { 0xeb, 0xfe }, 2 },
		{ 1, true, true, { 0xeb, 0xfd }, 2 },
		{ 126, true, true, { 0xeb, 0x80 }, 2 },
		{ 127, true, false, {0xe9, 0x7c, 0xff, 0xff, 0xff }, 5 },
		{ 0, false, true, { 0xeb, 0x00 }, 2 },
		{ 1, false, true, { 0xeb, 0x01 }, 2 },
		{ 127, false, true, { 0xeb, 0x7f }, 2 },
		{ 128, false, false, { 0xe9, 0x80, 0x00, 0x00, 0x00 }, 5 },
	};
	for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
		const Tbl *p = &tbl[i];
		for (int k = 0; k < 2; k++) {
			TestJmp jmp(p->offset, p->isBack, p->isShort, k == 0);
			const uint8 *q = (const uint8*)jmp.getCode();
			if (p->isBack) q += p->offset; /* skip nop */
			for (int j = 0; j < p->size; j++) {
				CYBOZU_TEST_EQUAL(q[j], p->result[j]);
			}
		}
	}
}

CYBOZU_TEST_AUTO(testJmpCx)
{
	struct TestJmpCx : public CodeGenerator {
		explicit TestJmpCx(void *p, bool useNewLabel)
			: Xbyak::CodeGenerator(16, p)
		{
			if (useNewLabel) {
				Label lp;
			L(lp);
#ifdef XBYAK64
				/*
					67 E3 FD ; jecxz lp
					E3 FB    ; jrcxz lp
				*/
				jecxz(lp);
				jrcxz(lp);
#else
				/*
					E3FE   ; jecxz lp
					67E3FB ; jcxz lp
				*/
				jecxz(lp);
				jcxz(lp);
#endif
			} else {
				inLocalLabel();
			L(".lp");
#ifdef XBYAK64
				/*
					67 E3 FD ; jecxz lp
					E3 FB    ; jrcxz lp
				*/
				jecxz(".lp");
				jrcxz(".lp");
#else
				/*
					E3FE   ; jecxz lp
					67E3FB ; jcxz lp
				*/
				jecxz(".lp");
				jcxz(".lp");
#endif
				outLocalLabel();
			}
		}
	};
	const struct {
		const char *p;
		size_t len;
	} tbl = {
#ifdef XBYAK64
		"\x67\xe3\xfd\xe3\xfb", 5
#else
		"\xe3\xfe\x67\xe3\xfb", 5
#endif
	};
	for (int j = 0; j < 2; j++) {
		char buf[16] = {};
		TestJmpCx code(buf, j == 0);
		CYBOZU_TEST_EQUAL(memcmp(buf, tbl.p, tbl.len), 0);
	}
}

#ifdef _MSC_VER
	#pragma warning(disable : 4310)
#endif
CYBOZU_TEST_AUTO(test2)
{
	struct TestJmp2 : public CodeGenerator {
	/*
	  1 00000000 90                      nop
	  2 00000001 90                      nop
	  3                                  f1:
	  4 00000002 <res 0000007E>          dummyX1 resb 126
	  6 00000080 EB80                     jmp f1
	  7
	  8                                  f2:
	  9 00000082 <res 0000007F>          dummyX2 resb 127
	 11 00000101 E97CFFFFFF               jmp f2
	 12
	 13
	 14 00000106 EB7F                    jmp f3
	 15 00000108 <res 0000007F>          dummyX3 resb 127
	 17                                  f3:
	 18
	 19 00000187 E980000000              jmp f4
	 20 0000018C <res 00000080>          dummyX4 resb 128
	 22                                  f4:
	*/
		TestJmp2(void *p, bool useNewLabel)
			: Xbyak::CodeGenerator(8192, p)
		{
			if (useNewLabel) {
				inLocalLabel();
				nop();
				nop();
			L(".f1");
				putNop(this, 126);
				jmp(".f1");
			L(".f2");
				putNop(this, 127);
				jmp(".f2", T_NEAR);

				jmp(".f3");
				putNop(this, 127);
			L(".f3");
				jmp(".f4", T_NEAR);
				putNop(this, 128);
			L(".f4");
				outLocalLabel();
			} else {
				nop();
				nop();
				Label f1, f2, f3, f4;
			L(f1);
				putNop(this, 126);
				jmp(f1);
			L(f2);
				putNop(this, 127);
				jmp(f2, T_NEAR);

				jmp(f3);
				putNop(this, 127);
			L(f3);
				jmp(f4, T_NEAR);
				putNop(this, 128);
			L(f4);
			}
		}
	};

	std::string ok;
	ok.resize(0x18C + 128, (char)0x90);
	ok[0x080] = (char)0xeb;
	ok[0x081] = (char)0x80;

	ok[0x101] = (char)0xe9;
	ok[0x102] = (char)0x7c;
	ok[0x103] = (char)0xff;
	ok[0x104] = (char)0xff;
	ok[0x105] = (char)0xff;

	ok[0x106] = (char)0xeb;
	ok[0x107] = (char)0x7f;

	ok[0x187] = (char)0xe9;
	ok[0x188] = (char)0x80;
	ok[0x189] = (char)0x00;
	ok[0x18a] = (char)0x00;
	ok[0x18b] = (char)0x00;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			TestJmp2 c(i == 0 ? 0 : Xbyak::AutoGrow, j == 0);
			c.ready();
			std::string m((const char*)c.getCode(), c.getSize());
			CYBOZU_TEST_EQUAL(m, ok);
		}
	}
}

#ifdef XBYAK32
int add5(int x) { return x + 5; }
int add2(int x) { return x + 2; }

CYBOZU_TEST_AUTO(test3)
{
	struct Grow : Xbyak::CodeGenerator {
		Grow(int dummySize)
			: Xbyak::CodeGenerator(128, Xbyak::AutoGrow)
		{
			mov(eax, 100);
			push(eax);
			call((void*)add5);
			add(esp, 4);
			push(eax);
			call((void*)add2);
			add(esp, 4);
			ret();
			for (int i = 0; i < dummySize; i++) {
				db(0);
			}
		}
	};
	for (int dummySize = 0; dummySize < 40000; dummySize += 10000) {
		printf("dummySize=%d\n", dummySize);
		Grow g(dummySize);
		g.ready();
		int (*f)() = (int (*)())g.getCode();
		int x = f();
		const int ok = 107;
		CYBOZU_TEST_EQUAL(x, ok);
	}
}
#endif

Xbyak::uint8 bufL[4096 * 32];
Xbyak::uint8 bufS[4096 * 2];

struct MyAllocator : Xbyak::Allocator {
	Xbyak::uint8 *alloc(size_t size)
	{
		if (size < sizeof(bufS)) {
			printf("test use bufS(%d)\n", (int)size);
			return bufS;
		}
		if (size < sizeof(bufL)) {
			printf("test use bufL(%d)\n", (int)size);
			return bufL;
		}
		fprintf(stderr, "no memory %d\n", (int)size);
		exit(1);
	}
	void free(Xbyak::uint8 *)
	{
	}
} myAlloc;

CYBOZU_TEST_AUTO(test4)
{
	struct Test4 : Xbyak::CodeGenerator {
		Test4(int size, void *mode, bool useNewLabel)
			: CodeGenerator(size, mode)
		{
			if (useNewLabel) {
				Label x;
				jmp(x);
				putNop(this, 10);
			L(x);
				ret();
			} else {
				inLocalLabel();
				jmp(".x");
				putNop(this, 10);
			L(".x");
				ret();
				outLocalLabel();
			}
		}
	};
	for (int i = 0; i < 2; i++) {
		const bool useNewLabel = i == 0;
		std::string fm, gm;
		Test4 fc(1024, 0, useNewLabel);
		Test4 gc(5, Xbyak::AutoGrow, !useNewLabel);
		gc.ready();
		fm.assign((const char*)fc.getCode(), fc.getSize());
		gm.assign((const char*)gc.getCode(), gc.getSize());
		CYBOZU_TEST_EQUAL(fm, gm);
	}
}

CYBOZU_TEST_AUTO(test5)
{
	struct Test5 : Xbyak::CodeGenerator {
		explicit Test5(int size, int count, void *mode)
			: CodeGenerator(size, mode, &myAlloc)
		{
			using namespace Xbyak;
			inLocalLabel();
			mov(ecx, count);
			xor_(eax, eax);
		L(".lp");
			for (int i = 0; i < count; i++) {
				L(Label::toStr(i));
				add(eax, 1);
				int to = 0;
				if (i < count / 2) {
					to = count - 1 - i;
				} else {
					to = count  - i;
				}
				if (i == count / 2) {
					jmp(".exit", T_NEAR);
				} else {
					jmp(Label::toStr(to), T_NEAR);
				}
			}
		L(".exit");
			sub(ecx, 1);
			jnz(".lp", T_NEAR);
			ret();
			outLocalLabel();
		}
	};
	std::string fm, gm;
	const int count = 50;
	int ret;
	Test5 fc(1024 * 64, count, 0);
	ret = ((int (*)())fc.getCode())();
	CYBOZU_TEST_EQUAL(ret, count * count);
	fm.assign((const char*)fc.getCode(), fc.getSize());
	Test5 gc(10, count, Xbyak::AutoGrow);
	gc.ready();
	ret = ((int (*)())gc.getCode())();
	CYBOZU_TEST_EQUAL(ret, count * count);
	gm.assign((const char*)gc.getCode(), gc.getSize());
	CYBOZU_TEST_EQUAL(fm, gm);
}

size_t getValue(const uint8* p)
{
	size_t v = 0;
	for (size_t i = 0; i < sizeof(size_t); i++) {
		v |= size_t(p[i]) << (i * 8);
	}
	return v;
}

void checkAddr(const uint8 *p, size_t offset, size_t expect)
{
	size_t v = getValue(p + offset);
	CYBOZU_TEST_EQUAL(v, size_t(p) + expect);
}

CYBOZU_TEST_AUTO(MovLabel)
{
	struct MovLabelCode : Xbyak::CodeGenerator {
		MovLabelCode(bool grow, bool useNewLabel)
			: Xbyak::CodeGenerator(grow ? 128 : 4096, grow ? Xbyak::AutoGrow : 0)
		{
#ifdef XBYAK64
			const Reg64& a = rax;
#else
			const Reg32& a = eax;
#endif
			if (useNewLabel) {
				nop(); // 0x90
				Label lp1, lp2;
			L(lp1);
				nop();
				mov(a, lp1); // 0xb8 + <4byte> / 0x48bb + <8byte>
				nop();
				mov(a, lp2); // 0xb8
				// force realloc if AutoGrow
				putNop(this, 256);
				nop();
			L(lp2);
			} else {
				inLocalLabel();
				nop(); // 0x90
			L(".lp1");
				nop();
				mov(a, ".lp1"); // 0xb8 + <4byte> / 0x48bb + <8byte>
				nop();
				mov(a, ".lp2"); // 0xb8
				// force realloc if AutoGrow
				putNop(this, 256);
				nop();
			L(".lp2");
				outLocalLabel();
			}
		}
	};

	const struct {
		int pos;
		uint8 ok;
	} tbl[] = {
#ifdef XBYAK32
		{ 0x00, 0x90 },
		// lp1:0x001
		{ 0x001, 0x90 },
		{ 0x002, 0xb8 },
		// 0x003
		{ 0x007, 0x90 },
		{ 0x008, 0xb8 },
		// 0x009
		{ 0x10d, 0x90 },
		// lp2:0x10e
#else
		{ 0x000, 0x90 },
		// lp1:0x001
		{ 0x001, 0x90 },
		{ 0x002, 0x48 },
		{ 0x003, 0xb8 },
		// 0x004
		{ 0x00c, 0x90 },
		{ 0x00d, 0x48 },
		{ 0x00e, 0xb8 },
		// 0x00f
		{ 0x117, 0x90 },
		// lp2:0x118
#endif
	};
	for (int j = 0; j < 2; j++) {
		const bool grow = j == 0;
		for (int k = 0; k < 2; k++) {
			const bool useNewLabel = k == 0;
			MovLabelCode code(grow, useNewLabel);
			if (grow) code.ready();
			const uint8* const p = code.getCode();
			for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
				int pos = tbl[i].pos;
				uint8 x = p[pos];
				uint8 ok = tbl[i].ok;
				CYBOZU_TEST_EQUAL(x, ok);
			}
#ifdef XBYAK32
			checkAddr(p, 0x03, 0x001);
			checkAddr(p, 0x09, 0x10e);
#else
			checkAddr(p, 0x04, 0x001);
			checkAddr(p, 0x0f, 0x118);
#endif
		}
	}
}

CYBOZU_TEST_AUTO(testMovLabel2)
{
	struct MovLabel2Code : Xbyak::CodeGenerator {
		MovLabel2Code()
		{
#ifdef XBYAK64
			const Reg64& a = rax;
			const Reg64& c = rcx;
#else
			const Reg32& a = eax;
			const Reg32& c = ecx;
#endif
			xor_(a, a);
			xor_(c, c);
			jmp("in");
			ud2();
		L("@@"); // L1
			add(a, 2);
			mov(c, "@f");
			jmp(c); // goto L2
			ud2();
		L("in");
			mov(c, "@b");
			add(a, 1);
			jmp(c); // goto L1
			ud2();
		L("@@"); // L2
			add(a, 4);
			ret();
		}
	};
	MovLabel2Code code;
	int ret = code.getCode<int (*)()>()();
	CYBOZU_TEST_EQUAL(ret, 7);
}

CYBOZU_TEST_AUTO(testF_B)
{
	struct Code : Xbyak::CodeGenerator {
		Code(int type)
		{
			inLocalLabel();
			xor_(eax, eax);
			switch (type) {
			case 0:
			L("@@");
				inc(eax);
				cmp(eax, 1);
				je("@b");
				break;
			case 1:
				test(eax, eax);
				jz("@f");
				ud2();
			L("@@");
				break;
			case 2:
			L("@@");
				inc(eax);
				cmp(eax, 1); // 1, 2
				je("@b");
				cmp(eax, 2); // 2, 3
				je("@b");
				break;
			case 3:
			L("@@");
				inc(eax);
				cmp(eax, 1); // 1, 2
				je("@b");
				cmp(eax, 2); // 2, 3
				je("@b");
				jmp("@f");
				ud2();
			L("@@");
				break;
			case 4:
			L("@@");
				inc(eax);
				cmp(eax, 1); // 1, 2
				je("@b");
				cmp(eax, 2); // 2, 3
				je("@b");
				jmp("@f");
				ud2();
			L("@@");
				inc(eax); // 4, 5
				cmp(eax, 4);
				je("@b");
				break;
			case 5:
			L("@@");
			L("@@");
				inc(eax);
				cmp(eax, 1);
				je("@b");
				break;
			case 6:
			L("@@");
			L("@@");
			L("@@");
				inc(eax);
				cmp(eax, 1);
				je("@b");
				break;
			case 7:
				jmp("@f");
			L("@@");
				inc(eax); // 1, 2
				cmp(eax, 1);
				je("@b");
				cmp(eax, 2);
				jne("@f"); // not jmp
				inc(eax); // 3
			L("@@");
				inc(eax); // 4, 5, 6
				cmp(eax, 4);
				je("@b");
				cmp(eax, 5);
				je("@b");
				jmp("@f");
				jmp("@f");
				jmp("@b");
			L("@@");
				break;
			}
			ret();
			outLocalLabel();
		}
	};
	const int expectedTbl[] = {
		2, 0, 3, 3, 5, 2, 2, 6
	};
	for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(expectedTbl); i++) {
		Code code((int)i);
		int ret = code.getCode<int (*)()>()();
		CYBOZU_TEST_EQUAL(ret, expectedTbl[i]);
	}
}

CYBOZU_TEST_AUTO(test6)
{
	struct TestLocal : public Xbyak::CodeGenerator {
		TestLocal(bool grow)
			: Xbyak::CodeGenerator(grow ? 128 : 4096, grow ? Xbyak::AutoGrow : 0)
		{
			xor_(eax, eax);
			inLocalLabel();
			jmp("start0", T_NEAR);
			L(".back");
			inc(eax); // 8
			jmp(".next", T_NEAR);
			L("start2");
			inc(eax); // 7
			jmp(".back", T_NEAR);
				inLocalLabel();
				L(".back");
				inc(eax); // 5
				putNop(this, 128);
				jmp(".next", T_NEAR);
				L("start1");
				inc(eax); // 4
				jmp(".back", T_NEAR);
					inLocalLabel();
					L(".back");
					inc(eax); // 2
					jmp(".next", T_NEAR);
					L("start0");
					inc(eax); // 1
					jmp(".back", T_NEAR);
					L(".next");
					inc(eax); // 3
					jmp("start1", T_NEAR);
					outLocalLabel();
				L(".next");
				inc(eax); // 6
				jmp("start2", T_NEAR);
				outLocalLabel();
			L(".next");
			inc(eax); // 9
			jmp("start3", T_NEAR);
				inLocalLabel();
				L(".back");
				inc(eax); // 14
				jmp("exit", T_NEAR);
			L("start4");
				inc(eax); // 13
				jmp(".back", T_NEAR);
				outLocalLabel();
			L("start3");
				inc(eax); // 10
				inLocalLabel();
				jmp(".next", T_NEAR);
				L(".back");
				inc(eax); // 12
				jmp("start4", T_NEAR);
				L(".next");
				inc(eax); // 11
				jmp(".back", T_NEAR);
				outLocalLabel();
			outLocalLabel();
			L("exit");
			inc(eax); // 15
			ret();
		}
	};

	for (int i = 0; i < 2; i++) {
		const bool grow = i == 1;
		printf("test6 grow=%d\n", i);
		TestLocal code(grow);
		if (grow) code.ready();
		int (*f)() = code.getCode<int (*)()>();
		int a = f();
		CYBOZU_TEST_EQUAL(a, 15);
	}
}

CYBOZU_TEST_AUTO(test_jcc)
{
	struct A : Xbyak::CodeGenerator {
		A()
		{
			add(eax, 5);
			ret();
		}
	};
	struct B : Xbyak::CodeGenerator {
		B(bool grow, const void *p) : Xbyak::CodeGenerator(grow ? 0 : 4096, grow ? Xbyak::AutoGrow : 0)
		{
			mov(eax, 1);
			add(eax, 2);
			jnz(p);
		}
	};
	A a;
	const void *p = a.getCode<const void*>();
	for (int i = 0; i < 2; i++) {
		bool grow = i == 1;
		B b(grow, p);
		if (grow) {
			b.ready();
		}
		int (*f)() = b.getCode<int (*)()>();
		CYBOZU_TEST_EQUAL(f(), 8);
	}
}

CYBOZU_TEST_AUTO(testNewLabel)
{
	struct Code : Xbyak::CodeGenerator {
		Code(bool grow)
			: Xbyak::CodeGenerator(grow ? 128 : 4096, grow ? Xbyak::AutoGrow : 0)
		{
			xor_(eax, eax);
			{
				Label label1;
				Label label2;
				Label label3;
				Label label4;
				Label exit;
				jmp(label1, T_NEAR);
			L(label2);
				inc(eax); // 2
				jmp(label3, T_NEAR);
			L(label4);
				inc(eax); // 4
				jmp(exit, T_NEAR);
				putNop(this, 128);
			L(label3);
				inc(eax); // 3
				jmp(label4, T_NEAR);
			L(label1);
				inc(eax); // 1
				jmp(label2, T_NEAR);
			L(exit);
			}
			{
				Label label1;
				Label label2;
				Label label3;
				Label label4;
				Label exit;
				jmp(label1);
			L(label2);
				inc(eax); // 6
				jmp(label3);
			L(label4);
				inc(eax); // 8
				jmp(exit);
			L(label3);
				inc(eax); // 7
				jmp(label4);
			L(label1);
				inc(eax); // 5
				jmp(label2);
			L(exit);
			}
			Label callLabel;
			{	// eax == 8
				Label label1;
				Label label2;
			L(label1);
				inc(eax); // 9, 10, 11, 13
				cmp(eax, 9);
				je(label1);
				// 10, 11, 13
				inc(eax); // 11, 12, 13
				cmp(eax, 11);
				je(label1);
				// 12, 13
				cmp(eax, 12);
				je(label2);
				inc(eax); // 14
				cmp(eax, 14);
				je(label2);
				ud2();
			L(label2); // 14
				inc(eax); // 13, 15
				cmp(eax, 13);
				je(label1);
			}
			call(callLabel);
			ret();
		L(callLabel);
			inc(eax); // 16
			ret();
		}
	};
	for (int i = 0; i < 2; i++) {
		const bool grow = i == 1;
		printf("testNewLabel grow=%d\n", grow);
		Code code(grow);
		if (grow) code.ready();
		int (*f)() = code.getCode<int (*)()>();
		int r = f();
		CYBOZU_TEST_EQUAL(r, 16);
	}
}

CYBOZU_TEST_AUTO(returnLabel)
{
	struct Code : Xbyak::CodeGenerator {
		Code()
		{
			xor_(eax, eax);
		Label L1 = L();
			test(eax, eax);
			Label exit;
			jnz(exit);
			inc(eax); // 1
			Label L2;
			call(L2);
			jmp(L1);
		L(L2);
			inc(eax); // 2
			ret();
		L(exit);
			inc(eax); // 3
			ret();
		}
	};
	Code code;
	int (*f)() = code.getCode<int (*)()>();
	int r = f();
	CYBOZU_TEST_EQUAL(r, 3);
}

CYBOZU_TEST_AUTO(testAssign)
{
	struct Code : Xbyak::CodeGenerator {
		Code(bool grow)
			: Xbyak::CodeGenerator(grow ? 128 : 4096, grow ? Xbyak::AutoGrow : 0)
		{
			xor_(eax, eax);
			Label dst, src;
		L(src);
			inc(eax);
			cmp(eax, 1);
			je(dst);
			inc(eax); // 2, 3, 5
			cmp(eax, 5);
			putNop(this, 128);
			jne(dst, T_NEAR);
			ret();
		assignL(dst, src);
			// test of copy  label
			{
				Label sss(dst);
				{
					Label ttt;
					ttt = src;
				}
			}
		}
	};
	for (int i = 0; i < 2; i++) {
		const bool grow = i == 0;
		printf("testAssign grow=%d\n", grow);
		Code code(grow);
		if (grow) code.ready();
		int (*f)() = code.getCode<int (*)()>();
		int ret = f();
		CYBOZU_TEST_EQUAL(ret, 5);
    }
}

CYBOZU_TEST_AUTO(doubleDefine)
{
	struct Code : Xbyak::CodeGenerator {
		Code()
		{
			{
				Label label;
			L(label);
				// forbitten double L()
				CYBOZU_TEST_EXCEPTION(L(label), Xbyak::Error);
			}
			{
				Label label;
				jmp(label);
				CYBOZU_TEST_ASSERT(hasUndefinedLabel());
			}
			{
				Label label1, label2;
			L(label1);
				jmp(label2);
				assignL(label2, label1);
				// forbitten double assignL
				CYBOZU_TEST_EXCEPTION(assignL(label2, label1), Xbyak::Error);
			}
			{
				Label label1, label2;
			L(label1);
				jmp(label2);
				// forbitten assignment to label1 set by L()
				CYBOZU_TEST_EXCEPTION(assignL(label1, label2), Xbyak::Error);
			}
		}
	} code;
}

struct GetAddressCode1 : Xbyak::CodeGenerator {
	void test()
	{
		Xbyak::Label L1, L2, L3;
		nop();
	L(L1);
		const uint8_t *p1 = getCurr();
		CYBOZU_TEST_EQUAL_POINTER(L1.getAddress(), p1);

		nop();
		jmp(L2);
		nop();
		jmp(L3);
	L(L2);
		CYBOZU_TEST_EQUAL_POINTER(L2.getAddress(), getCurr());
		// L3 is not defined
		CYBOZU_TEST_EQUAL_POINTER(L3.getAddress(), 0);

		// L3 is set by L1
		assignL(L3, L1);
		CYBOZU_TEST_EQUAL_POINTER(L3.getAddress(), p1);
	}
};

struct CodeLabelTable : Xbyak::CodeGenerator {
	static const int ret0 = 3;
	static const int ret1 = 5;
	static const int ret2 = 8;
	CodeLabelTable()
	{
		using namespace Xbyak;
#ifdef XBYAK64_WIN
		const Reg64& p0 = rcx;
		const Reg64& a = rax;
#elif defined (XBYAK64_GCC)
		const Reg64& p0 = rdi;
		const Reg64& a = rax;
#else
		const Reg32& p0 = edx;
		const Reg32& a = eax;
		mov(edx, ptr [esp + 4]);
#endif
		Label labelTbl, L0, L1, L2;
		mov(a, labelTbl);
		jmp(ptr [a + p0 * sizeof(void*)]);
	L(labelTbl);
		putL(L0);
		putL(L1);
		putL(L2);
	L(L0);
		mov(a, ret0);
		ret();
	L(L1);
		mov(a, ret1);
		ret();
	L(L2);
		mov(a, ret2);
		ret();
	}
};

CYBOZU_TEST_AUTO(LabelTable)
{
	CodeLabelTable c;
	int (*f)(int) = c.getCode<int (*)(int)>();
	CYBOZU_TEST_EQUAL(f(0), c.ret0);
	CYBOZU_TEST_EQUAL(f(1), c.ret1);
	CYBOZU_TEST_EQUAL(f(2), c.ret2);
}

CYBOZU_TEST_AUTO(getAddress1)
{
	GetAddressCode1 c;
	c.test();
}

struct GetAddressCode2 : Xbyak::CodeGenerator {
	Xbyak::Label L1, L2, L3;
	size_t a1;
	size_t a3;
	explicit GetAddressCode2(int size)
		: Xbyak::CodeGenerator(size, size == 4096 ? 0 : Xbyak::AutoGrow)
		, a1(0)
		, a3(0)
	{
		bool autoGrow = size != 4096;
		nop();
	L(L1);
		if (autoGrow) {
			CYBOZU_TEST_EQUAL_POINTER(L1.getAddress(), 0);
		}
		a1 = getSize();
		nop();
		jmp(L2);
		if (autoGrow) {
			CYBOZU_TEST_EQUAL_POINTER(L2.getAddress(), 0);
		}
	L(L3);
		a3 = getSize();
		if (autoGrow) {
			CYBOZU_TEST_EQUAL_POINTER(L3.getAddress(), 0);
		}
		nop();
		assignL(L2, L1);
		if (autoGrow) {
			CYBOZU_TEST_EQUAL_POINTER(L2.getAddress(), 0);
		}
	}
};

CYBOZU_TEST_AUTO(getAddress2)
{
	const int sizeTbl[] = {
		2, 128, // grow
		4096 // not grow
	};
	for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(sizeTbl); i++) {
		int size = sizeTbl[i];
		GetAddressCode2 c(size);
		c.ready();
		const uint8_t *p = c.getCode();
		CYBOZU_TEST_EQUAL(c.L1.getAddress(), p + c.a1);
		CYBOZU_TEST_EQUAL(c.L3.getAddress(), p + c.a3);
		CYBOZU_TEST_EQUAL(c.L2.getAddress(), p + c.a1);
	}
}

#ifdef XBYAK64
CYBOZU_TEST_AUTO(rip)
{
	int a[] = { 1, 10 };
	int b[] = { 100, 1000 };
	struct Code : Xbyak::CodeGenerator {
		Code(const int *a, const int *b)
		{
			Label label1, label2;
			jmp("@f");
		L(label1);
			db(a[0], 4);
			db(a[1], 4);
		L("@@");
			mov(eax, ptr [rip + label1]);       // a[0]
			mov(ecx, ptr [rip + label1+4]);     // a[1]
			mov(edx, ptr [rip + label2-8+2+6]); // b[0]
			add(ecx, ptr [rip + 16+label2-12]); // b[1]
			add(eax, ecx);
			add(eax, edx);
			ret();
		L(label2);
			db(b[0], 4);
			db(b[1], 4);

			// error
			CYBOZU_TEST_EXCEPTION(rip + label1 + label2, Xbyak::Error);
		}
	} code(a, b);
	int ret = code.getCode<int (*)()>()();
	CYBOZU_TEST_EQUAL(ret, a[0] + a[1] + b[0] + b[1]);
}

int ret1234()
{
	return 1234;
}

int ret9999()
{
	return 9999;
}

CYBOZU_TEST_AUTO(rip_jmp)
{
	struct Code : Xbyak::CodeGenerator {
		Code()
		{
			Label label;
			xor_(eax, eax);
			call(ptr [rip + label]);
			mov(ecx, eax);
			call(ptr [rip + label + 8]);
			add(eax, ecx);
			ret();
		L(label);
			db((size_t)ret1234, 8);
			db((size_t)ret9999, 8);
		}
	} code;
	int ret = code.getCode<int (*)()>()();
	CYBOZU_TEST_EQUAL(ret, ret1234() + ret9999());
}

#ifdef XBYAK64_GCC
CYBOZU_TEST_AUTO(rip_addr)
{
	/*
		assume |&x - &code| < 2GiB
	*/
	static int x = 5;
	struct Code : Xbyak::CodeGenerator {
		Code()
		{
			mov(eax, 123);
			mov(ptr[rip + &x], eax);
			ret();
		}
	} code;
	code.getCode<void (*)()>()();
	CYBOZU_TEST_EQUAL(x, 123);
}
#endif
CYBOZU_TEST_AUTO(rip_addr_with_fixed_buf)
{
	MIE_ALIGN(4096) static char buf[8192];
	static char *p = buf + 4096;
	static int *x0 = (int*)buf;
	static int *x1 = x0 + 1;
	struct Code : Xbyak::CodeGenerator {
		Code() : Xbyak::CodeGenerator(4096, p)
		{
			mov(eax, 123);
			mov(ptr[rip + x0], eax);
			mov(dword[rip + x1], 456);
			mov(byte[rip + 1 + x1 + 3], 99);
			ret();
		}
	} code;
	code.setProtectModeRE();
	code.getCode<void (*)()>()();
	CYBOZU_TEST_EQUAL(*x0, 123);
	CYBOZU_TEST_EQUAL(*x1, 456);
	CYBOZU_TEST_EQUAL(buf[8], 99);
	code.setProtectModeRW();
}
#endif