-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasmstructs.c
More file actions
409 lines (353 loc) · 10 KB
/
Copy pathasmstructs.c
File metadata and controls
409 lines (353 loc) · 10 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
/* asmstructs.c */
/*****************************************************************************/
/* AS-Portierung */
/* */
/* structure handling */
/* */
/*****************************************************************************/
/* $Id: asmstructs.c,v 1.12 2016/09/22 15:36:15 alfred Exp $ */
/*****************************************************************************
* $Log: asmstructs.c,v $
* Revision 1.12 2016/09/22 15:36:15 alfred
* - use platform-dependent format string for LongInt
*
* Revision 1.11 2016/09/12 19:46:56 alfred
* - initialize some elements in constructor
*
* Revision 1.10 2015/10/28 17:54:33 alfred
* - allow substructures of same name in different structures
*
* Revision 1.9 2015/10/23 08:43:33 alfred
* - beef up & fix structure handling
*
* Revision 1.8 2015/10/18 20:08:52 alfred
* - when expanding structure, also regard sub-structures
*
* Revision 1.7 2015/10/18 19:02:16 alfred
* - first reork/fix of nested structure handling
*
* Revision 1.6 2014/12/07 19:13:59 alfred
* - silence a couple of Borland C related warnings and errors
*
* Revision 1.5 2014/12/05 11:09:10 alfred
* - eliminate Nil
*
* Revision 1.4 2013-02-14 21:05:31 alfred
* - add missing bookkeeping for expanded structs
*
* Revision 1.3 2004/01/17 16:18:38 alfred
* - fix some more GCC 3.3 quarrel
*
* Revision 1.2 2004/01/17 16:12:50 alfred
* - some quirks for GCC 3.3
*
* Revision 1.1 2003/11/06 02:49:19 alfred
* - recreated
*
* Revision 1.6 2002/11/20 20:25:04 alfred
* - added unions
*
* Revision 1.5 2002/11/16 20:50:02 alfred
* - added expansion routine
*
* Revision 1.4 2002/11/15 23:30:31 alfred
* - added search routine
*
* Revision 1.3 2002/11/11 21:56:57 alfred
* - store/display struct elements
*
* Revision 1.2 2002/11/11 21:12:32 alfred
* - first working edition
*
*****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include "nls.h"
#include "nlmessages.h"
#include "strutil.h"
#include "trees.h"
#include "as.h"
#include "asmdef.h"
#include "asmsub.h"
#include "asmpars.h"
#include "asmstructs.h"
#include "as.rsc"
/*****************************************************************************/
typedef struct sStructNode
{
TTree Tree;
Boolean Defined;
PStructRec StructRec;
} TStructNode, *PStructNode;
/*****************************************************************************/
PStructStack StructStack, /* momentan offene Strukturen */
pInnermostNamedStruct;
int StructSaveSeg; /* gesichertes Segment waehrend Strukturdef.*/
PStructNode StructRoot = NULL;
/*****************************************************************************/
PStructRec CreateStructRec(void)
{
PStructRec Neu;
Neu = (PStructRec) malloc(sizeof(TStructRec));
if (Neu)
{
Neu->TotLen = 0;
Neu->Elems = NULL;
Neu->ExtChar = '\0';
Neu->DoExt = Neu->IsUnion = False;
}
return Neu;
}
void DestroyStructRec(PStructRec StructRec)
{
PStructElem Old;
while (StructRec->Elems)
{
Old = StructRec->Elems;
StructRec->Elems = Old->Next;
free(Old->Name);
free(Old);
}
free(StructRec);
}
void AddStructElem(PStructRec StructRec, char *Name, Boolean IsStruct, LongInt Offset)
{
PStructElem Neu, Run;
Neu = (PStructElem) malloc(sizeof(TStructElem));
if (Neu)
{
Neu->Name = strdup(Name);
if (!CaseSensitive)
NLS_UpString(Neu->Name);
Neu->IsStruct = IsStruct;
Neu->Offset = Offset;
Neu->Next = NULL;
if (!StructRec->Elems)
StructRec->Elems = Neu;
else
{
for (Run = StructRec->Elems; Run->Next; Run = Run->Next);
Run->Next = Neu;
}
}
BumpStructLength(StructRec, Offset);
}
void BuildStructName(char *pResult, unsigned ResultLen, const char *pName)
{
PStructStack ZStruct;
String tmp2;
strmaxcpy(pResult, pName, ResultLen);
for (ZStruct = StructStack; ZStruct; ZStruct = ZStruct->Next)
if (ZStruct->StructRec->DoExt && ZStruct->Name[0])
{
sprintf(tmp2, "%s%c", ZStruct->pBaseName, ZStruct->StructRec->ExtChar);
strmaxprep(pResult, tmp2, ResultLen);
}
}
void AddStructSymbol(const char *pName, LargeWord Value)
{
PStructStack ZStruct;
/* what we get is offset/length in current structure. Add to
it all offsets in parent structures, i.e. leave out SaveCurrPC
of bottom of stack which contains saved PC of non-struct segment: */
for (ZStruct = StructStack; ZStruct->Next; ZStruct = ZStruct->Next)
Value += ZStruct->SaveCurrPC;
{
String tmp, tmp2;
sprintf(tmp2, "%s%c", pInnermostNamedStruct->Name, pInnermostNamedStruct->StructRec->ExtChar);
strmaxcpy(tmp, pName, sizeof(tmp));
strmaxprep(tmp, tmp2, sizeof(tmp));
EnterIntSymbol(tmp, Value, SegNone, False);
}
}
void BumpStructLength(PStructRec StructRec, LongInt Length)
{
if (StructRec->TotLen < Length)
StructRec->TotLen = Length;
}
static Boolean StructAdder(PTree *PDest, PTree Neu, void *pData)
{
PStructNode NewNode = (PStructNode) Neu, *Node;
Boolean Protest = *((Boolean*)pData), Result = False;
if (!PDest)
{
NewNode->Defined = TRUE;
return True;
}
Node = (PStructNode*) PDest;
if ((*Node)->Defined)
{
if (Protest)
WrXError(1815, Neu->Name);
else
{
DestroyStructRec((*Node)->StructRec);
(*Node)->StructRec = NewNode->StructRec;
}
}
else
{
DestroyStructRec((*Node)->StructRec);
(*Node)->StructRec = NewNode->StructRec;
(*Node)->Defined = True;
return True;
}
return Result;
}
void AddStruct(PStructRec StructRec, char *Name, Boolean Protest)
{
PStructNode Node;
PTree TreeRoot;
Node = (PStructNode) malloc(sizeof(TStructNode));
if (Node)
{
Node->Tree.Left = Node->Tree.Right = NULL;
Node->Tree.Name = strdup(Name);
if (!CaseSensitive)
NLS_UpString(Node->Tree.Name);
Node->Tree.Attribute = MomSectionHandle;
Node->Defined = FALSE;
Node->StructRec = StructRec;
TreeRoot = &(StructRoot->Tree);
EnterTree(&TreeRoot, &(Node->Tree), StructAdder, &Protest);
StructRoot = (PStructNode)TreeRoot;
}
}
static PStructRec FoundStruct_FNode(LongInt Handle, char *Name)
{
PStructNode Lauf;
Lauf = (PStructNode) SearchTree((PTree)StructRoot, Name, Handle);
return Lauf ? Lauf->StructRec : NULL;
}
Boolean FoundStruct(PStructRec *Erg, const char *pName)
{
PSaveSection Lauf;
String Part;
strmaxcpy(Part, pName, 255);
if (!CaseSensitive)
NLS_UpString(Part);
*Erg = FoundStruct_FNode(MomSectionHandle, Part);
if (*Erg)
return True;
Lauf = SectionStack;
while (Lauf)
{
*Erg = FoundStruct_FNode(Lauf->Handle, Part);
if (*Erg)
return True;
Lauf = Lauf->Next;
}
return False;
}
static void ResDef(PTree Tree, void *pData)
{
UNUSED(pData);
((PStructNode)Tree)->Defined = FALSE;
}
void ResetStructDefines(void)
{
IterTree((PTree)StructRoot, ResDef, NULL);
}
typedef struct
{
LongInt Sum;
} TPrintContext;
static void PrintDef(PTree Tree, void *pData)
{
PStructNode Node = (PStructNode)Tree;
PStructElem Elem;
TPrintContext *pContext = (TPrintContext*)pData;
String s;
UNUSED(pData);
WrLstLine("");
pContext->Sum++;
strmaxcpy(s, Node->Tree.Name, 255);
if (Node->Tree.Attribute != -1)
{
strmaxcat(s, "[", 255);
strmaxcat(s, GetSectionName(Node->Tree.Attribute), 255);
strmaxcat(s, "]", 255);
}
WrLstLine(s);
for (Elem = Node->StructRec->Elems; Elem; Elem = Elem->Next)
{
sprintf(s, "%3" PRILongInt " ", Elem->Offset);
strmaxcat(s, Elem->Name, 255);
WrLstLine(s);
}
}
void PrintStructList(void)
{
TPrintContext Context;
String s;
if (!StructRoot)
return;
NewPage(ChapDepth, True);
WrLstLine(getmessage(Num_ListStructListHead1));
WrLstLine(getmessage(Num_ListStructListHead2));
Context.Sum = 0;
IterTree((PTree)StructRoot, PrintDef, &Context);
sprintf(s, "%" PRILongInt "%s", Context.Sum,
getmessage((Context.Sum == 1) ? Num_ListStructSumMsg : Num_ListStructSumsMsg));
}
static void ClearNode(PTree Tree, void *pData)
{
PStructNode Node = (PStructNode) Tree;
UNUSED(pData);
DestroyStructRec(Node->StructRec);
}
void ClearStructList(void)
{
PTree TreeRoot;
TreeRoot = &(StructRoot->Tree);
StructRoot = NULL;
DestroyTree(&TreeRoot, ClearNode, NULL);
}
static void ExpandStruct_One(PStructRec StructRec, char *pVarPrefix, char *pStructPrefix, LargeWord Base)
{
PStructElem StructElem;
int VarLen, RemVarLen, StructLen, RemStructLen;
VarLen = strlen(pVarPrefix);
pVarPrefix[VarLen] = StructRec->ExtChar;
RemVarLen = STRINGSIZE - 3 - VarLen;
StructLen = strlen(pStructPrefix);
pStructPrefix[StructLen] = StructRec->ExtChar;
RemStructLen = STRINGSIZE - 3 - StructLen;
if ((RemVarLen > 1) && (RemStructLen > 1))
{
for (StructElem = StructRec->Elems; StructElem; StructElem = StructElem->Next)
{
strmaxcpy(pVarPrefix + VarLen + 1, StructElem->Name, RemVarLen);
HandleLabel(pVarPrefix, Base + StructElem->Offset);
if (StructElem->IsStruct)
{
TStructRec *pSubStruct;
Boolean Found;
strmaxcpy(pStructPrefix + StructLen + 1, StructElem->Name, RemStructLen);
Found = FoundStruct(&pSubStruct, pStructPrefix);
if (Found)
ExpandStruct_One(pSubStruct, pVarPrefix, pStructPrefix, Base + StructElem->Offset);
}
}
}
pVarPrefix[VarLen] = '\0';
pStructPrefix[StructLen] = '\0';
}
void ExpandStruct(PStructRec StructRec)
{
if (!LabPart[0]) WrError(2040);
else
{
String CompVarName, CompStructName;
strmaxcpy(CompVarName, LabPart, sizeof(CompVarName));
strmaxcpy(CompStructName, LOpPart, sizeof(CompStructName));
ExpandStruct_One(StructRec, LabPart, LOpPart, EProgCounter());
CodeLen = StructRec->TotLen;
BookKeeping();
DontPrint = True;
}
}
void asmstruct_init(void)
{
StructRoot = NULL;
}