jasmin java int addint a int b return
最も簡単なjasminの例 javaソース(一部) int add(int a, int b){ return a+b; } djava 全体比較は, iadd/cmp. html を参照 jasminソース(一部). method add(II)I. limit stack 2. limit locals 3 iload_1 iload_2 iadd ireturn. end method 本日はフレーム内計算しかしないので, 他の部分には言及しない 11
良く似たjasminの例 javaソース(一部) int add 2(int a, int b){ int c; c=a+b; return c; } djava jasminソース(一部). method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method 12
jasminメソッドの引数(例). method add(II)I. limit stack 2. limit locals 3 iload_1 iload_2 iadd ireturn. end method • (II)Iは引数と返り値の 型を規定. – II 整数が二つ – I 整数が1つ int add(int a, int b){ return a+b; } 14
ローカル変数の対応. method add(II)I. limit stack 2. limit locals 3 iload_1 iload_2 iadd ireturn. end method • 3つあるという意味. • 0から順に番号付けさ れている. 1番 0番 2番 int add(int a, int b){ return a+b; } インスタンスへのリファレンス 16
他の例のローカル変数の対応. method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 0番 iadd istore_3 iload_3 ireturn. end method 1番 3番 2番 int add 2(int a, int b){ int c; c=a+b; return c; } インスタンスへのリファレンス 17
フレーム内計算の例 1/3 add 2(600, 123) を呼ぶ場合. . method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method ローカル変数 0 1 2 3 スタック 600 123 iload_1 ローカル変数 0 1 2 3 スタック 600 123 600 iload_2 教科書p. 29とほぼ同じ 18
フレーム内計算の例 2/3 add 2(600, 123) を呼ぶ場合. . method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method ローカル変数 0 1 2 3 スタック 600 123 600 iadd ローカル変数 0 1 2 3 スタック 600 123 723 istore_3 教科書p. 29とほぼ同じ 19
フレーム内計算の例 3/3 add 2(600, 123) を呼ぶ場合 . method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method 教科書p. 29とほぼ同じ ローカル変数 0 1 2 3 スタック 600 123 723 iload_3 ローカル変数 0 1 2 3 スタック 600 123 723 ireturn (フレーム自体が消滅,来週の話) 20
フレーム内計算のテキスト表現 add 2(600, 123) を呼ぶ場合. method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method local=[this, 600, 123, ] stack=[] iload_1 local=[this, 600, 123, ] stack=[600] iload_2 local=[this, 600, 123, ] stack=[600, 123] iadd local=[this, 600, 123, ] stack=[723] istore_3 local=[this, 600, 123, 723] stack=[] iload_3 local=[this, 600, 123, 723] stack=[723] ireturn 22
stack長は必要最低限である add 2(600, 123) を呼ぶ場合. method add 2(II)I. limit stack 2. limit locals 4 iload_1 iload_2 iadd istore_3 iload_3 ireturn. end method local=[this, 600, 123, ] stack=[] iload_1 local=[this, 600, 123, ] stack=[600] iload_2 local=[this, 600, 123, ] stack=[600, 123] iadd local=[this, 600, 123, ] stack=[723] istore_3 local=[this, 600, 123, 723] stack=[] iload_3 local=[this, 600, 123, 723] stack=[723] ireturn 23
if文の展開例 int larger(int a, int b){ ① ② int c; ③ if(a>b) c=a; else c=b; return c; } フレーム内の計算過 程は,ifelse/exe. html を参照. . method larger(II)I. limit stack 2. limit locals 4 iload_1 iload_2 if_icmple Label 1 iload_1 istore_3 goto Label 2 Label 1: iload_2 istore_3 Label 2: iload_3 ireturn. end method 24
ループの展開の例 単に 0からn-1まで 加算するルーチン djava int for. Loop(int n){ ① int s=0; ② ③ for(int c=0; c<n; c++) s+=c; return s; } . method for. Loop(I)I Label 1: . limit stack 2 iload_2. limit locals 4 iload_3 iconst_0 iadd istore_2 iconst_0 iinc 3 1 istore_3 Label 2: goto Label 2 iload_3 iload_1 if_icmplt Label 1 iload_2 ireturn. end method 25
- Slides: 26