Comments (6)
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.
Ahh, I understand. How does 8cc treat things like ++ -- & inside cpp directives? just the same as identifiers?
from 8cc.
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.
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.
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.
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)
- Add 'install' option to Makefile?
- how to compile HOT 1
- Null dereference
- internal error for static initial value of function pointer
- failed to parse function return function pointers HOT 4
- Compound assignment lvalue evaluated twice.
- Struct arguments size 8 - 32 bytes not passed in registers
- tentative definitions not handled
- Error in tag scoping
- Make libFirm frontend (C11) and backend (x86-64) HOT 1
- internal error for the use of rvalue struct
- is 8cc support to link a executable? HOT 2
- `make test` fails with msg about -fPIC HOT 6
- Hello After applying make and make test command, what should we do for linux ubuntu.
- Segmentation Faults 2017-05-16 HOT 2
- memory leak at vformat function. HOT 1
- Segmentation fault compiling suckless/ubase package HOT 1
- Can't see any assembly code for a self-hosting compiler HOT 4
- Error in Make Test! HOT 5
- del
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 8cc.