<commit_before>#=======================================================================
# arm-sim.py
#=======================================================================

import sys
# TODO: figure out a better way to set PYTHONENV
sys.path.append('..')
sys.path.append('/work/bits0/dml257/hg-pypy/pypy')

from pydgin.storage import Memory
from pydgin.misc    import load_program
from bootstrap      import syscall_init, memory_size
from isa            import decode

from pydgin.debug     import Debug, pad, pad_hex
from rpython.rlib.jit import JitDriver, hint

#-----------------------------------------------------------------------
# help message
#-----------------------------------------------------------------------
# the help message to display on --help

help_message = """
Pydgin PARC Instruction Set Simulator
usage: %s <args> <sim_exe> <sim_args>

<sim_exe>  the executable to be simulated
<sim_args> arguments to be passed to the simulated executable
<args>     the following optional arguments are supported:

  --help          show this message and exit
  --test          run in testing mode (for running asm tests)
  --debug <flags> enable debug flags in a comma-separated form (e.g.
                  "--debug syscalls,insts"). the following flags are
                  supported:
       insts              cycle-by-cycle instructions
       rf                 register file accesses
       mem                memory accesses
       regdump            register dump
       syscalls           syscall information
       bootstrap          initial stack and register state

  --max-insts <i> run until the maximum number of instructions

"""

#-----------------------------------------------------------------------
# jit
#-----------------------------------------------------------------------

# for debug printing in PYPYLOG
def get_location( pc ):
  # TODO: add the disassembly of the instruction here as well
  return "pc: %x" % pc

jitdriver = JitDriver( greens =['pc'],
                       reds   =['state'],
                       get_printable_location=get_location,
                     )

def jitpolicy(driver):
  from rpython.jit.codewriter.policy import JitPolicy
  return JitPolicy()

#-----------------------------------------------------------------------
# run
#-----------------------------------------------------------------------
def run( state, max_insts=0 ):
  s = state

  while s.status == 0:

    jitdriver.jit_merge_point(
      pc       = s.fetch_pc(),
      state    = s,
    )

    # constant-fold pc and mem
    pc  = hint( s.fetch_pc(), promote=True )
    old = pc
    mem = hint( s.mem, promote=True )

    if s.debug.enabled( "insts" ):
      print pad( "%x" % pc, 6, " ", False ),

    # the print statement in memcheck conflicts with @elidable in iread.
    # So we use normal read if memcheck is enabled which includes the
    # memory checks

    if s.debug.enabled( "memcheck" ):
      inst = mem.read( pc, 4 )
    else:
      # we use trace elidable iread instead of just read
      inst = mem.iread( pc, 4 )

    inst_str, exec_fun = decode( inst )

    if s.debug.enabled( "insts" ):
      print "%s %s %s" % (
              pad_hex( inst ),
              pad( inst_str, 8 ),
              pad( "%d" % s.ncycles, 8 ), ),

    exec_fun( s, inst )

    s.ncycles += 1    # TODO: should this be done inside instruction definition?
    if s.stats_en: s.stat_ncycles += 1

    if s.debug.enabled( "insts" ):
      print
    if s.debug.enabled( "regdump" ):
      s.rf.print_regs( per_row=4 )
      print '%s%s%s%s' % (
        'N' if s.N else '-',
        'Z' if s.Z else '-',
        'C' if s.C else '-',
        'V' if s.V else '-'
      )

    # check if we have reached the end of the maximum instructions and
    # exit if necessary
    if max_insts != 0 and s.ncycles >= max_insts:
      print "Reached the max_insts (%d), exiting." % max_insts
      break

    if s.fetch_pc() < old:
      jitdriver.can_enter_jit(
        pc       = s.fetch_pc(),
        state    = s,
      )

  print 'DONE! Status =', s.status
  print 'Instructions Executed =', s.ncycles

#-----------------------------------------------------------------------
# entry_point
#-----------------------------------------------------------------------
def entry_point( argv ):

  filename_idx = 0
  debug_flags = []
  testbin = False
  max_insts = 0

  # we're using a mini state machine to parse the args, and these are two
  # states we have
<commit_msg>[arm] fixes to berkin's max_insts for jit translation<commit_after>#=======================================================================
# arm-sim.py
#=======================================================================

