2000Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: Re: [MUD&#45;Dev] Storing tokens with flex &#38; bison -->
<!--X-From-R13: "Xba O. Znzoreg" <wyflfvapNvk.argpbz.pbz> -->
<!--X-Date: Mon, 03 Jan 2000 15:01:30 &#45;0800 -->
<!--X-Message-Id: 004e01bf563c$1e6623a0$020101df@JonLambert -->
<!--X-Content-Type: text/plain -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, Re: [MUD-Dev] Storing tokens with flex &amp; bison</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:jlsysinc#ix,netcom.com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00044.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00046.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00032.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00048.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00045">Author</A>
&nbsp;|&nbsp;<A HREF="#00045">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00045">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>Re: [MUD-Dev] Storing tokens with flex &amp; bison</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: &lt;<A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A>&gt;</LI>
<LI><em>Subject</em>: Re: [MUD-Dev] Storing tokens with flex &amp; bison</LI>
<LI><em>From</em>: "Jon A. Lambert" &lt;<A HREF="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</A>&gt;</LI>
<LI><em>Date</em>: Mon, 3 Jan 2000 17:44:12 -0500</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Sender</em>: <A HREF="mailto:mud-dev-admin#kanga,nu">mud-dev-admin#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
Chris Gray wrote:
&gt;[Jon A. Lambert:]
&gt;
&gt;[Oh dear, I'm running on again. Sorry about that!]
&gt;
No problem.  Me too.

&gt;&gt; I've noticed that you can obtain lots of little optimization tweaks by just using 
&gt;&gt; "lazy evaluation".  That is instead of emitting an opcode as soon as it is 
&gt;&gt; identified, pass that opcode along as an integer to the next routine in the 
&gt;&gt; parse tree as you begin to evaluate the next token.  
[snip]
&gt;
&gt;Normally, you just do the special-case check at the place where you have
&gt;all of the needed information. In this case, it would be in the processing
&gt;for the '+' operation. I don't have an 'add1' opcode, so this particular
&gt;example doesn't work for me, but here is a more generic one, which is
&gt;a minor kind of "compile-time expression evaluation":
&gt;
&gt;    case ex_negate:
&gt;    case ex_fNegate:
&gt; ex = ex-&gt;ex_v.ex_expressionPtr-&gt;ex_right;
&gt; if (ex-&gt;ex_kind == ex_int) {
&gt;     bcGenConst(- ex-&gt;ex_v.ex_integer);
&gt; } else {
&gt;     bcComp(ex);
&gt;     bcWriteOp(bc_neg);
&gt; }
&gt; break;
&gt;
&gt;If the operand for the unary '-' operator is a constant, then just generate
&gt;the negative of that constant, rather than generating the positive
&gt;value and then a 'neg' byte-code instruction.
&gt;
&gt;Some compilers do a lot of special cases like this. Others have a lot
&gt;of "tree rewriting rules". They work through the built-up parse trees,
&gt;applying various rewriting rules wherever their patterns match. The result
&gt;is still a valid parse tree, but it has been optimized with a bunch of
&gt;special cases.
&gt;

Not to belabor the point, since I think we're in agreement although coming from
different angles.  The interesting thing about Crenshaw's toy compiler is that it's a 
single phase compiler.   The AST is the current state of the compiler's run-time 
stack and code generation is done as it parser executes.   Admittedly this 
limits your optimizing options since the AST disappears when the parsing is
complete.  It is rather elegantly simple though.

I thought I'd include another example clipped from the Crenshaw tutorial for the 
benefit of anyone else reading.   He also discusses the unary negate optimization 
you mention above elsewhere. 

---cut---
There is  a concept in compiler theory called "lazy" translation.
The  idea is that you typically don't just  emit  code  at  every
action.  In fact, at the extreme you don't emit anything  at all,
until  you  absolutely  have to.  To accomplish this, the actions
associated with the parsing routines  typically  don't  just emit
code.  Sometimes  they  do,  but  often  they  simply  return in-
formation back to the caller.  Armed with  such  information, the
caller can then make a better choice of what to do.

For example, given the statement

               x = x + 3 - 2 - (5 - 4)  ,

our compiler will dutifully spit  out a stream of 18 instructions
to load each parameter into  registers,  perform  the arithmetic,
and store the result.  A lazier evaluation  would  recognize that
the arithmetic involving constants can  be  evaluated  at compile
time, and would reduce the expression to

               x = x + 0  .

An  even  lazier  evaluation would then be smart enough to figure
out that this is equivalent to

               x = x  ,

which  calls  for  no  action  at  all.   We could reduce 18  in-
structions to zero!
---end cut---

As you can see Crenshaw is in conflict with your .sig ---
He designs in inefficiency in and removes it later... ;-)

&gt;&gt; Another possible optimization is for the parser to generate code for
&gt;&gt; a virtual machine that uses a combination of register and stack instructions.
&gt;&gt; The parser can be made smart enough to know to use registers.
&gt;
&gt;I thought about that a bit for my system. If you step back a bit and look
&gt;at what a modern CPU is actually doing, you find that the register set is
&gt;very much like a stack - they will both end up in the processor's data
&gt;cache. The key thing to look at is how many native machine instructions
&gt;it takes to execute the sequence you want to look at. With a register
&gt;set, you are going to have to extract a register number from the byte-code
&gt;stream, and use that to index the array of registers in the virtual machine.
&gt;That takes native machine instructions, perhaps more than are needed to
&gt;do stack operations, given that many real CPU's have post-increment and
&gt;pre-decrement addressing modes that the native compiler can generate.
&gt;

Hmm, I was thinking in terms of how many instructions my VM will be traversing 
rather than how the hardware is going to handle those instructions. 
Now I could implement my VM class as a 3 register machine using the members:

int a, x, y;
// instead of
int registers[3];

Much of this does depends on how your C/C++ compiler generates and 
optimizes the code you write anyway.  In Builder, accessing an array with 
a constant subscript incurs no more overhead than any other automatic
variable.

From the bytecode angle the register is an intrinsic part of the opcode, that is
it is determined at compilation time.  Now push() and pop() operations 
in the VM are going to be to checking for a stack overflow or underflow, no?   
There not just going to be doing a *sp++ = value; or return --*sp;

So a register specific bytecode like "lda_int"  simply calls:

lda_int(int value) 
{
    a = value;
}

push(int value) 
{
    if (sp == stacktop)
        throw(ErrorStackFault); // Or whatever you do...
    *sp++ = value;
}

And quite possibly there are more savings in using an "add_a_x" register opcode 
as opposed to a stack-based "add" opcode.  

add_a_x()
{
    push(a + x);
}

add()
{
    push(pop()+pop());
}

Certainly you can inline all these VM functions for more fun.   
There may also be possible optimizations by keeping loop counters 
accessible in registers rather than on the stack.

I don't know.  It may be something worth revisiting.  
Besides is there anything more swell than a VM that looks like a 6502? :-)

