278 lines
24 KiB
HTML
Executable File
278 lines
24 KiB
HTML
Executable File
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="generator" content="TempleOS V5.03">
|
|
<meta name="viewport" content="width=device-width">
|
|
<link rel="stylesheet" href="/style/templeos.css">
|
|
<script src="/script/templeos.js"></script>
|
|
<style type="text/css">
|
|
.cF0{color:#000000;background-color:#ffffff;}
|
|
.cF1{color:#0000aa;background-color:#ffffff;}
|
|
.cF2{color:#00aa00;background-color:#ffffff;}
|
|
.cF3{color:#00aaaa;background-color:#ffffff;}
|
|
.cF4{color:#aa0000;background-color:#ffffff;}
|
|
.cF5{color:#aa00aa;background-color:#ffffff;}
|
|
.cF6{color:#aa5500;background-color:#ffffff;}
|
|
.cF7{color:#aaaaaa;background-color:#ffffff;}
|
|
.cF8{color:#555555;background-color:#ffffff;}
|
|
.cF9{color:#5555ff;background-color:#ffffff;}
|
|
.cFA{color:#55ff55;background-color:#ffffff;}
|
|
.cFB{color:#55ffff;background-color:#ffffff;}
|
|
.cFC{color:#ff5555;background-color:#ffffff;}
|
|
.cFD{color:#ff55ff;background-color:#ffffff;}
|
|
.cFE{color:#ffff55;background-color:#ffffff;}
|
|
.cFF{color:#ffffff;background-color:#ffffff;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<pre id="content">
|
|
<a name="l1"></a><span class=cF2>/*</span><span class=cF5>
|
|
<a name="l2"></a><u>-] Main Compiler</span><span class=cF0>
|
|
<a name="l3"></a></u> </span><span class=cF2>The mini compiler is like the main compiler, except the main compiler's </span><span class=cF0>
|
|
<a name="l4"></a> </span><span class=cF2>lexical analyser removes comments and does preprocessing. </span><a href="/Wb/Compiler/Lex.HC.HTML#l441"><span class=cF4>Lex</span></a><span class=cF2>(),</span><a href="/Wb/Compiler/CMisc.HC.HTML#l63"><span class=cF4>Echo</span></a><span class=cF2>(ON).</span><span class=cF0>
|
|
<a name="l5"></a>
|
|
<a name="l6"></a> </span><span class=cF2>The main compiler generates </span><a href="/Wb/Compiler/CompilerA.HH.HTML#l20"><span class=cF4>Intermediate Code</span></a><span class=cF2> at the parser stage. See </span><span class=cF4>
|
|
<a name="l7"></a> </span><a href="/Wb/Compiler/PrsExp.HC.HTML#l264"><span class=cF4>PrsExpression</span></a><span class=cF2>(), </span><a href="/Wb/Compiler/PrsStmt.HC.HTML#l904"><span class=cF4>PrsStmt</span></a><span class=cF2>().</span><span class=cF0>
|
|
<a name="l8"></a>
|
|
<a name="l9"></a> </span><span class=cF2>The main compiler optimizes See </span><a href="/Wb/Compiler/CInit.HC.HTML#l16"><span class=cF4>Intermediate Code Attributes</span></a><span class=cF2>, </span><span class=cF4>
|
|
<a name="l10"></a> </span><a href="/Wb/Compiler/OptPass012.HC.HTML#l308"><span class=cF4>Combining Consts</span></a><span class=cF2>, </span><a href="/Wb/Compiler/OptPass3.HC.HTML#l557"><span class=cF4>Choosing Reg Vars</span></a><span class=cF2>. Use </span><a href="/Wb/Compiler/CMisc.HC.HTML#l50"><span class=cF4>PassTrace</span></a><span class=cF2>() to see the optimization </span><span class=cF0>
|
|
<a name="l11"></a> </span><span class=cF2>stages.</span><span class=cF0>
|
|
<a name="l12"></a>
|
|
<a name="l13"></a> </span><span class=cF2>The main compiler makes machine code in the back end. See </span><a href="/Wb/Kernel/KernelA.HH.HTML#l1666"><span class=cF4>IC Struct</span></a><span class=cF2>, </span><span class=cF4>
|
|
<a name="l14"></a> </span><a href="/Wb/Compiler/PrsLib.HC.HTML#l249"><span class=cF4>COCCompile</span></a><span class=cF2> and </span><a href="/Wb/Compiler/OptPass789A.HC.HTML#l518"><span class=cF4>OptPass789A</span></a><span class=cF2>(), </span><a href="/Wb/Compiler/BackA.HC.HTML#l224"><span class=cF4>BackEnd</span></a><span class=cF2>. Set </span><a href="/Wb/Compiler/CMisc.HC.HTML#l58"><span class=cF4>Trace</span></a><span class=cF2>(ON) to see the output of the </span><span class=cF0>
|
|
<a name="l15"></a> </span><span class=cF2>backend.</span><span class=cF0>
|
|
<a name="l16"></a></span><span class=cF5>
|
|
<a name="l17"></a><u>-] Mini Compiler</span><span class=cF0>
|
|
<a name="l18"></a></u> </span><span class=cF2>For this mini compiler, some things you should know about 64-bit asm:</span><span class=cF0>
|
|
<a name="l19"></a>
|
|
<a name="l20"></a> </span><span class=cF2>* Putting a 0x48, known as the REX byte, in front of an inst makes it 64-bit </span><span class=cF0>
|
|
<a name="l21"></a> </span><span class=cF2>size.</span><span class=cF0>
|
|
<a name="l22"></a>
|
|
<a name="l23"></a> </span><span class=cF2>* "PUSH EAX", "POP EAX" and "XOR EAX,EAX" will behave as 64-bit even without </span><span class=cF0>
|
|
<a name="l24"></a> </span><span class=cF2>REX because the stk is always 64 bit and because the XOR clears the upper </span><span class=cF0>
|
|
<a name="l25"></a> </span><span class=cF2>32-bits.</span><span class=cF0>
|
|
<a name="l26"></a>
|
|
<a name="l27"></a> </span><span class=cF2>It is okay in TempleOS to change RAX, RBX, RCX, RDX, R8 and R9 without </span><span class=cF0>
|
|
<a name="l28"></a> </span><span class=cF2>restoring them to their original values.</span><span class=cF0>
|
|
<a name="l29"></a></span><span class=cF2>*/</span><span class=cF0>
|
|
<a name="l30"></a>
|
|
<a name="l31"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>TK_EOF</span><span class=cF0> 0
|
|
<a name="l32"></a>#</span><span class=cF1>define</span><span class=cF0> TK_NUM 1
|
|
<a name="l33"></a>#</span><span class=cF1>define</span><span class=cF0> TK_OP 2
|
|
<a name="l34"></a>#</span><span class=cF1>define</span><span class=cF0> TK_LEFT 3
|
|
<a name="l35"></a>#</span><span class=cF1>define</span><span class=cF0> TK_RIGHT 4
|
|
<a name="l36"></a>
|
|
<a name="l37"></a>#</span><span class=cF1>define</span><span class=cF0> OP_MUL 1
|
|
<a name="l38"></a>#</span><span class=cF1>define</span><span class=cF0> OP_DIV 2
|
|
<a name="l39"></a>#</span><span class=cF1>define</span><span class=cF0> OP_ADD 3
|
|
<a name="l40"></a>#</span><span class=cF1>define</span><span class=cF0> OP_SUB 4
|
|
<a name="l41"></a>
|
|
<a name="l42"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>Lex</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> **_src,</span><span class=cF9>I64</span><span class=cF0> *num)
|
|
<a name="l43"></a>{</span><span class=cF2>//See </span><a href="/Wb/Compiler/Lex.HC.HTML#l441"><span class=cF4>Lex</span></a><span class=cF2>().</span><span class=cF0>
|
|
<a name="l44"></a> </span><span class=cF1>U8</span><span class=cF0> *src=*_src;
|
|
<a name="l45"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l46"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l47"></a> </span><span class=cF1>switch</span><span class=cF0> (*src) {
|
|
<a name="l48"></a> </span><span class=cF1>case</span><span class=cF0> 0:
|
|
<a name="l49"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>';'</span><span class=cF0>:
|
|
<a name="l50"></a> *_src=src;
|
|
<a name="l51"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TK_EOF</span><span class=cF0>;
|
|
<a name="l52"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>CH_SPACE</span><span class=cF0>:
|
|
<a name="l53"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'\r'</span><span class=cF0>:
|
|
<a name="l54"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'\n'</span><span class=cF0>:
|
|
<a name="l55"></a> src++;
|
|
<a name="l56"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l57"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'0'</span><span class=cF0>...</span><span class=cF6>'9'</span><span class=cF0>:
|
|
<a name="l58"></a> i=0;
|
|
<a name="l59"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l60"></a> i=i*10+*src-</span><span class=cF6>'0'</span><span class=cF0>;
|
|
<a name="l61"></a> src++;
|
|
<a name="l62"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF6>'0'</span><span class=cF0><=*src<=</span><span class=cF6>'9'</span><span class=cF0>);
|
|
<a name="l63"></a> *num=i;
|
|
<a name="l64"></a> *_src=src;
|
|
<a name="l65"></a> </span><span class=cF1>return</span><span class=cF0> TK_NUM;
|
|
<a name="l66"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'*'</span><span class=cF0>:
|
|
<a name="l67"></a> *num=OP_MUL;
|
|
<a name="l68"></a> *_src=src+1;
|
|
<a name="l69"></a> </span><span class=cF1>return</span><span class=cF0> TK_OP;
|
|
<a name="l70"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'/'</span><span class=cF0>:
|
|
<a name="l71"></a> *num=OP_DIV;
|
|
<a name="l72"></a> *_src=src+1;
|
|
<a name="l73"></a> </span><span class=cF1>return</span><span class=cF0> TK_OP;
|
|
<a name="l74"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'+'</span><span class=cF0>:
|
|
<a name="l75"></a> *num=OP_ADD;
|
|
<a name="l76"></a> *_src=src+1;
|
|
<a name="l77"></a> </span><span class=cF1>return</span><span class=cF0> TK_OP;
|
|
<a name="l78"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'-'</span><span class=cF0>:
|
|
<a name="l79"></a> *num=OP_SUB;
|
|
<a name="l80"></a> *_src=src+1;
|
|
<a name="l81"></a> </span><span class=cF1>return</span><span class=cF0> TK_OP;
|
|
<a name="l82"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'('</span><span class=cF0>:
|
|
<a name="l83"></a> *_src=src+1;
|
|
<a name="l84"></a> </span><span class=cF1>return</span><span class=cF0> TK_LEFT;
|
|
<a name="l85"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>')'</span><span class=cF0>:
|
|
<a name="l86"></a> *_src=src+1;
|
|
<a name="l87"></a> </span><span class=cF1>return</span><span class=cF0> TK_RIGHT;
|
|
<a name="l88"></a> </span><span class=cF1>default</span><span class=cF0>:
|
|
<a name="l89"></a> </span><span class=cF5>throw</span><span class=cF0>;
|
|
<a name="l90"></a> }
|
|
<a name="l91"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l92"></a>}
|
|
<a name="l93"></a>
|
|
<a name="l94"></a>#</span><span class=cF1>define</span><span class=cF0> PREC_EOF 0
|
|
<a name="l95"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>PREC_TERM</span><span class=cF0> 1
|
|
<a name="l96"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>PREC_MUL</span><span class=cF0> 2
|
|
<a name="l97"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>PREC_ADD</span><span class=cF0> 3
|
|
<a name="l98"></a>#</span><span class=cF1>define</span><span class=cF0> PREC_PAREN 4
|
|
<a name="l99"></a>
|
|
<a name="l100"></a></span><span class=cF1>extern</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> Parse(</span><span class=cF1>U8</span><span class=cF0> **_src,</span><span class=cF1>U8</span><span class=cF0> **_dst);
|
|
<a name="l101"></a>
|
|
<a name="l102"></a></span><span class=cF1>U0</span><span class=cF0> PrsTerm(</span><span class=cF1>U8</span><span class=cF0> **_src,</span><span class=cF1>U8</span><span class=cF0> **_dst,</span><span class=cF9>I64</span><span class=cF0> prec)
|
|
<a name="l103"></a>{</span><span class=cF2>//See </span><a href="/Wb/Compiler/PrsExp.HC.HTML#l264"><span class=cF4>PrsExpression</span></a><span class=cF2>().</span><span class=cF0>
|
|
<a name="l104"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l105"></a> </span><span class=cF1>U8</span><span class=cF0> *src2;
|
|
<a name="l106"></a> </span><span class=cF1>U8</span><span class=cF0> *dst2;
|
|
<a name="l107"></a> </span><span class=cF1>if</span><span class=cF0> (Parse</span><span class=cF7>(</span><span class=cF0>_src,_dst</span><span class=cF7>)</span><span class=cF0>==</span><span class=cF3>PREC_TERM</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l108"></a> src2=*_src;
|
|
<a name="l109"></a> dst2=*_dst;
|
|
<a name="l110"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) {
|
|
<a name="l111"></a></span><span class=cF2>//This is inefficient. The main compiler doesn't back-up like this.</span><span class=cF0>
|
|
<a name="l112"></a> i=Parse(&src2,&dst2);
|
|
<a name="l113"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF3>PREC_MUL</span><span class=cF0><=i<prec) </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l114"></a> *_src=src2;
|
|
<a name="l115"></a> *_dst=dst2;
|
|
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l117"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l118"></a> }
|
|
<a name="l119"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l120"></a> </span><span class=cF5>throw</span><span class=cF0>;
|
|
<a name="l121"></a>}
|
|
<a name="l122"></a>
|
|
<a name="l123"></a></span><span class=cF9>I64</span><span class=cF0> Parse(</span><span class=cF1>U8</span><span class=cF0> **_src,</span><span class=cF1>U8</span><span class=cF0> **_dst)
|
|
<a name="l124"></a>{</span><span class=cF2>//See </span><a href="/Wb/Compiler/PrsExp.HC.HTML#l264"><span class=cF4>PrsExpression</span></a><span class=cF2>().</span><span class=cF0>
|
|
<a name="l125"></a></span><span class=cF2>//See </span><a href="/Wb/Compiler/OpCodes.DD.HTML#l709"><span class=cF4>Opcode Fmts</span></a><span class=cF2> for details on asm insts.</span><span class=cF0>
|
|
<a name="l126"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l127"></a> </span><span class=cF1>U8</span><span class=cF0> *dst=*_dst;
|
|
<a name="l128"></a> </span><span class=cF1>switch</span><span class=cF0> (</span><span class=cF5>Lex</span><span class=cF7>(</span><span class=cF0>_src,&i</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l129"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>TK_EOF</span><span class=cF0>:
|
|
<a name="l130"></a> *dst++=0x58; </span><span class=cF2>//POP RAX</span><span class=cF0>
|
|
<a name="l131"></a> *dst++=0xC3; </span><span class=cF2>//RET</span><span class=cF0>
|
|
<a name="l132"></a> *_dst=dst;
|
|
<a name="l133"></a> </span><span class=cF1>return</span><span class=cF0> PREC_EOF;
|
|
<a name="l134"></a> </span><span class=cF1>case</span><span class=cF0> TK_NUM:
|
|
<a name="l135"></a> *dst++=0x48; </span><span class=cF2>//REX</span><span class=cF0>
|
|
<a name="l136"></a> *dst++=0xB8; </span><span class=cF2>//MOV RAX,immediate num</span><span class=cF0>
|
|
<a name="l137"></a> *dst(</span><span class=cF9>I64</span><span class=cF0> *)++=i;
|
|
<a name="l138"></a>
|
|
<a name="l139"></a> *dst++=0x50; </span><span class=cF2>//PUSH RAX</span><span class=cF0>
|
|
<a name="l140"></a> *_dst=dst;
|
|
<a name="l141"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_TERM</span><span class=cF0>;
|
|
<a name="l142"></a> </span><span class=cF1>case</span><span class=cF0> TK_LEFT:
|
|
<a name="l143"></a> PrsTerm(_src,_dst,PREC_PAREN);
|
|
<a name="l144"></a> </span><span class=cF1>if</span><span class=cF0> (Parse</span><span class=cF7>(</span><span class=cF0>_src,_dst</span><span class=cF7>)</span><span class=cF0>!=PREC_PAREN)
|
|
<a name="l145"></a> </span><span class=cF5>throw</span><span class=cF0>;
|
|
<a name="l146"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_TERM</span><span class=cF0>;
|
|
<a name="l147"></a> </span><span class=cF1>case</span><span class=cF0> TK_RIGHT:
|
|
<a name="l148"></a> </span><span class=cF1>return</span><span class=cF0> PREC_PAREN;
|
|
<a name="l149"></a> </span><span class=cF1>case</span><span class=cF0> TK_OP:
|
|
<a name="l150"></a> </span><span class=cF1>switch</span><span class=cF0> (i) {
|
|
<a name="l151"></a> </span><span class=cF1>case</span><span class=cF0> OP_MUL:
|
|
<a name="l152"></a> PrsTerm(_src,&dst,</span><span class=cF3>PREC_MUL</span><span class=cF0>);
|
|
<a name="l153"></a> *dst++=0x5A; </span><span class=cF2>//POP RDX</span><span class=cF0>
|
|
<a name="l154"></a> *dst++=0x58; </span><span class=cF2>//POP RAX</span><span class=cF0>
|
|
<a name="l155"></a>
|
|
<a name="l156"></a> *dst++=0x48; </span><span class=cF2>//REX</span><span class=cF0>
|
|
<a name="l157"></a> *dst++=0x0F;
|
|
<a name="l158"></a> *dst++=0xAF; </span><span class=cF2>//IMUL RAX,RDX</span><span class=cF0>
|
|
<a name="l159"></a> *dst++=0xC2;
|
|
<a name="l160"></a>
|
|
<a name="l161"></a> *dst++=0x50; </span><span class=cF2>//PUSH RAX</span><span class=cF0>
|
|
<a name="l162"></a> *_dst=dst;
|
|
<a name="l163"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_MUL</span><span class=cF0>;
|
|
<a name="l164"></a> </span><span class=cF1>case</span><span class=cF0> OP_DIV:
|
|
<a name="l165"></a> PrsTerm(_src,&dst,</span><span class=cF3>PREC_MUL</span><span class=cF0>);
|
|
<a name="l166"></a> *dst++=0x5B; </span><span class=cF2>//POP RBX</span><span class=cF0>
|
|
<a name="l167"></a> *dst++=0x58; </span><span class=cF2>//POP RAX</span><span class=cF0>
|
|
<a name="l168"></a>
|
|
<a name="l169"></a> *dst++=0x33; </span><span class=cF2>//XOR RDX,RDX</span><span class=cF0>
|
|
<a name="l170"></a> *dst++=0xD2;
|
|
<a name="l171"></a>
|
|
<a name="l172"></a> *dst++=0x48; </span><span class=cF2>//REX</span><span class=cF0>
|
|
<a name="l173"></a> *dst++=0xF7; </span><span class=cF2>//IDIV RBX</span><span class=cF0>
|
|
<a name="l174"></a> *dst++=0xFB;
|
|
<a name="l175"></a>
|
|
<a name="l176"></a> *dst++=0x50; </span><span class=cF2>//PUSH RAX</span><span class=cF0>
|
|
<a name="l177"></a> *_dst=dst;
|
|
<a name="l178"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_MUL</span><span class=cF0>;
|
|
<a name="l179"></a> </span><span class=cF1>case</span><span class=cF0> OP_ADD:
|
|
<a name="l180"></a> PrsTerm(_src,&dst,</span><span class=cF3>PREC_ADD</span><span class=cF0>);
|
|
<a name="l181"></a> *dst++=0x5A; </span><span class=cF2>//POP RDX</span><span class=cF0>
|
|
<a name="l182"></a> *dst++=0x58; </span><span class=cF2>//POP RAX</span><span class=cF0>
|
|
<a name="l183"></a>
|
|
<a name="l184"></a> *dst++=0x48; </span><span class=cF2>//REX</span><span class=cF0>
|
|
<a name="l185"></a> *dst++=0x03; </span><span class=cF2>//ADD RAX,RDX</span><span class=cF0>
|
|
<a name="l186"></a> *dst++=0xC2;
|
|
<a name="l187"></a>
|
|
<a name="l188"></a> *dst++=0x50; </span><span class=cF2>//PUSH RAX</span><span class=cF0>
|
|
<a name="l189"></a> *_dst=dst;
|
|
<a name="l190"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_ADD</span><span class=cF0>;
|
|
<a name="l191"></a> </span><span class=cF1>case</span><span class=cF0> OP_SUB:
|
|
<a name="l192"></a> PrsTerm(_src,&dst,</span><span class=cF3>PREC_ADD</span><span class=cF0>);
|
|
<a name="l193"></a> *dst++=0x5A; </span><span class=cF2>//POP RDX</span><span class=cF0>
|
|
<a name="l194"></a> *dst++=0x58; </span><span class=cF2>//POP RAX</span><span class=cF0>
|
|
<a name="l195"></a>
|
|
<a name="l196"></a> *dst++=0x48; </span><span class=cF2>//REX</span><span class=cF0>
|
|
<a name="l197"></a> *dst++=0x2B; </span><span class=cF2>//SUB RAX,RDX</span><span class=cF0>
|
|
<a name="l198"></a> *dst++=0xC2;
|
|
<a name="l199"></a>
|
|
<a name="l200"></a> *dst++=0x50; </span><span class=cF2>//PUSH RAX</span><span class=cF0>
|
|
<a name="l201"></a> *_dst=dst;
|
|
<a name="l202"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>PREC_ADD</span><span class=cF0>;
|
|
<a name="l203"></a> }
|
|
<a name="l204"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l205"></a>}
|
|
<a name="l206"></a>
|
|
<a name="l207"></a></span><span class=cF1>U0</span><span class=cF0> Main()
|
|
<a name="l208"></a>{
|
|
<a name="l209"></a> </span><span class=cF1>U8</span><span class=cF0> *src,*src2,*code,*dst;
|
|
<a name="l210"></a>
|
|
<a name="l211"></a> </span><span class=cF2>//Fixed size, no buffer overrun check.</span><span class=cF0>
|
|
<a name="l212"></a> </span><span class=cF2>//You can make it fancier if you like.</span><span class=cF0>
|
|
<a name="l213"></a> code=</span><span class=cF5>MAlloc</span><span class=cF0>(512,</span><span class=cF5>Fs</span><span class=cF0>->code_heap);
|
|
<a name="l214"></a>
|
|
<a name="l215"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l216"></a> </span><span class=cF6>"This will compile an expression\n"</span><span class=cF0>
|
|
<a name="l217"></a> </span><span class=cF6>"consisting of ints, parentheses\n"</span><span class=cF0>
|
|
<a name="l218"></a> </span><span class=cF6>"and the operators +,-,* and /.\n"</span><span class=cF0>;
|
|
<a name="l219"></a> src=</span><span class=cF5>GetStr</span><span class=cF0>;
|
|
<a name="l220"></a> </span><span class=cF1>if</span><span class=cF0> (*src) {
|
|
<a name="l221"></a> src2=src;
|
|
<a name="l222"></a> dst=code;
|
|
<a name="l223"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l224"></a> PrsTerm(&src2,&dst,PREC_PAREN);
|
|
<a name="l225"></a> </span><span class=cF1>if</span><span class=cF0> (Parse</span><span class=cF7>(</span><span class=cF0>&src2,&dst</span><span class=cF7>)</span><span class=cF0>!=PREC_EOF)
|
|
<a name="l226"></a> </span><span class=cF5>throw</span><span class=cF0>;
|
|
<a name="l227"></a> </span><span class=cF6>"$RED$This code is not efficient, but the compiler is simple.$FG$\n"</span><span class=cF0>;
|
|
<a name="l228"></a> </span><span class=cF5>Un</span><span class=cF0>(code,dst-code); </span><span class=cF2>//Unassemble the code we created.</span><span class=cF0>
|
|
<a name="l229"></a></span><span class=cF2>//</span><a href="/Wb/Kernel/KernelB.HH.HTML#l44"><span class=cF4>Call</span></a><span class=cF2>() is a function. See </span><a href="/Wb/Kernel/KUtils.HC.HTML#l144"><span class=cF4>_CALL</span></a><span class=cF2>::</span><span class=cF0>
|
|
<a name="l230"></a> </span><span class=cF2>//See also </span><a href="/Wb/Kernel/KernelB.HH.HTML#l49"><span class=cF4>CallInd</span></a><span class=cF2>(). See </span><a href="/Wb/Kernel/KUtils.HC.HTML#l332"><span class=cF4>_CALL_IND</span></a><span class=cF2>::</span><span class=cF0>
|
|
<a name="l231"></a> </span><span class=cF6>"$LTBLUE$Answer:%d$FG$\n"</span><span class=cF0>,</span><span class=cF5>Call</span><span class=cF0>(code);
|
|
<a name="l232"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l233"></a> </span><span class=cF6>"$RED$Error$FG$\n"</span><span class=cF0>;
|
|
<a name="l234"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
|
|
<a name="l235"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l236"></a> </span><span class=cF5>Free</span><span class=cF0>(src);
|
|
<a name="l237"></a> } </span><span class=cF1>else</span><span class=cF0> {
|
|
<a name="l238"></a> </span><span class=cF5>Free</span><span class=cF0>(src);
|
|
<a name="l239"></a> </span><span class=cF1>break</span><span class=cF0>;
|
|
<a name="l240"></a> }
|
|
<a name="l241"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l242"></a> </span><span class=cF5>Free</span><span class=cF0>(code);
|
|
<a name="l243"></a>}
|
|
<a name="l244"></a>
|
|
<a name="l245"></a>Main;
|
|
<a name="l246"></a>
|
|
</span></pre></body>
|
|
</html>
|