Live data from Hacker News

Show HN: My C compiler compiled itself

github.com

121–130 of 130 posts

Re: Show HN: My C compiler compiled itself

#121

Earlier quoted context omitted.

> sizeof is a unary operator so the parentheses are rarely necessary I thought parentheses where still required for types, and are optional for variables. Even if that's not so, I still use them for types and avoid them for variables just to add one more cue about what's being sized.

Not "variables," but "expressions." In both C and C++, `sizeof` is a unary operator that applies to a postfix expression, so e.g. `sizeof a[0]` and `sizeof p->x` are both OK. This can theoretically be confusing, although the only examples I know are silly and/or easily fixed: In C++, `p-> pmf` has surprisingly low precedence, so `sizeof p-> pmf` means `sizeof(p) ->* pmf`, which is ill-formed. https://gcc.gnu.org/onli…

Thank you very much for that clarification. I knew about using sizeof with expressions, but I so rarely used it that way that I never gave it much thought.

Re: Show HN: My C compiler compiled itself

#122
post #80

Earlier quoted context omitted.

I guess the full bootstrapping process would start with writing a basic compiler in assembly?

The full bootstrapping process starts with entering instructions in binary (or hex or octal) to build an assembler in the first place :)

That is correct the path chosen by the Bootstrappable Builds project indeed:

https://bootstrappable.org/ https://lwn.net/Articles/983340/

Re: Show HN: My C compiler compiled itself

#123
post #87

Earlier quoted context omitted.

I guess the full bootstrapping process would start with writing a basic compiler in assembly?

It depends on definition of full bootstrapping process. At some point, someone has to bootstrap in raw assembly but we don't need to do that in 2024. If I write a bootstrap compiler in c on a machine that can run c, I can write an alternate backend for any assembly language (that supports the necessary compilation features) and thus produce a compiler implemented in any assembly language without directly writing any…

These folks say that you do need that in 2024 (commented machine code actually):

https://bootstrappable.org/ https://lwn.net/Articles/983340/

Re: Show HN: My C compiler compiled itself

#126
post #89

Earlier quoted context omitted.

Saying this is an 'incremental makefile' isn't really correct, as changes to header files aren't going to lead to rebuilding. So, either you need to manually keep references to which .h files you include in your Makefiles up to date, or start worrying about M / MM / MG / MP, and of course you'd like those to be re-run when you change your files, and suddenly your Makefile is an awful lot less simple. This is the main…

You could have taught them `make clean` whenever there's any issues. I work with software engineers and I still have to remind them to try rm -rf ./build/* now and again, which always seems to solve the problem.

I do, but it’s easy for experts to forget, and when you are in your first few weeks of C it’s even easier to forget with everything else you have to remember.

Re: Show HN: My C compiler compiled itself

#127

Earlier quoted context omitted.

> sizeof is a unary operator so the parentheses are rarely necessary I thought parentheses where still required for types, and are optional for variables. Even if that's not so, I still use them for types and avoid them for variables just to add one more cue about what's being sized.

Not "variables," but "expressions." In both C and C++, `sizeof` is a unary operator that applies to a postfix expression, so e.g. `sizeof a[0]` and `sizeof p->x` are both OK. This can theoretically be confusing, although the only examples I know are silly and/or easily fixed: In C++, `p-> pmf` has surprisingly low precedence, so `sizeof p-> pmf` means `sizeof(p) ->* pmf`, which is ill-formed. https://gcc.gnu.org/onli…

Surprisingly, the sizeof operator applies to prefix expressions, too, except casting. So sizeof !x works, but sizeof (int)x doesn't, despite ! and casting having the same precedence.

The presence of (int) sort of promotes sizeof from "operator that applies to expressions" to "function that takes a type". I think this is a side effect of reusing the same keyword for two syntactically different constructs.

Re: Show HN: My C compiler compiled itself

#128
post #105

Earlier quoted context omitted.

I did mention that in other comments, including the C preprocessor helping. make is completely language agnostic, so it cannot “guess” header dependencies. In make, you get the dependencies you specify (whether manual or generated), that’s what I wanted to show.

Make makes the easy part easy but the rest quite hard -- which I think you agree with. It sure would be nice to have something like this: codegen/codegen.o: !codegen/codegen.c $(CC) -c $(CFLAGS) -o $@ $ '!' would run $(CC) with -MD to extract the header dependencies. Doing that back in 1995 would have increased the value and usability of GNU make immensely (and greatly reduced the need for all the other complexity it…

Wouldn't this require make to run CC with -MD on each instance of ! every time it's invoked, just to collect the list of dependencies, maybe just to discover that no file has changed?

Re: Show HN: My C compiler compiled itself

#129
post #92

Out of curiosity: would ./30cc_gcc (30cc complied by gcc-hosted 30cc) and ./30cc (30cc complied by self-hosted 30cc) be identical binary files, if 30cc was operating as expected?

No, gcc optimizes code output and can make code shorter and faster. A compiler can make many, many decisions which will show up as differences in the binary output.

In my question above, both compilers are 30cc, except one of the 30cc's was compiled by gcc and the other self-hosted.

Re: Show HN: My C compiler compiled itself

#130
post #129

Earlier quoted context omitted.

No, gcc optimizes code output and can make code shorter and faster. A compiler can make many, many decisions which will show up as differences in the binary output.

In my question above, both compilers are 30cc, except one of the 30cc's was compiled by gcc and the other self-hosted.

Ah, sorry. In that case, my guess is, probably yes. The outputs of both of these are probably the same. But not for certain - see:

https://www.cs.cmu.edu/~rdriley/487/papers/Thompson_1984_Ref...

Post reply on HN