#include "casl2.h"
#include "assemble.h"
-/* 値を格納するポインタ */
-WORD ptr;
+/* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
+ASMODE asmode = {false, false, false, false, false};
-/* ã\83ªã\83\86ã\83©ã\83«ï¼\88=ä»\98ã\81\8dã\81®å\80¤ï¼\89ã\82\92æ ¼ç´\8dã\81\99ã\82\8bã\83\9dã\82¤ã\83³ã\82¿ */
-WORD lptr;
+/* ã\82¢ã\82»ã\83³ã\83\96ã\83«æ\99\82ã\81®ã\83\97ã\83ã\83\91ã\83\86ã\82£ */
+ASPROP *asprop;
-/* 他のプログラムで参照する入口名 */
-char *prog;
+/* アセンブルのエラー定義 */
+CERR cerr_assemble[] = {
+ { 101, "label already defined" },
+ { 102, "label table is full" },
+ { 103, "label not found" },
+ { 104, "label length is too long" },
+ { 105, "no command in the line" },
+ { 106, "operand mismatch in assemble command" },
+ { 107, "no label in START" },
+ { 108, "not command of operand \"r\"" },
+ { 109, "not command of operand \"r1,r2\"" },
+ { 110, "not command of operand \"r,adr[,x]\"" },
+ { 111, "not command of operand \"adr[,x]\"" },
+ { 112, "not command of no operand" },
+ { 113, "operand too many in COMET II command" },
+ { 117, "operand too many in DC" },
+ { 118, "operand length too long" },
+ { 119, "out of COMET II memory" },
+ { 120, "GR0 in operand x" },
+ { 121, "cannot get operand token" },
+ { 122, "cannot create hash table" },
+ { 123, "unclosed quote" },
+ { 124, "more than one character in literal" },
+ { 125, "not GR in operand x" },
+};
-/* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
-ASMODE asmode = {false, false, false, false, false};
+bool addcerrlist_assemble()
+{
+ return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
+}
/* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */
/* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */
WORD r;
/* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
- (*(str+2) >= '0' && *(str+2) <= '7')))
+ (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
{
return 0xFFFF;
}
r = (WORD)(*(str+2) - '0');
- /* 指標レジスタとして用いることはできない */
+ /* GR0は指標レジスタとして用いることができない */
if(is_x == true && r == 0x0) {
setcerr(120, NULL); /* GR0 in operand x */
return 0x0;
WORD adr = 0x0;
if(*str == '=') {
adr = getliteral(str, pass);
- } else if((*str == '#') || isdigit(*str) || *str == '-') {
+ } else if(isdigit(*str) || *str == '-' || *str == '#') {
adr = nh2word(str);
} else {
if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
bool writememory(WORD word, WORD adr, PASS pass)
{
bool status = false;
+
/* COMET IIメモリオーバーの場合 */
- if(adr >= memsize) {
+ if(adr >= sys->memsize) {
setcerr(119, word2n(adr)); /* out of COMET II memory */
}
- if(cerrno == 0) {
- memory[adr] = word;
- if(pass == SECOND && (&asmode)->asdetail == true) {
+ if(cerr->num == 0) {
+ (sys->memory)[adr] = word;
+ if(pass == SECOND && asmode.asdetail == true) {
fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
}
status = true;
/* リテラルには、10進定数/16進定数/文字定数が含まれる */
WORD getliteral(const char *str, PASS pass)
{
- WORD adr = lptr;
+ WORD adr = asprop->lptr;
assert(*str == '=');
if(*(++str) == '\'') { /* 文字定数 */
writestr(str, true, pass);
} else {
- writememory(nh2word(str), lptr++, pass);
+ writememory(nh2word(str), (asprop->lptr)++, pass);
}
return adr;
}
/* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */
void writestr(const char *str, bool literal, PASS pass)
{
- assert(cerrno == 0 && *str == '\'');
+ assert(cerr->num == 0 && *str == '\'');
const char *p = str + 1;
bool lw = false;
for(; ;) {
/* 閉じ「'」がないまま文字列が終了した場合 */
if(*p == '\0') {
- setcerr(123, str); /* illegal string */
+ setcerr(123, str); /* unclosed quote */
break;
}
/* 「'」の場合、次の文字が「'」でない場合は正常終了 */
}
/*リテラルの場合はリテラル領域に書込 */
if(literal == true) {
- writememory(*(p++), lptr++, pass);
+ writememory(*(p++), (asprop->lptr)++, pass);
lw = true;
} else {
- writememory(*(p++), ptr++, pass);
+ writememory(*(p++), (asprop->ptr)++, pass);
}
}
}
if(*str == '#' || isdigit(*str) || *str == '-') {
adr = nh2word(str);
} else {
- if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) {
+ if(pass == SECOND && (adr = getlabel(asprop->prog, str)) == 0xFFFF) {
setcerr(103, str); /* label not found */
}
}
- writememory(adr, ptr++, pass);
+ writememory(adr, (asprop->ptr)++, pass);
}
}
bool assemblecmd(const CMDLINE *cmdl, PASS pass)
{
int i = 0;
- CASLCMD cmd = 0;
+ ASCMDID cmdid = 0;
bool status = false;
- CMDARRAY ascmd[] = {
+ ASCMD ascmd[] = {
{ START, 0, 1, "START" },
{ END, 0, 0, "END" },
{ DC, 1, OPDSIZE, "DC" },
setcerr(106, NULL); /* operand count mismatch */
return false;
}
- cmd = ascmd[i].cmdid;
+ cmdid = ascmd[i].cmdid;
break;
}
} while(ascmd[++i].cmdid != 0);
/* アセンブラ命令 */
- switch(cmd)
+ switch(cmdid)
{
case START:
if(cmdl->label == NULL) {
return false;
}
/* プログラム名の設定 */
- prog = strdup(cmdl->label);
+ asprop->prog = strdup_chk(cmdl->label, "asprop.prog");
/* オペランドがある場合、実行開始番地を設定 */
if(pass == SECOND && cmdl->opd->opdc == 1) {
- if((startptr = getlabel(prog, cmdl->opd->opdv[0])) == 0xFFFF) {
+ if((prog->start = getlabel(asprop->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
setcerr(103, cmdl->opd->opdv[0]); /* label not found */
}
}
case END:
/* リテラル領域の設定 */
if(pass == FIRST) {
- lptr = ptr;
+ asprop->lptr = asprop->ptr;
}
/* 実行終了番地と次のプログラムの実行開始番地を設定 */
else if(pass == SECOND) {
- endptr = lptr;
+ prog->end = asprop->lptr;
}
- prog = NULL;
+ asprop->prog = NULL;
status = true;
break;
case DS:
for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
- writememory(0x0, ptr++, pass);
- if(cerrno > 0) {
+ writememory(0x0, (asprop->ptr)++, pass);
+ if(cerr->num > 0) {
return false;
}
}
case DC:
for(i = 0; i < cmdl->opd->opdc; i++) {
writeDC(cmdl->opd->opdv[i], pass);
- if(cerrno > 0) {
+ if(cerr->num > 0) {
return false;
}
}
default:
return false;
}
- if(cerrno > 0) {
+ if(cerr->num > 0) {
status = false;
}
return status;
bool macrocmd(const CMDLINE *cmdl, PASS pass)
{
int i = 0;
- CASLCMD cmd;
+ MACROCMDID cmdid;
bool status = false;
- CMDARRAY macrocmd[] = {
+ MACROCMD macrocmd[] = {
{ IN, 2, 2, "IN" },
{ OUT, 2, 2, "OUT" },
{ RPUSH, 0, 0, "RPUSH" },
do {
if(strcmp(cmdl->cmd, macrocmd[i].cmd) == 0) {
- if(cmdl->opd->opdc < macrocmd[i].opdc_min || cmdl->opd->opdc > macrocmd[i].opdc_max) {
+ if(cmdl->opd->opdc < macrocmd[i].opdc_min ||
+ cmdl->opd->opdc > macrocmd[i].opdc_max)
+ {
setcerr(106, NULL); /* operand count mismatch */
return false;
}
- cmd = macrocmd[i].cmdid;
+ cmdid = macrocmd[i].cmdid;
break;
}
} while(macrocmd[++i].cmdid != 0);
- switch(cmd)
+ switch(cmdid)
{
case IN:
status = writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass);
setcerr(112, cmdl->cmd); /* not command of no operand */
return false;
}
- if(writememory(cmd, ptr++, pass) == true) {
+ if(writememory(cmd, (asprop->ptr)++, pass) == true) {
status = true;
}
}
return false;
}
cmd |= (r1 << 4);
- if(writememory(cmd, ptr++, pass) == true) {
+ if(writememory(cmd, (asprop->ptr)++, pass) == true) {
status = true;
}
}
return false;
}
cmd |= ((r1 << 4) | r2);
- if(cerrno == 0 && writememory(cmd, ptr++, pass) == true) {
+ if(cerr->num == 0 && writememory(cmd, (asprop->ptr)++, pass) == true) {
status = true;
}
}
- /* オペランド数2〜3。第2オペランドはアドレス、
- 第3オペランドは指標レジスタとして用いる汎用レジスタ */
+ /* オペランド数2〜3。第2オペランドはアドレス、 */
+ /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) {
if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF &&
(cmd = getcmdcode(cmdl->cmd, R_ADR_X)) == 0xFFFF)
}
cmd |= x;
}
- adr = getadr(prog, cmdl->opd->opdv[1], pass);
- writememory(cmd, ptr++, pass);
- writememory(adr, ptr++, pass);
- if(cerrno == 0) {
+ adr = getadr(asprop->prog, cmdl->opd->opdv[1], pass);
+ writememory(cmd, (asprop->ptr)++, pass);
+ writememory(adr, (asprop->ptr)++, pass);
+ if(cerr->num == 0) {
status = true;
}
} else {
- setcerr(113, cmdl->cmd); /* command not defined */
+ setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
return false;
}
}
/* オペランド数2の場合、第2オペランドは指標レジスタとして用いる汎用レジスタ */
if(cmdl->opd->opdc == 2) {
x = getgr(cmdl->opd->opdv[1], true);
- if(cerrno > 0) {
+ if(cerr->num > 0) {
return false;
}
cmd |= x;
adr = getlabel(NULL, cmdl->opd->opdv[0]);
}
if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) {
- adr = getadr(prog, cmdl->opd->opdv[0], pass);
+ adr = getadr(asprop->prog, cmdl->opd->opdv[0], pass);
}
- writememory(cmd, ptr++, pass);
- writememory(adr, ptr++, pass);
- if(cerrno == 0) {
+ writememory(cmd, (asprop->ptr)++, pass);
+ writememory(adr, (asprop->ptr)++, pass);
+ if(cerr->num == 0) {
status = true;
}
}
}
}
/* アセンブラ命令の処理 */
- else if(cerrno == 0 && assemblecmd(cmdl, pass) == true) {
+ else if(cerr->num == 0 && assemblecmd(cmdl, pass) == true) {
;
}
/* マクロ命令の書込 */
- else if(cerrno == 0 && macrocmd(cmdl, pass) == true) {
+ else if(cerr->num == 0 && macrocmd(cmdl, pass) == true) {
;
}
/* 機械語命令の書込 */
- else if(cerrno == 0 && cometcmd(cmdl, pass) == true) {
+ else if(cerr->num == 0 && cometcmd(cmdl, pass) == true) {
;
}
- else if(cerrno == 0) {
- setcerr(113, cmdl->cmd); /* command not defined */
+ else if(cerr->num == 0) {
+ setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
}
/* エラーが発生していないか確認 */
- if(cerrno == 0) {
+ if(cerr->num == 0) {
status = true;
}
return status;
}
+void printline(FILE *stream, const char *filename, int lineno, char *line) {
+ fprintf(stream, "%s:%5d:%s", filename, lineno, line);
+}
+
/* 指定された名前のファイルをアセンブル */
/* 2回実行される */
bool assemble(const char *file, PASS pass)
char *line;
FILE *fp;
- if(create_cmdtype_code() == false) {
- return false;
- }
+ addcerrlist_assemble();
if((fp = fopen(file, "r")) == NULL) {
perror(file);
return false;
}
for(; ;) {
- cmdl = malloc(sizeof(CMDLINE));
- line = malloc(LINESIZE+1);
+ cmdl = malloc_chk(sizeof(CMDLINE), "cmdl");
+ line = malloc_chk(LINESIZE + 1, "line");
if((line = fgets(line, LINESIZE, fp)) == NULL) {
break;
}
lineno++;
- if((pass == FIRST && (&asmode)->src == true) ||
- (pass == SECOND && (&asmode)->asdetail == true))
+ if((pass == FIRST && asmode.src == true) ||
+ (pass == SECOND && asmode.asdetail == true))
{
- fprintf(stdout, "%s:%5d:%s", file, lineno, line);
+ printline(stdout, file, lineno, line);
}
if((cmdl = linetok(line)) != NULL) {
if(pass == FIRST && cmdl->label != NULL) {
- if(addlabel(prog, cmdl->label, ptr) == false) {
+ if(addlabel(asprop->prog, cmdl->label, asprop->ptr) == false) {
break;
}
}
break;
}
}
- if(cerrno > 0) {
+ if(cerr->num > 0) {
break;
}
}
- if(cerrno > 0) {
- fprintf(stderr, "Assemble error - %d: %s\n %s:%d: %s\n", cerrno, cerrmsg, file, lineno, line);
+ if(cerr->num > 0) {
+ fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg);
+ printline(stderr, file, lineno, line);
status = false;
}
fclose(fp);