summaryrefslogtreecommitdiff
path: root/assembler/disasm-main.c
blob: b36555682f5e71247bc6c28c49407a0083dd3355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright © 2008 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>

#include "gen4asm.h"
#include "brw_eu.h"
#include "gen8_instruction.h"

static const struct option longopts[] = {
	{ NULL, 0, NULL, 0 }
};

static struct brw_program *
read_program (FILE *input)
{
    uint32_t			    inst[4];
    struct brw_program		    *program;
    struct brw_program_instruction  *entry, **prev;
    int			c;
    int			n = 0;

    program = malloc (sizeof (struct brw_program));
    program->first = NULL;
    prev = &program->first;
    while ((c = getc (input)) != EOF) {
	if (c == '0') {
	    if (fscanf (input, "x%x", &inst[n]) == 1) {
		++n;
		if (n == 4) {
		    entry = malloc (sizeof (struct brw_program_instruction));
		    memcpy (&entry->insn, inst, 4 * sizeof (uint32_t));
		    entry->next = NULL;
		    *prev = entry;
		    prev = &entry->next;
		    n = 0;
		}
	    }
	}
    }
    return program;
}

static struct brw_program *
read_program_binary (FILE *input)
{
    uint32_t			    temp;
    uint8_t			    inst[16];
    struct brw_program		    *program;
    struct brw_program_instruction  *entry, **prev;
    int			c;
    int			n = 0;

    program = malloc (sizeof (struct brw_program));
    program->first = NULL;
    prev = &program->first;
    while ((c = getc (input)) != EOF) {
	if (c == '0') {
	    if (fscanf (input, "x%2x", &temp) == 1) {
		inst[n++] = (uint8_t)temp;
		if (n == 16) {
		    entry = malloc (sizeof (struct brw_program_instruction));
		    memcpy (&entry->insn, inst, 16 * sizeof (uint8_t));
		    entry->next = NULL;
		    *prev = entry;
		    prev = &entry->next;
		    n = 0;
		}
	    }
	}
    }
    return program;
}

static void usage(void)
{
    fprintf(stderr, "usage: intel-gen4disasm [options] inputfile\n");
    fprintf(stderr, "\t-b, --binary                         C style binary output\n");
    fprintf(stderr, "\t-o, --output {outputfile}            Specify output file\n");
    fprintf(stderr, "\t-g, --gen <4|5|6|7|8|9>              Specify GPU generation\n");
}

int main(int argc, char **argv)
{
    struct brw_program	*program;
    FILE		*input = stdin;
    FILE		*output = stdout;
    char		*input_filename = NULL;
    char		*output_file = NULL;
    int			byte_array_input = 0;
    int			o;
    int			gen = 4;
    struct brw_program_instruction  *inst;

    while ((o = getopt_long(argc, argv, "o:bg:", longopts, NULL)) != -1) {
	switch (o) {
	case 'o':
	    if (strcmp(optarg, "-") != 0)
		output_file = optarg;
	    break;
	case 'b':
	    byte_array_input = 1;
	    break;
	case 'g':
	    gen = strtol(optarg, NULL, 10);

	    if (gen < 4 || gen > 9) {
		    usage();
		    exit(1);
	    }

	    break;
	default:
	    usage();
	    exit(1);
	}
    }
    argc -= optind;
    argv += optind;
    if (argc != 1) {
	usage();
	exit(1);
    }

    if (strcmp(argv[0], "-") != 0) {
	input_filename = argv[0];
	input = fopen(input_filename, "r");
	if (input == NULL) {
	    perror("Couldn't open input file");
	    exit(1);
	}
    }
    if (byte_array_input)
	program = read_program_binary (input);
    else
	program = read_program (input);
    if (!program)
	exit (1);
    if (output_file) {
	output = fopen (output_file, "w");
	if (output == NULL) {
	    perror("Couldn't open output file");
	    exit(1);
	}
    }

    for (inst = program->first; inst; inst = inst->next)
	if (gen >= 8)
	    gen8_disassemble(output, &inst->insn.gen8, gen);
	else
	    brw_disasm (output, &inst->insn.gen, gen);

    exit (0);
}