字节

字节 自己进行编码。

\[\begin{split}\begin{array}{llcll@{\qquad}l} \def\mathdef1966#1{{}}\mathdef1966{byte} & \href{../binary/values.html#binary-byte}{\mathtt{byte}} &::=& \def\mathdef2005#1{\mathtt{0x#1}}\mathdef2005{00} &\Rightarrow& \def\mathdef2006#1{\mathtt{0x#1}}\mathdef2006{00} \\ &&|&& \dots \\ &&|& \def\mathdef2007#1{\mathtt{0x#1}}\mathdef2007{FF} &\Rightarrow& \def\mathdef2008#1{\mathtt{0x#1}}\mathdef2008{FF} \\ \end{array}\end{split}\]

整数

所有 整数 使用 LEB128 可变长度整数编码进行编码,可以是无符号或有符号变体。

无符号整数无符号 LEB128 格式编码。作为附加约束,编码类型为 \(\href{../syntax/values.html#syntax-int}{\mathit{u}N}\) 的值的总字节数不得超过 \(\mathrm{ceil}(N/7)\) 个字节。

\[\begin{split}\begin{array}{llclll@{\qquad}l} \def\mathdef1966#1{{}}\mathdef1966{unsigned integer} & \href{../binary/values.html#binary-int}{\def\mathdef1967#1{{\mathtt{u}#1}}\mathdef1967{N}} &::=& n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^7 \wedge n < 2^N) \\ &&|& n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef2009#1{{\mathtt{u}#1}}\mathdef2009{(N\mathtt{-7})} &\Rightarrow& 2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge N > 7) \\ \end{array}\end{split}\]

有符号整数有符号 LEB128 格式编码,该格式使用二进制补码表示。作为附加约束,编码类型为 \(\href{../syntax/values.html#syntax-int}{\mathit{s}N}\) 的值的总字节数不得超过 \(\mathrm{ceil}(N/7)\) 个字节。

\[\begin{split}\begin{array}{llclll@{\qquad}l} \def\mathdef1966#1{{}}\mathdef1966{signed integer} & \href{../binary/values.html#binary-int}{\def\mathdef1973#1{{\mathtt{s}#1}}\mathdef1973{N}} &::=& n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n & (\mathrel{\mbox{if}} n < 2^6 \wedge n < 2^{N-1}) \\ &&|& n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}} &\Rightarrow& n-2^7 & (\mathrel{\mbox{if}} 2^6 \leq n < 2^7 \wedge n \geq 2^7-2^{N-1}) \\ &&|& n{:}\href{../binary/values.html#binary-byte}{\mathtt{byte}}~~m{:}\def\mathdef2010#1{{\mathtt{s}#1}}\mathdef2010{(N\mathtt{-7})} &\Rightarrow& 2^7\cdot m + (n-2^7) & (\mathrel{\mbox{if}} n \geq 2^7 \wedge N > 7) \\ \end{array}\end{split}\]

未解释整数 编码为有符号整数。

\[\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef1966#1{{}}\mathdef1966{uninterpreted integer} & \href{../binary/values.html#binary-int}{\def\mathdef1978#1{{\mathtt{i}#1}}\mathdef1978{N}} &::=& n{:}\href{../binary/values.html#binary-int}{\def\mathdef1973#1{{\mathtt{s}#1}}\mathdef1973{N}} &\Rightarrow& i & (\mathrel{\mbox{if}} n = \href{../exec/numerics.html#aux-signed}{\mathrm{signed}}_N(i)) \end{array}\]

注意

\(N > 7\) 的边条件在 \(\def\mathdef2011#1{{\mathit{u#1}}}\mathdef2011{}\)\(\def\mathdef2012#1{{\mathit{s#1}}}\mathdef2012{}\) 编码的非终端字节的生成中限制了编码的长度。但是,在这些边界内仍然允许“尾随零”。例如,\(\def\mathdef2013#1{\mathtt{0x#1}}\mathdef2013{03}\)\(\def\mathdef2014#1{\mathtt{0x#1}}\mathdef2014{83}~\def\mathdef2015#1{\mathtt{0x#1}}\mathdef2015{00}\) 都是作为 \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\) 的值 \(3\) 的有效编码。类似地,\(\def\mathdef2016#1{\mathtt{0x#1}}\mathdef2016{7e}\)\(\def\mathdef2017#1{\mathtt{0x#1}}\mathdef2017{FE}~\def\mathdef2018#1{\mathtt{0x#1}}\mathdef2018{7F}\)\(\def\mathdef2019#1{\mathtt{0x#1}}\mathdef2019{FE}~\def\mathdef2020#1{\mathtt{0x#1}}\mathdef2020{FF}~\def\mathdef2021#1{\mathtt{0x#1}}\mathdef2021{7F}\) 都是值 \(-2\) 作为 \(\href{../syntax/values.html#syntax-int}{\mathit{s16}}\) 的有效编码。

终端字节的值 \(n\) 上的边条件进一步强制执行,对于正值,这些字节中任何未使用的位必须为 \(0\),而对于负值必须为 \(1\)。例如,\(\def\mathdef2022#1{\mathtt{0x#1}}\mathdef2022{83}~\def\mathdef2023#1{\mathtt{0x#1}}\mathdef2023{10}\) 作为 \(\href{../syntax/values.html#syntax-int}{\mathit{u8}}\) 编码是格式错误的。类似地,\(\def\mathdef2024#1{\mathtt{0x#1}}\mathdef2024{83}~\def\mathdef2025#1{\mathtt{0x#1}}\mathdef2025{3E}\)\(\def\mathdef2026#1{\mathtt{0x#1}}\mathdef2026{FF}~\def\mathdef2027#1{\mathtt{0x#1}}\mathdef2027{7B}\) 作为 \(\href{../syntax/values.html#syntax-int}{\mathit{s8}}\) 编码都是格式错误的。

浮点数

浮点数 值由其 IEEE 754(第 3.4 节)位模式以 小端 字节序直接编码。

\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef1966#1{{}}\mathdef1966{floating-point value} & \href{../binary/values.html#binary-float}{\def\mathdef1981#1{{\mathtt{f}#1}}\mathdef1981{N}} &::=& b^\ast{:\,}\href{../binary/values.html#binary-byte}{\mathtt{byte}}^{N/8} &\Rightarrow& \href{../exec/numerics.html#aux-bytes}{\mathrm{bytes}}_{\href{../syntax/values.html#syntax-float}{\mathit{f}N}}^{-1}(b^\ast) \\ \end{array}\end{split}\]

名称

名称 编码为一个 字节向量,其中包含名称字符序列的 Unicode(第 3.9 节)UTF-8 编码。

\[\begin{split}\begin{array}{llclllll} \def\mathdef1966#1{{}}\mathdef1966{name} & \href{../binary/values.html#binary-name}{\mathtt{name}} &::=& b^\ast{:}\href{../binary/conventions.html#binary-vec}{\mathtt{vec}}(\href{../binary/values.html#binary-byte}{\mathtt{byte}}) &\Rightarrow& \href{../syntax/values.html#syntax-name}{\mathit{name}} && (\mathrel{\mbox{if}} \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(\href{../syntax/values.html#syntax-name}{\mathit{name}}) = b^\ast) \\ \end{array}\end{split}\]

表示此编码的辅助函数 \(\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}\) 定义如下

\[\begin{split}\begin{array}{@{}l@{}} \begin{array}{@{}lcl@{\qquad}l@{}} \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c^\ast) &=& (\href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c))^\ast \\[1ex] \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b & (\begin{array}[t]{@{}c@{~}l@{}} \mathrel{\mbox{if}} & c < \def\mathdef2028#1{\mathrm{U{+}#1}}\mathdef2028{80} \\ \wedge & c = b) \\ \end{array} \\ \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2 & (\begin{array}[t]{@{}c@{~}l@{}} \mathrel{\mbox{if}} & \def\mathdef2029#1{\mathrm{U{+}#1}}\mathdef2029{80} \leq c < \def\mathdef2030#1{\mathrm{U{+}#1}}\mathdef2030{800} \\ \wedge & c = 2^6(b_1-\def\mathdef2031#1{\mathtt{0x#1}}\mathdef2031{C0})+(b_2-\def\mathdef2032#1{\mathtt{0x#1}}\mathdef2032{80})) \\ \end{array} \\ \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3 & (\begin{array}[t]{@{}c@{~}l@{}} \mathrel{\mbox{if}} & \def\mathdef2033#1{\mathrm{U{+}#1}}\mathdef2033{800} \leq c < \def\mathdef2034#1{\mathrm{U{+}#1}}\mathdef2034{D800} \vee \def\mathdef2035#1{\mathrm{U{+}#1}}\mathdef2035{E000} \leq c < \def\mathdef2036#1{\mathrm{U{+}#1}}\mathdef2036{10000} \\ \wedge & c = 2^{12}(b_1-\def\mathdef2037#1{\mathtt{0x#1}}\mathdef2037{E0})+2^6(b_2-\def\mathdef2038#1{\mathtt{0x#1}}\mathdef2038{80})+(b_3-\def\mathdef2039#1{\mathtt{0x#1}}\mathdef2039{80})) \\ \end{array} \\ \href{../binary/values.html#binary-utf8}{\mathrm{utf8}}(c) &=& b_1~b_2~b_3~b_4 & (\begin{array}[t]{@{}c@{~}l@{}} \mathrel{\mbox{if}} & \def\mathdef2040#1{\mathrm{U{+}#1}}\mathdef2040{10000} \leq c < \def\mathdef2041#1{\mathrm{U{+}#1}}\mathdef2041{110000} \\ \wedge & c = 2^{18}(b_1-\def\mathdef2042#1{\mathtt{0x#1}}\mathdef2042{F0})+2^{12}(b_2-\def\mathdef2043#1{\mathtt{0x#1}}\mathdef2043{80})+2^6(b_3-\def\mathdef2044#1{\mathtt{0x#1}}\mathdef2044{80})+(b_4-\def\mathdef2045#1{\mathtt{0x#1}}\mathdef2045{80})) \\ \end{array} \\ \end{array} \\ \mathrel{\mbox{where}} b_2, b_3, b_4 < \def\mathdef2046#1{\mathtt{0x#1}}\mathdef2046{C0} \\ \end{array}\end{split}\]

注意

与其他一些格式不同,名称字符串没有以 0 结尾。