From 5b11c6b80a4c37704827aa52f2bbe37654521bea Mon Sep 17 00:00:00 2001
From: Recolic Keghart <root@recolic.net>
Date: Sun, 29 Dec 2019 22:49:57 +0800
Subject: [PATCH] trial

---
 nemu/Makefile             | 4 ++--
 nemu/include/common.h     | 2 +-
 nemu/src/cpu/exec/exec.cc | 8 +++-----
 nemu/src/memory/memory.cc | 9 ++++++++-
 4 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/nemu/Makefile b/nemu/Makefile
index e058045..57860b6 100644
--- a/nemu/Makefile
+++ b/nemu/Makefile
@@ -19,8 +19,8 @@ include Makefile.git
 CXX ?= g++
 LD = $(CXX)
 INCLUDES  = $(addprefix -I, $(INC_DIR))
-CFLAGS   += -O2 -MMD -Wall -ggdb3 $(INCLUDES) -fomit-frame-pointer -std=c++17
-CFLAGS   += -DDIFF_TEST_QEMU
+CFLAGS   += -O3 -MMD -Wall -ggdb $(INCLUDES) -fomit-frame-pointer -std=c++17
+# CFLAGS   += -DDIFF_TEST_QEMU
 
 # Source code generation before any targets.
 SUBDIRS = src/monitor/debug/expr_impl
diff --git a/nemu/include/common.h b/nemu/include/common.h
index b734ebc..83d7ae9 100644
--- a/nemu/include/common.h
+++ b/nemu/include/common.h
@@ -1,7 +1,7 @@
 #ifndef __COMMON_H__
 #define __COMMON_H__
 
-#define DEBUG
+//#define DEBUG
 //#define DIFF_TEST
 
 #if _SHARE
diff --git a/nemu/src/cpu/exec/exec.cc b/nemu/src/cpu/exec/exec.cc
index 3c67656..f6e847a 100644
--- a/nemu/src/cpu/exec/exec.cc
+++ b/nemu/src/cpu/exec/exec.cc
@@ -14,10 +14,8 @@ typedef struct {
 #define EMPTY              EX(inv)
 
 static inline void set_width(int width) {
-  if (width == 0) {
-    width = decoding.is_operand_size_16 ? 2 : 4;
-  }
-  decoding.src.width = decoding.dest.width = decoding.src2.width = width;
+  const auto tmp = width == 0 ? (decoding.is_operand_size_16 ? 2 : 4) : width;
+  decoding.src.width = decoding.dest.width = decoding.src2.width = tmp;
 }
 
 /* Instruction Decode and EXecute */
@@ -211,7 +209,7 @@ namespace EHelperImpl {
     idex(eip, &opcode_table[opcode]);
   }
 
-  make_EHelper(real) {
+  __attribute__((hot)) make_EHelper(real) {
     uint32_t opcode = instr_fetch(eip, 1);
     decoding.opcode = opcode;
     set_width(opcode_table[opcode].width);
diff --git a/nemu/src/memory/memory.cc b/nemu/src/memory/memory.cc
index 0206211..f4ce3bf 100644
--- a/nemu/src/memory/memory.cc
+++ b/nemu/src/memory/memory.cc
@@ -11,7 +11,14 @@ uint8_t pmem[PMEM_SIZE];
 
 /* Memory accessing interfaces */
 
-uint32_t paddr_read(paddr_t addr, int len) {
+__attribute__((hot)) uint32_t paddr_read(paddr_t addr, int len) {
+  switch(len) {
+    case 4: return pmem_rw(addr, uint32_t);
+    case 2: return pmem_rw(addr, uint32_t) & 0x0000ffff;
+    case 1: return pmem_rw(addr, uint32_t) & 0x000000ff;
+    case 3: return pmem_rw(addr, uint32_t) & 0x00ffffff;
+    case 0: return 0;
+  }
   return pmem_rw(addr, uint32_t) & (~0u >> ((4 - len) << 3));
 }
 
-- 
GitLab