| 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 |
/** @file fcml_lag_assembler.hpp |
| 21 |
* C++ wrapper for the Multi-pass FCML assembler. |
| 22 |
* |
| 23 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 24 |
* This project is released under the GNU Lesser General Public License. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef FCML_LAG_ASSEMBLER_HPP_ |
| 28 |
#define FCML_LAG_ASSEMBLER_HPP_ |
| 29 |
|
| 30 |
#include "fcml_assembler.hpp" |
| 31 |
#include "fcml_symbols.hpp" |
| 32 |
|
| 33 |
#include "fcml_lag_assembler.h" |
| 34 |
|
| 35 |
namespace fcml { |
| 36 |
|
| 37 |
/** Assembler result which contains all assembled instructions. */ |
| 38 |
class MultiPassAssemblerResult { |
| 39 |
public: |
| 40 |
|
| 41 |
/** |
| 42 |
* Default constructor. |
| 43 |
* @since 1.1.0 |
| 44 |
*/ |
| 45 |
MultiPassAssemblerResult() { |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Virtual destructor. |
| 50 |
* @since 1.1.0 |
| 51 |
*/ |
| 52 |
virtual ~MultiPassAssemblerResult() { |
| 53 |
} |
| 54 |
|
| 55 |
public: |
| 56 |
|
| 57 |
/** |
| 58 |
* Gets error container. |
| 59 |
* |
| 60 |
* @return Error container. |
| 61 |
* @since 1.1.0 |
| 62 |
*/ |
| 63 |
const ErrorContainer& getErrorContainer() const { |
| 64 |
return _errorContainer; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Gets number of assembled instructions. |
| 69 |
* @return Number of assembled instructions. |
| 70 |
* @since 1.1.0 |
| 71 |
*/ |
| 72 |
fcml_usize getSize() const { |
| 73 |
return static_cast<fcml_usize>(_assembledInstructions.size()); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets one assembled instruction by its index. |
| 78 |
* |
| 79 |
* @param index The index of the assembled instruction. |
| 80 |
* @return One assembled instruction at given index. |
| 81 |
* @throw BadArgumentException |
| 82 |
* @since 1.1.0 |
| 83 |
*/ |
| 84 |
const AssembledInstruction& operator[](fcml_usize index) const { |
| 85 |
if (index > _assembledInstructions.size()) { |
| 86 |
throw BadArgumentException(FCML_TEXT("Array index out of bound."), |
| 87 |
FCML_CEH_GEC_VALUE_OUT_OF_RANGE); |
| 88 |
} |
| 89 |
return _assembledInstructions[index]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Gets constant vector of all assembled instructions. |
| 94 |
* |
| 95 |
* @return Assembled instructions. |
| 96 |
* @since 1.1.0 |
| 97 |
*/ |
| 98 |
const std::vector<AssembledInstruction>& getAssembledInstructions() const { |
| 99 |
return _assembledInstructions; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Gets iterator which allows to iterate through the whole machine code |
| 104 |
* byte by byte. |
| 105 |
* |
| 106 |
* @return Iterator instance. |
| 107 |
* @since 1.1.0 |
| 108 |
*/ |
| 109 |
CodeIterator getCodeIterator() { |
| 110 |
return CodeIterator(_assembledInstructions); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Clears the result. Remember not to use the CodeIterator after the |
| 115 |
* result is cleared up. |
| 116 |
* @since 1.1.0 |
| 117 |
*/ |
| 118 |
void clear() { |
| 119 |
_errorContainer.clean(); |
| 120 |
_assembledInstructions.clear(); |
| 121 |
} |
| 122 |
|
| 123 |
protected: |
| 124 |
|
| 125 |
/** Only the assembler can modify these objects. */ |
| 126 |
friend class MultiPassAssembler; |
| 127 |
|
| 128 |
/** |
| 129 |
* Gets vector of all assembled instructions. |
| 130 |
* |
| 131 |
* @return Assembled instructions. |
| 132 |
* @since 1.1.0 |
| 133 |
*/ |
| 134 |
std::vector<AssembledInstruction>& getAssembledInstructions() { |
| 135 |
return _assembledInstructions; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Sets new error container for the result. |
| 140 |
* |
| 141 |
* @param errorContainer The new error container to be set. |
| 142 |
* @since 1.1.0 |
| 143 |
*/ |
| 144 |
void setErrorContainer(const ErrorContainer &errorContainer) { |
| 145 |
_errorContainer = errorContainer; |
| 146 |
} |
| 147 |
|
| 148 |
private: |
| 149 |
|
| 150 |
/** Errors container. */ |
| 151 |
ErrorContainer _errorContainer; |
| 152 |
/** Vector of all assembled instructions. */ |
| 153 |
std::vector<AssembledInstruction> _assembledInstructions; |
| 154 |
|
| 155 |
}; |
| 156 |
|
| 157 |
/** Assembler context. |
| 158 |
* @since 1.1.0 |
| 159 |
*/ |
| 160 |
class MultiPassAssemblerContext { |
| 161 |
|
| 162 |
public: |
| 163 |
|
| 164 |
/** |
| 165 |
* Default constructor. |
| 166 |
* @since 1.1.0 |
| 167 |
*/ |
| 168 |
MultiPassAssemblerContext() : |
| 169 |
_symbolTable( NULL) { |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Creates assembler context for given operating mode and instruction pointer. |
| 174 |
* @param operatingMode The operating mode. |
| 175 |
* @param ip The instruction pointer. |
| 176 |
* @since 1.1.0 |
| 177 |
*/ |
| 178 |
MultiPassAssemblerContext(EntryPoint::OperatingMode operatingMode, |
| 179 |
fcml_ip ip = 0) : |
| 180 |
_entryPoint(operatingMode, ip), _symbolTable( NULL) { |
| 181 |
} |
| 182 |
|
| 183 |
public: |
| 184 |
|
| 185 |
/** |
| 186 |
* Gets constant reference to assembler configuration. |
| 187 |
* |
| 188 |
* @return Assembler configuration. |
| 189 |
* @since 1.1.0 |
| 190 |
*/ |
| 191 |
const AssemblerConf& getConfig() const { |
| 192 |
return _config; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Gets reference to the assembler configuration. |
| 197 |
* |
| 198 |
* @return Assembler configuration. |
| 199 |
* @since 1.1.0 |
| 200 |
*/ |
| 201 |
AssemblerConf& getConfig() { |
| 202 |
return _config; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Sets a new assembler configuration. |
| 207 |
* |
| 208 |
* @return Assembler configuration. |
| 209 |
* @since 1.1.0 |
| 210 |
*/ |
| 211 |
void setConfig(const AssemblerConf &config) { |
| 212 |
_config = config; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Gets constant reference to the entry point. |
| 217 |
* |
| 218 |
* @return Entry point. |
| 219 |
* @since 1.1.0 |
| 220 |
*/ |
| 221 |
const EntryPoint& getEntryPoint() const { |
| 222 |
return _entryPoint; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Gets reference to the entry point. |
| 227 |
* |
| 228 |
* @return Entry point. |
| 229 |
* @since 1.1.0 |
| 230 |
*/ |
| 231 |
EntryPoint& getEntryPoint() { |
| 232 |
return _entryPoint; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Sets a new entry point. |
| 237 |
* |
| 238 |
* @param entryPoint Entry point to be set. |
| 239 |
* @since 1.1.0 |
| 240 |
*/ |
| 241 |
void setEntryPoint(const EntryPoint &entryPoint) { |
| 242 |
_entryPoint = entryPoint; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Sets a new instruction pointer. |
| 247 |
* |
| 248 |
* @param ip A new instruction pointer. |
| 249 |
* @since 1.1.0 |
| 250 |
*/ |
| 251 |
void setIP(fcml_ip ip) { |
| 252 |
_entryPoint.setIP(ip); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Increments the instruction pointer by given number of bytes. |
| 257 |
* |
| 258 |
* @param ip The number of bytes the instruction pointer should |
| 259 |
* be incremented by. |
| 260 |
* @since 1.1.0 |
| 261 |
*/ |
| 262 |
void incrementIP(fcml_ip ip) { |
| 263 |
_entryPoint.incrementIP(ip); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Sets a new processor operating mode for the entry point. |
| 268 |
* |
| 269 |
* @param operatingMode The new operating mode. |
| 270 |
* @since 1.1.0 |
| 271 |
*/ |
| 272 |
void setOperatingMode(EntryPoint::OperatingMode operatingMode) { |
| 273 |
_entryPoint.setOpMode(operatingMode); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Sets a new address size attribute for the entry point. |
| 278 |
* |
| 279 |
* @param addressSizeAttribute The address size attribute. |
| 280 |
* @since 1.1.0 |
| 281 |
*/ |
| 282 |
void setAddressSizeAttribute(fcml_usize addressSizeAttribute) { |
| 283 |
_entryPoint.setAddressSizeAttribute(addressSizeAttribute); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Sets a new operand size attribute for the entry point. |
| 288 |
* |
| 289 |
* @param operandSizeAttribute The operand size attribute. |
| 290 |
* @since 1.1.0 |
| 291 |
*/ |
| 292 |
void setOperandSizeAttribute(fcml_usize operandSizeAttribute) { |
| 293 |
_entryPoint.setOperandSizeAttribute(operandSizeAttribute); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Gets a pointer to the constant symbol table stored in the context. |
| 298 |
* |
| 299 |
* @return The pointer to the symbol table. |
| 300 |
* @since 1.1.0 |
| 301 |
*/ |
| 302 |
const SymbolTable* getSymbolTable() const { |
| 303 |
return _symbolTable; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Gets a pointer to the symbol table stored in the context. |
| 308 |
* |
| 309 |
* @return The pointer to the symbol table. |
| 310 |
* @since 1.1.0 |
| 311 |
*/ |
| 312 |
SymbolTable* getSymbolTable() { |
| 313 |
return _symbolTable; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Sets a new symbol table for the context. |
| 318 |
* |
| 319 |
* @param symbolTable The new symbol table to be set. |
| 320 |
* @since 1.1.0 |
| 321 |
*/ |
| 322 |
void setSymbolTable(SymbolTable *symbolTable) { |
| 323 |
_symbolTable = symbolTable; |
| 324 |
} |
| 325 |
|
| 326 |
private: |
| 327 |
/** The entry point. */ |
| 328 |
EntryPoint _entryPoint; |
| 329 |
/** The assembler configuration. */ |
| 330 |
AssemblerConf _config; |
| 331 |
/** The symbol table assigned to the context. */ |
| 332 |
SymbolTable *_symbolTable; |
| 333 |
}; |
| 334 |
|
| 335 |
/** |
| 336 |
* An assembler wrapper, as you can see the assembler context is |
| 337 |
* managed internally and |
| 338 |
* is not exposed outside. |
| 339 |
*/ |
| 340 |
class MultiPassAssembler: public NonCopyable, |
| 341 |
protected DialectAware, |
| 342 |
protected SymbolTableAware { |
| 343 |
public: |
| 344 |
|
| 345 |
/** |
| 346 |
* Creates multi-pass assembler for a dialect. |
| 347 |
* @param dialect The dialect for the assembler. |
| 348 |
* @throw InitException Cannot initialize the assembler. |
| 349 |
* @since 1.1.0 |
| 350 |
*/ |
| 351 |
MultiPassAssembler(Dialect &dialect) : |
| 352 |
_dialect(dialect) { |
| 353 |
fcml_ceh_error error = ::fcml_fn_assembler_init(extractDialect(dialect), |
| 354 |
&_assembler); |
| 355 |
if (error) { |
| 356 |
throw InitException(FCML_TEXT("Cannot initialize the assembler."), |
| 357 |
error); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Virtual destructor. |
| 363 |
* @since 1.1.0 |
| 364 |
*/ |
| 365 |
virtual ~MultiPassAssembler() { |
| 366 |
if (_assembler) { |
| 367 |
::fcml_fn_assembler_free(_assembler); |
| 368 |
_assembler = NULL; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
public: |
| 373 |
|
| 374 |
/** |
| 375 |
* Assembles given instruction model. |
| 376 |
* |
| 377 |
* @param ctx Assembler context. |
| 378 |
* @param instructions A pointer to the NULL terminated array of |
| 379 |
* the instructions. |
| 380 |
* @param[out] result Assembler result. |
| 381 |
* @throw AssemblingFailedException Assembler failed. |
| 382 |
* @return Error code. |
| 383 |
* @since 1.1.0 |
| 384 |
*/ |
| 385 |
fcml_ceh_error assemble(MultiPassAssemblerContext &ctx, |
| 386 |
const fcml_string *instructions, MultiPassAssemblerResult &result) { |
| 387 |
|
| 388 |
/* Prepare assembler context. */ |
| 389 |
fcml_st_lag_assembler_context context = { 0 }; |
| 390 |
|
| 391 |
AssemblerTypeConverter::convert(ctx.getConfig(), context.configuration); |
| 392 |
TypeConverter::convert(ctx.getEntryPoint(), context.entry_point); |
| 393 |
|
| 394 |
SymbolTable *symbolTable = ctx.getSymbolTable(); |
| 395 |
context.symbol_table = |
| 396 |
symbolTable ? extractSymbolTable(*symbolTable) : NULL; |
| 397 |
context.assembler = _assembler; |
| 398 |
|
| 399 |
/* Prepare assembler result. */ |
| 400 |
fcml_st_lag_assembler_result res; |
| 401 |
::fcml_fn_lag_assembler_result_prepare(&res); |
| 402 |
|
| 403 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 404 |
|
| 405 |
try { |
| 406 |
|
| 407 |
result.clear(); |
| 408 |
|
| 409 |
error = ::fcml_fn_lag_assemble(&context, instructions, &res); |
| 410 |
|
| 411 |
/* Failed or not, convert assembler errors. */ |
| 412 |
|
| 413 |
ErrorContainer errorContainer; |
| 414 |
ErrorTypeConverter::convert(res.errors, errorContainer); |
| 415 |
|
| 416 |
/* Prepares assembler result. */ |
| 417 |
|
| 418 |
result.setErrorContainer(errorContainer); |
| 419 |
|
| 420 |
if (error && ctx.getConfig().isThrowExceptionOnError()) { |
| 421 |
::fcml_fn_lag_assembler_result_free(&res); |
| 422 |
throw AssemblingFailedException( |
| 423 |
errorContainer.prepareErrorMessage( |
| 424 |
FCML_TEXT("Assembling failed")), errorContainer, |
| 425 |
error); |
| 426 |
} |
| 427 |
|
| 428 |
if (!error) { |
| 429 |
|
| 430 |
std::vector<AssembledInstruction> &assembledInstructions = |
| 431 |
result.getAssembledInstructions(); |
| 432 |
|
| 433 |
assembledInstructions.clear(); |
| 434 |
|
| 435 |
ErrorContainer instructionWarnings; |
| 436 |
fcml_st_assembled_instruction *next_instruction = |
| 437 |
res.instructions; |
| 438 |
while (next_instruction) { |
| 439 |
fcml_st_ceh_error_container &instruction_warnings = |
| 440 |
next_instruction->warnings; |
| 441 |
ErrorTypeConverter::convert(instruction_warnings, |
| 442 |
instructionWarnings); |
| 443 |
const AssembledInstruction assembledInstruction( |
| 444 |
next_instruction->code, |
| 445 |
next_instruction->code_length, instructionWarnings); |
| 446 |
assembledInstructions.push_back(assembledInstruction); |
| 447 |
next_instruction = next_instruction->next; |
| 448 |
} |
| 449 |
|
| 450 |
// Convert it back to the context because it might have been |
| 451 |
// modified during assembling process (IP incrementation etc). |
| 452 |
TypeConverter::convert(context.entry_point, |
| 453 |
ctx.getEntryPoint()); |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
::fcml_fn_lag_assembler_result_free(&res); |
| 458 |
|
| 459 |
} catch (std::exception &exc) { |
| 460 |
// If anything failed, free assembler results. |
| 461 |
::fcml_fn_lag_assembler_result_free(&res); |
| 462 |
throw exc; |
| 463 |
} |
| 464 |
|
| 465 |
return error; |
| 466 |
} |
| 467 |
|
| 468 |
private: |
| 469 |
|
| 470 |
// The dialect used by the assembler. |
| 471 |
Dialect &_dialect; |
| 472 |
// The initialized assembler instance used by the wrapper. |
| 473 |
fcml_st_assembler *_assembler; |
| 474 |
|
| 475 |
}; |
| 476 |
|
| 477 |
} |
| 478 |
|
| 479 |
#endif /* FCML_LAG_ASSEMBLER_HPP_ */ |