Coder Social home page Coder Social logo

Translate __FUNCTION__ about corrode HOT 6 CLOSED

jameysharp avatar jameysharp commented on May 9, 2024
Translate __FUNCTION__

from corrode.

Comments (6)

jameysharp avatar jameysharp commented on May 9, 2024 1

Whew, that's a lot of good questions! Bear with me here.

Your general approach is great, and I think it should work.

The comment about "wide character and string literals" is still accurate but, in hindsight, confusing to read: neither wide characters, nor wide string literals, are implemented. String literals composed of not-wide characters work fine. πŸ˜„

From my experiments, passing -U__FILE__ -U__LINE__ also works, and seems nicer than defining them to expand to themselves. You can implement that in Main.md; just add these names to the same list that has __BLOCKS__ in it.

Getting the name of the current function is going to be a little tricky. I promised you wouldn't have to think about monads and you're making a liar of me. πŸ˜‰ My suggestion: get __FILE__ and __LINE__ working in one patch, let me test and merge that, and then let's chat about how to plumb the function name into the right place.

I think once you fix the CType values for those two symbols, you'll be pretty much there, right?

By the way, I think C99 spells the function name __func__, and GCC also has a __PRETTY_FUNCTION__ that some things use. So when you tackle function names, let's make all three spellings work equivalently.

from corrode.

jdub avatar jdub commented on May 9, 2024

So, I got confused about dynamic generation (terminology?) of map entries and adding simple static entries:

    builtinSymbols =
        [ ("__builtin_bswap" ++ show w,
            ("u" ++ show w ++ "::swap_bytes",
                Just (Rust.Immutable,
                    IsFunc (IsInt Unsigned (BitWidth w))
                        [(Nothing, IsInt Unsigned (BitWidth w))] False
            )))
        | w <- [16, 32, 64]
        ]

I wanted to put a map element delimiting comma after the | w <- line, but that definitely didn't work. So I did what feels hacky, but works:

        ...
        | w <- [16, 32, 64]
        ]
        ++
        [ ("__FUNCTION__",
            ("pantalones",
                Just (Rust.Immutable, charType
            )))
        ]

The returned string is temporary, because I haven't done the function lookup yet, and the CType is obviously bollocks. getSymbolIdent requires that I return a CType, but I can't figure out what to use for a string literal… probably because:

TODO: Translate wide character and string literals.

D'oh!

How am I doing here?

Meanwhile, I now realise that __FILE__ and __LINE__ are predefined macros in the C preprocessor, which seems nice, until you realise they're correct for C but not for Rust. Hmm. Seems like the easiest way around this is to add -D__FILE__=__FILE__ -D__LINE__=__LINE__ to the cc -E flags.

from corrode.

jdub avatar jdub commented on May 9, 2024

Wait, I can use IsPtr / as_ptr(), as per the C string literal conversion below that FIXME. (Perhaps that FIXME is out of date.)

from corrode.

jdub avatar jdub commented on May 9, 2024

Ignoring the CType (and function name) for a moment, is this even remotely sensible?

        ...
        | w <- [16, 32, 64]
        ]
        ++
        [ ("__FILE__",
            ("file!().as_ptr()",
                Just (Rust.Immutable,
                    IsFunc (IsInt Unsigned (BitWidth 32))
                        [(Nothing, IsInt Unsigned (BitWidth 32))] False
            )))
        ,
          ("__LINE__",
            ("line!()",
                Just (Rust.Immutable,
                    IsFunc (IsInt Unsigned (BitWidth 32))
                        [(Nothing, IsInt Unsigned (BitWidth 32))] False
            )))
        ,
          ("__FUNCTION__",
            ("b\"function\\0\".as_ptr()",
                Just (Rust.Immutable,
                    IsFunc (IsInt Unsigned (BitWidth 32))
                        [(Nothing, IsInt Unsigned (BitWidth 32))] False
            )))
        ]

from corrode.

jameysharp avatar jameysharp commented on May 9, 2024

Hey @jdub, I said I'd help you implement this once I got your __FILE__/__LINE__ patch merged.

Well: After I merged that patch I realized that (1) the way I suggested you do that was kind of a weird way, and really I needed to clean up symbol lookup; and (2) I wasn't prepared to teach enough about reader monads to explain how to implement __FUNCTION__.

So I implemented this translation myself. I hope you'll forgive me for failing my pedagogical goals here! If you're interested, I'm hoping that the relevant two commits are pretty clear (a263511 and cefa0cf, respectively), and feel free to ask questions.

Note that the assert macro still doesn't generate valid Rust, at least on glibc, because of issue #76. But at least Corrode is happy to generate something for it, now!

from corrode.

jdub avatar jdub commented on May 9, 2024

Oh, a263511 makes craploads more sense! I thought the stringy literalness of that was ooky compared to everything else.

I'll have to read cefa0cf in context to better understand how getFunctionName interacts with everything else, but I'm glad it fits my assumptions of how it would fit in with getSymbolIdent.

from corrode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.