Răsfoiți Sursa

fixes for hlc, added ucs2 for wchar_16 emulation function

Nicolas Cannasse 9 ani în urmă
părinte
comite
e6bdee2c6a
12 a modificat fișierele cu 267 adăugiri și 33 ștergeri
  1. 1 0
      .gitignore
  2. 33 13
      Makefile
  3. 1 0
      hl.vcxproj
  4. 4 1
      src/hl.h
  5. 13 4
      src/hlc.h
  6. 6 6
      src/std/buffer.c
  7. 0 1
      src/std/error.c
  8. 0 5
      src/std/obj.c
  9. 1 1
      src/std/regexp.c
  10. 1 1
      src/std/string.c
  11. 0 1
      src/std/sys.c
  12. 207 0
      src/std/ucs2.c

+ 1 - 0
.gitignore

@@ -21,3 +21,4 @@ x64
 /src/_main4.c
 /src/_main4.c
 /src/_main5.c
 /src/_main5.c
 /src/_main6.c
 /src/_main6.c
+*.a

+ 33 - 13
Makefile

@@ -1,10 +1,12 @@
 CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre
 CFLAGS = -Wall -O3 -I src -msse2 -mfpmath=sse -std=c11 -I include/pcre
 LFLAGS =
 LFLAGS =
 
 
-SRC = src/alloc.c src/std/array.c src/std/buffer.c src/std/bytes.c src/std/cast.c src/std/date.c src/std/error.c \
-	src/std/fun.c src/std/maps.c src/std/math.c src/std/obj.c src/std/regexp.c src/std/string.c src/std/sys.c \
-	src/std/types.c
+SRC = src/alloc.o src/std/array.o src/std/buffer.o src/std/bytes.o src/std/cast.o src/std/date.o src/std/error.o \
+	src/std/fun.o src/std/maps.o src/std/math.o src/std/obj.o src/std/regexp.o src/std/string.o src/std/sys.o \
+	src/std/types.o src/std/ucs2.o
 
 
+BOOT = src/_main.o
+	
 # Cygwin
 # Cygwin
 ifeq ($(OS),Windows_NT)
 ifeq ($(OS),Windows_NT)
 
 
@@ -22,27 +24,45 @@ LFLAGS += -Wl,--export-dynamic
 
 
 endif
 endif
 
 
-all: hl32 hl64
+all: libs hlc32 clean hlc64
+
+libs: hl32lib hl64lib clean
+
+hlc32:
+	make ARCH=32 clean_o hlc
 
 
+hlc64:
+	make ARCH=64 clean_o hlc
+	
 hl32:
 hl32:
 	make ARCH=32 build
 	make ARCH=32 build
 	
 	
 hl64:
 hl64:
 	make ARCH=64 build
 	make ARCH=64 build
-
-bench:
-	(cd bench && haxe --interp -main Bench)
 	
 	
-use: all
-	cp hl32.exe Release/hl.exe
-	cp hl64.exe x64/Release/hl.exe
+hl32lib:
+	make ARCH=32 clean_o lib
+	
+hl64lib:
+	make ARCH=64 clean_o lib
+
+lib: ${SRC}
+	${AR} rcs hl${ARCH}lib.a ${SRC}
 
 
-build: $(SRC)
-	${CC} ${CFLAGS} ${LFLAGS} -o hl$(ARCH) ${SRC}
+hlc: ${BOOT}
+	${CC} ${CFLAGS} ${LFLAGS} -o hlc${ARCH} ${BOOT} hl${ARCH}lib.a
+	
+build: ${SRC}
+	${CC} ${CFLAGS} ${LFLAGS} -o hl${ARCH} ${SRC}
 	
 	
 .SUFFIXES : .c .o
 .SUFFIXES : .c .o
 
 
 .c.o :
 .c.o :
 	${CC} ${CFLAGS} -o $@ -c $<
 	${CC} ${CFLAGS} -o $@ -c $<
 	
 	
-.PHONY: hl32 hl64 bench
+clean_o:
+	rm -rf ${SRC} ${BOOT}
+	
+clean: clean_o
+	
+.PHONY: hl32 hl64 hlc32 hlc64 hlc

