Jelajahi Sumber

Make kernel build with clang.

It's a bit faster than g++ and seems to generate perfectly fine code.
The kernel is also roughly 10% smaller(!)
Andreas Kling 6 tahun lalu
induk
melakukan
ebf308d413

+ 12 - 12
AK/StdLib.h

@@ -114,31 +114,31 @@ struct IsPointer : __IsPointerHelper<typename RemoveCV<T>::Type> { };
 template<class> struct IsFunction : FalseType { };
 template<class> struct IsFunction : FalseType { };
 
 
 template<class Ret, class... Args> struct IsFunction<Ret(Args...)> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...)> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......)> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...)> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) volatile> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const volatile> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile> : TrueType { };
 
 
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) &> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) &> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const &> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) volatile &> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const volatile &> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &> : TrueType { };
 
 
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) &&> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) &&> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const &&> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const &&> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) volatile &&> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) volatile &&> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) volatile &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &&> : TrueType { };
 template<class Ret, class... Args> struct IsFunction<Ret(Args...) const volatile &&> : TrueType { };
-template<class Ret, class... Args> struct IsFunction<Ret(Args......) const volatile &&> : TrueType { };
+template<class Ret, class... Args> struct IsFunction<Ret(Args...,...) const volatile &&> : TrueType { };
 
 
 template<class T> struct IsRvalueReference : FalseType { };
 template<class T> struct IsRvalueReference : FalseType { };
 template<class T> struct IsRvalueReference<T&&> : TrueType { };
 template<class T> struct IsRvalueReference<T&&> : TrueType { };

+ 7 - 3
AK/StringImpl.cpp

@@ -29,9 +29,7 @@ static inline size_t allocationSizeForStringImpl(size_t length)
 
 
 RetainPtr<StringImpl> StringImpl::createUninitialized(size_t length, char*& buffer)
 RetainPtr<StringImpl> StringImpl::createUninitialized(size_t length, char*& buffer)
 {
 {
-    if (!length)
-        return theEmptyStringImpl();
-
+    ASSERT(length);
     void* slot = kmalloc(allocationSizeForStringImpl(length));
     void* slot = kmalloc(allocationSizeForStringImpl(length));
     if (!slot)
     if (!slot)
         return nullptr;
         return nullptr;
@@ -52,6 +50,8 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, Sho
 
 
     char* buffer;
     char* buffer;
     auto newStringImpl = createUninitialized(length, buffer);
     auto newStringImpl = createUninitialized(length, buffer);
+    if (!newStringImpl)
+        return nullptr;
     memcpy(buffer, cstring, length * sizeof(char));
     memcpy(buffer, cstring, length * sizeof(char));
 
 
     if (shouldChomp && buffer[length - 1] == '\n') {
     if (shouldChomp && buffer[length - 1] == '\n') {
@@ -108,6 +108,8 @@ RetainPtr<StringImpl> StringImpl::toLowercase() const
 slowPath:
 slowPath:
     char* buffer;
     char* buffer;
     auto lowercased = createUninitialized(m_length, buffer);
     auto lowercased = createUninitialized(m_length, buffer);
+    if (!lowercased)
+        return nullptr;
     for (size_t i = 0; i < m_length; ++i)
     for (size_t i = 0; i < m_length; ++i)
         buffer[i] = toASCIILowercase(m_characters[i]);
         buffer[i] = toASCIILowercase(m_characters[i]);
 
 
@@ -128,6 +130,8 @@ RetainPtr<StringImpl> StringImpl::toUppercase() const
 slowPath:
 slowPath:
     char* buffer;
     char* buffer;
     auto uppercased = createUninitialized(m_length, buffer);
     auto uppercased = createUninitialized(m_length, buffer);
+    if (!uppercased)
+        return nullptr;
     for (size_t i = 0; i < m_length; ++i)
     for (size_t i = 0; i < m_length; ++i)
         buffer[i] = toASCIIUppercase(m_characters[i]);
         buffer[i] = toASCIIUppercase(m_characters[i]);
 
 

+ 0 - 1
AK/StringImpl.h

@@ -42,7 +42,6 @@ private:
     void computeHash() const;
     void computeHash() const;
 
 
     size_t m_length { 0 };
     size_t m_length { 0 };
-    bool m_ownsBuffer { true };
     mutable bool m_hasHash { false };
     mutable bool m_hasHash { false };
     const char* m_characters { nullptr };
     const char* m_characters { nullptr };
     mutable unsigned m_hash { 0 };
     mutable unsigned m_hash { 0 };

+ 1 - 1
ELFLoader/ELFLoader.cpp

@@ -49,7 +49,7 @@ bool ELFLoader::layout()
         }
         }
     });
     });
 
 
