summaryrefslogtreecommitdiff
path: root/assembler/src/main.c
blob: 15ed5179fa7dde85f6cccc2d79e8653129fae99e (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* -*- c-basic-offset: 8 -*- */
/*
 * Copyright © 2006 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

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

#include "gen4asm.h"

extern FILE *yyin;

extern int errors;

long int gen_level = 40;
int advanced_flag = 0; /* 0: in unit of byte, 1: in unit of data element size */
int binary_like_output = 0; /* 0: default output style, 1: nice C-style output */
int need_export = 0;
char *input_filename = "<stdin>";
char *export_filename = NULL;

const char const *binary_prepend = "static const char gen_eu_bytes[] = {\n";

struct brw_program compiled_program;
struct program_defaults program_defaults = {.register_type = BRW_REGISTER_TYPE_F};

#define HASH_SIZE 37

struct hash_item {
	char *key;
	void *value;
	struct hash_item *next;
};

typedef struct hash_item *hash_table[HASH_SIZE];

static hash_table declared_register_table;

struct label_item {
	char *name;
	int addr;
	struct label_item *next;
};
static struct label_item *label_table;

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'},
	{"gen", required_argument, 0, 'g'},
	{ NULL, 0, NULL, 0 }
};

// jump distance used in branch instructions as JIP or UIP
static int jump_distance(int offset)
{
    // Gen4- bspec: the jump distance is in number of sixteen-byte units
    // Gen5+ bspec: the jump distance is in number of eight-byte units
    if(IS_GENp(5))
        offset *= 2;
    return offset;
}

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");
	fprintf(stderr, "\t-g, --gen <4|5|6|7>                  Specify GPU generation\n");
}

static int hash(char *key)
{
    unsigned ret = 0;
    while(*key)
        ret = (ret << 1) + (*key++);
    return ret % HASH_SIZE;
}

static void *find_hash_item(hash_table t, char *key)
{
    struct hash_item *p;
    for(p = t[hash(key)]; p; p = p->next)
	if(strcasecmp(p->key, key) == 0)
	    return p->value;
    return NULL;
}

static void insert_hash_item(hash_table t, char *key, void *v)
{
    int index = hash(key);
    struct hash_item *p = malloc(sizeof(*p));
    p->key = key;
    p->value = v;
    p->next = t[index];
    t[index] = p;
}

static void free_hash_table(hash_table t)
{
    struct hash_item *p, *next;
    int i;
    for (i = 0; i < HASH_SIZE; i++) {
	p = t[i];
	while(p) {
	    next = p->next;
	    free(p->key);
	    free(p->value);
	    free(p);
	    p = next;
	}
    }
}

struct declared_register *find_register(char *name)
{
    return find_hash_item(declared_register_table, name);
}

void insert_register(struct declared_register *reg)
{
    insert_hash_item(declared_register_table, reg->name, reg);
}

void add_label(char *name, int addr)
{
    struct label_item **p = &label_table;
    while(*p)
        p = &((*p)->next);
    *p = calloc(1, sizeof(**p));
    (*p)->name = name;
    (*p)->addr = addr;
}

/* Some assembly code have duplicated labels.
   Start from start_addr. Search as a loop. Return the first label found. */
int label_to_addr(char *name, int start_addr)
{
    /* return the first label just after start_addr, or the first label from the head */
    struct label_item *p;
    int r = -1;
    for(p = label_table; p; p = p->next) {
        if(strcmp(p->name, name) == 0) {
            if(p->addr >= start_addr) // the first label just after start_addr
                return p->addr;
            else if(r == -1) // the first label from the head
                r = p->addr;
        }
    }
    if(r == -1) {
        fprintf(stderr, "Can't find label %s\n", name);
        exit(1);
    }
    return r;
}

static void free_label_table(struct label_item *p)
{
    if(p) {
        free_label_table(p->next);
        free(p);
    }
}

struct entry_point_item {
	char *str;
	struct entry_point_item *next;
} *entry_point_table;

