X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fassemble.c;h=8b3aa61fd886d5f9061f44682c42c44e49e76315;hp=dfc4aa25f6f6a74647dda4813ae667fef46b7abe;hb=d609e3d54f40e0c4bd497a5287288d2fe3d78212;hpb=f34ec6671aa043a8d1a27ddb90e0bf9776ed27ac diff --git a/src/assemble.c b/src/assemble.c index dfc4aa2..8b3aa61 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -1,25 +1,29 @@ -#include "casl2.h" +#include +#include +#include +#include +#include + #include "assemble.h" +#include "cerr.h" -/* アセンブルモード: src, label, onlylabel, asdetail, onlyassemble */ +/** + * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble + */ ASMODE asmode = {false, false, false, false, false}; -/* 値を格納するポインタ */ -WORD ptr; - -/* リテラル(=付きの値)を格納するポインタ */ -WORD lptr; +/** + * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名: ptr, lptr, prog + */ +ASPTR *asptr; -/* 他のプログラムで参照する入口名 */ -char *prog; - -/* アセンブルのエラー定義 */ -CERRARRAY cerr_assemble[] = { +/** + * アセンブルのエラー定義 + */ +static 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\"" }, @@ -28,32 +32,58 @@ CERRARRAY cerr_assemble[] = { { 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); -} +WORD getadr(const char *prog, const char *str, PASS pass); + +WORD getgr(const char *str, bool is_x); + +WORD getliteral(const char *str, PASS pass); + +bool assemblecmd(const CMDLINE *cmdl, PASS pass); + +bool macrocmd(const CMDLINE *cmdl, PASS pass); + +void writeIN(const char *ibuf, const char *len, PASS pass); + +void writeOUT(const char *obuf, const char *len, PASS pass); + +void writeRPUSH(PASS pass); + +void writeRPOP(PASS pass); + +bool cometcmd(const CMDLINE *cmdl, PASS pass); + +bool writememory(WORD word, WORD adr, PASS pass); -/* 汎用レジスタを表す文字列「GR[0-7]」から、レジスタ番号[0-7]をWORD値で返す */ -/* 文字列が汎用レジスタを表さない場合は、0xFFFFを返す */ -/* is_xがtrueの場合は指標レジスタ。GR0は、COMET IIの仕様により、エラー発生 */ +void writestr(const char *str, bool literal, PASS pass); + +void writeDC(const char *str, PASS pass); + +bool assembletok(const CMDLINE *cmdl, PASS pass); + +bool assembleline(const char *line, PASS pass); + +void printline(FILE *stream, const char *filename, int lineno, char *line); + +/** + * 汎用レジスタを表す文字列「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'))) + (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1)))) { return 0xFFFF; } @@ -66,116 +96,33 @@ WORD getgr(const char *str, bool is_x) return r; } -/* アドレスを返す */ -/* アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる */ -WORD getadr(const char *prog, const char *str, PASS pass) -{ - WORD adr = 0x0; - if(*str == '=') { - adr = getliteral(str, pass); - } else if((*str == '#') || isdigit(*str) || *str == '-') { - adr = nh2word(str); - } else { - if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) { - if(prog != NULL) { - setcerr(103, str); /* label not found */ - } - } - } - return adr; -} - -/* WORD値wordをアドレスadrに書込 */ -/* 書込に成功した場合はtrue、失敗した場合はfalseを返す */ -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 && asmode.asdetail == true) { - fprintf(stdout, "\t#%04X\t#%04X\n", adr, word); - } - status = true; - } - return status; -} - -/* 定数の前に等号(=)をつけて記述されるリテラルを返す */ -/* リテラルには、10進定数/16進定数/文字定数が含まれる */ +/** + * 定数の前に等号(=)をつけて記述されるリテラルを返す + * リテラルには、10進定数/16進定数/文字定数が含まれる + */ WORD getliteral(const char *str, PASS pass) { - WORD adr = lptr; assert(*str == '='); + WORD adr = asptr->lptr; + if(*(++str) == '\'') { /* 文字定数 */ writestr(str, true, pass); } else { - writememory(nh2word(str), lptr++, pass); + writememory(nh2word(str), (asptr->lptr)++, pass); } return adr; } -/* ' 'で囲まれた文字定数をメモリに書込 */ -/* DC命令とリテラルで使い、リテラルの場合はリテラル領域に書込 */ -void writestr(const char *str, bool literal, PASS pass) -{ - assert(cerrno == 0 && *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(*(p++), lptr++, pass); - lw = true; - } else { - writememory(*(p++), ptr++, pass); - } - } -} - -/* アセンブラ命令DCをメモリに書込 */ -void writeDC(const char *str, PASS pass) -{ - WORD adr = 0x0; - if(*str == '\'') { - writestr(str, false, pass); - } else { - if(*str == '#' || isdigit(*str) || *str == '-') { - adr = nh2word(str); - } else { - if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) { - setcerr(103, str); /* label not found */ - } - } - writememory(adr, ptr++, pass); - } -} - -/* 命令がアセンブラ命令の場合は処理を実行 */ -/* 実行に成功した場合はtrue、それ以外の場合はfalseを返す */ +/** + * アセンブラ命令をメモリに書込 + * 実行に成功した場合はtrue、それ以外の場合はfalseを返す + */ 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" }, @@ -189,12 +136,12 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) 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) { @@ -202,31 +149,31 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) return false; } /* プログラム名の設定 */ - prog = strdup(cmdl->label); - /* オペランドがある場合、実行開始番地を設定 */ + asptr->prog = strdup_chk(cmdl->label, "asptr.prog"); + /* オペランドがある場合、実行開始アドレスを設定 */ if(pass == SECOND && cmdl->opd->opdc == 1) { - if((startptr = getlabel(prog, cmdl->opd->opdv[0])) == 0xFFFF) { + if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) { setcerr(103, cmdl->opd->opdv[0]); /* label not found */ } } status = true; break; case END: - /* リテラル領域の設定 */ + /* 1回目のアセンブルの場合は、リテラル領域開始アドレスを設定 */ if(pass == FIRST) { - lptr = ptr; + asptr->lptr = asptr->ptr; } - /* 実行終了番地と次のプログラムの実行開始番地を設定 */ + /* 2回目のアセンブルの場合は、リテラル領域終了アドレスを実行終了アドレスとして設定 */ else if(pass == SECOND) { - endptr = lptr; + execptr->end = asptr->lptr; } - prog = NULL; + FREE(asptr->prog); status = true; break; case DS: for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) { - writememory(0x0, ptr++, pass); - if(cerrno > 0) { + writememory(0x0, (asptr->ptr)++, pass); + if(cerr->num > 0) { return false; } } @@ -235,7 +182,7 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) 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; } } @@ -244,20 +191,22 @@ bool assemblecmd(const CMDLINE *cmdl, PASS pass) default: return false; } - if(cerrno > 0) { + if(cerr->num > 0) { status = false; } return status; } -/* 命令がマクロ命令の場合はメモリに書込 */ -/* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */ +/** + * macrocmd + * マクロ命令をメモリに書込 + * 書込に成功した場合はtrue、それ以外の場合はfalseを返す + */ bool macrocmd(const CMDLINE *cmdl, PASS pass) { int i = 0; - CASLCMD cmd; - bool status = false; - CMDARRAY macrocmd[] = { + MACROCMDID cmdid = 0; + MACROCMD macrocmd[] = { { IN, 2, 2, "IN" }, { OUT, 2, 2, "OUT" }, { RPUSH, 0, 0, "RPUSH" }, @@ -267,36 +216,142 @@ bool macrocmd(const CMDLINE *cmdl, PASS pass) 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); - break; + writeIN(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass); + return true; case OUT: - status = writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass); - break; + writeOUT(cmdl->opd->opdv[0], cmdl->opd->opdv[1], pass); + return true; case RPUSH: - status = writeRPUSH(pass); - break; + writeRPUSH(pass); + return true; case RPOP: - status = writeRPOP(pass); - break; + writeRPOP(pass); + return true; default: return false; } - return status; } -/* 機械語命令の書込 */ -/* 書込に成功した場合はtrue、それ以外の場合はfalseを返す */ +/** + * マクロ命令「IN IBUF,LEN」をメモリに書込 + * PUSH 0,GR1 + * PUSH 0,GR2 + * LAD GR1,IBUF + * LAD GR2,LEN + * SVC 1 + * POP GR2 + * POP GR1 + */ +void writeIN(const char *ibuf, const char *len, PASS pass) +{ + char *line = malloc_chk(LINESIZE + 1, "writeIN.line"); + + assembleline(" PUSH 0,GR1", pass); + assembleline(" PUSH 0,GR2", pass); + sprintf(line, " LAD GR1,%s", ibuf); + assembleline(line, pass); + sprintf(line, " LAD GR2,%s", len); + assembleline(line, pass); + assembleline(" SVC 1", pass); + assembleline(" POP GR2", pass); + assembleline(" POP GR1", pass); + + FREE(line); +} + +/** + * マクロ命令「OUT OBUF,LEN」をメモリに書込 + * PUSH 0,GR1 + * PUSH 0,GR2 + * LAD GR1,OBUF + * LAD GR2,LEN + * SVC 2 + * LAD GR1,=#A + * LAD GR2,=1 + * SVC 2 + * POP GR2 + * POP GR1 + */ +void writeOUT(const char *obuf, const char *len, PASS pass) +{ + char *line = malloc_chk(LINESIZE + 1, "writeOUT.line"); + + assembleline(" PUSH 0,GR1", pass); + assembleline(" PUSH 0,GR2", pass); + sprintf(line, " LAD GR1,%s", obuf); + assembleline(line, pass); + sprintf(line, " LAD GR2,%s", len); + assembleline(line, pass); + assembleline(" SVC 2", pass); + assembleline(" LAD GR1,=#A", pass); + assembleline(" LAD GR2,=1", pass); + assembleline(" SVC 2", pass); + assembleline(" POP GR2", pass); + assembleline(" POP GR1", pass); + FREE(line); +} + +/** マクロ命令「RPUSH」をメモリに書き込む + * PUSH 0,GR1 + * PUSH 0,GR2 + * PUSH 0,GR3 + * PUSH 0,GR4 + * PUSH 0,GR5 + * PUSH 0,GR6 + * PUSH 0,GR7 + */ +void writeRPUSH(PASS pass) +{ + int i; + char *line = malloc_chk(LINESIZE + 1, "writeRPUSH.line"); + + for(i = 1; i <= GRSIZE-1; i++) { + sprintf(line, " PUSH 0,GR%d", i); + assembleline(line, pass); + } + FREE(line); +} + +/** + * マクロ命令「RPOP」をメモリに書き込む + * POP GR7 + * POP GR6 + * POP GR5 + * POP GR4 + * POP GR3 + * POP GR3 + * POP GR2 + * POP GR1 + */ +void writeRPOP(PASS pass) +{ + int i; + char *line = malloc_chk(LINESIZE + 1, "writeRPOP.line"); + + for(i = GRSIZE-1; i >= 1; i--) { + sprintf(line, " POP GR%d", i); + assembleline(line, pass); + } + FREE(line); +} + +/** + * 機械語命令をメモリに書込 + * 書込に、成功した場合はtrue、失敗した場合はfalse、を返す + */ bool cometcmd(const CMDLINE *cmdl, PASS pass) { WORD cmd, adr, r1, r2, x; @@ -308,7 +363,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) setcerr(112, cmdl->cmd); /* not command of no operand */ return false; } - if(writememory(cmd, ptr++, pass) == true) { + if(writememory(cmd, (asptr->ptr)++, pass) == true) { status = true; } } @@ -321,7 +376,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) return false; } cmd |= (r1 << 4); - if(writememory(cmd, ptr++, pass) == true) { + if(writememory(cmd, (asptr->ptr)++, pass) == true) { status = true; } } @@ -332,11 +387,11 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) return false; } cmd |= ((r1 << 4) | r2); - if(cerrno == 0 && writememory(cmd, ptr++, pass) == true) { + if(cerr->num == 0 && writememory(cmd, (asptr->ptr)++, pass) == true) { status = true; } } - /* オペランド数2〜3。第2オペランドはアドレス、 */ + /* オペランド数2または3。第2オペランドはアドレス、 */ /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */ else if(cmdl->opd->opdc == 2 || cmdl->opd->opdc == 3) { if((cmd = getcmdcode(cmdl->cmd, R_ADR_X_)) == 0xFFFF && @@ -354,10 +409,10 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) } cmd |= x; } - adr = getadr(prog, cmdl->opd->opdv[1], pass); - writememory(cmd, ptr++, pass); - writememory(adr, ptr++, pass); - if(cerrno == 0) { + adr = getadr(asptr->prog, cmdl->opd->opdv[1], pass); + writememory(cmd, (asptr->ptr)++, pass); + writememory(adr, (asptr->ptr)++, pass); + if(cerr->num == 0) { status = true; } } else { @@ -365,7 +420,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) return false; } } - /* オペランド数1〜2。第1オペランドはアドレス */ + /* オペランド数1または2。第1オペランドはアドレス */ else if(cmdl->opd->opdc == 1 || cmdl->opd->opdc == 2) { if((cmd = getcmdcode(cmdl->cmd, ADR_X)) == 0xFFFF) { setcerr(111, cmdl->cmd); /* not command of operand "adr[,x]" */ @@ -374,7 +429,7 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) /* オペランド数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; @@ -386,100 +441,246 @@ bool cometcmd(const CMDLINE *cmdl, PASS pass) adr = getlabel(NULL, cmdl->opd->opdv[0]); } if(cmd != 0x8000 || (pass == SECOND && adr == 0xFFFF)) { - adr = getadr(prog, cmdl->opd->opdv[0], pass); + adr = getadr(asptr->prog, cmdl->opd->opdv[0], pass); } - writememory(cmd, ptr++, pass); - writememory(adr, ptr++, pass); - if(cerrno == 0) { + writememory(cmd, (asptr->ptr)++, pass); + writememory(adr, (asptr->ptr)++, pass); + if(cerr->num == 0) { status = true; } } return status; } -/* 命令行を1行アセンブルする */ -bool assembleline(const CMDLINE *cmdl, PASS pass) +/** + * COMET IIのメモリにアドレス値を書き込む + */ +bool writememory(WORD word, WORD adr, PASS pass) +{ + bool status = false; + + /* COMET IIメモリオーバーの場合 */ + if(adr >= sys->memsize) { + setcerr(119, word2n(adr)); /* out of COMET II memory */ + } + 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; + } + return status; +} + +/** + * 文字をメモリに書き込む + */ +void writestr(const char *str, bool literal, PASS pass) +{ + assert(*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(*(p++), (asptr->lptr)++, pass); + lw = true; + } else { + writememory(*(p++), (asptr->ptr)++, pass); + } + } +} + +/** + * DC命令の内容を書き込む + */ +void writeDC(const char *str, PASS pass) +{ + WORD adr = 0x0; + + if(*str == '\'') { + writestr(str, false, pass); + } else { + if(*str == '#' || isdigit(*str) || *str == '-') { + adr = nh2word(str); + } else { + if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) { + setcerr(103, str); /* label not found */ + } + } + writememory(adr, (asptr->ptr)++, pass); + } +} + +/** + * トークンをアセンブル + */ +bool assembletok(const CMDLINE *cmdl, PASS pass) { bool status = false; + /* 命令がない場合 */ if(cmdl->cmd == NULL){ - /* ラベルが定義されていて命令がない場合はエラー */ - if(cmdl->label != NULL) { - setcerr(105, NULL); /* no command in the line */ - } + ; } /* アセンブラ命令の処理 */ - 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) { + 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) { +/** + * ファイルストリームの現在行を番号付きで表示する + */ +void printline(FILE *stream, const char *filename, int lineno, char *line) +{ fprintf(stream, "%s:%5d:%s", filename, lineno, line); } -/* 指定された名前のファイルをアセンブル */ -/* 2回実行される */ +/** + * アドレスを返す + * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる + */ +WORD getadr(const char *prog, const char *str, PASS pass) +{ + WORD adr = 0x0; + + if(*str == '=') { + adr = getliteral(str, pass); + } else if(isdigit(*str) || *str == '-' || *str == '#') { + adr = nh2word(str); + } else { + if(pass == SECOND && (adr = getlabel(prog, str)) == 0xFFFF) { + if(prog != NULL) { + setcerr(103, str); /* label not found */ + } + } + } + return adr; +} + + +/** + * 1行をアセンブル + */ +bool assembleline(const char *line, PASS pass) +{ + CMDLINE *cmdl; + bool status = true; + int i; + + cmdl = linetok(line); + status = (cerr->num == 0) ? true : false; + if(cmdl != NULL) { + if(status == true) { + if(pass == FIRST && cmdl->label != NULL) { + status = addlabel(asptr->prog, cmdl->label, asptr->ptr); + } + if(status == true) { + status = assembletok(cmdl, pass); + } + FREE(cmdl->label); + } + if(cmdl->opd != NULL) { + for(i = 0; i < cmdl->opd->opdc; i++) { + FREE(cmdl->opd->opdv[i]); + } + } + FREE(cmdl->opd); + FREE(cmdl->cmd); + } + FREE(cmdl); + return status; +} + +/** + * アセンブルのエラーをエラーリストに追加 + */ +void addcerrlist_assemble() +{ + addcerrlist_tok(); + addcerrlist_word(); + addcerrlist_label(); + addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble); +} + +/** + * 指定された名前のファイルをアセンブル + * 2回実行される + */ bool assemble(const char *file, PASS pass) { int lineno = 0; bool status = true; - CMDLINE *cmdl; - char *line; + char *line = malloc_chk(LINESIZE + 1, "assemble.line"); FILE *fp; - addcerrlist_assemble(); if((fp = fopen(file, "r")) == NULL) { perror(file); return false; } - for(; ;) { - cmdl = malloc(sizeof(CMDLINE)); - line = malloc(LINESIZE + 1); - if((line = fgets(line, LINESIZE, fp)) == NULL) { - break; - } + while(fgets(line, LINESIZE, fp)) { lineno++; 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(addlabel(prog, cmdl->label, ptr) == false) { - break; - } - } - if(assembleline(cmdl, pass) == false) { - break; - } - } - if(cerrno > 0) { + if(assembleline(line, pass) == false) { break; } } - if(cerrno > 0) { - fprintf(stderr, "Assemble error - %d: %s\n", cerrno, cerrmsg); + if(cerr->num > 0) { + fprintf(stderr, "Assemble error - %d: %s\n", cerr->num, cerr->msg); printline(stderr, file, lineno, line); status = false; } + FREE(line); fclose(fp); return status; } + +/** + * 引数で指定したファイルにアセンブル結果を書込 + */ +void outassemble(const char *file) +{ + FILE *fp; + + if((fp = fopen(file, "w")) == NULL) { + perror(file); + exit(-1); + } + fwrite(sys->memory, sizeof(WORD), execptr->end, fp); + fclose(fp); +}