From b18060e646d6e2270ea426dcdb5214c6eb7d4438 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Thu, 26 Dec 2024 00:02:08 -0600 Subject: [PATCH] replace printf with debugf --- c/class_resolver.c | 24 +-- c/debug_class_file.c | 164 ++++++++--------- c/decode.c | 4 +- c/decode.inc.c | 416 +++++++++++++++++++++---------------------- c/execute.c | 6 +- c/frame.c | 44 ++--- c/hash_table.c | 6 +- c/memory_allocator.c | 28 +-- c/printf_dreamcast.c | 143 +++++++++++++++ c/printf_dreamcast.h | 17 ++ c/printf_hosted.h | 4 +- c/sh7091_scif.cpp | 15 ++ c/sh7091_scif.h | 11 ++ gen_decoder.py | 6 +- 14 files changed, 537 insertions(+), 351 deletions(-) create mode 100644 c/printf_dreamcast.h create mode 100644 c/sh7091_scif.cpp create mode 100644 c/sh7091_scif.h diff --git a/c/class_resolver.c b/c/class_resolver.c index 1e195db..fd4acbb 100644 --- a/c/class_resolver.c +++ b/c/class_resolver.c @@ -64,9 +64,9 @@ static int32_t class_resolver_create_fields_hash_table(struct class_entry * clas u2 name_index = class_file->fields[i].name_index; struct constant * name_constant = &class_file->constant_pool[name_index - 1]; assert(name_constant->tag == CONSTANT_Utf8); - printf("hash table entry for field: "); + debugf("hash table entry for field: "); print_utf8_string(name_constant); - printf("\n"); + debugf("\n"); struct field_info * field_info = &class_file->fields[i]; @@ -105,9 +105,9 @@ static void class_resolver_create_methods_hash_table(struct class_entry * class_ u2 descriptor_index = class_file->methods[i].descriptor_index; struct constant * descriptor_constant = &class_file->constant_pool[descriptor_index - 1]; assert(descriptor_constant->tag == CONSTANT_Utf8); - printf("hash table entry for method: "); + debugf("hash table entry for method: "); print_utf8_string(name_constant); - printf("\n"); + debugf("\n"); hash_table_add2(methods_hash_table_length, methods_hash_table, @@ -199,7 +199,7 @@ struct class_entry * class_resolver_lookup_class(int class_hash_table_length, const uint8_t * class_name, int class_name_length) { - printf("class_resolver_lookup_class: "); + debugf("class_resolver_lookup_class: "); for (int i = 0; i < class_name_length; i++) { fputc(class_name[i], stdout); } fputc('\n', stdout); @@ -218,7 +218,7 @@ struct class_entry * class_resolver_lookup_class_from_class_index(int class_hash int32_t class_index) { if (class_entry->attribute_entry[class_index - 1].class_entry != nullptr) { - printf("class_resolver_lookup_class_from_class_index %d: [cached]\n", class_index); + debugf("class_resolver_lookup_class_from_class_index %d: [cached]\n", class_index); return class_entry->attribute_entry[class_index - 1].class_entry; } @@ -247,7 +247,7 @@ struct field_entry * class_resolver_lookup_field(int fields_hash_table_length, const uint8_t * field_name, int field_name_length) { - printf("class_resolver_lookup_field: "); + debugf("class_resolver_lookup_field: "); for (int i = 0; i < field_name_length; i++) { fputc(field_name[i], stdout); } fputc('\n', stdout); @@ -266,7 +266,7 @@ struct field_entry * class_resolver_lookup_field_from_fieldref_index(int fields_ int fieldref_index) { if (class_entry->attribute_entry[fieldref_index - 1].field_entry != nullptr) { - printf("class_resolver_lookup_method_from_fieldref_index %d: [cached]\n", fieldref_index); + debugf("class_resolver_lookup_method_from_fieldref_index %d: [cached]\n", fieldref_index); return class_entry->attribute_entry[fieldref_index - 1].field_entry; } @@ -301,7 +301,7 @@ struct method_info * class_resolver_lookup_method(int methods_hash_table_length, const uint8_t * method_descriptor, int method_descriptor_length) { - printf("class_resolver_lookup_method: "); + debugf("class_resolver_lookup_method: "); for (int i = 0; i < method_name_length; i++) { fputc(method_name[i], stdout); } fputc(' ', stdout); for (int i = 0; i < method_descriptor_length; i++) { fputc(method_descriptor[i], stdout); } @@ -325,7 +325,7 @@ struct method_info * class_resolver_lookup_method_from_methodref_index(int metho int methodref_index) { if (class_entry->attribute_entry[methodref_index - 1].method_info != nullptr) { - printf("class_resolver_lookup_method_from_methodref_index %d: [cached]\n", methodref_index); + debugf("class_resolver_lookup_method_from_methodref_index %d: [cached]\n", methodref_index); return class_entry->attribute_entry[methodref_index - 1].method_info; } @@ -364,9 +364,9 @@ int32_t * class_resolver_lookup_string(int class_hash_table_length, struct class_entry * class_entry, const int string_index) { - printf("class_resolver_lookup_string: %d\n", string_index); + debugf("class_resolver_lookup_string: %d\n", string_index); if (class_entry->attribute_entry[string_index - 1].string_objectref != nullptr) { - printf("class_resolver_lookup_string: [cached]\n"); + debugf("class_resolver_lookup_string: [cached]\n"); return class_entry->attribute_entry[string_index - 1].string_objectref; } diff --git a/c/debug_class_file.c b/c/debug_class_file.c index 5856bf9..7ceb7de 100644 --- a/c/debug_class_file.c +++ b/c/debug_class_file.c @@ -20,80 +20,80 @@ void print_constant(struct constant * constant) { switch (constant->tag) { case CONSTANT_Class: - printf("CONSTANT_Class name_index=%d\n", + debugf("CONSTANT_Class name_index=%d\n", constant->class.name_index); break; case CONSTANT_Fieldref: - printf("CONSTANT_Fieldref class_index=%d name_and_type_index=%d\n", + debugf("CONSTANT_Fieldref class_index=%d name_and_type_index=%d\n", constant->fieldref.class_index, constant->fieldref.name_and_type_index); break; case CONSTANT_Methodref: - printf("CONSTANT_Methodref class_index=%d name_and_type_index=%d\n", + debugf("CONSTANT_Methodref class_index=%d name_and_type_index=%d\n", constant->methodref.class_index, constant->methodref.name_and_type_index); break; case CONSTANT_InterfaceMethodref: - printf("CONSTANT_InterfaceMethodref class_index=%d name_and_type_index=%d\n", + debugf("CONSTANT_InterfaceMethodref class_index=%d name_and_type_index=%d\n", constant->interfacemethodref.class_index, constant->interfacemethodref.name_and_type_index); break; case CONSTANT_String: - printf("CONSTANT_String string_index=%d\n", + debugf("CONSTANT_String string_index=%d\n", constant->string.string_index); break; case CONSTANT_Integer: - printf("CONSTANT_Integer bytes=%d\n", + debugf("CONSTANT_Integer bytes=%d\n", constant->integer.bytes); break; case CONSTANT_Float: - printf("CONSTANT_Float bytes=%f\n", + debugf("CONSTANT_Float bytes=%f\n", *(float *)(&constant->_float.bytes)); break; case CONSTANT_Long: - printf("CONSTANT_Long bytes=%" PRId64 "\n", + debugf("CONSTANT_Long bytes=%" PRId64 "\n", constant->_long.bytes); break; case CONSTANT_Double: - printf("CONSTANT_Double bytes=%f\n", + debugf("CONSTANT_Double bytes=%f\n", *(double *)(&constant->_double.bytes)); break; case CONSTANT_NameAndType: - printf("CONSTANT_NameAndType %d %d\n", + debugf("CONSTANT_NameAndType %d %d\n", constant->nameandtype.name_index, constant->nameandtype.descriptor_index); break; case CONSTANT_Utf8: - printf("CONSTANT_Utf8 length=%d bytes=", + debugf("CONSTANT_Utf8 length=%d bytes=", constant->utf8.length); print_utf8_string(constant); - printf("\n"); + debugf("\n"); break; case CONSTANT_MethodHandle: - printf("CONSTANT_MethodHandle reference_kind=%d reference_index=%d\n", + debugf("CONSTANT_MethodHandle reference_kind=%d reference_index=%d\n", constant->methodhandle.reference_kind, constant->methodhandle.reference_index); break; case CONSTANT_MethodType: - printf("CONSTANT_MethodType descriptor_index=%d\n", + debugf("CONSTANT_MethodType descriptor_index=%d\n", constant->methodtype.descriptor_index); break; case CONSTANT_Dynamic: - printf("CONSTANT_Dynamic bootstrap_method_attr_index=%d name_and_type_index=%d\n", + debugf("CONSTANT_Dynamic bootstrap_method_attr_index=%d name_and_type_index=%d\n", constant->dynamic.bootstrap_method_attr_index, constant->dynamic.name_and_type_index); break; case CONSTANT_InvokeDynamic: - printf("CONSTANT_InvokeDynamic bootstrap_method_attr_index=%d name_and_type_index=%d\n", + debugf("CONSTANT_InvokeDynamic bootstrap_method_attr_index=%d name_and_type_index=%d\n", constant->invokedynamic.bootstrap_method_attr_index, constant->invokedynamic.name_and_type_index); break; case CONSTANT_Module: - printf("CONSTANT_Module name_index=%d\n", + debugf("CONSTANT_Module name_index=%d\n", constant->module.name_index); break; case CONSTANT_Package: - printf("CONSTANT_Package name_index=%d\n", + debugf("CONSTANT_Package name_index=%d\n", constant->package.name_index); break; } @@ -102,7 +102,7 @@ void print_constant(struct constant * constant) void print_attribute(const char * indent, struct attribute_info * attribute, struct constant * constant_pool) { fputs(indent, stdout); - printf("attribute_name_index: %d\n", attribute->attribute_name_index); + debugf("attribute_name_index: %d\n", attribute->attribute_name_index); struct constant * attribute_name = &constant_pool[attribute->attribute_name_index - 1]; fputs(indent, stdout); fputs(" ", stdout); @@ -110,7 +110,7 @@ void print_attribute(const char * indent, struct attribute_info * attribute, str if (bytes_equal(attribute_name->utf8.length, attribute_name->utf8.bytes, "ConstantValue")) { fputs(indent, stdout); - printf("constantvalue_index %d\n", attribute->constantvalue->constantvalue_index); + debugf("constantvalue_index %d\n", attribute->constantvalue->constantvalue_index); struct constant * value = &constant_pool[attribute->constantvalue->constantvalue_index - 1]; @@ -125,15 +125,15 @@ void print_attribute(const char * indent, struct attribute_info * attribute, str } else if (bytes_equal(attribute_name->utf8.length, attribute_name->utf8.bytes, "Code")) { // print code fputs(indent, stdout); - printf("max_stack %d\n", attribute->code->max_stack); + debugf("max_stack %d\n", attribute->code->max_stack); fputs(indent, stdout); - printf("max_locals %d\n", attribute->code->max_locals); + debugf("max_locals %d\n", attribute->code->max_locals); fputs(indent, stdout); - printf("code_length %d\n", attribute->code->code_length); + debugf("code_length %d\n", attribute->code->code_length); // dump code fputs(indent, stdout); - printf("code:\n"); + debugf("code:\n"); uint32_t pc = 0; while (pc < attribute->code->code_length) { fputs(indent, stdout); @@ -143,63 +143,63 @@ void print_attribute(const char * indent, struct attribute_info * attribute, str fputs(indent, stdout); - printf("exception_table_length: %d\n", attribute->code->exception_table_length); + debugf("exception_table_length: %d\n", attribute->code->exception_table_length); fputs(indent, stdout); - printf("exceptions:\n"); + debugf("exceptions:\n"); for (int i = 0; i < attribute->code->exception_table_length; i++) { fputs(indent, stdout); - printf(" exception %d:\n", i); + debugf(" exception %d:\n", i); fputs(indent, stdout); - printf(" start_pc: %d\n", attribute->code->exception_table[i].start_pc); + debugf(" start_pc: %d\n", attribute->code->exception_table[i].start_pc); fputs(indent, stdout); - printf(" end_pc: %d\n", attribute->code->exception_table[i].end_pc); + debugf(" end_pc: %d\n", attribute->code->exception_table[i].end_pc); fputs(indent, stdout); - printf(" handler_pc: %d\n", attribute->code->exception_table[i].handler_pc); + debugf(" handler_pc: %d\n", attribute->code->exception_table[i].handler_pc); fputs(indent, stdout); - printf(" catch_type: %d\n", attribute->code->exception_table[i].catch_type); + debugf(" catch_type: %d\n", attribute->code->exception_table[i].catch_type); } fputs(indent, stdout); - printf("attributes_count: %d\n", attribute->code->attributes_count); + debugf("attributes_count: %d\n", attribute->code->attributes_count); fputs(indent, stdout); - printf("attributes:\n"); + debugf("attributes:\n"); for (int i = 0; i < attribute->code->attributes_count; i++) { char indent2[string_length(indent) + 2 + 1]; string_copy(indent2, indent); string_copy(indent2 + string_length(indent), " "); fputs(indent, stdout); - printf(" attribute %d:\n", i); + debugf(" attribute %d:\n", i); print_attribute(indent2, &attribute->code->attributes[i], constant_pool); } } else if (bytes_equal(attribute_name->utf8.length, attribute_name->utf8.bytes, "BootstrapMethods")) { fputs(indent, stdout); - printf("num_bootstrap_methods: %d\n", attribute->bootstrapmethods->num_bootstrap_methods); + debugf("num_bootstrap_methods: %d\n", attribute->bootstrapmethods->num_bootstrap_methods); fputs(indent, stdout); - printf("bootstrap methods:\n"); + debugf("bootstrap methods:\n"); for (int i = 0; i < attribute->bootstrapmethods->num_bootstrap_methods; i++) { fputs(indent, stdout); - printf(" bootstrap_method %d:\n", i); + debugf(" bootstrap_method %d:\n", i); fputs(indent, stdout); - printf(" bootstrap_method_ref: %d\n", attribute->bootstrapmethods->bootstrap_methods[i].bootstrap_method_ref); + debugf(" bootstrap_method_ref: %d\n", attribute->bootstrapmethods->bootstrap_methods[i].bootstrap_method_ref); fputs(indent, stdout); - printf(" num_bootstrap_arguments: %d\n", attribute->bootstrapmethods->bootstrap_methods[i].num_bootstrap_arguments); + debugf(" num_bootstrap_arguments: %d\n", attribute->bootstrapmethods->bootstrap_methods[i].num_bootstrap_arguments); fputs(indent, stdout); - printf(" bootstrap_arguments:\n"); + debugf(" bootstrap_arguments:\n"); for (int j = 0; j < attribute->bootstrapmethods->bootstrap_methods[i].num_bootstrap_arguments; j++) { fputs(indent, stdout); - printf(" bootstrap_argument %d: %d\n", j, attribute->bootstrapmethods->bootstrap_methods[i].bootstrap_arguments[j]); + debugf(" bootstrap_argument %d: %d\n", j, attribute->bootstrapmethods->bootstrap_methods[i].bootstrap_arguments[j]); } } } else if (bytes_equal(attribute_name->utf8.length, attribute_name->utf8.bytes, "NestHost")) { fputs(indent, stdout); - printf("host_class_index: %d\n", attribute->nesthost->host_class_index); + debugf("host_class_index: %d\n", attribute->nesthost->host_class_index); } else if (bytes_equal(attribute_name->utf8.length, attribute_name->utf8.bytes, "NestMembers")) { fputs(indent, stdout); - printf("number_of_classes: %d\n", attribute->nestmembers->number_of_classes); + debugf("number_of_classes: %d\n", attribute->nestmembers->number_of_classes); fputs(indent, stdout); - printf("classes:\n"); + debugf("classes:\n"); for (int i = 0; i < attribute->nestmembers->number_of_classes; i++) { fputs(indent, stdout); - printf(" class %d:\n", i); + debugf(" class %d:\n", i); fputs(indent, stdout); fputs(" ", stdout); print_constant(&constant_pool[attribute->nestmembers->classes[i] - 1]); @@ -213,72 +213,72 @@ void print_attribute(const char * indent, struct attribute_info * attribute, str void print_class_file(struct class_file * class_file) { - printf("magic %08x\n", class_file->magic); - printf("minor_version %d\n", class_file->minor_version); - printf("major_version %d\n", class_file->major_version); - printf("constant_pool_count %d\n", class_file->constant_pool_count); + debugf("magic %08x\n", class_file->magic); + debugf("minor_version %d\n", class_file->minor_version); + debugf("major_version %d\n", class_file->major_version); + debugf("constant_pool_count %d\n", class_file->constant_pool_count); - printf("constants:\n"); + debugf("constants:\n"); for (int i = 0; i < class_file->constant_pool_count - 1; i++) { if (class_file->constant_pool[i].tag != 0) { - printf("% 3d: ", i + 1); + debugf("% 3d: ", i + 1); print_constant(&class_file->constant_pool[i]); } } - printf("access_flags %04x\n", class_file->access_flags); - printf("this_class %d\n", class_file->this_class); - printf("super_class %d\n", class_file->super_class); - printf("interfaces_count %d\n", class_file->interfaces_count); + debugf("access_flags %04x\n", class_file->access_flags); + debugf("this_class %d\n", class_file->this_class); + debugf("super_class %d\n", class_file->super_class); + debugf("interfaces_count %d\n", class_file->interfaces_count); - printf("interfaces:\n"); + debugf("interfaces:\n"); for (int i = 0; i < class_file->interfaces_count; i++) { - printf("% 3d: %d\n", i + 1, class_file->interfaces[i]); + debugf("% 3d: %d\n", i + 1, class_file->interfaces[i]); } - printf("fields_count %d\n", class_file->fields_count); - printf("fields:\n"); + debugf("fields_count %d\n", class_file->fields_count); + debugf("fields:\n"); for (int i = 0; i < class_file->fields_count; i++) { - printf(" field %d:\n", i); - printf(" access_flags %d\n", class_file->fields[i].access_flags); - printf(" name_index %d\n", class_file->fields[i].name_index); - printf(" "); + debugf(" field %d:\n", i); + debugf(" access_flags %d\n", class_file->fields[i].access_flags); + debugf(" name_index %d\n", class_file->fields[i].name_index); + debugf(" "); print_constant(&class_file->constant_pool[class_file->fields[i].name_index - 1]); - printf(" descriptor_index %d\n", class_file->fields[i].descriptor_index); - printf(" "); + debugf(" descriptor_index %d\n", class_file->fields[i].descriptor_index); + debugf(" "); print_constant(&class_file->constant_pool[class_file->fields[i].descriptor_index - 1]); - printf(" attributes_count %d\n", class_file->fields[i].attributes_count); - printf(" attributes:\n"); + debugf(" attributes_count %d\n", class_file->fields[i].attributes_count); + debugf(" attributes:\n"); for (int j = 0; j < class_file->fields[i].attributes_count; j++) { - printf(" attribute %d:\n", j); + debugf(" attribute %d:\n", j); print_attribute(" ", &class_file->fields[i].attributes[j], class_file->constant_pool); } } - printf("methods_count %d\n", class_file->methods_count); - printf("methods:\n"); + debugf("methods_count %d\n", class_file->methods_count); + debugf("methods:\n"); for (int i = 0; i < class_file->methods_count; i++) { - printf(" method %d:\n", i); - printf(" access_flags %04x\n", class_file->methods[i].access_flags); - printf(" name_index %d\n", class_file->methods[i].name_index); - printf(" "); + debugf(" method %d:\n", i); + debugf(" access_flags %04x\n", class_file->methods[i].access_flags); + debugf(" name_index %d\n", class_file->methods[i].name_index); + debugf(" "); print_constant(&class_file->constant_pool[class_file->methods[i].name_index - 1]); - printf(" descriptor_index %d\n", class_file->methods[i].descriptor_index); - printf(" "); + debugf(" descriptor_index %d\n", class_file->methods[i].descriptor_index); + debugf(" "); print_constant(&class_file->constant_pool[class_file->methods[i].descriptor_index - 1]); - printf(" attributes_count %d\n", class_file->methods[i].attributes_count); - printf(" attributes:\n"); + debugf(" attributes_count %d\n", class_file->methods[i].attributes_count); + debugf(" attributes:\n"); for (int j = 0; j < class_file->methods[i].attributes_count; j++) { - printf(" attribute %d:\n", j); + debugf(" attribute %d:\n", j); print_attribute(" ", &class_file->methods[i].attributes[j], class_file->constant_pool); } } - printf("attributes_count %d\n", class_file->attributes_count); - printf("attributes:\n"); + debugf("attributes_count %d\n", class_file->attributes_count); + debugf("attributes:\n"); for (int i = 0; i < class_file->attributes_count; i++) { - printf(" attribute %d:\n", i); + debugf(" attribute %d:\n", i); print_attribute(" ", &class_file->attributes[i], class_file->constant_pool); } } diff --git a/c/decode.c b/c/decode.c index cccaed1..0162701 100644 --- a/c/decode.c +++ b/c/decode.c @@ -74,9 +74,9 @@ static inline int32_t aligned_s4(const void * buf) #define TABLESWITCH_PRINT_ARGS() \ do { \ for (int i = lowbyte; i <= highbyte; i++) { \ - printf(" %d: %d\n", i, aligned_s4(&table[i - lowbyte])); \ + debugf(" %d: %d\n", i, aligned_s4(&table[i - lowbyte])); \ } \ - printf("default: %d\n", defaultbyte); \ + debugf("default: %d\n", defaultbyte); \ } while (0); #define TABLESWITCH_NEXT_PC \ diff --git a/c/decode.inc.c b/c/decode.inc.c index da00461..a347c7c 100644 --- a/c/decode.inc.c +++ b/c/decode.inc.c @@ -3,1091 +3,1091 @@ uint32_t decode_print_instruction(const uint8_t * code, uint32_t pc) switch (code[pc]) { case 0: // nop { - printf("%4d: nop \n", pc); + debugf("%4d: nop \n", pc); return pc + 1; } case 1: // aconst_null { - printf("%4d: aconst_null \n", pc); + debugf("%4d: aconst_null \n", pc); return pc + 1; } case 2: // iconst_m1 { - printf("%4d: iconst_m1 \n", pc); + debugf("%4d: iconst_m1 \n", pc); return pc + 1; } case 3: // iconst_0 { - printf("%4d: iconst_0 \n", pc); + debugf("%4d: iconst_0 \n", pc); return pc + 1; } case 4: // iconst_1 { - printf("%4d: iconst_1 \n", pc); + debugf("%4d: iconst_1 \n", pc); return pc + 1; } case 5: // iconst_2 { - printf("%4d: iconst_2 \n", pc); + debugf("%4d: iconst_2 \n", pc); return pc + 1; } case 6: // iconst_3 { - printf("%4d: iconst_3 \n", pc); + debugf("%4d: iconst_3 \n", pc); return pc + 1; } case 7: // iconst_4 { - printf("%4d: iconst_4 \n", pc); + debugf("%4d: iconst_4 \n", pc); return pc + 1; } case 8: // iconst_5 { - printf("%4d: iconst_5 \n", pc); + debugf("%4d: iconst_5 \n", pc); return pc + 1; } case 9: // lconst_0 { - printf("%4d: lconst_0 \n", pc); + debugf("%4d: lconst_0 \n", pc); return pc + 1; } case 10: // lconst_1 { - printf("%4d: lconst_1 \n", pc); + debugf("%4d: lconst_1 \n", pc); return pc + 1; } case 11: // fconst_0 { - printf("%4d: fconst_0 \n", pc); + debugf("%4d: fconst_0 \n", pc); return pc + 1; } case 12: // fconst_1 { - printf("%4d: fconst_1 \n", pc); + debugf("%4d: fconst_1 \n", pc); return pc + 1; } case 13: // fconst_2 { - printf("%4d: fconst_2 \n", pc); + debugf("%4d: fconst_2 \n", pc); return pc + 1; } case 14: // dconst_0 { - printf("%4d: dconst_0 \n", pc); + debugf("%4d: dconst_0 \n", pc); return pc + 1; } case 15: // dconst_1 { - printf("%4d: dconst_1 \n", pc); + debugf("%4d: dconst_1 \n", pc); return pc + 1; } case 16: // bipush { int32_t byte = _s1(&code[pc + 1]); - printf("%4d: bipush %d\n", pc, byte); + debugf("%4d: bipush %d\n", pc, byte); return pc + 2; } case 17: // sipush { int32_t byte = _s2(&code[pc + 1]); - printf("%4d: sipush %d\n", pc, byte); + debugf("%4d: sipush %d\n", pc, byte); return pc + 3; } case 18: // ldc { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: ldc %u\n", pc, index); + debugf("%4d: ldc %u\n", pc, index); return pc + 2; } case 19: // ldc_w { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: ldc_w %u\n", pc, index); + debugf("%4d: ldc_w %u\n", pc, index); return pc + 3; } case 20: // ldc2_w { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: ldc2_w %u\n", pc, index); + debugf("%4d: ldc2_w %u\n", pc, index); return pc + 3; } case 21: // iload { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: iload %u\n", pc, index); + debugf("%4d: iload %u\n", pc, index); return pc + 2; } case 22: // lload { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: lload %u\n", pc, index); + debugf("%4d: lload %u\n", pc, index); return pc + 2; } case 23: // fload { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: fload %u\n", pc, index); + debugf("%4d: fload %u\n", pc, index); return pc + 2; } case 24: // dload { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: dload %u\n", pc, index); + debugf("%4d: dload %u\n", pc, index); return pc + 2; } case 25: // aload { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: aload %u\n", pc, index); + debugf("%4d: aload %u\n", pc, index); return pc + 2; } case 26: // iload_0 { - printf("%4d: iload_0 \n", pc); + debugf("%4d: iload_0 \n", pc); return pc + 1; } case 27: // iload_1 { - printf("%4d: iload_1 \n", pc); + debugf("%4d: iload_1 \n", pc); return pc + 1; } case 28: // iload_2 { - printf("%4d: iload_2 \n", pc); + debugf("%4d: iload_2 \n", pc); return pc + 1; } case 29: // iload_3 { - printf("%4d: iload_3 \n", pc); + debugf("%4d: iload_3 \n", pc); return pc + 1; } case 30: // lload_0 { - printf("%4d: lload_0 \n", pc); + debugf("%4d: lload_0 \n", pc); return pc + 1; } case 31: // lload_1 { - printf("%4d: lload_1 \n", pc); + debugf("%4d: lload_1 \n", pc); return pc + 1; } case 32: // lload_2 { - printf("%4d: lload_2 \n", pc); + debugf("%4d: lload_2 \n", pc); return pc + 1; } case 33: // lload_3 { - printf("%4d: lload_3 \n", pc); + debugf("%4d: lload_3 \n", pc); return pc + 1; } case 34: // fload_0 { - printf("%4d: fload_0 \n", pc); + debugf("%4d: fload_0 \n", pc); return pc + 1; } case 35: // fload_1 { - printf("%4d: fload_1 \n", pc); + debugf("%4d: fload_1 \n", pc); return pc + 1; } case 36: // fload_2 { - printf("%4d: fload_2 \n", pc); + debugf("%4d: fload_2 \n", pc); return pc + 1; } case 37: // fload_3 { - printf("%4d: fload_3 \n", pc); + debugf("%4d: fload_3 \n", pc); return pc + 1; } case 38: // dload_0 { - printf("%4d: dload_0 \n", pc); + debugf("%4d: dload_0 \n", pc); return pc + 1; } case 39: // dload_1 { - printf("%4d: dload_1 \n", pc); + debugf("%4d: dload_1 \n", pc); return pc + 1; } case 40: // dload_2 { - printf("%4d: dload_2 \n", pc); + debugf("%4d: dload_2 \n", pc); return pc + 1; } case 41: // dload_3 { - printf("%4d: dload_3 \n", pc); + debugf("%4d: dload_3 \n", pc); return pc + 1; } case 42: // aload_0 { - printf("%4d: aload_0 \n", pc); + debugf("%4d: aload_0 \n", pc); return pc + 1; } case 43: // aload_1 { - printf("%4d: aload_1 \n", pc); + debugf("%4d: aload_1 \n", pc); return pc + 1; } case 44: // aload_2 { - printf("%4d: aload_2 \n", pc); + debugf("%4d: aload_2 \n", pc); return pc + 1; } case 45: // aload_3 { - printf("%4d: aload_3 \n", pc); + debugf("%4d: aload_3 \n", pc); return pc + 1; } case 46: // iaload { - printf("%4d: iaload \n", pc); + debugf("%4d: iaload \n", pc); return pc + 1; } case 47: // laload { - printf("%4d: laload \n", pc); + debugf("%4d: laload \n", pc); return pc + 1; } case 48: // faload { - printf("%4d: faload \n", pc); + debugf("%4d: faload \n", pc); return pc + 1; } case 49: // daload { - printf("%4d: daload \n", pc); + debugf("%4d: daload \n", pc); return pc + 1; } case 50: // aaload { - printf("%4d: aaload \n", pc); + debugf("%4d: aaload \n", pc); return pc + 1; } case 51: // baload { - printf("%4d: baload \n", pc); + debugf("%4d: baload \n", pc); return pc + 1; } case 52: // caload { - printf("%4d: caload \n", pc); + debugf("%4d: caload \n", pc); return pc + 1; } case 53: // saload { - printf("%4d: saload \n", pc); + debugf("%4d: saload \n", pc); return pc + 1; } case 54: // istore { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: istore %u\n", pc, index); + debugf("%4d: istore %u\n", pc, index); return pc + 2; } case 55: // lstore { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: lstore %u\n", pc, index); + debugf("%4d: lstore %u\n", pc, index); return pc + 2; } case 56: // fstore { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: fstore %u\n", pc, index); + debugf("%4d: fstore %u\n", pc, index); return pc + 2; } case 57: // dstore { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: dstore %u\n", pc, index); + debugf("%4d: dstore %u\n", pc, index); return pc + 2; } case 58: // astore { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: astore %u\n", pc, index); + debugf("%4d: astore %u\n", pc, index); return pc + 2; } case 59: // istore_0 { - printf("%4d: istore_0 \n", pc); + debugf("%4d: istore_0 \n", pc); return pc + 1; } case 60: // istore_1 { - printf("%4d: istore_1 \n", pc); + debugf("%4d: istore_1 \n", pc); return pc + 1; } case 61: // istore_2 { - printf("%4d: istore_2 \n", pc); + debugf("%4d: istore_2 \n", pc); return pc + 1; } case 62: // istore_3 { - printf("%4d: istore_3 \n", pc); + debugf("%4d: istore_3 \n", pc); return pc + 1; } case 63: // lstore_0 { - printf("%4d: lstore_0 \n", pc); + debugf("%4d: lstore_0 \n", pc); return pc + 1; } case 64: // lstore_1 { - printf("%4d: lstore_1 \n", pc); + debugf("%4d: lstore_1 \n", pc); return pc + 1; } case 65: // lstore_2 { - printf("%4d: lstore_2 \n", pc); + debugf("%4d: lstore_2 \n", pc); return pc + 1; } case 66: // lstore_3 { - printf("%4d: lstore_3 \n", pc); + debugf("%4d: lstore_3 \n", pc); return pc + 1; } case 67: // fstore_0 { - printf("%4d: fstore_0 \n", pc); + debugf("%4d: fstore_0 \n", pc); return pc + 1; } case 68: // fstore_1 { - printf("%4d: fstore_1 \n", pc); + debugf("%4d: fstore_1 \n", pc); return pc + 1; } case 69: // fstore_2 { - printf("%4d: fstore_2 \n", pc); + debugf("%4d: fstore_2 \n", pc); return pc + 1; } case 70: // fstore_3 { - printf("%4d: fstore_3 \n", pc); + debugf("%4d: fstore_3 \n", pc); return pc + 1; } case 71: // dstore_0 { - printf("%4d: dstore_0 \n", pc); + debugf("%4d: dstore_0 \n", pc); return pc + 1; } case 72: // dstore_1 { - printf("%4d: dstore_1 \n", pc); + debugf("%4d: dstore_1 \n", pc); return pc + 1; } case 73: // dstore_2 { - printf("%4d: dstore_2 \n", pc); + debugf("%4d: dstore_2 \n", pc); return pc + 1; } case 74: // dstore_3 { - printf("%4d: dstore_3 \n", pc); + debugf("%4d: dstore_3 \n", pc); return pc + 1; } case 75: // astore_0 { - printf("%4d: astore_0 \n", pc); + debugf("%4d: astore_0 \n", pc); return pc + 1; } case 76: // astore_1 { - printf("%4d: astore_1 \n", pc); + debugf("%4d: astore_1 \n", pc); return pc + 1; } case 77: // astore_2 { - printf("%4d: astore_2 \n", pc); + debugf("%4d: astore_2 \n", pc); return pc + 1; } case 78: // astore_3 { - printf("%4d: astore_3 \n", pc); + debugf("%4d: astore_3 \n", pc); return pc + 1; } case 79: // iastore { - printf("%4d: iastore \n", pc); + debugf("%4d: iastore \n", pc); return pc + 1; } case 80: // lastore { - printf("%4d: lastore \n", pc); + debugf("%4d: lastore \n", pc); return pc + 1; } case 81: // fastore { - printf("%4d: fastore \n", pc); + debugf("%4d: fastore \n", pc); return pc + 1; } case 82: // dastore { - printf("%4d: dastore \n", pc); + debugf("%4d: dastore \n", pc); return pc + 1; } case 83: // aastore { - printf("%4d: aastore \n", pc); + debugf("%4d: aastore \n", pc); return pc + 1; } case 84: // bastore { - printf("%4d: bastore \n", pc); + debugf("%4d: bastore \n", pc); return pc + 1; } case 85: // castore { - printf("%4d: castore \n", pc); + debugf("%4d: castore \n", pc); return pc + 1; } case 86: // sastore { - printf("%4d: sastore \n", pc); + debugf("%4d: sastore \n", pc); return pc + 1; } case 87: // pop { - printf("%4d: pop \n", pc); + debugf("%4d: pop \n", pc); return pc + 1; } case 88: // pop2 { - printf("%4d: pop2 \n", pc); + debugf("%4d: pop2 \n", pc); return pc + 1; } case 89: // dup { - printf("%4d: dup \n", pc); + debugf("%4d: dup \n", pc); return pc + 1; } case 90: // dup_x1 { - printf("%4d: dup_x1 \n", pc); + debugf("%4d: dup_x1 \n", pc); return pc + 1; } case 91: // dup_x2 { - printf("%4d: dup_x2 \n", pc); + debugf("%4d: dup_x2 \n", pc); return pc + 1; } case 92: // dup2 { - printf("%4d: dup2 \n", pc); + debugf("%4d: dup2 \n", pc); return pc + 1; } case 93: // dup2_x1 { - printf("%4d: dup2_x1 \n", pc); + debugf("%4d: dup2_x1 \n", pc); return pc + 1; } case 94: // dup2_x2 { - printf("%4d: dup2_x2 \n", pc); + debugf("%4d: dup2_x2 \n", pc); return pc + 1; } case 95: // swap { - printf("%4d: swap \n", pc); + debugf("%4d: swap \n", pc); return pc + 1; } case 96: // iadd { - printf("%4d: iadd \n", pc); + debugf("%4d: iadd \n", pc); return pc + 1; } case 97: // ladd { - printf("%4d: ladd \n", pc); + debugf("%4d: ladd \n", pc); return pc + 1; } case 98: // fadd { - printf("%4d: fadd \n", pc); + debugf("%4d: fadd \n", pc); return pc + 1; } case 99: // dadd { - printf("%4d: dadd \n", pc); + debugf("%4d: dadd \n", pc); return pc + 1; } case 100: // isub { - printf("%4d: isub \n", pc); + debugf("%4d: isub \n", pc); return pc + 1; } case 101: // lsub { - printf("%4d: lsub \n", pc); + debugf("%4d: lsub \n", pc); return pc + 1; } case 102: // fsub { - printf("%4d: fsub \n", pc); + debugf("%4d: fsub \n", pc); return pc + 1; } case 103: // dsub { - printf("%4d: dsub \n", pc); + debugf("%4d: dsub \n", pc); return pc + 1; } case 104: // imul { - printf("%4d: imul \n", pc); + debugf("%4d: imul \n", pc); return pc + 1; } case 105: // lmul { - printf("%4d: lmul \n", pc); + debugf("%4d: lmul \n", pc); return pc + 1; } case 106: // fmul { - printf("%4d: fmul \n", pc); + debugf("%4d: fmul \n", pc); return pc + 1; } case 107: // dmul { - printf("%4d: dmul \n", pc); + debugf("%4d: dmul \n", pc); return pc + 1; } case 108: // idiv { - printf("%4d: idiv \n", pc); + debugf("%4d: idiv \n", pc); return pc + 1; } case 109: // ldiv { - printf("%4d: ldiv \n", pc); + debugf("%4d: ldiv \n", pc); return pc + 1; } case 110: // fdiv { - printf("%4d: fdiv \n", pc); + debugf("%4d: fdiv \n", pc); return pc + 1; } case 111: // ddiv { - printf("%4d: ddiv \n", pc); + debugf("%4d: ddiv \n", pc); return pc + 1; } case 112: // irem { - printf("%4d: irem \n", pc); + debugf("%4d: irem \n", pc); return pc + 1; } case 113: // lrem { - printf("%4d: lrem \n", pc); + debugf("%4d: lrem \n", pc); return pc + 1; } case 114: // frem { - printf("%4d: frem \n", pc); + debugf("%4d: frem \n", pc); return pc + 1; } case 115: // drem { - printf("%4d: drem \n", pc); + debugf("%4d: drem \n", pc); return pc + 1; } case 116: // ineg { - printf("%4d: ineg \n", pc); + debugf("%4d: ineg \n", pc); return pc + 1; } case 117: // lneg { - printf("%4d: lneg \n", pc); + debugf("%4d: lneg \n", pc); return pc + 1; } case 118: // fneg { - printf("%4d: fneg \n", pc); + debugf("%4d: fneg \n", pc); return pc + 1; } case 119: // dneg { - printf("%4d: dneg \n", pc); + debugf("%4d: dneg \n", pc); return pc + 1; } case 120: // ishl { - printf("%4d: ishl \n", pc); + debugf("%4d: ishl \n", pc); return pc + 1; } case 121: // lshl { - printf("%4d: lshl \n", pc); + debugf("%4d: lshl \n", pc); return pc + 1; } case 122: // ishr { - printf("%4d: ishr \n", pc); + debugf("%4d: ishr \n", pc); return pc + 1; } case 123: // lshr { - printf("%4d: lshr \n", pc); + debugf("%4d: lshr \n", pc); return pc + 1; } case 124: // iushr { - printf("%4d: iushr \n", pc); + debugf("%4d: iushr \n", pc); return pc + 1; } case 125: // lushr { - printf("%4d: lushr \n", pc); + debugf("%4d: lushr \n", pc); return pc + 1; } case 126: // iand { - printf("%4d: iand \n", pc); + debugf("%4d: iand \n", pc); return pc + 1; } case 127: // land { - printf("%4d: land \n", pc); + debugf("%4d: land \n", pc); return pc + 1; } case 128: // ior { - printf("%4d: ior \n", pc); + debugf("%4d: ior \n", pc); return pc + 1; } case 129: // lor { - printf("%4d: lor \n", pc); + debugf("%4d: lor \n", pc); return pc + 1; } case 130: // ixor { - printf("%4d: ixor \n", pc); + debugf("%4d: ixor \n", pc); return pc + 1; } case 131: // lxor { - printf("%4d: lxor \n", pc); + debugf("%4d: lxor \n", pc); return pc + 1; } case 132: // iinc { uint32_t index = _u1(&code[pc + 1]); uint32_t _const = _u1(&code[pc + 2]); - printf("%4d: iinc %u, %u\n", pc, index, _const); + debugf("%4d: iinc %u, %u\n", pc, index, _const); return pc + 3; } case 133: // i2l { - printf("%4d: i2l \n", pc); + debugf("%4d: i2l \n", pc); return pc + 1; } case 134: // i2f { - printf("%4d: i2f \n", pc); + debugf("%4d: i2f \n", pc); return pc + 1; } case 135: // i2d { - printf("%4d: i2d \n", pc); + debugf("%4d: i2d \n", pc); return pc + 1; } case 136: // l2i { - printf("%4d: l2i \n", pc); + debugf("%4d: l2i \n", pc); return pc + 1; } case 137: // l2f { - printf("%4d: l2f \n", pc); + debugf("%4d: l2f \n", pc); return pc + 1; } case 138: // l2d { - printf("%4d: l2d \n", pc); + debugf("%4d: l2d \n", pc); return pc + 1; } case 139: // f2i { - printf("%4d: f2i \n", pc); + debugf("%4d: f2i \n", pc); return pc + 1; } case 140: // f2l { - printf("%4d: f2l \n", pc); + debugf("%4d: f2l \n", pc); return pc + 1; } case 141: // f2d { - printf("%4d: f2d \n", pc); + debugf("%4d: f2d \n", pc); return pc + 1; } case 142: // d2i { - printf("%4d: d2i \n", pc); + debugf("%4d: d2i \n", pc); return pc + 1; } case 143: // d2l { - printf("%4d: d2l \n", pc); + debugf("%4d: d2l \n", pc); return pc + 1; } case 144: // d2f { - printf("%4d: d2f \n", pc); + debugf("%4d: d2f \n", pc); return pc + 1; } case 145: // i2b { - printf("%4d: i2b \n", pc); + debugf("%4d: i2b \n", pc); return pc + 1; } case 146: // i2c { - printf("%4d: i2c \n", pc); + debugf("%4d: i2c \n", pc); return pc + 1; } case 147: // i2s { - printf("%4d: i2s \n", pc); + debugf("%4d: i2s \n", pc); return pc + 1; } case 148: // lcmp { - printf("%4d: lcmp \n", pc); + debugf("%4d: lcmp \n", pc); return pc + 1; } case 149: // fcmpl { - printf("%4d: fcmpl \n", pc); + debugf("%4d: fcmpl \n", pc); return pc + 1; } case 150: // fcmpg { - printf("%4d: fcmpg \n", pc); + debugf("%4d: fcmpg \n", pc); return pc + 1; } case 151: // dcmpl { - printf("%4d: dcmpl \n", pc); + debugf("%4d: dcmpl \n", pc); return pc + 1; } case 152: // dcmpg { - printf("%4d: dcmpg \n", pc); + debugf("%4d: dcmpg \n", pc); return pc + 1; } case 153: // ifeq { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifeq %d\n", pc, branch); + debugf("%4d: ifeq %d\n", pc, branch); return pc + 3; } case 154: // ifne { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifne %d\n", pc, branch); + debugf("%4d: ifne %d\n", pc, branch); return pc + 3; } case 155: // iflt { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: iflt %d\n", pc, branch); + debugf("%4d: iflt %d\n", pc, branch); return pc + 3; } case 156: // ifge { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifge %d\n", pc, branch); + debugf("%4d: ifge %d\n", pc, branch); return pc + 3; } case 157: // ifgt { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifgt %d\n", pc, branch); + debugf("%4d: ifgt %d\n", pc, branch); return pc + 3; } case 158: // ifle { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifle %d\n", pc, branch); + debugf("%4d: ifle %d\n", pc, branch); return pc + 3; } case 159: // if_icmpeq { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmpeq %d\n", pc, branch); + debugf("%4d: if_icmpeq %d\n", pc, branch); return pc + 3; } case 160: // if_icmpne { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmpne %d\n", pc, branch); + debugf("%4d: if_icmpne %d\n", pc, branch); return pc + 3; } case 161: // if_icmplt { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmplt %d\n", pc, branch); + debugf("%4d: if_icmplt %d\n", pc, branch); return pc + 3; } case 162: // if_icmpge { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmpge %d\n", pc, branch); + debugf("%4d: if_icmpge %d\n", pc, branch); return pc + 3; } case 163: // if_icmpgt { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmpgt %d\n", pc, branch); + debugf("%4d: if_icmpgt %d\n", pc, branch); return pc + 3; } case 164: // if_icmple { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_icmple %d\n", pc, branch); + debugf("%4d: if_icmple %d\n", pc, branch); return pc + 3; } case 165: // if_acmpeq { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_acmpeq %d\n", pc, branch); + debugf("%4d: if_acmpeq %d\n", pc, branch); return pc + 3; } case 166: // if_acmpne { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: if_acmpne %d\n", pc, branch); + debugf("%4d: if_acmpne %d\n", pc, branch); return pc + 3; } case 167: // goto { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: goto %d\n", pc, branch); + debugf("%4d: goto %d\n", pc, branch); return pc + 3; } case 168: // jsr { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: jsr %d\n", pc, branch); + debugf("%4d: jsr %d\n", pc, branch); return pc + 3; } case 169: // ret { uint32_t index = _u1(&code[pc + 1]); - printf("%4d: ret %u\n", pc, index); + debugf("%4d: ret %u\n", pc, index); return pc + 2; } case 170: // tableswitch { TABLESWITCH_ARGS; - printf("%4d: tableswitch {\n", pc); + debugf("%4d: tableswitch {\n", pc); TABLESWITCH_PRINT_ARGS(); - printf("}\n"); + debugf("}\n"); return TABLESWITCH_NEXT_PC; } case 171: // lookupswitch { LOOKUPSWITCH_ARGS; - printf("%4d: lookupswitch {\n", pc); + debugf("%4d: lookupswitch {\n", pc); LOOKUPSWITCH_PRINT_ARGS(); - printf("}\n"); + debugf("}\n"); return LOOKUPSWITCH_NEXT_PC; } case 172: // ireturn { - printf("%4d: ireturn \n", pc); + debugf("%4d: ireturn \n", pc); return pc + 1; } case 173: // lreturn { - printf("%4d: lreturn \n", pc); + debugf("%4d: lreturn \n", pc); return pc + 1; } case 174: // freturn { - printf("%4d: freturn \n", pc); + debugf("%4d: freturn \n", pc); return pc + 1; } case 175: // dreturn { - printf("%4d: dreturn \n", pc); + debugf("%4d: dreturn \n", pc); return pc + 1; } case 176: // areturn { - printf("%4d: areturn \n", pc); + debugf("%4d: areturn \n", pc); return pc + 1; } case 177: // return { - printf("%4d: return \n", pc); + debugf("%4d: return \n", pc); return pc + 1; } case 178: // getstatic { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: getstatic %u\n", pc, index); + debugf("%4d: getstatic %u\n", pc, index); return pc + 3; } case 179: // putstatic { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: putstatic %u\n", pc, index); + debugf("%4d: putstatic %u\n", pc, index); return pc + 3; } case 180: // getfield { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: getfield %u\n", pc, index); + debugf("%4d: getfield %u\n", pc, index); return pc + 3; } case 181: // putfield { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: putfield %u\n", pc, index); + debugf("%4d: putfield %u\n", pc, index); return pc + 3; } case 182: // invokevirtual { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: invokevirtual %u\n", pc, index); + debugf("%4d: invokevirtual %u\n", pc, index); return pc + 3; } case 183: // invokespecial { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: invokespecial %u\n", pc, index); + debugf("%4d: invokespecial %u\n", pc, index); return pc + 3; } case 184: // invokestatic { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: invokestatic %u\n", pc, index); + debugf("%4d: invokestatic %u\n", pc, index); return pc + 3; } case 185: // invokeinterface { uint32_t index = _u2(&code[pc + 1]); uint32_t count = _u1(&code[pc + 3]); - printf("%4d: invokeinterface %u, %u\n", pc, index, count); + debugf("%4d: invokeinterface %u, %u\n", pc, index, count); return pc + 5; } case 186: // invokedynamic { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: invokedynamic %u\n", pc, index); + debugf("%4d: invokedynamic %u\n", pc, index); return pc + 5; } case 187: // new { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: new %u\n", pc, index); + debugf("%4d: new %u\n", pc, index); return pc + 3; } case 188: // newarray { uint32_t atype = _u1(&code[pc + 1]); - printf("%4d: newarray %u\n", pc, atype); + debugf("%4d: newarray %u\n", pc, atype); return pc + 2; } case 189: // anewarray { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: anewarray %u\n", pc, index); + debugf("%4d: anewarray %u\n", pc, index); return pc + 3; } case 190: // arraylength { - printf("%4d: arraylength \n", pc); + debugf("%4d: arraylength \n", pc); return pc + 1; } case 191: // athrow { - printf("%4d: athrow \n", pc); + debugf("%4d: athrow \n", pc); return pc + 1; } case 192: // checkcast { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: checkcast %u\n", pc, index); + debugf("%4d: checkcast %u\n", pc, index); return pc + 3; } case 193: // instanceof { uint32_t index = _u2(&code[pc + 1]); - printf("%4d: instanceof %u\n", pc, index); + debugf("%4d: instanceof %u\n", pc, index); return pc + 3; } case 194: // monitorenter { - printf("%4d: monitorenter \n", pc); + debugf("%4d: monitorenter \n", pc); return pc + 1; } case 195: // monitorexit { - printf("%4d: monitorexit \n", pc); + debugf("%4d: monitorexit \n", pc); return pc + 1; } case 196: // wide { WIDE_ARGS; - printf("%4d: wide {\n", pc); + debugf("%4d: wide {\n", pc); WIDE_PRINT_ARGS(); - printf("}\n"); + debugf("}\n"); return WIDE_NEXT_PC; } case 197: // multianewarray { uint32_t index = _u2(&code[pc + 1]); uint32_t dimensions = _u1(&code[pc + 3]); - printf("%4d: multianewarray %u, %u\n", pc, index, dimensions); + debugf("%4d: multianewarray %u, %u\n", pc, index, dimensions); return pc + 4; } case 198: // ifnull { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifnull %d\n", pc, branch); + debugf("%4d: ifnull %d\n", pc, branch); return pc + 3; } case 199: // ifnonnull { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: ifnonnull %d\n", pc, branch); + debugf("%4d: ifnonnull %d\n", pc, branch); return pc + 3; } case 200: // goto_w { int32_t branch = _s2(&code[pc + 1]); - printf("%4d: goto_w %d\n", pc, branch); + debugf("%4d: goto_w %d\n", pc, branch); return pc + 3; } case 201: // jsr_w { int32_t branch = _s4(&code[pc + 1]); - printf("%4d: jsr_w %d\n", pc, branch); + debugf("%4d: jsr_w %d\n", pc, branch); return pc + 5; } case 202: // breakpoint { - printf("%4d: breakpoint \n", pc); + debugf("%4d: breakpoint \n", pc); return pc + 1; } case 254: // impdep1 { - printf("%4d: impdep1 \n", pc); + debugf("%4d: impdep1 \n", pc); return pc + 1; } case 255: // impdep2 { - printf("%4d: impdep2 \n", pc); + debugf("%4d: impdep2 \n", pc); return pc + 1; } default: diff --git a/c/execute.c b/c/execute.c index fc47c8d..5b43ee1 100644 --- a/c/execute.c +++ b/c/execute.c @@ -682,7 +682,7 @@ void op_getfield(struct vm * vm, uint32_t index) &field_entry, &field_descriptor_constant); - printf("putfield instance_index %d\n", field_entry->instance_index); + debugf("putfield instance_index %d\n", field_entry->instance_index); int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame); int32_t * objectfields = &objectref[1]; @@ -1186,7 +1186,7 @@ void op_ireturn(struct vm * vm) value = (int32_t)value; break; default: - fprintf(stderr, "invalid conversion: %c\n", vm->current_frame->return_type); + debugf("invalid conversion: %c\n", vm->current_frame->return_type); assert(false); } operand_stack_push_u32(vm->current_frame, value); @@ -1719,7 +1719,7 @@ void op_putfield(struct vm * vm, uint32_t index) type or an array type, then the value must be a value of the field descriptor type. */ - printf("putfield instance_index %d\n", field_entry->instance_index); + debugf("putfield instance_index %d\n", field_entry->instance_index); switch (field_descriptor_constant->utf8.bytes[0]) { case 'B': [[fallthrough]]; diff --git a/c/frame.c b/c/frame.c index 7d73609..a465e34 100644 --- a/c/frame.c +++ b/c/frame.c @@ -53,9 +53,9 @@ static int descriptor_nargs(struct constant * descriptor_constant, uint8_t * ret assert(descriptor_constant->utf8.length >= 2); assert(descriptor_constant->utf8.bytes[0] == '('); - printf("method descriptor: "); + debugf("method descriptor: "); print_utf8_string(descriptor_constant); - printf("\n"); + debugf("\n"); int i = 1; int nargs = 0; @@ -88,7 +88,7 @@ static int descriptor_nargs(struct constant * descriptor_constant, uint8_t * ret bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry) { - printf("vm_initialize_class: "); + debugf("vm_initialize_class: "); struct constant * class_constant = &class_entry->class_file->constant_pool[class_entry->class_file->this_class - 1]; #ifdef DEBUG assert(class_constant->tag == CONSTANT_Class); @@ -98,7 +98,7 @@ bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry) assert(class_name_constant->tag == CONSTANT_Utf8); #endif print_constant(class_name_constant); - printf("\n"); + debugf("\n"); if (class_entry->initialization_state == CLASS_INITIALIZED) return true; @@ -143,7 +143,7 @@ bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry) name_constant->utf8.length); assert(field_info != nullptr); class_entry->static_fields[field_entry->static_index] = constantvalue->integer.bytes; - printf(" constantvalue: %d\n", constantvalue->integer.bytes); + debugf(" constantvalue: %d\n", constantvalue->integer.bytes); break; } @@ -169,7 +169,7 @@ bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry) method_descriptor_length); if (method_info != nullptr) { assert((method_info->access_flags & METHOD_ACC_STATIC) != 0); - printf("\n"); + debugf("\n"); // tamper with next_pc vm->current_frame->next_pc = vm->current_frame->pc; @@ -180,7 +180,7 @@ bool vm_initialize_class(struct vm * vm, struct class_entry * class_entry) return false; } else { class_entry->initialization_state = CLASS_INITIALIZED; - printf(" does not exist for this class\n"); + debugf(" does not exist for this class\n"); } return true; @@ -218,10 +218,10 @@ void vm_special_method_call(struct vm * vm, struct class_entry * class_entry, st struct constant * descriptor_constant = &class_entry->class_file->constant_pool[method_info->descriptor_index - 1]; int nargs = descriptor_nargs(descriptor_constant, &vm->current_frame->return_type); nargs += 1; - printf("nargs+1: %d\n", nargs); + debugf("nargs+1: %d\n", nargs); for (int i = 0; i < nargs; i++) { uint32_t value = operand_stack_pop_u32(old_frame); - printf("local[%d] = %x\n", nargs - i - 1, value); + debugf("local[%d] = %x\n", nargs - i - 1, value); vm->current_frame->local_variable[nargs - i - 1] = value; } @@ -229,7 +229,7 @@ void vm_special_method_call(struct vm * vm, struct class_entry * class_entry, st vm->current_frame->class_entry = class_entry; vm->current_frame->method = method_info; - printf("operand_stack_ix: %d\n", vm->current_frame->operand_stack_ix); + debugf("operand_stack_ix: %d\n", vm->current_frame->operand_stack_ix); } void vm_static_method_call(struct vm * vm, struct class_entry * class_entry, struct method_info * method_info) @@ -263,10 +263,10 @@ void vm_static_method_call(struct vm * vm, struct class_entry * class_entry, str struct constant * descriptor_constant = &class_entry->class_file->constant_pool[method_info->descriptor_index - 1]; int nargs = descriptor_nargs(descriptor_constant, &vm->current_frame->return_type); - printf("nargs %d\n", nargs); + debugf("nargs %d\n", nargs); for (int i = 0; i < nargs; i++) { uint32_t value = operand_stack_pop_u32(old_frame); - printf("local[%d] = %x\n", nargs - i - 1, value); + debugf("local[%d] = %x\n", nargs - i - 1, value); vm->current_frame->local_variable[nargs - i - 1] = value; } ; @@ -275,13 +275,13 @@ void vm_static_method_call(struct vm * vm, struct class_entry * class_entry, str vm->current_frame->class_entry = class_entry; vm->current_frame->method = method_info; - printf("operand_stack_ix: %d\n", vm->current_frame->operand_stack_ix); + debugf("operand_stack_ix: %d\n", vm->current_frame->operand_stack_ix); } void vm_method_return(struct vm * vm) { if (vm->current_frame->initialization_frame != 0) { - printf("vm_method_return: initialization_frame!=0\n"); + debugf("vm_method_return: initialization_frame!=0\n"); vm->current_frame->class_entry->initialization_state = CLASS_INITIALIZED; vm->current_frame->initialization_frame = 0; } @@ -344,29 +344,29 @@ void vm_method_return(struct vm * vm) case 'V': break; default: - fprintf(stderr, "return type not implemented: %c\n", old_frame->return_type); + debugf("return type not implemented: %c\n", old_frame->return_type); assert(false); break; } assert(old_frame->operand_stack_ix == 0); - printf("vm_method_return\n"); + debugf("vm_method_return\n"); } static void print_vm_stack(struct vm * vm) { - printf("[ "); + debugf("[ "); for (int i = 5; i > 0; i--) { if (i > vm->current_frame->operand_stack_ix) { - printf(" "); + debugf(" "); continue; } int32_t value = vm->current_frame->operand_stack[vm->current_frame->operand_stack_ix - i]; if (value > 32767 || value < -32768) - printf("0x%08x ", value); + debugf("0x%08x ", value); else - printf("%10d ", value); + debugf("%10d ", value); } - printf("]\n"); + debugf("]\n"); } void vm_execute(struct vm * vm) @@ -379,7 +379,7 @@ void vm_execute(struct vm * vm) struct method_info * old_method = vm->current_frame->method; decode_execute_instruction(vm, vm->current_frame->code->code, vm->current_frame->pc); if (vm->frame_stack.ix == 1) { - printf("terminate\n"); + debugf("terminate\n"); break; } if (vm->current_frame->method == old_method && vm->current_frame->pc == old_pc) { diff --git a/c/hash_table.c b/c/hash_table.c index f4c48ed..d01f7e3 100644 --- a/c/hash_table.c +++ b/c/hash_table.c @@ -40,7 +40,7 @@ void hash_table_init(int hash_table_length, void print_key(const uint8_t * key, int key_length) { - printf("key: "); + debugf("key: "); for (int i = 0; i < key_length; i++) fputc(key[i], stdout); fputc('\n', stdout); @@ -69,7 +69,7 @@ void hash_table_add(int hash_table_length, uint8_t * key_copy = malloc_class_arena(key_length); for (int i = 0; i < key_length; i++) key_copy[i] = key[i]; - printf("key copy: %p ", key_copy); + debugf("key copy: %p ", key_copy); print_key(key_copy, key_length); e->key = key_copy; @@ -97,7 +97,7 @@ struct hash_table_entry * hash_table_find(int hash_table_length, struct hash_table_entry * e = &entry[hash]; while (e != nullptr && e->key != nullptr) { - //printf("key find: %p ", e->key); + //debugf("key find: %p ", e->key); //print_key(e->key, e->key_length); if (e->key_length == key_length && key_equal(key, e->key, e->key_length)) { return e; diff --git a/c/memory_allocator.c b/c/memory_allocator.c index f9705cf..507b09c 100644 --- a/c/memory_allocator.c +++ b/c/memory_allocator.c @@ -77,37 +77,37 @@ void memory_free(void * p) int main() { memory_reset_free_list(); - printf("%p\n", memory); + debugf("%p\n", memory); void * p1 = memory_allocate(32); - printf("p1 %p\n", p1); + debugf("p1 %p\n", p1); void * p2 = memory_allocate(16); - printf("p2 %p\n", p2); + debugf("p2 %p\n", p2); void * p3 = memory_allocate(256); - printf("p3 %p\n", p3); + debugf("p3 %p\n", p3); void * p4 = memory_allocate(90); - printf("p4 %p\n", p4); - for (int i = 0; i < free_list_length; i++) { printf("%d ", free_list[i]); } - printf("\n"); + debugf("p4 %p\n", p4); + for (int i = 0; i < free_list_length; i++) { debugf("%d ", free_list[i]); } + debugf("\n"); memory_free(p2); memory_free(p1); void * p5 = memory_allocate(256); - printf("%p\n", p4); + debugf("%p\n", p4); - for (int i = 0; i < free_list_length; i++) { printf("%d ", free_list[i]); } - printf("\n"); + for (int i = 0; i < free_list_length; i++) { debugf("%d ", free_list[i]); } + debugf("\n"); void * p6 = memory_allocate(128); - printf("p5 %p\n", p5); + debugf("p5 %p\n", p5); memory_free(p4); void * p7 = memory_allocate(128); - printf("p6 %p\n", p6); + debugf("p6 %p\n", p6); void * p8 = memory_allocate(128); - printf("p7 %p\n", p7); + debugf("p7 %p\n", p7); void * p9 = memory_allocate(128); - printf("p8 %p\n", p8); + debugf("p8 %p\n", p8); } #endif diff --git a/c/printf_dreamcast.c b/c/printf_dreamcast.c index e69de29..7d4ae66 100644 --- a/c/printf_dreamcast.c +++ b/c/printf_dreamcast.c @@ -0,0 +1,143 @@ +#include +#include +#include + +#include "parse.h" +#include "unparse.h" +#include "printf.h" +#include "sh7091_scif.h" + +enum format_type { + FORMAT_BASE10, + FORMAT_BASE10_64, + FORMAT_BASE16, + FORMAT_STRING, + FORMAT_CHAR, + FORMAT_PERCENT, +}; + +struct format { + enum format_type type; + int pad_length; + char fill_char; +}; + +static const char * parse_escape(const char * format, struct format * ft); + +static const char * parse_fill_pad(const char * format, struct format * ft) +{ + if (*format == 0) + return format; + ft->fill_char = *format++; + format = parse_base10(format, &ft->pad_length); + return parse_escape(format, ft); +} + +static const char * parse_escape(const char * format, struct format * ft) +{ + switch (*format) { + case 0: + return format; + case 'd': + ft->type = FORMAT_BASE10; + return format + 1; + case 'l': + ft->type = FORMAT_BASE10_64; + return format + 1; + case 'x': + ft->type = FORMAT_BASE16; + return format + 1; + case 's': + ft->type = FORMAT_STRING; + return format + 1; + case 'c': + ft->type = FORMAT_CHAR; + return format + 1; + case '%': + ft->type = FORMAT_PERCENT; + return format + 1; + default: + return parse_fill_pad(format, ft); + } +} + +struct output_buffer global_output_buffer = {0}; + +void print_string(const char * s, int length) +{ + for (int i = 0; i < length; i++) { + print_char(s[i]); + } +} + +void _printf(const char * format, ...) +{ + va_list args; + va_start(args, format); + + while (true) { + if (*format == 0) + break; + + switch (*format) { + case '%': + { + struct format ft = {0}; + format = parse_escape(format + 1, &ft); + switch (ft.type) { + case FORMAT_BASE10: + { + int32_t num = va_arg(args, int32_t); + char s[10]; + int offset = unparse_base10(s, num, ft.pad_length, ft.fill_char); + print_trings(s, offset); + } + break; + case FORMAT_BASE10_64: + { + int64_t num = va_arg(args, int64_t); + char s[20]; + int offset = unparse_base10_64(s, num, ft.pad_length, ft.fill_char); + print_string(s, offset); + } + break; + case FORMAT_BASE16: + { + uint32_t num = va_arg(args, uint32_t); + char s[8]; + int offset = unparse_base16(s, num, ft.pad_length, ft.fill_char); + print_string(s, offset); + } + break; + case FORMAT_STRING: + { + const char * s = va_arg(args, const char *); + while (*s != 0) { + char c = *s++; + print_char(c); + } + } + break; + case FORMAT_CHAR: + { + const int c = va_arg(args, const int); + print_char((char)c); + } + break; + case FORMAT_PERCENT: + print_char('%'); + break; + } + } + break; + default: + { + char c = *format++; + print_char(c); + } + break; + } + } + + va_end(args); +} diff --git a/c/printf_dreamcast.h b/c/printf_dreamcast.h new file mode 100644 index 0000000..607552b --- /dev/null +++ b/c/printf_dreamcast.h @@ -0,0 +1,17 @@ +#pragma once + +static inline void print_char(char c) +{ + scif_character(c); +} + +void print_string(const char * s, int length) +{ + for (int i = 0; i < length; i++) { + print_char(s[i]); + } +} + +#define printf(...) _printf(__VA_ARGS__) +#define debugf(...) _printf(__VA_ARGS__) +#define debugc(c) print_char(c) diff --git a/c/printf_hosted.h b/c/printf_hosted.h index 1e6b64e..a0ed230 100644 --- a/c/printf_hosted.h +++ b/c/printf_hosted.h @@ -2,5 +2,5 @@ #include -#define debugf(fmt, ...) printf((fmt), __VA_ARGS__); -#define debugc(c) putc(stdout, c); +#define debugf(...) printf(__VA_ARGS__) +#define debugc(c) putc(stdout, c) diff --git a/c/sh7091_scif.cpp b/c/sh7091_scif.cpp new file mode 100644 index 0000000..c5cf598 --- /dev/null +++ b/c/sh7091_scif.cpp @@ -0,0 +1,15 @@ +#include "sh7091/sh7091.hpp" +#include "sh7091/sh7091_bits.hpp" + +#include "sh7091_scif.h" + +void scif_character(const char c) +{ + using namespace scif; + // wait for transmit fifo to become partially empty + while ((sh7091.SCIF.SCFSR2 & scfsr2::tdfe::bit_mask) == 0); + + sh7091.SCIF.SCFSR2 &= ~scfsr2::tdfe::bit_mask; + + sh7091.SCIF.SCFTDR2 = static_cast(c); +} diff --git a/c/sh7091_scif.h b/c/sh7091_scif.h new file mode 100644 index 0000000..419a1f5 --- /dev/null +++ b/c/sh7091_scif.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void scif_character(const char c); + +#ifdef __cplusplus +} +#endif diff --git a/gen_decoder.py b/gen_decoder.py index 0d08bb9..418bf03 100644 --- a/gen_decoder.py +++ b/gen_decoder.py @@ -115,15 +115,15 @@ def generate_print_fixed_width_instruction(instruction): if argument_values: argument_values = ", " + argument_values mnemonic = instruction.mnemonic.ljust(13) - yield f'printf("%4d: {mnemonic} {argument_format}\\n", pc{argument_values});' + yield f'debugf("%4d: {mnemonic} {argument_format}\\n", pc{argument_values});' yield f"return pc + {1 + instruction.arguments_size};" def generate_print_variable_width_instruction(instruction): mnemonic = instruction.mnemonic.ljust(13) yield f"{instruction.mnemonic.upper()}_ARGS;" - yield f'printf("%4d: {mnemonic} {{\\n", pc);' + yield f'debugf("%4d: {mnemonic} {{\\n", pc);' yield f"{instruction.mnemonic.upper()}_PRINT_ARGS();" - yield 'printf("}\\n");' + yield 'debugf("}\\n");' yield f"return {instruction.mnemonic.upper()}_NEXT_PC;" def generate_print_decoder():