Ejemplo n.º 1
0
 def write_method(self):
     acc = []
     access = self.method.access
     self.constructor = False
     for modifier in access:
         if modifier == 'constructor':
             self.constructor = True
             continue
         acc.append(modifier)
     self.write('\n%s' % self.space())
     self.write_ext(('NEWLINE', '\n%s' % (self.space())))
     if acc:
         self.write('%s ' % ' '.join(acc))
         self.write_ext(('PROTOTYPE_ACCESS', '%s ' % ' '.join(acc)))
     if self.constructor:
         name = get_type(self.method.cls_name).split('.')[-1]
         self.write(name)
         self.write_ext(('NAME_METHOD_PROTOTYPE', '%s' % name, self.method))
     else:
         self.write(
             '%s %s' % (get_type(self.method.type), self.method.name))
         self.write_ext(
             ('PROTOTYPE_TYPE', '%s' % get_type(self.method.type)))
         self.write_ext(('SPACE', ' '))
         self.write_ext(
             ('NAME_METHOD_PROTOTYPE',
             '%s' % self.method.name, self.method))
     params = self.method.lparams
     if 'static' not in access:
         params = params[1:]
     proto = ''
     self.write_ext(('PARENTHESIS_START', '('))
     if self.method.params_type:
         proto = ', '.join(['%s p%s' % (get_type(p_type), param) for
                     p_type, param in zip(self.method.params_type, params)])
         first = True
         for p_type, param in zip(self.method.params_type, params):
             if not first:
                 self.write_ext(('COMMA', ', '))
             else:
                 first = False
             self.write_ext(('ARG_TYPE', '%s' % get_type(p_type)))
             self.write_ext(('SPACE', ' '))
             self.write_ext(
                 ('NAME_ARG', 'p%s' % param, p_type, self.method))
     self.write_ext(('PARENTHESIS_END', ')'))
     self.write('(%s)' % proto)
     if self.graph is None:
         self.write(';\n')
         self.write_ext(('METHOD_END_NO_CONTENT', ';\n'))
         return
     self.write('\n%s{\n' % self.space())
     self.write_ext(('METHOD_START', '\n%s{\n' % self.space()))
     self.inc_ind()
     self.visit_node(self.graph.entry)
     self.dec_ind()
     self.write('%s}\n' % self.space())
     self.write_ext(('METHOD_END', '%s}\n' % self.space()))
Ejemplo n.º 2
0
 def visit_move_exception(self, var, data=None):
     var.declared = True
     var_type = var.get_type() or 'unknownType'
     self.write('%s v%s' % (get_type(var_type), var.name))
     self.write_ext(
         ('EXCEPTION_TYPE', '%s' % get_type(var_type), data.type))
     self.write_ext(('SPACE', ' '))
     self.write_ext(
         ('NAME_CLASS_EXCEPTION', 'v%s' % var.value(), data.type, data))
Ejemplo n.º 3
0
 def visit_variable(self, var):
     var_type = var.get_type() or 'unknownType'
     if not var.declared:
         self.write('%s ' % get_type(var_type))
         self.write_ext(
             ('VARIABLE_TYPE', '%s' % get_type(var_type), var_type))
         self.write_ext(('SPACE', ' '))
         var.declared = True
     self.write('v%s' % var.name)
     self.write_ext(('NAME_VARIABLE', 'v%s' % var.name, var, var_type))
Ejemplo n.º 4
0
    def __init__(self, cls_name, field_type, field_name):
        super(StaticExpression, self).__init__()
        self.cls = util.get_type(cls_name)
        self.ftype = field_type
        self.name = field_name

        self.clsdesc = cls_name
Ejemplo n.º 5
0
 def visit_filled_new_array(self, atype, size, args):
     self.write('new %s {' % get_type(atype), data="NEW_ARRAY_FILLED")
     for idx, arg in enumerate(args):
         arg.visit(self)
         if idx + 1 < len(args):
             self.write(', ', data="COMMA")
     self.write('})', data="NEW_ARRAY_FILLED_END")
Ejemplo n.º 6
0
 def visit_decl(self, var):
     if not var.declared:
         var_type = var.get_type() or 'unknownType'
         self.write('%s%s v%s' % (
             self.space(), get_type(var_type),
             var.name), data="DECLARATION")
         self.end_ins()
Ejemplo n.º 7
0
    def __init__(self, rhs, klass, ftype, name):
        super(StaticInstruction, self).__init__()
        self.rhs = rhs.v
        self.cls = util.get_type(klass)
        self.ftype = ftype
        self.name = name
        self.var_map[rhs.v] = rhs

        self.clsdesc = klass
Ejemplo n.º 8
0
    def __init__(self, arg, klass, ftype, name):
        super(InstanceExpression, self).__init__()
        self.arg = arg.v
        self.cls = util.get_type(klass)
        self.ftype = ftype
        self.name = name
        self.var_map[arg.v] = arg

        self.clsdesc = klass