I guess this might lead to the question of whether it is better to have more 
or less opcodes for your VM's instruction set.  I will note that the Java VM 
has quite a boatload of special case opcodes, it even distinguishes between 
pushing an int and pushing a constant one or zero.  So is it better to accomplish 
something by generating a longer series of simple bytecode operations, or 
implement a lot of special purpose bytecodes?  

&gt;&gt; And finally, there are optimizations that you just can't do with a RD parser. 
&gt;&gt; You can pass the generated byte-code into a post processor that rescans
&gt;&gt; it for patterns that can be simplified.  Call it a byte-code optimizer, or
&gt;&gt; peephole optimizer.  The only problem with this last bit is that pretty printing
&gt;&gt; or reverse disassembly to source becomes rather difficult.  I've noticed a
&gt;&gt; lot of mud servers do not do this and seek to support reverse code generation
&gt;&gt; instead of storing the source code somewhere.  Perhaps they are missing
&gt;&gt; out on some major optimization possibilities.
&gt;
&gt;Those can readily be done in conjunction with recursive descent parsing.
&gt;Let's make sure we understand the context here. Recursive descent parsing
&gt;normally only means the parsing of the input token sequence into complete
&gt;statements and functions understood by the language parser as a whole.
&gt;That parser can be tied into direct code generation which emits byte-codes,
&gt;or can produce a "parse-tree" data structure.
&gt;
&gt;In the latter case, a recursive walk over that tree will produce a byte-code
&gt;stream. That recursive walk is *not* known as recursive descent parsing.
&gt;I don't think I've seen the term "recursive descent" used for things other
&gt;than parsing, just to avoid the possible confusions.
&gt;

&gt;In any case, optimizations can be done regardless of what parsing or
&gt;code-generation techniques are used. In my MUD, I build a parse tree,
&gt;because that is the form that non-byte-code-compiled code runs from,
&gt;directly. In my Draco compiler for 8080 and later for MC68000, the recursive
&gt;descent parser directly emitted native machine code, which was filtered
&gt;through a pretty good peephole optimizer. Peephole optimizers, and special
&gt;cases like we've been discussing, can make for pretty reasonable generated
&gt;code. Nothing like gcc and commercial compilers which do things like
&gt;register colouring and full program restructuring, of course!
&gt;

Yes, you are correct.  What I mean to say is that you can't normally do those
peephole operations in the parsing phase.  You have two phases then, 
the first generates the tree (recursive descent parser),  the second walks it 
(code generation).   It is interesting though that there are a lot of possible 
configurations here.  

For example, Eric Harth's Smart compiler does a 4 phase compilation:
1) Lexical Analysis - reads the source and produces a stream of tokens. (scanner)
2) Syntax Analysis - reads the token stream and produces an AST (RDP)
3) Semantic Analysis - reads the AST and does type checking, assigns scope 
and builds symbolic tables. 
4) Code Generation - reads the AST and symbolic tables and outputs bytecode.

I don't think speed of compilation is really an issue at least in the context of 
a mud server's internal programming language.  Typically in an LP, Moo, Cold,
Muck, etc. you are compiling rather small pieces of method code or single objects
so you can add as many phases or passes to the compilation as you need.
Of course online compilation should be multitasked and handled just like any 
other mud event especially if you have a user programmable setup.

