Comments (12)
relevant documentation:
C11 6.6 Constant expressions:
"10 An implementation may accept other forms of constant expressions" - ugh, I guess we just need to accept whatever gcc accepts.
from 8cc.
This is the most significant missing feature as far as I know. We need to calculate an address in the compiler and propagate that information to the codegen. The current data structure was not designed with this feature in mind. I'm planning to do that after the codegen rewrite because I'll redesign the data structure for that.
from 8cc.
It is a really tough problem, Here are some crazy examples:
// Works fine on gcc.
int *x = &(int[]){1, 2 ,3}[1];
// Fails on gcc, not constant, even though it is entirely known at compile time.
int x = (int[]){1, 2, 3}[1];
// Works fine on gcc, error on clang, standard says it should not work.
static char []x = *&"foo";
I'm currently revisiting this, because I am implementing it for my own compiler. Basically I am implementing an AST interpreter which operates on values with various types, with concrete or abstract values (which includes nesting in structs and arrays). It is still tricky to match other compilers exactly. Even gcc and clang support different things. This is probably one of the most frustrating and underspecified parts of the C standard for me so far :(
It gets more annoying when mixing constant and non constant expressions in local variable inits.
I would love to see how you solve this, If I solve it to a level I am happy with in my own compiler, I may try and port the solution to 8cc.
from 8cc.
commit 3e05274 adds some progress to this.
from 8cc.
@rui314 - Thought about this any more?
I am thinking it needs two passes. One to check types, array dimensions, and selector fields while parsing, another pass to split inits into two parts - Static and dynamic inits. Globals would only be allowed to have a static component.
The static data would be represented via datastructures something like:
StaticPtrDerived(Sz, PtrLabel, Offset)
StaticConstant(Sz, Value)
StaticString(Value)
StaticArray(CType, Dim, map[Offset] SubValues) // Uninitialized Holes are inferred
StaticStruct(CType, map[Offset] SubValues)
Each would also have an optional assembly Label. There would also need to be a way for anonymous fields to register themselves with a label and with the function.
from 8cc.
Not sure if flattening the Array and Struct static data to be a list of primitive types is worth it as another part.
from 8cc.
I'm currently pretty busy creating a new linker from scratch (https://github.com/llvm-mirror/lld/tree/master/COFF), and I also have other things to do. I'll revisit this later.
from 8cc.
No problem, I'm working on my own solution too, I generally learned a lot from studying your stuff, which Is why I follow this closely still.
About linker: Is linker speed a serious issue for builds? I personally think the C/C++ world has become ridiculous due to bloat and headers. Building linux, llvm and gcc from scratch takes at least 10-20 minutes each on my quad core + ssd + 16 gigs of ram, I read that the plan9 kernel can build itself from scratch on a raspberry pi 2 in 1 minute.
from 8cc.
It really does when you create tens or hundreds of megabytes of executables. It is just too slow. Another problem is that no existing linkers can use multi-cores well. Parallelizing compilation is easy because it can be done just by running many compiler processes simultaneously. But you cannot parallelize the final link, and if the final link uses only one cpu, you are going to waste your time.
from 8cc.
Hmm, yeah, I imagine a 20 or 30 second link after one line of code change is annoying, especially if you have more cores. Sounds pretty cool, With your new knowledge of linkers, linking directly like you attempted before in memory is probably easier haha.
from 8cc.
It is also nice to know that I can write good code even in C++ :) I wrote a fair amount of C++11 code for that, and I kind of like the language.
from 8cc.
Haha, the only time I've enjoyed writing C++ was before I knew python and when I was using Visual Studio. Do you use an IDE for C++?
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.