Debian Codegraph @

src/fcml_assembler.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 <stddef.h>
21
22 #include <fcml_assembler.h>
23 #include <fcml_errors.h>
24
25 #include "fcml_dialect_int.h"
26 #include "fcml_encoding.h"
27 #include "fcml_env_int.h"
28 #include "fcml_utils.h"
29 #include "fcml_trace.h"
30 #include "fcml_coll.h"
31
32 /** Initialized assembler instance. */
33 typedef struct enc_assembler {
34 /** Map of supported instructions, with mnemonic as key and
35 * instructions addressing modes as values.
36 */
37 fcml_coll_map instructions_map;
38 /** Map of supported pseudo operations */
39 fcml_coll_map pseudo_operations_map;
40 /** Dialect used to initialize assembler. */
41 fcml_st_dialect_context_int *dialect_context;
42 } enc_assembler;
43
44 /** Type for pseudo operations encoders. */
45 typedef fcml_ceh_error (*pseudo_operation_encoder)(
46 const fcml_st_instruction *instruction,
47 fcml_st_assembler_result *result);
48
49 /** Holds definition of a pseudo operation encoder. */
50 typedef struct pseudo_operation_desc {
51 /** ID of the pseudo operation. */
52 fcml_en_pseudo_operations pseudo_operation;
53 /** Function responsible for encoding pseudo operation. */
54 pseudo_operation_encoder pseudo_operation_encoder;
55 } pseudo_operation_desc;
56
57 /** Pseudo operation encoder for "encode byte" (DB). */
58 static fcml_ceh_error db_pseudo_operation_encoder(const fcml_st_instruction*,
59 fcml_st_assembler_result*);
60
61 /** Array of all supported pseudo operation encoders. */
62 static pseudo_operation_desc supported_pseudo_operations[] = {
63 { FP_DB, &db_pseudo_operation_encoder },
64 { FP_NO_PSEUDO_OP, NULL }
65 };
66
67 static fcml_bool verify_instruction(const fcml_st_instruction *instruction);
68
69 static void free_instruction_chain(fcml_st_assembled_instruction *chain);
70
71 /** It encodes a pseudo operation like "db" if no intruction were found
72 * for given bytes. */
73 static fcml_ceh_error encode_pseudo_operation(
74 const fcml_st_assembler_context *asm_context,
75 const fcml_coll_map pseudo_operations_map,
76 const fcml_st_instruction *instruction,
77 fcml_st_assembler_result *result);
78
79 static fcml_ceh_error init_pseudo_operation_encodings(
80 fcml_st_dialect_context_int *dialect,
81 fcml_coll_map *pseudo_operations_map);
82
83 static fcml_ceh_error assemble_core(fcml_st_assembler_context *asm_context,
84 const fcml_st_instruction *instruction,
85 fcml_st_assembler_result *result);
86
87 /** see documentation in header files */
88 fcml_ceh_error LIB_CALL fcml_fn_assembler_init(const fcml_st_dialect *dialect,
89 fcml_st_assembler **assembler) {
90 /* Allocate assembler instance.*/
91 FCML_ENV_ALLOC_CLEAR(enc_asm, enc_assembler);
92 if (!enc_asm) {
93 return FCML_CEH_GEC_OUT_OF_MEMORY;
94 }
95
96 /* Adds all known instructions to the instructions map. Every supported
97 * instruction has its own preinitialized inctructions encoders which
98 * are used to encode them later when the assember is used. They are
99 * initialized once and shared later in order to save memory and CPU.
100 */
101 fcml_ceh_error error = fcml_fn_init_instructions_addr_modes(
102 (fcml_st_dialect_context_int*) dialect,
103 &(enc_asm->instructions_map));
104 if (error) {
105 fcml_fn_env_memory_free(enc_asm);
106 return error;
107 }
108
109 /* Initialize pseudo operations encoding. */
110 error = init_pseudo_operation_encodings(
111 (fcml_st_dialect_context_int*) dialect,
112 &(enc_asm->pseudo_operations_map));
113 if (error) {
114 fcml_fn_free_instructions_addr_modes(enc_asm->instructions_map);
115 fcml_fn_env_memory_free(enc_asm);
116 return error;
117 }
118
119 enc_asm->dialect_context = (fcml_st_dialect_context_int*) dialect;
120
121 *assembler = (fcml_st_assembler*) enc_asm;
122
123 return FCML_CEH_GEC_NO_ERROR;
124 }
125
126 /** see documentation in header files */
127 void LIB_CALL fcml_fn_assembler_instruction_detach(
128 fcml_st_assembled_instruction **chain,
129 fcml_st_assembled_instruction *instruction) {
130
131 if (!chain || !instruction) {
132 return;
133 }
134
135 fcml_st_assembled_instruction **current = chain;
136 fcml_bool found = FCML_FALSE;
137
138 do {
139 if (*current == instruction) {
140 *current = instruction->next;
141 found = FCML_TRUE;
142 }
143 current = &((*current)->next);
144 } while (!found && *current);
145 }
146
147 /** see documentation in header files */
148 void LIB_CALL fcml_fn_assembler_instruction_free(
149 fcml_st_assembled_instruction *instruction) {
150 if (instruction) {
151 if (instruction->code) {
152 fcml_fn_env_memory_free(instruction->code);
153 }
154 fcml_fn_ceh_free_errors_only(&(instruction->warnings));
155 fcml_fn_env_memory_free(instruction);
156 }
157 }
158
159 /** see documentation in header files */
160 void LIB_CALL fcml_fn_assembler_result_free(fcml_st_assembler_result *result) {
161 if (result) {
162 // Free errors but leave container as is.
163 fcml_fn_ceh_free_errors_only(&(result->errors));
164 // Free all available instructions in the chain.
165 free_instruction_chain(result->instructions);
166 // Prepare for next usage.
167 fcml_fn_assembler_result_prepare(result);
168 }
169 }
170
171 /** see documentation in header files */
172 void LIB_CALL fcml_fn_assembler_result_prepare(fcml_st_assembler_result *result) {
173 // Clean assembler result container before it is first used.
174 if (result) {
175 fcml_fn_env_memory_clear(result, sizeof(fcml_st_assembler_result));
176 }
177 }
178
179 /** see documentation in header files */
180 fcml_ceh_error LIB_CALL fcml_fn_assemble(fcml_st_assembler_context *context,
181 const fcml_st_instruction *instruction,
182 fcml_st_assembler_result *result) {
183 /* Check if there is something already available in the result and
184 * free it in such a case.
185 */
186 fcml_fn_assembler_result_free(result);
187 fcml_ceh_error error = assemble_core(context, instruction, result);
188 if (error) {
189 // Try to convert error code to error message if there is such need.
190 fcml_fn_utils_conv_gec_to_error_info(
191 context->configuration.enable_error_messages, &(result->errors),
192 error);
193 }
194 return error;
195 }
196
197 /** see documentation in header files */
198 void LIB_CALL fcml_fn_assembler_free(fcml_st_assembler *assembler) {
199 if (assembler) {
200 enc_assembler *enc_asm = (enc_assembler*) assembler;
201 if (enc_asm->instructions_map) {
202 fcml_fn_free_instructions_addr_modes(enc_asm->instructions_map);
203 }
204 /* Frees pseudo operations encoding. */
205 if (enc_asm->pseudo_operations_map) {
206 fcml_fn_coll_map_free(enc_asm->pseudo_operations_map);
207 }
208 /* Assembler is not the owner of the dialect context. */
209 fcml_fn_env_memory_free(enc_asm);
210 }
211 }
212
213 /** This is an internal function used by the assembler
214 * internals to extract a dialect instance from the assembler.
215 */
216 fcml_st_dialect* fcml_fn_assembler_extract_dialect(fcml_st_assembler *assembler) {
217 fcml_st_dialect *dialect = NULL;
218 if (assembler) {
219 enc_assembler *enc_asm = (enc_assembler*) assembler;
220 dialect = (fcml_st_dialect*) enc_asm->dialect_context;
221 }
222 return dialect;
223 }
224
225 static void free_instruction_chain(fcml_st_assembled_instruction *chain) {
226 /* Free every instruction in the chain. */
227 while (chain) {
228 fcml_st_assembled_instruction *instruction = chain;
229 chain = chain->next;
230 fcml_fn_env_memory_free(instruction->code);
231 fcml_fn_env_memory_free(instruction);
232 }
233 }
234
235 /** The core assembling logic. */
236 static fcml_ceh_error assemble_core(fcml_st_assembler_context *asm_context,
237 const fcml_st_instruction *instruction,
238 fcml_st_assembler_result *result) {
239
240 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
241
242 /* Sanity check. */
243 if (!result || !instruction || !instruction->mnemonic || !asm_context) {
244 return FCML_CEH_GEC_INVALID_INPUT;
245 }
246
247 if (!verify_instruction(instruction)) {
248 return FCML_CEH_GEC_INVALID_INSTRUCTION_MODEL;
249 }
250
251 /* Validate and prepare the entry point. */
252 error = fcml_fn_utils_prepare_entry_point(&(asm_context->entry_point));
253 if (error) {
254 return error;
255 }
256
257 /* Take into account that the dialect can modify the source instruction by
258 optionally preconfigure it for the assembler, so we have to
259 use a local copy here, to keep the original instance untouched. */
260 fcml_st_instruction tmp_instruction = *instruction;
261
262 enc_assembler *enc_asm = (enc_assembler*) asm_context->assembler;
263
264 /* The first place where the dialect can interfere with the
265 instruction definition. */
266 fcml_fnp_asm_preprocessor preprocessor =
267 enc_asm->dialect_context->asm_preprocessor;
268 if (preprocessor) {
269 preprocessor((fcml_st_dialect*) enc_asm->dialect_context,
270 NULL, NULL, F_UNKNOWN, &tmp_instruction, NULL);
271 }
272
273 /* Find instruction addressing modes/forms. */
274 fcml_st_instruction_addr_modes *addr_modes = NULL;
275 error = fcml_fn_get_instruction_addr_modes(enc_asm->instructions_map,
276 tmp_instruction.mnemonic, &addr_modes);
277 if (error) {
278 if (error == FCML_CEH_GEC_UNKNOWN_MNEMONIC) {
279 return encode_pseudo_operation(asm_context,
280 enc_asm->pseudo_operations_map, instruction, result);
281 } else {
282 return error;
283 }
284 }
285
286 /* Executes the found instruction encoder. */
287 if (addr_modes) {
288 if (addr_modes->encoder) {
289 fcml_st_asm_encoder_result enc_result = { { 0 } };
290 error = addr_modes->encoder(asm_context, enc_asm->dialect_context,
291 addr_modes, &tmp_instruction, &enc_result);
292 if (!error) {
293 result->instructions = enc_result.instructions;
294 result->number_of_instructions =
295 enc_result.number_of_instructions;
296 result->chosen_instruction = enc_result.chosen_instruction;
297 /* Increment IP by chosen instruction length. */
298 if (result->chosen_instruction
299 && asm_context->configuration.increment_ip) {
300 asm_context->entry_point.ip +=
301 result->chosen_instruction->code_length;
302 }
303 }
304 /* Convert encoding result to assembler result. */
305 result->errors = enc_result.errors;
306 } else {
307 FCML_TRACE_MSG( "Unavailable instruction encoder." );
308 error = FCML_CEH_GEC_INTERNAL_ERROR;
309 }
310 } else {
311 error = FCML_CEH_GEC_UNKNOWN_MNEMONIC;
312 }
313
314 /* Free results but only if assembling failed. */
315 if (error) {
316 /* Free only instructions, error messages should be returned to user. */
317 free_instruction_chain(result->instructions);
318 result->instructions = NULL;
319 result->chosen_instruction = NULL;
320 result->number_of_instructions = 0;
321 }
322
323 return error;
324 }
325
326 /** Allocates new instance of assembled instruction for given number
327 * of machine code bytes.
328 */
329 static fcml_st_assembled_instruction* allocate_assembled_instruction(
330 fcml_usize size) {
331 fcml_st_assembled_instruction *instruction =
332 (fcml_st_assembled_instruction*) fcml_fn_env_memory_alloc_clear(
333 sizeof(fcml_st_assembled_instruction));
334 if (!instruction) {
335 return NULL;
336 }
337
338 instruction->code = (fcml_uint8_t*) fcml_fn_env_memory_alloc_clear(size);
339 if (!instruction->code) {
340 fcml_fn_env_memory_free(instruction);
341 return NULL;
342 }
343
344 instruction->code_length = size;
345
346 return instruction;
347 }
348
349 /** Encodes "encode byte" (DB) pseudo operation. */
350 static fcml_ceh_error db_pseudo_operation_encoder(
351 const fcml_st_instruction *instruction,
352 fcml_st_assembler_result *result) {
353
354 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
355
356 if (instruction->operands_count == 1
357 && instruction->operands[0].type == FCML_OT_IMMEDIATE) {
358
359 fcml_uint8_t value;
360 error = fcml_fn_utils_conv_int_to_uint8(
361 &(instruction->operands[0].immediate), &value);
362 if (error) {
363 return FCML_CEH_GEC_VALUE_OUT_OF_RANGE;
364 }
365
366 result->instructions = allocate_assembled_instruction(1);
367 if (!result->instructions) {
368 return FCML_CEH_GEC_OUT_OF_MEMORY;
369 }
370
371 result->instructions->code[0] = value;
372 } else {
373 error = FCML_CEH_GEC_INVALID_OPPERAND;
374 }
375
376 return error;
377 }
378
379 /** Goes through the array of pseudo operations descriptors in order to find
380 * one for the given pseudo operation ID. */
381 static pseudo_operation_desc* find_desc_for_pseudo_operation(
382 fcml_en_pseudo_operations pseudo_operation) {
383 pseudo_operation_desc *desc = supported_pseudo_operations;
384 while (desc->pseudo_operation != FP_NO_PSEUDO_OP) {
385 if (desc->pseudo_operation == pseudo_operation) {
386 return desc;
387 }
388 desc++;
389 }
390 return NULL;
391 }
392
393 /** Initializes encoding map for pseudo operations. */
394 static fcml_ceh_error init_pseudo_operation_encodings(
395 fcml_st_dialect_context_int *dialect,
396 fcml_coll_map *pseudo_operations_map) {
397
398 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
399
400 fcml_int map_error;
401 fcml_coll_map po_map = fcml_fn_coll_map_alloc(
402 &fcml_coll_map_descriptor_string, 10, &map_error);
403 if (map_error) {
404 return fcml_fn_utils_conv_map_error(map_error);
405 }
406
407 fcml_st_dialect_pseudo_op_mnemonic *mnemonics_map =
408 dialect->get_pseudo_op_mnemonics();
409 if (mnemonics_map) {
410 while (mnemonics_map->mnemonic) {
411 pseudo_operation_desc *desc = find_desc_for_pseudo_operation(
412 mnemonics_map->pseudo_operation);
413 if (desc) {
414 fcml_fn_coll_map_put(po_map, mnemonics_map->mnemonic, desc,
415 &map_error);
416 if (map_error) {
417 fcml_fn_coll_map_free(po_map);
418 return fcml_fn_utils_conv_map_error(map_error);
419 }
420 } else {
421 FCML_TRACE( "Unsupported pseudo operation code: %d.",
422 mnemonics_map->pseudo_operation );
423 }
424 mnemonics_map++;
425 }
426 }
427
428 *pseudo_operations_map = po_map;
429
430 return error;
431 }
432
433 /** If the given instruction holds pseudo operation,
434 * this function tries to encode it.
435 */
436 static fcml_ceh_error encode_pseudo_operation(
437 const fcml_st_assembler_context *asm_context,
438 const fcml_coll_map pseudo_operations_map,
439 const fcml_st_instruction *instruction,
440 fcml_st_assembler_result *result) {
441
442 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
443
444 pseudo_operation_desc *desc = (pseudo_operation_desc*)
445 fcml_fn_coll_map_get(pseudo_operations_map, instruction->mnemonic);
446
447 if (desc) {
448 /* There is a pseudo operation encoder for the mnemonic,
449 * so try to encode it. */
450 error = desc->pseudo_operation_encoder(instruction, result);
451 if (!error) {
452 result->chosen_instruction = result->instructions;
453 result->number_of_instructions = 1;
454 }
455 } else {
456 error = FCML_CEH_GEC_UNKNOWN_MNEMONIC;
457 }
458
459 return error;
460 }
461
462 /** A basic check for the instructions provided by the callers. */
463 static fcml_bool verify_instruction(const fcml_st_instruction *instruction) {
464 int i;
465 fcml_bool last_found = FCML_FALSE;
466 fcml_bool result = FCML_TRUE;
467
468 /* Check if there are no gaps in the operands list. */
469 for (i = 0; i < FCML_OPERANDS_COUNT; i++) {
470 if (instruction->operands[i].type == FCML_OT_NONE) {
471 last_found = FCML_TRUE;
472 } else {
473 if (last_found) {
474 result = FCML_FALSE;
475 break;
476 }
477 }
478 }
479
480 return result;
481 }
482