temp liblily. progress pause until new hblang.

This commit is contained in:
koniifer 2025-02-09 23:56:22 +00:00
parent cdbc16816e
commit b72db1ce44
6 changed files with 128 additions and 10 deletions

View file

@ -1,6 +1,11 @@
# Lily
an attempt at a cross-platform standard library for hblang.
> [!CAUTION]
> # hblang is currently very broken
> like super broken. please don't use lily right now.<br>
> hblang is getting rewritten in zig, so soon we will have this all working again.
> [!IMPORTANT]
> all features, targets, modules, etc, are provisional and may be subject to change or deletion

26
build
View file

@ -9,6 +9,7 @@ readonly SRC_DIR="$SCRIPT_DIR/src"
readonly TEST_DIR="$SRC_DIR/test"
readonly BUILD_PATH="$SCRIPT_DIR/target"
readonly CHECKSUM_FILE="$BUILD_PATH/checksum"
readonly LIBLILY_DIR="$SCRIPT_DIR/liblily"
hbc_flags="--backend-flags=opt_level=none,regalloc_checker=true,enable_verifier=true,use_colocated_libcalls=true,enable_pcc=true,enable_llvm_abi_extensions=true,preserve_frame_pointers=true"
linker="" target="default" run=0 test="none" dump_asm=0 debug=1
@ -20,9 +21,9 @@ warn() { printf "\033[33mWARN\033[0m: %s\n" "$1"; }
# ! for now, ignore this.
check_hbc() {
command -v hbc >/dev/null 2>&1 && return 0
log "hblang compiler not found in \$PATH"
command -v cargo >/dev/null 2>&1 || die "missing both hbc and cargo"
# command -v hbc >/dev/null 2>&1 && return 0
# log "hblang compiler not found in \$PATH"
# command -v cargo >/dev/null 2>&1 || die "missing both hbc and cargo"
cargo_ver=$(cargo install --list | awk '/holey-bytes/ {if(match($0,/[0-9a-f]+/)) print substr($0,RSTART,8)}')
[ "$cargo_ver" = "$HBC_COMMIT" ] && return 0
@ -36,7 +37,7 @@ check_changes() {
previous_checksum=""
[ -r "$CHECKSUM_FILE" ] && previous_checksum=$(cat "$CHECKSUM_FILE")
current_checksum=$( (
find "$SRC_DIR" -type f -exec md5sum {} + | LC_ALL=C sort
find "$SRC_DIR" "$LIBLILY_DIR" -type f -exec md5sum {} + | LC_ALL=C sort
printf "%s%s%s" "$linker" "$target" "$hbc_flags"
) | md5sum)
echo "$current_checksum" >"$CHECKSUM_FILE"
@ -193,7 +194,7 @@ run_test() {
[ $debug -eq 0 ] && {
strip --strip-all --strip-debug "$BUILD_PATH/$target/lily.o"
}
$linker "$BUILD_PATH/test/$name.o" -o "$BUILD_PATH/test/$name" || die "linking failed:"
$linker "$BUILD_PATH/$target/lily.o" -L"$BUILD_PATH/$target/" -llily -o "$BUILD_PATH/$target/lily" || die "linking failed:"
if [ -n "$timeout_val" ]; then
# shellcheck disable=SC2086
@ -237,6 +238,12 @@ pretty_exitcode() {
# todo: this spawns every test all at once. consider limiting to $(nproc) tests at once.
do_tests() {
detect_linker
# temporary because yes :thumbsup:
mkdir -p "$BUILD_PATH/$target" || die "failed to create build dir"
$linker -c "$LIBLILY_DIR/lily.c" -o "$BUILD_PATH/$target/liblily.o"
ar rcs "$BUILD_PATH/$target/liblily.a" "$BUILD_PATH/$target/liblily.o"
mkdir -p "$BUILD_PATH/test" || die "failed to create test dir"
tmpfile=$(mktemp) || die "Failed to create temporary file"
@ -274,7 +281,12 @@ do_build() {
[ $debug -eq 0 ] && {
strip --strip-all --strip-debug "$BUILD_PATH/$target/lily.o"
}
$linker "$BUILD_PATH/$target/lily.o" -o "$BUILD_PATH/$target/lily"
# temporary because yes :thumbsup:
$linker -c "$LIBLILY_DIR/lily.c" -o "$BUILD_PATH/$target/liblily.o"
ar rcs "$BUILD_PATH/$target/liblily.a" "$BUILD_PATH/$target/liblily.o"
$linker "$BUILD_PATH/$target/lily.o" -L"$BUILD_PATH/$target/" -llily -o "$BUILD_PATH/$target/lily" || die "linking failed"
fi
fi
@ -284,7 +296,7 @@ do_build() {
main() {
parse_args "$@"
mkdir -p "$BUILD_PATH" || die "can't create build dir"
#check_hbc
# check_hbc
case $test in
none) do_build ;;

19
liblily/lily.c Normal file
View file

@ -0,0 +1,19 @@
#include "lily.h"
#if POSIX
#define _GNU_SOURCE 1
#include <sys/mman.h>
#include <sys/random.h>
#include <stdio.h>
#include <unistd.h>
u32 LILY_POSIX_PROT_READWRITE() {
return PROT_READ | PROT_WRITE;
}
u32 LILY_POSIX_MAP_SHAREDANONYMOUS() {
return MAP_SHARED | MAP_ANONYMOUS;
}
u32 LILY_POSIX_MREMAP_MAYMOVE() {
return MREMAP_MAYMOVE;
}
#endif

28
liblily/lily.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef __LILY_H_DEFINED
#define __LILY_H_DEFINED
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_POSIX_VERSION)
#define POSIX 1
#else
#define POSIX 0
#endif
typedef unsigned long long int lily_uint;
typedef unsigned int u32;
typedef unsigned short int u16;
typedef unsigned char u8;
typedef u8 bool;
bool is_posix() {
return POSIX;
}
#if POSIX
// we export these constants as functions
// because we cant import c constants in hblang yet
u32 LILY_POSIX_PROT_READWRITE();
u32 LILY_POSIX_MAP_SHAREDANONYMOUS();
u32 LILY_POSIX_MREMAP_MAYMOVE();
#endif // POSIX
#endif // __LILY_H_DEFINED

View file

@ -16,8 +16,8 @@ $add := fn(sum: uint, x: uint): uint {
}
main := fn(argc: uint, argv: []^void): uint {
sum := Generator.{}.into_iter().take(50).fold(add, 0)
lily.print(sum)
// sum := Generator.{}.into_iter().take(50).fold(add, 0)
// lily.print(sum)
// // ! (libc) (compiler) bug: .collect(T) does not work.
// if lily.Target.current() != .LibC {
@ -79,6 +79,17 @@ main := fn(argc: uint, argv: []^void): uint {
// lily.print("parent")
// }
a := @unwrap(lily.Target.alloc(1024))
i := 0
loop if i == 1024 break else {
defer i += 1;
*(a + i) = 'A'
// doesnt increment??? strange...
lily.print(a + i)
}
// print my screams to stdout
lily.print(a[0..250])
// Allocator := lily.alloc.ArenaAllocator
// allocator := Allocator.new()
// defer allocator.deinit()

View file

@ -24,4 +24,47 @@ $page_size := fn(): uint {
// also temp
$calculate_pages := fn(size: uint): uint {
return (size + page_size() - 1) / page_size()
}
}
// mmap := fn(ptr: ?^u8, len: uint, prot: u32, flags: u32, fd: u32, offset: uint): ?^u8 @import()
// munmap := fn(ptr: ^u8, len: uint): void @import()
// mremap := fn(ptr: ^u8, old_len: uint, new_len: uint, flags: u32): ?^u8 @import()
// getpagesize := fn(): u32 @import()
// LILY_POSIX_PROT_READWRITE := fn(): u32 @import()
// LILY_POSIX_MAP_SHAREDANONYMOUS := fn(): u32 @import()
// LILY_POSIX_MREMAP_MAYMOVE := fn(): u32 @import()
// causes segfault. nice.
// $alloc := fn(len: uint): ?^u8 return mmap(
// null,
// len,
// LILY_POSIX_PROT_READWRITE(),
// LILY_POSIX_MAP_SHARED(),
// -1,
// 0,
// )
// alloc := alloc_zeroed
// $alloc_zeroed := fn(len: uint): ?^u8 return mmap(
// null,
// len,
// LILY_POSIX_PROT_READWRITE(),
// LILY_POSIX_MAP_SHAREDANONYMOUS(),
// -1,
// 0,
// )
// $realloc := fn(ptr: ^u8, len: uint, len_new: uint): ?^u8 return mremap(
// ptr,
// len,
// len_new,
// LILY_POSIX_MREMAP_MAYMOVE(),
// )
// $dealloc := fn(ptr: ^u8, len: uint): void munmap(ptr, len)
// $page_size := fn(): uint return getpagesize()
// $calculate_pages := fn(size: uint): uint {
// return (size + page_size() - 1) / page_size()
// }