commit 3982443b346c6cf5fba7b34e24a805a05304cb07
parent de77deba6d07d981f1f7b8d45b2fad47eeeb1152
Author: Travis Geiselbrecht <geist@foobox.com>
Date:   Fri,  6 Apr 2012 00:12:38 -0700
assembler: add -O <outputmode> option
available options are
pretty: same as before
hex: one word per line, hex format
binary: raw binary
Diffstat:
| M | assembler.c |  |  | 46 | +++++++++++++++++++++++++++++++++++++--------- | 
1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/assembler.c b/assembler.c
@@ -56,6 +56,12 @@ static int token;
 static char tstring[128];
 static u16 tnumber;
 
+enum outformat {
+	OUTFORMAT_PRETTY,
+	OUTFORMAT_HEX,
+	OUTFORMAT_BINARY,
+};
+
 void die(const char *fmt, ...) {
 	va_list ap;
 	fprintf(stderr,"%s:%d: ", filename, linenumber);
@@ -331,7 +337,7 @@ done:
 	fclose(fin);
 }
 
-void emit(const char *fn) {
+void emit(const char *fn, enum outformat format) {
 	FILE *fp;
 	u16 *pc = image;
 	u16 *end = image + PC;
@@ -347,12 +353,19 @@ void emit(const char *fn) {
 	if (!fp) die("cannot write file");
 
 	while (pc < end) {
-		if (pc == dis) {
-			char out[128];
-			dis = disassemble(pc, out);
-			fprintf(fp, "%04x\t%04x:\t%s\n", *pc, (unsigned)(pc-image), out);
-		} else {
+		if (format == OUTFORMAT_PRETTY) {
+			if (pc == dis) {
+				char out[128];
+				dis = disassemble(pc, out);
+				fprintf(fp, "%04x\t%04x:\t%s\n", *pc, (unsigned)(pc-image), out);
+			} else {
+				fprintf(fp, "%04x\n", *pc);
+			}
+		} else if (format == OUTFORMAT_HEX) {
 			fprintf(fp, "%04x\n", *pc);
+		} else if (format == OUTFORMAT_BINARY) {
+			/* XXX handle host endian */
+			fwrite(pc, sizeof(*pc), 1, fp);
 		}
 		pc++;
 	}
@@ -362,11 +375,13 @@ void emit(const char *fn) {
 
 static void usage(int argc, char **argv)
 {
-	fprintf(stderr, "usage: %s [-o output] <input file(s)>\n", argv[0]);
+	fprintf(stderr, "usage: %s [-o output] [-O output_format] <input file(s)>\n", argv[0]);
+	fprintf(stderr, "\toutput_format can be one of: pretty, hex, binary\n");
 }
 
 int main(int argc, char **argv) {
 	const char *outfn = "out.hex";
+	enum outformat oformat = OUTFORMAT_PRETTY;
 
 	for (;;) {
 		int c;
@@ -375,10 +390,11 @@ int main(int argc, char **argv) {
 		static struct option long_options[] = {
 			{"help", 0, 0, 'h'},
 			{"output", 1, 0, 'o'},
+			{"outformat", 1, 0, 'O'},
 			{0, 0, 0, 0},
 		};
 
-		c = getopt_long(argc, argv, "ho:", long_options, &option_index);
+		c = getopt_long(argc, argv, "ho:O:", long_options, &option_index);
 		if (c == -1)
 			break;
 
@@ -389,6 +405,18 @@ int main(int argc, char **argv) {
 			case 'o':
 				outfn = optarg;
 				break;
+			case 'O':
+				if (!strcasecmp(optarg, "binary")) {
+					oformat = OUTFORMAT_BINARY;
+				} else if (!strcasecmp(optarg, "hex")) {
+					oformat = OUTFORMAT_HEX;
+				} else if (!strcasecmp(optarg, "pretty")) {
+					oformat = OUTFORMAT_PRETTY;
+				} else {
+					usage(argc, argv);
+					return 1;
+				}
+				break;
 			default:
 				usage(argc, argv);
 				return 1;
@@ -412,7 +440,7 @@ int main(int argc, char **argv) {
 	if (PC != 0) {
 		linebuffer[0] = 0;
 		resolve_fixups();
-		emit(outfn);
+		emit(outfn, oformat);
 	}
 	return 0;
 }