{"id":847,"date":"2022-07-06T14:08:12","date_gmt":"2022-07-06T05:08:12","guid":{"rendered":"https:\/\/www8281uo.sakura.ne.jp\/blog\/?p=847"},"modified":"2022-07-06T14:08:12","modified_gmt":"2022-07-06T05:08:12","slug":"c-%e8%a8%80%e8%aa%9e%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e3%81%8b%e3%82%89-mips-%e3%82%a2%e3%82%bb%e3%83%b3%e3%83%96%e3%83%aa%e3%82%92%e7%94%9f%e6%88%90%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www8281uo.sakura.ne.jp\/blog\/?p=847","title":{"rendered":"C \u8a00\u8a9e\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089 MIPS \u30a2\u30bb\u30f3\u30d6\u30ea\u3092\u751f\u6210\u3059\u308b\u65b9\u6cd5\u3068\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3088\u308b\u6700\u9069\u5316\u306b\u3064\u3044\u3066"},"content":{"rendered":"\n<p>\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fb\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u8b1b\u7fa9\u3067 MIPS \u547d\u4ee4\u30bb\u30c3\u30c8\u3092\u4f7f\u3063\u3066\u8aac\u660e\u3092\u3057\u3066\u3044\u308b\u304c\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u751f\u6210\u3059\u308b\u30a2\u30bb\u30f3\u30d6\u30ea\u304c\u6c17\u306b\u306a\u308b\u4eba\u3082\u3044\u308b\u3068\u601d\u3046\u306e\u3067\u3053\u306e\u8a18\u4e8b\u3067\u6271\u3063\u3066\u304a\u304f\u3002<\/p>\n\n\n\n<p>\u6700\u8fd1\u306e\u74b0\u5883\u3060\u3068 clang \u3068\u3044\u3046\u30b3\u30f3\u30d1\u30a4\u30e9\u3092\u4f7f\u3046\u306e\u304c\u826f\u3044\u3002Windows \u3067\u306f\u3001msys2 \u3092\u4f7f\u3046\u3068 clang \u3092\u7c21\u5358\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u74b0\u5883\u3065\u304f\u308a\uff1aclang \u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n\n\n\n<p>msys2 \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u5f8c\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5165\u529b\u3059\u308b\u3068 clang \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ pacman -S msys\/clang<\/pre>\n\n\n\n<p>\u73fe\u5728\uff082022\/7\/4\uff09\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u4ee5\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ clang --version\nclang version 11.0.0 (https:\/\/github.com\/msys2\/MSYS2-packages ca391a3660d17cdee1e94d5afd2e72a4f750cddb)\nTarget: x86_64-pc-windows-msys\nThread model: posix\nInstalledDir: \/bin<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">mips \u30bf\u30fc\u30b2\u30c3\u30c8\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u751f\u6210<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b3\u30f3\u30d1\u30a4\u30eb\u5bfe\u8c61\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\uff08fib.c\uff09<\/h3>\n\n\n\n<p>\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3092\u518d\u5e30\u7684\u306b\u8a08\u7b97\u3059\u308b\u305f\u3081\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f8b\u306b\u3059\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int fib(int n)\n{\n  if (n == 0)\n    return 0;\n  else if (n == 1)\n    return 1;\n  else\n    return fib(n-1) + fib(n-2);\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u30a2\u30bb\u30f3\u30d6\u30ea\u751f\u6210<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30a2\u30bb\u30f3\u30d6\u30ea\u8a00\u8a9e\u3092\u51fa\u529b\u3067\u304d\u308b\uff08\u51fa\u529b\u306f fib.s \u3068\u3044\u3046\u540d\u524d\u306e\u30d5\u30a1\u30a4\u30eb\uff09\u3002\u6700\u9069\u5316\u3092\u3057\u306a\u3044\uff08\uff1d\u6975\u529b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u8868\u73fe\u306e\u307e\u307e\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u3092\u51fa\u529b\uff09\u5834\u5408\u306f <code>-O0<\/code> \u3092\u3064\u3051\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ clang -O0 -S -target mips -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -mcpu=mips32 fib.c<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u6210\u7d50\u679c<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\t.text\n\t.abicalls\n\t.option\tpic0\n\t.section\t.mdebug.abi32,\"\",@progbits\n\t.nan\tlegacy\n\t.text\n\t.file\t\"fib.c\"\n\t.globl\tfib                             # -- Begin function fib\n\t.p2align\t2\n\t.type\tfib,@function\n\t.set\tnomicromips\n\t.set\tnomips16\n\t.ent\tfib\nfib:                                    # @fib\n\t.frame\t$fp,40,$ra\n\t.mask \t0xc0000000,-4\n\t.fmask\t0x00000000,0\n\t.set\tnoreorder\n\t.set\tnomacro\n\t.set\tnoat\n# %bb.0:\n\taddiu\t$sp, $sp, -40\n\tsw\t$ra, 36($sp)                    # 4-byte Folded Spill\n\tsw\t$fp, 32($sp)                    # 4-byte Folded Spill\n\tmove\t$fp, $sp\n\tsw\t$4, 24($fp)\n\tlw\t$1, 24($fp)\n\tbnez\t$1, $BB0_3\n\tnop\n# %bb.1:\n\tj\t$BB0_2\n\tnop\n$BB0_2:\n\tsw\t$zero, 28($fp)\n\tj\t$BB0_7\n\tnop\n$BB0_3:\n\tlw\t$1, 24($fp)\n\taddiu\t$2, $zero, 1\n\tbne\t$1, $2, $BB0_6\n\tnop\n# %bb.4:\n\tj\t$BB0_5\n\tnop\n$BB0_5:\n\taddiu\t$1, $zero, 1\n\tsw\t$1, 28($fp)\n\tj\t$BB0_7\n\tnop\n$BB0_6:\n\tlw\t$1, 24($fp)\n\taddiu\t$4, $1, -1\n\tjal\tfib\n\tnop\n\tlw\t$1, 24($fp)\n\taddiu\t$4, $1, -2\n\tsw\t$2, 20($fp)                     # 4-byte Folded Spill\n\tjal\tfib\n\tnop\n\tlw\t$1, 20($fp)                     # 4-byte Folded Reload\n\taddu\t$2, $1, $2\n\tsw\t$2, 28($fp)\n\tj\t$BB0_7\n\tnop\n$BB0_7:\n\tlw\t$2, 28($fp)\n\tmove\t$sp, $fp\n\tlw\t$fp, 32($sp)                    # 4-byte Folded Reload\n\tlw\t$ra, 36($sp)                    # 4-byte Folded Reload\n\taddiu\t$sp, $sp, 40\n\tjr\t$ra\n\tnop\n\t.set\tat\n\t.set\tmacro\n\t.set\treorder\n\t.end\tfib\n$func_end0:\n\t.size\tfib, ($func_end0)-fib\n                                        # -- End function\n\t.ident\t\"clang version 11.0.0 (https:\/\/github.com\/msys2\/MSYS2-packages ca391a3660d17cdee1e94d5afd2e72a4f750cddb)\"\n\t.section\t\".note.GNU-stack\",\"\",@progbits\n\t.addrsig\n\t.addrsig_sym fib\n\t.text<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7d50\u679c\u306e\u898b\u65b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>BB \u306f Basic Block\uff08\u57fa\u672c\u30d6\u30ed\u30c3\u30af\uff09\u3092\u610f\u5473\u3057\u3066\u3044\u308b\u3002<ul><li>\u4e00\u3064\u306e\u5165\u308a\u53e3\uff08\u3059\u306a\u308f\u3061\u3001\u5185\u90e8\u306e\u30b3\u30fc\u30c9\u304c\u4ed6\u306e\u30b3\u30fc\u30c9\u306e\u5206\u5c90\u5148\u306b\u306a\u3063\u3066\u3044\u306a\u3044)\u3068\u4e00\u3064\u306e\u51fa\u53e3\u3092\u6301\u3061\u3001\u5185\u90e8\u306b\u5206\u5c90\u3092\u542b\u307e\u306a\u3044\u30b3\u30fc\u30c9\u3092\u6307\u3059\u3002\uff08<a href=\"https:\/\/ja.wikipedia.org\/wiki\/%E5%9F%BA%E6%9C%AC%E3%83%96%E3%83%AD%E3%83%83%E3%82%AF#:~:text=%E5%9F%BA%E6%9C%AC%E3%83%96%E3%83%AD%E3%83%83%E3%82%AF%EF%BC%88%E3%81%8D%E3%81%BB%E3%82%93%E3%83%96%E3%83%AD%E3%83%83%E3%82%AF,%E3%82%B8%E3%83%A3%E3%83%B3%E3%83%97%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%81%8C%E3%81%A7%E3%81%8D%E3%82%8B%E3%80%82\">Wikipedia<\/a>\uff09<\/li><\/ul><\/li><li>\u30b9\u30bf\u30c3\u30af\u9818\u57df\u306b 40 \u30d0\u30a4\u30c8\u78ba\u4fdd\u3059\u308b\uff08\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u3044\u308b\u306e\u306f 20 \u30d0\u30a4\u30c8\uff09<\/li><li>move \u547d\u4ee4\u306a\u3069\u306f\u30a2\u30bb\u30f3\u30d6\u30ea\u3067\u8a31\u3055\u308c\u3066\u3044\u308b\u6c4e\u7528\u8868\u73fe\u3067\u3001\u3053\u306e\u5f8c\u6a5f\u68b0\u8a9e\u306b\u30a2\u30bb\u30f3\u30d6\u30eb\u3059\u308b\u3068\u304d\u306b\u306f\u5b9f\u5728\u3059\u308b\u547d\u4ee4\u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u308b\u3002<\/li><li>MIPS \u306b\u306f <code>push<\/code>, <code>pop<\/code> \u306a\u3069\u30b9\u30bf\u30c3\u30af\u3092\u64cd\u4f5c\u3059\u308b\u547d\u4ee4\u306f\u306a\u3044\u306e\u3067\u3001\u30b9\u30bf\u30c3\u30af\u9818\u57df\u306b\u5bfe\u3057\u3066 load word (<code>lw<\/code>), store word (<code>sw<\/code>) \u3057\u3066\u3044\u308b\u3002<\/li><li><code>$fp<\/code> \u306e\u5024\u306f <code>$sp<\/code> \u3068\u540c\u3058\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/li><li>MIPS \u306f\u9045\u5ef6\u30b9\u30ed\u30c3\u30c8\u3092\uff11\u3064\u3082\u3064\u306e\u3067\u3001\u5206\u5c90\u547d\u4ee4\u306e\u6b21\u306e\u547d\u4ee4\u307e\u3067\u5206\u5c90\u524d\u306b\u5b9f\u884c\u3055\u308c\u308b\u3002\u4e0a\u306e\u30a2\u30bb\u30f3\u30d6\u30ea\u3067\u306f <code>nop<\/code> \u547d\u4ee4\u304c\u7f6e\u304b\u308c\u3066\u3044\u308b\u3002<\/li><li><code>$BB0_6<\/code> \u3067 <code>jal fib<\/code> \u304c\uff12\u56de\u547c\u3070\u308c\u3066\u3044\u3066\u3001\u3053\u308c\u3089\u306f <code>fib(n-1)<\/code> \u3068 <code>fib(n-2)<\/code> \u306b\u305d\u308c\u305e\u308c\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u95a2\u6570\u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u3068\u30b9\u30bf\u30c3\u30af<\/h3>\n\n\n\n<p>\u3053\u306e\u6642\u3001<code>fib()<\/code> \u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u56de\u6570\u3092\u8abf\u3079\u3066\u307f\u3088\u3046\u3002\u30c7\u30d0\u30c3\u30ac\uff08<code>gdb<\/code>\uff09\u3092\u4f7f\u3046\u3068\u8abf\u3079\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u672c\u8a18\u4e8b\u3067\u306f <code>fib(10)<\/code> \u3092\u8a08\u7b97\u3057\u305f\u3068\u304d\u306e\u69d8\u5b50\u3092\u793a\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ gdb a.exe\nGNU gdb (GDB) 11.1\nCopyright (C) 2021 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\nType \"show copying\" and \"show warranty\" for details.\nThis GDB was configured as \"x86_64-pc-msys\".\nType \"show configuration\" for configuration details.\nFor bug reporting instructions, please see:\n&lt;https:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;.\nFind the GDB manual and other documentation resources online at:\n    &lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;.\n\nFor help, type \"help\".\nType \"apropos word\" to search for commands related to \"word\"...\nTraceback (most recent call last):\n  File \"&lt;string&gt;\", line 3, in &lt;module&gt;\nModuleNotFoundError: No module named 'libstdcxx'\n\/etc\/gdbinit:6: Error in sourced command file:\nError while executing Python code.\nReading symbols from a.exe...\n(gdb) b fib\nBreakpoint 1 at 0x10040108a\n(gdb) ignore 1 100000\nWill ignore next 100000 crossings of breakpoint 1.\n(gdb) r\nStarting program: \/path\/to\/a.exe\n[New Thread 6748.0x4874]\n[New Thread 6748.0x3e68]\n[New Thread 6748.0x2534]\nc: 55\n[Thread 6748.0x5c20 exited with code 0]\n[Thread 6748.0x2534 exited with code 0]\n[Thread 6748.0x4874 exited with code 0]\n[Inferior 1 (process 6748) exited normally]\n(gdb) info breakpoints\nNum     Type           Disp Enb Address            What\n1       breakpoint     keep y   0x000000010040108a &lt;fib+10&gt;\n        breakpoint already hit 177 times\n        ignore next 99823 hits\n(gdb) q<\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u7d50\u679c\u304b\u3089\u3001<code>fib(10)<\/code> \u306e\u8a08\u7b97\u306b177\u56de <code>fib()<\/code> \u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002<\/p>\n\n\n\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u305d\u308c\u81ea\u4f53\u304b\u3089\u3001 <code>fig(k)<\/code> \u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u306f <code>fib(k+1)<\/code> \u3068 <code>fib(k+2)<\/code> \u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u306e\u548c\u3067\u3042\u308b\u3002<code>fib(k)<\/code> \u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u3092 <code>g(k)<\/code> \u3068\u66f8\u304f\u3053\u3068\u306b\u3059\u308c\u3070\u3001<code>g(k) = g(k+1) + g(k+2)<\/code> \u3067\u3042\u308a\u3001\u3053\u308c\u306f\u30d5\u30a3\u30dc\u30ca\u30c3\u30c1\u6570\u5217\u3068\u985e\u4f3c\u306e\u69cb\u9020\u3092\u6301\u3063\u3066\u3044\u308b\u3002<code>fib()<\/code> \u547c\u3073\u51fa\u3057\u56de\u6570\u306e\u7dcf\u6570\u306f <code>k<\/code> \u306b\u3064\u3044\u3066\u548c\u3092\u3068\u308c\u3070\u3088\u3044\u3002<code>fib(1)<\/code> \u306f <code>fib(0)<\/code> \u3092\u547c\u3070\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3059\u308c\u3070\u3001 <code>fib(n)<\/code> \u3092\u5f97\u308b\u305f\u3081\u306b\u306f <code>fib(k)<\/code> \u3092 <code>k=0,...,n<\/code> \u307e\u3067\u8db3\u3057\u3066\u3055\u3089\u306b <code>fib(n-1)<\/code> \u3092\u52a0\u3048\u305f\u56de\u6570\u3068\u4e00\u81f4\u3059\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u306f\u305a\u3060\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001\u95a2\u6570\u547c\u3073\u51fa\u3057\u306e\u6df1\u3055\u306f <code>fib(n)<\/code> \u3092\u8003\u3048\u308b\u3068\u304d\u306b <code>fib(n-1)<\/code> \u3092\u8a08\u7b97\u3059\u308b\u30d1\u30b9\u304c\u6700\u3082\u6df1\u304f\u306a\u308b\u3002\u305d\u3057\u3066\u3001\u3053\u308c\u306f <code>fib(1)<\/code> \u306b\u305f\u3069\u308a\u7740\u304f\u307e\u3067\u30cd\u30b9\u30c8\u3059\u308b\u306e\u3067\u6700\u5927\u3067 <code>n-1<\/code> \u306e\u6df1\u3055\u3068\u306a\u308b\u3002\u4e0a\u306e\u30b3\u30fc\u30c9\u3067\u306f\uff11\u56de\u306e\u547c\u3073\u51fa\u3057\u306b40\u30d0\u30a4\u30c8\u306e\u30b9\u30bf\u30c3\u30af\u304c\u5fc5\u8981\u306a\u306e\u3067\u3001<code>40 \u00d7 (n-1)<\/code> \u306e\u30b9\u30bf\u30c3\u30af\u9818\u57df\u304c\u5c11\u306a\u304f\u3068\u3082\u5fc5\u8981\u3067\u3001\u5927\u304d\u306a <code>n<\/code> \u306b\u3064\u3044\u3066\u8a08\u7b97\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30b9\u30bf\u30c3\u30af\u9818\u57df\u3092\u4f7f\u3044\u3064\u304f\u3057\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u30a8\u30e9\u30fc\u3068\u306a\u308b\uff08\u30b9\u30bf\u30c3\u30af\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u3068\u3044\u3046\u3002\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u3088\u3046\uff09\u3002<\/p>\n\n\n\n<p>\u4e0a\u8a18\u3092\u3061\u3083\u3093\u3068\u7406\u89e3\u3059\u308c\u3070\u3001\u3068\u3066\u3082\u975e\u52b9\u7387\u306a\u3053\u3068\u3092\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3068\u601d\u3046\u3002\uff08\u6574\u6570\u306e\u8db3\u3057\u7b97\u3057\u304b\u3057\u306a\u3044\u306e\u306b\u3001\u7b54\u3048\u306e\u6570\u5024\u3088\u308a\u3082\u591a\u3044\u56de\u6570\u306e\u95a2\u6570\u547c\u3073\u51fa\u3057\u304c\u5fc5\u8981\u3002\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u77e5\u3063\u3066\u304a\u304f\u3068\u3088\u3044\u304b\u3082\u3057\u308c\u306a\u3044\u3053\u3068<\/h2>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u7d39\u4ecb\u3059\u308b MIPS \u30a2\u30bb\u30f3\u30d6\u30ea\u751f\u6210\u65b9\u6cd5\u306f\u30af\u30ed\u30b9\u30b3\u30f3\u30d1\u30a4\u30eb\u3068\u3044\u3046\u3082\u306e\u3067\u3001\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u542b\u307e\u308c\u308b\u95a2\u6570\u306a\u3069\u3092\u547c\u3076\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u5834\u5408\u304c\u3042\u308b\u3002\u5b9f\u969b\u306b\u5b9f\u884c\u3057\u305f\u3044\u308f\u3051\u3067\u306a\u3051\u308c\u3070\u3001\u305d\u306e\u3088\u3046\u306a\u95a2\u6570\u306f\u9069\u5f53\u306a\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u5ba3\u8a00\u3060\u3051\u3057\u3066\u304a\u3044\u3066\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308c\u3070\u3088\u3044\u3002\u4f8b\u3048\u3070\u3001printf \u306e\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u304a\u3051\u3070\u3088\u3044\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int printf(const char *, ...);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u9069\u5316 (-O2) \u3059\u308b\u3068\u3069\u3046\u306a\u308b\u304b\uff1f<\/h2>\n\n\n\n<p>\u30b3\u30f3\u30d1\u30a4\u30e9\u306f\u30d7\u30ed\u30bb\u30c3\u30b5\u3067\u5b9f\u884c\u3057\u305f\u3068\u304d\u306b\u9069\u3057\u305f\uff08\u4f8b\u3048\u3070\u3001\u9ad8\u901f\u306b\u306a\u308b\u3088\u3046\u306b\u3001\u306a\u3069\uff09\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u51fa\u529b\u3059\u308b\u305f\u3081\u306e\u6700\u9069\u5316\u6a5f\u80fd\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u3066\u3001\u305d\u308c\u3092\u4f7f\u3046\u3068\u7c21\u5358\u306b10\u500d\u304f\u3089\u3044\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u6027\u80fd\u304c\u5909\u308f\u3063\u305f\u308a\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u6a19\u6e96\u7684\u306a\u6700\u9069\u5316\u3067\u3042\u308b <code>-O2<\/code> \u3092\u3064\u3051\u305f\u6642\u306b\u51fa\u529b\u7d50\u679c\u304c\u3069\u3046\u5909\u308f\u308b\u304b\u898b\u3066\u307f\u3088\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ clang -O2 -S -target mips -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -mcpu=mips32 fib.c<\/pre>\n\n\n\n<p>\u3053\u306e\u6642\u751f\u6210\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">        .text\n        .abicalls\n        .option pic0\n        .section        .mdebug.abi32,\"\",@progbits\n        .nan    legacy\n        .text\n        .file   \"fib.c\"\n        .globl  fib                             # -- Begin function fib\n        .p2align        2\n        .type   fib,@function\n        .set    nomicromips\n        .set    nomips16\n        .ent    fib\nfib:                                    # @fib\n        .frame  $fp,32,$ra\n        .mask   0xc0030000,-4\n        .fmask  0x00000000,0\n        .set    noreorder\n        .set    nomacro\n        .set    noat\n# %bb.0:\n        addiu   $sp, $sp, -32\n        sw      $ra, 28($sp)                    # 4-byte Folded Spill\n        sw      $fp, 24($sp)                    # 4-byte Folded Spill\n        sw      $17, 20($sp)                    # 4-byte Folded Spill\n        sw      $16, 16($sp)                    # 4-byte Folded Spill\n        move    $fp, $sp\n        move    $16, $4\n        sltiu   $1, $16, 2\n        bnez    $1, $BB0_2\n        addiu   $17, $zero, 0\n$BB0_1:                                 # =&gt;This Inner Loop Header: Depth=1\n        jal     fib\n        addiu   $4, $16, -1\n        addiu   $16, $16, -2\n        sltiu   $1, $16, 2\n        beqz    $1, $BB0_1\n        addu    $17, $2, $17\n$BB0_2:\n        addu    $2, $16, $17\n        move    $sp, $fp\n        lw      $16, 16($sp)                    # 4-byte Folded Reload\n        lw      $17, 20($sp)                    # 4-byte Folded Reload\n        lw      $fp, 24($sp)                    # 4-byte Folded Reload\n        lw      $ra, 28($sp)                    # 4-byte Folded Reload\n        jr      $ra\n        addiu   $sp, $sp, 32\n        .set    at\n        .set    macro\n        .set    reorder\n        .end    fib\n$func_end0:\n        .size   fib, ($func_end0)-fib\n                                        # -- End function\n        .ident  \"clang version 11.0.0 (https:\/\/github.com\/msys2\/MSYS2-packages ca391a3660d17cdee1e94d5afd2e72a4f750cddb)\"\n        .section        \".note.GNU-stack\",\"\",@progbits\n        .addrsig\n        .text<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><code>$BB0_1<\/code> \u3092\u307f\u308b\u3068\u3001<code>jal fib<\/code> \u306f\uff11\u884c\u3057\u304b\u3067\u3066\u304a\u3089\u305a\u3001 <code>beqz $1, $BB0_1<\/code> \u306b\u3088\u308a <code>$BB0_1<\/code> \u304c\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3055\u308c\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002<ul><li>\u3053\u306e\u518d\u5e30\u547c\u3073\u51fa\u3057\u306b\u7740\u76ee\u3059\u308b\u3068\u3001\uff11\u56de\u76ee\u306f <code>fib(n-1)<\/code> \u3092\u547c\u3073\u51fa\u3059\u304c\u3001\uff12\u56de\u76ee\u306f <code>fib(n-3)<\/code> \u3092\u547c\u3093\u3067\u304a\u308a\u3001\u5143\u306e\u5b9f\u88c5\u3068\u7570\u306a\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002<\/li><li>\u3053\u308c\u306f\u3001<code>fib(n) = fib(n-1) + fib(n-2)<\/code> \u3068\u8a08\u7b97\u3059\u308b\u304b\u3001<code>fib(n) = fib(n-1) + fib(n-3) + ...<\/code> \u3068\u8a08\u7b97\u3059\u308b\u304b\u306e\u9055\u3044\u3002\u3064\u307e\u308a\u3001\u95a2\u6570\u5185\u90e8\u306e\u69cb\u9020\u3092\u89e3\u91c8\u3057\u3066\u7b49\u4fa1\u306a\u7d50\u679c\u3092\u51fa\u529b\u3059\u308b\u95a2\u6570\u306b\u66f8\u304d\u63db\u3048\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002<\/li><li>\u3055\u3089\u306b\u3001<code>fib(0)<\/code> \u3092\u518d\u5e30\u7684\u306b\u547c\u3073\u51fa\u3059\u51e6\u7406\u304c\u6392\u9664\u3055\u308c\u3066\u3044\u308b\u3002<code>fib(0) = 0<\/code> \u306a\u306e\u3067\u3053\u308c\u3092\u547c\u3073\u51fa\u3059\u5fc5\u8981\u306f\u306a\u3044\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u95a2\u6570\u547c\u3073\u51fa\u3057\u56de\u6570<\/h3>\n\n\n\n<p>\u3053\u306e\u6700\u9069\u5316\u306b\u3088\u308a\u547c\u3073\u51fa\u3057\u56de\u6570\u304c\u3069\u3046\u306a\u308b\u304b\u3092\u8003\u3048\u3066\u307f\u3088\u3046\u3002<code>fib(n-2)<\/code> \u306e\u547c\u3073\u51fa\u3057\u304c\u306a\u304f\u306a\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u547c\u3073\u51fa\u3057\u56de\u6570\u306f <code>fib(k)<\/code> \u306e <code>k=0,...,n-1<\/code> \u307e\u3067\u306e\u548c + 1 \u306b\u6291\u3048\u3089\u308c\u308b\u3002\uff08<code>fib(0)<\/code> \u306e\u518d\u5e30\u547c\u3073\u51fa\u3057\u304c\u306a\u304f\u306a\u3063\u305f\u52b9\u679c\u306f <code>fib(2)<\/code> \u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u3068\u4e00\u81f4\u3059\u308b\u3002\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ gdb a.exe\nGNU gdb (GDB) 11.1\nCopyright (C) 2021 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\nType \"show copying\" and \"show warranty\" for details.\nThis GDB was configured as \"x86_64-pc-msys\".\nType \"show configuration\" for configuration details.\nFor bug reporting instructions, please see:\n&lt;https:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;.\nFind the GDB manual and other documentation resources online at:\n    &lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;.\n\nFor help, type \"help\".\nType \"apropos word\" to search for commands related to \"word\"...\nTraceback (most recent call last):\n  File \"&lt;string&gt;\", line 3, in &lt;module&gt;\nModuleNotFoundError: No module named 'libstdcxx'\n\/etc\/gdbinit:6: Error in sourced command file:\nError while executing Python code.\nReading symbols from a.exe...\n(gdb) b fib\nBreakpoint 1 at 0x10040108c\n(gdb) ignore 1 100000\nWill ignore next 100000 crossings of breakpoint 1.\n(gdb) r\nStarting program: \/path\/to\/a.exe\n[New Thread 16864.0x2e10]\n[New Thread 16864.0x62f8]\n[New Thread 16864.0x49b8]\nc: 55\n[Thread 16864.0x5bf4 exited with code 0]\n[Thread 16864.0x49b8 exited with code 0]\n[Thread 16864.0x62f8 exited with code 0]\n[Inferior 1 (process 16864) exited normally]\n(gdb) info breakpoints\nNum     Type           Disp Enb Address            What\n1       breakpoint     keep y   0x000000010040108c &lt;fib+12&gt;\n        breakpoint already hit 89 times\n        ignore next 99911 hits\n(gdb) q<\/pre>\n\n\n\n<p>\u5143\u306e\u7d04\u534a\u5206\u306e89\u56de\u306e fib() \u547c\u3073\u51fa\u3057\u3067\u6e08\u3093\u3067\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5225\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\uff08GCC\uff09\u3060\u3068\u3069\u3046\u306a\u308b\u304b\uff1f<\/h2>\n\n\n\n<p>GCC \u306e\u5834\u5408\u306f MIPS \u306e\u30af\u30ed\u30b9\u30b3\u30f3\u30d1\u30a4\u30eb\u74b0\u5883\u3092\u7528\u610f\u3059\u308b\u306e\u304c\u3084\u3084\u9762\u5012\u306a\u306e\u3067\u5272\u611b\u3059\u308b\u304c\u3001\u306a\u3093\u3068 <code>fib(10)<\/code> \u3092\u8a08\u7b97\u3059\u308b\u305f\u3081\u306e <code>fib()<\/code> \u306e\u547c\u3073\u51fa\u3057\u56de\u6570\u306f\uff12\u56de\u306b\u306a\u308b\u3002\u3053\u308c\u306f tail call optimization \u3068\u3044\u3046\u30b3\u30f3\u30d1\u30a4\u30e9\u6280\u8853\u3092\u4f7f\u3063\u3066\u3001\u95a2\u6570\u547c\u3073\u51fa\u3057\u3092\u95a2\u6570\u5185\u306e\u53cd\u5fa9\u51e6\u7406\u306b\u7f6e\u304d\u63db\u3048\u308b\u6700\u9069\u5316\u304c\u884c\u308f\u308c\u308b\u304b\u3089\u3067\u3042\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ gcc -O2 fib.c main.c\n$ gdb a.exe\nGNU gdb (GDB) 11.1\nCopyright (C) 2021 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\nType \"show copying\" and \"show warranty\" for details.\nThis GDB was configured as \"x86_64-pc-msys\".\nType \"show configuration\" for configuration details.\nFor bug reporting instructions, please see:\n&lt;https:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;.\nFind the GDB manual and other documentation resources online at:\n    &lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;.\n\nFor help, type \"help\".\nType \"apropos word\" to search for commands related to \"word\"...\nTraceback (most recent call last):\n  File \"&lt;string&gt;\", line 3, in &lt;module&gt;\nModuleNotFoundError: No module named 'libstdcxx'\n\/etc\/gdbinit:6: Error in sourced command file:\nError while executing Python code.\nReading symbols from a.exe...\n(gdb) b fib\nBreakpoint 1 at 0x100401090\n(gdb) ignore 1 100000\nWill ignore next 100000 crossings of breakpoint 1.\n(gdb) r\nStarting program: \/path\/to\/a.exe\n[New Thread 15940.0x7004]\n[New Thread 15940.0x74f8]\n[New Thread 15940.0x6bdc]\nc: 55\n[Thread 15940.0x6510 exited with code 0]\n[Thread 15940.0x6bdc exited with code 0]\n[Thread 15940.0x74f8 exited with code 0]\n[Inferior 1 (process 15940) exited normally]\n(gdb) info breakpoints\nNum     Type           Disp Enb Address            What\n1       breakpoint     keep y   0x0000000100401090 &lt;fib+16&gt;\n        breakpoint already hit 2 times\n        ignore next 99998 hits\n(gdb) q<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001\u540c\u3058\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3082\u6700\u9069\u5316\u306e\u6709\u7121\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306e\u9055\u3044\u306a\u3069\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u5909\u308f\u308a\u3001\u4f7f\u7528\u3059\u308b\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u30ea\u30bd\u30fc\u30b9\uff08\u30b9\u30bf\u30c3\u30af\u9818\u57df\u306a\u3069\uff09\u304c\u5927\u304d\u304f\u5909\u308f\u308b\u3002<\/p>\n\n\n\n<p>\u666e\u6bb5\u610f\u8b58\u305b\u305a\u4f7f\u3063\u3066\u3044\u308b\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306e\u767a\u5c55\u306f\u3053\u306e\u3088\u3046\u306a\u6280\u8853\u306e\u7a4d\u307f\u91cd\u306d\u306b\u3088\u3063\u3066\u652f\u3048\u3089\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308b\u3068\u601d\u3046\u3002\u8208\u5473\u3092\u6301\u3063\u305f\u4eba\u306f\u305c\u3072\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fb\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u306a\u3069\u306b\u89aa\u3057\u3093\u3067\u307b\u3057\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fb\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u8b1b\u7fa9\u3067 MIPS \u547d\u4ee4\u30bb\u30c3\u30c8\u3092\u4f7f\u3063\u3066\u8aac\u660e\u3092\u3057\u3066\u3044\u308b\u304c\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u304c\u751f\u6210\u3059\u308b\u30a2\u30bb\u30f3\u30d6\u30ea\u304c\u6c17\u306b\u306a\u308b\u4eba\u3082\u3044\u308b\u3068\u601d\u3046\u306e\u3067\u3053\u306e\u8a18\u4e8b\u3067\u6271\u3063\u3066\u304a\u304f\u3002 \u6700\u8fd1\u306e\u74b0\u5883\u3060\u3068 clang \u3068\u3044\u3046\u30b3\u30f3\u30d1\u30a4\u30e9\u3092\u4f7f\u3046\u306e\u304c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-847","post","type-post","status-publish","format-standard","hentry","category-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=847"}],"version-history":[{"count":0,"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/847\/revisions"}],"wp:attachment":[{"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www8281uo.sakura.ne.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}