<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re


class Instruction(object):
    def __init__(self):
        self._register_lut = {
            'r0': 0,  # General Register 0
            'r1': 1,  # General Register 1
            'r2': 2,  # General Register 2
            'r3': 3,  # General Register 3
            'r4': 4,  # General Register 4
            'r5': 5,  # General Register 5
            'r6': 6,  # General Register 6
            'r7': 7,  # General Register 7
        }

    def _get_register(self, reg):
        if reg in self._register_lut:
            return self._register_lut[reg]
        else:
            raise LookupError('Register is not implemented: %s' % reg)
            return ''

    def _get_complement(self, i, bits=8):
        if i < 0:
            i = abs(i + (1 << bits))
        return i

    def _str_to_int(self, string, bits=8):
        i = 0
        if len(string) < 3:
            i = int(string, 10)
        elif string[0:2] == '0b' or string[0:3] == '-0b':
            i = int(string, 2)
        elif string[0:2] == '0o' or string[0:3] == '-0o':
            i = int(string, 8)
        elif string[0:2] == '0x' or string[0:3] == '-0x':
            i = int(string, 16)
        else:
            i = int(string)

        return self._get_complement(i, bits)


class RInstruction(Instruction):
    def __init__(self):
        Instruction.__init__(self)

        self.instruction = 0xc000

    def compile(self, assembler):
        return self.instruction


class R1Instruction(RInstruction):
    def __init__(self, args):
        RInstruction.__init__(self)

        self.rd = self._get_register(args[0])
        self.rs = self._get_register(args[1])

        self.instruction += (self.rd << 8)
        self.instruction += (self.rs << 11)


class R2Instruction(RInstruction):
    def __init__(self, args):
        RInstruction.__init__(self)

        self.rd = self._get_register(args[0])
        self.d = int(args[1])

        self.instruction += (self.rd << 8)
        self.instruction += self.d


class LSInstruction(Instruction):
    def __init__(self, args):
        Instruction.__init__(self)

        self.ra = self._get_register(args[0])
        self.d = int(args[1])
        self.rb = self._get_register(args[2])

        self.instruction = 0x0000
        self.instruction += self.d
        self.instruction += (self.rb << 8)
        self.instruction += (self.ra << 11)

    def compile(self, assembler):
        return self.instruction


class IBInstruction(Instruction):
    def __init__(self):
        Instruction.__init__(self)
        self.instruction = 0x8000


class BInstruction(Instruction):
    def __init__(self, args):
        Instruction.__init__(self)
<commit_msg>Bugfix LSInstruction d field sign extension<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re


class Instruction(object):
    def __init__(self):
        self._register_lut = {
            'r0': 0,  # General Register 0
            'r1': 1,  # General Register 1
            'r2': 2,  # General Register 2
            'r3': 3,  # General Register 3
            'r4': 4,  # General Register 4
            'r5': 5,  # General Register 5
            'r6': 6,  # General Register 6
            'r7': 7,  # General Register 7
        }

    def _get_register(self, reg):
        if reg in self._register_lut:
            return self._register_lut[reg]
        else:
            raise LookupError('Register is not implemented: %s' % reg)
            return ''

    def _get_complement(self, i, bits=8):
        if i < 0:
            i = abs(i + (1 << bits))
        return i

    def _str_to_int(self, string, bits=8):
        i = 0
        if len(string) < 3:
            i = int(string, 10)
        elif string[0:2] == '0b' or string[0:3] == '-0b':
            i = int(string, 2)
        elif string[0:2] == '0o' or string[0:3] == '-0o':
            i = int(string, 8)
        elif string[0:2] == '0x' or string[0:3] == '-0x':
            i = int(string, 16)
        else:
            i = int(string)

        return self._get_complement(i, bits)


class RInstruction(Instruction):
    def __init__(self):
        Instruction.__init__(self)

        self.instruction = 0xc000

    def compile(self, assembler):
        return self.instruction


class R1Instruction(RInstruction):
    def __init__(self, args):
        RInstruction.__init__(self)

        self.rd = self._get_register(args[0])
        self.rs = self._get_register(args[1])

        self.instruction += (self.rd << 8)
        self.instruction += (self.rs << 11)


class R2Instruction(RInstruction):
    def __init__(self, args):
        RInstruction.__init__(self)

        self.rd = self._get_register(args[0])
        self.d = int(args[1])

        self.instruction += (self.rd << 8)
        self.instruction += self.d


class LSInstruction(Instruction):
    def __init__(self, args):
        Instruction.__init__(self)

        self.ra = self._get_register(args[0])
        self.d = self._str_to_int(args[0], bits=8)
        self.rb = self._get_register(args[2])

        self.instruction = 0x0000
        self.instruction += self.d
        self.instruction += (self.rb << 8)
        self.instruction += (self.ra << 11)

    def compile(self, assembler):
        return self.instruction


class IBInstruction(Instruction):
    def __init__(self):
        Instruction.__init__(self)
        self.instruction = 0x8000


class BInstruction(Instruction):
    def __init__(self, args):
        Instruction.__init__(self)

{'fraction_non_alphanumeric': 0.128085029724374, 'fraction_numerical': 0.030805260313457036, 'mean_word_length': 2.04052573932092, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 3, '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': '312379', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1740, 'n_words': 426}