Skip to content

Commit

Permalink
fix 14
Browse files Browse the repository at this point in the history
  • Loading branch information
DawnMagnet committed Jun 26, 2024
1 parent 1949430 commit cb08a85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
32 changes: 16 additions & 16 deletions course14/lec14_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ let program = Program::{

```moonbit
fn Instruction::to_wasm(self : Instruction, buffer : Buffer) -> Unit {
match self {
Add => buffer.write_string("i32.add ")
Local_Get(val) => buffer.write_string("local.get $" + val + " ")
_ => buffer.write_string("...")
}
match self {
Add => buffer.write_string("i32.add ")
Local_Get(val) => buffer.write_string("local.get $" + val + " ")
_ => buffer.write_string("...")
}
}
```

Expand All @@ -297,11 +297,11 @@ let program = Program::{

```moonbit
enum Expression {
Number(Int)
Plus(Expression, Expression)
Minus(Expression, Expression)
Multiply(Expression, Expression)
Divide(Expression, Expression)
Number(Int)
Plus(Expression, Expression)
Minus(Expression, Expression)
Multiply(Expression, Expression)
Divide(Expression, Expression)
}
```

Expand All @@ -311,12 +311,12 @@ let program = Program::{

```moonbit no-check
fn compile_expression(expression : Expression) -> List[Instruction] {
match expression {
Number(i) => List::[Const(I32(i))]
Plus(a, b) => compile_expression(a) + compile_expression(b) + List::[Add]
Minus(a, b) => compile_expression(a) + compile_expression(b) + List::[Sub]
_ => List::[]
}
match expression {
Number(i) => List::[Const(I32(i))]
Plus(a, b) => compile_expression(a) + compile_expression(b) + List::[Add]
Minus(a, b) => compile_expression(a) + compile_expression(b) + List::[Sub]
_ => List::[]
}
}
```

Expand Down
6 changes: 3 additions & 3 deletions course14/lec14_script_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Hello everyone, welcome to the modern programming ideology course brought to you by the Basic Software Center of the IDEA Research Institute.
# Case Study: Stack-based Virtual Machine

Today, we are going to try to implement a simple stack-based virtual machine, taking the backend Wasm used by Moonbit as an example. In the first part, we will introduce what a stack-based virtual machine is and the instructions we are going to implement.

Expand All @@ -8,11 +8,11 @@ Let's extend the topic a bit. Interested students can search and learn. In fact,

In addition to compiling and interpreting execution, another way is to combine the two. A typical example is Java. The Java virtual machine was created to achieve the purpose of writing once and running everywhere, defining an instruction set that is not based on any platform, and then implementing an interpreter on different platforms. So the first step is to compile from the source code to this virtual instruction set, and then the interpreter interprets and executes the instruction set. The interpreter here is also a virtual machine. There are two common types of virtual machines: one is the stack-based virtual machine, where operands are stored on the stack, and data follows the LIFO (Last In First Out) principle. The other is the register-based virtual machine, similar to a normal computer, where operands are stored in registers. The stack-based virtual machine is relatively simple to implement and has a smaller code size, while the register-based virtual machine is closer to the actual computer structure and has higher performance.

Now that we have mentioned WebAssembly, let's give it a brief introduction. WebAssembly, as its name suggests, Web+Assembly, is a virtual instruction set. It was initially used on the web and can be used in browsers, but since it is an instruction set, it can be executed on other platforms as long as a virtual machine is implemented, so there are also runtimes like Wasmtime, WAMR, WasmEdge, etc. It is also the first compilation backend of MoonBit. One of its major features is that its instruction set also has a type system, so there is a great guarantee of security. Today we will take a subset of the WebAssembly instruction set as an example.
Now that we have mentioned WebAssembly, let's give it a brief introduction. WebAssembly, as its name suggests, Web+Assembly, is a virtual instruction set. It was initially used on the web and can be used in browsers, but since it is an instruction set, it can be executed on other platforms as long as a virtual machine is implemented, so there are also runtimes like [Wasmtime](https://github.com/bytecodealliance/wasmtime), (WAMR)[https://github.com/bytecodealliance/wasm-micro-runtime], [WasmEdge](https://wasmedge.org/), etc. It is also the first compilation backend of MoonBit. One of its major features is that its instruction set also has a type system, so there is a great guarantee of security. Today we will take a subset of the WebAssembly instruction set as an example.

The data we consider today is only 32-bit signed integers, that is, Int in Moonbit. To meet the needs of control flow, we use non-zero integers to represent true, and zero to represent false. Our instruction set includes: data operations, data storage, and control flow. On the data side, const can define an integer constant, as well as addition, subtraction, equality judgment, and modulo. Data storage includes fetching and storing operations from local parameters. Control flow includes if-else and function calls.

The definition in Moonbit is basically a one-to-one copy: data is defined using an enumeration type, although we only have 32-bit signed integers; instructions, as just introduced, include the definition of constants, addition, modulo, equality, and function calls, local fetching and setting values, all identified by strings. Finally, there is conditional judgment, which includes an integer and two instruction lists. The integer represents how many calculation results will be left after this expression block ends, and the two instruction lists correspond to the situations when the condition is true and false, respectively.
[The definition of types in Moonbit](https://www.moonbitlang.com/docs/syntax#built-in-data-structures) is basically a one-to-one copy: data is defined using an enumeration type, although we only have 32-bit signed integers; instructions, as just introduced, include the definition of constants, addition, modulo, equality, and function calls, local fetching and setting values, all identified by strings. Finally, there is conditional judgment, which includes an integer and two instruction lists. The integer represents how many calculation results will be left after this expression block ends, and the two instruction lists correspond to the situations when the condition is true and false, respectively.

The function type definition includes the function name, input parameters, output parameters, local variables, and the function body. Here, since we only have one data type, we only record their respective names and quantities, without considering the specific corresponding types. A complete program includes multiple functions and an optional function as the program entry point.

Expand Down

0 comments on commit cb08a85

Please sign in to comment.