-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ed774a5
Showing
110 changed files
with
12,433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* linguist-language=Go |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2022 <chaishushan{AT}gmail.com>. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
# | ||
# waBook: Mini Markdown Book | ||
# https://github.com/wa-lang/wabook | ||
# | ||
|
||
default: | ||
wabook serve | ||
|
||
build: | ||
-rm book | ||
wabook build | ||
-rm book/.gitignore | ||
-rm book/.nojekyll | ||
-rm -rf book/.git | ||
|
||
deploy: | ||
-@make clean | ||
wabook build | ||
-rm book/.gitignore | ||
-rm -rf book/.git | ||
-rm -rf book/examples | ||
|
||
cd book && git init | ||
cd book && git add . | ||
cd book && git commit -m "first commit" | ||
cd book && git branch -M gh-pages | ||
cd book && git remote add origin git@github.com:wa-lang/ugo-compiler-book.git | ||
cd book && git push -f origin gh-pages | ||
|
||
clean: | ||
-rm -rf ./book | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
<!DOCTYPE HTML> | ||
<html lang="zh" class="sidebar-visible no-js light"> | ||
<head> | ||
<!-- Book generated using https://github.com/wa-lang/wabook --> | ||
<meta charset="UTF-8"> | ||
<title>附录 - µGo语言实现</title> | ||
<!-- Custom HTML head --> | ||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="theme-color" content="#ffffff" /> | ||
|
||
<link rel="icon" href="../favicon.svg"> | ||
<link rel="shortcut icon" href="../favicon.png"> | ||
<link rel="stylesheet" href="../static/wabook/css/variables.css"> | ||
<link rel="stylesheet" href="../static/wabook/css/general.css"> | ||
<link rel="stylesheet" href="../static/wabook/css/chrome.css"> | ||
<link rel="stylesheet" href="../static/wabook/css/print.css" media="print"> | ||
<!-- Fonts --> | ||
<link rel="stylesheet" href="../static/wabook/FontAwesome/css/font-awesome.css"> | ||
<link rel="stylesheet" href="../static/wabook/fonts/fonts.css"> | ||
<!-- Highlight.js Stylesheets --> | ||
<link rel="stylesheet" href="../static/wabook/highlight.css"> | ||
<link rel="stylesheet" href="../static/wabook/tomorrow-night.css"> | ||
<link rel="stylesheet" href="../static/wabook/ayu-highlight.css"> | ||
|
||
<!-- Custom theme stylesheets --> | ||
</head> | ||
<body> | ||
<!-- Provide site root to javascript --> | ||
<script type="text/javascript"> | ||
var path_to_root = "../"; | ||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light"; | ||
</script> | ||
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes --> | ||
<script type="text/javascript"> | ||
try { | ||
var theme = localStorage.getItem('wabook-theme'); | ||
var sidebar = localStorage.getItem('wabook-sidebar'); | ||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) { | ||
localStorage.setItem('wabook-theme', theme.slice(1, theme.length - 1)); | ||
} | ||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) { | ||
localStorage.setItem('wabook-sidebar', sidebar.slice(1, sidebar.length - 1)); | ||
} | ||
} catch (e) { } | ||
</script> | ||
|
||
<!-- Set the theme before any content is loaded, prevents flash --> | ||
<script type="text/javascript"> | ||
var theme; | ||
try { theme = localStorage.getItem('wabook-theme'); } catch(e) { } | ||
if (theme === null || theme === undefined) { theme = default_theme; } | ||
var html = document.querySelector('html'); | ||
html.classList.remove('no-js') | ||
html.classList.remove('light') | ||
html.classList.add(theme); | ||
html.classList.add('js'); | ||
</script> | ||
|
||
<!-- Hide / unhide sidebar before it is displayed --> | ||
<script type="text/javascript"> | ||
var html = document.querySelector('html'); | ||
var sidebar = 'hidden'; | ||
if (document.body.clientWidth >= 1080) { | ||
try { sidebar = localStorage.getItem('wabook-sidebar'); } catch(e) { } | ||
sidebar = sidebar || 'visible'; | ||
} | ||
html.classList.remove('sidebar-visible'); | ||
html.classList.add("sidebar-" + sidebar); | ||
</script> | ||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents"> | ||
<div class="sidebar-scrollbox"> | ||
<ol class="chapter"> | ||
<li class="chapter-item expanded "> | ||
<a href="../index.html" >µGo语言实现</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../preface.html" >前言</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch1-basic/readme.html" ><strong aria-hidden="true">1.</strong> 基础</a> | ||
</li> | ||
<ol class="section"> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch1-basic/ch1-01-ugo.html" ><strong aria-hidden="true">1.1.</strong> µGo简介</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch1-basic/ch1-02-mini-compiler.html" ><strong aria-hidden="true">1.2.</strong> 最小编译器</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch1-basic/ch1-03-llvm-ir.html" ><strong aria-hidden="true">1.3.</strong> LLVM汇编简介</a> | ||
</li> | ||
</ol> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch2-expr/readme.html" ><strong aria-hidden="true">2.</strong> 表达式</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch3-hello-ugo/readme.html" ><strong aria-hidden="true">3.</strong> 最小µGo程序</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch4-block-and-var/readme.html" ><strong aria-hidden="true">4.</strong> 变量和作用域</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch5-if-for/readme.html" ><strong aria-hidden="true">5.</strong> if分支和for循环</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch6-func/readme.html" ><strong aria-hidden="true">6.</strong> 函数和递归</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../ch7-pkgs-files/readme.html" ><strong aria-hidden="true">7.</strong> 多文件和多包支持</a> | ||
</li> | ||
<li class="chapter-item expanded "> | ||
<a href="../appendix/readme.html" class="active"><strong aria-hidden="true">8.</strong> 附录</a> | ||
</li> | ||
</ol> | ||
|
||
</div> | ||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div> | ||
</nav> | ||
|
||
<div id="page-wrapper" class="page-wrapper"> | ||
|
||
<div class="page"> | ||
<div id="menu-bar-hover-placeholder"></div> | ||
<div id="menu-bar" class="menu-bar sticky bordered"> | ||
<div class="left-buttons"> | ||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> | ||
<i class="fa fa-bars"></i> | ||
</button> | ||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> | ||
<i class="fa fa-paint-brush"></i> | ||
</button> | ||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> | ||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li> | ||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> | ||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> | ||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> | ||
</ul> | ||
</div> | ||
|
||
<h1 class="menu-title"><a href="../index.html">µGo语言实现</a></h1> | ||
|
||
<div class="right-buttons"> | ||
<a href="https://github.com/wa-lang/ugo-compiler-book" title="Git repository" aria-label="Git repository"> | ||
<i id="git-repository-button" class="fa fa-github"></i> | ||
</a> | ||
<a href="https://github.com/wa-lang/ugo-compiler-book/edit/master/appendix/readme.md" title="Suggest an edit" aria-label="Suggest an edit"> | ||
<i id="git-edit-button" class="fa fa-edit"></i> | ||
</a> | ||
</div> | ||
</div> | ||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> | ||
<script type="text/javascript"> | ||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); | ||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); | ||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { | ||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); | ||
}); | ||
</script> | ||
|
||
<div id="content" class="content"> | ||
<!-- Page table of contents --> | ||
<div class="sidetoc"><nav class="pagetoc"></nav></div> | ||
|
||
<main> | ||
<ul dir="auto"><li><em>凹语言(Go实现, 面向WASM设计): <a href="https://github.com/wa-lang/wa">https://github.com/wa-lang/wa</a></em></li><li><em>WaBook(Go语言实现的MD电子书构建工具): <a href="https://github.com/wa-lang/wabook">https://github.com/wa-lang/wabook</a></em></li></ul><hr> | ||
|
||
<h1>附录</h1> | ||
<h2>LLVM 参考</h2> | ||
<h3>标识符</h3> | ||
<p>LLIR中命名标识符有两种:全局标识符、局部标识符。命名的标识符命名规则是:<code>[@%][a-zA-Z$._][a-zA-Z$._0-9]*</code>,以<code>@</code>开头的是全局标识符,以<code>%</code>开头的是局部标识符。</p> | ||
<h4>全局标识符</h4> | ||
<p>全局标识符主要用于表示全局的变量或常量,还有全局函数的命名。下面是全局标识符的一些例子:</p> | ||
<pre><code class="language-llvm">@count = global i32 21 | ||
@hello = internal constant [6 x i8] c"hello\00" | ||
|
||
declare i32 @puts(i8* nocapture) nounwind | ||
|
||
define void @main() { | ||
ret void | ||
} | ||
</code></pre> | ||
<p>其中<code>@count</code>是一个全局的变量,类型是<code>i32</code>,初始值为21;<code>@hello</code>是一个内部的字符串常量;<code>@puts</code>是外部导入的函数名;<code>@main</code>是新定义的函数名。</p> | ||
<p>因为<code>$.</code>都是合法的标识符名称,可以通过<code>$.</code>来为全局的标识符构造不同层级的名字空间。比如下面的例子:</p> | ||
<pre><code class="language-llvm">@main.count = global i32 21 | ||
@std.math.pi = global double 3.14 | ||
|
||
@main.count.$type = .... | ||
</code></pre> | ||
<p>其中<code>@main.count</code>可以表示为<code>main</code>包下面的<code>count</code>全局变量,而<code>@std.math.pi</code>可以表示为<code>std.math</code>包下面的<code>pi</code>变量。最后的<code>@main.count.$type</code>带有<code>$</code>字符,可以用户表示和<code>count</code>全局变量相关的其它信息。</p> | ||
<p>关于全局变量的其它细节可以暂时忽略,在后文会详细解释。</p> | ||
<h4>局部标识符</h4> | ||
<p>除了全局标识符之外还有局部标识符。局部标识符主要用于表示函数的参数和函数内局部变量(局部变量可以对应未寄存器)。下面是局部标识符的一些例子:</p> | ||
<pre><code class="language-llvm">define i32 @main(i32 %argc, i8** %argv) nounwind { | ||
ret i32 0 | ||
} | ||
</code></pre> | ||
<p>其中<code>%argc</code>和<code>%argv</code>都是以<code>%</code>开头表示局部变量,对应<code>@main</code>函数的两个输出参数。</p> | ||
<p>也可以通过<code>$.</code>来为布局的标识符构造不同层级的名字空间。比如下面的例子:</p> | ||
<pre><code class="language-llvm">define i32 @main() { | ||
%str.addr = getelementptr [6 x i8],[6 x i8]* @"hello\0a", i64 0, i64 0 | ||
ret i32 0 | ||
} | ||
</code></pre> | ||
<p><code>%str.addr</code>可以用于表示字符串中的地址属性。</p> | ||
<h4>未命名标识符</h4> | ||
<p>除了命名的标识符之外,还有未命名的标识符,它们以无符号的数字表示:</p> | ||
<pre><code class="language-llvm">@0 = global i32 100 | ||
@1 = global i32 200 | ||
@2 = global i32 300 | ||
|
||
define void @main() { | ||
%1 = load i32, i32* @0 | ||
ret void | ||
} | ||
</code></pre> | ||
<p>其中<code>@0</code>/<code>@1</code>/<code>@2</code>分别是三个未命名的全局标识符,对应三个<code>i32</code>类型的全局变量。而<code>@main</code>函数内部的<code>%1</code>表示一个局部的未命名标识符,可以理解为一个寄存器。</p> | ||
<p>在LLVM8中,全局的未命名标识符好像必须从<code>@0</code>开始编号,局部的未命名标识符好像必须从<code>%1</code>开始编号,具体原因还有待分析。</p> | ||
<h4>特殊字符</h4> | ||
<p>在标识符还可以通过转义字符的方式表示任意的特殊字符:</p> | ||
<pre><code class="language-llvm">@"hello\0a" = internal constant [6 x i8] c"hello\00" | ||
</code></pre> | ||
<p>如果标识符有转义字符存在则必须用双引号包围起来,比如<code>@"hello\0a"</code>。转义字符串的格式为<code>\xx</code>,其中<code>xx</code>是十六进制表达的ASCII值,比如<code>\0a</code>表示换行符<code>\n</code>。几乎可以使用任意的Unicode字符来命名标识符,只要用UTF8编码后再用转义字符就可以表示了。</p> | ||
|
||
|
||
|
||
|
||
|
||
<div id="giscus-container"></div> | ||
|
||
|
||
|
||
<footer class="page-footer"> | ||
<span>© 2021-2022 | <a href="https://github.com/chai2010">柴树杉</a> 保留所有权利</span> | ||
</footer> | ||
|
||
</main> | ||
|
||
<nav class="nav-wrapper" aria-label="Page navigation"> | ||
<!-- Mobile navigation buttons --> | ||
|
||
<a rel="prev" href="../ch7-pkgs-files/readme.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> | ||
<i class="fa fa-angle-left"></i> | ||
</a> | ||
|
||
|
||
<div style="clear: both"></div> | ||
</nav> | ||
</div> | ||
</div> | ||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation"> | ||
|
||
<a rel="prev" href="../ch7-pkgs-files/readme.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> | ||
<i class="fa fa-angle-left"></i> | ||
</a> | ||
|
||
|
||
</nav> | ||
|
||
</div> | ||
|
||
<script type="text/javascript"> | ||
window.playground_copyable = true; | ||
</script> | ||
<script src="../static/wabook/mark.min.js" type="text/javascript" charset="utf-8"></script> | ||
<script src="../static/wabook/clipboard.min.js" type="text/javascript" charset="utf-8"></script> | ||
<script src="../static/wabook/highlight.js" type="text/javascript" charset="utf-8"></script> | ||
<script src="../static/wabook/book.js" type="text/javascript" charset="utf-8"></script> | ||
|
||
<script type="text/javascript" charset="utf-8"> | ||
var pagePath = "appendix/readme.md" | ||
</script> | ||
|
||
<!-- Custom JS scripts --> | ||
|
||
<script src="../static/wabook/giscus.js" type="text/javascript" charset="utf-8"></script> | ||
|
||
|
||
</body> | ||
</html> |
Oops, something went wrong.