From: nyatla Date: Thu, 15 Sep 2011 08:59:08 +0000 (+0000) Subject: git-svn-id: http://svn.osdn.jp/svnroot/mimic/trunk@14 47198e57-cb75-475f-84c4-a814cd6... X-Git-Tag: SVN-final~314 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=f531965ddef456e2127361b45f6598156a95f26c;p=mimic%2FMiMicSDK.git git-svn-id: svn.osdn.jp/svnroot/mimic/trunk@14 47198e57-cb75-475f-84c4-a814cd6f29e0 --- diff --git a/lib/src/NyLPC_stdlib.c b/lib/src/NyLPC_stdlib.c index 2022341..020d3de 100644 --- a/lib/src/NyLPC_stdlib.c +++ b/lib/src/NyLPC_stdlib.c @@ -64,3 +64,14 @@ void NyLPC_debugHook(const char* m,int l) return; } +void NyLPC_TCharArrayPtr_seek(struct NyLPC_TCharArrayPtr* i_struct,NyLPC_TUInt16 i_seek) +{ + i_struct->ptr+=i_seek; + i_struct->len-=i_seek; +} + +void NyLPC_TUInt32ArrayPtr_seek(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_TUInt16 i_seek) +{ + i_struct->ptr+=i_seek; + i_struct->len-=i_seek; +} \ No newline at end of file diff --git a/lib/src/include/NyLPC_mimicVm.h b/lib/src/include/NyLPC_mimicVm.h index 6af19b6..2a0f236 100644 --- a/lib/src/include/NyLPC_mimicVm.h +++ b/lib/src/include/NyLPC_mimicVm.h @@ -30,6 +30,7 @@ #ifndef NYLPC_MIMICVM_H_ #define NYLPC_MIMICVM_H_ #include "../mimicvm/NyLPC_cMiMicTxtCompiler.h" +#include "../mimicvm/NyLPC_cMiMicDbCompiler.h" #include "../mimicvm/NyLPC_cMiMicVM.h" #endif diff --git a/lib/src/include/NyLPC_stdlib.h b/lib/src/include/NyLPC_stdlib.h index 62e3c1c..d581058 100644 --- a/lib/src/include/NyLPC_stdlib.h +++ b/lib/src/include/NyLPC_stdlib.h @@ -420,6 +420,49 @@ NyLPC_TUInt32 NyLPC_TUInt32_bswap(NyLPC_TUInt32 n); #define NyLPC_TUInt32_castto_TBool(a) ((a)?NyLPC_TBool_TRUE:NyLPC_TBool_FALSE) +//---------------------------------------------------------------------- + +/** + * 長さ付TChar配列の構造体です。 + * + */ +struct NyLPC_TCharArrayPtr +{ + /** 配列のポインタ */ + NyLPC_TChar* ptr; + /** 配列の長さ */ + NyLPC_TUInt16 len; +}; +/** + * ptrの位置をi_seekだけ進行します。 + * @param i_struct + * 操作する構造体 + * @param i_seek + * シークするバイト長 + */ +void NyLPC_TCharArrayPtr_seek(struct NyLPC_TCharArrayPtr* i_struct,NyLPC_TUInt16 i_seek); + + +/** + * 長さ付TUInt32配列の構造体です。 + * + */ +struct NyLPC_TUInt32ArrayPtr +{ + /** 配列のポインタ */ + NyLPC_TUInt32* ptr; + /** 要素数 */ + NyLPC_TUInt16 len; +}; +/** + * ptrの位置をi_seekだけ進行します。 + * @param i_struct + * 操作する構造体 + * @param i_seek + * シークする要素長 + */ +void NyLPC_TUInt32ArrayPtr_seek(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_TUInt16 i_seek); + diff --git a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c index 0c36e32..45c2f5d 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c +++ b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c @@ -3,86 +3,92 @@ - - void NyLPC_cMiMicDbCompiler_initialize(NyLPC_TcMiMicDbCompiler_t* i_inst) { i_inst->_bc_fragment_len=0; - return; } + /** - * + * この関数は、MiMicDBのフラグメントをコンパイルします。 * @param o_parsed_len * 変換した文字列の長さ。 * @return * ステータスを返す。 */ -NyLPC_TcMiMicDbCompiler_ST NyLPC_cMiMicDbCompiler_getValue(NyLPC_TcMiMicDbCompiler_t* i_inst,const NyLPC_TChar* i_bc_fragment,NyLPC_TUInt16 i_bc_len,NyLPC_TUInt32* o_val,NyLPC_TUInt16* o_parsed_bc) +NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment(NyLPC_TcMiMicDbCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,NyLPC_TUInt32* o_val,NyLPC_TUInt16* o_parsed_bc) { int i; NyLPC_TUInt8 c=i_inst->_bc_fragment_len; - if(c==0 && i_bc_len>=8){ + if(c==0 && i_bc->len>=8){ //キャッシュ0でソースが十分あるときは直接変換 - if(NyLPC_mimicvm_txt2UInt(i_bc_fragment,8,o_val)){ + if(NyLPC_mimicvm_txt2UInt(i_bc->ptr,8,o_val)){ *o_parsed_bc=8; - return NyLPC_TcMiMicDbCompiler_ST_OK; + return NyLPC_TcMiMicDbCompiler_RET_OK; }else{ i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; - return NyLPC_TcMiMicDbCompiler_ST_ERROR; + return NyLPC_TcMiMicDbCompiler_RET_ERROR; } }else{ //キャッシュが0でなければ、パディングして変換 - for(i=0;i_tmp[c]=i_bc_fragment[i]; + for(i=0;ilen;i++){ + i_inst->_tmp[c]=i_bc->ptr[i]; c++; //8個のバイトコードが溜まったら変換 if(c==8){ i_inst->_bc_fragment_len=0; if(NyLPC_mimicvm_txt2UInt(i_inst->_tmp,8,o_val)){ - *o_parsed_bc=i; - return NyLPC_TcMiMicDbCompiler_ST_OK; + *o_parsed_bc=i+1;//見チェック + return NyLPC_TcMiMicDbCompiler_RET_OK; }else{ i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; - return NyLPC_TcMiMicDbCompiler_ST_ERROR; + return NyLPC_TcMiMicDbCompiler_RET_ERROR; } } } i_inst->_bc_fragment_len=c; - *o_parsed_bc=i_bc_len; - return NyLPC_TcMiMicDbCompiler_ST_CONTINUE; + *o_parsed_bc=i_bc->len; + return NyLPC_TcMiMicDbCompiler_RET_CONTINUE; } } /** - * 複数の数値を一括して変換する。 + * MiMicDBフラグメントを1文字パースします。 + */ +NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment2(NyLPC_TcMiMicDbCompiler_t* i_inst,NyLPC_TChar i_bc,NyLPC_TUInt32* o_val) +{ + struct NyLPC_TCharArrayPtr bc; + NyLPC_TUInt16 l; + bc.ptr=&i_bc; + bc.len=1; + return NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&bc,o_val,&l); +} + +/** + * 複数の数値を一括して変換する。テストしえてないから注意な。 * @return * 0 - エラー。変換エラーが発生した。 * 00); - while(l>0){ + NyLPC_Assert(i_bc->len>0); + while(i_bc->len>0){ //空き領域チェック - if(val_len==0){ + if(wp.len==0){ //空き領域不足 i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_OUT_BUFFER_TOO_SHORT; return 0; } - switch(NyLPC_cMiMicDbCompiler_getValue(i_inst,rp,l,val,&s)){ - case NyLPC_TcMiMicDbCompiler_ST_OK: + switch(NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&rp,o_val->ptr,&s)){ + case NyLPC_TcMiMicDbCompiler_RET_OK: //入力ポインタの移動 - l-=s; - rp+=s; + NyLPC_TCharArrayPtr_seek(&rp,s); //出力ポインタの移動 - val++; - val_len--; + NyLPC_TUInt32ArrayPtr_seek(&wp,1); continue; - case NyLPC_TcMiMicDbCompiler_ST_CONTINUE: + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: //フラグメント化してたらエラー i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FRAGMENT_UNIT; default: @@ -90,7 +96,7 @@ NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_getValues(NyLPC_TcMiMicDbCompiler_t* i_inst } } //変換完了 - return i_val_len-val_len; + return o_val->len-wp.len; } @@ -98,34 +104,35 @@ NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_getValues(NyLPC_TcMiMicDbCompiler_t* i_inst #ifndef TEST void main(void) { - const char* BC="00000001"; + int i=0; + struct NyLPC_TCharArrayPtr bc; + const char* BC="0000000100000002"; const char* rp; NyLPC_TcMiMicDbCompiler_t inst; - NyLPC_TUInt16 s,l; NyLPC_TUInt32 obuf[1024]; + NyLPC_TUInt16 pl; NyLPC_cMiMicDbCompiler_initialize(&inst); - s=strlen(BC); + bc.ptr=(char*)BC; + bc.len=strlen(BC); rp=BC; + NyLPC_cMiMicDbCompiler_compileFragment(&inst,&bc,obuf,&pl); /* for(;;){ - s=NyLPC_cMiMicDbCompiler_getValues(&inst,rp,s,obuf,1024); - if(s==0){ - printf("エラー"); - } - }*/ - - for(;;){ - if(NyLPC_cMiMicDbCompiler_getValue(&inst,rp,s,obuf,&l)==NyLPC_TcMiMicDbCompiler_ST_ERROR){ - printf("エラー"); - }else{ - s-=l; - rp+=l; - if(s<=0){ - - break; - } + switch(NyLPC_cMiMicDbCompiler_compileFragment2(&inst,*bc.ptr,&obuf[i])) + { + case NyLPC_TcMiMicDbCompiler_RET_ERROR: + printf("ERROR"); + return; + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: + NyLPC_TCharArrayPtr_seek(&bc,1); + break; + case NyLPC_TcMiMicDbCompiler_RET_OK: + i++; + NyLPC_TCharArrayPtr_seek(&bc,1); + break; } } +*/ return; } #endif diff --git a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h index b4e65e4..5a6a9af 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h +++ b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h @@ -7,41 +7,46 @@ #ifndef NYLPC_CMIMICDBCOMPILER_H_ #define NYLPC_CMIMICDBCOMPILER_H_ +#include "NyLPC_stdlib.h" typedef struct NyLPC_TcMiMicDbCompiler NyLPC_TcMiMicDbCompiler_t; typedef NyLPC_TUInt8 NyLPC_TcMiMicDbCompiler_ERROR; -//•s–¾‚ȃGƒ‰[ +//不明なエラー #define NyLPC_TcMiMicDbCompiler_ERROR_UNKNOWN 0x00 -//o—̓oƒbƒtƒ@‚ª‘«‚è‚È‚¢B +//出力バッファが足りない。 #define NyLPC_TcMiMicDbCompiler_ERROR_OUT_BUFFER_TOO_SHORT 0x01 -//BC‚ÌŒ`Ž®‚ª‚¨‚©‚µ‚¢B +//BCの形式がおかしい。 #define NyLPC_TcMiMicDbCompiler_ERROR_FORMAT 0x02 -//BCƒtƒ‰ƒOƒƒ“ƒg’PˆÊ‚ª‚¨‚©‚µ‚¢B(“r’†‚ŏI‚í‚Á‚Ä‚éH) +//BCフラグメント単位がおかしい。(途中で終わってる?) #define NyLPC_TcMiMicDbCompiler_ERROR_FRAGMENT_UNIT 0x03 -typedef NyLPC_TUInt8 NyLPC_TcMiMicDbCompiler_ST; -#define NyLPC_TcMiMicDbCompiler_ST_OK 1 -#define NyLPC_TcMiMicDbCompiler_ST_CONTINUE 2 -#define NyLPC_TcMiMicDbCompiler_ST_ERROR 3 +typedef NyLPC_TUInt8 NyLPC_TcMiMicDbCompiler_RET; +#define NyLPC_TcMiMicDbCompiler_RET_OK 1 +#define NyLPC_TcMiMicDbCompiler_RET_CONTINUE 2 +#define NyLPC_TcMiMicDbCompiler_RET_ERROR 3 struct NyLPC_TcMiMicDbCompiler { NyLPC_TUInt8 _bc_fragment_len; - NyLPC_TcMiMicDbCompiler_ERROR error_reason;//ƒGƒ‰[——R - NyLPC_TChar _tmp[8];//ƒeƒ“ƒ|ƒ‰ƒŠ + NyLPC_TcMiMicDbCompiler_ERROR error_reason;//エラー理由 + NyLPC_TChar _tmp[8];//テンポラリ }; void NyLPC_cMiMicDbCompiler_initialize(NyLPC_TcMiMicDbCompiler_t* i_inst); #define NyLPC_cMiMicDbCompiler_finalize(i); -NyLPC_TcMiMicDbCompiler_ST NyLPC_cMiMicDbCompiler_getValue(NyLPC_TcMiMicDbCompiler_t* i_inst,const NyLPC_TChar* i_bc_fragment,NyLPC_TUInt16 i_bc_len,NyLPC_TUInt32* o_val,NyLPC_TUInt16* o_parsed_bc); -NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_getValues(NyLPC_TcMiMicDbCompiler_t* i_inst,const NyLPC_TChar* i_bc_fragment,NyLPC_TUInt16 i_bc_len,NyLPC_TUInt32 o_val[],NyLPC_TUInt16 i_val_len); +NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment2(NyLPC_TcMiMicDbCompiler_t* i_inst,NyLPC_TChar i_bc,NyLPC_TUInt32* o_val); +NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_compile(NyLPC_TcMiMicDbCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,struct NyLPC_TUInt32ArrayPtr* o_val); +/** + * フラグメントを集積中であるかを返します。TRUEのとき、パース中であり、フラグメント待ち状態です。 + */ +#define NyLPC_cMiMicDbCompiler_hasFragment(i) ((i)->_bc_fragment_len>0) diff --git a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c index ef88881..d9d7454 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c +++ b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c @@ -27,7 +27,7 @@ #include #include "NyLPC_cMiMicTxtCompiler.h" #include "NyLPC_cMiMicVM_protected.h" - +#include "NyLPC_mimicvm_utils_protected.h" struct TInstructionDef @@ -49,51 +49,40 @@ static void NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(NyLPC_TcMiMicVM_OPR_TYPE i_type,N void NyLPC_cMiMicBcCompiler_initialize(NyLPC_TcMiMicTxtCompiler_t* i_inst) { i_inst->out_buf=NULL; -} - -/** - * コンパイル結果を保存するバッファをセットします。 - * @param i_output - * インストラクションのバイトイメージを格納するバッファ領域。4バイト境界に合わせてください。 - * @param i_length - * i_outputの長さ。少なくとも4バイト以上が必要です。 - * - */ -void NyLPC_cMiMicTxtCompiler_setOutputBuf(NyLPC_TcMiMicTxtCompiler_t* i_inst,NyLPC_TUInt8* i_output,NyLPC_TUInt16 i_length) -{ - i_inst->out_buf=i_output; - i_inst->buf_len=i_length; - i_inst->buf_left=i_length; i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; i_inst->tmp_len=0; } +#define NyLPC_TcMiMicTxtCompiler_ST_OPC 0x01 //OPをパース中 +#define NyLPC_TcMiMicTxtCompiler_ST_OPR 0x02 //オペランドを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_CTR 0x03 //制御パラメータを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_OK 0x04 //パースを完了した。(.EMD検出) +#define NyLPC_TcMiMicTxtCompiler_ST_NG 0xff //パースでエラーが発生ウェーイ + + /** - * バイトコードフラグメントをコンパイラに入力します。 + * バイトコードフラグメントから1命令をコンパイルして、i_binへ出力します。 * バイトコードは、バイナリ値に変換されます。 * バイナリ値は、MiMicVMの実行形式です。 * @return - * 実行結果を返します。TRUEのときは、ステータスをチェックしてください。 + * 実行結果を返します。NyLPC_TcMiMicTxtCompiler_RET_OKの場合に、i_binへo_bin_lenの長さのインストラクションを出力します。 * */ -NyLPC_TBool NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i_inst,const NyLPC_TChar* i_bc,NyLPC_TUInt16 i_length,NyLPC_TUInt16* o_len) +NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len,NyLPC_TUInt16* o_parsed_bc) { union NyLPC_TcMiMicVM_TInstruction* wptr; int i; - if(i_inst->out_buf==NULL){ - return NyLPC_TBool_FALSE; - } - for(i=0;ilen;i++) { switch(i_inst->st){ case NyLPC_TcMiMicTxtCompiler_ST_OPC: if(i_inst->tmp_len>2){ NyLPC_OnErrorGoto(ERROR); } - i_inst->tmp[i_inst->tmp_len]=*(i_bc+i); + i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); i_inst->tmp_len++; if(i_inst->tmp_len==2){ //[A-Z]{2}がそろった。命令コードか制御命令か判定 @@ -101,55 +90,56 @@ NyLPC_TBool NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i //命令コードならインストラクションの情報をもらってくる。 NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(i_inst->_current_oprtype,&i_inst->_oprbc_len,&i_inst->_inst_len); //バッファがインストラクション長よりも長いか調べておく。 - if(i_inst->_inst_len>i_inst->buf_left){ + if(i_inst->_inst_len>i_bin->len){ NyLPC_OnErrorGoto(ERROR); } - //オペランドがあるなら、オペランド解析へ - if(i_inst->_oprbc_len>0){ - //オペランド解析の準備 - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPR; - }else{ - //ゼロなら命令の記録 - wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_inst->out_buf; + //オペランドが無ければ、命令確定。 + if(i_inst->_oprbc_len<=0){ + //インストラクションの出力と処理したBC長の計算 + wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; wptr->op.opc=i_inst->_current_opc; wptr->op.oprtype=i_inst->_current_oprtype; - i_inst->out_buf+=i_inst->_inst_len; - i_inst->buf_left-=i_inst->_inst_len; - if(i_inst->buf_left_inst_len; + //解析バッファの長さをリセットして、次のBCブロックのパース準備 + i_inst->tmp_len=0; + return NyLPC_TcMiMicTxtCompiler_RET_OK;//命令確定。 } + //オペランドがあるなら、パース対象を変更。 + i_inst->tmp_len=0; + i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPR; }else if(bc2ctrlc(i_inst->tmp,&(i_inst->_current_opc),&(i_inst->_current_oprtype))){ //制御コードの解析 if(i_inst->_oprbc_len>0){ //パラメータのある制御命令未定義だからエラー。 NyLPC_OnErrorGoto(ERROR); - }else{ - //END制御命令ならパース終了 - if(i_inst->_current_opc==NyLPC_TcMiMicVM_CP_TYPE_END){ - *o_len=i+1; - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OK; - return NyLPC_TBool_TRUE; - } } + //END制御命令以外ならエラー + if(i_inst->_current_opc!=NyLPC_TcMiMicVM_CP_TYPE_END){ + NyLPC_OnErrorGoto(ERROR); + } + //END命令 + *o_parsed_bc=i+1; //パースしたBCの長さ + *o_bin_len=0; + i_inst->tmp_len=0; + return NyLPC_TcMiMicTxtCompiler_RET_OK_END;//命令確定。(パース完了) }else{ + //不明な命令 NyLPC_OnErrorGoto(ERROR); } - //解析バッファの長さをリセット - i_inst->tmp_len=0; - } break; case NyLPC_TcMiMicTxtCompiler_ST_CTR: + NyLPC_OnErrorGoto(ERROR); case NyLPC_TcMiMicTxtCompiler_ST_OPR: //オペランド蓄積. - i_inst->tmp[i_inst->tmp_len]=*(i_bc+i); + i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); i_inst->tmp_len++; //オペランド長さになるまで値を追記 if(i_inst->_oprbc_len==i_inst->tmp_len){ //オペランドの収集が完了したらパース - wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_inst->out_buf; + wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; wptr->op.opc=i_inst->_current_opc; wptr->op.oprtype=i_inst->_current_oprtype; //オペランドの変換処理 @@ -187,35 +177,46 @@ NyLPC_TBool NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i default: NyLPC_OnErrorGoto(ERROR); } - //OPR解析成功。wpをずらす。 - i_inst->out_buf+=i_inst->_inst_len; - i_inst->buf_left-=i_inst->_inst_len; - //バッファ空いてる?(少なくともOP+OPTの分) - if(i_inst->buf_left_inst_len; i_inst->tmp_len=0; i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; + return NyLPC_TcMiMicTxtCompiler_RET_OK; } break; default: NyLPC_OnErrorGoto(ERROR); } } - *o_len=i_length; - return NyLPC_TBool_TRUE; + *o_bin_len=0; + *o_parsed_bc=i_bc->len; + return NyLPC_TcMiMicTxtCompiler_RET_CONTINUE; ERROR: i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_NG; - return NyLPC_TBool_FALSE; + return NyLPC_TcMiMicTxtCompiler_RET_NG; } -NyLPC_TUInt16 NyLPC_cMiMicBcCompiler_getCompiledInstructionLength(NyLPC_TcMiMicTxtCompiler_t* i_inst) + + +/** + * フラグメント入力のMiMicBCをコンパイルします。 + * この関数は、1文字のフラグメントMiMicBCをコンパイラに入力します。 + * @return + * 実行結果を返します。TRUEのときは、ステータスをチェックしてください。 + * + */ +NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicBcCompiler_compileFragment2(NyLPC_TcMiMicTxtCompiler_t* i_inst,NyLPC_TChar i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len) { - return i_inst->buf_len-i_inst->buf_left; + struct NyLPC_TCharArrayPtr bc; + NyLPC_TUInt16 bc_len; + bc.ptr=&i_bc; + bc.len=1; + return NyLPC_cMiMicBcCompiler_compileFragment(i_inst,&bc,i_bin,o_bin_len,&bc_len); } + /** * 2バイトのバイトコードを、制御コードに変換します。 */ @@ -351,15 +352,15 @@ static void NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(NyLPC_TcMiMicVM_OPR_TYPE i_type,N NyLPC_TUInt8 oprbc_len; NyLPC_TUInt8 ist_len; }_tbl[]={ - {NyLPC_TcMiMicVM_OPR_TYPE_NONE, 0, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_WM, (1+1)*2, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H08, (1+1)*2, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H16, (1+2)*2, 8}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H32, (1+4)*2, 8}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM, (1)*2, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_H8, (1)*2, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_H16, (2)*2, 4}, - {NyLPC_TcMiMicVM_OPR_TYPE_H32, (4)*2, 8}, + {NyLPC_TcMiMicVM_OPR_TYPE_NONE, 0, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_WM, (1+1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H08, (1+1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H16, (1+2)*2, 2}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H32, (1+4)*2, 2}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM, (1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H8, (1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H16, (2)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H32, (4)*2, 2}, {0,0,0} }; int i; @@ -378,29 +379,43 @@ static void NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(NyLPC_TcMiMicVM_OPR_TYPE i_type,N #ifndef TEST void main(void) { - const char* BC="AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005DA0400000000DE0400000000EA04EE04EF00000000ZAZZ"; - const char* rp; + struct NyLPC_TCharArrayPtr bc; + const char* BC="AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005DA0400000000DE0400000000EA04EE04EF00000000ZAZZ.E"; NyLPC_TcMiMicTxtCompiler_t inst; - NyLPC_TUInt16 s,l; - NyLPC_TUInt8 obuf[1024]; + struct NyLPC_TUInt32ArrayPtr bin; + + NyLPC_TUInt16 l,bl; + NyLPC_TUInt32 obuf[1024]; NyLPC_cMiMicBcCompiler_initialize(&inst); - s=strlen(BC); - rp=BC; - NyLPC_cMiMicTxtCompiler_setOutputBuf(&inst,obuf,1024); + bc.ptr=(char* )BC; + bc.len=strlen(BC); + bin.ptr=obuf; + bin.len=5; for(;;){ - if(!NyLPC_cMiMicBcCompiler_compileFragment(&inst,rp,1,&l)){ - printf("エラー"); - }else{ - s-=l; - rp+=l; - if(s<=0){ - break; - } + switch(NyLPC_cMiMicBcCompiler_compileFragment(&inst,&bc,&bin,&bl,&l)) + { + case NyLPC_TcMiMicTxtCompiler_RET_OK: + //命令確定。 + NyLPC_TUInt32ArrayPtr_seek(&bin,bl); + NyLPC_TCharArrayPtr_seek(&bc,l); + break; + case NyLPC_TcMiMicTxtCompiler_RET_OK_END: + //命令終端 + printf("OK"); + break; + case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: + //蓄積中。 + NyLPC_TCharArrayPtr_seek(&bc,l); + break; + case NyLPC_TcMiMicTxtCompiler_RET_NG: + printf("エラー"); + return; + default: + break; } } - return; } #endif diff --git a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h index 1cfd3e9..3af718e 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h +++ b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h @@ -44,8 +44,6 @@ typedef NyLPC_TUInt8 NyLPC_TcMiMicTxtCompiler_ST; struct NyLPC_TcMiMicTxtCompiler{ NyLPC_TUInt8* out_buf; - NyLPC_TUInt16 buf_len; //出力バッファの長さ - NyLPC_TUInt16 buf_left; //バッファの残り長さ NyLPC_TUInt8 st;//ステータス NyLPC_TcMiMicVM_OP_TYPE _current_opc; //解析中のオペコード NyLPC_TUInt8 _current_oprtype; //解析中のオペランドタイプ @@ -55,11 +53,21 @@ struct NyLPC_TcMiMicTxtCompiler{ NyLPC_TChar tmp[24]; }; +typedef NyLPC_TUInt8 NyLPC_TcMiMicTxtCompiler_RET; +#define NyLPC_TcMiMicTxtCompiler_RET_OK 0x00 +#define NyLPC_TcMiMicTxtCompiler_RET_OK_END 0x01 //命令のパースに成功し、かつEND命令を検出。 +#define NyLPC_TcMiMicTxtCompiler_RET_CONTINUE 0x02 //続きのBCを要求している。 +#define NyLPC_TcMiMicTxtCompiler_RET_NG 0x80 + + + void NyLPC_cMiMicBcCompiler_initialize(NyLPC_TcMiMicTxtCompiler_t* i_inst); #define NyLPC_cMiMicBcCompiler_finalize(i) -void NyLPC_cMiMicTxtCompiler_setOutputBuf(NyLPC_TcMiMicTxtCompiler_t* i_inst,NyLPC_TUInt8* out_buf,NyLPC_TUInt16 i_length); -NyLPC_TUInt16 NyLPC_cMiMicBcCompiler_getCompiledInstructionLength(NyLPC_TcMiMicTxtCompiler_t* i_inst); -NyLPC_TBool NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i_inst,const NyLPC_TChar* i_bc,NyLPC_TUInt16 i_length,NyLPC_TUInt16* o_len); -#define NyLPC_cMiMicBcCompiler_getStatus(i) ((i)->st) +NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicBcCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len,NyLPC_TUInt16* o_parsed_bc); +NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicBcCompiler_compileFragment2(NyLPC_TcMiMicTxtCompiler_t* i_inst,NyLPC_TChar i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len); + + + + #endif /* NYLPC_CMIMICTXTCOMPILER_H_ */ diff --git a/lib/src/mimicvm/NyLPC_mimicvm_utils.c b/lib/src/mimicvm/NyLPC_mimicvm_utils.c index 88ac679..50ce080 100644 --- a/lib/src/mimicvm/NyLPC_mimicvm_utils.c +++ b/lib/src/mimicvm/NyLPC_mimicvm_utils.c @@ -43,3 +43,4 @@ NyLPC_TBool NyLPC_mimicvm_txt2UInt(const NyLPC_TChar* i_txt,NyLPC_TUInt8 i_num,v } return NyLPC_TBool_TRUE; } + diff --git a/lib/src/mimicvm/NyLPC_mimicvm_utils_protected.h b/lib/src/mimicvm/NyLPC_mimicvm_utils_protected.h index 42185f9..c2899c1 100644 --- a/lib/src/mimicvm/NyLPC_mimicvm_utils_protected.h +++ b/lib/src/mimicvm/NyLPC_mimicvm_utils_protected.h @@ -10,6 +10,9 @@ #include "NyLPC_stdlib.h" + + + NyLPC_TBool NyLPC_mimicvm_txt2UInt(const NyLPC_TChar* i_txt,NyLPC_TUInt8 i_num,void* out);