&gt;Either I'm not understanding what you are getting at, or you are
&gt;misinterpreting what I was getting at. I was not referring to evaluation
&gt;at compile time (e.g. my example above of the compile-time versus
&gt;run-time evaluation of the unary negation operator).

Other than me tripping over terminology again, I still don't agree with the
following:

---&gt;Byte-code execution really only makes sense, I believe, for a strongly
typed language. If run-time checks and conversions are needed, they will
likely cost far more time than the basic interpretation of the code, and
so going to byte-code instead of staying with something more direct, could
be a waste of time, in that byte-code won't speed execution up much.

I agree that overhead is higher, I still think it's less than direct interpretation.
And there are other reasons where byte-code makes a lot of sense.  It's
a common target for multiple use languages and functional complexity is 
split between parsing and execution rather than having a monolithic 
interpreter.  There are also a whole hosts of structures built and checked 
during compilation to bytecode that are avoided altogether in the execution of 
that code, like classes symbol tables, object symbol tables, local symbol tables,
method signature checking, etc.  Perhaps your statement holds true for a 
strictly procedural language, I don't think it holds true for an OO mud language. 

&gt;With a non-strongly-typed language (assuming no smart compiler that can
&gt;deduce the types), either the byte-code emitted is considerably more
&gt;complex, or the C code for the byte-code engine is considerably more
&gt;complex. Either way, it runs slower, perhaps significantly slower.

I do weak-typing and use a very simple byte-code.  I only use one
byte-code for the add operation.  And the byte-code engine is pretty
simple too:

void add_op() throw
{
  Var rval;
  Var lval;
  Var retval; 

  rval = pop_op();
  lval = pop_op();
  retval = lval + rval;
  push_op(retval);
}

It looks like what one would do with a strongly-typed system.  All the  
complexity and overhead is hiding under the covers of C++. 

Var&amp; Var::operator=(const Var&amp; value);  
friend Var operator+(const Var&amp; v1,const Var&amp; v2) throw(TypeError);  

You'll find all the switches, if-else chains, and conversion exceptions in 
those functions.  Yes it's overhead but it is not complex.  Maintenance, 
debugging and readability are very good.  At least I think so.  

--
--*     Jon A. Lambert - TychoMUD Email: jlsysinc#nospam,ix.netcom.com     *--
--*     Mud Server Developer's Page &lt;<A  HREF="http://jlsysinc.home.netcom.com">http://jlsysinc.home.netcom.com</A>&gt;      *--
--* "No Free man shall ever be debarred the use of arms." Thomas Jefferson *--





_______________________________________________
MUD-Dev maillist  -  MUD-Dev#kanga,nu
<A  HREF="http://www.kanga.nu/lists/listinfo/mud-dev">http://www.kanga.nu/lists/listinfo/mud-dev</A>

</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00044.html">[MUD-Dev] Microthreads for Python</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00046.html">Re: [MUD-Dev] Embedded languages, object persistance... ack.</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00032.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00048.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00045"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00045"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>Re: [MUD-Dev] Storing tokens with flex &amp; bison</STRONG>, <EM>(continued)</EM>
<ul compact>
<ul compact>
<ul compact>
<LI><strong><A NAME="00067" HREF="msg00067.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
Dominic J. Eidson <a href="mailto:sauron#the-infinite,org">sauron#the-infinite,org</a>, Fri 07 Jan 2000, 22:35 GMT
</LI>
</ul>
<LI><strong><A NAME="00120" HREF="msg00120.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
J C Lawrence <a href="mailto:claw#kanga,nu">claw#kanga,nu</a>, Tue 18 Jan 2000, 08:47 GMT
</LI>
</ul>
<LI><strong><A NAME="00030" HREF="msg00030.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
Jon A. Lambert <a href="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</a>, Mon 03 Jan 2000, 05:23 GMT
</LI>
<LI><strong><A NAME="00032" HREF="msg00032.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
cg <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Mon 03 Jan 2000, 06:20 GMT
</LI>
<LI><strong><A NAME="00045" HREF="msg00045.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
Jon A. Lambert <a href="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</a>, Mon 03 Jan 2000, 23:01 GMT
</LI>
<LI><strong><A NAME="00048" HREF="msg00048.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
cg <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 04 Jan 2000, 07:07 GMT
</LI>
<LI><strong><A NAME="00068" HREF="msg00068.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
cg <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Sat 08 Jan 2000, 04:27 GMT
<UL>
<LI><strong><A NAME="00069" HREF="msg00069.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
Chris Jones <a href="mailto:cjones#v-wave,com">cjones#v-wave,com</a>, Sat 08 Jan 2000, 07:19 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00116" HREF="msg00116.html">Re: [MUD-Dev] Storing tokens with flex &amp; bison</A></strong>, 
Jon A. Lambert <a href="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</a>, Tue 18 Jan 2000, 07:01 GMT
</LI>
</ul>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>