replace printf with debugf

This commit is contained in:
Zack Buhman 2024-12-26 00:02:08 -06:00
parent cedeb20353
commit b18060e646
14 changed files with 537 additions and 351 deletions

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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 \

File diff suppressed because it is too large Load Diff

View File

@ -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]];

View File

@ -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("<clinit>\n");
debugf("<clinit>\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("<clinit> does not exist for this class\n");
debugf("<clinit> 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) {

View File

@ -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;

View File

@ -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

View File

@ -0,0 +1,143 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#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);
}

17
c/printf_dreamcast.h Normal file
View File

@ -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)

View File

@ -2,5 +2,5 @@
#include <stdio.h>
#define debugf(fmt, ...) printf((fmt), __VA_ARGS__);
#define debugc(c) putc(stdout, c);
#define debugf(...) printf(__VA_ARGS__)
#define debugc(c) putc(stdout, c)

15
c/sh7091_scif.cpp Normal file
View File

@ -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<uint8_t>(c);
}

11
c/sh7091_scif.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void scif_character(const char c);
#ifdef __cplusplus
}
#endif

View File

@ -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():