64 lines
1.5 KiB
LLVM
64 lines
1.5 KiB
LLVM
; expect: run
|
|
; result: {"variant":"ok","payload":[4660]}
|
|
; phase: 1
|
|
; spec: docs/spec/16-standard-library.md §2.3; docs/spec/07a-air-instructions.md
|
|
module test.endian_read
|
|
air 0
|
|
lang 0.1
|
|
features [arrays, ownership]
|
|
|
|
type @ReadU16 = enum { ok(u16), short_input }
|
|
|
|
fn @main() -> @ReadU16
|
|
effects []
|
|
entry:
|
|
%storage = local.alloc array<u8, 2>
|
|
local.zero %storage
|
|
%m = borrow.mut %storage
|
|
%dst = slice.make %m
|
|
%zero = const.usize 0
|
|
%one = const.usize 1
|
|
%lo = const.u8 52
|
|
%hi = const.u8 18
|
|
%d0 = borrow.index %dst, %zero
|
|
store %d0, %lo
|
|
borrow.end %d0
|
|
%d1 = borrow.index %dst, %one
|
|
store %d1, %hi
|
|
borrow.end %d1
|
|
borrow.end %dst
|
|
borrow.end %m
|
|
%r = borrow.ref %storage
|
|
%src = slice.make %r
|
|
%result = call @read_u16_le(%src)
|
|
borrow.end %src
|
|
borrow.end %r
|
|
ret %result
|
|
|
|
fn @read_u16_le(%src: slice<u8>) -> @ReadU16
|
|
effects []
|
|
entry:
|
|
%n = slice.len %src
|
|
%two = const.usize 2
|
|
%short = lt.usize %n, %two
|
|
cbr %short, @short_input(), @decode()
|
|
short_input:
|
|
%failure = enum.make @ReadU16.short_input()
|
|
ret %failure
|
|
decode:
|
|
%zero = const.usize 0
|
|
%one = const.usize 1
|
|
%e0 = borrow.index %src, %zero
|
|
%b0 = load %e0
|
|
borrow.end %e0
|
|
%e1 = borrow.index %src, %one
|
|
%b1 = load %e1
|
|
borrow.end %e1
|
|
%lo = cast.checked.u16 %b0
|
|
%hi = cast.checked.u16 %b1
|
|
%eight = const.u16 8
|
|
%shifted = shl.u16 %hi, %eight
|
|
%value = or.u16 %lo, %shifted
|
|
%success = enum.make @ReadU16.ok(%value)
|
|
ret %success
|