+ 1 - 0
hl.vcxproj

@@ -171,6 +171,7 @@
     <ClCompile Include="src\std\string.c" />
     <ClCompile Include="src\std\string.c" />
     <ClCompile Include="src\std\sys.c" />
     <ClCompile Include="src\std\sys.c" />
     <ClCompile Include="src\std\types.c" />
     <ClCompile Include="src\std\types.c" />
+    <ClCompile Include="src\std\ucs2.c" />
     <ClCompile Include="src\_main.c" />
     <ClCompile Include="src\_main.c" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>

+ 4 - 1
src/hl.h

@@ -132,6 +132,7 @@ typedef long long int64;
 #	include <wchar.h>
 #	include <wchar.h>
 typedef wchar_t	uchar;
 typedef wchar_t	uchar;
 #	define USTR(str)	L##str
 #	define USTR(str)	L##str
+#	define HL_NATIVE_WCHAR_FUN
 #	define usprintf		swprintf
 #	define usprintf		swprintf
 #	define uprintf		wprintf
 #	define uprintf		wprintf
 #	define ustrlen		wcslen
 #	define ustrlen		wcslen
@@ -141,6 +142,7 @@ typedef wchar_t	uchar;
 #	define utoi(s,end)	wcstol(s,end,10)
 #	define utoi(s,end)	wcstol(s,end,10)
 #	define ucmp(a,b)	wcscmp(a,b)
 #	define ucmp(a,b)	wcscmp(a,b)
 #	define strtou(out,size,str) mbstowcs(out,str,size)	
 #	define strtou(out,size,str) mbstowcs(out,str,size)	
+#	define utostr(out,size,str) wcstombs(out,str,size)	
 #else
 #else
 #	include <stdarg.h>
 #	include <stdarg.h>
 typedef unsigned short uchar;
 typedef unsigned short uchar;
@@ -152,9 +154,10 @@ extern double utod( const uchar *str, uchar **end );
 extern int utoi( const uchar *str, uchar **end );
 extern int utoi( const uchar *str, uchar **end );
 extern int ucmp( const uchar *a, const uchar *b );
 extern int ucmp( const uchar *a, const uchar *b );
 extern int strtou( uchar *out, int out_size, const char *str ); 
 extern int strtou( uchar *out, int out_size, const char *str ); 
+extern int utostr( char *out, int out_size, const uchar *str ); 
 extern int usprintf( uchar *out, int out_size, const uchar *fmt, ... );
 extern int usprintf( uchar *out, int out_size, const uchar *fmt, ... );
-extern int uprintf( const uchar *fmt, ... );
 extern int uvsprintf( uchar *out, const uchar *fmt, va_list arglist );
 extern int uvsprintf( uchar *out, const uchar *fmt, va_list arglist );
+extern void uprintf( const uchar *fmt, const uchar *str );
 #endif
 #endif
 
 
 // ---- TYPES -------------------------------------------
 // ---- TYPES -------------------------------------------

+ 13 - 4
src/hlc.h

@@ -31,17 +31,26 @@
 #	define PAD_64_VAL
 #	define PAD_64_VAL
 #endif
 #endif
 
 
+#ifdef HLC_BOOT
+
+// undefine some commonly used names that can clash with class/var name
+#undef CONST
+
+// disable some warnings triggered by HLC code generator
+
 #ifdef HL_VCC
 #ifdef HL_VCC
 #	pragma warning(disable:4702) // unreachable code 
 #	pragma warning(disable:4702) // unreachable code 
 #	pragma warning(disable:4100) // unreferenced param
 #	pragma warning(disable:4100) // unreferenced param
 #	pragma warning(disable:4101) // unreferenced local var
 #	pragma warning(disable:4101) // unreferenced local var
 #	pragma warning(disable:4723) // potential divide by 0
 #	pragma warning(disable:4723) // potential divide by 0
