Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/27229 )

Change subject: util: Refactor the string->register packing function in the m5 utility.
......................................................................

util: Refactor the string->register packing function in the m5 utility.

This change removes the responsibility for checking the number of
arguments and handing the default of no string back into init_param and
out of the function which packs strings into registers. It also renames
the function to more closely match its purpose, and rewrites it to be a
bit simpler and (IMHO) easier to follow.

Importantly, rather than doing a hand implemented strcpy which would
follow the endianness of the target/simulated platform, this change
makes this function pack the registers explicitly in little endian byte
order. This way on the consuming end in gem5, the initParam function
doesn't have to care what the guest endianness is, it can just translate
them from little endian to whatever the host endianness is (very likely
also little endian).

Change-Id: Ie9f79ecb8d4584c6e47a2793a31ccaa8c7c15986
---
M util/m5/src/m5.c
1 file changed, 15 insertions(+), 14 deletions(-)



diff --git a/util/m5/src/m5.c b/util/m5/src/m5.c
index 83174d0..ae9068b 100644
--- a/util/m5/src/m5.c
+++ b/util/m5/src/m5.c
@@ -76,23 +76,24 @@
 }

 void
-parse_str_args_to_regs(int argc, char *argv[], uint64_t regs[], int len)
+pack_str_into_regs(const char *str, uint64_t regs[], int num_regs)
 {
-    if (argc > 1 || (argc > 0 && strlen(argv[0]) > len * sizeof(uint64_t)))
+    const size_t RegSize = sizeof(regs[0]);
+    const size_t MaxLen = num_regs * RegSize;
+
+    size_t len = strlen(str);
+
+    if (len > MaxLen)
         usage();

-    int i;
-    for (i = 0; i < len; i++)
-        regs[i] = 0;
+    memset(regs, 0, MaxLen);

-    if (argc == 0)
-        return;
-
-    int n;
-    for (n = 0, i = 0; i < len && n < strlen(argv[0]); n++) {
-        *((char *)(&regs[i]) + (n % 8)) = argv[0][n];
-        if ((n % 8) == 7)
-            i++;
+    while (len) {
+        for (int offset = 0; offset < RegSize && len; offset++, len--) {
+            int shift = offset * 8;
+            *regs |= (uint64_t)(uint8_t)*str++ << shift;
+        }
+        regs++;
     }
 }

@@ -277,7 +278,7 @@
         usage();

     uint64_t key_str[2];
-    parse_str_args_to_regs(argc, argv, key_str, 2);
+    pack_str_into_regs(argc == 0 ? "" : argv[0], key_str, 2);
     uint64_t val = m5_init_param(key_str[0], key_str[1]);
     printf("%"PRIu64, val);
 }

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/27229
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ie9f79ecb8d4584c6e47a2793a31ccaa8c7c15986
Gerrit-Change-Number: 27229
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabebl...@google.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to