Example #1
0
    def set_HX(self, value):
        """
            Sets the value of the H and X registers as a single 16-bit unit.
        """

        #split the value into a MSB and LSB, and assign those to H and X respectively
        self.H, self.X = split_word(value)
Example #2
0
    def set_HX(self, value):
        """
            Sets the value of the H and X registers as a single 16-bit unit.
        """

        #split the value into a MSB and LSB, and assign those to H and X respectively
        self.H, self.X = split_word(value)
Example #3
0
def shorten_passage(df, max_len=500):

    sys.setrecursionlimit(1000000)
    rouge = Rouge(metrics=['rouge-l'])

    passages = df['passage'].values
    querys = df['query'].values
    p_tmp = []
    cc = 0
    for p, q in zip(passages, querys):
        p_list = utils.split_word(p)
        if len(p_list) <= max_len:
            p_tmp.append(p)
        else:
            cc += 1
            chuck_num = len(p_list) // max_len
            scores = []
            for i in range(chuck_num + 1):
                pp = ''.join(p_list[i * max_len:(i + 1) * max_len])
                score = rouge.get_scores(pp, q, avg=True)['rouge-l']['r']
                scores.append(score)
            index = np.argmax(scores)
            pp = ''.join(p_list[index * max_len:(index + 1) * max_len])
            p_tmp.append(pp)
    df['passage'] = p_tmp
    print('shorten passage num: %d/%d' % (cc, len(df)))

    return df
Example #4
0
def build_vocab_embedding(list_df, vocab_path, embedding_in_zh,
                          embedding_in_en, embedding_out):
    data = []
    for df in list_df:
        if 'answer' in df:
            data = data + df[['query', 'passage', 'answer'
                              ]].values.flatten().tolist()
        else:
            data = data + df[['query', 'passage']].values.flatten().tolist()

    vocab = set()
    for d in data:
        d_list = utils.split_word(d)
        for dd in d_list:
            vocab.add(dd)
    print('data, word_nums:%d' % len(vocab))

    # zh
    try:
        model_zh = gensim.models.KeyedVectors.load_word2vec_format(
            embedding_in_zh)
    except Exception as e:
        model_zh = gensim.models.KeyedVectors.load_word2vec_format(
            embedding_in_zh, binary=True, unicode_errors='ignore')

    tmp = set()
    for word in vocab:
        if word in model_zh:
            tmp.add(word)
    print('word_nums in pre-embedding:%d/%d, ratio:%.4f' %
          (len(tmp), len(vocab), len(tmp) / len(vocab)))

    # w2i = {'<pad>': 0, '<unk>': 1, ' ': 2}
    # i2w = {0: '<pad>', 1: '<unk>', 2: ' '}
    w2i = {'<pad>': 0}
    i2w = {0: '<pad>'}
    c = 1
    embedding = np.zeros([len(tmp) + 3, model_zh.vector_size])
    for word in tmp:
        w2i[word] = c
        i2w[c] = word
        if word in model_zh:
            embedding[c] = model_zh[word]
        c += 1
    w2i['<unk>'] = len(tmp) + 1
    i2w[len(tmp) + 1] = '<unk>'
    w2i[' '] = len(tmp) + 2
    i2w[len(tmp) + 2] = ' '
    lang = {'w2i': w2i, 'i2w': i2w}
    print('vacab length: %d' % (c + 2))
    print('embedding size:', embedding.shape)

    # save
    with open(vocab_path, 'wb') as file:
        pickle.dump(lang, file)
    np.save(embedding_out, embedding)
Example #5
0
    def push_word(self, word):
        """
            Pushes a given word onto the stack.
        """

        #split the word into two bytes
        msb, lsb = split_word(word)

        #then, push those bytes- pushing the LSB first, so the bytes remain in big endian order
        self.push_byte(lsb)
        self.push_byte(msb)
Example #6
0
    def set_ram_word(self, addr, word):
        """
            Sets a word in RAM, with checks. Requires ram to be initialized.
        """

        #split the word into two seperate bytes
        msb, lsb = split_word(word)

        #and write each of the two bytes, in big endian order:
        self.set_ram_byte(addr, msb)
        self.set_ram_byte(addr + 1, lsb)
