C/C++ 编译器如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1085490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:44:14  来源:igfitidea点击:

How do C/C++ compilers work?

c++ccompiler-constructioncompiler-optimization

提问by Justicle

After over a decade of C/C++ coding, I've noticed the following pattern - very good programmers tend to have detailed knowledge of the innards of the compiler.

经过十多年的 C/C++ 编码,我注意到以下模式 - 非常优秀的程序员往往对编译器的内部结构有详细的了解。

I'm a reasonably good programmer, and I have an ad-hoc collection of compiler "superstitions", so I'd like to reboot my knowledge and start from the basics.

我是一个相当不错的程序员,我有一个编译器“迷信”的临时集合,所以我想重新启动我的知识并从基础开始。

Can anyone recommend links to online resources or favorite books? I'm particularly interested in C/C++ compiling, optimization, GCC and LLVM.

任何人都可以推荐在线资源或最喜欢的书籍的链接吗?我对 C/C++ 编译、优化、GCC 和 LLVM 特别感兴趣。

采纳答案by sourabh jaiswal

Start with the dragon book....(stress more on code optimization and code generation)

从龙书开始....(更强调代码优化和代码生成)

Go onto write a toy compiler for an educational programming language like Decaf or Cool.., you may use parser generators (lex and yacc) for your front end(to make life easier and focus on more imp stuff)....

继续为诸如 Decaf 或 Cool 之类的教育编程语言编写一个玩具编译器..,您可以为前端使用解析器生成器(lex 和 yacc)(使生活更轻松并专注于更多的东西)...

Then read gcc internals book along with browsing gcc source code.

然后阅读 gcc internals book 以及浏览 gcc 源代码。

回答by NoMoreZealots

