sysprog
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    8
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: DYKC, CLANG, C LANGUAGE, compiler, jit, arm --- # [你所不知道的 C 語言](https://hackmd.io/@sysprog/c-prog/):編譯器原理和案例分析 Copyright (**慣C**) 2018 [宅色夫](https://wiki.csie.ncku.edu.tw/User/jserv) ==[直播錄影](https://www.youtube.com/watch?v=Cf9kXtLuQZ8)== ## 如何打造一個具體而微的 C 語言編譯器? * [700 行系列](https://www.slideshare.net/ccckmit/jserv/22) 與隨感 * 整整 20 年前,我在苗栗老家用著緩慢的 Pentium 電腦搭配硬碟,在 Slackware Linux 用舊版 gcc 編譯和安裝 gcc-2.8.0 (1998 年 1 月 14 日釋出) 和 gcc-2.8.1 (同年 3 月釋出)。 * gcc 8.2.0 版於 2018 年 7 月 26 日釋出 * 你可以想像今日是工業編譯器技術標準的 gcc,在整整 20 年前,原始碼打包後僅 8 MB,全部程式碼不到 53 萬行 (C 語言佔其中近 33 萬行),而且測試程式碼才 241 行嗎?對比最新的 gcc-8.2.0,用 tar + gzip 壓縮打包後,後者佔了 109 MB,原始程式碼的規模已達到約 752 萬行 * 20 年的時間,一個專案從「免費又好用」變成「帶領產業變革的標準」,原始程式碼膨漲 14 倍 (過程中移除 [gcj](https://en.wikipedia.org/wiki/GNU_Compiler_for_Java) 一類的子專案,實際上要大得多) * gcc-2.8.0 程式碼中,可見到 Richard Stallman 修改程式碼的蹤跡 (參見 ChangeLog 檔案)。 [AMaCC](https://github.com/jserv/amacc) 是由台灣國立成功大學師生開發的 self-compiling 的 C 語言編譯器,可產生 Arm 架構的執行檔 (ELF 格式,運作於 GNU/Linux)、也支援 [just-in-time (JIT) 編譯和執行](https://en.wikipedia.org/wiki/Just-in-time_compilation),原始程式碼還不到 1500 行,在本次講座中,我們揭開 AMaCC 背後的原理和實作議題。 預期會接觸到 IR (Intermediate representation), dynamic linking, relocation, symbol table, parsing tree, language frontend, Arm 指令編碼和 ABI 等等。 在 COSCUP 2018 的演講 ==[用1500 行建構可自我編譯的 C 編譯器](https://hackmd.io/coscup18-source-c-compiler)== - [ ] AMaCC 產生出來的執行檔 > $ file amacc > amacc: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=25cce9ff5ec975579eed9f890511f1c2bd64610d, with debug_info, not stripped - [ ] 程式碼行數 ```shell $ cloc amacc.c 1 text file. ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C 1 122 216 1473 ------------------------------------------------------------------------------- ``` ## 編譯器和軟體工業強度息息相關 依據 IBM 的簡報指出: [ [source](https://www.facebook.com/alexsjyin/posts/1983929114979146) ] * 1960 年代的 F-4 Phantom 戰鬥機: 僅有 8% 需要軟體控制 (組合語言開發); * 1970 年代的 F-16 Falcon 戰鬥機: 45% 需要軟體控制 (JOVIAL 程式語言開發,類似 ALGOL,主要針對嵌入式系統用於編寫軍用飛機電子系統); * 1980 年代的 F-22 Raptor 戰鬥機: 80% 規範仰賴軟體 (Ada83 程式語言開發),程式碼規模為 170 萬行; * 2000 年之後的 F-35 Lightning 戰鬥機: 2400 萬行程式碼,用 C/C++ 撰寫; 自動駕駛車的軟體規模預計會百倍於飛行控制系統,而開發階段所必須投入發展的軟體驗證環境和方法對開發都至關重大。韓國的 Hyundai Mobis 計畫與 KAIST 合作開發 MAIST,著眼於後續自駕車的軟體性能及成本的影響非常龐大。 [IEEE 1012](https://standards.ieee.org/project/1012.html): 安全相關應用軟體開發 [IEC 61508-3](http://www.iec.ch/functionalsafety/standards/page3.htm): 以每小時危險失效率 (PFH) 或失效危險機率 (PFD) 的形式訂立許多可靠度要求 [形式化驗證](https://hackmd.io/s/H1xxp3pF0) ## 背景知識 * [編譯器和最佳化原理篇](https://hackmd.io/s/Hy72937Me) * [函式呼叫篇](https://hackmd.io/s/SJ6hRj-zg) * [動態連結器和執行時期篇](https://hackmd.io/s/HkK7Uf4Ml) * [虛擬機器設計與實作](https://hackmd.io/s/SkBsZoReb) ## C 程式的解析和語意 * [descent 點評幾本編譯器設計書籍](http://descent-incoming.blogspot.com/2017/01/blog-post.html) (中文為主) * [手把手教你建構 C 語言編譯器](https://github.com/lotabout/write-a-C-interpreter) * [c4](https://github.com/rswier/c4) 是很好的切入點,原作者 Robert Swierczek 還有另一個[更完整的 C 編譯器實作](https://github.com/rswier/swieros/blob/master/root/bin/c.c) * AMaCC 在 Robert Swierczek 的基礎上,額外實作 C 語言的 struct, switch-case, for, C-style comment 支援,並且重寫了 IR 執行程式碼,得以輸出合法 GNU/Linux ELF 執行檔 (支援 [armhf](https://wiki.debian.org/ArmHardFloatPort) ABI) 和 JIT 編譯 * [逐步開發編譯器](http://descent-incoming.blogspot.com/2018/01/44.html) # IR (Intermediate representation) C 語言程式解析後,會轉為 opcode,可透過 [interpreter](https://github.com/jserv/amacc/tree/interpreter) 執行,或者: 1. JIT 編譯程式碼,透過 mmap 系統呼叫和指定 function pointer 去執行 2. 透過 codegen 搭配 ELF 輸出,過程中需要進行 relocation,以便得知執行時期的地址計算 [Interpreter, Compiler, JIT from scratch](https://www.slideshare.net/jserv/jit-compiler) AMaCC 的 `-s` 命令列選項可輸出 IR: ```C qemu-arm -L /usr/arm-linux-gnueabihf ./amacc -s tests/hello.c 1: #include <stdio.h> 2: 3: int main() 4: { 5: printf("hello, world\n"); ENT 0 IMM -161644536 PSH PRTF ADJ 1 6: return 0; IMM 0 LEV 7: } LEV ``` 針對 Arm 架構的 JIT 執行: ```C #include <sys/mman.h> #include <stdlib.h> #include <stdio.h> int main(int ac, char **av) { char *jitmem; int *je, var; jitmem = mmap(0, 256, 7, 0x22, -1, 0); je = (int *)jitmem; *je++ = 0xe59f000c; // ldr r0, [pc, #12] *je++ = 0xe5901000; // ldr r1, [r0] *je++ = 0xe2811009; // add r1, r1, #9 *je++ = 0xe5801000; // str r1, [r0] *je++ = 0xe1a0f00e; // mov pc, lr *je = (int)&var; __clear_cache(jitmem, je); var = ac; bsearch(&av, av, 1, 1, (void*) jitmem); printf("ac = %d, var = %d\n", ac, var); return 0; } ``` 搭配閱讀 [How to JIT - an introduction](https://eli.thegreenplace.net/2013/11/05/how-to-jit-an-introduction) JIT compiler 的效益: [How to write a very simple JIT compiler](https://github.com/spencertipping/jit-tutorial) ## 程式語言設計和編譯器考量 * 影片: [Brian Kernighan on successful language design](https://www.youtube.com/watch?v=Sg4U4r_AgJU)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully