2009-03-11 Zoltan Varga <vargaz@gmail.com>
[mono-debugger.git] / mono / dis / push-pop.h
blob3ededc25600e3af831195426991d317e751305b8
1 /* Poping */
2 /* 1 bit */
3 #define Pop0 1
4 /* 2 bits */
5 #define Pop1 2
6 /* 3 bits */
7 #define PopI 8
8 /* 1 bit */
9 #define PopI8 64
10 /* 1 bit */
11 #define Pop8 128
12 /* 1 bit */
13 #define PopR4 256
14 /* 1 bit */
15 #define PopR8 512
16 /* 1 bit */
17 #define PopRef 1024
18 /* 1 bit */
19 #define VarPop 2048
21 /* Pushing */
22 #define Push0 1
23 #define PushI 2
24 #define PushI8 4
25 #define PushR4 8
26 #define PushR8 16
27 #define PushRef 32
28 #define VarPush 64
29 #define Push1 128
32 * dis-cil.c: Disassembles CIL byte codes
34 * Author:
35 * Miguel de Icaza (miguel@ximian.com)
37 * (C) 2001 Ximian, Inc.
39 #include <config.h>
40 #include <glib.h>
41 #include <stdio.h>
42 #include "meta.h"
43 #include "dump.h"
44 #include "dis-cil.h"
46 /* Poping */
47 /* 1 bit */
48 #define Pop0 1
49 /* 2 bits */
50 #define Pop1 2
51 /* 3 bits */
52 #define PopI 8
53 /* 1 bit */
54 #define PopI8 64
55 /* 1 bit */
56 #define Pop8 128
57 /* 1 bit */
58 #define PopR4 256
59 /* 1 bit */
60 #define PopR8 512
61 /* 1 bit */
62 #define PopRef 1024
63 /* 1 bit */
64 #define VarPop 2048
66 /* Pushing */
67 #define Push0 1
68 #define PushI 2
69 #define PushI8 4
70 #define PushR4 8
71 #define PushR8 16
72 #define PushRef 32
73 #define VarPush 64
74 #define Push1 128
76 enum {
77 InlineBrTarget,
78 InlineField,
79 InlineI,
80 InlineI8,
81 InlineMethod,
82 InlineNone,
83 InlineR,
84 InlineSig,
85 InlineString,
86 InlineSwitch,
87 InlineTok,
88 InlineType,
89 InlineVar,
90 ShortInlineBrTarget,
91 ShortInlineI,
92 ShortInlineR,
93 ShortInlineVar
96 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
97 { b, c, d, e, g, h, i },
99 typedef struct {
100 char *name;
101 int pop, push;
102 int argument;
103 int bytes;
104 unsigned char o1, o2;
105 } opcode_t;
107 static opcode_t opcodes [300] = {
108 #include "mono/cil/opcode.def"
111 void
112 disassemble_cil (MonoMetadata *m, const unsigned char *start, int size)
114 const unsigned char *end = start + size;
115 const unsigned char *ptr = start;
116 opcode_t *entry;
118 while (ptr < end){
119 if (*ptr == 0xfe){
120 ptr++;
121 entry = &opcodes [*ptr + 256];
122 } else
123 entry = &opcodes [*ptr];
125 ptr++;
127 fprintf (output, "\tIL_%04x: %s ", ptr - start, entry->name);
128 switch (entry->argument){
129 case InlineBrTarget: {
130 gint target = *(gint32 *) ptr;
131 fprintf (output, "IL_%04x", ptr + 4 + target);
132 ptr += 4;
133 break;
136 case InlineField: {
137 token = *(guint32 *) ptr;
138 fprintf (output, "fieldref-0x%08x", token);
139 ptr += 4;
140 break;
143 case InlineI: {
144 int value = *(int *) ptr;
146 fprintf (output, "%d", value);
147 ptr += 4;
148 break;
151 case InlineI8: {
152 gint64 top = *(guint64 *) value;
154 fprintf (output, "%ld", top);
155 ptr += 8;
156 break;
159 case InlineMethod: {
160 token = *(guint32 *) ptr;
161 fprintf (output, "method-0x%08x", token);
162 ptr += 4;
163 break;
166 case InlineNone:
167 break;
169 case InlineR: {
170 double r = *(double *) ptr;
171 fprintf (output, "%g", r);
172 ptr += 8;
173 break;
176 case InlineSig: {
177 guint32 token = *(guint32 *) ptr;
178 fprintf (output, "signature-0x%08x", token);
179 ptr += 4;
180 break;
183 case InlineString: {
184 guint32 token = *(guint32 *) ptr;
186 fprintf (output, "string-%0x08x", token);
187 ptr += 4;
188 break;
191 case InlineSwitch: {
192 guint32 count = *(guint32 *) ptr;
193 guint32 i;
195 ptr += 4;
196 fprintf (output, "(\n\t\t\t");
197 for (i = 0; i < count; i++){
198 fprintf (output, "IL_%x", *(guint32 *) ptr);
199 ptr += 4;
201 fprintf (output, "\t\t\t)");
202 break;
205 case InlineTok: {
206 guint32 token = *(guint32 *) ptr;
208 fprintf (output, "TOKEN_%08x", token);
209 ptr += 4;
210 break;
213 case InlineType: {
214 guint32 token = *(guint32 *) ptr;
216 fprintf (output, "Type-%08x", token);
217 ptr += 4;
218 break;
221 case InlineVar: {
222 gint16 var_idx = *(gint16 *) ptr;
224 fprintf (output, "variable-%d\n", var_idx);
225 ptr += 2;
226 break;
229 case ShortInlineBrTarget: {
230 signed char x = *ptr;
232 fprintf (output, "IL_%04x", ptr - start + 1 + x);
233 ptr++:
234 break;
237 case ShortInlineI: {
238 char x = *ptr;
240 fprintf (output, "0x%02x", x);
241 ptr++;
242 break;
245 case ShortInlineR: {
246 float f = *(float *) ptr;
248 fprintf (output, "%g", (double) f);
249 ptr += 4;
250 break;
253 case ShortInlineVar: {
254 signed char x = *ptr;
256 fprintf (output, "Varidx-%d", (int) x);
257 ptr++;
258 break;
263 fprintf (output, "\n");