tinycc/win32/lib/crt1.c
2026-04-04 20:02:31 +07:00

150 lines
3.8 KiB
C

// =============================================
// crt1.c
// _UNICODE for tchar.h, UNICODE for API
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define _UNKNOWN_APP 0
#define _CONSOLE_APP 1
#define _GUI_APP 2
#define _MCW_PC 0x00030000 // Precision Control
#define _PC_24 0x00020000 // 24 bits
#define _PC_53 0x00010000 // 53 bits
#define _PC_64 0x00000000 // 64 bits
#ifdef _UNICODE
#define __tgetmainargs __wgetmainargs
#define _tstart _wstart
#define _tmain wmain
#define _runtmain _runwmain
#define get_tenviron _get_wenviron
#else
#define __tgetmainargs __getmainargs
#define _tstart _start
#define _tmain main
#define _runtmain _runmain
#define get_tenviron _get_environ
#endif
typedef struct { int newmode; } _startupinfo;
int __cdecl __tgetmainargs(int *pargc, _TCHAR ***pargv, _TCHAR ***penv, int globb, _startupinfo*);
int __cdecl get_tenviron(_TCHAR ***penv);
void __cdecl __set_app_type(int apptype);
unsigned int __cdecl _controlfp(unsigned int new_value, unsigned int mask);
extern int _tmain(int argc, _TCHAR * argv[], _TCHAR * env[]);
#ifdef UNICODE
__attribute__((weak)) wchar_t **__cdecl __rt_get_wenviron(void);
#else
__attribute__((weak)) char **__cdecl __rt_get_environ(void);
#endif
#include "crtinit.c"
/* Allow command-line globbing with "int _dowildcard = 1;" in the user source */
int _dowildcard;
static LONG WINAPI catch_sig(EXCEPTION_POINTERS *ex)
{
return _XcptFilter(ex->ExceptionRecord->ExceptionCode, ex);
}
void _tstart(void)
{
int ret;
_TCHAR **env = NULL;
_startupinfo start_info = {0};
SetUnhandledExceptionFilter(catch_sig);
// Sets the current application type
__set_app_type(_CONSOLE_APP);
// Set default FP precision to 53 bits (8-byte double)
// _MCW_PC (Precision control) is not supported on ARM
#if defined __i386__ || defined __x86_64__
_controlfp(_PC_53, _MCW_PC);
#endif
__tgetmainargs(&__argc, &__targv, &env, _dowildcard, &start_info);
run_ctors(__argc, __targv, env);
ret = _tmain(__argc, __targv, env);
run_dtors();
exit(ret);
}
// =============================================
// for 'tcc -run ,,,'
__attribute__((weak)) void __run_on_exit(int ret)
{
(void)ret;
}
static void run_stdio_init(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
int _runtmain(int argc, /* as tcc passed in */ char **argv)
{
int ret;
_TCHAR **env = NULL;
#ifdef UNICODE
if (__rt_get_wenviron) {
env = __rt_get_wenviron();
#if defined __i386__ || defined __x86_64__
_controlfp(_PC_53, _MCW_PC);
#endif
run_stdio_init();
run_ctors(argc, (_TCHAR **)argv, env);
ret = _tmain(argc, (_TCHAR **)argv, env);
run_dtors();
__run_on_exit(ret);
return ret;
}
#else
if (__rt_get_environ) {
env = __rt_get_environ();
#if defined __i386__ || defined __x86_64__
_controlfp(_PC_53, _MCW_PC);
#endif
run_stdio_init();
run_ctors(argc, (_TCHAR **)argv, env);
ret = _tmain(argc, (_TCHAR **)argv, env);
run_dtors();
__run_on_exit(ret);
return ret;
}
#endif
#ifdef UNICODE
_startupinfo start_info = {0};
__tgetmainargs(&__argc, &__targv, &env, _dowildcard, &start_info);
/* may be wrong when tcc has received wildcards (*.c) */
if (argc < __argc) {
__targv += __argc - argc;
__argc = argc;
}
#else
__argc = argc;
__targv = argv;
get_tenviron(&env);
#endif
#if defined __i386__ || defined __x86_64__
_controlfp(_PC_53, _MCW_PC);
#endif
run_stdio_init();
run_ctors(__argc, __targv, env);
ret = _tmain(__argc, __targv, env);
run_dtors();
__run_on_exit(ret);
return ret;
}
// =============================================