[转载]Using SPIM with Modern Compiler Implementation
原始连接:<a href="http://www.cs.princeton.edu/~appel/modern/spim/">[url]http://www.cs.princeton.edu/~appel/modern/spim/[/url]</a><br /><br /><a href="http://www.cs.wisc.edu/~larus/spim.html">SPIM</a> is a simulator for the <a href="http://www.sgi.com/MIPS/">MIPS</a> instruction set. MIPS is a simple, clean, and efficient RISC computer architecture; <a href="www.sgi.com">Silicon Graphics</a> workstations and <a href="http://www.nintendo.com/n64">Nintendo 64</a> use MIPS processors. <h2>Why use SPIM?</h2>A valuable part of the learning experience for students of compiler design is the implementation of a real compiler and code generator. I recommend that students build a code generator for a RISC architecture (such as MIPS, Sparc, PowerPC, or Alpha) because <ul><li>RISC code generators are much simpler than code generators for machines such as the Pentium, and </li><li>using a RISC instruction set gives the student better insight into current issues in computer architecture. </li></ul><p>But many people do not have access to a RISC-based workstation. The SPIM simulator allows MIPS assembly-language to be assembled and run on a Pentium PC running Microsoft Windows (or on a Unix workstation of almost any type). </p><h3>But on the other hand...</h3>There is a great satisfaction to be had in writing a compiler that generates real native code that runs on the bare machine. If you have a Pentium and you want this satisfaction, <strong>Section 9.2</strong> of <i>Modern Compiler Implementation</i> shows how to handle Complex Instruction Set Computers (CISC). <h2>Where is SPIM?</h2><a href="http://www.cs.wisc.edu/~larus/spim.html">SPIM is available</a> from the University of Wisconsin; it can be freely used and distributed for non-commercial purposes. <p>The documentation for SPIM also includes a detailed summary of the MIPS instruction set and assembly language. </p><h2>How can I use SPIM in my Tiger compiler?</h2>I have found that MIPS assembly language generated by my Tiger compiler runs under SPIM with very few modifications. <p /><ol><li>A normal Tiger compiler takes a file <tt><font face="新宋体">myprog.tig</font></tt> and produces a file <tt><font face="新宋体">myprog.s</font></tt>, which is then assembled and linked with a <tt><font face="新宋体">runtime.o</font></tt> object file derived from a <a href="runtime.c"><tt><font face="新宋体">runtime.c</font></tt></a>. <p>SPIM has no link-loader, and its system calls are incompatible with those assumed by <tt><font face="新宋体">runtime.c</font></tt>. A new version of the runtime is necessary: the file <a href="runtime.s"><tt><font face="新宋体">runtime.s</font></tt></a>. <strong>Warning: This file is not fully tested! Only <tt><font face="新宋体">main</font></tt>, <tt><font face="新宋体">initArray</font></tt>, and <tt><font face="新宋体">print</font></tt> are tested.</strong> </p><p>Instead of link-loading, it is necessary to concatenate the Tiger-compiler output file <tt><font face="新宋体">myprog.s</font></tt> with <tt><font face="新宋体">runtime.s</font></tt> (either one can go first) and load the combined file into the SPIM simulator. </p></li><li>Unlike a real MIPS assembler, SPIM has no support for string literals in the text segment, so it is necessary to use a <tt><font face="新宋体">.data</font></tt> directive before emitting string literals, and a <tt><font face="新宋体">.text</font></tt> directive afterwards. </li><li>In procedure-entry prologues, my Tiger compiler for MIPS generated the lines <pre>L12_framesize=60addiu $sp,$sp,-L12_framesize
</pre>and then within the function <tt><font face="新宋体">L12</font></tt> it generates offsets such as <pre>sw $25,-24+L12_framesize($sp)
</pre>The SPIM assembler seems unable to handle <i>negated</i> labels in expressions, so I had to modify the prologue to read as, <pre>L12_framesize=60
addiu $sp,$sp,-60
</pre>However, the offset expressions such as <tt><font face="新宋体">-24+L12_framesize($sp)</font></tt> work just fine. </li><li>The <tt><font face="新宋体">not</font></tt> function in <tt><font face="新宋体">runtime.s</font></tt> has been renamed to <tt><font face="新宋体">_not</font></tt> because opcode-names in SPIM are not legal labels. </li></ol>Miraculously, no other changes were necessary to my Tiger compiler to make the output runnable under PC-SPIM. <p><i>Andrew Appel</i> </p>
页:
[1]