From c29f75143d403dab761701c678cf0274583ab56c Mon Sep 17 00:00:00 2001
From: Recolic Keghart <root@recolic.net>
Date: Thu, 26 Dec 2019 20:55:53 +0800
Subject: [PATCH] >  Manual commit:  Adjust makefile for qemu-diff U201614531
 recolic Linux RECOLICPC 5.4.2-arch1-1 #1 SMP PREEMPT Thu, 05 Dec 2019
 12:29:40 +0000 x86_64 GNU/Linux  20:55:53 up 4 days,  2:04,  1 user,  load
 average: 0.65, 0.50, 0.47 e3d87462f69f26419a41b6a38703c531e489c

---
 nemu/include/cpu/rtl.h                   |  2 +-
 nemu/src/cpu/exec/arith.cc               |  1 +
 nemu/src/cpu/exec/logic.cc               | 15 +++++++++++++++
 nemu/src/monitor/debug/expr_impl/lexer.l |  2 +-
 nemu/src/monitor/debug/ui.cc             |  9 +++++----
 nemu/tools/qemu-diff/Makefile            | 14 +++++++-------
 nemu/tools/qemu-diff/include/common.h    |  4 ----
 nemu/tools/qemu-diff/src/gdb-host.cc     | 10 +++++-----
 nemu/tools/qemu-diff/src/protocol.cc     | 10 +++++-----
 9 files changed, 40 insertions(+), 27 deletions(-)

