Unified · Split
| @@ -434,6 +434,7 @@ int parse_btype(int *type_ptr, AttributeDef *ad); · 433 unmodified lines | ||
| 434 | 434 | int type_decl(int *v, int t, int td); |
| 435 | 435 | |
| 436 | 436 | void error(const char *fmt, ...); |
| 437 | +void rt_error(unsigned long pc, const char *fmt, ...); | |
| 437 | 438 | void vpushi(int v); |
| 438 | 439 | void vset(int t, int r, int v); |
| 439 | 440 | void type_to_str(char *buf, int buf_size, |
| @@ -5969,6 +5970,18 @@ static void rt_printline(unsigned long wanted_pc) · 5529 unmodified lines | ||
| 5969 | 5970 | } |
| 5970 | 5971 | } |
| 5971 | 5972 | |
| 5973 | +/* emit a run time error at position 'pc' */ | |
| 5974 | +void rt_error(unsigned long pc, const char *fmt, ...) | |
| 5975 | +{ | |
| 5976 | + va_list ap; | |
| 5977 | + va_start(ap, fmt); | |
| 5978 | + | |
| 5979 | + rt_printline(pc); | |
| 5980 | + vfprintf(stderr, fmt, ap); | |
| 5981 | + fprintf(stderr, "\n"); | |
| 5982 | + exit(255); | |
| 5983 | + va_end(ap); | |
| 5984 | +} | |
| 5972 | 5985 | |
| 5973 | 5986 | /* signal handler for fatal errors */ |
| 5974 | 5987 | static void sig_error(int signum, siginfo_t *siginf, void *puc) |
| @@ -5982,32 +5995,30 @@ static void sig_error(int signum, siginfo_t *siginf, void *puc) · 7 unmodified lines | ||
| 5982 | 5995 | #error please put the right sigcontext field |
| 5983 | 5996 | #endif |
| 5984 | 5997 | |
| 5985 | − rt_printline(pc); | |
| 5986 | − | |
| 5987 | 5998 | switch(signum) { |
| 5988 | 5999 | case SIGFPE: |
| 5989 | 6000 | switch(siginf->si_code) { |
| 5990 | 6001 | case FPE_INTDIV: |
| 5991 | 6002 | case FPE_FLTDIV: |
| 5992 | − fprintf(stderr, "division by zero\n"); | |
| 6003 | + rt_error(pc, "division by zero"); | |
| 5993 | 6004 | break; |
| 5994 | 6005 | default: |
| 5995 | − fprintf(stderr, "floating point exception\n"); | |
| 6006 | + rt_error(pc, "floating point exception"); | |
| 5996 | 6007 | break; |
| 5997 | 6008 | } |
| 5998 | 6009 | break; |
| 5999 | 6010 | case SIGBUS: |
| 6000 | 6011 | case SIGSEGV: |
| 6001 | − fprintf(stderr, "dereferencing invalid pointer\n"); | |
| 6012 | + rt_error(pc, "dereferencing invalid pointer"); | |
| 6002 | 6013 | break; |
| 6003 | 6014 | case SIGILL: |
| 6004 | − fprintf(stderr, "illegal instruction\n"); | |
| 6015 | + rt_error(pc, "illegal instruction"); | |
| 6005 | 6016 | break; |
| 6006 | 6017 | case SIGABRT: |
| 6007 | − fprintf(stderr, "abort() called\n"); | |
| 6018 | + rt_error(pc, "abort() called"); | |
| 6008 | 6019 | break; |
| 6009 | 6020 | default: |
| 6010 | − fprintf(stderr, "caught signal %d\n", signum); | |
| 6021 | + rt_error(pc, "caught signal %d", signum); | |
| 6011 | 6022 | break; |
| 6012 | 6023 | } |
| 6013 | 6024 | exit(255); |