Added missing file.
[tinycc/k1w1.git] / tccoswin32.c
blob86a70d3289095edbf8dd5244c4d301143a397ddd
1 /*
2 * Windows specific functions for TCC
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "tcc.h"
22 /* return the PC at frame level 'level'. Return non zero if not found */
23 static int rt_get_caller_pc(unsigned long *paddr,
24 CONTEXT *uc, int level)
26 unsigned long fp;
27 int i;
29 if (level == 0) {
30 *paddr = uc->Eip;
31 return 0;
32 } else {
33 fp = uc->Ebp;
34 for(i=1;i<level;i++) {
35 /* XXX: check address validity with program info */
36 if (fp <= 0x1000 || fp >= 0xc0000000)
37 return -1;
38 fp = ((unsigned long *)fp)[0];
40 *paddr = ((unsigned long *)fp)[1];
41 return 0;
45 /* emit a run time error at position 'pc' */
46 static void rt_error(CONTEXT *uc, const char *fmt, ...)
48 va_list ap;
49 unsigned long pc;
50 int i;
52 va_start(ap, fmt);
53 fprintf(stderr, "Runtime error: ");
54 vfprintf(stderr, fmt, ap);
55 fprintf(stderr, "\n");
56 for(i=0;i<num_callers;i++) {
57 if (rt_get_caller_pc(&pc, uc, i) < 0)
58 break;
59 if (i == 0)
60 fprintf(stderr, "at ");
61 else
62 fprintf(stderr, "by ");
63 pc = rt_printline(pc);
64 if (pc == rt_prog_main && pc)
65 break;
67 va_end(ap);
70 static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info)
72 printf("CPU exception: code=0x%8.8x addr=0x%8.8x",
73 ex_info->ExceptionRecord->ExceptionCode,
74 ex_info->ExceptionRecord->ExceptionAddress);
76 rt_error(ex_info->ContextRecord, "error");
78 /* TODO: Call longjmp here to return control to user code. */
80 return EXCEPTION_CONTINUE_SEARCH;
83 /* Generate a stack backtrace when a CPU exception occurs. */
84 void handle_cpu_exception(void)
86 SetUnhandledExceptionFilter(cpu_exception_handler);