- WebAssembly 教程
- WebAssembly - 首页
- WebAssembly - 概述
- WebAssembly - 简介
- WebAssembly - WASM
- WebAssembly - 安装
- WebAssembly - 编译成WASM的工具
- WebAssembly - 程序结构
- WebAssembly - Javascript
- WebAssembly - Javascript API
- WebAssembly - 在Firefox中调试WASM
- WebAssembly - “Hello World”
- WebAssembly - 模块
- WebAssembly - 验证
- WebAssembly - 文本格式
- WebAssembly - 将WAT转换为WASM
- WebAssembly - 动态链接
- WebAssembly - 安全性
- WebAssembly - 与C语言一起使用
- WebAssembly - 与C++一起使用
- WebAssembly - 与Rust一起使用
- WebAssembly - 与Go一起使用
- WebAssembly - 与Nodejs一起使用
- WebAssembly - 示例
- WebAssembly 有用资源
- WebAssembly - 快速指南
- WebAssembly - 有用资源
- WebAssembly - 讨论
WebAssembly - 程序结构
WebAssembly,也称为WASM,是一种二进制格式的低级代码,旨在以最有效的方式在浏览器中执行。WebAssembly代码具有以下概念:
- 值
- 类型
- 指令
让我们详细了解一下。
值
WebAssembly中的值用于存储复杂数据,例如文本、字符串和向量。WebAssembly支持以下内容:
- 字节
- 整数
- 浮点数
- 名称
字节
字节是WebAssembly支持的最简单的值形式。该值采用十六进制格式。
例如表示为b的字节也可以取自然数n,其中n < 256。
byte ::= 0x00| .... |0xFF
整数
在WebAssembly中,支持的整数如下:
- i32:32位整数
- i64:64位整数
浮点数
在WebAssembly中,支持的浮点数如下:
- f32:32位浮点数
- f64:64位浮点数
名称
名称是字符序列,其标量值由Unicode定义,可在以下链接中找到:http://www.unicode.org/versions/Unicode12.1.0/。
类型
WebAssembly中的实体分类为类型。支持的类型如下:
- 值类型
- 结果类型
- 函数类型
- 限制
- 内存类型
- 表类型
- 全局类型
- 外部类型
让我们逐一学习它们。
值类型
WebAssembly支持的值类型如下:
- i32:32位整数
- i64:64位整数
- f32:32位浮点数
- f64:64位浮点数
valtype ::= i32|i64|f32|f64
结果类型
括号内的值被执行并存储在结果类型中。结果类型是代码块(由值组成)执行的输出。
resulttype::=[valtype?]
函数类型
函数类型将接收参数向量并返回结果向量。
functype::=[vec(valtype)]--> [vec(valtype)]
限制
限制是与内存和表类型相关的存储范围。
limits ::= {min u32, max u32}
内存类型
内存类型处理线性内存及其大小范围。
memtype ::= limits
表类型
表类型由分配给它的元素类型分类。
tabletype ::= limits elemtype elemtype ::= funcref
表类型取决于分配给它的最小和最大大小的限制。
全局类型
全局类型保存具有可以更改或保持不变的值的全局变量。
globaltype ::= mut valtype mut ::= const|var
外部类型
外部类型处理导入和外部值。
externtype ::= func functype | table tabletype | mem memtype | global globaltype
指令
WebAssembly代码是遵循堆栈机器模型的指令序列。由于WebAssembly遵循堆栈机器模型,因此指令被压入堆栈。
例如,函数的参数值从堆栈中弹出,结果被压回堆栈。最后,堆栈中只有一个值,即结果。
一些常用的指令如下:
- 数值指令
- 变量指令
数值指令
数值指令是对数值执行的操作。
例如nn, mm ::= 32|64 ibinop ::= add|sub|mul|div_sx|rem_sx|and|or|xor irelop ::= eq | ne | lt_sx | gt_sx | le_sx | ge_sx frelop ::= eq | ne | lt | gt | le | ge
变量指令
变量指令与访问局部和全局变量有关。
例如
访问局部变量:
get_local $a get_local $b
**设置**局部变量:
set_local $a set_local $b
**访问**全局变量:
get_global $a get_global $b
**设置**全局变量:
set_global $a set_global $b
广告