| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 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_types.h> |
| 23 |
|
| 24 |
#include "fcml_dialect_int.h" |
| 25 |
#include "fcml_ceh.h" |
| 26 |
#include "fcml_coll.h" |
| 27 |
#include "fcml_env_int.h" |
| 28 |
#include "fcml_utils.h" |
| 29 |
|
| 30 |
fcml_st_mp_mnemonic* fcml_fn_asm_dialect_alloc_mnemonic_with_suffix( |
| 31 |
fcml_st_mp_mnemonic *mnemonic, fcml_string suffix) { |
| 32 |
|
| 33 |
if (!suffix) { |
| 34 |
return fcml_fn_asm_dialect_alloc_mnemonic(mnemonic); |
| 35 |
} |
| 36 |
|
| 37 |
FCML_ENV_ALLOC_CLEAR(new_mnemonic, fcml_st_mp_mnemonic); |
| 38 |
if (!new_mnemonic) { |
| 39 |
/* Out of memory.*/ |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
*new_mnemonic = *mnemonic; |
| 44 |
|
| 45 |
fcml_usize mnemonic_len = fcml_fn_env_str_strlen(mnemonic->mnemonic); |
| 46 |
fcml_usize suffix_len = fcml_fn_env_str_strlen(suffix); |
| 47 |
fcml_string dest_mnemonic = fcml_fn_env_str_stralloc( |
| 48 |
mnemonic_len + suffix_len + 1); |
| 49 |
if (dest_mnemonic) { |
| 50 |
fcml_fn_env_str_strncpy(dest_mnemonic, mnemonic->mnemonic, |
| 51 |
mnemonic_len); |
| 52 |
fcml_fn_env_str_strncpy(dest_mnemonic + mnemonic_len, suffix, |
| 53 |
suffix_len); |
| 54 |
new_mnemonic->mnemonic = dest_mnemonic; |
| 55 |
} else { |
| 56 |
/* Out of memory, free allocated mnemonic.*/ |
| 57 |
fcml_fn_env_memory_free(new_mnemonic); |
| 58 |
new_mnemonic = NULL; |
| 59 |
} |
| 60 |
|
| 61 |
return new_mnemonic; |
| 62 |
} |
| 63 |
|
| 64 |
fcml_st_mp_mnemonic* fcml_fn_asm_dialect_alloc_mnemonic( |
| 65 |
fcml_st_mp_mnemonic *mnemonic) { |
| 66 |
|
| 67 |
FCML_ENV_ALLOC_CLEAR(new_mnemonic, fcml_st_mp_mnemonic); |
| 68 |
if (!new_mnemonic) { |
| 69 |
/* Out of memory.*/ |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
*new_mnemonic = *mnemonic; |
| 74 |
|
| 75 |
fcml_string dest_mnemonic = fcml_fn_env_str_strdup(mnemonic->mnemonic); |
| 76 |
if (dest_mnemonic) { |
| 77 |
new_mnemonic->mnemonic = dest_mnemonic; |
| 78 |
} else { |
| 79 |
/* Out of memory, free allocated mnemonic.*/ |
| 80 |
fcml_fn_env_memory_free(new_mnemonic); |
| 81 |
new_mnemonic = NULL; |
| 82 |
} |
| 83 |
|
| 84 |
return new_mnemonic; |
| 85 |
} |
| 86 |
|
| 87 |
void fcml_fn_asm_dialect_free_mnemonic(fcml_st_mp_mnemonic *mnemonic) { |
| 88 |
if (mnemonic) { |
| 89 |
if (mnemonic->mnemonic) { |
| 90 |
fcml_fn_env_str_strfree(mnemonic->mnemonic); |
| 91 |
} |
| 92 |
fcml_fn_env_memory_free(mnemonic); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
fcml_ceh_error fcml_fn_asm_dialect_alloc_mnemonic_lookup( |
| 97 |
fcml_st_dialect_context_int **out_dialect_context, |
| 98 |
fcml_st_dialect_mnemonic *mnemonic, fcml_usize capacity) { |
| 99 |
|
| 100 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 101 |
|
| 102 |
FCML_ENV_ALLOC_CLEAR(dialect_context, fcml_st_dialect_context_int); |
| 103 |
if (!dialect_context) { |
| 104 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 105 |
} |
| 106 |
|
| 107 |
/* Prepare mnemonic lookup.*/ |
| 108 |
fcml_int map_error; |
| 109 |
dialect_context->dialect_mnemonics_lookup = fcml_fn_coll_map_alloc( |
| 110 |
&fcml_coll_map_descriptor_uint32, capacity, &map_error); |
| 111 |
if (map_error) { |
| 112 |
fcml_fn_env_memory_free(dialect_context); |
| 113 |
return fcml_fn_utils_conv_map_error(map_error); |
| 114 |
} |
| 115 |
|
| 116 |
fcml_st_dialect_mnemonic *current = mnemonic; |
| 117 |
while (current->mnemonic) { |
| 118 |
fcml_fn_coll_map_put(dialect_context->dialect_mnemonics_lookup, |
| 119 |
&(current->instruction), current, &map_error); |
| 120 |
if (map_error) { |
| 121 |
fcml_fn_coll_map_free(dialect_context->dialect_mnemonics_lookup); |
| 122 |
fcml_fn_env_memory_free(dialect_context); |
| 123 |
return fcml_fn_utils_conv_map_error(map_error); |
| 124 |
} |
| 125 |
current++; |
| 126 |
} |
| 127 |
|
| 128 |
*out_dialect_context = dialect_context; |
| 129 |
|
| 130 |
return error; |
| 131 |
} |
| 132 |
|
| 133 |
void fcml_fn_asm_dialect_free_mnemonic_lookup( |
| 134 |
fcml_st_dialect_context_int *dialect_context) { |
| 135 |
if (dialect_context && dialect_context->dialect_mnemonics_lookup) { |
| 136 |
fcml_fn_coll_map_free(dialect_context->dialect_mnemonics_lookup); |
| 137 |
} |
| 138 |
} |