changeset 2207:de3d20e58555

Mostly fix label sorting post-refactor
author Michael Pavone <pavone@retrodev.com>
date Sat, 27 Aug 2022 17:35:28 -0700
parents 4c265d2f6c88
children 3809a0bd680e
files disasm.c tern.c tern.h
diffstat 3 files changed, 26 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/disasm.c	Sat Aug 27 17:07:57 2022 -0700
+++ b/disasm.c	Sat Aug 27 17:35:28 2022 -0700
@@ -3,14 +3,14 @@
 label_def *find_label(disasm_context *context, uint32_t address)
 {
 	char key[MAX_INT_KEY_SIZE];
-	tern_int_key(address & context->address_mask, key);
+	tern_sortable_int_key(address & context->address_mask, key);
 	return tern_find_ptr(context->labels, key);
 }
 
 label_def *add_find_label(disasm_context *context, uint32_t address)
 {
 	char key[MAX_INT_KEY_SIZE];
-	tern_int_key(address & context->address_mask, key);
+	tern_sortable_int_key(address & context->address_mask, key);
 	label_def *def = tern_find_ptr(context->labels, key);
 	if (!def)
 	{
--- a/tern.c	Sat Aug 27 17:07:57 2022 -0700
+++ b/tern.c	Sat Aug 27 17:35:28 2022 -0700
@@ -293,13 +293,33 @@
 
 char * tern_int_key(uint32_t key, char * buf)
 {
-	char * cur = buf;
+
+	int len = 0;
+	uint32_t tmp = key;
+	while (tmp)
+	{
+		tmp >>= 7;
+		++len;
+	}
+	buf[len] = 0;
+	char * cur = buf + len - 1;
 	while (key)
 	{
-		*(cur++) = (key & 0x7F) + 1;
+		*(cur--) = (key & 0x7F) + 1;
 		key >>= 7;
 	}
-	*cur = 0;
+	return buf;
+}
+
+char * tern_sortable_int_key(uint32_t key, char * buf)
+{
+	buf[MAX_INT_KEY_SIZE - 1] = 0;
+	char * cur = buf + MAX_INT_KEY_SIZE - 2;
+	while (cur >= buf)
+	{
+		*(cur--) = (key & 0x7F) + 1;
+		key >>= 7;
+	}
 	return buf;
 }
 
--- a/tern.h	Sat Aug 27 17:07:57 2022 -0700
+++ b/tern.h	Sat Aug 27 17:35:28 2022 -0700
@@ -53,6 +53,7 @@
 uint32_t tern_count(tern_node *head);
 void tern_foreach(tern_node *head, iter_fun fun, void *data);
 char * tern_int_key(uint32_t key, char * buf);
+char * tern_sortable_int_key(uint32_t key, char * buf);
 void tern_free(tern_node *head);
 
 #endif //TERN_H_