/
MPT_Put.cc
338 lines (294 loc) · 10.2 KB
/
MPT_Put.cc
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
/******************************************************************
*
* File: MPT_Put.c
* Purpose: Putting a MPT tree to an MP Link
* Author: Olaf Bachman (obachman@mathematik.uni-kl.de)
* Created: 12/96
*
* Change History (most recent first):
*
******************************************************************/
#include "MPT.h"
/* The following needs to be update */
#if 0
MPT_Status_t MPT_PutTree(MP_Link_pt link, MPT_Tree_pt tree)
{
if (tree != NULL)
{
MPT_Node_pt node = tree->node;
MP_NodeType_t type = node->type;
mpt_failr(MPT_PutNode(link, node));
/* Put the args, if there are any */
if (type == MP_CommonOperatorType || type == MP_OperatorType)
{
MP_Common_t nvalue = MP_COMMON_T(node->nvalue);
MP_DictTag_t ndict = node->dict;
/* we might have to push the current tree onto the stack of
recursive typespecs */
if (ndict == MP_ProtoDict)
{
if (nvalue == MP_CopProtoRecUnion)
MPT_PushRecUnion(tree);
else if (nvalue == MP_CopProtoRecStruct)
MPT_PushRecStruct(tree);
}
/* Put Args */
mpt_failr(MPT_PutArgs(link, tree->args, node->numchild,
MPT_ProtoAnnotValue(node)));
/* And, pop the recursive typesepcs again */
if (ndict == MP_ProtoDict)
{
if (nvalue == MP_CopProtoRecUnion)
MPT_PopRecUnion();
else if (nvalue == MP_CopProtoRecStruct)
MPT_PopRecStruct();
}
return MPT_Success;
}
else if (type == MPT_ExternalDataType)
return MPT_PutExternalData(link, tree->args);
/* else, it is a node which has no arguments */
else return MPT_Success;
}
else
return MPT_Success;
}
MPT_Status_t MPT_PutNode(MP_Link_pt link, MPT_Node_pt node)
{
MP_NumAnnot_t numannot = node->numannot;
MP_NodeType_t type = node->type;
if (type != MPT_ExternalDataType)
{
/* Put the node Header */
mp_failr(IMP_PutNodeHeader(link, type, node->dict,
MP_COMMON_T(node->nvalue),
numannot, node->numchild));
/* Put the node value, if necessary */
if (MP_IsStringBasedType(type) ||
type == MP_OperatorType)
mp_failr(IMP_PutString(link, MP_STRING_T(node->nvalue)));
else if (MP_Is32BitNumericType(type))
mp_failr(IMP_PutUint32(link, MP_UINT32_T(node->nvalue)));
else if (MP_Is64BitNumericType(type))
mpt_failr(IMP_PutReal64(link, MP_REAL64_T(node->nvalue)));
else if (type == MP_ApIntType)
mpt_failr(MPT_PutApInt(link, node->nvalue));
else if (type == MP_ApRealType)
mpt_failr(MPT_PutApReal(link, node->nvalue));
else if (! MP_IsCommonType(type) || MP_Is8BitNumericType(type))
return MPT_SetError(MPT_WrongNodeType);
/* take care of annots, if there are any */
if (numannot > 0)
{
MP_Uint32_t i;
MPT_Annot_pt *annots = node->annots, annot;
MPT_Assume(annots != NULL);
for (i=0; i<numannot; i++)
{
annot = annots[i];
MPT_Assume(annot != NULL);
mp_failr(MP_PutAnnotationPacket(link, annot->dict, annot->type,
annot->flags));
if (MP_IsAnnotValuated(annot->flags))
mpt_failr(MPT_PutTree(link, annot->value));
}
}
}
return MPT_Success;
}
MPT_Status_t MPT_PutArgs(MP_Link_pt link, MPT_Arg_pt args, MP_NumChild_t nc,
MPT_Tree_pt typespec)
{
MP_NumChild_t i;
/* return, if args == NULL, i.e. node is a leave node of a tree */
if (args == NULL || nc == 0) return MPT_Success;
/* Check for non-prototyped data */
if (typespec == NULL)
{
for (i=0; i<nc; i++)
mp_failr(MPT_PutTree(link, MPT_TREE_PT(args[i])));
return MPT_Success;
}
else
{
/* Now we are in the realm of prototyped data */
MPT_Node_pt pnode = typespec->node;
MP_NodeType_t ptype = pnode->type;
MP_DictTag_t pdict = pnode->dict;
/* Check only for arrays of basic numeric types */
if (ptype == MP_CommonMetaType && pdict == MP_ProtoDict)
{
MP_Common_t metatype = MP_COMMON_T(pnode->nvalue);
if (IMP_Is8BitMetaType(metatype))
mp_return(IMP_Put8BitVector(link, MP_UINT8_PT(args), nc));
else if (IMP_Is32BitNumericMetaType(metatype))
mp_return(IMP_Put32BitVector(link, MP_UINT32_PT(args), nc));
else if (IMP_Is64BitNumericMetaType(metatype))
mp_return(IMP_Put8BitVector(link, MP_REAL64_PT(args), nc));
}
/* Everything else is handled item by item */
for (i=0;i<nc; i++)
mpt_failr(MPT_PutTypespecedArg(link, args[i], typespec));
return MPT_Success;
}
}
MPT_Status_t MPT_PutTypespecedArg(MP_Link_pt link, MPT_Arg_t arg,
MPT_Tree_pt typespec)
{
/* check for trivial cases */
if (arg == NULL) return MPT_Success;
if (typespec != NULL)
{
MPT_Node_pt typespec_node = typespec->node;
MPT_Arg_pt typespec_args = typespec->args;
MP_DictTag_t ndict = typespec_node->dict;
MP_NodeType_t ntype = typespec_node->type;
MP_NumChild_t nc = typespec_node->numchild;
MP_Common_t cvalue = MP_COMMON_T(typespec_node->nvalue);
/* check special Common Meta types from ProtoDict */
if (ndict == MP_ProtoDict && ntype == MP_CommonMetaType)
{
/* do nothing for types with sizeof <= sizeof(MP_Arg_t) */
if (IMP_Is32BitNumericMetaType(cvalue))
mp_return(IMP_PutUint32(link, MP_UINT32_T(arg)));
else if (IMP_Is8BitNumericMetaType(cvalue))
mp_return(IMP_PutUint8(link, MP_UINT8_T(arg)));
else if(IMP_Is64BitNumericMetaType(cvalue))
mp_return(IMP_PutUint64(link, MP_REAL64_T(arg)));
else if (cvalue == MP_CmtProtoIMP_ApInt)
mp_return(MPT_PutApInt(link, arg));
else if (cvalue == MP_CmtProtoIMP_ApReal)
mp_return(MPT_PutApReal(link, arg));
else if (IMP_IsStringBasedMetaType(cvalue))
mp_return(IMP_PutString(link, MP_STRING_T(arg)));
else if (cvalue == MP_CmtProtoRecStruct)
return MPT_PutTypespecedData(link, arg, MPT_RecStructTree);
else if (cvalue == MP_CmtProtoRecUnion)
return MPT_PutTypespecedData(arg, MPT_RecUnionTree);
}
/* special MP Operators */
if (ntype == MP_CommonOperatorType && ndict == MP_ProtoDict)
{
/* ProtoD:Union */
if (cvalue == MP_CopProtoUnion || cvalue == MP_CopProtoRecUnion)
{
MPT_Union_pt un = MPT_UNION_PT(arg);
MPT_Assume(un->tag <= nc);
IMP_PutUint32(link, un->tag);
if (un->tag > 0)
return MPT_PutTypespecedArg(link, un->arg, typespec_args[un->tag -1]);
}
/* ProtoD:Struct */
if (cvalue == MP_CopProtoStruct || cvalue == MP_CopProtoRecStruct)
{
MP_NumChild_t i;
MPT_Arg_pt st_args = MPT_ARG_PT(arg);
for (i=0; i<nc; i++)
mpt_failr(MPT_PutTypespecedArg(link, st_args[i], typespec_args[i]));
return MPT_Success;
}
}
/* Meta Operators */
if (ntype == MP_CommonMetaOperatorType || ntype == MP_MetaOperatorType)
{
MPT_DynArgs_pt dynargs = NULL;
MPT_Arg_pt args;
typespec = MPT_ProtoAnnotValue(typespec_node);
if (nc == 0)
{
dynargs = MPT_DYNARGS_PT(arg);
nc = dynargs->length;
args = dynargs->args;
}
else
args = MPT_ARG_PT(arg);
return MPT_PutArgs(link, args, nc, typespec);
}
}
/* Everything else is taken to be a tree */
return MP_PutTree(link, MPT_TREE_PT(arg));
}
#endif /* needs to be updated */
MPT_Status_t MPT_PutTree(MP_Link_pt link, MPT_Tree_pt tree)
{
if (tree != NULL)
{
MPT_Node_pt node = tree->node;
MP_NodeType_t type = node->type;
mpt_failr(MPT_PutNode(link, node));
/* Put the args, if there are any */
if (type == MP_CommonOperatorType || type == MP_OperatorType)
{
MP_Common_t nvalue = MP_COMMON_T(node->nvalue);
MP_DictTag_t ndict = node->dict;
/* Put Args */
return MPT_PutArgs(link, tree->args, node->numchild,
MPT_ProtoAnnotValue(node));
}
else return MPT_Success;
}
else
return MPT_Success;
}
MPT_Status_t MPT_PutNode(MP_Link_pt link, MPT_Node_pt node)
{
MP_NumAnnot_t numannot = node->numannot;
MP_NodeType_t type = node->type;
if (type != MPT_ExternalDataType)
{
/* Put the node Header */
mp_failr(IMP_PutNodeHeader(link, type, node->dict,
MP_COMMON_T(node->nvalue),
numannot, node->numchild));
/* Put the node value, if necessary */
if (MP_IsStringBasedType(type) ||
type == MP_OperatorType)
mp_failr(IMP_PutString(link, MP_STRING_T(node->nvalue)));
else if (MP_Is32BitNumericType(type))
mp_failr(IMP_PutUint32(link, MP_UINT32_T(node->nvalue)));
else if (MP_Is64BitNumericType(type))
mp_failr(IMP_PutReal64(link, MP_REAL64_T(node->nvalue)));
else if (type == MP_ApIntType)
mpt_failr(MPT_PutApInt(link, node->nvalue));
else if (type == MP_ApRealType)
mpt_failr(MPT_PutApReal(link, node->nvalue));
else if (! MP_IsCommonType(type) || MP_Is8BitNumericType(type))
return MPT_SetError(MPT_WrongNodeType);
/* take care of annots, if there are any */
if (numannot > 0)
{
MP_Uint32_t i;
MPT_Annot_pt *annots = node->annots, annot;
MPT_Assume(annots != NULL);
for (i=0; i<numannot; i++)
{
annot = annots[i];
MPT_Assume(annot != NULL);
mp_failr(MP_PutAnnotationPacket(link, annot->dict, annot->type,
annot->flags));
if (MP_IsAnnotValuated(annot->flags))
mpt_failr(MPT_PutTree(link, annot->value));
}
}
}
return MPT_Success;
}
MPT_Status_t MPT_PutArgs(MP_Link_pt link, MPT_Arg_pt args, MP_NumChild_t nc,
MPT_Tree_pt typespec)
{
MP_NumChild_t i;
/* return, if args == NULL, i.e. node is a leave node of a tree */
if (args == NULL || nc == 0) return MPT_Success;
/* Check for non-prototyped data */
if (typespec == NULL)
{
for (i=0; i<nc; i++)
mp_failr(MPT_PutTree(link, MPT_TREE_PT(args[i])));
return MPT_Success;
}
else
{
/* not yet implemented */
return MPT_Failure;
}
}