-    m_image.for_each_section_of_type(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) {
+    m_image.for_each_section_of_type(SHT_PROGBITS, [this] (const ELFImage::Section& section) {
 #ifdef ELFLOADER_DEBUG
 #ifdef ELFLOADER_DEBUG
         kprintf("ELFLoader: Copying progbits section: %s\n", section.name());
         kprintf("ELFLoader: Copying progbits section: %s\n", section.name());
 #endif
 #endif

+ 4 - 4
Kernel/Makefile

@@ -55,19 +55,19 @@ BOOTLOADER = Boot/boot.bin
 IMAGE = .floppy-image
 IMAGE = .floppy-image
 ARCH_FLAGS =
 ARCH_FLAGS =
 STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib #-nostdinc
 STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib #-nostdinc
-KERNEL_FLAGS = -ffreestanding -fno-stack-protector -fno-ident
+KERNEL_FLAGS = -ffreestanding -fno-stack-protector -fno-ident -fno-builtin
 WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings
 WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings
 FLAVOR_FLAGS = -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic
 FLAVOR_FLAGS = -mregparm=3 -march=i386 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic
-OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
+OPTIMIZATION_FLAGS = -Oz -fno-asynchronous-unwind-tables
 INCLUDE_FLAGS = -I.. -I.
 INCLUDE_FLAGS = -I.. -I.
-SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn 
+#SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn 
 
 
 DEFINES = -DSERENITY -DKERNEL -DSANITIZE_PTRS
 DEFINES = -DSERENITY -DKERNEL -DSANITIZE_PTRS
 
 
 CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(KERNEL_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
 CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(KERNEL_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
 #CXX = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-g++
 #CXX = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-g++
 #LD = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-ld
 #LD = /usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-ld
-CXX = g++-8
+CXX = clang
 LD = ld
 LD = ld
 LDFLAGS = -T linker.ld --strip-debug -melf_i386 --gc-sections --build-id=none -z norelro -z now
 LDFLAGS = -T linker.ld --strip-debug -melf_i386 --gc-sections --build-id=none -z norelro -z now
 
 

+ 4 - 2
Kernel/MemoryManager.h

@@ -52,7 +52,8 @@ private:
     PhysicalAddress m_paddr;
     PhysicalAddress m_paddr;
 };
 };
 
 
-struct PageDirectory {
+class PageDirectory {
+public:
     dword entries[1024];
     dword entries[1024];
     RetainPtr<PhysicalPage> physical_pages[1024];
     RetainPtr<PhysicalPage> physical_pages[1024];
 
 
@@ -91,7 +92,8 @@ private:
     Vector<RetainPtr<PhysicalPage>> m_physical_pages;
     Vector<RetainPtr<PhysicalPage>> m_physical_pages;
 };
 };
 
 
-struct Region : public Retainable<Region> {
+class Region : public Retainable<Region> {
+public:
     Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false);
     Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false);
     Region(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false);
     Region(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false);
     Region(LinearAddress, size_t, RetainPtr<VirtualFileSystem::Node>&&, String&&, bool r, bool w);
     Region(LinearAddress, size_t, RetainPtr<VirtualFileSystem::Node>&&, String&&, bool r, bool w);

+ 6 - 2
Kernel/StdLib.cpp

@@ -3,6 +3,8 @@
 #include "kmalloc.h"
 #include "kmalloc.h"
 #include <AK/Types.h>
 #include <AK/Types.h>
 
 
+extern "C" {
+
 void memcpy(void *dest, const void *src, DWORD n)
 void memcpy(void *dest, const void *src, DWORD n)
 {
 {
     BYTE* bdest = (BYTE*)dest;
     BYTE* bdest = (BYTE*)dest;
@@ -71,8 +73,10 @@ int memcmp(const void* v1, const void* v2, size_t n)
     return 0;
     return 0;
 }
 }
 
 
-extern "C" void __cxa_pure_virtual() NORETURN;
-extern "C" void __cxa_pure_virtual()
+void __cxa_pure_virtual() NORETURN;
+void __cxa_pure_virtual()
 {
 {
     ASSERT_NOT_REACHED();
     ASSERT_NOT_REACHED();
 }
 }
+
+}

+ 4 - 0
Kernel/StdLib.h

@@ -2,6 +2,8 @@
 
 
 #include "types.h"
 #include "types.h"
 
 
+extern "C" {
+
 void memcpy(void*, const void*, DWORD);
 void memcpy(void*, const void*, DWORD);
 void strcpy(char*, const char*);
 void strcpy(char*, const char*);
 int strcmp(char const*, const char*);
 int strcmp(char const*, const char*);
@@ -10,3 +12,5 @@ void *memset(void*, BYTE, DWORD);
 char *strdup(const char*);
 char *strdup(const char*);
 int memcmp(const void*, const void*, size_t);
 int memcmp(const void*, const void*, size_t);
 char* strrchr(const char* str, int ch);
 char* strrchr(const char* str, int ch);
+
+}

+ 1 - 1
Kernel/Syscall.cpp

@@ -8,7 +8,7 @@ extern "C" void syscall_entry(RegisterDump&);
 extern "C" void syscall_ISR();
 extern "C" void syscall_ISR();
 extern volatile RegisterDump* syscallRegDump;
 extern volatile RegisterDump* syscallRegDump;
 
 
-asm volatile(
+asm(
     ".globl syscall_ISR \n"
     ".globl syscall_ISR \n"
     "syscall_ISR:\n"
     "syscall_ISR:\n"
     "    pusha\n"
     "    pusha\n"

+ 1 - 1
VirtualFileSystem/Ext2FileSystem.cpp

@@ -824,7 +824,7 @@ bool Ext2FileSystem::setBlockAllocationState(GroupIndex group, BlockIndex bi, bo
     ASSERT(block);
     ASSERT(block);
     auto bitmap = Bitmap::wrap(block.pointer(), block.size());
     auto bitmap = Bitmap::wrap(block.pointer(), block.size());
     bool currentState = bitmap.get(bitIndex);
     bool currentState = bitmap.get(bitIndex);
-    kprintf("ext2fs: setBlockAllocationState(%u) %u -> %u\n", block, currentState, newState);
+    kprintf("ext2fs: setBlockAllocationState(%u) %u -> %u\n", bi, currentState, newState);
 
 
     if (currentState == newState)
     if (currentState == newState)
         return true;
         return true;