Something black, white, which prints, and is read all over

Lyndon Tremblay humasect at shaw.ca
Wed Mar 23 13:37:29 PST 2005


Here is some code. It is not meant for anything beyond a proof, so,
unpolished. But I will go with commercialism and send this little bytecode
printer:

Use:

"printMethod: [inform: 'test']."

Will "print" the contents of the block. Uses code from mobius/?.slate
(decodeImmediate)

-lyndon

---------

lobby addPrototype: #Interpreter derivedFrom: {Cloneable}.
Interpreter addSlot: #codePosition valued: Nil.
Interpreter addSlot: #codeSize valued: Nil.
Interpreter addSlot: #method valued: Nil.

i@(Interpreter traits) newWithMethod: method@(CompiledMethod traits)
[
  i clone `>> [ method: method. codeSize: method code size. codePosition:
0. ]
].

i@(Interpreter traits) decodeImmediate
[| n code val |
  n: i codePosition.
  code: (i method code at: n).
  val: (code bitAnd: 16r7F).
  [code >= 16r80]
 whileTrue:
   [n: n + 1.
    code: (i method code at: n).
    val: (val << 7 bitOr: (code bitAnd: 16r7F))].
  i codePosition: n + 1.
  val
].

i@(Interpreter traits) print
[| op val |
   Console ; 'code size: ' ; i codeSize print ; '\n'.
   [i codePosition < i codeSize] whileTrue:
  [
   op: (i method code at: i codePosition).
   i codePosition: i codePosition + 1.
   val: op >> 4.
   val = 16rF ifTrue: [ val: i decodeImmediate ].

   Console ; i codePosition print ; ': <' ; op print ; '> \t'.

   (op bitAnd: 16r0F) caseOf: {
0 -> [Console ; 'sendmsg (' ; val print ; ')'].
1 -> [Console ; 'load var ' ; val print].
2 -> [Console ; 'stor var ' ; val print].
3 -> [Console ; 'load free ' ; val print].
4 -> [Console ; 'stor free ' ; val print].
5 -> [Console ; 'load lit ' ; (i method literals at: val) print].
6 -> [Console ; 'load sel ' ; (i method selectors at: val) print].
7 -> [Console ; 'pop' ; val print].
8 -> [Console ; 'new array' ; val print].
9 -> [Console ; 'new block' ; val print].
10 -> [Console ; 'branchkey' ; val print].
11 -> [Console ; 'sendmsg& ' ; val print].
12 -> [Console ; 'returnf ' ; val print].
13 -> [Console ; 'push int' ; val print].
14 -> [Console ; '(ext ' ; op print ; ')'].
}.

   Console ; '\n'.
   "i returnFrom: 0."
   ].
].

h@(lobby traits) printMethod: method@(CompiledMethod traits)
[
  (h Interpreter newWithMethod: method) print
].





More information about the Slate mailing list