Skip to content

Commit

Permalink
Better lowering of distinct types. Noreturn function call expr recogn…
Browse files Browse the repository at this point in the history
…ized as a "jump" for escape analysis. Preferring "def" in libs. To upper / to lower for ascii. Initial dynlib support.
  • Loading branch information
lerno committed May 21, 2023
1 parent a877d44 commit ddd0497
Show file tree
Hide file tree
Showing 55 changed files with 582 additions and 419 deletions.
2 changes: 1 addition & 1 deletion lib/std/collections/list.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module std::collections::list<Type>;
import std::math;

typedef ElementPredicate = fn bool(Type *type);
def ElementPredicate = fn bool(Type *type);

struct List
{
Expand Down
6 changes: 3 additions & 3 deletions lib/std/collections/object.c3
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ fn Object* Object.get_or_create_obj(Object* o, String key)
return container;
}

typedef ObjectInternalMap @private = HashMap<String, Object*>;
typedef ObjectInternalList @private = List<Object*>;
typedef ObjectInternalMapEntry @private = Entry<String, Object*>;
def ObjectInternalMap @private = HashMap<String, Object*>;
def ObjectInternalList @private = List<Object*>;
def ObjectInternalMapEntry @private = Entry<String, Object*>;

2 changes: 1 addition & 1 deletion lib/std/collections/priorityqueue.c3
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
module std::collections::priorityqueue<Type>;
import std::collections::list;

typedef Heap = List<Type>;
def Heap = List<Type>;

struct PriorityQueue
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/allocators/arena_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn void*! ArenaAllocator._alloc(ArenaAllocator* this, usz size, usz alignment, u
usz end = (usz)(aligned_pointer_to_offset - this.data.ptr) + size - offset;
if (end > total_len) return AllocationFailure.OUT_OF_MEMORY?;
this.used = end;
void *mem = aligned_pointer_to_offset - offset;
void* mem = aligned_pointer_to_offset - offset;
ArenaAllocatorHeader* header = mem - ArenaAllocatorHeader.sizeof;
header.size = size;
return mem;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/allocators/heap_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module std::core::mem::allocator;

typedef MemoryAllocFn = fn char[]!(usz);
def MemoryAllocFn = fn char[]!(usz);

struct SimpleHeapAllocator
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/allocators/tracking_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module std::core::mem::allocator;
import std::collections::map;

typedef PtrMap = HashMap<uptr, usz>;
def PtrMap = HashMap<uptr, usz>;

// A simple tracking allocator.
// It tracks allocations using a hash map but
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn void default_panic(String message, String file, String function, uint line)
$$trap();
}

typedef PanicFn = fn void(String message, String file, String function, uint line);
def PanicFn = fn void(String message, String file, String function, uint line);

PanicFn panic = &default_panic;

Expand Down
60 changes: 30 additions & 30 deletions lib/std/core/cinterop.c3
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,70 @@ $assert C_LONG_SIZE <= C_LONG_LONG_SIZE;

$switch ($$C_INT_SIZE)
$case 64:
typedef CInt = long;
typedef CUInt = ulong;
def CInt = long;
def CUInt = ulong;
$case 32:
typedef CInt = int;
typedef CUInt = uint;
def CInt = int;
def CUInt = uint;
$case 16:
typedef CInt = short;
typedef CUInt = ushort;
def CInt = short;
def CUInt = ushort;
$default:
$error "Invalid C int size";
$endswitch

$switch ($$C_LONG_SIZE)
$case 64:
typedef CLong = long;
typedef CULong = ulong;
def CLong = long;
def CULong = ulong;
$case 32:
typedef CLong = int;
typedef CULong = uint;
def CLong = int;
def CULong = uint;
$case 16:
typedef CLong = short;
typedef CULong = ushort;
def CLong = short;
def CULong = ushort;
$default:
$error "Invalid C long size";
$endswitch

$switch ($$C_SHORT_SIZE)
$case 32:
typedef CShort = int;
typedef CUShort = uint;
def CShort = int;
def CUShort = uint;
$case 16:
typedef CShort = short;
typedef CUShort = ushort;
def CShort = short;
def CUShort = ushort;
$case 8:
typedef CShort = ichar;
typedef CUShort = char;
def CShort = ichar;
def CUShort = char;
$default:
$error "Invalid C short size";
$endswitch

$switch ($$C_LONG_LONG_SIZE)
$case 128:
typedef CLongLong = int128;
typedef CULongLong = uint128;
def CLongLong = int128;
def CULongLong = uint128;
$case 64:
typedef CLongLong = long;
typedef CULongLong = ulong;
def CLongLong = long;
def CULongLong = ulong;
$case 32:
typedef CLongLong = int;
typedef CULongLong = uint;
def CLongLong = int;
def CULongLong = uint;
$case 16:
typedef CLongLong = short;
typedef CULongLong = ushort;
def CLongLong = short;
def CULongLong = ushort;
$default:
$error "Invalid C long long size";
$endswitch



typedef CSChar = ichar;
typedef CUChar = char;
def CSChar = ichar;
def CUChar = char;

$if $$C_CHAR_IS_SIGNED:
typedef CChar = ichar;
def CChar = ichar;
$else
typedef CChar = char;
def CChar = char;
$endif
2 changes: 1 addition & 1 deletion lib/std/core/dstring.c3
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module std::core::dstring;

typedef DString = distinct void*;
def DString = distinct void*;

const usz MIN_CAPACITY @private = 16;

Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/mem_allocator.c3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DEFAULT_SIZE_PREFIX_ALIGNMENT = usz.alignof;
const Allocator* NULL_ALLOCATOR = &_NULL_ALLOCATOR;
const Allocator* LIBC_ALLOCATOR = &_SYSTEM_ALLOCATOR;

typedef AllocatorFunction = fn void*!(Allocator* allocator, usz new_size, usz alignment, usz offset, void* old_pointer, AllocationKind kind);
def AllocatorFunction = fn void*!(Allocator* allocator, usz new_size, usz alignment, usz offset, void* old_pointer, AllocationKind kind);

struct Allocator
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/runtime.c3
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct SubArrayContainer
usz len;
}

typedef TestFn = fn void!();
def TestFn = fn void!();

struct TestRunner
{
Expand Down
30 changes: 27 additions & 3 deletions lib/std/core/string.c3
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module std::core::string;
import std::ascii;

typedef ZString = distinct inline char*;
typedef Char32 = uint;
typedef Char16 = ushort;
def ZString = distinct inline char*;
def Char32 = uint;
def Char16 = ushort;

fault UnicodeResult
{
Expand Down Expand Up @@ -361,6 +361,30 @@ fn Char32[]! String.to_utf32(String s, Allocator* using = mem::heap())
return data[:codepoints];
}

fn void String.convert_ascii_to_lower(String s)
{
foreach (&c : s) if (*c >= 'A' && *c <= 'Z') *c += 'a' - 'A';
}

fn String String.ascii_to_lower(String s, Allocator* using = mem::heap())
{
String copy = s.copy(using);
copy.convert_ascii_to_lower();
return copy;
}

fn void String.convert_ascii_to_upper(String s)
{
foreach (&c : s) if (*c >= 'a' && *c <= 'z') *c -= 'a' - 'A';
}

fn String String.ascii_to_upper(String s, Allocator* using = mem::heap())
{
String copy = s.copy(using);
copy.convert_ascii_to_upper();
return copy;
}

fn String! from_utf32(Char32[] utf32, Allocator* using = mem::heap())
{
usz len = conv::utf8len_for_utf32(utf32);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/hash/fnv32a.c3
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// a copy of which can be found in the LICENSE_STDLIB file.
module std::hash::fnv32a;

typedef Fnv32a = distinct uint;
def Fnv32a = distinct uint;

const FNV32A_START @private = 0x811c9dc5;
const FNV32A_MUL @private = 0x01000193;
Expand Down
12 changes: 6 additions & 6 deletions lib/std/io/io_printf.c3
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fault FormattingFault
INVALID_FORMAT_TYPE,
}

typedef OutputFn = fn void!(char c, void* buffer);
typedef ToStringFunction = fn String(void* value, Allocator *using);
typedef ToFormatFunction = fn void!(void* value, Formatter* formatter);
typedef FloatType = double;
def OutputFn = fn void!(char c, void* buffer);
def ToStringFunction = fn String(void* value, Allocator *using);
def ToFormatFunction = fn void!(void* value, Formatter* formatter);
def FloatType = double;

fn usz! printf(String format, args...) @maydiscard
{
Expand Down Expand Up @@ -472,8 +472,8 @@ fn usz! Formatter.vprintf(Formatter* this, String format, any[] anys)
return this.idx;
}

typedef StringFunctionMap @private = HashMap<typeid, ToStringFunction>;
typedef FormatterFunctionMap @private = HashMap<typeid, ToFormatFunction>;
def StringFunctionMap @private = HashMap<typeid, ToStringFunction>;
def FormatterFunctionMap @private = HashMap<typeid, ToFormatFunction>;

FormatterFunctionMap toformat_functions @private;
StringFunctionMap tostring_functions @private;
26 changes: 13 additions & 13 deletions lib/std/io/io_stream.c3
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module std::io;

typedef CloseStreamFn = fn void!(Stream*);
typedef FlushStreamFn = fn void!(Stream*);
typedef SeekStreamFn = fn usz!(Stream*, isz offset, Seek seek);
typedef LenStreamFn = fn usz(Stream*);
typedef AvailableStreamFn = fn usz(Stream*);
typedef ReadStreamFn = fn usz!(Stream*, char[] bytes);
typedef ReadFromStreamFn = fn usz!(Stream*, Stream*);
typedef ReadByteStreamFn = fn char!(Stream*);
typedef PushbackByteStreamFn = fn void!(Stream*);
typedef WriteStreamFn = fn usz!(Stream*, char[] bytes);
typedef WriteToStreamFn = fn usz!(Stream*, Stream* out);
typedef WriteByteStreamFn = fn void!(Stream*, char c);
typedef DestroyStreamFn = fn void!(Stream*);
def CloseStreamFn = fn void!(Stream*);
def FlushStreamFn = fn void!(Stream*);
def SeekStreamFn = fn usz!(Stream*, isz offset, Seek seek);
def LenStreamFn = fn usz(Stream*);
def AvailableStreamFn = fn usz(Stream*);
def ReadStreamFn = fn usz!(Stream*, char[] bytes);
def ReadFromStreamFn = fn usz!(Stream*, Stream*);
def ReadByteStreamFn = fn char!(Stream*);
def PushbackByteStreamFn = fn void!(Stream*);
def WriteStreamFn = fn usz!(Stream*, char[] bytes);
def WriteToStreamFn = fn usz!(Stream*, Stream* out);
def WriteByteStreamFn = fn void!(Stream*, char c);
def DestroyStreamFn = fn void!(Stream*);

struct StreamInterface
{
Expand Down
14 changes: 7 additions & 7 deletions lib/std/io/os/file.c3
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module std::io::os;
import libc;

typedef FopenFn = fn void*!(String, String);
typedef FreopenFn = fn void*!(void*, String, String);
typedef FcloseFn = fn void!(void*);
typedef FseekFn = fn void!(void*, isz, Seek);
typedef FtellFn = fn usz!(void*);
typedef FwriteFn = fn usz!(void*, char[] buffer);
typedef FreadFn = fn usz!(void*, char[] buffer);
def FopenFn = fn void*!(String, String);
def FreopenFn = fn void*!(void*, String, String);
def FcloseFn = fn void!(void*);
def FseekFn = fn void!(void*, isz, Seek);
def FtellFn = fn usz!(void*);
def FwriteFn = fn usz!(void*, char[] buffer);
def FreadFn = fn usz!(void*, char[] buffer);

$if !$defined(native_fopen_fn):
FopenFn native_fopen_fn @weak;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/path.c3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const char PREFERRED_SEPARATOR_WIN32 = '\\';
const char PREFERRED_SEPARATOR_POSIX = '/';
const char PREFERRED_SEPARATOR = env::os_is_win32() ? PREFERRED_SEPARATOR_WIN32 : PREFERRED_SEPARATOR_POSIX;

typedef PathList = List<Path>;
def PathList = List<Path>;

fault PathResult
{
Expand Down
Loading

0 comments on commit ddd0497

Please sign in to comment.