From 81429ad8e8daee9bc90e4558525535e4b37dbe5c Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 5 Mar 2021 21:08:58 +0200 Subject: libk: add int_t min max size constants --- drivers/makefile | 2 +- libk/types.h | 29 ++++++++++++++++--------- libk/types/test.cc | 64 ++++++++++++++++++++++++++++++++++++------------------ makefile | 13 +++++++---- src/makefile | 2 +- toolchain.makefile | 2 ++ 6 files changed, 75 insertions(+), 37 deletions(-) diff --git a/drivers/makefile b/drivers/makefile index 2a1fa0c..9405f0b 100644 --- a/drivers/makefile +++ b/drivers/makefile @@ -6,7 +6,7 @@ HWH := drivers/hardware.h autogen := $(autogen) $(HWH) $(HWH): .config - @echo " GEN $@" + @echo " GEN $@" @echo \/\* autogenerated hardware description \*\/ > $(HWH) @echo \#pragma once >> $(HWH) ifdef CONFIG_video0_enable diff --git a/libk/types.h b/libk/types.h index 4c50a35..7f16206 100644 --- a/libk/types.h +++ b/libk/types.h @@ -4,27 +4,36 @@ typedef unsigned short size_t; typedef int ssize_t; typedef unsigned char uint8_t; +constexpr uint8_t uint8_t_min = 0; +constexpr uint8_t uint8_t_max = 0xff; typedef unsigned short uint16_t; constexpr uint16_t uint16_t_min = 0; constexpr uint16_t uint16_t_max = 0xffff; typedef unsigned int uint32_t; -typedef unsigned long long int uint64_t; +constexpr uint32_t uint32_t_min = 0; +constexpr uint32_t uint32_t_max = 0xffffffff; + +typedef unsigned long long uint64_t; +constexpr uint64_t uint64_t_min = 0; +constexpr uint64_t uint64_t_max = 0xffffffffffffffff; typedef char int8_t; +constexpr int8_t int8_t_max = uint8_t_max / 2; +constexpr int8_t int8_t_min = -int8_t_max - 1; + typedef short int16_t; +constexpr int16_t int16_t_max = uint16_t_max / 2; +constexpr int16_t int16_t_min = -int16_t_max - 1; + typedef int int32_t; -typedef long long int int64_t; +constexpr int32_t int32_t_max = uint32_t_max / 2; +constexpr int32_t int32_t_min = -int32_t_max - 1; -/* in x86: - * byte: 1 byte, 8 bits - * word: 2 bytes, 16 bits - * dword: 4 bytes, 32 bits - * qword: 8 bytes, 64 bits - * */ -typedef uint16_t uword_t; -typedef int16_t word_t; +typedef long long int int64_t; +constexpr int64_t int64_t_max = uint64_t_max / 2; +constexpr int64_t int64_t_min = -int64_t_max - 1; // Type Traits diff --git a/libk/types/test.cc b/libk/types/test.cc index f93d883..b3f29d9 100644 --- a/libk/types/test.cc +++ b/libk/types/test.cc @@ -1,29 +1,51 @@ -#include +#include + +namespace libk { +#include "types.h" +} // types -static_assert(sizeof(size_t) >= 2); +static_assert(sizeof(libk::size_t) >= 2); + +static_assert(sizeof(libk::uint8_t) == 1); +static_assert(libk::uint8_t_max == UINT8_MAX); + +static_assert(sizeof(libk::uint16_t) == 2); +static_assert(libk::uint16_t_max == UINT16_MAX); + +static_assert(sizeof(libk::uint32_t) == 4); +static_assert(libk::uint32_t_max == UINT32_MAX); -static_assert(sizeof(uint8_t) == 1); -static_assert(sizeof(uint16_t) == 2); -static_assert(sizeof(uint32_t) == 4); -static_assert(sizeof(uint64_t) == 8); +static_assert(sizeof(libk::uint64_t) == 8); +static_assert(libk::uint64_t_max == UINT64_MAX); -static_assert(sizeof(int8_t) == 1); -static_assert(sizeof(int16_t) == 2); -static_assert(sizeof(int32_t) == 4); -static_assert(sizeof(int64_t) == 8); +static_assert(sizeof(libk::int8_t) == 1); +static_assert(libk::int8_t_min == INT8_MIN); +static_assert(libk::int8_t_max == INT8_MAX); + +static_assert(sizeof(libk::int16_t) == 2); +static_assert(libk::int16_t_min == INT16_MIN); +static_assert(libk::int16_t_max == INT16_MAX); + +static_assert(sizeof(libk::int32_t) == 4); +static_assert(libk::int32_t_min == INT32_MIN); +static_assert(libk::int32_t_max == INT32_MAX); + +static_assert(sizeof(libk::int64_t) == 8); +static_assert(libk::int64_t_min == INT64_MIN); +static_assert(libk::int64_t_max == INT64_MAX); // is_same -static_assert(is_same() == true); -static_assert(is_same() == false); +static_assert(libk::is_same() == true); +static_assert(libk::is_same() == false); // is_unsigned -static_assert(is_unsigned() == true); -static_assert(is_unsigned() == true); -static_assert(is_unsigned() == true); -static_assert(is_unsigned() == true); - -static_assert(is_unsigned() == false); -static_assert(is_unsigned() == false); -static_assert(is_unsigned() == false); -static_assert(is_unsigned() == false); +static_assert(libk::is_unsigned() == true); +static_assert(libk::is_unsigned() == true); +static_assert(libk::is_unsigned() == true); +static_assert(libk::is_unsigned() == true); + +static_assert(libk::is_unsigned() == false); +static_assert(libk::is_unsigned() == false); +static_assert(libk::is_unsigned() == false); +static_assert(libk::is_unsigned() == false); diff --git a/makefile b/makefile index 9af9064..829d566 100644 --- a/makefile +++ b/makefile @@ -17,20 +17,25 @@ CXX_INCLUDE := $(addprefix -I, $(CXX_INCLUDE)) CXX_SYSTEM_INCLUDE := $(addprefix -isystem, $(CXX_SYSTEM_INCLUDE)) glitch.elf: $(autogen) $(AS_OBJ) $(CXX_OBJ) - @echo " LD $@" + @echo " LD $@" @$(LD) $(LD_FLAGS) -o $@ $(AS_OBJ) $(CXX_OBJ) $(AS_OBJ): $(OBJ_DIR)/%.o: %.S @mkdir -p $(@D) - @echo " AS $<" + @echo " AS $<" @$(AS) -target $(TARGET) -nostdlib -Wall -Wextra -c $^ -o $@ -include $(CXX_DEP) -$(CXX_OBJ) $(CXX_TEST_OBJ): $(OBJ_DIR)/%.o : %.cc +$(CXX_OBJ): $(OBJ_DIR)/%.o : %.cc @mkdir -p $(@D) - @echo " CXX $<" + @echo " CXX $<" @$(CXX) -target $(TARGET) $(CXX_FLAGS) $(CXX_INCLUDE) $(CXX_SYSTEM_INCLUDE) -MMD -c $< -o $@ +$(CXX_TEST_OBJ): $(OBJ_DIR)/%.o : %.cc + @mkdir -p $(@D) + @echo " TEST $<" + @$(TEST_CXX) -target $(TARGET) $(CXX_TEST_FLAGS) $(CXX_INCLUDE) -MMD -c $< -o $@ + clean: @rm -rf $(autogen) $(AS_OBJ) $(CXX_OBJ) $(CXX_DEP) $(CXX_TEST_OBJ) glitch.elf isodir diff --git a/src/makefile b/src/makefile index d4b2ac8..ceaf5ef 100644 --- a/src/makefile +++ b/src/makefile @@ -12,7 +12,7 @@ CXX_OBJ += src/kernel.o \ src/scheduler.o src/cpu/irq.h: $(OBJ_DIR)/src/idt/stubs.o - @echo " GEN $@" + @echo " GEN $@" @echo '#pragma once' > $@ @echo 'extern "C" {' >> $@ @echo '/* interrupt map */' >> $@ diff --git a/toolchain.makefile b/toolchain.makefile index 0631904..ae791c1 100644 --- a/toolchain.makefile +++ b/toolchain.makefile @@ -11,6 +11,7 @@ LD := ld.lld LD_FLAGS := -T linker.ld CXX := clang++ +TEST_CXX := clang++ CXX_FLAGS := -std=c++20 \ -mkernel -ffreestanding -nostdlib -nostdinc -nostdinc++ \ -Wall -Wextra -Werror=pedantic -O2 \ @@ -18,6 +19,7 @@ CXX_FLAGS := -std=c++20 \ -Werror=shadow-all \ -Wold-style-cast -Wconversion \ -Wconsumed +CXX_TEST_FLAGS := -std=c++20 -Wall -Wextra -Werror=pedantic CXX_INCLUDE := $(CURDIR)/libk $(CURDIR)/src $(CURDIR)/drivers CXX_SYSTEM_INCLUDE := $(CURDIR)/grub -- cgit v1.2.1