<commit_before>import sys
import os
import subprocess

from parser import parse, optimize
from parser import OUTPUT, INPUT, LOOP, ENDLOOP, MOVE
from parser import ADD, SET, MULCOPY, SCAN

BUFSIZE = 8192

def compile(code):
    tokens = parse(code)
    tokens = optimize(tokens)
    output = """.section .bss
    .lcomm mem, """ + str(BUFSIZE) + """
    .set startidx, mem + """ + str(int(BUFSIZE/2)) + """
.section .text
.global _start
_start:
    xor %r12, %r12
    movq $startidx, %rbx
"""
    loops = []
    loopnum = 0
    for token, value in tokens:
        if token == ADD:
            offset, value = value
            if offset == 0:
                dest = "%r12"
            else:
                dest = str(offset*8)+"(%rbx)"
            if value == 1 and dest == "%r12":
                output += "    inc " + dest + "\n"
            elif value >= 1:
                output += "    add $" + str(value) + ", " + dest + "\n"
            elif value == -1 and dest == "%r12":
                output += "    dec " + dest + "\n"
            elif value <= -1:
                output += "    sub $" + str(-value) + ", " + dest + "\n"
        elif token == MULCOPY:
            src, dest, mul = value
            if src == 0:
                src = "%r12"
            else:
                src = str(src*8)+"(%rbx)"
            if dest == 0:
                dest = "%r12"
            else:
                dest = str(dest*8)+"(%rbx)"

            if mul not in (-1, 1):
                output += "    movq " + src + ", %rax\n" \
                          "    movq $" + str(abs(mul)) + ", %rdx\n" \
                          "    mul %rdx\n"
                src = "%rax"
            if mul > 0:
                output += "    add " + src + ", " + dest + "\n"
            else:
                output += "    sub " + src + ", " + dest + "\n"
                
        elif token == SET:
                offset, value = value
                if offset == 0 and value == 0:
                    output += "    xor %r12, %r12\n"
                elif offset == 0:
                    output += "    movq $" + str(value) + ", %r12\n"
                else:
                    output += "    movq $" + str(value) + ", "+str(offset*8)+"(%rbx)\n"
        elif token == MOVE:
            if value:
                output += "    movq %r12, (%rbx)\n"
                if value > 0:
                    output += "    add $" + str(8*value) + ", %rbx\n"
                else:
                    output += "    sub $" + str(-8*value) + ", %rbx\n"
                output += "    movq (%rbx), %r12\n"
        elif token == LOOP:
            loopnum += 1
            loops.append(loopnum)
            output += "    cmp $0, %r12\n" \
                      "    jz endloop" + str(loopnum) + '\n' \
                      "    loop" + str(loopnum) + ":\n"
        elif token == ENDLOOP:
            loop = str(loops.pop())
            output += "    cmp $0, %r12\n" \
                      "    jnz loop" + loop + '\n' \
                      "    endloop" + loop + ':\n'
        elif token == SCAN:
            # Slighly more optimal than normal loop and move
            loopnum += 1

            output += "    cmp $0, %r12\n" \<commit_msg>Optimize zero compare (probably no major difference)<commit_after>import sys
import os
import subprocess

from parser import parse, optimize
from parser import OUTPUT, INPUT, LOOP, ENDLOOP, MOVE
from parser import ADD, SET, MULCOPY, SCAN

BUFSIZE = 8192

def compile(code):
    tokens = parse(code)
    tokens = optimize(tokens)
    output = """.section .bss
    .lcomm mem, """ + str(BUFSIZE) + """
    .set startidx, mem + """ + str(int(BUFSIZE/2)) + """
.section .text
.global _start
_start:
    xor %r12, %r12
    movq $startidx, %rbx
"""
    loops = []
    loopnum = 0
    for token, value in tokens:
        if token == ADD:
            offset, value = value
            if offset == 0:
                dest = "%r12"
            else:
                dest = str(offset*8)+"(%rbx)"
            if value == 1 and dest == "%r12":
                output += "    inc " + dest + "\n"
            elif value >= 1:
                output += "    add $" + str(value) + ", " + dest + "\n"
            elif value == -1 and dest == "%r12":
                output += "    dec " + dest + "\n"
            elif value <= -1:
                output += "    sub $" + str(-value) + ", " + dest + "\n"
        elif token == MULCOPY:
            src, dest, mul = value
            if src == 0:
                src = "%r12"
            else:
                src = str(src*8)+"(%rbx)"
            if dest == 0:
                dest = "%r12"
            else:
                dest = str(dest*8)+"(%rbx)"

            if mul not in (-1, 1):
                output += "    movq " + src + ", %rax\n" \
                          "    movq $" + str(abs(mul)) + ", %rdx\n" \
                          "    mul %rdx\n"
                src = "%rax"
            if mul > 0:
                output += "    add " + src + ", " + dest + "\n"
            else:
                output += "    sub " + src + ", " + dest + "\n"
                
        elif token == SET:
                offset, value = value
                if offset == 0 and value == 0:
                    output += "    xor %r12, %r12\n"
                elif offset == 0:
                    output += "    movq $" + str(value) + ", %r12\n"
                else:
                    output += "    movq $" + str(value) + ", "+str(offset*8)+"(%rbx)\n"
        elif token == MOVE:
            if value:
                output += "    movq %r12, (%rbx)\n"
                if value > 0:
                    output += "    add $" + str(8*value) + ", %rbx\n"
                else:
                    output += "    sub $" + str(-8*value) + ", %rbx\n"
                output += "    movq (%rbx), %r12\n"
        elif token == LOOP:
            loopnum += 1
            loops.append(loopnum)
            output += "    test %r12, %r12\n" \
                      "    jz endloop" + str(loopnum) + '\n' \
                      "    loop" + str(loopnum) + ":\n"
        elif token == ENDLOOP:
            loop = str(loops.pop())
            output += "    test %r12, %r12\n" \
                      "    jnz loop" + loop + '\n' \
                      "    endloop" + loop + ':\n'
        elif token == SCAN:
            # Slighly more optimal than normal loop and move
            loopnum += 1

            output += "    test %r12, %r12\n" \
{'fraction_non_alphanumeric': 0.15126309303758473, 'fraction_numerical': 0.019254467036352432, 'mean_word_length': 1.095191997418522, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '37857', 'n_tokens_mistral': 2107, 'n_tokens_neox': 1954, 'n_words': 546}