+#else
+#	pragma GCC diagnostic ignored "-Wunused-variable"
+#	pragma GCC diagnostic ignored "-Wunused-function"
+#	pragma GCC diagnostic ignored "-Wunused-but-set-variable"
+#	pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#	pragma GCC diagnostic ignored "-Wcomment" // comment in comment
 #endif
 #endif
 
 
-#undef CONST
-
-#ifndef HLC_NO_STATIC
-
 static void hl_null_access() {
 static void hl_null_access() {
 	hl_error_msg(USTR("Null access"));
 	hl_error_msg(USTR("Null access"));
 }
 }

+ 6 - 6
src/std/buffer.c

@@ -114,19 +114,19 @@ static void hl_buffer_addr( hl_buffer *b, void *data, hl_type *t, vlist *stack )
 	uchar buf[32];
 	uchar buf[32];
 	switch( t->kind ) {
 	switch( t->kind ) {
 	case HI8:
 	case HI8:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),*(char*)data));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),(int)*(char*)data));
 		break;
 		break;
 	case HI16:
 	case HI16:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),*(short*)data));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),(int)*(short*)data));
 		break;
 		break;
 	case HI32:
 	case HI32:
 		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),*(int*)data));
 		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),*(int*)data));
 		break;
 		break;
 	case HF32:
 	case HF32:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.16f"),*(float*)data));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.9f"),*(float*)data));
 		break;
 		break;
 	case HF64:
 	case HF64:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.19g"),*(double*)data));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.17g"),*(double*)data));
 		break;
 		break;
 	case HBYTES:
 	case HBYTES:
 		hl_buffer_str(b,*(uchar**)data);
 		hl_buffer_str(b,*(uchar**)data);
@@ -174,10 +174,10 @@ static void hl_buffer_rec( hl_buffer *b, vdynamic *v, vlist *stack ) {
 		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),v->v.i));
 		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%d"),v->v.i));
 		break;
 		break;
 	case HF32:
 	case HF32:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.16f"),v->v.f));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.9f"),v->v.f));
 		break;
 		break;
 	case HF64:
 	case HF64:
-		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.19g"),v->v.d));
+		hl_buffer_str_sub(b,buf,usprintf(buf,32,USTR("%.17g"),v->v.d));
 		break;
 		break;
 	case HBOOL:
 	case HBOOL:
 		if( v->v.b )
 		if( v->v.b )

+ 0 - 1
src/std/error.c

@@ -1,4 +1,3 @@
-#define HLC_NO_STATIC
 #include <hlc.h>
 #include <hlc.h>
 #include <stdarg.h>
 #include <stdarg.h>
 #include <string.h>
 #include <string.h>

+ 0 - 5
src/std/obj.c

@@ -272,11 +272,6 @@ vvirtual *hl_to_virtual( hl_type *vt, vdynamic *obj ) {
 	return v;
 	return v;
 }
 }
 
 
