Skip to content

Commit 13d9528

Browse files
committed
Added support for global state when using 'WinMain' instead of traditional 'main' entry point
1 parent c690af5 commit 13d9528

File tree

6 files changed

+12
-6
lines changed

6 files changed

+12
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ UNIX_LLVM_BUILD_INCLUDE=
2525
UNIX_LIBCURL_LIB=/opt/homebrew/opt/curl/lib
2626
UNIX_LIBCURL_INCLUDE=/opt/homebrew/opt/curl/lib
2727
UNIX_LIBCURL_BUILD_INCLUDE=
28-
DEBUG_ADDRESS_SANITIZE=true
28+
DEBUG_ADDRESS_SANITIZE=false
2929
#UNIX_CC=gcc
3030
#UNIX_CXX=g++
3131
#UNIX_LLVM_LIB=/usr/lib/llvm-7/lib

include/AST/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef struct {
7272
#define AST_FUNC_AUTOGEN TRAIT_9
7373
#define AST_FUNC_VARIADIC TRAIT_A
7474
#define AST_FUNC_IMPLICIT TRAIT_B
75+
#define AST_FUNC_WINMAIN TRAIT_C
7576

7677
// Additional AST function traits for builtin uses
7778
#define AST_FUNC_WARN_BAD_PRINTF_FORMAT TRAIT_2_1

src/AST/ast.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,9 @@ void ast_func_create_template(ast_func_t *func, strong_cstr_t name, bool is_stdc
873873
if(is_stdcall) func->traits |= AST_FUNC_STDCALL;
874874
if(is_foreign) func->traits |= AST_FUNC_FOREIGN;
875875
if(is_implicit) func->traits |= AST_FUNC_IMPLICIT;
876+
877+
// Handle WinMain
878+
if(strcmp(name, "WinMain") == 0 && export_as && strcmp(export_as, "WinMain") == 0) func->traits |= AST_FUNC_WINMAIN;
876879
}
877880

878881
bool ast_func_is_polymorphic(ast_func_t *func){

src/BKEND/ir_to_llvm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ errorcode_t ir_to_llvm(compiler_t *compiler, object_t *object){
16121612
const char *linker = "ld.exe"; // May need to change depending on system etc.
16131613
const char *linker_options = "--start-group";
16141614
const char *root = compiler->root;
1615-
const char *subsystem = (compiler->traits & COMPILER_WINDOWED) ? "-subsystem,windows " : "";
1615+
const char *subsystem = (compiler->traits & COMPILER_WINDOWED) ? "--subsystem windows " : "";
16161616
link_command = mallocandsprintf("\"\"%s%s\" -static \"%scrt2.o\" \"%scrtbegin.o\" %s%s \"%s\" \"%slibdep.a\" C:/Windows/System32/msvcrt.dll %s-o \"%s\"\"", root, linker, root, root, linker_options, linker_additional, object_filename, root, subsystem, compiler->output_filename);
16171617
}
16181618
#else
@@ -1622,7 +1622,7 @@ errorcode_t ir_to_llvm(compiler_t *compiler, object_t *object){
16221622
const char *linker = "cross-compile-windows/x86_64-w64-mingw32-ld";
16231623
const char *linker_options = "";
16241624
const char *root = compiler->root;
1625-
const char *subsystem = (compiler->traits & COMPILER_WINDOWED) ? "-subsystem,windows " : "";
1625+
const char *subsystem = (compiler->traits & COMPILER_WINDOWED) ? "--subsystem windows " : "";
16261626

16271627
strong_cstr_t cross_linker = mallocandsprintf("%s%s", compiler->root, linker);
16281628
if(!file_exists(cross_linker)){

src/IRGEN/ir_gen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ errorcode_t ir_gen_functions_body_statements(compiler_t *compiler, object_t *obj
340340
ast_expr_t **statements = ast_func->statements;
341341
length_t statements_length = ast_func->statements_length;
342342

343-
if(ast_func->traits & AST_FUNC_MAIN){
343+
if(ast_func->traits & AST_FUNC_MAIN || ast_func->traits & AST_FUNC_WINMAIN){
344344
// Initialize all global variables
345345
if(ir_gen_globals_init(&builder)){
346346
module_func->basicblocks = builder.basicblocks;
@@ -375,7 +375,7 @@ errorcode_t ir_gen_functions_body_statements(compiler_t *compiler, object_t *obj
375375
// Make sure to update references that may have been invalidated
376376
ast_func = &object->ast.funcs[ast_func_id];
377377

378-
if(ast_func->traits & AST_FUNC_MAIN){
378+
if(ast_func->traits & AST_FUNC_MAIN || ast_func->traits & AST_FUNC_WINMAIN){
379379
handle_deference_for_globals(&builder);
380380
build_deinit_svars(&builder);
381381
}

src/IRGEN/ir_gen_stmt.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ errorcode_t ir_gen_stmt_return(ir_builder_t *builder, ast_expr_return_t *stmt, b
530530
bool is_in_main_function;
531531
bool is_in_pass_function;
532532
bool is_in_defer_function;
533+
bool is_in_winmain_function;
533534
bool autogen_enabled;
534535

535536
// 'ast_func' is only guaranteed to be valid within this scope.
@@ -542,6 +543,7 @@ errorcode_t ir_gen_stmt_return(ir_builder_t *builder, ast_expr_return_t *stmt, b
542543
is_in_main_function = ast_func->traits & AST_FUNC_MAIN;
543544
is_in_pass_function = ast_func->traits & AST_FUNC_PASS;
544545
is_in_defer_function = ast_func->traits & AST_FUNC_DEFER;
546+
is_in_winmain_function = ast_func->traits & AST_FUNC_WINMAIN;
545547
autogen_enabled = ast_func->traits & AST_FUNC_AUTOGEN;
546548
}
547549

@@ -602,7 +604,7 @@ errorcode_t ir_gen_stmt_return(ir_builder_t *builder, ast_expr_return_t *stmt, b
602604
if(ir_gen_variable_deference(builder, NULL)) return FAILURE;
603605

604606
// Make '__defer__()' calls for global variables and (anonymous) static variables running out of scope
605-
if(is_in_main_function){
607+
if(is_in_main_function || is_in_winmain_function){
606608
handle_deference_for_globals(builder);
607609
build_deinit_svars(builder);
608610
}

0 commit comments

Comments
 (0)