gcc gnatmake Ada 编译器是用什么语言编写的?

发布于 2024-12-16 16:28:37 字数 162 浏览 3 评论 0原文

我是一名计算机科学专业的学生。目前我们正在学习Ada编程语言。现在编译非常简单:gnatmake source.adb 所以我不知道 gnatmake 实际做什么或是什么(脚本?二进制?其他什么?)。不管怎样,我想知道编译器本身(还有解析器,如果用不同的语言编写)是用什么语言编码的?

I'm a computer science student. Currently we are learning the Ada programming language. The compilation is very straightforward for now: gnatmake source.adb so I don't have any idea of what gnatmake actually does or is (script? binary? something else?). Anyway, I was wondering in what language the compiler itself (also the parser, if written in a different language) was coded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随遇而安 2024-12-23 16:28:37

典型的编译器带有两个基本工具:编译器和链接器。

编译器将语言源代码(文本)翻译成机器语言目标文件(二进制文件)。它可能会包含一些对象重定位信息,以帮助链接器。

链接器获取多个机器语言目标文件并将它们全部链接到一个机器可执行文件中。

您通常必须告诉编译器在哪里可以找到执行编译所需的所有额外文件,然后必须告诉链接器将可执行文件链接在一起所需的每个文件的名称,包括您可能使用的系统库。这可能会变得非常复杂,这就是像 make 这样的构建工具的用武之地。Ada

的定义方式是应该始终有某种图书馆员来为您跟踪这些信息。因此,要执行系统的完整构建,您所要做的就是要求图书馆员为您构建可执行文件。

Gnat 处理此库管理功能的方式是假设(除非另有说明)Ada 单元(包)名称与源文件名和目标文件名之间存在一一对应的关系。因此,如果它需要 X 的规范来完成编译,它就会知道去 X.ads 查找规范文件。当链接的时候,它知道它将在 Xo 中找到它的目标文件。这意味着,如果它需要包 X 中的例程才能成功链接,它确切地知道如何为您执行此操作,而愚蠢的链接器只会失败并告诉您“的半加密名称”符号”它找不到。

因此,您不必像使用 C 或 C++ 那样为程序中的每个单元键入单独的编译命令,而是可以使用 gnatmake 来编译给定可执行名称所需的每个单元,并且然后为您将它们链接在一起。一步到位,简单易行。


至于你的最后一个问题,Gnat Ada 编译器几乎完全是用 Ada 编写的。编译器用自己的语言编写并使用自己编译是相当典型的。这称为自托管

然而,它与 GCC 绑定在一起,因此与 GCC 通用的部分(我认为从树格式转换为目标目标代码的部分以及链接器)是用 C 编写的。

Your typical compiler comes with two basic tools: The compiler and the linker.

The comiler translates the language source code (text) into machine language object files (binaries). It will perhaps include some object relocation information, to help the linker.

The linker takes multiple machine language object files and links them all together into one machine executable.

You typically have to tell the compiler where to find all the extra files it needs to perform a compilation, and then you have to tell the linker the name of every file needed to link together your executable, including and system libraries you might use. This can get pretty dang complicated, which is where build tools like make come in.

Ada is defined in such a way that there should always be some kind of librarian to keep track of this information for you. Thus to perform a complete build of your system, all you should have to do is ask the librarian to build your executable for you.

The way Gnat handles this librarian functionality is that it assumes (unless told otherwise) that there's a one-to-one correspondence between Ada unit (package) names and source and object file names. Thus if it needs the specification for X to complete a compile, it knows to go to X.ads to find the specification file. When its time to link, it knows it will find its object file in X.o. This means that if it needs a routine from package X to successfully link, it knows exactly how to do that for you, where a dumb linker would just fail and tell you the semi-encrypted name of the "symbol" it can't find.

So instead of typing out individual compile commands for every unit in your program like you'd have to do with C or C++, you can just use gnatmake to compile every unit the given executable name would need, and then link them together for you. One step, easy peasy.


As for your last question, the Gnat Ada compiler is written almost entirely in Ada. It is fairly typical for compilers to be written in their own language, and compiled using themselves. This is called self-hosting.

However, it is tied into GCC, so the parts that are common with GCC (the part that translates from tree format to target object code and the linker I believe) are written in C.

木格 2024-12-23 16:28:37

在 GCC 源代码树 (gcc/ada) 的 Ada 部分中,有大约 2000 个 Ada 源文件和大约 50 个 C 源文件。 C 充当操作系统和 GCC 其他部分的粘合剂。一些(至少 50 个)Ada 源文件用于 gnatmake 等工具。

您可以通过多种方式使用 -v 选项来了解有关 gnatmake 功能的更多信息:我最常用的是 gnatmake -v source.adb 来了解概述,gnatmake source.adb -cargs -v 来显示如何调用编译器,gnatmake source.adb -largs -v 来了解编译器的概述GNAT 链接器gnatlink 执行此操作,gnatmake source.adb -largs -Wl,-v 详细显示 gnatlink 如何调用系统链接器。

Within the Ada part of the GCC source tree (gcc/ada), there are ~2000 Ada source files and ~50 C source files. The C acts as glue to the OS and to other parts of GCC. Some (at least 50) of the Ada source files are for tools such as gnatmake.

You can see more about what gnatmake does by using the -v option in various ways: the ones I use most often are gnatmake -v source.adb for an overview, gnatmake source.adb -cargs -v to show how the compiler is called, gnatmake source.adb -largs -v for an overview of what the GNAT linker gnatlink does, and gnatmake source.adb -largs -Wl,-v to show in detail how gnatlink calls the system linker.

我不在是我 2024-12-23 16:28:37

gnatmake 只是一个用 Ada 编写的工具。它的作用类似于 make。编译器本身是GNAT,也是用Ada 编写的。它是用多种语言编写的 GCC 的一部分。

gnatmake is just a tool written in Ada. It acts like make. The compiler itself is GNAT, which is written in Ada, too. It is part of GCC, which is written in lots of languages.

一萌ing 2024-12-23 16:28:37

回答问题“这是什么?”的部分。假设您使用的是 unix/linux 类型系统:

$ which gnatmake
/usr/bin/gnatmake
$ file /usr/bin/gnatmake
/usr/bin/gnatmake: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

...所以它是一个二进制文件:)

To answer the part of the question "What is it?" and asuming you are on a unix/linux type system:

$ which gnatmake
/usr/bin/gnatmake
$ file /usr/bin/gnatmake
/usr/bin/gnatmake: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

... so it is a binary :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文