Нуждающийся
Регистрация: 10.02.2007
Сообщений: 99
Написано 18 полезных сообщений (для 28 пользователей)
|
Re: создание MIDletPascal compiler'a
Но сначал добьем дизассемблер
добавляем переменную (массив)

var
ByteCode: array [1..209] of String = (
'aconst_null', 'iconst_m1', 'iconst_0', 'iconst_1', 'iconst_2', 'iconst_3',
'iconst_4', 'iconst_5', 'lconst_0', 'lconst_1', 'fconst_0', 'fconst_1',
'fconst_2', 'dconst_0', 'dconst_1', 'bipush', 'sipush', 'ldc1',
'ldc2', 'ldc2w', 'iload', 'lload', 'fload', 'dload',
'aload', 'iload_0', 'iload_1', 'iload_2', 'iload_3', 'lload_0',
'lload_1', 'lload_2', 'lload_3', 'fload_0', 'fload_1', 'fload_2',
'fload_3', 'dload_0', 'dload_1', 'dload_2', 'dload_3', 'aload_0',
'aload_1', 'aload_2', 'aload_3', 'iaload', 'laload', 'faload',
'daload', 'aaload', 'baload', 'caload', 'saload', 'istore',
'lstore', 'fstore', 'dstore', 'astore', 'istore_0', 'istore_1',
'istore_2', 'istore_3', 'lstore_0', 'lstore_1', 'lstore_2', 'lstore_3',
'fstore_0', 'fstore_1', 'fstore_2', 'fstore_3', 'dstore_0', 'dstore_1',
'dstore_2', 'dstore_3', 'astore_0', 'astore_1', 'astore_2', 'astore_3',
'iastore', 'lastore', 'fastore', 'dastore', 'aastore', 'bastore',
'castore', 'sastore', 'pop', 'pop2', 'dup', 'dup_x1',
'dup_x2', 'dup2', 'dup2_x1', 'dup2_x2', 'swap', 'iadd',
'ladd', 'fadd', 'dadd', 'isub', 'lsub', 'fsub',
'dsub', 'imul', 'lmul', 'fmul', 'dmul', 'idiv',
'ldiv', 'fdiv', 'ddiv', 'irem', 'lrem', 'frem',
'drem', 'ineg', 'leg', 'fneg', 'dneg', 'ishl',
'lshl', 'ishr', 'lshr', 'iushr', 'lushr', 'iand',
'land', 'ior', 'lor', 'ixor', 'lxor', 'iinc',
'i2l', 'i2f', 'i2d', 'l2i', 'l2f', 'l2d',
'f2i', 'f2l', 'f2d', 'd2i', 'd2l', 'd2f',
'int2byte', 'int2char', 'int2short', 'lcmp', 'fcmpl', 'fcmpg',
'dcmpl', 'dcmpg', 'ifeq', 'ifne', 'iflt', 'ifge',
'ifgt', 'ifle', 'if_icmpeq', 'if_icmpne', 'if_icmplt', 'if_icmpge',
'if_icmpgt', 'if_icmple', 'if_acmpeq', 'if_acmpne', 'goto', 'jsr',
'ret', 'tableswitch', 'lookupswitch', 'ireturn', 'lreturn', 'freturn',
'dreturn', 'areturn', 'return', 'getstatic', 'putstatic', 'getfield',
'putfield', 'invokevirtual','invokenonvirtual', 'invokestatic', 'invokeinterface', '',
'new', 'newarray', 'anewarray', 'arraylength', 'athrow', 'checkcast',
'instanceof', 'monitorenter', 'monitorexit', 'wide', 'multianewarray', 'ifnull',
'ifnonnull', 'goto_w', 'jsr_w', 'breakpoint', '', '',
'', '', '', '', 'ret_w'
);
немного изменяем нашу фукцию
procedure load_attribute_info;
var
op,p1,p2: byte;
begin
и тыкаем (где разбор кода следучее)
write(#10#13,TAB);
n := 0;
while n < pCode_attribute^.code_length do
begin
get(p,1);
op:= byte(p^);
write(hexw(n),'h',TAB,hex(byte(p^)),' ');
case byte(p^) of
18 : // 1
begin
get(p,1); p1:=byte(p^); write(hex(byte(p^)),' ');
write(TAB2,ByteCode[op],' ',hex(p1),'h');
inc(n);
end;
17,19,20,132,
153..168,178..184,
187,189,192,193,
198,199,209 : // 2
begin
get(p,1); p1:=byte(p^); write(hex(byte(p^)),' ');
get(p,1); p2:=byte(p^); write(hex(byte(p^)),' ');
write(TAB,ByteCode[op],' ',hex(p1),hex(p2),'h');
inc(n,2);
end;
197 : // 3
begin
inc(n,3);
end;
185,200,201 : // 4
begin
inc(n,4);
end;
171 : // 10
begin
end;
170 : // 14
begin
end;
else
write(TAB2,ByteCode[byte(p^)]);
end;
inc(n);
write(#10#13,TAB);
end;
writeln;
writeln;
И вот мы ужо имеем дизассемблер!!!
Вот файло какое я коцал MyClass.java
interface Constants {
int NO=0;
int YES=1;
}
interface Constants2 {
int NO=0;
int YES=1;
}
class MyClass implements Constants {
public static void main (String [] args) {
int i = 0;
if (i==NO) System.out.println ("NO");
else System.out.println ("YES");
}
}
Вот шо из етого вышло

0009h 1 1 CONSTANT_Utf8-Unicode [7] = MyClass
0013h 2 7 CONSTANT_Class : 1
0016h 3 1 CONSTANT_Utf8-Unicode [16] = java/lang/Object
0029h 4 7 CONSTANT_Class : 3
002Ch 5 1 CONSTANT_Utf8-Unicode [9] = Constants
0038h 6 7 CONSTANT_Class : 5
003Bh 7 1 CONSTANT_Utf8-Unicode [4] = main
0042h 8 1 CONSTANT_Utf8-Unicode [22] = ([Ljava/lang/String;)V
005Bh 9 1 CONSTANT_Utf8-Unicode [4] = Code
0062h 10 1 CONSTANT_Utf8-Unicode [15] = LineNumberTable
0074h 11 1 CONSTANT_Utf8-Unicode [3] = out
007Ah 12 1 CONSTANT_Utf8-Unicode [21] = Ljava/io/PrintStream;
0092h 13 12 CONSTANT_NameAndType : 11 12
0097h 14 1 CONSTANT_Utf8-Unicode [16] = java/lang/System
00AAh 15 7 CONSTANT_Class : 14
00ADh 16 9 CONSTANT_Fieldref : 15 13
00B2h 17 1 CONSTANT_Utf8-Unicode [2] = NO
00B7h 18 8 CONSTANT_String : 17
00BAh 19 1 CONSTANT_Utf8-Unicode [7] = println
00C4h 20 1 CONSTANT_Utf8-Unicode [21] = (Ljava/lang/String;)V
00DCh 21 12 CONSTANT_NameAndType : 19 20
00E1h 22 1 CONSTANT_Utf8-Unicode [19] = java/io/PrintStream
00F7h 23 7 CONSTANT_Class : 22
00FAh 24 10 CONSTANT_Methodref : 23 21
00FFh 25 1 CONSTANT_Utf8-Unicode [3] = YES
0105h 26 8 CONSTANT_String : 25
0108h 27 1 CONSTANT_Utf8-Unicode [6] = <init>
0111h 28 1 CONSTANT_Utf8-Unicode [3] = ()V
0117h 29 12 CONSTANT_NameAndType : 27 28
011Ch 30 10 CONSTANT_Methodref : 4 29
0121h 31 1 CONSTANT_Utf8-Unicode [10] = SourceFile
012Eh 32 1 CONSTANT_Utf8-Unicode [12] = MyClass.java
013Eh access_flags : 32
0140h this_class : 2
0142h super_class : 4
0144h interfaces [1] 6 -> [5]Constants,
0148h field_info [0]
014Ah method_info [2]
014Ch method_info : 0-----------
access_flags : 9
name_index : [7] -> main
signature_index : 8
attributes_count : 1 offs:[0154h]
0154h GenericAttribute_info
attribute_name : 9 -> Code
attribute_length : 66
015Ah Code:
0160h max_stack : 2
0160h max_locals : 2
015Eh code_length : 26
0000h 03 iconst_0
0001h 3C istore_1
0002h 1B iload_1
0003h 9A 00 0E ifne 000Eh
0006h B2 00 10 getstatic 0010h
0009h 12 12 ldc1 12h
000Bh B6 00 18 invokevirtual 0018h
000Eh A7 00 0B goto 000Bh
0011h B2 00 10 getstatic 0010h
0014h 12 1A ldc1 1Ah
0016h B6 00 18 invokevirtual 0018h
0019h B1 return
0180h GenericAttribute_info
attribute_name : 10 -> LineNumberTable
attribute_length : 22
019Ch method_info : 1-----------
access_flags : 0
name_index : [27] -> <init>
signature_index : 28
attributes_count : 1 offs:[01A4h]
01A4h GenericAttribute_info
attribute_name : 9 -> Code
attribute_length : 33
01AAh Code:
01B0h max_stack : 1
01B0h max_locals : 1
01AEh code_length : 5
0000h 2A aload_0
0001h B7 00 1E invokenonvirtual 001Eh
0004h B1 return
01BBh GenericAttribute_info
attribute_name : 10 -> LineNumberTable
attribute_length : 10
OFFSET: 01CBh
01CBh attribute_info [1]
01CDh GenericAttribute_info
attribute_name : 31 -> SourceFile
attribute_length : 2
00 20
видим дизассемблерный код
03 iconst_0
3C istore_1
1B iload_1
9A 00 0E ifne 000Eh
B2 00 10 getstatic 0010h
12 ldc1
12 ldc1
B6 00 18 invokevirtual 0018h
A7 00 0B goto 000Bh
B2 00 10 getstatic 0010h
12 ldc1
1A iload_0
B6 00 18 invokevirtual 0018h
B1 return
ЗЫ...
Я тут разобрал токма двубайтовые операции, просто исходников маловат пока, если есть возможность накидайте простых исходников
с разными выпендрезами, типа исключит. ситуации и т.д... Чоб протестить.
Ассемблирование можно пустить в обратном порядке, тоисть по инструкции просто брать адрес в массиве
и собирать исходный класс.
Последний раз редактировалось satan, 28.09.2008 в 08:42.
|