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 int xmov;
48 uint32_t literal;
49 const char *label;
50
51 bool has_immed : 1;
52 bool has_shift : 1;
53 bool has_bit : 1;
54 bool is_literal : 1;
55 bool rep : 1;
56 };
57
58 struct asm_label {
59 unsigned offset;
60 const char *label;
61 };
62
63 struct asm_instruction *next_instr(int tok);
64 void decl_label(const char *str);
65
66
67 static inline uint32_t
parse_reg(const char * str)68 parse_reg(const char *str)
69 {
70 char *retstr;
71 long int ret;
72
73 if (!strcmp(str, "$rem"))
74 return 0x1c;
75 else if (!strcmp(str, "$addr"))
76 return 0x1d;
77 else if (!strcmp(str, "$addr2"))
78 return 0x1e;
79 else if (!strcmp(str, "$data"))
80 return 0x1f;
81
82 ret = strtol(str + 1, &retstr, 16);
83
84 if (*retstr != '\0') {
85 printf("invalid register: %s\n", str);
86 exit(2);
87 }
88
89 return ret;
90 }
91
92 static inline uint32_t
parse_literal(const char * str)93 parse_literal(const char *str)
94 {
95 char *retstr;
96 long int ret;
97
98 ret = strtol(str + 1, &retstr, 16);
99
100 if (*retstr != ']') {
101 printf("invalid literal: %s\n", str);
102 exit(2);
103 }
104
105 return ret;
106 }
107
108 static inline uint32_t
parse_bit(const char * str)109 parse_bit(const char *str)
110 {
111 return strtol(str + 1, NULL, 10);
112 }
113
114 unsigned parse_control_reg(const char *name);
115
116 /* string trailing ':' off label: */
117 static inline const char *
parse_label_decl(const char * str)118 parse_label_decl(const char *str)
119 {
120 char *s = strdup(str);
121 s[strlen(s) - 1] = '\0';
122 return s;
123 }
124
125 void yyset_in (FILE * _in_str );
126
127
128 #endif /* _ASM_H_ */
129