holey-bytes/lang/README.md

1213 lines
19 KiB
Markdown
Raw Normal View History

2024-06-15 03:37:50 -05:00
# HERE SHALL THE DOCUMENTATION RESIDE
2024-06-20 04:18:36 -05:00
## Enforced Political Views
2024-06-15 03:38:16 -05:00
2024-06-20 04:18:36 -05:00
- worse is better
- less is more
- embrace `unsafe {}`
- adhere `macro_rules!`
- pessimization == death (put in `std::pin::Pin` and left with hungry crabs)
- importing external dependencies == death (`fn(dependencies) -> ExecutionStrategy`)
- above sell not be disputed, discussed, or questioned
## What hblang is
Holey-Bytes-Language (hblang for short) (*.hb) is the only true language targeting hbvm byte code. hblang is low level, manually managed, and procedural. Its rumored to be better then writing hbasm and you should probably use it for complex applications.
## What hblang isnt't
hblang knows what it isn't, because it knows what it is, hblang computes this by sub...
## Examples
Examples are also used in tests. To add an example that runs during testing add:
2024-06-15 03:46:53 -05:00
<pre>
2024-06-15 03:49:02 -05:00
#### &lt;name&gt
2024-06-15 03:48:42 -05:00
```hb
&lt;example&gt
```
2024-06-15 03:46:53 -05:00
</pre>
and also:
```rs
2024-10-27 13:55:11 -05:00
<name>;
2024-06-15 03:46:53 -05:00
```
2024-10-27 13:55:11 -05:00
to the `run_tests` macro at the bottom of the `src/son.rs`.
2024-06-15 03:46:53 -05:00
2024-06-20 04:18:36 -05:00
### Tour Examples
Following examples incrementally introduce language features and syntax.
#### main_fn
2024-06-15 03:37:50 -05:00
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return 1
2024-06-15 03:37:50 -05:00
}
2024-06-20 04:18:36 -05:00
```
2024-06-15 03:37:50 -05:00
2024-06-20 04:18:36 -05:00
#### arithmetic
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-14 04:26:54 -05:00
return 10 - 20 / 2 + 4 * (2 + 2) - 4 * 4 + (1 << 0) + -1
2024-06-20 04:18:36 -05:00
}
```
2024-10-29 07:36:12 -05:00
#### floating_point_arithmetic
```hb
main := fn(): f32 {
return 10. - 20. / 2. + 4. * (2. + 2.) - 4. * 4. + -1.
}
```
2024-06-20 04:18:36 -05:00
#### functions
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
return add_one(10) + add_two(20)
2024-06-20 04:18:36 -05:00
}
2024-10-25 07:51:33 -05:00
add_two := fn(x: uint): uint {
2024-07-19 14:04:22 -05:00
return x + 2
2024-06-20 04:18:36 -05:00
}
2024-10-25 07:51:33 -05:00
add_one := fn(x: uint): uint {
2024-07-19 14:04:22 -05:00
return x + 1
2024-06-15 03:37:50 -05:00
}
```
2024-06-25 12:46:48 -05:00
#### comments
```hb
// commant is an item
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-06-25 12:46:48 -05:00
// comment is a statement
2024-09-01 14:05:17 -05:00
2024-07-19 14:04:22 -05:00
foo(/* comment is an exprression /* if you are crazy */ */)
return 0
2024-06-25 12:46:48 -05:00
}
2024-07-19 14:04:22 -05:00
foo := fn(comment: void): void return /* comment evaluates to void */
2024-06-25 12:46:48 -05:00
// comments might be formatted in the future
```
2024-06-15 03:37:50 -05:00
#### if_statements
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
return fib(10)
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
fib := fn(x: uint): uint {
2024-06-15 03:37:50 -05:00
if x <= 2 {
2024-07-19 14:04:22 -05:00
return 1
2024-06-15 03:37:50 -05:00
} else {
2024-07-19 14:04:22 -05:00
return fib(x - 1) + fib(x - 2)
2024-06-15 03:37:50 -05:00
}
}
```
2024-06-20 04:18:36 -05:00
#### variables
2024-06-15 03:37:50 -05:00
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
ඞ := 1
b := 2
ඞ += 1
return ඞ - b
2024-06-15 03:37:50 -05:00
}
```
2024-06-20 04:18:36 -05:00
#### loops
2024-06-15 03:37:50 -05:00
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
return fib(10)
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
fib := fn(n: uint): uint {
2024-07-19 14:04:22 -05:00
a := 0
b := 1
2024-09-06 11:50:28 -05:00
loop if n == 0 break else {
2024-07-19 14:04:22 -05:00
c := a + b
a = b
b = c
n -= 1
2024-06-20 04:18:36 -05:00
}
2024-07-19 14:04:22 -05:00
return a
2024-06-15 03:37:50 -05:00
}
```
2024-06-20 04:18:36 -05:00
#### pointers
2024-06-15 03:37:50 -05:00
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-10-27 13:55:11 -05:00
n := @as(^uint, null)
if n != null return 9001
2024-07-19 14:04:22 -05:00
a := 1
b := &a
2024-10-13 08:22:16 -05:00
boundary := 1000
b = b + boundary - 2
b = b - (boundary - 2)
2024-07-19 14:04:22 -05:00
modify(b)
drop(a)
return *b - 2
2024-06-20 04:18:36 -05:00
}
2024-06-15 03:37:50 -05:00
2024-10-25 07:51:33 -05:00
modify := fn(a: ^uint): void {
2024-07-19 14:04:22 -05:00
*a = 2
return
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
drop := fn(a: uint): void {
2024-07-19 14:04:22 -05:00
return
2024-06-15 03:37:50 -05:00
}
```
#### structs
```hb
Ty := struct {
// comment
2024-10-25 07:51:33 -05:00
a: uint,
2024-06-15 03:37:50 -05:00
}
Ty2 := struct {
ty: Ty,
2024-10-25 07:51:33 -05:00
c: uint,
2024-06-15 03:37:50 -05:00
}
useless := struct {}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
// `packed` structs have no padding (all fields are alighred to 1)
2024-09-22 11:17:30 -05:00
if @sizeof(packed struct {a: u8, b: u16}) != 3 {
return 9001
}
2024-10-24 02:43:07 -05:00
finst := Ty2.{ty: .{a: 4}, c: 3}
2024-10-12 06:07:49 -05:00
inst := odher_pass(finst)
if inst.c == 3 {
return pass(&inst.ty)
}
return 0
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
pass := fn(t: ^Ty): uint {
2024-10-24 02:43:07 -05:00
return t.a
2024-06-15 03:37:50 -05:00
}
odher_pass := fn(t: Ty2): Ty2 {
2024-07-19 14:04:22 -05:00
return t
2024-06-15 03:37:50 -05:00
}
```
#### hex_octal_binary_literals
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
hex := 0xFF
decimal := 255
octal := 0o377
binary := 0b11111111
if hex == decimal & octal == decimal & binary == decimal {
return 0
}
return 1
}
```
2024-06-20 04:18:36 -05:00
#### struct_operators
2024-06-15 03:37:50 -05:00
```hb
2024-06-20 04:18:36 -05:00
Point := struct {
2024-10-25 07:51:33 -05:00
x: uint,
y: uint,
2024-06-15 03:37:50 -05:00
}
2024-06-20 04:18:36 -05:00
Rect := struct {
a: Point,
b: Point,
2024-06-15 03:37:50 -05:00
}
2024-10-17 08:48:22 -05:00
Color := packed struct {b: u8, g: u8, r: u8, a: u8}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-10-17 08:48:22 -05:00
i := Color.(0, 0, 0, 0)
i += .(1, 1, 1, 1)
if i.r + i.g + i.b + i.a != 4 {
return 1008
}
2024-07-19 14:04:22 -05:00
a := Point.(1, 2)
b := Point.(3, 4)
2024-06-20 04:18:36 -05:00
2024-07-19 14:04:22 -05:00
d := Rect.(a + b, b - a)
zp := Point.(0, 0)
d2 := Rect.(zp - b, a)
2024-07-19 14:04:22 -05:00
d2 += d
2024-06-20 04:18:36 -05:00
2024-07-19 14:04:22 -05:00
c := d2.a + d2.b
return c.x + c.y
2024-06-15 03:37:50 -05:00
}
```
2024-06-20 04:18:36 -05:00
#### global_variables
2024-06-15 03:37:50 -05:00
```hb
2024-07-19 14:04:22 -05:00
global_var := 10
2024-06-20 04:18:36 -05:00
2024-07-19 14:04:22 -05:00
complex_global_var := fib(global_var) - 5
2024-06-20 04:18:36 -05:00
2024-10-25 07:51:33 -05:00
fib := fn(n: uint): uint {
2024-06-20 04:18:36 -05:00
if 2 > n {
2024-07-19 14:04:22 -05:00
return n
2024-06-20 04:18:36 -05:00
}
2024-07-19 14:04:22 -05:00
return fib(n - 1) + fib(n - 2)
2024-06-20 04:18:36 -05:00
}
2024-06-15 03:37:50 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-08 05:00:07 -05:00
complex_global_var += 5
2024-07-19 14:04:22 -05:00
return complex_global_var
2024-06-15 03:37:50 -05:00
}
2024-06-20 04:18:36 -05:00
```
note: values of global variables are evaluated at compile time
2024-06-15 03:37:50 -05:00
2024-06-20 04:18:36 -05:00
#### directives
```hb
2024-07-19 14:04:22 -05:00
foo := @use("foo.hb")
2024-06-15 03:37:50 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
byte := @as(u8, 10)
same_type_as_byte := @as(@TypeOf(byte), 30)
wide_uint := @as(u32, 40)
truncated_uint := @as(u8, @intcast(wide_uint))
2024-10-29 08:24:31 -05:00
widened_float := @as(f64, @floatcast(1.))
int_from_float := @as(int, @fti(1.))
float_from_int := @as(f64, @itf(@as(int, 1)))
2024-07-19 14:04:22 -05:00
size_of_Type_in_bytes := @sizeof(foo.Type)
align_of_Type_in_bytes := @alignof(foo.Type)
hardcoded_pointer := @as(^u8, @bitcast(10))
2024-10-25 07:51:33 -05:00
ecall_that_returns_uint := @as(uint, @eca(1, foo.Type.(10, 20), 5, 6))
2024-10-21 12:57:55 -05:00
embedded_array := @as([u8; 15], @embed("text.txt"))
2024-09-01 19:38:11 -05:00
return @inline(foo.foo)
2024-06-15 03:37:50 -05:00
}
2024-07-08 04:00:35 -05:00
// in module: foo.hb
2024-07-08 04:00:35 -05:00
Type := struct {
2024-10-25 07:51:33 -05:00
brah: uint,
blah: uint,
2024-07-08 04:00:35 -05:00
}
2024-09-01 19:38:11 -05:00
2024-10-25 07:51:33 -05:00
foo := fn(): uint return 0
2024-10-13 13:01:18 -05:00
// in module: text.txt
arbitrary text
2024-06-20 04:18:36 -05:00
```
2024-06-15 03:37:50 -05:00
2024-10-27 13:55:11 -05:00
- `@use(<string>)`: imports a module based on relative path, cycles are allowed when importing
2024-06-20 04:18:36 -05:00
- `@TypeOf(<expr>)`: results into literal type of whatever the type of `<expr>` is, `<expr>` is not included in final binary
2024-10-27 13:55:11 -05:00
- `@as(<ty>, <expr>)`: hint to the compiler that `@TypeOf(<expr>) == <ty>`
2024-06-20 04:18:36 -05:00
- `@intcast(<expr>)`: needs to be used when conversion of `@TypeOf(<expr>)` would loose precision (widening of integers is implicit)
2024-10-27 13:55:11 -05:00
- `@sizeof(<ty>), @alignof(<ty>)`: get size and align of a type in bytes
- `@bitcast(<expr>)`: tell compiler to assume `@TypeOf(<expr>)` is whatever is inferred, so long as size matches
- `@eca(...<expr>)`: invoke `eca` instruction, where return type is inferred and `<expr>...` are arguments passed to the call in the standard call convention
- `@embed(<string>)`: include relative file as an array of bytes
2024-09-01 19:38:11 -05:00
- `@inline(<func>, ...<args>)`: equivalent to `<func>(...<args>)` but function is guaranteed to inline, compiler will otherwise never inline
2024-06-15 03:37:50 -05:00
2024-07-02 07:49:05 -05:00
#### c_strings
```hb
2024-10-25 07:51:33 -05:00
str_len := fn(str: ^u8): uint {
2024-07-19 14:04:22 -05:00
len := 0
2024-07-07 06:42:48 -05:00
loop if *str == 0 break else {
2024-07-19 14:04:22 -05:00
len += 1
str += 1
2024-07-02 07:49:05 -05:00
}
2024-07-19 14:04:22 -05:00
return len
2024-07-02 07:49:05 -05:00
}
2024-07-07 06:42:48 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-07 06:42:48 -05:00
// when string ends with '\0' its a C string and thus type is '^u8'
2024-09-04 11:43:08 -05:00
some_str := "abඞ\n\r\t\{35}\{36373839}\0"
2024-07-19 14:04:22 -05:00
len := str_len(some_str)
some_other_str := "fff\0"
lep := str_len(some_other_str)
return lep + len
2024-07-07 06:42:48 -05:00
}
2024-07-02 07:49:05 -05:00
```
2024-07-08 04:00:35 -05:00
#### struct_patterns
```hb
2024-07-19 14:04:22 -05:00
.{fib, fib_iter, Fiber} := @use("fibs.hb")
2024-07-08 04:00:35 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
.{a, b} := Fiber.{a: 10, b: 10}
return fib(a) - fib_iter(b)
2024-07-08 04:00:35 -05:00
}
// in module: fibs.hb
2024-07-19 14:04:22 -05:00
Fiber := struct {a: u8, b: u8}
2024-07-08 04:00:35 -05:00
2024-10-25 07:51:33 -05:00
fib := fn(n: uint): uint if n < 2 {
2024-07-19 14:04:22 -05:00
return n
2024-07-08 04:00:35 -05:00
} else {
2024-07-19 14:04:22 -05:00
return fib(n - 1) + fib(n - 2)
}
2024-07-08 04:00:35 -05:00
2024-10-25 07:51:33 -05:00
fib_iter := fn(n: uint): uint {
2024-07-19 14:04:22 -05:00
a := 0
b := 1
2024-07-08 04:00:35 -05:00
loop if n == 0 break else {
2024-07-19 14:04:22 -05:00
c := a + b
a = b
b = c
n -= 1
2024-07-08 04:00:35 -05:00
}
2024-07-19 14:04:22 -05:00
return a
2024-07-08 04:00:35 -05:00
}
```
2024-07-08 11:08:58 -05:00
#### arrays
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
addr := @as(u16, 0x1FF)
2024-10-21 11:57:23 -05:00
msg := [u8].(0, 0, @intcast(addr), @intcast(addr >> 8))
_force_stack := &msg
2024-10-25 07:51:33 -05:00
arr := [uint].(1, 2, 4)
return pass(&arr) + msg[3]
2024-07-08 11:08:58 -05:00
}
2024-10-25 07:51:33 -05:00
pass := fn(arr: ^[uint; 3]): uint {
2024-07-19 14:04:22 -05:00
return arr[0] + arr[1] + arr[arr[1]]
2024-07-08 11:08:58 -05:00
}
```
2024-09-01 19:38:11 -05:00
#### inline
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-01 19:38:11 -05:00
return @inline(foo, 1, 2, 3) - 6
}
gb := 0
2024-10-25 07:51:33 -05:00
foo := fn(a: uint, b: uint, c: uint): uint {
2024-10-22 09:53:48 -05:00
if false | gb != 0 return 1
2024-09-01 19:38:11 -05:00
return a + b + c
}
```
#### idk
```hb
2024-10-25 07:51:33 -05:00
_edge_case := @as(uint, idk)
2024-09-12 11:42:21 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
big_array := @as([u8; 128], idk)
i := 0
loop if i >= 128 break else {
big_array[i] = 69
i += 1
}
return big_array[42]
}
```
2024-10-27 13:55:11 -05:00
note: this does not work on scalar values
2024-10-22 00:20:08 -05:00
#### generic_functions
```hb
add := fn($T: type, a: T, b: T): T return a + b
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return add(u32, 2, 2) - add(uint, 1, 3)
2024-10-22 00:20:08 -05:00
}
```
2024-06-20 04:18:36 -05:00
### Incomplete Examples
2024-06-15 03:37:50 -05:00
#### comptime_pointers
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
$integer := 7
modify(&integer)
return integer
}
2024-10-25 07:51:33 -05:00
modify := fn($num: ^uint): void {
2024-07-19 14:04:22 -05:00
$: *num = 0
}
```
2024-06-20 04:18:36 -05:00
#### generic_types
2024-06-15 03:37:50 -05:00
```hb
2024-06-24 10:26:00 -05:00
MALLOC_SYS_CALL := 69
FREE_SYS_CALL := 96
2024-10-25 07:51:33 -05:00
malloc := fn(size: uint, align: uint): ^void return @eca(MALLOC_SYS_CALL, size, align)
free := fn(ptr: ^void, size: uint, align: uint): void return @eca(FREE_SYS_CALL, ptr, size, align)
2024-06-24 10:26:00 -05:00
2024-06-20 04:18:36 -05:00
Vec := fn($Elem: type): type {
return struct {
data: ^Elem,
2024-10-25 07:51:33 -05:00
len: uint,
cap: uint,
2024-07-19 14:04:22 -05:00
}
2024-06-20 04:18:36 -05:00
}
2024-07-19 14:04:22 -05:00
new := fn($Elem: type): Vec(Elem) return Vec(Elem).{data: @bitcast(0), len: 0, cap: 0}
2024-06-24 10:26:00 -05:00
deinit := fn($Elem: type, vec: ^Vec(Elem)): void {
2024-06-24 10:45:58 -05:00
free(@bitcast(vec.data), vec.cap * @sizeof(Elem), @alignof(Elem));
2024-07-19 14:04:22 -05:00
*vec = new(Elem)
2024-07-19 06:51:38 -05:00
return
2024-06-24 10:26:00 -05:00
}
push := fn($Elem: type, vec: ^Vec(Elem), value: Elem): ^Elem {
if vec.len == vec.cap {
if vec.cap == 0 {
2024-07-19 14:04:22 -05:00
vec.cap = 1
2024-06-24 10:26:00 -05:00
} else {
2024-07-19 14:04:22 -05:00
vec.cap *= 2
2024-06-24 10:26:00 -05:00
}
2024-07-19 14:04:22 -05:00
new_alloc := @as(^Elem, @bitcast(malloc(vec.cap * @sizeof(Elem), @alignof(Elem))))
if new_alloc == 0 return @bitcast(0)
2024-07-19 14:04:22 -05:00
src_cursor := vec.data
dst_cursor := new_alloc
end := vec.data + vec.len
2024-06-24 10:26:00 -05:00
loop if src_cursor == end break else {
2024-07-19 14:04:22 -05:00
*dst_cursor = *src_cursor
src_cursor += 1
dst_cursor += 1
2024-06-24 10:26:00 -05:00
}
2024-06-24 10:45:58 -05:00
if vec.len != 0 {
2024-07-19 14:04:22 -05:00
free(@bitcast(vec.data), vec.len * @sizeof(Elem), @alignof(Elem))
2024-06-24 10:45:58 -05:00
}
2024-07-19 14:04:22 -05:00
vec.data = new_alloc
2024-06-24 10:26:00 -05:00
}
slot := vec.data + vec.len;
2024-07-19 14:04:22 -05:00
*slot = value
vec.len += 1
return slot
2024-06-24 10:26:00 -05:00
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
vec := new(uint)
_f := push(uint, &vec, 69)
2024-07-19 14:04:22 -05:00
res := *vec.data
2024-10-25 07:51:33 -05:00
deinit(uint, &vec)
2024-07-19 14:04:22 -05:00
return res
2024-06-15 03:37:50 -05:00
}
```
2024-06-20 04:18:36 -05:00
#### fb_driver
2024-06-15 03:37:50 -05:00
```hb
2024-10-25 07:51:33 -05:00
arm_fb_ptr := fn(): uint return 100
x86_fb_ptr := fn(): uint return 100
2024-06-20 04:18:36 -05:00
2024-10-25 07:51:33 -05:00
check_platform := fn(): uint {
2024-07-19 14:04:22 -05:00
return x86_fb_ptr()
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
set_pixel := fn(x: uint, y: uint, width: uint): uint {
2024-09-08 10:11:33 -05:00
return y * width + x
2024-06-20 04:18:36 -05:00
}
2024-06-15 03:37:50 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
fb_ptr := check_platform()
width := 100
height := 30
x := 0
y := 0
2024-09-15 13:14:56 -05:00
//t := 0
2024-09-08 10:11:33 -05:00
i := 0
2024-07-19 14:04:22 -05:00
loop {
2024-09-15 13:14:56 -05:00
if x < height {
//t += set_pixel(x, y, height)
2024-07-19 14:04:22 -05:00
x += 1
2024-09-15 13:14:56 -05:00
i += 1
2024-07-19 14:04:22 -05:00
} else {
x = 0
y += 1
2024-09-15 13:14:56 -05:00
if set_pixel(x, y, height) != i return 0
if y == width break
2024-07-19 14:04:22 -05:00
}
}
2024-09-15 13:14:56 -05:00
return i
2024-06-15 03:37:50 -05:00
}
```
2024-06-20 04:18:36 -05:00
### Purely Testing Examples
#### reading_idk
```hb
main := fn(): int {
a := @as(int, idk)
return a
}
```
#### nonexistent_ident_import
```hb
main := @use("foo.hb").main
// in module: foo.hb
foo := fn(): void {
return
}
foo := fn(): void {
return
}
main := @use("bar.hb").mian
// in module: bar.hb
main := fn(): void {
return
}
```
2024-10-25 17:34:22 -05:00
#### big_array_crash
```hb
SIN_TABLE := [int].(0, 174, 348, 523, 697, 871, 1045, 1218, 1391, 1564, 1736, 1908, 2079, 2249, 2419, 2588, 2756, 2923, 3090, 3255, 3420, 3583, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848, 5000, 5150, 5299, 5446, 5591, 5735, 5877, 6018, 6156, 6293, 6427, 6560, 6691, 6819, 6946, 7071, 7193, 7313, 7431, 7547, 7660, 7771, 7880, 7986, 8090, 8191, 8290, 8386, 8480, 8571, 8660, 8746, 8829, 8910, 8987, 9063, 9135, 9205, 9271, 9335, 9396, 9455, 9510, 9563, 9612, 9659, 9702, 9743, 9781, 9816, 9848, 9877, 9902, 9925, 9945, 9961, 9975, 9986, 9993, 9998, 10000)
main := fn(): int return SIN_TABLE[10]
```
2024-10-24 06:25:30 -05:00
#### returning_global_struct
```hb
Color := struct {r: u8, g: u8, b: u8, a: u8}
white := Color.(255, 255, 255, 255)
random_color := fn(): Color {
return white
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-10-24 06:25:30 -05:00
val := random_color()
2024-10-25 07:51:33 -05:00
return @as(uint, val.r) + val.g + val.b + val.a
2024-10-24 06:25:30 -05:00
}
```
2024-10-24 06:58:58 -05:00
#### small_struct_bitcast
```hb
Color := struct {r: u8, g: u8, b: u8, a: u8}
white := Color.(255, 255, 255, 255)
u32_to_color := fn(v: u32): Color return @as(Color, @bitcast(u32_to_u32(@bitcast(v))))
2024-10-24 07:08:17 -05:00
u32_to_u32 := fn(v: u32): u32 return v
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return u32_to_color(@bitcast(white)).r
}
```
#### small_struct_assignment
```hb
Color := struct {r: u8, g: u8, b: u8, a: u8}
white := Color.(255, 255, 255, 255)
black := Color.(0, 0, 0, 0)
2024-10-25 07:51:33 -05:00
main := fn(): uint {
f := black
f = white
return f.a
2024-10-24 06:58:58 -05:00
}
```
2024-10-24 09:26:28 -05:00
#### intcast_store
```hb
SetMsg := packed struct {a: u8, count: u32, size: u32, src: ^u8, dest: ^u8}
set := fn($Expr: type, src: ^Expr, dest: ^Expr, count: uint): u32 {
l := SetMsg.(5, @intcast(count), @intcast(@sizeof(Expr)), @bitcast(src), @bitcast(dest))
return l.count
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return set(uint, &0, &0, 1024)
2024-10-24 09:26:28 -05:00
}
2024-10-24 12:57:36 -05:00
```
#### string_flip
```hb
2024-10-25 07:51:33 -05:00
U := struct {u: uint}
main := fn(): uint {
2024-10-24 12:57:36 -05:00
arr := @as([U; 2 * 2], idk)
2024-10-24 09:26:28 -05:00
2024-10-24 12:57:36 -05:00
i := 0
loop if i == 2 * 2 break else {
arr[i] = .(i)
i += 1
}
i = 0
loop if i == 2 / 2 break else {
j := 0
loop if j == 2 break else {
a := i * 2 + j
b := (2 - i - 1) * 2 + j
tmp := arr[a]
arr[a] = arr[b]
arr[b] = tmp
j += 1
}
i += 1
}
return arr[0].u
}
2024-10-24 09:26:28 -05:00
```
#### wide_ret
```hb
OemIdent := struct {
dos_version: [u8; 8],
dos_version_name: [u8; 8],
}
Stru := struct {
a: u16,
b: u16,
}
small_struct := fn(): Stru {
return .{a: 0, b: 0}
}
2024-10-25 07:51:33 -05:00
maina := fn(major: uint, minor: uint): OemIdent {
_f := small_struct()
ver := [u8].(0, 0, 0, 3, 1, 0, 0, 0)
return OemIdent.(ver, ver)
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
m := maina(0, 0)
return m.dos_version[3] - m.dos_version_name[4]
}
```
2024-09-01 15:07:45 -05:00
#### comptime_min_reg_leak
```hb
2024-09-01 16:14:48 -05:00
a := @use("math.hb").min(100, 50)
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-01 16:14:48 -05:00
return a
}
// in module: math.hb
2024-10-25 07:51:33 -05:00
SIZEOF_uint := 32
SHIFT := SIZEOF_uint - 1
min := fn(a: uint, b: uint): uint {
2024-09-01 15:07:45 -05:00
c := a - b
return b + (c & c >> SHIFT)
}
```
2024-06-15 03:37:50 -05:00
#### different_types
```hb
Color := struct {
r: u8,
g: u8,
b: u8,
a: u8,
}
Point := struct {
x: u32,
y: u32,
}
Pixel := struct {
color: Color,
2024-10-25 07:51:33 -05:00
pouint: Point,
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-06-15 03:37:50 -05:00
pixel := Pixel.{
color: Color.{
r: 255,
g: 0,
b: 0,
a: 255,
},
2024-10-25 07:51:33 -05:00
pouint: Point.{
2024-06-15 03:37:50 -05:00
x: 0,
y: 2,
},
2024-07-19 14:04:22 -05:00
}
soupan := 1
2024-10-25 07:51:33 -05:00
if *(&pixel.pouint.x + soupan) != 2 {
2024-07-19 14:04:22 -05:00
return 0
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
if *(&pixel.pouint.y - 1) != 0 {
2024-07-19 14:04:22 -05:00
return 64
2024-06-15 03:37:50 -05:00
}
2024-10-25 07:51:33 -05:00
return pixel.pouint.x + pixel.pouint.y + pixel.color.r
2024-07-19 14:04:22 -05:00
+ pixel.color.g + pixel.color.b + pixel.color.a
2024-06-15 03:37:50 -05:00
}
```
2024-07-18 10:55:55 -05:00
#### struct_return_from_module_function
```hb
2024-07-19 14:04:22 -05:00
bar := @use("bar.hb")
2024-07-18 10:55:55 -05:00
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-07-19 14:04:22 -05:00
return 7 - bar.foo().x - bar.foo().y - bar.foo().z
2024-07-18 10:55:55 -05:00
}
// in module: bar.hb
2024-07-20 11:52:24 -05:00
foo := fn(): Foo {
2024-07-19 14:04:22 -05:00
return .{x: 3, y: 2, z: 2}
2024-07-18 10:55:55 -05:00
}
2024-07-20 11:52:24 -05:00
2024-10-25 07:51:33 -05:00
Foo := struct {x: uint, y: u32, z: u32}
2024-07-18 10:55:55 -05:00
```
#### sort_something_viredly
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return sqrt(100)
}
2024-10-25 07:51:33 -05:00
sqrt := fn(x: uint): uint {
temp := 0
g := 0
b := 32768
bshift := 15
loop if b == 0 {
break
} else {
bshift -= 1
temp = b + (g << 1)
temp <<= bshift
if x >= temp {
g += b
x -= temp
}
b >>= 1
}
return g
}
```
2024-10-27 13:13:25 -05:00
#### struct_in_register
2024-09-01 16:14:48 -05:00
```hb
ColorBGRA := struct {b: u8, g: u8, r: u8, a: u8}
MAGENTA := ColorBGRA.{b: 205, g: 0, r: 205, a: 255}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-01 16:14:48 -05:00
color := MAGENTA
return color.r
}
```
#### comptime_function_from_another_file
```hb
stn := @use("stn.hb")
CONST_A := 100
CONST_B := 50
a := stn.math.min(CONST_A, CONST_B)
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-01 16:14:48 -05:00
return a
}
// in module: stn.hb
math := @use("math.hb")
// in module: math.hb
2024-10-25 07:51:33 -05:00
SIZEOF_uint := 32
SHIFT := SIZEOF_uint - 1
min := fn(a: uint, b: uint): uint {
2024-09-01 16:14:48 -05:00
c := a - b
return b + (c & c >> SHIFT)
}
```
2024-09-01 20:21:39 -05:00
#### inline_test
```hb
2024-10-25 07:51:33 -05:00
fna := fn(a: uint, b: uint, c: uint): uint return a + b + c
2024-09-01 20:21:39 -05:00
2024-10-25 07:51:33 -05:00
scalar_values := fn(): uint {
2024-10-24 02:43:07 -05:00
return @inline(fna, 1, @inline(fna, 2, 3, 4), -10)
2024-09-01 20:21:39 -05:00
}
2024-10-25 07:51:33 -05:00
A := struct {a: uint}
AB := struct {a: A, b: uint}
2024-09-01 20:21:39 -05:00
2024-10-25 07:51:33 -05:00
mangle := fn(a: A, ab: AB): uint {
2024-10-24 02:43:07 -05:00
return a.a + ab.a.a - ab.b
2024-09-01 20:21:39 -05:00
}
2024-10-25 07:51:33 -05:00
structs := fn(): uint {
2024-10-24 02:43:07 -05:00
return @inline(mangle, .(0), .(.(@inline(mangle, .(20), .(.(5), 5))), 20))
2024-09-01 21:45:42 -05:00
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-10-24 02:43:07 -05:00
if scalar_values() != 0 return 1
if structs() != 0 return structs()
2024-10-24 02:43:07 -05:00
return 0
2024-09-01 21:45:42 -05:00
}
2024-09-01 20:21:39 -05:00
```
2024-09-04 09:54:34 -05:00
#### inlined_generic_functions
```hb
abs := fn($Expr: type, x: Expr): Expr {
mask := x >> @bitcast(@sizeof(Expr) - 1)
return (x ^ mask) - mask
}
2024-10-25 07:51:33 -05:00
main := fn(): uint {
return @inline(abs, uint, -10)
}
```
2024-09-04 09:54:34 -05:00
#### some_generic_code
```hb
some_func := fn($Elem: type): void {
return
}
main := fn(): void {
2024-09-04 11:48:25 -05:00
some_func(u8)
2024-09-04 09:54:34 -05:00
return
}
```
2024-09-05 19:42:07 -05:00
#### integer_inference_issues
```hb
.{integer_range} := @use("random.hb")
main := fn(): void {
a := integer_range(0, 1000)
return
}
// in module: random.hb
2024-10-25 07:51:33 -05:00
integer_range := fn(min: uint, max: uint): uint {
2024-09-17 11:11:07 -05:00
return @eca(3, 4) % (@bitcast(max) - min + 1) + min
2024-09-05 19:42:07 -05:00
}
```
2024-09-06 11:50:28 -05:00
2024-10-25 08:45:00 -05:00
#### signed_to_unsigned_upcast
```hb
main := fn(): uint return @as(i32, 1)
```
2024-09-08 10:11:33 -05:00
#### writing_into_string
```hb
outl := fn(): void {
msg := "whahaha\0"
_u := @as(u8, 0)
2024-09-08 10:11:33 -05:00
return
}
inl := fn(): void {
msg := "luhahah\0"
2024-09-08 10:11:33 -05:00
return
}
main := fn(): void {
outl()
inl()
return
}
```
2024-09-09 15:17:54 -05:00
#### request_page
```hb
request_page := fn(page_count: u8): ^u8 {
msg := "\{00}\{01}xxxxxxxx\0"
msg_page_count := msg + 1;
*msg_page_count = page_count
2024-09-17 11:11:07 -05:00
return @eca(3, 2, msg, 12)
2024-09-09 15:17:54 -05:00
}
create_back_buffer := fn(total_pages: int): ^u32 {
if total_pages <= 0xFF {
2024-10-22 00:20:08 -05:00
return @bitcast(request_page(@intcast(total_pages)))
2024-09-09 15:17:54 -05:00
}
ptr := request_page(255)
remaining := total_pages - 0xFF
loop if remaining <= 0 break else {
if remaining < 0xFF {
2024-10-22 00:20:08 -05:00
_f := request_page(@intcast(remaining))
2024-09-09 15:17:54 -05:00
} else {
2024-10-22 00:20:08 -05:00
_f := request_page(0xFF)
2024-09-09 15:17:54 -05:00
}
remaining -= 0xFF
}
return @bitcast(ptr)
}
main := fn(): void {
2024-10-22 00:20:08 -05:00
_f := create_back_buffer(400)
2024-09-09 15:17:54 -05:00
return
}
```
2024-09-09 18:15:18 -05:00
#### tests_ptr_to_ptr_copy
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-09 18:15:18 -05:00
back_buffer := @as([u8; 1024 * 10], idk)
n := 0
loop if n >= 1024 break else {
back_buffer[n] = 64
n += 1
}
n = 1
loop if n >= 10 break else {
*(@as(^[u8; 1024], @bitcast(&back_buffer)) + n) = *@as(^[u8; 1024], @bitcast(&back_buffer))
n += 1
}
return back_buffer[1024 * 2]
}
```
2024-09-28 08:13:32 -05:00
2024-11-03 08:03:59 -06:00
#### null_check_test
```hb
main := fn(): unit {
ptr := @as(?^uint, null)
*ptr = 0
if ptr == null {
return 1
}
return *ptr
}
```
2024-10-18 01:43:50 -05:00
### Just Testing Optimizations
#### const_folding_with_arg
```hb
2024-10-25 07:51:33 -05:00
main := fn(arg: uint): uint {
2024-10-18 01:43:50 -05:00
// reduces to 0
return arg + 0 - arg * 1 + arg + 1 + arg + 2 + arg + 3 - arg * 3 - 6
}
```
#### branch_assignments
```hb
2024-10-25 07:51:33 -05:00
main := fn(arg: uint): uint {
2024-10-18 01:43:50 -05:00
if arg == 1 {
arg = 1
} else if arg == 0 {
arg = 2
} else {
arg = 3
}
return arg
}
```
#### exhaustive_loop_testing
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-10-22 15:57:40 -05:00
loop break
x := 0
loop {
x += 1
break
}
2024-10-18 01:43:50 -05:00
if multiple_breaks(0) != 3 {
return 1
}
if multiple_breaks(4) != 10 {
return 2
}
if state_change_in_break(0) != 0 {
return 3
}
if state_change_in_break(4) != 10 {
return 4
}
if continue_and_state_change(10) != 10 {
return 5
}
if continue_and_state_change(3) != 0 {
return 6
}
2024-10-27 05:32:34 -05:00
infinite_loop()
return 0
}
infinite_loop := fn(): void {
2024-10-23 05:26:07 -05:00
f := 0
2024-10-22 15:57:40 -05:00
loop {
2024-10-23 05:26:07 -05:00
if f == 1 {
f = 0
} else {
f = 1
}
f = continue_and_state_change(0)
2024-10-22 15:57:40 -05:00
}
2024-10-18 01:43:50 -05:00
}
2024-10-25 07:51:33 -05:00
multiple_breaks := fn(arg: uint): uint {
2024-10-18 01:43:50 -05:00
loop if arg < 10 {
arg += 1
if arg == 3 break
} else break
return arg
}
2024-10-25 07:51:33 -05:00
state_change_in_break := fn(arg: uint): uint {
2024-10-18 01:43:50 -05:00
loop if arg < 10 {
if arg == 3 {
arg = 0
break
}
arg += 1
} else break
return arg
}
2024-10-25 07:51:33 -05:00
continue_and_state_change := fn(arg: uint): uint {
2024-10-18 01:43:50 -05:00
loop if arg < 10 {
if arg == 2 {
arg = 4
continue
}
if arg == 3 {
arg = 0
break
}
arg += 1
} else break
return arg
}
```
2024-09-28 08:13:32 -05:00
#### pointer_opts
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
2024-09-28 08:13:32 -05:00
mem := &0;
*mem = 1;
*mem = 2
b := *mem + *mem
clobber(mem)
b -= *mem
return b
}
2024-10-25 07:51:33 -05:00
clobber := fn(cb: ^uint): void {
2024-09-28 08:13:32 -05:00
*cb = 4
return
}
```
2024-10-18 06:11:11 -05:00
#### conditional_stores
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
cnd := cond()
2024-10-18 06:11:11 -05:00
mem := &1
if cnd == 0 {
2024-10-18 06:11:11 -05:00
*mem = 0
} else {
*mem = 2
}
return *mem
}
2024-10-25 07:51:33 -05:00
cond := fn(): uint return 0
2024-10-18 06:11:11 -05:00
```
#### loop_stores
```hb
2024-10-25 07:51:33 -05:00
main := fn(): uint {
mem := &10
loop if *mem == 0 break else {
*mem -= 1
}
return *mem
}
```
2024-10-26 13:29:31 -05:00
#### dead_code_in_loop
```hb
main := fn(): uint {
n := 0
loop if n < 10 {
if n < 10 break
n += 1
} else break
loop if n == 0 return n
return 1
}
```
2024-10-27 05:32:34 -05:00
#### infinite_loop_after_peephole
```hb
main := fn(): uint {
n := 0
f := 0
loop if n != 0 break else {
f += 1
}
return f
}
```
2024-10-28 12:39:42 -05:00
#### aliasing_overoptimization
```hb
Foo := struct {ptr: ^uint, rnd: uint}
main := fn(): uint {
mem := &2
stru := Foo.(mem, 0);
*stru.ptr = 0
return *mem
}
```
2024-10-29 09:04:07 -05:00
#### global_aliasing_overptimization
```hb
var := 0
main := fn(): uint {
var = 2
clobber()
return var
}
clobber := fn(): void {
var = 0
}
```
#### overwrite_aliasing_overoptimization
```hb
Foo := struct {a: int, b: int}
Bar := struct {f: Foo, b: int}
main := fn(): int {
value := Bar.{b: 1, f: .(4, 1)}
value.f = opaque()
return value.f.a - value.f.b - value.b
}
opaque := fn(): Foo {
return .(3, 2)
}