Skip to content

Commit

Permalink
Merge pull request #3 from MichalStrehovsky/bit32
Browse files Browse the repository at this point in the history
Compile as x86
  • Loading branch information
ascpixi committed Jan 16, 2024
2 parents 5764b6b + a5a97da commit 974bf58
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/BFlat.ZeroLib/Internal/Stubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static unsafe void RhpAssignRef(void** dst, void* r)
static unsafe MethodTable** AllocObject(uint size)
{
#if WINDOWS
[DllImport("kernel32")]
[DllImport("kernel32", EntryPoint = "_LocalAlloc@8")]
static extern MethodTable** LocalAlloc(uint flags, uint size);
MethodTable** result = LocalAlloc(0x40, size);
#elif LINUX
Expand Down
4 changes: 2 additions & 2 deletions src/BFlat.ZeroLib/System/Console.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private enum BOOL : int
TRUE = 1,
}

[DllImport("kernel32")]
[DllImport("kernel32", EntryPoint = "_GetStdHandle@4")]
private static unsafe extern IntPtr GetStdHandle(int c);

private readonly static IntPtr s_outputHandle = GetStdHandle(-11);
Expand Down Expand Up @@ -181,7 +181,7 @@ public static void SetCursorPosition(int x, int y)
SetConsoleCursorPosition(s_outputHandle, new COORD { X = (short)x, Y = (short)y });
}

[DllImport("kernel32", EntryPoint = "WriteConsoleW")]
[DllImport("kernel32", EntryPoint = "_WriteConsoleW@20")]
private static unsafe extern BOOL WriteConsole(IntPtr handle, void* buffer, int numChars, int* charsWritten, void* reserved);

public static unsafe void Write(char c)
Expand Down
2 changes: 1 addition & 1 deletion src/BFlat.ZeroLib/System/Environment.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static partial class Environment

public static long TickCount64 => GetTickCount64();

[DllImport("kernel32")]
[DllImport("kernel32", EntryPoint = "_RaiseFailFastException@12")]
private static extern void RaiseFailFastException(IntPtr a, IntPtr b, int flags);

