File: Operand.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (572 lines) | stat: -rw-r--r-- 13,905 bytes parent folder | download | duplicates (2)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "stdafx.h"
#include "Operand.h"
#include "Exception.h"
#include "Core/StrBuf.h"
#include "Core/Str.h"
#include <iomanip>

namespace code {

	static Word dual(Nat a, Nat b) {
		return Word(a) << Word(32) | Word(b);
	}

	static Nat firstDual(Word d) {
		return Nat((d >> 32) & 0xFFFFFFFF);
	}

	static Nat secondDual(Word d) {
		return Nat(d & 0xFFFFFFFF);
	}

	using runtime::someEngine;

	Operand::Operand() : opType(opNone), opPtr(null), opNum(0) {}

	Operand::Operand(Reg r) : opType(opRegister), opPtr(null), opNum(r), opSize(code::size(r)) {
		if (r == noReg)
			throw new (someEngine()) InvalidValue(S("noReg is not a proper register!"));
	}

	Operand::Operand(CondFlag c) : opType(opCondFlag), opPtr(null), opNum(c), opSize() {}

	Operand::Operand(Block b) : opType(opBlock), opPtr(null), opNum(b.id), opSize() {
		if (b == Block())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty block."));
	}

	Operand::Operand(Var v) : opType(opVariable), opPtr(null), opNum(v.id), opSize(v.size()) {
		if (v == Var())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty variable."));
	}

	Operand::Operand(Label l) : opType(opLabel), opPtr(null), opNum(l.id), opSize(Size::sPtr) {
		if (l == Label())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty label."));
	}

	Operand::Operand(Ref ref) : opType(opReference), opPtr(ref.to), opNum(0), opSize(Size::sPtr) {}

	Operand::Operand(Reference *ref) : opType(opReference), opPtr(ref->to), opNum(0), opSize(Size::sPtr) {}

	Operand::Operand(SrcPos pos) : opType(opSrcPos), opPtr(pos.file), opNum(), opSize() {
		opNum = Word(pos.start) | (Word(pos.end) << 32);
	}

	Operand::Operand(Word c, Size size) : opType(opConstant), opPtr(null), opNum(c), opSize(size) {}

	Operand::Operand(Size s, Size size) : opType(opDualConstant), opPtr(null), opOffset(s), opSize(size) {}

	Operand::Operand(Offset o, Size size) : opType(opDualConstant), opPtr(null), opOffset(o), opSize(size) {}

	Operand::Operand(RootObject *obj) : opType(opObjReference), opPtr(obj), opOffset(), opSize(Size::sPtr) {}

	Operand::Operand(OffsetRef r, Size size) : opType(opOffReference), opPtr(r.to), opOffset(r.delta), opSize(size) {}

	Operand::Operand(Reg r, Offset offset, Size size) : opType(opRelative), opPtr(null), opNum(r), opOffset(offset), opSize(size) {
		// Note: it is OK if 'r == noReg'. That means absolute addressing.
	}

	Operand::Operand(Reg r, OffsetRef ref, Size size) : opType(opRelative), opPtr(ref.to), opNum(r), opOffset(ref.delta), opSize(size) {
		// Note: it is OK if 'r == noReg'. That means absolute addressing.
	}

	Operand::Operand(Var v, Offset offset, Size size) : opType(opVariable), opPtr(null), opNum(v.id), opOffset(offset), opSize(size) {
		if (v == Var())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty variable."));
	}

	Operand::Operand(Var v, OffsetRef ref, Size size) : opType(opVariable), opPtr(ref.to), opNum(v.id), opOffset(ref.delta), opSize(size) {
		if (v == Var())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty variable."));
	}

	Operand::Operand(Label l, Offset offset, Size size) : opType(opRelativeLbl), opPtr(null), opNum(l.id), opOffset(offset), opSize(size) {
		if (l == Label())
			throw new (someEngine()) InvalidValue(S("Can not create an operand of an empty label."));
	}

	Bool Operand::operator ==(const Operand &o) const {
		if (opType != o.opType)
			return false;

		switch (opType) {
		case opNone:
			return true;
		case opConstant:
		case opRegister:
		case opCondFlag:
		case opBlock:
		case opLabel:
			return opNum == o.opNum;
		case opDualConstant:
			return opOffset == o.opOffset;
		case opRelativeLbl:
			return opNum == o.opNum && opOffset == o.opOffset;
		case opVariable:
		case opRelative:
			return opNum == o.opNum && opOffset == o.opOffset && opPtr == o.opPtr;
		case opReference:
		case opObjReference:
			return opPtr == o.opPtr;
		case opOffReference:
			return opPtr == o.opPtr && opOffset == o.opOffset;
		case opSrcPos:
			if (opNum != o.opNum)
				return false;
			if (opPtr == null && o.opPtr == null)
				return true;
			if (opPtr == null || o.opPtr == null)
				return false;
			return *(Url *)opPtr == *(Url *)o.opPtr;
		default:
			assert(false, L"Unknown type!");
			return false;
		}
	}

	Bool Operand::operator !=(const Operand &o) const {
		return !(*this == o);
	}

	Bool Operand::empty() const {
		return opType == opNone;
	}

	Bool Operand::any() const {
		return !empty();
	}

	OpType Operand::type() const {
		if (opType == opDualConstant)
			return opConstant;
		else
			return opType;
	}

	Size Operand::size() const {
		return opSize;
	}

	Bool Operand::readable() const {
		switch (type()) {
		case opNone:
		case opCondFlag:
		case opBlock:
		case opSrcPos:
			// TODO: Add more returning false here.
			return false;
		default:
			return true;
		}
	}

	Bool Operand::writable() const {
		switch (type()) {
		case opRegister:
		case opRelative:
		case opVariable:
			return true;
		default:
			return false;
		}
	}

	void Operand::ensureReadable(op::OpCode op) const {
		if (!readable()) {
			Engine &e = someEngine();
			Str *msg = TO_S(e, S("For instruction ") << name(op) << S(": ") << *this << S(" is not readable."));
			throw new (e) InvalidValue(msg);
		}
	}

	void Operand::ensureWritable(op::OpCode op) const {
		if (!writable()) {
			Engine &e = someEngine();
			Str *msg = TO_S(e, S("For instruction ") << name(op) << S(": ") << *this << S(" is not writable."));
			throw new (e) InvalidValue(msg);
		}
	}

	Bool Operand::hasRegister() const {
		switch (opType) {
		case opRegister:
		case opRelative:
			return true;
		default:
			return false;
		}
	}

	Word Operand::constant() const {
		if (type() != opConstant)
			throw new (someEngine()) InvalidValue(S("Not a constant!"));
		if (opType == opConstant) {
			return opNum;
		} else {
			return opOffset.current();
		}
	}

	Reg Operand::reg() const {
		if (!hasRegister())
			throw new (someEngine()) InvalidValue(S("Not a register!"));
		return Reg(opNum);
	}

	Offset Operand::offset() const {
		if (type() == opDualConstant)
			return Offset();

		// Nothing bad happens if this is accessed wrongly.
		return opOffset;
	}

	CondFlag Operand::condFlag() const {
		if (type() != opCondFlag)
			throw new (someEngine()) InvalidValue(S("Not a CondFlag!"));
		return CondFlag(opNum);
	}

	Block Operand::block() const {
		if (type() != opBlock)
			throw new (someEngine()) InvalidValue(S("Not a block!"));
		return Block(Nat(opNum));
	}

	Var Operand::var() const {
		if (type() != opVariable)
			throw new (someEngine()) InvalidValue(S("Not a variable!"));
		return Var(Nat(opNum), size());
	}

	Ref Operand::ref() const {
		switch (type()) {
		case opReference:
			return Ref((RefSource *)opPtr);
		default:
			throw new (someEngine()) InvalidValue(S("Not a reference!"));
		}
	}

	RefSource *Operand::refSource() const {
		if (type() != opReference)
			throw new (someEngine()) InvalidValue(S("Not a reference!"));
		return (RefSource *)opPtr;
	}

