#include "casl2.h"
#include "assemble.h"
+/* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */
+ASMODE asmode = {false, false, false, false, false};
+
/* 値を格納するポインタ */
WORD ptr;
/* 他のプログラムで参照する入口名 */
char *prog;
-/* 汎用レジスタを表す文字列「GR[0-7]」をWORD値に変換 */
-/* is_xがtrueの場合は、指標レジスタとして用いる汎用レジスタ */
+/* アセンブルのエラー定義 */
+CERRARRAY 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" },
+};
+
+bool addcerrlist_assemble()
+{
+ return addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
+}
+
+/* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */
/* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */
+/* is_xがtrueの場合は指標レジスタ。GR0は、COMET IIの仕様により、エラー発生 */
WORD getgr(const char *str, bool is_x)
{
assert(str != NULL);
WORD r;
+ /* 「GR[0-7]」以外の文字列では、0xFFFFを返して終了 */
if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
(*(str+2) >= '0' && *(str+2) <= '7')))
{
return 0xFFFF;
}
r = (WORD)(*(str+2) - '0');
- /* COMET IIの仕様により、GR0は指標レジスタとして用いることはできない */
+ /* GR0は指標レジスタとして用いることができない */
if(is_x == true && r == 0x0) {
setcerr(120, NULL); /* GR0 in operand x */
return 0x0;
bool writememory(WORD word, WORD adr, PASS pass)
{
bool status = false;
+
/* COMET IIメモリオーバーの場合 */
if(adr >= memsize) {
setcerr(119, word2n(adr)); /* out of COMET II memory */
}
if(cerrno == 0) {
memory[adr] = word;
- if(pass == SECOND && asdetailmode == true) {
+ if(pass == SECOND && asmode.asdetail == true) {
fprintf(stdout, "\t#%04X\t#%04X\n", adr, word);
}
status = true;
{
WORD adr = lptr;
assert(*str == '=');
- str++;
- if(*str == '\'') { /* 文字定数 */
+ if(*(++str) == '\'') { /* 文字定数 */
writestr(str, true, pass);
} else {
writememory(nh2word(str), lptr++, pass);
void writestr(const char *str, bool literal, PASS pass)
{
assert(cerrno == 0 && *str == '\'');
- str++;
- while(*str != '\0') {
- if(*str == '\'') {
- if(*(str+1) != '\'') {
- break;
- }
- str++;
+ const char *p = str + 1;
+ bool lw = false;
+
+ for(; ;) {
+ /* 閉じ「'」がないまま文字列が終了した場合 */
+ if(*p == '\0') {
+ setcerr(123, str); /* unclosed quote */
+ break;
+ }
+ /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
+ if(*p == '\'' && *(++p) != '\'') {
+ break;
+ } else if(literal == true && lw == true) {
+ setcerr(124, str); /* more than one character in literal */
+ break;
}
/*リテラルの場合はリテラル領域に書込 */
if(literal == true) {
- writememory(*(str++), lptr++, pass);
+ writememory(*(p++), lptr++, pass);
+ lw = true;
} else {
- writememory(*(str++), ptr++, pass);
+ writememory(*(p++), ptr++, pass);
}
}
}
CMDARRAY ascmd[] = {
{ START, 0, 1, "START" },
{ END, 0, 0, "END" },
- { DC, 0, OPDSIZE, "DC" },
+ { DC, 1, OPDSIZE, "DC" },
{ DS, 1, 1, "DS" },
{ 0, 0, 0, NULL }
};
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)
/* オペランド数3 */
if(cmdl->opd->opdc == 3) {
if((x = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
+ setcerr(125, cmdl->cmd); /* not GR in operand x */
return false;
}
cmd |= x;
status = true;
}
} else {
- setcerr(113, cmdl->cmd); /* command not defined */
+ setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
return false;
}
}
;
}
else if(cerrno == 0) {
- setcerr(113, cmdl->cmd); /* command not defined */
+ setcerr(113, cmdl->cmd); /* operand too many in COMET II command */
}
/* エラーが発生していないか確認 */
if(cerrno == 0) {
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);
+ line = malloc(LINESIZE + 1);
if((line = fgets(line, LINESIZE, fp)) == NULL) {
break;
}
lineno++;
- if((pass == FIRST && srcmode == true) || (pass == SECOND && asdetailmode == true)) {
- fprintf(stdout, "%s:%5d:%s", file, lineno, line);
+ if((pass == FIRST && asmode.src == true) ||
+ (pass == SECOND && asmode.asdetail == true))
+ {
+ printline(stdout, file, lineno, line);
}
if((cmdl = linetok(line)) != NULL) {
if(pass == FIRST && cmdl->label != NULL) {
}
}
if(cerrno > 0) {
- fprintf(stderr, "Assemble error - %d: %s\n %s:%d: %s\n", cerrno, cerrmsg, file, lineno, line);
+ fprintf(stderr, "Assemble error - %d: %s\n", cerrno, cerrmsg);
+ printline(stderr, file, lineno, line);
status = false;
}
fclose(fp);