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

Method createRuntimeAssert

compiler/asserts.go:233–260  ·  view source on GitHub ↗

createRuntimeAssert is a common function to create a new branch on an assert bool, calling an assert func if the assert value is true (1).

(assert llvm.Value, blockPrefix, assertFunc string)

Source from the content-addressed store, hash-verified

231// createRuntimeAssert is a common function to create a new branch on an assert
232// bool, calling an assert func if the assert value is true (1).
233func (b *builder) createRuntimeAssert(assert llvm.Value, blockPrefix, assertFunc string) {
234 // Check whether we can resolve this check at compile time.
235 if !assert.IsAConstantInt().IsNil() {
236 val := assert.ZExtValue()
237 if val == 0 {
238 // Everything is constant so the check does not have to be emitted
239 // in IR. This avoids emitting some redundant IR.
240 return
241 }
242 }
243
244 // Put the fault block at the end of the function and the next block at the
245 // current insert position.
246 faultBlock := b.ctx.AddBasicBlock(b.llvmFn, blockPrefix+".throw")
247 nextBlock := b.insertBasicBlock(blockPrefix + ".next")
248 b.currentBlockInfo.exit = nextBlock // adjust outgoing block for phi nodes
249
250 // Now branch to the out-of-bounds or the regular block.
251 b.CreateCondBr(assert, faultBlock, nextBlock)
252
253 // Fail: the assert triggered so panic.
254 b.SetInsertPointAtEnd(faultBlock)
255 b.createRuntimeCall(assertFunc, nil, "")
256 b.CreateUnreachable()
257
258 // Ok: assert didn't trigger so continue normally.
259 b.SetInsertPointAtEnd(nextBlock)
260}
261
262// extendInteger extends the value to at least targetType using a zero or sign
263// extend. The resulting value is not truncated: it may still be bigger than

Calls 3

insertBasicBlockMethod · 0.95
createRuntimeCallMethod · 0.95
IsNilMethod · 0.80

Tested by

no test coverage detected