惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

Chino's Studio

LLVM Backend Practices - Part 1 · Mars Studio How to support debug on GPU 一篇不知道多久前翻译的译文--30年之后,QBasic依然是最棒的编程启蒙语言 · Mars Studio Instruction Selection In LLVM · Mars Studio REPL技术分析——Python的交互式 · Mars Studio 基于Hexo和github page搭建个人博客 · Mars Studio
REPL技术分析——Swift REPL模式 · Mars Studio
Chino Mars · 2020-02-07 · via Chino's Studio

技术路线

在IR层面支持REPL,提供swift解释器,swift提供了编译器runtime,提供基础的词法分析、语法分析、IR生成能力,并可基于llvm ir进行表达式eval,通过JIT的方式在解释器中支持REPL。

源码分析

  1. 头文件:include/swift/Immediate/Immediate.h,包含两个接口

    1
    2
    3
    4
    5
    int RunImmediately(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
    const IRGenOptions &IRGenOpts, const SILOptions &SILOpts,
    std::unique_ptr<SILModule> &&SM);
    void runREPL(CompilerInstance &CI, const ProcessCmdLine &CmdLine,
    bool ParseStdlib);
    • RunImmediately方法用于基于SIL(Swift Intermediate Language),立即Eval当前IR Module,相当于是解释器
    • runREPL方法提供给FrontendTool.cpp前端逻辑进行调用,作为REPL的main loop
  2. 源码文件:Immediate.cpp

实现几个功能:

1
2
3
4
5
6
7
8
9
10
11
12
void *loadSwiftRuntime(ArrayRef<std::string> runtimeLibPaths);
bool tryLoadLibraries(ArrayRef<LinkLibrary> LinkLibraries,
SearchPathOptions SearchPathOpts,
DiagnosticEngine &Diags);
bool linkLLVMModules(llvm::Module *Module,
std::unique_ptr<llvm::Module> SubModule);
bool autolinkImportedModules(ModuleDecl *M, const IRGenOptions &IRGenOpts);
int swift::RunImmediately(CompilerInstance &CI,
const ProcessCmdLine &CmdLine,
const IRGenOptions &IRGenOpts,
const SILOptions &SILOpts,
std::unique_ptr<SILModule> &&SM);
  • 加载swift编译器runtime:swiftCore
  • llvm ir module间合并
  • import module的支持
  • RunImmediately:将input翻译成llvm ir,调用llvm::ExecutionEngine直接执行llvm ir
  1. 源码文件:REPL.cpp

REPL的主函数,从Frontend.cpp进来,主要由一个读取用户input的main loop,对每个输入进行处理。依赖histedit的支持

Reference:

What’s REPL

Source Code Immediate.cpp

Source Code REPL.cpp

histedit.h: Line editor and history interface.