skip to main content
10.1145/3464432.3464777acmotherconferencesArticle/Chapter ViewAbstractPublication PagesprogrammingConference Proceedingsconference-collections
research-article

From ASTs to Machine Code with LLVM

Published:21 August 2021Publication History

ABSTRACT

A compiler is a program that translates source code written in a particular language into another language. Internally, the whole process is typically split into multiple stages that handle one particular aspect of this translation. One of these consists of translating the high-level representation of the program, typically an abstract syntax tree, into a simpler form that is suitable for analysis, optimizations, and code generation. This tutorial paper focuses on this process, and uses LLVM to compile programs into optimized machine code. LLVM is a language-agnostic compiler toolchain that handles program optimization and code generation. It is based on its own internal representation, called LLVM IR, which is then transformed into machine code.

We give a brief introduction to LLVM IR, and describe a few patterns to translate high-level language constructs expressed as abstract syntax trees. We implement these patterns in a compiler for a toy programming language, named Cocodol, which supports dynamic typing, unbounded loops, and higher-order functions.

References

  1. Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman, and F. Kenneth Zadeck. 1991. Efficiently Computing Static Single Assignment Form and the Control Dependence Graph. ACM Transactions on Programming Languages and Systems 13, 4 (1991), 451–490. https://doi.org/10.1145/115372.115320Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Changyi Gu. 2016. Power On and Bootloader. Apress, Berkeley, CA, 5–25. https://doi.org/10.1007/978-1-4842-1919-5_2Google ScholarGoogle Scholar
  3. Chris Lattner and Vikram S. Adve. 2004. LLVM: A Compilation Framework for Lifelong Program Analysis & Transformation. In 2nd IEEE / ACM International Symposium on Code Generation and Optimization (CGO 2004), 20-24 March 2004, San Jose, CA, USA. IEEE Computer Society, USA, 75–88. https://doi.org/10.1109/CGO.2004.1281665Google ScholarGoogle Scholar
  4. LLVM Project. 2021. Kaleidoscope: Implementing a Language with LLVM. https://llvm.org/docs/tutorial/. [Online; accessed 07-April-2021].Google ScholarGoogle Scholar
  5. Michael Rodler. 2018. Mapping High Level Constructs to LLVM IR. https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io. [Online; accessed 07-April-2021].Google ScholarGoogle Scholar
  1. From ASTs to Machine Code with LLVM

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in
    • Published in

      cover image ACM Other conferences
      Programming '21: Companion Proceedings of the 5th International Conference on the Art, Science, and Engineering of Programming
      March 2021
      76 pages
      ISBN:9781450389860
      DOI:10.1145/3464432

      Copyright © 2021 ACM

      Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 21 August 2021

      Permissions

      Request permissions about this article.

      Request Permissions

      Check for updates

      Qualifiers

      • research-article
      • Research
      • Refereed limited
    • Article Metrics

      • Downloads (Last 12 months)65
      • Downloads (Last 6 weeks)16

      Other Metrics

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader

    HTML Format

    View this article in HTML Format .

    View HTML Format