From 6b30a23640168f0b99e70ad87ab4c5a98015ee02 Mon Sep 17 00:00:00 2001 From: j8takagi Date: Thu, 17 Mar 2011 23:27:21 +0900 Subject: [PATCH] =?utf8?q?=E3=82=BD=E3=83=BC=E3=82=B9=E3=81=AE=E4=BF=AE?= =?utf8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- include/assemble.h | 5 ++++ src/assemble.c | 1 + src/cerr.c | 16 ++++++------- src/cmd.c | 10 ++++---- src/label.c | 57 ++++++++++++++++++++++++++++++---------------- src/struct.c | 2 +- src/token.c | 2 +- 7 files changed, 58 insertions(+), 35 deletions(-) diff --git a/include/assemble.h b/include/assemble.h index aa2b599..f702328 100644 --- a/include/assemble.h +++ b/include/assemble.h @@ -119,6 +119,11 @@ typedef enum { SECOND = 1, } PASS; +/** + * ラベルのエラーをエラーリストに追加 + */ +void addcerrlist_label(); + /** * プログラム名とラベルに対応するアドレスをラベル表から検索する */ diff --git a/src/assemble.c b/src/assemble.c index 0e29454..35c3e9f 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -626,6 +626,7 @@ void addcerrlist_assemble() { addcerrlist_tok(); addcerrlist_word(); + addcerrlist_label(); addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble); } diff --git a/src/cerr.c b/src/cerr.c index d595275..6b9ab40 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -10,7 +10,7 @@ */ void cerr_init() { - cerr = malloc_chk(sizeof(CERR), "cerr"); + cerr = malloc_chk(sizeof(CERR *), "cerr"); cerr->num = 0; } @@ -27,19 +27,19 @@ CERRLIST *cerrlist = NULL; /** * エラーリストを作成または追加する */ -void addcerrlist(int newerrc, CERR newerrv[]) +void addcerrlist(int errc, CERR errv[]) { int i; - CERRLIST *p = NULL, *q; + CERRLIST *p = NULL, *q = malloc_chk(sizeof(CERRLIST *), "cerrlist"); - assert(newerrc > 0 && newerrv != NULL); - for(i = 0; i < newerrc; i++) { + assert(errc > 0 && errv != NULL); + for(i = 0; i < errc; i++) { if(p == NULL) { - p = q = malloc_chk(sizeof(CERRLIST), "cerrlist"); + p = q; } else { - p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next"); + p = p->next = malloc_chk(sizeof(CERRLIST *), "cerrlist.next"); } - p->cerr = &newerrv[i]; + p->cerr = &errv[i]; p->next = NULL; } p->next = cerrlist; diff --git a/src/cmd.c b/src/cmd.c index 870d975..f393c9a 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -73,11 +73,11 @@ unsigned hash_cmdtype(const char *cmd, CMDTYPE type) unsigned hashval; /* 命令名を設定 */ - keys[0] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[0]"); + keys[0] = malloc_chk(sizeof(HKEY *), "hash_cmdtype.keys[0]"); keys[0]->type = CHARS; keys[0]->val.s = strdup_chk(cmd, "keys[0].val.s"); /* 命令タイプを設定 */ - keys[1] = malloc_chk(sizeof(HKEY), "hash_cmdtype.keys[1]"); + keys[1] = malloc_chk(sizeof(HKEY *), "hash_cmdtype.keys[1]"); keys[1]->type = INT; keys[1]->val.i = (int)(type & 070); /* ハッシュ値の計算 */ @@ -101,7 +101,7 @@ bool create_cmdtype_code() cmdtabsize = comet2cmdsize; /* ハッシュ表のサイズ */ cmdtype_code = calloc_chk(cmdtabsize, sizeof(CMDTAB *), "cmdtype_code"); for(i = 0; i < comet2cmdsize; i++) { - p = malloc_chk(sizeof(CMDTAB), "create_cmdtype_code.p"); + p = malloc_chk(sizeof(CMDTAB *), "create_cmdtype_code.p"); hashval = hash_cmdtype(comet2cmd[i].name, comet2cmd[i].type); /* ハッシュ値の生成 */ p->next = cmdtype_code[hashval]; /* ハッシュ表に値を追加 */ p->cmd = &comet2cmd[i]; @@ -154,7 +154,7 @@ unsigned hash_code(WORD code) unsigned h; /* 命令コードを設定 */ - keys[0] = malloc_chk(sizeof(HKEY), "hash_code.key"); + keys[0] = malloc_chk(sizeof(HKEY *), "hash_code.key"); keys[0]->type = INT; keys[0]->val.i = (int)(code >> 8); h = hash(1, keys, cmdtabsize); @@ -174,7 +174,7 @@ bool create_code_type() cmdtabsize = comet2cmdsize; /* ハッシュ表のサイズ */ code_type = calloc_chk(comet2cmdsize, sizeof(CMDTAB *), "code_type"); for(i = 0; i < comet2cmdsize; i++) { - p = malloc_chk(sizeof(CMDTAB), "code_type.p"); + p = malloc_chk(sizeof(CMDTAB *), "code_type.p"); hashval = hash_code((&comet2cmd[i])->code); /* ハッシュ値の生成 */ p->next = code_type[hashval]; /* ハッシュ表に値を追加 */ p->cmd = &comet2cmd[i]; diff --git a/src/label.c b/src/label.c index a8baa1d..04a757d 100644 --- a/src/label.c +++ b/src/label.c @@ -11,6 +11,23 @@ static int labelcnt = 0; /* ラベル数 */ static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */ +/** + * ラベルのエラー定義 + */ +static CERR cerr_label[] = { + { 101, "label already defined" }, + { 102, "label table is full" }, + { 103, "label not found" }, +}; + +/** + * ラベルのエラーをエラーリストに追加 + */ +void addcerrlist_label() +{ + addcerrlist(ARRAYSIZE(cerr_label), cerr_label); +} + /** * プログラム名とラベルに対応するハッシュ値を返す */ @@ -61,7 +78,7 @@ WORD getlabel(const char *prog, const char *label) bool addlabel(const char *prog, const char *label, WORD adr) { assert(label != NULL); - LABELTAB *np; + LABELTAB *p; unsigned hashval; /* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */ @@ -70,23 +87,23 @@ bool addlabel(const char *prog, const char *label, WORD adr) return false; } /* メモリを確保 */ - np = malloc_chk(sizeof(LABELTAB), "labels.next"); + p = malloc_chk(sizeof(LABELTAB), "labels.next"); /* プログラム名を設定 */ if(prog == NULL) { - np->prog = NULL; + p->prog = NULL; } else { - np->prog = strdup_chk(prog, "labels.prog"); + p->prog = strdup_chk(prog, "labels.prog"); } /* ラベルを設定 */ - np->label = strdup_chk(label, "labels.label"); + p->label = strdup_chk(label, "labels.label"); /* アドレスを設定 */ - np->adr = adr; + p->adr = adr; /* ラベル数を設定 */ labelcnt++; /* ハッシュ表へ追加 */ hashval = labelhash(prog, label); - np->next = labels[hashval]; - labels[hashval] = np; + p->next = labels[hashval]; + labels[hashval] = p; return true; } @@ -103,29 +120,29 @@ int compare_adr(const void *a, const void *b) */ void printlabel() { - int i, asize = 0; + int i, s = 0; LABELTAB *p; - LABELARRAY *ar[labelcnt]; + LABELARRAY *l[labelcnt]; for(i = 0; i < LABELTABSIZE; i++) { for(p = labels[i]; p != NULL; p = p->next) { assert(p->label != NULL); - ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]"); + l[s] = malloc_chk(sizeof(LABELARRAY), "l[]"); if(p->prog == NULL) { - ar[asize]->prog = NULL; + l[s]->prog = NULL; } else { - ar[asize]->prog = strdup_chk(p->prog, "ar[].prog"); + l[s]->prog = strdup_chk(p->prog, "l[].prog"); } - ar[asize]->label = strdup_chk(p->label, "ar[].label"); - ar[asize++]->adr = p->adr; + l[s]->label = strdup_chk(p->label, "l[].label"); + l[s++]->adr = p->adr; } } - qsort(ar, asize, sizeof(*ar), compare_adr); - for(i = 0; i < asize; i++) { - if(ar[i]->prog != NULL) { - fprintf(stdout, "%s.", ar[i]->prog); + qsort(l, s, sizeof(*l), compare_adr); + for(i = 0; i < s; i++) { + if(l[i]->prog != NULL) { + fprintf(stdout, "%s.", l[i]->prog); } - fprintf(stdout, "%s ---> #%04X\n", ar[i]->label, ar[i]->adr); + fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr); } } diff --git a/src/struct.c b/src/struct.c index ef9a844..725ca00 100644 --- a/src/struct.c +++ b/src/struct.c @@ -32,7 +32,7 @@ void reset(int memsize, int clocks) } sys->cpu->sp = sys->cpu->pr = sys->cpu->fr = 0x0; /* CASL2プログラムの開始と終了のアドレスを初期化 */ - execptr = malloc_chk(sizeof(execptr), "prog"); + execptr = malloc_chk(sizeof(EXECPTR *), "prog"); } /** diff --git a/src/token.c b/src/token.c index c7bfade..ce2a69b 100644 --- a/src/token.c +++ b/src/token.c @@ -105,7 +105,7 @@ CMDLINE *linetok(const char *line) bool quoting = false; CMDLINE *cmdl = NULL; - if(line == NULL || strlen(line) == 0) { + if(*line == '\0') { return NULL; } tokens = strdup_chk(line, "tokens"); -- 2.18.0