std: Share byte parsing and formatting helpers

16903da60b004d5465ec1d75ff55f862b9f39b871834615e021551e15a199c52
Provide ASCII case conversion, checked unsigned radix parsing and
fixed-width hexadecimal integer formatting. Share delimiter splitting,
reverse search, byte-set trimming and ASCII case-insensitive equality.

Use the helpers in module loading, the snapshot runner, protocol and
tokenizer fixtures, assembler tests and kernel diagnostics. Keep each
parser's digit rules and accepted whitespace.

Record standalone verifier verdicts for the std-dependent fixtures.

Assisted-by: Codex:gpt-6
Change-Id: I5ea9dc2d5bc07c5fd9edde2d5b70ed7fd1fb417e
Alexis Sellier committed ago 1 parent 5ad4470f
lib/std/arch/rv64/asm/tests.rad +10 -8
1 1
//! RV64 assembler tests.
2 2
3 -
use std::testing;
4 -
use std::collections::dict;
5 -
use std::mem;
6 -
use std::mem::heap;
7 -
use std::lang::sexpr;
8 -
use std::lang::strings;
9 3
use std::arch::rv64;
10 4
use std::arch::rv64::encode;
11 5
use std::arch::rv64::printer;
6 +
use std::collections::dict;
7 +
use std::fmt;
8 +
use std::lang::sexpr;
9 +
use std::lang::strings;
10 +
use std::mem;
11 +
use std::mem::heap;
12 +
use std::testing;
12 13
13 14
use super::scanner;
14 15
15 16
unsafe static ASM_STRING_TABLE: [*[u8]; strings::TABLE_SIZE] = undefined;
16 17
unsafe static ASM_STRING_POOL: strings::Pool = undefined;
35 36
    for i in 0 .. count {
36 37
        let offset = i * declaration.len;
37 38
        for byte, j in declaration {
38 39
            set SOURCE[offset + j] = byte;
39 40
        }
40 -
        set SOURCE[offset + 11] = '0' + (i / 10) as u8;
41 -
        set SOURCE[offset + 12] = '0' + (i % 10) as u8;
41 +
        let mut buffer: [u8; fmt::U32_STR_LEN] = [0; fmt::U32_STR_LEN];
42 +
        let digits = fmt::formatU32(i, &mut buffer[..]);
43 +
        try! mem::copy(&mut SOURCE[offset + 13 - digits.len .. offset + 13], digits);
42 44
    }
43 45
    let program = try assembleSource(&SOURCE[.. count * declaration.len]);
44 46
    assert program.text.len == 0;
45 47
}
46 48
lib/std/char.rad +15 -0
28 28
export fn isAlpha(ch: u8) -> bool {
29 29
    return (ch >= 'a' and ch <= 'z')
30 30
        or (ch >= 'A' and ch <= 'Z');
31 31
}
32 32
33 +
/// Return `true` when `ch` is an ASCII letter or digit.
34 +
export fn isAlphanumeric(ch: u8) -> bool {
35 +
    return isAlpha(ch) or isDigit(ch);
36 +
}
37 +
38 +
/// Convert ASCII lowercase letters to uppercase. Leave other bytes unchanged.
39 +
export fn toUpper(ch: u8) -> u8 {
40 +
    return ch - 32 if ch >= 'a' and ch <= 'z' else ch;
41 +
}
42 +
43 +
/// Convert ASCII uppercase letters to lowercase. Leave other bytes unchanged.
44 +
export fn toLower(ch: u8) -> u8 {
45 +
    return ch + 32 if ch >= 'A' and ch <= 'Z' else ch;
46 +
}
47 +
33 48
/// Return `true` when `ch` is printable ASCII.
34 49
export fn isPrint(ch: u8) -> bool {
35 50
    return ch >= ' ' and ch <= '~';
36 51
}
lib/std/char/tests.rad +12 -0
38 38
    try testing::expect(super::isPrint(' '));
39 39
    try testing::expect(super::isPrint('~'));
40 40
    try testing::expectNot(super::isPrint(31));
41 41
    try testing::expectNot(super::isPrint(127));
42 42
}
43 +
44 +
/// ASCII case conversion leaves digits, punctuation and high bytes unchanged.
45 +
@test fn asciiCase() throws (testing::TestError) {
46 +
    for i in 0 .. 256 {
47 +
        let byte = i as u8;
48 +
        let upper = byte - 32 if byte >= 'a' and byte <= 'z' else byte;
49 +
        let lower = byte + 32 if byte >= 'A' and byte <= 'Z' else byte;
50 +
        assert super::toUpper(byte) == upper;
51 +
        assert super::toLower(byte) == lower;
52 +
        assert super::isAlphanumeric(byte) == (super::isAlpha(byte) or super::isDigit(byte));
53 +
    }
54 +
}
lib/std/fmt.rad +45 -18
1 1
//! Formatting utilities for converting values to strings.
2 -
use super::lang::alloc;
2 +
use std::bits;
3 3
use std::mem::heap;
4 +
use super::lang::alloc;
4 5
use super::mem;
5 6
6 7
/// Largest radix supported by ASCII digit conversion.
7 8
constant MAX_RADIX: u32 = 36;
8 9
/// Mask that clears the ASCII lowercase letter bit.
49 50
    magnitude: u64,
