<commit_before># -*- coding: utf-8 -*-

"""
First stage in numba translation. Translates Python bytecode to untyped
pykit IR.

Initially adapted from numbapro/npm/symbolic.py by <NAME>.
"""

from __future__ import print_function, division, absolute_import

import __builtin__
import inspect
import dis
import pprint
import operator
import collections
from collections import namedtuple

from numba2.errors import error_context, CompileError, EmptyStackError
from numba2.runtime.obj import tupleobject
from .bytecode import ByteCode

from pykit.ir import Function, Builder, Op, Const, Value, ops
from pykit import types

#===------------------------------------------------------------------===
# Translation
#===------------------------------------------------------------------===

COMPARE_OP_FUNC = {
    '>': operator.gt,
    '<': operator.lt,
    '>=': operator.ge,
    '<=': operator.le,
    '==': operator.eq,
    '!=': operator.ne,
    'in': operator.contains,
    'is': operator.is_,
    'exception match': isinstance,
}

const = lambda val: Const(val, types.Opaque)

class Translate(object):
    """
    Translate bytecode to untypes pykit IR.
    """

    def __init__(self, func):
        self.func = func
        self.bytecode = ByteCode(func)

        # -------------------------------------------------
        # Find predecessors

        self.blocks = {}            # offset -> Block
        self.block2offset = {}      # Block -> offset
        self.allocas = {}           # varname -> alloca
        self.stacks = {}            # Block -> value stack
        self.exc_handlers = set()   # { Block }

        # -------------------------------------------------
        # Block stacks

        self.block_stack   = []
        self.loop_stack    = []
        self.except_stack  = []
        self.finally_stack = []

        # -------------------------------------------------
        # CFG

        self.predecessors = collections.defaultdict(set)
        self.phis = collections.defaultdict(list)

        # -------------------------------------------------
        # Variables and scoping

        self.code = self.bytecode.code
        self.varnames = self.bytecode.code.co_varnames
        self.consts = self.bytecode.code.co_consts
        self.names = self.bytecode.code.co_names
        self.argnames = self.varnames[:self.bytecode.code.co_argcount]

        self.globals = dict(vars(__builtin__))
        self.builtins = set(self.globals.values())
        self.globals.update(self.func.func_globals)

        # -------------------------------------------------
        # Error checks

        argspec = inspect.getargspec(self.func)
        assert not argspec.varargs, "does not support varargs"
        assert not argspec.keywords, "does not support keywords"

    def initialize(self):
        """Initialize pykit untypes structures"""

        # Setup Function
        sig = types.Function(types.Opaque, [types.Opaque] * len(self.argnames),
                             False)
        self.dst = Function(func_name(self.func), self.argnames, sig)

        # Setup Builder
        self.builder = Builder(self.dst)

        # Setup Blocks
        for offset in self.bytecode.labels:
            block = self.dst.new_block("Block%d" % offset)
            self.blocks[offset] = block
            self.stacks[block] = []

        # Setup Variables
        self.builder.position_at_beginning(self.dst.startblock)
        for varname in self.varnames:
            stackvar = self.builder.alloca(types.Pointer(types.Opaque),
                                           result=varname)
            self.allocas[varname] = stackvar

            # Initialize function arguments
            if varname in self.argnames:
                self.builder.store(self.dst.get_arg(varname), stackvar)

    def interpret(self):
        self.curblock = self.dst.startblock

        for inst in self.bytecode:
            if inst.offset in self.blocks:
                # Block switch<commit_msg>Use temporary name to assign argument to temporary stack variable<commit_after># -*- coding: utf-8 -*-

"""
First stage in numba translation. Translates Python bytecode to untyped
pykit IR.

Initially adapted from numbapro/npm/symbolic.py by <NAME>.
"""

from __future__ import print_function, division, absolute_import

import __builtin__
import inspect
import dis
import pprint
import operator
import collections
from collections import namedtuple

from numba2.errors import error_context, CompileError, EmptyStackError
from numba2.runtime.obj import tupleobject
from .bytecode import ByteCode

from pykit.ir import Function, Builder, Op, Const, Value, ops
from pykit import types

#===------------------------------------------------------------------===
# Translation
#===------------------------------------------------------------------===

COMPARE_OP_FUNC = {
    '>': operator.gt,
    '<': operator.lt,
    '>=': operator.ge,
    '<=': operator.le,
    '==': operator.eq,
    '!=': operator.ne,
    'in': operator.contains,
    'is': operator.is_,
    'exception match': isinstance,
}

const = lambda val: Const(val, types.Opaque)

class Translate(object):
    """
    Translate bytecode to untypes pykit IR.
    """

    def __init__(self, func):
        self.func = func
        self.bytecode = ByteCode(func)

        # -------------------------------------------------
        # Find predecessors

        self.blocks = {}            # offset -> Block
        self.block2offset = {}      # Block -> offset
        self.allocas = {}           # varname -> alloca
        self.stacks = {}            # Block -> value stack
        self.exc_handlers = set()   # { Block }

        # -------------------------------------------------
        # Block stacks

        self.block_stack   = []
        self.loop_stack    = []
        self.except_stack  = []
        self.finally_stack = []

        # -------------------------------------------------
        # CFG

        self.predecessors = collections.defaultdict(set)
        self.phis = collections.defaultdict(list)

        # -------------------------------------------------
        # Variables and scoping

        self.code = self.bytecode.code
        self.varnames = self.bytecode.code.co_varnames
        self.consts = self.bytecode.code.co_consts
        self.names = self.bytecode.code.co_names
        self.argnames = self.varnames[:self.bytecode.code.co_argcount]

        self.globals = dict(vars(__builtin__))
        self.builtins = set(self.globals.values())
        self.globals.update(self.func.func_globals)

        # -------------------------------------------------
        # Error checks

        argspec = inspect.getargspec(self.func)
        assert not argspec.varargs, "does not support varargs"
        assert not argspec.keywords, "does not support keywords"

    def initialize(self):
        """Initialize pykit untypes structures"""

        # Setup Function
        sig = types.Function(types.Opaque, [types.Opaque] * len(self.argnames),
                             False)
        self.dst = Function(func_name(self.func), self.argnames, sig)

        # Setup Builder
        self.builder = Builder(self.dst)

        # Setup Blocks
        for offset in self.bytecode.labels:
            block = self.dst.new_block("Block%d" % offset)
            self.blocks[offset] = block
            self.stacks[block] = []

        # Setup Variables
        self.builder.position_at_beginning(self.dst.startblock)
        for varname in self.varnames:
            stackvar = self.builder.alloca(types.Pointer(types.Opaque),
                                           result=self.dst.temp(varname))
            self.allocas[varname] = stackvar

            # Initialize function arguments
            if varname in self.argnames:
                self.builder.store(self.dst.get_arg(varname), stackvar)

    def interpret(self):
        self.curblock = self.dst.startblock

        for inst in self.bytecode:
            if inst.offset in self.blocks:
                # Block switch
{'fraction_non_alphanumeric': 0.188857178309964, 'fraction_numerical': 0.0009926789924308227, 'mean_word_length': 1.6236979166666667, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2635517', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2093, 'n_words': 484}