Example #7
0
    def push_word(self, word):
        """
            Pushes a given word onto the stack.
        """

        #split the word into two bytes
        msb, lsb = split_word(word)

        #then, push those bytes- pushing the LSB first, so the bytes remain in big endian order
        self.push_byte(lsb)
        self.push_byte(msb)
Example #8
0
    def set_ram_word(self, addr, word):
        """
            Sets a word in RAM, with checks. Requires ram to be initialized.
        """

        #split the word into two seperate bytes
        msb, lsb = split_word(word)

        #and write each of the two bytes, in big endian order:
        self.set_ram_byte(addr, msb)
        self.set_ram_byte(addr + 1, lsb)
Example #9
0
def gen_tag_index(df):
    df = df[['query', 'passage']]
    data = df.values.flatten().tolist()
    tag2i = {'<pad>': 0, '<unk>': 1}
    cc = 2
    for d in data:
        _, tags = utils.split_word(d, have_tag=True)

        for t in tags:
            if t not in tag2i:
                tag2i[t] = cc
                cc += 1

    with open(config.tag_path, 'wb') as file:
        pickle.dump(tag2i, file)
    print('word flag num:%d' % len(tag2i))  # 98个
Example #10
0
def convert_sub_words_to_vec(inputs):
    vec = []
    for word in inputs:
        vec.append(convert_words_to_vec(ut.split_word(word)))
    return vec
Example #11
0
pd.set_option('display.max_columns', None)
warnings.filterwarnings("ignore")

if not os.path.exists(train_process_file):
    print('开始预处理数据......')
    df_train = pd.read_csv(train_data, sep="###__###", header=None, encoding='utf-8', nrows=train_num)
    df_train.columns = ['ID', 'Age', 'Gender', 'Education', 'Query_List']

    # 数据过滤
    df_train = df_train[(df_train.Age.values != 0) & (df_train.Gender.values != 0) &
                        (df_train.Education.values != 0)]

    # 分词处理
    df_train['Query_List'] = df_train['Query_List'].apply(
        lambda x: utils.split_word(x, stopwords_file))
    # print(df_train.head())

    df_test = pd.read_csv(test_data, sep="###__###", header=None, encoding='utf-8', nrows=test_num)
    df_test.columns = ['ID', 'Query_List']
    # print(df_test.shape)

    # 分词处理
    df_test['Query_List'] = df_test['Query_List'].apply(
        lambda x: utils.split_word(x, stopwords_file))
    # print(df_test.head())

    # 写出数据
    df_train.to_csv(processed_data + 'train_process.csv', index=False)
    df_test.to_csv(processed_data + 'test_process.csv', index=False)
