03fb1508907a5f2c1f2be21930e8dc06418e2266
[mesa.git] / src / freedreno / afuc / asm.h
1 /*
2 * Copyright (c) 2017 Rob Clark <robdclark@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef _ASM_H_
25 #define _ASM_H_
26
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include "afuc.h"
30
31 extern int gpuver;
32
33 /**
34 * Intermediate representation for an instruction, before final encoding.
35 * This mostly exists because we need to resolve label offset's in a 2nd
36 * pass, but also so that parser.y doesn't really need to care so much
37 * about the different encodings for 2src regs vs 1src+immed, or mnemonics
38 */
39 struct asm_instruction {
40 int tok;
41 int dst;
42 int src1;
43 int src2;
44 int immed;
45 int shift;
46 int bit;
47 uint32_t literal;
48 const char *label;
49
50 bool has_immed : 1;
51 bool has_shift : 1;
52 bool has_bit : 1;
53 bool is_literal : 1;
54 bool rep : 1;
55 };
56
57 struct asm_label {
58 unsigned offset;
59 const char *label;
60 };
61
62 struct asm_instruction *next_instr(int tok);
63 void decl_label(const char *str);
64
65
66 static inline uint32_t
67 parse_reg(const char *str)
68 {
69 char *retstr;
70 long int ret;
71
72 if (!strcmp(str, "$rem"))
73 return 0x1c;
74 else if (!strcmp(str, "$addr"))
75 return 0x1d;
76 else if (!strcmp(str, "$addr2"))
77 return 0x1e;
78 else if (!strcmp(str, "$data"))
79 return 0x1f;
80
81 ret = strtol(str + 1, &retstr, 16);
82
83 if (*retstr != '\0') {
84 printf("invalid register: %s\n", str);
85 exit(2);
86 }
87
88 return ret;
89 }
90
91 static inline uint32_t
92 parse_literal(const char *str)
93 {
94 char *retstr;
95 long int ret;
96
97 ret = strtol(str + 1, &retstr, 16);
98
99 if (*retstr != ']') {
100 printf("invalid literal: %s\n", str);
101 exit(2);
102 }
103
104 return ret;
105 }
106
107 static inline uint32_t
108 parse_bit(const char *str)
109 {
110 return strtol(str + 1, NULL, 10);
111 }
112
113 unsigned parse_control_reg(const char *name);
114
115 /* string trailing ':' off label: */
116 static inline const char *
117 parse_label_decl(const char *str)
118 {
119 char *s = strdup(str);
120 s[strlen(s) - 1] = '\0';
121 return s;
122 }
123
124 void yyset_in (FILE * _in_str );
125
126
127 #endif /* _ASM_H_ */