mirror of
https://github.com/SSCLI/sscli_20021101
synced 2026-06-08 12:28:57 +00:00
9fa3874800
Moved the original file to the archive subfolder.
260 lines
7.3 KiB
HTML
260 lines
7.3 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<title>MyC Compiler Sample</title>
|
|
<link rel="stylesheet" type="text/css" href="../../../docs/rotor.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
<h1>MyC Compiler Sample</h1>
|
|
|
|
|
|
<h2>Sample Overview</h2>
|
|
|
|
|
|
<p>The goal of the MyC compiler is to show the implementation of various
|
|
features of the CLI. </p>
|
|
|
|
|
|
<p>The following table shows the command-line options for this sample.</p>
|
|
|
|
<table border="1" width="90%">
|
|
<tr>
|
|
<th>Option</th>
|
|
<th>Description</th>
|
|
<tr>
|
|
<td><b>/debug </b>
|
|
<td>Generates debug information.<tr>
|
|
<td><b>/nodebug </b>
|
|
<td>Prevents generation of debug information.<tr>
|
|
<td><b>/list </b>
|
|
<td>Generates an assembly listing (.asm).<tr>
|
|
<td><b>/dll </b>
|
|
<td>Creates a DLL assembly.
|
|
<tr>
|
|
<td><b>/exe </b>
|
|
<td>Creates an executable assembly.
|
|
<tr>
|
|
<td><b>/outdir:path </b>
|
|
<td>Creates output files in the current directory.</tr>
|
|
</table>
|
|
|
|
|
|
<h3>Architecture Diagram</h3>
|
|
|
|
|
|
<p align="center"><img border="1" src="mycarch.gif" width="372" height="733"></p>
|
|
|
|
|
|
<p align="left"> </p>
|
|
|
|
|
|
<h3>MyC Language Specification</h3>
|
|
<h4>Goals</h4>
|
|
<p>The goal of the MYC compiler is to show common intermediate language (CIL)
|
|
instructions and the CLI in action. </p>
|
|
<h4>Overview </h4>
|
|
<p>The compiler itself is a simple recursive descent parser with a single pass
|
|
code generator. It generates an assembler source file which is then used as
|
|
input to the IL Assembler. </p>
|
|
<h4>Language Specification </h4>
|
|
<p>The language is a subset of the C language with
|
|
simplified declarations, both external and local. </p>
|
|
<h4>Data Types </h4>
|
|
<p>The only supported data types are <b>int</b> and <b>void</b>. Limiting
|
|
these choices allows a simpler sample compiler design. </p>
|
|
<h4>Data Declaration </h4>
|
|
<p>Variables can be declared static or local. Implicit
|
|
|
|
static declarations occur outside of function declarations. Local declarations
|
|
can occur only at the beginnings of functions prior to statements. </p>
|
|
<p>For example: </p>
|
|
<PRE>int func1()
|
|
{
|
|
int y;
|
|
}
|
|
int x;
|
|
|
|
int func2()
|
|
{
|
|
static int z;
|
|
};
|
|
</PRE>
|
|
<P>
|
|
In this example the variable <i>x</i> is a static declaration (by default) and the
|
|
variable <i>y</i> is a local variable in function <b>func1()</b>. The variable
|
|
|
|
<i>z</i> is actually a static, even though it is declared within a function.
|
|
<h4>Flow Control </h4>
|
|
<UL>
|
|
<LI><b>if-else</b> blocks
|
|
<LI><b>while</b> loops
|
|
<LI><b>for</b> loops </LI>
|
|
</UL>
|
|
<h4>Restrictions </h4>
|
|
<UL>
|
|
<LI>Integer data
|
|
<LI>Outer declarations only
|
|
<LI>Static methods and function calls only </LI>
|
|
</UL>
|
|
<h3>Backus-Naur Definition</h3>
|
|
|
|
|
|
<p>letter ::= "A-Za-z";<br>
|
|
digit ::= "0-9";<br>
|
|
<br>
|
|
name ::= letter { letter | digit };<br>
|
|
integer ::= digit { digit };<br>
|
|
<br>
|
|
ident ::= name | function_call;<br>
|
|
function_call ::= name "(" [expr {, expr}] ")";<br>
|
|
<br>
|
|
factor ::= (ident | integer | "(" expr ")" );<br>
|
|
unary_factor ::= ["+"|"-"] factor;<br>
|
|
<br>
|
|
term1 ::= ["*"|"/"] factor;<br>
|
|
term0 ::= factor { term1 };<br>
|
|
first_term ::= unary_factor term1;<br>
|
|
<br>
|
|
math_expr ::= first_term { ["+"|"-"] term0 }<br>
|
|
rel_expr ::= math_expr ("=="|"!="|"<"|">"|">="|"<=") math_expr;<br>
|
|
not_factor ::= ["!"] rel_expr;<br>
|
|
term_bool ::= not_factor { ("&" | "&&") not_factor };<br>
|
|
bool_expr ::= term_bool { ("|" | "^") term_bool };<br>
|
|
expr ::= bool_expr;<br>
|
|
<br>
|
|
assign = ident "=" expr;<br>
|
|
assign_stmt ::= assign ";" ;<br>
|
|
if_stmt ::= "if" "(" expr ")" stmt_block [ "else" inner_block ];<br>
|
|
while_stmt ::= "while" "(" expr ")" inner_block;<br>
|
|
for_stmt ::= "for" "(" assign ";" expr ";" assign ")" inner_block<br>
|
|
break_stmt ::= "break" ";";<br>
|
|
cont_stmt ::= "continue" ";";<br>
|
|
ret_stmt ::= "return" expr ";";<br>
|
|
stmt ::= (<br>
|
|
if_stmt<br>
|
|
| while_stmt<br>
|
|
| for_stmt<br>
|
|
| break_stmt<br>
|
|
| cont_stmt<br>
|
|
| ret_stmt<br>
|
|
| assign_stmt<br>
|
|
);<br>
|
|
inner_block ::= "{" { stmt } "}";<br>
|
|
outer_block ::= "{" { inner_decl } { stmt } "}";<br>
|
|
<br>
|
|
inner_decl ::= [ class ] type ident { "," ident } ";";<br>
|
|
<br>
|
|
class ::= "extern" | "static" | "auto";<br>
|
|
type ::= "int" | "void";<br>
|
|
<br>
|
|
params ::= type ident { , type ident };<br>
|
|
outer_decl ::= [ class ] type ident { "," ident } ";";<br>
|
|
func_decl ::= [ class ] type ident "(" params ")" outer_block;<h2>Sample Source and Build Output Locations</h2>
|
|
|
|
|
|
<p>The sample source is found in sscli\samples\compilers\myc\src directory. The source
|
|
files are:</p>
|
|
|
|
|
|
<ul class="none">
|
|
<li><a href="src/asm.cs">asm.cs</a></li>
|
|
<li><a href="src/emit.cs">emit.cs</a></li>
|
|
<li><a href="src/exe.cs">exe.cs</a></li>
|
|
<li><a href="src/iasm.cs">iasm.cs</a></li>
|
|
<li><a href="src/io.cs">io.cs</a></li>
|
|
<li><a href="src/myc.cs">myc.cs</a></li>
|
|
<li><a href="src/parse.cs">parse.cs</a></li>
|
|
<li><a href="src/tok.cs">tok.cs</a></li>
|
|
<li><a href="src/var.cs">var.cs</a></li>
|
|
<li><a href="src/varlist.cs">varlist.cs</a></li>
|
|
</ul>
|
|
|
|
|
|
<p>The build output location is %TARGETCOMPLUS%\samples\compilers\myc.
|
|
The output file is an executable assembly named myc.exe.</p>
|
|
|
|
|
|
<h2>Building the Sample</h2>
|
|
|
|
|
|
<p>All samples are built from the buildall script. </p>
|
|
|
|
|
|
<p>You can also build all the
|
|
samples by switching to the root of the sample directory, sscli\samples, and typing
|
|
<code>build -c</code>.</p>
|
|
|
|
|
|
<p>You can build this specific sample by switching to the sample directory and typing
|
|
<code>build -c</code>.</p>
|
|
|
|
|
|
<h2>Running the Sample</h2>
|
|
|
|
|
|
<p>Because the MyC language does not support referencing external assemblies,
|
|
use the Runtime Debugger to step through test applications built using MyC.</p>
|
|
|
|
|
|
<ol>
|
|
<li>Run env.bat or source the env.csh or env.sh script files depending on
|
|
your platform.</li>
|
|
<li>Build the SSCLI using the buildall script or batch file.</li>
|
|
<li>Switch to the %TARGETCOMPLUS%\samples\compilers\myc directory.</li>
|
|
<li>Type the following command:<blockquote>
|
|
|
|
|
|
<p>clix myc /debug <i>inputfile</i></p>
|
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
<p>where <i>inputfile</i> is the name of a MyC source file. For example:</p>
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
|
<p>clix myc /debug tflow.myc</p>
|
|
|
|
|
|
</blockquote>
|
|
</li>
|
|
<li>Debug the resulting assembly:</li>
|
|
</ol>
|
|
<blockquote>
|
|
<blockquote>
|
|
|
|
|
|
<p>cordbg tflow.exe</p>
|
|
|
|
|
|
</blockquote>
|
|
</blockquote>
|
|
<h2>
|
|
Known Issues </h2>
|
|
|
|
|
|
<p>The debug line number count is off by 1.</p>
|
|
|
|
|
|
<h2>References</h2>
|
|
<ul>
|
|
<li><i>Modern Compiler Implementation in C</i>, by Andrew W Appel.</li>
|
|
<li><i>Algorithms in C++</i> by Robert Sedgewick.</li>
|
|
<li><i>Advanced Compiler Design and Implementations</i>, by Steven S Muchnick.</li>
|
|
<li><i>Compiler Construction</i>, by Niklaus Wirth.</li>
|
|
<li><i>Principles of Compiler Design</i>, by Alfred V. Aho and and Jeffrey D. Ullman.</li>
|
|
</ul>
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
<p><i>Copyright (c) 2002 Microsoft Corporation. All rights reserved.</i></p>
|
|
</body>
|
|
</html> |