Skip to contentSkip to conversation

intel-gen4asm: have a C-like binary output

PATCH In review Ben Widawsky 2 participants 3 messages Mar 18, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 intel-gen4asm: have a C-like binary output

From: Ben Widawsky <[email protected]>

Have the assembler support creating a byte array for binary blob-like inclusion. In my case, to write some exception handler which is not jit'd.

I don't have push access, so if anyone thinks this is useful, please push

---
 src/main.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 47 insertions(+), 6 deletions(-)
src/main.c +47 -6
index def2655..785cce8 100644
@@ -40,10 +40,13 @@ extern int errors;
 
 long int gen_level = 4;
 int advanced_flag = 0; /* 0: in unit of type, 1: in unit of data element size */
+int binary_like_output = 0; /* 0: default type, 1: nice c like output */
 int need_export = 0;
 char *input_filename = "<stdin>";
 char *export_filename = NULL;
 
+const char const *binary_prepend = "static const char gen4_bytes[] = {\n";
+
 struct brw_program compiled_program;
 struct program_defaults program_defaults;
 
@@ -53,6 +56,7 @@ static struct declared_register *declared_register_table[HASHSZ];
 
 static const struct option longopts[] = {
 	{"advanced", no_argument, 0, 'a'},
+	{"binary", no_argument, 0, 'b'},
 	{"export", required_argument, 0, 'e'},
 	{"input_list", required_argument, 0, 'l'},
 	{"output", required_argument, 0, 'o'},
@@ -65,6 +69,7 @@ static void usage(void)
 	fprintf(stderr, "usage: intel-gen4asm [options] inputfile\n");
 	fprintf(stderr, "OPTIONS:\n");
 	fprintf(stderr, "\t-a, --advanced                       Set advanced flag\n");
+	fprintf(stderr, "\t-b, --binary                         C style binary output\n");
 	fprintf(stderr, "\t-e, --export {exportfile}            Export label file\n");
 	fprintf(stderr, "\t-l, --input_list {entrytablefile}    Input entry_table_list file\n");
 	fprintf(stderr, "\t-o, --output {outputfile}            Specify output file\n");
@@ -168,6 +173,38 @@ static int is_entry_point(char *s)
 	return 0;
 }
 
