Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / bug1839277.c
1 /*
2     bug 1839277 & 1839299
3 */
4
5 #include <testfwk.h>
6
7 code struct Value {
8   code char* Name[2];
9 } Values[2]= {{{"abc", "def"}}, {{"ghi", "jkl"}}};
10
11 char i=1;
12
13 void
14 testBug1839277(void)
15 {
16         volatile char code* * p;
17         unsigned long v = 0;
18 //first subexpression 'Values[0].Name' is evaluted as follows:
19 //mov     r2,#_Values
20 //mov     r3,#(_Values >> 8)
21 //mov     r4,#(_Values >> 16) ;this is wrong - should be 'mov r4,#128' shouldn't it?
22 //second subexpression 'Values[1].Name' is evaluted as follows:
23 //mov     a,#0x04
24 //add     a,#_Values
25 //mov     r2,a
26 //clr     a
27 //addc    a,#(_Values >> 8)
28 //mov     r3,a
29 //mov     r4,#128 ;this is all right
30         p = i ? Values[0].Name : Values[1].Name;
31 #if defined(SDCC_mcs51)
32         v = (unsigned long)p;
33         ASSERT((unsigned char)(v>>16)==0x80);
34 #endif
35
36 //everything is all right with explicit typecast - but why do I need it?
37         p = i ? (char code**)Values[0].Name : (char code**)Values[1].Name;
38 #if defined(SDCC_mcs51)
39         v = (unsigned long)p;
40         ASSERT((unsigned char)(v>>16)==0x80);
41 #endif
42
43 //this is the best/optimal version - again with explicit typecast
44 //Question: Why is it necessary to have explicit typecast to make things right?
45         p = i ? (char code* code*)Values[0].Name : (char code* code*)Values[1].Name;
46 #if defined(SDCC_mcs51)
47         v = (unsigned long)p;
48         ASSERT((unsigned char)(v>>16)==0x80);
49 #endif
50 }
51
52 void
53 testBug1839299(void)
54 {
55         volatile char code* * p;
56         unsigned long v = 0;
57 //'Values[0].Name' subexpression is evaluated as follows first:
58 //mov     r2,#_Values
59 //mov     r3,#(_Values >> 8)
60 //mov     r4,#(_Values >> 16) ;this is wrong - see bug 1839277
61   p= i ? Values[0].Name : Values[1].Name;
62 //this assignment has some sideeffect on the following one
63 //in fact it is the evaluation of 'Values[0].Name' itself has the effect, not the assignment
64   p= Values[0].Name;
65 //'Values[0].Name' subexpression is evaluated as follows second:
66 //mov     r2,#_Values
67 //mov     r3,#(_Values >> 8)
68 //mov     r4,#0x00 ;this is different from first occurrence but also wrong
69   p= i ? Values[0].Name : Values[1].Name;
70 #if defined(SDCC_mcs51)
71         v = (unsigned long)p;
72         ASSERT((unsigned char)(v>>16)==0x80);
73 #endif
74 }