blob: 84cf410fe8c6aaf182d5d282095a4b7229593c11 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceTypes.cpp - Primitive type properties ---------------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a few attributes of Subzero primitive types.
11//
12//===----------------------------------------------------------------------===//
13
14#include "IceDefs.h"
15#include "IceTypes.h"
16
17namespace Ice {
18
19namespace {
20
21const struct {
22 size_t TypeWidthInBytes;
23 size_t TypeAlignInBytes;
24 const char *DisplayString;
25} TypeAttributes[] = {
26#define X(tag, size, align, str) \
27 { size, align, str } \
28 ,
29 ICETYPE_TABLE
30#undef X
31 };
32
33const size_t TypeAttributesSize =
34 sizeof(TypeAttributes) / sizeof(*TypeAttributes);
35
36} // end anonymous namespace
37
38size_t typeWidthInBytes(Type Ty) {
39 size_t Width = 0;
40 size_t Index = static_cast<size_t>(Ty);
41 if (Index < TypeAttributesSize) {
42 Width = TypeAttributes[Index].TypeWidthInBytes;
43 } else {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070044 llvm_unreachable("Invalid type for typeWidthInBytes()");
Jim Stichnothf7c9a142014-04-29 10:52:43 -070045 }
46 return Width;
47}
48
49size_t typeAlignInBytes(Type Ty) {
50 size_t Align = 0;
51 size_t Index = static_cast<size_t>(Ty);
52 if (Index < TypeAttributesSize) {
53 Align = TypeAttributes[Index].TypeAlignInBytes;
54 } else {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070055 llvm_unreachable("Invalid type for typeAlignInBytes()");
Jim Stichnothf7c9a142014-04-29 10:52:43 -070056 }
57 return Align;
58}
59
60// ======================== Dump routines ======================== //
61
62template <> Ostream &operator<<(Ostream &Str, const Type &Ty) {
63 size_t Index = static_cast<size_t>(Ty);
64 if (Index < TypeAttributesSize) {
65 Str << TypeAttributes[Index].DisplayString;
66 } else {
67 Str << "???";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070068 llvm_unreachable("Invalid type for printing");
Jim Stichnothf7c9a142014-04-29 10:52:43 -070069 }
70
71 return Str;
72}
73
74} // end of namespace Ice