[r403]: cmajor++ / Llvm / Llvm.Ir / Constant.cpp  Maximize  Restore  History

Download this file

597 lines (512 with data), 16.1 kB

  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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Llvm.Ir/Constant.hpp>
#include <Llvm.Ir/Type.hpp>
#include <map>
#include <stack>
#include <sstream>
#include <iomanip>
#include <stdexcept>
namespace Llvm { namespace Ir {
using Soul::Util::byte;
using Soul::Util::FloatToString;
using Soul::Util::DoubleToString;
using Soul::Util::LongLongToString;
using Soul::Util::ULongLongToString;
using Soul::Util::UIntToString;
class NullCache;
typedef std::shared_ptr<NullCache> NullCachePtr;
class NullCache
{
public:
static void Init();
static void Done();
static NullCachePtr Instance();
ObjectPtr GetNullObjectFor(TypePtr type);
private:
static NullCachePtr instance;
static std::stack<NullCachePtr> instanceStack;
typedef std::map<TypePtr, ObjectPtr> NullObjectMap;
typedef NullObjectMap::const_iterator NullObjectMapIt;
NullObjectMap nullObjectMap;
std::vector<UniqueObjectPtr> nulls;
};
NullCachePtr NullCache::instance;
std::stack<NullCachePtr> NullCache::instanceStack;
void NullCache::Init()
{
instanceStack.push(instance);
instance = NullCachePtr(new NullCache());
}
void NullCache::Done()
{
instance = instanceStack.top();
instanceStack.pop();
}
NullCachePtr NullCache::Instance()
{
return instance;
}
ObjectPtr NullCache::GetNullObjectFor(TypePtr type)
{
NullObjectMapIt i = nullObjectMap.find(type);
if (i != nullObjectMap.end())
{
return i->second;
}
ObjectPtr null(new NullConstant(type));
nulls.push_back(UniqueObjectPtr(null));
nullObjectMap.insert(std::make_pair(type, null));
return null;
}
char Nibble(byte nibble)
{
const char* n = "0123456789ABCDEF";
return n[nibble];
}
std::string Hex(byte c)
{
std::string hex;
hex.append(1, Nibble(c >> 4)).append(1, Nibble(c & 0x0F));
return hex;
}
std::string MakeStringConstantName(const std::string& s, bool metadataSyntax)
{
std::string name;
if (metadataSyntax)
{
name.append("\"");
}
else
{
name.append("c\"");
}
for (char c : s)
{
if (c != '"' && c != '\\' && c >= 32 && c <= 126)
{
name.append(1, c);
}
else
{
name.append("\\" + Hex(byte(c)));
}
}
if (metadataSyntax)
{
name.append("\"");
}
else
{
name.append("\\00\"");
}
return name;
}
std::string MakeStringConstantName(const std::string& s)
{
return MakeStringConstantName(s, false);
}
class ConstantCache;
typedef std::shared_ptr<ConstantCache> ConstantCachePtr;
class ConstantCache
{
public:
static void Init();
static void Done();
static ConstantCachePtr Instance();
ObjectPtr MakeConstant(const std::string& name, TypePtr type);
ObjectPtr MakeBooleanConstant(bool value);
ObjectPtr MakeI8Constant(int value);
ObjectPtr MakeI16Constant(int value);
ObjectPtr MakeI32Constant(int value);
ObjectPtr MakeUI32Constant(uint value);
ObjectPtr MakeI64Constant(i64 value);
ObjectPtr MakeUI64Constant(ui64 value);
ObjectPtr MakeFloatConstant(float value);
ObjectPtr MakeDoubleConstant(double value);
ObjectPtr MakeCharConstant(char value);
ObjectPtr MakeStringConstant(const std::string& value);
private:
ConstantCache();
static ConstantCachePtr instance;
static std::stack<ConstantCachePtr> instanceStack;
typedef std::map<std::pair<std::string, TypePtr>, ObjectPtr> ConstantMap;
typedef ConstantMap::const_iterator ConstantMapIt;
ConstantMap constantMap;
typedef std::map<int, ObjectPtr> IntConstantMap;
typedef IntConstantMap::const_iterator IntConstantMapIt;
IntConstantMap i8ConstantMap;
IntConstantMap i16ConstantMap;
IntConstantMap i32ConstantMap;
typedef std::map<uint, ObjectPtr> UIntConstantMap;
typedef UIntConstantMap::const_iterator UIntConstantMapIt;
UIntConstantMap ui32ConstantMap;
typedef std::map<i64, ObjectPtr> Int64ConstantMap;
typedef Int64ConstantMap::const_iterator Int64ConstantMapIt;
Int64ConstantMap i64ConstantMap;
typedef std::map<ui64, ObjectPtr> UInt64ConstantMap;
typedef UInt64ConstantMap::const_iterator UInt64ConstantMapIt;
UInt64ConstantMap ui64ConstantMap;
typedef std::map<float, ObjectPtr> FloatConstantMap;
typedef FloatConstantMap::const_iterator FloatConstantMapIt;
FloatConstantMap floatConstantMap;
typedef std::map<double, ObjectPtr> DoubleConstantMap;
typedef DoubleConstantMap::const_iterator DoubleConstantMapIt;
DoubleConstantMap doubleConstantMap;
typedef std::map<char, ObjectPtr> CharConstantMap;
typedef CharConstantMap::const_iterator CharConstantMapIt;
CharConstantMap charConstantMap;
typedef std::map<std::string, ObjectPtr> StringConstantMap;
typedef StringConstantMap::const_iterator StringConstantMapIt;
StringConstantMap stringConstantMap;
std::vector<UniqueObjectPtr> constants;
BooleanConstant trueConstant;
BooleanConstant falseConstant;
};
ConstantCache::ConstantCache(): trueConstant(true), falseConstant(false)
{
trueConstant.SetOwned();
falseConstant.SetOwned();
}
ConstantCachePtr ConstantCache::instance;
std::stack<ConstantCachePtr> ConstantCache::instanceStack;
void ConstantCache::Init()
{
instanceStack.push(instance);
instance = ConstantCachePtr(new ConstantCache());
}
void ConstantCache::Done()
{
instance = instanceStack.top();
instanceStack.pop();
}
ConstantCachePtr ConstantCache::Instance()
{
return instance;
}
ObjectPtr ConstantCache::MakeBooleanConstant(bool value)
{
return value ? &trueConstant : &falseConstant;
}
ObjectPtr ConstantCache::MakeConstant(const std::string& name, TypePtr type)
{
std::pair<std::string, TypePtr> p = std::make_pair(name, type);
ConstantMapIt i = constantMap.find(p);
if (i != constantMap.end())
{
return i->second;
}
ObjectPtr constant = new Constant(name, type);
constant->SetOwned();
constantMap[p] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr CreateConstant(const std::string& name, TypePtr type)
{
return ConstantCache::Instance()->MakeConstant(name, type);
}
ObjectPtr ConstantCache::MakeI8Constant(int value)
{
IntConstantMapIt i = i8ConstantMap.find(value);
if (i != i8ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new I8Constant(value);
constant->SetOwned();
i8ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeI16Constant(int value)
{
IntConstantMapIt i = i16ConstantMap.find(value);
if (i != i16ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new I16Constant(value);
constant->SetOwned();
i16ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeI32Constant(int value)
{
IntConstantMapIt i = i32ConstantMap.find(value);
if (i != i32ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new I32Constant(value);
constant->SetOwned();
i32ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeUI32Constant(uint value)
{
UIntConstantMapIt i = ui32ConstantMap.find(value);
if (i != ui32ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new UI32Constant(value);
constant->SetOwned();
ui32ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeI64Constant(i64 value)
{
Int64ConstantMapIt i = i64ConstantMap.find(value);
if (i != i64ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new I64Constant(value);
constant->SetOwned();
i64ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeUI64Constant(ui64 value)
{
UInt64ConstantMapIt i = ui64ConstantMap.find(value);
if (i != ui64ConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new UI64Constant(value);
constant->SetOwned();
ui64ConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeFloatConstant(float value)
{
FloatConstantMapIt i = floatConstantMap.find(value);
if (i != floatConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new FloatConstant(value);
constant->SetOwned();
floatConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeDoubleConstant(double value)
{
DoubleConstantMapIt i = doubleConstantMap.find(value);
if (i != doubleConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new DoubleConstant(value);
constant->SetOwned();
doubleConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeCharConstant(char value)
{
CharConstantMapIt i = charConstantMap.find(value);
if (i != charConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new CharConstant(value);
constant->SetOwned();
charConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
ObjectPtr ConstantCache::MakeStringConstant(const std::string& value)
{
StringConstantMapIt i = stringConstantMap.find(value);
if (i != stringConstantMap.end())
{
return i->second;
}
ObjectPtr constant = new StringConstant(value);
constant->SetOwned();
stringConstantMap[value] = constant;
constants.push_back(UniqueObjectPtr(constant));
return constant;
}
Constant::Constant(const std::string& valueName_, TypePtr type_): Object(valueName_, type_)
{
}
void Constant::InitTo(Emitter& emitter, TypePtr type, ObjectPtr to)
{
to->InitFrom(emitter, type, *this);
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, Constant& constant)
{
throw std::runtime_error("cannot init constant from constant");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, Global& global)
{
throw std::runtime_error("cannot init constant from global");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, RegVar& regVar)
{
throw std::runtime_error("cannot init constant from register variable");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, StackVar& stackVar)
{
throw std::runtime_error("cannot init constant from stack variable");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, MemberVar& memberVar)
{
throw std::runtime_error("cannot init constant from member variable");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, RefVar& refVar)
{
throw std::runtime_error("cannot init constant from reference variable");
}
void Constant::InitFrom(Emitter& emitter, TypePtr type, Parameter& parameter)
{
throw std::runtime_error("cannot init constant from parameter");
}
void Constant::AssignTo(Emitter& emitter, TypePtr type, ObjectPtr to)
{
to->AssignFrom(emitter, type, *this);
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, Constant& constant)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, Global& global)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, RegVar& regVar)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, StackVar& stackVar)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, MemberVar& memberVar)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, RefVar& refVar)
{
throw std::runtime_error("cannot assign constant");
}
void Constant::AssignFrom(Emitter& emitter, TypePtr type, Parameter& parameter)
{
throw std::runtime_error("cannot assign constant");
}
BooleanConstant::BooleanConstant(bool value_): Constant(value_ ? "true" : "false", I1())
{
}
ObjectPtr True()
{
return ConstantCache::Instance()->MakeBooleanConstant(true);
}
ObjectPtr False()
{
return ConstantCache::Instance()->MakeBooleanConstant(false);
}
ObjectPtr CreateBooleanConstant(bool value)
{
return ConstantCache::Instance()->MakeBooleanConstant(value);
}
CharConstant::CharConstant(char value_): Constant(IntToString(byte(value_)), I8())
{
}
ObjectPtr CreateCharConstant(char value)
{
return ConstantCache::Instance()->MakeCharConstant(value);
}
StringConstant::StringConstant(const std::string& value_): Constant(MakeStringConstantName(value_), TypePtr(CreateStringType(int(value_.length()) + 1)))
{
}
ObjectPtr CreateStringConstant(const std::string& value)
{
return ConstantCache::Instance()->MakeStringConstant(value);
}
NullConstant::NullConstant(TypePtr ptrType_): Constant("null", ptrType_)
{
}
ObjectPtr Null(TypePtr ptrType)
{
return NullCache::Instance()->GetNullObjectFor(ptrType);
}
I8Constant::I8Constant(int value_): Constant(IntToString(value_), I8())
{
}
ObjectPtr CreateI8Constant(int value)
{
return ConstantCache::Instance()->MakeI8Constant(value);
}
I16Constant::I16Constant(int value_): Constant(IntToString(value_), I16())
{
}
ObjectPtr CreateI16Constant(int value)
{
return ConstantCache::Instance()->MakeI16Constant(value);
}
I32Constant::I32Constant(int value_): Constant(IntToString(value_), I32())
{
}
ObjectPtr CreateI32Constant(int value)
{
return ConstantCache::Instance()->MakeI32Constant(value);
}
UI32Constant::UI32Constant(uint value_): Constant(UIntToString(value_), I32())
{
}
ObjectPtr CreateUI32Constant(uint value)
{
return ConstantCache::Instance()->MakeUI32Constant(value);
}
I64Constant::I64Constant(i64 value_): Constant(LongLongToString(value_), I64())
{
}
ObjectPtr CreateI64Constant(i64 value)
{
return ConstantCache::Instance()->MakeI64Constant(value);
}
UI64Constant::UI64Constant(ui64 value_): Constant(ULongLongToString(value_), I64())
{
}
ObjectPtr CreateUI64Constant(ui64 value)
{
return ConstantCache::Instance()->MakeUI64Constant(value);
}
FloatConstant::FloatConstant(float value_): Constant(FloatToString(value_, true), Float())
{
}
ObjectPtr CreateFloatConstant(float value)
{
return ConstantCache::Instance()->MakeFloatConstant(value);
}
DoubleConstant::DoubleConstant(double value_): Constant(DoubleToString(value_, true), Double())
{
}
ObjectPtr CreateDoubleConstant(double value)
{
return ConstantCache::Instance()->MakeDoubleConstant(value);
}
void ConstantInit()
{
NullCache::Init();
ConstantCache::Init();
}
void ConstantDone()
{
ConstantCache::Done();
NullCache::Done();
}
} } // namespace Llvm::Ir