From: j8takagi Date: Wed, 24 Feb 2010 02:52:56 +0000 (+0900) Subject: ラベルの出力時、アドレス順に並べ替えるように仕様変更 X-Git-Tag: v0.1~17 X-Git-Url: https://j8takagi.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0de529cf2de73a298288fd294b24b964abdba34;p=YACASL2.git ラベルの出力時、アドレス順に並べ替えるように仕様変更 --- diff --git a/include/assemble.h b/include/assemble.h index ae746fd..7c6b347 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -1,13 +1,16 @@ #ifndef YACASL2_ASSEMBLE_INCLUDED #define YACASL2_ASSEMBLE_INCLUDED -/* CASL IIの制限 */ +/* CASL IIの仕様 */ +enum { + LABELSIZE = 8, /* ラベルの最大文字数 */ + OPDSIZE = 40, /* オペラントの最大数。CASL IIシミュレータの制限 */ +}; + +/* YACASL2の制限 */ enum { LINESIZE = 1024, /* 行の最大文字数 */ TOKENSIZE = 256, /* トークンの最大文字数 */ - LABELSIZE = 8, /* ラベルの最大文字数 */ - LABELTABSIZE = 256, /* ラベルの最大数 */ - OPDSIZE = 40, /* オペラントの最大数 */ }; /* アセンブルモード */ @@ -49,6 +52,13 @@ typedef struct { char *cmd; } CMDARRAY; +/* ラベル配列 */ +typedef struct { + char *prog; + char *label; + WORD adr; +} LABELARRAY; + /* ラベル表 */ typedef struct _LABELTAB { struct _LABELTAB *next; @@ -57,6 +67,10 @@ typedef struct _LABELTAB { WORD adr; } LABELTAB; +enum { + LABELTABSIZE = 251, /* ラベル表のサイズ */ +}; + /* アセンブラが、1回目か、2回目か、を表す */ typedef enum { FIRST = 0, diff --git a/src/label.c b/src/label.c index d740b2e..120223c 100644 --- a/src/label.c +++ b/src/label.c @@ -1,7 +1,8 @@ #include "casl2.h" #include "assemble.h" -LABELTAB *labels[LABELTABSIZE]; +int labelcnt = 0; /* ラベル数 */ +LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ /* プログラム名とラベルに対応するハッシュ値を返す */ unsigned labelhash(const char *prog, const char *label) @@ -46,7 +47,7 @@ bool addlabel(const char *prog, const char *label, WORD adr) setcerr(101, label); /* label already defined */ return false; } - np = (LABELTAB *) malloc(sizeof(*np)); + np = malloc(sizeof(*np)); if(np == NULL || (np->label = strdup(label)) == NULL || (prog != NULL && (np->prog = strdup(prog)) == NULL)) { @@ -56,7 +57,8 @@ bool addlabel(const char *prog, const char *label, WORD adr) if(prog != NULL) { keys[i++] = strdup(prog); } - keys[i] = strdup(label);; + labelcnt++; + keys[i] = strdup(label); hashval = labelhash(prog, label); np->next = labels[hashval]; labels[hashval] = np; @@ -64,19 +66,33 @@ bool addlabel(const char *prog, const char *label, WORD adr) return true; } +int compare_adr(const void *a, const void *b) +{ + return (**(const LABELARRAY **)a).adr - (**(const LABELARRAY **)b).adr; +} + /* ラベル表を表示する */ void printlabel() { - int i; - LABELTAB *np; + int i, asize = 0; + LABELTAB *np = malloc(sizeof(LABELTAB *)); + LABELARRAY **ar = malloc(labelcnt * sizeof(LABELARRAY **)); + for(i = 0; i < LABELTABSIZE; i++) { for(np = labels[i]; np != NULL; np = np->next) { - if(np->prog == NULL) { - fprintf(stdout, "%s ---> #%04X\n", np->label, np->adr); - } else { - fprintf(stdout, "%s.%s ---> #%04X\n", np->prog, np->label, np->adr); - } + ar[asize] = malloc(sizeof(LABELARRAY *)); + ar[asize]->prog = (np->prog == NULL ? NULL : strdup(np->prog)); + ar[asize]->label = strdup(np->label); + ar[asize]->adr = np->adr; + asize++; + } + } + qsort(ar, asize, sizeof(*ar), compare_adr); + for(i = 0; i < asize; i++) { + if(ar[i]->prog != NULL) { + fprintf(stdout, "%s.", ar[i]->prog); } + fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr); } } @@ -86,7 +102,7 @@ void freelabel() int i; LABELTAB *np, *nq; for(i = 0; i < LABELTABSIZE; i++) { - for(np = labels[i]; np != NULL; np = nq){ + for(np = labels[i]; np != NULL; np = nq) { nq = np->next; free(np->prog); free(np->label); diff --git a/test/integration/casl2/hello/0.txt b/test/integration/casl2/hello/0.txt index 95e88bc..a19f1d1 100644 --- a/test/integration/casl2/hello/0.txt +++ b/test/integration/casl2/hello/0.txt @@ -9,9 +9,9 @@ Assemble ../../../../as/hello.casl (0) ../../../../as/hello.casl: 6: END Label:::: -MAIN.LEN ---> #0020 MAIN ---> #0000 MAIN.OBUF ---> #0013 +MAIN.LEN ---> #0020 Assemble ../../../../as/hello.casl (1) ../../../../as/hello.casl: 1:MAIN START diff --git a/test/integration/casl2/opt_sL/0.txt b/test/integration/casl2/opt_sL/0.txt index 631d972..82b7bf2 100644 --- a/test/integration/casl2/opt_sL/0.txt +++ b/test/integration/casl2/opt_sL/0.txt @@ -20,9 +20,9 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 diff --git a/test/integration/casl2/opt_slA/0.txt b/test/integration/casl2/opt_slA/0.txt index c6612f3..3697893 100644 --- a/test/integration/casl2/opt_slA/0.txt +++ b/test/integration/casl2/opt_slA/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl diff --git a/test/integration/casl2/opt_slaO/0.txt b/test/integration/casl2/opt_slaO/0.txt index 15d65fc..94a889c 100644 --- a/test/integration/casl2/opt_slaO/0.txt +++ b/test/integration/casl2/opt_slaO/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl diff --git a/test/integration/casl2/opt_slaOn/0.txt b/test/integration/casl2/opt_slaOn/0.txt index 3abb87d..640f1dd 100644 --- a/test/integration/casl2/opt_slaOn/0.txt +++ b/test/integration/casl2/opt_slaOn/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl diff --git a/test/integration/casl2/opt_slaoT/0.txt b/test/integration/casl2/opt_slaoT/0.txt index 2e4d9aa..003a22e 100644 --- a/test/integration/casl2/opt_slaoT/0.txt +++ b/test/integration/casl2/opt_slaoT/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl diff --git a/test/integration/casl2/opt_slaot/0.txt b/test/integration/casl2/opt_slaot/0.txt index 45823bf..2073d84 100644 --- a/test/integration/casl2/opt_slaot/0.txt +++ b/test/integration/casl2/opt_slaot/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl diff --git a/test/integration/casl2/opt_slaotd/0.txt b/test/integration/casl2/opt_slaotd/0.txt index 7cf50cb..afd4055 100644 --- a/test/integration/casl2/opt_slaotd/0.txt +++ b/test/integration/casl2/opt_slaotd/0.txt @@ -20,12 +20,12 @@ Assemble ../../../../as/sum_10.casl (0) ../../../../as/sum_10.casl: 17: END Label:::: -MAIN.STEP ---> #0013 -MAIN.LST ---> #0012 -MAIN.FIN ---> #000F MAIN ---> #0000 -MAIN.FST ---> #0011 MAIN.LOOP ---> #0006 +MAIN.FIN ---> #000F +MAIN.FST ---> #0011 +MAIN.LST ---> #0012 +MAIN.STEP ---> #0013 Assemble ../../../../as/sum_10.casl (1) ../../../../as/sum_10.casl: 1:;;; sum_10.casl