![](/media/cache/profiles/98c39996bf1543e974747a2549b3107c.50x50_q85.png)
JVM test
ravem.cpp@9:4ea7773ea2b0, 2013-09-07 (annotated)
- Committer:
- lynxeyed_atsu
- Date:
- Sat Sep 07 04:59:10 2013 +0000
- Revision:
- 9:4ea7773ea2b0
- Parent:
- 6:b9d0d96b052f
added if_icmp* mnemonics
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lynxeyed_atsu | 5:047542b65d00 | 1 | /* |
lynxeyed_atsu | 5:047542b65d00 | 2 | * RAVEM.c |
lynxeyed_atsu | 5:047542b65d00 | 3 | * |
lynxeyed_atsu | 5:047542b65d00 | 4 | * Created on: return 2013/07/18 |
lynxeyed_atsu | 5:047542b65d00 | 5 | * Author: return lynxeyed |
lynxeyed_atsu | 5:047542b65d00 | 6 | */ |
lynxeyed_atsu | 5:047542b65d00 | 7 | #include <stdio.h> |
lynxeyed_atsu | 5:047542b65d00 | 8 | #include <string.h> |
lynxeyed_atsu | 5:047542b65d00 | 9 | #include "ravem.h" |
lynxeyed_atsu | 5:047542b65d00 | 10 | |
lynxeyed_atsu | 5:047542b65d00 | 11 | |
lynxeyed_atsu | 6:b9d0d96b052f | 12 | |
lynxeyed_atsu | 6:b9d0d96b052f | 13 | /* |
lynxeyed_atsu | 5:047542b65d00 | 14 | const unsigned char vm_array[] = { |
lynxeyed_atsu | 6:b9d0d96b052f | 15 | 0xCA, 0xFE, 0xBA, 0xBE, 0x00, 0x00, 0x00, 0x32, 0x00, 0x35, 0x0A, 0x00, 0x0F, |
lynxeyed_atsu | 5:047542b65d00 | 16 | 0x00, 0x1B, 0x09, 0x00, 0x1C, 0x00, 0x1D, 0x08, 0x00, 0x1E, 0x0A, 0x00, 0x1F, 0x00, 0x20, 0x0A, |
lynxeyed_atsu | 5:047542b65d00 | 17 | 0x00, 0x0B, 0x00, 0x21, 0x0A, 0x00, 0x1F, 0x00, 0x22, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 18 | 0x03, 0xE8, 0x0A, 0x00, 0x0B, 0x00, 0x23, 0x07, 0x00, 0x24, 0x07, 0x00, 0x25, 0x0A, 0x00, 0x0B, |
lynxeyed_atsu | 5:047542b65d00 | 19 | 0x00, 0x1B, 0x0A, 0x00, 0x0B, 0x00, 0x26, 0x08, 0x00, 0x27, 0x07, 0x00, 0x28, 0x01, 0x00, 0x06, |
lynxeyed_atsu | 5:047542b65d00 | 20 | 0x3C, 0x69, 0x6E, 0x69, 0x74, 0x3E, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, |
lynxeyed_atsu | 5:047542b65d00 | 21 | 0x6F, 0x64, 0x65, 0x01, 0x00, 0x0F, 0x4C, 0x69, 0x6E, 0x65, 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, |
lynxeyed_atsu | 5:047542b65d00 | 22 | 0x54, 0x61, 0x62, 0x6C, 0x65, 0x01, 0x00, 0x03, 0x72, 0x75, 0x6E, 0x01, 0x00, 0x0D, 0x53, 0x74, |
lynxeyed_atsu | 5:047542b65d00 | 23 | 0x61, 0x63, 0x6B, 0x4D, 0x61, 0x70, 0x54, 0x61, 0x62, 0x6C, 0x65, 0x07, 0x00, 0x24, 0x01, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 24 | 0x04, 0x6D, 0x61, 0x69, 0x6E, 0x01, 0x00, 0x16, 0x28, 0x5B, 0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, |
lynxeyed_atsu | 5:047542b65d00 | 25 | 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3B, 0x29, 0x56, 0x01, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 26 | 0x0A, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x46, 0x69, 0x6C, 0x65, 0x01, 0x00, 0x0A, 0x68, 0x6F, |
lynxeyed_atsu | 5:047542b65d00 | 27 | 0x67, 0x65, 0x31, 0x2E, 0x6A, 0x61, 0x76, 0x61, 0x0C, 0x00, 0x10, 0x00, 0x11, 0x07, 0x00, 0x29, |
lynxeyed_atsu | 5:047542b65d00 | 28 | 0x0C, 0x00, 0x2A, 0x00, 0x2B, 0x01, 0x00, 0x0E, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x20, 0x4E, |
lynxeyed_atsu | 5:047542b65d00 | 29 | 0x75, 0x6D, 0x62, 0x65, 0x72, 0x3D, 0x07, 0x00, 0x2C, 0x0C, 0x00, 0x2D, 0x00, 0x2E, 0x0C, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 30 | 0x2F, 0x00, 0x30, 0x0C, 0x00, 0x31, 0x00, 0x2E, 0x0C, 0x00, 0x32, 0x00, 0x33, 0x01, 0x00, 0x1E, |
lynxeyed_atsu | 5:047542b65d00 | 31 | 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x72, |
lynxeyed_atsu | 5:047542b65d00 | 32 | 0x75, 0x70, 0x74, 0x65, 0x64, 0x45, 0x78, 0x63, 0x65, 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x01, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 33 | 0x05, 0x68, 0x6F, 0x67, 0x65, 0x31, 0x0C, 0x00, 0x34, 0x00, 0x11, 0x01, 0x00, 0x0B, 0x54, 0x68, |
lynxeyed_atsu | 5:047542b65d00 | 34 | 0x72, 0x65, 0x61, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x01, 0x00, 0x10, 0x6A, 0x61, 0x76, 0x61, |
lynxeyed_atsu | 5:047542b65d00 | 35 | 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x54, 0x68, 0x72, 0x65, 0x61, 0x64, 0x01, 0x00, 0x10, 0x6A, |
lynxeyed_atsu | 5:047542b65d00 | 36 | 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x01, |
lynxeyed_atsu | 5:047542b65d00 | 37 | 0x00, 0x03, 0x6F, 0x75, 0x74, 0x01, 0x00, 0x15, 0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x69, 0x6F, |
lynxeyed_atsu | 5:047542b65d00 | 38 | 0x2F, 0x50, 0x72, 0x69, 0x6E, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6D, 0x3B, 0x01, 0x00, 0x13, |
lynxeyed_atsu | 5:047542b65d00 | 39 | 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x69, 0x6F, 0x2F, 0x50, 0x72, 0x69, 0x6E, 0x74, 0x53, 0x74, 0x72, |
lynxeyed_atsu | 5:047542b65d00 | 40 | 0x65, 0x61, 0x6D, 0x01, 0x00, 0x05, 0x70, 0x72, 0x69, 0x6E, 0x74, 0x01, 0x00, 0x15, 0x28, 0x4C, |
lynxeyed_atsu | 5:047542b65d00 | 41 | 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, |
lynxeyed_atsu | 5:047542b65d00 | 42 | 0x3B, 0x29, 0x56, 0x01, 0x00, 0x07, 0x67, 0x65, 0x74, 0x4E, 0x61, 0x6D, 0x65, 0x01, 0x00, 0x14, |
lynxeyed_atsu | 5:047542b65d00 | 43 | 0x28, 0x29, 0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x53, 0x74, 0x72, |
lynxeyed_atsu | 5:047542b65d00 | 44 | 0x69, 0x6E, 0x67, 0x3B, 0x01, 0x00, 0x07, 0x70, 0x72, 0x69, 0x6E, 0x74, 0x6C, 0x6E, 0x01, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 45 | 0x05, 0x73, 0x6C, 0x65, 0x65, 0x70, 0x01, 0x00, 0x04, 0x28, 0x4A, 0x29, 0x56, 0x01, 0x00, 0x05, |
lynxeyed_atsu | 5:047542b65d00 | 46 | 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x20, 0x00, 0x0B, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 47 | 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x11, 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, 0x00, 0x1D, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 48 | 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2A, 0xB7, 0x00, 0x01, 0xB1, 0x00, 0x00, 0x00, 0x01, |
lynxeyed_atsu | 5:047542b65d00 | 49 | 0x00, 0x13, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x14, |
lynxeyed_atsu | 5:047542b65d00 | 50 | 0x00, 0x11, 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, 0x00, 0x60, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 51 | 0x00, 0x1F, 0xB2, 0x00, 0x02, 0x12, 0x03, 0xB6, 0x00, 0x04, 0xB2, 0x00, 0x02, 0x2A, 0xB6, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 52 | 0x05, 0xB6, 0x00, 0x06, 0x14, 0x00, 0x07, 0xB8, 0x00, 0x09, 0xA7, 0xFF, 0xE8, 0x4C, 0xA7, 0xFF, |
lynxeyed_atsu | 5:047542b65d00 | 53 | 0xE4, 0x00, 0x01, 0x00, 0x12, 0x00, 0x18, 0x00, 0x1B, 0x00, 0x0A, 0x00, 0x02, 0x00, 0x13, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 54 | 0x00, 0x00, 0x1A, 0x00, 0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08, 0x00, 0x06, 0x00, 0x12, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 55 | 0x08, 0x00, 0x18, 0x00, 0x0A, 0x00, 0x1B, 0x00, 0x09, 0x00, 0x1C, 0x00, 0x0A, 0x00, 0x15, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 56 | 0x00, 0x00, 0x07, 0x00, 0x02, 0x00, 0x5A, 0x07, 0x00, 0x16, 0x00, 0x09, 0x00, 0x17, 0x00, 0x18, |
lynxeyed_atsu | 5:047542b65d00 | 57 | 0x00, 0x01, 0x00, 0x12, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x21, |
lynxeyed_atsu | 5:047542b65d00 | 58 | 0xBB, 0x00, 0x0B, 0x59, 0xB7, 0x00, 0x0C, 0x4C, 0xBB, 0x00, 0x0B, 0x59, 0xB7, 0x00, 0x0C, 0x4D, |
lynxeyed_atsu | 5:047542b65d00 | 59 | 0x2B, 0xB6, 0x00, 0x0D, 0x2C, 0xB6, 0x00, 0x0D, 0xB2, 0x00, 0x02, 0x12, 0x0E, 0xB6, 0x00, 0x06, |
lynxeyed_atsu | 5:047542b65d00 | 60 | 0xB1, 0x00, 0x00, 0x00, 0x01, 0x00, 0x13, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x06, 0x00, 0x00, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 61 | 0x11, 0x00, 0x08, 0x00, 0x12, 0x00, 0x10, 0x00, 0x14, 0x00, 0x14, 0x00, 0x15, 0x00, 0x18, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 62 | 0x17, 0x00, 0x20, 0x00, 0x18, 0x00, 0x01, 0x00, 0x19, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1A, 0x00, |
lynxeyed_atsu | 5:047542b65d00 | 63 | 0x00, 0x00 |
lynxeyed_atsu | 5:047542b65d00 | 64 | }; |
lynxeyed_atsu | 6:b9d0d96b052f | 65 | */ |
lynxeyed_atsu | 5:047542b65d00 | 66 | char cp_str[32]; |
lynxeyed_atsu | 5:047542b65d00 | 67 | int32_t total_const_pool_num; |
lynxeyed_atsu | 5:047542b65d00 | 68 | int32_t methods_count; |
lynxeyed_atsu | 5:047542b65d00 | 69 | |
lynxeyed_atsu | 6:b9d0d96b052f | 70 | void bc_init(void){ |
lynxeyed_atsu | 6:b9d0d96b052f | 71 | bytecode_read_init(); |
lynxeyed_atsu | 6:b9d0d96b052f | 72 | } |
lynxeyed_atsu | 6:b9d0d96b052f | 73 | char *bc_seek(int bc_num, int length){ |
lynxeyed_atsu | 6:b9d0d96b052f | 74 | |
lynxeyed_atsu | 6:b9d0d96b052f | 75 | return bytecode_read(bc_num, length); |
lynxeyed_atsu | 5:047542b65d00 | 76 | } |
lynxeyed_atsu | 5:047542b65d00 | 77 | |
lynxeyed_atsu | 5:047542b65d00 | 78 | int32_t getLengthOfConstantInfo(uint8_t constantType) |
lynxeyed_atsu | 5:047542b65d00 | 79 | { |
lynxeyed_atsu | 5:047542b65d00 | 80 | switch (constantType) { |
lynxeyed_atsu | 5:047542b65d00 | 81 | |
lynxeyed_atsu | 5:047542b65d00 | 82 | case CONSTANT_Class: |
lynxeyed_atsu | 5:047542b65d00 | 83 | case CONSTANT_String: |
lynxeyed_atsu | 5:047542b65d00 | 84 | case CONSTANT_MethodType: |
lynxeyed_atsu | 5:047542b65d00 | 85 | case CONSTANT_InvokeDynamic: |
lynxeyed_atsu | 5:047542b65d00 | 86 | return 3; |
lynxeyed_atsu | 5:047542b65d00 | 87 | |
lynxeyed_atsu | 5:047542b65d00 | 88 | case CONSTANT_Fieldref: |
lynxeyed_atsu | 5:047542b65d00 | 89 | case CONSTANT_Methodref: |
lynxeyed_atsu | 5:047542b65d00 | 90 | case CONSTANT_InterfaceMethodref: |
lynxeyed_atsu | 5:047542b65d00 | 91 | case CONSTANT_Integer: |
lynxeyed_atsu | 5:047542b65d00 | 92 | case CONSTANT_Float: |
lynxeyed_atsu | 5:047542b65d00 | 93 | case CONSTANT_MethodHandle: |
lynxeyed_atsu | 5:047542b65d00 | 94 | case CONSTANT_NameAndType: |
lynxeyed_atsu | 5:047542b65d00 | 95 | return 5; |
lynxeyed_atsu | 5:047542b65d00 | 96 | |
lynxeyed_atsu | 5:047542b65d00 | 97 | case CONSTANT_Long: |
lynxeyed_atsu | 5:047542b65d00 | 98 | case CONSTANT_Double: |
lynxeyed_atsu | 5:047542b65d00 | 99 | return 9; |
lynxeyed_atsu | 5:047542b65d00 | 100 | |
lynxeyed_atsu | 5:047542b65d00 | 101 | case CONSTANT_Utf8: |
lynxeyed_atsu | 5:047542b65d00 | 102 | return (-1); //4 or more (see 'length') |
lynxeyed_atsu | 5:047542b65d00 | 103 | |
lynxeyed_atsu | 5:047542b65d00 | 104 | default: |
lynxeyed_atsu | 5:047542b65d00 | 105 | return 0; |
lynxeyed_atsu | 5:047542b65d00 | 106 | } |
lynxeyed_atsu | 5:047542b65d00 | 107 | } |
lynxeyed_atsu | 5:047542b65d00 | 108 | |
lynxeyed_atsu | 5:047542b65d00 | 109 | const_pool_t getConstantPoolInfo(int const_num){ |
lynxeyed_atsu | 5:047542b65d00 | 110 | |
lynxeyed_atsu | 5:047542b65d00 | 111 | const_pool_t c; |
lynxeyed_atsu | 5:047542b65d00 | 112 | int i=0, j=1, length; |
lynxeyed_atsu | 5:047542b65d00 | 113 | |
lynxeyed_atsu | 5:047542b65d00 | 114 | total_const_pool_num = (*bc_seek(8,1) << 8) + *bc_seek(9,1); |
lynxeyed_atsu | 5:047542b65d00 | 115 | |
lynxeyed_atsu | 5:047542b65d00 | 116 | if ((const_num ==0)||(const_num >= total_const_pool_num)) |
lynxeyed_atsu | 5:047542b65d00 | 117 | { |
lynxeyed_atsu | 5:047542b65d00 | 118 | c.index = 0; |
lynxeyed_atsu | 5:047542b65d00 | 119 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 120 | } |
lynxeyed_atsu | 5:047542b65d00 | 121 | |
lynxeyed_atsu | 5:047542b65d00 | 122 | while(1) |
lynxeyed_atsu | 5:047542b65d00 | 123 | { |
lynxeyed_atsu | 5:047542b65d00 | 124 | c.tag = *bc_seek(10+i,1); // tag |
lynxeyed_atsu | 5:047542b65d00 | 125 | length = getLengthOfConstantInfo(*bc_seek(10+i,1)); |
lynxeyed_atsu | 5:047542b65d00 | 126 | |
lynxeyed_atsu | 5:047542b65d00 | 127 | // Constant_UTF8 |
lynxeyed_atsu | 5:047542b65d00 | 128 | if(length == -1){ |
lynxeyed_atsu | 5:047542b65d00 | 129 | length = ((*bc_seek(11+i,1)<<8) + *bc_seek(12+i,1)) + 3 /* tag + length = 3byte*/ ; |
lynxeyed_atsu | 5:047542b65d00 | 130 | } |
lynxeyed_atsu | 5:047542b65d00 | 131 | |
lynxeyed_atsu | 5:047542b65d00 | 132 | if(j == const_num)break; |
lynxeyed_atsu | 5:047542b65d00 | 133 | if((c.tag == CONSTANT_Long)||(c.tag == CONSTANT_Double)){ |
lynxeyed_atsu | 5:047542b65d00 | 134 | j = j + 2; |
lynxeyed_atsu | 5:047542b65d00 | 135 | }else{ |
lynxeyed_atsu | 5:047542b65d00 | 136 | j = j + 1; |
lynxeyed_atsu | 5:047542b65d00 | 137 | } |
lynxeyed_atsu | 5:047542b65d00 | 138 | |
lynxeyed_atsu | 5:047542b65d00 | 139 | if(j >= total_const_pool_num)break; |
lynxeyed_atsu | 5:047542b65d00 | 140 | i += length; |
lynxeyed_atsu | 5:047542b65d00 | 141 | } |
lynxeyed_atsu | 5:047542b65d00 | 142 | |
lynxeyed_atsu | 5:047542b65d00 | 143 | c.index = ((*bc_seek(11+i,1)<<8) + *bc_seek(12+i,1)); |
lynxeyed_atsu | 5:047542b65d00 | 144 | c.index2 = ((*bc_seek(13+i,1)<<8) + *bc_seek(14+i,1)); |
lynxeyed_atsu | 5:047542b65d00 | 145 | c.bc_num = 10 + i; //todo: fix this |
lynxeyed_atsu | 5:047542b65d00 | 146 | switch(c.tag) |
lynxeyed_atsu | 5:047542b65d00 | 147 | { |
lynxeyed_atsu | 5:047542b65d00 | 148 | case CONSTANT_Utf8: |
lynxeyed_atsu | 5:047542b65d00 | 149 | memcpy((char*)cp_str, bc_seek(13+i,c.index), c.index); |
lynxeyed_atsu | 5:047542b65d00 | 150 | cp_str[c.index] = '\0'; |
lynxeyed_atsu | 5:047542b65d00 | 151 | c.stack_pt = (char *)&cp_str; |
lynxeyed_atsu | 5:047542b65d00 | 152 | break; |
lynxeyed_atsu | 5:047542b65d00 | 153 | |
lynxeyed_atsu | 5:047542b65d00 | 154 | //case CONSTANT_Float: |
lynxeyed_atsu | 5:047542b65d00 | 155 | //case CONSTANT_Long: |
lynxeyed_atsu | 5:047542b65d00 | 156 | //case CONSTANT_Double: |
lynxeyed_atsu | 5:047542b65d00 | 157 | case CONSTANT_Integer: |
lynxeyed_atsu | 5:047542b65d00 | 158 | case CONSTANT_Float: |
lynxeyed_atsu | 5:047542b65d00 | 159 | c.index = (*bc_seek(11+i,1)<<24) + (*bc_seek(12+i,1) << 16) + (*bc_seek(13+i,1)<<8) + *bc_seek(14+i,1); |
lynxeyed_atsu | 5:047542b65d00 | 160 | break; |
lynxeyed_atsu | 5:047542b65d00 | 161 | case CONSTANT_Double: |
lynxeyed_atsu | 5:047542b65d00 | 162 | case CONSTANT_Long: |
lynxeyed_atsu | 5:047542b65d00 | 163 | c.index = (*bc_seek(11+i,1)<<24) + (*bc_seek(12+i,1) << 16) + (*bc_seek(13+i,1)<<8) + *bc_seek(14+i,1); |
lynxeyed_atsu | 5:047542b65d00 | 164 | c.index2 = (*bc_seek(15+i,1)<<24) + (*bc_seek(16+i,1) << 16) + (*bc_seek(17+i,1)<<8) + *bc_seek(18+i,1); |
lynxeyed_atsu | 5:047542b65d00 | 165 | break; |
lynxeyed_atsu | 5:047542b65d00 | 166 | default: |
lynxeyed_atsu | 5:047542b65d00 | 167 | break; |
lynxeyed_atsu | 5:047542b65d00 | 168 | |
lynxeyed_atsu | 5:047542b65d00 | 169 | } |
lynxeyed_atsu | 5:047542b65d00 | 170 | |
lynxeyed_atsu | 5:047542b65d00 | 171 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 172 | } |
lynxeyed_atsu | 5:047542b65d00 | 173 | |
lynxeyed_atsu | 5:047542b65d00 | 174 | //seek class_name_index,Strings,Numerics from Constant Pool |
lynxeyed_atsu | 5:047542b65d00 | 175 | const_pool_t seekConstClassNumString(int const_num) |
lynxeyed_atsu | 5:047542b65d00 | 176 | { |
lynxeyed_atsu | 5:047542b65d00 | 177 | const_pool_t c; |
lynxeyed_atsu | 5:047542b65d00 | 178 | c = getConstantPoolInfo(const_num); |
lynxeyed_atsu | 5:047542b65d00 | 179 | |
lynxeyed_atsu | 5:047542b65d00 | 180 | switch (c.tag) |
lynxeyed_atsu | 5:047542b65d00 | 181 | { |
lynxeyed_atsu | 5:047542b65d00 | 182 | case CONSTANT_String: |
lynxeyed_atsu | 5:047542b65d00 | 183 | case CONSTANT_Class: |
lynxeyed_atsu | 5:047542b65d00 | 184 | c = getConstantPoolInfo(c.index); |
lynxeyed_atsu | 5:047542b65d00 | 185 | break; |
lynxeyed_atsu | 5:047542b65d00 | 186 | |
lynxeyed_atsu | 5:047542b65d00 | 187 | default: |
lynxeyed_atsu | 5:047542b65d00 | 188 | break; |
lynxeyed_atsu | 5:047542b65d00 | 189 | } |
lynxeyed_atsu | 5:047542b65d00 | 190 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 191 | } |
lynxeyed_atsu | 5:047542b65d00 | 192 | |
lynxeyed_atsu | 5:047542b65d00 | 193 | // for methodref & Fieldref |
lynxeyed_atsu | 5:047542b65d00 | 194 | const_pool_t seekClassIndex(int const_num){ |
lynxeyed_atsu | 5:047542b65d00 | 195 | const_pool_t c; |
lynxeyed_atsu | 5:047542b65d00 | 196 | c = getConstantPoolInfo(const_num); |
lynxeyed_atsu | 5:047542b65d00 | 197 | |
lynxeyed_atsu | 5:047542b65d00 | 198 | switch(c.tag) |
lynxeyed_atsu | 5:047542b65d00 | 199 | { |
lynxeyed_atsu | 5:047542b65d00 | 200 | case CONSTANT_Methodref: |
lynxeyed_atsu | 5:047542b65d00 | 201 | case CONSTANT_Fieldref: |
lynxeyed_atsu | 5:047542b65d00 | 202 | c = seekConstClassNumString(c.index); |
lynxeyed_atsu | 5:047542b65d00 | 203 | break; |
lynxeyed_atsu | 5:047542b65d00 | 204 | |
lynxeyed_atsu | 5:047542b65d00 | 205 | default: |
lynxeyed_atsu | 5:047542b65d00 | 206 | break; // in fact,we must program in error process but tedious;-P |
lynxeyed_atsu | 5:047542b65d00 | 207 | } |
lynxeyed_atsu | 5:047542b65d00 | 208 | |
lynxeyed_atsu | 5:047542b65d00 | 209 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 210 | } |
lynxeyed_atsu | 5:047542b65d00 | 211 | |
lynxeyed_atsu | 5:047542b65d00 | 212 | // For NameAndType:name index |
lynxeyed_atsu | 5:047542b65d00 | 213 | const_pool_t seekNameAndType_name(int const_num){ |
lynxeyed_atsu | 5:047542b65d00 | 214 | const_pool_t c; |
lynxeyed_atsu | 5:047542b65d00 | 215 | c = getConstantPoolInfo(const_num); |
lynxeyed_atsu | 5:047542b65d00 | 216 | |
lynxeyed_atsu | 5:047542b65d00 | 217 | |
lynxeyed_atsu | 5:047542b65d00 | 218 | switch(c.tag) |
lynxeyed_atsu | 5:047542b65d00 | 219 | { |
lynxeyed_atsu | 5:047542b65d00 | 220 | case CONSTANT_Methodref: |
lynxeyed_atsu | 5:047542b65d00 | 221 | case CONSTANT_Fieldref: |
lynxeyed_atsu | 5:047542b65d00 | 222 | c = getConstantPoolInfo(c.index2); //Constant_NAmeAndType |
lynxeyed_atsu | 5:047542b65d00 | 223 | // do not insert break; here |
lynxeyed_atsu | 5:047542b65d00 | 224 | |
lynxeyed_atsu | 5:047542b65d00 | 225 | case CONSTANT_NameAndType: |
lynxeyed_atsu | 5:047542b65d00 | 226 | c = seekConstClassNumString(c.index); |
lynxeyed_atsu | 5:047542b65d00 | 227 | break; |
lynxeyed_atsu | 5:047542b65d00 | 228 | |
lynxeyed_atsu | 5:047542b65d00 | 229 | default: |
lynxeyed_atsu | 5:047542b65d00 | 230 | break; // in fact,we must program in error process but tedious;-P |
lynxeyed_atsu | 5:047542b65d00 | 231 | } |
lynxeyed_atsu | 5:047542b65d00 | 232 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 233 | } |
lynxeyed_atsu | 5:047542b65d00 | 234 | |
lynxeyed_atsu | 5:047542b65d00 | 235 | |
lynxeyed_atsu | 5:047542b65d00 | 236 | // For NameAndType:descriptor |
lynxeyed_atsu | 5:047542b65d00 | 237 | const_pool_t seekNameAndType_desc(int const_num){ |
lynxeyed_atsu | 5:047542b65d00 | 238 | const_pool_t c; |
lynxeyed_atsu | 5:047542b65d00 | 239 | c = getConstantPoolInfo(const_num); |
lynxeyed_atsu | 5:047542b65d00 | 240 | |
lynxeyed_atsu | 5:047542b65d00 | 241 | switch(c.tag) |
lynxeyed_atsu | 5:047542b65d00 | 242 | { |
lynxeyed_atsu | 5:047542b65d00 | 243 | case CONSTANT_Methodref: |
lynxeyed_atsu | 5:047542b65d00 | 244 | case CONSTANT_Fieldref: |
lynxeyed_atsu | 5:047542b65d00 | 245 | c = getConstantPoolInfo(c.index2); // Constant_NAmeAndType |
lynxeyed_atsu | 5:047542b65d00 | 246 | c = seekConstClassNumString(c.index2); |
lynxeyed_atsu | 5:047542b65d00 | 247 | break; |
lynxeyed_atsu | 5:047542b65d00 | 248 | |
lynxeyed_atsu | 5:047542b65d00 | 249 | case CONSTANT_NameAndType: |
lynxeyed_atsu | 5:047542b65d00 | 250 | c = seekConstClassNumString(c.index2); |
lynxeyed_atsu | 5:047542b65d00 | 251 | break; |
lynxeyed_atsu | 5:047542b65d00 | 252 | |
lynxeyed_atsu | 5:047542b65d00 | 253 | default: |
lynxeyed_atsu | 5:047542b65d00 | 254 | break; |
lynxeyed_atsu | 5:047542b65d00 | 255 | |
lynxeyed_atsu | 5:047542b65d00 | 256 | } |
lynxeyed_atsu | 5:047542b65d00 | 257 | return c; |
lynxeyed_atsu | 5:047542b65d00 | 258 | } |
lynxeyed_atsu | 5:047542b65d00 | 259 | |
lynxeyed_atsu | 5:047542b65d00 | 260 | class_st seekCodeArrtibute(class_st cl, char* method_name,int strlen){ |
lynxeyed_atsu | 5:047542b65d00 | 261 | |
lynxeyed_atsu | 5:047542b65d00 | 262 | int i,j; |
lynxeyed_atsu | 5:047542b65d00 | 263 | int length,cmp; |
lynxeyed_atsu | 5:047542b65d00 | 264 | volatile int pointr; |
lynxeyed_atsu | 5:047542b65d00 | 265 | int attribute_length, attributes_count,fields_count; |
lynxeyed_atsu | 5:047542b65d00 | 266 | const_pool_t z; |
lynxeyed_atsu | 5:047542b65d00 | 267 | cl.code_length = 0; |
lynxeyed_atsu | 5:047542b65d00 | 268 | |
lynxeyed_atsu | 5:047542b65d00 | 269 | |
lynxeyed_atsu | 5:047542b65d00 | 270 | // get length of Constant_pool[last_num] |
lynxeyed_atsu | 5:047542b65d00 | 271 | z = getConstantPoolInfo(total_const_pool_num - 1); |
lynxeyed_atsu | 5:047542b65d00 | 272 | |
lynxeyed_atsu | 5:047542b65d00 | 273 | if(z.tag == CONSTANT_Utf8) length = z.index + 3; else length = getLengthOfConstantInfo(z.tag); |
lynxeyed_atsu | 5:047542b65d00 | 274 | |
lynxeyed_atsu | 5:047542b65d00 | 275 | pointr = z.bc_num + length; |
lynxeyed_atsu | 5:047542b65d00 | 276 | |
lynxeyed_atsu | 5:047542b65d00 | 277 | // seek pointr to interfaces_count |
lynxeyed_atsu | 5:047542b65d00 | 278 | pointr += |
lynxeyed_atsu | 5:047542b65d00 | 279 | + 2 // access_flag |
lynxeyed_atsu | 5:047542b65d00 | 280 | + 2 // this_class |
lynxeyed_atsu | 5:047542b65d00 | 281 | + 2 // super_class |
lynxeyed_atsu | 5:047542b65d00 | 282 | ; |
lynxeyed_atsu | 5:047542b65d00 | 283 | |
lynxeyed_atsu | 5:047542b65d00 | 284 | pointr += (((*bc_seek(pointr,1) << 8) + *bc_seek(pointr + 1,1)) * 2) + 2; // 2 = length of 'interfaces_count' |
lynxeyed_atsu | 5:047542b65d00 | 285 | |
lynxeyed_atsu | 5:047542b65d00 | 286 | // now, pointr's locate is fields_count(2bytes) |
lynxeyed_atsu | 5:047542b65d00 | 287 | fields_count = ((*bc_seek(pointr,1) << 8) + *bc_seek(pointr + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 288 | pointr += 2; // fields_count |
lynxeyed_atsu | 5:047542b65d00 | 289 | |
lynxeyed_atsu | 5:047542b65d00 | 290 | if(fields_count != 0){ |
lynxeyed_atsu | 5:047542b65d00 | 291 | // fields info |
lynxeyed_atsu | 5:047542b65d00 | 292 | for(i = 0 ; i < fields_count ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 293 | pointr += |
lynxeyed_atsu | 5:047542b65d00 | 294 | + 2 // access_flags |
lynxeyed_atsu | 5:047542b65d00 | 295 | + 2 // name_index |
lynxeyed_atsu | 5:047542b65d00 | 296 | + 2 // descriptor_index |
lynxeyed_atsu | 5:047542b65d00 | 297 | ; |
lynxeyed_atsu | 5:047542b65d00 | 298 | attributes_count = (*bc_seek(pointr,1) << 8) + *bc_seek(pointr + 1,1); |
lynxeyed_atsu | 5:047542b65d00 | 299 | pointr += 2; |
lynxeyed_atsu | 5:047542b65d00 | 300 | pointr += attributes_count; |
lynxeyed_atsu | 5:047542b65d00 | 301 | } |
lynxeyed_atsu | 5:047542b65d00 | 302 | } |
lynxeyed_atsu | 5:047542b65d00 | 303 | |
lynxeyed_atsu | 5:047542b65d00 | 304 | // now, pointr's locate is methods_count(2bytes) |
lynxeyed_atsu | 5:047542b65d00 | 305 | methods_count = ((*bc_seek(pointr,1) << 8) + *bc_seek(pointr + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 306 | pointr += 2; // method info (access_flag) |
lynxeyed_atsu | 5:047542b65d00 | 307 | for(i = 0 ; i < methods_count; i++) |
lynxeyed_atsu | 5:047542b65d00 | 308 | { |
lynxeyed_atsu | 5:047542b65d00 | 309 | |
lynxeyed_atsu | 5:047542b65d00 | 310 | pointr += 2; // name_index |
lynxeyed_atsu | 5:047542b65d00 | 311 | |
lynxeyed_atsu | 5:047542b65d00 | 312 | z = getConstantPoolInfo((*bc_seek(pointr,1)<<8)+*bc_seek(pointr + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 313 | cmp = strncmp((const char*)z.stack_pt,method_name,strlen); |
lynxeyed_atsu | 5:047542b65d00 | 314 | |
lynxeyed_atsu | 5:047542b65d00 | 315 | pointr += 2; //descriptor_index; |
lynxeyed_atsu | 5:047542b65d00 | 316 | pointr += 2; |
lynxeyed_atsu | 5:047542b65d00 | 317 | |
lynxeyed_atsu | 5:047542b65d00 | 318 | attributes_count = ((*bc_seek(pointr,1) << 8) + *bc_seek(pointr + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 319 | pointr += 2; //attributes_count; |
lynxeyed_atsu | 5:047542b65d00 | 320 | |
lynxeyed_atsu | 5:047542b65d00 | 321 | for(j = 0 ; j < attributes_count ; j++) |
lynxeyed_atsu | 5:047542b65d00 | 322 | { |
lynxeyed_atsu | 5:047542b65d00 | 323 | pointr = pointr + 2; //attribute_name_index |
lynxeyed_atsu | 5:047542b65d00 | 324 | attribute_length = (*bc_seek(pointr,1) << 24) |
lynxeyed_atsu | 5:047542b65d00 | 325 | + (*bc_seek(pointr + 1,1) << 16) |
lynxeyed_atsu | 5:047542b65d00 | 326 | + (*bc_seek(pointr + 2,1) << 8) |
lynxeyed_atsu | 5:047542b65d00 | 327 | + *bc_seek(pointr + 3,1) |
lynxeyed_atsu | 5:047542b65d00 | 328 | ; |
lynxeyed_atsu | 5:047542b65d00 | 329 | |
lynxeyed_atsu | 5:047542b65d00 | 330 | pointr += 4; //attribute_length |
lynxeyed_atsu | 5:047542b65d00 | 331 | if(!cmp) |
lynxeyed_atsu | 5:047542b65d00 | 332 | { |
lynxeyed_atsu | 5:047542b65d00 | 333 | cl.stack_num = (*bc_seek(pointr,1) << 8) + *bc_seek(pointr+1,1); |
lynxeyed_atsu | 5:047542b65d00 | 334 | cl.local_num = (*bc_seek(pointr+2,1) << 8) + *bc_seek(pointr+3,1); |
lynxeyed_atsu | 5:047542b65d00 | 335 | cl.code_length = (*bc_seek(pointr + 4,1) << 24) |
lynxeyed_atsu | 5:047542b65d00 | 336 | + (*bc_seek(pointr + 5,1) << 16) |
lynxeyed_atsu | 5:047542b65d00 | 337 | + (*bc_seek(pointr + 6,1) << 8) |
lynxeyed_atsu | 5:047542b65d00 | 338 | + *bc_seek(pointr + 7,1) |
lynxeyed_atsu | 5:047542b65d00 | 339 | ; |
lynxeyed_atsu | 5:047542b65d00 | 340 | cl.bc_offset = pointr + 8; |
lynxeyed_atsu | 5:047542b65d00 | 341 | cl.code_offset = 0; |
lynxeyed_atsu | 5:047542b65d00 | 342 | } |
lynxeyed_atsu | 5:047542b65d00 | 343 | pointr += attribute_length; // code attribute |
lynxeyed_atsu | 5:047542b65d00 | 344 | } |
lynxeyed_atsu | 5:047542b65d00 | 345 | } |
lynxeyed_atsu | 5:047542b65d00 | 346 | |
lynxeyed_atsu | 5:047542b65d00 | 347 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 348 | } |
lynxeyed_atsu | 5:047542b65d00 | 349 | |
lynxeyed_atsu | 5:047542b65d00 | 350 | char* getStringFromOperandStack(class_st cl) |
lynxeyed_atsu | 5:047542b65d00 | 351 | { |
lynxeyed_atsu | 5:047542b65d00 | 352 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 353 | const_pool_t cp; |
lynxeyed_atsu | 5:047542b65d00 | 354 | |
lynxeyed_atsu | 5:047542b65d00 | 355 | for(i = 0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 356 | if(cl.op_stack_type[i]==0){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 357 | if(i == 0)return NULL; // return NULL if operand stack has no stack |
lynxeyed_atsu | 5:047542b65d00 | 358 | break; |
lynxeyed_atsu | 5:047542b65d00 | 359 | } |
lynxeyed_atsu | 5:047542b65d00 | 360 | } |
lynxeyed_atsu | 5:047542b65d00 | 361 | |
lynxeyed_atsu | 5:047542b65d00 | 362 | i = i - 1; |
lynxeyed_atsu | 5:047542b65d00 | 363 | |
lynxeyed_atsu | 5:047542b65d00 | 364 | if(cl.op_stack_type[i] == Stack_ConstantPool){ |
lynxeyed_atsu | 5:047542b65d00 | 365 | cp = seekConstClassNumString(cl.op_stack[i]); |
lynxeyed_atsu | 5:047542b65d00 | 366 | cl.op_stack_type[i] = Stack_Nothing; |
lynxeyed_atsu | 5:047542b65d00 | 367 | return (char*)cp.stack_pt; |
lynxeyed_atsu | 5:047542b65d00 | 368 | } |
lynxeyed_atsu | 5:047542b65d00 | 369 | if(cl.op_stack_type[i] == Stack_IntType){ |
lynxeyed_atsu | 5:047542b65d00 | 370 | cl.op_stack_type[i] = Stack_Nothing; |
lynxeyed_atsu | 5:047542b65d00 | 371 | sprintf(cp_str,"%d",cl.op_stack[i]); |
lynxeyed_atsu | 5:047542b65d00 | 372 | return cp_str; |
lynxeyed_atsu | 5:047542b65d00 | 373 | } |
lynxeyed_atsu | 5:047542b65d00 | 374 | if(cl.op_stack_type[i] == Stack_CharType){ |
lynxeyed_atsu | 5:047542b65d00 | 375 | cl.op_stack_type[i] = Stack_Nothing; |
lynxeyed_atsu | 5:047542b65d00 | 376 | sprintf(cp_str,"%c",(char)cl.op_stack[i]); |
lynxeyed_atsu | 5:047542b65d00 | 377 | return cp_str; |
lynxeyed_atsu | 5:047542b65d00 | 378 | } |
lynxeyed_atsu | 5:047542b65d00 | 379 | |
lynxeyed_atsu | 5:047542b65d00 | 380 | return NULL; |
lynxeyed_atsu | 5:047542b65d00 | 381 | } |
lynxeyed_atsu | 5:047542b65d00 | 382 | |
lynxeyed_atsu | 5:047542b65d00 | 383 | int getIntegerFromOperandStack(class_st cl){ |
lynxeyed_atsu | 5:047542b65d00 | 384 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 385 | const_pool_t cp; |
lynxeyed_atsu | 5:047542b65d00 | 386 | |
lynxeyed_atsu | 5:047542b65d00 | 387 | for(i = 0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 388 | if(cl.op_stack_type[i]==0){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 389 | if(i == 0)return 0; |
lynxeyed_atsu | 5:047542b65d00 | 390 | break; |
lynxeyed_atsu | 5:047542b65d00 | 391 | } |
lynxeyed_atsu | 5:047542b65d00 | 392 | } |
lynxeyed_atsu | 5:047542b65d00 | 393 | i = i - 1; |
lynxeyed_atsu | 5:047542b65d00 | 394 | |
lynxeyed_atsu | 5:047542b65d00 | 395 | if(cl.op_stack_type[i] == Stack_ConstantPool){ // Const_Integer |
lynxeyed_atsu | 5:047542b65d00 | 396 | cp = seekConstClassNumString(cl.op_stack[i]); |
lynxeyed_atsu | 5:047542b65d00 | 397 | cl.op_stack_type[i] = 0; |
lynxeyed_atsu | 5:047542b65d00 | 398 | if((cp.tag == CONSTANT_Long)||(cp.tag == CONSTANT_Double)) |
lynxeyed_atsu | 5:047542b65d00 | 399 | return (cp.index2); |
lynxeyed_atsu | 5:047542b65d00 | 400 | else |
lynxeyed_atsu | 5:047542b65d00 | 401 | return (cp.index); |
lynxeyed_atsu | 5:047542b65d00 | 402 | } |
lynxeyed_atsu | 5:047542b65d00 | 403 | |
lynxeyed_atsu | 5:047542b65d00 | 404 | if(cl.op_stack_type[i] == Stack_IntType){ // stack direct |
lynxeyed_atsu | 5:047542b65d00 | 405 | cl.op_stack_type[i] = 0; |
lynxeyed_atsu | 5:047542b65d00 | 406 | return cl.op_stack[i]; |
lynxeyed_atsu | 5:047542b65d00 | 407 | } |
lynxeyed_atsu | 5:047542b65d00 | 408 | return 0; |
lynxeyed_atsu | 5:047542b65d00 | 409 | } |
lynxeyed_atsu | 5:047542b65d00 | 410 | |
lynxeyed_atsu | 5:047542b65d00 | 411 | class_st setStackFromConstantPool(class_st cl, int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 412 | |
lynxeyed_atsu | 5:047542b65d00 | 413 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 414 | for(i=0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 415 | if(cl.op_stack_type[i]==0){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 416 | break; |
lynxeyed_atsu | 5:047542b65d00 | 417 | } |
lynxeyed_atsu | 5:047542b65d00 | 418 | } |
lynxeyed_atsu | 5:047542b65d00 | 419 | |
lynxeyed_atsu | 5:047542b65d00 | 420 | if((i == cl.stack_num - 1)&&(cl.op_stack_type[i] != 0)) |
lynxeyed_atsu | 5:047542b65d00 | 421 | return cl;// stack full |
lynxeyed_atsu | 5:047542b65d00 | 422 | |
lynxeyed_atsu | 5:047542b65d00 | 423 | cl.op_stack_type[i] = Stack_ConstantPool; |
lynxeyed_atsu | 5:047542b65d00 | 424 | cl.op_stack[i] = cp_num; |
lynxeyed_atsu | 5:047542b65d00 | 425 | i = i + 1; |
lynxeyed_atsu | 5:047542b65d00 | 426 | if(i == cl.stack_num) |
lynxeyed_atsu | 5:047542b65d00 | 427 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 428 | else |
lynxeyed_atsu | 5:047542b65d00 | 429 | cl.op_stack_type[i] = Stack_Nothing; |
lynxeyed_atsu | 5:047542b65d00 | 430 | |
lynxeyed_atsu | 5:047542b65d00 | 431 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 432 | } |
lynxeyed_atsu | 5:047542b65d00 | 433 | |
lynxeyed_atsu | 5:047542b65d00 | 434 | class_st changeStackType(class_st cl, int type){ |
lynxeyed_atsu | 5:047542b65d00 | 435 | |
lynxeyed_atsu | 5:047542b65d00 | 436 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 437 | for(i=0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 438 | if(cl.op_stack_type[i]==0){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 439 | break; |
lynxeyed_atsu | 5:047542b65d00 | 440 | } |
lynxeyed_atsu | 5:047542b65d00 | 441 | } |
lynxeyed_atsu | 5:047542b65d00 | 442 | |
lynxeyed_atsu | 5:047542b65d00 | 443 | i = i - 1; |
lynxeyed_atsu | 5:047542b65d00 | 444 | |
lynxeyed_atsu | 5:047542b65d00 | 445 | cl.op_stack_type[i] = type; |
lynxeyed_atsu | 5:047542b65d00 | 446 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 447 | } |
lynxeyed_atsu | 5:047542b65d00 | 448 | |
lynxeyed_atsu | 5:047542b65d00 | 449 | |
lynxeyed_atsu | 5:047542b65d00 | 450 | class_st setIntegerToStack(class_st cl, int num){ |
lynxeyed_atsu | 5:047542b65d00 | 451 | |
lynxeyed_atsu | 5:047542b65d00 | 452 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 453 | for(i = 0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 454 | if(cl.op_stack_type[i]==0){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 455 | break; |
lynxeyed_atsu | 5:047542b65d00 | 456 | } |
lynxeyed_atsu | 5:047542b65d00 | 457 | } |
lynxeyed_atsu | 5:047542b65d00 | 458 | |
lynxeyed_atsu | 5:047542b65d00 | 459 | if((i == cl.stack_num - 1)&&(cl.op_stack_type[i]!=0)) |
lynxeyed_atsu | 5:047542b65d00 | 460 | return cl;// stack full |
lynxeyed_atsu | 5:047542b65d00 | 461 | |
lynxeyed_atsu | 5:047542b65d00 | 462 | cl.op_stack_type[i] = Stack_IntType; |
lynxeyed_atsu | 5:047542b65d00 | 463 | cl.op_stack[i] = num; |
lynxeyed_atsu | 5:047542b65d00 | 464 | i = i + 1; |
lynxeyed_atsu | 5:047542b65d00 | 465 | if(i == cl.stack_num) |
lynxeyed_atsu | 5:047542b65d00 | 466 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 467 | else |
lynxeyed_atsu | 5:047542b65d00 | 468 | cl.op_stack_type[i] = Stack_Nothing; |
lynxeyed_atsu | 5:047542b65d00 | 469 | |
lynxeyed_atsu | 5:047542b65d00 | 470 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 471 | } |
lynxeyed_atsu | 5:047542b65d00 | 472 | |
lynxeyed_atsu | 5:047542b65d00 | 473 | class_st putField(class_st cl, int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 474 | |
lynxeyed_atsu | 5:047542b65d00 | 475 | |
lynxeyed_atsu | 5:047542b65d00 | 476 | if(strncmp((const char *)seekNameAndType_desc(cp_num).stack_pt,"I",1) == 0){ // int type |
lynxeyed_atsu | 5:047542b65d00 | 477 | if(cl.field_num == 0){ // first mem alloc |
lynxeyed_atsu | 5:047542b65d00 | 478 | |
lynxeyed_atsu | 5:047542b65d00 | 479 | cl.field_mem_reg = (int *)pool_alloc(sizeof(int) * 1); |
lynxeyed_atsu | 5:047542b65d00 | 480 | cl.field_mem_type = (int *)pool_alloc(sizeof(int) * 1); |
lynxeyed_atsu | 5:047542b65d00 | 481 | cl.field_num = cl.field_num + 1; |
lynxeyed_atsu | 5:047542b65d00 | 482 | |
lynxeyed_atsu | 5:047542b65d00 | 483 | cl.field_mem_reg[0] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 484 | cl.field_mem_type[0] = cp_num; |
lynxeyed_atsu | 5:047542b65d00 | 485 | |
lynxeyed_atsu | 5:047542b65d00 | 486 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 487 | } |
lynxeyed_atsu | 5:047542b65d00 | 488 | else{ // field_num != 0 |
lynxeyed_atsu | 5:047542b65d00 | 489 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 490 | for(i = 0; i < cl.field_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 491 | if(cl.field_mem_type[i] == cp_num){ //already exists |
lynxeyed_atsu | 5:047542b65d00 | 492 | cl.field_mem_reg[i] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 493 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 494 | } |
lynxeyed_atsu | 5:047542b65d00 | 495 | }// |
lynxeyed_atsu | 5:047542b65d00 | 496 | cl.field_num = cl.field_num + 1; |
lynxeyed_atsu | 5:047542b65d00 | 497 | cl.field_mem_reg = (int *)pool_realloc(cl.field_mem_reg, sizeof(int) * (cl.field_num)); |
lynxeyed_atsu | 5:047542b65d00 | 498 | cl.field_mem_type = (int *)pool_realloc(cl.field_mem_type, sizeof(int) * (cl.field_num)); |
lynxeyed_atsu | 5:047542b65d00 | 499 | |
lynxeyed_atsu | 5:047542b65d00 | 500 | cl.field_mem_reg[cl.field_num - 1] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 501 | cl.field_mem_type[cl.field_num - 1] = cp_num; |
lynxeyed_atsu | 5:047542b65d00 | 502 | |
lynxeyed_atsu | 5:047542b65d00 | 503 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 504 | } |
lynxeyed_atsu | 5:047542b65d00 | 505 | } |
lynxeyed_atsu | 5:047542b65d00 | 506 | |
lynxeyed_atsu | 5:047542b65d00 | 507 | //memcpy(&cl.field_mem_reg, &s,strlen(s)); |
lynxeyed_atsu | 5:047542b65d00 | 508 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 509 | } |
lynxeyed_atsu | 5:047542b65d00 | 510 | |
lynxeyed_atsu | 5:047542b65d00 | 511 | class_st getField(class_st cl,int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 512 | |
lynxeyed_atsu | 5:047542b65d00 | 513 | if(strncmp((const char *)seekNameAndType_desc(cp_num).stack_pt,"I",1) == 0){ // int type |
lynxeyed_atsu | 5:047542b65d00 | 514 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 515 | for(i = 0; i < cl.field_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 516 | if(cl.field_mem_type[i] == cp_num){ //already exists |
lynxeyed_atsu | 5:047542b65d00 | 517 | cl = setIntegerToStack(cl,cl.field_mem_reg[i]); |
lynxeyed_atsu | 5:047542b65d00 | 518 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 519 | } |
lynxeyed_atsu | 5:047542b65d00 | 520 | } |
lynxeyed_atsu | 5:047542b65d00 | 521 | char s[32]; |
lynxeyed_atsu | 5:047542b65d00 | 522 | sprintf(s,"Fnum=%d,memtype=%d,reg=%d\r\n",cl.field_num,cl.field_mem_type[0],cl.field_mem_reg[0]); |
lynxeyed_atsu | 5:047542b65d00 | 523 | uart_print(s); |
lynxeyed_atsu | 5:047542b65d00 | 524 | uart_print("getField error\r\n"); |
lynxeyed_atsu | 5:047542b65d00 | 525 | } |
lynxeyed_atsu | 5:047542b65d00 | 526 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 527 | } |
lynxeyed_atsu | 5:047542b65d00 | 528 | |
lynxeyed_atsu | 5:047542b65d00 | 529 | |
lynxeyed_atsu | 5:047542b65d00 | 530 | class_st decodeVM(class_st cl){ |
lynxeyed_atsu | 5:047542b65d00 | 531 | |
lynxeyed_atsu | 5:047542b65d00 | 532 | int now_code = 0; |
lynxeyed_atsu | 5:047542b65d00 | 533 | |
lynxeyed_atsu | 5:047542b65d00 | 534 | |
lynxeyed_atsu | 5:047542b65d00 | 535 | now_code = cl.bc_offset + cl.code_offset; |
lynxeyed_atsu | 5:047542b65d00 | 536 | switch(*bc_seek(now_code,1)){ |
lynxeyed_atsu | 5:047542b65d00 | 537 | case JAVA_nop: |
lynxeyed_atsu | 5:047542b65d00 | 538 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 539 | break; |
lynxeyed_atsu | 5:047542b65d00 | 540 | |
lynxeyed_atsu | 5:047542b65d00 | 541 | case JAVA_ldc2_w: |
lynxeyed_atsu | 5:047542b65d00 | 542 | cl = setStackFromConstantPool(cl, (*bc_seek(now_code + 1,1)<<8) + *bc_seek(now_code + 2,1)); |
lynxeyed_atsu | 5:047542b65d00 | 543 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 544 | break; |
lynxeyed_atsu | 5:047542b65d00 | 545 | case JAVA_ldc: |
lynxeyed_atsu | 5:047542b65d00 | 546 | cl = setStackFromConstantPool(cl, *bc_seek(now_code + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 547 | now_code = now_code + 2; |
lynxeyed_atsu | 5:047542b65d00 | 548 | break; |
lynxeyed_atsu | 5:047542b65d00 | 549 | |
lynxeyed_atsu | 5:047542b65d00 | 550 | case JAVA_iconst_0: |
lynxeyed_atsu | 5:047542b65d00 | 551 | cl = setIntegerToStack(cl,0); |
lynxeyed_atsu | 5:047542b65d00 | 552 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 553 | break; |
lynxeyed_atsu | 5:047542b65d00 | 554 | case JAVA_iconst_1: |
lynxeyed_atsu | 5:047542b65d00 | 555 | cl = setIntegerToStack(cl,1); |
lynxeyed_atsu | 5:047542b65d00 | 556 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 557 | break; |
lynxeyed_atsu | 5:047542b65d00 | 558 | case JAVA_iconst_2: |
lynxeyed_atsu | 5:047542b65d00 | 559 | cl = setIntegerToStack(cl,2); |
lynxeyed_atsu | 5:047542b65d00 | 560 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 561 | break; |
lynxeyed_atsu | 5:047542b65d00 | 562 | case JAVA_iconst_3: |
lynxeyed_atsu | 5:047542b65d00 | 563 | cl = setIntegerToStack(cl,3); |
lynxeyed_atsu | 5:047542b65d00 | 564 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 565 | break; |
lynxeyed_atsu | 5:047542b65d00 | 566 | case JAVA_iconst_4: |
lynxeyed_atsu | 5:047542b65d00 | 567 | cl = setIntegerToStack(cl,4); |
lynxeyed_atsu | 5:047542b65d00 | 568 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 569 | break; |
lynxeyed_atsu | 5:047542b65d00 | 570 | case JAVA_iconst_5: |
lynxeyed_atsu | 5:047542b65d00 | 571 | cl = setIntegerToStack(cl,5); |
lynxeyed_atsu | 5:047542b65d00 | 572 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 573 | break; |
lynxeyed_atsu | 5:047542b65d00 | 574 | |
lynxeyed_atsu | 5:047542b65d00 | 575 | case JAVA_aload: |
lynxeyed_atsu | 5:047542b65d00 | 576 | now_code = now_code + 2; |
lynxeyed_atsu | 5:047542b65d00 | 577 | break; |
lynxeyed_atsu | 5:047542b65d00 | 578 | |
lynxeyed_atsu | 5:047542b65d00 | 579 | case JAVA_iload_0: |
lynxeyed_atsu | 5:047542b65d00 | 580 | cl = setIntegerToStack(cl,cl.local_reg[0]); |
lynxeyed_atsu | 5:047542b65d00 | 581 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 582 | break; |
lynxeyed_atsu | 5:047542b65d00 | 583 | case JAVA_iload_1: |
lynxeyed_atsu | 5:047542b65d00 | 584 | cl = setIntegerToStack(cl,cl.local_reg[1]); |
lynxeyed_atsu | 5:047542b65d00 | 585 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 586 | break; |
lynxeyed_atsu | 5:047542b65d00 | 587 | case JAVA_iload_2: |
lynxeyed_atsu | 5:047542b65d00 | 588 | cl = setIntegerToStack(cl,cl.local_reg[2]); |
lynxeyed_atsu | 5:047542b65d00 | 589 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 590 | break; |
lynxeyed_atsu | 5:047542b65d00 | 591 | case JAVA_iload_3: |
lynxeyed_atsu | 5:047542b65d00 | 592 | cl = setIntegerToStack(cl,cl.local_reg[3]); |
lynxeyed_atsu | 5:047542b65d00 | 593 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 594 | break; |
lynxeyed_atsu | 5:047542b65d00 | 595 | |
lynxeyed_atsu | 5:047542b65d00 | 596 | case JAVA_aload_0: |
lynxeyed_atsu | 5:047542b65d00 | 597 | case JAVA_aload_1: |
lynxeyed_atsu | 5:047542b65d00 | 598 | case JAVA_aload_2: |
lynxeyed_atsu | 5:047542b65d00 | 599 | case JAVA_aload_3: |
lynxeyed_atsu | 5:047542b65d00 | 600 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 601 | break; |
lynxeyed_atsu | 5:047542b65d00 | 602 | |
lynxeyed_atsu | 5:047542b65d00 | 603 | case JAVA_astore: |
lynxeyed_atsu | 5:047542b65d00 | 604 | now_code = now_code + 2; |
lynxeyed_atsu | 5:047542b65d00 | 605 | break; |
lynxeyed_atsu | 5:047542b65d00 | 606 | |
lynxeyed_atsu | 5:047542b65d00 | 607 | case JAVA_istore_0: |
lynxeyed_atsu | 5:047542b65d00 | 608 | cl.local_reg[0] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 609 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 610 | break; |
lynxeyed_atsu | 5:047542b65d00 | 611 | case JAVA_istore_1: |
lynxeyed_atsu | 5:047542b65d00 | 612 | cl.local_reg[1] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 613 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 614 | break; |
lynxeyed_atsu | 5:047542b65d00 | 615 | case JAVA_istore_2: |
lynxeyed_atsu | 5:047542b65d00 | 616 | cl.local_reg[2] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 617 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 618 | break; |
lynxeyed_atsu | 5:047542b65d00 | 619 | case JAVA_istore_3: |
lynxeyed_atsu | 5:047542b65d00 | 620 | cl.local_reg[3] = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 621 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 622 | break; |
lynxeyed_atsu | 5:047542b65d00 | 623 | |
lynxeyed_atsu | 5:047542b65d00 | 624 | case JAVA_astore_0: |
lynxeyed_atsu | 5:047542b65d00 | 625 | case JAVA_astore_1: |
lynxeyed_atsu | 5:047542b65d00 | 626 | case JAVA_astore_2: |
lynxeyed_atsu | 5:047542b65d00 | 627 | case JAVA_astore_3: |
lynxeyed_atsu | 5:047542b65d00 | 628 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 629 | break; |
lynxeyed_atsu | 5:047542b65d00 | 630 | |
lynxeyed_atsu | 5:047542b65d00 | 631 | case JAVA_dup: |
lynxeyed_atsu | 5:047542b65d00 | 632 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 633 | break; |
lynxeyed_atsu | 5:047542b65d00 | 634 | |
lynxeyed_atsu | 5:047542b65d00 | 635 | case JAVA_iadd: |
lynxeyed_atsu | 5:047542b65d00 | 636 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 637 | setIntegerToStack(cl,getIntegerFromOperandStack(cl) + getIntegerFromOperandStack(cl)); |
lynxeyed_atsu | 5:047542b65d00 | 638 | break; |
lynxeyed_atsu | 5:047542b65d00 | 639 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 640 | case JAVA_iinc: |
lynxeyed_atsu | 9:4ea7773ea2b0 | 641 | cl.local_reg[*bc_seek(now_code + 1, 1)] = cl.local_reg[*bc_seek(now_code + 1, 1)] + (int8_t)(*bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 9:4ea7773ea2b0 | 642 | now_code = now_code + 3; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 643 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 644 | |
lynxeyed_atsu | 5:047542b65d00 | 645 | case JAVA_getstatic: |
lynxeyed_atsu | 5:047542b65d00 | 646 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 647 | break; |
lynxeyed_atsu | 5:047542b65d00 | 648 | |
lynxeyed_atsu | 5:047542b65d00 | 649 | case JAVA_getfield: |
lynxeyed_atsu | 5:047542b65d00 | 650 | cl = getField(cl,(*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 651 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 652 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 653 | |
lynxeyed_atsu | 5:047542b65d00 | 654 | case JAVA_putfield: |
lynxeyed_atsu | 5:047542b65d00 | 655 | cl = putField(cl,(*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 656 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 657 | break; |
lynxeyed_atsu | 5:047542b65d00 | 658 | |
lynxeyed_atsu | 5:047542b65d00 | 659 | case JAVA_i2l: |
lynxeyed_atsu | 5:047542b65d00 | 660 | //cl = changeStackType(cl, Stack_LongType); |
lynxeyed_atsu | 5:047542b65d00 | 661 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 662 | break; |
lynxeyed_atsu | 5:047542b65d00 | 663 | |
lynxeyed_atsu | 5:047542b65d00 | 664 | case JAVA_i2c: |
lynxeyed_atsu | 5:047542b65d00 | 665 | now_code = now_code + 1; |
lynxeyed_atsu | 5:047542b65d00 | 666 | cl = changeStackType(cl, Stack_CharType); |
lynxeyed_atsu | 5:047542b65d00 | 667 | break; |
lynxeyed_atsu | 5:047542b65d00 | 668 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 669 | case JAVA_if_icmplt: |
lynxeyed_atsu | 9:4ea7773ea2b0 | 670 | if(getIntegerFromOperandStack(cl) > getIntegerFromOperandStack(cl)){ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 671 | now_code = now_code +(int16_t)((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); // goto |
lynxeyed_atsu | 9:4ea7773ea2b0 | 672 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 673 | }else{ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 674 | now_code = now_code + 3; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 675 | } |
lynxeyed_atsu | 9:4ea7773ea2b0 | 676 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 677 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 678 | case JAVA_if_icmpge: |
lynxeyed_atsu | 9:4ea7773ea2b0 | 679 | if(getIntegerFromOperandStack(cl) <= getIntegerFromOperandStack(cl)){ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 680 | now_code = now_code +(int16_t)((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); // goto |
lynxeyed_atsu | 9:4ea7773ea2b0 | 681 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 682 | }else{ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 683 | now_code = now_code + 3; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 684 | } |
lynxeyed_atsu | 9:4ea7773ea2b0 | 685 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 686 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 687 | case JAVA_if_icmpgt: |
lynxeyed_atsu | 9:4ea7773ea2b0 | 688 | if(getIntegerFromOperandStack(cl) < getIntegerFromOperandStack(cl)){ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 689 | now_code = now_code +(int16_t)((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); // goto |
lynxeyed_atsu | 9:4ea7773ea2b0 | 690 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 691 | }else{ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 692 | now_code = now_code + 3; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 693 | } |
lynxeyed_atsu | 9:4ea7773ea2b0 | 694 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 695 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 696 | case JAVA_if_icmple: |
lynxeyed_atsu | 9:4ea7773ea2b0 | 697 | if(getIntegerFromOperandStack(cl) >= getIntegerFromOperandStack(cl)){ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 698 | now_code = now_code +(int16_t)((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); // goto |
lynxeyed_atsu | 9:4ea7773ea2b0 | 699 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 700 | }else{ |
lynxeyed_atsu | 9:4ea7773ea2b0 | 701 | now_code = now_code + 3; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 702 | } |
lynxeyed_atsu | 9:4ea7773ea2b0 | 703 | break; |
lynxeyed_atsu | 9:4ea7773ea2b0 | 704 | |
lynxeyed_atsu | 5:047542b65d00 | 705 | case JAVA_goto: |
lynxeyed_atsu | 5:047542b65d00 | 706 | now_code = now_code +(int16_t)((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 707 | break; |
lynxeyed_atsu | 5:047542b65d00 | 708 | |
lynxeyed_atsu | 5:047542b65d00 | 709 | case JAVA_bipush: |
lynxeyed_atsu | 5:047542b65d00 | 710 | cl = setIntegerToStack(cl,*bc_seek(now_code + 1,1)); |
lynxeyed_atsu | 5:047542b65d00 | 711 | now_code = now_code + 2; |
lynxeyed_atsu | 5:047542b65d00 | 712 | break; |
lynxeyed_atsu | 5:047542b65d00 | 713 | |
lynxeyed_atsu | 5:047542b65d00 | 714 | case JAVA_sipush: |
lynxeyed_atsu | 5:047542b65d00 | 715 | cl = setIntegerToStack(cl,((*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1))); |
lynxeyed_atsu | 5:047542b65d00 | 716 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 717 | break; |
lynxeyed_atsu | 5:047542b65d00 | 718 | |
lynxeyed_atsu | 5:047542b65d00 | 719 | case JAVA_invokevirtual: |
lynxeyed_atsu | 5:047542b65d00 | 720 | cl = invokevirtual_callFunction(cl,(*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 721 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 722 | break; |
lynxeyed_atsu | 5:047542b65d00 | 723 | |
lynxeyed_atsu | 5:047542b65d00 | 724 | case JAVA_invokestatic: |
lynxeyed_atsu | 5:047542b65d00 | 725 | cl = invokestatic_callFunction(cl,(*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 726 | now_code = cl.bc_offset + cl.code_offset; |
lynxeyed_atsu | 5:047542b65d00 | 727 | break; |
lynxeyed_atsu | 5:047542b65d00 | 728 | |
lynxeyed_atsu | 5:047542b65d00 | 729 | case JAVA_invokespecial: |
lynxeyed_atsu | 5:047542b65d00 | 730 | cl = invokespecial_callFunction(cl, (*bc_seek(now_code + 1, 1) << 8) + *bc_seek(now_code + 2, 1)); |
lynxeyed_atsu | 5:047542b65d00 | 731 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 732 | break; |
lynxeyed_atsu | 5:047542b65d00 | 733 | |
lynxeyed_atsu | 5:047542b65d00 | 734 | case JAVA_new: |
lynxeyed_atsu | 5:047542b65d00 | 735 | now_code = now_code + 3; |
lynxeyed_atsu | 5:047542b65d00 | 736 | break; |
lynxeyed_atsu | 5:047542b65d00 | 737 | |
lynxeyed_atsu | 5:047542b65d00 | 738 | case JAVA_return: |
lynxeyed_atsu | 5:047542b65d00 | 739 | cl.code_offset = 0; |
lynxeyed_atsu | 5:047542b65d00 | 740 | cl.threadCommand = Thread_returned; |
lynxeyed_atsu | 5:047542b65d00 | 741 | |
lynxeyed_atsu | 5:047542b65d00 | 742 | default: |
lynxeyed_atsu | 5:047542b65d00 | 743 | break; |
lynxeyed_atsu | 5:047542b65d00 | 744 | } |
lynxeyed_atsu | 5:047542b65d00 | 745 | |
lynxeyed_atsu | 5:047542b65d00 | 746 | cl.code_offset = now_code - cl.bc_offset; |
lynxeyed_atsu | 5:047542b65d00 | 747 | |
lynxeyed_atsu | 5:047542b65d00 | 748 | |
lynxeyed_atsu | 5:047542b65d00 | 749 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 750 | } |
lynxeyed_atsu | 5:047542b65d00 | 751 | |
lynxeyed_atsu | 9:4ea7773ea2b0 | 752 | |
lynxeyed_atsu | 5:047542b65d00 | 753 | class_st invokespecial_callFunction(class_st cl, int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 754 | |
lynxeyed_atsu | 5:047542b65d00 | 755 | char* func_name = (char *)seekNameAndType_name(cp_num).stack_pt; |
lynxeyed_atsu | 5:047542b65d00 | 756 | |
lynxeyed_atsu | 5:047542b65d00 | 757 | if(strncmp(func_name,"<init>",6) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 758 | if(strncmp((const char *)seekNameAndType_desc(cp_num).stack_pt,"(I",2) != 0) {return cl;} |
lynxeyed_atsu | 5:047542b65d00 | 759 | int i; |
lynxeyed_atsu | 5:047542b65d00 | 760 | for(i = 0 ; i < cl.stack_num ; i++){ |
lynxeyed_atsu | 5:047542b65d00 | 761 | if(cl.op_stack_type[i] == Stack_Nothing){ // seek top of stack |
lynxeyed_atsu | 5:047542b65d00 | 762 | if(i == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 763 | cl.threadCommand = Thread_getInitMethod; |
lynxeyed_atsu | 5:047542b65d00 | 764 | |
lynxeyed_atsu | 5:047542b65d00 | 765 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 766 | } |
lynxeyed_atsu | 5:047542b65d00 | 767 | break; |
lynxeyed_atsu | 5:047542b65d00 | 768 | } |
lynxeyed_atsu | 5:047542b65d00 | 769 | } |
lynxeyed_atsu | 5:047542b65d00 | 770 | i = i - 1; |
lynxeyed_atsu | 5:047542b65d00 | 771 | cl.threadCommand = Thread_getInitMethodWithStack; |
lynxeyed_atsu | 5:047542b65d00 | 772 | |
lynxeyed_atsu | 5:047542b65d00 | 773 | cl.threadArg = i; |
lynxeyed_atsu | 5:047542b65d00 | 774 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 775 | } |
lynxeyed_atsu | 5:047542b65d00 | 776 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 777 | } |
lynxeyed_atsu | 5:047542b65d00 | 778 | |
lynxeyed_atsu | 5:047542b65d00 | 779 | class_st invokestatic_callFunction(class_st cl, int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 780 | |
lynxeyed_atsu | 5:047542b65d00 | 781 | char* func_name = (char *)seekNameAndType_name(cp_num).stack_pt; |
lynxeyed_atsu | 5:047542b65d00 | 782 | |
lynxeyed_atsu | 5:047542b65d00 | 783 | if(strncmp(func_name,"sleep",5) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 784 | int hold_time = 0; |
lynxeyed_atsu | 5:047542b65d00 | 785 | hold_time = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 786 | switch(cl.threadCommand){ |
lynxeyed_atsu | 5:047542b65d00 | 787 | case Thread_Active: |
lynxeyed_atsu | 5:047542b65d00 | 788 | cl.threadCommand = Thread_inSleep; |
lynxeyed_atsu | 5:047542b65d00 | 789 | cl.threadArg = time_millis(); |
lynxeyed_atsu | 5:047542b65d00 | 790 | cl = setIntegerToStack(cl,hold_time); |
lynxeyed_atsu | 5:047542b65d00 | 791 | break; |
lynxeyed_atsu | 5:047542b65d00 | 792 | case Thread_inSleep: |
lynxeyed_atsu | 5:047542b65d00 | 793 | if(time_millis() - cl.threadArg >= hold_time){ |
lynxeyed_atsu | 5:047542b65d00 | 794 | cl.threadCommand = Thread_Active; |
lynxeyed_atsu | 5:047542b65d00 | 795 | cl.threadArg = 0; |
lynxeyed_atsu | 5:047542b65d00 | 796 | cl.code_offset = cl.code_offset + 3; |
lynxeyed_atsu | 5:047542b65d00 | 797 | hold_time = 0; |
lynxeyed_atsu | 5:047542b65d00 | 798 | }else{ |
lynxeyed_atsu | 5:047542b65d00 | 799 | cl = setIntegerToStack(cl,hold_time); |
lynxeyed_atsu | 5:047542b65d00 | 800 | } |
lynxeyed_atsu | 5:047542b65d00 | 801 | break; |
lynxeyed_atsu | 5:047542b65d00 | 802 | default: |
lynxeyed_atsu | 5:047542b65d00 | 803 | break; |
lynxeyed_atsu | 5:047542b65d00 | 804 | } |
lynxeyed_atsu | 5:047542b65d00 | 805 | |
lynxeyed_atsu | 5:047542b65d00 | 806 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 807 | }else if(strncmp(func_name,"portWrite",9) == 0){// writePort(bit,value) |
lynxeyed_atsu | 5:047542b65d00 | 808 | int value = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 809 | int bit = getIntegerFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 810 | port_write(0,bit,value); |
lynxeyed_atsu | 5:047542b65d00 | 811 | } |
lynxeyed_atsu | 5:047542b65d00 | 812 | cl.code_offset = cl.code_offset + 3; |
lynxeyed_atsu | 5:047542b65d00 | 813 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 814 | } |
lynxeyed_atsu | 5:047542b65d00 | 815 | |
lynxeyed_atsu | 5:047542b65d00 | 816 | //user code |
lynxeyed_atsu | 5:047542b65d00 | 817 | class_st invokevirtual_callFunction(class_st cl, int cp_num){ |
lynxeyed_atsu | 5:047542b65d00 | 818 | |
lynxeyed_atsu | 5:047542b65d00 | 819 | char* func_name = (char *)seekNameAndType_name(cp_num).stack_pt; |
lynxeyed_atsu | 5:047542b65d00 | 820 | |
lynxeyed_atsu | 5:047542b65d00 | 821 | if(strncmp(func_name,"println",7) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 822 | char str[64]; |
lynxeyed_atsu | 5:047542b65d00 | 823 | char *strn = getStringFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 824 | sprintf(str,"%s",strn); |
lynxeyed_atsu | 5:047542b65d00 | 825 | uart_print(str); |
lynxeyed_atsu | 5:047542b65d00 | 826 | uart_print("\r\n"); |
lynxeyed_atsu | 5:047542b65d00 | 827 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 828 | }else if(strncmp(func_name,"print",5) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 829 | char str[64]; |
lynxeyed_atsu | 5:047542b65d00 | 830 | char *strn = getStringFromOperandStack(cl); |
lynxeyed_atsu | 5:047542b65d00 | 831 | sprintf(str,"%s",strn); |
lynxeyed_atsu | 5:047542b65d00 | 832 | uart_print(str); |
lynxeyed_atsu | 5:047542b65d00 | 833 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 834 | }else if(strncmp(func_name,"read",4) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 835 | int getInt; |
lynxeyed_atsu | 5:047542b65d00 | 836 | getInt = uart_read(); |
lynxeyed_atsu | 5:047542b65d00 | 837 | cl = setIntegerToStack(cl,getInt); |
lynxeyed_atsu | 5:047542b65d00 | 838 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 839 | }else if(strncmp(func_name,"start",5) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 840 | cl.threadCommand = Thread_getStartMethod; |
lynxeyed_atsu | 5:047542b65d00 | 841 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 842 | }else if(strncmp(func_name,"getName",7) == 0){ |
lynxeyed_atsu | 5:047542b65d00 | 843 | cl = setIntegerToStack(cl, cl.myThreadNum); |
lynxeyed_atsu | 5:047542b65d00 | 844 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 845 | } |
lynxeyed_atsu | 5:047542b65d00 | 846 | |
lynxeyed_atsu | 5:047542b65d00 | 847 | return cl; |
lynxeyed_atsu | 5:047542b65d00 | 848 | } |
lynxeyed_atsu | 5:047542b65d00 | 849 |