-#define B2(t1,t2) ((t1) + ((t2) * HLAST))
-#define fetch_i(data,src,dst) src == dst ? *(int*)(data) : hl_dyn_casti(data,src,dst)
-#define fetch_d(data,src) src->kind == HF64 ? *(double*)(data) : hl_dyn_castd(data,src)
-#define fetch_p(data,src,dst) src == dst ? *(void**)(data) : hl_dyn_castp(data,src,dst)
-
 static hl_field_lookup *hl_dyn_alloc_field( vdynobj *o, int hfield, hl_type *t ) {
 static hl_field_lookup *hl_dyn_alloc_field( vdynobj *o, int hfield, hl_type *t ) {
 	int pad = hl_pad_size(o->dataSize, t);
 	int pad = hl_pad_size(o->dataSize, t);
 	int size = hl_type_size(t);
 	int size = hl_type_size(t);

+ 1 - 1
src/std/regexp.c

@@ -52,7 +52,7 @@ HL_PRIM ereg *regexp_regexp_new_options( vbyte *str, vbyte *opts ) {
 	if( p == NULL ) {
 	if( p == NULL ) {
 		hl_buffer *b = hl_alloc_buffer();
 		hl_buffer *b = hl_alloc_buffer();
 		hl_buffer_str(b,USTR("Regexp compilation error : "));
 		hl_buffer_str(b,USTR("Regexp compilation error : "));
-		hl_buffer_cstr(b,"abcde");
+		hl_buffer_cstr(b,error);
 		hl_buffer_str(b,USTR(" in "));
 		hl_buffer_str(b,USTR(" in "));
 		hl_buffer_str(b,(uchar*)str);
 		hl_buffer_str(b,(uchar*)str);
 		hl_error_msg(USTR("%s"),hl_buffer_content(b,NULL));
 		hl_error_msg(USTR("%s"),hl_buffer_content(b,NULL));

+ 1 - 1
src/std/string.c

@@ -9,7 +9,7 @@ HL_PRIM vbyte *hl_itos( int i, int *len ) {
 
 
 HL_PRIM vbyte *hl_ftos( double d, int *len ) {
 HL_PRIM vbyte *hl_ftos( double d, int *len ) {
 	uchar tmp[48];
 	uchar tmp[48];
-	int k = (int)usprintf(tmp,48,USTR("%.16g"),d);
+	int k = (int)usprintf(tmp,48,USTR("%.16g"),d); // don't use the last digit (eg 5.1 = 5.09999..996)
 	*len = k;
 	*len = k;
 	return hl_bcopy((vbyte*)tmp,(k + 1) << 1);
 	return hl_bcopy((vbyte*)tmp,(k + 1) << 1);
 }
 }

+ 0 - 1
src/std/sys.c

@@ -40,7 +40,6 @@ HL_PRIM int hl_random( int max ) {
 
 
 #ifndef HL_JIT
 #ifndef HL_JIT
 
 
-#define HLC_NO_STATIC
 #include <hlc.h>
 #include <hlc.h>
 #if defined(HL_VCC) && defined(_DEBUG)
 #if defined(HL_VCC) && defined(_DEBUG)
 #	include <crtdbg.h>
 #	include <crtdbg.h>

+ 207 - 0
src/std/ucs2.c

@@ -0,0 +1,207 @@
+#include <hl.h>
+#ifndef HL_NATIVE_WCHAR_FUN
+#include <stdarg.h>
+
+int ustrlen( const uchar *str ) {
+	const uchar *p = str;
+	while( *p ) p++;
+	return (int)(p - str);
+}
+
+uchar *ustrdup( const uchar *str ) {
+	int len = ustrlen(str);
+	int size = (len + 1) << 1;
+	uchar *d = (uchar*)malloc(size);
+	memcpy(d,str,size);
+	return d;
+}
+
+double utod( const uchar *str, uchar **end ) {
+	char buf[31];
+	char *bend;
+	int i = 0;
+	double v;
+	while( *str == ' ' ) str++;
+	while( i < 30 ) {
+		int c = *str++;
+		if( (c < '0' || c > '9') && c != '.' && c != 'e' && c != 'E' && c != '-' && c != '+' )
+			break;
+		buf[i++] = (char)c;
+	}
+	buf[i] = 0;
+	v = strtod(buf,&bend);
+	*end = (uchar*)(str - 1) + (bend - buf);
+	return v;
+}
+
+int utoi( const uchar *str, uchar **end ) {
+	char buf[17];
+	char *bend;
+	int i = 0;
+	int v;
+	while( *str == ' ' ) str++;
+	while( i < 16 ) {
+		int c = *str++;
+		if( (c < '0' || c > '9') && c != '-' )
+			break;
+		buf[i++] = (char)c;
+	}
+	buf[i] = 0;
+	v = strtol(buf,&bend,10);
+	*end = (uchar*)(str - 1) + (bend - buf);
+	return v;
+}
+
+int ucmp( const uchar *a, const uchar *b ) {
+	while(true) {
+		int d = (unsigned)*a - (unsigned)*b; 
+		if( d ) return d;
+		if( !*a ) return 0;
+		a++;
+		b++;
+	}
+}
+
+int strtou( uchar *out, int out_size, const char *str ) {
+	uchar *start = out;
+	if( out_size <= 0 ) return -1;
+	while( out_size-- ) {
+		if( *str == 0 ) break;
+		*out++ = *str++;
+	}
+	*out = 0;
+	return (int)(out - start);
+}
+
+int uvsprintf( uchar *out, const uchar *fmt, va_list arglist ) {
+	uchar *start = out;
+	char cfmt[20];
+	char tmp[32];
+	uchar c;
+	while(true) {
+sprintf_loop:
+		c = *fmt++;
+		switch( c ) {
+		case 0:
+			*out = 0;
+			return (int)(out - start);
+		case '%':
+			{
+				int i = 0, size = 0;
+				cfmt[i++] = '%';
+				while( true ) {
+					c = *fmt++;
+					cfmt[i++] = (char)c;
+					switch( c ) {
+					case 'd':
+						cfmt[i++] = 0;
+						size = sprintf(tmp,cfmt,va_arg(arglist,int));
+						goto sprintf_add;
+					case 'f':
+						cfmt[i++] = 0;
+						size = sprintf(tmp,cfmt,va_arg(arglist,double)); // according to GCC warning, float is promoted to double in var_args
+						goto sprintf_add;
+					case 'g':
+						cfmt[i++] = 0;
+						size = sprintf(tmp,cfmt,va_arg(arglist,double));
+						goto sprintf_add;
+					case 'x':
+					case 'X':
+						cfmt[i++] = 0;
+						if( cfmt[i-3] == 'l' )
+							size = sprintf(tmp,cfmt,va_arg(arglist,void*));
+						else
+							size = sprintf(tmp,cfmt,va_arg(arglist,int));
+						goto sprintf_add;
+					case 's':
+						if( i != 2 ) hl_fatal("Unsupported printf format"); // no support for precision qualifier
+						{
+							uchar *s = va_arg(arglist,uchar *);
+							while( *s )
+								*out++ = *s++;
+							goto sprintf_loop;
+						}
+					case '.':
+					case '0':
+					case '1':
+					case '2':
+					case '3':
+					case '4':
+					case '5':
+					case '6':
+					case '7':
+					case '8':
+					case '9':
+					case 'l':
+						break;
+					default:
+						hl_fatal("Unsupported printf format");
+						break;
+					}
+				}
+sprintf_add:
+				// copy from c string to u string
+				i = 0;
+				while( i < size )
+					*out++ = tmp[i++];
+			}
+			break;
+		default:
+			*out++ = c;
+			break;
+		}
+	}
+	return 0;
+}
+
+int usprintf( uchar *out, int out_size, const uchar *fmt, ... ) {
+	va_list args;
+	int ret;
+	va_start(args, fmt);
+	ret = uvsprintf(out, fmt, args);
+	va_end(args);
+	return ret;
+}
+
+// USE UTF-8 encoding
+int utostr( char *out, int out_size, const uchar *str ) {
+	char *start = out;
+	char *end = out + out_size - 1; // final 0
+	if( out_size <= 0 ) return 0;
+	while( out < end ) {
+		unsigned int c = *str++;
+		if( c == 0 ) break;
+		if( c < 0x80 )
+			*out++ = c;
+		else if( c < 0x800 ) {
+			if( out + 2 > end ) break;
+			*out++ = 0xC0|(c>>6);
+			*out++ = 0x80|(c&63);
+		} else {
+			if( out + 3 > end ) break;
+			*out++ = 0xE0|(c>>12);
+			*out++ = 0x80|((c>>6)&63);
+			*out++ = 0x80|(c&63);
+		}
+	}
+	*out = 0;
+	return (int)(out - start);
+}
+
+static char *utos( const uchar *s ) {
+	int len = ustrlen(s);
+	char *out = (char*)malloc(len + 1);
+	if( utostr(out,len+1,s) < 0 )
+		*out = 0;
+	return out;
+}
+
+void uprintf( const uchar *fmt, const uchar *str ) {
+	char *cfmt = utos(fmt);
+	char *cstr = utos(str);
+	printf(cfmt,cstr);
+	free(cfmt);
+	free(cstr);
+}
+
+#endif