How were new features added to C?

Started by
20 comments, last by BitMaster 11 years, 8 months ago

It's the other way round. The linker is the one responsible for "linking together" (whence the name) fully compiled source files. This step is (at least in C derived languages like C++) not part of the compiler but a separate step called by the build system.

It's linking together the assembly files? I thought it was linking together the object files, then assembling it. Googling shows you correct, thanks for the insight.
Advertisement

[quote name='rnlf' timestamp='1346180569' post='4974205']
It's the other way round. The linker is the one responsible for "linking together" (whence the name) fully compiled source files. This step is (at least in C derived languages like C++) not part of the compiler but a separate step called by the build system.

It's linking together the assembly files? I thought it was linking together the object files, then assembling it. Googling shows you correct, thanks for the insight.
[/quote]

Unless you ask the compiler to do so, it is unlikely to have assembly involved at all. The compiler builds object files, which are machine code along with additional information about their contents. The linker takes these and builds an exe, lib, DLL, whatever which is also machine code.

If assembly is involved, it comes between the compiler and the production of the object files, but there is no particular reason for a compiler to compile via assembly unless a human wishes to inspect it.
a little detour into Lex and Yacc , the historical tools to help dummies write compilers, instead of modifying gcc , it is less difficult to start with those.
Thanks for the suggestion Lightness1024, although Lex and Yacc appear outdated and Flex and Bison are the suggested alternative. This has turned out to actually be an interesting topic.
What's This?: basically, it's my blog. Click on it.
It's also worth mentioning that modern compilers actually work quite similarly (at least conceptually) to those early C++ compilers: most compilers still target some sort of intermediate language that provides a level of abstraction over the machine code.

The toolkit de jour is LLVM, which provides an assembly-like intermediate language, and a backend compiler that turns LLVM IR into machine code for a variety of different platforms (see: clang, llvm-gcc).

But mainline GCC also uses a pair of related intermediate languages (called GENERIC and GIMPLE). Mono/.NET provide the 'Common Intermediate Language', to which C#, VB.net and various other languages are compiled. Java has the JVM, Perl has Parrot, and so forth...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

So if I wanted to create a small language, like Squirrell (but one that has a compiler instead of an interpreter), what would be my best option:
a) Modify the gcc source to match my needs,
b) Write my own compiler using Flex and Bison or c) Write my own compiler from scratch

Basically, what would be the easiest option and take the least time? (Sorry if it is getting too offtopic)
What's This?: basically, it's my blog. Click on it.
Understanding the inner workings of a monster-sized project like GCC will likely take you as long as it will take you to write your own compiler using some existing parser generator tool or library.

But honestly, the kind of questions you are asking leads me to believe that you may not yet be ready for writing a compiler. There's much more to it than just reading a source file and spitting out object code. You will have to learn about formal language theory, semantic analysis, code generation (with or without intermediate assembler steps: you will need a strong understanding of your target processor's assembly language and with the processor as a whole), object and executable file formats, optimization, operating system APIs and about everything there is to know about common implementing techniques for your language features.

If this does not scare you, go ahead. You will learn a hell of a lot by doing so. But don't expect it to be an easy task and don't expect to finish it anytime soon. If you don't have any knowledge in these topics, expect it to take you years, rather than months.
I know I am not ready for this kind of thing; which is why I am making a game instead and why I am here on GameDev. I was thinking ahead for what to do when I am old and bored. :D
What's This?: basically, it's my blog. Click on it.

But honestly, the kind of questions you are asking leads me to believe that you may not yet be ready for writing a compiler. There's much more to it than just reading a source file and spitting out object code. You will have to learn about formal language theory, semantic analysis, code generation (with or without intermediate assembler steps: you will need a strong understanding of your target processor's assembly language and with the processor as a whole), object and executable file formats, optimization, operating system APIs and about everything there is to know about common implementing techniques for your language features.

I'm not sure that's actually true (disclaimer: I have a fair background in all those areas). It is definitely helpful to learn the formal stuff (graduate compiler theory courses are great), but eventually you need to dive in and actually write a compiler - and I don't think it matters if you start with that instead.

The majority of simple compilers are basically a filter to transform from source language A to target language B. Provided you pick a suitably featured language for B (i.e. something like LLVM IR, or CLI), the transform need not be anymore complicated than a python script using regular expressions (for example), and it can be a great learning experience.

I highly recommend diving into lexing/parsing (preferably with an easy framework like ANTLR), and gradually working your way up to a full-fledged compiler. It'll be a great learning exercise, if nothing else.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Yes, you're right. It may be as easy as doing some text processing. But I don't think that's what he really wants. If I hear someone talk about compiler programming, I immediately think about a "real" compiler instead of merely a preprocessor. But all in all you're right.

This topic is closed to new replies.

Advertisement