Skip to content

Commit

Permalink
[pscm-core] a new simplest implementation based on LLVM
Browse files Browse the repository at this point in the history
this patch implement following features:
1. compile pscm to LLVM IR and run with LLVM JIT
2. support integer only
3. type support
  • Loading branch information
PikachuHy committed Feb 24, 2024
1 parent 566cb02 commit 5106b62
Show file tree
Hide file tree
Showing 27 changed files with 2,164 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/macos_xmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: MacOS (xmake)

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
build_with_xmake_on_macos:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows_xmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Windows (xmake)

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
build_with_xmake_on_windows:
Expand Down
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ cc_library(
[
"src/**/*.cpp",
],
exclude = ["src/codegen/**"],
exclude = ["src/codegen/**", "src/core/**"],
) + select({
":mlir_codegen": glob(["src/codegen/**/*.cpp"]),
":mlir_codegen": glob(["src/codegen/**/*.cpp", "src/core/**/*.cpp", "src/core/**/*.h"]),
"//conditions:default": [],
}) + [
":src/version.cpp",
Expand Down
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ file(GLOB PSCM_SRCS "src/*.cpp")
target_sources(pscm PRIVATE ${PSCM_SRCS})
if (PSCM_ENABLE_MLIR_CODEGEN)
message(STATUS "Enable codegen with MLIR")
target_sources(pscm PUBLIC
src/core/Value.cpp
src/core/Value.h
src/core/Parser.cpp
src/core/Parser.h
src/core/Evaluator.cpp
src/core/Evaluator.h
src/core/SymbolTable.cpp
src/core/SymbolTable.h
src/core/Procedure.cpp
src/core/Procedure.h
src/core/JIT.cpp
src/core/JIT.h
src/core/Scheme.cpp
src/core/Scheme.h
src/core/Mangler.cpp
src/core/Mangler.h
src/core/Runtime.cpp
src/core/Runtime.h
)
target_compile_definitions(pscm PRIVATE PSCM_ENABLE_MLIR_CODEGEN)
file(GLOB CODEGEN_SRCS src/codegen/*.cpp src/codegen/mlir/*.cpp src/codegen/llvm_ir/*.cpp)
target_sources(pscm PRIVATE ${CODEGEN_SRCS})
Expand Down
10 changes: 10 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ function sidebarGuideZh() {
},
]
},
{
text: 'pscm-core',
collapsible: true,
items: [
{
text: 'pscm-core 简介',
link: '/cn/pscm_core'
},
]
},
{
text: 'pscm-build',
collapsible: true,
Expand Down
44 changes: 44 additions & 0 deletions docs/cn/pscm_core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pscm core

pscm core 是重新写的一套PikachuHy's Scheme实现。
从 core 这个名字也可以看出,我希望它足够的小,但有足够的有用。

## Why

之前做的实现,在支持 Continuation 和 Unicode 后,变得比较难演进。
所以我想再写一个更小的实现,不再拘泥 R5RS 标准。
我对它的定位更多是一个前缀表达式的语言。

## 设计目标

- 学习 LLVM
- 将 Scheme 编译到 LLVM IR,然后用 LLVM JIT运行。(也可以考虑直接出一个可执行文件)
- 强类型

::: warning
pscm 依然处于非常简陋的状态
:::

当前的版本

以下特性已实现编译到 LLVM IR,并使用 LLVM JIT运行。

- 基于 LLVM 17 开发
- 支持整型 `integer`,和整型数组 `array<integer>`
- 支持的操作: `map`, `+`, `-`, `>`, `<`
- 支持简单的函数定义和调用。函数在调用时才做类型检查和代码生成。只定义,不调用不会有任何的效果。

## 整体设计

不再使用之前基于 `Cell` 的实现,现在使用大量的指针和类继承。
基本类型是 `Value`,然后会把各种 `Value` 编译到 `AST`
这里相当于前端的语法是前缀表达式,后面的流程和传统的 `C/C++` 差不多。

Codegen 时,把各种 `AST` 生成 LLVM IR,接着就可以使用 LLVM JIT执行LLVM IR。

```
Cell -> Value -> AST -> LLVM IR
```

目前依旧复用了老的pscm的parser,暂时没有重新写一套,所以刚拿到的依旧是 `Cell`

3 changes: 3 additions & 0 deletions include/pscm/common_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#define PSCM_THROW_EXCEPTION(msg) \
PSCM_ERROR("Exception occurred here: {0}", (msg)); \
throw ::pscm::Exception(msg)

#define PSCM_UNIMPLEMENTED() PSCM_THROW_EXCEPTION("Unimplemented")

#define PSCM_ASSERT(e) \
if (!(e)) { \
PSCM_ERROR("ASSERT FAILED here: {0}", #e); \
Expand Down
Loading

0 comments on commit 5106b62

Please sign in to comment.