Revert broken changes.
[mono.git] / mcs / class / Mono.CSharp.Debugger / AssemblerWriterI386.cs
1 //
2 // Mono.CSharp.Debugger/AssemblerWriterI386.cs
3 //
4 // Author:
5 //   Martin Baulig (martin@gnome.org)
6 //
7 // IAssemblerWriter implementation for the Intel i386.
8 //
9 // (C) 2002 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.IO;
14         
15 namespace Mono.CSharp.Debugger
16 {
17         public class AssemblerWriterI386 : IAssemblerWriter
18         {
19                 public AssemblerWriterI386 (StreamWriter writer) {
20                         this.writer = writer;
21                 }
22
23                 private StreamWriter writer;
24                 private static int next_anon_label_idx = 0;
25
26                 public void WriteLabel (string label)
27                 {
28                         writer.WriteLine (".L_" + label + ":");
29                 }
30
31                 public int GetNextLabelIndex ()
32                 {
33                         return ++next_anon_label_idx;
34                 }
35
36                 public int WriteLabel ()
37                 {
38                         int index = ++next_anon_label_idx;
39
40                         WriteLabel (index);
41
42                         return index;
43                 }
44
45                 public void WriteLabel (int index)
46                 {
47                         char[] output = { '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
48                                           ':', '\n' };
49
50                         unchecked {
51                                 int value = (int) index;
52                                 output [3] = hex [(value & 0xf00000) >> 20];
53                                 output [4] = hex [(value & 0x0f0000) >> 16];
54                                 output [5] = hex [(value & 0x00f000) >> 12];
55                                 output [6] = hex [(value & 0x000f00) >>  8];
56                                 output [7] = hex [(value & 0x0000f0) >>  4];
57                                 output [8] = hex [(value & 0x00000f)];
58                         }
59
60                         writer.Write (output, 0, output.Length);
61                 }
62
63                 private static readonly char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
64                                                        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
65
66                 public void WriteUInt8 (bool value)
67                 {
68                         WriteUInt8 (value ? 1 : 0);
69                 }
70
71                 public void WriteUInt8 (int value)
72                 {
73                         char[] output = { '\t', '.', 'b', 'y', 't', 'e', '\t', '\t',
74                                           '0', 'x', '\0', '\0',
75                                           '\n' };
76
77                         unchecked {
78                                 output [10] = hex [(value & 0xf0) >> 4];
79                                 output [11] = hex [value & 0x0f];
80                         }
81
82                         writer.Write (output, 0, output.Length);
83                 }
84
85                 public void WriteInt8 (int value)
86                 {
87                         char[] output = { '\t', '.', 'b', 'y', 't', 'e', '\t', '\t',
88                                           '0', 'x', '\0', '\0',
89                                           '\n' };
90
91                         unchecked {
92                                 uint uvalue = (uint) value;
93                                 output [10] = hex [(uvalue & 0xf0) >> 4];
94                                 output [11] = hex [uvalue & 0x0f];
95                         }
96
97                         writer.Write (output, 0, output.Length);
98                 }
99
100                 public void Write2Bytes (int a, int b)
101                 {
102                         char[] output = { '\t', '.', 'b', 'y', 't', 'e', '\t', '\t',
103                                           '0', 'x', '\0', '\0', ',', ' ',
104                                           '0', 'x', '\0', '\0',
105                                           '\n' };
106
107                         unchecked {
108                                 uint ua = (uint) a;
109                                 uint ub = (uint) b;
110                                 output [10] = hex [(ua & 0xf0) >> 4];
111                                 output [11] = hex [ua & 0x0f];
112                                 output [16] = hex [(ub & 0xf0) >> 4];
113                                 output [17] = hex [ub & 0x0f];
114                         }
115
116                         writer.Write (output, 0, output.Length);
117                 }
118
119                 public void WriteUInt16 (int value)
120                 {
121                         writer.WriteLine ("\t.2byte\t\t" + value);
122                 }
123
124                 public void WriteInt16 (int value)
125                 {
126                         writer.WriteLine ("\t.2byte\t\t" + value);
127                 }
128
129                 public void WriteUInt32 (int value)
130                 {
131                         char[] output = { '\t', '.', 'l', 'o', 'n', 'g', '\t', '\t',
132                                           '0', 'x', '\0', '\0', '\0', '\0', '\0', '\0',
133                                           '\0', '\0', '\n' };
134
135                         unchecked {
136                                 output [10] = hex [(value & 0xf0000000) >> 28];
137                                 output [11] = hex [(value & 0x0f000000) >> 24];
138                                 output [12] = hex [(value & 0x00f00000) >> 20];
139                                 output [13] = hex [(value & 0x000f0000) >> 16];
140                                 output [14] = hex [(value & 0x0000f000) >> 12];
141                                 output [15] = hex [(value & 0x00000f00) >>  8];
142                                 output [16] = hex [(value & 0x000000f0) >>  4];
143                                 output [17] = hex [(value & 0x0000000f)];
144                         }
145
146                         writer.Write (output, 0, output.Length);
147                 }
148
149                 public void WriteInt32 (int value)
150                 {
151                         char[] output = { '\t', '.', 'l', 'o', 'n', 'g', '\t', '\t',
152                                           '0', 'x', '\0', '\0', '\0', '\0', '\0', '\0',
153                                           '\0', '\0', '\n' };
154
155                         unchecked {
156                                 uint uvalue = (uint) value;
157                                 output [10] = hex [(uvalue & 0xf0000000) >> 28];
158                                 output [11] = hex [(uvalue & 0x0f000000) >> 24];
159                                 output [12] = hex [(uvalue & 0x00f00000) >> 20];
160                                 output [13] = hex [(uvalue & 0x000f0000) >> 16];
161                                 output [14] = hex [(uvalue & 0x0000f000) >> 12];
162                                 output [15] = hex [(uvalue & 0x00000f00) >>  8];
163                                 output [16] = hex [(uvalue & 0x000000f0) >>  4];
164                                 output [17] = hex [(uvalue & 0x0000000f)];
165                         }
166
167                         writer.Write (output, 0, output.Length);
168                 }
169
170                 public void WriteSLeb128 (int value)
171                 {
172                         writer.WriteLine ("\t.sleb128\t" + value);
173                 }
174
175                 public void WriteULeb128 (int value)
176                 {
177                         writer.WriteLine ("\t.uleb128\t" + value);
178                 }
179
180                 public void WriteAddress (int value)
181                 {
182                         if (value == 0)
183                                 writer.WriteLine ("\t.long\t\t0");
184                         else
185                                 writer.WriteLine ("\t.long\t\t" + value);
186                 }
187
188                 public void WriteString (string value)
189                 {
190                         writer.WriteLine ("\t.string\t\t\"" + value + "\"");
191                 }
192
193                 public void WriteSectionStart (String section)
194                 {
195                         writer.WriteLine ("\t.section\t." + section);
196                 }
197
198                 public void WriteSectionEnd ()
199                 {
200                         writer.WriteLine ("\t.previous\n");
201                 }
202
203                 public void WriteRelativeOffset (int start_label, int end_label)
204                 {
205                         char[] output = { '\t', '.', 'l', 'o', 'n', 'g', '\t', '\t',
206                                           '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
207                                           ' ', '-', ' ',
208                                           '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
209                                           '\n' };
210
211                         unchecked {
212                                 output [11] = hex [(end_label & 0xf00000) >> 20];
213                                 output [12] = hex [(end_label & 0x0f0000) >> 16];
214                                 output [13] = hex [(end_label & 0x00f000) >> 12];
215                                 output [14] = hex [(end_label & 0x000f00) >>  8];
216                                 output [15] = hex [(end_label & 0x0000f0) >>  4];
217                                 output [16] = hex [(end_label & 0x00000f)];
218                                 output [23] = hex [(start_label & 0xf00000) >> 20];
219                                 output [24] = hex [(start_label & 0x0f0000) >> 16];
220                                 output [25] = hex [(start_label & 0x00f000) >> 12];
221                                 output [26] = hex [(start_label & 0x000f00) >>  8];
222                                 output [27] = hex [(start_label & 0x0000f0) >>  4];
223                                 output [28] = hex [(start_label & 0x00000f)];
224                         }
225
226                         writer.Write (output, 0, output.Length);
227                 }
228
229                 public void WriteShortRelativeOffset (int start_label, int end_label)
230                 {
231                         char[] output = { '\t', '.', 'b', 'y', 't', 'e', '\t', '\t',
232                                           '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
233                                           ' ', '-', ' ',
234                                           '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
235                                           '\n' };
236
237                         unchecked {
238                                 output [11] = hex [(end_label & 0xf00000) >> 20];
239                                 output [12] = hex [(end_label & 0x0f0000) >> 16];
240                                 output [13] = hex [(end_label & 0x00f000) >> 12];
241                                 output [14] = hex [(end_label & 0x000f00) >>  8];
242                                 output [15] = hex [(end_label & 0x0000f0) >>  4];
243                                 output [16] = hex [(end_label & 0x00000f)];
244                                 output [23] = hex [(start_label & 0xf00000) >> 20];
245                                 output [24] = hex [(start_label & 0x0f0000) >> 16];
246                                 output [25] = hex [(start_label & 0x00f000) >> 12];
247                                 output [26] = hex [(start_label & 0x000f00) >>  8];
248                                 output [27] = hex [(start_label & 0x0000f0) >>  4];
249                                 output [28] = hex [(start_label & 0x00000f)];
250                         }
251
252                         writer.Write (output, 0, output.Length);
253                 }
254
255                 public void WriteAbsoluteOffset (string label)
256                 {
257                         writer.WriteLine ("\t.long\t\t.L_" + label);
258                 }
259
260                 public void WriteAbsoluteOffset (int index)
261                 {
262                         char[] output = { '\t', '.', 'l', 'o', 'n', 'g', '\t', '\t',
263                                           '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
264                                           '\n' };
265
266                         unchecked {
267                                 output [11] = hex [(index & 0xf00000) >> 20];
268                                 output [12] = hex [(index & 0x0f0000) >> 16];
269                                 output [13] = hex [(index & 0x00f000) >> 12];
270                                 output [14] = hex [(index & 0x000f00) >>  8];
271                                 output [15] = hex [(index & 0x0000f0) >>  4];
272                                 output [16] = hex [(index & 0x00000f)];
273                         }
274
275                         writer.Write (output, 0, output.Length);
276
277                 }
278
279                 public object StartSubsectionWithSize ()
280                 {
281                         int start_index = ++next_anon_label_idx;
282                         int end_index = ++next_anon_label_idx;
283
284                         WriteRelativeOffset (start_index, end_index);
285                         WriteLabel (start_index);
286
287                         return end_index;
288                 }
289
290                 public object StartSubsectionWithShortSize ()
291                 {
292                         int start_index = ++next_anon_label_idx;
293                         int end_index = ++next_anon_label_idx;
294
295                         WriteShortRelativeOffset (start_index, end_index);
296                         WriteLabel (start_index);
297
298                         return end_index;
299                 }
300
301                 public void EndSubsection (object end_index)
302                 {
303                         WriteLabel ((int) end_index);
304                 }
305         }
306 }