Bug 21185 - add i128 and u128 support to rust lexer and parser
Summary: add i128 and u128 support to rust lexer and parser
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: rust (show other bugs)
Version: unknown
: P2 normal
Target Milestone: 14.1
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks: 20991
  Show dependency treegraph
 
Reported: 2017-02-20 04:27 UTC by Tom Tromey
Modified: 2023-04-17 17:05 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2017-02-20 04:27:36 UTC
Rust nightly has support for i128 and u128 types.
gdb should add support for these at some point;
certainly after the feature is ungated, but maybe before.
See https://github.com/rust-lang/rust/issues/35118
Comment 1 Tom Tromey 2017-05-19 00:18:57 UTC
I tried a hack that typedefs LONGEST and ULONGEST using __int128.
This found a couple of type errors in the source.
Unfortunately the resulting gdb doesn't seem to work properly.
Comment 2 Tom Tromey 2017-05-19 03:35:48 UTC
I fixed up a few things and I'm sending this through the buildbot.
I wonder though if this makes sense to put in.
And, I wonder if my simple changes are really sufficient, given
what I've already had to patch.
Comment 3 Tom Tromey 2017-05-19 04:50:01 UTC
And, hah, the results are really bad.
Comment 4 Pedro Alves 2017-05-19 11:31:37 UTC
I expect you'll run into lots of assumptions that LONGEST == 64-bit.

Do many places in the codebase need to learn about the types?  What does the hack spare you from doing manually?  Arithmetic?
Comment 5 Tom Tromey 2017-05-19 15:34:34 UTC
(In reply to Pedro Alves from comment #4)
> I expect you'll run into lots of assumptions that LONGEST == 64-bit.

Yeah.  There were some spots in a few tdeps that assume that CORE_ADDR
and ULONGEST are the same type.  aarch64-tdep helpfully had self-tests
pointing out some invalid assumptions -- not so with the other ones.
And print-utils makes 64 bit assumptions.

> Do many places in the codebase need to learn about the types?  What does the
> hack spare you from doing manually?  Arithmetic?

I don't have a complete overview of the situation, but some things:

* Printing a 128-bit integer doesn't work well right now.
  ... but that's print-utils, which currently use LONGEST/ULONGEST.
  Could be done manually for Rust, but since GCC has __int128 that
  doesn't seem best.

* Arithmetic.  This maybe means reimplementing a bunch of the value API?
  E.g. value_as_long returns a LONGEST.
  Could operate on the bytes instead, GMP-style.

Hmm, maybe that's all I've got so far.

I pushed the int-128 branch to my github in case you want to take a peek.
Comment 6 Pedro Alves 2017-05-19 19:07:05 UTC
I'm having mixed feelings about LONGEST->__int128  :-(  A lot of code uses (U)LONGEST for indexes, sizes and also to hold register values.  Most of these places won't ever need the extended precision.  I'm not sure whether forcing everything to use 128-bit is a good idea, both efficiency/ABI -wise, and memory-space wise.  :-/
Comment 7 Tom Tromey 2017-05-19 19:35:53 UTC
(In reply to Pedro Alves from comment #6)
> I'm having mixed feelings about LONGEST->__int128  :-(  A lot of code uses
> (U)LONGEST for indexes, sizes and also to hold register values.  Most of
> these places won't ever need the extended precision.  I'm not sure whether
> forcing everything to use 128-bit is a good idea, both efficiency/ABI -wise,
> and memory-space wise.  :-/

Yes, I agree.  I took this approach because I thought there was a chance
that I could get 128-bit support working this way; because my feeling is
that the other ways are probably difficult.

What would be a better plan?

Printing seems reasonably contained, perhaps.  We could introduce
analogues of the print-utils functions that work on gdb_byte arrays,
supporting larger widths (avoiding the need for __int128 on the host).
I suppose there aren't many spots that would need to be changed to make
this work.

Arithmetic seems difficult.  Maybe a new type for this, then (the hard part)
change various places to use the new type rather than (U)LONGEST?
It seems like it would hard to even find the places, unless maybe the new
type was a class that did not allow conversions from integer types.
Hmm...

I wonder if the Python layer also needs updates to be able to represent
such values in Python.

This sounds like a project probably out of reach of my free time.
Comment 8 Tom Tromey 2017-05-22 03:20:21 UTC
Maybe printing doesn't need much attention, since now I see
the print_*_chars family of functions in valprint.c.
Weird that I've never run into these before.
Comment 9 Pedro Alves 2017-05-22 15:49:51 UTC
> It seems like it would hard to even find the places, unless maybe the new
> type was a class that did not allow conversions from integer types.
> Hmm...

Yeah, I think a class (plus arithmetic operators) would be what I'd try too.  
Maybe we could borrow gcc's wide_int (or parts of it).

Not sure about disabling conversions from integer types.  I'd assume it to be handy, to allow natural initialization from 0 and 1 [integer foo = 1;], etc.  I'd think that it's conversion to (narrower) integer types that would be an issue.
Comment 10 Tom Tromey 2017-05-22 22:25:44 UTC
(In reply to Pedro Alves from comment #9)
> > It seems like it would hard to even find the places, unless maybe the new
> > type was a class that did not allow conversions from integer types.
> > Hmm...
> 
> Yeah, I think a class (plus arithmetic operators) would be what I'd try too.
> 
> Maybe we could borrow gcc's wide_int (or parts of it).
> 
> Not sure about disabling conversions from integer types.  I'd assume it to
> be handy, to allow natural initialization from 0 and 1 [integer foo = 1;],
> etc.  I'd think that it's conversion to (narrower) integer types that would
> be an issue.

Yeah, duh, that makes sense.

I've got some cleanups to the scalar printing code that might be useful.
Not quite done -- there's some weird stuff in there.
I haven't looked at the arithmetic stuff.
Comment 11 Tom Tromey 2017-05-25 01:57:08 UTC
This is pretty close, I think, but it turns out there are some tests
that rely on the current behavior where gdb will print 128 bit values
with zero padding; whereas the patch series I have turns this off.
Updating the tests isn't too bad, so far, but since some occur in
arch-specific tests I suspect there will be some that I can't test.
Comment 12 Tom Tromey 2017-08-05 22:01:31 UTC
Printing got fixed in bug 16225, but I think I want to leave this one
open to implement arithmetic.
Comment 13 Tom Tromey 2023-02-23 16:37:20 UTC
I'm going to close this in favor of the other bug.

*** This bug has been marked as a duplicate of bug 20991 ***
Comment 14 Tom Tromey 2023-03-01 21:51:13 UTC
Un-duping and repurposing again as the rust lexer / parser
part of 128-bit ints.
Comment 15 Tom Tromey 2023-03-27 20:28:53 UTC
Working on a patch.
Comment 17 Sourceware Commits 2023-04-17 17:03:47 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d760ae22b964995234e14e090ba179311382b90d

commit d760ae22b964995234e14e090ba179311382b90d
Author: Tom Tromey <tromey@adacore.com>
Date:   Mon Mar 27 13:05:03 2023 -0600

    Add 128-bit integer support to the Rust parser
    
    This adds support for 128-bit integers to the Rust parser.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=21185
Comment 18 Tom Tromey 2023-04-17 17:05:43 UTC
Really fixed.