blob: fbb6ae3349a15f1c82dcd44aede79c14940627dd [file] [log] [blame]
Keith Packardae85b102008-04-23 12:52:58 -07001/*
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 Lespiau4ca1a042013-01-19 00:30:18 +000030#include "brw_eu.h"
Keith Packardae85b102008-04-23 12:52:58 -070031
32static const struct option longopts[] = {
33 { NULL, 0, NULL, 0 }
34};
35
36static struct brw_program *
37read_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 Widawsky83a5c382011-03-23 22:08:39 -070044
Keith Packardae85b102008-04-23 12:52:58 -070045 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 Widawsky83a5c382011-03-23 22:08:39 -070066static struct brw_program *
67read_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 Packardae85b102008-04-23 12:52:58 -070097static void usage(void)
98{
Damien Lespiau4ca1a042013-01-19 00:30:18 +000099 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 Packardae85b102008-04-23 12:52:58 -0700103}
104
105int 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 Widawsky83a5c382011-03-23 22:08:39 -0700112 int byte_array_input = 0;
Keith Packardae85b102008-04-23 12:52:58 -0700113 int o;
Damien Lespiau4ca1a042013-01-19 00:30:18 +0000114 int gen = 4;
Keith Packardae85b102008-04-23 12:52:58 -0700115 struct brw_program_instruction *inst;
116
Damien Lespiau4ca1a042013-01-19 00:30:18 +0000117 while ((o = getopt_long(argc, argv, "o:bg:", longopts, NULL)) != -1) {
Keith Packardae85b102008-04-23 12:52:58 -0700118 switch (o) {
119 case 'o':
120 if (strcmp(optarg, "-") != 0)
121 output_file = optarg;
122 break;
Ben Widawsky83a5c382011-03-23 22:08:39 -0700123 case 'b':
124 byte_array_input = 1;
125 break;
Damien Lespiau4ca1a042013-01-19 00:30:18 +0000126 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 Packardae85b102008-04-23 12:52:58 -0700135 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 Widawsky83a5c382011-03-23 22:08:39 -0700155 if (byte_array_input)
156 program = read_program_binary (input);
157 else
158 program = read_program (input);
Keith Packardae85b102008-04-23 12:52:58 -0700159 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 Lespiaua45a4712013-01-21 19:28:41 +0000170 brw_disasm (output, &inst->instruction.gen, gen);
Keith Packardae85b102008-04-23 12:52:58 -0700171 exit (0);
172}