Debian Codegraph @

src/fcml_encoding.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2024 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "fcml_encoding.h"
21
22 #include <stdio.h>
23 #include <fcml_dialect.h>
24 #include <fcml_errors.h>
25 #include <fcml_optimizers.h>
26 #include <fcml_choosers.h>
27 #include <fcml_assembler.h>
28
29 #include "fcml_def.h"
30 #include "fcml_env_int.h"
31 #include "fcml_hints.h"
32 #include "fcml_messages.h"
33 #include "fcml_mnemonic_parser.h"
34 #include "fcml_modrm.h"
35 #include "fcml_modrm_encoder.h"
36 #include "fcml_stream.h"
37 #include "fcml_trace.h"
38 #include "fcml_utils.h"
39
40 #define FCML_ASM_MAX_PART_PROCESSORS 40
41
42 /* REX prefix.*/
43 #define FCML_ENCODE_REX_BASE 0x40
44 #define FCML_ENCODE_REX_W(rex,w) (rex | (w << 3))
45 #define FCML_ENCODE_REX_R(rex,r) (rex | (r << 2))
46 #define FCML_ENCODE_REX_X(rex,x) (rex | (x << 1))
47 #define FCML_ENCODE_REX_B(rex,b) (rex | b)
48
49 /* XOP/VEX prefixes.*/
50 #define FCML_ENCODE_VEXOP_MMMM(vexop,x) ((vexop) | ((x) & 0x1F))
51 #define FCML_ENCODE_VEXOP_VVVV(vexop,x) ((vexop) | ((~(x) & 0x0F) << 3))
52 #define FCML_ENCODE_VEXOP_PP(vexop,x) ((vexop) | ((x) & 0x03))
53 #define FCML_ENCODE_VEXOP_W(vexop,x) ((vexop) | (((x) & 0x01) << 7))
54 #define FCML_ENCODE_VEXOP_L(vexop,x) ((vexop) | (((x) & 0x01) << 2))
55
56 /* R,X and B are stored in 1's complement form.*/
57 #define FCML_ENCODE_VEXOP_R(vexop,x) ((vexop) | ((~(x) & 0x01) << 7))
58 #define FCML_ENCODE_VEXOP_X(vexop,x) ((vexop) | ((~(x) & 0x01) << 6))
59 #define FCML_ENCODE_VEXOP_B(vexop,x) ((vexop) | ((~(x) & 0x01) << 5))
60
61 /* EVEX prefix */
62 #define FCML_ENCODE_EVEX_PREFIX_LENGTH 4
63
64 #define FCML_ENCODE_EVEX_P0_R(evex,x) ((evex) | ((~(x) & 0x01) << 7))
65 #define FCML_ENCODE_EVEX_P0_X(evex,x) ((evex) | ((~(x) & 0x01) << 6))
66 #define FCML_ENCODE_EVEX_P0_B(evex,x) ((evex) | ((~(x) & 0x01) << 5))
67 #define FCML_ENCODE_EVEX_P0_R_prim(evex,x) ((evex) | ((~(x) & 0x01) << 4))
68 #define FCML_ENCODE_EVEX_P0_mm(evex,x) ((evex) | ((x) & 0x03))
69
70 #define FCML_ENCODE_EVEX_P1_W(evex,x) ((evex) | (((x) & 0x01) << 7))
71 #define FCML_ENCODE_EVEX_P1_vvvv(evex,x) FCML_ENCODE_VEXOP_VVVV(evex, x)
72 #define FCML_ENCODE_EVEX_P1_pp(evex,x) FCML_ENCODE_VEXOP_PP(evex, x)
73
74 #define FCML_ENCODE_EVEX_P2_z(evex,x) ((evex) | (((x) & 0x01) << 7))
75 #define FCML_ENCODE_EVEX_P2_b(evex,x) ((evex) | (((x) & 0x01) << 4))
76 #define FCML_ENCODE_EVEX_P2_V_prim(evex,x) ((evex) | ((~(x) & 0x01) << 3))
77 #define FCML_ENCODE_EVEX_P2_aaa(evex,x) ((evex) | ((x) & 0x07))
78 #define FCML_ENCODE_EVEX_P2_LL(evex,x) ((evex) | (((x) & 0x03) << 5))
79
80 #define FCML_ASM_FCF 16
81
82 /* Addressing mode processing phase. */
83 typedef enum addr_mode_proc_phase {
84 AMPP_UNDEFINED,
85 AMPP_ACCEPT,
86 AMPP_PROCESS
87 } addr_mode_proc_phase;
88
89 /* An error container that holds an information whether errors messages are
90 enabled or not. */
91 struct error_container {
92 /** True if optional error and warning messages should be collected
93 * during processing. */
94 fcml_bool enabled;
95 /* A common error container. */
96 fcml_st_ceh_error_container errors;
97 };
98
99 /*
100 * This container holds errors collected while all addressing modes are
101 * processed.
102 */
103 typedef struct addr_mode_error {
104 /** Addressing mode processing failed in the phase: */
105 addr_mode_proc_phase phase;
106 /** Error code */
107 fcml_ceh_error error_code;
108 /** Local errors container, dedicated to addressing mode processing. */
109 struct error_container errors;
110 /** Ordinal number of failed instruction part processor. */
111 fcml_int ipp_failed;
112 } addr_mode_error;
113
114 typedef struct extension_prefixes_fields {
115 /* VEX/XOP/EVEX.vvvv. */
116 fcml_uint8_t vvvv;
117 /* EVEX.V'. */
118 fcml_uint8_t V_prim;
119 /* VEX/XOP.mmmmm, EVEX.mm */
120 fcml_uint8_t mmmm;
121 /* EVEX.z field. */
122 fcml_bool z;
123 /* EVEX.b field. */
124 fcml_bool b;
125 /* EVEX.aaa. */
126 fcml_uint8_t aaa;
127 /* Embedded rounding mode. */
128 fcml_nuint8_t er;
129 /* Vector length explicitly set for instruction. Do not set L'L. */
130 fcml_bool explicit_vector_length;
131 } extension_prefixes_fields;
132
133 /* Responsible for calculating memory data size. Custom calculators have \
134 * to implement this function.
135 */
136 typedef fcml_usize (*memory_data_size_calculator)(
137 const fcml_st_instruction *instruction, fcml_ptr args);
138
139 typedef struct addr_mode_desc_details {
140 /* Which OSA values are allowed for addressing mode. */
141 fcml_flags allowed_osa;
142 /* Which ASA values are allowed for addressing mode. */
143 fcml_flags allowed_asa;
144 /* Whether it's a conditional instruction or not. */
145 fcml_bool is_conditional;
146 /* Type of condition used for instruction. */
147 fcml_st_condition condition;
148 /* Optional custom memory data size calculator. */
149 memory_data_size_calculator ds_calculator;
150 /* Predefined calculator configuration. */
151 fcml_ptr ds_calculator_args;
152 } addr_mode_desc_details;
153
154 /* Encoded operand register details. */
155 struct opcode_reg {
156 fcml_uint8_t opcode_reg;
157 fcml_uint8_t ext_b;
158 };
159
160 /* is5 encoder consists of two parts which need to exchange state. */
161 struct isX_state {
162 fcml_uint8_t is5_byte;
163 };
164
165 /* Some hints which can be set by encoders in order to control
166 * ModR/M encoding process. */
167 struct modrm_hints {
168 fcml_bool is_sib_alternative_hint;
169 fcml_bool is_rel_alternative_hint;
170 fcml_bool is_abs_alternative_hint;
171 };
172
173 typedef struct encoding_context {
174 /* Assembler configuration. */
175 const fcml_st_assembler_context *assembler_context;
176 /* Instruction being encoded. */
177 const fcml_st_instruction *instruction;
178 /* Mnemonic chosen for addressing mode being encoded. */
179 const fcml_st_mp_mnemonic *mnemonic;
180 /* All optimizer related control fields are stored here. */
181 fcml_st_asm_optimizer_processing_details optimizer_processing_details;
182 /* isX related state used to exchange information between is5 encoders. */
183 struct isX_state isX_state;
184 /* Prefixes fields set by encoders. Used to encode instruction prefixes. */
185 extension_prefixes_fields epf;
186 /* Used to force specific non-default segment register. */
187 fcml_st_register segment_override;
188 /* All encoders are allowed to fill ModR/M details which
189 * are then used to encode ModR/M field. */
190 fcml_st_modrm mod_rm;
191 /* ModR/M related operand hints. */
192 struct modrm_hints modrm_hints;
193 /* ModR/M encoded into an intermediate form. All ModR/M fields
194 * can be found here. Provided by 'ipp_ModRM_encoder'. */
195 fcml_st_encoded_modrm encoded_mod_rm;
196 /* Opcode register to be encoded directly in the opcode and REX field. */
197 struct opcode_reg opcode_reg;
198 /* Encoded instruction size in bytes. */
199 fcml_nuint8_t instruction_size;
200 /* Information for the REX encoder that the REX prefix is needed
201 * to encode a register opcode. */
202 fcml_bool reg_opcode_needs_rex;
203 /* Operand size calculator selected for processed addressing mode.*/
204 memory_data_size_calculator ds_calculator;
205 /* Arguments for data size calculator. */
206 fcml_ptr ds_calculator_args;
207 /* Error messages.*/
208 struct error_container errors;
209 /* Number of lastly proceeded instruction part processor. */
210 fcml_int last_ipp;
211 #ifdef FCML_DEBUG
212 fcml_int __def_index;
213 #endif
214 } encoding_context;
215
216 /** Optimizer context for acceptors. */
217 struct acceptor_optimizer_ctx {
218 /** Allowed values of the operand size attribute calculated by the
219 * assembler engine.
220 * It is the optimizer who decides which one should be finally used.
221 */
222 fcml_st_nullable_size_flags allowed_eosa;
223 /** Allowed values of the address size attribute calculated by the
224 * assembler engine.
225 * It is optimizer who decides which one should be finally used.
226 */
227 fcml_st_nullable_size_flags allowed_easa;
228 /** Vector length of AVX instructions. Set to FCML_DS_UNDEF if not used. */
229 fcml_usize vector_length;
230 };
231
232 struct acceptor_ctx {
233 /** Assembler behavior can be configured here.*/
234 const fcml_st_assembler_conf configuration;
235 /* Instruction entry point configuration. */
236 const fcml_st_entry_point *entry_point;
237 /* Assembled instruction. */
238 const fcml_st_instruction *instruction;
239 /* Mnemonic chosen for addressing mode being encoded. */
240 const fcml_st_mp_mnemonic *mnemonic;
241 /* All optimizer related control fields are stored here. */
242 struct acceptor_optimizer_ctx optimizer_ctx;
243 /* Operand size calculator selected for processed addressing mode.*/
244 memory_data_size_calculator ds_calculator;
245 /* Arguments for data size calculator. */
246 fcml_ptr ds_calculator_args;
247 /* Error messages.*/
248 struct error_container error_container;
249 /* Number of lastly proceeded instruction part processor. */
250 fcml_int last_ipp;
251 #ifdef FCML_DEBUG
252 fcml_int __def_index;
253 #endif
254 };
255
256 struct inst_part;
257
258 /* Instruction part post processor. */
259 typedef fcml_ceh_error (*inst_part_post_processor)(encoding_context *context,
260 struct inst_part *, fcml_ptr post_processor_args);
261
262 /* Represents a part of an assembled instruction,
263 for instance imm8 or ModRM part. */
264 typedef struct inst_part {
265 fcml_uint8_t code[10];
266 fcml_int code_length;
267 fcml_ptr post_processor_args;
268 inst_part_post_processor post_processor;
269 } inst_part;
270
271 typedef struct inst_part_container {
272 /* Address of the instruction parts array.*/
273 inst_part *parts;
274 /* Number of instruction parts in the array.*/
275 fcml_int count;
276 } inst_part_container;
277
278 struct inst_addr_modes;
279
280 typedef enum part_processor_phase {
281 /* ModR/M and prefixes arguments filling. */
282 IPPP_FIRST_PHASE,
283 /* ModR/M encoding. */
284 IPPP_SECOND_PHASE,
285 /* Prefixes are applied in this phase. */
286 IPPP_THIRD_PHASE
287 } part_processor_phase;
288
289 typedef struct init_context {
290 fcml_coll_map inst_map;
291 fcml_st_dialect_context_int *dialect_context;
292 } init_context;
293
294 /** Builds addressing mode encodings for given addressing mode.
295 * All the built encodings are registered in the provided context. */
296 typedef fcml_ceh_error (*addr_mode_encoding_builder)(
297 init_context*, const fcml_st_def_instruction_desc*,
298 const fcml_st_def_addr_mode_desc*);
299
300 typedef struct operand_encoder_args {
301 part_processor_phase phase;
302 encoding_context *context;
303 const fcml_st_def_addr_mode_desc *addr_mode_def;
304 fcml_st_def_decoded_addr_mode *addr_mode;
305 const fcml_st_operand *operand;
306 inst_part *operand_enc;
307 } operand_encoder_args;
308
309 /* Encodes operand to a final array of bytes. */
310 typedef fcml_ceh_error (*operand_encoder)(operand_encoder_args*);
311
312 typedef struct operand_acceptor_args {
313 struct acceptor_ctx *context;
314 const fcml_st_operand *operand;
315 const fcml_st_def_addr_mode_desc *addr_mode_desc;
316 const addr_mode_desc_details *addr_mode_details;
317 fcml_st_def_decoded_addr_mode *addr_mode;
318 inst_part *operand_enc;
319 } operand_acceptor_args;
320
321 /* Checks if given instruction operand can be encoded for currently
322 * considered instruction addressing mode. If all the operands are
323 * accepted, the instruction addressing mode can be used to encode
324 * instruction. */
325 typedef fcml_ceh_error (*operand_acceptor)(operand_acceptor_args *args);
326
327 /* Instruction part encoder arguments. */
328 typedef struct ipp_encoder_args {
329 part_processor_phase phase;
330 encoding_context *context;
331 const addr_mode_desc_details *addr_mode_details;
332 const fcml_st_def_addr_mode_desc *addr_mode_def;
333 inst_part *instruction_part;
334 fcml_ptr args;
335 } ipp_encoder_args;
336
337 typedef struct ipp_operand_wrapper_args {
338 /* This value is set to true if operand is defined for given
339 * instruction addressing mode. */
340 fcml_bool operand_defined;
341 /* Decoder operand addressing.*/
342 fcml_st_def_decoded_addr_mode *decoded_addr_mode;
343 /* Operands acceptor.*/
344 operand_acceptor operand_acceptor;
345 /* Function responsible for encoding operand.*/
346 operand_encoder operand_encoder;
347 /* Index of the operand to encode.*/
348 int operand_index;
349 /* Operand hints.*/
350 fcml_hints hints;
351 } ipp_operand_wrapper_args;
352
353 /* Instruction part encoder.
354 * After instruction part is accepted it is encoded to an array of bytes
355 * by the encoder. */
356 typedef fcml_ceh_error (*ipp_encoder)(ipp_encoder_args *args);
357
358 /* Instruction part acceptors arguments. */
359 typedef struct ipp_acceptor_args {
360 struct acceptor_ctx *context;
361 const fcml_st_instruction *instruction;
362 const addr_mode_desc_details *addr_mode_details;
363 const fcml_st_def_addr_mode_desc *addr_mode_def;
364 fcml_ptr args;
365 } ipp_acceptor_args;
366
367 /* Instruction part acceptor.
368 * Responsible for checking if corresponding instruction part can be
369 * encoded for the currently encoded instruction. */
370 typedef fcml_ceh_error (*ipp_acceptor)(ipp_acceptor_args*);
371
372 typedef struct operand_encoder_def {
373 operand_encoder encoder;
374 operand_acceptor acceptor;
375 fcml_fp_hts_instruction_hints_calculator hints_calculator;
376 } operand_encoder_def;
377
378 typedef enum ipp_type {
379 /* Instruction part processor is responsible only for verifying state,
380 * so it doesn't build anything. */
381 IPPT_VERIFIER,
382 /** Instruction part processor is responsible for building a part
383 * of an instruction. */
384 IPPT_ENCODER
385 } ipp_type;
386
387 /* Describes one instruction part processor. */
388 typedef struct ipp_desc {
389 fcml_string ipp_name;
390 ipp_encoder encoder;
391 ipp_acceptor acceptor;
392 ipp_type type;
393 fcml_ptr args;
394 fcml_fp_env_memory_free_handler args_deallocator;
395 /* True if a given instruction part processor should be also invoked
396 * for short instruction forms. */
397 fcml_bool is_short_form_supported;
398 } ipp_desc;
399
400 typedef struct ipp_factory_args {
401 fcml_uint32_t flags;
402 const fcml_st_def_instruction_desc *instruction;
403 const fcml_st_def_addr_mode_desc *addr_mode;
404 /* Pointer to instruction level hints which may
405 * be optionally set by IPP builders. */
406 fcml_hints *hints;
407 } ipp_factory_args;
408
409 /* Prepares instruction part processor for given instruction's
410 * addressing mode. */
411 typedef ipp_desc (*ipp_factory)(ipp_factory_args*, fcml_ceh_error*);
412
413 /* Chain of instruction part processors chosen for addressing mode. */
414 typedef struct ipp_chain {
415 struct ipp_chain *next_processor;
416 ipp_desc descriptor;
417 } ipp_chain;
418
419 /* Consists all information needed to encode given
420 * instruction addressing mode. */
421 typedef struct addr_mode_encoding {
422 /* Instruction code. */
423 fcml_en_instruction instruction;
424 /* Mnemonic configuration chosen for given addressing mode.*/
425 const fcml_st_mp_mnemonic *mnemonic;
426 /* Instruction definition.*/
427 const fcml_st_def_addr_mode_desc *addr_mode_desc;
428 /* Some pre-calculated information about instruction addressing mode.
429 * This structure can be helpful, because some details of the addressing
430 * mode can be pre-calculate while addressing mode is processed in
431 * initialization mode. So we can get some performance improvements.
432 */
433 addr_mode_desc_details addr_mode_details;
434 /* Chain of instruction part processors registered for
435 * instruction addressing mode.
436 */
437 ipp_chain *ipp_chain;
438 /* Number of instruction parts needed to assemble described instruction. */
439 fcml_int instruction_parts;
440 /* Addressing mode related hints. */
441 fcml_hints hints;
442 /* True if details are cloned for alternative mnemonics. */
443 fcml_bool is_cloned;
444 } addr_mode_encoding;
445
446 typedef struct ipp_chain_builder_out {
447 /* Generated instruction part processors chain for given
448 * instruction's addressing mode. */
449 ipp_chain *ipp_chain;
450 /* Instruction level addressing mode related hints. */
451 fcml_hints hints;
452 } ipp_chain_builder_out;
453
454 /* Builds a chain of instruction part processors responsible for
455 * encoding given instruction's addressing mode. */
456 typedef fcml_ceh_error (*ipp_chain_builder)(const fcml_st_def_addr_mode_desc*,
457 const fcml_st_def_instruction_desc*, ipp_chain_builder_out *out);
458
459 /** Used by addressing modes encoding builders to enrich encoders. */
460 typedef void (*addr_mode_encoding_enricher)(addr_mode_encoding*,
461 fcml_ptr args);
462
463 static void free_instruction_addr_modes(
464 fcml_st_instruction_addr_modes *addr_modes,
465 fcml_st_dialect_context_int *dialect_context);
466
467 static inline fcml_bool is_short_form(const fcml_st_instruction *instruction,
468 const fcml_st_mp_mnemonic *mnemonic);
469
470 /* Hints optimizer not to try another addressing mode but
471 * to break processing immediately. It can be used in
472 * case of incorrect instruction which would fail anyway
473 * no matter what configuration was used by the optimizer.
474 */
475 static void break_optimization(encoding_context *context);
476
477 /* *************************/
478 /* DEBUG related functions.*/
479 /* *************************/
480
481 #ifdef FCML_DEBUG
482
483 static fcml_char print_data_attr_flags_buff[64];
484
485 static fcml_string debug_print_attr_size_flags(fcml_flags flags) {
486 int index = 0;
487 if(flags == FCML_EN_ASF_ANY) {
488 sprintf(print_data_attr_flags_buff, "ANY");
489 } else {
490 if(flags & FCML_EN_ASF_16) {
491 index += sprintf(print_data_attr_flags_buff, "2");
492 }
493 if(flags & FCML_EN_ASF_32) {
494 index += sprintf(print_data_attr_flags_buff + index, "4");
495 }
496 if(flags & FCML_EN_ASF_64) {
497 index += sprintf(print_data_attr_flags_buff + index, "8");
498 }
499 }
500 return print_data_attr_flags_buff;
501 }
502
503 static fcml_char print_data_flags_buff[512];
504
505 static fcml_string debug_print_size_flags(
506 fcml_st_asm_optimizer_processing_details *size_flags) {
507
508 int index = 0;
509
510 /* Vector length. */
511 if(size_flags->vector_length) {
512 index += sprintf(print_data_flags_buff, "Vector length: %d, ",
513 size_flags->vector_length);
514 } else {
515 index += sprintf(print_data_flags_buff, "L: NS, ");
516 }
517
518 /* Allowed EOSA.*/
519 if(size_flags->allowed_eosa.is_set) {
520 index += sprintf(print_data_flags_buff + index,
521 "Allowed OSA: [%s], ",
522 debug_print_attr_size_flags(
523 size_flags->allowed_eosa.flags));
524 } else {
525 index += sprintf(print_data_flags_buff + index,
526 "Allowed OSA: NS, ");
527 }
528
529 /* Allowed EASA.*/
530 if(size_flags->allowed_easa.is_set) {
531 index += sprintf(print_data_flags_buff + index,
532 "Allowed ASA: [%s], ", debug_print_attr_size_flags(
533 size_flags->allowed_easa.flags));
534 } else {
535 index += sprintf(print_data_flags_buff + index,
536 "Allowed ASA: NS, ");
537 }
538
539 /* Chosen OSA.*/
540 index += sprintf(print_data_flags_buff + index, "EOSA: %d, ",
541 size_flags->eosa);
542
543 /* Chosen OSA.*/
544 index += sprintf(print_data_flags_buff + index, "EASA: %d",
545 size_flags->easa);
546
547 return print_data_flags_buff;
548 }
549
550 #endif
551
552 /***********************************/
553 /* Error handling helper functions */
554 /***********************************/
555
556 static void move_errors(struct error_container *destination,
557 struct error_container *source) {
558 fcml_fn_ceh_move_errors(&destination->errors, &source->errors);
559 }
560
561 static void free_errors(struct error_container *errors) {
562 if (errors) {
563 fcml_fn_ceh_free_errors_only(&errors->errors);
564 }
565 }
566
567 static fcml_bool has_errors(struct error_container *errors) {
568 return errors != NULL && errors->errors.errors != NULL;
569 }
570
571 static void add_error(struct error_container *errors, fcml_string message,
572 fcml_ceh_error error_code) {
573 if (errors->enabled) {
574 fcml_bool add_error = FCML_TRUE;
575 fcml_st_ceh_error_info *current = errors->errors.errors;
576 // Only one error message is currently allowed.
577 while (current) {
578 if (current->code == FCML_EN_CEH_EL_ERROR) {
579 add_error = FCML_FALSE;
580 break;
581 }
582 current = current->next_error;
583 }
584 if (add_error) {
585 fcml_fn_ceh_add_error(&errors->errors, message, error_code,
586 FCML_EN_CEH_EL_ERROR);
587 }
588 }
589 }
590
591 enum fcml_ien_asm_comparator_type {
592 FCML_IEN_CT_EQUAL, FCML_IEN_CT_EQUAL_OR_LESS,
593 };
594
595 static void clean_context(encoding_context *context) {
596 fcml_fn_env_memory_clear(&(context->optimizer_processing_details),
597 sizeof(fcml_st_asm_optimizer_processing_details));
598 /* Clears segment override set by acceptor functions.*/
599 context->segment_override.reg = 0;
600 context->segment_override.size = FCML_DS_UNDEF;
601 context->segment_override.type = FCML_REG_UNDEFINED;
602 context->segment_override.x64_exp = FCML_FALSE;
603 }
604
605 static fcml_st_memory_stream inst_part_stream(inst_part *inst_part) {
606 fcml_st_memory_stream stream;
607 stream.base_address = &(inst_part->code);
608 stream.offset = 0;
609 stream.size = sizeof(inst_part->code);
610 return stream;
611 }
612
613 static fcml_usize get_effective_address_size(encoding_context *context) {
614 if (context->optimizer_processing_details.easa) {
615 return context->optimizer_processing_details.easa;
616 }
617 return context->assembler_context->entry_point.address_size_attribute;
618 }
619
620 static fcml_usize calculate_operand_size(const fcml_st_instruction *instruction,
621 memory_data_size_calculator ds_calculator,
622 fcml_ptr ds_calculator_args, fcml_usize size_operator,
623 fcml_uint8_t encoded_size_operator) {
624 /* Remember, some addressing modes use registers that do not match
625 * memory data size. These addressing modes can accept unknown operand
626 * size so we do not calculate size operators for them, because such
627 * calculated size wouldn't match data size anyway.
628 */
629 if (size_operator == FCML_OS_UNDEFINED
630 && !FCML_IS_EOS_OPT(encoded_size_operator)) {
631 if (ds_calculator) {
632 size_operator = ds_calculator(instruction, ds_calculator_args);
633 }
634 }
635 return size_operator;
636 }
637
638 static void handle_segment_override(operand_encoder_args *args) {
639 const fcml_st_operand *operand = args->operand;
640
641 const fcml_st_register *base = &(operand->address.effective_address.base);
642 const fcml_st_segment_selector *segment_selector =
643 &(operand->address.segment_selector);
644 const fcml_st_register *segment_register =
645 &(segment_selector->segment_selector);
646
647 if (segment_register->type == FCML_REG_SEG) {
648 if (segment_register->type == FCML_REG_SEG) {
649 if (!(args->addr_mode_def->instruction_group & FCML_AMT_BRANCH)
650 && !(base->type == FCML_REG_GPR
651 && (base->reg == FCML_REG_BP
652 || base->reg == FCML_REG_SP))) {
653 if (segment_register->reg != FCML_REG_DS) {
654 args->context->segment_override = *segment_register;
655 }
656 }
657 }
658 }
659 }
660
661 static fcml_usize non_standard_attribute_size_calculator(
662 const fcml_st_entry_point *entry_point, fcml_usize operand_size,
663 fcml_usize l, fcml_usize u, fcml_ceh_error *error) {
664 fcml_usize attribute_size = 0;
665 if (operand_size == l) {
666 attribute_size = 16;
667 } else if (operand_size == u) {
668 if (entry_point->operand_size_attribute == FCML_DS_16) {
669 FCML_TRACE_MSG("Addressing mode not available for 16 bit EOSA.");
670 *error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
671 } else {
672 attribute_size = entry_point->operand_size_attribute;
673 }
674 }
675 return attribute_size;
676 }
677
678 struct operand_size_decoder_args {
679 fcml_uint8_t encoded_operand_size;
680 fcml_usize operand_size;
681 fcml_usize element_size;
682 fcml_nuint8_t bcast;
683 enum fcml_ien_asm_comparator_type comparator;
684 };
685
686 static fcml_ceh_error decode_encoded_static_operand_size(
687 const struct operand_size_decoder_args args,
688 fcml_usize *encoded_data_size) {
689
690 fcml_usize operand_size = args.operand_size;
691 fcml_uint8_t encoded_operand_size = FCML_GET_OS(args.encoded_operand_size);
692
693 if (operand_size || !FCML_IS_EOS_OPT(args.encoded_operand_size)) {
694 if (args.comparator == FCML_IEN_CT_EQUAL) {
695 if (args.bcast.is_not_null) {
696 // Memory operand size in case of broadcast is always same
697 // as element size (input size) so the real memory operand
698 // size is multiplication of the element size and broadcast
699 // multiplier.
700 if (operand_size != args.element_size) {
701 FCML_TRACE("Unsupported broadcast operand size. " \
702 "Expected %d got %d.", operand_size,
703 encoded_operand_size * 8);
704 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
705 } else if (args.bcast.value * args.element_size !=
706 encoded_operand_size * 8) {
707 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
708 }
709 } else if (encoded_operand_size * 8 != operand_size) {
710 FCML_TRACE("Unsupported operand size. Expected %d got %d.",
711 operand_size, encoded_operand_size * 8);
712 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
713 }
714 }
715
716 if (encoded_operand_size * 8 < operand_size) {
717 FCML_TRACE("Unsupported operand size. Expected greater " \
718 "or equal to %d got %d.", operand_size,
719 encoded_operand_size * 8);
720 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
721 }
722
723 if (encoded_data_size != NULL) {
724 *encoded_data_size = encoded_operand_size * 8;
725 }
726 }
727
728 return FCML_CEH_GEC_NO_ERROR;
729 }
730
731 static fcml_ceh_error decode_encoded_operand_size(encoding_context *context,
732 const struct operand_size_decoder_args args,
733 fcml_usize *encoded_data_size) {
734
735 fcml_uint8_t encoded_operand_size = FCML_GET_OS(args.encoded_operand_size);
736 fcml_usize operand_size = args.operand_size;
737
738 if ((operand_size == FCML_OS_UNDEFINED)
739 && FCML_IS_EOS_OPT(args.encoded_operand_size)) {
740 /* Operand size is optional and does not have to be specified for
741 * the addressing mode. */
742 return FCML_CEH_GEC_NO_ERROR;
743 }
744
745 if (encoded_operand_size == FCML_EOS_UNDEFINED) {
746 return FCML_CEH_GEC_NO_ERROR;
747 }
748
749 if (encoded_data_size != NULL) {
750 *encoded_data_size = args.operand_size;
751 }
752
753 if (!FCML_IS_EOS_DYNAMIC(encoded_operand_size)) {
754 return decode_encoded_static_operand_size(args, encoded_data_size);
755 }
756
757 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
758 fcml_usize effective_address_size = FCML_DS_UNDEF;
759 fcml_usize effective_operand_size = FCML_DS_UNDEF;
760 fcml_usize vector_length;
761
762 switch (encoded_operand_size) {
763 case FCML_EOS_UNDEFINED:
764 break;
765 case FCML_EOS_L:
766 vector_length = (args.bcast.is_not_null) ?
767 args.bcast.value * args.element_size : operand_size;
768 if (!fcml_fn_utils_validate_vector_len(vector_length)) {
769 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
770 }
771 /* If size is not set yet it sets it to the calculated
772 * value, but if it's already set it only checks if the newly
773 * calculated one has the same size.
774 */
775 if (vector_length) {
776 fcml_st_asm_optimizer_processing_details *flags =
777 &(context->optimizer_processing_details);
778 if (flags->vector_length && flags->vector_length != vector_length) {
779 /* All dynamic operands have to be of the same size.*/
780 FCML_TRACE("Vector length differs expected %d got %d.",
781 flags->vector_length, vector_length);
782 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
783 } else {
784 flags->vector_length = vector_length;
785 }
786 }
787 break;
788 case FCML_EOS_EASA:
789 if (operand_size == FCML_DS_16 || operand_size == FCML_DS_32
790 || operand_size == FCML_DS_64) {
791 effective_address_size = operand_size;
792 } else {
793 FCML_TRACE_MSG("Unsupported encoded EOSA size.");
794 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
795 }
796 break;
797 case FCML_EOS_EOSA:
798 if (operand_size == FCML_DS_16 || operand_size == FCML_DS_32
799 || operand_size == FCML_DS_64) {
800 effective_operand_size = operand_size;
801 } else {
802 FCML_TRACE_MSG("Unsupported encoded EASA size.");
803 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
804 }
805 break;
806 case FCML_EOS_14_28:
807 effective_operand_size = non_standard_attribute_size_calculator(
808 &(context->assembler_context->entry_point),
809 operand_size, 14 * 8, 28 * 8, &error);
810 if (!error)
811 return error;
812 break;
813 case FCML_EOS_32_64:
814 effective_operand_size = non_standard_attribute_size_calculator(
815 &(context->assembler_context->entry_point),
816 operand_size, 16 * 8, 32 * 8, &error);
817 if (!error)
818 return error;
819 break;
820 case FCML_EOS_94_108:
821 effective_operand_size = non_standard_attribute_size_calculator(
822 &(context->assembler_context->entry_point),
823 operand_size, 94 * 8, 108 * 8, &error);
824 if (!error)
825 return error;
826 break;
827 case FCML_EOS_FPI:
828 case FCML_EOS_FP:
829 /* Far pointer indirect.*/
830 effective_operand_size = operand_size - FCML_DS_16;
831 break;
832 default:
833 FCML_TRACE_MSG("Unsupported encoded operand size.");
834 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
835 }
836
837 return error;
838 }
839
840 /**
841 * Sets new vector length. If there is one already set it cannot be overridden.
842 */
843 static fcml_bool set_vector_length(fcml_usize *dest_vector_length,
844 fcml_flags vector_length) {
845 fcml_bool result = FCML_TRUE;
846 if (vector_length != FCML_DS_UNDEF) {
847 if (*dest_vector_length) {
848 if (*dest_vector_length != vector_length) {
849 return FCML_FALSE;
850 }
851 } else {
852 *dest_vector_length = vector_length;
853 }
854 }
855 return result;
856 }
857
858 /**
859 * Sets size flags that are needed to assemble processed instruction.
860 * This function sets new set of flags or reduces existing set.
861 */
862 static fcml_bool set_size_flags(fcml_st_nullable_size_flags *flags,
863 fcml_flags allowed_flags) {
864 /* If there are flags already set, just reduce them. */
865 if (flags->is_set) {
866 /* Common set of existing flags and the new ones. */
867 fcml_flags reduced_flags = flags->flags & allowed_flags;
868 if (!reduced_flags) {
869 /* At least one allowed ASA size has to be left after
870 * masking existing flags.
871 */
872 return FCML_FALSE;
873 }
874 /* Set new set of flags, take into account that these might be a
875 * subset of existing ones.
876 */
877 flags->flags = reduced_flags;
878 } else {
879 flags->flags = allowed_flags;
880 flags->is_set = FCML_TRUE;
881 }
882 return FCML_TRUE;
883 }
884
885 static inline fcml_flags allowed_flags_for_op_mode(const fcml_st_entry_point *entry_point) {
886 switch (entry_point->op_mode) {
887 case FCML_OM_64_BIT:
888 return FCML_EN_ASF_ALL;
889 case FCML_OM_32_BIT:
890 return FCML_EN_ASF_16 | FCML_EN_ASF_32;
891 case FCML_OM_16_BIT:
892 return FCML_EN_ASF_16;
893 }
894 return FCML_EN_ASF_ANY;
895 }
896
897 static fcml_bool accept_data_size(struct acceptor_ctx *context,
898 const fcml_st_def_addr_mode_desc *addr_mode_desc,
899 fcml_uint8_t encoded_operand_size,
900 fcml_usize operand_size, enum fcml_ien_asm_comparator_type comparator) {
901
902 const fcml_st_entry_point *entry_point = context->entry_point;
903 struct acceptor_optimizer_ctx *optimizer = &(context->optimizer_ctx);
904
905 fcml_flags osa_flags = FCML_EN_ASF_ANY;
906 fcml_flags asa_flags = FCML_EN_ASF_ANY;
907
908 /* True if allowed addressing modes have been accepted. */
909 fcml_bool accepted = FCML_TRUE;
910
911 if ((operand_size == FCML_OS_UNDEFINED) &&
912 FCML_IS_EOS_OPT(encoded_operand_size)) {
913 /* If operand size is optional, we do not need to accept it.*/
914 return FCML_TRUE;
915 }
916
917 switch (FCML_GET_OS(encoded_operand_size)) {
918 case FCML_EOS_UNDEFINED:
919 break;
920 case FCML_EOS_L:
921 if (FCML_DEF_PREFIX_L_IGNORE_OS(addr_mode_desc->allowed_prefixes)) {
922 accepted = (operand_size == entry_point->operand_size_attribute);
923 } else {
924 accepted = ((operand_size == FCML_DS_128) ||
925 (operand_size == FCML_DS_256) ||
926 (operand_size == FCML_DS_512));
927 }
928 break;
929 case FCML_EOS_EASA:
930 if (entry_point->op_mode == FCML_OM_32_BIT) {
931 accepted = ((operand_size == FCML_DS_16) ||
932 (operand_size == FCML_DS_32));
933 } else {
934 accepted = ((operand_size == FCML_DS_32) ||
935 (operand_size == FCML_DS_64));
936 }
937 if (accepted) {
938 asa_flags = (operand_size / FCML_ASM_FCF);
939 }
940 break;
941 case FCML_EOS_EOSA:
942 if (entry_point->op_mode == FCML_OM_32_BIT) {
943 accepted = ((operand_size == FCML_DS_16) ||
944 (operand_size == FCML_DS_32));
945 } else {
946 accepted = ((operand_size == FCML_DS_16) ||
947 (operand_size == FCML_DS_32) ||
948 (operand_size == FCML_DS_64));
949 }
950 if (accepted) {
951 osa_flags = (operand_size / FCML_ASM_FCF);
952 }
953 break;
954 case FCML_EOS_14_28:
955 if (operand_size == 14 * 8) {
956 osa_flags |= FCML_EN_ASF_16;
957 } else if (operand_size == 28 * 8) {
958 osa_flags |= (FCML_EN_ASF_32 | FCML_EN_ASF_64);
959 } else {
960 accepted = FCML_FALSE;
961 }
962 break;
963 case FCML_EOS_32_64:
964 if ((operand_size == FCML_DS_16 * 2) || (FCML_DS_32 * 2) ||
965 (FCML_DS_64 * 2)) {
966 osa_flags = (operand_size / 2 / FCML_ASM_FCF);
967 } else {
968 accepted = FCML_FALSE;
969 }
970 break;
971 case FCML_EOS_94_108:
972 if (operand_size == 94 * 8) {
973 osa_flags |= FCML_EN_ASF_16;
974 } else if (operand_size == 108 * 8) {
975 osa_flags |= (FCML_EN_ASF_32 | FCML_EN_ASF_64);
976 } else {
977 accepted = FCML_FALSE;
978 }
979 break;
980 case FCML_EOS_FPI:
981 case FCML_EOS_FP:
982 /* Far pointer indirect.*/
983 switch (operand_size) {
984 case FCML_DS_16 + FCML_DS_16:
985 osa_flags |= FCML_EN_ASF_16;
986 break;
987 case FCML_DS_32 + FCML_DS_16:
988 osa_flags |= FCML_EN_ASF_32;
989 break;
990 case FCML_DS_64 + FCML_DS_16:
991 osa_flags |= FCML_EN_ASF_64;
992 break;
993 default:
994 accepted = FCML_FALSE;
995 }
996 break;
997 default:
998 if (comparator == FCML_IEN_CT_EQUAL) {
999 accepted = (FCML_GET_OS(encoded_operand_size) * 8 == operand_size);
1000 } else {
1001 accepted = (FCML_GET_OS(encoded_operand_size) * 8 >= operand_size);
1002 }
1003 if (!accepted && FCML_IS_EOS_OPT(encoded_operand_size) &&
1004 operand_size == 0) {
1005 /* Operand size is optional, so return TRUE anyway.*/
1006 accepted = FCML_TRUE;
1007 }
1008 break;
1009 }
1010
1011 if (osa_flags && !set_size_flags(&(optimizer->allowed_eosa), osa_flags)) {
1012 accepted = FCML_FALSE;
1013 }
1014
1015 if (asa_flags && !set_size_flags(&(optimizer->allowed_easa), asa_flags)) {
1016 accepted = FCML_FALSE;
1017 }
1018
1019 return accepted;
1020 }
1021
1022 /*********************************
1023 * Operand encoders.
1024 *********************************/
1025
1026 /*******************/
1027 /* Immediate value */
1028 /*******************/
1029
1030 /**
1031 * Converts an integer value being encoded to fit a destination IMM field.
1032 *
1033 * In general signed integer values are expected here, but of course user
1034 * has a freedom to provide unsigned values too. The values also do not have
1035 * to be of the same size as the IMM fields. Consequently, if size does not
1036 * match a conversion has to be carried out. It introduces an interesting
1037 * kind of problem. While the integer value is of the same size (in bytes) as
1038 * the destination IMM field or is smaller, then we can convert it without any
1039 * problems, but things get a bit more complicated when the value is stored
1040 * in a bigger field. For instance, in case when IMM field is 32-bit long and
1041 * the provided value has 64-bits. If we were sure that the provided sign was
1042 * correct we could do the needed conversions in the similar way as in the
1043 * previous case. Unfortunately it's not always the case. Consider parsing of
1044 * the following instruction:
1045 *
1046 * adc rbp,0ffffffffffffffffh
1047 *
1048 * (Disassembler may be configured to render sign-extended values)
1049 *
1050 * In such a case it's the parser that is responsible for converting the
1051 * given literal to an integer value. The parser is context less so
1052 * in case of hexadecimal values it's unable to infer the correct
1053 * sign. In this particular case the unsigned 64-bit integer is used
1054 * by the parser to represent the value while it should be considered
1055 * as signed one in this particular context - but we know that being
1056 * aware of the context. The algorithm here is a result of a trade off,
1057 * just to handle such cases correctly. Having said that, it's a subjective
1058 * decision and there is a chance that the algorithm will change in
1059 * the future.
1060 */
1061 static fcml_bool conv_int_and_set_flag(const fcml_st_integer *value,
1062 fcml_st_integer *imm, fcml_usize imm_size, fcml_usize exp_size,
1063 fcml_flags flag, fcml_flags *flags) {
1064 /* exp_size - potentially sign-extended size expected by an instruction */
1065 fcml_st_integer tmp = *value;
1066 if (!exp_size) {
1067 /* In this case immediate values are not sign-extended
1068 * so integer value size expected by an instruction in the same
1069 * as the IMM field size. */
1070 exp_size = imm_size;
1071 } else {
1072 /* Real size is specified so most likely it's an instruction
1073 * which sign-extends the integer values from IMM fields before
1074 * usage. */
1075 if (tmp.size == exp_size) {
1076 /* Sizes are the same, so it's probably the case where
1077 * disassembler rendered sign-extended value in the first
1078 * place. So with high certainty we can treat it as a
1079 * signed value. */
1080 tmp.is_signed = FCML_TRUE;
1081 }
1082 }
1083 fcml_ceh_error error = fcml_fn_utils_conv_int_to_int(&tmp, imm, exp_size,
1084 imm_size);
1085 if (!error && flags) {
1086 *flags |= flag;
1087 }
1088 return !error;
1089 }
1090
1091 /*
1092 * Checks if given integer encoded in fcml_st_integer structure fits
1093 * IMM field of given size. The rules of encoding such values
1094 * in respect to their signs and sizes are not so obvious and
1095 * to some respect subjective, so there is a risk of changes
1096 * in the future. Considering the fact that the accepting phase where
1097 * this function is used is in general a kind of optimization in order
1098 * to reject addressing modes which cannot be used to encode given instruction
1099 * as soon as possible it's not required to be 100% accurate. That is, it
1100 * cannot return false negatives but false positives are allowed because
1101 * such addressing modes will be rejected by encoders anyway.
1102 * Consequently, this implementation may return false positives in
1103 * some rare cases but it is fast and does not duplicate the logic
1104 * from encoding phase. It just rejects addressing modes which
1105 * are explicitly incorrect for given values.
1106 */
1107 static fcml_bool int_fit_imm(const fcml_st_integer *value,
1108 fcml_usize imm_size) {
1109 fcml_uint64_t i64;
1110 fcml_uint32_t i32;
1111 fcml_uint16_t i16;
1112 /* Values which occupies less or same number of bytes that IMM
1113 * value always fits. */
1114 if (value->size > imm_size) {
1115 switch (imm_size) {
1116 case FCML_DS_8:
1117 switch (value->size) {
1118 case FCML_DS_64:
1119 i64 = (fcml_uint64_t)(value->int64 & 0xFFFFFFFFFFFFFF00UL);
1120 return i64 == 0x0UL || i64 == 0xFFFFFFFFFFFFFF00UL;
1121 case FCML_DS_32:
1122 i32 = (fcml_uint32_t)(value->int32 & 0xFFFFFF00);
1123 return i32 == 0 || i32 == 0xFFFFFF00;
1124 case FCML_DS_16:
1125 i16 = (fcml_uint16_t)(value->int16 & 0xFF00);
1126 return i16 == 0 || i16 == 0xFF00;
1127 }
1128 break;
1129 case FCML_DS_16:
1130 switch (value->size) {
1131 case FCML_DS_64:
1132 i64 = (fcml_uint64_t)(value->int64 & 0xFFFFFFFFFFFF0000UL);
1133 return i64 == 0x0UL || i64 == 0xFFFFFFFFFFFF0000UL;
1134 case FCML_DS_32:
1135 i32 = (fcml_uint32_t)(value->int32 & 0xFFFF0000);
1136 return i32 == 0 || i32 == 0xFFFF0000;
1137 }
1138 break;
1139 case FCML_DS_32:
1140 if (value->size == FCML_DS_64) {
1141 i64 = (fcml_uint64_t)(value->int64 & 0xFFFFFFFF00000000UL);
1142 return i64 == 0x0UL || i64 == 0xFFFFFFFF00000000UL;
1143 }
1144 break;
1145 }
1146 }
1147 return FCML_TRUE;
1148 }
1149
1150 static fcml_ceh_error operand_acceptor_imm(operand_acceptor_args *args) {
1151 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1152 struct acceptor_ctx *context = args->context;
1153 const fcml_st_operand *operand = args->operand;
1154
1155 if (operand->type == FCML_OT_IMMEDIATE) {
1156 const fcml_st_def_tma_imm *addr_mode_args =
1157 (fcml_st_def_tma_imm*)args->addr_mode->addr_mode_args;
1158 const fcml_st_integer *imm = &(operand->immediate);
1159
1160 /* EOSA flags potentially allowed for given addressing mode. */
1161 fcml_flags accepted_flags = 0;
1162
1163 fcml_flags flags = context->optimizer_ctx.allowed_eosa.flags;
1164 if (!flags) {
1165 flags = allowed_flags_for_op_mode(context->entry_point);
1166 }
1167
1168 fcml_uint8_t imm_size = addr_mode_args->encoded_size;
1169 fcml_bool fits = FCML_FALSE;
1170
1171 /* At this point some EASA flags may be already rejected, we have to
1172 * take it into account and check these which are still valid.
1173 */
1174 if (imm_size != FCML_EOS_EOSA) {
1175 fits = int_fit_imm(imm, FCML_GET_OS(imm_size) * 8);
1176 } else if (imm_size == FCML_EOS_EOSA) {
1177 if ((flags & FCML_EN_ASF_16) && int_fit_imm(imm, FCML_DS_16)) {
1178 accepted_flags |= FCML_EN_ASF_16;
1179 fits |= FCML_TRUE;
1180 }
1181 if ((flags & FCML_EN_ASF_32) && int_fit_imm(imm, FCML_DS_32)) {
1182 accepted_flags |= FCML_EN_ASF_32;
1183 fits |= FCML_TRUE;
1184 }
1185 if ((flags & FCML_EN_ASF_64) && (int_fit_imm(imm, FCML_DS_32)
1186 || addr_mode_args->is_64bit_imm_allowed)) {
1187 accepted_flags |= FCML_EN_ASF_64;
1188 fits |= FCML_TRUE;
1189 }
1190 }
1191
1192 if (!fits || (accepted_flags && !set_size_flags(
1193 &(context->optimizer_ctx.allowed_eosa), accepted_flags))) {
1194 FCML_TRACE_MSG("Accept IMM: Can not accept IMM value.");
1195 error = FCML_CEH_GEC_INVALID_OPPERAND;
1196 }
1197
1198 return error;
1199 } else {
1200 error = FCML_CEH_GEC_INVALID_OPPERAND;
1201 }
1202
1203 return error;
1204 }
1205
1206 static fcml_ceh_error operand_encoder_imm(operand_encoder_args *args) {
1207 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1208
1209 if (args->phase == IPPP_FIRST_PHASE) {
1210 fcml_st_def_tma_imm *addr_mode_args =
1211 (fcml_st_def_tma_imm*)args->addr_mode->addr_mode_args;
1212 /* Encoded size of IMM field. */
1213 fcml_uint8_t enc_imm_size = addr_mode_args->encoded_size;
1214 /* Encoded size of the value while it's interpreted by an instruction.
1215 * In some cases instruction extends the value encoded
1216 * in IMM field to larger values. For instance an 8-bit integer
1217 * value may be sign extended to 32-bites before its use. */
1218 fcml_uint8_t enc_ext_size = addr_mode_args->encoded_ex_size;
1219 fcml_usize eosa = args->context->optimizer_processing_details.eosa;
1220
1221 /* Remember that only one addressing mode of MOV instruction for
1222 * 64 bits mode can encode IMM64. */
1223 fcml_usize imm_size_for_eosa = eosa;
1224 if (!addr_mode_args->is_64bit_imm_allowed && eosa == FCML_DS_64) {
1225 imm_size_for_eosa = FCML_DS_32;
1226 }
1227
1228 if ((enc_imm_size == FCML_EOS_EOSA || enc_ext_size == FCML_EOS_EOSA)
1229 && !eosa) {
1230 FCML_TRACE_MSG("EOSA size not set! Processing failed.");
1231 return FCML_CEH_GEC_INTERNAL_ERROR;
1232 }
1233
1234 fcml_bool fits = FCML_FALSE; /* True if offset fits IMM field. */
1235 const fcml_st_integer *imm = &(args->operand->immediate);
1236
1237 fcml_usize imm_size = enc_imm_size == FCML_EOS_EOSA ?
1238 imm_size_for_eosa : FCML_GET_OS(enc_imm_size) * 8;
1239 fcml_usize operand_size = enc_ext_size == FCML_EOS_EOSA ?
1240 eosa : FCML_GET_OS(enc_ext_size) * 8;
1241
1242 fcml_st_integer dest_imm;
1243 fits = conv_int_and_set_flag(imm, &dest_imm, imm_size, operand_size, 0, NULL);
1244 if (fits) {
1245 inst_part *operand_enc = args->operand_enc;
1246 fcml_st_memory_stream stream = fcml_fn_stream_init(
1247 &(operand_enc->code), 0, sizeof(operand_enc->code));
1248 error = fcml_fn_utils_encode_integer(&stream, &dest_imm);
1249 if (!error) {
1250 operand_enc->code_length = stream.offset;
1251 }
1252 } else {
1253 error = FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
1254 }
1255 }
1256
1257 return error;
1258 }
1259
1260 /*********************/
1261 /* Explicit register */
1262 /*********************/
1263
1264 static fcml_ceh_error operand_acceptor_explicit_reg(
1265 operand_acceptor_args *args) {
1266 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1267 fcml_st_def_tma_explicit_reg *addr_mode_args =
1268 (fcml_st_def_tma_explicit_reg*)args->addr_mode->addr_mode_args;
1269 const fcml_st_operand *operand = args->operand;
1270
1271 if (operand->type != FCML_OT_REGISTER
1272 || operand->reg.type != addr_mode_args->reg_type
1273 || operand->reg.reg != addr_mode_args->reg_num
1274 || !accept_data_size(args->context, args->addr_mode_desc,
1275 addr_mode_args->encoded_reg_size,
1276 operand->reg.size, FCML_IEN_CT_EQUAL)) {
1277 error = FCML_CEH_GEC_INVALID_OPPERAND;
1278 }
1279
1280 return error;
1281 }
1282
1283 /* Example: FCML_OP_EXPLICIT_REG(FCML_REG_GPR, FCML_REG_AL, FCML_EOS_BYTE)*/
1284 static fcml_ceh_error operand_encoder_explicit_reg(operand_encoder_args *args) {
1285 if (args->phase == IPPP_FIRST_PHASE) {
1286 fcml_st_def_tma_explicit_reg *addr_mode_args =
1287 (fcml_st_def_tma_explicit_reg*) args->addr_mode->addr_mode_args;
1288 /* Encode IMM.*/
1289 fcml_usize encoded_size;
1290 struct operand_size_decoder_args dargs = {0};
1291 dargs.encoded_operand_size = addr_mode_args->encoded_reg_size;
1292 dargs.operand_size = args->operand->reg.size;
1293 dargs.comparator = FCML_IEN_CT_EQUAL;
1294 return decode_encoded_operand_size(args->context, dargs, &encoded_size);
1295 }
1296 return FCML_CEH_GEC_NO_ERROR;
1297 }
1298
1299 /*******************/
1300 /* Opcode register */
1301 /*******************/
1302
1303 static fcml_ceh_error operand_acceptor_opcode_reg(operand_acceptor_args *args) {
1304
1305 const fcml_st_operand *operand = args->operand;
1306 fcml_st_def_tma_opcode_reg *addr_mode_args =
1307 (fcml_st_def_tma_opcode_reg*) args->addr_mode->addr_mode_args;
1308
1309 if (operand->type != FCML_OT_REGISTER
1310 || operand->reg.type != addr_mode_args->reg_type) {
1311 return FCML_CEH_GEC_INVALID_OPPERAND;
1312 }
1313
1314 if (!accept_data_size(args->context, args->addr_mode_desc,
1315 addr_mode_args->encoded_reg_size,
1316 operand->reg.size, FCML_IEN_CT_EQUAL)) {
1317 return FCML_CEH_GEC_INVALID_OPPERAND;
1318 }
1319
1320 return FCML_CEH_GEC_NO_ERROR;
1321 }
1322
1323 static fcml_ceh_error operand_encoder_opcode_reg(operand_encoder_args *args) {
1324 const fcml_st_operand *operand = args->operand;
1325 encoding_context *context = args->context;
1326 if (args->phase == IPPP_FIRST_PHASE) {
1327 if (operand->reg.reg > 7) {
1328 context->opcode_reg.opcode_reg = operand->reg.reg - 8;
1329 context->opcode_reg.ext_b = FCML_TRUE;
1330 } else {
1331 context->opcode_reg.opcode_reg = operand->reg.reg;
1332 }
1333 if (context->assembler_context->entry_point.op_mode == FCML_OM_64_BIT) {
1334 context->reg_opcode_needs_rex = operand->reg.x64_exp;
1335 }
1336 }
1337 return FCML_CEH_GEC_NO_ERROR;
1338 }
1339
1340 /***********************************/
1341 /* Immediate relative displacement */
1342 /***********************************/
1343
1344 static fcml_ceh_error operand_acceptor_immediate_dis_relative(
1345 operand_acceptor_args *args) {
1346 /* In 64-bit mode OSA is fixed at 64 bits. Just in case,
1347 * because addressing mode acceptor is responsible for interpreting
1348 * FCML_DEF_OPCODE_FLAGS_FORCE_64BITS_EOSA flag and forcing appropriate
1349 * OSA size. */
1350 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1351 struct acceptor_ctx *context = args->context;
1352 const fcml_st_operand *operand = args->operand;
1353 fcml_bool is_offset = (operand->type == FCML_OT_ADDRESS
1354 && operand->address.address_form == FCML_AF_OFFSET);
1355 fcml_bool is_imm = (operand->type == FCML_OT_IMMEDIATE);
1356
1357 if (is_imm || is_offset) {
1358 fcml_flags flags = 0;
1359 if (context->entry_point->op_mode == FCML_OM_64_BIT) {
1360 flags = FCML_EN_ASF_64;
1361 } else {
1362 fcml_sf_def_tma_immediate_dis_relative *rel_args =
1363 (fcml_sf_def_tma_immediate_dis_relative*)
1364 args->addr_mode->addr_mode_args;
1365 if (is_imm || (rel_args->encoded_size != FCML_EOS_UNDEFINED)) {
1366 /* In case of IMM operand size operator is not available, so we
1367 * have to try both operand size attributes.
1368 */
1369 flags = FCML_EN_ASF_32 | FCML_EN_ASF_16;
1370 } else {
1371 fcml_usize size_operator = operand->address.size_operator;
1372 switch (size_operator) {
1373 case FCML_DS_UNDEF:
1374 flags = FCML_EN_ASF_32 | FCML_EN_ASF_16;
1375 break;
1376 case FCML_DS_16:
1377 flags = FCML_EN_ASF_16;
1378 break;
1379 case FCML_DS_32:
1380 flags = FCML_EN_ASF_32;
1381 break;
1382 case FCML_DS_64:
1383 /* We are not in 64-bit mode. */
1384 error = FCML_CEH_GEC_INVALID_OPPERAND;
1385 break;
1386 }
1387 }
1388 }
1389 if (!set_size_flags(&(context->optimizer_ctx.allowed_eosa), flags)) {
1390 error = FCML_CEH_GEC_INVALID_OPPERAND;
1391 }
1392 } else {
1393 error = FCML_CEH_GEC_INVALID_OPPERAND;
1394 }
1395
1396 return error;
1397 }
1398
1399 static fcml_ceh_error convert_to_rel(fcml_st_integer *value,
1400 fcml_usize expected_size, fcml_uint8_t encoded_rel_size) {
1401 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1402 if (encoded_rel_size == FCML_EOS_BYTE) {
1403 error = fcml_fn_utils_conv_int_to_int(value, value,
1404 expected_size, FCML_DS_8);
1405 if (error) {
1406 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1407 }
1408 }
1409 return error;
1410 }
1411
1412 static int disp_size_for_enc_rel(fcml_usize osa, fcml_uint8_t rel_size) {
1413 if (rel_size == FCML_EOS_UNDEFINED) {
1414 return (osa == FCML_DS_64) ? 4 : osa / 8;
1415 } else {
1416 return rel_size;
1417 }
1418 }
1419
1420 /* Immediate address can be provided as an immediate operand value
1421 * or address offset. Both of them have to be converted to an integer
1422 * value for further processing.
1423 * */
1424 static fcml_ceh_error convert_operand_to_offset(
1425 const fcml_st_operand *operand, fcml_st_integer *offset,
1426 fcml_usize osa) {
1427
1428 fcml_st_integer addr = { 0 };
1429
1430 if (operand->type == FCML_OT_IMMEDIATE) {
1431 addr = operand->immediate;
1432 } else if (operand->type == FCML_OT_ADDRESS &&
1433 operand->address.address_form == FCML_AF_OFFSET) {
1434 fcml_ceh_error error = fcml_fn_utils_offset_to_integer(
1435 &(operand->address.offset), &addr);
1436 if (error) {
1437 return error;
1438 }
1439 } else {
1440 return FCML_CEH_GEC_INVALID_OPPERAND;
1441 }
1442
1443 if (fcml_fn_utils_conv_int_to_int(&addr, offset, osa, osa)) {
1444 return FCML_CEH_GEC_INVALID_OPPERAND;
1445 }
1446
1447 return FCML_CEH_GEC_NO_ERROR;
1448 }
1449
1450 /* It's implemented as post processor because final instruction size is
1451 * needed to calculate displacements. */
1452 static fcml_ceh_error ipp_imm_dis_rel_post_processor(
1453 encoding_context *context, inst_part *inst_part,
1454 fcml_ptr args, fcml_uint8_t enc_rel_size) {
1455
1456 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1457
1458 /* Sanity checks */
1459 if (enc_rel_size != FCML_EOS_UNDEFINED && enc_rel_size != FCML_EOS_BYTE) {
1460 FCML_TRACE_MSG("Wrong size of encoded relative address.");
1461 return FCML_CEH_GEC_INVALID_OPPERAND;
1462 } else if (FCML_IS_NULL(context->instruction_size)) {
1463 /* Should never happened.*/
1464 return FCML_CEH_GEC_INTERNAL_ERROR;
1465 }
1466
1467 /* Optimizer is responsible for setting optimal OSA value.*/
1468 fcml_usize osa = context->optimizer_processing_details.eosa;
1469
1470 fcml_st_integer offset = { 0 };
1471 error = convert_operand_to_offset((fcml_st_operand*) args, &offset, osa);
1472 if (error) {
1473 return error;
1474 }
1475
1476 fcml_st_integer disp = {0};
1477 fcml_uint8_t inst_size = context->instruction_size.value;
1478 fcml_ip ip = context->assembler_context->entry_point.ip;
1479
1480 disp.is_signed = FCML_TRUE;
1481
1482 int disp_size = disp_size_for_enc_rel(osa, enc_rel_size);
1483 switch (osa) {
1484 case FCML_DS_16: {
1485 /* There are some inconsistencies around this addressing mode
1486 * in the Intel manual. The solution applied here is the result of
1487 * checking how processors tend to interpret it in practice. In
1488 * general it's consistent with the pseudo-code for the JMP
1489 * instruction in the Intel manual. */
1490 fcml_int16_t offset16 = offset.int16;
1491 /* 16 higher bits of 32-bit EIP should be cut off here. */
1492 fcml_int32_t eip = (fcml_int32_t) (ip + inst_size + disp_size);
1493 fcml_int16_t rel16 = offset16 - (fcml_int16_t)eip;
1494 disp.int16 = rel16;
1495 disp.size = FCML_DS_16;
1496 error = convert_to_rel(&disp, FCML_DS_16, enc_rel_size);
1497 break;
1498 }
1499 case FCML_DS_32: {
1500 fcml_int32_t offset32 = offset.int32;
1501 fcml_int32_t eip = (fcml_int32_t) (ip + inst_size + disp_size);
1502 disp.int32 = offset32 - eip;
1503 disp.size = FCML_DS_32;
1504 error = convert_to_rel(&disp, FCML_DS_32, enc_rel_size);
1505 break;
1506 }
1507 case FCML_DS_64: {
1508 fcml_int64_t offset64 = offset.int64;
1509 fcml_int64_t rip = ip + inst_size + disp_size;
1510 fcml_int64_t rel = offset64 - rip;
1511 if (!safe_cast_i6432(rel)) {
1512 return FCML_CEH_GEC_INVALID_OPPERAND;
1513 }
1514 disp.int32 = (fcml_int32_t)(offset64 - rip);
1515 disp.size = FCML_DS_32;
1516 error = convert_to_rel(&disp, FCML_DS_32, enc_rel_size);
1517 break;
1518 }
1519 }
1520
1521 if (error) {
1522 if (error == FCML_CEH_GEC_INVALID_OPPERAND_SIZE &&
1523 enc_rel_size == FCML_EOS_BYTE) {
1524 /* In this case we can hint optimizer not to try different
1525 ASA/OSA combinations, because it doesn't make sense
1526 anyway. They will fail too. */
1527 break_optimization(context);
1528 }
1529 return error;
1530 }
1531
1532 /* Encode ModR/M and displacement.*/
1533 fcml_st_memory_stream stream = inst_part_stream(inst_part);
1534 fcml_fn_utils_encode_integer(&stream, &disp);
1535 inst_part->code_length = stream.offset;
1536
1537 return error;
1538 }
1539
1540 static fcml_ceh_error ipp_imm_disp_rel_post_processor_8(
1541 encoding_context *context, inst_part *instruction_part,
1542 fcml_ptr post_processor_args) {
1543 return ipp_imm_dis_rel_post_processor(context, instruction_part,
1544 post_processor_args, FCML_EOS_BYTE);
1545 }
1546
1547 static fcml_ceh_error ipp_imm_disp_rel_post_processor_undef(
1548 encoding_context *context, inst_part *instruction_part,
1549 fcml_ptr post_processor_args) {
1550 return ipp_imm_dis_rel_post_processor(context, instruction_part,
1551 post_processor_args, FCML_EOS_UNDEFINED);
1552 }
1553
1554 static fcml_ceh_error operand_encoder_imm_disp_relative(
1555 operand_encoder_args *args) {
1556
1557 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1558 inst_part *operand_enc = args->operand_enc;
1559
1560 if (args->phase == IPPP_SECOND_PHASE) {
1561 fcml_sf_def_tma_immediate_dis_relative *addr_mode_args =
1562 (fcml_sf_def_tma_immediate_dis_relative*)
1563 args->addr_mode->addr_mode_args;
1564
1565 operand_enc->code_length = 0;
1566
1567 if (addr_mode_args->encoded_size == FCML_EOS_BYTE) {
1568 operand_enc->post_processor = ipp_imm_disp_rel_post_processor_8;
1569 } else if (addr_mode_args->encoded_size == FCML_EOS_UNDEFINED) {
1570 operand_enc->post_processor = ipp_imm_disp_rel_post_processor_undef;
1571 } else {
1572 /* Unsupported operand size encoded in operand arguments.*/
1573 error = FCML_CEH_GEC_INVALID_OPPERAND;
1574 }
1575
1576 operand_enc->post_processor_args = (fcml_ptr)args->operand;
1577 }
1578
1579 return error;
1580 }
1581
1582 /***************/
1583 /* Far pointer */
1584 /***************/
1585
1586 static fcml_ceh_error operand_acceptor_far_pointer(
1587 operand_acceptor_args *args) {
1588 fcml_ceh_error result = FCML_CEH_GEC_NO_ERROR;
1589 const fcml_st_operand *operand = args->operand;
1590 struct acceptor_ctx *context = args->context;
1591 if (operand->type != FCML_OT_FAR_POINTER) {
1592 return FCML_CEH_GEC_INVALID_OPPERAND;
1593 }
1594 switch (context->entry_point->op_mode) {
1595 case FCML_OM_16_BIT:
1596 if (operand->far_pointer.offset_size == FCML_DS_16) {
1597 if (!set_size_flags(&(context->optimizer_ctx.allowed_eosa),
1598 FCML_EN_ASF_16)) {
1599 /* Size can not be used by this operand. */
1600 result = FCML_CEH_GEC_INVALID_OPPERAND;
1601 }
1602 } else {
1603 /* 32 bit offset is not supported in 16 bit mode. */
1604 result = FCML_CEH_GEC_INVALID_OPPERAND;
1605 }
1606 break;
1607 case FCML_OM_32_BIT:
1608 if (!set_size_flags(&(context->optimizer_ctx.allowed_eosa),
1609 (operand->far_pointer.offset_size == FCML_DS_16) ?
1610 (FCML_EN_ASF_16 | FCML_EN_ASF_32) : FCML_EN_ASF_32)) {
1611 /* Size can not be used by this operand. */
1612 result = FCML_CEH_GEC_INVALID_OPPERAND;
1613 }
1614 break;
1615 case FCML_OM_64_BIT:
1616 result = FCML_CEH_GEC_INVALID_OPPERAND;
1617 break;
1618 }
1619 return result;
1620 }
1621
1622 static fcml_ceh_error operand_encoder_far_pointer(operand_encoder_args *args) {
1623 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1624
1625 if (args->phase == IPPP_FIRST_PHASE) {
1626 /* Prepare stream to instruction part's buffer. */
1627 fcml_st_memory_stream stream = inst_part_stream(args->operand_enc);
1628
1629 /* Write offset.*/
1630 if (args->operand->far_pointer.offset_size == FCML_DS_16) {
1631 fcml_fn_stream_write_word(&stream, (fcml_uint16_t) args->
1632 operand->far_pointer.offset16);
1633 args->operand_enc->code_length = 4;
1634 } else {
1635 fcml_fn_stream_write_dword(&stream,(fcml_uint32_t) args->
1636 operand->far_pointer.offset32);
1637 args->operand_enc->code_length = 6;
1638 }
1639
1640 /* Write segment.*/
1641 fcml_fn_stream_write_word(&stream,
1642 args->operand->far_pointer.segment);
1643 }
1644 return error;
1645 }
1646
1647 /************************************/
1648 /* Explicit GPR register addressing */
1649 /************************************/
1650
1651 static fcml_ceh_error accept_segment_register(
1652 struct acceptor_ctx *context,
1653 const fcml_st_register *segment_register,
1654 fcml_uint8_t encoded_segment_register) {
1655
1656 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1657
1658 /* If custom segment register has been chosen, check if
1659 * it's allowed in the given context. */
1660 if (segment_register->type != FCML_REG_UNDEFINED) {
1661 if (segment_register->type == FCML_REG_SEG) {
1662 if (segment_register->reg !=
1663 FCML_SEG_GET_REG(encoded_segment_register)) {
1664 if (!(encoded_segment_register & FCML_SEG_ALLOW_OVERRIDE)) {
1665 /* Register can not be overridden. */
1666 add_error(&context->error_container,
1667 fcml_fn_msg_get_message(
1668 FCML_MC_SEGMENT_REGISTER_CAN_NOT_BE_OVERRIDDEN),
1669 FCML_CEH_MEC_ERROR_ILLEGAL_SEG_REG_OVERRIDE);
1670 error = FCML_CEH_GEC_INVALID_REGISTER;
1671 }
1672 }
1673 } else {
1674 /* Wrong register type, segment register should be used. */
1675 add_error(&context->error_container,
1676 fcml_fn_msg_get_message(
1677 FCML_MC_SEGMENT_WRONG_REGISTER_TYPE_SEG),
1678 FCML_CEH_MEC_ERROR_INVALID_REGISTER_TYPE_SEG);
1679 error = FCML_CEH_GEC_INVALID_REGISTER_TYPE;
1680 }
1681 }
1682
1683 return error;
1684 }
1685
1686 static fcml_ceh_error operand_acceptor_explicit_gps_reg_addressing(
1687 operand_acceptor_args *args) {
1688
1689 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1690 const fcml_st_operand *operand = args->operand;
1691 fcml_st_def_tma_explicit_gps_reg_addressing *addr_mode_args =
1692 (fcml_st_def_tma_explicit_gps_reg_addressing*)
1693 args->addr_mode->addr_mode_args;
1694
1695 if ((operand->type != FCML_OT_ADDRESS)
1696 || (operand->address.effective_address.base.reg !=
1697 addr_mode_args->reg_num)) {
1698 error = FCML_CEH_GEC_INVALID_OPPERAND;
1699 } else if (!accept_data_size(args->context, args->addr_mode_desc,
1700 FCML_EOS_EASA, operand->address.effective_address.base.size,
1701 FCML_IEN_CT_EQUAL)) {
1702 error = FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
1703 } else if (!accept_data_size(args->context, args->addr_mode_desc,
1704 addr_mode_args->encoded_operand_size, operand->address.size_operator,
1705 FCML_IEN_CT_EQUAL)) {
1706 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1707 } else {
1708 error = accept_segment_register(args->context,
1709 &(operand->address.segment_selector.segment_selector),
1710 addr_mode_args->encoded_segment_register);
1711 }
1712
1713 return error;
1714 }
1715
1716 static fcml_ceh_error operand_encoder_explicit_gps_reg_addressing(operand_encoder_args *args) {
1717 const fcml_st_operand *operand = args->operand;
1718
1719 /* We don't care here if the segment register is valid or not.
1720 * It's acceptor that is responsible for rejecting invalid segment
1721 * register in the first place. */
1722 if (args->phase == IPPP_FIRST_PHASE) {
1723 fcml_st_def_tma_explicit_gps_reg_addressing *addr_mode_args =
1724 (fcml_st_def_tma_explicit_gps_reg_addressing*)
1725 args->addr_mode->addr_mode_args;
1726 fcml_uint8_t encoded_segment_register = addr_mode_args->encoded_segment_register;
1727 const fcml_st_register *segment_register = &(operand->address.segment_selector.segment_selector);
1728 if (segment_register->type == FCML_REG_SEG) {
1729 if (segment_register->reg != (encoded_segment_register & 0x7F)) {
1730 if (encoded_segment_register & FCML_SEG_ALLOW_OVERRIDE) {
1731 args->context->segment_override = *segment_register;
1732 }
1733 }
1734 }
1735 }
1736 return FCML_CEH_GEC_NO_ERROR;
1737 }
1738
1739 /***************/
1740 /* Explicit IB */
1741 /***************/
1742
1743 static fcml_ceh_error operand_acceptor_explicit_ib(
1744 operand_acceptor_args *args) {
1745 fcml_ceh_error error = FCML_CEH_GEC_INVALID_OPPERAND;
1746 const fcml_st_operand *operand = args->operand;
1747 if (operand->type == FCML_OT_IMMEDIATE) {
1748 fcml_st_integer dest;
1749 /* We are interested in errors only here. */
1750 error = fcml_fn_utils_conv_int_to_int(
1751 &(operand->immediate), &dest, FCML_DS_8, FCML_DS_8);
1752 }
1753 return error;
1754 }
1755
1756 static fcml_ceh_error operand_encoder_explicit_ib(operand_encoder_args *args) {
1757 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1758 if (args->phase == IPPP_FIRST_PHASE) {
1759 error = FCML_CEH_GEC_INVALID_OPPERAND;
1760 fcml_st_def_tma_explicit_ib *exp_ib =
1761 (fcml_st_def_tma_explicit_ib*) args->addr_mode->addr_mode_args;
1762 fcml_st_integer dest;
1763 fcml_ceh_error res = fcml_fn_utils_conv_int_to_int(
1764 &(args->operand->immediate), &dest, FCML_DS_8, FCML_DS_8);
1765 if (!res) {
1766 if (exp_ib->ib == (fcml_uint8_t) dest.int8) {
1767 error = FCML_CEH_GEC_NO_ERROR;
1768 }
1769 }
1770 }
1771 return error;
1772 }
1773
1774 /***************************/
1775 /* Segment relative offset */
1776 /***************************/
1777
1778 static fcml_ceh_error operand_acceptor_segment_relative_offset(
1779 operand_acceptor_args *args) {
1780
1781 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1782
1783 const fcml_st_operand *operand = args->operand;
1784 if (operand->type == FCML_OT_ADDRESS) {
1785
1786 fcml_st_def_tma_segment_relative_offset *addr_mode_args =
1787 (fcml_st_def_tma_segment_relative_offset*)
1788 args->addr_mode->addr_mode_args;
1789
1790 const fcml_st_address *address = &(operand->address);
1791
1792 if (address->address_form != FCML_AF_OFFSET) {
1793 return FCML_CEH_GEC_INVALID_OPPERAND;
1794 }
1795
1796 struct acceptor_ctx *context = args->context;
1797 if (operand->address.size_operator != FCML_OS_UNDEFINED) {
1798 if (!accept_data_size(context, args->addr_mode_desc,
1799 addr_mode_args->encoded_operand_size,
1800 operand->address.size_operator, FCML_IEN_CT_EQUAL)) {
1801 return FCML_CEH_GEC_INVALID_OPPERAND;
1802 }
1803 }
1804
1805 fcml_bool is_convertable = FCML_FALSE;
1806 fcml_flags flags = FCML_EN_ASF_ANY;
1807 fcml_st_integer src_addr = { 0 }, conv_addr = { 0 };
1808
1809 /* Convert IMM value to address. */
1810 error = fcml_fn_utils_offset_to_integer(&(address->offset), &src_addr);
1811 if (error) {
1812 return FCML_CEH_GEC_INVALID_OPPERAND;
1813 }
1814
1815 /* Check if address can be converted to 16, 32 or 64 bits. */
1816 src_addr.is_signed = FCML_TRUE;
1817
1818 /* TODO: change to: int_fit_imm */
1819 is_convertable |= conv_int_and_set_flag(&src_addr, &conv_addr,
1820 FCML_DS_32, FCML_DS_32, FCML_EN_ASF_32, &flags);
1821 if (context->entry_point->op_mode == FCML_OM_64_BIT) {
1822 is_convertable |= conv_int_and_set_flag(&src_addr, &conv_addr,
1823 FCML_DS_64, FCML_DS_64, FCML_EN_ASF_64, &flags);
1824 } else {
1825 is_convertable |= conv_int_and_set_flag(&src_addr, &conv_addr,
1826 FCML_DS_16, FCML_DS_16, FCML_EN_ASF_16, &flags);
1827 }
1828
1829 if (!set_size_flags(&(context->optimizer_ctx.allowed_easa), flags)) {
1830 error = FCML_CEH_GEC_INVALID_OPPERAND;
1831 }
1832
1833 } else {
1834 error = FCML_CEH_GEC_INVALID_OPPERAND;
1835 }
1836
1837 return error;
1838 }
1839
1840 static fcml_ceh_error operand_encoder_segment_relative_offset(
1841 operand_encoder_args *args) {
1842 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1843
1844 if (args->phase == IPPP_FIRST_PHASE) {
1845 fcml_usize asa = args->context->optimizer_processing_details.easa;
1846
1847 if (asa != FCML_DS_UNDEF) {
1848 const fcml_st_address *address = &(args->operand->address);
1849
1850 /* Convert IMM value to address. */
1851 fcml_st_integer offset = { 0 };
1852 error = fcml_fn_utils_offset_to_integer(&(address->offset),
1853 &offset);
1854 if (!error) {
1855 /* Convert address to size indicated by ASA.*/
1856 error = fcml_fn_utils_conv_int_to_int(&offset,
1857 &offset, asa, asa);
1858 if (!error) {
1859
1860 /* Encode offset.*/
1861 fcml_st_memory_stream stream = inst_part_stream(args->operand_enc);
1862 fcml_fn_utils_encode_integer(&stream, &offset);
1863 args->operand_enc->code_length = stream.offset;
1864 }
1865 }
1866 } else {
1867 /* ASA should be set by optimizer.*/
1868 error = FCML_CEH_GEC_INVALID_OPPERAND;
1869 }
1870
1871 if (!error) {
1872 handle_segment_override(args);
1873 }
1874 }
1875 return error;
1876 }
1877
1878 /***************/
1879 /* ModR/M - rm */
1880 /***************/
1881
1882 /* TODO: Move to fcml_operand_decorators. */
1883 static fcml_ceh_error accept_bcast_decorator(fcml_bool is_bcast_supported,
1884 fcml_nuint8_t bcast, fcml_usize element_size,
1885 struct acceptor_optimizer_ctx *optimizer_ctx,
1886 fcml_uint8_t encoded_memory_operand_size) {
1887
1888 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1889
1890 if (!bcast.is_not_null || !is_bcast_supported) {
1891 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
1892 }
1893
1894 fcml_usize noe = bcast.value;
1895
1896 /* Check if broadcast size is correct. */
1897 if (noe != 2 && noe != 4 && noe != 8 && noe != 16 && noe != 32
1898 && noe != 64) {
1899 return FCML_CEH_GEC_INVALID_OPERAND_DECORATOR;
1900 }
1901
1902 fcml_usize bcast_accessed_mem_size = noe * element_size;
1903 fcml_uint8_t encoded_static_operand_size =
1904 FCML_GET_OS(encoded_memory_operand_size);
1905
1906 switch (encoded_static_operand_size) {
1907 case FCML_EOS_L: {
1908 /* Force vector length basing on broadcast element size and number
1909 * of such elements. */
1910 fcml_usize vector_length = bcast_accessed_mem_size;
1911 if (!set_vector_length(&(optimizer_ctx->vector_length),
1912 vector_length)) {
1913 FCML_TRACE("Vector length differs expected %d got %d.",
1914 vector_length, optimizer_ctx->vector_length);
1915 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1916 }
1917 break;
1918 }
1919 default:
1920 /* Take into account only directly encoded sizes. */
1921 if (!FCML_IS_EOS_DYNAMIC(encoded_memory_operand_size)) {
1922 if (encoded_static_operand_size * 8 != bcast_accessed_mem_size) {
1923 FCML_TRACE("Unsupported broadcast operand size. "
1924 "Expected %d got %d.", bcast_accessed_mem_size,
1925 encoded_static_operand_size * 8);
1926 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1927 }
1928 }
1929 break;
1930 }
1931
1932 return error;
1933 }
1934
1935 static fcml_ceh_error operand_acceptor_rm(operand_acceptor_args *args) {
1936
1937 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
1938 struct acceptor_ctx *context = args->context;
1939 const fcml_st_operand *operand = args->operand;
1940 const fcml_st_def_tma_rm *addr_mode_args = (fcml_st_def_tma_rm*)
1941 args->addr_mode->addr_mode_args;
1942
1943 fcml_bool is_reg = operand->type == FCML_OT_REGISTER;
1944 fcml_bool is_mem = operand->type == FCML_OT_ADDRESS;;
1945 fcml_bool result = ((addr_mode_args->flags & FCML_RMF_R) && is_reg) ||
1946 ((addr_mode_args->flags & FCML_RMF_M) && is_mem);
1947
1948 if (result) {
1949 if (is_reg) {
1950 if (addr_mode_args->reg_type != operand->reg.type) {
1951 /* Different register type.*/
1952 error = FCML_CEH_GEC_INVALID_OPPERAND;
1953 } else if ((context->instruction->prefixes & FCML_PREFIX_LOCK)
1954 && args->addr_mode->access_mode == FCML_AM_WRITE) {
1955 /* Lock prefixes are only allowed for destination
1956 * memory operands. */
1957 error = FCML_CEH_GEC_INVALID_OPPERAND;
1958 } else {
1959 if (!accept_data_size(context, args->addr_mode_desc,
1960 addr_mode_args->encoded_register_operand_size,
1961 operand->reg.size, FCML_IEN_CT_EQUAL)) {
1962 FCML_TRACE_MSG("Unsupported register size.");
1963 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1964 }
1965 }
1966 }
1967 if (is_mem) {
1968
1969 /* OSA. */
1970
1971 /* TODO: Refactoring - this piece of code is getting too
1972 * complicated. */
1973
1974 /* TODO: Move it to accept_data_size. */
1975 if (operand->decorators.bcast.is_not_null) {
1976
1977 fcml_usize element_size =
1978 FCML_GET_SIMD_ELEMENT_SIZE(args->addr_mode_desc->details);
1979
1980 /* AVX-512 Broadcast decorator. */
1981 error = accept_bcast_decorator(
1982 addr_mode_args->is_bcast, operand->decorators.bcast,
1983 element_size, &(context->optimizer_ctx),
1984 addr_mode_args->encoded_memory_operand_size);
1985
1986 /* Accept broadcast memory location size. */
1987 if (operand->address.size_operator &&
1988 element_size != operand->address.size_operator) {
1989 FCML_TRACE_MSG("Unsupported memory operand size.");
1990 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
1991 }
1992 } else {
1993 fcml_usize mem_data_size = calculate_operand_size(context->instruction,
1994 context->ds_calculator, context->ds_calculator_args,
1995 operand->address.size_operator,
1996 addr_mode_args->encoded_memory_operand_size);
1997 if (!accept_data_size(context, args->addr_mode_desc,
1998 addr_mode_args->encoded_memory_operand_size, mem_data_size,
1999 FCML_IEN_CT_EQUAL)) {
2000 FCML_TRACE_MSG("Unsupported memory operand size.");
2001 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2002 }
2003 }
2004
2005 /* Check VSIB. */
2006 if (!error && addr_mode_args->is_vsib) {
2007 const fcml_st_register *index =
2008 &(operand->address.effective_address.index);
2009 if (index->type == FCML_REG_SIMD) {
2010 fcml_usize vsib_reg_size = fcml_fn_def_vsib_reg_to_ds(
2011 addr_mode_args->vector_index_register);
2012 if (vsib_reg_size != FCML_DS_UNDEF &&
2013 vsib_reg_size != index->size) {
2014 FCML_TRACE_MSG("Wrong VSIB size.");
2015 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2016 }
2017 } else {
2018 FCML_TRACE_MSG("VSIB encoding needs SIMD index" \
2019 " register.");
2020 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2021 }
2022 }
2023
2024 /* Accept segment registers if any of them have been set. */
2025 if (!error) {
2026
2027 const fcml_st_effective_address *effective_address =
2028 &(operand->address.effective_address);
2029 const fcml_st_segment_selector *segment_selector =
2030 &(operand->address.segment_selector);
2031 const fcml_st_register *segment_register =
2032 &(segment_selector->segment_selector);
2033
2034 if (segment_register->type == FCML_REG_SEG) {
2035 /* Segment register set, so it has to be accepted.*/
2036 if (args->addr_mode_desc->instruction_group
2037 & FCML_AMT_BRANCH) {
2038 /* For branch instructions only CS register
2039 * can be used and override is not acceptable.
2040 */
2041 if (segment_register->reg != FCML_REG_CS) {
2042 error = FCML_CEH_GEC_INVALID_OPPERAND;
2043 }
2044 } else if ((effective_address->base.type
2045 == FCML_REG_GPR)
2046 && (effective_address->base.reg
2047 == FCML_REG_BP
2048 || effective_address->base.reg
2049 == FCML_REG_SP)) {
2050 /* For SP/BP registers SS segment register has
2051 * to be used.
2052 */
2053 if (segment_register->reg != FCML_REG_SS) {
2054 error = FCML_CEH_GEC_INVALID_OPPERAND;
2055 }
2056 }
2057 if (error) {
2058 /* Segment register can not be overridden. */
2059 add_error(&context->error_container,
2060 fcml_fn_msg_get_message(
2061 FCML_MC_SEGMENT_REGISTER_CAN_NOT_BE_OVERRIDDEN),
2062 FCML_CEH_MEC_ERROR_ILLEGAL_SEG_REG_OVERRIDE);
2063 }
2064 } else if(segment_register->type != FCML_REG_UNDEFINED) {
2065 /* Wrong register type. */
2066 add_error(&context->error_container,
2067 fcml_fn_msg_get_message(
2068 FCML_MC_SEGMENT_WRONG_REGISTER_TYPE_SEG),
2069 FCML_CEH_MEC_ERROR_INVALID_REGISTER_TYPE_SEG);
2070 }
2071 }
2072
2073 if (!error) {
2074
2075 /* ASA. */
2076 fcml_en_operating_mode op_mode = context->entry_point->op_mode;
2077
2078 fcml_st_modrm mod_rm = { 0 };
2079 mod_rm.address = operand->address;
2080
2081 fcml_flags easa_flags = 0;
2082 error = fcml_fn_modrm_calculate_effective_address_size(
2083 &mod_rm, &easa_flags);
2084
2085 if (!error) {
2086
2087 /* Potential 16-bit ASA is not available in
2088 64-bit mode. */
2089 if (op_mode == FCML_OM_64_BIT) {
2090 easa_flags &= ~FCML_EN_ASF_16;
2091 }
2092
2093 /* Potential 64-bit ASA is not available in
2094 16-bit mode. */
2095 if (op_mode == FCML_OM_16_BIT) {
2096 easa_flags &= ~FCML_EN_ASF_64;
2097 }
2098
2099 if(!set_size_flags(&(context->optimizer_ctx.allowed_easa),
2100 easa_flags)) {
2101 FCML_TRACE_MSG("Accept ModR/M: Can not accept " \
2102 "ASA size.");
2103 error = FCML_CEH_GEC_INVALID_OPPERAND;
2104 }
2105 }
2106 }
2107 }
2108 } else {
2109 error = FCML_CEH_GEC_INVALID_OPPERAND;
2110 }
2111 return error;
2112 }
2113
2114 static fcml_ceh_error operand_encoder_rm(operand_encoder_args *args) {
2115
2116 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2117 const fcml_st_operand *operand = args->operand;
2118 encoding_context *context = args->context;
2119 const fcml_st_entry_point *entry_point =
2120 &(context->assembler_context->entry_point);
2121 fcml_st_def_tma_rm *addr_mode_args = (fcml_st_def_tma_rm*)
2122 args->addr_mode->addr_mode_args;
2123
2124 if (args->phase == IPPP_FIRST_PHASE) {
2125
2126 if (operand->type == FCML_OT_REGISTER) {
2127 context->mod_rm.reg.is_not_null = FCML_TRUE;
2128 context->mod_rm.reg.value = operand->reg.reg;
2129
2130 if (entry_point->op_mode == FCML_OM_64_BIT) {
2131 context->mod_rm.reg_opcode_needs_rex = operand->reg.x64_exp;
2132 }
2133
2134 /* Modify data size flags if there is such need.*/
2135 struct operand_size_decoder_args dargs = {0};
2136 dargs.encoded_operand_size =
2137 addr_mode_args->encoded_register_operand_size;
2138 dargs.operand_size = operand->reg.size;
2139 dargs.comparator = FCML_IEN_CT_EQUAL;
2140 error = decode_encoded_operand_size(context, dargs, NULL);
2141
2142 } else {
2143 /* Set hints for ModR/M instruction part encoder.*/
2144 context->modrm_hints.is_sib_alternative_hint = (operand->hints
2145 & FCML_OP_HINT_SIB_ENCODING);
2146
2147 if (entry_point->op_mode == FCML_OM_64_BIT) {
2148 context->modrm_hints.is_abs_alternative_hint = (operand->hints
2149 & FCML_OP_HINT_ABSOLUTE_ADDRESSING);
2150 context->modrm_hints.is_rel_alternative_hint = (operand->hints
2151 & FCML_OP_HINT_RELATIVE_ADDRESSING);
2152 } else {
2153 context->modrm_hints.is_abs_alternative_hint = FCML_FALSE;
2154 context->modrm_hints.is_rel_alternative_hint = FCML_FALSE;
2155 }
2156
2157 context->mod_rm.address = operand->address;
2158
2159 /* Do not calculate vector length, if broadcast is used. */
2160 fcml_usize mem_data_size = calculate_operand_size(
2161 context->instruction, context->ds_calculator,
2162 context->ds_calculator_args, operand->address.size_operator,
2163 addr_mode_args->encoded_memory_operand_size);
2164
2165 fcml_size element_size = FCML_GET_SIMD_ELEMENT_SIZE(
2166 args->addr_mode_def->details);
2167
2168 struct operand_size_decoder_args dargs = {0};
2169 dargs.encoded_operand_size =
2170 addr_mode_args->encoded_memory_operand_size;
2171 dargs.operand_size = mem_data_size;
2172 dargs.element_size = element_size;
2173 dargs.bcast = operand->decorators.bcast;
2174 dargs.comparator = FCML_IEN_CT_EQUAL;
2175 error = decode_encoded_operand_size(context, dargs, NULL);
2176
2177 /* Encode broadcast. */
2178 if (!error && operand->decorators.bcast.is_not_null) {
2179 context->epf.b = FCML_TRUE;
2180 }
2181
2182 /* TODO: Change to early return. */
2183
2184 /* Encode VSIB */
2185 if (!error && addr_mode_args->is_vsib) {
2186 const fcml_st_register *index =
2187 &(operand->address.effective_address.index);
2188 if (index->type == FCML_REG_SIMD) {
2189 if (addr_mode_args->vector_index_register == FCML_VSIB_UNDEF &&
2190 !set_vector_length(
2191 &(context->optimizer_processing_details.vector_length),
2192 index->size)) {
2193 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2194 }
2195 } else {
2196 FCML_TRACE_MSG("VSIB encoding needs SIMD index"
2197 " register.");
2198 error = FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2199 }
2200 }
2201
2202 if (!error) {
2203 handle_segment_override(args);
2204 }
2205 }
2206 }
2207
2208 return error;
2209 }
2210
2211 /**************/
2212 /* ModR/M - r */
2213 /**************/
2214
2215 static fcml_ceh_error operand_acceptor_r(operand_acceptor_args *args) {
2216 fcml_st_def_tma_r *addr_mode_args = (fcml_st_def_tma_r*)
2217 args->addr_mode->addr_mode_args;
2218 const fcml_st_operand *operand = args->operand;
2219
2220 if (operand->type != FCML_OT_REGISTER
2221 || operand->reg.type != addr_mode_args->reg_type) {
2222 return FCML_CEH_GEC_INVALID_OPPERAND;
2223 }
2224
2225 /* Some registers allow UNDEF size to be defined for them. */
2226 if ((operand->reg.type != FCML_REG_DR
2227 && operand->reg.type != FCML_REG_CR)
2228 && operand->reg.size != FCML_DS_UNDEF) {
2229 if (!accept_data_size(args->context, args->addr_mode_desc,
2230 addr_mode_args->encoded_register_operand_size, operand->reg.size,
2231 FCML_IEN_CT_EQUAL)) {
2232 return FCML_CEH_GEC_INVALID_OPPERAND;
2233 }
2234 }
2235
2236 return FCML_CEH_GEC_NO_ERROR;
2237 }
2238
2239 static fcml_ceh_error operand_encoder_r(operand_encoder_args *args) {
2240 encoding_context *context = args->context;
2241 const fcml_st_operand *operand = args->operand;
2242
2243 if (args->phase == IPPP_FIRST_PHASE) {
2244 fcml_st_def_tma_r *addr_mode_args = (fcml_st_def_tma_r*)
2245 args->addr_mode->addr_mode_args;
2246 context->mod_rm.reg_opcode = operand->reg.reg;
2247
2248 if (context->assembler_context->entry_point.op_mode == FCML_OM_64_BIT) {
2249 context->mod_rm.reg_opcode_needs_rex = operand->reg.x64_exp;
2250 }
2251
2252 if ((operand->reg.type != FCML_REG_DR
2253 && operand->reg.type != FCML_REG_CR)
2254 && operand->reg.size != FCML_DS_UNDEF) {
2255
2256 struct operand_size_decoder_args dargs = {0};
2257 dargs.encoded_operand_size =
2258 addr_mode_args->encoded_register_operand_size;
2259 dargs.operand_size = operand->reg.size;
2260 dargs.comparator = FCML_IEN_CT_EQUAL;
2261 fcml_ceh_error error;
2262 error = decode_encoded_operand_size(context, dargs, NULL);
2263 if (error) {
2264 return FCML_CEH_GEC_INVALID_OPPERAND;
2265 }
2266 }
2267 }
2268
2269 return FCML_CEH_GEC_NO_ERROR;
2270 }
2271
2272 /********/
2273 /* VVVV */
2274 /********/
2275
2276 static fcml_ceh_error operand_acceptor_vex_vvvv(operand_acceptor_args *args) {
2277 fcml_st_def_tma_vex_vvvv_reg *addr_mode_args =
2278 (fcml_st_def_tma_vex_vvvv_reg*) args->addr_mode->addr_mode_args;
2279 const fcml_st_operand *operand = args->operand;
2280
2281 if (operand->type != FCML_OT_REGISTER
2282 || operand->reg.type != addr_mode_args->reg_type) {
2283 return FCML_CEH_GEC_INVALID_OPPERAND;
2284 }
2285
2286 if (!accept_data_size(args->context, args->addr_mode_desc,
2287 addr_mode_args->encoded_register_size, operand->reg.size,
2288 FCML_IEN_CT_EQUAL)) {
2289 return FCML_CEH_GEC_INVALID_OPPERAND;
2290 }
2291
2292 return FCML_CEH_GEC_NO_ERROR;
2293 }
2294
2295 static fcml_ceh_error operand_encoder_vex_vvvv(operand_encoder_args *args) {
2296 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2297 encoding_context *context = args->context;
2298 const fcml_st_operand *operand = args->operand;
2299
2300 if (args->phase == IPPP_FIRST_PHASE) {
2301 extension_prefixes_fields *epf = &(context->epf);
2302 const fcml_st_register *reg = &(operand->reg);
2303 /* Only 16 registers can be encoded using VEX/XOP/EVEX.vvvv field. */
2304 epf->vvvv = reg->reg & 0x0F;
2305 /* High 16 registers has to be encoded using EVEX.V'. */
2306 epf->V_prim = (reg->reg >> 4) & 0x01;
2307
2308 fcml_st_def_tma_vex_vvvv_reg *addr_mode_args =
2309 (fcml_st_def_tma_vex_vvvv_reg*)args->addr_mode->addr_mode_args;
2310
2311 // TODO: The same piece of code is used in ModRM:reg encoder.
2312 if ((operand->reg.type != FCML_REG_DR
2313 && operand->reg.type != FCML_REG_CR)
2314 && operand->reg.size != FCML_DS_UNDEF) {
2315 struct operand_size_decoder_args dargs = {0};
2316 dargs.encoded_operand_size =
2317 addr_mode_args->encoded_register_size;
2318 dargs.operand_size = operand->reg.size;
2319 dargs.comparator = FCML_IEN_CT_EQUAL;
2320 error = decode_encoded_operand_size(context, dargs, NULL);
2321 if (error) {
2322 error = FCML_CEH_GEC_INVALID_OPPERAND;
2323 }
2324 }
2325 }
2326
2327 return error;
2328 }
2329
2330 /*--------*/
2331 /* is4/is5*/
2332 /*--------*/
2333
2334 static fcml_ceh_error operand_acceptor_isX(operand_acceptor_args *args) {
2335 struct acceptor_ctx *context = args->context;
2336 const fcml_st_operand *operand = args->operand;
2337 const fcml_st_def_tma_is *is_args = (fcml_st_def_tma_is*)
2338 args->addr_mode->addr_mode_args;
2339
2340 if (((is_args->flags & FCML_ISF_IS4) || (is_args->flags & FCML_ISF_IS5_SRC))
2341 && is_simd_reg(operand)) {
2342 if (!accept_data_size(context, args->addr_mode_desc, FCML_EOS_L,
2343 operand->reg.size, FCML_IEN_CT_EQUAL)) {
2344 return FCML_CEH_GEC_INVALID_OPPERAND;
2345 }
2346 } else if ((is_args->flags & FCML_ISF_IS5_M2Z) && is_imm(operand)) {
2347 fcml_int8_t imm_value;
2348 fcml_ceh_error error = fcml_fn_utils_conv_int_to_int8(
2349 &(operand->immediate), &imm_value);
2350 if (error || imm_value > 3) {
2351 return FCML_CEH_GEC_INVALID_OPPERAND;
2352 }
2353 } else {
2354 return FCML_CEH_GEC_INVALID_OPPERAND;
2355 }
2356
2357 return FCML_CEH_GEC_NO_ERROR;
2358 }
2359
2360 static fcml_ceh_error operand_encoder_isX(operand_encoder_args *args) {
2361 inst_part *operand_enc = args->operand_enc;
2362 encoding_context *context = args->context;
2363 fcml_st_def_tma_is *is_args = (fcml_st_def_tma_is*)
2364 args->addr_mode->addr_mode_args;
2365
2366 if (args->phase == IPPP_FIRST_PHASE) {
2367 if (is_args->flags & FCML_ISF_IS4) {
2368 operand_enc->code[0] = args->operand->reg.reg << 4;
2369 operand_enc->code_length = 1;
2370 } else if (is_args->flags & FCML_ISF_IS5) {
2371 if (is_args->flags & FCML_ISF_IS5_SRC) {
2372 /* Store is5 source register for last is5 instruction
2373 * part encoder. */
2374 context->isX_state.is5_byte = args->operand->reg.reg << 4;
2375 } else if (is_args->flags & FCML_ISF_IS5_M2Z) {
2376 fcml_int8_t m2z;
2377 fcml_ceh_error error = fcml_fn_utils_conv_int_to_int8(
2378 &(args->operand->immediate), &m2z);
2379 if (error) {
2380 return FCML_CEH_GEC_INVALID_OPPERAND;
2381 }
2382 operand_enc->code[0] = context->isX_state.is5_byte | m2z;
2383 operand_enc->code_length = 1;
2384 } else {
2385 return FCML_CEH_GEC_INTERNAL_ERROR;
2386 }
2387 } else {
2388 return FCML_CEH_GEC_INTERNAL_ERROR;
2389 }
2390 }
2391
2392 return FCML_CEH_GEC_NO_ERROR;
2393 }
2394
2395 /**************/
2396 /* Pseudo-Op. */
2397 /**************/
2398
2399 static fcml_ceh_error operand_acceptor_pseudo_op(operand_acceptor_args *args) {
2400 struct acceptor_ctx *context = args->context;
2401 const fcml_st_operand *operand = args->operand;
2402 fcml_st_def_tma_pseudo_op *pseudo_op_args =
2403 (fcml_st_def_tma_pseudo_op*) args->addr_mode->addr_mode_args;
2404 fcml_bool is_pseudo_op = context->mnemonic->pseudo_op.is_not_null;
2405
2406 if (operand->type == FCML_OT_IMMEDIATE && !is_pseudo_op) {
2407 /* IMM8 encoding is given directly in operand.*/
2408 fcml_st_integer destination;
2409 /* TODO: change to: int_fit_imm */
2410 if (!conv_int_and_set_flag(
2411 &(operand->immediate), &destination, FCML_DS_8, FCML_DS_8,
2412 0, NULL)) {
2413 return FCML_CEH_GEC_INVALID_OPPERAND;
2414 }
2415 if (destination.int8 & (~pseudo_op_args->mask)) {
2416 add_error(&context->error_container,
2417 fcml_fn_msg_get_message(
2418 FCML_MC_SEGMENT_INVALID_PSEUDO_OPCODE_IMM),
2419 FCML_CEH_MEC_ERROR_INVALID_PSEUDO_OPCODE_VALUE);
2420 return FCML_CEH_GEC_INVALID_OPPERAND;
2421 }
2422 } else if (!is_pseudo_op || operand->type != FCML_OT_NONE) {
2423 /* IMM8 is not available in operand, so its value should be given
2424 * in mnemonic configuration. In such case, operand
2425 * shouldn't exist.
2426 */
2427 return FCML_CEH_GEC_INVALID_OPPERAND;
2428 }
2429
2430 return FCML_CEH_GEC_NO_ERROR;
2431 }
2432
2433 static fcml_ceh_error operand_encoder_pseudo_op(operand_encoder_args *args ) {
2434 inst_part *operand_enc = args->operand_enc;
2435 const fcml_st_operand *operand = args->operand;
2436 encoding_context *context = args->context;
2437 if (args->phase == IPPP_FIRST_PHASE) {
2438 if (operand->type == FCML_OT_IMMEDIATE) {
2439 /* IMM8 encoding is given directly in operand.*/
2440 fcml_st_integer destination;
2441 /* TODO: change to: int_fit_imm */
2442 if (!conv_int_and_set_flag(
2443 &(operand->immediate), &destination, FCML_DS_8,
2444 FCML_DS_8, 0, NULL)) {
2445 return FCML_CEH_GEC_INVALID_OPPERAND;
2446 }
2447 operand_enc->code[0] = destination.int8;
2448 } else if (context->mnemonic->pseudo_op.is_not_null) {
2449 /* IMM8 is not available in operand, so its value should
2450 * be given in mnemonic configuration. */
2451 operand_enc->code[0] = context->mnemonic->pseudo_op.value;
2452 }
2453 operand_enc->code_length = 1;
2454 }
2455 return FCML_CEH_GEC_NO_ERROR;
2456 }
2457
2458 /********************/
2459 /* Virtual operand. */
2460 /********************/
2461
2462 static fcml_ceh_error operand_acceptor_virtual_op(operand_acceptor_args *args) {
2463 struct acceptor_ctx *context = args->context;
2464 const fcml_st_operand *operand = args->operand;
2465 fcml_st_def_tma_virtual_op *addr_mode_args =
2466 (fcml_st_def_tma_virtual_op*)args->addr_mode->addr_mode_args;
2467 struct acceptor_optimizer_ctx *optimizer_ctx = &(context->optimizer_ctx);
2468
2469 if (operand->type != FCML_OT_NONE &&
2470 operand->type != FCML_OT_VIRTUAL) {
2471 return FCML_CEH_GEC_INVALID_OPPERAND;
2472 }
2473
2474 if (FCML_IS_DECOR_SAE_REQ(addr_mode_args->decorators) &&
2475 !operand->decorators.sae) {
2476 return FCML_CEH_GEC_MISSING_DECORATOR;
2477 }
2478
2479 /* Handle explicit vector length for SAE and ER aware instructions. */
2480 if (operand->decorators.sae || operand->decorators.er.is_not_null) {
2481
2482 fcml_uint8_t tuple_type =
2483 FCML_GET_SIMD_TUPLETYPE(args->addr_mode_desc->details);
2484
2485 fcml_usize vector_length = FCML_DS_UNDEF;
2486
2487 /* For the following kind of SIMD instructions vector length
2488 is explicitly set. */
2489 if(tuple_type == FCML_TT_FV) {
2490 vector_length = FCML_DS_512;
2491 } else if (tuple_type == FCML_TT_T1S || tuple_type == FCML_TT_T1F) {
2492 vector_length = FCML_DS_128;
2493 }
2494
2495 if (!set_vector_length(&(optimizer_ctx->vector_length), vector_length)) {
2496 FCML_TRACE("Vector length differs expected %d got %d.",
2497 optimizer_ctx->vector_length, vector_length);
2498 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
2499 }
2500 }
2501
2502 return FCML_CEH_GEC_NO_ERROR;
2503 }
2504
2505 static fcml_ceh_error operand_encoder_virtual_op(operand_encoder_args *args) {
2506 encoding_context *context = args->context;
2507 const fcml_st_operand *operand = args->operand;
2508
2509 if (args->phase == IPPP_FIRST_PHASE) {
2510 if (operand->decorators.sae) {
2511 context->epf.b = FCML_TRUE;
2512 context->epf.er.value = 0;
2513 context->epf.er.is_not_null = FCML_TRUE;
2514 }
2515 if (operand->decorators.er.is_not_null) {
2516 context->epf.er = operand->decorators.er;
2517 context->epf.b = FCML_TRUE;
2518 }
2519 } else if (args->phase == IPPP_THIRD_PHASE) {
2520 /* Make sure that register to register addressing mode is used, when
2521 * SAE or ER decorators are used. */
2522 fcml_st_def_tma_virtual_op *addr_mode_args =
2523 (fcml_st_def_tma_virtual_op*) args->addr_mode->addr_mode_args;
2524 if ((FCML_IS_DECOR_SAE(addr_mode_args->decorators) &&
2525 operand->decorators.sae)
2526 || (FCML_IS_DECOR_ER(addr_mode_args->decorators)
2527 && operand->decorators.er.is_not_null)) {
2528 if (!context->mod_rm.reg.is_not_null) {
2529 return FCML_CEH_GEC_INVALID_OPPERAND;
2530 }
2531 }
2532 }
2533
2534 return FCML_CEH_GEC_NO_ERROR;
2535 }
2536
2537 static operand_encoder_def operand_encoders[] = {
2538 { NULL, NULL, NULL },
2539 { operand_encoder_imm, operand_acceptor_imm, NULL },
2540 { operand_encoder_explicit_reg, operand_acceptor_explicit_reg, NULL },
2541 { operand_encoder_opcode_reg, operand_acceptor_opcode_reg, NULL },
2542 { operand_encoder_imm_disp_relative,
2543 operand_acceptor_immediate_dis_relative, NULL },
2544 { operand_encoder_far_pointer, operand_acceptor_far_pointer,
2545 fcml_fn_hts_ihc_far_pointer },
2546 { operand_encoder_explicit_gps_reg_addressing,
2547 operand_acceptor_explicit_gps_reg_addressing, NULL },
2548 { operand_encoder_explicit_ib, operand_acceptor_explicit_ib, NULL },
2549 { operand_encoder_segment_relative_offset,
2550 operand_acceptor_segment_relative_offset, NULL },
2551 { operand_encoder_rm, operand_acceptor_rm,
2552 fcml_fn_hts_ihc_modrm_hints },
2553 { operand_encoder_r, operand_acceptor_r, NULL },
2554 { operand_encoder_vex_vvvv, operand_acceptor_vex_vvvv, NULL },
2555 { operand_encoder_isX, operand_acceptor_isX, NULL },
2556 { operand_encoder_rm, operand_acceptor_rm,
2557 fcml_fn_hts_ihc_modrm_hints },
2558 { operand_encoder_pseudo_op, operand_acceptor_pseudo_op, NULL },
2559 { operand_encoder_virtual_op, operand_acceptor_virtual_op, NULL }
2560 };
2561
2562 /*************************
2563 * Instruction encoders. *
2564 *************************/
2565
2566 static fcml_ceh_error accept_addr_mode(struct acceptor_ctx *ctx,
2567 const addr_mode_encoding *addr_mode) {
2568
2569 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2570
2571 const fcml_st_instruction *instruction = ctx->instruction;
2572
2573 #ifdef FCML_DEBUG
2574 FCML_TRACE("Accepting addressing mode: %d, prefixes: 0x%08X, opcode: 0x%08X.", ctx->__def_index, \
2575 addr_mode->addr_mode_desc->allowed_prefixes, \
2576 addr_mode->addr_mode_desc->opcode_flags);
2577 #endif
2578
2579 int index = 0;
2580
2581 ipp_acceptor_args acceptor_args = {0};
2582 acceptor_args.context = ctx;
2583 acceptor_args.instruction = instruction;
2584 acceptor_args.addr_mode_details = &(addr_mode->addr_mode_details);
2585 acceptor_args.addr_mode_def = addr_mode->addr_mode_desc;
2586
2587 ipp_chain *current_processor = addr_mode->ipp_chain;
2588 while (current_processor) {
2589 ipp_desc *descriptor = &(current_processor->descriptor);
2590 ctx->last_ipp = index;
2591 if (!is_short_form(ctx->instruction, ctx->mnemonic)
2592 || descriptor->is_short_form_supported) {
2593 if (descriptor->acceptor) {
2594 acceptor_args.args = descriptor->args;
2595 error = descriptor->acceptor(&acceptor_args);
2596 if (error != FCML_CEH_GEC_NO_ERROR) {
2597 FCML_TRACE("Addressing mode not accepted. Acceptor (%s) "
2598 "failed: %d",
2599 descriptor->ipp_name, index);
2600 break;
2601 }
2602 }
2603 }
2604 current_processor = current_processor->next_processor;
2605 index++;
2606 }
2607
2608 #ifdef FCML_DEBUG
2609 if(error == FCML_CEH_GEC_NO_ERROR) {
2610 FCML_TRACE("Accepted addressing mode %d - prefixes: 0x%04X, opcode:" \
2611 " 0x%02X.", ctx->__def_index,
2612 addr_mode->addr_mode_desc->allowed_prefixes,
2613 addr_mode->addr_mode_desc->opcode_flags);
2614 }
2615 #endif
2616
2617 return error;
2618 }
2619
2620 /* All currently supported encoding phases. */
2621 static part_processor_phase execution_phases[] = {
2622 IPPP_FIRST_PHASE,
2623 IPPP_SECOND_PHASE,
2624 IPPP_THIRD_PHASE
2625 };
2626
2627 static fcml_uint8_t inst_parts_len(inst_part *inst_part, fcml_int count) {
2628 fcml_uint8_t length = 0;
2629 while (count-- > 0) {
2630 length += inst_part++->code_length;
2631 }
2632 return length;
2633 }
2634
2635 /* Responsible for assembling given addressing mode using attributes
2636 * provided by the context.
2637 */
2638 fcml_ceh_error fcml_ifn_asm_process_addr_mode(encoding_context *context,
2639 addr_mode_encoding *addr_mode,
2640 inst_part_container *inst_part_container) {
2641
2642 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2643
2644 /* Allocate instruction parts.*/
2645 inst_part *part = (inst_part*)fcml_fn_env_memory_alloc_clear(
2646 sizeof(inst_part) * addr_mode->instruction_parts);
2647 if (!part) {
2648 return FCML_CEH_GEC_OUT_OF_MEMORY;
2649 }
2650
2651 ipp_encoder_args encoder_args = { IPPP_FIRST_PHASE };
2652 encoder_args.context = context;
2653 encoder_args.addr_mode_details = &(addr_mode->addr_mode_details);
2654 encoder_args.addr_mode_def = addr_mode->addr_mode_desc;
2655
2656 int phase;
2657 int phases_count = sizeof(execution_phases) / sizeof(part_processor_phase);
2658 for (phase = 0; !error && phase < phases_count; phase++) {
2659 /* Gets through all the instruction part processors for every phase. */
2660 inst_part *current_instruction_part = part;
2661 ipp_chain *current_processor = addr_mode->ipp_chain;
2662 fcml_bool first = FCML_TRUE;
2663 int index = 0;
2664 while (current_processor) {
2665 context->last_ipp = index;
2666 ipp_desc *descriptor = &(current_processor->descriptor);
2667 if (descriptor->encoder &&
2668 (!is_short_form(context->instruction, context->mnemonic)
2669 || descriptor->is_short_form_supported)) {
2670 if (!first && descriptor->type == IPPT_ENCODER) {
2671 current_instruction_part++;
2672 }
2673 first = FCML_FALSE;
2674 encoder_args.phase = execution_phases[phase];
2675 encoder_args.args = descriptor->args;
2676 /* If IPP is just a verifier, do not pass instruction part
2677 * there. It's not needed because IPP doesn't encode any
2678 * bytes anyway. */
2679 encoder_args.instruction_part = descriptor->type ==
2680 IPPT_VERIFIER ? NULL : current_instruction_part;;
2681 error = descriptor->encoder(&encoder_args);
2682 if (error) {
2683 #ifdef FCML_DEBUG
2684 FCML_TRACE("Processor failed (%s) %d - prefixes: 0x%04X, opcode: 0x%02X: %d",
2685 descriptor->ipp_name,
2686 context->__def_index,
2687 addr_mode->addr_mode_desc->allowed_prefixes,
2688 addr_mode->addr_mode_desc->opcode_flags, error);
2689 #endif
2690 /* Something failed.*/
2691 break;
2692 }
2693 } else if(descriptor->encoder == NULL &&
2694 descriptor->acceptor == NULL) {
2695 break;
2696 }
2697
2698 current_processor = current_processor->next_processor;
2699 index++;
2700 }
2701 }
2702
2703 if (!error) {
2704
2705 /* Calculate instruction size.*/
2706 FCML_SET_VALUE(context->instruction_size,
2707 inst_parts_len(part, addr_mode->instruction_parts));
2708
2709 fcml_bool recalculate_size = FCML_FALSE;
2710
2711 /* Execute potential post processors.*/
2712 int i;
2713 for (i = 0; i < addr_mode->instruction_parts; i++) {
2714 inst_part *ip = &(part[i]);
2715 if (ip->post_processor) {
2716 error = ip->post_processor(context, ip,
2717 ip->post_processor_args);
2718 if (error) {
2719 break;
2720 }
2721 recalculate_size = FCML_TRUE;
2722 }
2723 }
2724
2725 if (recalculate_size) {
2726 FCML_SET_VALUE(context->instruction_size,
2727 inst_parts_len(part, addr_mode->instruction_parts));
2728 }
2729
2730 #ifdef FCML_DEBUG
2731 FCML_TRACE("Proceeded addressing mode %d - prefixes: 0x%04X, opcode:" \
2732 " 0x%02X (%s).", context->__def_index,
2733 addr_mode->addr_mode_desc->allowed_prefixes,
2734 addr_mode->addr_mode_desc->opcode_flags,
2735 debug_print_size_flags(
2736 &(context->optimizer_processing_details)));
2737 #endif
2738 } else {
2739 #ifdef FCML_DEBUG
2740 FCML_TRACE("Failed to process addressing mode %d - prefixes: 0x%04X," \
2741 " opcode: 0x%02X (%s).", context->__def_index,
2742 addr_mode->addr_mode_desc->allowed_prefixes,
2743 addr_mode->addr_mode_desc->opcode_flags,
2744 debug_print_size_flags(
2745 &(context->optimizer_processing_details)));
2746 #endif
2747 }
2748
2749 if (error) {
2750 fcml_fn_env_memory_free(part);
2751 } else {
2752 inst_part_container->parts = part;
2753 inst_part_container->count = addr_mode->instruction_parts;
2754 }
2755
2756 return error;
2757 }
2758
2759 void fcml_ifn_asm_assemble_instruction_parts(
2760 fcml_st_assembled_instruction *assembled_instruction,
2761 inst_part_container *instruction_part_container) {
2762 int i, count = instruction_part_container->count;
2763 int offset = 0;
2764 for (i = 0; i < count; i++) {
2765 inst_part *part =
2766 &(instruction_part_container->parts[i]);
2767 if (part->code_length > 0) {
2768 fcml_fn_env_memory_copy(assembled_instruction->code + offset,
2769 part->code, part->code_length);
2770 offset += part->code_length;
2771 }
2772 }
2773 /* Just to be consequent.*/
2774 assembled_instruction->code_length = offset;
2775 }
2776
2777 void fcml_ifn_asm_free_assembled_instruction(
2778 fcml_st_assembled_instruction *assembled_instruction) {
2779 if (assembled_instruction) {
2780 if (assembled_instruction->code) {
2781 fcml_fn_env_memory_free(assembled_instruction->code);
2782 }
2783 fcml_fn_ceh_free_errors_only(&(assembled_instruction->warnings));
2784 fcml_fn_env_memory_free(assembled_instruction);
2785 }
2786 }
2787
2788 static void fcml_ifn_asm_fill_instruction_details(encoding_context *context,
2789 addr_mode_encoding *addr_mode,
2790 fcml_st_assembled_instruction *assembled_instruction) {
2791 assembled_instruction->details.instruction_group =
2792 addr_mode->addr_mode_desc->instruction_group;
2793 /* If EOSA and EASA differ from the default one
2794 it means that they have been overridden. */
2795 const fcml_st_entry_point *entry_point =
2796 &(context->assembler_context->entry_point);
2797 fcml_usize osa = entry_point->operand_size_attribute;
2798 fcml_usize asa = entry_point->address_size_attribute;
2799 fcml_usize eosa = context->optimizer_processing_details.eosa;
2800 fcml_usize easa = context->optimizer_processing_details.easa;
2801
2802 if (osa != eosa) {
2803 assembled_instruction->details.osa_override = FCML_TRUE;
2804 }
2805
2806 if (asa != easa) {
2807 assembled_instruction->details.asa_override = FCML_TRUE;
2808 }
2809 }
2810
2811 /**
2812 * Assembles an instruction using the provided addressing mode
2813 * encoder. The encoder still may fail, as the adressing mode
2814 * accepting phase is not 100% correct and may return
2815 * false positives in some cases.
2816 */
2817 static fcml_ceh_error fcml_ifn_asm_assemble_instruction(
2818 encoding_context *context,
2819 addr_mode_encoding *addr_mode,
2820 fcml_st_assembled_instruction **assembled_instruction) {
2821
2822 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2823
2824 inst_part_container instruction_part_container = { 0 };
2825
2826 /* Prepares encoded instruction parts. */
2827 error = fcml_ifn_asm_process_addr_mode(context, addr_mode,
2828 &instruction_part_container);
2829
2830 /* Assemble instruction parts.*/
2831 if (!error) {
2832 fcml_usize code_length = context->instruction_size.value;
2833
2834 /* Allocate a memory block for the assembled code. */
2835 FCML_ENV_ALLOC_CLEAR(asm_inst, fcml_st_assembled_instruction);
2836 if (asm_inst) {
2837 asm_inst->code = (fcml_uint8_t*) fcml_fn_env_memory_alloc(
2838 code_length);
2839 if (asm_inst->code) {
2840 asm_inst->code_length = code_length;
2841 /* Copies instruction parts to the destination code array. */
2842 fcml_ifn_asm_assemble_instruction_parts(asm_inst,
2843 &instruction_part_container);
2844 fcml_ifn_asm_fill_instruction_details(context, addr_mode, asm_inst);
2845 *assembled_instruction = asm_inst;
2846 } else {
2847 error = FCML_CEH_GEC_OUT_OF_MEMORY;
2848 }
2849 } else {
2850 error = FCML_CEH_GEC_OUT_OF_MEMORY;
2851 }
2852
2853 if (error && asm_inst) {
2854 fcml_fn_env_memory_free(asm_inst);
2855 }
2856 }
2857
2858 /* Always free instruction parts when they are not needed anymore. */
2859 if (instruction_part_container.parts) {
2860 fcml_fn_env_memory_free(instruction_part_container.parts);
2861 }
2862
2863 return error;
2864 }
2865
2866 /** Arguments passed through the optimization process. */
2867 typedef struct fcml_st_asm_enc_optimizer_callback_args {
2868 /** Currently processed addressing mode. */
2869 addr_mode_encoding *addr_mode;
2870 /** Encoding context instance. */
2871 encoding_context *context;
2872 /** All assembled instructions end up here. */
2873 fcml_st_asm_encoder_result *result;
2874 } fcml_st_asm_enc_optimizer_callback_args;
2875
2876 /** Assembles an instruction and attaches it to the instructions chain. */
2877 static fcml_ceh_error fcml_ifn_assemble_and_collect_instruction(fcml_ptr args) {
2878
2879 /* Restore important information from callback arguments.*/
2880 fcml_st_asm_enc_optimizer_callback_args *callback_args =
2881 (fcml_st_asm_enc_optimizer_callback_args*)args;
2882 encoding_context *context = callback_args->context;
2883 addr_mode_encoding *addr_mode = callback_args->addr_mode;
2884 fcml_st_asm_encoder_result *result = callback_args->result;
2885
2886 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
2887
2888 fcml_st_assembled_instruction *assembled_instruction;
2889
2890 error = fcml_ifn_asm_assemble_instruction(context, addr_mode,
2891 &assembled_instruction);
2892 if (error) {
2893 return error;
2894 }
2895
2896 #ifdef FCML_DEBUG
2897 assembled_instruction->__def_index = context->__def_index;
2898 #endif
2899
2900 fcml_bool ignore = FCML_FALSE;
2901
2902 /* Check if such instruction has been already assembled. */
2903 fcml_st_assembled_instruction *instruction = result->instructions;
2904 while (instruction) {
2905 if (instruction->code_length == assembled_instruction->code_length
2906 && fcml_fn_env_memory_cmp(instruction->code,
2907 assembled_instruction->code,
2908 instruction->code_length)) {
2909 /* Instructions are the same.*/
2910 ignore = FCML_TRUE;
2911 break;
2912 }
2913 instruction = instruction->next;
2914 }
2915
2916 if (!ignore) {
2917 /* Insert newly assembled instruction to the front of the
2918 * instructions chain.
2919 */
2920 fcml_st_assembled_instruction *instructions =
2921 result->instructions;
2922 result->instructions = assembled_instruction;
2923 assembled_instruction->next = instructions;
2924
2925 result->number_of_instructions++;
2926
2927 } else {
2928 /* Free ignored instruction.*/
2929 fcml_ifn_asm_free_assembled_instruction(assembled_instruction);
2930 }
2931
2932 return FCML_CEH_GEC_NO_ERROR;
2933 }
2934
2935 static fcml_bool accept_instruction_hints(fcml_hints addr_mode_dest_hints,
2936 fcml_hints instruction_hints) {
2937 if ((instruction_hints & FCML_HINT_INDIRECT_POINTER)
2938 && !(addr_mode_dest_hints & FCML_HINT_INDIRECT_POINTER)) {
2939 FCML_TRACE_MSG("Addressing mode doesn't support indirect addressing.");
2940 return FCML_FALSE;
2941 }
2942 if ((instruction_hints & FCML_HINT_DIRECT_POINTER)
2943 && !(addr_mode_dest_hints & FCML_HINT_DIRECT_POINTER)) {
2944 FCML_TRACE_MSG("Addressing mode doesn't support direct addressing.");
2945 return FCML_FALSE;
2946 }
2947 if (instruction_hints & FCML_HINT_FAR_POINTER) {
2948 return addr_mode_dest_hints & FCML_HINT_FAR_POINTER;
2949 }
2950 if (instruction_hints & FCML_HINT_NEAR_POINTER) {
2951 return addr_mode_dest_hints & FCML_HINT_NEAR_POINTER;
2952 }
2953 return FCML_TRUE;
2954 }
2955
2956 /* Sets attribute size flag for given real size. */
2957 static void set_attribute_size_flag(fcml_usize attribute_size,
2958 fcml_st_nullable_size_flags *flags) {
2959 fcml_flags flag = 0;
2960 switch (attribute_size) {
2961 case FCML_DS_16:
2962 flag = FCML_EN_ASF_16;
2963 break;
2964 case FCML_DS_32:
2965 flag = FCML_EN_ASF_32;
2966 break;
2967 case FCML_DS_64:
2968 flag = FCML_EN_ASF_64;
2969 break;
2970 }
2971 if (flag) {
2972 flags->flags |= flag;
2973 flags->is_set = FCML_TRUE;
2974 }
2975 }
2976
2977 void fcml_ifn_chooser_extract(fcml_ptr instruction_ptr,
2978 fcml_st_instruction_code *instruction_code) {
2979 if (instruction_ptr) {
2980 fcml_st_assembled_instruction *instruction =
2981 (fcml_st_assembled_instruction*) instruction_ptr;
2982 instruction_code->code = instruction->code;
2983 instruction_code->code_length = instruction->code_length;
2984 instruction_code->details = instruction->details;
2985 }
2986 }
2987
2988 fcml_ptr fcml_ifn_chooser_next(fcml_ptr instruction_ptr) {
2989 if (instruction_ptr) {
2990 fcml_st_assembled_instruction *instruction =
2991 (fcml_st_assembled_instruction*) instruction_ptr;
2992 return instruction->next;
2993 }
2994 return NULL;
2995 }
2996
2997 static fcml_st_chooser_context prepare_chooser_context(
2998 fcml_st_assembled_instruction *instructions) {
2999 fcml_st_chooser_context context;
3000 context.extract = &fcml_ifn_chooser_extract;
3001 context.next = &fcml_ifn_chooser_next;
3002 context.instruction = instructions;
3003 return context;
3004 }
3005
3006 static void prepare_optimizer_processing_details(
3007 const addr_mode_encoding *addr_mode,
3008 fcml_st_asm_optimizer_processing_details *processing_details) {
3009
3010 const fcml_st_mp_mnemonic *mnemonic = addr_mode->mnemonic;
3011
3012 /* Apply address attribute size restrictions from mnemonic definition.*/
3013 fcml_usize asa = mnemonic->supported_asa;
3014 processing_details->easa = asa;
3015 if (asa) {
3016 set_attribute_size_flag(asa, &(processing_details->allowed_easa));
3017 }
3018
3019 /* Apply operand attribute size restrictions from mnemonic definition.*/
3020 fcml_usize osa = mnemonic->supported_osa;
3021 processing_details->eosa = osa;
3022 if (osa) {
3023 set_attribute_size_flag(osa, &(processing_details->allowed_eosa));
3024 }
3025
3026 /* Apply L fields restrictions from mnemonic definition. */
3027 if (mnemonic->l.is_not_null) {
3028 processing_details->vector_length =
3029 fcml_fn_mp_l_to_vector_length(mnemonic->l.value);
3030 }
3031 }
3032
3033 static struct acceptor_ctx prep_acceptor_ctx(encoding_context *context) {
3034 struct acceptor_optimizer_ctx optimizer_ctx = {
3035 context->optimizer_processing_details.allowed_eosa,
3036 context->optimizer_processing_details.allowed_easa,
3037 context->optimizer_processing_details.vector_length
3038 };
3039 /* This error container is here to collect errors from acceptors
3040 * invoked for only one addressing mode being considered by
3041 * the processor. */
3042 struct error_container ctx_error_container = {
3043 context->assembler_context->configuration.enable_error_messages,
3044 { NULL, NULL }
3045 };
3046 struct acceptor_ctx acceptor_ctx = {
3047 context->assembler_context->configuration,
3048 &(context->assembler_context->entry_point),
3049 context->instruction,
3050 context->mnemonic,
3051 optimizer_ctx,
3052 context->ds_calculator,
3053 context->ds_calculator_args,
3054 ctx_error_container
3055 };
3056 return acceptor_ctx;
3057 }
3058
3059 static void handle_addr_mode_errors(
3060 addr_mode_error *addr_modes_errors,
3061 struct error_container *local_errors,
3062 fcml_ceh_error error_code, fcml_int ipp_failed,
3063 addr_mode_proc_phase phase) {
3064
3065 fcml_bool choose_error = FCML_FALSE;
3066
3067 /* Priorities are as follows: phase, number of failed ipp, error codes. */
3068
3069 /* Phase. */
3070 if (phase == addr_modes_errors->phase) {
3071 /* IPP ordinal. */
3072 if (ipp_failed == addr_modes_errors->ipp_failed) {
3073 /* Error code. */
3074 fcml_ceh_error cec = addr_modes_errors->error_code;
3075 if (cec == FCML_CEH_GEC_NO_ERROR
3076 || (cec == FCML_CEH_GEC_INVALID_OPPERAND
3077 && (cec != error_code))) {
3078 choose_error = FCML_TRUE;
3079 }
3080 } else if (ipp_failed > addr_modes_errors->ipp_failed) {
3081 choose_error = FCML_TRUE;
3082 }
3083 } else if (phase > addr_modes_errors->phase) {
3084 choose_error = FCML_TRUE;
3085 }
3086
3087 if (choose_error) {
3088 /* Move all errors from the local errors container prepared for the
3089 * currently processed addressing mode to the one shared by all the
3090 * addressing modes being considered.
3091 */
3092 move_errors(&addr_modes_errors->errors, local_errors);
3093 addr_modes_errors->error_code = error_code;
3094 addr_modes_errors->ipp_failed = ipp_failed;
3095 addr_modes_errors->phase = phase;
3096 }
3097 }
3098
3099 static fcml_ceh_error encode_addressing_mode_core(
3100 encoding_context *context, addr_mode_encoding *addr_mode,
3101 addr_mode_error *addr_mode_errors, fcml_st_asm_encoder_result *result) {
3102
3103 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3104
3105 const fcml_st_assembler_context *asm_ctx = context->assembler_context;
3106
3107 /* Checks if the addressing mode matches the hints, if there are any.
3108 To ignore addressing modes which will fail to encode the instruction
3109 as soon as possible, to save CPU. */
3110 if (!context->instruction->hints || accept_instruction_hints(
3111 addr_mode->hints, context->instruction->hints)) {
3112
3113 /* Prepare processing context for optimizer using currently
3114 used addressing mode. */
3115 prepare_optimizer_processing_details(addr_mode,
3116 &(context->optimizer_processing_details));
3117
3118 struct acceptor_ctx acceptor_ctx = prep_acceptor_ctx(context);
3119 error = accept_addr_mode(&acceptor_ctx, addr_mode);
3120 if (!error) {
3121
3122 fcml_st_asm_enc_optimizer_callback_args args;
3123 args.addr_mode = addr_mode;
3124 args.context = context;
3125 args.result = result;
3126
3127 fcml_st_asm_optimizer_context optimizer_context = { NULL };
3128 optimizer_context.entry_point = &(asm_ctx->entry_point);
3129 optimizer_context.optimizer_flags =
3130 asm_ctx->configuration.optimizer_flags;
3131
3132 /* An optimizer implementation can be provided by the user. */
3133 fcml_fnp_asm_optimizer optimizer =
3134 asm_ctx->configuration.optimizer;
3135 if (!optimizer) {
3136 optimizer = &fcml_fn_asm_default_optimizer;
3137 }
3138
3139 fcml_st_asm_optimizer_processing_details *optimizer_details =
3140 &(context->optimizer_processing_details);
3141
3142 optimizer_details->allowed_eosa =
3143 acceptor_ctx.optimizer_ctx.allowed_eosa;
3144 optimizer_details->allowed_easa =
3145 acceptor_ctx.optimizer_ctx.allowed_easa;
3146 optimizer_details->vector_length =
3147 acceptor_ctx.optimizer_ctx.vector_length;
3148
3149 error = optimizer(&optimizer_context,
3150 &(context->optimizer_processing_details),
3151 fcml_ifn_assemble_and_collect_instruction, &args);
3152 } else {
3153 /* Addressing mode hasn't been accepted, so handle errors. */
3154 handle_addr_mode_errors(addr_mode_errors,
3155 &acceptor_ctx.error_container, error, acceptor_ctx.last_ipp,
3156 AMPP_PROCESS);
3157 }
3158
3159 /* If there are any locally collected errors left (haven't been moved
3160 * to the shared container) just free them.
3161 * See: handle_addr_mode_errors. */
3162 free_errors(&acceptor_ctx.error_container);
3163 } else {
3164 FCML_TRACE_MSG("Addressing mode ignored due to hints incompatibility.");
3165 }
3166
3167 return error;
3168 }
3169
3170 static fcml_usize count_operands(fcml_st_instruction *instruction) {
3171 fcml_int i;
3172 fcml_usize count = 0;
3173 /* Check if there are no operator gaps. */
3174 for(i = 0; i < FCML_OPERANDS_COUNT; i++) {
3175 if (instruction->operands[i].type == FCML_OT_NONE) {
3176 break;
3177 } else {
3178 count++;
3179 }
3180 }
3181 return count;
3182 }
3183
3184 static fcml_ceh_error instruction_encoder_IA(
3185 fcml_st_assembler_context *asm_context,
3186 fcml_st_dialect_context_int *dialect_context,
3187 fcml_st_instruction_addr_modes *addr_modes,
3188 fcml_st_instruction *instruction,
3189 fcml_st_asm_encoder_result *result) {
3190
3191 /* Make a local copy of the instruction, because it still can be
3192 changed by the preprocessor. */
3193 fcml_st_instruction tmp_instruction = *instruction;
3194 fcml_usize operands_count = count_operands(&tmp_instruction);
3195 fcml_bool errors_enabled = asm_context->configuration.enable_error_messages;
3196
3197 encoding_context context = { 0 };
3198 context.assembler_context = asm_context;
3199 context.instruction = &tmp_instruction;
3200 /* Share the global errors container in order to collect
3201 * all errors in one place accessible by the caller.
3202 */
3203 context.errors.enabled = errors_enabled;
3204 context.errors.errors = result->errors;
3205
3206 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3207
3208 if (addr_modes) {
3209 /* Try to encode the instruction by using all found addressing modes. */
3210 if (addr_modes->addr_modes_encodings->size) {
3211 /* Container for errors related to addressing modes processing.
3212 * It's shared by all the addressing modes. */
3213 addr_mode_error addr_mode_errors = { AMPP_UNDEFINED,
3214 FCML_CEH_GEC_NO_ERROR, { errors_enabled, {NULL} }, 0};
3215
3216 /* Gets the first addressing mode available for the chosen
3217 instruction definition. */
3218 fcml_st_coll_list_element *addr_mode_elem =
3219 addr_modes->addr_modes_encodings->head;
3220
3221 #ifdef FCML_DEBUG
3222 int index = 0;
3223 #endif
3224 /* This loop iterates through all available addressing
3225 modes one by one. */
3226 while (addr_mode_elem) {
3227 error = FCML_CEH_GEC_NO_ERROR;
3228 addr_mode_encoding *addr_mode = (addr_mode_encoding*)
3229 addr_mode_elem->item;
3230
3231 /* Encoding context has to be prepared for new addressing
3232 mode because it is reused. */
3233 clean_context(&context);
3234
3235 /* This flag is used by preprocessor to signal the fact
3236 that instruction has been changed. */
3237 fcml_bool inst_changed = FCML_FALSE;
3238 /* Call instruction preprocessor again, passing found mnemonic
3239 definition this time. Parsed mnemonic can contain some
3240 dialect specific information, so we have to provide
3241 dialect a way to modify instruction definition basing
3242 on that information. */
3243 fcml_fnp_asm_preprocessor preprocessor =
3244 dialect_context->asm_preprocessor;
3245 if (preprocessor) {
3246 preprocessor((fcml_st_dialect*) dialect_context,
3247 addr_mode->addr_mode_desc, addr_mode->mnemonic,
3248 addr_mode->instruction, &tmp_instruction,
3249 &inst_changed);
3250 }
3251
3252 /* Make it accessible through encoding context. Certain
3253 encoders might need this information. */
3254 context.mnemonic = addr_mode->mnemonic;
3255
3256 #ifdef FCML_DEBUG
3257 context.__def_index = index;
3258 #endif
3259
3260 /* Every addressing mode has its own data size calculators. */
3261 context.ds_calculator =
3262 addr_mode->addr_mode_details.ds_calculator;
3263 context.ds_calculator_args =
3264 addr_mode->addr_mode_details.ds_calculator_args;
3265
3266 /* Try to encode the instruction using the prepared
3267 addressing mode. */
3268 error = encode_addressing_mode_core(&context, addr_mode,
3269 &addr_mode_errors, result);
3270
3271 addr_mode_elem = addr_mode_elem->next;
3272
3273 #ifdef FCML_DEBUG
3274 index++;
3275 #endif
3276 /* Restore the instruction if the preprocessor has changed anything.*/
3277 if (inst_changed) {
3278 tmp_instruction = *instruction;
3279 }
3280 }
3281 /* Addressing modes related errors have higher priority than
3282 any others. */
3283 if (has_errors(&addr_mode_errors.errors)) {
3284 /* Move collected errors to the global context. */
3285 move_errors(&context.errors, &addr_mode_errors.errors);
3286 }
3287 /* Set current error code to the best error collected while
3288 processing addressing modes. */
3289 if (addr_mode_errors.error_code) {
3290 error = addr_mode_errors.error_code;
3291 }
3292 /* Check if there is at least one assembled instruction.*/
3293 if (result->number_of_instructions == 0) {
3294 /* Instruction has been found but can not be assembled in
3295 this form or addressing mode. */
3296 if (!error) {
3297 /* Just in case. Use more general error code, but in
3298 theory it should never happened because acceptors and
3299 optimizers should return more accurate error codes. */
3300 error = FCML_CEH_GEC_INVALID_INSTRUCTION_FORM;
3301 }
3302 } else {
3303 /* Assembling process has succeed so free errors but leave
3304 warning as they are. */
3305 fcml_fn_ceh_free_errors_only_with_level(&context.errors.errors,
3306 FCML_EN_CEH_EL_ERROR);
3307 /* Last error code should be also cleaned. */
3308 error = FCML_CEH_GEC_NO_ERROR;
3309 }
3310
3311 if (!error) {
3312 /* Choose instruction.*/
3313 fcml_fnp_asm_instruction_chooser chooser =
3314 asm_context->configuration.chooser;
3315 if (!chooser) {
3316 chooser = &fcml_fn_asm_default_instruction_chooser;
3317 }
3318
3319 if (result->number_of_instructions == 1) {
3320 /* There is only one instruction so chooser is
3321 not needed. */
3322 result->chosen_instruction = result->instructions;
3323 } else {
3324 /* Prepares chooser context. */
3325 fcml_st_chooser_context chooser_context =
3326 prepare_chooser_context(result->instructions);
3327 /* Chooses most appropriate instruction. */
3328 result->chosen_instruction =
3329 (fcml_st_assembled_instruction*) chooser(
3330 &chooser_context);
3331 }
3332 }
3333 } else {
3334 FCML_TRACE_MSG("There is no addressing mode for given " \
3335 "instruction. It should never happened, so it's an" \
3336 " internal bug.");
3337 error = FCML_CEH_GEC_INTERNAL_ERROR;
3338 }
3339 }
3340
3341 /* Make sure collected errors have been copied to the result. */
3342 result->errors = context.errors.errors;
3343
3344 return error;
3345 }
3346
3347 static void free_ipp_desc(ipp_desc desc) {
3348 if (desc.args_deallocator) {
3349 desc.args_deallocator(desc.args);
3350 }
3351 }
3352
3353 static void free_ipp_chain(ipp_chain *chain) {
3354 if (chain) {
3355 free_ipp_chain(chain->next_processor);
3356 free_ipp_desc(chain->descriptor);
3357 fcml_fn_env_memory_free(chain);
3358 }
3359 }
3360
3361 static void ipp_operand_wrapper_args_deallocator(fcml_ptr ptr) {
3362 ipp_operand_wrapper_args *wrapper_wrgs =
3363 (ipp_operand_wrapper_args*) ptr;
3364 if (wrapper_wrgs->decoded_addr_mode) {
3365 fcml_fnp_def_free_addr_mode(wrapper_wrgs->decoded_addr_mode);
3366 }
3367 fcml_fn_env_memory_free(wrapper_wrgs);
3368 }
3369
3370 static fcml_bool accept_operand_hints(fcml_hints addr_mode_hints,
3371 fcml_hints operand_hints) {
3372 /** If caller expects SIMD addressing mode to be chosen, then
3373 * reject all non SIMD ones. */
3374 if ((operand_hints & FCML_OP_HINT_MULTIMEDIA_INSTRUCTION)
3375 && !(addr_mode_hints & FCML_OP_HINT_MULTIMEDIA_INSTRUCTION)) {
3376 return FCML_FALSE;
3377 }
3378 return FCML_TRUE;
3379 }
3380
3381 static fcml_ceh_error ipp_operand_wrapper_acceptor(
3382 ipp_acceptor_args *args) {
3383 const fcml_st_instruction *instruction = args->instruction;
3384 ipp_operand_wrapper_args *wrapper_args =
3385 (ipp_operand_wrapper_args*) args->args;
3386 const fcml_st_operand *operand =
3387 &(instruction->operands[wrapper_args->operand_index]);
3388 if (wrapper_args->operand_acceptor) {
3389 /* Check hints.*/
3390 fcml_hints operand_hints = operand->hints;
3391 if (!operand_hints || accept_operand_hints(
3392 wrapper_args->hints, operand_hints)) {
3393 operand_acceptor_args acceptor_args = {0};
3394 acceptor_args.context = args->context,
3395 acceptor_args.addr_mode_details = args->addr_mode_details;
3396 acceptor_args.addr_mode_desc = args->addr_mode_def;
3397 acceptor_args.addr_mode = wrapper_args->decoded_addr_mode;
3398 acceptor_args.operand = operand;
3399 acceptor_args.operand_enc = NULL;
3400 return wrapper_args->operand_acceptor(&acceptor_args);
3401 } else {
3402 FCML_TRACE_MSG("Hints do not match.");
3403 return FCML_CEH_GEC_INVALID_OPPERAND;
3404 }
3405 } else {
3406 if (!wrapper_args->operand_defined) {
3407 /* This operand shouldn't be set for the instruction being
3408 * encoded. */
3409 if (operand->type != FCML_OT_NONE) {
3410 return FCML_CEH_GEC_INVALID_OPPERAND;
3411 }
3412 }
3413 return FCML_CEH_GEC_NO_ERROR;
3414 }
3415 }
3416
3417 static fcml_ceh_error ipp_operand_wrapper_encoder(ipp_encoder_args *args) {
3418 ipp_operand_wrapper_args *wrapper_args =
3419 (ipp_operand_wrapper_args*) args->args;
3420 const fcml_st_operand *operand = &(args->context->instruction->
3421 operands[wrapper_args->operand_index]);
3422 if (wrapper_args->operand_encoder) {
3423 operand_encoder_args wrapped_encoder_args = { IPPP_FIRST_PHASE };
3424 wrapped_encoder_args.phase = args->phase;
3425 wrapped_encoder_args.context = args->context;
3426 wrapped_encoder_args.addr_mode_def = args->addr_mode_def;
3427 wrapped_encoder_args.addr_mode = wrapper_args->decoded_addr_mode;
3428 wrapped_encoder_args.operand = operand;
3429 wrapped_encoder_args.operand_enc = args->instruction_part;
3430 return wrapper_args->operand_encoder(&wrapped_encoder_args);
3431 } else {
3432 return FCML_CEH_GEC_NO_ERROR;
3433 }
3434 }
3435
3436 static ipp_desc ipp_operand_wrapper_factory(ipp_factory_args *args,
3437 fcml_ceh_error *error) {
3438
3439 ipp_desc descriptor = { 0 };
3440
3441 FCML_ENV_ALLOC_CLEAR(wrapper_args, ipp_operand_wrapper_args);
3442 if (!wrapper_args) {
3443 *error = FCML_CEH_GEC_OUT_OF_MEMORY;
3444 return descriptor;
3445 }
3446
3447 fcml_uint32_t operand_index = args->flags;
3448 fcml_operand_desc operand_desc = args->addr_mode->operands[operand_index];
3449
3450 if (operand_desc != FCML_NA) {
3451 *error = fcml_fn_def_decode_addr_mode_args(operand_desc,
3452 &(wrapper_args->decoded_addr_mode));
3453 if (*error) {
3454 fcml_fn_env_memory_free(wrapper_args);
3455 return descriptor;
3456 }
3457
3458 operand_encoder_def *def =
3459 &operand_encoders[wrapper_args->decoded_addr_mode->addr_mode];
3460
3461 wrapper_args->operand_encoder = def->encoder;
3462 wrapper_args->operand_acceptor = def->acceptor;
3463 wrapper_args->operand_defined = FCML_TRUE;
3464
3465 if (def->hints_calculator) {
3466 fcml_st_hts_calculated_hints hints = def->hints_calculator(
3467 args->addr_mode, wrapper_args->decoded_addr_mode);
3468 *args->hints |= hints.instruction_hints;
3469 wrapper_args->hints = hints.operand_hints;
3470 }
3471
3472 descriptor.type = IPPT_ENCODER;
3473 } else {
3474 wrapper_args->operand_defined = FCML_FALSE;
3475 /* This operand is not defined for the given instruction,
3476 * so let's treat this IPP as a verifier just in order
3477 * to allow the wrapper to verify if the operand it handles
3478 * is not set for an instruction being encoded. The
3479 * acceptor will be called in order to verify that, but
3480 * instruction part won't be generated for this IPP, because
3481 * encoder won't be even called.
3482 */
3483 descriptor.type = IPPT_VERIFIER;
3484 }
3485
3486 descriptor.args_deallocator = ipp_operand_wrapper_args_deallocator;
3487 descriptor.args = wrapper_args;
3488 descriptor.encoder = ipp_operand_wrapper_encoder;
3489 descriptor.acceptor = ipp_operand_wrapper_acceptor;
3490 wrapper_args->operand_index = operand_index;
3491 return descriptor;
3492 }
3493
3494 /***********************************/
3495 /* XOP/VEX opcode encoder factory. */
3496 /***********************************/
3497
3498 static fcml_ceh_error ipp_AVX_opcode_encoder(ipp_encoder_args *args) {
3499 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3500 inst_part *instruction_part = args->instruction_part;
3501 if (args->phase == IPPP_FIRST_PHASE) {
3502 /* Encoder has to set some flags for XOP/VEX prefix encoder. */
3503 extension_prefixes_fields *epf = &(args->context->epf);
3504 int opcode_bytes = FCML_DEF_OPCODE_FLAGS_OPCODE_NUM(
3505 addr_mode_def->opcode_flags);
3506 int i = 0;
3507 int code_index = 0;
3508 fcml_uint8_t opcode_byte = addr_mode_def->opcode[i++];
3509 switch (opcode_byte) {
3510 case 0x0F: {
3511 opcode_byte = addr_mode_def->opcode[i++];
3512 switch (opcode_byte) {
3513 case 0x38:
3514 epf->mmmm = 0x02;
3515 break;
3516 case 0x3A:
3517 epf->mmmm = 0x03;
3518 break;
3519 default:
3520 instruction_part->code[code_index++] = opcode_byte;
3521 epf->mmmm = 0x01;
3522 break;
3523 }
3524 break;
3525 }
3526 case 0x08:
3527 case 0x09:
3528 case 0x0A:
3529 epf->mmmm = opcode_byte;
3530 break;
3531 }
3532 for (; i < opcode_bytes; i++) {
3533 instruction_part->code[code_index++] = addr_mode_def->opcode[i];
3534 }
3535 instruction_part->code_length = code_index;
3536 }
3537 return FCML_CEH_GEC_NO_ERROR;
3538 }
3539
3540 static ipp_desc ipp_AVX_opcode_encoder_factory(ipp_factory_args *args,
3541 fcml_ceh_error *error) {
3542 ipp_desc descriptor = { 0 };
3543 if (FCML_DEF_PREFIX_IS_AVX_REQ(args->addr_mode->allowed_prefixes)) {
3544 descriptor.type = IPPT_ENCODER;
3545 descriptor.args = NULL;
3546 descriptor.encoder = ipp_AVX_opcode_encoder;
3547 descriptor.acceptor = NULL;
3548 }
3549 return descriptor;
3550 }
3551
3552 /***********************************/
3553 /* Generic primary opcode encoder. */
3554 /***********************************/
3555
3556 typedef fcml_uint8_t (*opcode_byte_encoder)(encoding_context*,
3557 const addr_mode_desc_details*, const fcml_st_def_addr_mode_desc*,
3558 fcml_uint8_t opcode);
3559
3560 typedef struct opcode_byte_encoder_wrapper {
3561 opcode_byte_encoder opcode_byte_encoder;
3562 } opcode_byte_encoder_wrapper;
3563
3564 static fcml_ceh_error ipp_generic_primary_opcode_encoder(ipp_encoder_args *args) {
3565 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3566 inst_part *instruction_part = args->instruction_part;
3567 if (args->phase == IPPP_SECOND_PHASE) {
3568 opcode_byte_encoder opcode_byte_encoder = NULL;
3569 opcode_byte_encoder_wrapper *wrapper = (opcode_byte_encoder_wrapper*)args->args;
3570 if (wrapper) {
3571 opcode_byte_encoder = wrapper->opcode_byte_encoder;
3572 }
3573 int opcode_bytes = FCML_DEF_OPCODE_FLAGS_OPCODE_NUM(
3574 addr_mode_def->opcode_flags);
3575 int primary_opcode = FCML_DEF_OPCODE_FLAGS_PRIMARY_OPCODE(
3576 addr_mode_def->opcode_flags);
3577 int i;
3578 for (i = 0; i < opcode_bytes; i++) {
3579 fcml_uint8_t opcode_byte = addr_mode_def->opcode[i];
3580 instruction_part->code[i] = (i == primary_opcode) ?
3581 opcode_byte_encoder(args->context, args->addr_mode_details,
3582 addr_mode_def, opcode_byte) : opcode_byte;
3583 }
3584 instruction_part->code_length = opcode_bytes;
3585 }
3586
3587 return FCML_CEH_GEC_NO_ERROR;
3588 }
3589
3590 /*********************************************************/
3591 /* Opcode with condition encoded (tttn) encoder factory. */
3592 /*********************************************************/
3593
3594 static fcml_uint8_t conditional_primary_opcode_byte_encoder(
3595 encoding_context *context,
3596 const addr_mode_desc_details *addr_mode_details,
3597 const fcml_st_def_addr_mode_desc *addr_mode_def, fcml_uint8_t opcode) {
3598 return opcode + addr_mode_details->condition.condition_type * 2 +
3599 (addr_mode_details->condition.is_negation ? 1 : 0);
3600 }
3601
3602 static opcode_byte_encoder_wrapper conditional_opcode_encoder_wrapper = {
3603 &conditional_primary_opcode_byte_encoder
3604 };
3605
3606 static ipp_desc ipp_conditional_opcode_encoder_factory(ipp_factory_args *args,
3607 fcml_ceh_error *error) {
3608 ipp_desc descriptor = { 0 };
3609 if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_TTTN(args->addr_mode->opcode_flags)) {
3610 descriptor.type = IPPT_ENCODER;
3611 descriptor.args = &conditional_opcode_encoder_wrapper;
3612 descriptor.encoder = ipp_generic_primary_opcode_encoder;
3613 descriptor.acceptor = NULL;
3614 }
3615 return descriptor;
3616 }
3617
3618 /**************************************************/
3619 /* Opcode with register encoded encoder factory. */
3620 /**************************************************/
3621
3622 static fcml_uint8_t reg_primary_opcode_byte_encoder(
3623 encoding_context *context,
3624 const addr_mode_desc_details *addr_mode_details,
3625 const fcml_st_def_addr_mode_desc *addr_mode_def, fcml_uint8_t opcode) {
3626 return opcode + context->opcode_reg.opcode_reg;
3627 }
3628
3629 static opcode_byte_encoder_wrapper primary_opcode_encoder_wrapper = {
3630 &reg_primary_opcode_byte_encoder
3631 };
3632
3633 static ipp_desc ipp_reg_opcode_encoder_factory(ipp_factory_args *args,
3634 fcml_ceh_error *error) {
3635 ipp_desc descriptor = { 0 };
3636 if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_REG(args->addr_mode->opcode_flags)) {
3637 descriptor.type = IPPT_ENCODER;
3638 descriptor.args = &primary_opcode_encoder_wrapper;
3639 descriptor.encoder = ipp_generic_primary_opcode_encoder;
3640 descriptor.acceptor = NULL;
3641 }
3642 return descriptor;
3643 }
3644
3645 /**********************************/
3646 /* Simple opcode encoder factory. */
3647 /**********************************/
3648
3649 static fcml_ceh_error ipp_simple_opcode_encoder(ipp_encoder_args *args) {
3650 inst_part *instruction_part = args->instruction_part;
3651 if (args->phase == IPPP_FIRST_PHASE) {
3652 int opcode_bytes = FCML_DEF_OPCODE_FLAGS_OPCODE_NUM(
3653 args->addr_mode_def->opcode_flags);
3654 int i;
3655 for (i = 0; i < opcode_bytes; i++) {
3656 instruction_part->code[i] = args->addr_mode_def->opcode[i];
3657 }
3658 instruction_part->code_length = opcode_bytes;
3659 }
3660 return FCML_CEH_GEC_NO_ERROR;
3661 }
3662
3663 static ipp_desc ipp_simple_opcode_encoder_factory(ipp_factory_args *args,
3664 fcml_ceh_error *error) {
3665 ipp_desc descriptor = { 0 };
3666 descriptor.type = IPPT_ENCODER;
3667 descriptor.args = NULL;
3668 descriptor.encoder = ipp_simple_opcode_encoder;
3669 descriptor.acceptor = NULL;
3670 return descriptor;
3671 }
3672
3673 /*******************/
3674 /* Suffix encoder. */
3675 /*******************/
3676
3677 static fcml_ceh_error ipp_instruction_suffix_encoder(ipp_encoder_args *args) {
3678 inst_part *instruction_part = args->instruction_part;
3679 if (args->phase == IPPP_FIRST_PHASE) {
3680 if (args->context->mnemonic->suffix.is_not_null) {
3681 instruction_part->code[0] = args->context->mnemonic->suffix.value;
3682 instruction_part->code_length = 1;
3683 }
3684 }
3685 return FCML_CEH_GEC_NO_ERROR;
3686 }
3687
3688 static ipp_desc ipp_suffix_encoder_factory(ipp_factory_args *args,
3689 fcml_ceh_error *error) {
3690 ipp_desc descriptor = { 0 };
3691 if (FCML_DEF_PREFIX_SUFFIX(args->addr_mode->allowed_prefixes)) {
3692 descriptor.type = IPPT_ENCODER;
3693 descriptor.args = NULL;
3694 descriptor.encoder = ipp_instruction_suffix_encoder;
3695 descriptor.acceptor = NULL;
3696 }
3697 return descriptor;
3698 }
3699
3700 /*******************************/
3701 /* Prefixes encoder factories. */
3702 /*******************************/
3703
3704 /* Branch hints.*/
3705
3706 static fcml_ceh_error ipp_branch_hints_prefix_encoder(ipp_encoder_args *args) {
3707 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3708 inst_part *instruction_part = args->instruction_part;
3709 if (args->phase == IPPP_THIRD_PHASE) {
3710 const fcml_st_instruction *instruction = args->context->instruction;
3711 if (instruction->prefixes & FCML_PREFIX_BRANCH_HINT) {
3712 instruction_part->code[0] = 0x3E;
3713 instruction_part->code_length = 1;
3714 } else if (instruction->prefixes & FCML_PREFIX_NOBRANCH_HINT) {
3715 instruction_part->code[0] = 0x2E;
3716 instruction_part->code_length = 1;
3717 }
3718 }
3719 return error;
3720 }
3721
3722 static ipp_desc ipp_branch_hints_prefix_encoder_factory(
3723 ipp_factory_args *args, fcml_ceh_error *error) {
3724 ipp_desc descriptor = { 0 };
3725 /* Branch hints are allowed only for JCC instructions.*/
3726 if (args->instruction->instruction == F_JCC) {
3727 descriptor.type = IPPT_ENCODER;
3728 descriptor.args = NULL;
3729 descriptor.encoder = ipp_branch_hints_prefix_encoder;
3730 descriptor.acceptor = NULL;
3731 }
3732 return descriptor;
3733 }
3734
3735 /* REP prefix.*/
3736
3737 static fcml_ceh_error ipp_rep_prefix_encoder(ipp_encoder_args *args) {
3738 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3739 inst_part *instruction_part = args->instruction_part;
3740 if (args->phase == IPPP_THIRD_PHASE) {
3741 if (args->context->instruction->prefixes & FCML_PREFIX_REP) {
3742 instruction_part->code[0] = 0xF3;
3743 instruction_part->code_length = 1;
3744 }
3745 }
3746 return error;
3747 }
3748
3749 static ipp_desc ipp_rep_prefix_encoder_factory(ipp_factory_args *args,
3750 fcml_ceh_error *error) {
3751 ipp_desc descriptor = { 0 };
3752 if (FCML_DEF_PREFIX_REP_XRELEASE_ALLOWED(
3753 args->addr_mode->allowed_prefixes) &&
3754 !FCML_DEF_PREFIX_HLE_ENABLED(args->addr_mode->allowed_prefixes)) {
3755 descriptor.type = IPPT_ENCODER;
3756 descriptor.args = NULL;
3757 descriptor.encoder = ipp_rep_prefix_encoder;
3758 descriptor.acceptor = NULL;
3759 }
3760 return descriptor;
3761 }
3762
3763 /* REPNE prefix.*/
3764
3765 static fcml_ceh_error ipp_repne_prefix_encoder(ipp_encoder_args *args) {
3766 inst_part *instruction_part = args->instruction_part;
3767 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3768 if (args->phase == IPPP_THIRD_PHASE) {
3769 if (args->context->instruction->prefixes & FCML_PREFIX_REPNE) {
3770 instruction_part->code[0] = 0xF2;
3771 instruction_part->code_length = 1;
3772 }
3773 }
3774 return error;
3775 }
3776
3777 static ipp_desc ipp_repne_prefix_encoder_factory(ipp_factory_args *args,
3778 fcml_ceh_error *error) {
3779 ipp_desc descriptor = { 0 };
3780 if (FCML_DEF_PREFIX_REPNE_XACQUIRE_ALLOWED(
3781 args->addr_mode->allowed_prefixes) &&
3782 !FCML_DEF_PREFIX_HLE_ENABLED(args->addr_mode->allowed_prefixes)) {
3783 descriptor.type = IPPT_ENCODER;
3784 descriptor.args = NULL;
3785 descriptor.encoder = ipp_repne_prefix_encoder;
3786 descriptor.acceptor = NULL;
3787 }
3788 return descriptor;
3789 }
3790
3791 /* HLE prefixes.*/
3792
3793 static fcml_ceh_error ipp_hle_prefixes_prefix_encoder(ipp_encoder_args *args) {
3794 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3795 encoding_context *context = args->context;
3796 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3797 inst_part *instruction_part = args->instruction_part;
3798 if (args->phase == IPPP_THIRD_PHASE) {
3799 fcml_bool found = FCML_FALSE;
3800 if (context->instruction->prefixes & FCML_PREFIX_XACQUIRE) {
3801 if (FCML_DEF_PREFIX_REPNE_XACQUIRE_ALLOWED(
3802 addr_mode_def->allowed_prefixes)) {
3803 instruction_part->code[0] = 0xF2;
3804 instruction_part->code_length = 1;
3805 found = FCML_TRUE;
3806 } else {
3807 add_error(&context->errors,
3808 fcml_fn_msg_get_message(
3809 FCML_MC_SEGMENT_HLE_PREFIXES_NOT_ALLOWED),
3810 FCML_CEH_MEC_ERROR_HLE_PREFIX_NOT_ALLOWED);
3811 return FCML_CEH_GEC_INVALID_PREFIX;
3812 }
3813 }
3814 if (context->instruction->prefixes & FCML_PREFIX_XRELEASE) {
3815 if (found) {
3816 /* Only one HLA prefix is allowed for instruction.*/
3817 add_error(&context->errors,
3818 fcml_fn_msg_get_message(
3819 FCML_MC_SEGMENT_HLE_MORE_THAN_ONE_PREFIX),
3820 FCML_CEH_MEC_ERROR_HLE_MORE_THAN_ONE_PREFIX);
3821 return FCML_CEH_GEC_INVALID_PREFIX;
3822 }
3823 if (FCML_DEF_PREFIX_REP_XRELEASE_ALLOWED(
3824 addr_mode_def->allowed_prefixes)) {
3825 instruction_part->code[0] = 0xF3;
3826 instruction_part->code_length = 1;
3827 } else {
3828 add_error(&context->errors,
3829 fcml_fn_msg_get_message(
3830 FCML_MC_SEGMENT_HLE_PREFIXES_NOT_ALLOWED),
3831 FCML_CEH_MEC_ERROR_HLE_PREFIX_NOT_ALLOWED);
3832 return FCML_CEH_GEC_INVALID_PREFIX;
3833 }
3834 }
3835 }
3836 return error;
3837 }
3838
3839 static ipp_desc ipp_hle_prefixes_encoder_factory(ipp_factory_args *args,
3840 fcml_ceh_error *error) {
3841 ipp_desc descriptor = { 0 };
3842 if (FCML_DEF_PREFIX_HLE_ENABLED(args->addr_mode->allowed_prefixes)) {
3843 descriptor.type = IPPT_ENCODER;
3844 descriptor.args = NULL;
3845 descriptor.encoder = ipp_hle_prefixes_prefix_encoder;
3846 descriptor.acceptor = NULL;
3847 }
3848 return descriptor;
3849 }
3850
3851 /* Lock prefix.*/
3852
3853 static fcml_ceh_error ipp_lock_prefix_encoder(ipp_encoder_args *args) {
3854 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3855 encoding_context *context = args->context;
3856 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3857 inst_part *instruction_part = args->instruction_part;
3858 if (args->phase == IPPP_THIRD_PHASE) {
3859 /* For instruction that supports LOCK prefixes as well as HLA ones,
3860 * LOCK prefix is mandatory. XCHG is only one exception from
3861 * that rule, and in its case
3862 * lock prefix can be ignored.
3863 */
3864 if (FCML_DEF_PREFIX_HLE_ENABLED(addr_mode_def->allowed_prefixes)
3865 && ((context->instruction->prefixes & FCML_PREFIX_XACQUIRE)
3866 || (context->instruction->prefixes
3867 & FCML_PREFIX_XRELEASE))) {
3868 if (!(context->instruction->prefixes & FCML_PREFIX_LOCK)) {
3869 /* XCHG instruction do not need LOCK prefix.*/
3870 if (addr_mode_def->opcode[0] != 0x86
3871 && addr_mode_def->opcode[0] != 0x87) {
3872 return FCML_CEH_GEC_INVALID_PREFIX;
3873 }
3874 }
3875 }
3876 if (context->instruction->prefixes & FCML_PREFIX_LOCK) {
3877 instruction_part->code[0] = 0xF0;
3878 instruction_part->code_length = 1;
3879 }
3880 }
3881 return error;
3882 }
3883
3884 static ipp_desc ipp_lock_prefix_encoder_factory(ipp_factory_args *args,
3885 fcml_ceh_error *error) {
3886 ipp_desc descriptor = { 0 };
3887 if (FCML_DEF_PREFIX_LOCK_ALLOWED(args->addr_mode->allowed_prefixes)) {
3888 descriptor.type = IPPT_ENCODER;
3889 descriptor.args = NULL;
3890 descriptor.encoder = ipp_lock_prefix_encoder;
3891 descriptor.acceptor = NULL;
3892 }
3893 return descriptor;
3894 }
3895
3896 /* Segment override prefix.*/
3897
3898 static fcml_uint8_t prefix_override_map[] = {
3899 0x26, 0x2e, 0x36, 0x3e, 0x64, 0x65
3900 };
3901
3902 static fcml_ceh_error ipp_segment_override_prefix_encoder(
3903 ipp_encoder_args *args) {
3904 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
3905 inst_part *instruction_part = args->instruction_part;
3906 if (args->phase == IPPP_THIRD_PHASE) {
3907 if (args->context->segment_override.type == FCML_REG_SEG) {
3908 fcml_uint8_t seg_reg_num = args->context->segment_override.reg;
3909 if (seg_reg_num <= 5) {
3910 instruction_part->code[0] = prefix_override_map[seg_reg_num];
3911 instruction_part->code_length = 1;
3912 } else {
3913 /* Wrong register type. This check is done just is case,
3914 * this code should never be invoked due to earlier
3915 * sanity checks. */
3916 FCML_TRACE_MSG("Unknown segment register found.");
3917 error = FCML_CEH_GEC_INTERNAL_ERROR;
3918 }
3919 }
3920 }
3921 return error;
3922 }
3923
3924 static ipp_desc ipp_segment_override_prefix_encoder_factory(
3925 ipp_factory_args *args, fcml_ceh_error *error) {
3926 ipp_desc descriptor = { 0 };
3927 descriptor.type = IPPT_ENCODER;
3928 descriptor.args = NULL;
3929 descriptor.encoder = ipp_segment_override_prefix_encoder;
3930 descriptor.acceptor = NULL;
3931 return descriptor;
3932 }
3933
3934 /* Mandatory prefixes.*/
3935
3936 static fcml_ceh_error ipp_mandatory_prefixes_encoder(
3937 ipp_encoder_args *args) {
3938 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3939 inst_part *instruction_part = args->instruction_part;
3940 if (args->phase == IPPP_THIRD_PHASE) {
3941 fcml_uint8_t prefix = 0x66;
3942 if (FCML_DEF_PREFIX_MANDATORY_F2(addr_mode_def->allowed_prefixes)) {
3943 prefix = 0xF2;
3944 } else if (FCML_DEF_PREFIX_MANDATORY_F3(
3945 addr_mode_def->allowed_prefixes)) {
3946 prefix = 0xF3;
3947 }
3948 instruction_part->code[0] = prefix;
3949 instruction_part->code_length = 1;
3950 }
3951 return FCML_CEH_GEC_NO_ERROR;
3952 }
3953
3954 static ipp_desc ipp_mandatory_prefixes_encoder_factory(ipp_factory_args *args,
3955 fcml_ceh_error *error) {
3956
3957 ipp_desc descriptor = { 0 };
3958 const fcml_st_def_addr_mode_desc *addr_mode = args->addr_mode;
3959
3960 fcml_bool is_mandatory =
3961 FCML_DEF_PREFIX_MANDATORY_66(addr_mode->allowed_prefixes) ||
3962 FCML_DEF_PREFIX_MANDATORY_F2(addr_mode->allowed_prefixes) ||
3963 FCML_DEF_PREFIX_MANDATORY_F3(addr_mode->allowed_prefixes);
3964
3965 /* Mandatory prefixes cannot be applied to AVX instructions, as they
3966 are encoded in a different way there, as parts of
3967 their VEX/XOP/EVEX prefixes. */
3968 fcml_bool is_avx = FCML_DEF_PREFIX_IS_AVX_REQ(addr_mode->allowed_prefixes);
3969
3970 if (is_mandatory && !is_avx) {
3971 descriptor.type = IPPT_ENCODER;
3972 descriptor.args = NULL;
3973 descriptor.encoder = ipp_mandatory_prefixes_encoder;
3974 descriptor.acceptor = NULL;
3975 }
3976
3977 return descriptor;
3978 }
3979
3980 /* 66 prefix. */
3981
3982 static fcml_ceh_error ipp_66_prefix_encoder(ipp_encoder_args *args) {
3983 encoding_context *context = args->context;
3984 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
3985 inst_part *instruction_part = args->instruction_part;
3986 if (args->phase != IPPP_THIRD_PHASE) {
3987 return FCML_CEH_GEC_NO_ERROR;
3988 }
3989 fcml_bool encode = FCML_FALSE;
3990 fcml_uint64_t addr_type = addr_mode_def->instruction_group;
3991
3992 const fcml_st_entry_point *entry_point =
3993 &(context->assembler_context->entry_point);
3994 fcml_usize osa = entry_point->operand_size_attribute;
3995 fcml_usize eosa = context->optimizer_processing_details.eosa;
3996 encode = (osa == FCML_DS_16 && eosa == FCML_DS_32)
3997 || (osa == FCML_DS_32 && eosa == FCML_DS_16)
3998 || (osa == FCML_DS_64 && eosa == FCML_DS_32);
3999
4000 if (encode) {
4001 instruction_part->code[0] = 0x66;
4002 instruction_part->code_length = 1;
4003 }
4004
4005 return FCML_CEH_GEC_NO_ERROR;
4006 }
4007
4008 static ipp_desc ipp_66_prefix_encoder_factory(
4009 ipp_factory_args *args, fcml_ceh_error *error) {
4010 ipp_desc descriptor = { 0 };
4011 /* SIMD instructions do not need 0x66 to change EOSA. */
4012 fcml_bool is_simd = args->addr_mode->instruction_group & FCML_AMT_SIMD;
4013 /* Mandatory prefixes are handled by a dedicated IPP. */
4014 fcml_bool is_mandatory = FCML_DEF_PREFIX_MANDATORY_66(
4015 args->addr_mode->allowed_prefixes);
4016 if (!is_mandatory && !is_simd) {
4017 descriptor.type = IPPT_ENCODER;
4018 descriptor.args = NULL;
4019 descriptor.encoder = ipp_66_prefix_encoder;
4020 descriptor.acceptor = NULL;
4021 }
4022 return descriptor;
4023 }
4024
4025 /* 67 prefix. */
4026
4027 static fcml_ceh_error ipp_67_prefix_encoder(ipp_encoder_args *args) {
4028 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
4029 encoding_context *context = args->context;
4030 inst_part *instruction_part = args->instruction_part;
4031 if (args->phase == IPPP_THIRD_PHASE) {
4032 /* If effective address size is not set, it means that instruction
4033 * is not interested in ASA and just doesn't use it. */
4034 const fcml_st_entry_point *entry_point =
4035 &(context->assembler_context->entry_point);
4036 fcml_usize asa = entry_point->address_size_attribute;
4037 fcml_usize easa = context->optimizer_processing_details.easa;
4038 if (easa && (asa != easa)) {
4039 if ((asa == FCML_DS_16 && easa == FCML_DS_32)
4040 || (asa == FCML_DS_32 && easa == FCML_DS_16)
4041 || (asa == FCML_DS_64 && easa == FCML_DS_32)) {
4042 instruction_part->code[0] = 0x67;
4043 instruction_part->code_length = 1;
4044 } else {
4045 error = FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
4046 }
4047 }
4048 }
4049 return error;
4050 }
4051
4052 static ipp_desc ipp_67_prefix_encoder_factory(ipp_factory_args *args,
4053 fcml_ceh_error *error) {
4054 ipp_desc descriptor = { 0 };
4055 /* 67 prefix cannot be applied to AVX instructions. */
4056 if (!FCML_DEF_PREFIX_IS_AVX_REQ(args->addr_mode->allowed_prefixes)) {
4057 descriptor.type = IPPT_ENCODER;
4058 descriptor.args = NULL;
4059 descriptor.encoder = ipp_67_prefix_encoder;
4060 descriptor.acceptor = NULL;
4061 }
4062 return descriptor;
4063 }
4064
4065 /************/
4066 /** EVEX **/
4067 /************/
4068
4069 static fcml_uint8_t encode_pp_prefix_field(fcml_uint32_t allowed_prefixes) {
4070 fcml_uint8_t pp = 0;
4071 if (FCML_DEF_PREFIX_MANDATORY_66(allowed_prefixes)) {
4072 pp = 0x01;
4073 } else if (FCML_DEF_PREFIX_MANDATORY_F2(allowed_prefixes)) {
4074 pp = 0x03;
4075 } else if (FCML_DEF_PREFIX_MANDATORY_F3(allowed_prefixes)) {
4076 pp = 0x02;
4077 }
4078 return pp;
4079 }
4080
4081 /* Encodes decorators if there are any defined for an operand. */
4082 static void encode_decorators(encoding_context *context,
4083 const fcml_st_def_addr_mode_desc *addr_mode_def,
4084 fcml_operand_decorators decorators,
4085 const fcml_st_operand *operand) {
4086
4087
4088 /* Opmask decorator. */
4089 if (FCML_IS_DECOR_OPMASK_REG(decorators)) {
4090 if (operand->decorators.operand_mask_reg.type == FCML_REG_OPMASK) {
4091 context->epf.aaa = operand->decorators.operand_mask_reg.reg;
4092 }
4093 }
4094
4095 /* Zeroing-masking is not supported by instructions that write to memory. */
4096 if (operand->type == FCML_OT_REGISTER && FCML_IS_DECOR_Z(decorators)) {
4097 context->epf.z = operand->decorators.z;
4098 }
4099
4100 if (FCML_IS_DECOR_SAE(decorators) || FCML_IS_DECOR_ER(decorators)) {
4101 fcml_uint8_t tuple_type = FCML_GET_SIMD_TUPLETYPE(addr_mode_def->details);
4102 /* For these tuple types vector length is explicitly set and
4103 * and there is no need to set L, L' flags. */
4104 if(tuple_type == FCML_TT_FV || tuple_type == FCML_TT_T1S || tuple_type == FCML_TT_T1F) {
4105 context->epf.explicit_vector_length = FCML_TRUE;
4106 }
4107 }
4108 }
4109
4110 static fcml_ceh_error ipp_EVEX_prefix_encoder(ipp_encoder_args *args) {
4111 encoding_context *context = args->context;
4112 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4113 inst_part *instruction_part = args->instruction_part;
4114
4115 if (args->phase == IPPP_FIRST_PHASE) {
4116 fcml_bool l0 = FCML_DEF_PREFIX_L_0(addr_mode_def->allowed_prefixes);
4117 fcml_bool l1 = FCML_DEF_PREFIX_L_1(addr_mode_def->allowed_prefixes);
4118 fcml_bool l_prim_0 = FCML_DEF_PREFIX_L_prim_0(addr_mode_def->allowed_prefixes);
4119 fcml_bool l_prim_1 = FCML_DEF_PREFIX_L_prim_1(addr_mode_def->allowed_prefixes);
4120
4121 if (!l_prim_1) {
4122 if (l1) {
4123 context->optimizer_processing_details.vector_length = FCML_DS_256;
4124 } else if (l0) {
4125 context->optimizer_processing_details.vector_length = FCML_DS_128;
4126 }
4127 } else if (!l1) {
4128 context->optimizer_processing_details.vector_length = FCML_DS_512;
4129 }
4130
4131 // Some decorators can be encoded here globally, as we do not need any
4132 // custom code dedicated for given operand type. The only information
4133 // which is needed here is a list of supported decorators and actual
4134 // decorators from the instruction model for a given operand. In fact
4135 // we don't even care if we are dealing with a register operand or a
4136 // memory addressing as long as the operand supports decorators.
4137 for (int i = 0; i < context->instruction->operands_count; i++) {
4138 fcml_operand_decorators supported_decorators =
4139 FCML_DECORATORS(addr_mode_def->operands[i]);
4140 const fcml_st_operand *operand = &context->instruction->operands[i];
4141 encode_decorators(context, addr_mode_def, supported_decorators, operand);
4142 }
4143 }
4144
4145 if (args->phase == IPPP_SECOND_PHASE) {
4146 /* If vector length wasn't set in the first phase, we have to assume
4147 * that 128-bit vector length is used. Properly set vector length is
4148 * needed to encode compressed disp8 for instance. */
4149 // TODO: Vector length is used by fcml_ifn_asm_ipp_ModRM_encoder in the second phase.
4150 // We should consider moving this code there or somewhere else to make sure
4151 // it's properly set in the second phase.
4152 if (!context->optimizer_processing_details.vector_length) {
4153 context->optimizer_processing_details.vector_length = FCML_DS_128;
4154 }
4155 }
4156
4157 if (args->phase == IPPP_THIRD_PHASE) {
4158
4159 extension_prefixes_fields *epf = &(context->epf);
4160 fcml_st_encoded_modrm *encoded_mod_rm = &(context->encoded_mod_rm);
4161
4162 fcml_uint8_t p0 = 0;
4163 p0 = FCML_ENCODE_EVEX_P0_R(p0, encoded_mod_rm->ext_R);
4164 p0 = FCML_ENCODE_EVEX_P0_X(p0, encoded_mod_rm->ext_X);
4165 p0 = FCML_ENCODE_EVEX_P0_B(p0, encoded_mod_rm->ext_B);
4166 p0 = FCML_ENCODE_EVEX_P0_R_prim(p0, encoded_mod_rm->ext_R_prim);
4167 p0 = FCML_ENCODE_EVEX_P0_mm(p0, epf->mmmm);
4168
4169 /* Third bit of P1 is always set to 1. */
4170 fcml_uint8_t p1 = 0x04;
4171 p1 = FCML_ENCODE_EVEX_P1_W(p1, FCML_DEF_PREFIX_W_1(
4172 addr_mode_def->allowed_prefixes));
4173 p1 = FCML_ENCODE_EVEX_P1_vvvv(p1, epf->vvvv);
4174 p1 = FCML_ENCODE_EVEX_P1_pp(p1, encode_pp_prefix_field(
4175 addr_mode_def->allowed_prefixes));
4176
4177 fcml_uint8_t p2 = 0;
4178 p2 = FCML_ENCODE_EVEX_P2_z(p2, epf->z);
4179
4180 if (epf->er.is_not_null) {
4181 /* Embedded rounding mode is encoded on EVEX.L'L. */
4182 p2 = FCML_ENCODE_EVEX_P2_LL(p2, epf->er.value);
4183 } else {
4184 fcml_uint8_t LL = 0;
4185 if (!context->epf.explicit_vector_length) {
4186 switch(context->optimizer_processing_details.vector_length) {
4187 case FCML_DS_256:
4188 LL = 0x01;
4189 break;
4190 case FCML_DS_512:
4191 LL = 0x02;
4192 break;
4193 }
4194 }
4195 p2 = FCML_ENCODE_EVEX_P2_LL(p2, LL);
4196 }
4197
4198 p2 = FCML_ENCODE_EVEX_P2_b(p2, epf->b);
4199 /* EVEX.V' is used by extended VSIB encoding and direct register
4200 encoding. */
4201 p2 = FCML_ENCODE_EVEX_P2_V_prim(p2, encoded_mod_rm->ext_V_prim |
4202 epf->V_prim);
4203 p2 = FCML_ENCODE_EVEX_P2_aaa(p2, epf->aaa);
4204
4205 instruction_part->code[0] = 0x62;
4206 instruction_part->code[1] = p0;
4207 instruction_part->code[2] = p1;
4208 instruction_part->code[3] = p2;
4209 instruction_part->code_length = FCML_ENCODE_EVEX_PREFIX_LENGTH;
4210 }
4211
4212 return FCML_CEH_GEC_NO_ERROR;
4213 }
4214
4215 static ipp_desc ipp_EVEX_prefix_encoder_factory(ipp_factory_args *args,
4216 fcml_ceh_error *error) {
4217 ipp_desc descriptor = { 0 };
4218 if (FCML_DEF_PREFIX_EVEX_REQ(args->addr_mode->allowed_prefixes)) {
4219 descriptor.type = IPPT_ENCODER;
4220 descriptor.args = NULL;
4221 descriptor.encoder = ipp_EVEX_prefix_encoder;
4222 descriptor.acceptor = NULL;
4223 }
4224 return descriptor;
4225 }
4226
4227 /********************/
4228 /** VEX/XOP Prefix **/
4229 /********************/
4230
4231 static fcml_ceh_error ipp_VEX_XOP_prefix_encoder(ipp_encoder_args *args) {
4232
4233 encoding_context *context = args->context;
4234 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4235 inst_part *instruction_part = args->instruction_part;
4236
4237 if (args->phase == IPPP_FIRST_PHASE) {
4238 if (FCML_DEF_PREFIX_L_1(addr_mode_def->allowed_prefixes)) {
4239 context->optimizer_processing_details.vector_length = FCML_DS_256;
4240 } else if (FCML_DEF_PREFIX_L_0(addr_mode_def->allowed_prefixes)) {
4241 context->optimizer_processing_details.vector_length = FCML_DS_128;
4242 }
4243 } else if (args->phase == IPPP_THIRD_PHASE) {
4244 extension_prefixes_fields *epf = &(context->epf);
4245 fcml_st_encoded_modrm *encoded_mod_rm = &(context->encoded_mod_rm);
4246 /* Check if one byte VEX encoding can be used.*/
4247 fcml_bool is_vex = FCML_DEF_PREFIX_VEX_REQ(
4248 addr_mode_def->allowed_prefixes);
4249 /* Remember, the 2-byte VEX implies a leading 0Fh opcode byte,
4250 * it's why "m-mmmm" field should be 1. */
4251 fcml_bool is_two_bytes_vex =
4252 (!context->assembler_context->configuration.force_three_byte_VEX
4253 && is_vex && epf->mmmm == 0x01
4254 && !FCML_DEF_PREFIX_W_1(addr_mode_def->allowed_prefixes)
4255 && !encoded_mod_rm->ext_X && !encoded_mod_rm->ext_B);
4256
4257 fcml_uint8_t prefix_bytes[3];
4258 fcml_uint8_t prefix_size = 0;
4259
4260 /* Encode PP.*/
4261 fcml_uint8_t pp = encode_pp_prefix_field(
4262 addr_mode_def->allowed_prefixes);
4263
4264 fcml_uint8_t prefix = 0;
4265
4266 /* If an instruction syntax can be encoded using the two-byte form,
4267 * it can also be encoded using the three byte form of VEX. Three byte
4268 * VEX can be forced using configuration or "long_form"
4269 * instruction level hint. */
4270 if (context->assembler_context->configuration.force_three_byte_VEX
4271 || (context->instruction->hints &
4272 FCML_HINT_LONG_FORM_POINTER)) {
4273 is_two_bytes_vex = FCML_FALSE;
4274 }
4275
4276 fcml_usize vector_length =
4277 context->optimizer_processing_details.vector_length;
4278
4279 /* VEX/XOP supports 128/256 vector length only. */
4280 if (vector_length > FCML_DS_256) {
4281 add_error(&context->errors,
4282 fcml_fn_msg_get_message(
4283 FCML_MC_CEH_GEC_INVALID_OPPERAND_SIZE),
4284 FCML_CEH_MEC_ERROR_INVALID_VECTOR_LENGTH);
4285 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
4286 }
4287
4288 if (is_two_bytes_vex) {
4289 /* Two bytes VEX prefix.*/
4290 prefix = FCML_ENCODE_VEXOP_R(prefix, encoded_mod_rm->ext_R);
4291 prefix = FCML_ENCODE_VEXOP_VVVV(prefix, epf->vvvv);
4292 if (vector_length == FCML_DS_256) {
4293 prefix = FCML_ENCODE_VEXOP_L(prefix, 0x01);
4294 }
4295 prefix = FCML_ENCODE_VEXOP_PP(prefix, pp);
4296 prefix_bytes[0] = 0xC5;
4297 prefix_bytes[1] = prefix;
4298 prefix_size = 2;
4299 } else {
4300 /* Three bytes VEX or XOP prefix. */
4301 prefix_bytes[0] = (is_vex) ? 0xC4 : 0x8F;
4302 prefix = FCML_ENCODE_VEXOP_R(prefix, encoded_mod_rm->ext_R);
4303 prefix = FCML_ENCODE_VEXOP_X(prefix, encoded_mod_rm->ext_X);
4304 prefix = FCML_ENCODE_VEXOP_B(prefix, encoded_mod_rm->ext_B);
4305 prefix = FCML_ENCODE_VEXOP_MMMM(prefix, epf->mmmm);
4306 prefix_bytes[1] = prefix;
4307 prefix = 0;
4308 prefix = FCML_ENCODE_VEXOP_W(prefix,
4309 FCML_DEF_PREFIX_W_1(addr_mode_def->allowed_prefixes));
4310 prefix = FCML_ENCODE_VEXOP_VVVV(prefix, epf->vvvv);
4311 if (vector_length == FCML_DS_256) {
4312 prefix = FCML_ENCODE_VEXOP_L(prefix, 0x01);
4313 }
4314 prefix = FCML_ENCODE_VEXOP_PP(prefix, pp);
4315 prefix_bytes[2] = prefix;
4316 prefix_size = 3;
4317 }
4318
4319 int i;
4320 for (i = 0; i < prefix_size; i++) {
4321 instruction_part->code[i] = prefix_bytes[i];
4322 }
4323 instruction_part->code_length = prefix_size;
4324 }
4325
4326 return FCML_CEH_GEC_NO_ERROR;
4327 }
4328
4329 static ipp_desc ipp_VEX_XOP_prefix_encoder_factory(
4330 ipp_factory_args *args, fcml_ceh_error *error) {
4331 ipp_desc descriptor = { 0 };
4332 if (FCML_DEF_PREFIX_VEX_REQ(args->addr_mode->allowed_prefixes) ||
4333 FCML_DEF_PREFIX_XOP_REQ(args->addr_mode->allowed_prefixes)) {
4334 descriptor.type = IPPT_ENCODER;
4335 descriptor.args = NULL;
4336 descriptor.encoder = ipp_VEX_XOP_prefix_encoder;
4337 descriptor.acceptor = NULL;
4338 }
4339 return descriptor;
4340 }
4341
4342 /* REX prefix.*/
4343
4344 static fcml_ceh_error ipp_REX_prefix_encoder(ipp_encoder_args *args) {
4345 encoding_context *context = args->context;
4346 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4347 inst_part *instruction_part = args->instruction_part;
4348
4349 if (args->phase == IPPP_THIRD_PHASE) {
4350 /* REX prefix is only available in 64 bit mode. Neither VEX nor
4351 * XOP are allowed here, but it's checked before this encoder
4352 * is registered for
4353 */
4354 if (context->assembler_context->entry_point.op_mode == FCML_OM_64_BIT) {
4355
4356 fcml_uint8_t rex = FCML_ENCODE_REX_BASE;
4357 fcml_st_asm_optimizer_processing_details *size_flags =
4358 &(context->optimizer_processing_details);
4359 fcml_st_encoded_modrm *encoded_mod_rm = &(context->encoded_mod_rm);
4360 fcml_st_modrm *mod_rm = &(context->mod_rm);
4361
4362 /* W field.*/
4363 fcml_usize eosa = size_flags->eosa;
4364 if (FCML_DEF_PREFIX_W_1(addr_mode_def->allowed_prefixes)) {
4365 if (eosa != 0 && eosa != FCML_DS_64) {
4366 /* Operand size should be 64 bits.*/
4367 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
4368 }
4369 rex = FCML_ENCODE_REX_W(rex, 1);
4370 } else if (eosa == FCML_DS_64
4371 && !FCML_DEF_OPCODE_FLAGS_FORCE_64BITS_EOSA(
4372 addr_mode_def->opcode_flags) &&
4373 !FCML_DEF_OPCODE_FLAGS_64BITS_EOSA_BY_DEFAULT(
4374 addr_mode_def->opcode_flags)) {
4375 /* When OSA is forced to 64 bits, W flag is needless. */
4376 rex = FCML_ENCODE_REX_W(rex, 1);
4377 }
4378
4379 /* Fields.*/
4380 rex = FCML_ENCODE_REX_R(rex, encoded_mod_rm->ext_R);
4381 rex = FCML_ENCODE_REX_X(rex, encoded_mod_rm->ext_X);
4382 rex = FCML_ENCODE_REX_B(rex, encoded_mod_rm->ext_B);
4383 rex = FCML_ENCODE_REX_B(rex, context->opcode_reg.ext_b);
4384
4385 /* Assembler configuration.*/
4386 const fcml_st_assembler_conf *cfg =
4387 &(context->assembler_context->configuration);
4388
4389 /* Even if REX do not contains any flags set in some cases
4390 * registers BPL, SPL, DIL, SIL needs REX to be defined.
4391 * Additionally we can force it to occur by setting a
4392 * configuration flag. */
4393 if (rex != FCML_ENCODE_REX_BASE
4394 || (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_MODRM(
4395 addr_mode_def->opcode_flags)
4396 && cfg->force_rex_prefix)
4397 || mod_rm->reg_opcode_needs_rex
4398 || context->reg_opcode_needs_rex) {
4399 instruction_part->code[0] = rex;
4400 instruction_part->code_length = 1;
4401 }
4402 }
4403 }
4404
4405 return FCML_CEH_GEC_NO_ERROR;
4406 }
4407
4408 static ipp_desc ipp_REX_prefix_encoder_factory(ipp_factory_args *args,
4409 fcml_ceh_error *error) {
4410
4411 ipp_desc descriptor = { 0 };
4412
4413 /* 66 prefix cannot be applied to AVX instructions.
4414 * Remember that this prefix can be a mandatory one.
4415 */
4416 if (FCML_DEF_OPCODE_FLAGS_64_BIT_MODE_SUPPORTED(args->addr_mode->opcode_flags)
4417 && !FCML_DEF_PREFIX_IS_AVX_REQ(args->addr_mode->allowed_prefixes)) {
4418 descriptor.type = IPPT_ENCODER;
4419 descriptor.args = NULL;
4420 descriptor.encoder = ipp_REX_prefix_encoder;
4421 descriptor.acceptor = NULL;
4422 }
4423
4424 return descriptor;
4425 }
4426
4427 /*****************************/
4428 /* ModR/M encoder factories. */
4429 /*****************************/
4430
4431 static fcml_ceh_error ipp_rip_post_processor(encoding_context *context,
4432 inst_part *instruction_part, fcml_ptr post_processor_args) {
4433
4434 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
4435 const fcml_st_assembler_context *assembler_context =
4436 context->assembler_context;
4437 fcml_st_encoded_modrm *encoded_mod_rm = &(context->encoded_mod_rm);
4438
4439 if (FCML_IS_NULL(context->instruction_size)) {
4440 /* Should never happened.*/
4441 FCML_TRACE_MSG("instruction size has to be set here.");
4442 error = FCML_CEH_GEC_INTERNAL_ERROR;
4443 } else {
4444 /* Encode ModR/M and displacement.*/
4445 fcml_st_memory_stream stream = inst_part_stream(
4446 instruction_part);
4447 fcml_fn_stream_write(&stream, encoded_mod_rm->modrm);
4448 error = fcml_fn_modrm_encode_rip_offset(&stream,
4449 assembler_context->entry_point.ip,
4450 context->instruction_size.value, encoded_mod_rm);
4451 }
4452 return error;
4453 }
4454
4455 static fcml_ceh_error ipp_ModRM_encoder(ipp_encoder_args *args) {
4456 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
4457 encoding_context *context = args->context;
4458 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4459 const fcml_st_assembler_context *assembler_context =
4460 context->assembler_context;
4461 inst_part *instruction_part = args->instruction_part;
4462
4463 if (args->phase == IPPP_SECOND_PHASE) {
4464 fcml_st_modrm_encoder_context ctx;
4465 ctx.op_mode = assembler_context->entry_point.op_mode;
4466 /* Chooses relative or absolute addressing. Configuration as well
4467 * as rel/abs hints take effect only in 64 bit addressing mode. */
4468 ctx.choose_rip_encoding = !assembler_context->configuration.choose_abs_encoding;
4469 ctx.choose_sib_encoding = assembler_context->configuration.choose_sib_encoding;
4470
4471 /* Hints have higher precedence than configuration. */
4472 if (context->modrm_hints.is_sib_alternative_hint) {
4473 ctx.choose_sib_encoding = FCML_TRUE;
4474 /* SIB hint has higher precedence than RIP configuration. There
4475 * is no way to encode RIP using SIB, so RIP flag has to be
4476 * cleared in order to encode absolute offset using requested
4477 * SIB encoding. */
4478 ctx.choose_rip_encoding = FCML_FALSE;
4479 }
4480
4481 if (context->modrm_hints.is_rel_alternative_hint) {
4482 /* RIP encoding has been forced by using "rel" hint. */
4483 ctx.choose_rip_encoding = FCML_TRUE;
4484 }
4485
4486 if (context->modrm_hints.is_abs_alternative_hint) {
4487 /* Absolute offset encoding has been forced by using "abs" hint. */
4488 ctx.choose_rip_encoding = FCML_FALSE;
4489 }
4490
4491 ctx.chosen_effective_address_size = 0;
4492 ctx.effective_address_size = get_effective_address_size(context);
4493 ctx.input_size = FCML_GET_SIMD_ELEMENT_SIZE(addr_mode_def->details);
4494 ctx.vector_length = context->optimizer_processing_details.vector_length;
4495 ctx.is_sib_alternative = FCML_FALSE;
4496
4497 /* EVEX, compressed disp8. */
4498 ctx.b = context->epf.b;
4499 ctx.is_evex = FCML_DEF_PREFIX_EVEX_REQ(addr_mode_def->allowed_prefixes);
4500 ctx.tuple_type = FCML_GET_SIMD_TUPLETYPE(addr_mode_def->details);
4501
4502 if (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_EXT(addr_mode_def->opcode_flags)) {
4503 context->mod_rm.reg_opcode = FCML_DEF_OPCODE_FLAGS_OPCODE_EXT(
4504 addr_mode_def->opcode_flags);
4505 }
4506
4507 /* Encodes ModR/M bytes.*/
4508 error = fcml_fn_modrm_encode(&ctx, &(context->mod_rm),
4509 &(context->encoded_mod_rm));
4510 if (!error) {
4511 if (context->optimizer_processing_details.easa
4512 == ctx.chosen_effective_address_size) {
4513 if (context->encoded_mod_rm.is_rip
4514 && !context->encoded_mod_rm.is_rip_encoded) {
4515 /* ModR/M + 4bytes displacement.*/
4516 instruction_part->code_length = 5;
4517 instruction_part->post_processor = ipp_rip_post_processor;
4518 instruction_part->post_processor_args = NULL;
4519 } else {
4520 fcml_st_memory_stream stream =
4521 inst_part_stream(instruction_part);
4522 fcml_st_encoded_modrm *encoded_modrm =
4523 &(context->encoded_mod_rm);
4524 fcml_fn_stream_write(&stream, encoded_modrm->modrm);
4525 if (encoded_modrm->sib.is_not_null) {
4526 fcml_fn_stream_write(&stream, encoded_modrm->sib.value);
4527 }
4528 if (encoded_modrm->displacement_size) {
4529 fcml_fn_stream_write_bytes(&stream,
4530 &(encoded_modrm->displacement),
4531 encoded_modrm->displacement_size);
4532 }
4533 instruction_part->code_length = stream.offset;
4534 }
4535 } else {
4536 /* ModRM encoded to the form not supported by current
4537 * addressing mode. For example, addressing mode needs ASA 32,
4538 * but assembled ModRM required 16 bit ASA. */
4539 error = FCML_CEH_GEC_INVALID_ADDRESS_SIZE;
4540 }
4541 }
4542 }
4543 return error;
4544 }
4545
4546 static ipp_desc ipp_ModRM_encoder_factory(ipp_factory_args *args,
4547 fcml_ceh_error *error) {
4548 ipp_desc descriptor = { 0 };
4549 /* Check if instruction has ModR/M byte.*/
4550 if (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_MODRM(args->addr_mode->opcode_flags)) {
4551 descriptor.type = IPPT_ENCODER;
4552 descriptor.args = NULL;
4553 descriptor.encoder = ipp_ModRM_encoder;
4554 descriptor.acceptor = NULL;
4555 }
4556 return descriptor;
4557 }
4558
4559 /*******************************/
4560 /* Addressing mode validators. */
4561 /*******************************/
4562
4563 static fcml_ceh_error ipp_addr_mode_acceptor(ipp_acceptor_args *args) {
4564 struct acceptor_ctx *context = args->context;
4565 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4566 const addr_mode_desc_details *addr_mode_details = args->addr_mode_details;
4567 fcml_en_operating_mode op_mode = context->entry_point->op_mode;
4568 struct acceptor_optimizer_ctx *optimizer_ctx = &(context->optimizer_ctx);
4569 fcml_uint32_t opcode_flags = addr_mode_def->opcode_flags;
4570
4571 if (!FCML_DEF_OPCODE_FLAGS_64_BIT_MODE_SUPPORTED(opcode_flags)
4572 && op_mode == FCML_OM_64_BIT) {
4573 return FCML_CEH_GEC_INVALID_OPERATING_MODE;
4574 } else if (!FCML_DEF_OPCODE_FLAGS_16_32_BIT_MODE_SUPPORTED(opcode_flags)
4575 && (op_mode == FCML_OM_16_BIT || op_mode == FCML_OM_32_BIT)) {
4576 return FCML_CEH_GEC_INVALID_OPERATING_MODE;
4577 }
4578
4579 /* Set restrictions if there are any.*/
4580 if (addr_mode_details->allowed_osa != FCML_EN_ASF_ANY) {
4581 optimizer_ctx->allowed_eosa.flags = addr_mode_details->allowed_osa;
4582 optimizer_ctx->allowed_eosa.is_set = FCML_TRUE;
4583 }
4584
4585 if (addr_mode_details->allowed_asa != FCML_EN_ASF_ANY) {
4586 optimizer_ctx->allowed_easa.flags = addr_mode_details->allowed_asa;
4587 optimizer_ctx->allowed_easa.is_set = FCML_TRUE;
4588 }
4589
4590 /* Force 64 bit OSA in 64 bit addressing mode.*/
4591 if (op_mode == FCML_OM_64_BIT) {
4592 if (FCML_DEF_OPCODE_FLAGS_FORCE_64BITS_EOSA(opcode_flags)) {
4593 optimizer_ctx->allowed_eosa.flags = FCML_EN_ASF_64;
4594 optimizer_ctx->allowed_eosa.is_set = FCML_TRUE;
4595 } else if (FCML_DEF_OPCODE_FLAGS_64BITS_EOSA_BY_DEFAULT(opcode_flags)) {
4596 /* Remember, if 64 bit EOSA is used by default it can
4597 * be overridden to 16 bits only. */
4598 optimizer_ctx->allowed_eosa.flags =
4599 (FCML_EN_ASF_64 | FCML_EN_ASF_16);
4600 optimizer_ctx->allowed_eosa.is_set = FCML_TRUE;
4601 }
4602 }
4603 return FCML_CEH_GEC_NO_ERROR;
4604 }
4605
4606 static ipp_desc ipp_addr_mode_acceptor_factory(ipp_factory_args *args,
4607 fcml_ceh_error *error) {
4608 ipp_desc descriptor = { 0 };
4609 descriptor.type = IPPT_VERIFIER;
4610 descriptor.args = NULL;
4611 descriptor.encoder = NULL;
4612 descriptor.acceptor = ipp_addr_mode_acceptor;
4613 return descriptor;
4614 }
4615
4616 /************************/
4617 /* Prefixes validators. */
4618 /************************/
4619
4620 static fcml_ceh_error ipp_prefixes_acceptor(ipp_acceptor_args *args) {
4621 /* Check if LOCK prefix is allowed for given addressing mode.*/
4622 const fcml_st_instruction *instruction = args->instruction;
4623 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4624 if (instruction->prefixes) {
4625 if ((instruction->prefixes & FCML_PREFIX_LOCK)
4626 && !FCML_DEF_PREFIX_LOCK_ALLOWED(
4627 addr_mode_def->allowed_prefixes)) {
4628 return FCML_CEH_GEC_INVALID_PREFIX;
4629 }
4630 /* Check if REP prefix is allowed for given addressing mode.*/
4631 if ((instruction->prefixes & FCML_PREFIX_REP)
4632 && !FCML_DEF_PREFIX_REP_XRELEASE_ALLOWED(
4633 addr_mode_def->allowed_prefixes)) {
4634 return FCML_CEH_GEC_INVALID_PREFIX;
4635 }
4636 /* Check if REPNE prefix is allowed for given addressing mode.*/
4637 if ((instruction->prefixes & FCML_PREFIX_REPNE)
4638 && !FCML_DEF_PREFIX_REPNE_XACQUIRE_ALLOWED(
4639 addr_mode_def->allowed_prefixes)) {
4640 return FCML_CEH_GEC_INVALID_PREFIX;
4641 }
4642 /* Check if XACQUIRED prefix is allowed for given addressing mode.*/
4643 if ((instruction->prefixes & FCML_PREFIX_XACQUIRE)
4644 && (!FCML_DEF_PREFIX_HLE_ENABLED(
4645 addr_mode_def->allowed_prefixes)
4646 || !FCML_DEF_PREFIX_REPNE_XACQUIRE_ALLOWED(
4647 addr_mode_def->allowed_prefixes))) {
4648 return FCML_CEH_GEC_INVALID_PREFIX;
4649 }
4650 /* Check if XRELEASE prefix is allowed for given addressing mode.*/
4651 if ((instruction->prefixes & FCML_PREFIX_XRELEASE)
4652 && (!FCML_DEF_PREFIX_HLE_ENABLED(
4653 addr_mode_def->allowed_prefixes)
4654 || !FCML_DEF_PREFIX_REP_XRELEASE_ALLOWED(
4655 addr_mode_def->allowed_prefixes))) {
4656 return FCML_CEH_GEC_INVALID_PREFIX;
4657 }
4658 }
4659 return FCML_CEH_GEC_NO_ERROR;
4660 }
4661
4662 static ipp_desc ipp_prefixes_acceptor_factory( ipp_factory_args *args,
4663 fcml_ceh_error *error) {
4664 ipp_desc descriptor = { 0 };
4665 descriptor.type = IPPT_VERIFIER;
4666 descriptor.args = NULL;
4667 descriptor.encoder = NULL;
4668 descriptor.acceptor = ipp_prefixes_acceptor;
4669 return descriptor;
4670 }
4671
4672 /*********************************/
4673 /* Operand decorators acceptors. */
4674 /*********************************/
4675
4676 /**
4677 * Decorators should be accepted here. If a given rule cannot be
4678 * generalized and there is a need to handle it in a concrete operator
4679 * acceptor of course it is fine, but we advice to consider this function
4680 * first in order not to spread decorators acceptance rules all around.
4681 */
4682 static fcml_ceh_error ipp_op_decorator_acceptor(ipp_acceptor_args *args) {
4683 const fcml_st_def_addr_mode_desc *addr_mode_def = args->addr_mode_def;
4684 const fcml_st_instruction *instruction = args->instruction;
4685
4686 int i;
4687 for(i = 0; i < FCML_OPERANDS_COUNT; i++) {
4688 fcml_operand_decorators decorators =
4689 FCML_DECORATORS(addr_mode_def->operands[i]);
4690 const fcml_st_operand *operand = &(instruction->operands[i]);
4691 const fcml_st_operand_decorators *opcode_dec = &(operand->decorators);
4692
4693 if (opcode_dec->bcast.is_not_null &&
4694 !FCML_IS_DECOR_BCAST(decorators)) {
4695 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
4696 }
4697
4698 /* Opmask register */
4699 const fcml_st_register *opmask_reg = &opcode_dec->operand_mask_reg;
4700 if (opmask_reg->type != FCML_REG_UNDEFINED) {
4701 /* k0 register cannot be addressed as a predicate operand */
4702 if (opmask_reg->type != FCML_REG_OPMASK ||
4703 opmask_reg->reg == FCML_REG_K0) {
4704 return FCML_CEH_GEC_INVALID_OPERAND_DECORATOR;
4705 }
4706 if (!FCML_IS_DECOR_OPMASK_REG(decorators)) {
4707 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
4708 }
4709 }
4710 /* Opmask register can be mandatory for some instructions. */
4711 if (FCML_IS_DECOR_OPMASK_REG_REQ(decorators) &&
4712 opmask_reg->type != FCML_REG_OPMASK) {
4713 return FCML_CEH_GEC_INVALID_OPERAND_DECORATOR;
4714 }
4715 /* Zeroing-masking */
4716 if (opcode_dec->z) {
4717 /* Zeroing-masking is not supported for memory addressing.
4718 * At the moment it's supported only by register operands. */
4719 if (!FCML_IS_DECOR_Z(decorators) ||
4720 operand->type != FCML_OT_REGISTER ||
4721 // Zeroing-masking is allowed only if write
4722 // mask is defined.
4723 opmask_reg->type == FCML_REG_UNDEFINED) {
4724 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
4725 }
4726 }
4727
4728 if (opcode_dec->sae && !FCML_IS_DECOR_SAE(decorators)) {
4729 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
4730 }
4731 if (opcode_dec->er.is_not_null && !FCML_IS_DECOR_ER(decorators)) {
4732 return FCML_CEH_GEC_NOT_SUPPORTED_DECORATOR;
4733 }
4734
4735 /* Sets vector length for SAE and ER decorators. We net this piece of
4736 * code here in order to have vector length properly set before the
4737 * first operand is proceeded. */
4738 if (FCML_IS_DECOR_SAE(decorators) || FCML_IS_DECOR_ER(decorators)) {
4739 fcml_bool er_or_sae = opcode_dec->sae || opcode_dec->er.is_not_null;
4740 if (er_or_sae) {
4741 fcml_usize vector_length = FCML_DS_UNDEF;
4742 fcml_uint8_t tuple_type =
4743 FCML_GET_SIMD_TUPLETYPE(addr_mode_def->details);
4744 /* For the following kind of SIMD instructions vector length
4745 is explicitly set. */
4746 if(tuple_type == FCML_TT_FV) {
4747 vector_length = FCML_DS_512;
4748 } else if (tuple_type == FCML_TT_T1S || tuple_type == FCML_TT_T1F) {
4749 vector_length = FCML_DS_128;
4750 }
4751
4752 if (!set_vector_length(
4753 &(args->context->optimizer_ctx.vector_length),
4754 vector_length)) {
4755 FCML_TRACE("Vector length differs expected %d got %d.",
4756 args->context->optimizer_ctx.vector_length, vector_length);
4757 return FCML_CEH_GEC_INVALID_OPPERAND_SIZE;
4758 }
4759 }
4760 }
4761 }
4762
4763 return FCML_CEH_GEC_NO_ERROR;
4764 }
4765
4766 static ipp_desc ipp_op_decorators_acceptor_factory(ipp_factory_args *args,
4767 fcml_ceh_error *error) {
4768 ipp_desc descriptor = { 0 };
4769 descriptor.type = IPPT_VERIFIER;
4770 descriptor.args = NULL;
4771 descriptor.encoder = NULL;
4772 descriptor.acceptor = ipp_op_decorator_acceptor;
4773 return descriptor;
4774 }
4775
4776 /********************************************/
4777 /* Instruction parts factories definitions. */
4778 /********************************************/
4779
4780 typedef struct ipp_factory_details {
4781 ipp_factory factory;
4782 fcml_string ipp_name;
4783 fcml_uint32_t flags;
4784 } ipp_factory_details;
4785
4786 typedef enum ipp_choice_type {
4787 /* Only one instruction part processor from the given sequence may
4788 * be chosen for given addressing mode encoding. */
4789 FCML_IPCT_ONE,
4790 /* All instruction part processors from given sequence may be added
4791 * to given addressing mode encoding. */
4792 FCML_IPCT_ALL
4793 } ipp_choice_type;
4794
4795 typedef struct ipp_factory_sequence {
4796 ipp_factory_details *details;
4797 ipp_choice_type choice_type;
4798 fcml_bool is_short_form_supported;
4799 } ipp_factory_sequence;
4800
4801 #define FCML_IPP_FACTORY(fn, flags) { fn, #fn "-" #flags, flags }
4802
4803 /* List of instruction part encoders for instruction opcode. */
4804 static ipp_factory_details ipp_factories_opcode_for_IA[] = {
4805 FCML_IPP_FACTORY(ipp_AVX_opcode_encoder_factory, 0),
4806 FCML_IPP_FACTORY(ipp_reg_opcode_encoder_factory, 0),
4807 FCML_IPP_FACTORY(ipp_conditional_opcode_encoder_factory, 0),
4808 FCML_IPP_FACTORY(ipp_simple_opcode_encoder_factory, 0 ),
4809 { NULL, NULL, 0 }
4810 };
4811
4812 /* List of instruction addressing mode acceptors. */
4813 static ipp_factory_details ipp_factories_acceptors_IA[] = {
4814 FCML_IPP_FACTORY(ipp_addr_mode_acceptor_factory, 0),
4815 FCML_IPP_FACTORY(ipp_prefixes_acceptor_factory, 0),
4816 FCML_IPP_FACTORY(ipp_op_decorators_acceptor_factory, 0),
4817 { NULL, NULL, 0 }
4818 };
4819
4820 /* List of instruction part encoders for instruction prefixes. */
4821 static ipp_factory_details ipp_factories_prefixes_for_IA[] = {
4822 FCML_IPP_FACTORY(ipp_branch_hints_prefix_encoder_factory, 0),
4823 FCML_IPP_FACTORY(ipp_rep_prefix_encoder_factory, 0),
4824 FCML_IPP_FACTORY(ipp_repne_prefix_encoder_factory, 0),
4825 FCML_IPP_FACTORY(ipp_hle_prefixes_encoder_factory, 0),
4826 FCML_IPP_FACTORY(ipp_lock_prefix_encoder_factory, 0),
4827 FCML_IPP_FACTORY(ipp_segment_override_prefix_encoder_factory, 0),
4828 FCML_IPP_FACTORY(ipp_66_prefix_encoder_factory, 0),
4829 FCML_IPP_FACTORY(ipp_67_prefix_encoder_factory, 0),
4830 FCML_IPP_FACTORY(ipp_mandatory_prefixes_encoder_factory, 0),
4831 FCML_IPP_FACTORY(ipp_REX_prefix_encoder_factory, 0),
4832 FCML_IPP_FACTORY(ipp_VEX_XOP_prefix_encoder_factory, 0),
4833 FCML_IPP_FACTORY(ipp_EVEX_prefix_encoder_factory, 0),
4834 { NULL, NULL, 0 }
4835 };
4836
4837 /* ModR/M byte encoder.*/
4838 static ipp_factory_details ipp_factories_ModRM_for_IA[] = {
4839 FCML_IPP_FACTORY(ipp_ModRM_encoder_factory, 0),
4840 { NULL, NULL, 0 }
4841 };
4842
4843 /* List of instruction part encoders for instruction operands.*/
4844 static ipp_factory_details ipp_factories_operands_for_IA[] = {
4845 FCML_IPP_FACTORY(ipp_operand_wrapper_factory, 0),
4846 FCML_IPP_FACTORY(ipp_operand_wrapper_factory, 1),
4847 FCML_IPP_FACTORY(ipp_operand_wrapper_factory, 2),
4848 FCML_IPP_FACTORY(ipp_operand_wrapper_factory, 3),
4849 FCML_IPP_FACTORY(ipp_operand_wrapper_factory, 4),
4850 { NULL, NULL, 0 }
4851 };
4852
4853 static ipp_factory_details ipp_factories_suffixes_for_IA[] = {
4854 FCML_IPP_FACTORY(ipp_suffix_encoder_factory, 0),
4855 { NULL, NULL, 0 }
4856 };
4857
4858 static ipp_factory_sequence ipp_factory_sequences_for_IA[] = {
4859 { ipp_factories_acceptors_IA, FCML_IPCT_ALL, FCML_TRUE },
4860 { ipp_factories_prefixes_for_IA, FCML_IPCT_ALL, FCML_TRUE },
4861 { ipp_factories_opcode_for_IA, FCML_IPCT_ONE, FCML_TRUE },
4862 { ipp_factories_ModRM_for_IA, FCML_IPCT_ALL, FCML_FALSE },
4863 { ipp_factories_operands_for_IA, FCML_IPCT_ALL, FCML_FALSE },
4864 { ipp_factories_suffixes_for_IA, FCML_IPCT_ALL, FCML_FALSE },
4865 { NULL }
4866 };
4867
4868 /* Instruction part processors chain builder dedicated for
4869 * Intel and AMD processors. */
4870 static fcml_ceh_error ipp_chain_builder_for_IA(
4871 const fcml_st_def_addr_mode_desc *addr_mode_desc,
4872 const fcml_st_def_instruction_desc *instruction,
4873 ipp_chain_builder_out *out) {
4874
4875 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
4876 int instruction_parts = 0;
4877 ipp_chain *chain = NULL;
4878 ipp_chain *current_chain = NULL;
4879 ipp_factory_sequence *sequence = &ipp_factory_sequences_for_IA[0];
4880
4881 while (sequence->details) {
4882 int ipp_counter = 0;
4883 ipp_choice_type choice = sequence->choice_type;
4884 ipp_factory_details *factory = sequence->details;
4885
4886 while (factory->factory) {
4887 ipp_factory_args factory_args = { 0 };
4888 factory_args.flags = factory->flags;
4889 factory_args.instruction = instruction;
4890 factory_args.addr_mode = addr_mode_desc;
4891 factory_args.hints = &(out->hints);
4892
4893 ipp_desc desc = factory->factory(&factory_args, &error);
4894 if (!error && (desc.encoder || desc.acceptor)) {
4895 ipp_counter++;
4896 desc.ipp_name = factory->ipp_name;
4897 desc.is_short_form_supported = sequence->
4898 is_short_form_supported;
4899
4900 /* Check max number of the instruction part processors.*/
4901 if (ipp_counter > FCML_ASM_MAX_PART_PROCESSORS) {
4902 FCML_TRACE_MSG("Max number of instructions part processors" \
4903 " has been reached.");
4904 free_ipp_desc(desc);
4905 free_ipp_chain(chain);
4906 return FCML_CEH_GEC_INTERNAL_ERROR;
4907 }
4908
4909 /* Allocate chain element for new instruction part encoder.*/
4910 FCML_ENV_ALLOC_CLEAR(new_chain, ipp_chain);
4911 if (!new_chain) {
4912 free_ipp_desc(desc);
4913 free_ipp_chain(chain);
4914 return FCML_CEH_GEC_OUT_OF_MEMORY;
4915 }
4916
4917 if (!chain) {
4918 chain = new_chain;
4919 current_chain = new_chain;
4920 } else {
4921 current_chain->next_processor = new_chain;
4922 current_chain = new_chain;
4923 }
4924
4925 current_chain->descriptor = desc;
4926
4927 if (choice == FCML_IPCT_ONE) {
4928 break;
4929 }
4930 }
4931
4932 if (error) {
4933 free_ipp_chain(chain);
4934 return error;
4935 }
4936 factory++;
4937 }
4938 sequence++;
4939 }
4940
4941 out->ipp_chain = chain;
4942
4943 return error;
4944 }
4945
4946 /* Chooses instruction part processor factory for given instruction type. */
4947 static ipp_chain_builder choose_ipp_chain_builder(
4948 fcml_en_def_instruction_type type) {
4949 ipp_chain_builder builder = NULL;
4950 switch (type) {
4951 case FCML_EN_IT_IA:
4952 builder = ipp_chain_builder_for_IA;
4953 break;
4954 }
4955 return builder;
4956 }
4957
4958 /* Default data size calculator. */
4959
4960 typedef struct data_size_calc_args {
4961 fcml_int reg_index;
4962 } data_size_calc_args;
4963
4964 static fcml_usize reg_based_memory_data_size_calculator(
4965 const fcml_st_instruction *instruction, fcml_ptr args) {
4966 fcml_usize data = FCML_DS_UNDEF;
4967 data_size_calc_args *dsc_args = (data_size_calc_args*) args;
4968 if (instruction->operands_count > dsc_args->reg_index) {
4969 const fcml_st_operand *operand =
4970 &(instruction->operands[dsc_args->reg_index]);
4971 if (operand->type == FCML_OT_REGISTER) {
4972 return operand->reg.size;
4973 }
4974 }
4975 return data;
4976 }
4977
4978 static fcml_ceh_error prepare_mem_data_size_calculator(
4979 const fcml_st_def_addr_mode_desc *addr_mode_desc,
4980 addr_mode_desc_details *details) {
4981
4982 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
4983 fcml_bool mem_access = FCML_FALSE;
4984 fcml_int reg_index = -1;
4985
4986 /* Just in case.*/
4987 details->ds_calculator = NULL;
4988 details->ds_calculator_args = NULL;
4989
4990 fcml_int i;
4991 for (i = 0; i < FCML_OPERANDS_COUNT; i++) {
4992 fcml_operand_desc operand_desc = addr_mode_desc->operands[i];
4993 fcml_uint64_t addr_mode = FCML_GET_ADDR_MODE(operand_desc);
4994 if (addr_mode == FCML_NA) {
4995 break;
4996 }
4997 if (FCMP_DEF_IS_ADDR_MODE(addr_mode, FCML_OP_RM_BASE) &&
4998 (operand_desc & FCML_RMF_M)) {
4999 mem_access = FCML_TRUE;
5000 }
5001 if (FCMP_DEF_IS_ADDR_MODE(addr_mode, FCML_OP_R_BASE)
5002 || FCMP_DEF_IS_ADDR_MODE(addr_mode, FCML_OP_OPCODE_REG_BASE)
5003 || FCMP_DEF_IS_ADDR_MODE(addr_mode, FCML_OP_EXPLICIT_REG_BASE)
5004 || FCMP_DEF_IS_ADDR_MODE(addr_mode,
5005 FCML_OP_VEX_VVVV_REG_BASE)) {
5006 reg_index = i;
5007 }
5008 }
5009
5010 /* We check for memory access existence, because it makes no sense
5011 * to register custom memory operand size calculator if there
5012 * is no memory access there. */
5013 if (mem_access && reg_index != -1) {
5014 FCML_ENV_ALLOC_CLEAR(args, data_size_calc_args);
5015 if (!args) {
5016 return FCML_CEH_GEC_OUT_OF_MEMORY;
5017 }
5018 args->reg_index = reg_index;
5019 details->ds_calculator = &reg_based_memory_data_size_calculator;
5020 details->ds_calculator_args = args;
5021 }
5022
5023 return error;
5024 }
5025
5026 /* Calculates some flags basing on addressing modes descriptions
5027 * in order to get some performance benefits further.
5028 */
5029 static fcml_ceh_error precalculate_addr_mode(
5030 const fcml_st_def_addr_mode_desc *addr_mode_desc,
5031 addr_mode_desc_details *details) {
5032
5033 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5034 fcml_uint32_t allowed_prefixes = addr_mode_desc->allowed_prefixes;
5035 fcml_uint32_t opcode_flags = addr_mode_desc->opcode_flags;
5036
5037 /* Calculate OSA and ASA flags.*/
5038
5039 fcml_flags osa_flags = FCML_EN_ASF_ANY;
5040 fcml_flags asa_flags = FCML_EN_ASF_ANY;
5041
5042 if (!(addr_mode_desc->instruction_group & FCML_AMT_SIMD)) {
5043 if (FCML_DEF_PREFIX_W_1(allowed_prefixes)) {
5044 osa_flags |= FCML_EN_ASF_64;
5045 asa_flags |= (FCML_EN_ASF_32 | FCML_EN_ASF_64);
5046 }
5047 if (FCML_DEF_PREFIX_W_0(allowed_prefixes)) {
5048 osa_flags |= (FCML_EN_ASF_32 | FCML_EN_ASF_16);
5049 }
5050 }
5051
5052 if (FCML_DEF_OPCODE_FLAGS_IS_EOSA_RESTRICTION(opcode_flags)) {
5053 fcml_flags osa_restriction_flags = FCML_EN_ASF_ANY;
5054 if (FCML_DEF_OPCODE_FLAGS_EOSA_16(opcode_flags)) {
5055 osa_restriction_flags |= FCML_EN_ASF_16;
5056 }
5057 if (FCML_DEF_OPCODE_FLAGS_EOSA_32(opcode_flags)) {
5058 osa_restriction_flags |= FCML_EN_ASF_32;
5059 }
5060 if (FCML_DEF_OPCODE_FLAGS_EOSA_64(opcode_flags)) {
5061 osa_restriction_flags |= FCML_EN_ASF_64;
5062 }
5063 osa_flags = (osa_flags == FCML_EN_ASF_ANY) ?
5064 osa_restriction_flags :
5065 osa_flags & osa_restriction_flags;
5066 }
5067
5068 if (FCML_DEF_OPCODE_FLAGS_IS_EASA_RESTRICTION(opcode_flags)) {
5069 fcml_flags asa_restriction_flags = FCML_EN_ASF_ANY;
5070 if (FCML_DEF_OPCODE_FLAGS_EASA_16(opcode_flags)) {
5071 asa_restriction_flags |= FCML_EN_ASF_16;
5072 }
5073 if (FCML_DEF_OPCODE_FLAGS_EASA_32(opcode_flags)) {
5074 asa_restriction_flags |= FCML_EN_ASF_32;
5075 }
5076 if (FCML_DEF_OPCODE_FLAGS_EASA_64(opcode_flags)) {
5077 asa_restriction_flags |= FCML_EN_ASF_64;
5078 }
5079 asa_flags = (asa_flags == FCML_EN_ASF_ANY) ?
5080 asa_restriction_flags :
5081 asa_flags & asa_restriction_flags;
5082 }
5083
5084 /* Sets calculated values in details.*/
5085 details->allowed_asa = asa_flags;
5086 details->allowed_osa = osa_flags;
5087
5088 return error;
5089 }
5090
5091 static void free_addr_mode_encoding(
5092 fcml_st_dialect_context_int *dialect_context,
5093 addr_mode_encoding *addr_mode) {
5094 /* Processor chains and calculator arguments are shared for clones
5095 * so do not free them. */
5096 if (addr_mode) {
5097 if (!addr_mode->is_cloned) {
5098 if (addr_mode->ipp_chain) {
5099 free_ipp_chain(addr_mode->ipp_chain);
5100 }
5101 /* Free arguments allocated for memory data size
5102 * calculator if there are any. */
5103 fcml_ptr args = addr_mode->addr_mode_details.ds_calculator_args;
5104 if (args) {
5105 fcml_fn_env_memory_free(args);
5106 }
5107 }
5108 if (addr_mode->mnemonic) {
5109 dialect_context->free_mnemonic(
5110 (fcml_st_mp_mnemonic*)addr_mode->mnemonic);
5111 }
5112 fcml_fn_env_memory_free(addr_mode);
5113 }
5114 }
5115
5116 /* A freeing handler dedicated for map holding instruction addressing modes containers. */
5117 static void free_instruction_entry(fcml_ptr key, fcml_ptr value,
5118 fcml_ptr args) {
5119 free_instruction_addr_modes((fcml_st_instruction_addr_modes*) value,
5120 (fcml_st_dialect_context_int*) args);
5121 }
5122
5123 /* Handler for hash map responsible for freeing addressing modes. */
5124 static void free_addr_mode_item_handler(fcml_ptr item_value, fcml_ptr args) {
5125 addr_mode_encoding *addr_mode =
5126 (addr_mode_encoding*) item_value;
5127 fcml_st_dialect_context_int *dialect_context =
5128 (fcml_st_dialect_context_int*) args;
5129 free_addr_mode_encoding(dialect_context, addr_mode);
5130 }
5131
5132 static fcml_fp_instruction_encoder choose_instruction_encoder(
5133 fcml_en_def_instruction_type instruction_type) {
5134 fcml_fp_instruction_encoder encoder = NULL;
5135 switch (instruction_type) {
5136 case FCML_EN_IT_IA:
5137 encoder = instruction_encoder_IA;
5138 break;
5139 }
5140 return encoder;
5141 }
5142
5143 /* Frees instruction addressing modes container and all addressing modes stored there. */
5144 static void free_instruction_addr_modes(
5145 fcml_st_instruction_addr_modes *addr_modes,
5146 fcml_st_dialect_context_int *dialect_context) {
5147 if (addr_modes) {
5148 if (addr_modes->addr_modes_encodings) {
5149 fcml_fn_coll_list_free(addr_modes->addr_modes_encodings,
5150 free_addr_mode_item_handler, dialect_context);
5151 }
5152 fcml_fn_env_memory_free(addr_modes);
5153 }
5154 }
5155
5156 /* Allocates container for addressing modes related to given mnemonic. */
5157 static fcml_ceh_error alloc_instruction_addr_modes(
5158 const fcml_string mnemonic,
5159 const fcml_st_def_instruction_desc *instruction,
5160 fcml_st_instruction_addr_modes **out_addr_modes) {
5161 FCML_ENV_ALLOC_CLEAR(addr_modes, fcml_st_instruction_addr_modes);
5162 if (addr_modes) {
5163 /* Allocate list for addressing modes. */
5164 addr_modes->addr_modes_encodings = fcml_fn_coll_list_alloc();
5165 if (addr_modes->addr_modes_encodings) {
5166 /* Do not care about freeing this piece of memory.
5167 * Procedure responsible for freeing addressing mode is
5168 * responsible for it. */
5169 addr_modes->mnemonic = mnemonic;
5170 addr_modes->encoder = choose_instruction_encoder(
5171 instruction->instruction_type);
5172 *out_addr_modes = addr_modes;
5173 return FCML_CEH_GEC_NO_ERROR;
5174 } else {
5175 fcml_fn_env_memory_free(addr_modes);
5176 return FCML_CEH_GEC_OUT_OF_MEMORY;
5177 }
5178 } else {
5179 /* Out of memory.*/
5180 return FCML_CEH_GEC_OUT_OF_MEMORY;
5181 }
5182 }
5183
5184 /* Prepares addressing modes encoders for all mnemonics associated with given instruction. */
5185 static fcml_ceh_error build_addr_modes_encodings_for_mnemonics(
5186 init_context *init_context,
5187 const fcml_st_def_instruction_desc *instruction,
5188 const fcml_st_def_addr_mode_desc *addr_mode_desc,
5189 addr_mode_encoding *addr_mode) {
5190
5191 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5192
5193 fcml_st_condition *condition = NULL;
5194 if (addr_mode->addr_mode_details.is_conditional) {
5195 condition = &(addr_mode->addr_mode_details.condition);
5196 }
5197
5198 /* Space for mnemonics.*/
5199 fcml_st_mp_mnemonic *mnemonics[FCML_ASM_DIALECT_MAX_MNEMONIC_COUNT];
5200
5201 /* Instruction can be registered for more than one mnemonic.*/
5202 int mnemonic_count = 0;
5203
5204 error = init_context->dialect_context->get_mnemonic(
5205 (fcml_st_dialect*) init_context->dialect_context, instruction,
5206 addr_mode_desc, condition, mnemonics, &mnemonic_count);
5207 if (error) {
5208 return error;
5209 }
5210
5211 /* Prepare addressing mode encoders for every mnemonic. */
5212 int i;
5213 for (i = 0; i < mnemonic_count && !error; i++) {
5214
5215 fcml_st_instruction_addr_modes *addr_modes =
5216 (fcml_st_instruction_addr_modes*) fcml_fn_coll_map_get(
5217 init_context->inst_map, mnemonics[i]->mnemonic);
5218 if (!addr_modes) {
5219 /* Allocates new instruction addressing modes container for
5220 * the new mnemonic. */
5221 error = alloc_instruction_addr_modes(mnemonics[i]->mnemonic,
5222 instruction, &addr_modes);
5223 if (error) {
5224 return error;
5225 }
5226 /* Puts prepared structure under mnemonic key.*/
5227 fcml_int map_error;
5228 fcml_fn_coll_map_put(init_context->inst_map,
5229 mnemonics[i]->mnemonic, addr_modes, &map_error);
5230 if (map_error) {
5231 free_instruction_addr_modes(addr_modes, init_context->dialect_context);
5232 error = fcml_fn_utils_conv_map_error(map_error);
5233 }
5234 }
5235
5236 if (!error) {
5237 fcml_bool is_cloned = (i > 0);
5238 addr_mode_encoding *encoding_for_mnemonic = addr_mode;
5239
5240 if (is_cloned) {
5241 /* Clone encoding details for secondary mnemonics.*/
5242 FCML_ENV_ALLOC_CLEAR(encoding_clone, addr_mode_encoding);
5243 if (encoding_clone) {
5244 *encoding_clone = *encoding_for_mnemonic;
5245 encoding_clone->is_cloned = FCML_TRUE;
5246 encoding_for_mnemonic = encoding_clone;
5247 } else {
5248 error = FCML_CEH_GEC_OUT_OF_MEMORY;
5249 }
5250 }
5251
5252 if (!error) {
5253 /* Every addressing mode has its own mnemonic configuration,
5254 * it doesn't matter if it is cloned addressing mode or not
5255 * it is responsible for deallocating it.
5256 */
5257 encoding_for_mnemonic->mnemonic = mnemonics[i];
5258 if (!fcml_fn_coll_list_add_back(
5259 addr_modes->addr_modes_encodings,
5260 encoding_for_mnemonic)) {
5261 /* Free holder allocated earlier.*/
5262 if (is_cloned) {
5263 fcml_fn_env_memory_free(encoding_for_mnemonic);
5264 }
5265 error = FCML_CEH_GEC_OUT_OF_MEMORY;
5266 }
5267 }
5268 }
5269
5270 if (error) {
5271 /* In case of error free all mnemonics which haven't been
5272 * added to the addressing mode list.
5273 */
5274 for (; i < mnemonic_count; i++) {
5275 fcml_fn_asm_dialect_free_mnemonic(mnemonics[i]);
5276 }
5277 }
5278 }
5279
5280 return error;
5281 }
5282
5283 /* Returns a number of instruction parts needed to encode instruction using
5284 * given instruction part processors chain. Only encoders need instruction
5285 * parts as they generate real instruction bytes. */
5286 static int inst_parts_for_chain(ipp_chain *chain) {
5287 int parts = 0;
5288 while(chain) {
5289 if (chain->descriptor.type == IPPT_ENCODER) {
5290 parts++;
5291 }
5292 chain = chain->next_processor;
5293 }
5294 return parts;
5295 }
5296
5297 static fcml_ceh_error generic_addr_mode_encoding_builder(
5298 init_context *init_context,
5299 const fcml_st_def_instruction_desc *instruction,
5300 const fcml_st_def_addr_mode_desc *addr_mode_desc,
5301 addr_mode_encoding_enricher enricher, fcml_ptr enricher_args) {
5302
5303 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5304
5305 FCML_ENV_ALLOC_CLEAR(addr_mode, addr_mode_encoding);
5306
5307 if (!addr_mode) {
5308 return FCML_CEH_GEC_OUT_OF_MEMORY;
5309 }
5310
5311 /* Copy instruction hints from instruction form definition. */
5312 addr_mode->hints = addr_mode_desc->instruction_hints;
5313 addr_mode->addr_mode_desc = addr_mode_desc;
5314 addr_mode->instruction = instruction->instruction;
5315
5316 /* Prepare addressing mode details.*/
5317 addr_mode_desc_details *details = &(addr_mode->addr_mode_details);
5318
5319 error = precalculate_addr_mode(addr_mode_desc, details);
5320 if (error) {
5321 free_addr_mode_encoding(init_context->dialect_context, addr_mode);
5322 return error;
5323 }
5324
5325 error = prepare_mem_data_size_calculator(addr_mode_desc, details);
5326 if (error) {
5327 free_addr_mode_encoding(init_context->dialect_context, addr_mode);
5328 return error;
5329 }
5330
5331 if (enricher) {
5332 enricher(addr_mode, enricher_args);
5333 }
5334
5335 ipp_chain_builder builder = choose_ipp_chain_builder(
5336 instruction->instruction_type);
5337
5338 /* Populates addressing mode encoding with instruction
5339 * part processors and instruction level hints. */
5340 ipp_chain_builder_out out = {NULL};
5341 error = builder(addr_mode_desc, instruction, &out);
5342 if (error) {
5343 free_addr_mode_encoding(init_context->dialect_context, addr_mode);
5344 return error;
5345 }
5346
5347 addr_mode->ipp_chain = out.ipp_chain;
5348 addr_mode->instruction_parts = inst_parts_for_chain(out.ipp_chain);
5349 addr_mode->hints = out.hints | addr_mode_desc->instruction_hints;
5350
5351 /* The addressing mode encoder built above is a generic one for the
5352 * processed instruction, bet be have to provide specific parameterized
5353 * encoders for every mnemonic which represents given instruction.
5354 * The function below is responsible for that. */
5355 error = build_addr_modes_encodings_for_mnemonics(init_context,
5356 instruction, addr_mode_desc, addr_mode);
5357 if (error) {
5358 free_addr_mode_encoding(init_context->dialect_context, addr_mode);
5359 return error;
5360 }
5361
5362 return error;
5363 }
5364
5365 typedef struct cond_enricher_args {
5366 /* Instruction condition code. */
5367 fcml_int condition_num;
5368 } cond_enricher_args;
5369
5370 /* Enriches addressing mode encoder by adding details about
5371 * instruction condition. */
5372 static void conditional_addr_more_encoding_enricher(
5373 addr_mode_encoding *addr_mode, fcml_ptr args) {
5374 cond_enricher_args *enricher_args = (cond_enricher_args*) args;
5375 addr_mode_desc_details *details = &(addr_mode->addr_mode_details);
5376 details->is_conditional = FCML_TRUE;
5377 details->condition.is_negation = (enricher_args->condition_num % 2 == 1);
5378 details->condition.condition_type = (fcml_en_condition_type)
5379 (enricher_args->condition_num / 2);
5380 }
5381
5382 /* Builder for conditional instructions (tttn) */
5383 static fcml_ceh_error conditional_addr_mode_encoding_builder(
5384 init_context *init_context,
5385 const fcml_st_def_instruction_desc *instruction,
5386 const fcml_st_def_addr_mode_desc *addr_mode_desc) {
5387 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5388 int i;
5389 for (i = 0; i < FCML_NUMBER_OF_CONDITIONS * 2 && !error; i++) {
5390 cond_enricher_args enricher_args = { 0 };
5391 enricher_args.condition_num = i;
5392 error = generic_addr_mode_encoding_builder(
5393 init_context, instruction, addr_mode_desc,
5394 &conditional_addr_more_encoding_enricher,
5395 &enricher_args);
5396 }
5397 return error;
5398 }
5399
5400 static fcml_ceh_error default_addr_mode_encoding_builder(
5401 init_context *init_context,
5402 const fcml_st_def_instruction_desc *instruction,
5403 const fcml_st_def_addr_mode_desc *addr_mode) {
5404 return generic_addr_mode_encoding_builder(init_context, instruction,
5405 addr_mode, NULL, NULL);
5406 }
5407
5408 static addr_mode_encoding_builder encoding_builder_factory(
5409 fcml_st_def_addr_mode_desc *addr_mode) {
5410 if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_TTTN(addr_mode->opcode_flags)) {
5411 return &conditional_addr_mode_encoding_builder;
5412 }
5413 return &default_addr_mode_encoding_builder;
5414 }
5415
5416 /* All the generated addressing modes encodings are registered in
5417 * instruction map see: init_context. */
5418 static fcml_ceh_error build_instruction_addr_modes_encodings(
5419 init_context *init_context,
5420 fcml_st_def_instruction_desc *instruction) {
5421 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5422 int i;
5423 for (i = 0; i < instruction->opcode_desc_count && !error; i++) {
5424 fcml_st_def_addr_mode_desc *addr_mode = &(instruction->addr_modes[i]);
5425 /* Prepare encoders for given addressing mode.*/
5426 addr_mode_encoding_builder builder =
5427 encoding_builder_factory(addr_mode);
5428 error = builder(init_context, instruction, addr_mode);
5429 }
5430 return error;
5431 }
5432
5433 fcml_ceh_error fcml_fn_init_instructions_addr_modes(
5434 fcml_st_dialect_context_int *dialect_context,
5435 fcml_coll_map *inst_map) {
5436
5437 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
5438 fcml_coll_map instructions_map;
5439 /* Allocate map for all instructions.*/
5440 fcml_st_coll_map_descriptor inst_map_desc =
5441 fcml_coll_map_descriptor_string;
5442
5443 inst_map_desc.entry_free_function = free_instruction_entry;
5444 inst_map_desc.entry_free_args = dialect_context;
5445
5446 fcml_int map_error;
5447 instructions_map = fcml_fn_coll_map_alloc(&inst_map_desc, 64, &map_error);
5448 if (map_error) {
5449 return fcml_fn_utils_conv_map_error(map_error);
5450 }
5451
5452 init_context init_context;
5453 init_context.dialect_context = dialect_context;
5454 init_context.inst_map = instructions_map;
5455
5456 int i = 0;
5457 fcml_st_def_instruction_desc *inst = &(fcml_ext_instructions_def[i++]);
5458 while (inst->instruction != F_UNKNOWN && !error) {
5459 /* Adds all addressing modes */
5460 error = build_instruction_addr_modes_encodings(&init_context, inst);
5461 inst = &(fcml_ext_instructions_def[i++]);
5462 }
5463
5464 if (error) {
5465 /* Free everything that have been properly allocated. */
5466 fcml_fn_coll_map_free(instructions_map);
5467 instructions_map = NULL;
5468 }
5469
5470 *inst_map = instructions_map;
5471
5472 return error;
5473 }
5474
5475 void fcml_fn_free_instructions_addr_modes(fcml_coll_map instructions_map) {
5476 if (instructions_map) {
5477 fcml_fn_coll_map_free(instructions_map);
5478 }
5479 }
5480
5481 fcml_ceh_error fcml_fn_get_instruction_addr_modes(
5482 fcml_coll_map instructions_map, fcml_string mnemonic,
5483 fcml_st_instruction_addr_modes **addr_modes) {
5484
5485 if (!instructions_map) {
5486 return FCML_CEH_GEC_NOT_INITIALIZED;
5487 }
5488
5489 *addr_modes = (fcml_st_instruction_addr_modes*)
5490 fcml_fn_coll_map_get(instructions_map, mnemonic);
5491 if (!*addr_modes) {
5492 return FCML_CEH_GEC_UNKNOWN_MNEMONIC;
5493 }
5494
5495 return FCML_CEH_GEC_NO_ERROR;
5496 }
5497
5498 static void break_optimization(encoding_context *context) {
5499 context->optimizer_processing_details.break_optimization = FCML_TRUE;
5500 }
5501
5502 static inline fcml_bool is_short_form(const fcml_st_instruction *instruction,
5503 const fcml_st_mp_mnemonic *mnemonic) {
5504 return mnemonic->is_shortcut && instruction->operands_count == 0;
5505 }
5506