Skip to content

Fix printing of unsigned integers #1896

Closed

Description

@certik
def main():
    i: u8 = u8(255)
    print(i)

main()

This prints:

$ lpython a.py
-1

But should print 255.

Activity

  1. Smit-create commented on Jun 13, 2023

    @Smit-create
    Collaborator

    So it needs a fix in the LLVM backend, and the fix basically requires keeping the value between [0, 2**(kind*8)). This should be handled at every assignment of unsigned integers and also while initializing. We need not worry about C backend as it will handle the same by itself.

    This also needs some more fixes in LLVM because right now we treat i8 the same as u8 while i8 is unable to store 255. So we need to fix this by:

    u_type = getIntType(max(a_kind*2, 8));
    
  2. certik commented on Jun 13, 2023

    @certik
    ContributorAuthor

    LLVM treats u8 and i8 the same. It's the same LLVM type. The u8(255) is exactly the same bit representation as i8(-1). So I think the only fix we have to do is in the visit_Print() to treat the u8 correctly.

  3. added a commit that references this issue on Jun 13, 2023
    4d2c137
  4. Smit-create commented on Jun 13, 2023

    @Smit-create
    Collaborator

    The fix I was suggesting not just only applies to print. If we fix just print there can be many other failures for the same reason. See the following:

    def main():
        siz: Const[u8] = u8(128)
        ai16: i16[siz] = empty(siz, dtype=int16)
    
    main()
    

    This fails in LLVM but not in C because in LLVM siz is -1.

  5. certik commented on Jun 13, 2023

    @certik
    ContributorAuthor

    The example you posted doesn't work: #1898. But assuming it "works", for now I would not allow unsigned integers in empty. So empty(siz) should give an error message in AST->ASR as well as in verify() that unsigned integer is not supported. In ASR this ends up being part of the "dimensions" and we should enforce in verify() that only signed integers are allowed. I made this #1899.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions