struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bug1839277.c
blob68c81b7d124f569700c4c9b50ce87bdd46909da1
1 /*
2 bug 1839277 & 1839299
3 */
5 #include <testfwk.h>
7 const __code struct Value {
8 const __code char* Name[2];
9 } Values[2]= {{{"abc", "def"}}, {{"ghi", "jkl"}}};
11 char i = 1;
13 void
14 testBug1839277 (void)
16 const char __code* const * volatile p;
17 unsigned long v = 0;
18 //first subexpression 'Values[0].Name' is evaluated 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 evaluated 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
36 //everything is all right with explicit typecast - but why do I need it?
37 p = i ? (const char __code* const *)Values[0].Name : (const char __code* const *)Values[1].Name;
38 #if defined(SDCC_mcs51)
39 v = (unsigned long)p;
40 ASSERT ((unsigned char)(v >> 16) == 0x80);
41 #endif
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 ? (const char __code* const __code*)Values[0].Name : (const char __code* const __code*)Values[1].Name;
46 #if defined(SDCC_mcs51)
47 v = (unsigned long)p;
48 ASSERT ((unsigned char)(v >> 16) == 0x80);
49 #endif
52 void
53 testBug1839299 (void)
55 const char __code* const * volatile 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