summaryrefslogtreecommitdiff
path: root/support/regression/tests/bug-2964.c
blob: d56f4ac17571475c6d5ec455810418f2e363dbb5 (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
/*
   bug-2964.c

   A bug in stm8 code generation for left shift triggered by stack allocation allocating
   result and right operands of a left shift to the stack, to overlapping locations.
*/

#include <testfwk.h>

unsigned long long j;

unsigned char c;
unsigned long long i;

// Just some function that won't be inlined, so calls to it will result in spilling of local variables.
void g(void)
{
	j += 42 + i;
}

unsigned long long f1(void)
{
	unsigned char d = c + 1;
	j |= 7;
	unsigned long long l1 = i + 1;
	g();
	g();
	unsigned long long l2 = l1 << d;

	return l2;
}

#ifndef __SDCC_pdk14 // Lack of RAM
unsigned long long f2(void)
{
	unsigned char d = c + 1;
	j |= 7;
	unsigned long long l1 = i + 1;
	g();
	g();
	g();
	g();
	unsigned long long l2 = l1 << d;

	return l2;
}

unsigned long long f3(void)
{
	unsigned char d = c + 1;
	g();
	g();
	unsigned long long l2 = i << d;

	return l2;
}
#endif

void testBug(void)
{
	j = 1;

	c = 0;
	i = 1;
	ASSERT(f1() == 4);

#ifndef __SDCC_pdk14 // Lack of RAM
	c = 1;
	i = 3;
	ASSERT(f2() == 16);

	c = 1;
	i = 8;
	ASSERT(f3() == 32);
#endif
}