The Compiler Frontend: Parsing and Type Checking

Anil Madhavapeddy, Yaron M. Minsky · Cambridge University Press eBooks · 2022

Parsing and Type CheckingCompiling source code into executable programs involves a fairly complex set of libraries, linkers, and assemblers.While Dune mostly hides this complexity from you, it's still useful to understand how these pieces work so that you can debug performance problems, or come up with solutions for unusual situations that aren't well handled by existing tools.OCaml has a strong emphasis on static type safety and rejects source code that doesn't meet its requirements as early as possible.The compiler does this by running the source code through a series of checks and transformations.Each stage performs its job (e.g., type checking, optimization, or code generation) and discards some information from the previous stage.The nal native code output is low-level assembly code that doesn't know anything about the OCaml modules or objects that the compiler started with.In this chapter, we'll cover the following topics: • An overview of the compiler codebase and the compilation pipeline, and what each stage represents • Parsing, which goes from raw text to the abstract syntax tree • PPX's, which further transform the AST • Type-checking, including module resolution The details of the remainder of the compilation process, which gets all the way to executable code comes next, in Chapter 27 (The Compiler Backend: Bytecode and Native code). 26.1An Overview of the ToolchainThe OCaml tools accept textual source code as input, using the lename extensions .mland .mlifor modules and signatures, respectively.We explained the basics of the build process in Chapter 5 (Files, Modules, and Programs), so we'll assume you've built a few OCaml programs already by this point.Each source le represents a compilation unit that is built separately.The compiler generates intermediate les with dierent lename extensions to use as it advances through the compilation stages.The linker takes a collection of compiled units and

Read the paper · More papers on PaperTik