Compiler Text are good, but they are a bit heavy for teaching yourself. Hyman Crenshaw has a "Book" that was a series of articles you can download and read call "Lets Build a Compiler." It follows a "Learn By Doing" methodology that is great if you didn't get anything out of taking formal classes on the subject, or it's been WAY too many years since took it (that's my case). It holds your hand and leads you through writting a compiler instead of smacking you around with Lambda Calculus and deep theoretical issues that only academia cares about. It was a good way to stir up those brain cells that only had a fuzzy memory of writting something on the Vax (YEAH, that right a VAX!) many many moons ago at school. It's written very conversationally and easy to just sit down and read, unlike most text books which require several pots of coffee just to get past the first chapter. Once you have a basis for understanding then more traditional text such as the Dragon book are great references to expand on your understanding. (And personal I like the Dead Tree versions, I printed out Hyman's, it's much easier to read in a comfortable position than on a laptop. And the Ebook readers are too expensive for something that doesn't actually feel like you're reading a real book yet.)

Compiler Text 很好,但它们对于自学来说有点沉重。Hyman Crenshaw 有一本“书”,它是一系列文章,您可以下载并阅读称为“让我们构建编译器”。它遵循一种“边做边学”的方法,如果你没有从关于这个主题的正式课程中得到任何东西,或者它已经很多年了(这就是我的情况),这是很好的。它牵着你的手,引导你编写一个编译器,而不是用 Lambda 微积分和只有学术界关心的深刻理论问题来对付你。这是一个很好的方式来激发那些脑细胞的好方法,这些脑细胞只是在很多个月前在学校里写过一些东西的模糊记忆(是的,这就是 VAX!)。它写得非常对话,很容易坐下来阅读,不像大多数教科书需要几壶咖啡才能通过第一章。一旦你有了理解的基础,那么龙书等更传统的文本是扩展你理解的很好的参考。(我个人喜欢 Dead Tree 版本,我打印了 Hyman's,在舒适的位置上阅读比在笔记本电脑上容易得多。而且电子书阅读器太贵了,你实际上并不觉得你正在阅读真正的书。)在舒适的位置上阅读比在笔记本电脑上容易得多。而且电子书阅读器太贵了,以至于您实际上还没有在阅读一本真正的书。)在舒适的位置上阅读比在笔记本电脑上容易得多。而且电子书阅读器太贵了,以至于您实际上还没有在阅读一本真正的书。)

What some might call a "downside" is that it's written in Pascal, but I thought that just made me think about it more than if someone had given me a working C program to start with. Appart from that it was written with the 68000 in mind, which is only being used in embedded systems at this point time. Again for me this wasn't a problem, I knew 68000 asm and 68000 asm is easier to read than some other asm.

有些人可能称之为“缺点”是它是用 Pascal 编写的,但我认为这让我更加思考它,而不是如果有人给我一个工作的 C 程序开始。除此之外,它是为 68000 编写的,目前仅在嵌入式系统中使用。对我来说这又不是问题,我知道 68000 asm 和 68000 asm 比其他一些 asm 更容易阅读。

回答by J-16 SDiZ

If you want dead-tree edition, try The Art of Compiler Design: Theory and Practice.

如果您想要死树版本,请尝试编译器设计的艺术:理论与实践

回答by Norman Ramsey

As noted by Pete Eddy, Hyman Crenshaw's tutorial is excellent for newbies. But if you want to see how to a real, production C compiler works—one which was designedby brilliant engineers instead of created by throwing code at the wall until something stuck—get yourself a copy of Fraser and Hanson's A Retargetable C Compiler: Design and Implementation, which contains the source code to the very clean lcccompiler. Explanations of the design and implementation are mixed in with the code. It is not a first book for a beginner, but it will repay careful study, and you can get a used copy for $35.

正如 Pete Eddy 所指出的,Hyman Crenshaw 的教程非常适合新手。但是,如果您想了解真正的生产 C 编译器如何工作的——它是由杰出的工程师设计的,而不是通过将代码扔到墙上直到出现问题来创建的——请给自己一份 Fraser 和 Hanson 的A Retargetable C Compiler: Design的副本和 implementation,其中包含非常干净的lcc编译器的源代码。设计和实现的解释与代码混合在一起。这不是初学者的第一本书,但它会回报仔细研究,您可以花 35 美元获得一本二手书。

For a longer blurb about lcc, see Compile C Faster on Linux.

对于较长的Blurb有关lcc,见编译C在Linux上更快

The lcc web page also has links to a number of good textbooks. I don't know of an intro text that I really like, however.

lcc 网页也有许多优秀教科书的链接。但是,我不知道我真正喜欢的介绍文字。

P.S. Sorry you got ripped off at Uni.

PS 对不起,你在大学被骗了。

回答by plan9assembler

see Fabrice Bellard's otcc source code

参见 Fabrice Bellard 的 otcc 源代码

http://bellard.org/otcc/

http://bellard.org/otcc/

回答by Patrick Cornelissen

Depending on what you exactly want to know, you should have a look at pipes&filter pattern, because as far as I know this (or something similar) is used in a lot of compilers in the last years.

根据您确切想知道的内容,您应该查看管道和过滤器模式,因为据我所知,过去几年在很多编译器中都使用了这个(或类似的东西)。

When my compiler knowledge is not too outdated it works like this:

当我的编译器知识不太过时时,它的工作方式如下:

Parse sourcecode into symbolic representation

将源代码解析为符号表示

Clean up symbolic representation, do some normalization

清理符号表示,做一些规范化

Optimization of the symbolic tree based on certain rules

基于一定规则的符号树优化

write out executable code based on symbolic tree

基于符号树写出可执行代码

Of course dependencies etc. have to be resolved too.

当然依赖等也必须解决。

And of course having a look at gcc or javac sourcecode may help in getting more detailed understanding.

当然,查看 gcc 或 javac 源代码可能有助于获得更详细的理解。

回答by RBerteig

It may also be valuable to pick up and read the source code to a compiler. I doubt that GCC is the best first choice, since it is burdened with full compatibility to more than 20 years of evolution of the language. But I'm also sure that a reading of its source, guided by one of the internal reference manuals, would be educational.

获取源代码并将其读取给编译器也可能很有价值。我怀疑 GCC 是否是最好的首选,因为它背负着对语言 20 多年演变的完全兼容性。但我也确信,在其中一本内部参考手册的指导下阅读其来源将具有教育意义。

I'd seriously consider looking at the source to a scripting language that is internally compiled to a bytecode for a virtual machine. Several languages fit that description, but I would start with Lua. The language is small, and the VM is novel. The source codeis also small and the bits I've looked at have been very clear although lightly commented.

我会认真考虑查看脚本语言的源代码,该语言在内部编译为虚拟机的字节码。有几种语言符合这种描述,但我将从Lua开始。语言小,VM新颖。该源代码也很小,我已经看过了位已经很清楚了,虽然轻易评论。

回答by name

have a look on Kaleidoscope. You can write your own compiler in just a few days with LLVM.

看看万花筒。您可以在短短几天内使用 LLVM 编写自己的编译器。