Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Keith Packard |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <getopt.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "gen4asm.h" |
Damien Lespiau | 4ca1a04 | 2013-01-19 00:30:18 +0000 | [diff] [blame] | 30 | #include "brw_eu.h" |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 31 | |
| 32 | static const struct option longopts[] = { |
| 33 | { NULL, 0, NULL, 0 } |
| 34 | }; |
| 35 | |
| 36 | static struct brw_program * |
| 37 | read_program (FILE *input) |
| 38 | { |
| 39 | uint32_t inst[4]; |
| 40 | struct brw_program *program; |
| 41 | struct brw_program_instruction *entry, **prev; |
| 42 | int c; |
| 43 | int n = 0; |
Ben Widawsky | 83a5c38 | 2011-03-23 22:08:39 -0700 | [diff] [blame] | 44 | |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 45 | program = malloc (sizeof (struct brw_program)); |
| 46 | program->first = NULL; |
| 47 | prev = &program->first; |
| 48 | while ((c = getc (input)) != EOF) { |
| 49 | if (c == '0') { |
| 50 | if (fscanf (input, "x%x", &inst[n]) == 1) { |
| 51 | ++n; |
| 52 | if (n == 4) { |
| 53 | entry = malloc (sizeof (struct brw_program_instruction)); |
| 54 | memcpy (&entry->instruction, inst, 4 * sizeof (uint32_t)); |
| 55 | entry->next = NULL; |
| 56 | *prev = entry; |
| 57 | prev = &entry->next; |
| 58 | n = 0; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return program; |
| 64 | } |
| 65 | |
Ben Widawsky | 83a5c38 | 2011-03-23 22:08:39 -0700 | [diff] [blame] | 66 | static struct brw_program * |
| 67 | read_program_binary (FILE *input) |
| 68 | { |
| 69 | uint32_t temp; |
| 70 | uint8_t inst[16]; |
| 71 | struct brw_program *program; |
| 72 | struct brw_program_instruction *entry, **prev; |
| 73 | int c; |
| 74 | int n = 0; |
| 75 | |
| 76 | program = malloc (sizeof (struct brw_program)); |
| 77 | program->first = NULL; |
| 78 | prev = &program->first; |
| 79 | while ((c = getc (input)) != EOF) { |
| 80 | if (c == '0') { |
| 81 | if (fscanf (input, "x%2x", &temp) == 1) { |
| 82 | inst[n++] = (uint8_t)temp; |
| 83 | if (n == 16) { |
| 84 | entry = malloc (sizeof (struct brw_program_instruction)); |
| 85 | memcpy (&entry->instruction, inst, 16 * sizeof (uint8_t)); |
| 86 | entry->next = NULL; |
| 87 | *prev = entry; |
| 88 | prev = &entry->next; |
| 89 | n = 0; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return program; |
| 95 | } |
| 96 | |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 97 | static void usage(void) |
| 98 | { |
Damien Lespiau | 4ca1a04 | 2013-01-19 00:30:18 +0000 | [diff] [blame] | 99 | fprintf(stderr, "usage: intel-gen4disasm [options] inputfile\n"); |
| 100 | fprintf(stderr, "\t-b, --binary C style binary output\n"); |
| 101 | fprintf(stderr, "\t-o, --output {outputfile} Specify output file\n"); |
| 102 | fprintf(stderr, "\t-g, --gen <4|5|6|7> Specify GPU generation\n"); |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int main(int argc, char **argv) |
| 106 | { |
| 107 | struct brw_program *program; |
| 108 | FILE *input = stdin; |
| 109 | FILE *output = stdout; |
| 110 | char *input_filename = NULL; |
| 111 | char *output_file = NULL; |
Ben Widawsky | 83a5c38 | 2011-03-23 22:08:39 -0700 | [diff] [blame] | 112 | int byte_array_input = 0; |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 113 | int o; |
Damien Lespiau | 4ca1a04 | 2013-01-19 00:30:18 +0000 | [diff] [blame] | 114 | int gen = 4; |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 115 | struct brw_program_instruction *inst; |
| 116 | |
Damien Lespiau | 4ca1a04 | 2013-01-19 00:30:18 +0000 | [diff] [blame] | 117 | while ((o = getopt_long(argc, argv, "o:bg:", longopts, NULL)) != -1) { |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 118 | switch (o) { |
| 119 | case 'o': |
| 120 | if (strcmp(optarg, "-") != 0) |
| 121 | output_file = optarg; |
| 122 | break; |
Ben Widawsky | 83a5c38 | 2011-03-23 22:08:39 -0700 | [diff] [blame] | 123 | case 'b': |
| 124 | byte_array_input = 1; |
| 125 | break; |
Damien Lespiau | 4ca1a04 | 2013-01-19 00:30:18 +0000 | [diff] [blame] | 126 | case 'g': |
| 127 | gen = strtol(optarg, NULL, 10); |
| 128 | |
| 129 | if (gen < 4 || gen > 7) { |
| 130 | usage(); |
| 131 | exit(1); |
| 132 | } |
| 133 | |
| 134 | break; |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 135 | default: |
| 136 | usage(); |
| 137 | exit(1); |
| 138 | } |
| 139 | } |
| 140 | argc -= optind; |
| 141 | argv += optind; |
| 142 | if (argc != 1) { |
| 143 | usage(); |
| 144 | exit(1); |
| 145 | } |
| 146 | |
| 147 | if (strcmp(argv[0], "-") != 0) { |
| 148 | input_filename = argv[0]; |
| 149 | input = fopen(input_filename, "r"); |
| 150 | if (input == NULL) { |
| 151 | perror("Couldn't open input file"); |
| 152 | exit(1); |
| 153 | } |
| 154 | } |
Ben Widawsky | 83a5c38 | 2011-03-23 22:08:39 -0700 | [diff] [blame] | 155 | if (byte_array_input) |
| 156 | program = read_program_binary (input); |
| 157 | else |
| 158 | program = read_program (input); |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 159 | if (!program) |
| 160 | exit (1); |
| 161 | if (output_file) { |
| 162 | output = fopen (output_file, "w"); |
| 163 | if (output == NULL) { |
| 164 | perror("Couldn't open output file"); |
| 165 | exit(1); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | for (inst = program->first; inst; inst = inst->next) |
Damien Lespiau | a45a471 | 2013-01-21 19:28:41 +0000 | [diff] [blame] | 170 | brw_disasm (output, &inst->instruction.gen, gen); |
Keith Packard | ae85b10 | 2008-04-23 12:52:58 -0700 | [diff] [blame] | 171 | exit (0); |
| 172 | } |