jwz - 6502asm.com [entries|archive|friends|userinfo]
jwz

  www.jwz.org
  userinfo
  archive
  rss

Links
[»| DNA (Log) (iCal) WebCollage (LJ) Mixtapes ]

6502asm.com [Tue, 10-Apr-2007 9:50 PM]
Previous Entry Add to Memories Tell a Friend Next Entry
[Tags|]
[music |Not Breathing -- Bowels of Cthulhu]

So wrong: 6502 compatible compiler and emulator in javascript.

Back when I was writing the emacs-lisp compiler, I considered adding an AppleSoft BASIC front-end for it that would directly emit emacs-VM bytecodes. Fortunately, I reconsidered.

However, I now demand that someone implement JavaScript in xscreensaver so that it can run 6502 demos.

Previously, previously.

linkReply

Comments:
[User Picture]From: [info]xthread
Wed, 11-Apr-2007 4:58 AM (UTC)

(Link)


  1. Oh, that so makes my head hurt.
  2. Gosh, I wonder if my UCSD P-system floppies can still be read by anything...
  3. Now we just need to teach it how to run Brevity, and old really, really, really light-weight 6502 OS and assembly environment


I considered adding an AppleSoft BASIC front-end for it that would directly emit emacs-VM bytecodes

You're a good man for not doing it. We'd have to write an Emacs boot-virus if you had. But you're a bad, bad man for thinking it.
[User Picture]From: [info]kehoea
Wed, 11-Apr-2007 9:16 AM (UTC)

(Link)

On the subject of the emacs-lisp compiler, is there a reason quote doesn't have a byte code? It's the most called non-byte-coded function on startup for me.
[User Picture]From: [info]strspn
Wed, 11-Apr-2007 10:42 AM (UTC)

(Link)

Use 'sexp instead of (quote sexp).
[User Picture]From: [info]kehoea
Wed, 11-Apr-2007 11:29 AM (UTC)

(Link)

I was talking about this, profiling results on startup from a recent XEmacs 21.5:
Calls Function Name                         Ticks/
===== ===========================================/
13212 (in internal-external conversion)       219/
 9232 quote                                     1/
 3701 equalp                                    1/
 3353 compare-strings                           8/
 2803 autoload                                  7/
 2343 (in expand-file-name)                    90/
 1900 (in char-byte conversion)                 0/
 1717 x-get-resource                            6/
 1508 put                                       1/
 1319 characterp                                0/
 1180 vectorp                                   0/
  995 keywordp                                  0/
  930                                           1/
  863 get-face                                  1/
  846 byte-code                                12/
  825 face-property                             3/
  […]
And if you look at lread.c, 'whatever is effectively a macro that expands to (quote whatever).
[User Picture]From: [info]jwz
Wed, 11-Apr-2007 6:27 PM (UTC)

(Link)

It's not a function, it's a special-form, which must be handled explicitly by both the compiler and interpreter. The interpreter must be doing something really dumb if it ever calls it as a function.
[User Picture]From: [info]kehoea
Wed, 11-Apr-2007 8:37 PM (UTC)

(Link)

Well, maybe, but the interpreter appears to have always done it—I'm not sure what else it’s supposed to do when handed a cons with 'quote in initial position.

The 9,000 calls at startup are mostly a result of the autoload infrastructure--two calls to quote for each autoloaded function, three for each autoloaded macro—plus the byte-compilation boilerplate infrastructure (three calls per file). But this only comes to 6364 calls, comparing with obarray and the load-history; the rest appear to come from (defalias …) and (provide ...) calls, which are more or less unavoidable, as I understand it.
[User Picture]From: [info]jwz
Wed, 11-Apr-2007 8:47 PM (UTC)

(Link)

It doesn't make any sense that it's calling quote. It's not a function, it's a special form. If the interpreter called it like a function (eval'ed its arg) then... it wouldn't be quote. So it's obviously special-casing it, or nothing would work. Special-forms are the things that are left after you've expanded all the macros that can't be implemented as functions because they don't follow the funcall rules of argument evaluation (e.g., lambda, quote, if, or.)

(I refuse to go debug this for you. There are some things I just won't do. But I suspect that it's not really calling it, and whatever you used to generate those stats is wrong.)
[User Picture]From: [info]kehoea
Wed, 11-Apr-2007 9:09 PM (UTC)

(Link)

It's not eval'ing its args. Here's the function:
DEFUN ("quote", Fquote, 1, UNEVALLED, 0, /*
[...]
*/
       (args))
{
  return XCAR (args);
}
I am reasonably sure putting a C breakpoint on Fquote and saying leval "(backtrace)" is a sensible way of working this out.

save-excursion and any number of other functions use UNEVALLED and have assigned byte-codes. Looks like I need to go code something and see what breaks.
[User Picture]From: [info]jwz
Wed, 11-Apr-2007 9:14 PM (UTC)

(Link)

"UNEVALLED" means "I am a special form, not a function."
From: [info]grayscalewolf
Thu, 12-Apr-2007 8:45 AM (UTC)

(Link)

The lisp interpreter engine treats C functions that are declared UNEVALLED very differently from normal functions. Here's the relevant code:

http://cvs.xemacs.org/viewcvs.cgi/XEmacs/xemacs/src/eval.c?annotate=1.95#3635

In particular, it doesn't do all the work to eval the args and setup a lisp frame, and the cost of the C function call is tiny.

When you byte-compile a function that has (quote x) or 'x, the byte-compiler knows to eliminate the quote. If you use M-x disassemble on the function, you'll probably see it's used the 'constant' bytecode.

However, an .elc file is not a pure-binary format. It's a sequence of eval-ed statements, and it probably has statements that look something like this:

(defalias 'foo #[nil "bytecode here" [symbol-table here]])

All the uses of 'foo are going to call the interpreter's (quote).

Since your profile data shows that the time spent in quote is much less than 0.5% of the total time, this is probably not worth worrying about.
[User Picture]From: [info]kehoea
Thu, 12-Apr-2007 9:12 AM (UTC)

(Link)

Since your profile data shows that the time spent in quote is much less than 0.5% of the total time, …
That proportion was not to be trusted, it was a debug build. But happily, with optimisation, things look even better.

There are several other special forms with byte codes; save-excursion, save-window-restriction, save-restriction are some of them, and their code is not amazingly more complex than that of quote. The question was curiosity on my part.
[User Picture]From: [info]positricity
Wed, 11-Apr-2007 11:59 AM (UTC)

(Link)

Will I be smacked down for suggesting you simply use Spidermonkey or Rhino? :-)
[User Picture]From: [info]korgmeister
Wed, 11-Apr-2007 3:17 PM (UTC)

(Link)

Given that I'm studying electronic engineering and looking to get into embedded systems programming, this is actually pretty damn cool!
From: [info]node
Wed, 11-Apr-2007 7:45 PM (UTC)

(Link)

Chris Toshok went the other way, just last week.
[User Picture]From: [info]joel
Thu, 12-Apr-2007 5:02 AM (UTC)

Also, Bender

(Link)

[User Picture]From: [info]nester
Thu, 12-Apr-2007 3:13 PM (UTC)

Re: Also, Bender

(Link)

HAHAHA, YES!
[User Picture]From: [info]xah_lee
Wed, 31-Oct-2007 2:47 AM (UTC)

(Link)

do you still code emacs lisp now?

actually i just learned emacs lisp about 2005, while being a pro Perl coder since 1998. Emacs lisp is truely fantastic for text processing. Afaik, nothing beats it.