1 / 15

6.3 インタプリタ (1)インタプリタ( interpreter )とは

6.3 インタプリタ (1)インタプリタ( interpreter )とは. ① 解釈実行系、直接実行系、通訳系等と呼ばれる。 ②原則的には原始プログラムとデータを入力して結果を出力。 ③中間言語を出力して中間言語を解釈実行する方法もある。 ④仮想的な機械語を設定し、これを解釈実行するタイプもある(これをエミュレータということもある)。 ただし、最後の例は、マクロ定義と組み合わせることで コンパイラとなりうる。. (2)スタックマシンエミュレータの例. private void Eval_Initialize() { ptrEvalStack=0;

Download Presentation

6.3 インタプリタ (1)インタプリタ( interpreter )とは

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6.3 インタプリタ(1)インタプリタ(interpreter)とは6.3 インタプリタ(1)インタプリタ(interpreter)とは ①解釈実行系、直接実行系、通訳系等と呼ばれる。 ②原則的には原始プログラムとデータを入力して結果を出力。 ③中間言語を出力して中間言語を解釈実行する方法もある。 ④仮想的な機械語を設定し、これを解釈実行するタイプもある(これをエミュレータということもある)。 ただし、最後の例は、マクロ定義と組み合わせることで コンパイラとなりうる。

  2. (2)スタックマシンエミュレータの例 private void Eval_Initialize() { ptrEvalStack=0; ptrNameTable=0; ptrProgStack=0; } 初期化

  3. トップレベルのみの概要(1) private void Eval() { NameData P1,P2; int i=0; pushEvalStack(new NameData("*Function*")); while(i<numStatement) { nextIP =i+1; if (AllStatement[i].operation=="end") { MessageBox.Show(“プログラムの終了です。カウンタ=” + i.ToString()); break; } switch (AllStatement[i].operation) { case "+" :Eval_add();break; case "-" :Eval_sub();break; case "%" :Eval_mod();break;

  4. トップレベルのみの概要(2) case "*" :Eval_mult();break; case "mod" :Eval_mod();break; case "/" :Eval_dev();break; case "^" :Eval_exp();break; case "=" :Eval_set();break; case "++" :Eval_PPset();break; case "--" :Eval_MMset();break; case "++$" :Eval_BeforPPset();break; case "--$" :Eval_BeforMMset();break; case "-$" :Eval_minus();break; case "+$" :Eval_plus();break; case "+=" :Eval_AsignPset();break; case "-=" :Eval_AsignMset();break; case "*=" :Eval_AsignMultset();break; case "/=" :Eval_AsignDivset();break; case "==" :Eval_equal();break; case "!=" :Eval_not_equal();break; case ">" :Eval_greater_than();break;

  5. トップレベルのみの概要(3) case ">" :Eval_greater_than();break; case ">=" :Eval_greater_than_equal();break; case "<" :Eval_less_than();break; case "<=" :Eval_less_than_equal();break; case "||" :Eval_or();break; case "&&" :Eval_and();break; case "%%" :Eval_exclusive_or();break; case "!" :Eval_not();break; case "ArgEnd" :Eval_Arg();break; case "Func" :Eval_func(AllStatement[i].str); break; case "*dimStart" :Eval_dimStart(AllStatement[i].str); break; case "dim" :Eval_dim();break; case "*PStart*" :Eval_PStart();break; case "*Parm*" :Eval_Param(AllStatement[i].str); break; case "return" :Eval_return();break;

  6. トップレベルのみの概要(4) case "StNo" :StatementNo=int.Parse(AllStatement[i].str); ptrEvalStack=0;break; case "goto" :P1=Eval(popEvalStack()); nextIP=(int)P1.Val; break; case "then" :P2=Eval(popEvalStack()); P1=Eval(popEvalStack()); if(P1.Type=="Number" && P2.Type=="Number" ) { if(P1.Val ==0) nextIP=(int)P2.Val; } else MessageBox.Show("評価エラーです"); break; default :pushEvalStack(AllStatement[i]);break; } i=nextIP; } }

  7. (3)仮想マシンエミュレータ ①命令取出(命令フェッチ)   実行アドレス(注)の命令を取り出してオペランドに分離する。 ②次のアドレス計算   次の実行アドレスを設定。なお、ジャンプ命令は次の実行アドレスを変更することで実現される。 ③命令実行   命令の実行。 ④命令実行後の状態フラグ設定   算術演算等により演算結果の状態フラグを設定する。この状態フラグより、JP, JZ, JM でジャンプするかどうかを判定する。 (注)実行アドレスは、命令カウンタ(instruction counter)、プログラムカウンタ(program counter)とも呼ばれる。 

  8. 以下VB6による例 Private Sub 初期化() SVC_Call = 0 ExecAddress = 0 End Sub 初期化

  9. 1命令の実行 Private Sub Command2_Click() 命令取出  アドレス計算  命令実行 If SVC_Flag Then If SVC_Call = 0 Then CRLF = Chr(13) & Chr(10) Text3.Text = Right(Text3.Text, 5000) & CRLF & _ "プログラムの終了です." ExecAddress = 0 命令表示 End If End If End Sub

  10. 命令取出し Private Sub 命令取出() A = MCode(ExecAddress): MPart(0) = A \ &H100: A2 = A Mod &H100 If MPart(0) >= &H80 Then MPart(1) = A2 \ &H10: MPart(2) = A2 Mod &H10 MPart(3) = MCode(ExecAddress + 1) ExecAddress = ExecAddress + 2 ElseIf MPart(0) = &H50 Then MPart(1) = A2 \ &H10: MPart(2) = A2 Mod &H10 ExecAddress = ExecAddress + 1 Else MPart(1) = 0: MPart(2) = 0: MPart(3) = A2 ExecAddress = ExecAddress + 1 End If End Sub

  11. アドレス計算 Private Sub アドレス計算() If MPart(0) >= &H80 Then If MPart(2) = 0 Then EffectAddress = MPart(3) Else ID = MPart(2) EffectAddress = Register(ID) + MPart(3) End If Else EffectAddress = MPart(3) End If End Sub

  12. 命令実行直後の状態フラグ設定 Private Sub 状態フラグ設定(Rno) Status(0) = 0: Status(1) = 0: Status(2) = 0 If Register(Rno) > 0 Then Status(0) = 1 ElseIf Register(Rno) = 0 Then Status(1) = 1 Else Status(2) = 1 End If End Sub

  13. 命令実行(その1) Private Sub 命令実行() SVC_Flag = False Select Case MPart(0) Case &HA0: Register(MPart(1)) = MCode(EffectAddress) 'Load 状態フラグ設定 MPart(1) Case &HA2: MCode(EffectAddress) = Register(MPart(1)) 'Store 状態フラグ設定 MPart(1) Case &HB0: Register(MPart(1)) = EffectAddress 'LI (Load Imediate) 状態フラグ設定 MPart(1) Case &H81: Register(MPart(1)) = Register(MPart(1)) + _ MCode(EffectAddress) 'ADD 状態フラグ設定 MPart(1) Case &H82: Register(MPart(1)) = Register(MPart(1)) - _ MCode(EffectAddress) 'SUB 状態フラグ設定 MPart(1) Case &H83: Register(MPart(1)) = Register(MPart(1)) * _ MCode(EffectAddress) 'MULT 状態フラグ設定 MPart(1)

  14. 命令実行(その2) Case &H84: Register(MPart(1)) = Register(MPart(1)) \ _ MCode(EffectAddress) 'DIV 状態フラグ設定 MPart(1)  ・   ・  (中略)  ・ Case &HE0: ExecAddress = EffectAddress 'JMP Case &HE1: If Status(0) <> 0 Then ExecAddress = EffectAddress 'JP (Jump Plus) Case &HE2: If Status(1) <> 0 Then ExecAddress = EffectAddress 'JZ (Jump Zero) Case &HE3: If Status(2) <> 0 Then ExecAddress = EffectAddress 'JM (Jump Minus) ・   ・  (中略)  ・

  15. 命令実行(その3)  ・   ・  (中略)  ・ Case &H61: Register(MPart(1)) = Val(InputBox("データ入力")) Case &H1: SVC_Call = MPart(1): SVC_Flag = True 'SVC_Call End Select End Sub

More Related