# Path: luz_asm_sim/lib/asmlib/asm_common_types.py

# 
# Path: luz_asm_sim/lib/commonlib/portability.py
# def is_int_type(obj):
#     if sys.hexversion > 0x03000000:
#         return isinstance(obj, int)
#     else:
#         return isinstance(obj, int) or isinstance(obj, long)
# 
# Path: luz_asm_sim/lib/asmlib/asmparser.py
# class ParseError(Exception): pass
# class AsmParser(object):
#     def __init__(self, yacctab='parsetab'):
#     def parse(self, text):
#     def _lex_error_func(self, msg):
#     def p_asm_file(self, p):
#     def p_asm_line_1(self, p):
#     def p_asm_line_2(self, p):
#     def p_empty_line(self, p):
#     def p_directive_1(self, p):
#     def p_directive_2(self, p):
#     def p_instruction_1(self, p):
#     def p_instruction_2(self, p):
#     def p_label_def(self, p):
#     def p_arguments_opt(self, p):
#     def p_arguments_1(self, p):
#     def p_arguments_2(self, p):
#     def p_argument_1(self, p):
#     def p_argument_2(self, p):
#     def p_argument_3(self, p):
#     def p_argument_4(self, p):
#     def p_argument_5(self, p):
#     def p_number(self, p):
#     def p_error(self, p):


import re
from .asm_common_types import ImportType, RelocType
from ..commonlib.utils import (
    extract_bitfield, num_fits_in_nbits, build_bitfield)
from ..commonlib.luz_opcodes import *
from ..commonlib.portability import is_int_type
from .asmparser import (
    AsmParser, Id, Number, String, MemRef)
        Expected: Id with id = $r0 .. $r31, or one of the aliases
        for the registers, defined in register_alias.

        Returns the numeric representation of the register.
        If an invalid input is given, InstructionError is
        raised.
    """
    if isinstance(arg, Id) and arg.id.startswith('$'):
        if arg.id in register_alias:
            return register_alias[arg.id]

        m = re.match('\$r(\d\d?)$', arg.id)
        if m and int(m.group(1)) <= 31:
            return int(m.group(1))
        else:
            raise InstructionError('Invalid register: %s' % arg.id)
    else:
        raise InstructionError('Invalid register: %s' % str(arg))


def _const(arg, maxbits=16):
    """ Constructor for numeric constants.
        Expected: a Number, that is expected to fit into maxbits
        bits. This number can either be signed or unsigned - this
        is to be controlled by the assembly programmer.

        Otherwise, InstructionError is raised.

        The number is returned as an integer.
    """

# Path: luz_asm_sim/lib/asmlib/asm_common_types.py

# 
# Path: luz_asm_sim/lib/commonlib/portability.py
# def is_int_type(obj):
#     if sys.hexversion > 0x03000000:
#         return isinstance(obj, int)
#     else:
#         return isinstance(obj, int) or isinstance(obj, long)
# 
# Path: luz_asm_sim/lib/asmlib/asmparser.py
# class ParseError(Exception): pass
# class AsmParser(object):
#     def __init__(self, yacctab='parsetab'):
#     def parse(self, text):
#     def _lex_error_func(self, msg):
#     def p_asm_file(self, p):
#     def p_asm_line_1(self, p):
#     def p_asm_line_2(self, p):
#     def p_empty_line(self, p):
#     def p_directive_1(self, p):
#     def p_directive_2(self, p):
#     def p_instruction_1(self, p):
#     def p_instruction_2(self, p):
#     def p_label_def(self, p):
#     def p_arguments_opt(self, p):
#     def p_arguments_1(self, p):
#     def p_arguments_2(self, p):
#     def p_argument_1(self, p):
#     def p_argument_2(self, p):
#     def p_argument_3(self, p):
#     def p_argument_4(self, p):
#     def p_argument_5(self, p):
#     def p_number(self, p):
#     def p_error(self, p):


# Path: luz_asm_sim/lib/asmlib/asm_instructions.py
import re
from .asm_common_types import ImportType, RelocType
from ..commonlib.utils import (
    extract_bitfield, num_fits_in_nbits, build_bitfield)
from ..commonlib.luz_opcodes import *
from ..commonlib.portability import is_int_type
from .asmparser import (
    AsmParser, Id, Number, String, MemRef)

        Expected: Id with id = $r0 .. $r31, or one of the aliases
        for the registers, defined in register_alias.

        Returns the numeric representation of the register.
        If an invalid input is given, InstructionError is
        raised.
    """
    if isinstance(arg, Id) and arg.id.startswith('$'):
        if arg.id in register_alias:
            return register_alias[arg.id]

        m = re.match('\$r(\d\d?)$', arg.id)
        if m and int(m.group(1)) <= 31:
            return int(m.group(1))
        else:
            raise InstructionError('Invalid register: %s' % arg.id)
    else:
        raise InstructionError('Invalid register: %s' % str(arg))


def _const(arg, maxbits=16):
    """ Constructor for numeric constants.
        Expected: a Number, that is expected to fit into maxbits
        bits. This number can either be signed or unsigned - this
        is to be controlled by the assembly programmer.

        Otherwise, InstructionError is raised.

        The number is returned as an integer.
    """

    if not (isinstance(arg, Number) and is_int_type(arg.val)):