2 * Copyright(c) 2019-2022 rev.ng Labs Srl. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #define TCGV_NAME_SIZE 7
27 #define MAX_WRITTEN_REGS 32
28 #define OFFSET_STR_LEN 32
29 #define ALLOC_LIST_LEN 32
30 #define ALLOC_NAME_SIZE 32
31 #define INIT_LIST_LEN 32
32 #define OUT_BUF_LEN (1024 * 1024)
33 #define SIGNATURE_BUF_LEN (128 * 1024)
34 #define HEADER_BUF_LEN (128 * 1024)
36 /* Variadic macros to wrap the buffer printing functions */
37 #define EMIT(c, ...) \
39 g_string_append_printf((c)->out_str, __VA_ARGS__); \
42 #define EMIT_SIG(c, ...) \
44 g_string_append_printf((c)->signature_str, __VA_ARGS__); \
47 #define EMIT_HEAD(c, ...) \
49 g_string_append_printf((c)->header_str, __VA_ARGS__); \
53 * Type of register, assigned to the HexReg.type field
55 typedef enum { GENERAL_PURPOSE
, CONTROL
, MODIFIER
, DOTNEW
} HexRegType
;
57 typedef enum { UNKNOWN_SIGNEDNESS
, SIGNED
, UNSIGNED
} HexSignedness
;
60 * Semantic record of the REG tokens, identifying registers
62 typedef struct HexReg
{
63 uint8_t id
; /**< Identifier of the register */
64 HexRegType type
; /**< Type of the register */
65 unsigned bit_width
; /**< Bit width of the reg, 32 or 64 bits */
69 * Data structure, identifying a TCGv temporary value
71 typedef struct HexTmp
{
72 unsigned index
; /**< Index of the TCGv temporary value */
76 * Enum of the possible immediated, an immediate is a value which is known
77 * at tinycode generation time, e.g. an integer value, not a TCGv
89 * Semantic record of the IMM token, identifying an immediate constant
91 typedef struct HexImm
{
93 char id
; /**< Identifier, used when type is VARIABLE */
94 uint64_t value
; /**< Immediate value, used when type is VALUE */
95 uint64_t index
; /**< Index, used when type is QEMU_TMP */
97 enum ImmUnionTag type
; /**< Type of the immediate */
101 * Semantic record of the PRED token, identifying a predicate
103 typedef struct HexPred
{
104 char id
; /**< Identifier of the predicate */
108 * Semantic record of the SAT token, identifying the saturate operator
109 * Note: All saturates are assumed to implicitly set overflow.
111 typedef struct HexSat
{
112 HexSignedness signedness
; /**< Signedness of the sat. op. */
116 * Semantic record of the CAST token, identifying the cast operator
118 typedef struct HexCast
{
119 unsigned bit_width
; /**< Bit width of the cast operator */
120 HexSignedness signedness
; /**< Unsigned flag for the cast operator */
124 * Semantic record of the EXTRACT token, identifying the cast operator
126 typedef struct HexExtract
{
127 unsigned bit_width
; /**< Bit width of the extract operator */
128 unsigned storage_bit_width
; /**< Actual bit width of the extract operator */
129 HexSignedness signedness
; /**< Unsigned flag for the extract operator */
133 * Semantic record of the MPY token, identifying the fMPY multiplication
136 typedef struct HexMpy
{
137 unsigned first_bit_width
; /**< Bit width of 1st operand of fMPY */
138 unsigned second_bit_width
; /**< Bit width of 2nd operand of fMPY */
139 HexSignedness first_signedness
; /**< Signedness of 1st operand of fMPY */
140 HexSignedness second_signedness
; /**< Signedness of 2nd operand of fMPY */
144 * Semantic record of the VARID token, identifying declared variables
145 * of the input language
147 typedef struct HexVar
{
148 GString
*name
; /**< Name of the VARID variable */
152 * Data structure uniquely identifying a declared VARID variable, used for
153 * keeping track of declared variable, so that any variable is declared only
154 * once, and its properties are propagated through all the subsequent instances
158 GString
*name
; /**< Name of the VARID variable */
159 uint8_t bit_width
; /**< Bit width of the VARID variable */
160 HexSignedness signedness
; /**< Unsigned flag for the VARID var */
164 * Enum of the possible rvalue types, used in the HexValue.type field
166 typedef enum RvalueUnionTag
{
167 REGISTER
, REGISTER_ARG
, TEMP
, IMMEDIATE
, PREDICATE
, VARID
171 * Semantic record of the rvalue token, identifying any numeric value,
172 * immediate or register based. The rvalue tokens are combined together
173 * through the use of several operators, to encode expressions
175 typedef struct HexValue
{
177 HexReg reg
; /**< rvalue of register type */
178 HexTmp tmp
; /**< rvalue of temporary type */
179 HexImm imm
; /**< rvalue of immediate type */
180 HexPred pred
; /**< rvalue of predicate type */
181 HexVar var
; /**< rvalue of declared variable type */
183 RvalueUnionTag type
; /**< Type of the rvalue */
184 unsigned bit_width
; /**< Bit width of the rvalue */
185 HexSignedness signedness
; /**< Unsigned flag for the rvalue */
186 bool is_dotnew
; /**< rvalue of predicate type is dotnew? */
190 * State of ternary operator
192 typedef enum TernaryState
{ IN_LEFT
, IN_RIGHT
} TernaryState
;
195 * Data structure used to handle side effects inside ternary operators
197 typedef struct Ternary
{
203 * Operator type, used for referencing the correct operator when calling the
204 * gen_bin_op() function, which in turn will generate the correct code to
205 * execute the operation between the two rvalues
207 typedef enum OpType
{
208 ADD_OP
, SUB_OP
, MUL_OP
, ASL_OP
, ASR_OP
, LSR_OP
, ANDB_OP
, ORB_OP
,
209 XORB_OP
, ANDL_OP
, MINI_OP
, MAXI_OP
213 * Data structure including instruction specific information, to be cleared
214 * out after the compilation of each instruction
216 typedef struct Inst
{
217 GString
*name
; /**< Name of the compiled instruction */
218 char *code_begin
; /**< Beginning of instruction input code */
219 char *code_end
; /**< End of instruction input code */
220 unsigned tmp_count
; /**< Index of the last declared TCGv temp */
221 unsigned qemu_tmp_count
; /**< Index of the last declared int temp */
222 unsigned if_count
; /**< Index of the last declared if label */
223 unsigned error_count
; /**< Number of generated errors */
224 GArray
*allocated
; /**< Allocated declaredVARID vars */
225 GArray
*init_list
; /**< List of initialized registers */
226 GArray
*strings
; /**< Strings allocated by the instruction */
230 * Data structure representing the whole translation context, which in a
231 * reentrant flex/bison parser just like ours is passed between the scanner
232 * and the parser, holding all the necessary information to perform the
233 * parsing, this data structure survives between the compilation of different
236 typedef struct Context
{
237 void *scanner
; /**< Reentrant parser state pointer */
238 char *input_buffer
; /**< Buffer containing the input code */
239 GString
*out_str
; /**< String containing the output code */
240 GString
*signature_str
; /**< String containing the signatures code */
241 GString
*header_str
; /**< String containing the header code */
242 FILE *defines_file
; /**< FILE * of the generated header */
243 FILE *output_file
; /**< FILE * of the C output file */
244 FILE *enabled_file
; /**< FILE * of the list of enabled inst */
245 GArray
*ternary
; /**< Array to track nesting of ternary ops */
246 unsigned total_insn
; /**< Number of instructions in input file */
247 unsigned implemented_insn
; /**< Instruction compiled without errors */
248 Inst inst
; /**< Parsing data of the current inst */
251 #endif /* IDEF_PARSER_H */