| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package p |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"runtime" |
| 10 |
) |
| 11 |
|
| 12 |
var y int |
| 13 |
|
| 14 |
//go:noinline |
| 15 |
func Square(x int) { |
| 16 |
var pc0, pc1 [1]uintptr |
| 17 |
runtime.Callers(1, pc0[:]) // get PC at entry |
| 18 |
|
| 19 |
// a switch using jump table |
| 20 |
switch x { |
| 21 |
case 1: |
| 22 |
y = 1 |
| 23 |
case 2: |
| 24 |
y = 4 |
| 25 |
case 3: |
| 26 |
y = 9 |
| 27 |
case 4: |
| 28 |
y = 16 |
| 29 |
case 5: |
| 30 |
y = 25 |
| 31 |
case 6: |
| 32 |
y = 36 |
| 33 |
case 7: |
| 34 |
y = 49 |
| 35 |
case 8: |
| 36 |
y = 64 |
| 37 |
default: |
| 38 |
panic("too large") |
| 39 |
} |
| 40 |
|
| 41 |
// check PC is in the same function |
| 42 |
runtime.Callers(1, pc1[:]) |
| 43 |
if pc1[0] < pc0[0] || pc1[0] > pc0[0]+1000000 { |
| 44 |
fmt.Printf("jump across DSO boundary. pc0=%x, pc1=%x\n", pc0[0], pc1[0]) |
| 45 |
panic("FAIL") |
| 46 |
} |
| 47 |
|
| 48 |
if y != x*x { |
| 49 |
fmt.Printf("x=%d y=%d!=%d\n", x, y, x*x) |
| 50 |
panic("FAIL") |
| 51 |
} |
| 52 |
} |