Re: [Tinycc-devel] Tinycc-devel Digest, Vol 134, Issue 16

2014-06-23 Thread Michael Matz

Hi,

On Sun, 22 Jun 2014, jiang wrote:


This is my patch (see Annex)

tcc result is correct
-- 254 / 30 / 126


That's not the correct result.  If you think it is the probably because 
you're confused by the semantics of assignments as rvalue.



I guess gcc  mvc repeated use of the register, so wrong


No, GCC and MSVC are correct.


struct {
unsigned a:9, b:5, c:7;
} _s, *s = _s;
int n = 250;

s-a = s-b = s-c = n + 4;


To show that GCC is correct (and TCC wrong, and your patch still wrong) 
rewrite the above into the explicit expansion according to associativity 
of '=':


  s-c = n + 4;  // s-c == 254  127 == 126
  s-b = s-c;   // s-b == 126  31  == 30
  s-a = s-b;   // s-a == 30

In particular the value loaded into s-a comes from s-b, and hence is the 
truncated value, which isn't changed anymore as 5 bits fits into 9 bits.



Ciao,
Michael.

___
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel


Re: [Tinycc-devel] Tinycc-devel Digest, Vol 134, Issue 16

2014-06-21 Thread jiang

This is my patch (see Annex)

tcc result is correct
-- 254 / 30 / 126

1 1 1 1 1 1 1 0 - 254
254  0x7f (c:7)

1 1 1 1 1 1 1 0 - 254
0 1 1 1 1 1 1 1 - 0x7f
--
0 1 1 1 1 1 1 0 - 126

(b:5)
0 1 1 1 1 1 1 0 - 126
0 0 0 1 1 1 1 1 - 0x1f
--
0 0 0 1 1 1 1 0 - 30


(a:9)
0 0 0 0 1 1 1 1 0 - 30
1 1 1 1 1 1 1 1 1 - 0x1ff
--
0 0 0 1 1 1 1 0 - 30

I guess gcc  mvc repeated use of the register, so wrong

于 2014年06月21日 22:25, tinycc-devel-requ...@nongnu.org 写道:

jiang wrote:

/* bitfield store handling */
+ SValue tmp;
+ tmp = vtop[0];
[...]
+ vtop--;
+ vpushv(tmp);



This is still not a solution. See

#include stdio.h
int main(int argc, char **argv)
{
struct {
unsigned a:9, b:5, c:7;
} _s, *s = _s;
int n = 250;

s-a = s-b = s-c = n + 4;
printf(-- %d / %d / %d\n, s-a, s-b, s-c);
return 0;
}
-- 432 / 16 / 126

gcc  msvc:
-- 30 / 30 / 126
tcc release_0_9_26
-- 254 / 30 / 126

FWIW, the line above is the reason why I'm trying to investigate
this. Let's see if Mr. jiang can come up with something useful
(and how long it will take).

-- gr 


commit 61bf739f355787a06270bf5be380dc86c4c8c891
Author: jiang 30155...@qq.com
Date:   Sun Jun 22 01:29:05 2014 +0800

bug:
struct { unsigned a:9, b:7, c:5; } s;
s.a = s.b = s.c = 3;
 printf(%d / %d / %d\n, s.a, s.b, s.c);

out:
0 / 0 / 3

and:

struct {
  unsigned a:9, b:5, c:7;
 } _s, *s = _s;
 int n = 250;

 s-a = s-b = s-c = n + 4;
  printf(-- %d / %d / %d\n, s-a, s-b, s-c);
out:
- 0 / 0 / 126

diff --git a/tccgen.c b/tccgen.c
index 7906ccf..d70506a 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -2562,6 +2562,12 @@ ST_FUNC void vstore(void)
 /* leave source on stack */
 } else if (ft  VT_BITFIELD) {
 /* bitfield store handling */
+if(cc)
+vpushv(vtop[0]);
+else
+gv_dup();
+vrott(3);
+
 bit_pos = (ft  VT_STRUCT_SHIFT)  0x3f;
 bit_size = (ft  (VT_STRUCT_SHIFT + 6))  0x3f;
 /* remove bit field info to avoid loops */
@@ -2598,6 +2604,7 @@ ST_FUNC void vstore(void)
 gen_op('|');
 /* store result */
 vstore();
+vtop--;
 } else {
 #ifdef CONFIG_TCC_BCHECK
 /* bound check case */
@@ -4086,7 +4093,7 @@ ST_FUNC void unary(void)
 } else if (tok == '[') {
 next();
 gexpr();
-if(tcc_state-warn_char_subscripts  (vtop-type.t  
(VT_BTYPE|VT_UNSIGNED)) == VT_BYTE)
+if(tcc_state-warn_char_subscripts  (vtop-type.t  
(VT_BTYPE|VT_DEFSIGN|VT_UNSIGNED)) == VT_BYTE)
 tcc_warning(array subscript has type 'char');
 gen_op('+');
 indir();
diff --git a/tests/tests2/03_struct.c b/tests/tests2/03_struct.c
index c5d48c5..48e6fd5 100644
--- a/tests/tests2/03_struct.c
+++ b/tests/tests2/03_struct.c
@@ -27,5 +27,30 @@ int main()
printf(%d\n, jones[1].boris);
printf(%d\n, jones[1].natasha);
 
+   struct sbf1 {
+int f1 : 3;
+int : 2;
+int f2 : 1;
+int : 0;
+int f3 : 5;
+int f4 : 7;
+unsigned int f5 : 7;
+   } st1;
+   st1.f1 = st1.f2 = st1.f3 = st1.f4 = st1.f5 = 3;
+   printf(%d %d %d %d %d\n,
+ st1.f1, st1.f2, st1.f3, st1.f4, st1.f5);
+
+   struct { unsigned a:9, b:7, c:5; } s1;
+s1.a = s1.b = s1.c = 3;
+   printf(%d / %d / %d\n, s1.a, s1.b, s1.c); 
+
+   struct {
+unsigned a:9, b:5, c:7;
+   } s2, *ps = s2;
+   int n = 250;
+
+   ps-a = ps-b = ps-c = n + 4;
+   printf(%d / %d / %d\n, ps-a, ps-b, ps-c);
+
return 0;
 }
diff --git a/tests/tests2/03_struct.expect b/tests/tests2/03_struct.expect
index ecbf589..00caf0d 100644
--- a/tests/tests2/03_struct.expect
+++ b/tests/tests2/03_struct.expect
@@ -4,3 +4,6 @@
 34
 56
 78
+3 -1 3 3 3
+3 / 3 / 3
+254 / 30 / 126
___
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel