logo       

Re: Progrmaming in Bytecode?: msg#00809

lang.smalltalk.squeak.general

Subject: Re: Progrmaming in Bytecode?

Aaron <reic0024@xxxxxxxxx> wrote...

>I know this may sound sadistic,

Perverse, maybe, but not sadistic.

>but how could I go about programming in
>Smalltalk bytecode, directly? I've seen assemblers for Java, that do the
>same job as assemblers for native CPUs. I imagine such a tool doesn't
>exist for SMalltalk at this time, but perhaps it wouldn't be hard to put
>one together in my quest to play with bytecode.
>
>How could I write methods in bytecode directly?
>
>I know- it sounds sick. I've never wanted to program any sort of machine
>language, but today- out of nowhere- I got a hankering to do so.
>Progrmaming 'machine language' on the SVM seemed like a good place to
>learn. :)

Seize the moment!

In the process, you can learn how to write an assembler, a wonderful exercise,
and something one can do in just a page or two of Squeak.

Just to get started (and crash your Squeak a few times ;-), look at

CompiledMethod class
newBytes: numberOfBytes trailerBytes: trailer
nArgs: nArgs nTemps: nTemps nStack: stackSize
nLits: nLits primitive: primitiveIndex

This will make up a blank method with room for the requested number of bytes
and literals, and with a valid header that encodes the other parameters.

Then look at the senders of this method. They include, or course the big
Compiler>>generate: that builds most methods, but you will also find a couple
of very simple applications for producing methods that do, for instance,
nothing but return self, or return an instvar.

Having created a blank method object, you can store whatever you want into its
bytes and into its literals, using code similar to that in #generate: and the
simpler methods.

In order to try out your new method, you'll have to install it in a method
dictionary, and send a message to an instance whose class has access to that
message. You can find what you need for that in the senders of #generate:.
You may find Compiler>>evaluate:... to be especially handy, as it is designed
to build a method, install it, execute it and throw it away -- it's what Squeak
uses to evaluate a string as a do-it.

That should enable you to program bytes "in binary" -- ie supplying actual
bytecode numbers and literal objects.

To build an assembler, it will probably be most convenient for you to start
with the printed output from CompiledMethod>>symbolic (this is what you see
when you select "show bytecodes" in the browser.

What you should then do is write a Squeak program that can "assemble" such a
listing. Your first version can use labels and goto's, but you'll probably
want to add some nicer control structures. Also you'll either need to supply
all the literals and use some reference scheme like lit1, lit2, etc, or you'll
have to gather up the literals as you go, the way the compiler does.

That should be enough to at least get you into the jungle. Send us a letter
once in a while to let us know how you're doing!

- Dan




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise