MCPcopy
hub / github.com/tinygo-org/tinygo / createLookupBoundsCheck

Method createLookupBoundsCheck

compiler/asserts.go:19–35  ·  view source on GitHub ↗

createLookupBoundsCheck emits a bounds check before doing a lookup into a slice. This is required by the Go language spec: an index out of bounds must cause a panic. The caller should make sure that index is at least as big as arrayLen.

(arrayLen, index llvm.Value)

Source from the content-addressed store, hash-verified

17// cause a panic.
18// The caller should make sure that index is at least as big as arrayLen.
19func (b *builder) createLookupBoundsCheck(arrayLen, index llvm.Value) {
20 if b.info.nobounds {
21 // The //go:nobounds pragma was added to the function to avoid bounds
22 // checking.
23 return
24 }
25
26 // Extend arrayLen if it's too small.
27 if index.Type().IntTypeWidth() > arrayLen.Type().IntTypeWidth() {
28 // The index is bigger than the array length type, so extend it.
29 arrayLen = b.CreateZExt(arrayLen, index.Type(), "")
30 }
31
32 // Now do the bounds check: index >= arrayLen
33 outOfBounds := b.CreateICmp(llvm.IntUGE, index, arrayLen, "")
34 b.createRuntimeAssert(outOfBounds, "lookup", "lookupPanic")
35}
36
37// createSliceBoundsCheck emits a bounds check before a slicing operation to make
38// sure it is within bounds.

Callers 1

createExprMethod · 0.95

Calls 2

createRuntimeAssertMethod · 0.95
TypeMethod · 0.45

Tested by

no test coverage detected