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
|
diff -rc aterm-1142707243.10633/aterm/aterm.c aterm/aterm/aterm.c
*** aterm-1142707243.10633/aterm/aterm.c 2006-02-08 11:35:28.000000000 +0100
--- aterm/aterm/aterm.c 2006-04-25 17:10:52.000000000 +0200
***************
*** 193,198 ****
--- 193,199 ----
/* that have char == 2 bytes, and sizeof(header_type) == 2 */
assert(sizeof(header_type) == sizeof(ATerm *));
assert(sizeof(header_type) >= 4);
+ assert(sizeof(ATerm) == sizeof(MachineWord));
/*}}} */
/*{{{ Initialize buffer */
diff -rc aterm-1142707243.10633/aterm/memory.c aterm/aterm/memory.c
*** aterm-1142707243.10633/aterm/memory.c 2006-03-09 15:02:56.000000000 +0100
--- aterm/aterm/memory.c 2006-04-25 18:22:00.000000000 +0200
***************
*** 119,130 ****
hash_number(tmp,3))
*/
#define HASHNUMBER3(t)\
! FINISH(COMBINE(START(((MachineWord*)t)[0]), ((MachineWord*)t)[2]))
#define HASHNUMBER4(t)\
! FINISH(COMBINE(COMBINE(START(((MachineWord*)t)[0]), \
! ((MachineWord*)t)[2]),((MachineWord*)t)[3]))
#define HASHINT(val) \
FINISH(COMBINE(START( (AT_INT<<SHIFT_TYPE) ), val))
--- 119,171 ----
hash_number(tmp,3))
*/
+ /* The ATerm library use some heavy aliasing. For instance, the
+ various ATermXXX structures are referenced through MachineWord
+ arrays. This is not generally allowed by the C standard --- see
+ C99, section 6.5, clause 7. In particular, this means that you
+ cannot assign something through an ATermXXX pointer, e.g.,
+
+ protoAppl->header = header;
+
+ and then read it through a MachineWord*, e.g.,
+
+ hnr = hash_number((ATerm) protoAppl, 2);
+
+ (hash_number walks over the term by casting it to a MachineWord*).
+
+ However, the same clause of the C standard also specifies that you
+ *can* read the memory location through a union type that contains
+ both the original type (e.g. ATermAppl) and the type used to read
+ the memory location (e.g. MachineWord). That's what we do
+ below: we have a union of all the types that occur in the various
+ ATerm types. We then read the "w" element of the union. The
+ compiler is not allowed to assume absence of aliasing with the
+ other types in the union.
+
+ A better solution would be to hash the term through a character
+ pointer (since *any* memory location can be legally read as a
+ character), but I'm too lazy right now. Performance might also
+ suffer if we do that. */
+
+ typedef union
+ {
+ MachineWord w;
+ header_type header;
+ ATerm term;
+ ATermList list;
+ int i;
+ double d;
+ void* p;
+ } Aliaser;
+
+ #define GET_WORD(t, n) (((Aliaser*) (((MachineWord*) t) + n))->w)
+
#define HASHNUMBER3(t)\
! FINISH(COMBINE(START(GET_WORD(t, 0)), GET_WORD(t, 2)))
#define HASHNUMBER4(t)\
! FINISH(COMBINE(COMBINE(START(GET_WORD(t, 0)), \
! GET_WORD(t, 2)), GET_WORD(t, 3)))
#define HASHINT(val) \
FINISH(COMBINE(START( (AT_INT<<SHIFT_TYPE) ), val))
***************
*** 132,144 ****
#endif /* HASHPEM */
! #define PROTO_APPL_ARGS ((ATerm *) (protoTerm + ARG_OFFSET))
#define SET_PROTO_APPL_ARG(i, a) \
! (PROTO_APPL_ARGS[(i)] = (a))
#define GET_PROTO_APPL_ARG(i) \
! (PROTO_APPL_ARGS[(i)])
#define CHECK_TERM(t) \
assert((t) != NULL \
--- 173,185 ----
#endif /* HASHPEM */
! #define PROTO_APPL_ARGS (protoTerm + ARG_OFFSET)
#define SET_PROTO_APPL_ARG(i, a) \
! (PROTO_APPL_ARGS[(i)] = (MachineWord) (a))
#define GET_PROTO_APPL_ARG(i) \
! ((ATerm) PROTO_APPL_ARGS[(i)])
#define CHECK_TERM(t) \
assert((t) != NULL \
***************
*** 323,336 ****
#else
static HashNumber hash_number(ATerm t, int size)
{
- MachineWord *words = (MachineWord *) t;
int i;
HashNumber hnr;
! hnr = START(HIDE_AGE_MARK(words[0]));
for (i=2; i<size; i++) {
! hnr = COMBINE(hnr, words[i]);
}
return FINISH(hnr);
--- 364,376 ----
#else
static HashNumber hash_number(ATerm t, int size)
{
int i;
HashNumber hnr;
! hnr = START(HIDE_AGE_MARK(GET_WORD(t, 0)));
for (i=2; i<size; i++) {
! hnr = COMBINE(hnr, GET_WORD(t, i));
}
return FINISH(hnr);
***************
*** 338,351 ****
static HashNumber hash_number_anno(ATerm t, int size, ATerm anno)
{
- MachineWord *words = (MachineWord *) t;
int i;
HashNumber hnr;
! hnr = START(HIDE_AGE_MARK(words[0]));
for (i=2; i<size; i++) {
! hnr = COMBINE(hnr, words[i]);
}
hnr = COMBINE(hnr, (MachineWord)anno);
--- 378,390 ----
static HashNumber hash_number_anno(ATerm t, int size, ATerm anno)
{
int i;
HashNumber hnr;
! hnr = START(HIDE_AGE_MARK(GET_WORD(t, 0)));
for (i=2; i<size; i++) {
! hnr = COMBINE(hnr, GET_WORD(t, i));
}
hnr = COMBINE(hnr, (MachineWord)anno);
***************
*** 1639,1645 ****
protoAppl->header = header;
CHECK_HEADER(protoAppl->header);
! if (args != PROTO_APPL_ARGS) {
for (i=0; i<arity; i++) {
CHECK_TERM(args[i]);
SET_PROTO_APPL_ARG(i, args[i]);
--- 1678,1684 ----
protoAppl->header = header;
CHECK_HEADER(protoAppl->header);
! if (args != (ATerm *) PROTO_APPL_ARGS) {
for (i=0; i<arity; i++) {
CHECK_TERM(args[i]);
SET_PROTO_APPL_ARG(i, args[i]);
***************
*** 1680,1686 ****
hashtable[hnr] = cur;
}
! if (args != PROTO_APPL_ARGS) {
for (i=0; i<arity; i++) {
protected_buffer[i] = NULL;
}
--- 1719,1725 ----
hashtable[hnr] = cur;
}
! if (args != (ATerm *) PROTO_APPL_ARGS) {
for (i=0; i<arity; i++) {
protected_buffer[i] = NULL;
}
***************
*** 2144,2150 ****
}
SET_PROTO_APPL_ARG(n, arg);
! result = ATmakeApplArray(sym, PROTO_APPL_ARGS);
annos = AT_getAnnotations((ATerm)appl);
if (annos != NULL) {
result = (ATermAppl)AT_setAnnotations((ATerm)result, annos);
--- 2183,2189 ----
}
SET_PROTO_APPL_ARG(n, arg);
! result = ATmakeApplArray(sym, (ATerm *) PROTO_APPL_ARGS);
annos = AT_getAnnotations((ATerm)appl);
if (annos != NULL) {
result = (ATermAppl)AT_setAnnotations((ATerm)result, annos);
|