X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fassemble.c;h=e982082aba5bd931bd88bf853252656371f8b178;hp=de255b13b4267ff0a5d722538bd7b1328f5b0462;hb=86e559d164166966a797a1e5855871d48e087ddd;hpb=9a1bf3c55911d68225635c91b68de98490c25a6f diff --git a/src/assemble.c b/src/assemble.c index de255b1..e982082 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -307,7 +307,7 @@ static CMD macrocmd[] = { { "", NULL } }; -ASPTR *asptr; +ASPTR *asptr = NULL; ASMODE asmode = {false, false, false, false, false}; @@ -318,11 +318,11 @@ void printline(FILE *stream, const char *filename, int lineno, char *line) 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) { @@ -336,29 +336,31 @@ WORD getadr(const char *prog, const char *str, PASS pass) 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; } 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); @@ -368,7 +370,7 @@ WORD getliteral(const char *str, PASS pass) void writememory(WORD word, WORD adr, PASS pass) { - char *n; + char *n = NULL; /* メモリオーバーの場合、エラー発生 */ if(adr >= sys->memsize) { @@ -384,29 +386,26 @@ void writememory(WORD word, WORD adr, PASS pass) 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); } } } @@ -418,7 +417,7 @@ void writedc(const char *str, PASS pass) 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) { @@ -435,7 +434,7 @@ void assemble_start(const CMDLINE *cmdl, PASS pass) setcerr(106, ""); /* operand count mismatch */ return; } - if(*(cmdl->label) == '\0') { + if(!cmdl->label[0]) { setcerr(107, ""); /* no label in START */ return; } @@ -461,17 +460,16 @@ void assemble_end(const CMDLINE *cmdl, PASS pass) else if(pass == SECOND) { execptr->end = asptr->lptr; } - *(asptr->prog) = '\0'; + strcpy(asptr->prog, ""); } 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; @@ -481,12 +479,11 @@ void assemble_ds(const CMDLINE *cmdl, PASS pass) 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; @@ -496,7 +493,9 @@ void assemble_dc(const CMDLINE *cmdl, PASS pass) void assemble_in(const CMDLINE *cmdl, PASS pass) { - char *line = malloc_chk(LINESIZE + 1, "assemble_in.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_in.line"); if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) { setcerr(106, ""); /* operand count mismatch */ return; @@ -515,7 +514,9 @@ void assemble_in(const CMDLINE *cmdl, PASS pass) void assemble_out(const CMDLINE *cmdl, PASS pass) { - char *line = malloc_chk(LINESIZE + 1, "assemble_out.line"); + char *line = NULL; + + line = malloc_chk(LINESIZE + 1, "assemble_out.line"); if(cmdl->opd->opdc == 0 || cmdl->opd->opdc > 2) { setcerr(106, ""); /* operand count mismatch */ return; @@ -537,13 +538,14 @@ void assemble_out(const CMDLINE *cmdl, PASS pass) void assemble_rpush(const CMDLINE *cmdl, PASS pass) { - int i; - char *line = malloc_chk(LINESIZE + 1, "assemble_rpush.line"); + char *line = NULL; + + 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); } @@ -552,13 +554,14 @@ void assemble_rpush(const CMDLINE *cmdl, PASS pass) void assemble_rpop(const CMDLINE *cmdl, PASS pass) { - int i; - char *line = malloc_chk(LINESIZE + 1, "assemble_rpop.line"); + char *line = NULL; + + 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); } @@ -567,9 +570,9 @@ void assemble_rpop(const CMDLINE *cmdl, PASS pass) 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); @@ -581,7 +584,10 @@ bool casl2cmd(CMD *cmdtbl, const CMDLINE *cmdl, PASS pass) 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) { @@ -669,7 +675,7 @@ bool assemble_comet2cmd(const CMDLINE *cmdl, PASS pass) bool assembletok(const CMDLINE *cmdl, PASS pass) { /* 命令がない場合 */ - if(*(cmdl->cmd) == '\0') { + if(!cmdl->cmd[0]) { return true; } /* アセンブラ命令またはマクロ命令の書込 */ @@ -686,15 +692,14 @@ bool assembletok(const CMDLINE *cmdl, PASS pass) 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); } } @@ -703,7 +708,7 @@ bool assembleline(const char *line, PASS pass) } 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]); } } @@ -722,8 +727,8 @@ bool assembleline(const char *line, PASS pass) 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; @@ -747,13 +752,13 @@ bool assemblefile(const char *file, PASS pass) return (cerr->num == 0) ? true : false; } -void assemble(int filec, char *filev[], WORD adr) +bool assemble(int filec, char *filev[], WORD adr) { int i; PASS pass; WORD bp[filec]; + bool stat = false; - create_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を作成 */ asptr = malloc_chk(sizeof(ASPTR), "asptr"); /* アセンブル時のプロパティ用の領域確保 */ asptr->prog = malloc_chk(LABELSIZE + 1, "asptr.prog"); asptr->ptr = adr; @@ -772,7 +777,8 @@ void assemble(int filec, char *filev[], WORD adr) fprintf(stdout, "\nAssemble %s (%d)\n", filev[i], pass); } /* ファイルをアセンブル */ - if(assemblefile(filev[i], pass) == false) { + stat = assemblefile(filev[i], pass); + if(stat == false) { goto asfin; } } @@ -785,10 +791,10 @@ void assemble(int filec, char *filev[], WORD adr) } } asfin: - freelabel(); /* ラベルハッシュ表を解放 */ - free_cmdtype_code(); /* 命令の名前とタイプがキーのハッシュ表を解放 */ - FREE(asptr->prog); /* アセンブル時のプロパティを解放 */ + freelabel(); /* ラベルハッシュ表を解放 */ + FREE(asptr->prog); /* アセンブル時のプロパティを解放 */ FREE(asptr); + return stat; } /* assemble.hで定義された関数群 */ @@ -802,7 +808,7 @@ void addcerrlist_assemble() void outassemble(const char *file) { - FILE *fp; + FILE *fp = NULL; if((fp = fopen(file, "w")) == NULL) { perror(file);