简体   繁体   English

阅读 GHC 核心

[英]Reading GHC Core

Core is GHC's intermediate language. Core 是 GHC 的中间语言。 Reading Core can help you better understand the performance of your program. Reading Core 可以帮助您更好地了解程序的性能。 Someone asked me for documentation or tutorials on reading Core, but I couldn't find much.有人向我询问有关阅读 Core 的文档或教程,但我找不到太多。

What documentation is available for reading GHC Core?哪些文档可用于阅读 GHC Core?

Here's what I've found so far:这是我到目前为止发现的:

GHC Core is the System FC language into which all Haskell is translated. GHC Core 是系统 FC语言,所有 Haskell 都被翻译成该语言。 The (approximate) grammar for Core is given by: Core 的(近似)语法由下式给出:

在此处输入图像描述

Core is closely related to the simpler and better known System F . Core 与更简单、更广为人知的System F密切相关。 All transformations GHC does on the Core level are type-preserving refactorings of this Core representation, to improve performance. GHC 在核心级别所做的所有转换都是此核心表示的类型保留重构,以提高性能。 And, not so well known, you can write directly in Core to program GHC.而且,不太为人所知,您可以直接在 Core 中编写 GHC 程序。

GHC Core fits in the compiler pipeline (as it was in 2002, sans-LLVM and CMM): GHC Core 适合编译器管道(就像 2002 年一样,sans-LLVM 和 CMM):

在此处输入图像描述

The primary documents to learn about GHC Core are:了解 GHC Core 的主要文件是:

Related material that can aid understanding:有助于理解的相关材料:

  • The GHC -fext-core output GHC -fext-core output
  • I spent a lot of time learning Core by reading GHC source.我花了很多时间通过阅读 GHC 源码来学习 Core。 Some is described in my undergraduate thesis from 2002, from page 16.有些在我 2002 年的本科论文中有所描述,从第 16 页开始。
  • From using the ghc-core tool, to generate Core in a format I find pleasing.从使用ghc-core工具,到生成我喜欢的格式的 Core。

Core in turn is translated into STG code, which looks something like:核心又被翻译成 STG 代码,看起来像:

在此处输入图像描述

The funny names in Core are encoded in the "Z-encoding": Core 中有趣的名字被编码在“Z 编码”中:

在此处输入图像描述

GHC Core's types and kinds (from Tolmach's paper): GHC Core 的类型和种类(来自 Tolmach 的论文):

在此处输入图像描述

Finally, GHC's primops appear regularly in GHC Core output, when you have optimized your Haskell down to the basic instructions GHC knows about.最后,当您将 Haskell 优化到 GHC 知道的基本指令时,GHC 的primops会定期出现在 GHC Core output 中。 The primop set is given as a set of Core functions in a pre-processed file. primop 集作为预处理文件中的一组核心函数给出。

A tip: If you don't care about type annotations and coercions use -ddump-simpl together with the -dsuppress-all option.提示:如果您不关心类型注释和强制,请使用-ddump-simpl simpl 和-dsuppress-all选项。 The Core output should be much more readable.核心 output 应该更具可读性。

Although not exactly the GHC Core language, as Don mentions the STG language is quite similar.虽然不完全是 GHC 核心语言,但正如 Don 提到的 STG 语言非常相似。 I recently went through the exercise of proving type safety of the STG language + machine, and afterwards I found I could understand Core easily.最近做了一个证明STG语言+机器类型安全的练习,后来发现自己可以轻松理解Core了。

The text I used to learn STG is quite accessible: Implementing Lazy Functional Languages on Stock Hardware: The Spineless Tagless G-machine by Simon Peyton-Jones.我用来学习 STG 的文本很容易理解:在 Stock Hardware 上实现惰性函数式语言:Simon Peyton-Jones 的 Spineless Tagless G-machine Much of the paper is concerned with implementation details, but I recommend section 4 in particular as a top-to-bottom explanation of the STG language that gives motivations for some of the counter-intuitive design decisions and provides translations of familiar examples like map .这篇论文的大部分内容都与实现细节有关,但我特别推荐第 4 节作为对 STG 语言的自上而下的解释,它为一些违反直觉的设计决策提供了动机,并提供了map等熟悉示例的翻译。

"An External Representation for the GHC Core Language" is a document which can be found in the installation of ghc ( share/doc/ghc/core.pdf ) or on the internet . “GHC 核心语言的外部表示”是一个文档,可以在 ghc 的安装 ( share/doc/ghc/core.pdf ) 或互联网上找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM