Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AssemblerWriterI386.cs « Mono.CSharp.Debugger « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58ce935bdd0e909e0159ccb9c257ee2bb647f005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// Mono.CSharp.Debugger/AssemblerWriterI386.cs
//
// Author:
//   Martin Baulig (martin@gnome.org)
//
// IAssemblerWriter implementation for the Intel i386.
//
// (C) 2002 Ximian, Inc.  http://www.ximian.com
//

using System;
using System.IO;
	
namespace Mono.CSharp.Debugger
{
	public class AssemblerWriterI386 : IAssemblerWriter
	{
		public AssemblerWriterI386 (StreamWriter writer) {
			this.writer = writer;
			writer.WriteLine ("#NOAPP");
		}

		private StreamWriter writer;
		private static int next_anon_label_idx = 0;
		private bool in_section = false;

		public void WriteLabel (string label)
		{
			writer.WriteLine (".L_" + label + ":");
		}

		public int GetNextLabelIndex ()
		{
			return ++next_anon_label_idx;
		}

		public int WriteLabel ()
		{
			int index = ++next_anon_label_idx;

			WriteLabel (index);

			return index;
		}

		public void WriteLabel (int index)
		{
			char[] output = { '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  ':', '\n' };

			unchecked {
				int value = (int) index;
				output [3] = hex [(value & 0xf00000) >> 20];
				output [4] = hex [(value & 0x0f0000) >> 16];
				output [5] = hex [(value & 0x00f000) >> 12];
				output [6] = hex [(value & 0x000f00) >>  8];
				output [7] = hex [(value & 0x0000f0) >>  4];
				output [8] = hex [(value & 0x00000f)];
			}

			writer.Write (output, 0, output.Length);
		}

		private static readonly char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7',
						       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		public void WriteUInt8 (bool value)
		{
			WriteUInt8 (value ? 1 : 0);
		}

		public void WriteUInt8 (int value)
		{
			char[] output = { '\t', '.', 'b', 'y', 't', 'e', ' ',
					  '0', 'x', '\0', '\0',
					  '\n' };

			unchecked {
				output [9] = hex [(value & 0xf0) >> 4];
				output [10] = hex [value & 0x0f];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteInt8 (int value)
		{
			char[] output = { '\t', '.', 'b', 'y', 't', 'e', ' ',
					  '0', 'x', '\0', '\0',
					  '\n' };

			unchecked {
				uint uvalue = (uint) value;
				output [9] = hex [(uvalue & 0xf0) >> 4];
				output [10] = hex [uvalue & 0x0f];
			}

			writer.Write (output, 0, output.Length);
		}

		public void Write2Bytes (int a, int b)
		{
			char[] output = { '\t', '.', 'b', 'y', 't', 'e', ' ',
					  '0', 'x', '\0', '\0', ',', ' ',
					  '0', 'x', '\0', '\0',
					  '\n' };

			unchecked {
				uint ua = (uint) a;
				uint ub = (uint) b;
				output [9] = hex [(ua & 0xf0) >> 4];
				output [10] = hex [ua & 0x0f];
				output [15] = hex [(ub & 0xf0) >> 4];
				output [16] = hex [ub & 0x0f];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteUInt16 (int value)
		{
			writer.WriteLine ("\t.word " + value);
		}

		public void WriteInt16 (int value)
		{
			writer.WriteLine ("\t.word " + value);
		}

		public void WriteUInt32 (int value)
		{
			char[] output = { '\t', '.', 'l', 'o', 'n', 'g', ' ',
					  '0', 'x', '\0', '\0', '\0', '\0', '\0', '\0',
					  '\0', '\0', '\n' };

			unchecked {
				output [9] = hex [(value & 0xf0000000) >> 28];
				output [10] = hex [(value & 0x0f000000) >> 24];
				output [11] = hex [(value & 0x00f00000) >> 20];
				output [12] = hex [(value & 0x000f0000) >> 16];
				output [13] = hex [(value & 0x0000f000) >> 12];
				output [14] = hex [(value & 0x00000f00) >>  8];
				output [15] = hex [(value & 0x000000f0) >>  4];
				output [16] = hex [(value & 0x0000000f)];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteInt32 (int value)
		{
			char[] output = { '\t', '.', 'l', 'o', 'n', 'g', ' ',
					  '0', 'x', '\0', '\0', '\0', '\0', '\0', '\0',
					  '\0', '\0', '\n' };

			unchecked {
				uint uvalue = (uint) value;
				output [9] = hex [(uvalue & 0xf0000000) >> 28];
				output [10] = hex [(uvalue & 0x0f000000) >> 24];
				output [11] = hex [(uvalue & 0x00f00000) >> 20];
				output [12] = hex [(uvalue & 0x000f0000) >> 16];
				output [13] = hex [(uvalue & 0x0000f000) >> 12];
				output [14] = hex [(uvalue & 0x00000f00) >>  8];
				output [15] = hex [(uvalue & 0x000000f0) >>  4];
				output [16] = hex [(uvalue & 0x0000000f)];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteSLeb128 (int value)
		{
			writer.WriteLine ("\t.sleb128 " + value);
		}

		public void WriteULeb128 (int value)
		{
			writer.WriteLine ("\t.uleb128 " + value);
		}

		public void WriteAddress (int value)
		{
			if (value == 0)
				writer.WriteLine ("\t.long 0");
			else
				writer.WriteLine ("\t.long " + value);
		}

		public void WriteString (string value)
		{
			writer.WriteLine ("\t.string \"" + value + "\"");
		}

		public void WriteSectionStart (String section)
		{
			if (in_section)
				throw new Exception ();
			in_section = true;
			writer.WriteLine ("\t.section ." + section);
		}

		public void WriteSectionEnd ()
		{
			in_section = false;
		}

		public void WriteRelativeOffset (int start_label, int end_label)
		{
			char[] output = { '\t', '.', 'l', 'o', 'n', 'g', ' ',
					  '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  ' ', '-', ' ',
					  '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  '\n' };

			unchecked {
				output [10] = hex [(end_label & 0xf00000) >> 20];
				output [11] = hex [(end_label & 0x0f0000) >> 16];
				output [12] = hex [(end_label & 0x00f000) >> 12];
				output [13] = hex [(end_label & 0x000f00) >>  8];
				output [14] = hex [(end_label & 0x0000f0) >>  4];
				output [15] = hex [(end_label & 0x00000f)];
				output [22] = hex [(start_label & 0xf00000) >> 20];
				output [23] = hex [(start_label & 0x0f0000) >> 16];
				output [24] = hex [(start_label & 0x00f000) >> 12];
				output [25] = hex [(start_label & 0x000f00) >>  8];
				output [26] = hex [(start_label & 0x0000f0) >>  4];
				output [27] = hex [(start_label & 0x00000f)];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteShortRelativeOffset (int start_label, int end_label)
		{
			char[] output = { '\t', '.', 'b', 'y', 't', 'e', ' ',
					  '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  ' ', '-', ' ',
					  '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  '\n' };

			unchecked {
				output [10] = hex [(end_label & 0xf00000) >> 20];
				output [11] = hex [(end_label & 0x0f0000) >> 16];
				output [12] = hex [(end_label & 0x00f000) >> 12];
				output [13] = hex [(end_label & 0x000f00) >>  8];
				output [14] = hex [(end_label & 0x0000f0) >>  4];
				output [15] = hex [(end_label & 0x00000f)];
				output [22] = hex [(start_label & 0xf00000) >> 20];
				output [23] = hex [(start_label & 0x0f0000) >> 16];
				output [24] = hex [(start_label & 0x00f000) >> 12];
				output [25] = hex [(start_label & 0x000f00) >>  8];
				output [26] = hex [(start_label & 0x0000f0) >>  4];
				output [27] = hex [(start_label & 0x00000f)];
			}

			writer.Write (output, 0, output.Length);
		}

		public void WriteAbsoluteOffset (string label)
		{
			writer.WriteLine ("\t.long .L_" + label);
		}

		public void WriteAbsoluteOffset (int index)
		{
			char[] output = { '\t', '.', 'l', 'o', 'n', 'g', ' ',
					  '.', 'L', '_', '\0', '\0', '\0', '\0', '\0', '\0',
					  '\n' };

			unchecked {
				output [10] = hex [(index & 0xf00000) >> 20];
				output [11] = hex [(index & 0x0f0000) >> 16];
				output [12] = hex [(index & 0x00f000) >> 12];
				output [13] = hex [(index & 0x000f00) >>  8];
				output [14] = hex [(index & 0x0000f0) >>  4];
				output [15] = hex [(index & 0x00000f)];
			}

			writer.Write (output, 0, output.Length);

		}

		public object StartSubsectionWithSize ()
		{
			int start_index = ++next_anon_label_idx;
			int end_index = ++next_anon_label_idx;

			WriteRelativeOffset (start_index, end_index);
			WriteLabel (start_index);

			return end_index;
		}

		public object StartSubsectionWithShortSize ()
		{
			int start_index = ++next_anon_label_idx;
			int end_index = ++next_anon_label_idx;

			WriteShortRelativeOffset (start_index, end_index);
			WriteLabel (start_index);

			return end_index;
		}

		public void EndSubsection (object end_index)
		{
			WriteLabel ((int) end_index);
		}
	}
}