<commit_before>// Dependencies ---------------------------------------------------------------
// ----------------------------------------------------------------------------
var Token = require('../parser/Lexer').Token;


// Assembly Instruction Optimizer ---------------------------------------------
// ----------------------------------------------------------------------------
function Optimizer(instr, unsafe, next, nextTwo, nextThree) {

    var opCode = instr.raw[0];
    switch(opCode) {

        // ld a,0 -> xor a
        //
        // -> save 1 byte and 3 T-states
        case 0x3E:
            if (instr.resolvedArg === 0x00) {
                instr.rewrite('xor', 4, [0x07 + 0xA8]);
                return 1;
            }
            break;

        // cp 0 -> or a
        //
        // save 1 byte and 3 T-states
        case 0xFE:
            if (instr.resolvedArg === 0x00) {
                instr.rewrite('or', 4, [0x07 + 0xB0]);
            }
            break;

        // ld a,[someLabel] -> ldh a,$XX
        case 0xFA:
            // Transform memory loads into high loads if argument is
            // in the range of 0xff00-0xffff
            if (instr.resolvedArg >= 0xff00 && instr.resolvedArg <= 0xffff) {

                instr.rewrite(
                    'ldh', 12, [0xF0],
                    new Token('NUMBER', instr.resolvedArg & 0xff, instr.index),
                    true
                );

                return 1;

            }<commit_msg>Fix a optimizer producing incorrect code for `ld a,0 -> xor a` optimizations.<commit_after>// Dependencies ---------------------------------------------------------------
// ----------------------------------------------------------------------------
var Token = require('../parser/Lexer').Token;


// Assembly Instruction Optimizer ---------------------------------------------
// ----------------------------------------------------------------------------
function Optimizer(instr, unsafe, next, nextTwo, nextThree) {

    var opCode = instr.raw[0];
    switch(opCode) {

        // ld a,0 -> xor a
        //
        // -> save 1 byte and 3 T-states
        case 0x3E:
            if (instr.resolvedArg === 0x00) {
                instr.rewrite('xor', 4, [0x07 + 0xA8]);
                return 1;
            }
            break;

        // cp 0 -> or a
        //
        // save 1 byte and 3 T-states
        case 0xFE:
            if (instr.resolvedArg === 0x00) {
                instr.rewrite('or', 4, [0x07 + 0xB0]);
                return 1;
            }
            break;

        // ld a,[someLabel] -> ldh a,$XX
        case 0xFA:
            // Transform memory loads into high loads if argument is
            // in the range of 0xff00-0xffff
            if (instr.resolvedArg >= 0xff00 && instr.resolvedArg <= 0xffff) {

                instr.rewrite(
                    'ldh', 12, [0xF0],
                    new Token('NUMBER', instr.resolvedArg & 0xff, instr.index),
                    true
                );

                return 1;

            }
{'fraction_non_alphanumeric': 0.27008491182233835, 'fraction_numerical': 0.02939255388634879, 'mean_word_length': 0.8396396396396396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6851749', 'n_tokens_mistral': 831, 'n_tokens_neox': 723, 'n_words': 207}