projects
/
YACASL2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ソースの修正
[YACASL2.git]
/
src
/
label.c
diff --git
a/src/label.c
b/src/label.c
index
1b9b806
..
69a37dc
100644
(file)
--- a/
src/label.c
+++ b/
src/label.c
@@
-1,48
+1,84
@@
-#include "casl2.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "cerr.h"
+#include "cmem.h"
+#include "hash.h"
#include "assemble.h"
#include "assemble.h"
-int labelcnt = 0; /* ラベル数 */
-LABELTAB *labels[LABELTABSIZE]; /* ラベル表 */
+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);
+}
+
+/**
+ * プログラム名とラベルに対応するハッシュ値を返す
+ */
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;
}
}
-/* ラベル表からアドレスを検索する */
+/**
+ * プログラム名とラベルに対応するアドレスをラベル表から検索する
+ */
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;
}
-/* プログラム名、ラベル、アドレスをラベル表に追加する */
+/**
+ * プログラム名、ラベル、アドレスをラベル表に追加する
+ */
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;
/* 登録されたラベルを検索。すでに登録されている場合はエラー発生 */
@@
-51,74
+87,80
@@
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;
}
+/**
+ * ラベルを比較した結果を返す
+ */
int compare_adr(const void *a, const void *b)
{
return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
}
int compare_adr(const void *a, const void *b)
{
return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
}
-/* ラベル表を表示する */
+/**
+ * ラベル表を表示する
+ */
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);
}
}
}
}
-/* ラベル表を解放する */
+/**
+ * ラベル表を解放する
+ */
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(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);
}
}
}
}
}
}