static int read_entry_file(char *fn)
{
	FILE *entry_table_file;
	char buf[2048];
	struct entry_point_item **p = &entry_point_table;
	if (!fn)
		return 0;
	if ((entry_table_file = fopen(fn, "r")) == NULL)
		return -1;
	while (fgets(buf, sizeof(buf)-1, entry_table_file) != NULL) {
		// drop the final char '\n'
		if(buf[strlen(buf)-1] == '\n')
			buf[strlen(buf)-1] = 0;
		*p = calloc(1, sizeof(struct entry_point_item));
		(*p)->str = strdup(buf);
		p = &((*p)->next);
	}
	fclose(entry_table_file);
	return 0;
}

static int is_entry_point(char *s)
{
	struct entry_point_item *p;
	for (p = entry_point_table; p; p = p->next) {
	    if (strcmp(p->str, s) == 0)
		return 1;
	}
	return 0;
}

static void free_entry_point_table(struct entry_point_item *p) {
	if (p) {
		free_entry_point_table(p->next);
		free(p->str);
		free(p);
	}
}

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,\n"
				"\t0x%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;
	char *entry_table_file = NULL;
	FILE *output = stdout;
	FILE *export_file;
	struct brw_program_instruction *entry, *entry1, *tmp_entry;
	int err, inst_offset;
	char o;
	while ((o = getopt_long(argc, argv, "e:l:o:g:ab", longopts, NULL)) != -1) {
		switch (o) {
		case 'o':
			if (strcmp(optarg, "-") != 0)
				output_file = optarg;

			break;

		case 'g': {
			char *dec_ptr, *end_ptr;
			unsigned long decimal;

			gen_level = strtol(optarg, &dec_ptr, 10) * 10;

			if (*dec_ptr == '.') {
				decimal = strtoul(++dec_ptr, &end_ptr, 10);
				if (end_ptr != dec_ptr && *end_ptr == '\0') {
					if (decimal > 10) {
						fprintf(stderr, "Invalid Gen X decimal version\n");
						exit(1);
					}
					gen_level += decimal;
				}
			}

			if (gen_level < 40 || gen_level > 75) {
				usage();
				exit(1);
			}

			break;
		}

		case 'a':
			advanced_flag = 1;
			break;
		case 'b':
			binary_like_output = 1;
			break;

		case 'e':
			need_export = 1;
			if (strcmp(optarg, "-") != 0)
				export_filename = optarg;
			break;

		case 'l':
			if (strcmp(optarg, "-") != 0)
				entry_table_file = optarg;
			break;

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

	if (strcmp(argv[0], "-") != 0) {
		input_filename = argv[0];
		yyin = fopen(input_filename, "r");
		if (yyin == NULL) {
			perror("Couldn't open input file");
			exit(1);
		}
	}

	err = yyparse();

	if (strcmp(argv[0], "-"))
		fclose(yyin);

	yylex_destroy();

	if (err || errors)
		exit (1);

	if (output_file) {
		output = fopen(output_file, "w");
		if (output == NULL) {
			perror("Couldn't open output file");
			exit(1);
		}

	}

	if (read_entry_file(entry_table_file)) {
		fprintf(stderr, "Read entry file error\n");
		exit(1);
	}
	inst_offset = 0 ;
	for (entry = compiled_program.first;
		entry != NULL; entry = entry->next) {
	    entry->inst_offset = inst_offset;
	    entry1 = entry->next;
	    if (entry1 && entry1->islabel && is_entry_point(entry1->string)) {
		// insert NOP instructions until (inst_offset+1) % 4 == 0
		while (((inst_offset+1) % 4) != 0) {
		    tmp_entry = calloc(sizeof(*tmp_entry), 1);
		    tmp_entry->instruction.header.opcode = BRW_OPCODE_NOP;
		    entry->next = tmp_entry;
		    tmp_entry->next = entry1;
		    entry = tmp_entry;
		    tmp_entry->inst_offset = ++inst_offset;
		}
	    }
	    if (!entry->islabel)
              inst_offset++;
	}

	for (entry = compiled_program.first; entry; entry = entry->next)
	    if (entry->islabel)
		add_label(entry->string, entry->inst_offset);

	if (need_export) {
		if (export_filename) {
			export_file = fopen(export_filename, "w");
		} else {
			export_file = fopen("export.inc", "w");
		}
		for (entry = compiled_program.first;
			entry != NULL; entry = entry->next) {
		    if (entry->islabel) 
			fprintf(export_file, "#define %s_IP %d\n",
				entry->string, (IS_GENx(5) ? 2 : 1)*(entry->inst_offset));
		}
		fclose(export_file);
	}

	for (entry = compiled_program.first; entry; entry = entry->next) {
	    struct brw_instruction *inst = & entry->instruction;

	    if (inst->first_reloc_target)
		inst->first_reloc_offset = label_to_addr(inst->first_reloc_target, entry->inst_offset) - entry->inst_offset;

	    if (inst->second_reloc_target)
		inst->second_reloc_offset = label_to_addr(inst->second_reloc_target, entry->inst_offset) - entry->inst_offset;

	    if (inst->second_reloc_offset) {
		// this is a branch instruction with two offset arguments
		entry->instruction.bits3.branch_2_offset.JIP = jump_distance(inst->first_reloc_offset);
		entry->instruction.bits3.branch_2_offset.UIP = jump_distance(inst->second_reloc_offset);
	    } else if (inst->first_reloc_offset) {
		// this is a branch instruction with one offset argument
		int offset = inst->first_reloc_offset;
		/* bspec: Unlike other flow control instructions, the offset used by JMPI is relative to the incremented instruction pointer rather than the IP value for the instruction itself. */
		
		int is_jmpi = entry->instruction.header.opcode == BRW_OPCODE_JMPI; // target relative to the post-incremented IP, so delta == 1 if JMPI
		if(is_jmpi)
		    offset --;
		offset = jump_distance(offset);
		if (is_jmpi && (gen_level == 75))
			offset = offset * 8;

		if(!IS_GENp(6)) {
		    entry->instruction.bits3.JIP = offset;
		    if(entry->instruction.header.opcode == BRW_OPCODE_ELSE)
			entry->instruction.bits3.branch_2_offset.UIP = 1; /* Set the istack pop count, which must always be 1. */
		} else if(IS_GENx(6)) {
		    /* TODO: endif JIP pos is not in Gen6 spec. may be bits1 */
		    int opcode = entry->instruction.header.opcode;
		    if(opcode == BRW_OPCODE_CALL || opcode == BRW_OPCODE_JMPI)
			entry->instruction.bits3.JIP = offset; // for CALL, JMPI
		    else
			entry->instruction.bits1.branch.JIP = offset; // for CASE,ELSE,FORK,IF,WHILE
		} else if(IS_GENp(7)) {
		    int opcode = entry->instruction.header.opcode;
		    /* Gen7 JMPI Restrictions in bspec:
		     * The JIP data type must be Signed DWord
		     */
		    if(opcode == BRW_OPCODE_JMPI)
			entry->instruction.bits3.JIP = offset;
		    else
			entry->instruction.bits3.branch_2_offset.JIP = offset;
		}
	    }
	}

	if (binary_like_output)
		fprintf(output, "%s", binary_prepend);

	for (entry = compiled_program.first;
		entry != NULL;
		entry = entry1) {
	    entry1 = entry->next;
	    if (!entry->islabel)
		print_instruction(output, entry);
	    else
		free(entry->string);
	    free(entry);
	}
	if (binary_like_output)
		fprintf(output, "};");

	free_entry_point_table(entry_point_table);
	free_hash_table(declared_register_table);
	free_label_table(label_table);

	fflush (output);
	if (ferror (output)) {
	    perror ("Could not flush output file");
	    if (output_file)
		unlink (output_file);
	    err = 1;
	}
	return err;
}