(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
authorDarkvater
Sun, 02 Oct 2005 22:39:56 +0000
changeset 2482 374f6395847d
parent 2481 6748e40ceb70
child 2483 e2d5f55daf63
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Fix warning in graph_gui.c with const problem
functions.h
graph_gui.c
map.c
minilzo.c
network_core.h
openttd.vcproj
saveload.c
stdafx.h
strgen/strgen.c
train_cmd.c
viewport.c
win32.c
win64.asm
--- a/functions.h	Sun Oct 02 12:31:00 2005 +0000
+++ b/functions.h	Sun Oct 02 22:39:56 2005 +0000
@@ -131,8 +131,8 @@
 
 
 // Used for profiling
-#define TIC() { extern uint32 rdtsc(void); uint32 _xxx_ = rdtsc(); static float __avg__;
-#define TOC(s) 	_xxx_ = rdtsc() - _xxx_; __avg__=__avg__*0.99+_xxx_*0.01; printf("%s: %8d %f\n", s, _xxx_,__avg__); }
+#define TIC() { extern uint32 _rdtsc(void); uint32 _xxx_ = _rdtsc(); static float __avg__;
+#define TOC(s) 	_xxx_ = _rdtsc() - _xxx_; __avg__=__avg__*0.99+_xxx_*0.01; printf("%s: %8d %f\n", s, _xxx_,__avg__); }
 
 
 void SetDate(uint date);
--- a/graph_gui.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/graph_gui.c	Sun Oct 02 22:39:56 2005 +0000
@@ -824,10 +824,10 @@
 
 static void CompanyLeagueWndProc(Window *w, WindowEvent *e)
 {
-	switch(e->event) {
+	switch (e->event) {
 	case WE_PAINT: {
 		const Player* p;
-		const Player* plist[MAX_PLAYERS];
+		Player const *plist[MAX_PLAYERS];
 		uint pl_num;
 		uint i;
 
@@ -840,7 +840,7 @@
 		}
 		assert(pl_num > 0);
 
-		qsort(plist, pl_num, sizeof(Player*), _perf_hist_comp);
+		qsort((void*)plist, pl_num, sizeof(Player*), _perf_hist_comp);
 
 		i = 0;
 		do {
--- a/map.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/map.c	Sun Oct 02 22:39:56 2005 +0000
@@ -7,6 +7,11 @@
 #include "macros.h"
 #include "map.h"
 
+#if defined(_MSC_VER) && _MSC_VER >= 1400 /* VStudio 2005 is stupid! */
+/* Why the hell is that not in all MSVC headers?? */
+_CRTIMP void __cdecl _assert(void *, void *, unsigned);
+#endif
+
 uint _map_log_x;
 uint _map_size_x;
 uint _map_size_y;
--- a/minilzo.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/minilzo.c	Sun Oct 02 22:39:56 2005 +0000
@@ -329,6 +329,13 @@
 #  endif
 #endif
 #if !defined(__LZO_HAVE_PTR_T)
+#	if defined(_WIN64)
+		typedef unsigned __int64 lzo_ptr_t;
+		typedef signed __int64   lzo_sptr_r;
+#		define __LZO_HAVE_PTR_T
+#	endif
+#endif
+#if !defined(__LZO_HAVE_PTR_T)
 #  if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
 #	if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
 	   typedef unsigned long	lzo_ptr_t;
--- a/network_core.h	Sun Oct 02 12:31:00 2005 +0000
+++ b/network_core.h	Sun Oct 02 22:39:56 2005 +0000
@@ -12,10 +12,10 @@
 #ifdef ENABLE_NETWORK
 
 // Windows stuff
-#if defined(WIN32)
-#include <windows.h>
+#if defined(WIN32) || defined(WIN64)
 #include <winsock2.h>
 #include <ws2tcpip.h>
+#include <windows.h>
 
 #if !(defined(__MINGW32__) || defined(__CYGWIN__))
 	// Windows has some different names for some types..
--- a/openttd.vcproj	Sun Oct 02 12:31:00 2005 +0000
+++ b/openttd.vcproj	Sun Oct 02 22:39:56 2005 +0000
@@ -826,6 +826,9 @@
 			<File
 				RelativePath=".\ai\default\default.c">
 			</File>
+			<File
+				RelativePath=".\ai\default\default.h">
+			</File>
 			<Filter
 				Name="trolly"
 				Filter="">
--- a/saveload.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/saveload.c	Sun Oct 02 22:39:56 2005 +0000
@@ -958,7 +958,9 @@
 
 // This is needed to zlib uses the stdcall calling convention on visual studio
 #ifdef _MSC_VER
-#define ZLIB_WINAPI
+#	ifndef ZLIB_WINAPI
+#		define ZLIB_WINAPI
+#	endif
 #endif
 
 #include <zlib.h>
--- a/stdafx.h	Sun Oct 02 12:31:00 2005 +0000
+++ b/stdafx.h	Sun Oct 02 22:39:56 2005 +0000
@@ -121,14 +121,25 @@
 #  define NOT_REACHED() _assume(0)
 # endif
 int CDECL snprintf(char *str, size_t size, const char *format, ...);
+#if _MSC_VER < 1400
 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
+#endif
 # undef TTD_ALIGNMENT_4
 # undef TTD_ALIGNMENT_2
 # define GCC_PACK
 #endif
 
+// Needed for old PlatformSDK versions (e.g. VC6)
+#if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
+#	ifndef _W64
+#		define _W64
+#	endif
+typedef _W64 int INT_PTR, *PINT_PTR;
+typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
+#endif
+
 // Windows has always LITTLE_ENDIAN
-#if defined(WIN32) || defined(__OS2__)
+#if defined(WIN32) || defined(__OS2__) || defined(WIN64)
   #define TTD_LITTLE_ENDIAN
 #else
 // Else include endian[target/host].h, which has the endian-type, autodetected by the Makefile
--- a/strgen/strgen.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/strgen/strgen.c	Sun Oct 02 22:39:56 2005 +0000
@@ -8,7 +8,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
-#if !defined(WIN32) || defined(__CYGWIN__)
+#if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
 #include <unistd.h>
 #endif
 
@@ -996,7 +996,7 @@
 		unlink("tmp.xxx");
 	} else {
 		// else rename tmp.xxx into filename
-#if defined(WIN32)
+#if defined(WIN32) || defined(WIN64)
 		unlink(filename);
 #endif
 		if (rename("tmp.xxx", filename) == -1) Fatal("rename() failed");
--- a/train_cmd.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/train_cmd.c	Sun Oct 02 22:39:56 2005 +0000
@@ -1943,7 +1943,7 @@
 static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0};
 #if PF_BENCHMARK
 #if !defined(_MSC_VER)
-unsigned int rdtsc()
+unsigned int _rdtsc()
 {
      unsigned int high, low;
 
@@ -1951,7 +1951,8 @@
      return low;
 }
 #else
-static unsigned int _declspec(naked) rdtsc(void)
+#ifndef _M_AMD64
+static unsigned int _declspec(naked) _rdtsc(void)
 {
 	_asm {
 		rdtsc
@@ -1960,6 +1961,7 @@
 }
 #endif
 #endif
+#endif
 
 
 
@@ -1969,7 +1971,7 @@
 	TrainTrackFollowerData fd;
 	uint best_track;
 #if PF_BENCHMARK
-	int time = rdtsc();
+	int time = _rdtsc();
 	static float f;
 #endif
 
@@ -2040,7 +2042,7 @@
 	}
 
 #if PF_BENCHMARK
-	time = rdtsc() - time;
+	time = _rdtsc() - time;
 	f = f * 0.99 + 0.01 * time;
 	printf("PF time = %d %f\n", time, f);
 #endif
--- a/viewport.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/viewport.c	Sun Oct 02 22:39:56 2005 +0000
@@ -2229,7 +2229,7 @@
 		VpStartPreSizing();
 
 	if ( (int)icon < 0)
-		SetAnimatedMouseCursor(_animcursors[~icon]);
+		SetAnimatedMouseCursor(_animcursors[~(int32)icon]);
 	else
 		SetMouseCursor(icon);
 }
--- a/win32.c	Sun Oct 02 12:31:00 2005 +0000
+++ b/win32.c	Sun Oct 02 22:39:56 2005 +0000
@@ -61,6 +61,11 @@
 }
 
 #ifdef _MSC_VER
+#	ifdef _M_AMD64
+void* _get_save_esp(void);
+uint64 _rdtsc(void);
+#	endif
+
 static const char *_exception_string;
 #endif
 
@@ -452,6 +457,37 @@
 	if (_exception_string)
 		output += sprintf(output, "Reason: %s\r\n", _exception_string);
 
+#ifdef _M_AMD64
+	output += sprintf(output, "Exception %.8X at %.16IX\r\n"
+		"Registers:\r\n"
+		"RAX: %.16llX RBX: %.16llX RCX: %.16llX RDX: %.16llX\r\n"
+		"RSI: %.16llX RDI: %.16llX RBP: %.16llX RSP: %.16llX\r\n"
+		"R8:  %.16llX R9:  %.16llX R10: %.16llX R11: %.16llX\r\n"
+		"R12: %.16llX R13: %.16llX R14: %.16llX R15: %.16llX\r\n"
+		"RIP: %.16llX EFLAGS: %.8X\r\n"
+		"\r\nBytes at CS:RIP:\r\n",
+		ep->ExceptionRecord->ExceptionCode,
+		ep->ExceptionRecord->ExceptionAddress,
+		ep->ContextRecord->Rax,
+		ep->ContextRecord->Rbx,
+		ep->ContextRecord->Rcx,
+		ep->ContextRecord->Rdx,
+		ep->ContextRecord->Rsi,
+		ep->ContextRecord->Rdi,
+		ep->ContextRecord->Rbp,
+		ep->ContextRecord->Rsp,
+		ep->ContextRecord->R8,
+		ep->ContextRecord->R9,
+		ep->ContextRecord->R10,
+		ep->ContextRecord->R11,
+		ep->ContextRecord->R12,
+		ep->ContextRecord->R13,
+		ep->ContextRecord->R14,
+		ep->ContextRecord->R15,
+		ep->ContextRecord->Rip,
+		ep->ContextRecord->EFlags
+	);
+#else
 	output += sprintf(output, "Exception %.8X at %.8X\r\n"
 		"Registers:\r\n"
 		" EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\r\n"
@@ -471,9 +507,14 @@
 		ep->ContextRecord->Eip,
 		ep->ContextRecord->EFlags
 	);
+#endif
 
 	{
+#ifdef _M_AMD64
+		byte *b = (byte*)ep->ContextRecord->Rip;
+#else
 		byte *b = (byte*)ep->ContextRecord->Eip;
+#endif
 		int i;
 		for (i = 0; i != 24; i++) {
 			if (IsBadReadPtr(b, 1)) {
@@ -491,7 +532,11 @@
 
 	{
 		int i,j;
+#ifdef _M_AMD64
+		uint32 *b = (uint32*)ep->ContextRecord->Rsp;
+#else
 		uint32 *b = (uint32*)ep->ContextRecord->Esp;
+#endif
 		for (j = 0; j != 24; j++) {
 			for (i = 0; i != 8; i++) {
 				if (IsBadReadPtr(b,sizeof(uint32))) {
@@ -530,8 +575,13 @@
 	CloseConsoleLogIfActive();
 
 	if (_safe_esp) {
+#ifdef _M_AMD64
+		ep->ContextRecord->Rip = (DWORD64)Handler2;
+		ep->ContextRecord->Rsp = (DWORD64)_safe_esp;
+#else
 		ep->ContextRecord->Eip = (DWORD)Handler2;
 		ep->ContextRecord->Esp = (DWORD)_safe_esp;
+#endif
 		return EXCEPTION_CONTINUE_EXECUTION;
 	}
 
@@ -541,9 +591,13 @@
 
 static void Win32InitializeExceptions(void)
 {
+#ifdef _M_AMD64
+	_safe_esp = _get_save_esp();
+#else
 	_asm {
 		mov _safe_esp, esp
 	}
+#endif
 
 	SetUnhandledExceptionFilter(ExceptionHandler);
 }
@@ -997,8 +1051,8 @@
 }
 
 
-#if defined(_MSC_VER)
-uint32 _declspec(naked) rdtsc(void)
+#if defined(_MSC_VER) && !defined(_M_AMD64)
+uint64 _declspec(naked) _rdtsc(void)
 {
 	_asm {
 		rdtsc
@@ -1025,9 +1079,9 @@
 
 	// redirect unbuffered STDIN, STDOUT, STDERR to the console
 #if !defined(__CYGWIN__)
-	*stdout = *_fdopen( _open_osfhandle((long)hand, _O_TEXT), "w" );
-	*stdin = *_fdopen(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
-	*stderr = *_fdopen(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
+	*stdout = *_fdopen( _open_osfhandle((intptr_t)hand, _O_TEXT), "w" );
+	*stdin = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" );
+	*stderr = *_fdopen(_open_osfhandle((intptr_t)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" );
 #else
 	// open_osfhandle is not in cygwin
 	*stdout = *fdopen(1, "w" );
@@ -1082,9 +1136,9 @@
 	// setup random seed to something quite random
 #if defined(_MSC_VER)
 	{
-		uint64 seed = rdtsc();
-		_random_seeds[0][0] = GB(seed,  0, 32);
-		_random_seeds[0][1] = GB(seed, 32, 32);
+		ULARGE_INTEGER seed; seed.QuadPart = _rdtsc();
+		_random_seeds[0][0] = seed.LowPart;
+		_random_seeds[0][1] = seed.HighPart;
 	}
 #else
 	_random_seeds[0][0] = GetTickCount();
@@ -1155,6 +1209,7 @@
 	return ret;
 }
 
+#if _MSC_VER < 1400 /* Already defined in VS 2005 */
 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
 {
 	int ret;
@@ -1162,6 +1217,7 @@
 	if (ret < 0) str[size - 1] = '\0';
 	return ret;
 }
+#endif
 
 /**
  * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/win64.asm	Sun Oct 02 22:39:56 2005 +0000
@@ -0,0 +1,17 @@
+		.CODE
+
+PUBLIC	_get_save_esp
+_get_save_esp:
+		MOV	RAX,RSP
+		RET
+
+PUBLIC	_rdtsc
+_rdtsc:
+		RDTSC
+		MOV RCX,RDX
+		SHL RCX,32
+		AND RAX,0FFFFFFFFh
+		OR  RAX,RCX
+		RET
+
+		END