<commit_before>// Copyright (c) 2016-2017 <NAME> <<EMAIL>>
// Provided under the MIT License: https://mit-license.org

#ifndef X86_OPCODE_H_

// Primary Reference:
//
//   Intel® 64 and IA-32 Architectures Software Developer Manuals, Volume 2
//
// Which can be downloaded as a pdf for free at:
//
//   https://software.intel.com/en-us/articles/intel-sdm
//
// Bookmarks:
//  - Instruction format Description      Vol. 2A Chapter 2.1
//    - Format diagram                    Section 2.1, Figure 2-1
//    - Addressing Modes Constants Table  Section 2.1.5, Tables 2-1, 2-2, 2-3
//    - REX Prefixes are for 64bit mode   Section 2.2
//  - Instruction set constants and args  Chapters 3-5
//    - Understanding the tables          Section 3.1

// TODO: Real error handling or enforce the constraints by api design
#if !defined(Assert)
#warn "If you don't #define assert to cause an error if the condition is true, " \
      "then it is possible to attempt to encode invalid instructions. This is " \
      "undefined behavior. Actual error handling is coming when I get to it." \
#define Assert(cond) ; //nothing
#endif

struct opcode_unpacked {
    // uint8_t Prefixes[4]; // not used
    uint8_t Opcode[2]; // 6 bits last two are direction and operand length
    bool HasModRM;
    uint8_t ModRM; // set operands: mod 2 bit, reg/opcode 3 bits, R/M 3 bits
    uint8_t SIB; // addressing modes: scale 2 bits, index 4 bits, base 3 bits
    uint8_t DispCount;
    uint8_t Displacement[4];
    uint8_t ImmCount;
    uint8_t Immediate[4];
};

enum addressing_mode {
    // ModRM bits 7,6 (0 is LSB)
    MEM        = 0x00,
    MEM_DISP8  = 0x40,
    MEM_DISP32 = 0x80,
    REG        = 0xC0,
    MODE_NONE  = 0xFF,
};

// no strings (for addressing_mode), it doesn't work with indexes

enum reg {
    // ModRM bits 2,1,0 (dest operand) or 5,4,3 (src operand) (0 is LSB)<commit_msg>Added additional comment to Assert guard block Now it works as an independent assembler api!<commit_after>// Copyright (c) 2016-2017 <NAME> <<EMAIL>>
// Provided under the MIT License: https://mit-license.org

#ifndef X86_OPCODE_H_

// Primary Reference:
//
//   Intel® 64 and IA-32 Architectures Software Developer Manuals, Volume 2
//
// Which can be downloaded as a pdf for free at:
//
//   https://software.intel.com/en-us/articles/intel-sdm
//
// Bookmarks:
//  - Instruction format Description      Vol. 2A Chapter 2.1
//    - Format diagram                    Section 2.1, Figure 2-1
//    - Addressing Modes Constants Table  Section 2.1.5, Tables 2-1, 2-2, 2-3
//    - REX Prefixes are for 64bit mode   Section 2.2
//  - Instruction set constants and args  Chapters 3-5
//    - Understanding the tables          Section 3.1

// TODO: Real error handling or enforce the constraints by api design
#if !defined(Assert)
#warn "If you don't #define assert to cause an error if the condition is true, " \
      "then it is possible to attempt to encode invalid instructions. This is " \
      "undefined behavior. Actual error handling is coming when I get to it." \
// You can also just make a function if you want.
// The Assert used in this project is in utils.h
#define Assert(cond) ; //nothing
#endif

struct opcode_unpacked {
    // uint8_t Prefixes[4]; // not used
    uint8_t Opcode[2]; // 6 bits last two are direction and operand length
    bool HasModRM;
    uint8_t ModRM; // set operands: mod 2 bit, reg/opcode 3 bits, R/M 3 bits
    uint8_t SIB; // addressing modes: scale 2 bits, index 4 bits, base 3 bits
    uint8_t DispCount;
    uint8_t Displacement[4];
    uint8_t ImmCount;
    uint8_t Immediate[4];
};

enum addressing_mode {
    // ModRM bits 7,6 (0 is LSB)
    MEM        = 0x00,
    MEM_DISP8  = 0x40,
    MEM_DISP32 = 0x80,
    REG        = 0xC0,
    MODE_NONE  = 0xFF,
};

// no strings (for addressing_mode), it doesn't work with indexes

enum reg {
    // ModRM bits 2,1,0 (dest operand) or 5,4,3 (src operand) (0 is LSB)
{'fraction_non_alphanumeric': 0.10163015792154866, 'fraction_numerical': 0.04228222109016811, 'mean_word_length': 2.5895795246800732, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4252532', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1266, 'n_words': 494}