+static void
+print_instruction(FILE *output, struct brw_program_instruction *entry)
+{
+	if (binary_like_output) {
+		fprintf(output, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x,"
+				"0x%02x, 0x%02x, 0x%02x, 0x%02x,"
+				"0x%02x, 0x%02x, 0x%02x, 0x%02x,"
+				"0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
+			((unsigned char *)(&entry->instruction))[0],
+			((unsigned char *)(&entry->instruction))[1],
+			((unsigned char *)(&entry->instruction))[2],
+			((unsigned char *)(&entry->instruction))[3],
+			((unsigned char *)(&entry->instruction))[4],
+			((unsigned char *)(&entry->instruction))[5],
+			((unsigned char *)(&entry->instruction))[6],
+			((unsigned char *)(&entry->instruction))[7],
+			((unsigned char *)(&entry->instruction))[8],
+			((unsigned char *)(&entry->instruction))[9],
+			((unsigned char *)(&entry->instruction))[10],
+			((unsigned char *)(&entry->instruction))[11],
+			((unsigned char *)(&entry->instruction))[12],
+			((unsigned char *)(&entry->instruction))[13],
+			((unsigned char *)(&entry->instruction))[14],
+			((unsigned char *)(&entry->instruction))[15]);
+	} else {
+		fprintf(output, "   { 0x%08x, 0x%08x, 0x%08x, 0x%08x },\n",
+			((int *)(&entry->instruction))[0],
+			((int *)(&entry->instruction))[1],
+			((int *)(&entry->instruction))[2],
+			((int *)(&entry->instruction))[3]);
+	}
+}
 int main(int argc, char **argv)
 {
 	char *output_file = NULL;
@@ -177,7 +214,7 @@ int main(int argc, char **argv)
 	struct brw_program_instruction *entry, *entry1, *tmp_entry;
 	int err, inst_offset;
 	char o;
-	while ((o = getopt_long(argc, argv, "e:l:o:g:a", longopts, NULL)) != -1) {
+	while ((o = getopt_long(argc, argv, "e:l:o:g:ab", longopts, NULL)) != -1) {
 		switch (o) {
 		case 'o':
 			if (strcmp(optarg, "-") != 0)
@@ -198,6 +235,9 @@ int main(int argc, char **argv)
 		case 'a':
 			advanced_flag = 1;
 			break;
+		case 'b':
+			binary_like_output = 1;
+			break;
 
 		case 'e':
 			need_export = 1;
@@ -317,20 +357,21 @@ int main(int argc, char **argv)
 	}
 
 
+	if (binary_like_output)
+		fprintf(output, "%s", binary_prepend);
+
 	for (entry = compiled_program.first;
 		entry != NULL;
 		entry = entry1) {
 	    entry1 = entry->next;
 	    if (!entry->islabel)
-		fprintf(output, "   { 0x%08x, 0x%08x, 0x%08x, 0x%08x },\n",
-			((int *)(&entry->instruction))[0],
-			((int *)(&entry->instruction))[1],
-			((int *)(&entry->instruction))[2],
-			((int *)(&entry->instruction))[3]);
+		print_instruction(output, entry);
 	    else
 		free(entry->string);
 	    free(entry);
 	}
+	if (binary_like_output)
+		fprintf(output, "};");
 
 	free_register_table();
 	fflush (output);
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 5 lines · Ben Widawsky

From: Ben Widawsky <[email protected]>

Have the assembler support creating a byte array for binary blob-like inclusion. In my case, to write some exception handler which is not jit'd.

I just noticed that the spacing is a little messed up in this one. If someone wants to push this, but doesn't want to fix it, send me an email and I'll resubmit.

Here's sample output. It should be shorter lines, with proper spacing...

static const char gen4_bytes[] = {
        0x05, 0x02, 0x00, 0x00,0x00, 0x1c, 0x00, 0x30,0x00, 0x10, 0x00, 0x00,0xf9, 0xff, 0xff, 0xff,
        0x08, 0x02, 0x00, 0x00,0x1d, 0x1d, 0x04, 0x20,0x00, 0x0e, 0x00, 0x00,0x06, 0x00, 0x00, 0x00,
        0x40, 0x02, 0x00, 0x00,0xbd, 0x43, 0x04, 0x20,0x04, 0x00, 0x00, 0x00,0x00, 0x0e, 0x00, 0x00,
        0x41, 0x02, 0x00, 0x00,0xbd, 0x1f, 0x04, 0x20,0x04, 0x00, 0x00, 0x00,0x00, 0x10, 0x00, 0x00,
        0x01, 0x02, 0x60, 0x00,0xbe, 0x03, 0x00, 0x20,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,
        0x01, 0x22, 0x80, 0x00,0xbe, 0x03, 0x00, 0x20,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,
        0x40, 0x02, 0x00, 0x00,0xbe, 0x1f, 0x02, 0x20,0x04, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,
        0x31, 0x00, 0x60, 0x00,0x1e, 0x1c, 0x00, 0x20,0x00, 0x00, 0x00, 0x50,0x00, 0x00, 0x18, 0x10,
        0x01, 0x00, 0x60, 0x00,0xbe, 0x03, 0xe0, 0x20,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,
        0x40, 0x02, 0x00, 0x00,0xbe, 0x1f, 0xe2, 0x20,0x04, 0x00, 0x00, 0x00,0x00, 0x01, 0x00, 0x00,
        0x31, 0x00, 0x60, 0x00,0x1e, 0x1c, 0xe0, 0x20,0x00, 0x00, 0x00, 0x50,0x00, 0x00, 0x18, 0x12,
};

Ben

#2 lore raw
In reply to Ben Widawsky
Quoted 8 lines · Ben Widawsky

From: Ben Widawsky <[email protected]>

Have the assembler support creating a byte array for binary blob-like inclusion. In my case, to write some exception handler which is not jit'd.

I don't have push access, so if anyone thinks this is useful, please push

Seems useful to have around.

You should have access already.

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx