78dae846f22326b6f56eafdbcbe59e445f175875
[fw/sdcc] / support / regression / tests / bug1839277.c
1 /*
2     bug 1839277
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 testBug(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 }