Example #12
0
    def simulation_step(self):
        """execute the python simulation by one step"""

        l = self.get_line()
        f = self.get_file()
        if f in self.breakpoints:
            if l in self.breakpoints[f]:
                raise BreakSim

        instruction = self.instructions[self.program_counter]
        current_stack = self.registers.get(register_map.tos, 0)
        self.max_stack = max([current_stack, self.max_stack])

        if self.profile:
            trace = instruction.get("trace", "-")
            lines = self.files.get(trace.filename, {})
            lines[trace.lineno] = lines.get(trace.lineno, 0) + 1
            self.files[trace.filename] = lines

        if "literal" in instruction:
            literal = instruction["literal"]

        if "label" in instruction:
            literal = instruction["label"]

        # read operands
        #
        a = instruction.get("a", 0)
        b = instruction.get("b", 0)
        z = instruction.get("z", 0)

        operand_b = self.registers.get(b, 0)
        operand_a = self.registers.get(a, 0)

        this_instruction = self.program_counter
        self.program_counter += 1
        wait = False
        result = None

        if instruction["op"] == "stop":
            self.program_counter = this_instruction
            wait = True
            for file_ in self.input_files.values():
                file_.close()
            for file_ in self.output_files.values():
                file_.close()
            raise StopSim

        elif instruction["op"] == "literal":
            if literal & 0x8000:
                result = 0xffff0000 | literal
            else:
                result = literal
        elif instruction["op"] == "addl":
            result = literal + operand_a
        elif instruction["op"] == "literal_hi":
            result = operand_a & 0x0000ffff | ((literal & 0xffff) << 16)
        elif instruction["op"] == "store":
            self.memory[operand_a] = operand_b
        elif instruction["op"] == "load":
            result = self.memory.get(operand_a, 0)
        elif instruction["op"] == "call":
            result = this_instruction + 1
            self.program_counter = literal
        elif instruction["op"] == "return":
            self.program_counter = operand_a
        elif instruction["op"] == "a_lo":
            result = self.a_lo
            self.a_lo = operand_a
        elif instruction["op"] == "b_lo":
            result = self.b_lo
            self.b_lo = operand_a
        elif instruction["op"] == "a_hi":
            result = self.a_hi
            self.a_hi = operand_a
        elif instruction["op"] == "b_hi":
            result = self.b_hi
            self.b_hi = operand_a
        elif instruction["op"] == "not":
            result = uint32(~operand_a)
        elif instruction["op"] == "int_to_long":
            if operand_a & 0x80000000:
                result = uint32(0xffffffff)
            else:
                result = uint32(0x00000000)
        elif instruction["op"] == "int_to_float":
            f = float(int32(self.a_lo))
            self.a_lo = float_to_bits(f)
        elif instruction["op"] == "float_to_int":
            i = bits_to_float(self.a_lo)
            if math.isnan(i):
                self.a_lo = int32(0)
            else:
                self.a_lo = int32(i)
        elif instruction["op"] == "long_to_double":
            double = float(int64(join_words(self.a_hi, self.a_lo)))
            if math.isnan(double):
                self.a_hi, self.a_lo = split_word(0, 0)
            else:
                self.a_hi, self.a_lo = split_word(double_to_bits(double))
        elif instruction["op"] == "double_to_long":
            bits = int(bits_to_double(join_words(self.a_hi, self.a_lo)))
            self.a_hi, self.a_lo = split_word(bits)
        elif instruction["op"] == "float_to_double":
            f = bits_to_float(self.a_lo)
            bits = double_to_bits(f)
            self.a_hi, self.a_lo = split_word(bits)
        elif instruction["op"] == "double_to_float":
            f = bits_to_double(join_words(self.a_hi, self.a_lo))
            self.a_lo = float_to_bits(f)
        elif instruction["op"] == "add":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + int(uint32(b))
            self.carry, result = split_word(lw)
            self.carry &= 1
        elif instruction["op"] == "add_with_carry":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + int(uint32(b)) + self.carry
            self.carry, result = split_word(lw)
            self.carry &= 1
        elif instruction["op"] == "subtract":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + ~int(uint32(b)) + 1
            self.carry, result = split_word(lw)
            self.carry &= 1
            self.carry ^= 1
        elif instruction["op"] == "subtract_with_carry":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + ~int(uint32(b)) + self.carry
            self.carry, result = split_word(lw)
            self.carry &= 1
            self.carry ^= 1
        elif instruction["op"] == "multiply":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) * int(uint32(b))
            self.carry, result = split_word(lw)
        elif instruction["op"] == "carry":
            a = operand_a
            b = operand_b
            result = uint32(self.carry)
        elif instruction["op"] == "or":
            a = operand_a
            b = operand_b
            result = uint32(a | b)
        elif instruction["op"] == "and":
            a = operand_a
            b = operand_b
            result = uint32(a & b)
        elif instruction["op"] == "xor":
            a = operand_a
            b = operand_b
            result = uint32(a ^ b)
        elif instruction["op"] == "shift_left":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) >> (32 - uint32(b))
            if b == 32:
                result = uint32(0)
            else:
                result = uint32(a) << uint32(b)
        elif instruction["op"] == "shift_left_with_carry":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            carry_in = self.carry
            self.carry = uint32(a) >> (32 - uint32(b))
            if b == 32:
                result = uint32(0) | carry_in
            else:
                result = uint32(a) << uint32(b) | carry_in
        elif instruction["op"] == "shift_right":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) << (32 - uint32(b))
            result = uint32(int32(a) >> uint32(b))
        elif instruction["op"] == "unsigned_shift_right":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) << (32 - uint32(b))
            if b == 32:
                result = uint32(0)
            else:
                result = uint32(a) >> uint32(b)
        elif instruction["op"] == "shift_right_with_carry":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            carry_in = self.carry
            self.carry = uint32(a) << (32 - uint32(b))
            if b == 32:
                result = uint32(0) | carry_in
            else:
                result = uint32(a) >> uint32(b) | carry_in
        elif instruction["op"] == "greater":
            a = operand_a
            b = operand_b
            result = int32(int32(a) > int32(b))
        elif instruction["op"] == "greater_equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) >= int32(b))
        elif instruction["op"] == "unsigned_greater":
            a = operand_a
            b = operand_b
            result = int32(uint32(a) > uint32(b))
        elif instruction["op"] == "unsigned_greater_equal":
            a = operand_a
            b = operand_b
            result = int32(uint32(a) >= uint32(b))
        elif instruction["op"] == "equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) == int32(b))
        elif instruction["op"] == "not_equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) != int32(b))
        elif instruction["op"] == "jmp_if_false":
            if operand_a == 0:
                self.program_counter = literal
        elif instruction["op"] == "jmp_if_true":
            if operand_a != 0:
                self.program_counter = literal
        elif instruction["op"] == "goto":
            self.program_counter = literal
        elif instruction["op"] == "file_read":
            value = self.input_files[instruction["filename"]].getline()
            result = uint32(value)
        elif instruction["op"] == "float_file_write":
            self.output_files[instruction["file_name"]].write(
                "%.7f\n" %
                bits_to_float(operand_a))
        elif instruction["op"] == "unsigned_file_write":
            self.output_files[instruction["file_name"]].write(
                "%i\n" %
                uint32(operand_a))
        elif instruction["op"] == "file_write":
            self.output_files[instruction["file_name"]].write(
                "%i\n" %
                int32(operand_a))
        elif instruction["op"] == "read":
            if operand_a not in self.inputs:
                result = 0
            else:
                input_ = self.inputs[operand_a]
                if input_.src_rdy and input_.dst_rdy:
                    result = input_.q
                    input_.next_dst_rdy = False
                else:
                    input_.next_dst_rdy = True
                    wait = True
        elif instruction["op"] == "ready":
            if operand_a not in self.inputs:
                operand_a = 0
            else:
                input_ = self.inputs[operand_a]
                if input_.src_rdy:
                    result = uint32(1)
                else:
                    result = uint32(0)
        elif instruction["op"] == "output_ready":
            if operand_a not in self.outputs:
                operand_a = 0
            else:
                output_ = self.outputs[operand_a]
                if output_.dst_rdy:
                    result = uint32(1)
                else:
                    result = uint32(0)
        elif instruction["op"] == "write":
            if operand_a not in self.outputs:
                pass
            else:
                output_ = self.outputs[operand_a]
                if output_.src_rdy and output_.dst_rdy:
                    output_.next_src_rdy = False
                else:
                    output_.q = operand_b
                    output_.next_src_rdy = True
                    wait = True
        elif instruction["op"] == "float_add":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ + floatb)
        elif instruction["op"] == "float_subtract":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ - floatb)
        elif instruction["op"] == "float_multiply":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ * floatb)
        elif instruction["op"] == "float_divide":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            try:
                result = float_to_bits(float_ / floatb)
            except ZeroDivisionError:
                result = float_to_bits(float("nan"))
        elif instruction["op"] == "long_float_add":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(
                double_to_bits(double + doubleb)
            )
        elif instruction["op"] == "long_float_subtract":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(
                double_to_bits(double - doubleb)
            )
        elif instruction["op"] == "long_float_multiply":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(
                double_to_bits(double * doubleb)
            )
        elif instruction["op"] == "long_float_divide":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            try:
                self.a_hi, self.a_lo = split_word(
                    double_to_bits(double / doubleb))
            except ZeroDivisionError:
                self.a_hi, self.a_lo = split_word(
                    double_to_bits(float("nan")))
        elif instruction["op"] == "long_float_file_write":
            long_word = join_words(self.a_hi, self.a_lo)
            self.output_files[instruction["file_name"]].write(
                "%.16f\n" %
                bits_to_double(long_word))
        elif instruction["op"] == "long_file_write":
            long_word = join_words(self.a_hi, self.a_lo)
            self.output_files[instruction["file_name"]].write(
                "%f\n" %
                long_word)
        elif instruction["op"] == "assert":
            if operand_a == 0:
                raise ChipsAssertionFail(
                    instruction["file"],
                    instruction["line"])
        elif instruction["op"] == "report":
            print "%d (report (int) at line: %s in file: %s)" % (
                int32(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_report":
            print "%d (report (long) at line: %s in file: %s)" % (
                int64(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "float_report":
            print "%f (report (float) at line: %s in file: %s)" % (
                bits_to_float(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_float_report":
            print "%s (report (double) at line: %s in file: %s)" % (
                bits_to_double(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "unsigned_report":
            print "%d (report (unsigned) at line: %s in file: %s)" % (
                uint32(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_unsigned_report":
            print "%d (report (unsigned long) at line: %s in file: %s)" % (
                uint64(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "wait_clocks":
            if self.timer == operand_a:
                wait = False
                self.timer = 0
            else:
                wait = True
                self.timer += 1

        else:
            print "Unknown machine instruction", instruction["op"]
            sys.exit(-1)

        # Write data back
        if result is not None:
            self.registers[z] = result

        # manipulate stack pointer
        if wait:
            self.program_counter = this_instruction
Example #13
0
def jieduan(df):
    passages = df['passage'].values
    querys = df['query'].values
    a_item = df['a_item'].values
    b_item = df['b_item'].values
    c_item = df['c_item'].values

    # cut p
    flag_p = []
    for p in passages:
        p_list = utils.split_word(p.strip())
        if len(p_list) > 500:
            flag_p.append(False)
        else:
            flag_p.append(True)

    # cut q
    flag_q = []
    for q in querys:
        q_list = utils.split_word(q.strip())
        if len(q_list) > 30:
            flag_q.append(False)
        else:
            flag_q.append(True)

    # cut a_item
    flag_a = []
    for a in a_item:
        a_list = utils.split_word(a.strip())
        if len(a_list) > 5:
            flag_a.append(False)
        else:
            flag_a.append(True)

    # cut b_item
    flag_b = []
    for b in b_item:
        b_list = utils.split_word(b.strip())
        if len(b_list) > 5:
            flag_b.append(False)
        else:
            flag_b.append(True)

    # cut c_item
    flag_c = []
    for c in c_item:
        c_list = utils.split_word(c.strip())
        if len(c_list) > 5:
            flag_c.append(False)
        else:
            flag_c.append(True)

    assert len(flag_p) == len(flag_q) == len(flag_a) == len(flag_b) == len(
        flag_c)

    flag = []
    for fp, fq, fa, fb, fc in zip(flag_p, flag_q, flag_a, flag_b, flag_c):
        if fp and fq and fa and fb and fc:
            flag.append(True)
        else:
            flag.append(False)
    print('训练/验证集, 长度截断,保留数据_num:%d/%d, ratio:%.4f' %
          (sum(flag), len(flag), sum(flag) / len(flag)))

    df['jieduan_flag'] = flag

    return df
Example #14
0
def load_data(df_file, vocab_path, q_max_len=30, p_max_len=500, a_max_len=5):
    """
    load data from .csv
    # 1. load
    # 2. index, tag(词性), 是否在答案中出现, 是否是标题
    # 3. padding
    return: content, question, zhengli, fuli, wfqd, answer
    """

    # load
    df = pd.read_csv(df_file)
    querys = df['query'].values.tolist()
    passages = df['passage'].values.tolist()
    zhenglis = df['zhengli'].values.tolist()
    fulis = df['fuli'].values.tolist()
    wfqds = df['wfqd'].values.tolist()
    wfqd_list = wfqd.wfqd_list

    if 'answer' in df:
        answers = df['answer'].values.tolist()
        answers_tmp = []
        for answer, zhengli, fuli in zip(answers, zhenglis, fulis):
            if answer.strip() == zhengli:
                answers_tmp.append(0)
            elif answer.strip() == fuli:
                answers_tmp.append(1)
            elif answer.strip() in wfqd_list:
                answers_tmp.append(2)
            else:
                print(
                    'load_data, meet wrong data, answer:%s, zhengli:%s, fuli:%s'
                    % (answer, zhengli, fuli))

    # words
    p_index = [utils.split_word(pp) for pp in passages]
    q_index = [utils.split_word(qq) for qq in querys]
    zhengli_index = [utils.split_word(zhengli) for zhengli in zhenglis]
    fuli_index = [utils.split_word(fuli) for fuli in fulis]
    wfqd_index = [utils.split_word(w) for w in wfqds]

    # words -> index
    q_index = utils.words2index(q_index, vocab_path)
    p_index = utils.words2index(p_index, vocab_path)
    zhengli_index = utils.words2index(zhengli_index, vocab_path)
    fuli_index = utils.words2index(fuli_index, vocab_path)
    wfqd_index = utils.words2index(wfqd_index, vocab_path)

    # padding
    q_index = utils.pad(q_index, q_max_len)
    p_index = utils.pad(p_index, p_max_len)
    zhengli_index = utils.pad(zhengli_index, a_max_len)
    fuli_index = utils.pad(fuli_index, a_max_len)
    wfqd_index = utils.pad(wfqd_index, a_max_len)

    if 'answer' in df:
        return [
            p_index, q_index, zhengli_index, fuli_index, wfqd_index,
            answers_tmp
        ]
    else:
        return [p_index, q_index, zhengli_index, fuli_index.wfqd_index]
Example #15
0
    def simulation_step(self):
        """execute the python simulation by one step"""

        l = self.get_line()
        f = self.get_file()
        if f in self.breakpoints:
            if l in self.breakpoints[f]:
                raise BreakSim

        instruction = self.instructions[self.program_counter]
        current_stack = self.registers.get(register_map.tos, 0)
        self.max_stack = max([current_stack, self.max_stack])

        if self.profile:
            trace = instruction.get("trace", "-")
            lines = self.files.get(trace.filename, {})
            lines[trace.lineno] = lines.get(trace.lineno, 0) + 1
            self.files[trace.filename] = lines

        if "literal" in instruction:
            literal = instruction["literal"]

        if "label" in instruction:
            literal = instruction["label"]

        # read operands
        #
        a = instruction.get("a", 0)
        b = instruction.get("b", 0)
        z = instruction.get("z", 0)

        operand_b = self.registers.get(b, 0)
        operand_a = self.registers.get(a, 0)

        this_instruction = self.program_counter
        self.program_counter += 1
        wait = False
        result = None

        if instruction["op"] == "stop":
            self.program_counter = this_instruction
            wait = True
            for file_ in self.input_files.values():
                file_.close()
            for file_ in self.output_files.values():
                file_.close()
            raise StopSim

        elif instruction["op"] == "literal":
            if literal & 0x8000:
                result = 0xffff0000 | literal
            else:
                result = literal
        elif instruction["op"] == "addl":
            result = literal + operand_a
        elif instruction["op"] == "literal_hi":
            result = operand_a & 0x0000ffff | ((literal & 0xffff) << 16)
        elif instruction["op"] == "store":
            self.memory[operand_a] = operand_b
        elif instruction["op"] == "load":
            result = self.memory.get(operand_a, 0)
        elif instruction["op"] == "call":
            result = this_instruction + 1
            self.program_counter = literal
        elif instruction["op"] == "return":
            self.program_counter = operand_a
        elif instruction["op"] == "a_lo":
            result = self.a_lo
            self.a_lo = operand_a
        elif instruction["op"] == "b_lo":
            result = self.b_lo
            self.b_lo = operand_a
        elif instruction["op"] == "a_hi":
            result = self.a_hi
            self.a_hi = operand_a
        elif instruction["op"] == "b_hi":
            result = self.b_hi
            self.b_hi = operand_a
        elif instruction["op"] == "not":
            result = uint32(~operand_a)
        elif instruction["op"] == "int_to_long":
            if operand_a & 0x80000000:
                result = uint32(0xffffffff)
            else:
                result = uint32(0x00000000)
        elif instruction["op"] == "int_to_float":
            f = float(int32(self.a_lo))
            self.a_lo = float_to_bits(f)
        elif instruction["op"] == "float_to_int":
            i = bits_to_float(self.a_lo)
            if math.isnan(i):
                self.a_lo = int32(0)
            else:
                self.a_lo = int32(i)
        elif instruction["op"] == "long_to_double":
            double = float(int64(join_words(self.a_hi, self.a_lo)))
            if math.isnan(double):
                self.a_hi, self.a_lo = split_word(0, 0)
            else:
                self.a_hi, self.a_lo = split_word(double_to_bits(double))
        elif instruction["op"] == "double_to_long":
            bits = int(bits_to_double(join_words(self.a_hi, self.a_lo)))
            self.a_hi, self.a_lo = split_word(bits)
        elif instruction["op"] == "float_to_double":
            f = bits_to_float(self.a_lo)
            bits = double_to_bits(f)
            self.a_hi, self.a_lo = split_word(bits)
        elif instruction["op"] == "double_to_float":
            f = bits_to_double(join_words(self.a_hi, self.a_lo))
            self.a_lo = float_to_bits(f)
        elif instruction["op"] == "add":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + int(uint32(b))
            self.carry, result = split_word(lw)
            self.carry &= 1
        elif instruction["op"] == "add_with_carry":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + int(uint32(b)) + self.carry
            self.carry, result = split_word(lw)
            self.carry &= 1
        elif instruction["op"] == "subtract":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + ~int(uint32(b)) + 1
            self.carry, result = split_word(lw)
            self.carry &= 1
            self.carry ^= 1
        elif instruction["op"] == "subtract_with_carry":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) + ~int(uint32(b)) + self.carry
            self.carry, result = split_word(lw)
            self.carry &= 1
            self.carry ^= 1
        elif instruction["op"] == "multiply":
            a = operand_a
            b = operand_b
            lw = int(uint32(a)) * int(uint32(b))
            self.carry, result = split_word(lw)
        elif instruction["op"] == "carry":
            a = operand_a
            b = operand_b
            result = uint32(self.carry)
        elif instruction["op"] == "or":
            a = operand_a
            b = operand_b
            result = uint32(a | b)
        elif instruction["op"] == "and":
            a = operand_a
            b = operand_b
            result = uint32(a & b)
        elif instruction["op"] == "xor":
            a = operand_a
            b = operand_b
            result = uint32(a ^ b)
        elif instruction["op"] == "shift_left":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) >> (32 - uint32(b))
            if b == 32:
                result = uint32(0)
            else:
                result = uint32(a) << uint32(b)
        elif instruction["op"] == "shift_left_with_carry":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            carry_in = self.carry
            self.carry = uint32(a) >> (32 - uint32(b))
            if b == 32:
                result = uint32(0) | carry_in
            else:
                result = uint32(a) << uint32(b) | carry_in
        elif instruction["op"] == "shift_right":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) << (32 - uint32(b))
            result = uint32(int32(a) >> uint32(b))
        elif instruction["op"] == "unsigned_shift_right":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            self.carry = uint32(a) << (32 - uint32(b))
            if b == 32:
                result = uint32(0)
            else:
                result = uint32(a) >> uint32(b)
        elif instruction["op"] == "shift_right_with_carry":
            a = operand_a
            b = int(operand_b)
            b = b if b <= 32 else 32
            carry_in = self.carry
            self.carry = uint32(a) << (32 - uint32(b))
            if b == 32:
                result = uint32(0) | carry_in
            else:
                result = uint32(a) >> uint32(b) | carry_in
        elif instruction["op"] == "greater":
            a = operand_a
            b = operand_b
            result = int32(int32(a) > int32(b))
        elif instruction["op"] == "greater_equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) >= int32(b))
        elif instruction["op"] == "unsigned_greater":
            a = operand_a
            b = operand_b
            result = int32(uint32(a) > uint32(b))
        elif instruction["op"] == "unsigned_greater_equal":
            a = operand_a
            b = operand_b
            result = int32(uint32(a) >= uint32(b))
        elif instruction["op"] == "equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) == int32(b))
        elif instruction["op"] == "not_equal":
            a = operand_a
            b = operand_b
            result = int32(int32(a) != int32(b))
        elif instruction["op"] == "jmp_if_false":
            if operand_a == 0:
                self.program_counter = literal
        elif instruction["op"] == "jmp_if_true":
            if operand_a != 0:
                self.program_counter = literal
        elif instruction["op"] == "goto":
            self.program_counter = literal
        elif instruction["op"] == "file_read":
            value = self.input_files[instruction["filename"]].getline()
            result = uint32(value)
        elif instruction["op"] == "float_file_write":
            self.output_files[instruction["file_name"]].write(
                "%.7f\n" % bits_to_float(operand_a))
        elif instruction["op"] == "unsigned_file_write":
            self.output_files[instruction["file_name"]].write(
                "%i\n" % uint32(operand_a))
        elif instruction["op"] == "file_write":
            self.output_files[instruction["file_name"]].write("%i\n" %
                                                              int32(operand_a))
        elif instruction["op"] == "read":
            if operand_a not in self.inputs:
                result = 0
            else:
                input_ = self.inputs[operand_a]
                if input_.src_rdy and input_.dst_rdy:
                    result = input_.q
                    input_.next_dst_rdy = False
                else:
                    input_.next_dst_rdy = True
                    wait = True
        elif instruction["op"] == "ready":
            if operand_a not in self.inputs:
                operand_a = 0
            else:
                input_ = self.inputs[operand_a]
                if input_.src_rdy:
                    result = uint32(1)
                else:
                    result = uint32(0)
        elif instruction["op"] == "output_ready":
            if operand_a not in self.outputs:
                operand_a = 0
            else:
                output_ = self.outputs[operand_a]
                if output_.dst_rdy:
                    result = uint32(1)
                else:
                    result = uint32(0)
        elif instruction["op"] == "write":
            if operand_a not in self.outputs:
                pass
            else:
                output_ = self.outputs[operand_a]
                if output_.src_rdy and output_.dst_rdy:
                    output_.next_src_rdy = False
                else:
                    output_.q = operand_b
                    output_.next_src_rdy = True
                    wait = True
        elif instruction["op"] == "float_add":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ + floatb)
        elif instruction["op"] == "float_subtract":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ - floatb)
        elif instruction["op"] == "float_multiply":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            result = float_to_bits(float_ * floatb)
        elif instruction["op"] == "float_divide":
            a = operand_a
            b = operand_b
            float_ = bits_to_float(a)
            floatb = bits_to_float(b)
            try:
                result = float_to_bits(float_ / floatb)
            except ZeroDivisionError:
                result = float_to_bits(float("nan"))
        elif instruction["op"] == "long_float_add":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(double_to_bits(double + doubleb))
        elif instruction["op"] == "long_float_subtract":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(double_to_bits(double - doubleb))
        elif instruction["op"] == "long_float_multiply":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            self.a_hi, self.a_lo = split_word(double_to_bits(double * doubleb))
        elif instruction["op"] == "long_float_divide":
            double = bits_to_double(join_words(self.a_hi, self.a_lo))
            doubleb = bits_to_double(join_words(self.b_hi, self.b_lo))
            try:
                self.a_hi, self.a_lo = split_word(
                    double_to_bits(double / doubleb))
            except ZeroDivisionError:
                self.a_hi, self.a_lo = split_word(double_to_bits(float("nan")))
        elif instruction["op"] == "long_float_file_write":
            long_word = join_words(self.a_hi, self.a_lo)
            self.output_files[instruction["file_name"]].write(
                "%.16f\n" % bits_to_double(long_word))
        elif instruction["op"] == "long_file_write":
            long_word = join_words(self.a_hi, self.a_lo)
            self.output_files[instruction["file_name"]].write("%f\n" %
                                                              long_word)
        elif instruction["op"] == "assert":
            if operand_a == 0:
                raise ChipsAssertionFail(instruction["file"],
                                         instruction["line"])
        elif instruction["op"] == "report":
            print "%d (report (int) at line: %s in file: %s)" % (
                int32(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_report":
            print "%d (report (long) at line: %s in file: %s)" % (
                int64(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "float_report":
            print "%f (report (float) at line: %s in file: %s)" % (
                bits_to_float(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_float_report":
            print "%s (report (double) at line: %s in file: %s)" % (
                bits_to_double(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "unsigned_report":
            print "%d (report (unsigned) at line: %s in file: %s)" % (
                uint32(self.a_lo),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "long_unsigned_report":
            print "%d (report (unsigned long) at line: %s in file: %s)" % (
                uint64(join_words(self.a_hi, self.a_lo)),
                instruction["line"],
                instruction["file"],
            )
        elif instruction["op"] == "wait_clocks":
            if self.timer == operand_a:
                wait = False
                self.timer = 0
            else:
                wait = True
                self.timer += 1

        else:
            print "Unknown machine instruction", instruction["op"]
            sys.exit(-1)

        # Write data back
        if result is not None:
            self.registers[z] = result

        # manipulate stack pointer
        if wait:
            self.program_counter = this_instruction