Valgrindで見つかったメモリー周辺の問題点を修正
[YACASL2.git] / src / exec.c
index 4da91b7..feb735c 100644 (file)
@@ -9,7 +9,6 @@
  * 実行エラーの定義
  */
 static CERR cerr_exec[] = {
-    { 201, "Loading - full of COMET II memory" },
     { 202, "SVC input - out of Input memory" },
     { 203, "SVC output - out of COMET II memory" },
     { 204, "Program Register (PR) - out of COMET II memory" },
@@ -17,6 +16,15 @@ static CERR cerr_exec[] = {
     { 206, "Address - out of COMET II memory" },
     { 207, "Stack Pointer (SP) - out of COMET II memory" },
     { 209, "not GR in operand x" },
+    { 210, "not command code of COMET II" },
+};
+
+/**
+ * アセンブル結果読み込みエラーの定義
+ */
+static CERR cerr_load[] = {
+    { 201, "Loading - full of COMET II memory" },
+    { 208, "object file is not specified" },
 };
 
 /**
@@ -25,13 +33,20 @@ static CERR cerr_exec[] = {
 EXECMODE execmode = {false, false, false};
 
 /**
- * 実行エラーをエラーリストに追加
+ * アセンブル結果読み込みエラーをエラーリストに追加
  */
-bool addcerrlist_exec()
+void addcerrlist_load()
 {
-    return addcerrlist(ARRAYSIZE(cerr_exec), cerr_exec);
+    addcerrlist(ARRAYSIZE(cerr_load), cerr_load);
 }
 
+/**
+ * 実行エラーをエラーリストに追加
+ */
+void addcerrlist_exec()
+{
+    addcerrlist(ARRAYSIZE(cerr_exec), cerr_exec);
+}
 /**
  * 指定されたファイルからアセンブル結果を読み込む
  */
@@ -80,7 +95,7 @@ static void svcin()
         sys->memory[sys->cpu->gr[1]+i] = *(buffer + i);
     }
     sys->memory[sys->cpu->gr[2]] = i + 1;
-    free_chk(buffer, "buffer");
+    FREE(buffer);
 }
 
 /**
@@ -332,6 +347,16 @@ static WORD srl(WORD val0, WORD val1)
     return res;
 }
 
+/**
+ * プログラムレジスタ(PR)を表す文字列を返す
+ **/
+static char *pr2str(WORD pr) {
+    char *str = malloc_chk(CERRSTRSIZE + 1, "pr2str.pr");
+
+    sprintf(str, "PR:#%04X", pr);
+    return str;
+}
+
 /**
  * 仮想マシンCOMET IIの実行
  */
@@ -339,7 +364,6 @@ bool exec()
 {
     WORD op, r_r1, x_r2, val;
     CMDTYPE cmdtype;
-    char *errpr = malloc_chk(CERRSTRSIZE + 1, "exec.errpr");
     clock_t clock_begin, clock_end;
 
     if(execmode.trace == true) {
@@ -354,18 +378,15 @@ bool exec()
         clock_begin = clock();
         /* プログラムレジスタのアドレスが主記憶の範囲外の場合はエラー */
         if(sys->cpu->pr >= sys->memsize) {
-            sprintf(errpr, "PR:#%04X", sys->cpu->pr);
-            setcerr(204, errpr);    /* Program Register (PR) - out of COMET II memory */
+            setcerr(204, pr2str(sys->cpu->pr));    /* Program Register (PR) - out of COMET II memory */
         }
         /* スタック領域を確保できない場合はエラー */
         else if(sys->cpu->sp <= execptr->end) {
-            sprintf(errpr, "PR:#%04X", sys->cpu->pr);
-            setcerr(205, errpr);    /* Stack Pointer (SP) - cannot allocate stack buffer */
+            setcerr(205, pr2str(sys->cpu->pr));    /* Stack Pointer (SP) - cannot allocate stack buffer */
         }
         /* スタック領域のアドレスが主記憶の範囲外の場合はエラー */
         else if(sys->cpu->sp > sys->memsize) {
-            sprintf(errpr, "PR:#%04X", sys->cpu->pr);
-            setcerr(207, errpr);    /* Stack Pointer (SP) - out of COMET II memory */
+            setcerr(207, pr2str(sys->cpu->pr));    /* Stack Pointer (SP) - out of COMET II memory */
         }
         /* エラー発生時は終了 */
         if(cerr->num > 0) {
@@ -374,7 +395,9 @@ bool exec()
         /* 命令の取り出し */
         op = sys->memory[sys->cpu->pr] & 0xFF00;
         /* 命令の解読 */
-        cmdtype = getcmdtype(op);
+        if((cmdtype = getcmdtype(op)) == NOTCMD) {
+            setcerr(210, pr2str(sys->cpu->pr));          /* not command code of COMET II */
+        }
         r_r1 = (sys->memory[sys->cpu->pr] >> 4) & 0xF;
         x_r2 = sys->memory[sys->cpu->pr] & 0xF;
         /* traceオプション指定時、レジスタを出力 */
@@ -396,8 +419,7 @@ bool exec()
         if(cmdtype == R1_R2) {
             /* オペランドの数値が汎用レジスタの範囲外の場合はエラー */
             if(x_r2 > GRSIZE - 1) {
-                sprintf(errpr, "PR:#%04X", sys->cpu->pr-1);
-                setcerr(209, errpr);    /* not GR in operand x */
+                setcerr(209, pr2str(sys->cpu->pr-1));    /* not GR in operand x */
                 goto execerr;
             }
             val = sys->cpu->gr[x_r2];
@@ -405,8 +427,7 @@ bool exec()
         else if(cmdtype ==  R_ADR_X || cmdtype == R_ADR_X_ || cmdtype == ADR_X) {
             /* オペランドの数値が汎用レジスタの範囲外の場合はエラー */
             if(x_r2 > GRSIZE - 1) {
-                sprintf(errpr, "PR:#%04X", sys->cpu->pr-1);
-                setcerr(209, errpr);    /* not GR in operand x */
+                setcerr(209, pr2str(sys->cpu->pr-1));    /* not GR in operand x */
                 goto execerr;
             }
             /* 実効アドレス(値または値が示す番地)を取得  */
@@ -418,14 +439,12 @@ bool exec()
             /* ロード/算術論理演算命令/比較演算命令では、アドレスに格納されている内容を取得 */
             if(cmdtype == R_ADR_X_) {
                 if(val >= sys->memsize) {
-                    sprintf(errpr, "PR:#%04X", sys->cpu->pr-1);
-                    setcerr(206, errpr);    /* Address - out of COMET II memory */
+                    setcerr(206, pr2str(sys->cpu->pr-1));    /* Address - out of COMET II memory */
                     goto execerr;
                 }
                 val = sys->memory[val];
             }
         }
-        free_chk(errpr, "errpr");
         /* 主オペランドが1から4の場合、第2ビットを無視 */
         if(op >= 0x1000 && op <= 0x4FFF) {
             op &= 0xFB00;
@@ -555,6 +574,5 @@ bool exec()
     return true;
 execerr:
     fprintf(stderr, "Execute error - %d: %s\n", cerr->num, cerr->msg);
-    cerr->msg = NULL;           /* Ubuntu 10.04 PPCでcerr.msg開放時にSegmentation Faultが発生する現象を回避するため */
     return false;
 }