50 51
    /// Radix used by the literal.
51 52
    radix: Radix,
52 53
}
53 54
55 +
/// Write lowercase hexadecimal byte pairs.
56 +
/// The buffer must hold two bytes per input byte.
57 +
export fn formatHex(bytes: &[u8], buffer: &'r mut [u8]) -> &'r [u8] {
58 +
    assert bytes.len <= buffer.len / 2;
59 +
    let digits = "0123456789abcdef";
60 +
    for byte, i in bytes {
61 +
        set buffer[i * 2] = digits[(byte >> 4) as u32];
62 +
        set buffer[i * 2 + 1] = digits[(byte & 15) as u32];
63 +
    }
64 +
    return &buffer[.. bytes.len * 2];
65 +
}
66 +
67 +
/// Write an unsigned 64-bit integer as 16 lowercase hexadecimal digits.
68 +
/// The buffer must hold at least 16 bytes.
69 +
export fn formatHexU64(value: u64, buffer: &'r mut [u8]) -> &'r [u8] {
70 +
    let mut bytes: [u8; 8] = [0; 8];
71 +
    mem::store64Le(&mut bytes[..], 0, bits::byteSwap64(value));
72 +
    return formatHex(&bytes[..], buffer);
73 +
}
74 +
54 75
/// Write a u32 at the end of the buffer and return the written bytes.
55 76
export fn formatU32(val: u32, buffer: &'r mut [u8]) -> &'r [u8] {
56 77
    assert buffer.len >= U32_STR_LEN;
57 78
58 79
    let mut x: u32 = val;
212 233
        case '0'  => return 0,
213 234
        else      => return ch,
214 235
    }
215 236
}
216 237
238 +
/// Parse unsigned digits in radix 2 through 36, with either letter case.
239 +
/// Require at least one digit. Signs, separators and whitespace are not permitted.
240 +
export fn parseUint(text: &[u8], radix: u32) -> u64 throws (ParseError) {
241 +
    if text.len == 0 or radix < 2 or radix > MAX_RADIX {
242 +
        throw ParseError::Invalid;
243 +
    }
244 +
    let mut value: u64 = 0;
245 +
    let radix64 = radix as u64;
246 +
    for ch in text {
247 +
        let digit = digitFromAscii(ch, radix) else throw ParseError::InvalidDigit;
248 +
        if value > U64_MAX / radix64 {
249 +
            throw ParseError::Overflow;
250 +
        }
251 +
        set value *= radix64;
252 +
        if value > U64_MAX - digit as u64 {
253 +
            throw ParseError::Overflow;
254 +
        }
255 +
        set value += digit as u64;
256 +
    }
257 +
    return value;
258 +
}
259 +
217 260
/// Parse an unsigned integer literal (binary, decimal, or hexadecimal).
218 261
export fn parseInt(text: *[u8]) -> IntLiteral throws (ParseError) {
219 262
    if text.len == 0 {
220 263
        throw ParseError::Invalid;
221 264
    }
235 278
        }
236 279
        if start >= text.len {
237 280
            throw ParseError::Invalid;
238 281
        }
239 282
    }
240 -
    let mut value: u64 = 0;
241 -
    let radix64: u64 = radix as u64;
242 -
    for i in start .. text.len {
243 -
        let ch = text[i];
244 -
        let digit = digitFromAscii(ch, radix) else {
245 -
            throw ParseError::InvalidDigit;
246 -
        };
247 -
        if value > U64_MAX / radix64 {
248 -
            throw ParseError::Overflow;
249 -
        }
250 -
        set value *= radix64;
251 -
252 -
        if value > U64_MAX - digit as u64 {
253 -
            throw ParseError::Overflow;
254 -
        }
255 -
        set value += digit as u64;
256 -
    }
283 +
    let value = try parseUint(&text[start ..], radix);
257 284
    return IntLiteral { text, magnitude: value, radix: radixType };
258 285
}
259 286
260 287
/// Process escape sequences in a raw string, writing the result into `dst`.
261 288
/// Return the written bytes. The destination must have space for the result.
lib/std/lang/module.rad +4 -4
6 6
7 7
export mod printer;
8 8
9 9
@test mod tests;
10 10
11 -
use std::mem;
11 +
use std::char;
12 12
use std::lang::ast;
13 13
use std::lang::strings;
14 +
use std::mem;
14 15
15 16
/// Maximum number of modules tracked in a single compilation graph.
16 17
export constant MAX_MODULES: u32 = 256;
17 18
/// Maximum number of characters for a module path.
18 19
constant MAX_PATH_LEN: u32 = 256;
297 298
    }
298 299
    let mut start: u32 = 0;
299 300
    for end in 0 .. logical.len + 1 {
300 301
        if end < logical.len and logical[end] <> '/' {
301 302
            let byte = logical[end];
302 -
            let letter = (byte >= 'a' and byte <= 'z')
303 -
                or (byte >= 'A' and byte <= 'Z')
303 +
            let letter = char::isAlpha(byte)
304 304
                or byte == '_';
305 -
            let digit = byte >= '0' and byte <= '9';
305 +
            let digit = char::isDigit(byte);
306 306
            if not letter and not (digit and end > start) {
307 307
                throw ModuleError::InvalidPath;
308 308
            }
309 309
            continue;
310 310
        }
lib/std/mem.rad +71 -0
76 76
        return nil;
77 77
    }
