编译器与解释器

代码都是需要CPU去执行的,但是CPU只认识机器码。而机器码对人类来说是非常不友好的,为了开发效率程序猿通常使用高级语言进行编程。所以需要借助工具将高级语言转换成机器可以识别和执行的机器码。这类工具就是编译器和解释器。

Thus, both compilers and interpreters generally turn source code (text files) into tokens, both may (or may not) generate a parse tree, and both may generate immediate instructions (for a stack machine, quadruple code, or by other means). The basic difference is that a compiler system, including a (built in or separate) linker, generates a stand-alone machine code program, while an interpreter system instead performs the actions described by the high level program.

编译器的时间开销主要在程序运行前,解释器时间开销在程序运行时。 编译型程序通常可移植性差 解释性语言通常需要对中间码进行加密和混淆 解释下语言编写的程序需要目标设备上带有解释器

Access to variables is also slower in an interpreter because the mapping of identifiers to storage locations must be done repeatedly at run-time

编译器

将高级语言代码转换为低级语言代码(汇编、目标码、机器码)

Compiler design

类别:

  • cross compiler(跨平台编译,编译后的程序可以运行在多个平台)
  • bootstrap compiler(启动编译器,自己编译自己)
  • decompiler(反向编译,把低级语言代码转换成高级语言代码)
  • source-to-source compiler(高级语言源代码之间互相转换)
  • compiler compiler(生成语法解析器,如yaac)

主要功能:

  • preprocessing
  • lexical analysis
  • parsing
  • semantic analysis
  • intermediate code conversion
  • code optimization
  • code generation

工作阶段

  • one-pass compiler(早期,受限于计算机硬件水平)
  • multi-pass compiler
    • front end,verifies syntax and semantics(Clang)
      • line reconstruction(tokenize?)
      • lexical analysis // lexing
      • syntax analysis // parsing -> CST(concret syntax tree) -> AST(abstract syntax tree)
      • semantic analysis
    • middle end,performs optimizations on the IR(intermediate representation)
      • analysis: data-flow, use-definition, dependence, alias, pointer, escape, etc.
      • optimize: inline expansion, dead code elimination, constant propagation, loop transformation, automatic parallelization
    • back end,may perform more analysis,generates the target-dependent assembly code, performing register allocation in the process.performs instruction scheduling(LLVM)
      • machine dependent optimizations
      • code generation

抽象语法树(AST,abstract syntax tree)

机器码(machine code)

platform-dependent execute by cpu directly

字节码(byte code)

platform-independent execute by running environment(such as JVM), compiles to machine code when running

目标码(Object code)

微码(Micro code)

Just in time compilation

解释器

无需提前编译程序运行时直接执行源代码或者程序运行时将源代码转换成某种中间代码执行

AOT(Ahead of time)

即时编译,JIT(just in time compilation)

Compiler

Interpreter

Lexical analysis

Abstract syntax tree

https://cs.stackexchange.com/questions/45486/how-can-a-language-whose-compiler-is-written-in-c-ever-be-faster-than-c

https://towardsdatascience.com/understanding-compilers-for-humans-version-2-157f0edb02dd