annotate ir/irstruct.cpp @ 1253:752bed475b75

Fixed classinfo.interfaces for .. interfaces!
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Tue, 21 Apr 2009 20:19:53 +0200
parents 465a77c904d4
children 747fdd9245d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
1 #include "gen/llvm.h"
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
2
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
3 #include "mtype.h"
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
4 #include "aggregate.h"
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
5 #include "declaration.h"
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
6 #include "init.h"
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
7
157
5c17f81fc1c1 [svn r173] moved IR state previously stored in Type into IrType and a Type->IrType map; fixes #7
ChristianK
parents: 137
diff changeset
8 #include "gen/irstate.h"
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
9 #include "gen/tollvm.h"
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
10 #include "gen/logger.h"
800
d14e4594c7d7 Changed aggregate field initializers to be created lazily, fixes problem with static void arrays in aggregates.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 797
diff changeset
11 #include "gen/llvmhelpers.h"
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
12 #include "gen/utils.h"
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
13
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
14 #include "ir/irstruct.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
15 #include "ir/irtypeclass.h"
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
16
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
17 #include <algorithm>
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
18
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
19 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
20
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
21 IrStruct::IrStruct(AggregateDeclaration* aggr)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
22 : diCompositeType(NULL)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
23 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
24 aggrdecl = aggr;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
25
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
26 type = aggr->type;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
27
1231
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
28 packed = (type->ty == Tstruct)
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
29 ? type->alignsize() == 1
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
30 : false;
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
31
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
32 // above still need to be looked at
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
33
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
34 init = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
35 constInit = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
36
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
37 vtbl = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
38 constVtbl = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
39 classInfo = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
40 constClassInfo = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
41
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
42 classInterfacesArray = NULL;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
43 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
44
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
45 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
46
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
47 LLGlobalVariable * IrStruct::getInitSymbol()
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
48 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
49 if (init)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
50 return init;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
51
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
52 // create the initZ symbol
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
53 std::string initname("_D");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
54 initname.append(aggrdecl->mangle());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
55 initname.append("6__initZ");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
56
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
57 llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
58
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
59 init = new llvm::GlobalVariable(
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
60 type->irtype->getPA().get(), true, _linkage, NULL, initname, gIR->module);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
61
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
62 return init;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
63 }
137
ce7b81fb957f [svn r141] fixed more problems with classinfo
lindquist
parents: 136
diff changeset
64
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
65 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
66
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
67 llvm::Constant * IrStruct::getDefaultInit()
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
68 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
69 if (constInit)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
70 return constInit;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
71
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
72 if (type->ty == Tstruct)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
73 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
74 constInit = createStructDefaultInitializer();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
75 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
76 else
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
77 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
78 constInit = createClassDefaultInitializer();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
79 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
80
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
81 return constInit;
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
82 }
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
83
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
84 //////////////////////////////////////////////////////////////////////////////
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
85 //////////////////////////////////////////////////////////////////////////////
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
86 //////////////////////////////////////////////////////////////////////////////
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
87
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
88 // helper function that returns the static default initializer of a variable
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
89 LLConstant* get_default_initializer(VarDeclaration* vd, Initializer* init)
136
0e28624814e8 [svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
diff changeset
90 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
91 if (init)
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
92 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
93 return DtoConstInitializer(init->loc, vd->type, init);
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
94 }
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
95 else if (vd->init)
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
96 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
97 return DtoConstInitializer(vd->init->loc, vd->type, vd->init);
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
98 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
99 else
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
100 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
101 return DtoConstExpInit(vd->loc, vd->type, vd->type->defaultInit(vd->loc));
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
102 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
103 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
104
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
105 // helper function that adds zero bytes to a vector of constants
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
106 size_t add_zeros(std::vector<llvm::Constant*>& constants, size_t diff)
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
107 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
108 size_t n = constants.size();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
109 while (diff)
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
110 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
111 if (global.params.is64bit && diff % 8 == 0)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
112 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
113 constants.push_back(llvm::Constant::getNullValue(llvm::Type::Int64Ty));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
114 diff -= 8;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
115 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
116 else if (diff % 4 == 0)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
117 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
118 constants.push_back(llvm::Constant::getNullValue(llvm::Type::Int32Ty));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
119 diff -= 4;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
120 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
121 else if (diff % 2 == 0)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
122 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
123 constants.push_back(llvm::Constant::getNullValue(llvm::Type::Int16Ty));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
124 diff -= 2;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
125 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
126 else
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
127 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
128 constants.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
129 diff -= 1;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
130 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
131 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
132 return constants.size() - n;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
133 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
134
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
135 // Matches the way the type is built in IrTypeStruct
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
136 // maybe look at unifying the interface.
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
137
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
138 LLConstant * IrStruct::createStructDefaultInitializer()
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
139 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
140 IF_LOG Logger::println("Building default initializer for %s", aggrdecl->toPrettyChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
141 LOG_SCOPE;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
142
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
143 assert(type->ty == Tstruct && "cannot build struct default initializer for non struct type");
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
144
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
145 // start at offset zero
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
146 size_t offset = 0;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
147
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
148 // vector of constants
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
149 std::vector<llvm::Constant*> constants;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
150
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
151 // go through fields
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
152 ArrayIter<VarDeclaration> it(aggrdecl->fields);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
153 for (; !it.done(); it.next())
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
154 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
155 VarDeclaration* vd = it.get();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
156
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
157 if (vd->offset < offset)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
158 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
159 IF_LOG Logger::println("skipping field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
160 continue;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
161 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
162
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
163 IF_LOG Logger::println("using field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
164
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
165 // get next aligned offset for this field
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
166 size_t alignedoffset = offset;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
167 if (!packed)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
168 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
169 size_t alignsize = vd->type->alignsize();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
170 alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
171 }
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
172
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
173 // insert explicit padding?
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
174 if (alignedoffset < vd->offset)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
175 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
176 add_zeros(constants, vd->offset - alignedoffset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
177 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
178
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
179 // add initializer
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
180 constants.push_back(get_default_initializer(vd, NULL));
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
181
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
182 // advance offset to right past this field
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
183 offset = vd->offset + vd->type->size();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
184 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
185
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
186 // tail padding?
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
187 if (offset < aggrdecl->structsize)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
188 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
189 add_zeros(constants, aggrdecl->structsize - offset);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
190 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
191
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
192 // build constant struct
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
193 llvm::Constant* definit = llvm::ConstantStruct::get(constants, packed);
1245
465a77c904d4 Fixed all issues preventing Tango 0.99.8 to compile with `sh build-tango.sh --verbose ldc'.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1240
diff changeset
194 #if 0
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
195 IF_LOG Logger::cout() << "final default initializer: " << *definit << std::endl;
1245
465a77c904d4 Fixed all issues preventing Tango 0.99.8 to compile with `sh build-tango.sh --verbose ldc'.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1240
diff changeset
196 #endif
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
197
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
198 // sanity check
1231
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
199 if (definit->getType() != type->irtype->get())
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
200 {
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
201 assert(0 && "default initializer type does not match the default struct type");
212ec2d9d176 Fixed some minitest regressions.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1228
diff changeset
202 }
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
203
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
204 return definit;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
205 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
206
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
207 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
208 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
209 //////////////////////////////////////////////////////////////////////////////
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
210
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
211 // yet another rewrite of the notorious StructInitializer.
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
212
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
213 typedef std::pair<VarDeclaration*, llvm::Constant*> VCPair;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
214
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
215 bool struct_init_data_sort(const VCPair& a, const VCPair& b)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
216 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
217 return (a.first && b.first)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
218 ? a.first->offset < b.first->offset
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
219 : false;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
220 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
221
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
222 // this time a bit more inspired by the DMD code.
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
223
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
224 LLConstant * IrStruct::createStructInitializer(StructInitializer * si)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
225 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
226 IF_LOG Logger::println("Building StructInitializer of type %s", si->ad->toPrettyChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
227 LOG_SCOPE;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
228
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
229 // sanity check
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
230 assert(si->ad == aggrdecl && "struct type mismatch");
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
231 assert(si->vars.dim == si->value.dim && "inconsistent StructInitializer");
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
232
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
233 // array of things to build
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
234 llvm::SmallVector<VCPair, 16> data(aggrdecl->fields.dim);
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
235
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
236 // start by creating a map from initializer indices to field indices.
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
237 // I have no fucking clue why dmd represents it like this :/
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
238 size_t n = si->vars.dim;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
239 LLSmallVector<int, 16> datamap(n, 0);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
240 for (size_t i = 0; i < n; i++)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
241 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
242 for (size_t j = 0; 1; j++)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
243 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
244 assert(j < aggrdecl->fields.dim);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
245 if (aggrdecl->fields.data[j] == si->vars.data[i])
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
246 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
247 datamap[i] = j;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
248 break;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
249 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
250 }
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
251 }
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
252
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
253 // fill in explicit initializers
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
254 n = si->vars.dim;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
255 for (size_t i = 0; i < n; i++)
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
256 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
257 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i];
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
258 Initializer* ini = (Initializer*)si->value.data[i];
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
259
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
260 size_t idx = datamap[i];
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
261
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
262 if (data[idx].first != NULL)
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
263 {
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
264 Loc l = ini ? ini->loc : si->loc;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
265 error(l, "duplicate initialization of %s", vd->toChars());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
266 continue;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
267 }
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
268
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
269 IF_LOG Logger::println("Explicit initializer: %s @+%u", vd->toChars(), vd->offset);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
270 LOG_SCOPE;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
271
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
272 data[idx].first = vd;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
273 data[idx].second = get_default_initializer(vd, ini);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
274 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
275
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
276 // fill in implicit initializers
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
277 n = data.size();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
278 for (size_t i = 0; i < n; i++)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
279 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
280 VarDeclaration* vd = data[i].first;
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
281 if (vd)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
282 continue;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
283
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
284 vd = (VarDeclaration*)aggrdecl->fields.data[i];
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
285
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
286 unsigned vd_begin = vd->offset;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
287 unsigned vd_end = vd_begin + vd->type->size();
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
288
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
289 // make sure it doesn't overlap any explicit initializers.
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
290 VarDeclarationIter it(aggrdecl->fields);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
291 bool overlaps = false;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
292 size_t j = 0;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
293 for (; it.more(); it.next(), j++)
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
294 {
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
295 if (i == j || !data[j].first)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
296 continue;
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
297
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
298 unsigned f_begin = it->offset;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
299 unsigned f_end = f_begin + it->type->size();
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
300
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
301 if (vd_begin >= f_end || vd_end <= f_begin)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
302 continue;
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
303
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
304 overlaps = true;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
305 break;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
306 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
307 // add if no overlap found
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
308 if (!overlaps)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
309 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
310 IF_LOG Logger::println("Implicit initializer: %s @+%u", vd->toChars(), vd->offset);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
311 LOG_SCOPE;
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
312
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
313 data[i].first = vd;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
314 data[i].second = get_default_initializer(vd, NULL);
797
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
315 }
340acf1535d0 Removed KDevelop3 project files, CMake can generate them just fine!
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 737
diff changeset
316 }
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
317
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
318 // stop if there were errors
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
319 if (global.errors)
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
320 {
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
321 fatal();
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
322 }
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
323
1240
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
324 // sort data array by offset
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
325 std::sort(data.begin(), data.end(), struct_init_data_sort);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
326
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
327 // build array of constants and make sure explicit zero padding is inserted when necessary.
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
328 size_t offset = 0;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
329 std::vector<llvm::Constant*> constants;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
330 constants.reserve(n);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
331
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
332 for (size_t i = 0; i < n; i++)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
333 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
334 VarDeclaration* vd = data[i].first;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
335 if (vd == NULL)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
336 continue;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
337
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
338 // get next aligned offset for this field
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
339 size_t alignedoffset = offset;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
340 if (!packed)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
341 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
342 size_t alignsize = vd->type->alignsize();
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
343 alignedoffset = (offset + alignsize - 1) & ~(alignsize - 1);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
344 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
345
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
346 // insert explicit padding?
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
347 if (alignedoffset < vd->offset)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
348 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
349 size_t diff = vd->offset - alignedoffset;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
350 IF_LOG Logger::println("adding %zu bytes zero padding", diff);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
351 add_zeros(constants, diff);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
352 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
353
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
354 IF_LOG Logger::println("adding field %s", vd->toChars());
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
355
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
356 constants.push_back(data[i].second);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
357 offset = vd->offset + vd->type->size();
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
358 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
359
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
360 // tail padding?
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
361 if (offset < aggrdecl->structsize)
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
362 {
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
363 size_t diff = aggrdecl->structsize - offset;
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
364 IF_LOG Logger::println("adding %zu bytes zero padding", diff);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
365 add_zeros(constants, diff);
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
366 }
f295e51d2dd0 Fixed static struct initializers.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 1231
diff changeset
367
1228
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
368 // build constant
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
369 assert(!constants.empty());
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
370 llvm::Constant* c = llvm::ConstantStruct::get(&constants[0], constants.size(), packed);
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
371 IF_LOG Logger::cout() << "final struct initializer: " << *c << std::endl;
79758fd2f48a Added Doxygen file.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents: 946
diff changeset
372 return c;
737
041c1596d217 Removed warnings on ignored aligns. Only do aligment on packed structs, align(1) struct Packed { ... }
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 435
diff changeset
373 }