-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <assert.h>
-#include <errno.h>
-
#include "assemble.h"
-#include "cerr.h"
/**
- * アセンブルモード: src, label, onlylabel, asdetail, onlyassemble
+ * @brief ファイルストリームの現在行を番号付きで表示する
+ *
+ * @return なし
+ *
+ * @param *stream ファイルストリーム
+ * @param *filename ファイル名
+ * @param lineno 行番号
+ * @param line 行の文字列
*/
-ASMODE asmode = {false, false, false, false, false};
+void printline(FILE *stream, const char *filename, int lineno, char *line);
/**
- * アセンブル時の、現在およびリテラルのアドレスとプログラム入口名: ptr, lptr, prog
+ * @brief アドレスを返す
+ *
+ * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
+ *
+ * @return アドレス
+ *
+ * @param *prog プログラム名
+ * @param *str アドレスを表す文字列
+ * @param pass アセンブラが何回目かを表す数
*/
-ASPTR *asptr;
+WORD getadr(const char *prog, const char *str, PASS pass);
/**
- * アセンブルのエラー定義
+ * @brief 汎用レジスタを表す文字列からレジスタ番号を返す
+ *
+ * @brief 文字列が汎用レジスタを表さない場合は、0xFFFFを返す
+ *
+ * is_xがtrueの場合は指標レジスタとなり、GR0が指定された場合はCOMET IIの仕様によりエラー発生
+ *
+ * @return レジスタ番号[0-7]を表すWORD値
+ *
+ * @param *str 汎用レジスタを表す文字列。「GR0」「GR1」・・・「GR7」のいずれか
+ * @param is_x trueの場合は指標レジスタ
*/
-static CERR cerr_assemble[] = {
- { 106, "operand mismatch in CASL II 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" },
- { 119, "out of COMET II memory" },
- { 120, "GR0 in operand x" },
- { 122, "cannot create hash table" },
- { 124, "more than one character in literal" },
- { 125, "not GR in operand x" },
-};
-
-void printline(FILE *stream, const char *filename, int lineno, char *line);
-
-WORD getadr(const char *prog, const char *str, PASS pass);
-
-WORD getgr(const char *str, bool is_x);
+WORD grword(const char *str, bool is_x);
+/**
+ * @brief リテラルを返す
+ *
+ * リテラルには、10進定数/16進定数/文字定数が含まれる
+ *
+ * @param *str リテラル。定数の前に等号(=)をつけて記述される
+ * @param pass アセンブラが何回目かを表す数
+ */
WORD getliteral(const char *str, PASS pass);
+/**
+ * @brief アドレス値をメモリに書き込む
+ *
+ * @return なし
+ *
+ * @param word アドレス値
+ * @param adr アドレス
+ * @param pass アセンブラが何回目かを表す数
+ */
void writememory(WORD word, WORD adr, PASS pass);
+/**
+ * @brief 文字をメモリに書き込む
+ *
+ * @return なし
+ *
+ * @param *str アドレスを表す文字列。リテラル/10進定数/16進定数/アドレス定数が含まれる
+ * @param literal リテラルの場合はtrue
+ * @param pass アセンブラが何回目かを表す数
+ */
void writestr(const char *str, bool literal, PASS pass);
+/**
+ * @brief DC命令を書込
+ *
+ * @return なし
+ *
+ * @param str アドレスを表す文字列。10進定数/16進定数/アドレス定数が含まれる
+ * @param pass アセンブラが何回目かを表す数
+ */
void writedc(const char *str, PASS pass);
+/**
+ * @brief アセンブラ命令STARTの処理
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_start(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief アセンブラ命令DSの処理
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_ds(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief アセンブラ命令ENDの処理
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_end(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief アセンブラ命令DCの処理
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_dc(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief マクロ命令 "IN IBUF,LEN" をアセンブル
+ *
+ * @code
+ * PUSH 0,GR1
+ * PUSH 0,GR2
+ * LAD GR1,IBUF
+ * LAD GR2,LEN
+ * SVC 1
+ * POP GR2
+ * POP GR1
+ * @endcode
+ * @relatesalso Casl2cmd
+ *
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_in(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief マクロ命令 "OUT OBUF,LEN" をアセンブル
+ *
+ * @code
+ * 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
+ * @endcode
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_out(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief マクロ命令 "RPUSH" をメモリに書き込む
+ *
+ * @code
+ * PUSH 0,GR1
+ * PUSH 0,GR2
+ * PUSH 0,GR3
+ * PUSH 0,GR4
+ * PUSH 0,GR5
+ * PUSH 0,GR6
+ * PUSH 0,GR7
+ * @endcode
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_rpush(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief マクロ命令 "RPOP" をメモリに書き込む
+ *
+ * @code
+ * POP GR7
+ * POP GR6
+ * POP GR5
+ * POP GR4
+ * POP GR3
+ * POP GR3
+ * POP GR2
+ * POP GR1
+ * @endcode
+ * @relatesalso Casl2cmd
+ *
+ * @return なし
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
void assemble_rpop(const CMDLINE *cmdl, PASS pass);
+/**
+ * @class Casl2cmd
+ * @brief casl2cmdから関数ポインタで呼び出される関数
+ */
+
+/**
+ * @brief アセンブラ言語CASL IIの命令を処理する。
+ *
+ * エラー発生時は、cerrを設定
+ *
+ * @relatesalso Casl2cmd
+ *
+ * @return 命令が表で定義されている場合はtrue、それ以外の場合はfalseを返す
+ *
+ * @param *cmdtbl アセンブラ言語CASL IIの命令表
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブルが何回目か
+ */
bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief システムCOMET IIの命令をアセンブルする
+ *
+ * @return アセンブル成功時はtrue、失敗時はfalseを返す
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブルが何回目か
+ */
bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief トークンをアセンブルする
+ *
+ * @return アセンブル成功時はtrue、失敗時はfalseを返す
+ *
+ * @param *cmdl 1行分のラベル・コマンド・オペランド
+ * @param pass アセンブラが何回目かを表す数
+ */
bool assembletok(const CMDLINE *cmdl, PASS pass);
+/**
+ * @brief 1行をアセンブルする
+ *
+ * passが1の場合はラベルを登録し、2の場合はラベルからアドレスを読み込む
+ *
+ * @return アセンブル成功時はtrue、失敗時はfalseを返す
+ *
+ * @param *line 1行分の文字列
+ * @param pass アセンブラが何回目かを表す数
+ */
bool assembleline(const char *line, PASS pass);
/**
- * アセンブラ命令
+ * @brief アセンブルのエラー
+ */
+static CERR cerr_assemble[] = {
+ { 106, "operand mismatch in CASL II 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" },
+ { 119, "out of COMET II memory" },
+ { 120, "GR0 in operand x" },
+ { 122, "cannot create hash table" },
+ { 124, "more than one character in literal" },
+ { 125, "not GR in operand x" },
+};
+
+/**
+ * @brief アセンブラ命令
*/
static CMD ascmd[] = {
{ "START", assemble_start },
};
/**
- * マクロ命令
+ * @brief マクロ命令
*/
static CMD macrocmd[] = {
{ "OUT", assemble_out },
{ "", NULL }
};
-/**
- * アセンブルのエラーをエラーリストに追加
- */
-void addcerrlist_assemble()
-{
- addcerrlist_tok();
- addcerrlist_word();
- addcerrlist_label();
- addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
-}
+ASPTR *asptr = NULL;
+
+ASMODE asmode = {false, false, false, false, false};
-/**
- * ファイルストリームの現在行を番号付きで表示する
- */
void printline(FILE *stream, const char *filename, int lineno, char *line)
{
fprintf(stream, "%s:%5d:%s", filename, lineno, line);
}
-/**
- * アドレスを返す\n
- * アドレスには、リテラル/10進定数/16進定数/アドレス定数が含まれる
- */
WORD getadr(const char *prog, const char *str, PASS pass)
{
- WORD adr = 0x0;
+ WORD adr = 0;
- if(*str == '=') {
+ if(str[0] == '=') {
adr = getliteral(str, pass);
- } else if(isdigit(*str) || *str == '-' || *str == '#') {
+ } else if(isdigit(str[0]) || str[0] == '-' || str[0] == '#') {
adr = nh2word(str);
} else {
if(pass == SECOND) {
return adr;
}
-/**
- * 汎用レジスタを表す文字列 "GR[0-7]" から、レジスタ番号 [0-7] をWORD値で返す\n
- * 文字列が汎用レジスタを表さない場合は、0xFFFFを返す\n
- * is_xがtrueの場合は指標レジスタ。GR0が指定された場合は、COMET IIの仕様によりエラー発生
- */
-WORD getgr(const char *str, bool is_x)
+WORD grword(const char *str, bool is_x)
{
- WORD r;
+ WORD r = 0;
/* "GR[0-7]" 以外の文字列では、0xFFFFを返して終了 */
- if(!(strlen(str) == 3 && strncmp(str, "GR", 2) == 0 &&
- (*(str+2) >= '0' && *(str+2) <= '0' + (GRSIZE - 1))))
+ if(strlen(str) != 3 ||
+ strncmp(str, "GR", 2) != 0 ||
+ str[2] < '0' || str[2] > '0' + (GRSIZE - 1))
{
return 0xFFFF;
}
- r = (WORD)(*(str+2) - '0');
+ r = (WORD)(str[2] - '0');
/* GR0は指標レジスタとして用いることができない */
if(is_x == true && r == 0x0) {
setcerr(120, ""); /* GR0 in operand x */
- return 0x0;
+ return 0;
}
return r;
}
-/**
- * 定数の前に等号(=)をつけて記述されるリテラルを返す\n
- * リテラルには、10進定数/16進定数/文字定数が含まれる
- */
WORD getliteral(const char *str, PASS pass)
{
- assert(*str == '=');
+ assert(str[0] == '=');
WORD adr = asptr->lptr;
- if(*(++str) == '\'') { /* 文字定数 */
+ str++;
+ if(str[0] == '\'') { /* 文字定数 */
writestr(str, true, pass);
} else {
writememory(nh2word(str), (asptr->lptr)++, pass);
return adr;
}
-/**
- * アドレス値をメモリに書き込む
- */
void writememory(WORD word, WORD adr, PASS pass)
{
+ char *n = NULL;
+
/* メモリオーバーの場合、エラー発生 */
if(adr >= sys->memsize) {
- setcerr(119, word2n(adr)); /* out of COMET II memory */
+ setcerr(119, (n = word2n(adr))); /* out of COMET II memory */
+ FREE(n)
return;
}
(sys->memory)[adr] = word;
}
}
-/**
- * 文字をメモリに書き込む
- */
void writestr(const char *str, bool literal, PASS pass)
{
- assert(*str == '\'');
- const char *p = str + 1;
+ assert(str[0] == '\'');
bool lw = false;
- for(; ;) {
- /* 閉じ「'」がないまま文字列が終了した場合 */
- if(*p == '\0') {
+ /* 「'」の場合、1文字スキップし、次の文字が「'」でなければ正常終了 */
+ for(int i = 1; str[i] != '\'' || str[++i] == '\''; i++) {
+ /* 「'」が閉じないまま文字列が終了した場合はエラー */
+ if(!str[i]) {
setcerr(123, str); /* unclosed quote */
break;
}
- /* 「'」の場合、次の文字が「'」でない場合は正常終了 */
- if(*p == '\'' && *(++p) != '\'') {
- break;
- } else if(literal == true && lw == true) {
+ if(literal == true && lw == true) {
setcerr(124, str); /* more than one character in literal */
break;
}
/*リテラルの場合はリテラル領域に書込 */
if(literal == true) {
- writememory(*(p++), (asptr->lptr)++, pass);
+ writememory(str[i], (asptr->lptr)++, pass);
lw = true;
} else {
- writememory(*(p++), (asptr->ptr)++, pass);
+ writememory(str[i], (asptr->ptr)++, pass);
}
}
}
-/**
- * DC命令を書込
- */
void writedc(const char *str, PASS pass)
{
- WORD adr = 0x0;
+ WORD adr = 0;
if(*str == '\'') {
writestr(str, false, pass);
} else {
- if(*str == '#' || isdigit(*str) || *str == '-') {
+ if(str[0] == '#' || isdigit(str[0]) || str[0] == '-') {
adr = nh2word(str);
} else {
if(pass == SECOND && (adr = getlabel(asptr->prog, str)) == 0xFFFF) {
}
}
-/**
- * アセンブラ命令STARTの処理
- * \relates casl2cmd
- */
void assemble_start(const CMDLINE *cmdl, PASS pass)
{
if(cmdl->opd->opdc > 1) {
setcerr(106, ""); /* operand count mismatch */
return;
}
- if(*(cmdl->label) == '\0') {
+ if(!cmdl->label[0]) {
setcerr(107, ""); /* no label in START */
return;
}
/* プログラム名の設定 */
strcpy(asptr->prog, cmdl->label);
- /* オペランドがある場合、実行開始アドレスを設定 */
- if(pass == SECOND && cmdl->opd->opdv[0] != NULL) {
- if((execptr->start = getlabel(asptr->prog, cmdl->opd->opdv[0])) == 0xFFFF) {
- setcerr(103, cmdl->opd->opdv[0]); /* label not found */
- }
+ /* オペランドがある場合、書き込みと実行の開始アドレスを設定 */
+ if(cmdl->opd->opdv[0] != NULL) {
+ asptr->ptr = execptr->start = getadr(asptr->prog, cmdl->opd->opdv[0], pass);
}
}
-/**
- * アセンブラ命令ENDの処理
- * \relates casl2cmd
- */
void assemble_end(const CMDLINE *cmdl, PASS pass)
{
if(cmdl->opd->opdc > 0) {
else if(pass == SECOND) {
execptr->end = asptr->lptr;
}
- *(asptr->prog) = '\0';
+ strcpy(asptr->prog, "");
}
-/**
- * アセンブラ命令DSの処理
- * \relates casl2cmd
- */
void assemble_ds(const CMDLINE *cmdl, PASS pass)
{
- int i;
if(cmdl->opd->opdc != 1) {
setcerr(106, ""); /* operand count mismatch */
return;
}
- for(i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
+ for(int i = 0; i < atoi(cmdl->opd->opdv[0]); i++) {
writememory(0x0, (asptr->ptr)++, pass);
if(cerr->num > 0) {
break;
}
}
-/**
- * アセンブラ命令DCの処理
- * \relates casl2cmd
- */
void assemble_dc(const CMDLINE *cmdl, PASS pass)
{
- int i;
if(cmdl->opd->opdc == 0 || cmdl->opd->opdc >= OPDSIZE) {
setcerr(106, ""); /* operand count mismatch */
return;
}
- for(i = 0; i < cmdl->opd->opdc; i++) {
+ for(int i = 0; i < cmdl->opd->opdc; i++) {
writedc(cmdl->opd->opdv[i], pass);
if(cerr->num > 0) {
break;
}
}
-/**
- * マクロ命令 "IN IBUF,LEN" をアセンブル\n
- * \code
- * PUSH 0,GR1
- * PUSH 0,GR2
- * LAD GR1,IBUF
- * LAD GR2,LEN
- * SVC 1
- * POP GR2
- * POP GR1
- * \endcode
- * \relates casl2cmd
- */
void assemble_in(const CMDLINE *cmdl, PASS pass)
{
char *line = malloc_chk(LINESIZE + 1, "assemble_in.line");
+
if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
setcerr(106, ""); /* operand count mismatch */
return;
FREE(line);
}
-/**
- * マクロ命令 "OUT OBUF,LEN" をアセンブル\n
- * \code
- * 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
- * \endcode
- * \relates casl2cmd
- */
void assemble_out(const CMDLINE *cmdl, PASS pass)
{
char *line = malloc_chk(LINESIZE + 1, "assemble_out.line");
+
if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) {
setcerr(106, ""); /* operand count mismatch */
return;
FREE(line);
}
-/**
- * マクロ命令 "RPUSH" をメモリに書き込む
- * \code
- * PUSH 0,GR1
- * PUSH 0,GR2
- * PUSH 0,GR3
- * PUSH 0,GR4
- * PUSH 0,GR5
- * PUSH 0,GR6
- * PUSH 0,GR7
- * \endcode
- * \relates casl2cmd
- */
void assemble_rpush(const CMDLINE *cmdl, PASS pass)
{
- int i;
char *line = malloc_chk(LINESIZE + 1, "assemble_rpush.line");
+
if(cmdl->opd->opdc > 0) {
setcerr(106, ""); /* operand count mismatch */
return;
}
- for(i = 1; i <= GRSIZE-1; i++) {
+ for(int i = 1; i <= GRSIZE-1; i++) {
sprintf(line, " PUSH 0,GR%d", i);
assembleline(line, pass);
}
FREE(line);
}
-/**
- * マクロ命令 "RPOP" をメモリに書き込む\n
- * \code
- * POP GR7
- * POP GR6
- * POP GR5
- * POP GR4
- * POP GR3
- * POP GR3
- * POP GR2
- * POP GR1
- * \endcode
- * \relates casl2cmd
- */
void assemble_rpop(const CMDLINE *cmdl, PASS pass)
{
- int i;
char *line = malloc_chk(LINESIZE + 1, "assemble_rpop.line");
+
if(cmdl->opd->opdc > 0) {
setcerr(106, ""); /* operand count mismatch */
return;
}
- for(i = GRSIZE-1; i >= 1; i--) {
+ for(int i = GRSIZE-1; i >= 1; i--) {
sprintf(line, " POP GR%d", i);
assembleline(line, pass);
}
FREE(line);
}
-/**
- * アセンブラ言語CASL IIの命令を処理\n
- * 命令が表で定義されている場合はtrue、それ以外の場合はfalseを返す\n
- * エラー発生時は、cerrを設定\n
- * 関数へのポインタで呼び出す関数は、Class Reference 参照
- * \class casl2cmd
- */
bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass)
{
- int i;
- void (*cmdptr)();
- for(i = 0; *(cmdtbl[i].name) != '\0'; i++) {
+ void (*cmdptr)() = NULL;
+
+ for(int i = 0; cmdtbl[i].name[0]; i++) {
if(strcmp(cmdl->cmd, cmdtbl[i].name) == 0) {
cmdptr = cmdtbl[i].ptr;
(*cmdptr)(cmdl, pass);
return false;
}
-/**
- * システムCOMET IIの命令をアセンブル\n
- * アセンブルに成功した場合はtrue、失敗した場合はfalseを返す
- */
bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass)
{
- WORD cmd, r_r1, x_r2, adr;
+ WORD cmd = 0;
+ WORD r_r1 = 0;
+ WORD x_r2 = 0;
+ WORD adr = 0;
/* オペランドなし */
if(cmdl->opd->opdc == 0) {
writememory(cmd, (asptr->ptr)++, pass);
}
/* 第1オペランドは汎用レジスタ */
- else if((r_r1 = getgr(cmdl->opd->opdv[0], false)) != 0xFFFF) {
+ else if((r_r1 = grword(cmdl->opd->opdv[0], false)) != 0xFFFF) {
/* オペランド数1 */
if(cmdl->opd->opdc == 1) {
if((cmd = getcmdcode(cmdl->cmd, R_)) == 0xFFFF) {
writememory(cmd, (asptr->ptr)++, pass);
}
/* オペランド数2。第2オペランドは汎用レジスタ */
- else if(cmdl->opd->opdc == 2 && (x_r2 = getgr(cmdl->opd->opdv[1], false)) != 0xFFFF) {
+ else if(cmdl->opd->opdc == 2 && (x_r2 = grword(cmdl->opd->opdv[1], false)) != 0xFFFF) {
if((cmd = getcmdcode(cmdl->cmd, R1_R2)) == 0xFFFF) {
setcerr(109, cmdl->cmd); /* not command of operand "r1,r2" */
return false;
cmd |= (r_r1 << 4); /* 第1オペランドは汎用レジスタ */
/* オペランド数3の場合 */
if(cmdl->opd->opdc == 3) { /* 第3オペランドは指標レジスタとして用いる汎用レジスタ */
- if((x_r2 = getgr(cmdl->opd->opdv[2], true)) == 0xFFFF) {
+ if((x_r2 = grword(cmdl->opd->opdv[2], true)) == 0xFFFF) {
setcerr(125, cmdl->cmd); /* not GR in operand x */
return false;
}
}
/* オペランド数2の場合 */
if(cmdl->opd->opdc == 2) { /* 第2オペランドは指標レジスタとして用いる汎用レジスタ */
- x_r2 = getgr(cmdl->opd->opdv[1], true);
+ x_r2 = grword(cmdl->opd->opdv[1], true);
if(cerr->num > 0) {
return false;
}
return (cerr->num == 0) ? true : false;
}
-/**
- * トークンをアセンブル
- */
bool assembletok(const CMDLINE *cmdl, PASS pass)
{
/* 命令がない場合 */
- if(*(cmdl->cmd) == '\0') {
+ if(!cmdl->cmd[0]) {
return true;
}
/* アセンブラ命令またはマクロ命令の書込 */
return (cerr->num == 0) ? true : false;
}
-/**
- * 1行をアセンブル\n
- * passが1の場合はラベルを登録し、2の場合はラベルからアドレスを読み込む
- */
bool assembleline(const char *line, PASS pass)
{
- CMDLINE *cmdl;
+ CMDLINE *cmdl = NULL;
bool stat = true;
- int i;
cmdl = linetok(line);
stat = (cerr->num == 0) ? true : false;
if(cmdl != NULL) {
if(stat == true) {
- if(pass == FIRST && *(cmdl->label) != '\0') {
+ if(pass == FIRST && cmdl->label[0]) {
stat = addlabel(asptr->prog, cmdl->label, asptr->ptr);
}
}
}
FREE(cmdl->label);
if(cmdl->opd != NULL) {
- for(i = 0; i < cmdl->opd->opdc; i++) {
+ for(int i = 0; i < cmdl->opd->opdc; i++) {
FREE(cmdl->opd->opdv[i]);
}
}
}
/**
- * 指定された名前のファイルをアセンブル\n
+ * @brief 指定された名前のファイルをアセンブル
+ *
* アセンブル完了時はtrue、エラー発生時はfalseを返す
*/
bool assemblefile(const char *file, PASS pass)
{
int lineno = 1;
- char *line;
- FILE *fp;
+ char *line = NULL;
+ FILE *fp = NULL;
if((fp = fopen(file, "r")) == NULL) {
cerr->num = errno;
return (cerr->num == 0) ? true : false;
}
-/**
- * 引数で指定したファイルにアセンブル結果を書込
- */
+bool assemble(int filec, char *filev[], WORD adr)
+{
+ int i;
+ PASS pass;
+ WORD bp[filec];
+ bool stat = false;
+
+ asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */
+ asptr->prog = malloc_chk(LABELSIZE + 1, "asptr.prog");
+ asptr->ptr = adr;
+ /* アセンブル。ラベル表作成のため、2回行う */
+ for(pass = FIRST; pass <= SECOND; pass++) {
+ for(i = 0; i < filec; i++) {
+ /* データの格納開始位置 */
+ if(pass == FIRST) {
+ bp[i] = asptr->ptr;
+ } else if(pass == SECOND) {
+ asptr->ptr = bp[i];
+ }
+ if(execmode.trace == true || execmode.dump == true ||
+ asmode.src == true || asmode.label == true || asmode.asdetail == true)
+ {
+ fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass);
+ }
+ /* ファイルをアセンブル */
+ stat = assemblefile(filev[i], pass);
+ if(stat == false) {
+ goto asfin;
+ }
+ }
+ if(pass == FIRST && asmode.label == true) {
+ fprintf(stdout, "\nLabel::::\n");
+ printlabel();
+ if(asmode.onlylabel == true) {
+ break;
+ }
+ }
+ }
+asfin:
+ freelabel(); /* ラベルハッシュ表を解放 */
+ FREE(asptr->prog); /* アセンブル時のプロパティを解放 */
+ FREE(asptr);
+ return stat;
+}
+
+/* assemble.hで定義された関数群 */
+void addcerrlist_assemble()
+{
+ addcerrlist_tok();
+ addcerrlist_word();
+ addcerrlist_label();
+ addcerrlist(ARRAYSIZE(cerr_assemble), cerr_assemble);
+}
+
void outassemble(const char *file)
{
- FILE *fp;
+ FILE *fp = NULL;
if((fp = fopen(file, "w")) == NULL) {
perror(file);