fcml  1.1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
fcml_assembler.hpp
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2015 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 
27 #ifndef FCML_ASSEMBLER_HPP_
28 #define FCML_ASSEMBLER_HPP_
29 
30 #include <vector>
31 #include <ostream>
32 
33 #include "fcml_assembler.h"
34 
35 #include "fcml_common.hpp"
36 #include "fcml_errors.hpp"
37 #include "fcml_dialect.hpp"
38 
39 namespace fcml {
40 
46 public:
47  AssemblingFailedException( const fcml_cstring msg, ErrorContainer errorContainer = ErrorContainer(), fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR ) :
48  ErrorContainerAwareException( msg, errorContainer, error ){
49  }
50 };
51 
56 public:
57 
67  AssembledInstruction( const fcml_uint8_t *buffer, fcml_usize len, const ErrorContainer &errorContainer ) {
68  set( buffer, len, errorContainer );
69  }
70 
76  set( cpy._code, cpy._codeLength, cpy._warningContainer );
77  }
78 
87  if ( &cpy != this ) {
88  if( this->_code ) {
89  delete [] this->_code;
90  }
91  set( cpy._code, cpy._codeLength, cpy._warningContainer );
92  }
93  return *this;
94  }
95 
100  if( _code ) {
101  delete [] _code;
102  _code = NULL;
103  }
104  }
105 
106 public:
107 
114  const fcml_uint8_t* getCode() const {
115  return _code;
116  }
117 
124  fcml_usize getCodeLength() const {
125  return _codeLength;
126  }
127 
135  return _warningContainer;
136  }
137 
138 private:
139 
148  void set( const fcml_uint8_t *buffer, fcml_usize len, const ErrorContainer warnigns ) {
149  _warningContainer = warnigns;
150  if( len > 0 ) {
151  _code = new fcml_uint8_t[len];
152  for( fcml_usize i = 0; i < len; i++ ) {
153  _code[i] = buffer[i];
154  }
155  } else {
156  _code = NULL;
157  }
158  _codeLength = len;
159  }
160 
161 private:
162 
164  ErrorContainer _warningContainer;
165 
167  fcml_uint8_t *_code;
168 
170  fcml_usize _codeLength;
171 
172 };
173 
174 class Assembler;
175 
178 public:
179 
184  _chosenInstructionIndex(-1) {
185  }
186 
187 public:
188 
196  if( _chosenInstructionIndex == -1 ) {
197  return NULL;
198  }
199  return &(_assembledInstructions[ _chosenInstructionIndex ]);
200  }
201 
209  return _errorContainer;
210  }
211 
218  fcml_usize getSize() const {
219  return _assembledInstructions.size();
220  }
221 
228  operator const AssembledInstruction*() const {
229  if( _chosenInstructionIndex == -1 ) {
230  return NULL;
231  }
232  return &(_assembledInstructions[_chosenInstructionIndex]);
233  }
234 
242  const AssembledInstruction& operator[] ( fcml_usize index ) const {
243  if( index > _assembledInstructions.size() ) {
244  throw BadArgumentException(FCML_TEXT("Array index out of bound."), FCML_CEH_GEC_VALUE_OUT_OF_RANGE);
245  }
246  return _assembledInstructions[index];
247  }
248 
257  friend std::basic_ostream<fcml_uint8_t>& operator<< ( std::basic_ostream<fcml_uint8_t> &out, const AssemblerResult &result ) {
258  const AssembledInstruction *assembled = result.getChosenInstruction();
259  if( assembled ) {
260  // If chosen instruction is not available, do not
261  // stream anything. It's not so common, because
262  // instructions choosers are obliged to chose something.
263  if( assembled->getCode() && assembled->getCodeLength() > 0 ) {
264  out.write( assembled->getCode(), assembled->getCodeLength() );
265  }
266  }
267  return out;
268  }
269 
274  void clear() {
275  _errorContainer.clean();
276  _assembledInstructions.clear();
277  _chosenInstructionIndex = -1;
278  }
279 
280 protected:
281 
282  friend Assembler;
283 
284  void setErrorContainer(const ErrorContainer& errorContainer) {
285  _errorContainer = errorContainer;
286  }
287 
288  std::vector<AssembledInstruction>& getAssembledInstructions() {
289  return _assembledInstructions;
290  }
291 
292  void setChoosenInstructionIndex( fcml_int index ) {
293  _chosenInstructionIndex = index;
294  }
295 
296 private:
297 
299  ErrorContainer _errorContainer;
301  std::vector<AssembledInstruction> _assembledInstructions;
303  fcml_int _chosenInstructionIndex;
304 
305 };
306 
312 public:
313 
318  _throwExceptionOnError(true),
319  _incrementIp(true),
320  _enableErrorMessages(true),
321  _chooseSibEncoding(false),
322  _chooseAbsEncoding(false),
323  _forceRexPrefix(false),
324  _forceThreeByteVEX(false),
325  _noBranchPrediction(false),
326  _optimizer(NULL),
327  _optimizerFlags(0),
328  _chooser(NULL) {
329  }
330 
331 public:
332 
336  bool isChooseAbsEncoding() const {
337  return _chooseAbsEncoding;
338  }
339 
343  void setChooseAbsEncoding(bool chooseAbsEncoding) {
344  _chooseAbsEncoding = chooseAbsEncoding;
345  }
346 
351  return _chooser;
352  }
353 
358  _chooser = chooser;
359  }
360 
364  bool isChooseSibEncoding() const {
365  return _chooseSibEncoding;
366  }
367 
371  void setChooseSibEncoding(bool chooseSibEncoding) {
372  _chooseSibEncoding = chooseSibEncoding;
373  }
374 
378  bool isEnableErrorMessages() const {
379  return _enableErrorMessages;
380  }
381 
385  void setEnableErrorMessages(bool enableErrorMessages) {
386  _enableErrorMessages = enableErrorMessages;
387  }
388 
392  bool isForceRexPrefix() const {
393  return _forceRexPrefix;
394  }
395 
399  void setForceRexPrefix(bool forceRexPrefix) {
400  _forceRexPrefix = forceRexPrefix;
401  }
402 
406  bool isForceThreeByteVex() const {
407  return _forceThreeByteVEX;
408  }
409 
413  void setForceThreeByteVex(bool forceThreeByteVex) {
414  _forceThreeByteVEX = forceThreeByteVex;
415  }
416 
420  bool isIncrementIp() const {
421  return _incrementIp;
422  }
423 
427  void setIncrementIp(bool incrementIp) {
428  _incrementIp = incrementIp;
429  }
430 
435  return _optimizer;
436  }
437 
442  _optimizer = optimizer;
443  }
444 
448  fcml_uint16_t getOptimizerFlags() const {
449  return _optimizerFlags;
450  }
451 
455  void setOptimizerFlags(fcml_uint16_t optimizerFlags) {
456  _optimizerFlags = optimizerFlags;
457  }
458 
465  bool isThrowExceptionOnError() const {
466  return _throwExceptionOnError;
467  }
468 
475  void setThrowExceptionOnError(bool throwExceptionOnError) {
476  _throwExceptionOnError = throwExceptionOnError;
477  }
478 
479 private:
480  bool _throwExceptionOnError;
481  bool _incrementIp;
482  bool _enableErrorMessages;
483  bool _chooseSibEncoding;
484  bool _chooseAbsEncoding;
485  bool _forceRexPrefix;
486  bool _forceThreeByteVEX;
487  bool _noBranchPrediction;
488  fcml_fnp_asm_optimizer _optimizer;
489  fcml_uint16_t _optimizerFlags;
491 };
492 
499 
500 public:
501 
506  }
507 
516  _entryPoint( operatingMode, ip ) {
517  }
518 
519 public:
520 
527  const AssemblerConf& getConfig() const {
528  return _config;
529  }
530 
538  return _config;
539  }
540 
549  void setConfig(const AssemblerConf &config) {
550  _config = config;
551  }
552 
559  const EntryPoint& getEntryPoint() const {
560  return _entryPoint;
561  }
562 
570  return _entryPoint;
571  }
580  void setEntryPoint(const EntryPoint &entryPoint) {
581  _entryPoint = entryPoint;
582  }
583 
590  void setIP( fcml_ip ip ) {
591  _entryPoint.setIP(ip);
592  }
593 
600  void incrementIP( fcml_ip ip ) {
601  _entryPoint.incrementIP( ip );
602  }
603 
611  _entryPoint.setOpMode( operatingMode );
612  }
613 
620  void setAddressSizeAttribute( fcml_usize addressSizeAttribute ) {
621  _entryPoint.setAddressSizeAttribute( addressSizeAttribute );
622  }
623 
630  void setOperandSizeAttribute( fcml_usize operandSizeAttribute ) {
631  _entryPoint.setOperandSizeAttribute( operandSizeAttribute );
632  }
633 
634 private:
635 
637  EntryPoint _entryPoint;
639  AssemblerConf _config;
640 
641 };
642 
648 public:
649 
650  static void convert( const fcml_st_assembler_context &src, AssemblerContext &dest ) {
651  // Converts assembler configuration and entry point. Both of them
652  // have dedicated conversion methods.
653  convert( src.configuration, dest.getConfig() );
654  TypeConverter::convert( src.entry_point, dest.getEntryPoint() );
655  }
656 
657  static void convert( const AssemblerContext &src, fcml_st_assembler_context &dest ) {
658  // Converts assembler configuration and entry point. Both of them
659  // have dedicated conversion methods.
660  convert( src.getConfig(), dest.configuration );
661  TypeConverter::convert( src.getEntryPoint(), dest.entry_point );
662  }
663 
664  static void convert( const fcml_st_assembler_conf &src, AssemblerConf &dest ) {
665  dest.setChooseAbsEncoding( FCML_TO_CPP_BOOL( src.choose_abs_encoding ) );
666  dest.setChooseSibEncoding( FCML_TO_CPP_BOOL( src.choose_sib_encoding ) );
667  dest.setChooser( src.chooser );
668  dest.setEnableErrorMessages( FCML_TO_CPP_BOOL( src.enable_error_messages ) );
669  dest.setForceRexPrefix( FCML_TO_CPP_BOOL( src.force_rex_prefix ) );
670  dest.setForceThreeByteVex( FCML_TO_CPP_BOOL( src.force_three_byte_VEX ) );
671  dest.setIncrementIp( FCML_TO_CPP_BOOL( src.increment_ip ) );
672  dest.setOptimizer( src.optimizer );
674  }
675 
676  static void convert( const AssemblerConf &src, fcml_st_assembler_conf &dest ) {
679  dest.chooser = src.getChooser();
681  dest.force_rex_prefix = src.isForceRexPrefix();
683  dest.increment_ip = src.isIncrementIp();
684  dest.optimizer = src.getOptimizer();
685  dest.optimizer_flags = src.getOptimizerFlags();
686  }
687 
688 };
689 
696 class Assembler: public NonCopyable, protected DialectAware {
697 public:
698 
706  Assembler(Dialect &dialect) :
707  _dialect(dialect) {
708  fcml_ceh_error error = ::fcml_fn_assembler_init( extractDialect( dialect ), &_assembler);
709  if (error) {
710  throw InitException(FCML_TEXT("Cannot initialize the assembler."), error);
711  }
712  }
713 
717  virtual ~Assembler() {
718  if (_assembler) {
719  ::fcml_fn_assembler_free(_assembler);
720  _assembler = NULL;
721  }
722  }
723 
724 public:
725 
736  fcml_ceh_error assemble( AssemblerContext &ctx, const Instruction &instruction, AssemblerResult &result ) {
737 
738  // Prepare assembler context.
740  AssemblerTypeConverter::convert( ctx, context );
741 
742  context.assembler = _assembler;
743 
744  // Prepare instruction.
745  fcml_st_instruction inst;
746  TypeConverter::convert( instruction, inst );
747 
748  // Prepare assembler result.
751 
753 
754  try {
755 
756  result.clear();
757 
758  error = ::fcml_fn_assemble( &context, &inst, &res );
759 
760  // Free instruction mnemonic.
761  TypeConverter::free( inst );
762 
763  // Failed or not, convert assembler errors.
764  ErrorContainer errorContainer;
765  ErrorTypeConverter::convert( res.errors, errorContainer );
766 
767  // Prepares assembler result.
768  result.setErrorContainer( errorContainer );
769 
770  if( error && ctx.getConfig().isThrowExceptionOnError() ) {
772  throw AssemblingFailedException( FCML_TEXT("Assembling failed."), errorContainer, error );
773  }
774 
775  if( !error ) {
776 
777  std::vector<AssembledInstruction> &assembledInstructions = result.getAssembledInstructions();
778 
779  assembledInstructions.clear();
780 
781  if( res.number_of_instructions > 0 ) {
782  ErrorContainer instructionWarnings;
783  fcml_int i = 0;
784  fcml_st_assembled_instruction *next_instruction = res.instructions;
785  while( next_instruction ) {
786  fcml_st_ceh_error_container &instruction_warnings = next_instruction->warnings;
787  ErrorTypeConverter::convert( instruction_warnings, instructionWarnings );
788  AssembledInstruction assembledInstruction( next_instruction->code, next_instruction->code_length, instructionWarnings );
789  assembledInstructions.push_back( assembledInstruction );
790  if( next_instruction == res.chosen_instruction ) {
791  result.setChoosenInstructionIndex(i);
792  }
793  next_instruction = next_instruction->next;
794  i++;
795  }
796  }
797 
798  // Convert it back to the context because it might have been
799  // modified during assembling process (IP incrementing etc).
800  TypeConverter::convert( context.entry_point, ctx.getEntryPoint() );
801 
802  }
803 
805 
806  } catch( std::exception &exc ) {
807  // If anything failed, free assembler results.
808  TypeConverter::free( inst );
810  throw exc;
811  }
812 
813  return error;
814  }
815 
821  Dialect& getDialect() const {
822  return _dialect;
823  }
824 
825 private:
826 
827  // A dialect used by the assembler.
828  Dialect &_dialect;
829 
830  // An initialized assembler instance.
831  fcml_st_assembler *_assembler;
832 
833 };
834 
838 class CodeIterator: public Iterator<fcml_uint8_t> {
839 public:
840 
846  CodeIterator( std::vector<AssembledInstruction> &assembledInstructions ) :
847  _buffer(NULL),
848  _len(0),
849  _pos(0),
850  _iterator(assembledInstructions.begin()),
851  _assembledInstructions(assembledInstructions) {
852  }
853 
857  virtual ~CodeIterator() {
858  }
859 
866  bool hasNext() {
867  if( _buffer && _pos >= _len ) {
868  _buffer = NULL;
869  }
870  if( !_buffer ) {
871  if( _iterator == _assembledInstructions.end() ) {
872  return false;
873  }
874  AssembledInstruction &current = *_iterator++;
875  _buffer = current.getCode();
876  _len = current.getCodeLength();
877  _pos = 0;
878  }
879  return true;
880  }
881 
889  fcml_uint8_t next() {
890  if( ( !_buffer || _pos >= _len ) && !hasNext() ) {
891  throw IllegalStateException( FCML_TEXT( "No more elements in the iterator." ) );
892  }
893  return _buffer[_pos++];
894  }
895 
896 private:
897 
899  const fcml_uint8_t *_buffer;
901  fcml_usize _len;
903  fcml_usize _pos;
905  std::vector<AssembledInstruction>::iterator _iterator;
907  std::vector<AssembledInstruction> &_assembledInstructions;
908 
909 };
910 
911 }
912 
913 #endif //FCML_ASSEMBLER_HPP_
AssemblerConf & getConfig()
Gets assembler configuration associated with the context.
Definition: fcml_assembler.hpp:537
AssembledInstruction(const AssembledInstruction &cpy)
Copy constructor.
Definition: fcml_assembler.hpp:75
fcml_ptr(LIB_CALL * fcml_fnp_asm_instruction_chooser)(fcml_st_chooser_context *chooser_context)
Instruction chooser function pointer declaration.
Definition: fcml_choosers.h:67
C++ wrappers common classes.
void setEnableErrorMessages(bool enableErrorMessages)
Definition: fcml_assembler.hpp:385
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_assembler.hpp:475
fcml_bool choose_abs_encoding
If memory address can be encoded as relative or absolute value choose the absolute addressing...
Definition: fcml_assembler.h:56
Assembler result.
Definition: fcml_assembler.hpp:177
Illegal state exception.
Definition: fcml_common.hpp:228
void setIP(fcml_ip ip)
Sets a new instruction pointer for the entry point.
Definition: fcml_common.hpp:626
void setOperandSizeAttribute(fcml_usize operandSizeAttribute)
Sets a new operand size attribute for the entry point.
Definition: fcml_common.hpp:606
fcml_usize getSize() const
Gets number of instructions alternatives available in the result.
Definition: fcml_assembler.hpp:218
Assembling failed.
Definition: fcml_assembler.hpp:45
fcml_st_assembler_conf configuration
Assembler behaviour can be configured here.
Definition: fcml_assembler.h:106
virtual ~Assembler()
Definition: fcml_assembler.hpp:717
void setChooseSibEncoding(bool chooseSibEncoding)
Definition: fcml_assembler.hpp:371
AssembledInstruction & operator=(const AssembledInstruction &cpy)
Copies one instruction into another.
Definition: fcml_assembler.hpp:86
void setIP(fcml_ip ip)
Sets instruction pointer directly into the entry point.
Definition: fcml_assembler.hpp:590
void incrementIP(fcml_ip ip)
Increments the instruction pointer by given number of bytes.
Definition: fcml_common.hpp:656
void setAddressSizeAttribute(fcml_usize addressSizeAttribute)
Sets a new address size attribute for the entry point.
Definition: fcml_assembler.hpp:620
Converts objects to their structures counterparts.
Definition: fcml_assembler.hpp:647
const fcml_uint8_t * getCode() const
Gets pointer to machine code buffer.
Definition: fcml_assembler.hpp:114
Assembler result.
Definition: fcml_assembler.h:90
bool isForceRexPrefix() const
Definition: fcml_assembler.hpp:392
Assembler context.
Definition: fcml_assembler.hpp:498
fcml_st_assembled_instruction * instructions
Chain of assembled instructions.
Definition: fcml_assembler.h:94
EntryPoint & getEntryPoint()
Gets reference to the entry point instance associated with the context.
Definition: fcml_assembler.hpp:569
Assembler runtime configuration.
Definition: fcml_assembler.h:48
Container for all collected errors and warnings.
Definition: fcml_errors.h:162
void setOptimizerFlags(fcml_uint16_t optimizerFlags)
Definition: fcml_assembler.hpp:455
AssembledInstruction(const fcml_uint8_t *buffer, fcml_usize len, const ErrorContainer &errorContainer)
Creates an assembled instruction basing on given code buffer and errors.
Definition: fcml_assembler.hpp:67
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
fcml_st_assembler * assembler
Assembler instance that should be used to assemble instructions.
Definition: fcml_assembler.h:104
struct fcml_st_assembled_instruction * next
Next assembled instruction in the chain.
Definition: fcml_assembler.h:77
Holds instruction pointer, processor operating mode and memory segment flags.
Definition: fcml_common.hpp:499
bool isForceThreeByteVex() const
Definition: fcml_assembler.hpp:406
Base class for all exceptions that are aware of ErrorContainer.
Definition: fcml_errors.hpp:347
void setAddressSizeAttribute(fcml_usize addressSizeAttribute)
Sets a new address size attribute for the entry point.
Definition: fcml_common.hpp:586
Generic instruction model.
Definition: fcml_common.h:611
bool isIncrementIp() const
Definition: fcml_assembler.hpp:420
LIB_EXPORT void LIB_CALL fcml_fn_assembler_result_prepare(fcml_st_assembler_result *result)
Prepares reusable result holder for assembler.
fcml_uint16_t optimizer_flags
This field is passed to the chosen optimizer.
Definition: fcml_assembler.h:64
const AssembledInstruction & operator[](fcml_usize index) const
Gets an assembled instruction reference by its index.
Definition: fcml_assembler.hpp:242
fcml_st_ceh_error_container warnings
Warning messages related to assembled instruction.
Definition: fcml_assembler.h:79
const AssembledInstruction * getChosenInstruction() const
Gets instruction chosen by the assembler as the preferred one.
Definition: fcml_assembler.hpp:195
Definition: fcml_assembler.hpp:39
void setChooseAbsEncoding(bool chooseAbsEncoding)
Definition: fcml_assembler.hpp:343
fcml_ceh_error assemble(AssemblerContext &ctx, const Instruction &instruction, AssemblerResult &result)
Assembles given generic instruction model.
Definition: fcml_assembler.hpp:736
fcml_st_entry_point entry_point
Instruction entry point configuration.
Definition: fcml_assembler.h:108
void setForceThreeByteVex(bool forceThreeByteVex)
Definition: fcml_assembler.hpp:413
Inherit from this class in order to get access to the native FCML dialect structure.
Definition: fcml_dialect.hpp:97
fcml_st_assembled_instruction * chosen_instruction
Instruction chosen by used instruction chooser; otherwise NULL.
Definition: fcml_assembler.h:96
Used mainly in case of integers and offsets.
Definition: fcml_errors.h:54
void setChooser(fcml_fnp_asm_instruction_chooser chooser)
Definition: fcml_assembler.hpp:357
bool isChooseSibEncoding() const
Definition: fcml_assembler.hpp:364
const EntryPoint & getEntryPoint() const
Gets reference to the constant entry point instance associated with the context.
Definition: fcml_assembler.hpp:559
void setIncrementIp(bool incrementIp)
Definition: fcml_assembler.hpp:427
LIB_EXPORT void LIB_CALL fcml_fn_assembler_result_free(fcml_st_assembler_result *result)
Cleans result holder.
const AssemblerConf & getConfig() const
Gets constant assembler configuration associated with the context.
Definition: fcml_assembler.hpp:527
bool isEnableErrorMessages() const
Definition: fcml_assembler.hpp:378
void clear()
Clears assembler result by removing all assembled instructions, errors and reseting the chosen instru...
Definition: fcml_assembler.hpp:274
An assembler wrapper.
Definition: fcml_assembler.hpp:696
fcml_bool enable_error_messages
True if optional error and warning messages should be collected during processing.
Definition: fcml_assembler.h:52
AssemblerResult()
Definition: fcml_assembler.hpp:183
fcml_int64_t fcml_ip
General instruction pointer holder.
Definition: fcml_common.h:95
Describes an assembled instruction.
Definition: fcml_assembler.hpp:55
fcml_fnp_asm_instruction_chooser getChooser() const
Definition: fcml_assembler.hpp:350
void setConfig(const AssemblerConf &config)
Copies given configuration to the instance associated with the context.
Definition: fcml_assembler.hpp:549
C++ wrapper for the base dialect.
bool hasNext()
Gets true if there is an another element in the iterator.
Definition: fcml_assembler.hpp:866
const ErrorContainer & getErrorContainer() const
Gets errors container.
Definition: fcml_assembler.hpp:208
Assembler runtime context.
Definition: fcml_assembler.h:102
OperatingMode
Supported operating modes.
Definition: fcml_common.hpp:506
void setOperatingMode(EntryPoint::OperatingMode operatingMode)
Sets processor operating mode directly into the entry point.
Definition: fcml_assembler.hpp:610
void setForceRexPrefix(bool forceRexPrefix)
Definition: fcml_assembler.hpp:399
fcml_uint8_t next()
Gets the next element from the iterator.
Definition: fcml_assembler.hpp:889
fcml_fnp_asm_instruction_chooser chooser
instruction chooser implementation that should be used by assembler to choose most appropriate instru...
Definition: fcml_assembler.h:67
A base iterator interface.
Definition: fcml_common.hpp:98
fcml_fnp_asm_optimizer optimizer
Optimizer implementation that should be used by assembler.
Definition: fcml_assembler.h:62
void setOptimizer(fcml_fnp_asm_optimizer optimizer)
Definition: fcml_assembler.hpp:441
void incrementIP(fcml_ip ip)
Increments entry point by given number of bytes.
Definition: fcml_assembler.hpp:600
fcml_usize getCodeLength() const
Gets number of bytes in the buffer.
Definition: fcml_assembler.hpp:124
Wraps multiple errors into one component.
Definition: fcml_errors.hpp:148
fcml_uint16_t getOptimizerFlags() const
Definition: fcml_assembler.hpp:448
fcml_bool force_rex_prefix
Sometimes REX prefix is useless so it is just omitted in the final machine code.
Definition: fcml_assembler.h:58
fcml_bool force_three_byte_VEX
Every 2 byte VEX/XOP prefix can be encoded using three byte form.
Definition: fcml_assembler.h:60
const ErrorContainer & getWarningContainer() const
Gets reference to the errors container.
Definition: fcml_assembler.hpp:134
fcml_uint8_t * code
Instruction machine code.
Definition: fcml_assembler.h:81
void setOpMode(OperatingMode opMode)
Sets a new processor operating mode for the entry point.
Definition: fcml_common.hpp:646
Describes an instruction.
Definition: fcml_common.hpp:6207
virtual ~CodeIterator()
Definition: fcml_assembler.hpp:857
Operation succeed.
Definition: fcml_errors.h:42
void clean()
Cleans all errors and warnings.
Definition: fcml_errors.hpp:296
fcml_fnp_asm_optimizer getOptimizer() const
Definition: fcml_assembler.hpp:434
AssemblerContext()
Definition: fcml_assembler.hpp:505
Assembler configuration.
Definition: fcml_assembler.hpp:311
void setOperandSizeAttribute(fcml_usize operandSizeAttribute)
Sets a new operand size attribute for the entry point.
Definition: fcml_assembler.hpp:630
Bad arguments.
Definition: fcml_common.hpp:217
fcml_ceh_error(LIB_CALL * fcml_fnp_asm_optimizer)(fcml_st_asm_optimizer_context *context, fcml_st_asm_optimizer_processing_details *ds_flags, fcml_fnp_asm_optimizer_callback callback, fcml_ptr args)
Function pointer declaration for optimizers.
Definition: fcml_optimizers.h:94
LIB_EXPORT void LIB_CALL fcml_fn_assembler_free(fcml_st_assembler *assembler)
Frees assembler instance.
bool isChooseAbsEncoding() const
Definition: fcml_assembler.hpp:336
Encoded instruction.
Definition: fcml_assembler.h:75
An abstract dialect.
Definition: fcml_dialect.hpp:41
virtual ~AssembledInstruction()
Definition: fcml_assembler.hpp:99
void setEntryPoint(const EntryPoint &entryPoint)
Copies given entry point to the instance associated with the context.
Definition: fcml_assembler.hpp:580
bool isThrowExceptionOnError() const
Returns true if exception should be thrown when assembling fails.
Definition: fcml_assembler.hpp:465
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_assembler_init(const fcml_st_dialect *dialect, fcml_st_assembler **assembler)
Initializes assembler for given dialect.
fcml_bool choose_sib_encoding
If there are SIB and "ModR/M only" encodings available, choose the SIB based one. ...
Definition: fcml_assembler.h:54
AssemblerContext(EntryPoint::OperatingMode operatingMode, fcml_ip ip=0)
Creates an entry point instance for given operating mode and optional instruction pointer...
Definition: fcml_assembler.hpp:515
fcml_st_dialect * extractDialect(const Dialect &dialect) const
Extracts the native FCML dialect from the dialect object.
Definition: fcml_dialect.hpp:119
fcml_usize number_of_instructions
Number of encoded instruction forms.
Definition: fcml_assembler.h:98
Structures and functions declarations related to one-line FCML assembler.
struct fcml_st_assembler fcml_st_assembler
Abstract assembler representation.
Definition: fcml_assembler.h:43
ErrorContainerAwareException(const fcml_cstring &msg, const ErrorContainer &errorContainer, fcml_ceh_error error=FCML_CEH_GEC_NO_ERROR)
Creates an error container aware exception instance and sets basic information for it...
Definition: fcml_errors.hpp:357
Component can not be initialized correctly.
Definition: fcml_common.hpp:206
fcml_st_ceh_error_container errors
Error and warning messages from assembler.
Definition: fcml_assembler.h:92
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:139
fcml_usize code_length
Instruction code length in bytes.
Definition: fcml_assembler.h:83
Dialect & getDialect() const
Gets dialect associated with the assembler.
Definition: fcml_assembler.hpp:821
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_assemble(fcml_st_assembler_context *context, const fcml_st_instruction *instruction, fcml_st_assembler_result *result)
Assembles one instruction encoded in the generic instruction model.
Iterates over machine code bytes from assembled instructions.
Definition: fcml_assembler.hpp:838
Object which shouldn't be copied can inherit from this class.
Definition: fcml_common.hpp:263
Assembler(Dialect &dialect)
Creates an assembler instance for given dialect.
Definition: fcml_assembler.hpp:706
C++ wrapper for the FCML errors handling.
CodeIterator(std::vector< AssembledInstruction > &assembledInstructions)
Creates a code iterator instance.
Definition: fcml_assembler.hpp:846
fcml_bool increment_ip
Set to true in order to force assembler to increment IP address by length of the assembled instructio...
Definition: fcml_assembler.h:50
AssemblerConf()
Definition: fcml_assembler.hpp:317