Skip to content

Commit 6dfc229

Browse files
Fixing a bug in call_function.asm
1 parent 99beb43 commit 6dfc229

File tree

5 files changed

+108
-37
lines changed

5 files changed

+108
-37
lines changed

llsc/src/CInstruction.cs

-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
831831
throw new Exception("Internal Compiler Error!");
832832

833833
byteCodeState.FreeRegister(returnValueRegister, stackSize);
834-
byteCodeState.instructions.Add(new LLI_Location_PseudoInstruction(functionPtr, stackSize, byteCodeState));
835834
byteCodeState.instructions.Add(new LLI_PushRegister((byte)functionPtr.position.registerIndex));
836835
pushedBytes += 8;
837836
}

llscript_asm/src/call_func.asm

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ sub rax, 8
148148
mov rbp, rsp
149149
add rbp, 32
150150

151+
152+
151153
; Remaining Params
152154
remaining_params:
153155
M_JUMP_TO_DO_CALL_IF_LAST
154156

155157
; Push all remaining params to the stack.
156-
mov rbp, qword ptr [rax]
158+
mov rdi, qword ptr [rax]
159+
mov qword ptr [rbp], rdi
157160

158161
; Move to the next param.
159162
sub rax, 8
@@ -167,8 +170,6 @@ add rbp, 8
167170
jmp remaining_params
168171

169172

170-
; TODO: Do we need to pop the values from the stack as well?
171-
172173

173174
do_call:
174175
; Let's figure out the return type!
@@ -196,6 +197,7 @@ movsd qword ptr [rsp], xmm0
196197
mov rax, qword ptr [rsp]
197198

198199

200+
199201
end_func:
200202

201203
; Move stack to where we previously stored it.

llscript_exec/project.lua

+2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ filter { }
3939
filter { "configurations:Debug*" }
4040
defines { "_DEBUG" }
4141
symbols "FastLink"
42+
links { "msvcrtd.lib", "vcruntimed.lib", "ucrtd.lib" }
4243

4344
filter { "configurations:Release" }
4445
defines { "NDEBUG" }
4546
optimize "Speed"
4647
symbols "On"
48+
ignoredefaultlibraries { "msvcrt" }

llscript_exec/src/main.c

+89-29
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,130 @@
1-
#include <stdio.h>
2-
#include <malloc.h>
3-
#include <string.h>
4-
5-
#include <conio.h>
1+
#include <Windows.h>
2+
#include <intrin.h>
63

74
#include "llshost.h"
85

9-
int32_t main(const int32_t argc, const char **pArgv)
6+
__forceinline void SetZero(void *pData, const size_t size)
7+
{
8+
uint8_t *pData8 = (uint8_t *)(pData);
9+
size_t i = 0;
10+
11+
if (size >= sizeof(__m128i))
12+
for (; i < size - (sizeof(__m128i) - 1); i += sizeof(__m128i))
13+
_mm_storeu_si128((__m128i *)(pData8 + i), _mm_setzero_si128());
14+
15+
for (; i < size; i++)
16+
pData8[i] = 0;
17+
}
18+
19+
#pragma function(memset)
20+
extern void *memset(void *pDst, int data, size_t size)
21+
{
22+
size_t i = 0;
23+
uint8_t *const pDst8 = (uint8_t *const)pDst;
24+
const __m128i data128 = _mm_set1_epi8((char)data);
25+
26+
if (size >= sizeof(__m128i))
27+
for (; i < size - (sizeof(__m128i) - 1); i += sizeof(__m128i))
28+
_mm_storeu_si128((__m128i *)(pDst8 + i), data128);
29+
30+
for (; i < size; i++)
31+
pDst8[i] = (uint8_t)data;
32+
33+
return pDst;
34+
}
35+
36+
#pragma function(memcpy)
37+
extern void *memcpy(void *pDst, const void *pSrc, size_t size)
1038
{
39+
size_t i = 0;
40+
const uint8_t *const pSrc8 = (const uint8_t *)pSrc;
41+
uint8_t *const pDst8 = (uint8_t *)pDst;
42+
43+
if (size >= sizeof(__m128i))
44+
for (; i < size - (sizeof(__m128i) - 1); i += sizeof(__m128i))
45+
_mm_storeu_si128((__m128i *)(pDst8 + i), _mm_loadu_si128((const __m128i *)(pSrc8 + i)));
46+
47+
for (; i < size; i++)
48+
pDst8[i] = pSrc8[i];
49+
50+
return pDst;
51+
}
52+
53+
#pragma comment (linker, "/ENTRY:entry_point")
54+
void entry_point()
55+
{
56+
int32_t argc = 0;
57+
const wchar_t **pArgv = CommandLineToArgvW(GetCommandLineW(), &argc);
58+
59+
HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
60+
1161
if (argc != 2)
1262
{
13-
puts("Invalid Parameter.\n\nUsage: llscript_exec <Filename>");
14-
puts("Build Time: " __TIMESTAMP__);
15-
return -1;
63+
const char out[] = "Invalid Parameter.\n\nUsage: llscript_exec <Filename>\nBuild Time: " __TIMESTAMP__ "\n";
64+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
65+
ExitProcess((UINT)-1);
1666
}
1767

1868
uint8_t *pByteCode = NULL;
1969

2070
// Read Bytecode from file.
2171
{
22-
FILE *pFile = fopen(pArgv[1], "rb");
72+
HANDLE file = CreateFileW(pArgv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2373

24-
if (pFile == NULL)
74+
if (file == NULL || file == INVALID_HANDLE_VALUE)
2575
{
26-
fputs("Failed to open file.", stderr);
27-
return -1;
76+
const char out[] = "Failed to open file.";
77+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
78+
ExitProcess((UINT)-1);
2879
}
2980

30-
fseek(pFile, 0, SEEK_END);
31-
const int64_t fileSize = _ftelli64(pFile);
32-
fseek(pFile, 0, SEEK_SET);
81+
LARGE_INTEGER fileSize = { 0 };
82+
83+
if (FALSE == GetFileSizeEx(file, &fileSize))
84+
{
85+
const char out[] = "Failed to retrieve file size.";
86+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
87+
ExitProcess((UINT)-1);
88+
}
3389

34-
if (fileSize <= 0)
90+
if (fileSize.QuadPart <= 0)
3591
{
36-
fputs("Invalid File.", stderr);
37-
return -1;
92+
const char out[] = "Invalid file size.";
93+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
94+
ExitProcess((UINT)-1);
3895
}
3996

40-
pByteCode = (uint8_t *)malloc(fileSize);
97+
pByteCode = (uint8_t *)VirtualAlloc(NULL, (size_t)fileSize.QuadPart, MEM_COMMIT, PAGE_READWRITE);
4198

4299
if (pByteCode == NULL)
43100
{
44-
fputs("Memory Allocation Failure.", stderr);
45-
return -1;
101+
const char out[] = "Memory Allocation Failure.";
102+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
103+
ExitProcess((UINT)-1);
46104
}
47105

48-
if ((size_t)fileSize != fread(pByteCode, 1, (size_t)fileSize, pFile))
106+
if (FALSE == ReadFile(file, pByteCode, fileSize.LowPart, NULL, NULL))
49107
{
50-
fputs("Failed to read file.", stderr);
51-
return -1;
108+
const char out[] = "Failed to read file.";
109+
WriteFile(stdOut, out, sizeof(out), NULL, NULL);
110+
ExitProcess((UINT)-1);
52111
}
53112

54-
fclose(pFile);
113+
CloseHandle(file);
55114
}
56115

57116
llshost_state_t state;
58-
memset(&state, 0, sizeof(state));
117+
SetZero(&state, sizeof(state));
59118

60119
state.pCode = pByteCode;
61120

62121
static uint8_t stack[LLS_DEFUALT_STACK_SIZE];
63-
memset(stack, 0, sizeof(stack));
122+
SetZero(stack, sizeof(stack));
64123
state.pStack = stack;
65124
state.stackSize = sizeof(stack);
66125

67126
llshost_from_state(&state);
68127

69-
return 0;
128+
ExitProcess((UINT)0);
129+
return;
70130
}

llscript_host/src/llshost.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,8 @@ void llshost_EvaluateCode(llshost_state_t *pState)
23762376
{
23772377
iregister[target_register] = result;
23782378
#ifdef LLS_DEBUG_MODE
2379-
printf("\t\t// Return Value: %" PRIu64 " / %" PRIi64 " (0x%" PRIX64 ")\n", result, (int64_t)result, result);
2379+
if (!silent)
2380+
printf("\t\t// Return Value: %" PRIu64 " / %" PRIi64 " (0x%" PRIX64 ")\n", result, (int64_t)result, result);
23802381

23812382
LOG_INSPECT_INTEGER(result, pState);
23822383
#endif
@@ -2385,7 +2386,8 @@ void llshost_EvaluateCode(llshost_state_t *pState)
23852386
{
23862387
fregister[target_register - LLS_IREGISTER_COUNT] = *(double *)&result;
23872388
#ifdef LLS_DEBUG_MODE
2388-
printf("\t\t// Return Value: %f (0x%" PRIX64 ")\n", *(double *)result, result);
2389+
if (!silent)
2390+
printf("\t\t// Return Value: %f (0x%" PRIX64 ")\n", *(double *)result, result);
23892391
#endif
23902392
}
23912393
ASSERT_NO_ELSE;
@@ -2493,7 +2495,10 @@ void llshost_EvaluateCode(llshost_state_t *pState)
24932495
LOG_X64(iregister[1]);
24942496
LOG_INFO_END();
24952497
LOG_END();
2496-
LOG_INSPECT_INTEGER(iregister[1], pState);
2498+
#ifdef LLS_DEBUG_MODE
2499+
if (!silent)
2500+
LOG_INSPECT_INTEGER(iregister[1], pState);
2501+
#endif
24972502

24982503
HMODULE(*LoadLibraryA)(LPCSTR lpLibFileName) = pState->pLoadLibrary;
24992504

@@ -2525,7 +2530,10 @@ void llshost_EvaluateCode(llshost_state_t *pState)
25252530
LOG_X64(iregister[2]);
25262531
LOG_INFO_END();
25272532
LOG_END();
2528-
LOG_INSPECT_INTEGER(iregister[2], pState);
2533+
#ifdef LLS_DEBUG_MODE
2534+
if (!silent)
2535+
LOG_INSPECT_INTEGER(iregister[2], pState);
2536+
#endif
25292537

25302538
FARPROC(*GetProcAddress)(HMODULE hModule, LPCSTR lpProcName) = pState->pGetProcAddress;
25312539

0 commit comments

Comments
 (0)