Created
April 29, 2020 18:08
-
-
Save andrewrk/2f81e5abc4d1228995f5444edfcdaf6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn entry(start_index: usize, slice: []usize, value: usize) ?usize { | |
var i: usize = start_index; | |
while (i >= 0) : (i -= 1) { | |
if (slice[i] == value) { | |
return i; | |
} | |
} | |
return null; | |
} | |
@v = voidvalue() | |
@usize = primitive(usize) | |
@OptUsize = optionaltype(@usize) | |
@Slice = slicetype(@usize) | |
@0 = int(0) | |
@1 = int(1) | |
@Fn = fntype([@usize, @Slice, @usize], @OptUsize) | |
@2 = fn(@Fn, { | |
%i_ptr = alloca(@usize) | |
%start_index = deref(%0) | |
store(%i_ptr, %start_index) | |
label(break, { | |
loop({ | |
label(continue, { | |
%i = deref(%i_ptr) | |
%cond = cmp(%i, gte, @0) | |
condbr(%cond, { | |
; loop body | |
%eptr = elemptr(%1, %i) | |
%elem = deref(%eptr) | |
%value = deref(%2) | |
%eq = cmp(%elem, eq, %value) | |
label(if, { | |
condbr(%eq, { | |
return(%i) | |
}, { | |
merge(if, @v) | |
}) | |
}) | |
merge(continue, @v) | |
}, { | |
merge(break, @v) | |
}) | |
}) | |
%i = deref(%i_ptr) | |
%new_i = add(%i, @1) | |
store(%i_ptr, %new_i) | |
repeat() | |
}) | |
}) | |
%null = null() | |
return(%null) | |
}) | |
@3 = str("entry") | |
@4 = export(@3, @2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment