> -----Original Message-----
> From: Michael Matz <m...@suse.de>
> Sent: Wednesday, January 15, 2025 09:50
> To: Robert Dubner <rdub...@symas.com>
> Cc: Richard Biener <rguent...@suse.de>; jklow...@symas.com; Joseph Myers
> <josmy...@redhat.com>; gcc-patches@gcc.gnu.org
> Subject: RE: [PATCH] COBOL 3/8 gen: GENERIC interface
> 
> Hello,
> 
> On Mon, 23 Dec 2024, Robert Dubner wrote:
> 
> > > +static tree
> > > +gg_get_larger_type(tree A, tree B)
> > > +  {
> > > +  tree larger = TREE_TYPE(B);
> > > +  if( TYPE_SIZE(TREE_TYPE(A)) > TYPE_SIZE(TREE_TYPE(B)) )
> > > +    {
> > > +    larger = TREE_TYPE(A);
> > >
> > > that doesn't work - TYPE_SIZE is a pointer to a tree.  You can use
> > > tree_int_cst_compare (TYPE_SIZE(TREE_TYPE(A)),
> > > TYPE_SIZE(TREE_TYPE(B))) == 1 instead.
> >
> > In any case, and given that gg_get_larger_type() is used by me in my
> > code on variables I define, rather than on user code on variables the
> > user defines, I believe it is doing what I need it to do.  For
> > example, when I need to multiply tree A by tree B, I need them to both
> > be of the same type in order to keep the TRUNC_DIV_EXPR from
> > complaining that they aren't the same type.  So, I use
> > gg_get_larger_type(A, B) to return the type of the one with the larger
> TYPE_SIZE, and then I cast both A and B to that type.
> 
> It may have been lost in the other (very interesting!) explanations
about
> Cobol peculiarities, but no, the gg_get_larger_type() as written (and
> cited above) is _not_ doing what you want it to do.  TYPE_SIZE is a
> pointer, hence 'TYPE_SIZE(foo) > TYPE_SIZE(bar)' compares two pointers,
> not two numbers, and so what it returns is not the larger-sized type but
> simply the one whose size description was allocated at higher addresses
in
> GCCs memory pool.  That may or may not coincide with those trees
> representing higher numbers.

Well, that's seriously not what I need, there.  

I love your "...may or may not coincide...", which brings us to the
mathematical formulation of Murphy's Law:

"If there is a fifty-fifty chance of something going wrong, nine times out
of ten it will."

So, let's see...

I have rewritten that function as

static tree
gg_get_larger_type(tree A, tree B)
  {
  tree larger = TREE_TYPE(B);
  if(    TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(A))) 
       > TREE_INT_CST_LOW(TYPE_SIZE(TREE_TYPE(B))) )
    {
    larger = TREE_TYPE(A);
    }
  return larger;
  }

I take heart from this.  The fact that the existing flawed code
nonetheless works in our hundreds of regression tests provides happy
evidence that my upstream efforts to make sure that operations on
variables take place between variables of the same type have (within
Murphy's limits) been successful, since when A and B are the same type,
then TREE_TYPE for both of them returns the same thing, and the comparison
is always between equal values.

But that's a case of being more lucky than good.

Thank you, thank you, thank you!

Bob D.

> 
> 
> Ciao,
> Michael.

Reply via email to