diff --git a/nemu/include/cpu/rtl.h b/nemu/include/cpu/rtl.h
index 273f134..1e029ed 100644
--- a/nemu/include/cpu/rtl.h
+++ b/nemu/include/cpu/rtl.h
@@ -155,7 +155,7 @@ static inline void rtl_sr(int r, const rtlreg_t* src1, int width) {
 
 static inline void rtl_not(rtlreg_t *dest, const rtlreg_t* src1) {
   // dest <- ~src1
-  TODO();
+  *dest = ~ *src1;
 }
 
 static inline void rtl_sext(rtlreg_t* dest, const rtlreg_t* src1, int width) {
diff --git a/nemu/src/cpu/exec/arith.cc b/nemu/src/cpu/exec/arith.cc
index 0a10a39..3ecaa30 100644
--- a/nemu/src/cpu/exec/arith.cc
+++ b/nemu/src/cpu/exec/arith.cc
@@ -5,6 +5,7 @@
 make_EHelper(add) {
 	rtl_sext(&t1, &id_dest->val, id_dest->width);
 	rtl_sext(&t2, &id_src->val, id_src->width);
+
 	rtl_add(&t0, &t1, &t2);
 	t3 = (t0 < t1);
 	rtl_set_CF(&t3);
diff --git a/nemu/src/cpu/exec/logic.cc b/nemu/src/cpu/exec/logic.cc
index dd28dfc..70add1b 100644
--- a/nemu/src/cpu/exec/logic.cc
+++ b/nemu/src/cpu/exec/logic.cc
@@ -1,8 +1,17 @@
 #include "cpu/exec.h"
 #include "cpu/cc.h"
 
+void sign_extend_if_required(const Operand &dest, Operand &src) {
+  if(dest.width > src.width) {
+    rtl_sext(&src.val, &src.val, src.width);
+    src.imm = src.val;
+    src.width = dest.width;
+  }
+}
+
 make_EHelper(test) {
   // `and` without write_back.
+  sign_extend_if_required(*id_dest, *id_src);
   rtl_and(&t1, &id_dest->val, &id_src->val);
 
   rtl_update_ZFSF(&t1, id_dest->width);
@@ -12,6 +21,10 @@ make_EHelper(test) {
 }
 
 make_EHelper(and) {
+  rlib::printfln("before test, id_dest={}, src={}", *id_dest, *id_src);
+  sign_extend_if_required(*id_dest, *id_src);
+  rlib::printfln("after test, id_dest={}, src={}", *id_dest, *id_src);
+
   rtl_and(&id_dest->val, &id_dest->val, &id_src->val);
   operand_write(id_dest, &id_dest->val);
 
@@ -22,6 +35,8 @@ make_EHelper(and) {
 }
 
 make_EHelper(xor) {
+  sign_extend_if_required(*id_dest, *id_src);
+
   rtl_xor(&id_dest->val, &id_dest->val, &id_src->val);
   operand_write(id_dest, &id_dest->val);
 
diff --git a/nemu/src/monitor/debug/expr_impl/lexer.l b/nemu/src/monitor/debug/expr_impl/lexer.l
index 649ac3d..851781d 100644
--- a/nemu/src/monitor/debug/expr_impl/lexer.l
+++ b/nemu/src/monitor/debug/expr_impl/lexer.l
@@ -13,7 +13,7 @@
 %%
 
 [ \t\n]       {}
-0x[0-9]+      {yylval.ival = (int)strtol(yytext, NULL, 16); return T_INT;}
+0x[0-9a-f]+      {yylval.ival = (int)strtol(yytext, NULL, 16); return T_INT;}
 [0-9]+        {yylval.ival = atoi(yytext); return T_INT;}
 "=="          {return T_EQUAL;}
 "!="          {return T_NEQUAL;}
diff --git a/nemu/src/monitor/debug/ui.cc b/nemu/src/monitor/debug/ui.cc
index 74944d1..ddb92d2 100644
--- a/nemu/src/monitor/debug/ui.cc
+++ b/nemu/src/monitor/debug/ui.cc
@@ -71,7 +71,7 @@ static struct {
   { "c", "Continue the execution of the program", cmd_c },
   { "n", "= GDB `n`", cmd_n },
   { "info", "= GDB `info`, supporting `info r` / `info w`", cmd_info },
-  { "x", "x <bytes> <start address>, dump memory content.", cmd_x },
+  { "x", "x <bytes> <startAddr or expr>, dump memory content.", cmd_x },
   { "w", "w <expr>, add watchpoint for $expr", cmd_w },
   { "p", "p <expr>, show value of $expr", cmd_p },
   { "d", "d <watchpoint id>, delete watchpoint by id", cmd_d },
@@ -185,12 +185,13 @@ static int cmd_info(char *_args) {
 
 static int cmd_x(char *_args) {
   if(_args == NULL)
-    throw std::runtime_error("Usage: x <size> <startAddr>");
+    throw std::runtime_error("Usage: x <size> <startAddr/expr>");
   auto args = string(_args).strip().split();
   if(args.size() != 2)
-    throw std::runtime_error("Usage: x <size> <startAddr>");
+    throw std::runtime_error("Usage: x <size> <startAddr/expr>");
   
-  println(dumpMem(std::stoull(args[1], 0, 16), args[0].as<uint64_t>()));
+  uint32_t beginAddr = evaluate_expr(args[1]);
+  println(dumpMem(beginAddr, args[0].as<uint64_t>()));
   return 0;
 }
 
diff --git a/nemu/tools/qemu-diff/Makefile b/nemu/tools/qemu-diff/Makefile
index e28f358..b77f8bb 100644
--- a/nemu/tools/qemu-diff/Makefile
+++ b/nemu/tools/qemu-diff/Makefile
@@ -6,20 +6,20 @@ BINARY ?= $(BUILD_DIR)/qemu-so
 .DEFAULT_GOAL = app
 
 # Compilation flags
-CC = gcc
-LD = gcc
+CXX ?= g++
+LD = $(CXX)
 INCLUDES  = $(addprefix -I, $(INC_DIR))
 CFLAGS   += -O2 -fPIC -MMD -Wall -Werror -ggdb3 $(INCLUDES) -fomit-frame-pointer
 
 # Files to be compiled
-SRCS = $(shell find src/ -name "*.c")
-OBJS = $(SRCS:src/%.c=$(OBJ_DIR)/%.o)
+SRCS = $(shell find src/ -name "*.cc")
+OBJS = $(SRCS:src/%.cc=$(OBJ_DIR)/%.o)
 
 # Compilation patterns
-$(OBJ_DIR)/%.o: src/%.c
-	@echo + CC $<
+$(OBJ_DIR)/%.o: src/%.cc
+	@echo + CXX $<
 	@mkdir -p $(dir $@)
-	@$(CC) $(CFLAGS)  -c -o $@ $<
+	@$(CXX) $(CFLAGS)  -c -o $@ $<
 
 
 # Depencies
diff --git a/nemu/tools/qemu-diff/include/common.h b/nemu/tools/qemu-diff/include/common.h
index c4ac3a4..c1a0e36 100644
--- a/nemu/tools/qemu-diff/include/common.h
+++ b/nemu/tools/qemu-diff/include/common.h
@@ -8,10 +8,6 @@
 #include <unistd.h>
 #include <stdlib.h>
 
-typedef uint8_t bool;
-#define true 1
-#define false 0
-
 #include "protocol.h"
 
 #endif
diff --git a/nemu/tools/qemu-diff/src/gdb-host.cc b/nemu/tools/qemu-diff/src/gdb-host.cc
index a48a5b2..deaaea6 100644
--- a/nemu/tools/qemu-diff/src/gdb-host.cc
+++ b/nemu/tools/qemu-diff/src/gdb-host.cc
@@ -12,7 +12,7 @@ bool gdb_connect_qemu(void) {
 }
 
 static bool gdb_memcpy_to_qemu_small(uint32_t dest, void *src, int len) {
-  char *buf = malloc(len * 2 + 128);
+  char *buf = (char *)malloc(len * 2 + 128);
   assert(buf != NULL);
   int p = sprintf(buf, "M0x%x,%x:", dest, len);
   int i;
@@ -31,7 +31,8 @@ static bool gdb_memcpy_to_qemu_small(uint32_t dest, void *src, int len) {
   return ok;
 }
 
-bool gdb_memcpy_to_qemu(uint32_t dest, void *src, int len) {
+bool gdb_memcpy_to_qemu(uint32_t dest, void *_src, int len) {
+  char *src = (char *)_src;
   const int mtu = 1500;
   bool ok = true;
   while (len > mtu) {
@@ -49,10 +50,9 @@ bool gdb_getregs(union gdb_regs *r) {
   size_t size;
   uint8_t *reply = gdb_recv(conn, &size);
 
-  int i;
   uint8_t *p = reply;
   uint8_t c;
-  for (i = 0; i < sizeof(union gdb_regs) / sizeof(uint32_t); i ++) {
+  for (size_t i = 0; i < sizeof(union gdb_regs) / sizeof(uint32_t); i ++) {
     c = p[8];
     p[8] = '\0';
     r->array[i] = gdb_decode_hex_str(p);
@@ -67,7 +67,7 @@ bool gdb_getregs(union gdb_regs *r) {
 
 bool gdb_setregs(union gdb_regs *r) {
   int len = sizeof(union gdb_regs);
-  char *buf = malloc(len * 2 + 128);
+  char *buf = (char *)malloc(len * 2 + 128);
   assert(buf != NULL);
   buf[0] = 'G';
 
diff --git a/nemu/tools/qemu-diff/src/protocol.cc b/nemu/tools/qemu-diff/src/protocol.cc
index 9b42e14..3405e15 100644
--- a/nemu/tools/qemu-diff/src/protocol.cc
+++ b/nemu/tools/qemu-diff/src/protocol.cc
@@ -65,7 +65,7 @@ uint64_t gdb_decode_hex_str(uint8_t *bytes) {
 
 
 static struct gdb_conn* gdb_begin(int fd) {
-  struct gdb_conn *conn = calloc(1, sizeof(struct gdb_conn *));
+  struct gdb_conn *conn = (gdb_conn *)calloc(1, sizeof(struct gdb_conn *));
   if (conn == NULL)
     err(1, "calloc");
 
@@ -174,7 +174,7 @@ void gdb_send(struct gdb_conn *conn, const uint8_t *command, size_t size) {
 static uint8_t* recv_packet(FILE *in, size_t *ret_size, bool* ret_sum_ok) {
   size_t i = 0;
   size_t size = 4096;
-  uint8_t *reply = malloc(size);
+  uint8_t *reply = (uint8_t *)malloc(size);
   if (reply == NULL)
     err(1, "malloc");
 
@@ -205,7 +205,7 @@ static uint8_t* recv_packet(FILE *in, size_t *ret_size, bool* ret_sum_ok) {
 
         // terminate it for good measure
         if (i == size) {
-          reply = realloc(reply, size + 1);
+          reply = (uint8_t *)realloc(reply, size + 1);
           if (reply == NULL)
             err(1, "realloc");
         }
@@ -234,7 +234,7 @@ static uint8_t* recv_packet(FILE *in, size_t *ret_size, bool* ret_sum_ok) {
             // get a bigger buffer if needed
             if (i + count > size) {
               size *= 2;
-              reply = realloc(reply, size);
+              reply = (uint8_t *)realloc(reply, size);
               if (reply == NULL)
                 err(1, "realloc");
             }
@@ -257,7 +257,7 @@ static uint8_t* recv_packet(FILE *in, size_t *ret_size, bool* ret_sum_ok) {
     // get a bigger buffer if needed
     if (i == size) {
       size *= 2;
-      reply = realloc(reply, size);
+      reply = (uint8_t *)realloc(reply, size);
       if (reply == NULL)
         err(1, "realloc");
     }
-- 
GitLab