0.3.2 |
|
---|---|
0.3.1 |
|
0.2.1 |
|
0.1.6 |
|
0.1.4 |
|
#120 in #bytecode
每月 27 次下载
用于146 个crate(21个直接使用)
1MB
31K SLoC
id: move-compiler title: Move源语言 custom_edit_url: https://github.com/move-language/move/edit/main/language/move-compiler/README.md
Move源语言
摘要
Move源语言是一种便于编写模块和脚本的编程语言,这些模块和脚本可以编译成Move字节码。
概述
Move源语言是一种基于表达式的语言,旨在简化编写Move程序(模块和脚本)的过程,同时不隐藏Move字节码中的核心概念。
目前,有用于Move的命令行工具。
- Move Check用于检查代码,但不生成字节码
- Move Build用于检查并将代码编译成字节码
未来应该有其他工具用于测试和模拟Move模块。
遗憾的是,没有关于语言语法或特性的文档。请参阅stdlib以获取示例。
设计原则
使命
提供一种简约、表达、安全和透明度高的语言,用于生成并链接Move字节码。
主要原则
-
比字节码更简洁 Move基于表达式,这使得它能够编写简洁和组合的程序,而无需额外的局部变量或结构。Move字节码是基于堆栈的(包含局部变量),因此没有堆栈访问的语言可能需要比字节码更冗长的语法。在Move源语言中,表达式允许在受控和安全的模式下直接在堆栈上编程,从而使得语言在功能上与字节码相同,但提供了更简洁和可读的环境。
-
Move字节码透明度 Move源语言试图将Move字节码中的概念提升到源语言中;它并不试图隐藏它们。字节码已经有一些强烈的观点(比你可能期望在字节码语言中找到的还要强烈),源语言试图保持这种编程模型和思维路线。本原则的目的是消除直接编写字节码的需要。此外,这也意味着与在已发布的模块中声明的函数和类型完全兼容。
-
比字节码更严格 源语言通常会添加额外的限制级别。在表达式层面上,这意味着不能任意操作栈(只能通过表达式进行),并且没有死代码或未使用的效果。在模块层面上,这可能意味着对未使用的类型或不可调用的函数发出额外的警告。在概念/程序层面上,这也意味着添加正式验证的集成。
次要原则
-
学习路径 语法选择和错误信息旨在提供自然的学习流程。例如,围绕表达式语法的某些选择可以改为更符合其他各种语言,但它们可能会损害基于表达式的语法的即插即用感,这可能会损害对Move源语言的深入理解。
-
协助常见社区模式 随着Move被更广泛地使用,模块的常见模式可能会出现。Move可能会添加新的语言特性,使这些模式更容易、更清晰或更安全。但是,如果它们违反了语言的某些其他关键设计目标/原则,则不会添加这些特性。
-
语义保留优化 优化是重要的开发者工具,因为它允许程序员以更自然的方式编写代码。然而,执行的所有优化都必须是语义保留的,以防止在优化代码中发生灾难性的利用或错误。也就是说,Move源语言的主要目标不是产生高度优化的代码,但这是一个很好的特性。
非原则
- 大量抽象 Move源语言并不打算隐藏Move字节码的细节,这包括从引用到全局存储的一切。可能会有一些抽象使得与这些项目交互更容易,但它们应该在Move的最低级别(字节码等效级别)始终可用。这并不意味着源语言当前提供的便利性,如易于字段访问或隐式冻结,违反了核心原则集,但便利性不应在字节码级别上模糊或不透明。但是,请注意,这并不妨碍向语言添加功能,例如将转换为编译器生成的动态检查的访问修饰符。只是语言并不是为了隐藏字节码设计选择而添加大量抽象的主动目标。
命令行选项
有两个可用的程序:Move check和Move build。
- 它们可以使用以下命令构建:
cargo build -p move-compiler
- 或直接运行
cargorun -pmove-compiler --binmove-check -- [参数]
cargorun -pmove-compiler --binmove-build -- [参数]
Move check是一个命令行工具,用于检查Move程序而不会生成字节码
move-check 0.0.1
Check Move source code, without compiling to bytecode.
USAGE:
move-check [OPTIONS] [--] [PATH_TO_SOURCE_FILE]...
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-s, --sender <ADDRESS> The sender address for modules and scripts
-d, --dependency <PATH_TO_DEPENDENCY_FILE>... The library files needed as dependencies
ARGS:
<PATH_TO_SOURCE_FILE>... The source files to check
Move build是一个命令行工具,用于检查Move程序并生成序列化字节码。依赖关系将不会编译。
move-build 0.0.1
Compile Move source to Move bytecode.
USAGE:
move-build [FLAGS] [OPTIONS] [--] [PATH_TO_SOURCE_FILE]...
FLAGS:
-m, --source-map Save bytecode source map to disk
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-s, --sender <ADDRESS> The sender address for modules and scripts
-d, --dependency <PATH_TO_DEPENDENCY_FILE>... The library files needed as dependencies
-o, --out-dir <PATH_TO_OUTPUT_DIRECTORY> The Move bytecode output directory [default: build]
ARGS:
<PATH_TO_SOURCE_FILE>... The source files to check and compile
文件夹结构
move-compiler # Main crate
├── src # Source code for Move lang
│ ├── lib.rs # The entry points into compilation
| |
│ ├── parser # Parsing the source input into an AST
│ │ ├── ast.rs # The target AST for Parsing
│ │ ├── mod.rs # Module for Parsing step
│ │ ├── lexer.rs # The lexer
│ │ └── syntax.rs # The parser
| |
│ ├── expansion # Expands module aliases. Fixes syntax that could not be fully expressed in the grammar (such as assignments and pack)
│ │ ├── ast.rs # The target AST for Expansion
│ │ ├── mod.rs # Module for Expansion step
│ │ └── translate.rs # Parser ~> Expansion
| |
│ ├── naming # Resolves names. This includes names in the current module, generics, locals, and builtin types/functions
│ │ ├── ast.rs # The target AST for Naming
│ │ ├── mod.rs # Module for Naming step
│ │ └── translate.rs # Expansion ~> Naming
| |
│ ├── typing # Type checks the program. The checking is bidirectional in that it infers types while also checking them
│ | ├── ast.rs # The target AST for Typing
│ | ├── mod.rs # Module for Typing step
│ | ├── translate.rs # Naming ~> Typing
│ | ├── core.rs # Core type system code. This includes the typing context and rules for types
│ | ├── expand.rs # After type inference, this expands all of the type variables with the inferred values
│ | └── globals.rs # After expanding type variables, this checks proper access for resources (checks acquires)
| |
│ ├── hlir # The High Level IR. It changes the AST into a statement based representation as opposed to expression based
│ │ ├── ast.rs # The target AST for statement-ification
│ │ ├── mod.rs # Module for High Level IR step
│ │ └── translate.rs # Typing ~> High Level IR
| |
│ ├── cfgir # The Control Flow Graph IR. It removes the structured control flow and puts the blocks into a CFG. There are then control flow sensitive checks performed
│ │ ├── ast.rs # The target AST for the CFG-ification
│ │ ├── mod.rs # Module for CFG IR step
│ │ ├── translate.rs # High Level IR ~> CFG IR
│ │ ├── absint.rs # Abstract Interpretation library for control flow sensitive checks
│ │ ├── cfg.rs # Defines the CFG itself (where the AST just labels the blocks)
│ │ ├── locals # Checks proper local usage (no use after move, no resources left in locals)
│ │ │ ├── mod.rs # The module for the check. Includes the transfer functions
│ │ │ └── state.rs # The state used for abstract interpretation
│ │ └── borrows # The Borrow Checker. Checks the reference safety properties
│ │ ├── borrow_map.rs # The borrow graph used by the abstract state. Maintains internal relationships about what references are borrowing from where
│ │ ├── mod.rs # The module for the check. Includes the transfer functions
│ │ └── state.rs # The state used for abstract interpretation
| |
│ ├── to_bytecode # Compilation to Move bytecode. Is not used by move-check
│ │ ├── mod.rs # Module for the compilation to bytecode
│ │ ├── translate.rs # CFG IR ~> Move bytecode
│ │ ├── context.rs # The context maps between IR construct and bytecode handles/offsets
│ │ ├── remove_fallthrough_jumps.rs # The CFG IR blocks always end in a jump; Move bytecode blocks can fall through. This optimizes the usage of fallthroughs (removing unncessary jumps)
│ │ └── labels_to_offsets.rs # During bytecode generation, the CFG IR labels are used. This switches the labels to bytecode offsets
| |
│ ├── shared # Shared Utilities
│ │ ├── mod.rs # Shared utility code used by all modules (such as source location code)
│ │ └── unique_map.rs # A wrapper around BTreeMap that produces errors on duplicate values
| |
│ ├── errors # Errors produced by the various checks
│ │ └── mod.rs # Module for Errors
| |
│ ├── command_line # Utilities used by both command line binnaries
│ | └── mod.rs # Module for Command LIne
| |
│ └── bin # Command line binaries
│ ├── move-check.rs # Defines the move-check command line tool
│ └── move-build.rs # Defines the move-build command line tool
|
└── stdlib # Move standard library
├── modules # Core modules
└── transaction_scripts # Core transaction scripts
依赖关系
~11-22MB
~293K SLoC