import sys
# TODO: figure out a better way to set PYTHONENV
sys.path.append('..')
sys.path.append('/work/bits0/dml257/hg-pypy/pypy')

from pydgin.storage import Memory
from pydgin.misc    import load_program
from bootstrap      import syscall_init, memory_size
from isa            import decode

from pydgin.debug     import Debug, pad, pad_hex
from rpython.rlib.jit import JitDriver, hint

#-----------------------------------------------------------------------
# help message
#-----------------------------------------------------------------------
# the help message to display on --help

help_message = """
Pydgin PARC Instruction Set Simulator
usage: %s <args> <sim_exe> <sim_args>

<sim_exe>  the executable to be simulated
<sim_args> arguments to be passed to the simulated executable
<args>     the following optional arguments are supported:

  --help          show this message and exit
  --test          run in testing mode (for running asm tests)
  --debug <flags> enable debug flags in a comma-separated form (e.g.
                  "--debug syscalls,insts"). the following flags are
                  supported:
       insts              cycle-by-cycle instructions
       rf                 register file accesses
       mem                memory accesses
       regdump            register dump
       syscalls           syscall information
       bootstrap          initial stack and register state

  --max-insts <i> run until the maximum number of instructions

"""

#-----------------------------------------------------------------------
# jit
#-----------------------------------------------------------------------

# for debug printing in PYPYLOG
def get_location( pc ):
  # TODO: add the disassembly of the instruction here as well
  return "pc: %x" % pc

jitdriver = JitDriver( greens =['pc',],
                       reds   =['max_insts','state',],
                       get_printable_location=get_location,
                     )

def jitpolicy(driver):
  from rpython.jit.codewriter.policy import JitPolicy
  return JitPolicy()

#-----------------------------------------------------------------------
# run
#-----------------------------------------------------------------------
def run( state, max_insts=0 ):
  s = state

  while s.status == 0:

    jitdriver.jit_merge_point(
      pc        = s.fetch_pc(),
      max_insts = max_insts,
      state     = s,
    )

    # constant-fold pc and mem
    pc  = hint( s.fetch_pc(), promote=True )
    old = pc
    mem = hint( s.mem, promote=True )

    if s.debug.enabled( "insts" ):
      print pad( "%x" % pc, 6, " ", False ),

    # the print statement in memcheck conflicts with @elidable in iread.
    # So we use normal read if memcheck is enabled which includes the
    # memory checks

    if s.debug.enabled( "memcheck" ):
      inst = mem.read( pc, 4 )
    else:
      # we use trace elidable iread instead of just read
      inst = mem.iread( pc, 4 )

    inst_str, exec_fun = decode( inst )

    if s.debug.enabled( "insts" ):
      print "%s %s %s" % (
              pad_hex( inst ),
              pad( inst_str, 8 ),
              pad( "%d" % s.ncycles, 8 ), ),

    exec_fun( s, inst )

    s.ncycles += 1    # TODO: should this be done inside instruction definition?
    if s.stats_en: s.stat_ncycles += 1

    if s.debug.enabled( "insts" ):
      print
    if s.debug.enabled( "regdump" ):
      s.rf.print_regs( per_row=4 )
      print '%s%s%s%s' % (
        'N' if s.N else '-',
        'Z' if s.Z else '-',
        'C' if s.C else '-',
        'V' if s.V else '-'
      )

    # check if we have reached the end of the maximum instructions and
    # exit if necessary
    if max_insts != 0 and s.ncycles >= max_insts:
      print "Reached the max_insts (%d), exiting." % max_insts
      break

    if s.fetch_pc() < old:
      jitdriver.can_enter_jit(
        pc        = s.fetch_pc(),
        max_insts = max_insts,
        state     = s,
      )

  print 'DONE! Status =', s.status
  print 'Instructions Executed =', s.ncycles

#-----------------------------------------------------------------------
# entry_point
#-----------------------------------------------------------------------
def entry_point( argv ):

  filename_idx = 0
  debug_flags = []
  testbin = False
  max_insts = 0

  # we're using a mini state machine to parse the args, and these are two
  # states we have

{'fraction_non_alphanumeric': 0.2411519015168169, 'fraction_numerical': 0.003737085073642559, 'mean_word_length': 1.537367540435025, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6763192', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2443, 'n_words': 823}