X11 sample rewritten; now it works again
[k8lst.git] / modules / disasm_old.st
blob995b90407ee4c0c6537a8f496cefa980d4c5482a
2  Little Smalltalk, Version 5
4  Copyright (C) 1987-2005 by Timothy A. Budd
5  Copyright (C) 2007 by Charles R. Childers
6  Copyright (C) 2005-2007 by Danny Reinhold
7  Copyright (C) 2010 by Ketmar // Vampire Avalon
9  ============================================================================
10  This license applies to the virtual machine and to the initial image of
11  the Little Smalltalk system and to all files in the Little Smalltalk
12  packages except the files explicitly licensed with another license(s).
13  ============================================================================
14  Permission is hereby granted, free of charge, to any person obtaining a copy
15  of this software and associated documentation files (the 'Software'), to deal
16  in the Software without restriction, including without limitation the rights
17  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  copies of the Software, and to permit persons to whom the Software is
19  furnished to do so, subject to the following conditions:
21  The above copyright notice and this permission notice shall be included in
22  all copies or substantial portions of the Software.
24  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  DEALINGS IN THE SOFTWARE.
32 Package [
33   Debug
37 Method extend [
38 disassemble: indent at: initPC for: initCount [
39   | pc low high pcend needCR ac pname bins |
40   pc := initPC + 1.
41   pcend := pc + initCount.
42   bins := #('<' '<=' '+' '-' '*' '/' '%' '>' '>=' '~=' '=' '&' '|' '==').
43   [ pc < pcend ] whileTrue: [
44     "Default, terminate line with CR"
45     needCR := true.
46     pc > byteCodes size ifTrue: [ ^self ].
47     "Show PC and indent listing of line"
48     ((pc - 1) printWidth: 4) print. ':' print.
49     1 to: indent do: [:x | ' ' print].
50     "Fetch basic opcode fields"
51     high := byteCodes at: pc.
52     pc := pc + 1.
53     low := high % 16.
54     high := high / 16.
55     (high = 0) ifTrue: [
56       high := low.
57       low := byteCodes at: pc.
58       pc := pc + 1.
59     ].
60     high = 0 ifTrue: [ 'NO MORE CODE' printNl. ^self ].
61     Case test: high;
62       case: 1 do: [ 'PushInstance ' print. low print ];
63       case: 2 do: [ 'PushArgument ' print. low print ];
64       case: 3 do: [ 'PushTemporary ' print. low print ];
65       case: 4 do: [ 'PushLiteral ' print. low print ];
66       case: 5 do: [
67         'PushConstant ' print.
68         Case test: low;
69           case: 0 do: [ 'nil' print ];
70           case: 1 do: [ 'true' print ];
71           case: 2 do: [ 'false' print ];
72           else: [:t | (t - 3) print ].
73       ];
74       case: 6 do: [ 'AssignInstance ' print. low print ];
75       case: 7 do: [ 'AssignArgument ' print. low print ];
76       case: 8 do: [ 'AssignTemporary ' print. low print ];
77       case: 9 do: [ 'MarkArguments ' print. low print ];
78       case: 10 do: [
79         low := byteCodes wordAt: pc.
80         pc := pc + 2.
81         ac := byteCodes at: pc.
82         pc := pc + 1.
83         high := low - (pc-1).
84         'PushBlock argCount=' print. ac printNl.
85         self disassemble: indent + 1 at: pc for: high.
86         pc := pc + high.
87         needCR := false
88       ];
89       case: 11 do: [
90         'SendUnary ' print.
91         Case test: low;
92           case: 0 do: [ 'isNil' print ];
93           case: 1 do: [ 'notNil' print ];
94           else: [:t | 'unknown #' print. t print ].
95       ];
96       case: 12 do: [
97         'SendBinary ' print.
98         ((low >= 0) and: [ low < bins size ])
99           ifTrue: [ (bins at: low + 1) print ]
100           ifFalse: [ 'unknown #' print. low print ].
101       ];
102       case: 13 do: [ 'SendMessage ' print. (literals at: (low+1)) print ];
103       case: 14 do: [
104         'DoPrimitive ' print.
105         high := byteCodes at: pc.
106         pc := pc + 1.
107         pname := System nameOfPrimitive: high.
108         pname ifNil: [ 'unknown #' print. high print ] ifNotNil: [ pname print ].
109         '; argc: ' print. low print.
110       ];
111       case: 15 do: [
112         "'DoSpecial ' print."
113         Case test: low;
114           case: 0 do: [ 'Breakpoint' print ];
115           case: 1 do: [ 'SelfReturn' print ];
116           case: 2 do: [ 'StackReturn' print ];
117           case: 3 do: [ 'BlockReturn' print ];
118           case: 4 do: [ 'Duplicate' print ];
119           case: 5 do: [ 'PopTop' print ];
120           when: [ :v | (v > 5) & (v < 11) ] do: [:bc|
121             Case test: bc;
122               case: 6 do: [ 'Branch ' print ];
123               case: 7 do: [ 'BranchIfTrue ' print ];
124               case: 8 do: [ 'BranchIfFalse ' print ];
125               case: 9 do: [ 'BranchIfNil ' print ];
126               case: 10 do: [ 'BranchIfNotNil ' print ].
127             high := byteCodes wordAt: pc.
128             pc := pc + 2.
129             high print
130           ];
131           case: 11 do: [
132             'SendToSuper ' print.
133             low := byteCodes at: pc.
134             pc := pc + 1.
135             (literals at: low+1) print
136           ];
137           case: 12 do: [ 'ThisContext' print ];
138           else: [:opc | 'unknown special opcode #' print. opc print ].
139       ];
140       else: [:opc | 'unknown opcode #' print. opc print ].
141     needCR ifTrue: [
142       Char newline print
143     ]
144   ]
147 disassemble [
148   self disassemble: 1 at: 0 for: (byteCodes size)
153 Class extend [
154 disasmMethod: nm [
155   | meth |
156   meth := self allMethods at: nm ifAbsent: [ ^self error: 'no such method' ].
157   meth disassemble.