Giter Site home page Giter Site logo

Comments (6)

rui314 avatar rui314 commented on July 23, 2024

Funny you should mention the operator precedence algorithm. I used to use that in parse.c to parse an expression. I rewrote it as a recursive-descent parser at some point.

The expression allowed after #if is not different from C expression. It's evaluated in the same way as described in C11 6.5. The difference is that all the identifiers are replaced with "0" before evaluation. As a result, a seemingly legitimate expression "#if sizeof(int)" is, for example, replaced with "#if 0(0)", which cause the compiler to fail. Although the grammar allows you to use more complex expression like casts after #if, you cannot access them because of the token replacement.

CPP expression is a subset of C constant expression. I don't think we should have another evaluator for CPP.

from 8cc.

andrewchambers avatar andrewchambers commented on July 23, 2024

Ahh, I understand. How does 8cc treat things like ++ -- & inside cpp directives? just the same as identifiers?

from 8cc.

andrewchambers avatar andrewchambers commented on July 23, 2024

And I agree that recursive descent is much easier to understand and debug. Even if its less efficient. 8cc is easily fast enough.

from 8cc.

rui314 avatar rui314 commented on July 23, 2024

Operators like + or & (or ++, which has no use in CPP) are classified as punctuator, not identifier, so they will remain the same after identifier replacement and just passed to the evaluator.

from 8cc.

andrewchambers avatar andrewchambers commented on July 23, 2024

Ah ok - I think you are mostly correct.

One note:

quoting https://gcc.gnu.org/onlinedocs/cpp/If.html

"It carries out all calculations in the widest integer type known to the compiler; on most machines supported by GCC this is 64 bits. This is not the same rule as the compiler uses to calculate the value of a constant expression, and may give different results in some cases."

Doesn't this means the cpp evaluator needs to use 64 bits, where normal constant folding may be 32 bits after integer promotion? I think overflow on #if is incredibly rare anyway so it shouldn't matter at all in practice.

On a side note, I've setup an automatic bug finder using the gcc test suite and creduce. It focuses only on code that both gcc and tcc can correctly compile.
In a few minutes I found at least 8 bugs that can be reduced to a few lines of code. I'll hold off until you are done reworking the backend, but i'll setup a vagrant vm image + instructions so you can easily reproduce what I am doing for yourself when you are ready.

from 8cc.

rui314 avatar rui314 commented on July 23, 2024

Maybe the easiest way to mimic the GCC behavior is to provide a way to set default integer type. By default, a pp-number, say "0", is converted to 0 of type int. We could define a flag to make the parser convert a token to of type long. While we are reading an expression from CPP, we would turn the flag on, so that all integers are automatically read as if they had L prefix. That forces the evaluator to compute the expression in 64 bit precision.

from 8cc.

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.