78 78
    return &input[.. input.len - suffix.len];
79 79
}
80 80
81 +
/// Return `input` without leading bytes that occur in `matches`.
82 +
export fn trimStartMatches(input: &'r [u8], matches: &[u8]) -> &'r [u8] {
83 +
    let mut start: u32 = 0;
84 +
    while start < input.len and find(matches, &input[start .. start + 1]) <> nil {
85 +
        set start += 1;
86 +
    }
87 +
    return &input[start ..];
88 +
}
89 +
90 +
/// Return `input` without trailing bytes that occur in `matches`.
91 +
export fn trimEndMatches(input: &'r [u8], matches: &[u8]) -> &'r [u8] {
92 +
    let mut end = input.len;
93 +
    while end > 0 and find(matches, &input[end - 1 .. end]) <> nil {
94 +
        set end -= 1;
95 +
    }
96 +
    return &input[.. end];
97 +
}
98 +
99 +
/// Return `input` without leading or trailing bytes that occur in `matches`.
100 +
export fn trimMatches(input: &'r [u8], matches: &[u8]) -> &'r [u8] {
101 +
    return trimEndMatches(trimStartMatches(input, matches), matches);
102 +
}
103 +
81 104
/// Return `input` without leading ASCII whitespace.
82 105
/// Whitespace is space, tab, line feed, form feed, or carriage return.
83 106
export fn trimAsciiStart(input: &'r [u8]) -> &'r [u8] {
84 107
    let mut start: u32 = 0;
85 108
    while start < input.len and char::isWhitespace(input[start]) {
115 138
        }
116 139
    }
117 140
    return nil;
118 141
}
119 142
143 +
/// Return the offset of the last occurrence of `needle` in `text`, or `nil`.
144 +
export fn rfind(text: &[u8], needle: &[u8]) -> ?u32 {
145 +
    if needle.len > text.len {
146 +
        return nil;
147 +
    }
148 +
    let mut start = text.len - needle.len;
149 +
    loop {
150 +
        if eq(&text[start .. start + needle.len], needle) {
151 +
            return start;
152 +
        }
153 +
        if start == 0 {
154 +
            return nil;
155 +
        }
156 +
        set start -= 1;
157 +
    }
158 +
}
159 +
160 +
/// A parsed prefix and the unconsumed input.
161 +
export record SplitOnce['r]: Copy {
162 +
    /// Bytes before the separator.
163 +
    head: &'r [u8],
164 +
    /// Bytes after the separator.
165 +
    tail: &'r [u8],
166 +
}
167 +
168 +
/// Split at the first separator byte, or return `nil` if it is absent.
169 +
export fn splitOnce(input: &'r [u8], separator: u8) -> ?SplitOnce['r] {
170 +
    for byte, i in input {
171 +
        if byte == separator {
172 +
            return SplitOnce { head: &input[.. i], tail: &input[i + 1 ..] };
173 +
        }
174 +
    }
175 +
    return nil;
176 +
}
177 +
178 +
/// Compare ASCII byte strings without case. Other bytes must match exactly.
179 +
export fn eqIgnoreAsciiCase(a: &[u8], b: &[u8]) -> bool {
180 +
    if a.len <> b.len {
181 +
        return false;
182 +
    }
183 +
    for byte, i in a {
184 +
        if char::toLower(byte) <> char::toLower(b[i]) {
185 +
            return false;
186 +
        }
187 +
    }
188 +
    return true;
189 +
}
190 +
120 191
/// Check whether `text` ends with `suffix`.
121 192
export fn endsWith(text: &[u8], suffix: &[u8]) -> bool {
122 193
    return suffix.len <= text.len and eq(&text[text.len - suffix.len ..], suffix);
123 194
}
124 195
lib/std/tests.rad +85 -5
1 1
//! Tests for formatting functions.
2 2
3 +
use std::collections::dict;
3 4
use std::fmt;
5 +
use std::io;
4 6
use std::mem;
5 -
use std::vec;
6 -
use std::testing;
7 -
use std::sys::host;
8 7
use std::mem::heap;
9 -
use std::io;
10 -
use std::collections::dict;
8 +
use std::sys::host;
9 +
use std::testing;
10 +
use std::vec;
11 11
12 12
/// Safe I/O and equality accept empty buffers.
13 13
@test fn testSafeEmptyBuffers() throws (testing::TestError) {
14 14
    io::print("");
15 15
    io::printError("");
657 657
658 658
    try testing::expect(vec::pop(&mut v, &mut popped));
659 659
    try testing::expect(popped.x == 3);
660 660
    try testing::expect(popped.y == 4);
661 661
}
662 +
663 +
/// Radix parsing requires digits and checks the complete unsigned range.
664 +
@test fn parseUnsignedRadix() throws (testing::TestError) {
665 +
    assert try! fmt::parseUint("00031", 10) == 31;
666 +
    assert try! fmt::parseUint("18446744073709551615", 10) == fmt::U64_MAX;
667 +
    assert try! fmt::parseUint("fF", 16) == 255;
668 +
    assert try! fmt::parseUint("101", 2) == 5;
669 +
    assert try! fmt::parseUint("Zz", 36) == 1295;
670 +
    assert try! fmt::parseUint("000", 10) == 0;
671 +
    for input in ["", "+1", "-1", " 1", "1 ", "0x10", "0b10", "1_0", "a"] {
672 +
        let mut failed = false;
673 +
        try fmt::parseUint(input, 10) catch {
674 +
            set failed = true;
675 +
        };
676 +
        assert failed;
677 +
    }
678 +
    for radix in [0, 1, 37] {
679 +
        let mut failed = false;
680 +
        try fmt::parseUint("1", radix) catch error {
681 +
            assert error == fmt::ParseError::Invalid;
682 +
            set failed = true;
683 +
        };
684 +
        assert failed;
685 +
    }
686 +
    for input in ["18446744073709551616", "99999999999999999999"] {
687 +
        let mut failed = false;
688 +
        try fmt::parseUint(input, 10) catch error {
689 +
            assert error == fmt::ParseError::Overflow;
690 +
            set failed = true;
691 +
        };
692 +
        assert failed;
693 +
    }
694 +
    let mut failed = false;
695 +
    try fmt::parseUint("10000000000000000", 16) catch error {
696 +
        assert error == fmt::ParseError::Overflow;
697 +
        set failed = true;
698 +
    };
699 +
    assert failed;
700 +
}
701 +
702 +
/// Fixed-width hexadecimal integers retain leading zeroes and byte order.
703 +
@test fn formatHexInteger() throws (testing::TestError) {
704 +
    let mut buffer: [u8; 20] = [0xa5; 20];
705 +
    assert mem::eq(fmt::formatHexU64(0, &mut buffer[..]), "0000000000000000");
706 +
    assert mem::eq(fmt::formatHexU64(0x0123456789abcdef, &mut buffer[..]), "0123456789abcdef");
707 +
    assert mem::eq(fmt::formatHexU64(fmt::U64_MAX, &mut buffer[..]), "ffffffffffffffff");
708 +
    assert buffer[16] == 0xa5;
709 +
}
710 +
711 +
/// Byte-string helpers preserve delimiters, narrow trim sets and non-ASCII bytes.
712 +
@test fn byteStringHelpers() throws (testing::TestError) {
713 +
    let part = mem::splitOnce("a=b=c", '=') else panic;
714 +
    assert mem::eq(part.head, "a") and mem::eq(part.tail, "b=c");
715 +
    let empty = mem::splitOnce("=", '=') else panic;
716 +
    assert empty.head.len == 0 and empty.tail.len == 0;
717 +
    assert mem::splitOnce("", '=') == nil;
718 +
    assert mem::splitOnce("abc", '=') == nil;
719 +
    assert mem::rfind("a/b/c", "/") == 3;
720 +
    assert mem::rfind("a", "a") == 0;
721 +
    assert mem::rfind("a", "") == 1;
722 +
    assert mem::rfind("", "") == 0;
723 +
    assert mem::rfind("a", "ab") == nil;
724 +
    assert mem::rfind("a", "b") == nil;
725 +
    assert mem::eq(mem::trimMatches(" \tvalue\t ", " \t"), "value");
726 +
    assert mem::eq(mem::trimStartMatches(" \tvalue ", " \t"), "value ");
727 +
    assert mem::eq(mem::trimEndMatches(" value \r", " \r"), " value");
728 +
    assert mem::eq(mem::trimMatches("\r\nvalue\r\n", " \t"), "\r\nvalue\r\n");
729 +
    assert mem::eq(mem::trimMatches(" x ", ""), " x ");
730 +
    assert mem::trimMatches(" \t", " \t").len == 0;
731 +
    assert mem::trimMatches("", " \t").len == 0;
732 +
    assert mem::eqIgnoreAsciiCase("Content-Length", "content-length");
733 +
    assert mem::eqIgnoreAsciiCase("", "");
734 +
    assert not mem::eqIgnoreAsciiCase("a", "aa");
735 +
    assert not mem::eqIgnoreAsciiCase("[", "{");
736 +
    let high: [u8; 2] = [0xc0, 'A'];
737 +
    let same: [u8; 2] = [0xc0, 'a'];
738 +
    let other: [u8; 2] = [0xe0, 'a'];
739 +
    assert mem::eqIgnoreAsciiCase(&high[..], &same[..]);
740 +
    assert not mem::eqIgnoreAsciiCase(&high[..], &other[..]);
741 +
}
test/runner.rad +16 -25
2 2
//!
3 3
//! Given a `.rad` source file, lowers it to IL and compares the output
4 4
//! against the corresponding `.ril` snapshot file. It also supports an
5 5
//! `assemble <input.ras> <output.rv64>` subcommand used by `bin-test`.
6 6
7 -
use std::io;
8 -
use std::mem;
9 -
use std::sys;
7 +
use std::arch::rv64;
8 +
use std::arch::rv64::asm;
9 +
use std::arch::rv64::image;
10 10
use std::fs;
11 -
use std::mem::heap;
11 +
use std::io;
12 12
use std::lang::ast;
13 13
use std::lang::il::printer;
14 +
use std::lang::lower;
14 15
use std::lang::parser;
15 -
use std::lang::scanner;
16 16
use std::lang::resolver;
17 +
use std::lang::scanner;
17 18
use std::lang::strings;
18 -
use std::lang::lower;
19 -
use std::arch::rv64;
20 -
use std::arch::rv64::asm;
21 -
use std::arch::rv64::image;
19 +
use std::mem;
20 +
use std::mem::heap;
21 +
use std::sys;
22 22
23 23
/// Buffer size for reading source files (8 KB).
24 24
constant SOURCE_BUF_SIZE: u32 = 8192;
25 25
/// Buffer size for reading expected IL files (32 KB).
26 26
constant EXPECTED_BUF_SIZE: u32 = 32768;
101 101
            set end = i;
102 102
            break;
103 103
        }
104 104
        set i += 1;
105 105
    }
106 -
    while end > 0 and (line[end - 1] == ' ' or line[end - 1] == '\t') {
107 -
        set end -= 1;
108 -
    }
109 -
    return &line[.. end];
106 +
    return &line[.. mem::trimEndMatches(&line[.. end], " \t").len];
110 107
}
111 108
112 109
/// Get next line from string at offset. Returns the line and updates offset past newline.
113 110
fn nextLine(s: *[u8], offset: &mut u32) -> *[u8] {
114 -
    let start = *offset;
115 -
    let mut i = start;
116 -
117 -
    while i < s.len and s[i] <> '\n' {
118 -
        set i += 1;
119 -
    }
120 -
    let line = &s[start .. i];
121 -
    if i < s.len {
122 -
        set *offset = i + 1;
123 -
    } else {
124 -
        set *offset = i;
111 +
    let input = &s[*offset ..];
112 +
    if let at = mem::find(input, "\n") {
113 +
        set *offset += at + 1;
114 +
        return &input[.. at];
125 115
    }
126 -
    return line;
116 +
    set *offset = s.len;
117 +
    return input;
127 118
}
128 119
129 120
/// Compare two strings ignoring comments, line by line.
130 121
fn stringsEqual(a: *[u8], b: *[u8]) -> bool {
131 122
    let mut ai: u32 = 0;
test/runtime/kernel/dispatchcheck.rad +17 -15
1 1
//! Runtime compiler ownership across scheduled windows and domain destruction.
2 2
export mod inputs;
3 3
export mod cases;
4 -
use kernel::fixture;
5 -
use kernel::state;
4 +
5 +
use std::fmt;
6 6
use kernel::abi;
7 -
use kernel::frames;
8 -
use kernel::slots;
9 -
use kernel::pages;
10 -
use kernel::capability;
11 -
use kernel::registry;
12 -
use kernel::loader;
13 -
use kernel::instances;
14 -
use kernel::domains;
15 7
use kernel::budgets;
16 -
use kernel::dispatch;
17 -
use kernel::sync;
18 8
use kernel::calls;
9 +
use kernel::capability;
10 +
use kernel::dispatch;
11 +
use kernel::domains;
12 +
use kernel::fixture;
13 +
use kernel::frames;
14 +
use kernel::instances;
19 15
use kernel::lifecycle;
16 +
use kernel::loader;
17 +
use kernel::pages;
18 +
use kernel::registry;
20 19
use kernel::remote;
20 +
use kernel::slots;
21 +
use kernel::state;
22 +
use kernel::sync;
21 23
22 24
/// Delay before the first user window, in timer ticks.
23 25
constant START_DELAY: u64 = 1000000;
24 26
/// End of the first interrupted load, relative to the first window start. It
25 27
/// must fall after the loader reserves its output and before its compilation
66 68
unsafe static FINAL: u64 = 0;
67 69
/// Write one diagnostic byte to the fixture UART.
68 70
fn put(byte: u8);
69 71
/// Write a fixed-width hexadecimal diagnostic value.
70 72
fn number(value: u64) {
71 -
    let digits = "0123456789abcdef";
72 -
    for i in 0 .. 16 {
73 -
        put(digits[((value >> ((15 - i) as u64 * 4)) & 15) as u32]);
73 +
    let mut text: [u8; 16] = [0; 16];
74 +
    for byte in fmt::formatHexU64(value, &mut text[..]) {
75 +
        put(byte);
74 76
    }
75 77
}
76 78
77 79
/// Map a validated physical page.
78 80
fn memory(address: u64) -> *mut u8;
test/smp/kernel/dispatchcheck.rad +22 -20
1 1
//! One domain executing separate user contexts on every online hart.
2 2
export mod inputs;
3 3
export mod cases;
4 4
export mod invariants;
5 -
use std::arch::rv64::shared;
5 +
6 6
use std::arch::rv64;
7 -
use kernel::fixture;
8 -
use kernel::limits;
9 -
use kernel::state;
7 +
use std::arch::rv64::shared;
8 +
use std::fmt;
10 9
use kernel::abi;
11 -
use kernel::frames;
12 -
use kernel::slots;
13 -
use kernel::pages;
14 -
use kernel::capability;
15 -
use kernel::registry;
16 -
use kernel::instances;
17 -
use kernel::domains;
18 -
use kernel::budgets;
19 -
use kernel::dispatch;
20 10
use kernel::boot;
21 -
use kernel::sync;
22 -
use kernel::remote;
11 +
use kernel::budgets;
23 12
use kernel::calls;
13 +
use kernel::capability;
14 +
use kernel::dispatch;
15 +
use kernel::domains;
24 16
use kernel::events;
25 -
use kernel::timers;
17 +
use kernel::fixture;
18 +
use kernel::frames;
19 +
use kernel::instances;
26 20
use kernel::interrupts;
21 +
use kernel::lifecycle;
22 +
use kernel::limits;
23 +
use kernel::pages;
27 24
use kernel::plic;
28 25
use kernel::registers;
29 -
use kernel::lifecycle;
26 +
use kernel::registry;
27 +
use kernel::remote;
28 +
use kernel::slots;
29 +
use kernel::state;
30 +
use kernel::sync;
31 +
use kernel::timers;
30 32
31 33
/// Delay before concurrent user windows, in timer ticks.
32 34
constant START_DELAY: u64 = 1000000;
33 35
34 36
/// Base work and migration duration, scaled by the square of hart count.
1090 1092
    return false;
1091 1093
}
1092 1094
1093 1095
/// Print one full-width hexadecimal diagnostic value.
1094 1096
fn number(value: u64) {
1095 -
    let digits = "0123456789abcdef";
1096 -
    for i in 0 .. 16 {
1097 -
        put(digits[((value >> ((15 - i) as u64 * 4)) & 15) as u32]);
1097 +
    let mut text: [u8; 16] = [0; 16];
1098 +
    for byte in fmt::formatHexU64(value, &mut text[..]) {
1099 +
        put(byte);
1098 1100
    }
1099 1101
}
test/tests/prog.tokenizer.rad +11 -17
1 1
//! returns: 0
2 +
//! uses-std
2 3
//! Tokenizer / lexer.
3 4
//! A simplified lexer that tokenizes arithmetic expressions into tokens,
4 5
//! then parses and evaluates them. Exercises: tagged unions with payloads,
5 6
//! match statements, optionals, if-let, while-let, for-in loops, records
6 7
//! with slice fields, and complex control flow.
7 8
9 +
use std::char;
10 +
use std::fmt;
11 +
use std::mem;
12 +
8 13
/// Token types produced by the lexer.
9 14
union Token: Copy {
10 15
    /// Integer literal with its value.
11 16
    Number(i32),
12 17
    /// +
65 70
    tokenCount: u32,
66 71
    pos: u32,
67 72
    pool: *unsafe mut ExprPool,
68 73
}
69 74
70 -
/// Check if a byte is a digit.
71 -
fn isDigit(c: u8) -> bool {
72 -
    return c >= 48 and c <= 57;
73 -
}
74 -
75 -
/// Check if a byte is whitespace.
76 -
fn isSpace(c: u8) -> bool {
77 -
    return c == 32 or c == 9 or c == 10 or c == 13;
78 -
}
79 -
80 75
/// Peek at the current character, returning nil at end.
81 76
unsafe fn peek(lex: *unsafe Lexer) -> ?u8 {
82 77
    if lex.pos < lex.source.len {
83 78
        return lex.source[lex.pos];
84 79
    }
92 87
    }
93 88
}
94 89
95 90
/// Skip whitespace characters.
96 91
unsafe fn skipWhitespace(lex: *unsafe mut Lexer) {
97 -
    while let ch = peek(lex); isSpace(ch) {
98 -
        advance(lex);
99 -
    }
92 +
    let remaining = mem::trimStartMatches(&lex.source[lex.pos ..], " \t\n\r").len;
93 +
    set lex.pos = lex.source.len - remaining;
100 94
}
101 95
102 96
/// Scan a number literal.
103 97
unsafe fn scanNumber(lex: *unsafe mut Lexer) -> i32 {
104 -
    let mut value: i32 = 0;
105 -
    while let ch = peek(lex); isDigit(ch) {
106 -
        set value = value * 10 + (ch - 48) as i32;
98 +
    let start = lex.pos;
99 +
    while let ch = peek(lex); char::isDigit(ch) {
107 100
        advance(lex);
108 101
    }
102 +
    let value = try! fmt::parseUint(&lex.source[start .. lex.pos], 10) as i32;
109 103
    return value;
110 104
}
111 105
112 106
/// Get the next token from the lexer.
113 107
unsafe fn nextToken(lex: *unsafe mut Lexer) -> Token {
114 108
    skipWhitespace(lex);
115 109
116 110
    if let ch = peek(lex) {
117 -
        if isDigit(ch) {
111 +
        if char::isDigit(ch) {
118 112
            return Token::Number(scanNumber(lex));
119 113
        }
120 114
        advance(lex);
121 115
        match ch {
122 116
            case 43 => {
test/tests/region.http.rad +25 -95
1 1
//! returns: 0
2 +
//! uses-std
2 3
//! Zero-copy parsing of pipelined requests with bounded headers and body lengths.
3 4
5 +
use std::fmt;
6 +
use std::mem;
7 +
4 8
/// Failure to parse the request subset used by this fixture.
5 9
union ParseError {
6 10
    /// Input ends before a complete line or body.
7 11
    Truncated,
8 12
    /// A line has an invalid format.
11 15
    TooMany,
12 16
    /// A body length is invalid or exceeds the fixture limit.
13 17
    Length,
14 18
}
15 19
16 -
/// A parsed prefix and the unconsumed input.
17 -
record Split['in]: Copy {
18 -
    /// Parsed bytes.
19 -
    head: &'in [u8],
20 -
    /// Remaining bytes.
21 -
    tail: &'in [u8],
22 -
}
23 -
24 20
/// A header whose fields borrow the request bytes.
25 21
record Header['in]: Copy {
26 22
    /// Header name.
27 23
    name: &'in [u8],
28 24
    /// Trimmed header value.
57 53
    input: &'in [u8],
58 54
    /// Offset of the next line.
59 55
    at: u32,
60 56
}
61 57
62 -
/// Compare two byte sequences.
63 -
fn same(a: &[u8], b: &[u8]) -> bool {
64 -
    if a.len <> b.len {
65 -
        return false;
66 -
    }
67 -
    for byte, i in a {
68 -
        if byte <> b[i] {
69 -
            return false;
70 -
        }
71 -
    }
72 -
    return true;
73 -
}
74 -
75 -
/// Fold an ASCII letter for a header-name comparison.
76 -
fn lower(byte: u8) -> u8 {
77 -
    if byte >= 'A' and byte <= 'Z' {
78 -
        return byte + 32;
79 -
    }
80 -
    return byte;
81 -
}
82 -
83 -
/// Compare ASCII header names without case.
84 -
fn headerName(a: &[u8], b: &[u8]) -> bool {
85 -
    if a.len <> b.len {
86 -
        return false;
87 -
    }
88 -
    for byte, i in a {
89 -
        if lower(byte) <> lower(b[i]) {
90 -
            return false;
91 -
        }
92 -
    }
93 -
    return true;
94 -
}
95 -
96 -
/// Split at a required delimiter, keeping both parts in the input region.
97 -
fn split(input: &'in [u8], delimiter: u8) -> Split['in] throws (ParseError) {
98 -
    for byte, i in input {
99 -
        if byte == delimiter {
100 -
            return Split { head: &input[.. i], tail: &input[i + 1 ..] };
101 -
        }
102 -
    }
103 -
    throw ParseError::Truncated;
104 -
}
105 -
106 58
/// Consume one CRLF-terminated line.
107 -
fn line(input: &'in [u8]) -> Split['in] throws (ParseError) {
108 -
    let part = try split(input, '\n');
109 -
    if part.head.len == 0 or part.head[part.head.len - 1] <> '\r' {
110 -
        throw ParseError::Malformed;
111 -
    }
112 -
    return Split { head: &part.head[.. part.head.len - 1], tail: part.tail };
59 +
fn line(input: &'in [u8]) -> mem::SplitOnce['in] throws (ParseError) {
60 +
    let part = mem::splitOnce(input, '\n') else throw ParseError::Truncated;
61 +
    let head = mem::stripSuffix("\r", part.head) else throw ParseError::Malformed;
62 +
    return mem::SplitOnce { head, tail: part.tail };
113 63
}
114 64
115 65
/// Advance to the next line while retaining its input region.
116 66
fn (cursor: &mut Cursor['in]) line() -> &'in [u8] throws (ParseError) {
117 67
    let part = try line(&cursor.input[cursor.at ..]);
118 68
    set cursor.at = cursor.input.len - part.tail.len;
119 69
    return part.head;
120 70
}
121 71
122 -
/// Trim spaces and tabs from a header value.
123 -
fn trim(input: &'in [u8]) -> &'in [u8] {
124 -
    let mut start: u32 = 0;
125 -
    let mut end = input.len;
126 -
    while start < end and (input[start] == ' ' or input[start] == '\t') {
127 -
        set start += 1;
128 -
    }
129 -
    while end > start and (input[end - 1] == ' ' or input[end - 1] == '\t') {
130 -
        set end -= 1;
131 -
    }
132 -
    return &input[start .. end];
133 -
}
134 -
135 72
/// Parse a decimal length of at most 65535 bytes without overflow.
136 73
fn length(input: &[u8]) -> u32 throws (ParseError) {
137 -
    if input.len == 0 {
74 +
    let value = try fmt::parseUint(input, 10) catch {
75 +
        throw ParseError::Length;
76 +
    };
77 +
    if value > 65535 {
138 78
        throw ParseError::Length;
139 79
    }
140 -
    let mut value: u32 = 0;
141 -
    for byte in input {
142 -
        if byte < '0' or byte > '9' {
143 -
            throw ParseError::Length;
144 -
        }
145 -
        let digit = (byte - '0') as u32;
146 -
        if value > (65535 - digit) / 10 {
147 -
            throw ParseError::Length;
148 -
        }
149 -
        set value = value * 10 + digit;
150 -
    }
151 -
    return value;
80 +
    return value as u32;
152 81
}
153 82
154 83
/// Parse one HTTP/1.1 request with at most one Content-Length header.
155 84
fn parse(input: &'in [u8]) -> Parsed['in] throws (ParseError) {
156 85
    let first = try line(input);
157 -
    let method = try split(first.head, ' ');
158 -
    let target = try split(method.tail, ' ');
159 -
    if method.head.len == 0 or target.head.len == 0 or not same(target.tail, "HTTP/1.1") {
86 +
    let method = mem::splitOnce(first.head, ' ') else throw ParseError::Truncated;
87 +
    let target = mem::splitOnce(method.tail, ' ') else throw ParseError::Truncated;
88 +
    if method.head.len == 0 or target.head.len == 0 or not mem::eq(target.tail, "HTTP/1.1") {
160 89
        throw ParseError::Malformed;
161 90
    }
162 91
    let mut headers: [?Header['in]; 4] = [nil; 4];
163 92
    let mut count: u32 = 0;
164 93
    let mut bodyLength: ?u32 = nil;
169 98
            break;
170 99
        }
171 100
        if count == headers.len {
172 101
            throw ParseError::TooMany;
173 102
        }
174 -
        let parts = try split(next, ':');
103 +
        let parts = mem::splitOnce(next, ':') else throw ParseError::Truncated;
175 104
        if parts.head.len == 0 {
176 105
            throw ParseError::Malformed;
177 106
        }
178 -
        let value = trim(parts.tail);
179 -
        if headerName(parts.head, "content-length") {
107 +
        // Trim spaces and tabs from a header value.
108 +
        let value = mem::trimMatches(parts.tail, " \t");
109 +
        if mem::eqIgnoreAsciiCase(parts.head, "content-length") {
180 110
            if bodyLength <> nil {
181 111
                throw ParseError::Length;
182 112
            }
183 113
            set bodyLength = try length(value);
184 114
        }
199 129
}
200 130
201 131
/// Find a header through a slice of optional borrowed records.
202 132
fn header(headers: &[?Header['in]], name: &[u8]) -> ?&'in [u8] {
203 133
    for item in headers {
204 -
        if let field = item; headerName(field.name, name) {
134 +
        if let field = item; mem::eqIgnoreAsciiCase(field.name, name) {
205 135
            return field.value;
206 136
        }
207 137
    }
208 138
    return nil;
209 139
}
225 155
@default fn main() -> i32 {
226 156
    let source: *[u8] =
227 157
        "POST /a HTTP/1.1\r\nHost: example\t\r\nContent-Length: 3\r\n\r\nabcGET /b HTTP/1.1\r\n\r\n";
228 158
    let input: &'in [u8] = &source[..] {
229 159
        let first = try! parse(input);
230 -
        assert same(first.request.method, "POST") and same(first.request.target, "/a");
231 -
        assert same(first.request.body, "abc") and first.request.count == 2;
160 +
        assert mem::eq(first.request.method, "POST") and mem::eq(first.request.target, "/a");
161 +
        assert mem::eq(first.request.body, "abc") and first.request.count == 2;
232 162
        let name = host(first) else panic "main: missing host";
233 -
        assert same(name, "example");
163 +
        assert mem::eq(name, "example");
234 164
        let second = try! parse(first.rest);
235 -
        assert same(second.request.method, "GET") and same(second.request.target, "/b");
165 +
        assert mem::eq(second.request.method, "GET") and mem::eq(second.request.target, "/b");
236 166
        assert second.request.body.len == 0 and second.request.count == 0;
237 167
        assert second.rest.len == 0;
238 168
        assert header(&second.request.headers[..], "host") == nil;
239 169
    }
240 170
    assert rejected("");
test/tests/region.iterator.rad +5 -2
1 1
//! returns: 0
2 +
//! uses-std
2 3
//! Shared contents keep their source region through mutable iterator views.
3 4
5 +
use std::fmt;
6 +
4 7
/// A token from the input.
5 8
union Token['src]: Copy {
6 9
    /// Identifier bytes.
7 10
    Ident(&'src [u8]),
8 11
    /// One decimal digit.
26 29
        return nil;
27 30
    }
28 31
    let start = l.pos;
29 32
    let ch = l.source[start];
30 33
    set l.pos += 1;
31 -
    if ch >= '0' and ch <= '9' {
32 -
        return Token::Number(ch - '0');
34 +
    if let digit = fmt::digitFromAscii(ch, 10) {
35 +
        return Token::Number(digit as u8);
33 36
    }
34 37
    return Token::Ident(&l.source[start .. l.pos]);
35 38
}
36 39
37 40
/// Return the input through a shared view of the cursor.
test/verify/expected +2 -0
63 63
prog.vm Type
64 64
range.arithmetic Type
65 65
record.scalar Class
66 66
ref.mut.ptr Type
67 67
region.heap Symbol
68 +
region.http Symbol
69 +
region.iterator Symbol
68 70
scratch.forge.opaque Type
69 71
slice.alloc.loop Type
70 72
slice.append.limit Slot
71 73
slice.append.limit.trap Symbol
72 74
slice.assign Type