Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: Epic Fail Date: Wed, 30 Apr 2014 22:02:11 -0400 Organization: Aioe.org NNTP Server Lines: 78 Message-ID: References: NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:30025 On Wed, 30 Apr 2014 07:45:47 -0400, Andrew Haley wrote: > We need to talk about real examples. > > The problem is that if you have a Forth word like, say: > > : foo 9 * ; > > you could implement this in the JVM as a method: > > public static long bar(long n) { > return n * 9; > } > > [snip IR and assembly] > > but in Forth we have a stack that is accessed by every word, > and can be left unbalanced, so we have to expose it as a > real array: > > public static int foo(long[] stack, int sp) { > stack[sp] *= 9; > return sp; > } > > [snip IR and assembly] > Is there any reason to return 'sp'? You didn't modify it... Why didn't you do something like this?: public static int foo(long[] stack, int sp) { long n; n=stack[sp]; n*=9; stack[sp]=n; return n; } I.e., wouldn't the local variable 'n' _allow_ the compiler to use a register thereby possibly reducing the code somewhat? Try it... I.e., in C, you might've chosen to pass a pointer to 'sp' in the parameter list instead. That would eliminate the following line in the main code, replacing it with the next: sp=foo(stack,sp); /* eliminated */ foo(stack,&sp); /* replacement */ Also, can you make 'stack' and 'sp' have file scope, be global, so you don't need to pass them? That's probably adding a small amount of parameter passing code. I.e., 'void' parameters, at least in C, eliminates some passing overhead: public static void foo(void) { long n; n=stack[sp]; n*=9; stack[sp]=n; } Boundary checks on the stack pointer for underflow and overflow probably adds some extra code too. I haven't figured out a clean or safe way to eliminate that from my interpreter, yet. That would be nice if I could. I don't see stack boundary checks in the code, but it looked like some was in the assembly. I'm assuming this is for C++ and some object-oriented feature was in play which inserted the checks. Rod Pemberton