From 2aa6def9bdd2f0ac8554ab67bda25e8b53e0fec3 Mon Sep 17 00:00:00 2001 From: wilkie Date: Sun, 9 May 2010 02:01:33 -0400 Subject: [PATCH 01/14] Trying hard to be tangoless --- binding/c.d | 183 ++++++++++++++++++++++++++++++++++++++--- math/common.d | 109 +++++++++++++++++++----- platform/unix/common.d | 1 - runtime/dstatic.d | 1 - runtime/object.d | 2 - 5 files changed, 262 insertions(+), 34 deletions(-) diff --git a/binding/c.d b/binding/c.d index 5b1fba42..674cb3e9 100644 --- a/binding/c.d +++ b/binding/c.d @@ -8,6 +8,8 @@ * */ +module binding.c; + /* C long types */ version(GNU) { @@ -26,23 +28,184 @@ else { /* stdarg */ -version(Tango) { - public import tango.stdc.stdarg; - public import tango.stdc.stdio : wchar_t, stdout, _iobuf, FILE, fpos_t; - - extern(C) int printf(char *,...); /// +version(GNU) { + public import std.c.stdarg; +} +else version(LDC) { + public import ldc.cstdarg; } else { - public import std.c.stdarg; - version(GNU) { - public import gcc.config.libc; - alias gcc.config.libc.fpos_t fpos_t; + static assert(false, "Compiler not supported."); + // public import std.c.stdarg +} + +/* stdout */ + +align(1) struct _iobuf { + version( Win32 ) { + char* _ptr; + int _cnt; + char* _base; + int _flag; + int _file; + int _charbuf; + int _bufsiz; + int __tmpnum; + } + else version( linux ) { + char* _read_ptr; + char* _read_end; + char* _read_base; + char* _write_base; + char* _write_ptr; + char* _write_end; + char* _buf_base; + char* _buf_end; + char* _save_base; + char* _backup_base; + char* _save_end; + void* _markers; + _iobuf* _chain; + int _fileno; + int _blksize; + int _old_offset; + ushort _cur_column; + byte _vtable_offset; + char[1] _shortbuf; + void* _lock; + } + else version( darwin ) { + ubyte* _p; + int _r; + int _w; + short _flags; + short _file; + __sbuf _bf; + int _lbfsize; + + int* function(void*) _close; + int* function(void*, char*, int) _read; + fpos_t* function(void*, fpos_t, int) _seek; + int* function(void*, char *, int) _write; + + __sbuf _ub; + __sFILEX* _extra; + int _ur; + + ubyte[3] _ubuf; + ubyte[1] _nbuf; + + __sbuf _lb; + + int _blksize; + fpos_t _offset; + } + else version( freebsd ) { + ubyte* _p; + int _r; + int _w; + short _flags; + short _file; + __sbuf _bf; + int _lbfsize; + + void* function() _cookie; + int* function(void*) _close; + int* function(void*, char*, int) _read; + fpos_t* function(void*, fpos_t, int) _seek; + int* function(void*, char *, int) _write; + + __sbuf _ub; + __sFILEX* _extra; + int _ur; + + ubyte[3] _ubuf; + ubyte[1] _nbuf; + + __sbuf _lb; + + int _blksize; + fpos_t _offset; + } + else version( solaris ) { + // From OpenSolaris + ubyte* _next; /* next position to read/write from */ + ubyte* _endw; /* end of write buffer */ + ubyte* _endr; /* end of read buffer */ + ubyte* _endb; /* end of buffer */ + _iobuf* _push; /* the stream that was pushed on */ + ushort _flags; /* type of stream */ + short _file; /* file descriptor */ + ubyte* _data; /* base of data buffer */ + ptrdiff_t _size; /* buffer size */ + ptrdiff_t _val; /* values or string lengths */ + + // #ifdef _SFIO_PRIVATE + // .. I don't think we really need this in D + // #endif } else { - public import std.c.stdio; + static assert( false, "Platform not supported." ); } } +const int _NFILE = 60; +alias _iobuf FILE; +alias int fpos_t; + +version(Win32) { + extern(C) extern FILE[_NFILE] _iob; + FILE* stdin = &_iob[0]; + FILE* stdout = &_iob[1]; + FILE* stderr = &_iob[2]; +} +else version(linux) { + extern(C) extern FILE* stdin; + extern(C) extern FILE* stdout; + extern(C) extern FILE* stderr; +} +else version(darwin) { + extern(C) extern FILE* __stdinp; + extern(C) extern FILE* __stdoutp; + extern(C) extern FILE* __stderrp; + + alias __stdinp stdin; + alias __stdoutp stdout; + alias __stderrp stderr; +} +else version(freebsd) { + extern(C) extern FILE[3] __sF; + + FILE* stdin = &__sF[0]; + FILE* stdout = &__sF[1]; + FILE* stderr = &__sF[2]; +} +else version(solaris) { + extern(C) extern FILE[_NFILE] __iob; + + FILE* stdin = &__iob[0]; + FILE* stdout = &__iob[1]; + FILE* stderr = &__iob[2]; +} +else { + static assert(false, "Platform not supported."); +} + +// wchar_t +version(Win32) { + alias ushort wchar_t; +} +else { + alias uint wchar_t; +} + +extern(C) FILE[_NFILE]* _imp__iob; + + //public import std.c.stdarg; + //public import std.c.stdio; + +extern(C) int printf(char *,...); + const int EOF = -1; const int FOPEN_MAX = 16; const int FILENAME_MAX = 4095; diff --git a/math/common.d b/math/common.d index c1d7e658..a864b6f6 100644 --- a/math/common.d +++ b/math/common.d @@ -20,30 +20,99 @@ template _mathFunc(string func) { `; } -mixin(_mathFunc!("sqrt")); +// Description: Will resolve to true when the object T inherits or is of type +// MathObject. +template IsMathObject(T) { + static if (T.stringof == "MathObject") { + const bool IsMathObject = true; + } + else static if (T == Object) { + const bool IsMathObject = false; + } + else { + const bool IsMathObject = IsMathObject!(Super!(T)); + } +} + +// Description: Will resolve to true when the type T can be used as input into +// a math function. +template IsMathType(T) { + static if (is(T : creal) || is(T : real) || is(T : double) || is(T : float) || IsIntType!(T) || IsMathObject!(T)) { + const bool IsMathType = true; + } + else { + const bool IsMathType = false; + } +} -// Abstract standard library (silliness) -version(Tango) { - // Tango - public import tango.math.Math; +template sqrt(T) { + static assert(IsMathType!(T), T.stringof ~ " cannot be used as input to this function."); + T sqrt(T x) { + static if (IsMathObject!(T)) { + return x.opSqrt(); + } + else static if (is(T : int)) { + float temp; + float x, y, rr; + + int ret; - // OK. DMD has an issue when you redefine intrinsics... - // that is, it forgets they exist. - // Eventually, this will cause an error. therefore, + rr = x; + y = rr * 0.5; + *cast(uint*)&temp = (0xbe6f0000 - *cast(uint*)&rr) >> 1; + x = temp; + x = (1.5 * x) - (x * x) * (x * y); - // XXX: REMOVE when compiler is fixed - private import Math = tango.math.Math; -} -else { - // Phobos - public import std.math; + if (r > 101123) { + x = (1.5 * x) - (x * x) * (x * y); + } + + ret = cast(int)((x * rr) + 0.5); - // OK. DMD has an issue when you redefine intrinsics... - // that is, it forgets they exist. - // Eventually, this will cause an error. therefore, + return ret; + } + else static if (is(T : creal)) { + if (x == 0.0) { + return x; + } + + real a,b; + a = fabs(x.re); + b = fabs(x.im); + + real y,z; + if (a >= b) { + y = b / a; + y *= y; + z = 1 + sqrt(1+y); + z *= 0.5; + z = sqrt(z); + z *= sqrt(a); + } + else { + y = a / b; + z = sqrt(1 + (y*y)); + z += y; + z = sqrt(0.5 * z); + z *= sqrt(b); + } - // XXX: REMOVE when compiler is fixed - private import Math = std.math; + creal ret; + if (z.re >= 0) { + c = z + (x.im / (z + z)) * 1.0i; + } + else { + if (x.im < 0) { + z = -z; + } + c = (x.im / (z + z)) + (z * 1.0i); + } + + return ret; + } + else { + // double, float, real + } + } } -alias Math.sqrt sqrt; diff --git a/platform/unix/common.d b/platform/unix/common.d index b8aa6bd5..e8d0473c 100644 --- a/platform/unix/common.d +++ b/platform/unix/common.d @@ -18,7 +18,6 @@ public import CairoX = binding.cairo.xlib; public import Pango = binding.pango.pango; -public import binding.c; public import Curses = binding.ncurses.ncurses; extern(C): diff --git a/runtime/dstatic.d b/runtime/dstatic.d index 0db85492..5e11d2cc 100644 --- a/runtime/dstatic.d +++ b/runtime/dstatic.d @@ -7,7 +7,6 @@ module runtime.dstatic; -import runtime.util; import runtime.common; extern(C): diff --git a/runtime/object.d b/runtime/object.d index cf8cb3d9..eb6984c1 100644 --- a/runtime/object.d +++ b/runtime/object.d @@ -8,9 +8,7 @@ module object; // Imports necessary routines used by the runtime -import runtime.util; import runtime.dstatic; -import runtime.dstubs; import runtime.exception; import runtime.error; From b06a8c6aadabe2586584e9f2cef7964266eac206 Mon Sep 17 00:00:00 2001 From: wilkie Date: Sun, 9 May 2010 04:14:47 -0400 Subject: [PATCH 02/14] Using binding.c for much of the Linux scaffold now. --- platform/unix/common.d | 1 + platform/unix/platform/vars/file.d | 2 ++ platform/unix/scaffold/console.d | 1 + platform/unix/scaffold/cui.d | 2 ++ platform/unix/scaffold/file.d | 2 ++ platform/unix/scaffold/time.d | 2 ++ winsamp.d | 2 ++ 7 files changed, 12 insertions(+) diff --git a/platform/unix/common.d b/platform/unix/common.d index e8d0473c..1c2c7c8e 100644 --- a/platform/unix/common.d +++ b/platform/unix/common.d @@ -18,6 +18,7 @@ public import CairoX = binding.cairo.xlib; public import Pango = binding.pango.pango; +import binding.c; public import Curses = binding.ncurses.ncurses; extern(C): diff --git a/platform/unix/platform/vars/file.d b/platform/unix/platform/vars/file.d index a3133882..39db0b34 100644 --- a/platform/unix/platform/vars/file.d +++ b/platform/unix/platform/vars/file.d @@ -12,6 +12,8 @@ module platform.vars.file; import platform.unix.common; +import binding.c; + struct FilePlatformVars { FILE* file; } diff --git a/platform/unix/scaffold/console.d b/platform/unix/scaffold/console.d index 9e9839e7..08fb6368 100644 --- a/platform/unix/scaffold/console.d +++ b/platform/unix/scaffold/console.d @@ -13,6 +13,7 @@ import djehuty; import platform.unix.common; import platform.unix.main; +import binding.c; import platform.application; diff --git a/platform/unix/scaffold/cui.d b/platform/unix/scaffold/cui.d index 76b96dd0..64b938d0 100644 --- a/platform/unix/scaffold/cui.d +++ b/platform/unix/scaffold/cui.d @@ -20,6 +20,8 @@ import core.definitions; import platform.application; +import binding.c; + void CuiStart(CuiPlatformVars* vars) { Curses.savetty(); diff --git a/platform/unix/scaffold/file.d b/platform/unix/scaffold/file.d index bbc55e81..b63df12e 100644 --- a/platform/unix/scaffold/file.d +++ b/platform/unix/scaffold/file.d @@ -13,6 +13,8 @@ import platform.unix.common; import platform.vars.file; +import binding.c; + import core.definitions; import core.string; import core.main; diff --git a/platform/unix/scaffold/time.d b/platform/unix/scaffold/time.d index ced7f2f2..15cd2b28 100644 --- a/platform/unix/scaffold/time.d +++ b/platform/unix/scaffold/time.d @@ -17,6 +17,8 @@ import core.string; import core.date; import core.time; +import binding.c; + // Timing private { struct tm { diff --git a/winsamp.d b/winsamp.d index 98b763dd..9dd7e541 100644 --- a/winsamp.d +++ b/winsamp.d @@ -70,6 +70,8 @@ class MyConsoleApp : Application { static this() { new MyConsoleApp(); } override void onApplicationStart() { + Console.putln(sin(0)); + Console.putln(cos(3.1415296)); } override bool onSignal(Dispatcher dsp, uint signal) { From b4976748d7325862e9dfa94ef5232b97593f2c3b Mon Sep 17 00:00:00 2001 From: wilkie Date: Wed, 12 May 2010 17:17:11 -0400 Subject: [PATCH 03/14] Fixed array allocation. Made the garbage collector simply wrap malloc. Tested out array allocation, and it seems to work. Have C main call the D main. --- Makefile | 15 +- binding/c.d | 12 +- binding/cairo/cairo.d | 6 +- binding/ncurses/ncurses.d | 6 +- binding/x/X.d | 19 +- binding/x/Xlib.d | 210 +++++++-------- compiler/ldc/cstdarg.di | 29 ++ compiler/ldc/vararg.d | 43 +++ core/color.d | 197 +++----------- core/definitions.d | 247 ++++++++---------- core/error.d | 8 +- core/event.d | 4 +- core/exception.d | 6 +- core/parameters.d | 17 -- djehuty.d | 3 +- math/common.d | 8 + platform/unix/main.d | 17 +- platform/unix/platform/definitions.d | 29 +- platform/unix/platform/vars/thread.d | 3 + platform/unix/scaffold/thread.d | 30 +-- runtime/array.d | 7 +- runtime/assocarray.d | 46 ++-- runtime/classinfo.d | 39 --- runtime/error.d | 5 +- runtime/exception.d | 7 + runtime/gc.d | 18 +- runtime/lifetime.d | 24 +- runtime/main.d | 5 + runtime/moduleinfo.d | 14 +- runtime/monitor.d | 6 + runtime/object.d | 152 +++++++++-- runtime/typeinfo.d | 4 +- runtime/{typeinfo => typeinfos}/ti_array.d | 12 +- .../{typeinfo => typeinfos}/ti_array_bool.d | 4 +- .../{typeinfo => typeinfos}/ti_array_byte.d | 4 +- .../ti_array_cdouble.d | 4 +- .../{typeinfo => typeinfos}/ti_array_cfloat.d | 4 +- .../{typeinfo => typeinfos}/ti_array_char.d | 4 +- .../{typeinfo => typeinfos}/ti_array_creal.d | 4 +- .../{typeinfo => typeinfos}/ti_array_dchar.d | 4 +- .../{typeinfo => typeinfos}/ti_array_double.d | 4 +- .../{typeinfo => typeinfos}/ti_array_float.d | 4 +- .../ti_array_idouble.d | 4 +- .../{typeinfo => typeinfos}/ti_array_ifloat.d | 4 +- .../{typeinfo => typeinfos}/ti_array_int.d | 4 +- .../{typeinfo => typeinfos}/ti_array_ireal.d | 4 +- .../{typeinfo => typeinfos}/ti_array_long.d | 4 +- .../{typeinfo => typeinfos}/ti_array_object.d | 2 +- .../{typeinfo => typeinfos}/ti_array_real.d | 4 +- .../{typeinfo => typeinfos}/ti_array_short.d | 4 +- .../{typeinfo => typeinfos}/ti_array_ubyte.d | 4 +- .../{typeinfo => typeinfos}/ti_array_uint.d | 4 +- .../{typeinfo => typeinfos}/ti_array_ulong.d | 4 +- .../{typeinfo => typeinfos}/ti_array_ushort.d | 4 +- .../{typeinfo => typeinfos}/ti_array_void.d | 4 +- .../{typeinfo => typeinfos}/ti_array_wchar.d | 4 +- .../{typeinfo => typeinfos}/ti_assocarray.d | 2 + runtime/{typeinfo => typeinfos}/ti_byte.d | 2 +- runtime/{typeinfo => typeinfos}/ti_cdouble.d | 2 +- runtime/{typeinfo => typeinfos}/ti_cfloat.d | 2 +- runtime/{typeinfo => typeinfos}/ti_char.d | 2 +- runtime/{typeinfo => typeinfos}/ti_creal.d | 2 +- runtime/{typeinfo => typeinfos}/ti_dchar.d | 2 +- runtime/{typeinfo => typeinfos}/ti_delegate.d | 2 +- runtime/{typeinfo => typeinfos}/ti_double.d | 10 +- runtime/{typeinfo => typeinfos}/ti_enum.d | 4 +- runtime/{typeinfo => typeinfos}/ti_float.d | 4 +- runtime/{typeinfo => typeinfos}/ti_function.d | 2 +- runtime/{typeinfo => typeinfos}/ti_idouble.d | 4 +- runtime/{typeinfo => typeinfos}/ti_ifloat.d | 4 +- runtime/{typeinfo => typeinfos}/ti_int.d | 2 +- .../{typeinfo => typeinfos}/ti_interface.d | 2 + runtime/{typeinfo => typeinfos}/ti_ireal.d | 4 +- runtime/{typeinfo => typeinfos}/ti_long.d | 2 +- runtime/{typeinfo => typeinfos}/ti_object.d | 2 +- runtime/{typeinfo => typeinfos}/ti_ptr.d | 2 +- runtime/{typeinfo => typeinfos}/ti_real.d | 4 +- runtime/{typeinfo => typeinfos}/ti_short.d | 2 +- .../{typeinfo => typeinfos}/ti_staticarray.d | 4 +- runtime/{typeinfo => typeinfos}/ti_struct.d | 4 +- runtime/{typeinfo => typeinfos}/ti_tuple.d | 2 +- runtime/{typeinfo => typeinfos}/ti_typedef.d | 2 +- runtime/{typeinfo => typeinfos}/ti_ubyte.d | 2 +- runtime/{typeinfo => typeinfos}/ti_uint.d | 2 +- runtime/{typeinfo => typeinfos}/ti_ulong.d | 2 +- runtime/{typeinfo => typeinfos}/ti_ushort.d | 2 +- runtime/{typeinfo => typeinfos}/ti_void.d | 2 +- runtime/{typeinfo => typeinfos}/ti_wchar.d | 2 +- runtime/types.d | 24 -- runtime/util.d | 62 +++++ synch/thread.d | 135 +--------- winsamp.d | 5 +- 92 files changed, 797 insertions(+), 855 deletions(-) create mode 100644 compiler/ldc/cstdarg.di create mode 100644 compiler/ldc/vararg.d delete mode 100644 core/parameters.d delete mode 100644 runtime/classinfo.d rename runtime/{typeinfo => typeinfos}/ti_array.d (95%) rename runtime/{typeinfo => typeinfos}/ti_array_bool.d (72%) rename runtime/{typeinfo => typeinfos}/ti_array_byte.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_cdouble.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_cfloat.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_char.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_creal.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_dchar.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_double.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_float.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_idouble.d (72%) rename runtime/{typeinfo => typeinfos}/ti_array_ifloat.d (72%) rename runtime/{typeinfo => typeinfos}/ti_array_int.d (62%) rename runtime/{typeinfo => typeinfos}/ti_array_ireal.d (72%) rename runtime/{typeinfo => typeinfos}/ti_array_long.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_object.d (97%) rename runtime/{typeinfo => typeinfos}/ti_array_real.d (94%) rename runtime/{typeinfo => typeinfos}/ti_array_short.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_ubyte.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_uint.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_ulong.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_ushort.d (63%) rename runtime/{typeinfo => typeinfos}/ti_array_void.d (72%) rename runtime/{typeinfo => typeinfos}/ti_array_wchar.d (63%) rename runtime/{typeinfo => typeinfos}/ti_assocarray.d (93%) rename runtime/{typeinfo => typeinfos}/ti_byte.d (94%) rename runtime/{typeinfo => typeinfos}/ti_cdouble.d (96%) rename runtime/{typeinfo => typeinfos}/ti_cfloat.d (96%) rename runtime/{typeinfo => typeinfos}/ti_char.d (94%) rename runtime/{typeinfo => typeinfos}/ti_creal.d (96%) rename runtime/{typeinfo => typeinfos}/ti_dchar.d (94%) rename runtime/{typeinfo => typeinfos}/ti_delegate.d (93%) rename runtime/{typeinfo => typeinfos}/ti_double.d (84%) rename runtime/{typeinfo => typeinfos}/ti_enum.d (63%) rename runtime/{typeinfo => typeinfos}/ti_float.d (94%) rename runtime/{typeinfo => typeinfos}/ti_function.d (92%) rename runtime/{typeinfo => typeinfos}/ti_idouble.d (70%) rename runtime/{typeinfo => typeinfos}/ti_ifloat.d (70%) rename runtime/{typeinfo => typeinfos}/ti_int.d (95%) rename runtime/{typeinfo => typeinfos}/ti_interface.d (97%) rename runtime/{typeinfo => typeinfos}/ti_ireal.d (71%) rename runtime/{typeinfo => typeinfos}/ti_long.d (95%) rename runtime/{typeinfo => typeinfos}/ti_object.d (95%) rename runtime/{typeinfo => typeinfos}/ti_ptr.d (95%) rename runtime/{typeinfo => typeinfos}/ti_real.d (94%) rename runtime/{typeinfo => typeinfos}/ti_short.d (94%) rename runtime/{typeinfo => typeinfos}/ti_staticarray.d (96%) rename runtime/{typeinfo => typeinfos}/ti_struct.d (96%) rename runtime/{typeinfo => typeinfos}/ti_tuple.d (95%) rename runtime/{typeinfo => typeinfos}/ti_typedef.d (95%) rename runtime/{typeinfo => typeinfos}/ti_ubyte.d (94%) rename runtime/{typeinfo => typeinfos}/ti_uint.d (95%) rename runtime/{typeinfo => typeinfos}/ti_ulong.d (95%) rename runtime/{typeinfo => typeinfos}/ti_ushort.d (94%) rename runtime/{typeinfo => typeinfos}/ti_void.d (94%) rename runtime/{typeinfo => typeinfos}/ti_wchar.d (94%) create mode 100644 runtime/util.d diff --git a/Makefile b/Makefile index bd09cf05..785f8c5a 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,9 @@ DFLAGS = # can be changed PLATFORM = WINDOWS -LFLAGS_LINUX = -Iplatform/unix -L-lX11 -L-lc -L-lm -L-lrt -L-lcairo -L-lpango-1.0 -L-lpangocairo-1.0 -L-lGL -L-llua5.1 -L-lncurses -J./tests -LFLAGS_MAC = -lobjc -framework Cocoa -framework Foundation -framework OpenGL -lncurses -llua5.1 -LFLAGS_WIN = -Iplatform/win +LFLAGS_LINUX = -Iplatform/unix -Icompiler -L-lX11 -L-lc -L-lm -L-lrt -L-lcairo -L-lpango-1.0 -L-lpangocairo-1.0 -L-lGL -L-llua5.1 -L-lncurses -J./tests +LFLAGS_MAC = -lobjc -framework Cocoa -framework Foundation -framework OpenGL -lncurses -llua5.1 -Icompiler +LFLAGS_WIN = -Iplatform/win -Icompiler ifeq (${MY_ARCH},MINGW32_NT-5.1) OBJEXT = .obj @@ -33,16 +33,17 @@ endif DFILES_PLATFORM_MAC = platform/osx/platform/application.d platform/osx/scaffold/system.d platform/osx/scaffold/thread.d platform/osx/scaffold/time.d platform/osx/scaffold/console.d platform/osx/platform/definitions.d platform/osx/common.d platform/osx/main.d platform/osx/scaffold/opengl.d platform/osx/scaffold/graphics.d platform/osx/scaffold/file.d platform/osx/scaffold/socket.d platform/osx/scaffold/window.d platform/osx/scaffold/color.d platform/osx/scaffold/menu.d platform/osx/scaffold/wave.d platform/osx/scaffold/view.d platform/osx/scaffold/directory.d platform/osx/gui/apploop.d binding/c.d binding/ncurses/ncurses.d platform/osx/scaffold/cui.d platform/osx/platform/vars/cui.d platform/osx/platform/vars/file.d platform/unix/common.d platform/osx/platform/vars/thread.d platform/osx/platform/vars/directory.d platform/osx/platform/vars/view.d platform/osx/platform/vars/semaphore.d platform/osx/platform/vars/condition.d platform/osx/platform/vars/window.d OBJC_FILES = platform/osx/objc/window.m platform/osx/objc/app.m platform/osx/objc/view.m -DFILES_PLATFORM_UNIX = platform/unix/platform/application.d platform/unix/scaffold/system.d platform/unix/scaffold/thread.d platform/unix/scaffold/time.d platform/unix/scaffold/console.d platform/unix/platform/definitions.d platform/unix/common.d binding/cairo/cairo.d binding/x/Xlib.d binding/x/X.d platform/unix/main.d platform/unix/scaffold/opengl.d platform/unix/scaffold/graphics.d platform/unix/scaffold/file.d platform/unix/scaffold/socket.d platform/unix/scaffold/window.d platform/unix/scaffold/color.d platform/unix/scaffold/menu.d platform/unix/scaffold/wave.d platform/unix/scaffold/view.d platform/unix/scaffold/directory.d platform/unix/gui/apploop.d platform/unix/gui/osbutton.d binding/c.d binding/ncurses/ncurses.d platform/unix/scaffold/cui.d platform/unix/platform/vars/cui.d +DFILES_PLATFORM_UNIX = platform/unix/platform/application.d platform/unix/scaffold/system.d platform/unix/scaffold/thread.d platform/unix/scaffold/time.d platform/unix/scaffold/console.d platform/unix/platform/definitions.d platform/unix/common.d binding/cairo/cairo.d binding/x/Xlib.d binding/x/X.d platform/unix/main.d platform/unix/scaffold/opengl.d platform/unix/scaffold/graphics.d platform/unix/scaffold/file.d platform/unix/scaffold/socket.d platform/unix/scaffold/window.d platform/unix/scaffold/color.d platform/unix/scaffold/menu.d platform/unix/scaffold/wave.d platform/unix/scaffold/view.d platform/unix/scaffold/directory.d platform/unix/gui/apploop.d platform/unix/gui/osbutton.d binding/c.d binding/ncurses/ncurses.d platform/unix/scaffold/cui.d platform/unix/platform/vars/cui.d platform/unix/platform/vars/wave.d platform/unix/platform/vars/window.d platform/unix/platform/vars/menu.d platform/unix/platform/vars/view.d platform/unix/platform/vars/region.d platform/unix/platform/vars/brush.d platform/unix/platform/vars/pen.d platform/unix/platform/vars/font.d binding/pango/pango.d platform/unix/platform/vars/directory.d platform/unix/platform/vars/file.d platform/unix/platform/vars/thread.d platform/unix/platform/vars/mutex.d platform/unix/platform/vars/semaphore.d platform/unix/platform/vars/library.d platform/unix/platform/vars/socket.d platform/unix/platform/vars/condition.d binding/cairo/xlib.d binding/cairo/features.d binding/pango/font.d binding/pango/types.d binding/pango/context.d binding/pango/pbreak.d binding/pango/engine.d binding/pango/fontset.d binding/pango/coverage.d binding/pango/glyph.d binding/pango/matrix.d binding/pango/attributes.d binding/pango/layout.d binding/pango/cairo.d binding/pango/script.d binding/pango/gravity.d binding/pango/fontmap.d binding/pango/item.d binding/pango/tabs.d binding/pango/glyphitem.d compiler/ldc/vararg.d compiler/ldc/cstdarg.di DFILES_PLATFORM_WIN = binding/win32/gdipluscolormatrix.d binding/win32/gdiplusinit.d binding/win32/gdiplusmem.d binding/win32/gdiplusbase.d binding/win32/gdiplusflat.d binding/win32/gdiplusstringformat.d binding/win32/gdiplusmetafile.d binding/win32/gdipluslinecaps.d binding/win32/gdiplusimagecodec.d binding/win32/gdiplusgpstubs.d binding/win32/gdiplusfontfamily.d binding/win32/gdiplusfontcollection.d binding/win32/gdiplusfont.d binding/win32/gdiplusenums.d binding/win32/gdiplustypes.d binding/win32/gdiplusregion.d binding/win32/gdipluscolor.d binding/win32/gdiplusbitmap.d binding/win32/gdipluseffects.d binding/win32/gdipluscachedbitmap.d binding/win32/gdipluspath.d binding/win32/gdiplusbrush.d binding/win32/gdipluspen.d binding/win32/gdiplusgraphics.d binding/win32/ws2def.d binding/win32/winsock2.d binding/win32/inaddr.d binding/win32/mmsystem.d binding/win32/wincon.d binding/win32/winbase.d binding/win32/winuser.d binding/win32/windef.d binding/win32/wingdi.d platform/win/platform/application.d platform/win/platform/vars/cui.d platform/win/scaffold/cui.d platform/win/scaffold/system.d platform/win/main.d platform/win/common.d platform/win/platform/vars/menu.d platform/win/platform/vars/view.d platform/win/platform/vars/semaphore.d platform/win/platform/vars/mutex.d platform/win/platform/vars/region.d platform/win/platform/vars/library.d platform/win/platform/vars/wave.d platform/win/platform/vars/pen.d platform/win/platform/vars/brush.d platform/win/platform/vars/window.d platform/win/platform/vars/file.d platform/win/platform/vars/directory.d platform/win/platform/vars/font.d platform/win/platform/vars/socket.d platform/win/scaffold/console.d platform/win/platform/definitions.d platform/win/scaffold/wave.d platform/win/scaffold/directory.d platform/win/scaffold/graphics.d platform/win/scaffold/thread.d platform/win/scaffold/menu.d platform/win/scaffold/window.d platform/win/scaffold/view.d platform/win/scaffold/color.d platform/win/scaffold/file.d platform/win/scaffold/socket.d platform/win/gui/osbutton.d platform/win/scaffold/time.d platform/win/widget.d platform/win/scaffold/opengl.d platform/win/widget.d platform/win/gui/apploop.d DFILES_PLATFORM_XOMB = platform/xomb/main.d platform/xomb/common.d platform/xomb/scaffold.d platform/xomb/vars.d platform/xomb/console.d platform/xomb/definitions.d platform/xomb/scaffolds/wave.d platform/xomb/scaffolds/graphics.d platform/xomb/scaffolds/thread.d platform/xomb/scaffolds/menu.d platform/xomb/scaffolds/window.d platform/xomb/scaffolds/view.d platform/xomb/scaffolds/color.d platform/xomb/scaffolds/file.d platform/xomb/scaffolds/socket.d platform/xomb/scaffolds/app.d platform/xomb/scaffolds/time.d platform/xomb/oscontrolinterface.d DFILES_ANALYZING = analyzing/debugger.d DFILES_LOCALES = locales/en_us.d locales/fr_fr.d locales/all.d -DFILES_CORE = core/date.d core/locale.d core/variant.d core/exception.d core/event.d core/library.d core/system.d core/regex.d core/arguments.d core/definitions.d core/application.d core/time.d core/timezone.d core/unicode.d core/endian.d core/stream.d core/string.d core/main.d core/color.d core/error.d +DFILES_CORE = core/date.d core/locale.d core/variant.d core/exception.d core/event.d core/library.d core/system.d core/regex.d core/arguments.d core/definitions.d core/application.d core/time.d core/timezone.d core/unicode.d core/endian.d core/stream.d core/string.d core/main.d core/color.d core/error.d core/util.d DFILES_GUI = gui/container.d gui/trackbar.d gui/radiogroup.d gui/progressbar.d gui/togglefield.d gui/listfield.d gui/listbox.d gui/vscrollbar.d gui/hscrollbar.d gui/button.d gui/textfield.d gui/window.d gui/widget.d gui/application.d DFILES_DATA = data/stack.d data/queue.d data/queue2.d data/fibonacci.d data/heap.d data/list.d data/iterable.d -DFILES_RUNTIME = #runtime/array.d runtime/apply.d #runtime/lifetime.d runtime/gc.d runtime/exception.d +DFILES_RUNTIME = runtime/dstatic.d runtime/switchstmt.d runtime/monitor.d runtime/array.d runtime/apply.d runtime/lifetime.d runtime/gc.d runtime/exception.d runtime/object.d runtime/typeinfo.d runtime/moduleinfo.d runtime/assocarray.d runtime/classinvariant.d runtime/error.d runtime/typeinfos/ti_array.d runtime/typeinfos/ti_array_bool.d runtime/typeinfos/ti_array_byte.d runtime/typeinfos/ti_array_cdouble.d runtime/typeinfos/ti_array_cfloat.d runtime/typeinfos/ti_array_char.d runtime/typeinfos/ti_array_creal.d runtime/typeinfos/ti_array_dchar.d runtime/typeinfos/ti_array_double.d runtime/typeinfos/ti_array_float.d runtime/typeinfos/ti_array_idouble.d runtime/typeinfos/ti_array_ifloat.d runtime/typeinfos/ti_array_int.d runtime/typeinfos/ti_array_ireal.d runtime/typeinfos/ti_array_long.d runtime/typeinfos/ti_array_object.d runtime/typeinfos/ti_array_real.d runtime/typeinfos/ti_array_short.d runtime/typeinfos/ti_array_ubyte.d runtime/typeinfos/ti_array_uint.d runtime/typeinfos/ti_array_ulong.d runtime/typeinfos/ti_array_ushort.d runtime/typeinfos/ti_array_void.d runtime/typeinfos/ti_array_wchar.d runtime/typeinfos/ti_assocarray.d runtime/typeinfos/ti_byte.d runtime/typeinfos/ti_cdouble.d runtime/typeinfos/ti_cfloat.d runtime/typeinfos/ti_char.d runtime/typeinfos/ti_creal.d runtime/typeinfos/ti_dchar.d runtime/typeinfos/ti_delegate.d runtime/typeinfos/ti_double.d runtime/typeinfos/ti_enum.d runtime/typeinfos/ti_float.d runtime/typeinfos/ti_function.d runtime/typeinfos/ti_idouble.d runtime/typeinfos/ti_ifloat.d runtime/typeinfos/ti_int.d runtime/typeinfos/ti_interface.d runtime/typeinfos/ti_ireal.d runtime/typeinfos/ti_long.d runtime/typeinfos/ti_object.d runtime/typeinfos/ti_ptr.d runtime/typeinfos/ti_real.d runtime/typeinfos/ti_short.d runtime/typeinfos/ti_staticarray.d runtime/typeinfos/ti_struct.d runtime/typeinfos/ti_tuple.d runtime/typeinfos/ti_typedef.d runtime/typeinfos/ti_ubyte.d runtime/typeinfos/ti_uint.d runtime/typeinfos/ti_ulong.d runtime/typeinfos/ti_ushort.d runtime/typeinfos/ti_void.d runtime/typeinfos/ti_wchar.d runtime/util.d runtime/common.d runtime/main.d + DFILES_PARSING = parsing/d/trees.d parsing/d/addexprunit.d parsing/d/andexprunit.d parsing/d/assignexprunit.d parsing/d/blockstmtunit.d parsing/d/switchstmtunit.d parsing/d/casestmtunit.d parsing/d/defaultstmtunit.d parsing/d/breakstmtunit.d parsing/d/continuestmtunit.d parsing/d/gotostmtunit.d parsing/d/returnstmtunit.d parsing/d/volatilestmtunit.d parsing/d/throwstmtunit.d parsing/d/postfixexprlistunit.d parsing/d/cmpexprunit.d parsing/d/conditionalexprunit.d parsing/d/declarationunit.d parsing/d/expressionunit.d parsing/d/importdeclunit.d parsing/d/isexprunit.d parsing/d/lexer.d parsing/d/logicalandexprunit.d parsing/d/logicalorexprunit.d parsing/d/moduledeclunit.d parsing/d/moduleunit.d parsing/d/mulexprunit.d parsing/d/nodes.d parsing/d/orexprunit.d parsing/d/parser.d parsing/d/postfixexprunit.d parsing/d/primaryexprunit.d parsing/d/shiftexprunit.d parsing/d/staticunit.d parsing/d/declaratorunit.d parsing/d/declaratorsuffixunit.d parsing/d/declaratortypeunit.d parsing/d/tokens.d parsing/d/enumdeclunit.d parsing/d/typeunit.d parsing/d/enumbodyunit.d parsing/d/aggregatedeclunit.d parsing/d/aggregatebodyunit.d parsing/d/classbodyunit.d parsing/d/templatebodyunit.d parsing/d/interfacebodyunit.d parsing/d/classdeclunit.d parsing/d/interfacedeclunit.d parsing/d/constructorunit.d parsing/d/destructorunit.d parsing/d/parameterlistunit.d parsing/d/functionbodyunit.d parsing/d/staticifunit.d parsing/d/versionunit.d parsing/d/debugunit.d parsing/d/unittestunit.d parsing/d/parameterunit.d parsing/d/basictypeunit.d parsing/d/statementunit.d parsing/d/pragmastmtunit.d parsing/d/staticassertunit.d parsing/d/foreachstmtunit.d parsing/d/scopedstmtunit.d parsing/d/forstmtunit.d parsing/d/typedeclarationunit.d parsing/d/unaryexprunit.d parsing/d/xorexprunit.d parsing/ast.d parsing/lexer.d parsing/token.d parsing/parser.d parsing/options.d parsing/cfg.d parsing/parseunit.d DFILES = djehuty.d DFILES_BINARY_CODECS = decoders/binary/decoder.d decoders/binary/base64.d decoders/binary/yEnc.d decoders/binary/deflate.d decoders/binary/zlib.d @@ -109,7 +110,7 @@ ifeq (${MY_ARCH},Darwin) else ifeq ($(PLATFORM),"WINDOWS") else - @$(DC) $< $(DFLAGS) -d-version=PlatformLinux -c -of$@ -O3 -J./tests -I./platform/unix + @$(DC) $< $(DFLAGS) -d-version=PlatformLinux -c -of$@ -O3 -J./tests -I./platform/unix -I./compiler endif endif diff --git a/binding/c.d b/binding/c.d index 674cb3e9..b5a51f95 100644 --- a/binding/c.d +++ b/binding/c.d @@ -29,10 +29,12 @@ else { /* stdarg */ version(GNU) { - public import std.c.stdarg; + private import std.c.stdarg; + alias va_list Cva_list; } else version(LDC) { - public import ldc.cstdarg; + private import ldc.cstdarg; + alias va_list Cva_list; } else { static assert(false, "Compiler not supported."); @@ -242,10 +244,10 @@ int ungetc(int,FILE *); /// size_t fread(void *,size_t,size_t,FILE *); /// size_t fwrite(void *,size_t,size_t,FILE *); /// int fprintf(FILE *,char *,...); /// -int vfprintf(FILE *,char *,va_list); /// -int vprintf(char *,va_list); /// +int vfprintf(FILE *,char *,Cva_list); /// +int vprintf(char *,Cva_list); /// int sprintf(char *,char *,...); /// -int vsprintf(char *,char *,va_list); /// +int vsprintf(char *,char *,Cva_list); /// int scanf(char *,...); /// int fscanf(FILE *,char *,...); /// int sscanf(char *,char *,...); /// diff --git a/binding/cairo/cairo.d b/binding/cairo/cairo.d index 41da36d3..127218cd 100644 --- a/binding/cairo/cairo.d +++ b/binding/cairo/cairo.d @@ -50,7 +50,7 @@ module binding.cairo.cairo; */ public import binding.cairo.features; -public import core.definitions; +import binding.c; extern (C): int cairo_version(); @@ -748,7 +748,7 @@ alias _cairo_font_face cairo_font_face_t; struct cairo_glyph_t { - Culong index; + Culong_t index; double x; double y; } @@ -1007,7 +1007,7 @@ void cairo_font_options_merge(cairo_font_options_t *options, cairo_font_options cairo_bool_t cairo_font_options_equal(cairo_font_options_t *options, cairo_font_options_t *other); -Culong cairo_font_options_hash(cairo_font_options_t *options); +Culong_t cairo_font_options_hash(cairo_font_options_t *options); void cairo_font_options_set_antialias(cairo_font_options_t *options, cairo_antialias_t antialias); diff --git a/binding/ncurses/ncurses.d b/binding/ncurses/ncurses.d index 67bee84a..98d47520 100644 --- a/binding/ncurses/ncurses.d +++ b/binding/ncurses/ncurses.d @@ -353,9 +353,9 @@ void use_env(bool); int vidattr(chtype); int vidputs(chtype, int function(int)); int vline(chtype, int); -int vwprintw(WINDOW*, char*, va_list); -int vw_printw(WINDOW*, char*, va_list); -int vwscanw(WINDOW*, char*, va_list); +int vwprintw(WINDOW*, char*,Cva_list); +int vw_printw(WINDOW*, char*, Cva_list); +int vwscanw(WINDOW*, char*, Cva_list); int waddch(WINDOW*, chtype); int waddchstr(WINDOW*, chtype*); int waddnstr(WINDOW*, char*, int); diff --git a/binding/x/X.d b/binding/x/X.d index c7872499..e4dd9e1d 100644 --- a/binding/x/X.d +++ b/binding/x/X.d @@ -18,16 +18,17 @@ */ module binding.x.X; -import core.definitions; + +import binding.c; const uint X_PROTOCOL=11; /* current protocol version */ const uint X_PROTOCOL_REVISION=0; /* current minor version */ /* Resources */ -alias Culong XID; -alias Culong Mask; -alias Culong VisualID; -alias Culong Time; +alias Culong_t XID; +alias Culong_t Mask; +alias Culong_t VisualID; +alias Culong_t Time; alias XID Atom; //alias needed because of None invariant shared for Atom and XID alias XID Window; @@ -87,7 +88,7 @@ const KeySym NoSymbol=0; /* Input Event Masks. Used as event-mask window attribute and as arguments to Grab requests. Not to be confused with event names. */ -enum EventMask:Clong +enum EventMask:Clong_t { NoEventMask =0, KeyPressMask =1<<0, @@ -385,7 +386,7 @@ enum WindowClass:int /* Window attributes for CreateWindow and ChangeWindowAttributes */ -enum WindowAttribute:Culong +enum WindowAttribute:Culong_t { CWBackPixmap =1<<0, CWBackPixel =1<<1, @@ -585,7 +586,7 @@ enum ArcMode:int }; /* GC components: masks used in CreateGC, CopyGC, ChangeGC, OR'ed into GC.stateChanges */ -enum GCMask:Culong +enum GCMask:Culong_t { GCFunction =1<<0, GCPlaneMask =1<<1, @@ -684,7 +685,7 @@ enum LedMode:int }; /* masks for ChangeKeyboardControl */ -enum KBMask:Culong +enum KBMask:Culong_t { KBKeyClickPercent =1<<0, KBBellPercent =1<<1, diff --git a/binding/x/Xlib.d b/binding/x/Xlib.d index 93922ec3..d07f143a 100644 --- a/binding/x/Xlib.d +++ b/binding/x/Xlib.d @@ -25,7 +25,7 @@ module binding.x.Xlib; public import binding.x.X; -import core.definitions; +import binding.c; const int XlibSpecificationRelease=6; version = X_HAVE_UTF8_STRING; @@ -132,9 +132,9 @@ struct XPixmapFormatValues struct XGCValues { GraphicFunction function_; /* logical operation*/ - Culong plane_mask; /* plane mask */ - Culong foreground; /* foreground pixel */ - Culong background; /* background pixel */ + Culong_t plane_mask; /* plane mask */ + Culong_t foreground; /* foreground pixel */ + Culong_t background; /* background pixel */ int line_width; /* line width */ LineStyle line_style; /* LineSolid, LineOnOffDash, LineDoubleDash */ CapStyle cap_style; /* CapNotLast, CapButt, CapRound, CapProjecting */ @@ -166,9 +166,9 @@ struct Visual XExtData *ext_data; /* hook for extension to hang data */ VisualID visualid; /* visual id of this visual */ int class_; /* class of screen (monochrome, etc.) */ - Culong red_mask; - Culong green_mask; - Culong blue_mask; /* mask values */ + Culong_t red_mask; + Culong_t green_mask; + Culong_t blue_mask; /* mask values */ int bits_per_rgb; /* log base 2 of distinct color values */ int map_entries; /* color map entries */ @@ -198,8 +198,8 @@ struct Screen{ Visual *root_visual; /* root visual */ GC default_gc; /* GC for the root root visual */ Colormap cmap; /* default color map */ - Culong white_pixel; - Culong black_pixel; /* White and Black pixel values */ + Culong_t white_pixel; + Culong_t black_pixel; /* White and Black pixel values */ int max_maps, min_maps; /* max and min color maps */ int backing_store; /* Never, WhenMapped, Always */ Bool save_unders; @@ -223,17 +223,17 @@ struct ScreenFormat struct XSetWindowAttributes { Pixmap background_pixmap; /* background or None or ParentRelative */ - Culong background_pixel; /* background pixel */ + Culong_t background_pixel; /* background pixel */ Pixmap border_pixmap; /* border of the window */ - Culong border_pixel; /* border pixel value */ + Culong_t border_pixel; /* border pixel value */ BitGravity bit_gravity; /* one of bit gravity values */ BitGravity win_gravity; /* one of the window gravity values */ BackingStoreHint backing_store; /* NotUseful, WhenMapped, Always */ - Culong backing_planes; /* planes to be preseved if possible */ - Culong backing_pixel; /* value to use in restoring planes */ + Culong_t backing_planes; /* planes to be preseved if possible */ + Culong_t backing_pixel; /* value to use in restoring planes */ Bool save_under; /* should bits under be saved? (popups) */ - Clong event_mask; /* set of events that should be saved */ - Clong do_not_propagate_mask;/* set of events that should not propagate */ + Clong_t event_mask; /* set of events that should be saved */ + Clong_t do_not_propagate_mask;/* set of events that should not propagate */ Bool override_redirect; /* Boolean value for override-redirect */ Colormap colormap; /* color map to be associated with window */ Cursor cursor; /* cursor to be displayed (or None) */ @@ -251,8 +251,8 @@ struct XWindowAttributes BitGravity bit_gravity; /* one of bit gravity values */ BitGravity win_gravity; /* one of the window gravity values */ BackingStoreHint backing_store; /* NotUseful, WhenMapped, Always */ - Culong backing_planes; /* planes to be preserved if possible */ - Culong backing_pixel; /* value to be used when restoring planes */ + Culong_t backing_planes; /* planes to be preserved if possible */ + Culong_t backing_pixel; /* value to be used when restoring planes */ Bool save_under; /* Boolean, should bits under be saved? */ Colormap colormap; /* color map to be associated with window */ Bool map_installed; /* Boolean, is color map currently installed*/ @@ -303,9 +303,9 @@ struct XImage int depth; /* depth of image */ int bytes_per_line; /* accelarator to next line */ int bits_per_pixel; /* bits per pixel (ZPixmap) */ - Culong red_mask; /* bits in z arrangment */ - Culong green_mask; - Culong blue_mask; + Culong_t red_mask; /* bits in z arrangment */ + Culong_t green_mask; + Culong_t blue_mask; XPointer obdata; /* hook for the object routines to hang on */ struct f { /* image manipulation routines */ XImage* function( @@ -320,10 +320,10 @@ struct XImage int /* bitmap_pad */, int /* bytes_per_line */) create_image; int function(XImage *)destroy_image; - Culong function(XImage *, int, int)get_pixel; - int function(XImage *, int, int, Culong)put_pixel; + Culong_t function(XImage *, int, int)get_pixel; + int function(XImage *, int, int, Culong_t)put_pixel; XImage function(XImage *, int, int, uint, uint)sub_image; - int function(XImage *, Clong)add_pixel; + int function(XImage *, Clong_t)add_pixel; }; }; @@ -343,7 +343,7 @@ struct XWindowChanges{ */ struct XColor { - Culong pixel; + Culong_t pixel; ushort red, green, blue; StoreColor flags; /* do_red, do_green, do_blue */ byte pad; @@ -399,7 +399,7 @@ struct XKeyboardState int key_click_percent; int bell_percent; uint bell_pitch, bell_duration; - Culong led_mask; + Culong_t led_mask; int global_auto_repeat; byte auto_repeats[32]; }; @@ -454,8 +454,8 @@ struct Display _XPrivate *private9; _XPrivate *private10; int qlen; /* Length of input event queue */ - Culong last_request_read; /* seq number of last event read */ - Culong request; /* sequence number of last request. */ + Culong_t last_request_read; /* seq number of last event read */ + Culong_t request; /* sequence number of last request. */ XPointer private11; XPointer private12; XPointer private13; @@ -467,8 +467,8 @@ struct Display int default_screen; /* default screen for operations */ int nscreens; /* number of screens on this server*/ Screen *screens; /* pointer to list of screens */ - Culong motion_buffer; /* size of motion buffer */ - Culong private16; + Culong_t motion_buffer; /* size of motion buffer */ + Culong_t private16; int min_keycode; /* minimum defined keycode */ int max_keycode; /* maximum defined keycode */ XPointer private17; @@ -488,7 +488,7 @@ struct XrmHashBucketRec{}; struct XKeyEvent { int type; /* of event */ - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* "event" window it is reported relative to */ @@ -507,7 +507,7 @@ typedef XKeyEvent XKeyReleasedEvent; struct XButtonEvent { int type; /* of event */ - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* "event" window it is reported relative to */ @@ -525,7 +525,7 @@ typedef XButtonEvent XButtonReleasedEvent; struct XMotionEvent{ int type; /* of event */ - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* "event" window reported relative to */ @@ -542,7 +542,7 @@ typedef XMotionEvent XPointerMovedEvent; struct XCrossingEvent{ int type; /* of event */ - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* "event" window reported relative to */ @@ -566,7 +566,7 @@ typedef XCrossingEvent XLeaveWindowEvent; struct XFocusChangeEvent{ int type; /* FocusIn or FocusOut */ - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* window of event */ @@ -586,7 +586,7 @@ typedef XFocusChangeEvent XFocusOutEvent; struct XKeymapEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -596,7 +596,7 @@ struct XKeymapEvent struct XExposeEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -607,7 +607,7 @@ struct XExposeEvent struct XGraphicsExposeEvent{ int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Drawable drawable; @@ -620,7 +620,7 @@ struct XGraphicsExposeEvent{ struct XNoExposeEvent{ int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Drawable drawable; @@ -630,7 +630,7 @@ struct XNoExposeEvent{ struct XVisibilityEvent{ int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -639,7 +639,7 @@ struct XVisibilityEvent{ struct XCreateWindowEvent{ int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window parent; /* parent of the window */ @@ -653,7 +653,7 @@ struct XCreateWindowEvent{ struct XDestroyWindowEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -663,7 +663,7 @@ struct XDestroyWindowEvent struct XUnmapEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -674,7 +674,7 @@ struct XUnmapEvent struct XMapEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -685,7 +685,7 @@ struct XMapEvent struct XMapRequestEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window parent; @@ -695,7 +695,7 @@ struct XMapRequestEvent struct XReparentEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -708,7 +708,7 @@ struct XReparentEvent struct XConfigureEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -723,7 +723,7 @@ struct XConfigureEvent struct XGravityEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -734,7 +734,7 @@ struct XGravityEvent struct XResizeRequestEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -744,7 +744,7 @@ struct XResizeRequestEvent struct XConfigureRequestEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window parent; @@ -754,13 +754,13 @@ struct XConfigureRequestEvent int border_width; Window above; WindowStackingMethod detail; /* Above, Below, TopIf, BottomIf, Opposite */ - Culong value_mask; + Culong_t value_mask; }; struct XCirculateEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window event; @@ -771,7 +771,7 @@ struct XCirculateEvent struct XCirculateRequestEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window parent; @@ -782,7 +782,7 @@ struct XCirculateRequestEvent struct XPropertyEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -794,7 +794,7 @@ struct XPropertyEvent struct XSelectionClearEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -805,7 +805,7 @@ struct XSelectionClearEvent struct XSelectionRequestEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window owner; @@ -819,7 +819,7 @@ struct XSelectionRequestEvent struct XSelectionEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window requestor; @@ -832,7 +832,7 @@ struct XSelectionEvent struct XColormapEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -844,13 +844,13 @@ struct XColormapEvent union XClientMessageEvent_data{ byte b[20]; short s[10]; - Clong l[5]; + Clong_t l[5]; }; struct XClientMessageEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; @@ -862,7 +862,7 @@ struct XClientMessageEvent struct XMappingEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display; /* Display the event was read from */ Window window; /* unused */ @@ -877,7 +877,7 @@ struct XErrorEvent int type; Display *display; /* Display the event was read from */ XID resourceid; /* resource id */ - Culong serial; /* serial number of failed request */ + Culong_t serial; /* serial number of failed request */ uint error_code; /* error code of failed request */ ubyte request_code; /* Major op-code of failed request */ ubyte minor_code; /* Minor op-code of failed request */ @@ -886,7 +886,7 @@ struct XErrorEvent struct XAnyEvent { int type; - Culong serial; /* # of last request processed by server */ + Culong_t serial; /* # of last request processed by server */ Bool send_event; /* true if this came from a SendEvent request */ Display *display;/* Display the event was read from */ Window window; /* window on which event was requested in event mask */ @@ -956,7 +956,7 @@ struct XCharStruct struct XFontProp { Atom name; - Culong card32; + Culong_t card32; }; struct XFontStruct{ @@ -1405,7 +1405,7 @@ extern XImage *XGetImage( int /* y */, uint /* width */, uint /* height */, - Culong /* plane_mask */, + Culong_t /* plane_mask */, ImageFormat /* format */ ); extern XImage *XGetSubImage( @@ -1415,7 +1415,7 @@ extern XImage *XGetSubImage( int /* y */, uint /* width */, uint /* height */, - Culong /* plane_mask */, + Culong_t /* plane_mask */, int /* format */, XImage* /* dest_image */, int /* dest_x */, @@ -1529,7 +1529,7 @@ extern Font XLoadFont( extern GC XCreateGC( Display* /* display */, Drawable /* d */, - Culong /* valuemask */, + Culong_t /* valuemask */, XGCValues* /* values */ ); extern GContext XGContextFromGC( @@ -1559,8 +1559,8 @@ extern Pixmap XCreatePixmapFromBitmapData( byte* /* data */, uint /* width */, uint /* height */, - Culong /* fg */, - Culong /* bg */, + Culong_t /* fg */, + Culong_t /* bg */, uint /* depth */ ); extern Window XCreateSimpleWindow( @@ -1571,8 +1571,8 @@ extern Window XCreateSimpleWindow( uint /* width */, uint /* height */, uint /* border_width */, - Culong /* border */, - Culong /* background */ + Culong_t /* border */, + Culong_t /* background */ ); extern Window XGetSelectionOwner( Display* /* display */, @@ -1646,10 +1646,10 @@ extern KeySym *XGetKeyboardMapping( extern KeySym XStringToKeysym( char* /* string */ ); -extern Clong XMaxRequestSize( +extern Clong_t XMaxRequestSize( Display* /* display */ ); -extern Clong XExtendedMaxRequestSize( +extern Clong_t XExtendedMaxRequestSize( Display* /* display */ ); extern char *XResourceManagerString( @@ -1658,7 +1658,7 @@ extern char *XResourceManagerString( extern char *XScreenResourceString( Screen* /* screen */ ); -extern Culong XDisplayMotionBufferSize( +extern Culong_t XDisplayMotionBufferSize( Display* /* display */ ); extern VisualID XVisualIDFromVisual( @@ -1720,25 +1720,25 @@ extern GC XDefaultGC( extern GC XDefaultGCOfScreen( Screen* /* screen */ ); -extern Culong XBlackPixel( +extern Culong_t XBlackPixel( Display* /* display */, int /* screen_number */ ); -extern Culong XWhitePixel( +extern Culong_t XWhitePixel( Display* /* display */, int /* screen_number */ ); -extern Culong XAllPlanes(); -extern Culong XBlackPixelOfScreen( +extern Culong_t XAllPlanes(); +extern Culong_t XBlackPixelOfScreen( Screen* /* screen */ ); -extern Culong XWhitePixelOfScreen( +extern Culong_t XWhitePixelOfScreen( Screen* /* screen */ ); -extern Culong XNextRequest( +extern Culong_t XNextRequest( Display* /* display */ ); -extern Culong XLastKnownRequestProcessed( +extern Culong_t XLastKnownRequestProcessed( Display* /* display */ ); extern char *XServerVendor( @@ -1764,7 +1764,7 @@ extern Screen *XScreenOfDisplay( extern Screen *XDefaultScreenOfDisplay( Display* /* display */ ); -extern Clong XEventMaskOfScreen( +extern Clong_t XEventMaskOfScreen( Screen* /* screen */ ); @@ -1897,9 +1897,9 @@ extern Status XAllocColorCells( Display* /* display */, Colormap /* colormap */, Bool /* contig */, - Culong* /* plane_masks_return */, + Culong_t* /* plane_masks_return */, uint /* nplanes */, - Culong* /* pixels_return */, + Culong_t* /* pixels_return */, uint /* npixels */ ); @@ -1907,14 +1907,14 @@ extern Status XAllocColorPlanes( Display* /* display */, Colormap /* colormap */, Bool /* contig */, - Culong* /* pixels_return */, + Culong_t* /* pixels_return */, int /* ncolors */, int /* nreds */, int /* ngreens */, int /* nblues */, - Culong* /* rmask_return */, - Culong* /* gmask_return */, - Culong* /* bmask_return */ + Culong_t* /* rmask_return */, + Culong_t* /* gmask_return */, + Culong_t* /* bmask_return */ ); extern Status XAllocNamedColor( @@ -2148,7 +2148,7 @@ extern int XCopyPlane( uint /* height */, int /* dest_x */, int /* dest_y */, - Culong /* plane */ + Culong_t /* plane */ ); extern int XDefaultDepth( @@ -2461,9 +2461,9 @@ extern int XFreeColormap( extern int XFreeColors( Display* /* display */, Colormap /* colormap */, - Culong* /* pixels */, + Culong_t* /* pixels */, int /* npixels */, - Culong /* planes */ + Culong_t /* planes */ ); extern int XFreeCursor( @@ -2543,7 +2543,7 @@ extern int XGetErrorText( extern Bool XGetFontProperty( XFontStruct*/* font_struct */, Atom /* atom */, - Culong* /* value_return */ + Culong_t* /* value_return */ ); extern Status XGetGCValues( @@ -2613,14 +2613,14 @@ extern int XGetWindowProperty( Display* /* display */, Window /* w */, Atom /* property */, - Culong /* long_offset */, - Culong /* long_length */, + Culong_t /* long_offset */, + Culong_t /* long_length */, Bool /* delete */, Atom /* req_type */, Atom* /* actual_type_return */, int* /* actual_format_return */, - Culong* /* nitems_return */, - Culong* /* bytes_after_return */, + Culong_t* /* nitems_return */, + Culong_t* /* bytes_after_return */, ubyte** /* prop_return */ ); @@ -3090,7 +3090,7 @@ extern int XSetArcMode( extern int XSetBackground( Display* /* display */, GC /* gc */, - Culong /* background */ + Culong_t /* background */ ); extern int XSetClipMask( @@ -3163,7 +3163,7 @@ extern int XSetFontPath( extern int XSetForeground( Display* /* display */, GC /* gc */, - Culong /* foreground */ + Culong_t /* foreground */ ); extern int XSetFunction( @@ -3208,7 +3208,7 @@ extern int XSetModifierMapping( extern int XSetPlaneMask( Display* /* display */, GC /* gc */, - Culong /* plane_mask */ + Culong_t /* plane_mask */ ); extern int XSetPointerMapping( @@ -3235,10 +3235,10 @@ extern int XSetSelectionOwner( extern int XSetState( Display* /* display */, GC /* gc */, - Culong /* foreground */, - Culong /* background */, + Culong_t /* foreground */, + Culong_t /* background */, GraphicFunction /* function */, - Culong /* plane_mask */ + Culong_t /* plane_mask */ ); extern int XSetStipple( @@ -3269,7 +3269,7 @@ extern int XSetTile( extern int XSetWindowBackground( Display* /* display */, Window /* w */, - Culong /* background_pixel */ + Culong_t /* background_pixel */ ); extern int XSetWindowBackgroundPixmap( @@ -3281,7 +3281,7 @@ extern int XSetWindowBackgroundPixmap( extern int XSetWindowBorder( Display* /* display */, Window /* w */, - Culong /* border_pixel */ + Culong_t /* border_pixel */ ); extern int XSetWindowBorderPixmap( @@ -3967,7 +3967,7 @@ align(1) struct XTextProperty { char *value; /* property data */ Atom encoding; /* type of property */ int format; /* 8, 16, or 32 */ - Culong nitems; /* number of items in value */ + Culong_t nitems; /* number of items in value */ } struct XSizeHintsAspect { @@ -3977,7 +3977,7 @@ struct XSizeHintsAspect { /* Values */ struct XSizeHints { - Clong flags; /* marks which fields in this structure are defined */ + Clong_t flags; /* marks which fields in this structure are defined */ int x; int y; /* Obsolete */ int width; diff --git a/compiler/ldc/cstdarg.di b/compiler/ldc/cstdarg.di new file mode 100644 index 00000000..4c980416 --- /dev/null +++ b/compiler/ldc/cstdarg.di @@ -0,0 +1,29 @@ +/* + * vararg support for extern(C) functions + */ + +module ldc.cstdarg; + +// Check for the right compiler +version(LDC) +{ + // OK +} +else +{ + static assert(false, "This module is only valid for LDC"); +} + +alias void* va_list; + +pragma(va_start) + void va_start(T)(va_list ap, ref T); + +pragma(va_arg) + T va_arg(T)(va_list ap); + +pragma(va_end) + void va_end(va_list args); + +pragma(va_copy) + void va_copy(va_list dst, va_list src); diff --git a/compiler/ldc/vararg.d b/compiler/ldc/vararg.d new file mode 100644 index 00000000..515cbb07 --- /dev/null +++ b/compiler/ldc/vararg.d @@ -0,0 +1,43 @@ +/* + * This module holds the implementation of special vararg templates for D style var args. + * + * Provides the functions tango.core.Vararg expects to be present! + */ + +module ldc.vararg; + +// Check for the right compiler +version(LDC) +{ + // OK +} +else +{ + static assert(false, "This module is only valid for LDC"); +} + +alias void* va_list; + +void va_start(T) ( out va_list ap, ref T parmn ) +{ + // not needed ! +} + +T va_arg(T)(ref va_list vp) +{ + T* arg = cast(T*) vp; + // ldc always aligns to size_t.sizeof in vararg lists + vp = cast(va_list) ( cast(void*) vp + ( ( T.sizeof + size_t.sizeof - 1 ) & ~( size_t.sizeof - 1 ) ) ); + return *arg; +} + +void va_end( va_list ap ) +{ + // not needed ! +} + +void va_copy( out va_list dst, va_list src ) +{ + // seems pretty useless ! + dst = src; +} diff --git a/core/color.d b/core/color.d index 7b7a53f6..b22931fd 100644 --- a/core/color.d +++ b/core/color.d @@ -12,110 +12,40 @@ module core.color; import platform.definitions; import core.definitions; -import core.parameters; import core.util; -// Color - - - -static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - alias ubyte ColorComponent; - alias uint ColorValue; -} -else static if (Colorbpp == Parameter_Colorbpp.Color16bpp) { - alias ushort ColorComponent; - alias ulong ColorValue; -} -else { - alias ubyte ColorComponent; - alias uint ColorValue; - pragma(msg, "WARNING: Colorbpp parameter is not set!"); -} - -static if (ColorType == Parameter_ColorType.ColorRGBA) { - align(1) struct _comps { - ColorComponent r; - ColorComponent g; - ColorComponent b; - ColorComponent a; - } -} -else static if (ColorType == Parameter_ColorType.ColorBGRA) { - align(1) struct _comps{ - ColorComponent b; - ColorComponent g; - ColorComponent r; - ColorComponent a; - } -} -else static if (ColorType == Parameter_ColorType.ColorABGR) { - align(1) struct _comps { - ColorComponent a; - ColorComponent b; - ColorComponent g; - ColorComponent r; - } -} -else static if (ColorType == Parameter_ColorType.ColorARGB) { - align(1) struct _comps { - ColorComponent a; - ColorComponent r; - ColorComponent g; - ColorComponent b; - } -} -else { - align(1) struct _comps { - ColorComponent r; - ColorComponent g; - ColorComponent b; - ColorComponent a; - } - pragma(msg, "WARNING: ColorType parameter is not set!"); -} - - -// a small function to convert an 8bpp value into -// the native bits per pixel (which is either 8bpp or 16bpp) -template _8toNativebpp(double comp) { - const ColorComponent _8toNativebpp = cast(ColorComponent)(comp * cast(double)((1 << (ColorComponent.sizeof*8)) - 1)); -} - // Section: Types // Description: This abstracts a color type. Internally, the structure is different for each platform depending on the native component ordering and the bits per pixel for the platform. -union Color { +struct Color { public: // -- Predefined values // Description: Black! - static const Color Black = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(0.0), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - - static const Color Green = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(1.0), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color Red = { _internal: { components: {r: _8toNativebpp!(1.0), g: _8toNativebpp!(0.0), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color Blue = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(0.0), b: _8toNativebpp!(1.0), a: _8toNativebpp!(1.0) } } }; + static const Color Black = { _red: 0.0, _green: 0.0, _blue: 0.0, _alpha: 1.0 }; - static const Color Magenta = { _internal: { components: {r: _8toNativebpp!(1.0), g: _8toNativebpp!(0.0), b: _8toNativebpp!(1.0), a: _8toNativebpp!(1.0) } } }; - static const Color Yellow = { _internal: { components: {r: _8toNativebpp!(1.0), g: _8toNativebpp!(1.0), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color Cyan = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(1.0), b: _8toNativebpp!(1.0), a: _8toNativebpp!(1.0) } } }; + static const Color Green = { _red: 0.0, _green: 1.0, _blue: 0.0, _alpha: 1.0 }; + static const Color Red = { _red: 1.0, _green: 0.0, _blue: 0.0, _alpha: 1.0 }; + static const Color Blue = { _red: 0.0, _green: 0.0, _blue: 1.0, _alpha: 1.0 }; - static const Color DarkGreen = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(0.5), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color DarkRed = { _internal: { components: {r: _8toNativebpp!(0.5), g: _8toNativebpp!(0.0), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color DarkBlue = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(0.0), b: _8toNativebpp!(0.5), a: _8toNativebpp!(1.0) } } }; + static const Color Magenta = { _red: 1.0, _green: 0.0, _blue: 1.0, _alpha: 1.0 }; + static const Color Yellow = { _red: 1.0, _green: 1.0, _blue: 0.0, _alpha: 1.0 }; + static const Color Cyan = { _red: 0.0, _green: 1.0, _blue: 1.0, _alpha: 1.0 }; - static const Color DarkMagenta = { _internal: { components: {r: _8toNativebpp!(0.5), g: _8toNativebpp!(0.0), b: _8toNativebpp!(0.5), a: _8toNativebpp!(1.0) } } }; - static const Color DarkYellow = { _internal: { components: {r: _8toNativebpp!(0.5), g: _8toNativebpp!(0.5), b: _8toNativebpp!(0.0), a: _8toNativebpp!(1.0) } } }; - static const Color DarkCyan = { _internal: { components: {r: _8toNativebpp!(0.0), g: _8toNativebpp!(0.5), b: _8toNativebpp!(0.5), a: _8toNativebpp!(1.0) } } }; + static const Color DarkGreen = { _red: 0.0, _green: 0.5, _blue: 0.0, _alpha: 1.0 }; + static const Color DarkRed = { _red: 0.5, _green: 0.0, _blue: 0.0, _alpha: 1.0 }; + static const Color DarkBlue = { _red: 0.0, _green: 0.0, _blue: 0.5, _alpha: 1.0 }; - static const Color DarkGray = { _internal: { components: {r: _8toNativebpp!(0.5), g: _8toNativebpp!(0.5), b: _8toNativebpp!(0.5), a: _8toNativebpp!(1.0) } } }; - static const Color Gray = { _internal: { components: {r: _8toNativebpp!(0.75), g: _8toNativebpp!(0.75), b: _8toNativebpp!(0.75), a: _8toNativebpp!(1.0) } } }; + static const Color DarkMagenta = { _red: 0.5, _green: 0.0, _blue: 0.5, _alpha: 1.0 }; + static const Color DarkYellow = { _red: 0.5, _green: 0.5, _blue: 0.0, _alpha: 1.0 }; + static const Color DarkCyan = { _red: 0.0, _green: 0.5, _blue: 0.5, _alpha: 1.0 }; - static const Color White = { _internal: { components: {r: _8toNativebpp!(1.0), g: _8toNativebpp!(1.0), b: _8toNativebpp!(1.0), a: _8toNativebpp!(1.0) } } }; + static const Color DarkGray = { _red: 0.5, _green: 0.5, _blue: 0.5, _alpha: 1.0 }; + static const Color Gray = { _red: 0.8, _green: 0.8, _blue: 0.8, _alpha: 1.0 }; - // -- + static const Color White = { _red: 1.0, _green: 1.0, _blue: 1.0, _alpha: 1.0 }; // Description: This function will set the color given the red, green, blue, and alpha components. static Color fromRGBA(double r, double g, double b, double a) { @@ -158,93 +88,51 @@ public: } double blue() { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - return cast(double)_internal.components.b / cast(double)0xFF; - } - else { - return cast(double)_internal.components.b / cast(double)0xFFFF; - } + return _blue; } void blue(double val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.b = cast(ubyte)(0xffp0 * val); - } - else { - _internal.components.b = cast(ubyte)(0xffffp0 * val); - } + _blue = val; + } + + void blue(ubyte val) { + _blue = cast(double)val/cast(double)ubyte.max; } double green() { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - return cast(double)_internal.components.g / cast(double)0xFF; - } - else { - return cast(double)_internal.components.g / cast(double)0xFFFF; - } + return _green; } void green(ubyte val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.g = val; - } - else { - _internal.components.g = (cast(double)val / cast(double)0xFF) * 0xFFFF; - } + _green = cast(double)val / cast(double)ubyte.max; } void green(double val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.g = cast(ubyte)(0xffp0 * val); - } - else { - _internal.components.g = cast(ubyte)(0xffffp0 * val); - } + _green = val; } double red() { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - return cast(double)_internal.components.r / cast(double)0xFF; - } - else { - return cast(double)_internal.components.r / cast(double)0xFFFF; - } + return _red; } void red(ubyte val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.r = val; - } - else { - _internal.components.r = (cast(double)val / cast(double)0xFF) * 0xFFFF; - } + _red = cast(double)val/cast(double)ubyte.max; } void red(double val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.r = cast(ubyte)(0xffp0 * val); - } - else { - _internal.components.r = cast(ubyte)(0xffffp0 * val); - } + _red = val; } double alpha() { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - return cast(double)_internal.components.a / cast(double)0xFF; - } - else { - return cast(double)_internal.components.a / cast(double)0xFFFF; - } + return _alpha; } void alpha(double val) { - static if (Colorbpp == Parameter_Colorbpp.Color8bpp) { - _internal.components.a = cast(ubyte)(0xffp0 * val); - } - else { - _internal.components.a = cast(ubyte)(0xffffp0 * val); - } + _alpha = val; + } + + void alpha(ubyte val) { + _alpha = cast(double)val / cast(double)ubyte.max; } void hue(double val) { @@ -298,19 +186,12 @@ public: return _lum; } - ColorValue value() { - return _internal.clr; - } - private: - union internal { - _comps components; - - ColorValue clr; - } - - internal _internal; + double _red; + double _green; + double _blue; + double _alpha; // cache HSL values bool _hslValid; diff --git a/core/definitions.d b/core/definitions.d index caff67fa..d53bcb28 100644 --- a/core/definitions.d +++ b/core/definitions.d @@ -10,46 +10,8 @@ module core.definitions; -import Platform = platform.definitions; - -// String -version(D_Version2) { - template _D2_Support_string() { - version(D_Version2) { - const char[] _D2_Support_string = `alias invariant(char)[] string;`; - } - else { - const char[] _D2_Support_string = `alias char[] string;`; - } - } - template _D2_Support_wstring() { - version(D_Version2) { - const char[] _D2_Support_wstring = `alias invariant(wchar)[] wstring;`; - } - else { - const char[] _D2_Support_wstring = `alias wchar[] wstring;`; - } - } - template _D2_Support_dstring() { - version(D_Version2) { - const char[] _D2_Support_dstring = `alias invariant(dchar)[] dstring;`; - } - else { - const char[] _D2_Support_dstring = `alias dchar[] dstring;`; - } - } - - mixin(_D2_Support_string!()); - mixin(_D2_Support_wstring!()); - mixin(_D2_Support_dstring!()); -} -else { - version(Tango) { - alias char[] string; - alias wchar[] wstring; - alias dchar[] dstring; - } -} +//import platform.definitions; +public import platform.definitions; // Section: Types @@ -180,112 +142,108 @@ struct Key { bool alt; bool shift; - const uint Backspace = Platform.KeyBackspace; - const uint Tab = Platform.KeyTab; - - const uint Return = Platform.KeyReturn; - const uint Pause = Platform.KeyPause; - const uint Escape = Platform.KeyEscape; - const uint Space = Platform.KeySpace; - - const uint PageUp = Platform.KeyPageUp; - const uint PageDown = Platform.KeyPageDown; - - const uint End = Platform.KeyEnd; - const uint Home = Platform.KeyHome; - - const uint Left = Platform.KeyArrowLeft; - const uint Right = Platform.KeyArrowRight; - const uint Up = Platform.KeyArrowUp; - const uint Down = Platform.KeyArrowDown; - - const uint Insert = Platform.KeyInsert; - const uint Delete = Platform.KeyDelete; - - const uint NumLock = Platform.KeyNumLock; - const uint ScrollLock = Platform.KeyScrollLock; - - const uint LeftShift = Platform.KeyLeftShift; - const uint RightShift = Platform.KeyRightShift; - - const uint LeftControl = Platform.KeyLeftControl; - const uint RightControl = Platform.KeyRightControl; - - const uint LeftAlt = Platform.KeyLeftAlt; - const uint RightAlt = Platform.KeyRightAlt; - - const uint Zero = Platform.Key0; - const uint One = Platform.Key1; - const uint Two = Platform.Key2; - const uint Three = Platform.Key3; - const uint Four = Platform.Key4; - const uint Five = Platform.Key5; - const uint Six = Platform.Key6; - const uint Seven = Platform.Key7; - const uint Eight = Platform.Key8; - const uint Nine = Platform.Key9; - - const uint SingleQuote = Platform.KeySingleQuote; - const uint Quote = Platform.KeyQuote; - const uint Comma = Platform.KeyComma; - const uint Period = Platform.KeyPeriod; - const uint Foreslash = Platform.KeyForeslash; - const uint Backslash = Platform.KeyBackslash; - const uint LeftBracket = Platform.KeyLeftBracket; - const uint RightBracket = Platform.KeyRightBracket; - const uint Semicolon = Platform.KeySemicolon; - const uint Minus = Platform.KeyMinus; - const uint Equals = Platform.KeyEquals; - - const uint A = Platform.KeyA; - const uint B = Platform.KeyB; - const uint C = Platform.KeyC; - const uint D = Platform.KeyD; - const uint E = Platform.KeyE; - const uint F = Platform.KeyF; - const uint G = Platform.KeyG; - const uint H = Platform.KeyH; - const uint I = Platform.KeyI; - const uint J = Platform.KeyJ; - const uint K = Platform.KeyK; - const uint L = Platform.KeyL; - const uint M = Platform.KeyM; - const uint N = Platform.KeyN; - const uint O = Platform.KeyO; - const uint P = Platform.KeyP; - const uint Q = Platform.KeyQ; - const uint R = Platform.KeyR; - const uint S = Platform.KeyS; - const uint T = Platform.KeyT; - const uint U = Platform.KeyU; - const uint V = Platform.KeyV; - const uint W = Platform.KeyW; - const uint X = Platform.KeyX; - const uint Y = Platform.KeyY; - const uint Z = Platform.KeyZ; - - const uint F1 = Platform.KeyF1; - const uint F2 = Platform.KeyF2; - const uint F3 = Platform.KeyF3; - const uint F4 = Platform.KeyF4; - const uint F5 = Platform.KeyF5; - const uint F6 = Platform.KeyF6; - const uint F7 = Platform.KeyF7; - const uint F8 = Platform.KeyF8; - const uint F9 = Platform.KeyF9; - const uint F10 = Platform.KeyF10; - const uint F11 = Platform.KeyF11; - const uint F12 = Platform.KeyF12; - const uint F13 = Platform.KeyF13; - const uint F14 = Platform.KeyF14; - const uint F15 = Platform.KeyF15; - const uint F16 = Platform.KeyF16; + const uint Backspace = KeyBackspace; + const uint Tab = KeyTab; + + const uint Return = KeyReturn; + const uint Pause = KeyPause; + const uint Escape = KeyEscape; + const uint Space = KeySpace; + + const uint PageUp = KeyPageUp; + const uint PageDown = KeyPageDown; + + const uint End = KeyEnd; + const uint Home = KeyHome; + + const uint Left = KeyArrowLeft; + const uint Right = KeyArrowRight; + const uint Up = KeyArrowUp; + const uint Down = KeyArrowDown; + + const uint Insert = KeyInsert; + const uint Delete = KeyDelete; + + const uint NumLock = KeyNumLock; + const uint ScrollLock = KeyScrollLock; + + const uint LeftShift = KeyLeftShift; + const uint RightShift = KeyRightShift; + + const uint LeftControl = KeyLeftControl; + const uint RightControl = KeyRightControl; + + const uint LeftAlt = KeyLeftAlt; + const uint RightAlt = KeyRightAlt; + + const uint Zero = Key0; + const uint One = Key1; + const uint Two = Key2; + const uint Three = Key3; + const uint Four = Key4; + const uint Five = Key5; + const uint Six = Key6; + const uint Seven = Key7; + const uint Eight = Key8; + const uint Nine = Key9; + + const uint SingleQuote = KeySingleQuote; + const uint Quote = KeyQuote; + const uint Comma = KeyComma; + const uint Period = KeyPeriod; + const uint Foreslash = KeyForeslash; + const uint Backslash = KeyBackslash; + const uint LeftBracket = KeyLeftBracket; + const uint RightBracket = KeyRightBracket; + const uint Semicolon = KeySemicolon; + const uint Minus = KeyMinus; + const uint Equals = KeyEquals; + + const uint A = KeyA; + const uint B = KeyB; + const uint C = KeyC; + const uint D = KeyD; + const uint E = KeyE; + const uint F = KeyF; + const uint G = KeyG; + const uint H = KeyH; + const uint I = KeyI; + const uint J = KeyJ; + const uint K = KeyK; + const uint L = KeyL; + const uint M = KeyM; + const uint N = KeyN; + const uint O = KeyO; + const uint P = KeyP; + const uint Q = KeyQ; + const uint R = KeyR; + const uint S = KeyS; + const uint T = KeyT; + const uint U = KeyU; + const uint V = KeyV; + const uint W = KeyW; + const uint X = KeyX; + const uint Y = KeyY; + const uint Z = KeyZ; + + const uint F1 = KeyF1; + const uint F2 = KeyF2; + const uint F3 = KeyF3; + const uint F4 = KeyF4; + const uint F5 = KeyF5; + const uint F6 = KeyF6; + const uint F7 = KeyF7; + const uint F8 = KeyF8; + const uint F9 = KeyF9; + const uint F10 = KeyF10; + const uint F11 = KeyF11; + const uint F12 = KeyF12; + const uint F13 = KeyF13; + const uint F14 = KeyF14; + const uint F15 = KeyF15; + const uint F16 = KeyF16; } -// Redefine Platform Hints -alias Platform.FontSans FontSans; -alias Platform.Char Char; - // Default parameters const int Default = -1; @@ -322,3 +280,4 @@ struct CuiEvent { CuiEventInfo info; uint aux; } + diff --git a/core/error.d b/core/error.d index 41b6b7c4..55f55c24 100644 --- a/core/error.d +++ b/core/error.d @@ -10,7 +10,7 @@ module core.error; -import core.definitions; +import core.exception; // Description: This is for non irrecoverable failure. class Error : Exception { @@ -46,4 +46,10 @@ static: super("Switch has no default",file,line); } } + + class NoCompare : RuntimeError { + this(string className) { + super("Class " ~ className ~ " needs an opCmp.", "", 0); + } + } } diff --git a/core/event.d b/core/event.d index bbdc3f6a..3657940e 100644 --- a/core/event.d +++ b/core/event.d @@ -8,7 +8,7 @@ module core.event; // Description: This class represents an object that can dispatch signals. -class Dispatcher { +class Dispatcher : Object { void onPush(Responder rsp) { } @@ -62,4 +62,4 @@ public: dsp.responder = this; dsp.onPush(this); } -} \ No newline at end of file +} diff --git a/core/exception.d b/core/exception.d index 1dbd3751..b645f301 100644 --- a/core/exception.d +++ b/core/exception.d @@ -11,9 +11,8 @@ module core.exception; import core.string; -import core.definitions; -/* -class Exception { + +class Exception : Object { this(string msg, string file = "", ulong line = 0) { _msg = msg.dup; _file = file.dup; @@ -45,7 +44,6 @@ private: char[] _file; ulong _line; } -*/ // Exceptions for IO abstract class IOException : Exception { diff --git a/core/parameters.d b/core/parameters.d deleted file mode 100644 index 1a033076..00000000 --- a/core/parameters.d +++ /dev/null @@ -1,17 +0,0 @@ -module core.parameters; - -// For Parameters (Internal, Platform Use) - -enum Parameter_Colorbpp -{ - Color8bpp, - Color16bpp // Needs to be 1 for 16bpp static color definitions -} - -enum Parameter_ColorType -{ - ColorRGBA, - ColorBGRA, - ColorARGB, - ColorABGR, -} \ No newline at end of file diff --git a/djehuty.d b/djehuty.d index e6958f17..e446b6e9 100644 --- a/djehuty.d +++ b/djehuty.d @@ -1,5 +1,7 @@ module djehuty; +public import core.exception; +public import core.error; public import core.definitions; public import core.string; public import core.unicode; @@ -19,4 +21,3 @@ public import core.endian; public import core.variant; public import core.date; public import core.locale; -public import core.exception; diff --git a/math/common.d b/math/common.d index ce16110a..c654ddb0 100644 --- a/math/common.d +++ b/math/common.d @@ -470,6 +470,13 @@ double abs(double x) { return *cast(double*)&intRepresentation; } +int abs(int x) { + if (x < 0) { + return -x; + } + return x; +} + long abs(long x) { if (x < 0) { return -x; @@ -480,3 +487,4 @@ long abs(long x) { double pow(double x, double y) { return x; } + diff --git a/platform/unix/main.d b/platform/unix/main.d index f2cbfed3..6b485720 100644 --- a/platform/unix/main.d +++ b/platform/unix/main.d @@ -89,8 +89,7 @@ extern(C) void segfault_handler(int signum) { throw new Exception("Access Violation"); } -void AppInit() -{ +void AppInit() { // ------------- */ // UTF-8 SUPPORT @@ -101,9 +100,12 @@ void AppInit() signal(SIGSEGV, &segfault_handler); } -int main(char[][] args){ - try { +import binding.c; +/+int main(string[] args){ +/* try { + printf("fudge\n"); AppInit(); + printf("fudge\n"); Arguments argList = Arguments.instance(); foreach(arg; args) { @@ -116,7 +118,8 @@ int main(char[][] args){ } catch(Object o) { Debugger.raiseException(cast(Exception)o); - } + }*/ - return ApplicationController.instance.exitCode; -} +// return ApplicationController.instance.exitCode; + return 0; +}+/ diff --git a/platform/unix/platform/definitions.d b/platform/unix/platform/definitions.d index c0f63751..0810f88c 100644 --- a/platform/unix/platform/definitions.d +++ b/platform/unix/platform/definitions.d @@ -9,27 +9,7 @@ module platform.definitions; -version(PlatformLinux) -{ - - -import core.parameters; - -// String Representation -alias char Char; - -// Color Representation -static const Parameter_Colorbpp Colorbpp = Parameter_Colorbpp.Color8bpp; -static const Parameter_ColorType ColorType = Parameter_ColorType.ColorBGRA; - - - - -// Graphical Types -//alias uint Pen; -//alias uint Brush; -//alias Pango.PangoFontDescription* Font; - +version(PlatformLinux): // Common Fonts @@ -39,10 +19,7 @@ const auto FontSans = "sans"c; const auto FontSerif = "serif"c; const auto FontSystem = "sans"c; - - - -import platform.unix.common; +import X = binding.x.X; const uint KeyBackspace = X.XK_BackSpace; const uint KeyTab = X.XK_Tab; @@ -160,4 +137,4 @@ const uint KeyRightControl = X.XK_Control_R; //0xA3 const uint KeyLeftAlt = X.XK_Meta_L; //0xA4 const uint KeyRightAlt = X.XK_Meta_R; //0xA5 -} + diff --git a/platform/unix/platform/vars/thread.d b/platform/unix/platform/vars/thread.d index 84063b71..dbb5fb1c 100644 --- a/platform/unix/platform/vars/thread.d +++ b/platform/unix/platform/vars/thread.d @@ -12,6 +12,9 @@ module platform.vars.thread; import platform.unix.common; +import synch.thread; + struct ThreadPlatformVars { pthread_t id; + Thread thread; } diff --git a/platform/unix/scaffold/thread.d b/platform/unix/scaffold/thread.d index aecf4cc6..32fbc6fe 100644 --- a/platform/unix/scaffold/thread.d +++ b/platform/unix/scaffold/thread.d @@ -34,13 +34,11 @@ void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) nanosleep(&timetoexpire, null); } -/* - extern (C) void *_djehuty_unix_thread_proc(void* udata) { - Thread t_info = cast(Thread)(udata); - ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t_info); + ThreadPlatformVars* threadVars = cast(ThreadPlatformVars*)(udata); + Thread t_info = threadVars.thread; t_info.run(); @@ -71,14 +69,14 @@ void ThreadStop(ref ThreadPlatformVars threadVars) if (threadVars.id == pthread_self()) { //soft exit - printf("thread - soft kill\n"); + //printf("thread - soft kill\n"); threadVars.id = 0; pthread_exit(null); } else { //hard exit - printf("thread - hard kill\n"); + //printf("thread - hard kill\n"); pthread_kill(threadVars.id, SIGKILL); } @@ -86,32 +84,12 @@ void ThreadStop(ref ThreadPlatformVars threadVars) } } -void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) -{ - timespec timetoexpire; - - timetoexpire.tv_sec = (milliseconds / 1000); - timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000; - - nanosleep(&timetoexpire, null); -} - bool ThreadIsCurrent(ref ThreadPlatformVars threadVars) { return false; } - -*/ - - - - - - - - // Semaphores void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue) diff --git a/runtime/array.d b/runtime/array.d index 57bf3855..719494f5 100644 --- a/runtime/array.d +++ b/runtime/array.d @@ -203,7 +203,6 @@ private template _array_init(T) { } } -/* void _d_array_init_i1(bool* array, size_t length, bool value) { _array_init(array[0..length], value); } @@ -236,6 +235,10 @@ void _d_array_init_pointer(void** array, size_t length, void* value) { _array_init(array[0..length], value); } +void _d_array_init_cdouble(cdouble[] array, cdouble value) { + _array_init(array[0..length], value); +} + void _d_array_init_mem(ubyte* array, size_t length, ubyte* value, size_t valueLength) { if (valueLength == 0 || length == 0) { return; @@ -252,9 +255,7 @@ void _d_array_init_mem(ubyte* array, size_t length, ubyte* value, size_t valueLe } } } -//*/ -/* size_t _d_array_cast_len(size_t length, size_t elementSize, size_t newElementSize) { if (newElementSize == 1) { return length * elementSize; diff --git a/runtime/assocarray.d b/runtime/assocarray.d index bb5640e9..842eed8f 100644 --- a/runtime/assocarray.d +++ b/runtime/assocarray.d @@ -12,6 +12,20 @@ import runtime.common; extern(C): +struct aaA { + aaA* left; + aaA* right; + hash_t hash; + // key // + // value // +} + +struct BB { + aaA*[] b; + size_t nodes; + TypeInfo keyti; +} + // Description: This runtime function will determine the number of entries in // an associative array. // Returns: The number of entries in the array. @@ -23,11 +37,11 @@ size_t _aaLen(AA aa) { // in an associative array at a particular key and add the entry if // it does not exist. // Returns: A pointer to the value associated with the given key. -void* _aaGetp(AA* aa, TypeInfo keyti, size_t valuesize, void* pkey) { +void* _aaGet(BB* aa, TypeInfo keyti, size_t valuesize, void* pkey) { return null; } -void* _aaGetRvaluep(AA aa, TypeInfo keyti, size_t valuesize, void* pkey) { +void* _aaGetRvalue(BB aa, TypeInfo keyti, size_t valuesize, void* pkey) { return null; } @@ -35,50 +49,50 @@ void* _aaGetRvaluep(AA aa, TypeInfo keyti, size_t valuesize, void* pkey) { // associative array indexed by a key. Invoked via "aa[key]" and "key in aa". // Returns: null when the value is not in aa, the pointer to the value // otherwise. -void* _aaInp(AA aa, TypeInfo keyti, void* pkey) { +void* _aaIn(BB aa, TypeInfo keyti, void* pkey) { return null; } // Description: This runtime function will delete a value with the given key. // It will do nothing if the value does not exist. -void _aaDelp(AA aa, TypeInfo keyti, void* pkey) { +void _aaDel(BB aa, TypeInfo keyti, void* pkey) { } // Description: This runtime function will produce an array of values for // an associative array. // Returns: An array of values. -Array _aaValues(AA aa, size_t keysize, size_t valuesize) { - Array r; - return r; +void[] _aaValues(BB aa, size_t keysize, size_t valuesize) { + return null; } // Description: This runtime function will rehash an associative array. -AA _aaRehash(AA* paa, TypeInfo keyti) { - AA r; - return r; +BB _aaRehash(BB* paa, TypeInfo keyti) { + BB ret; + return ret; } // Description: This runtime function will produce an array of keys of an // associative array. // Returns: An array of keys. -Array _aaKeys(AA aa, size_t keysize) { - Array r; - return r; +void[] _aaKeys(BB aa, size_t keysize) { + return null; } // Description: This runtime function will handle a foreach for an associative // array invoked as foreach(foo; aa). -int _aaApply(AA aa, size_t keysize, aa_dg_t dg) { +extern (D) typedef int delegate(void *) dg_t; +int _aaApply(BB aa, size_t keysize, dg_t dg) { return 0; } // Description: This runtime function will handle a foreach_reverse for an // associative array invoked as foreach_reverse(foo; aa). -int _aaApply2(AA aa, size_t keysize, aa_dg2_t dg) { +extern (D) typedef int delegate(void *, void *) dg2_t; +int _aaApply2(BB aa, size_t keysize, dg2_t dg) { return 0; } -BB* _d_assocarrayliteralTp(TypeInfo_AssociativeArray ti, size_t length, +BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, void* keys, void* values) { return null; } diff --git a/runtime/classinfo.d b/runtime/classinfo.d deleted file mode 100644 index fe82575f..00000000 --- a/runtime/classinfo.d +++ /dev/null @@ -1,39 +0,0 @@ -module runtime.classinfo; - -import core.string; -import core.definitions; - -// Description: The information stored for a class. Retrieved via the .classinfo property. -// It is stored as the first entry in the class' vtbl[]. -class ClassInfo { - byte[] init; - - string name; - void*[] vtbl; - - Interface[] interfaces; - - ClassInfo base; - void function() destructor; - void function(Object) classInvariant; - - uint flags; - void function() deallocator; - OffsetTypeInfo[] offTi; - - Object function() defaultConstructor; - - TypeInfo typeinfo; - - static ClassInfo find(string classname) { - // Loop through every module - // Then loop through every class - // Trying to find the class - return null; - } - - Object create() { - // Class factory - return null; - } -} diff --git a/runtime/error.d b/runtime/error.d index ef1b9c35..e957203d 100644 --- a/runtime/error.d +++ b/runtime/error.d @@ -9,6 +9,9 @@ module runtime.error; import core.definitions; import core.exception; +import core.error; + +extern(C): // Description: This is called when an assertion without a message fails. // file: The file that contains the error. @@ -36,5 +39,5 @@ void _d_array_bounds(string file, uint line ) { // file: The file that contains the error. // line: The line number of the error. void _d_switch_error(string file, uint line ) { - throw RuntimeError.NoDefaultCase(file, line); + throw new RuntimeError.NoDefaultCase(file, line); } diff --git a/runtime/exception.d b/runtime/exception.d index 286a9594..6ce403d3 100644 --- a/runtime/exception.d +++ b/runtime/exception.d @@ -19,3 +19,10 @@ void onOutOfMemoryError() { void _d_throw_exception(Object e) { } + +int _d_eh_personality(int ver, int actions, ulong eh_class, void* info, void* context) { + return 0; +} + +void _d_eh_resume_unwind(void* exception_struct) { +} diff --git a/runtime/gc.d b/runtime/gc.d index 4f0db914..344e8682 100644 --- a/runtime/gc.d +++ b/runtime/gc.d @@ -10,9 +10,11 @@ module runtime.gc; import synch.atomic; -extern(C): +import System = scaffold.system; + +import binding.c; -import scaffold.system; +extern(C): void gc_init() { GarbageCollector._initialize(); @@ -36,14 +38,17 @@ void gc_collect() { uint gc_getAttr(void* p) { return 0; + //GarbageCollector.getAttr(p); } uint gc_setAttr(void* p, uint a) { return 0; + //GarbageCollector.setAttr(p, a); } uint gc_clrAttr(void* p, uint a) { return 0; + //GarbageCollector.clearAttr(p, a); } void* gc_malloc(size_t sz, uint ba = 0) { @@ -109,15 +114,16 @@ static: } ubyte[] malloc(size_t length) { - return null; + printf("malloc %d\n", length); + return System.malloc(length); } ubyte[] realloc(ubyte[] original, size_t length) { - return null; + return System.realloc(original, length); } ubyte[] calloc(size_t length) { - return null; + return System.calloc(length); } size_t extend(ubyte[] original, size_t max, size_t size) { @@ -154,10 +160,12 @@ static: private: void _initialize() { + printf("GC initialized\n"); _inited = 1; } void _terminate() { + printf("GC terminated\n"); _inited = 0; } diff --git a/runtime/lifetime.d b/runtime/lifetime.d index 63469d69..b7385079 100644 --- a/runtime/lifetime.d +++ b/runtime/lifetime.d @@ -11,14 +11,16 @@ import runtime.exception; import runtime.common; import runtime.gc; -import io.console; - -//extern(C): +extern(C): Object _d_allocclass(ClassInfo ci) { return cast(Object)gc_malloc(ci.init.length, BlkAttr.FINALIZE | (ci.flags & 2 ? BlkAttr.NO_SCAN : 0)); } +void* _d_allocmemoryT(TypeInfo ti) { + return gc_malloc(ti.tsize(), 0); +} + Object _d_newclass(ClassInfo ci) { return null; } @@ -55,11 +57,11 @@ void _d_delclass(Object p) { private template _newarray(bool initialize, bool withZero) { void[] _newarray(TypeInfo ti, size_t length) { - Console.putln("hello"); if (cast(TypeInfo_Typedef)ti !is null) { // ti = ti.next; } size_t elementSize = ti.next.tsize(); + size_t returnLength = length; // Check to see if the size of the array can fit // within a size_t. If there is no overflow, then @@ -110,7 +112,7 @@ private template _newarray(bool initialize, bool withZero) { // we do not initialize them. Falling through will // work for those. - return ret; + return ret[0..returnLength]; } } @@ -203,11 +205,11 @@ void _d_callfinalizer(void* p) { void rt_finalize(void* p, bool det = true) { } -byte[] _d_arraysetlengthT(TypeInfo ti, size_t newlength, void[]* p) { +byte* _d_arraysetlengthT(TypeInfo ti, size_t newlength, size_t plength, byte* pdata) { return null; } -byte[] _d_arraysetlengthiT(TypeInfo ti, size_t newlength, void[]* p) { +byte* _d_arraysetlengthiT(TypeInfo ti, size_t newlength, size_t plength, byte* pdata) { return null; } @@ -215,14 +217,18 @@ void[] _d_arrayappendT(TypeInfo ti, void[]* px, byte[] y) { return null; } -byte[] _d_arrayappendcTp(TypeInfo ti, ref byte[] x, void* argp) { +// Description: This runtime function will append a single element to an +// array. +// ti: The TypeInfo of the base type of this array. +// array: The array to append the element. +// element: The element to append. +byte[] _d_arrayappendcT(TypeInfo ti, ref byte[] array, void* element) { return null; } byte[] _d_arraycatT(TypeInfo ti, byte[] x, byte[] y) { return null; } - byte[] _d_arraycatnT(TypeInfo ti, uint n, ...) { return null; } diff --git a/runtime/main.d b/runtime/main.d index 94940c6b..626314ea 100644 --- a/runtime/main.d +++ b/runtime/main.d @@ -5,6 +5,8 @@ * */ +module runtime.main; + import runtime.gc; // The user supplied D entry @@ -14,11 +16,14 @@ int main(char[][] args); // argc: The number of arguments // argv: An array of strings that specify the arguments. // Returns: The error code for the application. +import binding.c; + extern(C) int main(int argc, char** argv) { // Initialize the garbage collector gc_init(); // Gather arguments + main([]); // Terminate the garbage collector gc_term(); diff --git a/runtime/moduleinfo.d b/runtime/moduleinfo.d index b910d054..91b7f499 100644 --- a/runtime/moduleinfo.d +++ b/runtime/moduleinfo.d @@ -1,9 +1,7 @@ module runtime.moduleinfo; -import core.definitions; - // Description: This class describes a D module. -class ModuleInfo { +class ModuleInfo : Object { string name; ModuleInfo[] importedModules; ClassInfo[] localClasses; @@ -30,10 +28,18 @@ class ModuleInfo { return ret; } - ModuleInfo[] modules { + ModuleInfo[] modules() { return _modules.dup; } private: static ModuleInfo[] _modules; } + +// This linked list is created by a compiler generated function inserted +// into the .ctor list by the compiler. +struct ModuleReference { + ModuleReference* next; + ModuleInfo mod; +} +extern (C) ModuleReference* _Dmodule_ref; // start of linked list diff --git a/runtime/monitor.d b/runtime/monitor.d index 24ddb5bb..70512fc0 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -17,3 +17,9 @@ void _d_monitorenter(Object h) { void _d_monitorexit(Object h) { } + +void _d_criticalenter(void* dcs) { +} + +void _d_criticalexit(void* dcs) { +} diff --git a/runtime/object.d b/runtime/object.d index eb6984c1..0c79ee16 100644 --- a/runtime/object.d +++ b/runtime/object.d @@ -7,18 +7,37 @@ module object; -// Imports necessary routines used by the runtime -import runtime.dstatic; -import runtime.exception; -import runtime.error; +// Figure out the size of a pointer +static if ((ubyte*).sizeof == 8) { + version = Arch64; +} +else static if ((ubyte*).sizeof == 4) { + version = Arch32; +} -public import runtime.types; -public import runtime.classinfo; -public import runtime.typeinfo; +// Pointer sizes +version(Arch32) { + alias uint size_t; + alias int ptrdiff_t; + alias uint hash_t; +} +else { + alias ulong size_t; + alias long ptrdiff_t; + alias ulong hash_t; +} + +// String types +alias char[] string; +alias wchar[] wstring; +alias dchar[] dstring; // Description: The base class inherited by all classes. class Object { + void dispose() { + } + // Description: Returns a string representing this object. char[] toString() { return this.classinfo.name; @@ -26,25 +45,17 @@ class Object { // Description: Computes a hash representing this object hash_t toHash() { - string hashStr = (toStr(&this) ~ this.classinfo.name); - - hash_t hash = 0; - foreach(chr; hashStr) { - hash *= 9; - hash += cast(ubyte)chr; - } + return *cast(hash_t*)this; } - // Will compare two Object classes + // Description: Will compare two Object classes // Returns: 0 if equal, -1 if o is greater, 1 if o is smaller. int opCmp(Object o) { - // BUG: this prevents a compacting GC from working, needs to be fixed - //return cast(int)cast(void *)this - cast(int)cast(void *)o; - - throw new Error("need opCmp for class " ~ this.classinfo.name); + return 0; } - // Will compare two Object classes for equality. + // Description: Will compare two Object classes for equality. Defaults + // to a comparing references. // Returns: 0 if not equal. int opEquals(Object o) { return cast(int)(this is o); @@ -55,7 +66,104 @@ class Object { struct Interface { ClassInfo classinfo; // .classinfo for this interface (not for containing class) void *[] vtbl; - int offset; // offset to Interface 'this' from Object 'this' + ptrdiff_t offset; // offset to Interface 'this' from Object 'this' +} + +// Description: The information stored for a class. Retrieved via the .classinfo property. +// It is stored as the first entry in the class' vtbl[]. +class ClassInfo : Object { + byte[] init; + + string name; + void*[] vtbl; + + Interface[] interfaces; + + ClassInfo base; + void* destructor; + void function(Object) classInvariant; + + uint flags; + void* deallocator; + OffsetTypeInfo[] offTi; + + void* defaultConstructor; + + TypeInfo typeinfo; + + static ClassInfo find(string classname) { + // Loop through every module + // Then loop through every class + // Trying to find the class + return null; + } + + Object create() { + // Class factory + return null; + } } -public import djehuty; +public import runtime.typeinfo; + +public import runtime.typeinfos.ti_array; +public import runtime.typeinfos.ti_array_bool; +public import runtime.typeinfos.ti_array_byte; +public import runtime.typeinfos.ti_array_cdouble; +public import runtime.typeinfos.ti_array_cfloat; +public import runtime.typeinfos.ti_array_char; +public import runtime.typeinfos.ti_array_creal; +public import runtime.typeinfos.ti_array_dchar; +public import runtime.typeinfos.ti_array_double; +public import runtime.typeinfos.ti_array_float; +public import runtime.typeinfos.ti_array_idouble; +public import runtime.typeinfos.ti_array_ifloat; +public import runtime.typeinfos.ti_array_int; +public import runtime.typeinfos.ti_array_ireal; +public import runtime.typeinfos.ti_array_long; +public import runtime.typeinfos.ti_array_object; +public import runtime.typeinfos.ti_array_real; +public import runtime.typeinfos.ti_array_short; +public import runtime.typeinfos.ti_array_ubyte; +public import runtime.typeinfos.ti_array_uint; +public import runtime.typeinfos.ti_array_ulong; +public import runtime.typeinfos.ti_array_ushort; +public import runtime.typeinfos.ti_array_void; +public import runtime.typeinfos.ti_array_wchar; +public import runtime.typeinfos.ti_assocarray; +//public import runtime.typeinfos.ti_bool; +public import runtime.typeinfos.ti_byte; +public import runtime.typeinfos.ti_cdouble; +public import runtime.typeinfos.ti_cfloat; +public import runtime.typeinfos.ti_char; +public import runtime.typeinfos.ti_creal; +public import runtime.typeinfos.ti_dchar; +public import runtime.typeinfos.ti_delegate; +public import runtime.typeinfos.ti_double; +public import runtime.typeinfos.ti_enum; +public import runtime.typeinfos.ti_float; +public import runtime.typeinfos.ti_function; +public import runtime.typeinfos.ti_idouble; +public import runtime.typeinfos.ti_ifloat; +public import runtime.typeinfos.ti_int; +public import runtime.typeinfos.ti_interface; +public import runtime.typeinfos.ti_ireal; +public import runtime.typeinfos.ti_long; +public import runtime.typeinfos.ti_object; +public import runtime.typeinfos.ti_ptr; +public import runtime.typeinfos.ti_real; +public import runtime.typeinfos.ti_short; +public import runtime.typeinfos.ti_staticarray; +public import runtime.typeinfos.ti_struct; +public import runtime.typeinfos.ti_tuple; +public import runtime.typeinfos.ti_typedef; +public import runtime.typeinfos.ti_ubyte; +public import runtime.typeinfos.ti_uint; +public import runtime.typeinfos.ti_ulong; +public import runtime.typeinfos.ti_ushort; +public import runtime.typeinfos.ti_void; +public import runtime.typeinfos.ti_wchar; + +public import runtime.moduleinfo; + +public import core.exception; diff --git a/runtime/typeinfo.d b/runtime/typeinfo.d index 80ea075d..72c1a159 100644 --- a/runtime/typeinfo.d +++ b/runtime/typeinfo.d @@ -1,7 +1,5 @@ module runtime.typeinfo; -import core.definitions; - struct OffsetTypeInfo { size_t offset; TypeInfo ti; @@ -77,7 +75,7 @@ class TypeInfo { return null; } - ubyte[] init() { + void[] init() { return null; } diff --git a/runtime/typeinfo/ti_array.d b/runtime/typeinfos/ti_array.d similarity index 95% rename from runtime/typeinfo/ti_array.d rename to runtime/typeinfos/ti_array.d index dab870c7..1544c22e 100644 --- a/runtime/typeinfo/ti_array.d +++ b/runtime/typeinfos/ti_array.d @@ -5,9 +5,7 @@ * */ -module runtime.typeinfo.ti_array; - -import runtime.util; +module runtime.typeinfos.ti_array; class TypeInfo_Array : TypeInfo { char[] toString() { return value.toString() ~ "[]"; } @@ -144,7 +142,13 @@ class ArrayInfo(char[] TYPE) : TypeInfo { return 0; } - return memcmp(cast(ubyte*)s1.ptr, cast(ubyte*)s2.ptr, s1.length * T.sizeof) == 0; + foreach(size_t idx, element; s1) { + if (s2[idx] != element) { + return 0; + } + } + + return 1; } int compare(void* p1, void* p2) { diff --git a/runtime/typeinfo/ti_array_bool.d b/runtime/typeinfos/ti_array_bool.d similarity index 72% rename from runtime/typeinfo/ti_array_bool.d rename to runtime/typeinfos/ti_array_bool.d index d4b175b2..3082bd45 100644 --- a/runtime/typeinfo/ti_array_bool.d +++ b/runtime/typeinfos/ti_array_bool.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_bool; +module runtime.typeinfos.ti_array_bool; -import runtime.typeinfo.ti_array_ubyte; +import runtime.typeinfos.ti_array_ubyte; class TypeInfo_Ab : TypeInfo_Ah { char[] toString() { diff --git a/runtime/typeinfo/ti_array_byte.d b/runtime/typeinfos/ti_array_byte.d similarity index 63% rename from runtime/typeinfo/ti_array_byte.d rename to runtime/typeinfos/ti_array_byte.d index 28410bea..e6a42f10 100644 --- a/runtime/typeinfo/ti_array_byte.d +++ b/runtime/typeinfos/ti_array_byte.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_byte; +module runtime.typeinfos.ti_array_byte; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Ag : ArrayInfo!("byte") { } diff --git a/runtime/typeinfo/ti_array_cdouble.d b/runtime/typeinfos/ti_array_cdouble.d similarity index 94% rename from runtime/typeinfo/ti_array_cdouble.d rename to runtime/typeinfos/ti_array_cdouble.d index 044d400b..5b678e07 100644 --- a/runtime/typeinfo/ti_array_cdouble.d +++ b/runtime/typeinfos/ti_array_cdouble.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_cdouble; +module runtime.typeinfos.ti_array_cdouble; -import runtime.typeinfo.ti_cdouble; +import runtime.typeinfos.ti_cdouble; class TypeInfo_Ar : TypeInfo { char[] toString() { return "cdouble[]"; } diff --git a/runtime/typeinfo/ti_array_cfloat.d b/runtime/typeinfos/ti_array_cfloat.d similarity index 94% rename from runtime/typeinfo/ti_array_cfloat.d rename to runtime/typeinfos/ti_array_cfloat.d index b593c7e5..92a45cc4 100644 --- a/runtime/typeinfo/ti_array_cfloat.d +++ b/runtime/typeinfos/ti_array_cfloat.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_cfloat; +module runtime.typeinfos.ti_array_cfloat; -import runtime.typeinfo.ti_cfloat; +import runtime.typeinfos.ti_cfloat; class TypeInfo_Aq : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_array_char.d b/runtime/typeinfos/ti_array_char.d similarity index 63% rename from runtime/typeinfo/ti_array_char.d rename to runtime/typeinfos/ti_array_char.d index 9ca0937f..9a7b9ece 100644 --- a/runtime/typeinfo/ti_array_char.d +++ b/runtime/typeinfos/ti_array_char.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_char; +module runtime.typeinfos.ti_array_char; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Aa : ArrayInfo!("char") { } diff --git a/runtime/typeinfo/ti_array_creal.d b/runtime/typeinfos/ti_array_creal.d similarity index 94% rename from runtime/typeinfo/ti_array_creal.d rename to runtime/typeinfos/ti_array_creal.d index 5f510c16..913b5654 100644 --- a/runtime/typeinfo/ti_array_creal.d +++ b/runtime/typeinfos/ti_array_creal.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_creal; +module runtime.typeinfos.ti_array_creal; -import runtime.typeinfo.ti_creal; +import runtime.typeinfos.ti_creal; class TypeInfo_Ac : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_array_dchar.d b/runtime/typeinfos/ti_array_dchar.d similarity index 63% rename from runtime/typeinfo/ti_array_dchar.d rename to runtime/typeinfos/ti_array_dchar.d index 2c783509..45e2fbbd 100644 --- a/runtime/typeinfo/ti_array_dchar.d +++ b/runtime/typeinfos/ti_array_dchar.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_dchar; +module runtime.typeinfos.ti_array_dchar; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Aw : ArrayInfo!("dchar") { } diff --git a/runtime/typeinfo/ti_array_double.d b/runtime/typeinfos/ti_array_double.d similarity index 94% rename from runtime/typeinfo/ti_array_double.d rename to runtime/typeinfos/ti_array_double.d index 4782f2a9..02797445 100644 --- a/runtime/typeinfo/ti_array_double.d +++ b/runtime/typeinfos/ti_array_double.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_double; +module runtime.typeinfos.ti_array_double; -import runtime.typeinfo.ti_double; +import runtime.typeinfos.ti_double; class TypeInfo_Ad : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_array_float.d b/runtime/typeinfos/ti_array_float.d similarity index 94% rename from runtime/typeinfo/ti_array_float.d rename to runtime/typeinfos/ti_array_float.d index 51cc090b..8797c4a8 100644 --- a/runtime/typeinfo/ti_array_float.d +++ b/runtime/typeinfos/ti_array_float.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_float; +module runtime.typeinfos.ti_array_float; -import runtime.typeinfo.ti_float; +import runtime.typeinfos.ti_float; class TypeInfo_Af : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_array_idouble.d b/runtime/typeinfos/ti_array_idouble.d similarity index 72% rename from runtime/typeinfo/ti_array_idouble.d rename to runtime/typeinfos/ti_array_idouble.d index 17e3226b..dfe85aed 100644 --- a/runtime/typeinfo/ti_array_idouble.d +++ b/runtime/typeinfos/ti_array_idouble.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_idouble; +module runtime.typeinfos.ti_array_idouble; -import runtime.typeinfo.ti_array_double; +import runtime.typeinfos.ti_array_double; class TypeInfo_Ap : TypeInfo_Ad { char[] toString() { diff --git a/runtime/typeinfo/ti_array_ifloat.d b/runtime/typeinfos/ti_array_ifloat.d similarity index 72% rename from runtime/typeinfo/ti_array_ifloat.d rename to runtime/typeinfos/ti_array_ifloat.d index 55a87a22..969365f1 100644 --- a/runtime/typeinfo/ti_array_ifloat.d +++ b/runtime/typeinfos/ti_array_ifloat.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_ifloat; +module runtime.typeinfos.ti_array_ifloat; -import runtime.typeinfo.ti_array_float; +import runtime.typeinfos.ti_array_float; class TypeInfo_Ao : TypeInfo_Af { char[] toString() { diff --git a/runtime/typeinfo/ti_array_int.d b/runtime/typeinfos/ti_array_int.d similarity index 62% rename from runtime/typeinfo/ti_array_int.d rename to runtime/typeinfos/ti_array_int.d index 9dc9057a..77c2cd1f 100644 --- a/runtime/typeinfo/ti_array_int.d +++ b/runtime/typeinfos/ti_array_int.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_int; +module runtime.typeinfos.ti_array_int; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Ai : ArrayInfo!("int") { } diff --git a/runtime/typeinfo/ti_array_ireal.d b/runtime/typeinfos/ti_array_ireal.d similarity index 72% rename from runtime/typeinfo/ti_array_ireal.d rename to runtime/typeinfos/ti_array_ireal.d index d3091a00..8094238c 100644 --- a/runtime/typeinfo/ti_array_ireal.d +++ b/runtime/typeinfos/ti_array_ireal.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_ireal; +module runtime.typeinfos.ti_array_ireal; -import runtime.typeinfo.ti_array_real; +import runtime.typeinfos.ti_array_real; class TypeInfo_Aj : TypeInfo_Ae { char[] toString() { diff --git a/runtime/typeinfo/ti_array_long.d b/runtime/typeinfos/ti_array_long.d similarity index 63% rename from runtime/typeinfo/ti_array_long.d rename to runtime/typeinfos/ti_array_long.d index 8b94d855..825546a4 100644 --- a/runtime/typeinfo/ti_array_long.d +++ b/runtime/typeinfos/ti_array_long.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_long; +module runtime.typeinfos.ti_array_long; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Al : ArrayInfo!("long") { } diff --git a/runtime/typeinfo/ti_array_object.d b/runtime/typeinfos/ti_array_object.d similarity index 97% rename from runtime/typeinfo/ti_array_object.d rename to runtime/typeinfos/ti_array_object.d index f46eabfc..25de87d4 100644 --- a/runtime/typeinfo/ti_array_object.d +++ b/runtime/typeinfos/ti_array_object.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_array_object; +module runtime.typeinfos.ti_array_object; class TypeInfo_AC : TypeInfo { hash_t getHash(void *p) { diff --git a/runtime/typeinfo/ti_array_real.d b/runtime/typeinfos/ti_array_real.d similarity index 94% rename from runtime/typeinfo/ti_array_real.d rename to runtime/typeinfos/ti_array_real.d index d72aba4d..4557ef18 100644 --- a/runtime/typeinfo/ti_array_real.d +++ b/runtime/typeinfos/ti_array_real.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_real; +module runtime.typeinfos.ti_array_real; -import runtime.typeinfo.ti_real; +import runtime.typeinfos.ti_real; class TypeInfo_Ae : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_array_short.d b/runtime/typeinfos/ti_array_short.d similarity index 63% rename from runtime/typeinfo/ti_array_short.d rename to runtime/typeinfos/ti_array_short.d index 58307d77..4575a850 100644 --- a/runtime/typeinfo/ti_array_short.d +++ b/runtime/typeinfos/ti_array_short.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_short; +module runtime.typeinfos.ti_array_short; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_As : ArrayInfo!("short") { } diff --git a/runtime/typeinfo/ti_array_ubyte.d b/runtime/typeinfos/ti_array_ubyte.d similarity index 63% rename from runtime/typeinfo/ti_array_ubyte.d rename to runtime/typeinfos/ti_array_ubyte.d index 88189421..e675f0b8 100644 --- a/runtime/typeinfo/ti_array_ubyte.d +++ b/runtime/typeinfos/ti_array_ubyte.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_ubyte; +module runtime.typeinfos.ti_array_ubyte; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Ah : ArrayInfo!("ubyte") { } diff --git a/runtime/typeinfo/ti_array_uint.d b/runtime/typeinfos/ti_array_uint.d similarity index 63% rename from runtime/typeinfo/ti_array_uint.d rename to runtime/typeinfos/ti_array_uint.d index a82b954a..5f3c2761 100644 --- a/runtime/typeinfo/ti_array_uint.d +++ b/runtime/typeinfos/ti_array_uint.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_uint; +module runtime.typeinfos.ti_array_uint; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Ak : ArrayInfo!("uint") { } diff --git a/runtime/typeinfo/ti_array_ulong.d b/runtime/typeinfos/ti_array_ulong.d similarity index 63% rename from runtime/typeinfo/ti_array_ulong.d rename to runtime/typeinfos/ti_array_ulong.d index 0ceca3e0..462c5423 100644 --- a/runtime/typeinfo/ti_array_ulong.d +++ b/runtime/typeinfos/ti_array_ulong.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_ulong; +module runtime.typeinfos.ti_array_ulong; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Am : ArrayInfo!("ulong") { } diff --git a/runtime/typeinfo/ti_array_ushort.d b/runtime/typeinfos/ti_array_ushort.d similarity index 63% rename from runtime/typeinfo/ti_array_ushort.d rename to runtime/typeinfos/ti_array_ushort.d index b572a6e2..f4e633ff 100644 --- a/runtime/typeinfo/ti_array_ushort.d +++ b/runtime/typeinfos/ti_array_ushort.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_ushort; +module runtime.typeinfos.ti_array_ushort; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_At : ArrayInfo!("ushort") { } diff --git a/runtime/typeinfo/ti_array_void.d b/runtime/typeinfos/ti_array_void.d similarity index 72% rename from runtime/typeinfo/ti_array_void.d rename to runtime/typeinfos/ti_array_void.d index d3065dd7..45ec31fb 100644 --- a/runtime/typeinfo/ti_array_void.d +++ b/runtime/typeinfos/ti_array_void.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_array_void; +module runtime.typeinfos.ti_array_void; -import runtime.typeinfo.ti_array_ubyte; +import runtime.typeinfos.ti_array_ubyte; class TypeInfo_Av : TypeInfo_Ah { char[] toString() { diff --git a/runtime/typeinfo/ti_array_wchar.d b/runtime/typeinfos/ti_array_wchar.d similarity index 63% rename from runtime/typeinfo/ti_array_wchar.d rename to runtime/typeinfos/ti_array_wchar.d index 39da6f68..bc1ba22a 100644 --- a/runtime/typeinfo/ti_array_wchar.d +++ b/runtime/typeinfos/ti_array_wchar.d @@ -5,8 +5,8 @@ * */ -module runtime.typeinfo.ti_array_wchar; +module runtime.typeinfos.ti_array_wchar; -import runtime.typeinfo.ti_array; +import runtime.typeinfos.ti_array; class TypeInfo_Au : ArrayInfo!("wchar") { } diff --git a/runtime/typeinfo/ti_assocarray.d b/runtime/typeinfos/ti_assocarray.d similarity index 93% rename from runtime/typeinfo/ti_assocarray.d rename to runtime/typeinfos/ti_assocarray.d index 71ebe56e..bcecd46d 100644 --- a/runtime/typeinfo/ti_assocarray.d +++ b/runtime/typeinfos/ti_assocarray.d @@ -5,6 +5,8 @@ * */ +module runtime.typeinfos.ti_assocarray; + class TypeInfo_AssociativeArray : TypeInfo { char[] toString() { return value.toString() ~ "[" ~ key.toString() ~ "]"; diff --git a/runtime/typeinfo/ti_byte.d b/runtime/typeinfos/ti_byte.d similarity index 94% rename from runtime/typeinfo/ti_byte.d rename to runtime/typeinfos/ti_byte.d index f3c7d27e..221dc6a0 100644 --- a/runtime/typeinfo/ti_byte.d +++ b/runtime/typeinfos/ti_byte.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_byte; +module runtime.typeinfos.ti_byte; // byte diff --git a/runtime/typeinfo/ti_cdouble.d b/runtime/typeinfos/ti_cdouble.d similarity index 96% rename from runtime/typeinfo/ti_cdouble.d rename to runtime/typeinfos/ti_cdouble.d index 8ddb4346..82950cd9 100644 --- a/runtime/typeinfo/ti_cdouble.d +++ b/runtime/typeinfos/ti_cdouble.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_cdouble; +module runtime.typeinfos.ti_cdouble; class TypeInfo_r : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_cfloat.d b/runtime/typeinfos/ti_cfloat.d similarity index 96% rename from runtime/typeinfo/ti_cfloat.d rename to runtime/typeinfos/ti_cfloat.d index d7c1b67d..c7d91411 100644 --- a/runtime/typeinfo/ti_cfloat.d +++ b/runtime/typeinfos/ti_cfloat.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_cfloat; +module runtime.typeinfos.ti_cfloat; class TypeInfo_q : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_char.d b/runtime/typeinfos/ti_char.d similarity index 94% rename from runtime/typeinfo/ti_char.d rename to runtime/typeinfos/ti_char.d index 6ba30c4f..e633cc34 100644 --- a/runtime/typeinfo/ti_char.d +++ b/runtime/typeinfos/ti_char.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_char; +module runtime.typeinfos.ti_char; class TypeInfo_a : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_creal.d b/runtime/typeinfos/ti_creal.d similarity index 96% rename from runtime/typeinfo/ti_creal.d rename to runtime/typeinfos/ti_creal.d index da926d69..77b59fb7 100644 --- a/runtime/typeinfo/ti_creal.d +++ b/runtime/typeinfos/ti_creal.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_creal; +module runtime.typeinfos.ti_creal; class TypeInfo_c : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_dchar.d b/runtime/typeinfos/ti_dchar.d similarity index 94% rename from runtime/typeinfo/ti_dchar.d rename to runtime/typeinfos/ti_dchar.d index 6ec2a8ac..e66ed485 100644 --- a/runtime/typeinfo/ti_dchar.d +++ b/runtime/typeinfos/ti_dchar.d @@ -7,7 +7,7 @@ // dchar -module runtime.d.typeinfo.ti_dchar; +module runtime.typeinfos.ti_dchar; class TypeInfo_w : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_delegate.d b/runtime/typeinfos/ti_delegate.d similarity index 93% rename from runtime/typeinfo/ti_delegate.d rename to runtime/typeinfos/ti_delegate.d index 856b392a..53024f5c 100644 --- a/runtime/typeinfo/ti_delegate.d +++ b/runtime/typeinfos/ti_delegate.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_delegate; +module runtime.typeinfos.ti_delegate; class TypeInfo_Delegate : TypeInfo { hash_t getHash(void *p) { diff --git a/runtime/typeinfo/ti_double.d b/runtime/typeinfos/ti_double.d similarity index 84% rename from runtime/typeinfo/ti_double.d rename to runtime/typeinfos/ti_double.d index 6c0910df..026b1bfd 100644 --- a/runtime/typeinfo/ti_double.d +++ b/runtime/typeinfos/ti_double.d @@ -5,9 +5,15 @@ * */ -module runtime.typeinfo.ti_double; +module runtime.typeinfos.ti_double; -import runtime.util; +int isnan(real e) { + ushort* pe = cast(ushort *)&e; + ulong* ps = cast(ulong *)&e; + + return (pe[4] & 0x7FFF) == 0x7FFF && + *ps & 0x7FFFFFFFFFFFFFFF; +} class TypeInfo_d : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_enum.d b/runtime/typeinfos/ti_enum.d similarity index 63% rename from runtime/typeinfo/ti_enum.d rename to runtime/typeinfos/ti_enum.d index 171e00ba..961607ae 100644 --- a/runtime/typeinfo/ti_enum.d +++ b/runtime/typeinfos/ti_enum.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_enum; +module runtime.typeinfos.ti_enum; -import runtime.typeinfo.ti_typedef; +import runtime.typeinfos.ti_typedef; class TypeInfo_Enum : TypeInfo_Typedef { } diff --git a/runtime/typeinfo/ti_float.d b/runtime/typeinfos/ti_float.d similarity index 94% rename from runtime/typeinfo/ti_float.d rename to runtime/typeinfos/ti_float.d index b813a044..cfb1a514 100644 --- a/runtime/typeinfo/ti_float.d +++ b/runtime/typeinfos/ti_float.d @@ -5,9 +5,7 @@ * */ -module runtime.typeinfo.ti_float; - -import runtime.util; +module runtime.typeinfos.ti_float; class TypeInfo_f : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_function.d b/runtime/typeinfos/ti_function.d similarity index 92% rename from runtime/typeinfo/ti_function.d rename to runtime/typeinfos/ti_function.d index 188c5132..d15f45de 100644 --- a/runtime/typeinfo/ti_function.d +++ b/runtime/typeinfos/ti_function.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_function; +module runtime.typeinfos.ti_function; class TypeInfo_Function : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_idouble.d b/runtime/typeinfos/ti_idouble.d similarity index 70% rename from runtime/typeinfo/ti_idouble.d rename to runtime/typeinfos/ti_idouble.d index 41e0f543..3f49859f 100644 --- a/runtime/typeinfo/ti_idouble.d +++ b/runtime/typeinfos/ti_idouble.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_idouble; +module runtime.typeinfos.ti_idouble; -import runtime.typeinfo.ti_double; +import runtime.typeinfos.ti_double; class TypeInfo_p : TypeInfo_d { char[] toString() { diff --git a/runtime/typeinfo/ti_ifloat.d b/runtime/typeinfos/ti_ifloat.d similarity index 70% rename from runtime/typeinfo/ti_ifloat.d rename to runtime/typeinfos/ti_ifloat.d index 98584c50..8b0a32e6 100644 --- a/runtime/typeinfo/ti_ifloat.d +++ b/runtime/typeinfos/ti_ifloat.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_ifloat; +module runtime.typeinfos.ti_ifloat; -import runtime.typeinfo.ti_float; +import runtime.typeinfos.ti_float; class TypeInfo_o : TypeInfo_f { char[] toString() { diff --git a/runtime/typeinfo/ti_int.d b/runtime/typeinfos/ti_int.d similarity index 95% rename from runtime/typeinfo/ti_int.d rename to runtime/typeinfos/ti_int.d index 3df91f79..325bb6ed 100644 --- a/runtime/typeinfo/ti_int.d +++ b/runtime/typeinfos/ti_int.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_int; +module runtime.typeinfos.ti_int; class TypeInfo_i : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_interface.d b/runtime/typeinfos/ti_interface.d similarity index 97% rename from runtime/typeinfo/ti_interface.d rename to runtime/typeinfos/ti_interface.d index 66e61990..f0a3a026 100644 --- a/runtime/typeinfo/ti_interface.d +++ b/runtime/typeinfos/ti_interface.d @@ -5,6 +5,8 @@ * */ +module runtime.typeinfos.ti_interface; + class TypeInfo_Interface : TypeInfo { char[] toString() { return info.name; diff --git a/runtime/typeinfo/ti_ireal.d b/runtime/typeinfos/ti_ireal.d similarity index 71% rename from runtime/typeinfo/ti_ireal.d rename to runtime/typeinfos/ti_ireal.d index f2cd4cce..36dffd72 100644 --- a/runtime/typeinfo/ti_ireal.d +++ b/runtime/typeinfos/ti_ireal.d @@ -5,9 +5,9 @@ * */ -module runtime.typeinfo.ti_ireal; +module runtime.typeinfos.ti_ireal; -import runtime.typeinfo.ti_real; +import runtime.typeinfos.ti_real; class TypeInfo_j : TypeInfo_e { char[] toString() { diff --git a/runtime/typeinfo/ti_long.d b/runtime/typeinfos/ti_long.d similarity index 95% rename from runtime/typeinfo/ti_long.d rename to runtime/typeinfos/ti_long.d index b82f3f9d..4faa3429 100644 --- a/runtime/typeinfo/ti_long.d +++ b/runtime/typeinfos/ti_long.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_long; +module runtime.typeinfos.ti_long; class TypeInfo_l : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_object.d b/runtime/typeinfos/ti_object.d similarity index 95% rename from runtime/typeinfo/ti_object.d rename to runtime/typeinfos/ti_object.d index 3cf5fbe1..ceda1d71 100644 --- a/runtime/typeinfo/ti_object.d +++ b/runtime/typeinfos/ti_object.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_object; +module runtime.typeinfos.ti_object; class TypeInfo_Class : TypeInfo { hash_t getHash(void *p) { diff --git a/runtime/typeinfo/ti_ptr.d b/runtime/typeinfos/ti_ptr.d similarity index 95% rename from runtime/typeinfo/ti_ptr.d rename to runtime/typeinfos/ti_ptr.d index 526b4e70..5cd732da 100644 --- a/runtime/typeinfo/ti_ptr.d +++ b/runtime/typeinfos/ti_ptr.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_ptr; +module runtime.typeinfos.ti_ptr; class TypeInfo_Pointer : TypeInfo { hash_t getHash(void *p) { diff --git a/runtime/typeinfo/ti_real.d b/runtime/typeinfos/ti_real.d similarity index 94% rename from runtime/typeinfo/ti_real.d rename to runtime/typeinfos/ti_real.d index c7a22c96..3d321127 100644 --- a/runtime/typeinfo/ti_real.d +++ b/runtime/typeinfos/ti_real.d @@ -5,9 +5,7 @@ * */ -module runtime.typeinfo.ti_real; - -import runtime.util; +module runtime.typeinfos.ti_real; class TypeInfo_e : TypeInfo { char[] toString() { return "real"; } diff --git a/runtime/typeinfo/ti_short.d b/runtime/typeinfos/ti_short.d similarity index 94% rename from runtime/typeinfo/ti_short.d rename to runtime/typeinfos/ti_short.d index 4e0613f3..159a5423 100644 --- a/runtime/typeinfo/ti_short.d +++ b/runtime/typeinfos/ti_short.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_short; +module runtime.typeinfos.ti_short; class TypeInfo_s : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_staticarray.d b/runtime/typeinfos/ti_staticarray.d similarity index 96% rename from runtime/typeinfo/ti_staticarray.d rename to runtime/typeinfos/ti_staticarray.d index 3a15dfd6..9e47a6c9 100644 --- a/runtime/typeinfo/ti_staticarray.d +++ b/runtime/typeinfos/ti_staticarray.d @@ -5,7 +5,9 @@ * */ -module runtime.typeinfo.ti_staticarray; +module runtime.typeinfos.ti_staticarray; + +import runtime.util; class TypeInfo_StaticArray : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_struct.d b/runtime/typeinfos/ti_struct.d similarity index 96% rename from runtime/typeinfo/ti_struct.d rename to runtime/typeinfos/ti_struct.d index 17358ec5..ba8477fd 100644 --- a/runtime/typeinfo/ti_struct.d +++ b/runtime/typeinfos/ti_struct.d @@ -5,7 +5,9 @@ * */ -module runtime.typeinfo.ti_struct; +module runtime.typeinfos.ti_struct; + +import runtime.util; class TypeInfo_Struct : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_tuple.d b/runtime/typeinfos/ti_tuple.d similarity index 95% rename from runtime/typeinfo/ti_tuple.d rename to runtime/typeinfos/ti_tuple.d index dd654365..5136f1df 100644 --- a/runtime/typeinfo/ti_tuple.d +++ b/runtime/typeinfos/ti_tuple.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_tuple; +module runtime.typeinfos.ti_tuple; class TypeInfo_Tuple : TypeInfo { TypeInfo[] elements; diff --git a/runtime/typeinfo/ti_typedef.d b/runtime/typeinfos/ti_typedef.d similarity index 95% rename from runtime/typeinfo/ti_typedef.d rename to runtime/typeinfos/ti_typedef.d index 06a08159..13400e8d 100644 --- a/runtime/typeinfo/ti_typedef.d +++ b/runtime/typeinfos/ti_typedef.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_typedef; +module runtime.typeinfos.ti_typedef; class TypeInfo_Typedef : TypeInfo { char[] toString() { return name; } diff --git a/runtime/typeinfo/ti_ubyte.d b/runtime/typeinfos/ti_ubyte.d similarity index 94% rename from runtime/typeinfo/ti_ubyte.d rename to runtime/typeinfos/ti_ubyte.d index e66b8f44..556248ec 100644 --- a/runtime/typeinfo/ti_ubyte.d +++ b/runtime/typeinfos/ti_ubyte.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_ubyte; +module runtime.typeinfos.ti_ubyte; class TypeInfo_h : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_uint.d b/runtime/typeinfos/ti_uint.d similarity index 95% rename from runtime/typeinfo/ti_uint.d rename to runtime/typeinfos/ti_uint.d index 3cecc5dd..05f064fc 100644 --- a/runtime/typeinfo/ti_uint.d +++ b/runtime/typeinfos/ti_uint.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_uint; +module runtime.typeinfos.ti_uint; class TypeInfo_k : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_ulong.d b/runtime/typeinfos/ti_ulong.d similarity index 95% rename from runtime/typeinfo/ti_ulong.d rename to runtime/typeinfos/ti_ulong.d index 7cc2d333..b18a6419 100644 --- a/runtime/typeinfo/ti_ulong.d +++ b/runtime/typeinfos/ti_ulong.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_ulong; +module runtime.typeinfos.ti_ulong; class TypeInfo_m : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_ushort.d b/runtime/typeinfos/ti_ushort.d similarity index 94% rename from runtime/typeinfo/ti_ushort.d rename to runtime/typeinfos/ti_ushort.d index 30a9c11e..64642040 100644 --- a/runtime/typeinfo/ti_ushort.d +++ b/runtime/typeinfos/ti_ushort.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_ushort; +module runtime.typeinfos.ti_ushort; class TypeInfo_t : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_void.d b/runtime/typeinfos/ti_void.d similarity index 94% rename from runtime/typeinfo/ti_void.d rename to runtime/typeinfos/ti_void.d index 61e21766..590b036e 100644 --- a/runtime/typeinfo/ti_void.d +++ b/runtime/typeinfos/ti_void.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_void; +module runtime.typeinfos.ti_void; class TypeInfo_v : TypeInfo { char[] toString() { diff --git a/runtime/typeinfo/ti_wchar.d b/runtime/typeinfos/ti_wchar.d similarity index 94% rename from runtime/typeinfo/ti_wchar.d rename to runtime/typeinfos/ti_wchar.d index 637aa8f4..42e1ee04 100644 --- a/runtime/typeinfo/ti_wchar.d +++ b/runtime/typeinfos/ti_wchar.d @@ -5,7 +5,7 @@ * */ -module runtime.typeinfo.ti_wchar; +module runtime.typeinfos.ti_wchar; class TypeInfo_u : TypeInfo { char[] toString() { diff --git a/runtime/types.d b/runtime/types.d index 45de9a61..545b67db 100644 --- a/runtime/types.d +++ b/runtime/types.d @@ -7,27 +7,3 @@ module runtime.types; -// Figure out the size of a pointer -static if ((ubyte*).sizeof == 8) { - version = Arch64; -} -else static if ((ubyte*).sizeof == 4) { - version = Arch32; -} - -// Pointer sizes -version(Arch32) { - alias uint size_t; - alias int ptrdiff_t; - alias uint hash_t; -} -else { - alias ulong size_t; - alias long ptrdiff_t; - alias ulong hash_t; -} - -// String types -alias char[] string; -alias wchar[] wstring; -alias dchar[] dstring; diff --git a/runtime/util.d b/runtime/util.d new file mode 100644 index 00000000..08bbf89f --- /dev/null +++ b/runtime/util.d @@ -0,0 +1,62 @@ +module runtime.util; + +int memcmp(void* a, void* b, size_t n) { + ubyte* str_a = cast(ubyte*)a; + ubyte* str_b = cast(ubyte*)b; + + for(size_t i = 0; i < n; i++) { + if(*str_a != *str_b) { + return *str_a - *str_b; + } + + str_a++; + str_b++; + } + + return 0; +} + +char[] itoa(char[] buf, char base, long d) { + size_t p = buf.length - 1; + size_t startIdx = 0; + ulong ud = d; + bool negative = false; + + int divisor = 10; + + // If %d is specified and D is minus, put `-' in the head. + if(base == 'd' && d < 0) { + negative = true; + ud = -d; + } + else if(base == 'x') + divisor = 16; + + // Divide UD by DIVISOR until UD == 0. + do { + int remainder = ud % divisor; + buf[p--] = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10; + } + while (ud /= divisor) + + if(negative) + buf[p--] = '-'; + + return buf[p + 1 .. $]; +} + +int isnan(real e) { + ushort* pe = cast(ushort *)&e; + ulong* ps = cast(ulong *)&e; + + return (pe[4] & 0x7FFF) == 0x7FFF && + *ps & 0x7FFFFFFFFFFFFFFF; +} + +void memset(void *addr, int val, uint numBytes) { + ubyte *data = cast(ubyte*) addr; + + for(int i = 0; i < numBytes; i++) { + data[i] = cast(ubyte)val; + } +} diff --git a/synch/thread.d b/synch/thread.d index e3827d50..44a0d048 100644 --- a/synch/thread.d +++ b/synch/thread.d @@ -2,14 +2,6 @@ module synch.thread; import gui.window; -// Awww... my only runtime dependency -version(Tango) { - import Tango = tango.core.Thread; -} -else { - import Phobos = std.thread; -} - import platform.vars.thread; import scaffold.thread; @@ -30,9 +22,6 @@ class Thread { // Description: Will create a normal thread that does not have any external callback functions. this() { - stdThread = new overrideThread(); - stdThread.thread = this; - startTime = time = System.time; } @@ -41,9 +30,6 @@ class Thread { _thread_callback = callback; _thread_f_callback = null; - stdThread = new overrideThread(); - stdThread.thread = this; - startTime = time = System.time; } @@ -52,16 +38,13 @@ class Thread { _thread_f_callback = callback; _thread_callback = null; - stdThread = new overrideThread(); - stdThread.thread = this; + startTime = time = System.time; } ~this() { stop(); } - // the common function for the thread - // Description: This will be called upon execution of the thread. Normally, it will call the delegate, but if overriden, you can provide a function within the class to use as the execution space. void run() { if (_thread_callback !is null) { @@ -90,9 +73,9 @@ class Thread { // Description: This function will tell whether or not the current thread being executed is the thread created via this class. // Returns: Will return true when this thread is the current thread executing and false otherwise. - bool isCurrentThread() { + bool isCurrent() { if (_inited) { - return this is this.current(); //return ThreadIsCurrent(_pfvars); + return ThreadIsCurrent(_pfvars); } return false; @@ -111,24 +94,18 @@ class Thread { void start() { if (!_inited) { RegisterThread(this); - //ThreadStart(_pfvars, this); + ThreadStart(_pfvars, this); startTime = time = System.time; - if (stdThread is null) { - stdThread = new overrideThread(); - } - _inited = true; - stdThread.start(); } } // Description: This function will stop the thread prematurely. void stop() { if (_inited) { - //ThreadStop(_pfvars); - stdThread = null; + ThreadStop(_pfvars); UnregisterThread(this); } _inited = false; @@ -157,17 +134,6 @@ class Thread { static Thread current() { Thread ret; - version(LDC) { - if (Tango.Thread.getThis() in threadById) { - ret = threadById[Tango.Thread.getThis()]; - } - } - else { - if (Phobos.Thread.getThis() in threadById) { - ret = threadById[Phobos.Thread.getThis()]; - } - } - if (ret is null) { } @@ -182,8 +148,6 @@ protected: int _threadProc() { run(); - stdThread = null; - return 0; } @@ -196,80 +160,7 @@ protected: ThreadPlatformVars _pfvars; - overrideThread stdThread; - - version(GNU) - { - } - version(LDC) - { - alias Tango.Thread RuntimeThread; - class overrideThread : Tango.Thread - { - Thread thread; - RuntimeThread runtimeThread; - - this() { - super(&run); - } - - void run() - { - threadById[Tango.Thread.getThis()] = thread; - - try { - thread.run(); - } - catch (Object o) { - // Catch any unhandled exceptions - Debugger.raiseException(cast(Exception)o, thread.wnd, thread); - } - - stdThread = null; - - UnregisterThread(thread); - - return; - } - } - } - else - { - alias Phobos.Thread RuntimeThread; - class overrideThread - { - Thread thread; - RuntimeThread runtimeThread; - - this() { - runtimeThread = new Phobos.Thread(&run); - } - - void start() { - runtimeThread.start(); - } - - int run() { - threadById[Phobos.Thread.getThis()] = thread; - - try { - thread.run(); - } - catch (Object o) { - // Catch any unhandled exceptions - Debugger.raiseException(cast(Exception)o, thread.wnd, thread); - } - - stdThread = null; - - UnregisterThread(thread); - - return 0; - } - } - } - - static Thread[RuntimeThread] threadById; + static Thread[uint] threadById; } void ThreadModuleInit() { @@ -278,14 +169,14 @@ void ThreadModuleInit() { Thread mainThread = new Thread(); mainThread._inited = true; - version(Tango) { - mainThread.stdThread.runtimeThread = Tango.Thread.getThis(); - } - else { - mainThread.stdThread.runtimeThread = Phobos.Thread.getThis(); - } +// version(Tango) { + //mainThread.stdThread.runtimeThread = Tango.Thread.getThis(); +// } +// else { + //mainThread.stdThread.runtimeThread = Phobos.Thread.getThis(); +// } - Thread.threadById[mainThread.stdThread.runtimeThread] = mainThread; + //Thread.threadById[mainThread.stdThread.runtimeThread] = mainThread; } void ThreadUninit(ref Thread t) { diff --git a/winsamp.d b/winsamp.d index 9dd7e541..09f962f2 100644 --- a/winsamp.d +++ b/winsamp.d @@ -64,7 +64,6 @@ import networking.ftp; import spec.specification; import data.queue2; -import Math = tango.math.Math; class MyConsoleApp : Application { static this() { new MyConsoleApp(); } @@ -96,3 +95,7 @@ class MyConsoleApp : Application { ulong freak; Queue2!(string) q; } + +int main(string[] args) { + return 0; +} From e12b9462a99de750d42c4a8a9e314948f1157cbd Mon Sep 17 00:00:00 2001 From: wilkie Date: Wed, 12 May 2010 19:50:24 -0400 Subject: [PATCH 04/14] Added module constructors and destructors. Added some extra interfaces to ModuleInfo. --- core/error.d | 6 ++ runtime/array.d | 7 +- runtime/exception.d | 2 + runtime/gc.d | 4 + runtime/lifetime.d | 178 ++++++++++++++++++++++++++++++++++++------- runtime/main.d | 113 ++++++++++++++++++++++++++- runtime/moduleinfo.d | 58 ++++++++++---- winsamp.d | 85 +++++++++++++++++++++ 8 files changed, 403 insertions(+), 50 deletions(-) diff --git a/core/error.d b/core/error.d index 55f55c24..2deedb06 100644 --- a/core/error.d +++ b/core/error.d @@ -40,6 +40,12 @@ static: } } + class CyclicDependency : RuntimeError { + this(string moduleNameA, string moduleNameB) { + super("Cyclic Dependency detected between " ~ moduleNameA ~ " and " ~ moduleNameB, "", 0); + } + } + // Description: This Error is thrown when a switch statement does not have a default and there is no case available. class NoDefaultCase : RuntimeError { this(string file, ulong line) { diff --git a/runtime/array.d b/runtime/array.d index 719494f5..854f2055 100644 --- a/runtime/array.d +++ b/runtime/array.d @@ -268,7 +268,7 @@ size_t _d_array_cast_len(size_t length, size_t elementSize, size_t newElementSiz } // Description: This runtime function will simply set the length to reflect storing a different type. -void[] _d_arraycast(size_t toElementSize, size_t fromElementSize, void[] array) { +ubyte[] _d_arraycast(size_t toElementSize, size_t fromElementSize, ubyte[] array) { if (toElementSize == fromElementSize) { return array; } @@ -288,9 +288,8 @@ void[] _d_arraycast(size_t toElementSize, size_t fromElementSize, void[] array) size_t newLength = numbytes / toElementSize; - // Set the new length - *cast(size_t*)&array = newLength; - return array; + // Return the updated array length + return array.ptr[0..newLength]; } byte[] _d_arraycopy(size_t size, byte[] from, byte[] to) { diff --git a/runtime/exception.d b/runtime/exception.d index 6ce403d3..5294efa5 100644 --- a/runtime/exception.d +++ b/runtime/exception.d @@ -9,6 +9,7 @@ module runtime.exception; import core.exception; +import binding.c; extern(C): // Description: This function will carefully throw an out of memory exception. @@ -18,6 +19,7 @@ void onOutOfMemoryError() { } void _d_throw_exception(Object e) { + printf("throwing exception\n"); } int _d_eh_personality(int ver, int actions, ulong eh_class, void* info, void* context) { diff --git a/runtime/gc.d b/runtime/gc.d index 344e8682..0dae0de6 100644 --- a/runtime/gc.d +++ b/runtime/gc.d @@ -157,6 +157,10 @@ static: void removeRange(ubyte[] range) { } + size_t query(ubyte[] memory) { + return memory.length; + } + private: void _initialize() { diff --git a/runtime/lifetime.d b/runtime/lifetime.d index b7385079..1899d2da 100644 --- a/runtime/lifetime.d +++ b/runtime/lifetime.d @@ -14,21 +14,19 @@ import runtime.gc; extern(C): Object _d_allocclass(ClassInfo ci) { - return cast(Object)gc_malloc(ci.init.length, BlkAttr.FINALIZE | (ci.flags & 2 ? BlkAttr.NO_SCAN : 0)); + return cast(Object)gc_malloc(ci.init.length, 0); } void* _d_allocmemoryT(TypeInfo ti) { return gc_malloc(ti.tsize(), 0); } -Object _d_newclass(ClassInfo ci) { - return null; -} - void _d_delinterface(Interface*** p) { if (p is null) { return; } + + // I do not know... Interface* pi = **p; Object o = cast(Object)(p - pi.offset); _d_delclass(o); @@ -39,13 +37,21 @@ void _d_delclass(Object p) { return; } + // Object is a pointer to the object space + // So, the classinfo pointer is the first entry in the Object + // So, think of p as an Object*, and thus as a ClassInfo** ClassInfo** pc = cast(ClassInfo**)p; + + // Does the class have a classinfo pointer? if (*pc !is null) { ClassInfo c = **pc; rt_finalize(cast(void*)p); + // Call the deallocator if (c.deallocator !is null) { - //c.deallocator(); + void function(Object) deallocator; + deallocator = cast(void function(Object))c.deallocator; + deallocator(p); return; } } @@ -95,10 +101,8 @@ private template _newarray(bool initialize, bool withZero) { // Initialize all values we can size_t initIndex = 0; -// foreach(size_t idx, ref element; ret) { - for(size_t idx = 0; idx < length; idx++) { -// element = init[initIndex]; - ret[idx] = init[initIndex]; + foreach(ref element; ret) { + element = init[initIndex]; initIndex++; if (initIndex == init.length) { initIndex = 0; @@ -184,17 +188,17 @@ void* _d_newarraymvT(TypeInfo ti, size_t[] dimensions) { // Description: Will delete an array. // array: The array to delete. -void _d_delarray(void[] array) { +void _d_delarray(ubyte[] array) { if (array !is null) { - gc_free(cast(ubyte*)array.ptr); + GarbageCollector.free(array); } } // Description: Will simply remove the memory pointed to a ptr. // ptr: The pointer to the address to free. -void _d_delmemory(void* ptr) { +void _d_delmemory(ubyte* ptr) { if (ptr !is null) { - gc_free(cast(ubyte*)ptr); + gc_free(ptr); } } @@ -205,16 +209,110 @@ void _d_callfinalizer(void* p) { void rt_finalize(void* p, bool det = true) { } -byte* _d_arraysetlengthT(TypeInfo ti, size_t newlength, size_t plength, byte* pdata) { - return null; +private template _arraysetlength(bool initWithZero) { + ubyte* _arraysetlength(TypeInfo ti, size_t length, size_t oldLength, ubyte* oldData) { + size_t elementSize = ti.next.tsize(); + + size_t newSize = length * elementSize; + size_t memorySize = GarbageCollector.query(oldData[0..1]); + + ubyte[] newArray; + + if (memorySize <= newSize) { + // realloc + newArray = GarbageCollector.realloc(oldData[0..1], newSize); + } + else { + // just resize + newArray = oldData[0..newSize]; + } + + // Initialize the new space + static if (initWithZero) { + // Initialize the remaining space with zero + newArray[oldLength*elementSize..newSize] = 0; + } + else { + // Initialize the remaining space with the init value from ti + ubyte[] init = cast(ubyte[])ti.next.init(); + + // If there is no init vector, then just init to zero + if (init is null) { + newArray[oldLength*elementSize..newSize] = 0; + } + else { + // Initialize all values we can + size_t initIndex = 0; + + foreach(ref element; newArray[oldLength*elementSize..newSize]) { + element = init[initIndex]; + initIndex++; + if (initIndex == init.length) { + initIndex = 0; + } + } + } + } + + return newArray.ptr; + } } -byte* _d_arraysetlengthiT(TypeInfo ti, size_t newlength, size_t plength, byte* pdata) { - return null; +// Description: This runtime function will be called when the length of an +// array is updated. It will allocate extra space if necessary and will +// initialize new entries to 0. +// ti: The TypeInfo the represents the base type of the array. +// length: The updated length for the array. +// oldLength: The current length of the array. +// oldData: The pointer to the current array data. +// Returns: The updated pointer of the array data. +ubyte* _d_arraysetlengthT(TypeInfo ti, size_t length, size_t oldLength, ubyte* oldData) { + return _arraysetlength!(true)(ti, length, oldLength, oldData); } -void[] _d_arrayappendT(TypeInfo ti, void[]* px, byte[] y) { - return null; +// Description: This runtime function will be called when the length of an +// array is updated. It will allocate extra space if necessary and will +// initialize new entries to those indicated in the TypeInfo's init array. +// ti: The TypeInfo the represents the base type of the array. +// length: The updated length for the array. +// oldLength: The current length of the array. +// oldData: The pointer to the current array data. +// Returns: The updated pointer of the array data. +ubyte* _d_arraysetlengthiT(TypeInfo ti, size_t length, size_t oldLength, ubyte* oldData) { + return _arraysetlength!(false)(ti, length, oldLength, oldData); +} + +// Description: This runtime function will append two arrays. +// destArray: This is the array that will receive the concatonation. +// srcArray: This is the array that will be concatonated. +// Returns: The updated array. +ubyte[] _d_arrayappendT(TypeInfo ti, ref ubyte[] destArray, ubyte[] srcArray) { + size_t memorySize = GarbageCollector.query(destArray); + size_t elementSize = ti.next.tsize(); + + size_t oldLength = destArray.length; + size_t newSize = (destArray.length + srcArray.length) * elementSize; + + ubyte[] newArray; + if (memorySize <= newSize) { + // realloc + newArray = GarbageCollector.realloc(destArray, newSize); + } + else { + // just resize + newArray = destArray.ptr[0..newSize]; + } + // Add element + for(uint destIdx = oldLength * elementSize; destIdx < newSize; ) { + for(uint srcIdx = 0; srcIdx < srcArray.length * elementSize; srcIdx++) { + newArray[destIdx] = srcArray[srcIdx]; + destIdx++; + } + } + + destArray = (newArray.ptr)[0..oldLength+srcArray.length]; + + return destArray; } // Description: This runtime function will append a single element to an @@ -222,24 +320,48 @@ void[] _d_arrayappendT(TypeInfo ti, void[]* px, byte[] y) { // ti: The TypeInfo of the base type of this array. // array: The array to append the element. // element: The element to append. -byte[] _d_arrayappendcT(TypeInfo ti, ref byte[] array, void* element) { - return null; +ubyte[] _d_arrayappendcT(TypeInfo ti, ref ubyte[] array, ubyte* element) { + size_t memorySize = GarbageCollector.query(array); + size_t elementSize = ti.next.tsize(); + + size_t oldLength = array.length; + size_t newSize = (array.length + 1) * elementSize; + + ubyte[] newArray; + + if (memorySize <= newSize) { + // realloc + newArray = GarbageCollector.realloc(array, newSize); + } + else { + // just resize + newArray = array.ptr[0..newSize]; + } + + // Add element + for(uint i = 0; i < elementSize; i++) { + newArray[newSize-elementSize+i] = element[i]; + } + + // Update length + array = (newArray.ptr)[0..oldLength+1]; + + return array; } byte[] _d_arraycatT(TypeInfo ti, byte[] x, byte[] y) { return null; } + byte[] _d_arraycatnT(TypeInfo ti, uint n, ...) { return null; } -void[] _adDupT(TypeInfo ti, void[] a) { +ubyte[] _adDupT(TypeInfo ti, ubyte[] a) { ubyte[] ret = (cast(ubyte*)_d_newarrayvT(ti, a.length))[0..a.length*ti.next.tsize()]; - ubyte[] array = (cast(ubyte*)a)[0..a.length*ti.next.tsize()]; + ubyte[] array = a.ptr[0..a.length*ti.next.tsize()]; - foreach(size_t idx, ref element; ret) { - element = array[idx]; - } + ret[0..$] = array[0..$]; - return ret; + return ret.ptr[0..a.length]; } diff --git a/runtime/main.d b/runtime/main.d index 626314ea..1d91d1da 100644 --- a/runtime/main.d +++ b/runtime/main.d @@ -8,6 +8,9 @@ module runtime.main; import runtime.gc; +import runtime.moduleinfo; + +import core.error; // The user supplied D entry int main(char[][] args); @@ -18,14 +21,118 @@ int main(char[][] args); // Returns: The error code for the application. import binding.c; -extern(C) int main(int argc, char** argv) { +// Initializes data structures to aid in calling module constructors +private void moduleInfoInitialize() { + // Take the linked list of modules and load them into an array + ModuleReference* mod = _Dmodule_ref; + + size_t moduleCount = 0; + while(mod !is null) { + mod = mod.next; + moduleCount++; + } + + ModuleInfo._modules = new ModuleInfo[moduleCount]; + + mod = _Dmodule_ref; + + size_t idx = 0; + while(mod !is null) { + ModuleInfo._modules[idx] = mod.mod; + idx++; + mod = mod.next; + } + + ModuleInfo._dtors = new ModuleInfo[moduleCount]; + +} + +// Those module constructors that do not depend on other +// constructors being called. +private void moduleIndependentConstructors() { + // Call Module Independent Constructors + foreach(modInfo; ModuleInfo._modules) { + if (modInfo !is null && modInfo.ictor !is null) { + modInfo.ictor(); + } + } +} + +// Calls the module constructors and avoids cycles. +private void moduleConstructors(ModuleInfo from, ModuleInfo[] imports, ref int dtors) { + static const int CtorVisiting = 1; + static const int CtorVisited = 2; + foreach(mod; imports) { + if (mod is null) { + continue; + } + + if (mod.flags == CtorVisited) { + continue; + } + + if (mod.ctor !is null || mod.dtor !is null) { + if (mod.flags & CtorVisiting) { + // Already visiting this node... + // There is a cycle + throw new RuntimeError.CyclicDependency(from.name, mod.name); + } + mod.flags = CtorVisiting; + + moduleConstructors(mod, mod._importedModules, dtors); + + // Run the constructor + if (mod.ctor !is null) { + mod.ctor(); + } + + mod.flags = CtorVisited; + + // Save the destructor for later + if (mod.dtor !is null) { + ModuleInfo._dtors[dtors] = mod; + dtors++; + } + } + else { + mod.flags = CtorVisited; + moduleConstructors(mod, mod._importedModules, dtors); + } + } +} + +// Run each destructor +private void moduleDestructors() { + foreach(mod; ModuleInfo._dtors) { + if (mod.dtor !is null) { + mod.dtor(); + } + } +} + +private extern(C) int main(int argc, char** argv) { // Initialize the garbage collector gc_init(); + moduleInfoInitialize(); + moduleIndependentConstructors(); + + int numDtors = 0; + moduleConstructors(null, ModuleInfo._modules, numDtors); + + ModuleInfo._dtors = ModuleInfo._dtors[0..numDtors]; + // Gather arguments - main([]); + + // Run main + int exitCode = main([]); + + // Run the module destructors + moduleDestructors(); // Terminate the garbage collector gc_term(); - return 0; + + // End the application + return exitCode; } diff --git a/runtime/moduleinfo.d b/runtime/moduleinfo.d index 91b7f499..9085a317 100644 --- a/runtime/moduleinfo.d +++ b/runtime/moduleinfo.d @@ -2,24 +2,28 @@ module runtime.moduleinfo; // Description: This class describes a D module. class ModuleInfo : Object { - string name; - ModuleInfo[] importedModules; - ClassInfo[] localClasses; + string name() { + return _name.dup; + } - uint flags; + int opApply(int delegate(ref ModuleInfo) loopBody) { + int ret = 0; - void function() ctor; - void function() dtor; - void function() unitTest; + foreach(mod; _importedModules) { + ret = loopBody(mod); + if(ret) { + break; + } + } - void* xgetMembers; - void function() ictor; + return ret; + } - static int opApply(int delegate(ref ModuleInfo) loopBody) { + int opApply(int delegate(ref ClassInfo) loopBody) { int ret = 0; - foreach(mod; _modules) { - ret = loopBody(mod); + foreach(lclass; _localClasses) { + ret = loopBody(lclass); if(ret) { break; } @@ -28,18 +32,42 @@ class ModuleInfo : Object { return ret; } - ModuleInfo[] modules() { + static ModuleInfo[] modules() { return _modules.dup; } -private: +package: + string _name; + ModuleInfo[] _importedModules; + ClassInfo[] _localClasses; + + // For Cycle Dependency + uint flags; + + // Constructors, Deconstructors + void function() ctor; + void function() dtor; + void function() unitTest; + + // Special functions + void* xgetMembers; + + // Module Independent Constructor + void function() ictor; + +package: static ModuleInfo[] _modules; + static ModuleInfo[] _dtors; } +package: + // This linked list is created by a compiler generated function inserted // into the .ctor list by the compiler. struct ModuleReference { ModuleReference* next; ModuleInfo mod; } -extern (C) ModuleReference* _Dmodule_ref; // start of linked list + +// Start of the module linked list +extern (C) ModuleReference* _Dmodule_ref; diff --git a/winsamp.d b/winsamp.d index 09f962f2..5278f0b8 100644 --- a/winsamp.d +++ b/winsamp.d @@ -96,6 +96,91 @@ class MyConsoleApp : Application { Queue2!(string) q; } +import binding.c; + +class A { + this(int foo = 5) { + _foo = foo; + printf("class constructor %d\n", foo); + } + + int foobar() { + return _foo; + } +private: + int _foo; +} + int main(string[] args) { + printf("hello world\n"); + printf("---------------\n"); + int[] foobar = new int[10]; + foreach(size_t i, ref element; foobar) { + element = i; + } + foreach(ref element; foobar) { + printf("%d\n", element); + } + printf("%d\n", foobar.length); + printf("---------------\n"); + foobar ~= 42; + foreach(ref element; foobar) { + printf("%d\n", element); + } + printf("%d\n", foobar.length); + printf("---------------\n"); + int[] eff = new int[5]; + foreach(size_t i, ref element; eff) { + element = i + foobar.length; + } + foreach(element;eff) { + printf("%d\n", element); + } + printf("%d\n", eff.length); + printf("---------------\n"); + int[] result = foobar ~ eff; + foreach(element; result) { + printf("%d\n", element); + } + printf("%d\n", result.length); + printf("---------------\n"); + foreach(ref element; foobar) { + printf("%d\n", element); + } + printf("%d\n", foobar.length); + printf("---------------\n"); + foreach(element;eff) { + printf("%d\n", element); + } + printf("%d\n", eff.length); + printf("---------------\n"); + + eff.length = eff.length + 5; + + foreach(element;eff) { + printf("%d\n", element); + } + printf("%d\n", eff.length); + printf("---------------\n"); + + int[] duplicate = result.dup; + foreach(element; duplicate) { + printf("%d\n", element); + } + printf("%d\n", duplicate.length); + printf("---------------\n"); + + duplicate ~= result; + foreach(element; duplicate) { + printf("%d\n", element); + } + printf("%d\n", duplicate.length); + printf("---------------\n"); + + + A a = new A(15); + int ret = a.foobar(); + printf("%d\n", ret); + return 0; } From 5dc04d3a651eb4d30a62d2187785ccef2f48f071 Mon Sep 17 00:00:00 2001 From: wilkie Date: Fri, 14 May 2010 13:06:40 -0400 Subject: [PATCH 05/14] Added a very basic associative array implementation. --- runtime/assocarray.d | 226 +++++++++++++++++++++++++++++++++++-------- runtime/common.d | 1 + runtime/object.d | 5 +- runtime/util.d | 51 ++++++++++ winsamp.d | 25 +++++ 5 files changed, 268 insertions(+), 40 deletions(-) diff --git a/runtime/assocarray.d b/runtime/assocarray.d index 842eed8f..d753d769 100644 --- a/runtime/assocarray.d +++ b/runtime/assocarray.d @@ -8,91 +8,239 @@ module runtime.assocarray; -import runtime.common; +import runtime.gc; + +import synch.atomic; + +import binding.c; extern(C): -struct aaA { - aaA* left; - aaA* right; +struct Entry { hash_t hash; - // key // - // value // + ubyte[] key; + ubyte[] value; } -struct BB { - aaA*[] b; - size_t nodes; - TypeInfo keyti; +struct Bucket { + Entry[3] entries; + ulong usedCount; +} + +struct AssocArray { + Bucket[] buckets; + size_t valuesize; + size_t range; + size_t items; + TypeInfo keyTypeInfo; } // Description: This runtime function will determine the number of entries in // an associative array. // Returns: The number of entries in the array. -size_t _aaLen(AA aa) { - return 0; +size_t _aaLen(ref AssocArray aa) { + return aa.items; +} + +template _aaAccess(bool addKey) { + ubyte* _aaAccess(ref AssocArray* aa, TypeInfo keyti, size_t valuesize, ubyte* pkey) { + + // If we can update the array to add the key, + // and the array does not exist, then create it! + static if (addKey) { + if (aa is null) { + // Create a new associative array + aa = new AssocArray(); + + // Assign the associative array the TypeInfo of the keys + aa.keyTypeInfo = keyti; + + // Set up the default buckets + aa.buckets = new Bucket[8]; + aa.range = 8; + } + } + else { + // If the array does not exist, the item can't... + // so just return null + if (aa is null) { + return null; + } + } + + // Get the hash + hash_t hash = aa.keyTypeInfo.getHash(pkey); + + // Find the bucket + size_t bucketIndex = hash % aa.range; + + // If the bucket is in the lower half, it may have been rehashed to the upper half + if (bucketIndex < aa.buckets.length - aa.range) { + bucketIndex = hash % (2 * aa.range); + } + + printf("bucket index: %d\n", bucketIndex); + + // Search for the value + foreach(size_t idx, bucket; aa.buckets[bucketIndex].entries) { + printf("checking bucket %d @ %p\n", idx, bucket.key.ptr); + if (bucket.key.ptr !is null) { + // compare the bucket with the hash + printf("comparing...\n"); + int cmp = keyti.compare(pkey, bucket.key.ptr); + printf("cmp: %d\n", cmp); + if (cmp == 0) { + // Good, we found the item + printf("item found!\n"); + return bucket.value.ptr; + } + } + } + + printf("hmm, item not found\n"); + static if (addKey) { + // Add the value (rehashing if necessary) + + while(aa.buckets[bucketIndex].usedCount == aa.buckets[bucketIndex].entries.length) { + // Hmm, no free buckets... Rehash + printf("rehashing!\n"); + _aaRehash(aa, keyti); + + } + + // Scan for the empty item + Atomic.increment(aa.buckets[bucketIndex].usedCount); + Atomic.increment(aa.items); + foreach(ref bucket; aa.buckets[bucketIndex].entries) { + if (bucket.key.ptr is null) { + // Add item here by allocating memory for the key and value + bucket.hash = hash; + bucket.key = GarbageCollector.malloc(keyti.tsize()); + bucket.value = GarbageCollector.malloc(valuesize); + + // Insert the key into the table + bucket.key[0..keyti.tsize()] = pkey[0..keyti.tsize()]; + + // Return a pointer to the value + return bucket.value.ptr; + } + } + } + + return null; + } } // Description: This runtime function will return a pointer to the value // in an associative array at a particular key and add the entry if // it does not exist. // Returns: A pointer to the value associated with the given key. -void* _aaGet(BB* aa, TypeInfo keyti, size_t valuesize, void* pkey) { - return null; -} - -void* _aaGetRvalue(BB aa, TypeInfo keyti, size_t valuesize, void* pkey) { - return null; +ubyte* _aaGet(ref AssocArray* aa, TypeInfo keyti, size_t valuesize, ubyte* pkey) { + return _aaAccess!(true)(aa, keyti, valuesize, pkey); } // Description: This runtime function will get a pointer to a value in an // associative array indexed by a key. Invoked via "aa[key]" and "key in aa". // Returns: null when the value is not in aa, the pointer to the value // otherwise. -void* _aaIn(BB aa, TypeInfo keyti, void* pkey) { - return null; +ubyte* _aaIn(ref AssocArray aa, TypeInfo keyti, ubyte* pkey) { + AssocArray* arrayPointer = &aa; + return _aaAccess!(false)(arrayPointer, keyti, 0, pkey); } // Description: This runtime function will delete a value with the given key. // It will do nothing if the value does not exist. -void _aaDel(BB aa, TypeInfo keyti, void* pkey) { +void _aaDel(ref AssocArray aa, TypeInfo keyti, void* pkey) { +} + +// Description: This runtime function will produce an array of keys of an +// associative array. +// Returns: An array of keys. +ubyte[] _aaKeys(ref AssocArray aa, size_t keysize) { + // Sweep through every bucket, appending each key + ubyte[] ret; + foreach(bucket; aa.buckets) { + foreach(entry; bucket.entries) { + if (entry.key.ptr !is null) { + ret ~= entry.key; + } + } + } + return ret[0..aa.items]; } // Description: This runtime function will produce an array of values for // an associative array. // Returns: An array of values. -void[] _aaValues(BB aa, size_t keysize, size_t valuesize) { - return null; +ubyte[] _aaValues(ref AssocArray aa, size_t keysize, size_t valuesize) { + // Sweep through every bucket, appending each key + ubyte[] ret; + foreach(bucket; aa.buckets) { + foreach(entry; bucket.entries) { + if (entry.key.ptr !is null) { + ret ~= entry.value; + } + } + } + return ret[0..aa.items]; } // Description: This runtime function will rehash an associative array. -BB _aaRehash(BB* paa, TypeInfo keyti) { - BB ret; - return ret; -} - -// Description: This runtime function will produce an array of keys of an -// associative array. -// Returns: An array of keys. -void[] _aaKeys(BB aa, size_t keysize) { - return null; +AssocArray* _aaRehash(ref AssocArray* aa, TypeInfo keyti) { + // In this implementation, we will add buckets and rehash when necessary + // This will increase the space. If done prematurely, it may only help + // if we know we are going to do a lot of additional items in a time + // sensitive application. + + size_t rehashBucketIndex = aa.buckets.length - aa.range; + + // Increase the number of buckets + aa.buckets.length = aa.buckets.length + 1; + + // Rehash a bucket + foreach(ref element; aa.buckets[rehashBucketIndex].entries) { + if (element.key.ptr !is null) { + // Rehash + size_t newBucketIndex; + + newBucketIndex = element.hash % (2 * aa.range); + if (newBucketIndex != rehashBucketIndex) { + + // Move (TODO: lockfree atomic exchange?) + foreach(ref newElement; aa.buckets[newBucketIndex].entries) { + if (newElement.key.ptr is null) { + newElement = element; + } + } + + // Remove old references + element.key = null; + element.value = null; + aa.buckets[rehashBucketIndex].usedCount--; + aa.buckets[newBucketIndex].usedCount++; + } + } + } + + // Update range if possible + if (aa.buckets.length == (2 * aa.range)) { + aa.range *= 2; + } + + return aa; } // Description: This runtime function will handle a foreach for an associative // array invoked as foreach(foo; aa). extern (D) typedef int delegate(void *) dg_t; -int _aaApply(BB aa, size_t keysize, dg_t dg) { +int _aaApply(ref AssocArray aa, size_t keysize, dg_t dg) { return 0; } // Description: This runtime function will handle a foreach_reverse for an // associative array invoked as foreach_reverse(foo; aa). extern (D) typedef int delegate(void *, void *) dg2_t; -int _aaApply2(BB aa, size_t keysize, dg2_t dg) { +int _aaApply2(ref AssocArray aa, size_t keysize, dg2_t dg) { return 0; } -BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, - void* keys, void* values) { - return null; -} diff --git a/runtime/common.d b/runtime/common.d index aef410e4..fd38b704 100644 --- a/runtime/common.d +++ b/runtime/common.d @@ -33,6 +33,7 @@ package { NO_MOVE = 0b0000_0100, ALL_BITS = 0b1111_1111 } + } diff --git a/runtime/object.d b/runtime/object.d index 0c79ee16..7d4564d2 100644 --- a/runtime/object.d +++ b/runtime/object.d @@ -7,6 +7,8 @@ module object; +import runtime.util; + // Figure out the size of a pointer static if ((ubyte*).sizeof == 8) { version = Arch64; @@ -45,7 +47,8 @@ class Object { // Description: Computes a hash representing this object hash_t toHash() { - return *cast(hash_t*)this; + // Hash the pointer + return hash(cast(hash_t)this); } // Description: Will compare two Object classes diff --git a/runtime/util.d b/runtime/util.d index 08bbf89f..56e4a930 100644 --- a/runtime/util.d +++ b/runtime/util.d @@ -60,3 +60,54 @@ void memset(void *addr, int val, uint numBytes) { data[i] = cast(ubyte)val; } } + + // Some uniformly distributed hash function + // Reference: http://www.concentric.net/~Ttwang/tech/inthash.htm + hash_t hash(hash_t value) { + static if (hash_t.sizeof == 4) { + // 32 bit hash function + // The commented lines are equivalent to the following line + + int c2 = 0x27d4eb2d; // A prime number or odd constant + + value = (value ^ 61) ^ (value >>> 16); + + // value = value * 9 + value = value + (value << 3); + + value = value ^ (value >>> 4); + + value = value * c2; + + value = value ^ (value >>> 15); + + return value; + } + else static if (hash_t.sizeof == 8) { + // 64 bit hash function + // The commented lines are equivalent to the following line + + // value = (value << 21) - value - 1; + // NOTE: ~value == -value - 1 + value = ~value + (value << 21); + + value = value ^ (value >>> 24); + + // value = value * 265; // That is, value * (1 + 8 + 256) + value = (value + (value >> 3)) + (value << 8); + + value = value ^ (value >>> 14); + + // value = value * 21; // That is, value * (1 + 4 + 16) + value = (value + (value << 2)) + (value << 4); + + value = value ^ (value >>> 28); + value = value + (value << 31); + + return value; + } + else { + static assert(false, "Need a hash function."); + } + } + diff --git a/winsamp.d b/winsamp.d index 5278f0b8..a6cd8c9b 100644 --- a/winsamp.d +++ b/winsamp.d @@ -177,10 +177,35 @@ int main(string[] args) { printf("%d\n", duplicate.length); printf("---------------\n"); + int[] hahaha = [0,1,2,3,6,7,8,9]; + + foreach(element; hahaha) { + printf("%d\n", element); + } + printf("%d\n", hahaha.length); + printf("---------------\n"); + A a = new A(15); + A b = new A(15); int ret = a.foobar(); printf("%d\n", ret); + long flong = 0x1234123412341234; + printf("%lx\n", a.toHash()); + printf("%lx\n", b.toHash()); + + int[int] haha; + haha[4] = 3; + haha[8] = 6; + haha[16] = 13; + haha[32] = 24; + haha[64] = 123; + printf("%d %d %d %d %d\n", haha[4], haha[8], haha[16], haha[32], haha[64]); + printf("length: %d\n", haha.length); + int[] values = haha.keys; + foreach(val; values) { + printf(":%d\n", val); + } return 0; } From 00eff77f05964c07f4e165a5265f6de8ae2ab162 Mon Sep 17 00:00:00 2001 From: wilkie Date: Fri, 14 May 2010 13:12:28 -0400 Subject: [PATCH 06/14] Added deletion of keys to associative array implementation. Lockfree might be a nice addition to it sometime soon. --- runtime/assocarray.d | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/runtime/assocarray.d b/runtime/assocarray.d index d753d769..4a4119d2 100644 --- a/runtime/assocarray.d +++ b/runtime/assocarray.d @@ -42,7 +42,7 @@ size_t _aaLen(ref AssocArray aa) { return aa.items; } -template _aaAccess(bool addKey) { +template _aaAccess(bool addKey, bool deleteKey) { ubyte* _aaAccess(ref AssocArray* aa, TypeInfo keyti, size_t valuesize, ubyte* pkey) { // If we can update the array to add the key, @@ -82,17 +82,25 @@ template _aaAccess(bool addKey) { printf("bucket index: %d\n", bucketIndex); // Search for the value - foreach(size_t idx, bucket; aa.buckets[bucketIndex].entries) { - printf("checking bucket %d @ %p\n", idx, bucket.key.ptr); - if (bucket.key.ptr !is null) { + foreach(size_t idx, entry; aa.buckets[bucketIndex].entries) { + printf("checking bucket %d @ %p\n", idx, entry.key.ptr); + if (entry.key.ptr !is null) { // compare the bucket with the hash printf("comparing...\n"); - int cmp = keyti.compare(pkey, bucket.key.ptr); + int cmp = keyti.compare(pkey, entry.key.ptr); printf("cmp: %d\n", cmp); if (cmp == 0) { // Good, we found the item printf("item found!\n"); - return bucket.value.ptr; + + // Delete the key here + static if (deleteKey) { + entry.key = null; + entry.value = null; + Atomic.decrement(aa.buckets[bucketIndex].usedCount); + } + + return entry.value.ptr; } } } @@ -136,7 +144,7 @@ template _aaAccess(bool addKey) { // it does not exist. // Returns: A pointer to the value associated with the given key. ubyte* _aaGet(ref AssocArray* aa, TypeInfo keyti, size_t valuesize, ubyte* pkey) { - return _aaAccess!(true)(aa, keyti, valuesize, pkey); + return _aaAccess!(true, false)(aa, keyti, valuesize, pkey); } // Description: This runtime function will get a pointer to a value in an @@ -145,12 +153,14 @@ ubyte* _aaGet(ref AssocArray* aa, TypeInfo keyti, size_t valuesize, ubyte* pkey) // otherwise. ubyte* _aaIn(ref AssocArray aa, TypeInfo keyti, ubyte* pkey) { AssocArray* arrayPointer = &aa; - return _aaAccess!(false)(arrayPointer, keyti, 0, pkey); + return _aaAccess!(false, false)(arrayPointer, keyti, 0, pkey); } // Description: This runtime function will delete a value with the given key. // It will do nothing if the value does not exist. -void _aaDel(ref AssocArray aa, TypeInfo keyti, void* pkey) { +void _aaDel(ref AssocArray aa, TypeInfo keyti, ubyte* pkey) { + AssocArray* arrayPointer = &aa; + _aaAccess!(false, true)(arrayPointer, keyti, 0, pkey); } // Description: This runtime function will produce an array of keys of an @@ -216,8 +226,8 @@ AssocArray* _aaRehash(ref AssocArray* aa, TypeInfo keyti) { // Remove old references element.key = null; element.value = null; - aa.buckets[rehashBucketIndex].usedCount--; - aa.buckets[newBucketIndex].usedCount++; + Atomic.decrement(aa.buckets[rehashBucketIndex].usedCount); + Atomic.increment(aa.buckets[newBucketIndex].usedCount); } } } From 72c6d55aa74180e6bd365a2951288855395be2ef Mon Sep 17 00:00:00 2001 From: wilkie Date: Fri, 14 May 2010 13:17:44 -0400 Subject: [PATCH 07/14] Seems to work for associative array literals. --- winsamp.d | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/winsamp.d b/winsamp.d index a6cd8c9b..926edece 100644 --- a/winsamp.d +++ b/winsamp.d @@ -196,16 +196,26 @@ int main(string[] args) { printf("%lx\n", b.toHash()); int[int] haha; + haha[4] = 3; haha[8] = 6; haha[16] = 13; haha[32] = 24; haha[64] = 123; + printf("%d %d %d %d %d\n", haha[4], haha[8], haha[16], haha[32], haha[64]); printf("length: %d\n", haha.length); int[] values = haha.keys; foreach(val; values) { printf(":%d\n", val); } + + int[int] fooaa = [4: 3, 8: 6, 16: 13, 32: 24, 64: 123]; + + values = fooaa.keys; + foreach(val; values) { + printf(":%d\n", val); + } + return 0; } From 084f23d9381197f60e7f999ef240fe88f8b0ba69 Mon Sep 17 00:00:00 2001 From: wilkie Date: Fri, 14 May 2010 14:14:50 -0400 Subject: [PATCH 08/14] Change Exception to match that expected by other libraries... might change it back later. Fixed associative array implementation. It now can compile in the specification tests which are done through an associative array on strings. --- core/exception.d | 5 ++++- runtime/assocarray.d | 47 +++++++++++++++++++++++++------------------- runtime/gc.d | 1 - runtime/lifetime.d | 2 ++ 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/core/exception.d b/core/exception.d index b645f301..8dc766cf 100644 --- a/core/exception.d +++ b/core/exception.d @@ -12,18 +12,21 @@ module core.exception; import core.string; +import binding.c; + class Exception : Object { this(string msg, string file = "", ulong line = 0) { _msg = msg.dup; _file = file.dup; _line = line; + printf("EXCEPTION: %s %s\n", _msg.ptr, _file.ptr); } string name() { return this.classinfo.name.dup; } - string message() { + string msg() { return _msg.dup; } diff --git a/runtime/assocarray.d b/runtime/assocarray.d index 4a4119d2..20d00189 100644 --- a/runtime/assocarray.d +++ b/runtime/assocarray.d @@ -79,19 +79,13 @@ template _aaAccess(bool addKey, bool deleteKey) { bucketIndex = hash % (2 * aa.range); } - printf("bucket index: %d\n", bucketIndex); - // Search for the value foreach(size_t idx, entry; aa.buckets[bucketIndex].entries) { - printf("checking bucket %d @ %p\n", idx, entry.key.ptr); - if (entry.key.ptr !is null) { + if (entry.key !is null) { // compare the bucket with the hash - printf("comparing...\n"); int cmp = keyti.compare(pkey, entry.key.ptr); - printf("cmp: %d\n", cmp); if (cmp == 0) { // Good, we found the item - printf("item found!\n"); // Delete the key here static if (deleteKey) { @@ -100,41 +94,49 @@ template _aaAccess(bool addKey, bool deleteKey) { Atomic.decrement(aa.buckets[bucketIndex].usedCount); } + // Return a reference to the value return entry.value.ptr; } } } - printf("hmm, item not found\n"); static if (addKey) { // Add the value (rehashing if necessary) while(aa.buckets[bucketIndex].usedCount == aa.buckets[bucketIndex].entries.length) { // Hmm, no free buckets... Rehash - printf("rehashing!\n"); _aaRehash(aa, keyti); + // Find the bucket (if it has changed) + bucketIndex = hash % aa.range; + + // If the bucket is in the lower half, it may have been rehashed to the upper half + if (bucketIndex < aa.buckets.length - aa.range) { + bucketIndex = hash % (2 * aa.range); + } + } // Scan for the empty item Atomic.increment(aa.buckets[bucketIndex].usedCount); Atomic.increment(aa.items); - foreach(ref bucket; aa.buckets[bucketIndex].entries) { - if (bucket.key.ptr is null) { + foreach(ref entry; aa.buckets[bucketIndex].entries) { + if (entry.key is null) { // Add item here by allocating memory for the key and value - bucket.hash = hash; - bucket.key = GarbageCollector.malloc(keyti.tsize()); - bucket.value = GarbageCollector.malloc(valuesize); + entry.hash = hash; + entry.key = GarbageCollector.malloc(keyti.tsize()); + entry.value = GarbageCollector.malloc(valuesize); // Insert the key into the table - bucket.key[0..keyti.tsize()] = pkey[0..keyti.tsize()]; + entry.key[0..keyti.tsize()] = pkey[0..keyti.tsize()]; // Return a pointer to the value - return bucket.value.ptr; + return entry.value.ptr; } } } + // Did not find the item return null; } } @@ -171,7 +173,7 @@ ubyte[] _aaKeys(ref AssocArray aa, size_t keysize) { ubyte[] ret; foreach(bucket; aa.buckets) { foreach(entry; bucket.entries) { - if (entry.key.ptr !is null) { + if (entry.key !is null) { ret ~= entry.key; } } @@ -187,7 +189,7 @@ ubyte[] _aaValues(ref AssocArray aa, size_t keysize, size_t valuesize) { ubyte[] ret; foreach(bucket; aa.buckets) { foreach(entry; bucket.entries) { - if (entry.key.ptr !is null) { + if (entry.key !is null) { ret ~= entry.value; } } @@ -209,7 +211,7 @@ AssocArray* _aaRehash(ref AssocArray* aa, TypeInfo keyti) { // Rehash a bucket foreach(ref element; aa.buckets[rehashBucketIndex].entries) { - if (element.key.ptr !is null) { + if (element.key !is null) { // Rehash size_t newBucketIndex; @@ -218,14 +220,19 @@ AssocArray* _aaRehash(ref AssocArray* aa, TypeInfo keyti) { // Move (TODO: lockfree atomic exchange?) foreach(ref newElement; aa.buckets[newBucketIndex].entries) { - if (newElement.key.ptr is null) { + if (newElement.key is null) { newElement = element; + + // We found one, break + // Otherwise, we end up moving element twice :P + break; } } // Remove old references element.key = null; element.value = null; + Atomic.decrement(aa.buckets[rehashBucketIndex].usedCount); Atomic.increment(aa.buckets[newBucketIndex].usedCount); } diff --git a/runtime/gc.d b/runtime/gc.d index 0dae0de6..13fc3501 100644 --- a/runtime/gc.d +++ b/runtime/gc.d @@ -114,7 +114,6 @@ static: } ubyte[] malloc(size_t length) { - printf("malloc %d\n", length); return System.malloc(length); } diff --git a/runtime/lifetime.d b/runtime/lifetime.d index 1899d2da..4f882727 100644 --- a/runtime/lifetime.d +++ b/runtime/lifetime.d @@ -11,6 +11,8 @@ import runtime.exception; import runtime.common; import runtime.gc; +import binding.c; + extern(C): Object _d_allocclass(ClassInfo ci) { From dc35298279702550d9135ba8982a70ad08ec987f Mon Sep 17 00:00:00 2001 From: wilkie Date: Fri, 14 May 2010 23:27:23 -0400 Subject: [PATCH 09/14] Added LDC's exception handling code. All tests pass. --- Makefile | 2 +- compiler/ldc/{cstdarg.di => cstdarg.d} | 0 compiler/ldc/vararg.d | 25 +- core/exception.d | 11 +- data/queue.d | 2 +- examples/MoreDucks/MoreDucks.d | 7 +- io/console.d | 83 ++--- platform/unix/main.d | 13 +- platform/unix/scaffold/thread.d | 7 +- runtests.d | 10 +- runtime/array.d | 16 +- runtime/assocarray.d | 14 +- runtime/classinvariant.d | 5 +- runtime/error.d | 2 +- runtime/exception.d | 410 ++++++++++++++++++++++++- runtime/gc.d | 2 - runtime/lifetime.d | 18 +- runtime/main.d | 56 +++- runtime/monitor.d | 2 + runtime/object.d | 2 +- runtime/typeinfos/ti_array.d | 2 +- spec/packagespecification.d | 1 + specs/data/queue.ds | 2 +- synch/thread.d | 15 +- winsamp.d | 118 ++----- 25 files changed, 600 insertions(+), 225 deletions(-) rename compiler/ldc/{cstdarg.di => cstdarg.d} (100%) diff --git a/Makefile b/Makefile index 785f8c5a..6eca7ab4 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ endif DFILES_PLATFORM_MAC = platform/osx/platform/application.d platform/osx/scaffold/system.d platform/osx/scaffold/thread.d platform/osx/scaffold/time.d platform/osx/scaffold/console.d platform/osx/platform/definitions.d platform/osx/common.d platform/osx/main.d platform/osx/scaffold/opengl.d platform/osx/scaffold/graphics.d platform/osx/scaffold/file.d platform/osx/scaffold/socket.d platform/osx/scaffold/window.d platform/osx/scaffold/color.d platform/osx/scaffold/menu.d platform/osx/scaffold/wave.d platform/osx/scaffold/view.d platform/osx/scaffold/directory.d platform/osx/gui/apploop.d binding/c.d binding/ncurses/ncurses.d platform/osx/scaffold/cui.d platform/osx/platform/vars/cui.d platform/osx/platform/vars/file.d platform/unix/common.d platform/osx/platform/vars/thread.d platform/osx/platform/vars/directory.d platform/osx/platform/vars/view.d platform/osx/platform/vars/semaphore.d platform/osx/platform/vars/condition.d platform/osx/platform/vars/window.d OBJC_FILES = platform/osx/objc/window.m platform/osx/objc/app.m platform/osx/objc/view.m -DFILES_PLATFORM_UNIX = platform/unix/platform/application.d platform/unix/scaffold/system.d platform/unix/scaffold/thread.d platform/unix/scaffold/time.d platform/unix/scaffold/console.d platform/unix/platform/definitions.d platform/unix/common.d binding/cairo/cairo.d binding/x/Xlib.d binding/x/X.d platform/unix/main.d platform/unix/scaffold/opengl.d platform/unix/scaffold/graphics.d platform/unix/scaffold/file.d platform/unix/scaffold/socket.d platform/unix/scaffold/window.d platform/unix/scaffold/color.d platform/unix/scaffold/menu.d platform/unix/scaffold/wave.d platform/unix/scaffold/view.d platform/unix/scaffold/directory.d platform/unix/gui/apploop.d platform/unix/gui/osbutton.d binding/c.d binding/ncurses/ncurses.d platform/unix/scaffold/cui.d platform/unix/platform/vars/cui.d platform/unix/platform/vars/wave.d platform/unix/platform/vars/window.d platform/unix/platform/vars/menu.d platform/unix/platform/vars/view.d platform/unix/platform/vars/region.d platform/unix/platform/vars/brush.d platform/unix/platform/vars/pen.d platform/unix/platform/vars/font.d binding/pango/pango.d platform/unix/platform/vars/directory.d platform/unix/platform/vars/file.d platform/unix/platform/vars/thread.d platform/unix/platform/vars/mutex.d platform/unix/platform/vars/semaphore.d platform/unix/platform/vars/library.d platform/unix/platform/vars/socket.d platform/unix/platform/vars/condition.d binding/cairo/xlib.d binding/cairo/features.d binding/pango/font.d binding/pango/types.d binding/pango/context.d binding/pango/pbreak.d binding/pango/engine.d binding/pango/fontset.d binding/pango/coverage.d binding/pango/glyph.d binding/pango/matrix.d binding/pango/attributes.d binding/pango/layout.d binding/pango/cairo.d binding/pango/script.d binding/pango/gravity.d binding/pango/fontmap.d binding/pango/item.d binding/pango/tabs.d binding/pango/glyphitem.d compiler/ldc/vararg.d compiler/ldc/cstdarg.di +DFILES_PLATFORM_UNIX = platform/unix/platform/application.d platform/unix/scaffold/system.d platform/unix/scaffold/thread.d platform/unix/scaffold/time.d platform/unix/scaffold/console.d platform/unix/platform/definitions.d platform/unix/common.d binding/cairo/cairo.d binding/x/Xlib.d binding/x/X.d platform/unix/main.d platform/unix/scaffold/opengl.d platform/unix/scaffold/graphics.d platform/unix/scaffold/file.d platform/unix/scaffold/socket.d platform/unix/scaffold/window.d platform/unix/scaffold/color.d platform/unix/scaffold/menu.d platform/unix/scaffold/wave.d platform/unix/scaffold/view.d platform/unix/scaffold/directory.d platform/unix/gui/apploop.d platform/unix/gui/osbutton.d binding/c.d binding/ncurses/ncurses.d platform/unix/scaffold/cui.d platform/unix/platform/vars/cui.d platform/unix/platform/vars/wave.d platform/unix/platform/vars/window.d platform/unix/platform/vars/menu.d platform/unix/platform/vars/view.d platform/unix/platform/vars/region.d platform/unix/platform/vars/brush.d platform/unix/platform/vars/pen.d platform/unix/platform/vars/font.d binding/pango/pango.d platform/unix/platform/vars/directory.d platform/unix/platform/vars/file.d platform/unix/platform/vars/thread.d platform/unix/platform/vars/mutex.d platform/unix/platform/vars/semaphore.d platform/unix/platform/vars/library.d platform/unix/platform/vars/socket.d platform/unix/platform/vars/condition.d binding/cairo/xlib.d binding/cairo/features.d binding/pango/font.d binding/pango/types.d binding/pango/context.d binding/pango/pbreak.d binding/pango/engine.d binding/pango/fontset.d binding/pango/coverage.d binding/pango/glyph.d binding/pango/matrix.d binding/pango/attributes.d binding/pango/layout.d binding/pango/cairo.d binding/pango/script.d binding/pango/gravity.d binding/pango/fontmap.d binding/pango/item.d binding/pango/tabs.d binding/pango/glyphitem.d compiler/ldc/vararg.d compiler/ldc/cstdarg.d DFILES_PLATFORM_WIN = binding/win32/gdipluscolormatrix.d binding/win32/gdiplusinit.d binding/win32/gdiplusmem.d binding/win32/gdiplusbase.d binding/win32/gdiplusflat.d binding/win32/gdiplusstringformat.d binding/win32/gdiplusmetafile.d binding/win32/gdipluslinecaps.d binding/win32/gdiplusimagecodec.d binding/win32/gdiplusgpstubs.d binding/win32/gdiplusfontfamily.d binding/win32/gdiplusfontcollection.d binding/win32/gdiplusfont.d binding/win32/gdiplusenums.d binding/win32/gdiplustypes.d binding/win32/gdiplusregion.d binding/win32/gdipluscolor.d binding/win32/gdiplusbitmap.d binding/win32/gdipluseffects.d binding/win32/gdipluscachedbitmap.d binding/win32/gdipluspath.d binding/win32/gdiplusbrush.d binding/win32/gdipluspen.d binding/win32/gdiplusgraphics.d binding/win32/ws2def.d binding/win32/winsock2.d binding/win32/inaddr.d binding/win32/mmsystem.d binding/win32/wincon.d binding/win32/winbase.d binding/win32/winuser.d binding/win32/windef.d binding/win32/wingdi.d platform/win/platform/application.d platform/win/platform/vars/cui.d platform/win/scaffold/cui.d platform/win/scaffold/system.d platform/win/main.d platform/win/common.d platform/win/platform/vars/menu.d platform/win/platform/vars/view.d platform/win/platform/vars/semaphore.d platform/win/platform/vars/mutex.d platform/win/platform/vars/region.d platform/win/platform/vars/library.d platform/win/platform/vars/wave.d platform/win/platform/vars/pen.d platform/win/platform/vars/brush.d platform/win/platform/vars/window.d platform/win/platform/vars/file.d platform/win/platform/vars/directory.d platform/win/platform/vars/font.d platform/win/platform/vars/socket.d platform/win/scaffold/console.d platform/win/platform/definitions.d platform/win/scaffold/wave.d platform/win/scaffold/directory.d platform/win/scaffold/graphics.d platform/win/scaffold/thread.d platform/win/scaffold/menu.d platform/win/scaffold/window.d platform/win/scaffold/view.d platform/win/scaffold/color.d platform/win/scaffold/file.d platform/win/scaffold/socket.d platform/win/gui/osbutton.d platform/win/scaffold/time.d platform/win/widget.d platform/win/scaffold/opengl.d platform/win/widget.d platform/win/gui/apploop.d DFILES_PLATFORM_XOMB = platform/xomb/main.d platform/xomb/common.d platform/xomb/scaffold.d platform/xomb/vars.d platform/xomb/console.d platform/xomb/definitions.d platform/xomb/scaffolds/wave.d platform/xomb/scaffolds/graphics.d platform/xomb/scaffolds/thread.d platform/xomb/scaffolds/menu.d platform/xomb/scaffolds/window.d platform/xomb/scaffolds/view.d platform/xomb/scaffolds/color.d platform/xomb/scaffolds/file.d platform/xomb/scaffolds/socket.d platform/xomb/scaffolds/app.d platform/xomb/scaffolds/time.d platform/xomb/oscontrolinterface.d diff --git a/compiler/ldc/cstdarg.di b/compiler/ldc/cstdarg.d similarity index 100% rename from compiler/ldc/cstdarg.di rename to compiler/ldc/cstdarg.d diff --git a/compiler/ldc/vararg.d b/compiler/ldc/vararg.d index 515cbb07..add8e15a 100644 --- a/compiler/ldc/vararg.d +++ b/compiler/ldc/vararg.d @@ -7,37 +7,18 @@ module ldc.vararg; // Check for the right compiler -version(LDC) -{ +version(LDC) { // OK } -else -{ +else { static assert(false, "This module is only valid for LDC"); } alias void* va_list; -void va_start(T) ( out va_list ap, ref T parmn ) -{ - // not needed ! -} - -T va_arg(T)(ref va_list vp) -{ +T va_arg(T)(ref va_list vp) { T* arg = cast(T*) vp; // ldc always aligns to size_t.sizeof in vararg lists vp = cast(va_list) ( cast(void*) vp + ( ( T.sizeof + size_t.sizeof - 1 ) & ~( size_t.sizeof - 1 ) ) ); return *arg; } - -void va_end( va_list ap ) -{ - // not needed ! -} - -void va_copy( out va_list dst, va_list src ) -{ - // seems pretty useless ! - dst = src; -} diff --git a/core/exception.d b/core/exception.d index 8dc766cf..75ffeaa7 100644 --- a/core/exception.d +++ b/core/exception.d @@ -12,14 +12,11 @@ module core.exception; import core.string; -import binding.c; - class Exception : Object { this(string msg, string file = "", ulong line = 0) { _msg = msg.dup; _file = file.dup; _line = line; - printf("EXCEPTION: %s %s\n", _msg.ptr, _file.ptr); } string name() { @@ -77,8 +74,8 @@ static: // Exceptions for data structures abstract class DataException : Exception { - this(string msg) { - super(msg); + this(string msg, string file = "", ulong line = 0) { + super(msg, file, line); } static: @@ -90,8 +87,8 @@ static: } class OutOfBounds : DataException { - this(string objectName) { - super("Index out of bounds in " ~ objectName); + this(string objectName, string file = "", ulong line = 0) { + super("Index out of bounds in " ~ objectName, file, line); } } diff --git a/data/queue.d b/data/queue.d index f9e8b1ab..8ae647c2 100644 --- a/data/queue.d +++ b/data/queue.d @@ -17,7 +17,7 @@ class Queue(T) : Iterable!(T) { // data: The information you wish to store. It must correspond to the type of data you specified in the declaration of the class. void add(T data) { synchronized(this) { - LinkedListNode* newNode = new LinkedListNode; + LinkedListNode* newNode = new LinkedListNode(); newNode.data = data; if (_head is null) { diff --git a/examples/MoreDucks/MoreDucks.d b/examples/MoreDucks/MoreDucks.d index 7b79bc96..b2da5c88 100644 --- a/examples/MoreDucks/MoreDucks.d +++ b/examples/MoreDucks/MoreDucks.d @@ -82,7 +82,6 @@ private: class MyApp : GuiApplication { // Start an application instance - static this() { new MyApp(); } override void onApplicationStart() { wnd = new MyWindow(); @@ -97,3 +96,9 @@ class MyApp : GuiApplication { private: MyWindow wnd; } + +int main() { + auto app = new MyApp(); + app.run(); + return 0; +} diff --git a/io/console.d b/io/console.d index 3c1887f8..3d56ffa8 100644 --- a/io/console.d +++ b/io/console.d @@ -26,8 +26,8 @@ static: //Description: Sets the foreground color for the console. //fgclr: The foreground color to set. void forecolor(Color clr) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _fgcolor = clr; ConsoleSetColors(_fgcolor, _bgcolor); @@ -38,8 +38,8 @@ static: } void backcolor(Color clr) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _bgcolor = clr; ConsoleSetColors(_fgcolor, _bgcolor); @@ -51,36 +51,36 @@ static: // Description: Clears the console screen. void clear() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); ConsoleClear(); } void position(Coord coord) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _position(coord.x, coord.y); } void position(uint[] coord) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _position(coord[0], coord[1]); } void position(uint x, uint y) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _position(x,y); } Coord position() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); Coord ret; ConsoleGetPosition(cast(uint*)&ret.x,cast(uint*)&ret.y); @@ -91,16 +91,16 @@ static: // x: The number of columns for the caret to move. Negative values move down. // y: The number of rows for the caret. Negative values move up. void setRelative(int x, int y) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); ConsoleSetRelative(x,y); } // Description: Will show the caret. void showCaret() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); if (!_caretVisible) { ConsoleShowCaret(); @@ -110,8 +110,8 @@ static: // Description: Will hide the caret. void hideCaret() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); if (_caretVisible) { ConsoleHideCaret(); @@ -152,16 +152,16 @@ static: // Description: This function will save the current clipping context. void clipSave() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _clippingStack ~= _clippingRegions; } // Description: This function will restore a former clipping context. void clipRestore() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); if (_clippingStack.length > 0) { _clippingRegions = _clippingStack[$-1]; @@ -171,8 +171,8 @@ static: // Description: This function will clear the clipping context. void clipClear() { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _clippingRegions = null; } @@ -180,8 +180,8 @@ static: // Description: This function will add a rectangular region defined as screen coordinates that will clip the drawing surface. When a clipping context is not clear, only regions within rectangles will be drawn to the screen. // region: The rectangular region to add as a clipping region. void clipRect(Rect region) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _clippingRegions ~= region; } @@ -207,31 +207,31 @@ static: } void putv(Variadic vars) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _putv(vars); } void putlnv(Variadic vars) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _putv(vars); _putChar('\n'); } void putStringAt(uint x, uint y, string str) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _position(x,y); _putstring(str); } void putString(string str) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _putstring(str); } @@ -243,8 +243,8 @@ static: } void putAtv(uint x, uint y, Variadic vars) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _position(x, y); _putv(vars); @@ -253,13 +253,16 @@ static: // Description: Will print out this character to the screen and the current location. // chr: The UTF-32 character to print. void putChar(dchar chr) { - _lock.lock(); - scope(exit) _lock.unlock(); + lock(); + scope(exit) unlock(); _putChar(chr); } void lock() { + if (_lock is null) { + _lock = new Mutex(); + } _lock.lock(); } diff --git a/platform/unix/main.d b/platform/unix/main.d index 6b485720..9af01eb4 100644 --- a/platform/unix/main.d +++ b/platform/unix/main.d @@ -101,8 +101,9 @@ void AppInit() { } import binding.c; -/+int main(string[] args){ -/* try { +/* +int main(string[] args){ + try { printf("fudge\n"); AppInit(); printf("fudge\n"); @@ -118,8 +119,8 @@ import binding.c; } catch(Object o) { Debugger.raiseException(cast(Exception)o); - }*/ + } -// return ApplicationController.instance.exitCode; - return 0; -}+/ + return ApplicationController.instance.exitCode; +// return 0; +}*/ diff --git a/platform/unix/scaffold/thread.d b/platform/unix/scaffold/thread.d index 32fbc6fe..0bf26ffd 100644 --- a/platform/unix/scaffold/thread.d +++ b/platform/unix/scaffold/thread.d @@ -84,8 +84,11 @@ void ThreadStop(ref ThreadPlatformVars threadVars) } } -bool ThreadIsCurrent(ref ThreadPlatformVars threadVars) -{ +uint ThreadIdentifier() { + return pthread_self(); +} + +bool ThreadIsCurrent(ref ThreadPlatformVars threadVars) { return false; } diff --git a/runtests.d b/runtests.d index 71febf8d..bf7c8f95 100644 --- a/runtests.d +++ b/runtests.d @@ -41,8 +41,6 @@ private: // Do not change the class name, it is used in a test for djehuty proper! class DjehutyTester : Application { - static this() { new DjehutyTester(); } - void onApplicationStart() { options = new Opts(); @@ -85,6 +83,8 @@ private: } Console.putln(" ".times((packName ~ "." ~ ms.name).length), " : ", item.name, " ", feature); } + else { + } numFailures += tester.failures; numSuccesses += tester.successes; } @@ -109,3 +109,9 @@ private: Opts options; } + +int main() { + auto app = new DjehutyTester(); + app.run(); + return 0; +} diff --git a/runtime/array.d b/runtime/array.d index 854f2055..a7a8c0c0 100644 --- a/runtime/array.d +++ b/runtime/array.d @@ -100,6 +100,10 @@ int _adEq(void[] a1, void[] a2, TypeInfo ti) { // Description: This runtime function sorts an array and is invoked with // the sort property: array.sort ubyte[] _adSort(ubyte[] array, TypeInfo ti) { + if (array is null) { + return null; + } + ubyte[] cmp = (array.ptr)[0..array.length * ti.tsize()]; _qsort(cmp, ti.tsize(), ti); return array; @@ -207,23 +211,23 @@ void _d_array_init_i1(bool* array, size_t length, bool value) { _array_init(array[0..length], value); } -void _d_array_init_i8(ubyte[] array, ubyte value) { +void _d_array_init_i8(ubyte* array, size_t length, ubyte value) { _array_init(array[0..length], value); } -void _d_array_init_i16(ushort[] array, ushort value) { +void _d_array_init_i16(ushort* array, size_t length, ushort value) { _array_init(array[0..length], value); } -void _d_array_init_i32(uint[] array, uint value) { +void _d_array_init_i32(uint* array, size_t length, uint value) { _array_init(array[0..length], value); } -void _d_array_init_i64(ulong[] array, ulong value) { +void _d_array_init_i64(ulong* array, size_t length, ulong value) { _array_init(array[0..length], value); } -void _d_array_init_float(float[] array, float value) { +void _d_array_init_float(float* array, size_t length, float value) { _array_init(array[0..length], value); } @@ -235,7 +239,7 @@ void _d_array_init_pointer(void** array, size_t length, void* value) { _array_init(array[0..length], value); } -void _d_array_init_cdouble(cdouble[] array, cdouble value) { +void _d_array_init_cdouble(cdouble* array, size_t length, cdouble value) { _array_init(array[0..length], value); } diff --git a/runtime/assocarray.d b/runtime/assocarray.d index 20d00189..7012f918 100644 --- a/runtime/assocarray.d +++ b/runtime/assocarray.d @@ -56,8 +56,9 @@ template _aaAccess(bool addKey, bool deleteKey) { aa.keyTypeInfo = keyti; // Set up the default buckets - aa.buckets = new Bucket[8]; - aa.range = 8; + static const int startingSize = 2048; + aa.buckets = new Bucket[startingSize]; + aa.range = startingSize; } } else { @@ -92,6 +93,7 @@ template _aaAccess(bool addKey, bool deleteKey) { entry.key = null; entry.value = null; Atomic.decrement(aa.buckets[bucketIndex].usedCount); + Atomic.decrement(aa.items); } // Return a reference to the value @@ -170,6 +172,10 @@ void _aaDel(ref AssocArray aa, TypeInfo keyti, ubyte* pkey) { // Returns: An array of keys. ubyte[] _aaKeys(ref AssocArray aa, size_t keysize) { // Sweep through every bucket, appending each key + if (&aa is null) { + return null; + } + ubyte[] ret; foreach(bucket; aa.buckets) { foreach(entry; bucket.entries) { @@ -186,6 +192,10 @@ ubyte[] _aaKeys(ref AssocArray aa, size_t keysize) { // Returns: An array of values. ubyte[] _aaValues(ref AssocArray aa, size_t keysize, size_t valuesize) { // Sweep through every bucket, appending each key + if (&aa is null) { + return null; + } + ubyte[] ret; foreach(bucket; aa.buckets) { foreach(entry; bucket.entries) { diff --git a/runtime/classinvariant.d b/runtime/classinvariant.d index fd62805d..00d08a90 100644 --- a/runtime/classinvariant.d +++ b/runtime/classinvariant.d @@ -20,7 +20,10 @@ void _d_invariant(Object o) { do { // If the class has an invariant defined, execute it if(c.classInvariant !is null) { - (*c.classInvariant)(o); + void delegate() inv; + inv.ptr = cast(void*) o; + inv.funcptr = cast(void function()) c.classInvariant; + inv(); } // Go up class hierarchy, return the next ClassInfo diff --git a/runtime/error.d b/runtime/error.d index e957203d..882df257 100644 --- a/runtime/error.d +++ b/runtime/error.d @@ -32,7 +32,7 @@ void _d_assert_msg(string msg, string file, uint line ) { // file: The file that contains the error. // line: The line number of the error. void _d_array_bounds(string file, uint line ) { - throw new DataException.OutOfBounds("Array"); + throw new DataException.OutOfBounds("Array", file, line); } // Description: This is called when there is no valid case for the switch. diff --git a/runtime/exception.d b/runtime/exception.d index 5294efa5..70b558b0 100644 --- a/runtime/exception.d +++ b/runtime/exception.d @@ -7,24 +7,410 @@ module runtime.exception; -import core.exception; +// debug = EH_personality; -import binding.c; -extern(C): +// current EH implementation works on x86 +// if it has a working unwind runtime +version(X86) { + version(linux) version=X86_UNWIND; + version(darwin) version=X86_UNWIND; + version(solaris) version=X86_UNWIND; + version(freebsd) version=X86_UNWIND; +} +version(X86_64) { + version(linux) version=X86_UNWIND; + version(darwin) version=X86_UNWIND; + version(solaris) version=X86_UNWIND; + version(freebsd) version=X86_UNWIND; +} + +//version = HP_LIBUNWIND; + +private extern(C) void abort(); + +// D runtime functions +extern(C) { + int _d_isbaseof(ClassInfo oc, ClassInfo c); +} + +// libunwind headers +extern(C) +{ + enum _Unwind_Reason_Code : int + { + NO_REASON = 0, + FOREIGN_EXCEPTION_CAUGHT = 1, + FATAL_PHASE2_ERROR = 2, + FATAL_PHASE1_ERROR = 3, + NORMAL_STOP = 4, + END_OF_STACK = 5, + HANDLER_FOUND = 6, + INSTALL_CONTEXT = 7, + CONTINUE_UNWIND = 8 + } + + enum _Unwind_Action : int + { + SEARCH_PHASE = 1, + CLEANUP_PHASE = 2, + HANDLER_PHASE = 3, + FORCE_UNWIND = 4 + } + + alias void* _Unwind_Context_Ptr; + + alias void function(_Unwind_Reason_Code, _Unwind_Exception*) _Unwind_Exception_Cleanup_Fn; -// Description: This function will carefully throw an out of memory exception. -void onOutOfMemoryError() { - // Throw this without allocation - throw cast(MemoryException.OutOfMemory)cast(void*)MemoryException.OutOfMemory.classinfo.init; + struct _Unwind_Exception + { + ulong exception_class; + _Unwind_Exception_Cleanup_Fn exception_cleanup; + ptrdiff_t private_1; + ptrdiff_t private_2; + } + +// interface to HP's libunwind from http://www.nongnu.org/libunwind/ +version(HP_LIBUNWIND) +{ + void __libunwind_Unwind_Resume(_Unwind_Exception *); + _Unwind_Reason_Code __libunwind_Unwind_RaiseException(_Unwind_Exception *); + ptrdiff_t __libunwind_Unwind_GetLanguageSpecificData(_Unwind_Context_Ptr + context); + ptrdiff_t __libunwind_Unwind_GetIP(_Unwind_Context_Ptr context); + ptrdiff_t __libunwind_Unwind_SetIP(_Unwind_Context_Ptr context, + ptrdiff_t new_value); + ptrdiff_t __libunwind_Unwind_SetGR(_Unwind_Context_Ptr context, int index, + ptrdiff_t new_value); + ptrdiff_t __libunwind_Unwind_GetRegionStart(_Unwind_Context_Ptr context); + + alias __libunwind_Unwind_Resume _Unwind_Resume; + alias __libunwind_Unwind_RaiseException _Unwind_RaiseException; + alias __libunwind_Unwind_GetLanguageSpecificData + _Unwind_GetLanguageSpecificData; + alias __libunwind_Unwind_GetIP _Unwind_GetIP; + alias __libunwind_Unwind_SetIP _Unwind_SetIP; + alias __libunwind_Unwind_SetGR _Unwind_SetGR; + alias __libunwind_Unwind_GetRegionStart _Unwind_GetRegionStart; +} +else version(X86_UNWIND) +{ + void _Unwind_Resume(_Unwind_Exception*); + _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception*); + ptrdiff_t _Unwind_GetLanguageSpecificData(_Unwind_Context_Ptr context); + ptrdiff_t _Unwind_GetIP(_Unwind_Context_Ptr context); + ptrdiff_t _Unwind_SetIP(_Unwind_Context_Ptr context, ptrdiff_t new_value); + ptrdiff_t _Unwind_SetGR(_Unwind_Context_Ptr context, int index, + ptrdiff_t new_value); + ptrdiff_t _Unwind_GetRegionStart(_Unwind_Context_Ptr context); +} +else +{ + // runtime calls these directly + void _Unwind_Resume(_Unwind_Exception*) + { + } + _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception*) + { + return _Unwind_Reason_Code.FATAL_PHASE1_ERROR; + } +} + +} + +// error and exit +extern(C) private void fatalerror(char[] format) +{ + abort(); } -void _d_throw_exception(Object e) { - printf("throwing exception\n"); + +// helpers for reading certain DWARF data +private ubyte* get_uleb128(ubyte* addr, ref size_t res) +{ + res = 0; + size_t bitsize = 0; + + // read as long as high bit is set + while(*addr & 0x80) { + res |= (*addr & 0x7f) << bitsize; + bitsize += 7; + addr += 1; + if(bitsize >= size_t.sizeof*8) + fatalerror("tried to read uleb128 that exceeded size of size_t"); + } + // read last + if(bitsize != 0 && *addr >= 1 << size_t.sizeof*8 - bitsize) + fatalerror("Fatal error in EH code: tried to read uleb128 that exceeded size of size_t"); + res |= (*addr) << bitsize; + + return addr + 1; +} + +private ubyte* get_sleb128(ubyte* addr, ref ptrdiff_t res) +{ + res = 0; + size_t bitsize = 0; + + // read as long as high bit is set + while(*addr & 0x80) { + res |= (*addr & 0x7f) << bitsize; + bitsize += 7; + addr += 1; + if(bitsize >= size_t.sizeof*8) + fatalerror("tried to read sleb128 that exceeded size of size_t"); + } + // read last + if(bitsize != 0 && *addr >= 1 << size_t.sizeof*8 - bitsize) + fatalerror("tried to read sleb128 that exceeded size of size_t"); + res |= (*addr) << bitsize; + + // take care of sign + if(bitsize < size_t.sizeof*8 && ((*addr) & 0x40)) + res |= cast(ptrdiff_t)(-1) ^ ((1 << (bitsize+7)) - 1); + + return addr + 1; +} + + +// exception struct used by the runtime. +// _d_throw allocates a new instance and passes the address of its +// _Unwind_Exception member to the unwind call. The personality +// routine is then able to get the whole struct by looking at the data +// surrounding the unwind info. +struct _d_exception +{ + Object exception_object; + _Unwind_Exception unwind_info; +} + +// the 8-byte string identifying the type of exception +// the first 4 are for vendor, the second 4 for language +//TODO: This may be the wrong way around +const char[8] _d_exception_class = "LLDCD1\0\0"; + + +// +// x86 unwind specific implementation of personality function +// and helpers +// +version(X86_UNWIND) +{ + +// the personality routine gets called by the unwind handler and is responsible for +// reading the EH tables and deciding what to do +extern(C) _Unwind_Reason_Code _d_eh_personality(int ver, _Unwind_Action actions, ulong exception_class, _Unwind_Exception* exception_info, _Unwind_Context_Ptr context) +{ + // check ver: the C++ Itanium ABI only allows ver == 1 + if(ver != 1) + return _Unwind_Reason_Code.FATAL_PHASE1_ERROR; + + // check exceptionClass + //TODO: Treat foreign exceptions with more respect + if((cast(char*)&exception_class)[0..8] != _d_exception_class) + return _Unwind_Reason_Code.FATAL_PHASE1_ERROR; + + // find call site table, action table and classinfo table + // Note: callsite and action tables do not contain static-length + // data and will be parsed as needed + // Note: classinfo_table points past the end of the table + ubyte* callsite_table; + ubyte* action_table; + ClassInfo* classinfo_table; + _d_getLanguageSpecificTables(context, callsite_table, action_table, classinfo_table); + if (!callsite_table) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + + /* + find landing pad and action table index belonging to ip by walking + the callsite_table + */ + ubyte* callsite_walker = callsite_table; + + // get the instruction pointer + // will be used to find the right entry in the callsite_table + // -1 because it will point past the last instruction + ptrdiff_t ip = _Unwind_GetIP(context) - 1; + + // address block_start is relative to + ptrdiff_t region_start = _Unwind_GetRegionStart(context); + + // table entries + uint block_start_offset, block_size; + ptrdiff_t landing_pad; + size_t action_offset; + + while(true) { + // if we've gone through the list and found nothing... + if(callsite_walker >= action_table) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + + block_start_offset = *cast(uint*)callsite_walker; + block_size = *(cast(uint*)callsite_walker + 1); + landing_pad = *(cast(uint*)callsite_walker + 2); + if(landing_pad) + landing_pad += region_start; + callsite_walker = get_uleb128(callsite_walker + 3*uint.sizeof, action_offset); + + + // since the list is sorted, as soon as we're past the ip + // there's no handler to be found + if(ip < region_start + block_start_offset) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + + // if we've found our block, exit + if(ip < region_start + block_start_offset + block_size) + break; + } + + + // now we need the exception's classinfo to find a handler + // the exception_info is actually a member of a larger _d_exception struct + // the runtime allocated. get that now + _d_exception* exception_struct = cast(_d_exception*)(cast(ubyte*)exception_info - _d_exception.unwind_info.offsetof); + + // if there's no action offset and no landing pad, continue unwinding + if(!action_offset && !landing_pad) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + + // if there's no action offset but a landing pad, this is a cleanup handler + else if(!action_offset && landing_pad) + return _d_eh_install_finally_context(actions, landing_pad, exception_struct, context); + + /* + walk action table chain, comparing classinfos using _d_isbaseof + */ + ubyte* action_walker = action_table + action_offset - 1; + + ptrdiff_t ti_offset, next_action_offset; + while(true) { + action_walker = get_sleb128(action_walker, ti_offset); + // it is intentional that we not modify action_walker here + // next_action_offset is from current action_walker position + get_sleb128(action_walker, next_action_offset); + + // negative are 'filters' which we don't use + if(!(ti_offset >= 0)) + fatalerror("Filter actions are unsupported"); + + // zero means cleanup, which we require to be the last action + if(ti_offset == 0) { + if(!(next_action_offset == 0)) + fatalerror("Cleanup action must be last in chain"); + return _d_eh_install_finally_context(actions, landing_pad, exception_struct, context); + } + + // get classinfo for action and check if the one in the + // exception structure is a base + ClassInfo catch_ci = *(classinfo_table - ti_offset); + if(_d_isbaseof(exception_struct.exception_object.classinfo, catch_ci)) + return _d_eh_install_catch_context(actions, ti_offset, landing_pad, exception_struct, context); + + // we've walked through all actions and found nothing... + if(next_action_offset == 0) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + else + action_walker += next_action_offset; + } + + fatalerror("reached unreachable"); + return _Unwind_Reason_Code.FATAL_PHASE1_ERROR; +} + +// These are the register numbers for SetGR that +// llvm's eh.exception and eh.selector intrinsics +// will pick up. +// Hints for these can be found by looking at the +// EH_RETURN_DATA_REGNO macro in GCC, careful testing +// is required though. +version (X86_64) +{ + private int eh_exception_regno = 0; + private int eh_selector_regno = 1; +} else { + private int eh_exception_regno = 0; + private int eh_selector_regno = 2; +} + +private _Unwind_Reason_Code _d_eh_install_catch_context(_Unwind_Action actions, ptrdiff_t switchval, ptrdiff_t landing_pad, _d_exception* exception_struct, _Unwind_Context_Ptr context) +{ + + if(actions & _Unwind_Action.SEARCH_PHASE) + return _Unwind_Reason_Code.HANDLER_FOUND; + + else if(actions & _Unwind_Action.HANDLER_PHASE) + { + _Unwind_SetGR(context, eh_exception_regno, cast(ptrdiff_t)cast(void*)(exception_struct.exception_object)); + _Unwind_SetGR(context, eh_selector_regno, cast(ptrdiff_t)switchval); + _Unwind_SetIP(context, landing_pad); + return _Unwind_Reason_Code.INSTALL_CONTEXT; + } + + fatalerror("reached unreachable"); + return _Unwind_Reason_Code.FATAL_PHASE2_ERROR; +} + +private _Unwind_Reason_Code _d_eh_install_finally_context(_Unwind_Action actions, ptrdiff_t landing_pad, _d_exception* exception_struct, _Unwind_Context_Ptr context) +{ + // if we're merely in search phase, continue + if(actions & _Unwind_Action.SEARCH_PHASE) + return _Unwind_Reason_Code.CONTINUE_UNWIND; + + + _Unwind_SetGR(context, eh_exception_regno, cast(ptrdiff_t)exception_struct); + _Unwind_SetGR(context, eh_selector_regno, 0); + _Unwind_SetIP(context, landing_pad); + return _Unwind_Reason_Code.INSTALL_CONTEXT; +} + +private void _d_getLanguageSpecificTables(_Unwind_Context_Ptr context, ref ubyte* callsite, ref ubyte* action, ref ClassInfo* ci) +{ + ubyte* data = cast(ubyte*)_Unwind_GetLanguageSpecificData(context); + if (!data) + { + callsite = null; + action = null; + ci = null; + return; + } + + //TODO: Do proper DWARF reading here + if(*data++ != 0xff) + fatalerror("DWARF header has unexpected format 1"); + + if(*data++ != 0x00) + fatalerror("DWARF header has unexpected format 2"); + size_t cioffset; + data = get_uleb128(data, cioffset); + ci = cast(ClassInfo*)(data + cioffset); + + if(*data++ != 0x03) + fatalerror("DWARF header has unexpected format 3"); + size_t callsitelength; + data = get_uleb128(data, callsitelength); + action = data + callsitelength; + + callsite = data; +} + +} // end of x86 Linux specific implementation + + +extern(C) void _d_throw_exception(Object e) +{ + if (e !is null) + { + _d_exception* exc_struct = new _d_exception; + exc_struct.unwind_info.exception_class = *cast(ulong*)_d_exception_class.ptr; + exc_struct.exception_object = e; + _Unwind_Reason_Code ret = _Unwind_RaiseException(&exc_struct.unwind_info); + } + abort(); } -int _d_eh_personality(int ver, int actions, ulong eh_class, void* info, void* context) { - return 0; +extern(C) void _d_eh_resume_unwind(void* ex_struct) { + _d_exception* exception_struct = cast(_d_exception*)ex_struct; + _Unwind_Resume(&exception_struct.unwind_info); } -void _d_eh_resume_unwind(void* exception_struct) { +extern(C) void onOutOfMemoryError() { } diff --git a/runtime/gc.d b/runtime/gc.d index 13fc3501..2d0c0878 100644 --- a/runtime/gc.d +++ b/runtime/gc.d @@ -163,12 +163,10 @@ static: private: void _initialize() { - printf("GC initialized\n"); _inited = 1; } void _terminate() { - printf("GC terminated\n"); _inited = 0; } diff --git a/runtime/lifetime.d b/runtime/lifetime.d index 4f882727..3b1f6fdd 100644 --- a/runtime/lifetime.d +++ b/runtime/lifetime.d @@ -221,8 +221,10 @@ private template _arraysetlength(bool initWithZero) { ubyte[] newArray; if (memorySize <= newSize) { - // realloc - newArray = GarbageCollector.realloc(oldData[0..1], newSize); + newArray = GarbageCollector.malloc(newSize); + if (oldLength != 0) { + newArray[0..oldLength*elementSize] = oldData[0..oldLength*elementSize]; + } } else { // just resize @@ -297,8 +299,11 @@ ubyte[] _d_arrayappendT(TypeInfo ti, ref ubyte[] destArray, ubyte[] srcArray) { ubyte[] newArray; if (memorySize <= newSize) { - // realloc - newArray = GarbageCollector.realloc(destArray, newSize); + newArray = GarbageCollector.malloc(newSize); + size_t oldSize = oldLength * elementSize; + if (oldSize > 0) { + newArray[0..oldSize] = destArray.ptr[0..oldSize]; + } } else { // just resize @@ -332,8 +337,9 @@ ubyte[] _d_arrayappendcT(TypeInfo ti, ref ubyte[] array, ubyte* element) { ubyte[] newArray; if (memorySize <= newSize) { - // realloc - newArray = GarbageCollector.realloc(array, newSize); + newArray = GarbageCollector.malloc(newSize); + size_t oldSize = oldLength * elementSize; + newArray[0..oldSize] = array.ptr[0..oldSize]; } else { // just resize diff --git a/runtime/main.d b/runtime/main.d index 1d91d1da..a17256ce 100644 --- a/runtime/main.d +++ b/runtime/main.d @@ -10,7 +10,14 @@ module runtime.main; import runtime.gc; import runtime.moduleinfo; +import analyzing.debugger; + import core.error; +import core.arguments; + +import synch.thread; + +import scaffold.console; // The user supplied D entry int main(char[][] args); @@ -110,25 +117,54 @@ private void moduleDestructors() { } } +private size_t strlen(char* cstr) { + size_t ret = 0; + while(*cstr != '\0') { + ret++; + cstr++; + } + return ret; +} + private extern(C) int main(int argc, char** argv) { // Initialize the garbage collector gc_init(); - moduleInfoInitialize(); - moduleIndependentConstructors(); + int exitCode = -1; - int numDtors = 0; - moduleConstructors(null, ModuleInfo._modules, numDtors); + try { + moduleInfoInitialize(); + moduleIndependentConstructors(); - ModuleInfo._dtors = ModuleInfo._dtors[0..numDtors]; + int numDtors = 0; + moduleConstructors(null, ModuleInfo._modules, numDtors); - // Gather arguments + ModuleInfo._dtors = ModuleInfo._dtors[0..numDtors]; - // Run main - int exitCode = main([]); + // Gather arguments + Arguments argList = Arguments.instance(); + foreach(cstr; argv[0..argc]) { + argList.add(cstr[0..strlen(cstr)]); + } - // Run the module destructors - moduleDestructors(); + // Initialize the console + ConsoleInit(); + + // Make an instance for the main thread + Thread mainThread = new Thread(); + + // Run main + exitCode = main(argList.array); + + // Uninitialize the console + ConsoleUninit(); + + // Run the module destructors + moduleDestructors(); + } + catch(Object o) { + Debugger.raiseException(cast(Exception)o); + } // Terminate the garbage collector gc_term(); diff --git a/runtime/monitor.d b/runtime/monitor.d index 70512fc0..1e11d4b8 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -10,6 +10,8 @@ module runtime.monitor; +import synch.mutex; + extern(C): void _d_monitorenter(Object h) { diff --git a/runtime/object.d b/runtime/object.d index 7d4564d2..c8ba4886 100644 --- a/runtime/object.d +++ b/runtime/object.d @@ -84,7 +84,7 @@ class ClassInfo : Object { ClassInfo base; void* destructor; - void function(Object) classInvariant; + void* classInvariant; uint flags; void* deallocator; diff --git a/runtime/typeinfos/ti_array.d b/runtime/typeinfos/ti_array.d index 1544c22e..c7149d73 100644 --- a/runtime/typeinfos/ti_array.d +++ b/runtime/typeinfos/ti_array.d @@ -161,7 +161,7 @@ class ArrayInfo(char[] TYPE) : TypeInfo { len = s2.length; } - foreach(size_t idx, element; s1) { + foreach(size_t idx, element; s1[0..len]) { if (element < s2[idx]) { return -1; } diff --git a/spec/packagespecification.d b/spec/packagespecification.d index 4bfeb049..65e291f3 100644 --- a/spec/packagespecification.d +++ b/spec/packagespecification.d @@ -55,6 +55,7 @@ class PackageSpecification { } int opApply(int delegate(ref ModuleSpecification) loopBody) { + string[] mods = _modules.keys.sort; foreach(mod; _modules.keys.sort) { if (loopBody(_modules[mod])) { return 1; diff --git a/specs/data/queue.ds b/specs/data/queue.ds index e866ba69..3119b79e 100644 --- a/specs/data/queue.ds +++ b/specs/data/queue.ds @@ -21,7 +21,7 @@ describe queue() { should(list.peek() == item); } - it should_an_a_list_to_list() { + it should_add_a_list_to_list() { Queue!(int) list1 = new Queue!(int)(); Queue!(int) list2 = new Queue!(int)(); int item = 33; diff --git a/synch/thread.d b/synch/thread.d index 44a0d048..6d099607 100644 --- a/synch/thread.d +++ b/synch/thread.d @@ -22,6 +22,14 @@ class Thread { // Description: Will create a normal thread that does not have any external callback functions. this() { + // Check for creating thread + Thread callee = Thread.current(); + + if (callee is null) { + // We do not know this thread... add it + threadById[ThreadIdentifier()] = this; + } + startTime = time = System.time; } @@ -134,10 +142,13 @@ class Thread { static Thread current() { Thread ret; - if (ret is null) { + uint curID = ThreadIdentifier(); + + if (curID in threadById) { + return threadById[curID]; } - return ret; + return null; } protected: diff --git a/winsamp.d b/winsamp.d index 926edece..f50848ee 100644 --- a/winsamp.d +++ b/winsamp.d @@ -111,111 +111,33 @@ private: int _foo; } -int main(string[] args) { - printf("hello world\n"); - printf("---------------\n"); - int[] foobar = new int[10]; - foreach(size_t i, ref element; foobar) { - element = i; - } - foreach(ref element; foobar) { - printf("%d\n", element); - } - printf("%d\n", foobar.length); - printf("---------------\n"); - foobar ~= 42; - foreach(ref element; foobar) { - printf("%d\n", element); - } - printf("%d\n", foobar.length); - printf("---------------\n"); - int[] eff = new int[5]; - foreach(size_t i, ref element; eff) { - element = i + foobar.length; - } - foreach(element;eff) { - printf("%d\n", element); - } - printf("%d\n", eff.length); - printf("---------------\n"); - int[] result = foobar ~ eff; - foreach(element; result) { - printf("%d\n", element); - } - printf("%d\n", result.length); - printf("---------------\n"); - foreach(ref element; foobar) { - printf("%d\n", element); - } - printf("%d\n", foobar.length); - printf("---------------\n"); - foreach(element;eff) { - printf("%d\n", element); - } - printf("%d\n", eff.length); - printf("---------------\n"); - eff.length = eff.length + 5; +import spec.modulespecification; - foreach(element;eff) { - printf("%d\n", element); - } - printf("%d\n", eff.length); - printf("---------------\n"); - - int[] duplicate = result.dup; - foreach(element; duplicate) { - printf("%d\n", element); - } - printf("%d\n", duplicate.length); - printf("---------------\n"); - - duplicate ~= result; - foreach(element; duplicate) { - printf("%d\n", element); +import data.queue; +int main(string[] args) { + string foobar = "abc"; + foobar ~= 'd'; + Console.putln(3); + int[] foo; + foo ~= 3; + Console.putln(foo); + int[] foo2 = null; + foo2 ~= [2,4]; + Console.putln(foo2); + Console.putln(foo2.length); + synchronized { } - printf("%d\n", duplicate.length); - printf("---------------\n"); - int[] hahaha = [0,1,2,3,6,7,8,9]; - foreach(element; hahaha) { - printf("%d\n", element); - } - printf("%d\n", hahaha.length); - printf("---------------\n"); - - - A a = new A(15); - A b = new A(15); - int ret = a.foobar(); - printf("%d\n", ret); - - long flong = 0x1234123412341234; - printf("%lx\n", a.toHash()); - printf("%lx\n", b.toHash()); - - int[int] haha; - - haha[4] = 3; - haha[8] = 6; - haha[16] = 13; - haha[32] = 24; - haha[64] = 123; - - printf("%d %d %d %d %d\n", haha[4], haha[8], haha[16], haha[32], haha[64]); - printf("length: %d\n", haha.length); - int[] values = haha.keys; - foreach(val; values) { - printf(":%d\n", val); - } + Console.putln("lists test\n"); + int[3] arr = 1; + Queue!(int) list = new Queue!(int)(); - int[int] fooaa = [4: 3, 8: 6, 16: 13, 32: 24, 64: 123]; + list.add(arr); - values = fooaa.keys; - foreach(val; values) { - printf(":%d\n", val); - } + Console.putln(list.length == 3); + Console.putln(list.peek() == arr[2]); return 0; } From ee24791f3aa4848f632469436dcc3aa3757e864d Mon Sep 17 00:00:00 2001 From: wilkie Date: Sat, 15 May 2010 01:18:50 -0400 Subject: [PATCH 10/14] Added synchronized support to runtime. --- core/main.d | 52 --------------------- cui/application.d | 2 + cui/container.d | 10 +--- examples/CuiTetris/app.d | 10 ++-- graphics/bitmap.d | 3 +- graphics/view.d | 3 +- io/console.d | 2 + platform/unix/platform/vars/thread.d | 1 + platform/unix/scaffold/thread.d | 68 ++++++++++------------------ runtime/lifetime.d | 13 ++++-- runtime/monitor.d | 18 +++++++- synch/semaphore.d | 8 ++-- synch/thread.d | 36 +++------------ synch/timer.d | 6 ++- winsamp.d | 49 +++++++++++--------- 15 files changed, 108 insertions(+), 173 deletions(-) diff --git a/core/main.d b/core/main.d index a51e89a8..1425f18e 100644 --- a/core/main.d +++ b/core/main.d @@ -60,9 +60,6 @@ package: throw new Exception("Framework Already Started"); } - // Constitute the main thread class - ThreadModuleInit(); - // Check to make sure the app provided a suitable class to use if (app is null) { throw new Exception("No Application Class"); @@ -74,17 +71,6 @@ package: } void end(uint code = 0) { - // Tell all running threads that they should end to allow shutdown to commense - if (_threads !is null) { - _threadRegisterSemaphore.down(); - - foreach(th; _threads) { - th.pleaseStop(); - } - - _threadRegisterSemaphore.up(); - } - // Reset colors to something sane Console.forecolor = Color.White; Console.backcolor = Color.Black; @@ -96,43 +82,5 @@ package: bool _hasStarted = false; - Thread[] _threads; - - Semaphore _threadRegisterSemaphore; - Application app; } - -void RegisterThread(ref Thread thread) { - if (Djehuty._threadRegisterSemaphore is null) { - Djehuty._threadRegisterSemaphore = new Semaphore(1); - } - - Djehuty._threadRegisterSemaphore.down(); - Djehuty._threads ~= thread; - Djehuty._threadRegisterSemaphore.up(); -} - -void UnregisterThread(ref Thread thread) -{ - Djehuty._threadRegisterSemaphore.down(); - - if (Djehuty._threads !is null) { - foreach(i, th; Djehuty._threads) { - if (th is thread) { - if (Djehuty._threads.length == 1) { - Djehuty._threads = null; - } - else if (i >= Djehuty._threads.length - 1) { - Djehuty._threads = Djehuty._threads[0..i]; - } - else { - Djehuty._threads = Djehuty._threads[0..i] ~ Djehuty._threads[i+1..$]; - } - break; - } - } - } - - Djehuty._threadRegisterSemaphore.up(); -} diff --git a/cui/application.d b/cui/application.d index 8de8dff5..019e41f4 100644 --- a/cui/application.d +++ b/cui/application.d @@ -14,6 +14,8 @@ import platform.vars.cui; import io.console; +import binding.c; + // Description: This class represents a Text User Interface application (TUI). class CuiApplication : Application { public: diff --git a/cui/container.d b/cui/container.d index 9b21e4ac..858cbdf9 100644 --- a/cui/container.d +++ b/cui/container.d @@ -84,7 +84,6 @@ class CuiContainer : CuiWidget { } // Should clear the rest of the space not used by a widget - static string spaces = " "; Console.backcolor = Color.Gray; for (uint i; i < this.height; i++) { @@ -92,14 +91,7 @@ class CuiContainer : CuiWidget { io.console.Console.backcolor = Color.Black; uint numSpaces = this.width; - do { - uint pad = spaces.length; - if (numSpaces < pad) { - pad = numSpaces; - } - io.console.Console.put(spaces[0..pad]); - numSpaces -= pad; - } while (numSpaces > 0) + io.console.Console.put(" ".times(numSpaces)); } io.console.Console.clipRestore(); } diff --git a/examples/CuiTetris/app.d b/examples/CuiTetris/app.d index 90e6879c..af3889b9 100644 --- a/examples/CuiTetris/app.d +++ b/examples/CuiTetris/app.d @@ -8,13 +8,15 @@ import gamewindow; import binding.c; -class TermTetris : CuiApplication { +int main() { + auto app = new TermTetris; + app.run(); + return 0; +} - // Start an application instance - static this() { printf("HELLO!\n"); new TermTetris(); } +class TermTetris : CuiApplication { override void onApplicationStart() { - Console.putln("foo"); Console.hideCaret(); gameWindow = new GameWindow(); diff --git a/graphics/bitmap.d b/graphics/bitmap.d index 910f8f92..d7fd48d6 100644 --- a/graphics/bitmap.d +++ b/graphics/bitmap.d @@ -24,8 +24,7 @@ class Bitmap : View { this() { super(); - _buffer_mutex = new Semaphore; - _buffer_mutex.init(1); + _buffer_mutex = new Semaphore(1); } this(int width, int height) { diff --git a/graphics/view.d b/graphics/view.d index 18988067..93914317 100644 --- a/graphics/view.d +++ b/graphics/view.d @@ -21,10 +21,9 @@ import scaffold.view; class View { // Description: This will instantiate an uninitialized view. It will need to be created with the create() function in order to fully use. this() { - _mutex = new Semaphore; + _mutex = new Semaphore(1); _inited = false; - _mutex.init(1); _graphics = new Graphics(); diff --git a/io/console.d b/io/console.d index 3d56ffa8..8f15c3d1 100644 --- a/io/console.d +++ b/io/console.d @@ -6,6 +6,8 @@ import synch.mutex; import djehuty; +import binding.c; + // Section: Enums // Description: This enum gives possible events that can be triggered by the Console. diff --git a/platform/unix/platform/vars/thread.d b/platform/unix/platform/vars/thread.d index dbb5fb1c..aaab5152 100644 --- a/platform/unix/platform/vars/thread.d +++ b/platform/unix/platform/vars/thread.d @@ -17,4 +17,5 @@ import synch.thread; struct ThreadPlatformVars { pthread_t id; Thread thread; + void delegate() endCallback; } diff --git a/platform/unix/scaffold/thread.d b/platform/unix/scaffold/thread.d index 0bf26ffd..9c28fce3 100644 --- a/platform/unix/scaffold/thread.d +++ b/platform/unix/scaffold/thread.d @@ -24,8 +24,7 @@ import platform.vars.condition; import platform.vars.mutex; import platform.vars.semaphore; -void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) -{ +void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) { timespec timetoexpire; timetoexpire.tv_sec = (milliseconds / 1000); @@ -35,46 +34,39 @@ void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) } extern (C) -void *_djehuty_unix_thread_proc(void* udata) -{ +void *_djehuty_unix_thread_proc(void* udata) { ThreadPlatformVars* threadVars = cast(ThreadPlatformVars*)(udata); Thread t_info = threadVars.thread; t_info.run(); - threadVars.id = 0; - - ThreadUninit(t_info); - - pthread_exit(null); - + threadVars.endCallback(); return null; } +long ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread, void delegate() endCallback) { + threadVars.thread = thread; + threadVars.endCallback = endCallback; -void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread) -{ - int ret = pthread_create(&threadVars.id, null, &_djehuty_unix_thread_proc, cast(void *)thread); - if (ret) - { + int ret = pthread_create(&threadVars.id, null, &_djehuty_unix_thread_proc, &threadVars); + + if (ret) { // error creating thread threadVars.id = 0; } + + return threadVars.id; } -void ThreadStop(ref ThreadPlatformVars threadVars) -{ - if (threadVars.id) - { - if (threadVars.id == pthread_self()) - { +void ThreadStop(ref ThreadPlatformVars threadVars) { + if (threadVars.id) { + if (threadVars.id == pthread_self()) { //soft exit //printf("thread - soft kill\n"); threadVars.id = 0; pthread_exit(null); } - else - { + else { //hard exit //printf("thread - hard kill\n"); pthread_kill(threadVars.id, SIGKILL); @@ -95,29 +87,24 @@ bool ThreadIsCurrent(ref ThreadPlatformVars threadVars) { // Semaphores -void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue) -{ +void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue) { sem_init(&semVars.sem_id, 0, initialValue); } -void SemaphoreUninit(ref SemaphorePlatformVars semVars) -{ +void SemaphoreUninit(ref SemaphorePlatformVars semVars) { sem_destroy(&semVars.sem_id); } -void SemaphoreUp(ref SemaphorePlatformVars semVars) -{ +void SemaphoreUp(ref SemaphorePlatformVars semVars) { sem_post(&semVars.sem_id); } -void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms) -{ +void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms) { // TODO: semaphore timeout sem_wait(&semVars.sem_id); } -void SemaphoreDown(ref SemaphorePlatformVars semVars) -{ +void SemaphoreDown(ref SemaphorePlatformVars semVars) { sem_wait(&semVars.sem_id); } @@ -127,27 +114,22 @@ void SemaphoreDown(ref SemaphorePlatformVars semVars) // Mutexes -void MutexInit(ref MutexPlatformVars mutVars) -{ +void MutexInit(ref MutexPlatformVars mutVars) { pthread_mutex_init(&mutVars.mut_id, null); } -void MutexUninit(ref MutexPlatformVars mutVars) -{ +void MutexUninit(ref MutexPlatformVars mutVars) { pthread_mutex_destroy(&mutVars.mut_id); } -void MutexLock(ref MutexPlatformVars mutVars) -{ +void MutexLock(ref MutexPlatformVars mutVars) { pthread_mutex_lock(&mutVars.mut_id); } -void MutexLock(ref MutexPlatformVars mutVars, ref uint ms) -{ +void MutexLock(ref MutexPlatformVars mutVars, ref uint ms) { } -void MutexUnlock(ref MutexPlatformVars mutVars) -{ +void MutexUnlock(ref MutexPlatformVars mutVars) { pthread_mutex_unlock(&mutVars.mut_id); } diff --git a/runtime/lifetime.d b/runtime/lifetime.d index 3b1f6fdd..9868321f 100644 --- a/runtime/lifetime.d +++ b/runtime/lifetime.d @@ -217,13 +217,18 @@ private template _arraysetlength(bool initWithZero) { size_t newSize = length * elementSize; size_t memorySize = GarbageCollector.query(oldData[0..1]); + memorySize = oldLength; ubyte[] newArray; + if (newSize == 0) { + return null; + } + if (memorySize <= newSize) { newArray = GarbageCollector.malloc(newSize); if (oldLength != 0) { - newArray[0..oldLength*elementSize] = oldData[0..oldLength*elementSize]; + newArray[0..oldLength] = oldData[0..oldLength]; } } else { @@ -234,7 +239,7 @@ private template _arraysetlength(bool initWithZero) { // Initialize the new space static if (initWithZero) { // Initialize the remaining space with zero - newArray[oldLength*elementSize..newSize] = 0; + newArray[oldLength..newSize] = 0; } else { // Initialize the remaining space with the init value from ti @@ -242,13 +247,13 @@ private template _arraysetlength(bool initWithZero) { // If there is no init vector, then just init to zero if (init is null) { - newArray[oldLength*elementSize..newSize] = 0; + newArray[oldLength..newSize] = 0; } else { // Initialize all values we can size_t initIndex = 0; - foreach(ref element; newArray[oldLength*elementSize..newSize]) { + foreach(ref element; newArray[oldLength..newSize]) { element = init[initIndex]; initIndex++; if (initIndex == init.length) { diff --git a/runtime/monitor.d b/runtime/monitor.d index 1e11d4b8..6fa9eb3c 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -10,14 +10,30 @@ module runtime.monitor; -import synch.mutex; +import runtime.gc; + +import synch.semaphore; extern(C): +struct Monitor { + Semaphore semaphore; +} + void _d_monitorenter(Object h) { + // The monitor object is the second pointer in the object + Monitor* monitor = *(cast(Monitor**)h + 1); + if (monitor is null) { + monitor = new Monitor; + monitor.semaphore = new Semaphore(1); + *(cast(Monitor**)h + 1) = monitor; + } + monitor.semaphore.down(); } void _d_monitorexit(Object h) { + Monitor* monitor = *(cast(Monitor**)h + 1); + monitor.semaphore.up(); } void _d_criticalenter(void* dcs) { diff --git a/synch/semaphore.d b/synch/semaphore.d index 652a833f..30299ba1 100644 --- a/synch/semaphore.d +++ b/synch/semaphore.d @@ -5,6 +5,7 @@ import scaffold.thread; import platform.vars.semaphore; import core.definitions; +import binding.c; // Section: Core/Synchronization @@ -12,8 +13,7 @@ import core.definitions; class Semaphore { ~this() { - if (_inited) - { + if (_inited) { SemaphoreUninit(_pfvars); } } @@ -25,12 +25,12 @@ class Semaphore { // Description: Creates and initializes a semaphore. // initialValue: The initial count for the semaphore. this(uint initialValue) { - init(initialValue); + initialize(initialValue); } // Description: This function will initialize a semaphore and set it to an initial count. // initialValue: The initial count for the semaphore. - void init(uint initialValue) { + void initialize(uint initialValue) { SemaphoreInit(_pfvars, initialValue); _inited = true; } diff --git a/synch/thread.d b/synch/thread.d index 6d099607..91c9018b 100644 --- a/synch/thread.d +++ b/synch/thread.d @@ -101,8 +101,7 @@ class Thread { // Description: This function will start the thread and call the threadProc() function, which will in turn execute an external delegate if provided. void start() { if (!_inited) { - RegisterThread(this); - ThreadStart(_pfvars, this); + _id = ThreadStart(_pfvars, this, &end); startTime = time = System.time; @@ -114,7 +113,6 @@ class Thread { void stop() { if (_inited) { ThreadStop(_pfvars); - UnregisterThread(this); } _inited = false; } @@ -153,16 +151,16 @@ class Thread { protected: + void end() { + threadById[_id] = null; + _inited = false; + } + void delegate (bool) _thread_callback = null; void function (bool) _thread_f_callback = null; - int _threadProc() { - run(); - - return 0; - } - bool _inited; + int _id; long startTime; long time; @@ -174,26 +172,6 @@ protected: static Thread[uint] threadById; } -void ThreadModuleInit() { - - // create a Thread for the main thread - Thread mainThread = new Thread(); - mainThread._inited = true; - -// version(Tango) { - //mainThread.stdThread.runtimeThread = Tango.Thread.getThis(); -// } -// else { - //mainThread.stdThread.runtimeThread = Phobos.Thread.getThis(); -// } - - //Thread.threadById[mainThread.stdThread.runtimeThread] = mainThread; -} - -void ThreadUninit(ref Thread t) { - t._inited = false; -} - void ThreadSetWindow(ref Thread t, Window w) { t.wnd = w; } diff --git a/synch/timer.d b/synch/timer.d index a8bf2d66..ad9bd1c9 100644 --- a/synch/timer.d +++ b/synch/timer.d @@ -23,10 +23,14 @@ class Timer : Dispatcher { // Description: This function will set the time interval to periodly call the timerProc() function. // milliseconds: The number of milliseconds to wait before the next timer fire. - void setInterval(ulong milliseconds) { + void interval(ulong milliseconds) { _interval = milliseconds; } + ulong interval() { + return _interval; + } + // Description: This function will start the timer. void start() { //start the thread diff --git a/winsamp.d b/winsamp.d index f50848ee..63cd646f 100644 --- a/winsamp.d +++ b/winsamp.d @@ -66,11 +66,13 @@ import spec.specification; import data.queue2; class MyConsoleApp : Application { - static this() { new MyConsoleApp(); } override void onApplicationStart() { - Console.putln(sin(0)); - Console.putln(cos(3.1415296)); + Timer tmr = new Timer; + tmr.interval = 500; + push(tmr); + tmr.start; + for(;;){} } override bool onSignal(Dispatcher dsp, uint signal) { @@ -115,29 +117,32 @@ private: import spec.modulespecification; import data.queue; -int main(string[] args) { - string foobar = "abc"; - foobar ~= 'd'; - Console.putln(3); - int[] foo; - foo ~= 3; - Console.putln(foo); - int[] foo2 = null; - foo2 ~= [2,4]; - Console.putln(foo2); - Console.putln(foo2.length); - synchronized { - } +class MyWindow : CuiWindow { + this() { + push (new CuiLabel(2,3, 10, "hello")); + } - Console.putln("lists test\n"); - int[3] arr = 1; - Queue!(int) list = new Queue!(int)(); + override void onKeyDown(Key key) { + if (key.ctrl && key.code == Key.Q) { + Djehuty.app.exit(0); + } + } +} - list.add(arr); +class MyApp : CuiApplication { + override void onApplicationStart() { + push(new MyWindow); + } +} - Console.putln(list.length == 3); - Console.putln(list.peek() == arr[2]); +void foo(bool stop) { + Console.putln("hello"); + Console.putln("what is up?"); +} +int main(string[] args) { + auto app = new MyConsoleApp; + app.run(); return 0; } From 4602c5d59dcb7832f7b58bf2c0948b33626092b6 Mon Sep 17 00:00:00 2001 From: wilkie Date: Sat, 15 May 2010 22:54:23 -0400 Subject: [PATCH 11/14] Fixed Thread and Timer. --- data/iterable.d | 5 ++- data/list.d | 3 ++ examples/CuiTetris/gamecontrol.d | 29 +++++++------- examples/Snake/app.d | 10 +++-- examples/Snake/constants.d | 15 ++++--- examples/Snake/game.d | 4 +- examples/Snake/snake.d | 1 + examples/Snake/win.d | 7 ++-- gui/hscrollbar.d | 4 +- gui/vscrollbar.d | 4 +- platform/unix/scaffold/thread.d | 11 +++++- resource/sound.d | 2 +- runtests.d | 1 + runtime/monitor.d | 5 +++ synch/semaphore.d | 4 ++ synch/thread.d | 3 +- synch/timer.d | 67 ++++++++++---------------------- winsamp.d | 61 +++++++++++++++++++++-------- 18 files changed, 135 insertions(+), 101 deletions(-) diff --git a/data/iterable.d b/data/iterable.d index e2cf96c4..12bf8998 100644 --- a/data/iterable.d +++ b/data/iterable.d @@ -347,9 +347,12 @@ template foldr(T, R, S) { template member(T, S) { static assert(IsIterable!(T), "member: " ~ T.stringof ~ " is not iterable."); T member(S value, T list) { + Console.putln("member"); foreach(size_t i, S item; list) { + Console.putln("Comparing ", value, " vs ", item); if (value == item) { - return cast(T)(list[i..list.length]); + Console.putln("item found: ", list[i..list.length]); + return list[i..list.length]; } } return null; diff --git a/data/list.d b/data/list.d index f0609d2c..478b3919 100644 --- a/data/list.d +++ b/data/list.d @@ -15,6 +15,8 @@ import djehuty; public import data.iterable; +import io.console; + class List(T) : Iterable!(T) { this() { _data.length = 10; @@ -310,6 +312,7 @@ class List(T) : Iterable!(T) { int opApply(int delegate(ref size_t, ref T) loopFunc) { synchronized(this) { + Console.putln("opApply"); int ret; for(size_t i = 0; i < _count; i++) { diff --git a/examples/CuiTetris/gamecontrol.d b/examples/CuiTetris/gamecontrol.d index bc0870a3..3bdf4dd1 100644 --- a/examples/CuiTetris/gamecontrol.d +++ b/examples/CuiTetris/gamecontrol.d @@ -68,9 +68,8 @@ class GameControl : CuiWidget { } } else if (key.code == Key.Space) { - tmr.stop(); - lock.down(); + tmr.stop(); int result = 1; while(result == 1) { @@ -87,9 +86,8 @@ class GameControl : CuiWidget { drawPiece(); } - lock.up(); - tmr.start(); + lock.up(); } } @@ -186,19 +184,20 @@ class GameControl : CuiWidget { protected: void timerProc() { - lock.down(); - int result = board.moveDown(); + if (lock.tryDown()) { + int result = board.moveDown(); - if (result > 0) { - clearPiece(); - } - else if (result == -1) { - // cleared rows - raiseSignal(Event.ScoreUpdated); - drawBoard(); + if (result > 0) { + clearPiece(); + } + else if (result == -1) { + // cleared rows + raiseSignal(Event.ScoreUpdated); + drawBoard(); + } + drawPiece(); + lock.up(); } - drawPiece(); - lock.up(); } bool inited; diff --git a/examples/Snake/app.d b/examples/Snake/app.d index 509128e4..fb7134d7 100644 --- a/examples/Snake/app.d +++ b/examples/Snake/app.d @@ -2,11 +2,13 @@ import cui.application; import win; -class SnakeApp : CuiApplication { - static this() { - new SnakeApp(); - } +int main(string[] args) { + auto app = new SnakeApp(); + app.run(); + return 0; +} +class SnakeApp : CuiApplication { override void onApplicationStart() { push(new SnakeWindow()); } diff --git a/examples/Snake/constants.d b/examples/Snake/constants.d index 84a070c3..e880e885 100644 --- a/examples/Snake/constants.d +++ b/examples/Snake/constants.d @@ -1,4 +1,5 @@ import io.console; +import core.color; enum FrameWait : uint { Init = 1000, @@ -25,10 +26,14 @@ enum Tile : dchar { Void = ' ', } -enum TileColor : fgColor { - Head = fgColor.BrightBlue, - Block = fgColor.BrightCyan, - Food = fgColor.BrightGreen, - Portal = fgColor.BrightRed, +struct TileColor { + alias Color.Blue Head; + alias Color.Cyan Block; + alias Color.Green Food; + alias Color.Red Portal; +// static Color Head = Color.Blue; +// static Color Block = Color.Cyan; +// static Color Food = Color.Green; +// static Color Portal = Color.Red; } diff --git a/examples/Snake/game.d b/examples/Snake/game.d index 413d4bb6..36cf2341 100644 --- a/examples/Snake/game.d +++ b/examples/Snake/game.d @@ -100,8 +100,8 @@ private: Console.putChar(t); } - void drawTile(Posn p, Tile t, TileColor c) { - Console.setColor(c); + void drawTile(Posn p, Tile t, Color c) { + Console.forecolor = c; drawTile(p, t); } diff --git a/examples/Snake/snake.d b/examples/Snake/snake.d index ae75bf49..37dd820e 100644 --- a/examples/Snake/snake.d +++ b/examples/Snake/snake.d @@ -1,4 +1,5 @@ import djehuty; +import data.list; import posn; diff --git a/examples/Snake/win.d b/examples/Snake/win.d index eaa98d0c..684da38b 100644 --- a/examples/Snake/win.d +++ b/examples/Snake/win.d @@ -34,7 +34,7 @@ class SnakeWindow : CuiWindow { dirCoef = s.isVertical ? SpeedCoef.V : SpeedCoef.H; } - _timer.setInterval(_frame_wait * dirCoef / FrameWait.Multiplier); + _timer.interval = _frame_wait * dirCoef / FrameWait.Multiplier; } void gameOver(DeathBy d) { @@ -44,7 +44,7 @@ class SnakeWindow : CuiWindow { _timer.stop(); - Console.setColor(fgColor.BrightRed); + Console.forecolor = Color.Red; Console.putln("DEAD!"); } @@ -52,7 +52,8 @@ class SnakeWindow : CuiWindow { override void onInitialize() { super.onInitialize(); - Console.setColor(fgColor.White, bgColor.Black); + Console.forecolor = Color.White; + Console.backcolor = Color.Black; string[] instructions = ["move: arrows/wasd", "stop: q or die", "quit: any key"]; foreach (uint y, string s; instructions) { diff --git a/gui/hscrollbar.d b/gui/hscrollbar.d index a88da7b6..94af3e38 100644 --- a/gui/hscrollbar.d +++ b/gui/hscrollbar.d @@ -47,8 +47,8 @@ public: _readyTimer = new Timer(); _clickTimer = new Timer(); - _clickTimer.setInterval(50); - _readyTimer.setInterval(100); + _clickTimer.interval = 50; + _readyTimer.interval = 100; push(_readyTimer); push(_clickTimer); diff --git a/gui/vscrollbar.d b/gui/vscrollbar.d index 3b70e39c..03070a9c 100644 --- a/gui/vscrollbar.d +++ b/gui/vscrollbar.d @@ -51,8 +51,8 @@ public: _readyTimer = new Timer(); _clickTimer = new Timer(); - _clickTimer.setInterval(50); - _readyTimer.setInterval(100); + _clickTimer.interval = 50; + _readyTimer.interval = 100; push(_readyTimer); push(_clickTimer); diff --git a/platform/unix/scaffold/thread.d b/platform/unix/scaffold/thread.d index 9c28fce3..c825b63c 100644 --- a/platform/unix/scaffold/thread.d +++ b/platform/unix/scaffold/thread.d @@ -24,13 +24,19 @@ import platform.vars.condition; import platform.vars.mutex; import platform.vars.semaphore; +import binding.c; + void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) { timespec timetoexpire; timetoexpire.tv_sec = (milliseconds / 1000); timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000; - nanosleep(&timetoexpire, null); + timespec remaining; + + while(nanosleep(&timetoexpire, &remaining) != 0) { + timetoexpire = remaining; + } } extern (C) @@ -108,6 +114,9 @@ void SemaphoreDown(ref SemaphorePlatformVars semVars) { sem_wait(&semVars.sem_id); } +bool SemaphoreTry(ref SemaphorePlatformVars semVars) { + return (sem_trywait(&semVars.sem_id) == 0); +} diff --git a/resource/sound.d b/resource/sound.d index 41e1d5d1..702999fb 100644 --- a/resource/sound.d +++ b/resource/sound.d @@ -63,7 +63,7 @@ class Sound : Responder { push(wavDevice = new Audio); tmr = new Timer(); - tmr.setInterval(250); + tmr.interval = 250; push(tmr); diff --git a/runtests.d b/runtests.d index bf7c8f95..cb3a717f 100644 --- a/runtests.d +++ b/runtests.d @@ -75,6 +75,7 @@ private: foreach(item; ms) { foreach(feature; item) { auto tester = new Test(item, feature); + Console.putln("Testing", " : ", item.name, " ", feature); tester.run(); if (tester.failures > 0) { Console.forecolor = Color.Red; diff --git a/runtime/monitor.d b/runtime/monitor.d index 6fa9eb3c..48fc382e 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -13,6 +13,7 @@ module runtime.monitor; import runtime.gc; import synch.semaphore; +import io.console; extern(C): @@ -26,13 +27,17 @@ void _d_monitorenter(Object h) { if (monitor is null) { monitor = new Monitor; monitor.semaphore = new Semaphore(1); + // TODO: Should be an atomic exchange with null, and if it fails then + // proceed to use that object. *(cast(Monitor**)h + 1) = monitor; } + Console.putln("down"); monitor.semaphore.down(); } void _d_monitorexit(Object h) { Monitor* monitor = *(cast(Monitor**)h + 1); + Console.putln("up"); monitor.semaphore.up(); } diff --git a/synch/semaphore.d b/synch/semaphore.d index 30299ba1..f124cbfb 100644 --- a/synch/semaphore.d +++ b/synch/semaphore.d @@ -51,6 +51,10 @@ class Semaphore { SemaphoreDown(_pfvars, milliseconds); } + bool tryDown() { + return SemaphoreTry(_pfvars); + } + protected: SemaphorePlatformVars _pfvars; diff --git a/synch/thread.d b/synch/thread.d index 91c9018b..4b3992f7 100644 --- a/synch/thread.d +++ b/synch/thread.d @@ -101,11 +101,10 @@ class Thread { // Description: This function will start the thread and call the threadProc() function, which will in turn execute an external delegate if provided. void start() { if (!_inited) { + _inited = true; _id = ThreadStart(_pfvars, this, &end); startTime = time = System.time; - - _inited = true; } } diff --git a/synch/timer.d b/synch/timer.d index ad9bd1c9..a19ee5d5 100644 --- a/synch/timer.d +++ b/synch/timer.d @@ -13,7 +13,6 @@ class Timer : Dispatcher { } this(ulong interval) { - this(); _interval = interval; } @@ -34,30 +33,25 @@ class Timer : Dispatcher { // Description: This function will start the timer. void start() { //start the thread - if (_started) { + if (isRunning()) { stop(); } _thread = new timer_thread(this); - _started = true; - _thread.start(); } // Description: This function will stop the timer when the timerProc returns. void stop() { - if (!_started) { - return; - } - _thread._stop = true; - _started = false; } // Description: This function will return the state of the timer. // Returns: Will return true if the timer is currently in the running state, false otherwise. - bool isRunning() { return _started; } + bool isRunning() { + return (_thread !is null && _thread._stop == false); + } // Description: This function is called on every timer fire. It's normal operation is to call the callback provided by the callback property. One can override to provide a class based timer procedure. // Returns: If true is returned, the timer is stopped. Otherwise the timer will fire again after the interval depletes. Note: the time it takes to run the function is not accounted for in the interval at this time. @@ -67,50 +61,31 @@ class Timer : Dispatcher { protected: - class timer_thread : Thread { - this() { - _inCall = new Semaphore(1); - } - - this(Timer t) { - _timer = t; - this(); - } + ulong _interval = 0; - override void pleaseStop() { - if (!_stop) { - _timer.stop(); - } - _stop = true; - } + timer_thread _thread; +} - override void stop() { - pleaseStop(); - super.stop(); - } +private class timer_thread : Thread { + this(Timer t) { + _timer = t; + } - override void run() { - while (!_stop && _inited) { - sleep(_timer._interval); + override void run() { + while (_stop == false) { + sleep(_timer._interval); - _inCall.down(); - if (_stop || !_inited) { break; } - if (_timer.fire() == false) { break; } - _inCall.up(); - } + if (_stop == true) { break; } + _timer.raiseSignal(1); - _stop = true; - _inCall.up(); +// if (_timer.fire() == false) { break; } } - bool _stop; - Timer _timer; - Semaphore _inCall; + _stop = true; } - bool _started = false; - ulong _interval = 0; - - timer_thread _thread; + bool _stop; + Timer _timer; } + diff --git a/winsamp.d b/winsamp.d index 63cd646f..feffdf27 100644 --- a/winsamp.d +++ b/winsamp.d @@ -66,20 +66,6 @@ import spec.specification; import data.queue2; class MyConsoleApp : Application { - - override void onApplicationStart() { - Timer tmr = new Timer; - tmr.interval = 500; - push(tmr); - tmr.start; - for(;;){} - } - - override bool onSignal(Dispatcher dsp, uint signal) { - Console.putln("fire"); - return true; - } - void foo(bool bar) { Atomic.increment(fudge); while(fudge < 9) { @@ -120,14 +106,43 @@ import data.queue; class MyWindow : CuiWindow { this() { - push (new CuiLabel(2,3, 10, "hello")); + push (label = new CuiLabel(2,3, 10, "hello")); + tmr = new Timer; + tmr.interval = 10000; + push(tmr); + tmr.start; + } + + override bool onSignal(Dispatcher dsp, uint signal) { + if (dsp !is tmr) { + return false; + } + + static int i = 0; + i++; + if (signal == 1) { + label.text = "fuck!" ~ toStr(i); + } + if (label.text.length > 4) { + redraw(); + return true; + } + label.text = toStr(i); + redraw(); + return true; } override void onKeyDown(Key key) { if (key.ctrl && key.code == Key.Q) { Djehuty.app.exit(0); } + + tmr.stop(); + tmr.start(); } + + Timer tmr; + CuiLabel label; } class MyApp : CuiApplication { @@ -141,8 +156,20 @@ void foo(bool stop) { Console.putln("what is up?"); } +import math.random; +static const int REPEATS = 10000; int main(string[] args) { - auto app = new MyConsoleApp; - app.run(); + auto r = new Random(); + List!(char) lst = new List!(char)(['a', 'e', 'i', 'o', 'u']); + char v; + for (uint i = 0; i < REPEATS; i++) { + Console.putln("um"); + v = r.choose(lst); + Console.putln("chosen: ", v); + Console.putln(member(v, lst) is null); + } + + //auto app = new MyApp; + //app.run(); return 0; } From 7cd3cae8e89fdbcd56647c6eb26e5c435b843bde Mon Sep 17 00:00:00 2001 From: wilkie Date: Sat, 15 May 2010 22:56:55 -0400 Subject: [PATCH 12/14] Hmm. There is a synchronization issue. The code is valid, so I guess I need a lock-free List implementation. Why did this work with Tango? It shouldn't have. --- data/iterable.d | 3 --- data/list.d | 1 - runtime/monitor.d | 2 -- 3 files changed, 6 deletions(-) diff --git a/data/iterable.d b/data/iterable.d index 12bf8998..a31b3c96 100644 --- a/data/iterable.d +++ b/data/iterable.d @@ -347,11 +347,8 @@ template foldr(T, R, S) { template member(T, S) { static assert(IsIterable!(T), "member: " ~ T.stringof ~ " is not iterable."); T member(S value, T list) { - Console.putln("member"); foreach(size_t i, S item; list) { - Console.putln("Comparing ", value, " vs ", item); if (value == item) { - Console.putln("item found: ", list[i..list.length]); return list[i..list.length]; } } diff --git a/data/list.d b/data/list.d index 478b3919..657f2195 100644 --- a/data/list.d +++ b/data/list.d @@ -312,7 +312,6 @@ class List(T) : Iterable!(T) { int opApply(int delegate(ref size_t, ref T) loopFunc) { synchronized(this) { - Console.putln("opApply"); int ret; for(size_t i = 0; i < _count; i++) { diff --git a/runtime/monitor.d b/runtime/monitor.d index 48fc382e..8d657171 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -31,13 +31,11 @@ void _d_monitorenter(Object h) { // proceed to use that object. *(cast(Monitor**)h + 1) = monitor; } - Console.putln("down"); monitor.semaphore.down(); } void _d_monitorexit(Object h) { Monitor* monitor = *(cast(Monitor**)h + 1); - Console.putln("up"); monitor.semaphore.up(); } From b4b7cf01ea86d24bdfd31aa4f7a2e85c5a3e7680 Mon Sep 17 00:00:00 2001 From: wilkie Date: Sun, 16 May 2010 18:25:26 -0400 Subject: [PATCH 13/14] Fixed Monitor to allow the owning Thread to not block itself. --- runtests.d | 2 +- runtime/monitor.d | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/runtests.d b/runtests.d index cb3a717f..79321a9e 100644 --- a/runtests.d +++ b/runtests.d @@ -75,7 +75,7 @@ private: foreach(item; ms) { foreach(feature; item) { auto tester = new Test(item, feature); - Console.putln("Testing", " : ", item.name, " ", feature); +// Console.putln("Testing", " : ", item.name, " ", feature); tester.run(); if (tester.failures > 0) { Console.forecolor = Color.Red; diff --git a/runtime/monitor.d b/runtime/monitor.d index 8d657171..91c4bcb0 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -13,13 +13,18 @@ module runtime.monitor; import runtime.gc; import synch.semaphore; +import synch.thread; +import synch.atomic; + import io.console; extern(C): struct Monitor { Semaphore semaphore; -} + Thread owner; + ulong count; + } void _d_monitorenter(Object h) { // The monitor object is the second pointer in the object @@ -27,16 +32,32 @@ void _d_monitorenter(Object h) { if (monitor is null) { monitor = new Monitor; monitor.semaphore = new Semaphore(1); + monitor.owner = Thread.current; + monitor.count = 1; // TODO: Should be an atomic exchange with null, and if it fails then // proceed to use that object. *(cast(Monitor**)h + 1) = monitor; + monitor.semaphore.down(); + } + else if (monitor.owner !is Thread.current) { + monitor.semaphore.down(); + } + else { + Atomic.increment(monitor.count); } - monitor.semaphore.down(); } void _d_monitorexit(Object h) { Monitor* monitor = *(cast(Monitor**)h + 1); - monitor.semaphore.up(); + if (monitor.owner is Thread.current) { + Atomic.decrement(monitor.count); + if (monitor.count == 0) { + monitor.semaphore.up(); + } + } + else { + monitor.semaphore.up(); + } } void _d_criticalenter(void* dcs) { From cb754a5c6ff04db52c8d6ff8a272992d46ad4b6c Mon Sep 17 00:00:00 2001 From: wilkie Date: Mon, 17 May 2010 00:52:20 -0400 Subject: [PATCH 14/14] Updated Thread class and wrote specifications that cover the synchronized keyword. --- Makefile | 2 +- platform/unix/common.d | 1 + platform/unix/scaffold/thread.d | 6 ++- runtime/monitor.d | 2 +- specs/runtime/synchronized.ds | 65 +++++++++++++++++++++++++++++++++ synch/thread.d | 21 ++++------- 6 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 specs/runtime/synchronized.ds diff --git a/Makefile b/Makefile index 6eca7ab4..69eb71dc 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ DFILES_TESTING = spec/support.d spec/logic.d spec/itemspecification.d spec/packa DFILES_SYNCH = synch/atomic.d synch/condition.d synch/barrier.d synch/mutex.d synch/semaphore.d synch/thread.d synch/timer.d DFILES_RSC = -DFILES_SPECS = .specs/runtime/array.d .specs/runtime/foreach.d .specs/core/application.d .specs/core/arguments.d .specs/core/date.d .specs/core/exception.d .specs/core/regex.d .specs/core/string.d .specs/core/time.d .specs/core/unicode.d .specs/core/util.d .specs/core/variant.d .specs/data/fibonacci.d .specs/data/heap.d .specs/data/queue.d .specs/data/stack.d .specs/hashes/digest.d .specs/hashes/md5.d .specs/hashes/sha1.d .specs/hashes/sha224.d .specs/hashes/sha256.d .specs/math/random.d .specs/runtime/switch.d +DFILES_SPECS = .specs/runtime/array.d .specs/runtime/foreach.d .specs/core/application.d .specs/core/arguments.d .specs/core/date.d .specs/core/exception.d .specs/core/regex.d .specs/core/string.d .specs/core/time.d .specs/core/unicode.d .specs/core/util.d .specs/core/variant.d .specs/data/fibonacci.d .specs/data/heap.d .specs/data/queue.d .specs/data/stack.d .specs/hashes/digest.d .specs/hashes/md5.d .specs/hashes/sha1.d .specs/hashes/sha224.d .specs/hashes/sha256.d .specs/math/random.d .specs/runtime/switch.d .specs/runtime/synchronized.d SOURCES = $(DFILES_SPECS) $(DFILES) $(DFILES_RUNTIME) $(DFILES_LOCALES) $(DFILES_RESOURCE) $(DFILES_IO) $(DFILES_SYNCH) $(DFILES_PARSING) $(DFILES_OPENGL) $(DFILES_CUI) $(DFILES_ANALYZING) $(DFILES_SCRIPTING) $(DFILES_BINDING) $(DFILES_TESTING) $(DFILES_MATH) $(DFILES_GRAPHICS) $(DFILES_HASHES) $(DFILES_RSC) $(DFILES_NETWORKING) $(DFILES_INTERFACES) $(DFILES_DATA) $(DFILES_CONSOLE) $(DFILES_BINARY_CODECS) $(DFILES_CODEC) $(DFILES_IMAGE_CODECS) $(DFILES_AUDIO_CODECS) $(DFILES_CORE) $(DFILES_GUI) $(DFILES_PARSERS) diff --git a/platform/unix/common.d b/platform/unix/common.d index 1c2c7c8e..5550bc76 100644 --- a/platform/unix/common.d +++ b/platform/unix/common.d @@ -486,6 +486,7 @@ extern (C): int pthread_getattr_np(pthread_t, pthread_attr_t*); int pthread_getconcurrency(); int pthread_getcpuclockid(pthread_t, clockid_t*); + int pthread_yield(); int pthread_cond_init(pthread_cond_t *, pthread_condattr_t *); int pthread_cond_destroy(pthread_cond_t *); diff --git a/platform/unix/scaffold/thread.d b/platform/unix/scaffold/thread.d index c825b63c..14a4a7c9 100644 --- a/platform/unix/scaffold/thread.d +++ b/platform/unix/scaffold/thread.d @@ -26,7 +26,11 @@ import platform.vars.semaphore; import binding.c; -void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) { +void ThreadYield() { + pthread_yield(); +} + +void ThreadSleep(ulong milliseconds) { timespec timetoexpire; timetoexpire.tv_sec = (milliseconds / 1000); diff --git a/runtime/monitor.d b/runtime/monitor.d index 91c4bcb0..1622118d 100644 --- a/runtime/monitor.d +++ b/runtime/monitor.d @@ -24,7 +24,7 @@ struct Monitor { Semaphore semaphore; Thread owner; ulong count; - } +} void _d_monitorenter(Object h) { // The monitor object is the second pointer in the object diff --git a/specs/runtime/synchronized.ds b/specs/runtime/synchronized.ds new file mode 100644 index 00000000..ecd53a39 --- /dev/null +++ b/specs/runtime/synchronized.ds @@ -0,0 +1,65 @@ +module specs.runtime.synchronized; + +import synch.thread; +import io.console; + +describe runtime { + describe synchronized { + it should_block_threads { + // Lets not put it on the stack + static bool status = false; + + // something to adhere to + class A { + } + + A a = new A(); + + void foo(bool b) { + synchronized(a) { + status = true; + } + } + + Thread t = new Thread(&foo); + synchronized(a) { + t.start(); + Thread.sleep(500); + should(status == false); + } + + while(status == false) { + // Constantly yield + Thread.yield(); + } + } + + it should_not_block_the_same_thread_twice { + static bool status = false; + + // something to adhere to + class A { + } + + void run(bool b) { + A a = new A(); + + void foo() { + synchronized(a) { + status = true; + } + } + + synchronized(a) { + foo(); + } + } + + Thread t = new Thread(&run); + t.start; + + Thread.sleep(1000); + should(status == true); + } + } +} diff --git a/synch/thread.d b/synch/thread.d index 4b3992f7..42083955 100644 --- a/synch/thread.d +++ b/synch/thread.d @@ -79,23 +79,16 @@ class Thread { _thread_callback = null; } - // Description: This function will tell whether or not the current thread being executed is the thread created via this class. - // Returns: Will return true when this thread is the current thread executing and false otherwise. - bool isCurrent() { - if (_inited) { - return ThreadIsCurrent(_pfvars); - } - - return false; - } - // Description: This function will yield the thread for a certain amount of time. // milliseconds: The number of milliseconds to yield. - void sleep(ulong milliseconds) { + static void sleep(ulong milliseconds) { // we are given a long for length, windows only has an int function - if (_inited) { - ThreadSleep(_pfvars, milliseconds); - } + ThreadSleep(milliseconds); + } + + // Description: This function will yield the thread. + static void yield() { + ThreadYield(); } // Description: This function will start the thread and call the threadProc() function, which will in turn execute an external delegate if provided.