1 /// Copyright: Copyright (c) 2017-2019 Andrey Penechko.
2 /// License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
3 /// Authors: Andrey Penechko.
4 module tests.reg_alloc;
5 
6 import std.stdio;
7 import tester;
8 
9 Test[] regAllocTests() { return collectTests!(tests.reg_alloc)(); }
10 
11 extern(C) void external_void_func(){}
12 
13 @TestInfo(&tester1, [HostSymbol("callee", cast(void*)&external_void_func)])
14 immutable reg_alloc1 = q{--- reg_alloc1
15 	// test register allocation
16 	@extern(module, "host")
17 	void callee(); // uses all registers causing spilling
18 	i32 run(i32 a)
19 	{
20 		callee;
21 		return a;
22 	}
23 };
24 void tester1(ref TestContext ctx) {
25 	assert(ctx.getFunctionPtr!(int, int)("run")(1) == 1);
26 }
27 
28 @TestInfo(&tester2, [HostSymbol("callee", cast(void*)&external_void_func)])
29 immutable reg_alloc2 = q{--- reg_alloc2
30 	// test register allocation
31 	@extern(module, "host")
32 	void callee();
33 	i32 run(i32 a, i32 b, i32 c, i32 d)
34 	{
35 		i32 e = b * c;
36 		callee;
37 		return a + b + c + d + e;
38 	}
39 };
40 void tester2(ref TestContext ctx) {
41 	assert(ctx.getFunctionPtr!(int, int, int, int, int)("run")(1, 1, 1, 1) == 5);
42 }
43 
44 @TestInfo(&tester3, [HostSymbol("callee", cast(void*)&external_void_func)])
45 immutable reg_alloc3 = q{--- reg_alloc3
46 	// test register allocation
47 	@extern(module, "host")
48 	void callee();
49 	i32 run(i32 a, i32 b, i32 c, i32 d)
50 	{
51 		i32 e = b * c;
52 		return a + b + c + d + e;
53 	}
54 };
55 void tester3(ref TestContext ctx) {
56 	assert(ctx.getFunctionPtr!(int, int, int, int, int)("run")(1, 1, 1, 1) == 5);
57 }
58 
59 @TestInfo()
60 immutable reg_alloc4 = q{--- reg_alloc4
61 	void setRangeu8(u8* slice, u64 from, u64 to, u8 value) {}
62 	void formatInt(i64 i, u8[21]* output, u32 minSize, u8[]* result)
63 	{
64 		u32 numDigits = 0;
65 		if (i == 0)
66 		{
67 			if (minSize == 0)
68 				minSize = 1;
69 			setRangeu8((*output).ptr, 21 - minSize, 21, '0');
70 			(*output)[20] = '0';
71 			numDigits = minSize;
72 		}
73 		else
74 		{
75 			bool neg = i < 0;
76 			if (neg) {
77 				i = -i;
78 			}
79 			bool overflow = i < 0;
80 			if (overflow)
81 				i = i64.max;
82 
83 			while (i)
84 			{
85 				u8 c = cast(u8)('0' + i % 10);
86 				(*output)[21 - ++numDigits] = c;
87 				i /= 10;
88 			}
89 
90 			while (numDigits < minSize) {
91 				(*output)[21 - ++numDigits] = '0';
92 			}
93 
94 			if (neg) {
95 				(*output)[21 - ++numDigits] = '-';
96 			}
97 			if (overflow) {
98 				++(*output)[20];
99 			}
100 		}
101 		(*result).ptr = (*output).ptr + (21 - numDigits);
102 		(*result).length = numDigits;
103 	}
104 };