Ejemplo n.º 9
0
    def __init__(self, rhs, lhs, klass, atype, name):
        super(InstanceInstruction, self).__init__()
        self.lhs = lhs.v
        self.rhs = rhs.v
        self.atype = atype
        self.cls = util.get_type(klass)
        self.name = name
        self.var_map.update([(lhs.v, lhs), (rhs.v, rhs)])

        self.clsdesc = klass
Ejemplo n.º 10
0
    def get_source_ext(self):
        source = []
        if not self.inner and self.package:
            source.append(
            ('PACKAGE', [('PACKAGE_START', 'package '),
                         ('NAME_PACKAGE', '%s' % self.package),
                         ('PACKAGE_END', ';\n')]))
        list_proto = []
        list_proto.append(
            ('PROTOTYPE_ACCESS', '%s class ' % ' '.join(self.access)))
        list_proto.append(('NAME_PROTOTYPE', '%s' % self.name, self.package))
        superclass = self.superclass
        if superclass is not None and superclass != 'Ljava/lang/Object;':
            superclass = superclass[1:-1].replace('/', '.')
            list_proto.append(('EXTEND', ' extends '))
            list_proto.append(('NAME_SUPERCLASS', '%s' % superclass))

        if len(self.interfaces) > 0:
            list_proto.append(('IMPLEMENTS', ' implements '))
            for i, interface in enumerate(self.interfaces):
                if i != 0:
                    list_proto.append(('COMMA', ', '))
                list_proto.append(
                    ('NAME_INTERFACE', interface[1:-1].replace('/', '.')))
        list_proto.append(('PROTOTYPE_END', ' {\n'))
        source.append(("PROTOTYPE", list_proto))

        for field in self.fields:
            field_access_flags = field.get_access_flags()
            access = [util.ACCESS_FLAGS_FIELDS[flag] for flag in
                        util.ACCESS_FLAGS_FIELDS if flag & field_access_flags]
            f_type = util.get_type(field.get_descriptor())
            name = field.get_name()
            if access:
                access_str = '    %s ' % ' '.join(access)
            else:
                access_str = '    '
            source.append(
                ('FIELD', [('FIELD_ACCESS', access_str),
                           ('FIELD_TYPE', '%s' % f_type),
                           ('SPACE', ' '),
                           ('NAME_FIELD', '%s' % name, f_type, field),
                           ('FIELD_END', ';\n')]))

        #TODO: call get_source_ext for each subclass?
        for klass in self.subclasses.values():
            source.append((klass, klass.get_source()))

        for method in self.methods:
            if isinstance(method, DvMethod):
                source.append(("METHOD", method.get_source_ext()))
        source.append(("CLASS_END", [('CLASS_END', '}\n')]))
        return source
Ejemplo n.º 11
0
    def get_source(self):
        source = []
        if not self.inner and self.package:
            source.append('package %s;\n' % self.package)

        superclass, prototype = self.superclass, self.prototype
        if superclass is not None and superclass != 'Ljava/lang/Object;':
            superclass = superclass[1:-1].replace('/', '.')
            prototype += ' extends %s' % superclass

        if len(self.interfaces) > 0:
            prototype += ' implements %s' % ', '.join(
                        [n[1:-1].replace('/', '.') for n in self.interfaces])

        source.append('%s {\n' % prototype)
        for field in self.fields:
            name = field.get_name()
            access = util.get_access_field(field.get_access_flags())
            f_type = util.get_type(field.get_descriptor())
            source.append('    ')
            if access:
                source.append(' '.join(access))
                source.append(' ')
            if field.init_value:
                value = field.init_value.value
                if f_type == 'String':
                    value = '"%s"' % value
                elif field.proto == 'B':
                    value = '0x%x' % struct.unpack('b', value)[0]
                source.append('%s %s = %s;\n' % (f_type, name, value))
            else:
                source.append('%s %s;\n' % (f_type, name))

        for klass in self.subclasses.values():
            source.append(klass.get_source())

        for method in self.methods:
            if isinstance(method, DvMethod):
                source.append(method.get_source())
        source.append('}\n')
        return ''.join(source)
Ejemplo n.º 12
0
 def visit(self, visitor):
     return visitor.visit_check_cast(self.var_map[self.arg], util.get_type(self.type))
Ejemplo n.º 13
0
 def visit_exception(self, visitor):
     if self.exception_ins:
         visitor.visit_ins(self.exception_ins)
     else:
         visitor.write(get_type(self.catch_type))
Ejemplo n.º 14
0
 def visit_new_array(self, atype, size):
     self.write('new %s[' % get_type(atype[1:]), data="NEW_ARRAY")
     size.visit(self)
     self.write(']', data="NEW_ARRAY_END")
Ejemplo n.º 15
0
 def visit_new(self, atype, data=None):
     self.write('new %s' % get_type(atype))
     self.write_ext(('NEW', 'new '))
     self.write_ext(
         ('NAME_CLASS_NEW', '%s' % get_type(atype), data.type, data))