public static void FailFast(string message)
Expand Down
2 changes: 1 addition & 1 deletion src/SmolSharp.HelloWorld/SmolSharp.HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/SmolSharp.Mandelbrot/SmolSharp.Mandelbrot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/SmolSharp.Ocean/SmolSharp.Ocean.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/SmolSharp.Win32/CompressAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace SmolSharp.Win32
{
internal static unsafe class CompressAPI
{
[SuppressGCTransition, DllImport("Cabinet")]
[SuppressGCTransition, DllImport("Cabinet", EntryPoint = "_CreateDecompressor@12")]
public static extern bool CreateDecompressor(
CompressAlgorithm algorithm,
nint allocationRoutines,
nint* decompressorHandle
);

[SuppressGCTransition, DllImport("Cabinet")]
[SuppressGCTransition, DllImport("Cabinet", EntryPoint = "_Decompress@24")]
public static extern bool Decompress(
nint decompressorHandle,
void* compressedData,
Expand Down
8 changes: 4 additions & 4 deletions src/SmolSharp.Win32/GDI/Gdi32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ namespace SmolSharp.Win32.GDI
{
internal static unsafe class Gdi32
{
[SuppressGCTransition, DllImport("gdi32")]
[SuppressGCTransition, DllImport("gdi32", EntryPoint = "_SetPixel@16")]
public static extern uint SetPixel(
nint hdc,
int x, int y,
uint color
);

[SuppressGCTransition, DllImport("gdi32")]
[SuppressGCTransition, DllImport("gdi32", EntryPoint = "_ChoosePixelFormat@8")]
public static extern int ChoosePixelFormat(
nint hdc,
PixelFormatDescriptor* ppfd
);

[SuppressGCTransition, DllImport("gdi32")]
[SuppressGCTransition, DllImport("gdi32", EntryPoint = "_SetPixelFormat@12")]
public static extern bool SetPixelFormat(
nint hdc,
int format,
PixelFormatDescriptor* ppfd
);

[SuppressGCTransition, DllImport("gdi32")]
[SuppressGCTransition, DllImport("gdi32", EntryPoint = "_SwapBuffers@4")]
public static extern bool SwapBuffers(
nint hdc
);
Expand Down
10 changes: 5 additions & 5 deletions src/SmolSharp.Win32/GDI/OpenGL/GL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ internal static class GL
public const uint TRIANGLES = 0x4;

[SuppressGCTransition]
[DllImport("opengl32", EntryPoint = "wglCreateContext")]
[DllImport("opengl32", EntryPoint = "_wglCreateContext@4")]
public static extern nint CreateContext(nint hdc);

[SuppressGCTransition]
[DllImport("opengl32", EntryPoint = "wglMakeCurrent")]
[DllImport("opengl32", EntryPoint = "_wglMakeCurrent@8")]
public static extern bool MakeCurrent(nint hdc, nint ctx);

[SuppressGCTransition]
Expand All @@ -31,19 +31,19 @@ internal static class GL
public static extern void Clear(uint mask);

[SuppressGCTransition]
[DllImport("opengl32", EntryPoint = "wglGetProcAddress")]
[DllImport("opengl32", EntryPoint = "_wglGetProcAddress@4")]
public static unsafe extern void* GetProcAddress(byte* name);

[SuppressGCTransition]
[DllImport("opengl32", EntryPoint = "glDrawArrays")]
[DllImport("opengl32", EntryPoint = "_glDrawArrays@12")]
public static extern void DrawArrays(
uint mode,
int first,
int count
);

[SuppressGCTransition]
[DllImport("opengl32", EntryPoint = "glViewport")]
[DllImport("opengl32", EntryPoint = "_glViewport@16")]
public static extern void Viewport(int x, int y, uint width, uint height);

[SuppressGCTransition]
Expand Down
8 changes: 4 additions & 4 deletions src/SmolSharp.Win32/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SmolSharp.Win32
{
internal static unsafe class Kernel32
{
[SuppressGCTransition, DllImport("kernel32")]
[SuppressGCTransition, DllImport("kernel32", EntryPoint = "_CreateThread@24")]
public static extern nint CreateThread(
nint threadAttributes,
nint stackSize,
Expand All @@ -15,13 +15,13 @@ public static extern nint CreateThread(
nint lpThreadId = 0
);

[SuppressGCTransition, DllImport("kernel32")]
[SuppressGCTransition, DllImport("kernel32", EntryPoint = "_GlobalAlloc@8")]
public static extern void* GlobalAlloc(uint dwFlags, nint dwBytes);

[SuppressGCTransition, DllImport("kernel32")]
[SuppressGCTransition, DllImport("kernel32", EntryPoint = "_AllocConsole@0")]
public static extern bool AllocConsole();

[SuppressGCTransition, DllImport("kernel32")]
[SuppressGCTransition, DllImport("kernel32", EntryPoint = "_Sleep@4")]
public static extern void Sleep(uint ms);
}
}
14 changes: 7 additions & 7 deletions src/SmolSharp.Win32/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace SmolSharp.Win32
internal static unsafe class User32
{
[SuppressGCTransition]
[DllImport("user32", EntryPoint = "CreateWindowExA")]
[DllImport("user32", EntryPoint = "_CreateWindowExA@48")]
public static extern nint CreateWindowEx(
uint dwExStyles,
void* lpClassName,
Expand All @@ -24,15 +24,15 @@ public static extern nint CreateWindowEx(
);

[SuppressGCTransition]
[DllImport("user32", EntryPoint = "RegisterClassA")]
[DllImport("user32", EntryPoint = "_RegisterClassA@4")]
public static extern nint RegisterClass(WindowClassA* lpWndClass);

[SuppressGCTransition]
[DllImport("user32", EntryPoint = "ShowWindow")]
[DllImport("user32", EntryPoint = "_ShowWindow@8")]
public static extern nint ShowWindow(nint hWnd, int nCmdShow);

[SuppressGCTransition]
[DllImport("user32", EntryPoint = "GetMessageA")]
[DllImport("user32", EntryPoint = "_GetMessageA@16")]
public static extern int GetMessage(
WndMessage* lpMsg,
nint hwnd,
Expand All @@ -45,11 +45,11 @@ uint wMsgFilterMax
public static extern bool TranslateMessage(WndMessage* lpMsg);

[SuppressGCTransition]
[DllImport("user32", EntryPoint = "DispatchMessageA")]
[DllImport("user32", EntryPoint = "_DispatchMessageA@4")]
public static extern bool DispatchMessage(WndMessage* lpMsg);

[SuppressGCTransition]
[DllImport("user32", EntryPoint = "DefWindowProcA")]
[DllImport("user32", EntryPoint = "_DefWindowProcA@16")]
public static extern nint DefWindowProc(
nint hwnd,
uint msg,
Expand All @@ -58,7 +58,7 @@ nint lParam
);

[SuppressGCTransition]
[DllImport("user32")]
[DllImport("user32", EntryPoint = "_GetDC@4")]
public static extern nint GetDC(nint hwnd);

//[DllImport("user32")]
Expand Down
2 changes: 1 addition & 1 deletion src/SmolSharp.Win32/WinMM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SmolSharp.Win32
internal static class WinMM
{
[SuppressGCTransition]
[DllImport("winmm.dll", EntryPoint = "timeGetTime")]
[DllImport("winmm.dll", EntryPoint = "_timeGetTime@0")]
public static extern uint GetTime();
}
}
11 changes: 11 additions & 0 deletions src/SmolSharp.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<LinkerArg Include="user32.lib"></LinkerArg>
<LinkerArg Include="shell32.lib"></LinkerArg>
<LinkerArg Include="gdi32.lib"></LinkerArg>
<LinkerArg Include="&quot;$(IntermediateOutputPath)x86-stubs.obj&quot;"></LinkerArg>
</ItemGroup>

<PropertyGroup>
Expand Down Expand Up @@ -107,6 +108,16 @@
</PropertyGroup>
</Target>

<Target Name="RewriteToX86" BeforeTargets="SetupOSSpecificProps">
<PropertyGroup>
<_targetArchitecture>x86</_targetArchitecture>
</PropertyGroup>
</Target>

<Target Name="BuildX86Stubs" AfterTargets="SetupOSSpecificProps" BeforeTargets="LinkNative">
<Exec Command="&quot;$(_CppToolsDirectory)\ml.exe&quot; /c /Fo&quot;$(IntermediateOutputPath)x86-stubs.obj&quot; &quot;$(MSBuildThisFileDirectory)x86-stubs.asm&quot;" />
</Target>

<Target Name="RemoveSDKLibs" AfterTargets="SetupOSSpecificProps">
<ItemGroup>
<LinkerArg Remove="@(NativeLibrary->'&quot;%(Identity)&quot;')"/>
Expand Down
49 changes: 49 additions & 0 deletions src/x86-stubs.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.586
.xmm
.model flat

.const
align 8

_Int32ToUInt32 DQ 0000000000000000H
DQ 41F0000000000000H ; 2**32
_DP2to32 EQU (_Int32ToUInt32+8)

.code

PUBLIC RhpLng2Dbl
RhpLng2Dbl PROC
mov edx,dword ptr [esp+8]
mov ecx,dword ptr [esp+4]
xorps xmm1,xmm1
cvtsi2sd xmm1,edx ; convert (signed) upper bits
xorps xmm0,xmm0
cvtsi2sd xmm0,ecx ; convert (signed) lower bits
shr ecx,31 ; get sign bit into ecx
mulsd xmm1,_DP2to32 ; adjust upper value for bit position
addsd xmm0,_Int32ToUInt32[ecx*8] ; adjust to unsigned
addsd xmm0,xmm1 ; combine upper and lower
movsd mmword ptr [esp+4],xmm0
fld qword ptr [esp+4]
ret
RhpLng2Dbl ENDP

PUBLIC RhpByRefAssignRef
RhpByRefAssignRef PROC
movs dword ptr es:[edi],dword ptr [esi]
ret
RhpByRefAssignRef ENDP

PUBLIC RhpCheckedAssignRefEAX
RhpCheckedAssignRefEAX PROC
;
; This is not tested. Once you validate this does what it should, remove this int 3
int 3
;

mov DWORD PTR [edx], eax
ret
RhpCheckedAssignRefEAX ENDP


end

0 comments on commit 974bf58

Please sign in to comment.