projects
/
YACASL2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
exec.cを一部変更
[YACASL2.git]
/
src
/
label.c
diff --git
a/src/label.c
b/src/label.c
index
2c4214e
..
85fb172
100644
(file)
--- a/
src/label.c
+++ b/
src/label.c
@@
-11,11
+11,22
@@
static int labelcnt = 0; /* ラベル数 */
static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
static int labelcnt = 0; /* ラベル数 */
static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
-#ifndef UNITTEST
-static unsigned labelhash(const char *prog, const char *label);
+/**
+ * ラベルのエラー定義
+ */
+static CERR cerr_label[] = {
+ { 101, "label already defined" },
+ { 102, "label table is full" },
+ { 103, "label not found" },
+};
-static int compare_adr(const void *a, const void *b);
-#endif
+/**
+ * ラベルのエラーをエラーリストに追加
+ */
+void addcerrlist_label()
+{
+ addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
+}
/**
* プログラム名とラベルに対応するハッシュ値を返す
/**
* プログラム名とラベルに対応するハッシュ値を返す
@@
-23,33
+34,39
@@
static int compare_adr(const void *a, const void *b);
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
- int i = 0;
+ int i = 0, j;
+ unsigned h;
if(prog != NULL) {
if(prog != NULL) {
- keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key
[]
");
+ keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
keys[i]->type = CHARS;
keys[i]->type = CHARS;
- keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val");
+ keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
+ i++;
}
}
- keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key
[]
");
+ keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
keys[i]->type = CHARS;
keys[i]->type = CHARS;
- keys[i]->val.s = strdup_chk(label, "labelhash.key[].val");
- /* ハッシュ値を返す */
- return hash(i+1, keys, LABELTABSIZE);
+ keys[i]->val.s = strdup_chk(label, "labelhash.key.val");
+ h = hash(i+1, keys, LABELTABSIZE);
+ for(j = 0; j < i + 1; j++) {
+ FREE(keys[j]->val.s);
+ FREE(keys[j]);
+ }
+ return h;
}
/**
}
/**
- * ã
\83
©ã
\83
\99
ã
\83
«è¡¨ã
\81
\8b
ã
\82
\89
ã
\82
¢ã
\83
\89
ã
\83
‹
\82
¹ã
\82
\92
検索する
+ * ã
\83
\97
ã
\83
ã
\82
°ã
\83
©ã
\83
å
\90
\8d
ã
\81
¨ã
\83
©ã
\83
\99
ã
\83
«ã
\81
«å¯¾å¿
\9c
ã
\81
\99
ã
\82
\8b
ã
\82
¢ã
\83
\89
ã
\83
‹
\82
¹ã
\82
\92
ã
\83
©ã
\83
\99
ã
\83
«è¡¨ã
\81
\8b
ã
\82
\89
検索する
*/
WORD getlabel(const char *prog, const char *label)
{
assert(label != NULL);
*/
WORD getlabel(const char *prog, const char *label)
{
assert(label != NULL);
- LABELTAB *
n
p;
+ LABELTAB *p;
- for(
np = labels[labelhash(prog, label)]; np != NULL; np = n
p->next) {
- if((prog == NULL || (
np->prog != NULL && strcmp(prog, n
p->prog) == 0)) &&
- strcmp(label,
n
p->label) == 0)
+ for(
p = labels[labelhash(prog, label)]; p != NULL; p =
p->next) {
+ if((prog == NULL || (
p->prog != NULL && strcmp(prog,
p->prog) == 0)) &&
+ strcmp(label, p->label) == 0)
{
{
- return
n
p->adr;
+ return p->adr;
}
}
return 0xFFFF;
}
}
return 0xFFFF;
@@
-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);
bool addlabel(const char *prog, const char *label, WORD adr)
{
assert(label != NULL);
- LABELTAB *
n
p;
+ LABELTAB *p;
unsigned hashval;
/* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
unsigned hashval;
/* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
@@
-70,23
+87,23
@@
bool addlabel(const char *prog, const char *label, WORD adr)
return false;
}
/* メモリを確保 */
return false;
}
/* メモリを確保 */
-
n
p = malloc_chk(sizeof(LABELTAB), "labels.next");
+ p = malloc_chk(sizeof(LABELTAB), "labels.next");
/* プログラム名を設定 */
if(prog == NULL) {
/* プログラム名を設定 */
if(prog == NULL) {
-
n
p->prog = NULL;
+ p->prog = NULL;
} else {
} else {
-
n
p->prog = strdup_chk(prog, "labels.prog");
+ p->prog = strdup_chk(prog, "labels.prog");
}
/* ラベルを設定 */
}
/* ラベルを設定 */
-
n
p->label = strdup_chk(label, "labels.label");
+ p->label = strdup_chk(label, "labels.label");
/* アドレスを設定 */
/* アドレスを設定 */
-
n
p->adr = adr;
+ p->adr = adr;
/* ラベル数を設定 */
labelcnt++;
/* ハッシュ表へ追加 */
hashval = labelhash(prog, label);
/* ラベル数を設定 */
labelcnt++;
/* ハッシュ表へ追加 */
hashval = labelhash(prog, label);
-
n
p->next = labels[hashval];
- labels[hashval] =
n
p;
+ p->next = labels[hashval];
+ labels[hashval] = p;
return true;
}
return true;
}
@@
-103,30
+120,32
@@
int compare_adr(const void *a, const void *b)
*/
void printlabel()
{
*/
void printlabel()
{
- int i,
asize
= 0;
- LABELTAB *
n
p;
- LABELARRAY *
ar[labelcnt]
;
+ int i,
s
= 0;
+ LABELTAB *p;
+ LABELARRAY *
*l
;
+ l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
for(i = 0; i < LABELTABSIZE; i++) {
for(i = 0; i < LABELTABSIZE; i++) {
- for(
np = labels[i]; np != NULL; np = n
p->next) {
- assert(
n
p->label != NULL);
-
ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]
");
- if(
n
p->prog == NULL) {
-
ar[asize
]->prog = NULL;
+ for(
p = labels[i]; p != NULL; p =
p->next) {
+ assert(p->label != NULL);
+
l[s] = malloc_chk(sizeof(LABELARRAY), "lables
");
+ if(p->prog == NULL) {
+
l[s
]->prog = NULL;
} else {
} else {
-
ar[asize]->prog = strdup_chk(np->prog, "ar[]
.prog");
+
l[s]->prog = strdup_chk(p->prog, "labels
.prog");
}
}
-
ar[asize]->label = strdup_chk(np->label, "ar[]
.label");
-
ar[asize++]->adr = n
p->adr;
+
l[s]->label = strdup_chk(p->label, "labels
.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);
}
}
+ FREE(l);
}
/**
}
/**
@@
-135,16
+154,14
@@
void printlabel()
void freelabel()
{
int i;
void freelabel()
{
int i;
- LABELTAB *
np, *n
q;
+ LABELTAB *
p, *
q;
for(i = 0; i < LABELTABSIZE; i++) {
for(i = 0; i < LABELTABSIZE; i++) {
- for(np = labels[i]; np != NULL; np = nq) {
- nq = np->next;
- if(np->prog != NULL) {
- free_chk(np->prog, "np.prog");
- }
- free_chk(np->label, "np.label");
- free_chk(np, "np");
+ for(p = labels[i]; p != NULL; p = q) {
+ q = p->next;
+ FREE(p->prog);
+ FREE(p->label);
+ FREE(p);
}
}
}
}
}
}