/* Append a new buffer and copy content to it. */ static int bc_add(struct bufferchain *bc, const unsigned char *data, ssize_t size) { int ret = 0; ssize_t part = 0; debug2("bc_add: adding %"SSIZE_P" bytes at %"OFF_P, (ssize_p)size, (off_p)(bc->fileoff+bc->size)); if(size >=4) debug4("first bytes: %02x %02x %02x %02x", data[0], data[1], data[2], data[3]); while(size > 0) { /* Try to fill up the last buffer block. */ if(bc->last != NULL && bc->last->size < bc->last->realsize) { part = bc->last->realsize - bc->last->size; if(part > size) part = size; debug2("bc_add: adding %"SSIZE_P" B to existing block %p", (ssize_p)part, (void*)bc->last); memcpy(bc->last->data+bc->last->size, data, part); bc->last->size += part; size -= part; bc->size += part; data += part; } /* If there is still data left, put it into a new buffer block. */ if(size > 0 && (ret = bc_append(bc, size)) != 0) break; } return ret; }
// append a new buffer and copy content to it. static int bc_add( bufferchain_t *bc, const byte *data, mpg_ssize_t size ) { int ret = 0; mpg_ssize_t part = 0; while( size > 0 ) { // try to fill up the last buffer block. if( bc->last != NULL && bc->last->size < bc->last->realsize ) { part = bc->last->realsize - bc->last->size; if( part > size ) part = size; memcpy( bc->last->data + bc->last->size, data, part ); bc->last->size += part; size -= part; bc->size += part; data += part; } // if there is still data left, put it into a new buffer block. if( size > 0 && ( ret = bc_append( bc, size )) != 0 ) break; } return ret; }
/* Append a new buffer and copy content to it. */ static int bc_add(struct bufferchain *bc, const unsigned char *data, ssize_t size) { int ret = 0; if((ret = bc_append(bc, size)) == 0) memcpy(bc->last->data, data, size); return ret; }
/* * main */ void expr_parser(bc_t *bc_src) { // init bc_in = bc_src; bc_out = malloc(sizeof(bc_t)); bc_create(bc_out); byte code = CODE_PEEK(); // // empty! // if (code == kwTYPE_LINE || code == kwTYPE_EOC) { bc_destroy(bc_out); free(bc_out); return; } // // LET|CONST special code // if (code == kwTYPE_CMPOPR) { IP++; if (CODE(IP) != '=') { cev_opr_err(); bc_destroy(bc_out); free(bc_out); return; } else { IP++; cev_add2(kwTYPE_CMPOPR, '='); } } // start code = CODE_PEEK(); while (code != kwTYPE_EOC && code != kwTYPE_LINE && !comp_error) { if (kw_check_evexit(code)) { // separator cev_add1(code); IP++; // add sep. if (code == kwUSE) { cev_add_addr(0); // USE needs 2 ips cev_add_addr(0); IP += (ADDRSZ + ADDRSZ); } else if (code == kwAS) { if (CODE_PEEK() == kwTYPE_SEP) { // OPEN ... AS #1 cev_add1(kwTYPE_SEP); IP++; cev_add1(CODE(IP)); IP++; } } else { if (code == kwTYPE_SEP) { // Normal separator (,;) cev_add1(CODE(IP)); IP++; } } code = CODE_PEEK(); // next continue; } cev_log(); // do it code = CODE_PEEK(); // next } // finish if (bc_out->count) { bc_in->count = 0; bc_append(bc_in, bc_out); } bc_destroy(bc_out); free(bc_out); }
int main(){ JVM_METHOD *main_method; JVM_CLASS *cur_class_file; int int_var, long_var, float_var, double_var, byte_var, char_var, short_var; int i; cur_class_file = bc_new_class("TypeConversion", "asdf.f", NULL, NULL, JVM_ACC_PUBLIC|JVM_ACC_SUPER); bc_add_default_constructor(cur_class_file, JVM_ACC_PUBLIC); main_method = bc_new_method(cur_class_file, "main", "([Ljava/lang/String;)V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); int_var = bc_get_next_local(main_method, jvm_Int); long_var = bc_get_next_local(main_method, jvm_Long); float_var = bc_get_next_local(main_method, jvm_Float); double_var = bc_get_next_local(main_method, jvm_Double); char_var = bc_get_next_local(main_method, jvm_Char); byte_var = bc_get_next_local(main_method, jvm_Byte); short_var = bc_get_next_local(main_method, jvm_Short); /* Widening conversions */ bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2l); bc_gen_store_op(main_method, long_var, jvm_Long); bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2f); bc_gen_store_op(main_method, float_var, jvm_Float); bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2d); bc_gen_store_op(main_method, double_var, jvm_Double); bc_push_long_const(main_method, 10); bc_append(main_method, jvm_l2f); bc_gen_store_op(main_method, float_var, jvm_Float); bc_push_long_const(main_method, 10); bc_append(main_method, jvm_l2d); bc_gen_store_op(main_method, double_var, jvm_Double); bc_push_float_const(main_method, 10.0); bc_append(main_method, jvm_f2d); bc_gen_store_op(main_method, double_var, jvm_Double); /* Narrowing conversions */ bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2b); bc_gen_store_op(main_method, byte_var, jvm_Byte); bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2c); bc_gen_store_op(main_method, char_var, jvm_Char); bc_push_int_const(main_method, 10); bc_append(main_method, jvm_i2s); bc_gen_store_op(main_method, short_var, jvm_Short); bc_push_long_const(main_method, 10); bc_append(main_method, jvm_l2i); bc_gen_store_op(main_method, int_var, jvm_Int); bc_push_float_const(main_method, 10.0); bc_append(main_method, jvm_f2i); bc_gen_store_op(main_method, int_var, jvm_Int); bc_push_float_const(main_method, 10.0); bc_append(main_method, jvm_f2l); bc_gen_store_op(main_method, long_var, jvm_Long); bc_push_double_const(main_method, 10.0); bc_append(main_method, jvm_d2i); bc_gen_store_op(main_method, int_var, jvm_Int); bc_push_double_const(main_method, 10.0); bc_append(main_method, jvm_d2l); bc_gen_store_op(main_method, long_var, jvm_Long); bc_push_double_const(main_method, 10.0); bc_append(main_method, jvm_d2f); bc_gen_store_op(main_method, float_var, jvm_Float); bc_gen_return(main_method); bc_write_class(cur_class_file, "."); bc_free_class(cur_class_file); return 0; }
void gen_bitwise_xor_test(JVM_CLASS *cur_class_file) { JVM_CODE_GRAPH_NODE *int_if, *long_if, *target; int i, i_one, i_two, i_three, l_one, l_two, l_three; JVM_METHOD *bitwise_xor_method; int meth_idx, field_idx; bitwise_xor_method = bc_new_method(cur_class_file, "test_bitwise_xor", "()V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); i_one = bc_get_next_local(bitwise_xor_method, jvm_Int); bc_push_int_const(bitwise_xor_method, 1); bc_gen_store_op(bitwise_xor_method, i_one, jvm_Int); i_two = bc_get_next_local(bitwise_xor_method, jvm_Int); bc_push_int_const(bitwise_xor_method, 2); bc_gen_store_op(bitwise_xor_method, i_two, jvm_Int); i_three = bc_get_next_local(bitwise_xor_method, jvm_Int); bc_push_int_const(bitwise_xor_method, 3); bc_gen_store_op(bitwise_xor_method, i_three, jvm_Int); l_one = bc_get_next_local(bitwise_xor_method, jvm_Long); bc_push_long_const(bitwise_xor_method, 1); bc_gen_store_op(bitwise_xor_method, l_one, jvm_Long); l_two = bc_get_next_local(bitwise_xor_method, jvm_Long); bc_push_long_const(bitwise_xor_method, 2); bc_gen_store_op(bitwise_xor_method, l_two, jvm_Long); l_three = bc_get_next_local(bitwise_xor_method, jvm_Long); bc_push_long_const(bitwise_xor_method, 3); bc_gen_store_op(bitwise_xor_method, l_three, jvm_Long); bc_gen_load_op(bitwise_xor_method, i_one, jvm_Int); bc_gen_load_op(bitwise_xor_method, i_two, jvm_Int); bc_append(bitwise_xor_method, jvm_ixor); bc_gen_load_op(bitwise_xor_method, i_three, jvm_Int); int_if = bc_append(bitwise_xor_method, jvm_if_icmpne); bc_gen_load_op(bitwise_xor_method, l_one, jvm_Long); bc_gen_load_op(bitwise_xor_method, l_two, jvm_Long); bc_append(bitwise_xor_method, jvm_lxor); bc_gen_load_op(bitwise_xor_method, l_three, jvm_Long); bc_append(bitwise_xor_method, jvm_lcmp); long_if = bc_append(bitwise_xor_method, jvm_ifne); /* getstatic (System.out): */ field_idx = bc_new_fieldref(cur_class_file, "java.lang.System", "out", "Ljava.io.PrintStream;"); bc_append(bitwise_xor_method, jvm_getstatic, field_idx); /* load the arg to println(): */ bc_push_string_const(bitwise_xor_method, "Bitwise XOR: OK"); /* now invoke println(): */ meth_idx = bc_new_methodref(cur_class_file, "java.io.PrintStream", "println", "(Ljava.lang.String;)V"); bc_append(bitwise_xor_method, jvm_invokevirtual, meth_idx); bc_gen_return(bitwise_xor_method); target = bc_append(bitwise_xor_method, jvm_getstatic, field_idx); bc_push_string_const(bitwise_xor_method, "Bitwise XOR: Failed"); bc_append(bitwise_xor_method, jvm_invokevirtual, meth_idx); bc_set_branch_target(int_if, target); bc_set_branch_target(long_if, target); bc_gen_return(bitwise_xor_method); }
void gen_add_test(JVM_CLASS *cur_class_file) { JVM_CODE_GRAPH_NODE *int_if, *long_if, *float_if, *double_if, *target; int i, i_one, i_two, l_one, l_two, f_one, f_two, d_one, d_two; JVM_METHOD *add_method; int meth_idx, field_idx; add_method = bc_new_method(cur_class_file, "test_add", "()V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); i_one = bc_get_next_local(add_method, jvm_Int); bc_push_int_const(add_method, 1); bc_gen_store_op(add_method, i_one, jvm_Int); i_two = bc_get_next_local(add_method, jvm_Int); bc_push_int_const(add_method, 2); bc_gen_store_op(add_method, i_two, jvm_Int); l_one = bc_get_next_local(add_method, jvm_Long); bc_push_long_const(add_method, 1); bc_gen_store_op(add_method, l_one, jvm_Long); l_two = bc_get_next_local(add_method, jvm_Long); bc_push_long_const(add_method, 2); bc_gen_store_op(add_method, l_two, jvm_Long); f_one = bc_get_next_local(add_method, jvm_Float); bc_push_float_const(add_method, 1.0); bc_gen_store_op(add_method, f_one, jvm_Float); f_two = bc_get_next_local(add_method, jvm_Float); bc_push_float_const(add_method, 2.0); bc_gen_store_op(add_method, f_two, jvm_Float); d_one = bc_get_next_local(add_method, jvm_Double); bc_push_double_const(add_method, 1.0); bc_gen_store_op(add_method, d_one, jvm_Double); d_two = bc_get_next_local(add_method, jvm_Double); bc_push_double_const(add_method, 2.0); bc_gen_store_op(add_method, d_two, jvm_Double); bc_gen_load_op(add_method, i_one, jvm_Int); bc_gen_load_op(add_method, i_one, jvm_Int); bc_append(add_method, jvm_iadd); bc_gen_load_op(add_method, i_two, jvm_Int); int_if = bc_append(add_method, jvm_if_icmpne); bc_gen_load_op(add_method, l_one, jvm_Long); bc_gen_load_op(add_method, l_one, jvm_Long); bc_append(add_method, jvm_ladd); bc_gen_load_op(add_method, l_two, jvm_Long); bc_append(add_method, jvm_lcmp); long_if = bc_append(add_method, jvm_ifne); bc_gen_load_op(add_method, f_one, jvm_Float); bc_gen_load_op(add_method, f_one, jvm_Float); bc_append(add_method, jvm_fadd); bc_gen_load_op(add_method, f_two, jvm_Float); bc_append(add_method, jvm_fcmpl); float_if = bc_append(add_method, jvm_ifne); bc_gen_load_op(add_method, d_one, jvm_Double); bc_gen_load_op(add_method, d_one, jvm_Double); bc_append(add_method, jvm_dadd); bc_gen_load_op(add_method, d_two, jvm_Double); bc_append(add_method, jvm_dcmpl); double_if = bc_append(add_method, jvm_ifne); /* getstatic (System.out): */ field_idx = bc_new_fieldref(cur_class_file, "java.lang.System", "out", "Ljava.io.PrintStream;"); bc_append(add_method, jvm_getstatic, field_idx); /* load the arg to println(): */ bc_push_string_const(add_method, "Addition: OK"); /* now invoke println(): */ meth_idx = bc_new_methodref(cur_class_file, "java.io.PrintStream", "println", "(Ljava.lang.String;)V"); bc_append(add_method, jvm_invokevirtual, meth_idx); bc_gen_return(add_method); target = bc_append(add_method, jvm_getstatic, field_idx); bc_push_string_const(add_method, "Addition: Failed"); bc_append(add_method, jvm_invokevirtual, meth_idx); bc_set_branch_target(int_if, target); bc_set_branch_target(long_if, target); bc_set_branch_target(float_if, target); bc_set_branch_target(double_if, target); bc_gen_return(add_method); }
void gen_shift_test(JVM_CLASS *cur_class_file) { JVM_CODE_GRAPH_NODE *ishl_if, *lshl_if, *ishr_if, *lshr_if, *iushr_if, *lushr_if, *target; int i, i_one, i_two, l_one, l_two; JVM_METHOD *shift_method; int meth_idx, field_idx; shift_method = bc_new_method(cur_class_file, "test_shift", "()V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); i_one = bc_get_next_local(shift_method, jvm_Int); bc_push_int_const(shift_method, 1); bc_gen_store_op(shift_method, i_one, jvm_Int); i_two = bc_get_next_local(shift_method, jvm_Int); bc_push_int_const(shift_method, 2); bc_gen_store_op(shift_method, i_two, jvm_Int); l_one = bc_get_next_local(shift_method, jvm_Long); bc_push_long_const(shift_method, 1); bc_gen_store_op(shift_method, l_one, jvm_Long); l_two = bc_get_next_local(shift_method, jvm_Long); bc_push_long_const(shift_method, 2); bc_gen_store_op(shift_method, l_two, jvm_Long); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_ishl); bc_gen_load_op(shift_method, i_two, jvm_Int); ishl_if = bc_append(shift_method, jvm_if_icmpne); bc_gen_load_op(shift_method, l_one, jvm_Long); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_lshl); bc_gen_load_op(shift_method, l_two, jvm_Long); bc_append(shift_method, jvm_lcmp); lshl_if = bc_append(shift_method, jvm_ifne); bc_gen_load_op(shift_method, i_two, jvm_Int); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_ishr); bc_gen_load_op(shift_method, i_one, jvm_Int); ishr_if = bc_append(shift_method, jvm_if_icmpne); bc_gen_load_op(shift_method, l_two, jvm_Long); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_lshr); bc_gen_load_op(shift_method, l_one, jvm_Long); bc_append(shift_method, jvm_lcmp); lshr_if = bc_append(shift_method, jvm_ifne); bc_gen_load_op(shift_method, i_two, jvm_Int); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_iushr); bc_gen_load_op(shift_method, i_one, jvm_Int); iushr_if = bc_append(shift_method, jvm_if_icmpne); bc_gen_load_op(shift_method, l_two, jvm_Long); bc_gen_load_op(shift_method, i_one, jvm_Int); bc_append(shift_method, jvm_lushr); bc_gen_load_op(shift_method, l_one, jvm_Long); bc_append(shift_method, jvm_lcmp); lushr_if = bc_append(shift_method, jvm_ifne); /* getstatic (System.out): */ field_idx = bc_new_fieldref(cur_class_file, "java.lang.System", "out", "Ljava.io.PrintStream;"); bc_append(shift_method, jvm_getstatic, field_idx); /* load the arg to println(): */ bc_push_string_const(shift_method, "Shift: OK"); /* now invoke println(): */ meth_idx = bc_new_methodref(cur_class_file, "java.io.PrintStream", "println", "(Ljava.lang.String;)V"); bc_append(shift_method, jvm_invokevirtual, meth_idx); bc_gen_return(shift_method); target = bc_append(shift_method, jvm_getstatic, field_idx); bc_push_string_const(shift_method, "Shift: Failed"); bc_append(shift_method, jvm_invokevirtual, meth_idx); bc_set_branch_target(ishl_if, target); bc_set_branch_target(lshl_if, target); bc_set_branch_target(ishr_if, target); bc_set_branch_target(lshr_if, target); bc_set_branch_target(iushr_if, target); bc_set_branch_target(lushr_if, target); bc_gen_return(shift_method); }
int main(){ JVM_METHOD *main_method; JVM_CLASS *cur_class_file; int meth_idx; cur_class_file = bc_new_class("Arithmetic", "asdf.f", NULL, NULL, JVM_ACC_PUBLIC|JVM_ACC_SUPER); bc_add_default_constructor(cur_class_file, JVM_ACC_PUBLIC); main_method = bc_new_method(cur_class_file, "main", "([Ljava/lang/String;)V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_add", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_sub", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_mul", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_div", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_rem", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_neg", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_shift", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_bitwise_or", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_bitwise_and", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_bitwise_xor", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); meth_idx = bc_new_methodref(cur_class_file, "Arithmetic", "test_comparison", "()V"); bc_append(main_method, jvm_invokestatic, meth_idx); bc_gen_return(main_method); gen_add_test(cur_class_file); gen_sub_test(cur_class_file); gen_mul_test(cur_class_file); gen_div_test(cur_class_file); gen_rem_test(cur_class_file); gen_neg_test(cur_class_file); gen_shift_test(cur_class_file); gen_bitwise_or_test(cur_class_file); gen_bitwise_and_test(cur_class_file); gen_bitwise_xor_test(cur_class_file); gen_comparison_test(cur_class_file); bc_write_class(cur_class_file, "."); bc_free_class(cur_class_file); return 0; }
void gen_comparison_test(JVM_CLASS *cur_class_file) { JVM_CODE_GRAPH_NODE *long_if, *fcmpg_if, *fcmpl_if, *dcmpg_if, *dcmpl_if, *target; int i, l_eleven, l_two, f_eleven, f_two, d_eleven, d_two; JVM_METHOD *compare_method; int meth_idx, field_idx; compare_method = bc_new_method(cur_class_file, "test_comparison", "()V", JVM_ACC_PUBLIC|JVM_ACC_STATIC); l_two = bc_get_next_local(compare_method, jvm_Long); bc_push_long_const(compare_method, 2); bc_gen_store_op(compare_method, l_two, jvm_Long); l_eleven = bc_get_next_local(compare_method, jvm_Long); bc_push_long_const(compare_method, 11); bc_gen_store_op(compare_method, l_eleven, jvm_Long); f_two = bc_get_next_local(compare_method, jvm_Float); bc_push_float_const(compare_method, 2.0); bc_gen_store_op(compare_method, f_two, jvm_Float); f_eleven = bc_get_next_local(compare_method, jvm_Float); bc_push_float_const(compare_method, 11.0); bc_gen_store_op(compare_method, f_eleven, jvm_Float); d_two = bc_get_next_local(compare_method, jvm_Double); bc_push_double_const(compare_method, 2.0); bc_gen_store_op(compare_method, d_two, jvm_Double); d_eleven = bc_get_next_local(compare_method, jvm_Double); bc_push_double_const(compare_method, 11.0); bc_gen_store_op(compare_method, d_eleven, jvm_Double); bc_gen_load_op(compare_method, l_eleven, jvm_Long); bc_gen_load_op(compare_method, l_two, jvm_Long); bc_append(compare_method, jvm_lcmp); long_if = bc_append(compare_method, jvm_ifle); bc_gen_load_op(compare_method, f_eleven, jvm_Float); bc_gen_load_op(compare_method, f_two, jvm_Float); bc_append(compare_method, jvm_fcmpl); fcmpl_if = bc_append(compare_method, jvm_ifle); bc_gen_load_op(compare_method, f_two, jvm_Float); bc_gen_load_op(compare_method, f_eleven, jvm_Float); bc_append(compare_method, jvm_fcmpg); fcmpg_if = bc_append(compare_method, jvm_ifge); bc_gen_load_op(compare_method, d_eleven, jvm_Double); bc_gen_load_op(compare_method, d_two, jvm_Double); bc_append(compare_method, jvm_dcmpl); dcmpl_if = bc_append(compare_method, jvm_ifle); bc_gen_load_op(compare_method, d_two, jvm_Double); bc_gen_load_op(compare_method, d_eleven, jvm_Double); bc_append(compare_method, jvm_dcmpg); dcmpg_if = bc_append(compare_method, jvm_ifge); /* getstatic (System.out): */ field_idx = bc_new_fieldref(cur_class_file, "java.lang.System", "out", "Ljava.io.PrintStream;"); bc_append(compare_method, jvm_getstatic, field_idx); /* load the arg to println(): */ bc_push_string_const(compare_method, "Comparison: OK"); /* now invoke println(): */ meth_idx = bc_new_methodref(cur_class_file, "java.io.PrintStream", "println", "(Ljava.lang.String;)V"); bc_append(compare_method, jvm_invokevirtual, meth_idx); bc_gen_return(compare_method); target = bc_append(compare_method, jvm_getstatic, field_idx); bc_push_string_const(compare_method, "Comparison: Failed"); bc_append(compare_method, jvm_invokevirtual, meth_idx); bc_set_branch_target(long_if, target); bc_set_branch_target(fcmpl_if, target); bc_set_branch_target(fcmpg_if, target); bc_set_branch_target(dcmpl_if, target); bc_set_branch_target(dcmpg_if, target); bc_gen_return(compare_method); }