---
eip: 615
label: EIP-615
title: Subroutines and Static Jumps for the EVM
status: Stagnant
type: Standards Track
category: Core
created: 2016-12-10
official: https://eips.ethereum.org/EIPS/eip-615
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-615.md
canonical: https://eips.huatuo.xyz/zh/eip-615/
lang: zh
tldr: 一项用静态跳转、真正的子程序和部署时验证来取代 EVM 动态跳转的提案，让 EVM 代码更易于分析、验证和编译。
tags: [evm, opcodes, subroutines, validation, core]
---

# EIP-615: Subroutines and Static Jumps for the EVM

> In the 21st century, on a blockchain circulating billions of ETH, formal specification and verification are an essential tool against loss. Yet the design of the EVM makes this unnecessarily difficult. Further, the design of the EVM makes near-linear-time compilation to machine code difficult.

**TL;DR:** 一项用静态跳转、真正的子程序和部署时验证来取代 EVM 动态跳转的提案，让 EVM 代码更易于分析、验证和编译。

## 它解决什么问题？

EVM 只有一种跳转：跳到取自**栈**的地址的 `JUMP` / `JUMPI`。编译器甚至把这种动态跳转用于函数返回。由于跳转目标可以是运行时计算出的任意值，工具很难推断出合约的控制流。这使得下面这些工作变得困难或缓慢：

- 形式化验证和用于查找漏洞的静态分析，
- 把 EVM 字节码编译成高速的机器码，
- 提前证明代码不会遇到栈下溢或非法跳转。

作者认为，分析必须在接近线性（`n log n`）的时间内完成，否则攻击者可以构造合约，让验证器或优化器出现二次方级别的性能崩溃。

## 它如何运作

EIP-615 弃用新代码中的 `JUMP` 和 `JUMPI`，并加入与 Wasm 和 CPU 指令高度对应的结构化替代指令：

| 新操作码 | 用途 |
| --- | --- |
| `JUMPTO t` / `JUMPIF t` | 无条件 / 有条件跳转到**立即数**目标 |
| `JUMPV n, targets` | 通过内联目标向量实现的跳转表（switch） |
| `BEGINSUB n_args, n_results` | 标记子程序入口及其栈签名 |
| `JUMPSUB t` / `JUMPSUBV n, targets` | 直接或通过表调用子程序 |
| `RETURNSUB` | 返回到调用之后的指令 |
| `GETLOCAL n` / `PUTLOCAL n` | 读写帧局部的栈槽位 |
| `BEGINDATA` | 此后的内容是数据，而不是代码 |

子程序调用使用程序无法触及的独立返回栈和帧指针。新式字节码以头部（`\0evm` 加版本号）开头，并在**部署时验证**。可能造成栈下溢、跳转到无效位置或误用子程序的代码，会在上链之前就被拒绝。只有 gas 耗尽和栈溢出仍需要运行时检查。新旧代码的混用依赖账户版本控制（[EIP-1702](/eip-1702/)）。

## 示例代码

示意性汇编：一个把两个数相加的子程序，通过静态跳转调用。

```text
; header: "\0evm" + version
BEGINSUB 0, 0          ; main routine
  PUSH1 10
  PUSH1 11
  JUMPSUB add          ; immediate target, no dynamic jump
  ; stack now holds 21
  STOP

add:
BEGINSUB 2, 1          ; takes 2 args, returns 1
  ADD
  RETURNSUB

BEGINDATA              ; optional data section
```

可与旧版 EVM 对比：调用者压入返回地址，被调用者对栈上的任意值执行 `JUMP`。

## 需要注意

- 该提案处于 **Stagnant** 状态，从未被采纳。当前的 EVM 字节码仍然使用动态的 `JUMP` / `JUMPI`。
- 它的想法在后来的提案中得以延续。[EIP-2315](/eip-2315/)（简单子程序）已被 Withdrawn。包括 [EIP-3540](/eip-3540/)、[EIP-4750](/eip-4750/)（函数）以及元提案 [EIP-7692](/eip-7692/) 在内的 EVM 对象格式系列，重新引入了部署时验证和真正的函数，同样被列为 Stagnant。
- 这里的操作码编号和头部格式是该草案特有的。不要假定它们与任何后来的设计一致。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-615
