gpt4 book ai didi

gcc - 编译器如何跨平台(硬件)?

转载 作者:行者123 更新时间:2023-12-04 19:53:52 28 4
gpt4 key购买 nike

我刚刚意识到二进制编译器会将源代码转换为目标平台的二进制文件。有点明显...但如果编译器以这种方式工作,那么同一个编译器如何用于不同的系统,如 x86、ARM、MIPS 等?

难道他们不应该“知道”硬件平台的机器语言才能知道如何构建二进制文件吗?编译器(如 gcc)是否知道受支持的每个平台的机器语言?

该系统如何可能,以及编译器如何同时针对那么多平台进行优化?

最佳答案

是的,他们必须“了解”他们支持的每个平台的机器语言。这是生成机器代码所必需的。然而,编译是一个多步骤的过程。通常,编译的第一步对大多数架构都是通用的。

摘自维基百科

Structure of a compiler

Compilers bridge source programs in high-level languages with the underlying hardware.

A compiler requires

  1. determining the correctness of the syntax of programs,

  2. generating correct and efficient object code,

  3. run-time organization, and

  4. formatting output according to assembler and/or linker conventions.

A compiler consists of three main parts: the frontend, the middle-end, and the backend.

The front end

checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end.

The middle end

is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates another IR for the following backend. Most optimization efforts are focused on this part.

The back end

is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on. Although most algorithms for optimization are in NP, heuristic techniques are well-developed.

更多这篇文章描述了 structure of a compiler在这一个处理Cross compilers .

关于gcc - 编译器如何跨平台(硬件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15454308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com