diff gen_x86.c @ 792:724bbec47f86

Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
author Michael Pavone <pavone@retrodev.com>
date Sat, 25 Jul 2015 18:22:07 -0700
parents 7306b3967c51
children a7774fc2de4b
line wrap: on
line diff
--- a/gen_x86.c	Sat Jul 25 18:19:00 2015 -0700
+++ b/gen_x86.c	Sat Jul 25 18:22:07 2015 -0700
@@ -5,6 +5,7 @@
 */
 #include "gen_x86.h"
 #include "mem.h"
+#include "util.h"
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -187,8 +188,7 @@
 			disp >>= 8;
 			*(out++) = disp;
 		} else {
-			fprintf(stderr, "jmp: %p - %p = %lX\n", dest, out + 6, (long)disp);
-			exit(1);
+			fatal_error("jmp: %p - %p = %l which is out of range of a 32-bit displacementX\n", dest, out + 6, (long)disp);
 		}
 	}
 	code->cur = out;
@@ -200,8 +200,7 @@
 		size_t size = CODE_ALLOC_SIZE;
 		code_ptr next_code = alloc_code(&size);
 		if (!next_code) {
-			fputs("Failed to allocate memory for generated code\n", stderr);
-			exit(1);
+			fatal_error("Failed to allocate memory for generated code\n");
 		}
 		if (next_code != code->last + RESERVE_WORDS) {
 			//new chunk is not contiguous with the current one
@@ -231,8 +230,7 @@
 #ifdef X86_64
 		*out = PRE_REX;
 		if (src >= AH && src <= BH || dst >= AH && dst <= BH) {
-			fprintf(stderr, "attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
-			exit(1);
+			fatal_error("attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
 		}
 		if (size == SZ_Q) {
 			*out |= REX_QUAD;
@@ -247,8 +245,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X, src: %s, dst: %s, size: %s\n", opcode, x86_reg_names[src], x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X, src: %s, dst: %s, size: %s\n", opcode, x86_reg_names[src], x86_reg_names[dst], x86_sizes[size]);
 #endif
 	}
 	if (size == SZ_B) {
@@ -284,8 +281,7 @@
 #ifdef X86_64
 		*out = PRE_REX;
 		if (reg >= AH && reg <= BH) {
-			fprintf(stderr, "attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
-			exit(1);
+			fatal_error("attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
 		}
 		if (size == SZ_Q) {
 			*out |= REX_QUAD;
@@ -300,8 +296,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
 #endif
 	}
 	if (size == SZ_B) {
@@ -349,8 +344,7 @@
 #ifdef X86_64
 		*out = PRE_REX;
 		if (reg >= AH && reg <= BH) {
-			fprintf(stderr, "attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
-			exit(1);
+			fatal_error("attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
 		}
 		if (size == SZ_Q) {
 			*out |= REX_QUAD;
@@ -365,8 +359,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
 #endif
 	}
 	if (size == SZ_B) {
@@ -405,8 +398,7 @@
 #ifdef X86_64
 		*out = PRE_REX;
 		if (reg >= AH && reg <= BH) {
-			fprintf(stderr, "attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
-			exit(1);
+			fatal_error("attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
 		}
 		if (size == SZ_Q) {
 			*out |= REX_QUAD;
@@ -425,8 +417,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, base: %s, size: %s\n", opcode, x86_reg_names[reg], x86_reg_names[base], x86_sizes[size]);
 #endif
 	}
 	if (size == SZ_B) {
@@ -461,8 +452,7 @@
 #ifdef X86_64
 		*out = PRE_REX;
 		if (dst >= AH && dst <= BH) {
-			fprintf(stderr, "attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
-			exit(1);
+			fatal_error("attempt to use *H reg in an instruction requiring REX prefix. opcode = %X\n", opcode);
 		}
 		if (size == SZ_Q) {
 			*out |= REX_QUAD;
@@ -473,8 +463,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, opex, x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, opex, x86_reg_names[dst], x86_sizes[size]);
 #endif
 	}
 	if (size == SZ_B) {
@@ -509,8 +498,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, opex, x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, opex, x86_reg_names[dst], x86_sizes[size]);
 #endif
 	}
 	if (size != SZ_B) {
@@ -549,8 +537,7 @@
 #ifdef X86_64
 				*out = PRE_REX | REX_QUAD;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, size: %s\n", al_opcode, x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X, reg: %s, size: %s\n", al_opcode, x86_reg_names[dst], x86_sizes[size]);
 #endif
 			}
 		}
@@ -568,8 +555,7 @@
 			}
 			out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, op_ex, x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, op_ex, x86_reg_names[dst], x86_sizes[size]);
 #endif
 		}
 		if (dst >= AH && dst <= BH) {
@@ -620,8 +606,7 @@
 		}
 		out++;
 #else
-		fprintf(stderr, "Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, op_ex, x86_reg_names[dst], x86_sizes[size]);
-		exit(1);
+		fatal_error("Instruction requires REX prefix but this is a 32-bit build | opcode: %X:%X, reg: %s, size: %s\n", opcode, op_ex, x86_reg_names[dst], x86_sizes[size]);
 #endif
 	}
 	if (size != SZ_B) {
@@ -1921,8 +1906,7 @@
 			disp >>= 8;
 			*(out++) = disp;
 		} else {
-			fprintf(stderr, "jcc: %p - %p = %lX\n", dest, out + 6, (long)disp);
-			exit(1);
+			fatal_error("jcc: %p - %p = %lX which is out of range for a 32-bit displacement\n", dest, out + 6, (long)disp);
 		}
 	}
 	code->cur = out;
@@ -1948,8 +1932,7 @@
 			disp >>= 8;
 			*(out++) = disp;
 		} else {
-			fprintf(stderr, "jmp: %p - %p = %lX\n", dest, out + 6, (long)disp);
-			exit(1);
+			fatal_error("jmp: %p - %p = %lX which is out of range for a 32-bit displacement\n", dest, out + 6, (long)disp);
 		}
 	}
 	code->cur = out;
@@ -1997,8 +1980,7 @@
 		*(out++) = disp;
 	} else {
 		//TODO: Implement far call???
-		fprintf(stderr, "%p - %p = %lX\n", fun, out + 5, (long)disp);
-		exit(1);
+		fatal_error("call: %p - %p = %lX which is out of range for a 32-bit displacement\n", fun, out + 5, (long)disp);
 	}
 	code->cur = out;
 }