From: j8takagi Date: Wed, 8 Jul 2026 12:17:51 +0000 (+0900) Subject: リテラルをリテラルの一覧を表示する機能を追加 X-Git-Tag: v0.6p18~2 X-Git-Url: https://www.j8takagi.net/gitweb?a=commitdiff_plain;h=e0252a100450c89b469f023f0dd6998b725c5492;p=yacasl2.git リテラルをリテラルの一覧を表示する機能を追加 -l/オプションでラベル一覧を表示するときに、リテラル一覧も表示 --- diff --git a/include/assemble.h b/include/assemble.h index c824cff..08ece61 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -102,6 +102,12 @@ bool addlabel(const char *prog, const char *label, WORD adr); */ void printlabel(); +/** + * @brief リテラルを表示する + * + */ +void printliteral(); + /** * @brief ラベル表を解放する * diff --git a/src/assemble.c b/src/assemble.c index e24f0a8..0762485 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -66,10 +66,9 @@ void writememory(WORD word, WORD adr, PASS pass); * @brief 文字をメモリに書き込む * * @param *str アドレスを表す文字列。リテラル/10進定数/16進定数/アドレス定数が含まれる - * @param literal リテラルの場合はtrue * @param pass アセンブラが何回目かを表す数 */ -void writestr(const char *str, bool literal, PASS pass); +void writestr(const char *str, PASS pass); /** * @brief DC命令を書込 @@ -264,6 +263,7 @@ static CERR cerr_assemble[] = { { 122, "cannot create hash table" }, { 124, "more than one character in literal" }, { 125, "not GR in operand x" }, + { 128, "syntax error in literal" }, }; /** @@ -342,8 +342,24 @@ WORD getliteral(const char *str, PASS pass) { assert(str[0] == '='); str++; - WORD val = (str[0] == '\'') ? (WORD)str[1] : nh2word(str); + WORD val = 0; WORD adr = 0; + + if(str[0] == '\'') { + if(str[1] == '\'' && str[2] != '\'') { + setcerr(128, str); /* syntax error in literal */ + return 0; + } else if((str[1] == '\'' && str[2] == '\'' && str[3] == '\0') || str[2] == '\0' ) { + setcerr(123, str); /* unclosed quote */ + return 0; + } else if((str[1] == '\'' && str[2] == '\'' && str[3] != '\'') || str[2] != '\'') { + setcerr(124, str); /* more than one character in literal */ + return 0; + } + val = (WORD)str[1]; + } else { + val = nh2word(str); + } /* PASS FIRSTでは、lptrがまだ確定していない(ENDで初めて確定する)ため、 * 重複排除の登録・検索は行わず、従来通りダミーの書き込みのみ行う */ if(pass == FIRST) { @@ -379,29 +395,18 @@ void writememory(WORD word, WORD adr, PASS pass) } } -void writestr(const char *str, bool literal, PASS pass) +void writestr(const char *str, PASS pass) { assert(str[0] == '\''); - bool lw = false; - /* 「'」の場合、1文字スキップし、次の文字が「'」でなければ正常終了 */ + /* 「'」1文字スキップし、次の文字が「'」でなければ正常終了 */ for(int i = 1; str[i] != '\'' || str[++i] == '\''; i++) { /* 「'」が閉じないまま文字列が終了した場合はエラー */ if(!str[i]) { setcerr(123, str); /* unclosed quote */ break; } - if(literal == true && lw == true) { - setcerr(124, str); /* more than one character in literal */ - break; - } - /*リテラルの場合はリテラル領域に書込 */ - if(literal == true) { - writememory(str[i], (asptr->lptr)++, pass); - lw = true; - } else { - writememory(str[i], (asptr->ptr)++, pass); - } + writememory(str[i], (asptr->ptr)++, pass); } } @@ -410,7 +415,7 @@ void writedc(const char *str, PASS pass) WORD adr = 0; if(*str == '\'') { - writestr(str, false, pass); + writestr(str, pass); } else { if(str[0] == '#' || isdigit(str[0]) || str[0] == '-') { adr = nh2word(str); @@ -776,11 +781,15 @@ bool assemble(int filec, char *filev[], WORD adr) goto asfin; } } - if(pass == FIRST && asmode.label == true) { - fprintf(stdout, "\nLabel::::\n"); - printlabel(); - if(asmode.onlylabel == true) { - break; + if(asmode.label == true) { + if(pass == FIRST) { + printlabel(); + if(asmode.onlylabel == true) { + break; + } + } + if(pass == SECOND) { + printliteral(); } } } diff --git a/src/label.c b/src/label.c index 385922d..842e340 100644 --- a/src/label.c +++ b/src/label.c @@ -29,6 +29,15 @@ unsigned labelhash(const char *prog, const char *label); */ int compare_adr(const void *a, const void *b); +/** + * 定義されているラベルまたはリテラルの一覧を表示する + * + * @return ラベルが同一の場合は0、異なる場合は0以外 + * + * @param literal リテラルの場合はtrue、それ以外はfalse + */ +void printlabel_full(bool literal); + /** * @brief ラベル数 */ @@ -80,6 +89,37 @@ int compare_adr(const void *a, const void *b) return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr; } +void printlabel_full(bool literal) +{ + int s = 0; + LABELTAB *p = NULL; + LABELARRAY **l = NULL; + + l = calloc_chk(labelcnt, sizeof(LABELARRAY *), "printlabel_full"); + for(int i = 0; i < LABELTABSIZE; i++) { + for(p = labels[i]; p != NULL; p = p->next) { + assert(p->label->label != NULL); + if((literal == true && p->label->label[0] == '=') || (literal == false && p->label->label[0] != '=')) { + l[s++] = p->label; + } + } + } + if(s > 0) { + qsort(l, s, sizeof(*l), compare_adr); + fprintf(stdout, "\n%s::::\n", (literal == true) ? "Literal" : "Label"); + for(int i = 0; i < s; i++) { + if(l[i]->prog[0]) { + fprintf(stdout, "%s.", l[i]->prog); + } + fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr); + } + if(literal == true) { + fprintf(stdout, "\n"); + } + } + FREE(l); +} + /* assemble.hで定義された関数群 */ void addcerrlist_label() { @@ -134,25 +174,12 @@ bool addlabel(const char *prog, const char *label, WORD adr) void printlabel() { - int s = 0; - LABELTAB *p = NULL; - LABELARRAY **l = {NULL}; + printlabel_full(false); +} - l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels"); - for(int i = 0; i < LABELTABSIZE; i++) { - for(p = labels[i]; p != NULL; p = p->next) { - assert(p->label != NULL); - l[s++] = p->label; - } - } - qsort(l, s, sizeof(*l), compare_adr); - for(int i = 0; i < s; i++) { - if(l[i]->prog[0]) { - fprintf(stdout, "%s.", l[i]->prog); - } - fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr); - } - FREE(l); +void printliteral() +{ + printlabel_full(true); } void freelabel() diff --git a/test/system/casl2_smoke/hello/0.txt b/test/system/casl2_smoke/hello/0.txt index b53f2dc..4df3ce9 100644 --- a/test/system/casl2_smoke/hello/0.txt +++ b/test/system/casl2_smoke/hello/0.txt @@ -54,4 +54,9 @@ Assemble ../../../../as/sample/hello.casl (1) ../../../../as/sample/hello.casl: 5:LEN DC 13 #0020 #000D ../../../../as/sample/hello.casl: 6: END + +Literal:::: +=000A ---> #0021 +=0001 ---> #0022 + Hello, World!