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 <stdbool.h>
28 #include <stdint.h>
29 #include "afuc.h"
30
31 extern int gpuver;
32
33 struct asm_label {
34 unsigned offset;
35 const char *label;
36 };
37
38 struct afuc_instr *next_instr(afuc_opc opc);
39 void decl_label(const char *str);
40
41 static inline uint32_t
parse_reg(const char * str)42 parse_reg(const char *str)
43 {
44 char *retstr;
45 long int ret;
46
47 if (!strcmp(str, "$rem"))
48 return REG_REM;
49 else if (!strcmp(str, "$memdata"))
50 return REG_MEMDATA;
51 else if (!strcmp(str, "$addr"))
52 return REG_ADDR;
53 else if (!strcmp(str, "$regdata"))
54 return REG_REGDATA;
55 else if (!strcmp(str, "$usraddr"))
56 return REG_USRADDR;
57 else if (!strcmp(str, "$data"))
58 return 0x1f;
59
60 ret = strtol(str + 1, &retstr, 16);
61
62 if (*retstr != '\0') {
63 printf("invalid register: %s\n", str);
64 exit(2);
65 }
66
67 return ret;
68 }
69
70 static inline uint32_t
parse_literal(const char * str)71 parse_literal(const char *str)
72 {
73 char *retstr;
74 long int ret;
75
76 ret = strtol(str + 1, &retstr, 16);
77
78 if (*retstr != ']') {
79 printf("invalid literal: %s\n", str);
80 exit(2);
81 }
82
83 return ret;
84 }
85
86 static inline uint32_t
parse_bit(const char * str)87 parse_bit(const char *str)
88 {
89 return strtol(str + 1, NULL, 10);
90 }
91
92 unsigned parse_control_reg(const char *name);
93 unsigned parse_sqe_reg(const char *name);
94
95 /* string trailing ':' off label: */
96 static inline const char *
parse_label_decl(const char * str)97 parse_label_decl(const char *str)
98 {
99 char *s = strdup(str);
100 s[strlen(s) - 1] = '\0';
101 return s;
102 }
103
104 void yyset_in(FILE *_in_str);
105
106 #endif /* _ASM_H_ */
107