projects
/
YACASL2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
SRLコマンドテストパターンを追加
[YACASL2.git]
/
src
/
label.c
diff --git
a/src/label.c
b/src/label.c
index
a8baa1d
..
71285a0
100644
(file)
--- a/
src/label.c
+++ b/
src/label.c
@@
-8,19
+8,52
@@
#include "hash.h"
#include "assemble.h"
#include "hash.h"
#include "assemble.h"
-static int labelcnt = 0; /* ラベル数 */
-static LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
-
/**
* プログラム名とラベルに対応するハッシュ値を返す
/**
* プログラム名とラベルに対応するハッシュ値を返す
+ *
+ * @return ハッシュ値
+ *
+ * @param prog プログラム名
+ * @param label ラベル
+ */
+unsigned labelhash(const char *prog, const char *label);
+
+/**
+ * ラベルを比較した結果を返す。qsort内で使われる関数
+ *
+ * @return ラベルが同一の場合は0、異なる場合は0以外
+ *
+ * @param *a ラベルa
+ * @param *b ラベルb
+ */
+int compare_adr(const void *a, const void *b);
+
+/**
+ * @brief ラベル数
+ */
+static int labelcnt = 0;
+
+/**
+ * @brief ラベル表
*/
*/
+static LABELTAB *labels[LABELTABSIZE];
+
+/**
+ * @brief ラベルのエラー
+ */
+static CERR cerr_label[] = {
+ { 101, "label already defined" },
+ { 102, "label table is full" },
+ { 103, "label not found" },
+};
+
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
int i = 0, j;
unsigned h;
unsigned labelhash(const char *prog, const char *label)
{
HKEY *keys[2];
int i = 0, j;
unsigned h;
- if(
prog != NULL
) {
+ if(
*prog != '\0'
) {
keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
keys[i]->type = CHARS;
keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
keys[i]->type = CHARS;
keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
@@
-37,16
+70,24
@@
unsigned labelhash(const char *prog, const char *label)
return h;
}
return h;
}
-/**
- * プログラム名とラベルに対応するアドレスをラベル表から検索する
- */
+int compare_adr(const void *a, const void *b)
+{
+ return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
+}
+
+/* assemble.hで定義された関数群 */
+void addcerrlist_label()
+{
+ addcerrlist(ARRAYSIZE(cerr_label), cerr_label);
+}
+
WORD getlabel(const char *prog, const char *label)
{
WORD getlabel(const char *prog, const char *label)
{
- assert(label != NULL);
+ assert(
prog != NULL &&
label != NULL);
LABELTAB *p;
for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
LABELTAB *p;
for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
- if((
prog == NULL || (p->prog != NULL &&
strcmp(prog, p->prog) == 0)) &&
+ if((
*prog == '\0' || (
strcmp(prog, p->prog) == 0)) &&
strcmp(label, p->label) == 0)
{
return p->adr;
strcmp(label, p->label) == 0)
{
return p->adr;
@@
-55,13
+96,10
@@
WORD getlabel(const char *prog, const char *label)
return 0xFFFF;
}
return 0xFFFF;
}
-/**
- * プログラム名、ラベル、アドレスをラベル表に追加する
- */
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,68
+108,51
@@
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) {
- np->prog = NULL;
- } else {
- np->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;
}
-/**
- * ラベルを比較した結果を返す
- */
-int compare_adr(const void *a, const void *b)
-{
- return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
-}
-
-/**
- * ラベル表を表示する
- */
void printlabel()
{
void printlabel()
{
- int i,
asize
= 0;
+ int i,
s
= 0;
LABELTAB *p;
LABELTAB *p;
- LABELARRAY *
ar[labelcnt]
;
+ LABELARRAY *
*l
;
+ l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
for(i = 0; i < LABELTABSIZE; i++) {
for(p = labels[i]; p != NULL; p = p->next) {
assert(p->label != NULL);
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[]");
- if(p->prog == NULL) {
- ar[asize]->prog = NULL;
- } else {
- ar[asize]->prog = strdup_chk(p->prog, "ar[].prog");
- }
- ar[asize]->label = strdup_chk(p->label, "ar[].label");
- ar[asize++]->adr = p->adr;
+ l[s] = malloc_chk(sizeof(LABELARRAY), "lables");
+ l[s]->prog = strdup_chk(p->prog, "labels.prog");
+ 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) != '\0'
) {
+ 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[i]->prog);
+ FREE(l[i]->label);
+ FREE(l[i]);
}
}
+ FREE(l);
}
}
-/**
- * ラベル表を解放する
- */
void freelabel()
{
int i;
void freelabel()
{
int i;