daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. from pypy.translator.avm2.util import serialize_u32 as u32, Avm2Label
  3. from pypy.translator.avm2.constants import METHODFLAG_Activation, METHODFLAG_SetsDxns
  4. from decorator import decorator
  5.  
  6. INSTRUCTIONS = {}
  7.  
  8. @decorator
  9. def needs_specialized(fn, self, *args, **kwargs):
  10.       if not self.specialized:
  11.             raise ValueError, "Instruction needs to be specialized"
  12.       return fn(self, *args, **kwargs)
  13.  
  14. class _Avm2SpecializedInstruction(object):
  15.       pass
  16.  
  17. class _Avm2ShortInstruction(object):
  18.       specialized=False
  19.       def __init__(self, opcode, name, stack=0, scope=0, flags=0):
  20.             self.opcode = opcode
  21.             self.name   = name
  22.             self.stack  = stack
  23.             self.scope  = scope
  24.             self.flags  = flags
  25.  
  26.             INSTRUCTIONS[opcode] = self
  27.  
  28.       def _repr(self):
  29.             return "%s (0x%H)" % (self.name, self.opcode)
  30.      
  31.       def _set_assembler_props(self, asm):
  32.             asm.flags |= self.flags
  33.             asm.stack_depth += self.stack
  34.             asm.scope_depth += self.scope
  35.  
  36.       def _serialize(self):
  37.             return chr(self.opcode)
  38.  
  39.       serialize = _serialize
  40.       set_assembler_props = _set_assembler_props
  41.       __repr__ = _repr
  42.      
  43.       def specialize(self, **kwargs):
  44.             return type("_Avm2_Instruction_%s" % self.name,
  45.                          (_Avm2SpecializedInstruction, self.__class__),
  46.                          dict(kwargs.items(),
  47.                                specialized=True,
  48.                                set_assembler_props=self._set_assembler_props,
  49.                                serialize=self._serialize,
  50.                                __repr__=self._repr,
  51.                                opcode=self.opcode, name=self.name, stack=self.stack, scope=self.scope,
  52.                          ))()
  53.            
  54.  
  55.       def __call__(self):
  56.             return self.specialize()
  57.  
  58. class _Avm2DebugInstruction(_Avm2ShortInstruction):
  59.       @needs_specialized
  60.       def _serialize(self):
  61.             return chr(self.opcode) + \
  62.                    chr(self.debug_type & 0xFF) + \
  63.                    u32(self.index) + \
  64.                    chr(self.reg & 0xFF) + \
  65.                    u32(self.extra)
  66.  
  67.       def __call__(self, debug_type, index, reg, extra):
  68.             return self.specialize(debug_type=debug_type, index=index, reg=reg, extra=extra)
  69.  
  70. class _Avm2U8Instruction(_Avm2ShortInstruction):
  71.       @needs_specialized
  72.       def _serialize(self):
  73.             return chr(self.opcode) + chr(self.argument)
  74.      
  75.       def __call__(self, argument):
  76.             return self.specialize(argument=argument)
  77.  
  78. class _Avm2U30Instruction(_Avm2U8Instruction):
  79.       @needs_specialized
  80.       def _serialize(self):
  81.             if hasattr(self.argument, "__iter__"):
  82.                   return chr(self.opcode) + ''.join(u32(i) for i in self.argument)
  83.             else:
  84.                   return chr(self.opcode) + u32(self.argument)
  85.  
  86.       def __call__(self, argument, *arguments):
  87.             if arguments:
  88.                   self.argument = [argument] + list(arguments)
  89.             else:
  90.                   self.argument = argument
  91.  
  92. class _Avm2KillInstruction(_Avm2U30Instruction):
  93.       @needs_specialized
  94.       def _set_assembler_props(self, asm):
  95.             super(_Avm2KillInstruction, self)._set_assembler_props(asm)
  96.             asm.kill_local(self.argument)
  97.  
  98. class _Avm2NamespaceInstruction(_Avm2U30Instruction):
  99.       @needs_specialized
  100.       def _set_assembler_props(self, asm):
  101.             super(_Avm2NamespaceInstruction, self)._set_assembler_props(asm)
  102.             has_rtns      = asm.constants.has_RTNS(self.multiname)
  103.             has_rtname    = asm.constants.has_RTName(self.multiname)
  104.             self.argument = asm.constants.multiname_pool.index_for(self.multiname)
  105.  
  106.             asm.stack_depth -= int(has_rtns) + int(has_rtname)
  107.  
  108.       def __call__(self, multiname):
  109.             return self.specialize(multiname=multiname)
  110.      
  111. class _Avm2SetLocalInstruction(_Avm2U30Instruction):
  112.       @needs_specialized
  113.       def __call__(self, index):
  114.             _speed = {0: setlocal_0, 1: setlocal_1, 2: setlocal_2, 3: setlocal_3}
  115.             if index in _speed:
  116.                   return _speed[index]
  117.             return self.specialize(index=index)
  118.  
  119. class _Avm2GetLocalInstruction(_Avm2ShortInstruction):
  120.       def __call__(self, index):
  121.             _speed = {0: getlocal_0, 1: getlocal_1, 2: getlocal_2, 3: getlocal_3}
  122.             if index in _speed:
  123.                   return _speed[index]
  124.             return self.specialize(index=index)
  125.  
  126. class _Avm2OffsetInstruction(_Avm2ShortInstruction):
  127.       @needs_specialized
  128.       def _repr(self):
  129.             return repr(super(_Avm2OffsetInstruction, self))[:-2] + " lbl=%r)>" % self.lbl
  130.      
  131.       @needs_specialized
  132.       def _set_assembler_props(self, asm):
  133.             super(_Avm2OffsetInstruction, self)._set_assembler_props
  134.             if self.lbl is None:
  135.                   self.lbl = Avm2Label(asm)
  136.                   self.asm = asm
  137.      
  138.       @needs_specialized
  139.       def _serialize(self):
  140.             code = chr(self.opcode)
  141.             code += self.lbl.write_relative_offset(len(self.asm) + 4, len(self.asm) + 1)
  142.             return code
  143.      
  144.       def __call__(self, lbl=None):
  145.             return self.specialize(lbl=lbl)
  146.  
  147. class _Avm2LookupSwitchInstruction(_Avm2ShortInstruction):
  148.       @needs_specialized
  149.       def _set_assembler_props(self, asm):
  150.             super(_Avm2LookupSwitchInstruction, self)._set_assembler_props(asm)
  151.             self.asm = asm
  152.             if self.default_label is None:
  153.                   self.default_label = Avm2Label(asm)
  154.             if isinstance(self.case_labels, int):
  155.                   self.case_labels = [Avm2Label(asm) for i in xrange(self.case_labels)]
  156.                  
  157.       @needs_specialized
  158.       def _serialize(self):
  159.             code = chr(self.opcode)
  160.             base = len(self.asm)
  161.             code += self.default_label.write_relative_offset(base, base+1)
  162.             code += u32(len(self.case_labels) - 1)
  163.            
  164.             for lbl in self.case_labels:
  165.                   location = base + len(code)
  166.                   code += lbl.write_relative_offset(base, location)
  167.             return code
  168.                  
  169.       def __call__(self, default_label=None, case_labels=None):
  170.             return self.specialize(default_label=default_label, case_labels=case_labels)
  171.  
  172. class _Avm2LabelInstruction(_Avm2ShortInstruction):
  173.       @needs_specialized
  174.       def _set_assembler_props(self, asm):
  175.             super(_Avm2LabelInstruction, self)._set_assembler_props(asm)
  176.             if self.lbl == None:
  177.                   self.define = True
  178.                   self.lbl = Avm2Label(asm)
  179.             else:
  180.                   assert self.lbl.address == -1
  181.                   asm.stack_depth = self.lbl.stack_depth
  182.                   asm.scope_depth = self.lbl.scope_depth
  183.             self.lbl.address = len(asm)
  184.  
  185.       @needs_specialized
  186.       def _serialize(self):
  187.             if self.define:
  188.                   return label_internal.serialize()
  189.             return ""
  190.      
  191.       def __call__(self, lbl=None):
  192.             return self.specialize(lbl=lbl, define=False)
  193.  
  194. class _Avm2Call(_Avm2U30Instruction):
  195.       @needs_specialized
  196.       def _set_assembler_props(self, asm):
  197.             asm.stack_depth += 1 - (self.argument + 2) # push function/receiver/args; push result
  198.      
  199. class _Avm2Construct(_Avm2U30Instruction):
  200.       @needs_specialized
  201.       def _set_assembler_props(self, asm):
  202.             asm.stack_depth += 1 - (self.argument + 1) # push object/args; push result
  203.      
  204. class _Avm2ConstructSuper(_Avm2U30Instruction):
  205.       @needs_specialized
  206.       def _set_assembler_props(self, asm):
  207.             asm.stack_depth += self.argument + 1 # pop receiver/args
  208.  
  209. class _Avm2CallIDX(_Avm2U30Instruction):
  210.       @needs_specialized
  211.       def _serialize(self):
  212.             return chr(self.opcode) + u32(self.index) + u32(self.argument)
  213.  
  214.       @needs_specialized
  215.       def _set_assembler_props(self, asm):
  216.             self.index = asm.constants.multiname_pool.index_for(self.multiname)
  217.             asm.stack_depth += 1 - (self.argument + 1) # push object/args; push result
  218.      
  219.       def __call__(self, multiname, num_args):
  220.             return self.specialize(multiname=multiname, argument=num_args)
  221.  
  222. class _Avm2CallMN(_Avm2CallIDX):
  223.       is_void = False
  224.       @needs_specialized
  225.       def _set_assembler_props(self, asm):
  226.             has_rtns   = asm.constants.has_RTNS(self.multiname)
  227.             has_rtname = asm.constants.has_RTName(self.multiname)
  228.             asm.stack_depth += int(self.is_void) - (1 + int(has_rtns) + int(has_rtname) + self.argument)
  229.            
  230.  
  231.       def __call__(self, multiname, num_args):
  232.             return self.specialize(multiname=multiname, argument=num_args)
RAW Paste Data
Top