	Bool Operand::hasOffsetRef() const {
		switch (type()) {
		case opOffReference:
		case opRelative:
		case opVariable:
			return opPtr != null;
		default:
			return false;
		}
	}

	OffsetRef Operand::offsetRef() const {
		switch (type()) {
		case opOffReference:
		case opRelative:
		case opVariable:
			return OffsetRef((OffsetSource *)opPtr, opOffset);
		default:
			throw new (someEngine()) InvalidValue(S("Not a reference!"));
		}
	}

	RootObject *Operand::object() const {
		if (type() != opObjReference)
			throw new (someEngine()) InvalidValue(S("Not an object reference!"));
		return (RootObject *)opPtr;
	}

	MAYBE(Object *) Operand::obj() const {
		if (type() != opObjReference)
			return null;
		return as<Object>((RootObject *)opPtr);
	}

	MAYBE(TObject *) Operand::tObj() const {
		if (type() != opObjReference)
			return null;
		return as<TObject>((RootObject *)opPtr);
	}

	Label Operand::label() const {
		if (type() != opLabel && type() != opRelativeLbl)
			throw new (someEngine()) InvalidValue(S("Not a label!"));
		return Label(Nat(opNum));
	}

	SrcPos Operand::srcPos() const {
		if (type() != opSrcPos)
			throw new (someEngine()) InvalidValue(S("Not a SrcPos!"));
		Nat start = opNum & 0xFFFFFFFF;
		Nat end = opNum >> 32;
		return SrcPos((Url *)opPtr, start, end);
	}

	Operand Operand::replaceRegister(Reg replace) const {
		if (type() == opRegister)
			return Operand(asSize(replace, size()));
		else if (type() == opRelative)
			return Operand(asSize(replace, Size::sPtr), opOffset, opSize);
		else
			return *this;
	}

	wostream &operator <<(wostream &to, const Operand &o) {
		if (o.type() != opRegister
			&& o.type() != opCondFlag
			&& o.type() != opNone
			&& o.type() != opBlock
			&& o.type() != opSrcPos) {

			Size s = o.size();
			if (s == Size::sPtr)
				to << L"p";
			else if (s == Size::sByte)
				to << L"b";
			else if (s == Size::sInt)
				to << L"i";
			else if (s == Size::sLong)
				to << L"l";
			else
				to << L"(" << s << L")";
		}

		switch (o.type()) {
		case opNone:
			return to << L"<none>";
		case opConstant:
			if (o.opType == opDualConstant) {
				return to << o.opOffset;
			} else {
				if (o.size() == Size::sPtr) {
					return to << L"0x" << toHex(o.constant());
				} else {
					return to << o.constant();
				}
			}
		case opRegister:
			return to << code::name(o.reg());
		case opRelative:
			if (o.opPtr)
				return to << L"[" << code::name(o.reg()) << o.offsetRef() << L"]";
			else
				return to << L"[" << code::name(o.reg()) << o.offset() << L"]";
		case opRelativeLbl:
			return to << L"[" << L"Label" << o.opNum << o.offset() << L"]";
		case opVariable:
			if (o.opPtr) {
				return to << L"[Var" << o.opNum << L"+" << o.offsetRef() << L"]";
			} if (o.offset() != Offset()) {
				return to << L"[Var" << o.opNum << o.offset() << L"]";
			} else {
				return to << L"[Var" << o.opNum << L"]";
			}
		case opLabel:
			return to << L"Label" << o.opNum;
		case opBlock:
			return to << L"Block" << o.opNum;
		case opReference:
			return to << L"@" << o.ref().title();
		case opObjReference:
			if (o.object()) {
				return to << L"&'" << threadSafeToS(o.object())->c_str() << L"'";
			} else {
				return to << L"&null";
			}
		case opOffReference:
			return to << o.offsetRef();
		case opCondFlag:
			return to << code::name(o.condFlag());
		case opSrcPos:
			return to << o.srcPos();
		default:
			assert(false, L"Unknown type!");
			return to << L"<invalid>";
		}
	}

	void Operand::toS(StrBuf *to) const {
		if (type() != opRegister
			&& type() != opCondFlag
			&& type() != opNone
			&& type() != opBlock
			&& type() != opSrcPos) {

			Size s = size();
			if (s == Size::sPtr)
				*to << S("p");
			else if (s == Size::sByte)
				*to << S("b");
			else if (s == Size::sInt)
				*to << S("i");
			else if (s == Size::sLong)
				*to << S("l");
			else
				*to << S("(") << s << S(")");
		}

		switch (type()) {
		case opNone:
			*to << S("<none>");
			return;
		case opConstant:
			if (opType == opDualConstant) {
				*to << opOffset;
			} else {
				if (size() == Size::sPtr) {
					*to << S("0x") << hex(constant());
				} else {
					*to << constant();
				}
			}
			return;
		case opRegister:
			*to << code::name(reg());
			return;
		case opRelative:
			if (opPtr)
				*to << S("[") << code::name(reg()) << S("+") << offsetRef() << S("]");
			else
				*to << S("[") << code::name(reg()) << offset() << S("]");
			return;
		case opRelativeLbl:
			*to << S("[") << L"Label" << opNum << offset() << S("]");
			return;
		case opVariable:
			if (opPtr) {
				*to << S("[Var") << opNum << offsetRef() << S("]");
			} else if (offset() != Offset()) {
				*to << S("[Var") << opNum << offset() << S("]");
			} else {
				*to << S("[Var") << opNum << S("]");
			}
			return;
		case opLabel:
			*to << S("Label") << opNum;
			return;
		case opBlock:
			*to << S("Block") << opNum;
			return;
		case opReference:
			*to << S("@") << ref().title();
			return;
		case opObjReference:
			if (object()) {
				*to << S("&'") << threadSafeToS(object()) << S("'");
			} else {
				*to << S("&null");
			}
			return;
		case opOffReference:
			*to << offsetRef();
			return;
		case opCondFlag:
			*to << code::name(condFlag());
			return;
		case opSrcPos:
			*to << srcPos();
			return;
		default:
			*to << S("<invalid>");
			return;
		}
	}

	void Operand::dbg_dump() const {
		const nat *raw = (const nat *)this;
		for (nat i = 0; i < sizeof(Operand)/4; i++) {
			std::wcout << std::setw(2) << i << L": " << toHex(raw[i]) << endl;
		}
	}


	/**
	 * Create things:
	 */

	Operand byteConst(Byte v) {
		return xConst(Size::sByte, Word(v));
	}

	Operand intConst(Int v) {
		return xConst(Size::sInt, Long(v));
	}

	Operand intConst(Offset v) {
		return Operand(v, Size::sInt);
	}

	Operand intConst(OffsetRef v) {
		return Operand(v, Size::sInt);
	}

	Operand natConst(Nat v) {
		return xConst(Size::sNat, Word(v));
	}

	Operand natConst(Size v) {
		return Operand(v, Size::sNat);
	}

	Operand longConst(Long v) {
		return xConst(Size::sLong, v);
	}

	Operand wordConst(Word v) {
		return xConst(Size::sWord, v);
	}

	Operand floatConst(Float v) {
		union {
			Nat i;
			Float f;
		} convert;
		convert.f = v;
		return xConst(Size::sFloat, convert.i);
	}

	Operand doubleConst(Double v) {
		union {
			Word i;
			Double f;
		} convert;
		convert.f = v;
		return xConst(Size::sDouble, convert.i);
	}

	Operand ptrConst(Size v) {
		return Operand(v, Size::sPtr);
	}

	Operand ptrConst(Offset v) {
		return Operand(v, Size::sPtr);
	}

	Operand ptrConst(OffsetRef v) {
		return Operand(v, Size::sPtr);
	}

	Operand ptrConst(Nat v) {
		return xConst(Size::sPtr, v);
	}

	Operand xConst(Size s, Word v) {
		return Operand(v, s);
	}

	Operand objPtr(Object *o) {
		return Operand(o);
	}

	Operand objPtr(TObject *o) {
		return Operand(o);
	}

}