summaryrefslogtreecommitdiffstats
path: root/private/os2/client/thunk/thunkcom/symtab.c
blob: b74e19bf183290efd08b84d7d1f60cc6bf71ddba (plain) (blame)
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#define SCCSID  "@(#)symtab.c 13.13 90/08/28"

/*
 *      Thunk Compiler - Symbol Table Routines.
 *
 *      This is an OS/2 2.x specific file
 *      Microsoft Confidential
 *
 *      Copyright (c) Microsoft Corporation 1987, 1988, 1989
 *
 *      All Rights Reserved
 *
 *      Written 10/15/88 by Kevin Ross
 */


#include <stdio.h>
#include "thunk.h"
#include "types.h"
#include "symtab.h"


extern FILE *StdDbg;


/*
 *  BaseTable is used to access the base data types quickly.
 */
TypeNode *BaseTable[SYMTAB_LASTBASEUSED];

TypeNode *SymTable= NULL;
TypeNode *TypeTable = NULL;
FunctionNode *FunctionTable = NULL;
MapNode  *MapTable = NULL;

char *SemanticTable[5];


/***    sym_SymTabInit()  - Module initialization Routine
 *
 *      This routine is called before any other symbol table routines
 *      are used.
 *
 *      It will setup the table of base types, and initialize any other
 *      needed variables.
 *
 *      Entry:  none
 *
 *      Exit:   tables and variables are initialized.
 */

void sym_SymTabInit()

{
    BaseTable[SYMTAB_SHORT] = typ_MakeTypeNode(TYPE_SHORT);
    BaseTable[SYMTAB_SHORT]->iBaseType = TYPE_SHORT;
    BaseTable[SYMTAB_SHORT]->iBaseDataSize = 2;
    BaseTable[SYMTAB_SHORT]->pchBaseTypeName = "short";

    BaseTable[SYMTAB_LONG] = typ_MakeTypeNode(TYPE_LONG);
    BaseTable[SYMTAB_LONG]->iBaseType = TYPE_LONG;
    BaseTable[SYMTAB_LONG]->iBaseDataSize = 4;
    BaseTable[SYMTAB_LONG]->pchBaseTypeName = "long";

    BaseTable[SYMTAB_USHORT] = typ_MakeTypeNode(TYPE_USHORT);
    BaseTable[SYMTAB_USHORT]->iBaseType = TYPE_USHORT;
    BaseTable[SYMTAB_USHORT]->iBaseDataSize = 2;
    BaseTable[SYMTAB_USHORT]->pchBaseTypeName = "unsigned short";

    BaseTable[SYMTAB_ULONG] = typ_MakeTypeNode(TYPE_ULONG);
    BaseTable[SYMTAB_ULONG]->iBaseType = TYPE_ULONG;
    BaseTable[SYMTAB_ULONG]->iBaseDataSize = 4;
    BaseTable[SYMTAB_ULONG]->pchBaseTypeName = "unsigned long";

    BaseTable[SYMTAB_INT] = typ_MakeTypeNode(TYPE_INT);
    BaseTable[SYMTAB_INT]->iBaseType = TYPE_INT;
    BaseTable[SYMTAB_INT]->iBaseDataSize = -99;
    BaseTable[SYMTAB_INT]->pchBaseTypeName = "int";

    BaseTable[SYMTAB_UINT] = typ_MakeTypeNode(TYPE_UINT);
    BaseTable[SYMTAB_UINT]->iBaseType = TYPE_UINT;
    BaseTable[SYMTAB_UINT]->iBaseDataSize = -99;
    BaseTable[SYMTAB_UINT]->pchBaseTypeName = "unsigned int";

    BaseTable[SYMTAB_VOID] = typ_MakeTypeNode(TYPE_VOID);
    BaseTable[SYMTAB_VOID]->iBaseType = TYPE_VOID;
    BaseTable[SYMTAB_VOID]->iBaseDataSize = 1;
    BaseTable[SYMTAB_VOID]->pchBaseTypeName = "void";

    BaseTable[SYMTAB_UCHAR] = typ_MakeTypeNode(TYPE_UCHAR);
    BaseTable[SYMTAB_UCHAR]->iBaseType = TYPE_UCHAR;
    BaseTable[SYMTAB_UCHAR]->iBaseDataSize = 1;
    BaseTable[SYMTAB_UCHAR]->pchBaseTypeName = "unsigned char";

    BaseTable[SYMTAB_CHAR] = typ_MakeTypeNode(TYPE_CHAR);
    BaseTable[SYMTAB_CHAR]->iBaseType = TYPE_CHAR;
    BaseTable[SYMTAB_CHAR]->iBaseDataSize = 1;
    BaseTable[SYMTAB_CHAR]->pchBaseTypeName = "char";

    BaseTable[SYMTAB_STRING] = typ_MakeTypeNode(TYPE_STRING);
    BaseTable[SYMTAB_STRING]->iBaseDataSize = 1;
    BaseTable[SYMTAB_STRING]->pchBaseTypeName = "string";

    BaseTable[SYMTAB_NULLTYPE] = typ_MakeTypeNode(TYPE_NULLTYPE);
    BaseTable[SYMTAB_NULLTYPE]->iBaseType = TYPE_NULLTYPE;
    BaseTable[SYMTAB_NULLTYPE]->iBaseDataSize = 4;
    BaseTable[SYMTAB_NULLTYPE]->pchBaseTypeName = "nulltype";

    SemanticTable[0] = "";
    SemanticTable[1] = "Input";
    SemanticTable[2] = "Output";
    SemanticTable[3] = "InOut";
    SemanticTable[4] = "SizeOf";
}


/***    sym_FindSymbolTypeNode(pTab,pchSym)
 *
 *      This function will look down the list of symbols - pTab - and
 *      return a pointer to the TypeNode that it finds.
 *
 *      This is implemented as a linear list to get the compiler up and
 *      running.  It should be revised to a faster algorithm.
 *
 *      Entry:  pTab   - pointer to list of symbols.
 *              pchSym - pointer to symbol to find.
 *
 *      Exit:   returns a pointer to the typenode.
 */

TypeNode *sym_FindSymbolTypeNode(TypeNode *pTab,
                                 char *pchSym)

{
    while (pTab && pchSym) {
        if (pTab->pchIdent) {
            if (!strcmp(pTab->pchIdent,pchSym))
                return pTab;
        }
        pTab = pTab->pNextNode;
    }
    return (NULL);
}


/***    sym_FindSymbolTypeNodePair(pTab1,pTab2,ppT1,ppT2,pchSym)
 *
 *      This routine will look down the list of symbols - pTab1 - and
 *      return in ppT1 the pointer to the TypeNode it finds that matches
 *      the pchIdent with pchSym. It will also concurrently walk a second
 *      list of symbols, pTab2, and returns ppT2.
 *
 *      The net result is that FindSymbolTypeNodePair returns the pair of
 *      typenodes that represent the same parameter ordinal in two lists.
 *
 *      This is implemented as a linear list to get the compiler up and
 *      running.  It should be revised to a faster algorithm.
 */

int sym_FindSymbolTypeNodePair(TypeNode *pTab1,
                               TypeNode *pTab2,
                               TypeNode **ppT1,
                               TypeNode **ppT2,
                               char *pchSym)

{
    *ppT1 = pTab1;
    *ppT2 = pTab2;

    while (*ppT1 && *ppT2 && pchSym) {
        if ((*ppT1)->pchIdent)
            if (!strcmp((*ppT1)->pchIdent,pchSym))
                return 1;
        *ppT1 = (*ppT1)->pNextNode;
        *ppT2 = (*ppT2)->pNextNode;
    }
    return (0);
}


/***    sym_FindSymbolFunctionNode(pTab,pchSym)
 *
 *      FindSymbolFunctionNode will look down the list of symbols - pTab -
 *      and return a pointer to the FunctionNode that it finds.
 *
 *      This is implemented as a linear list to get the compiler up and
 *      running.  It should be revised to a faster algorithm.
 */

FunctionNode *sym_FindSymbolFunctionNode(FunctionNode *pTab,
                                         char *pchSym)

{
    while (pTab && pchSym) {
        if (!strcmp(pTab->pchFunctionName,pchSym))
            return (pTab);
        pTab = pTab->pNextFunctionNode;
    }
    return (NULL);
}


/***    sym_InsertTypeNode(ppTab,pNode)
 *
 *      InsertTypeNode will add pNode to the symbol table ppTab.
 *
 *      This is implemented as a linear list to get the compiler up and
 *      running.  This routine will always add the new symbol to the front
 *      of the current list.  It should be revised to a algorithm better
 *      for searching.
 */

void sym_InsertTypeNode(TypeNode **ppTab,
                        TypeNode *pNode)

{
    if (*ppTab) {
        pNode->pNextNode = *ppTab;
    }
    *ppTab = pNode;
}


/***    sym_InsertFunctionNode(ppTab,pFNode)
 *
 *      InsertFunctionNode will add pFNode to the symbol table ppTab.
 *
 *      This is implemented as a linear list to get the compiler up and
 *      running.  This routine will always add the new symbol to the front
 *      of the current list.  It should be revised to a algorithm better
 *      for searching.
 */

void sym_InsertFunctionNode(FunctionNode **ppTab,
                            FunctionNode *pFNode)

{
    if (*ppTab) {
        pFNode->pNextFunctionNode = *ppTab;
    }
    *ppTab = pFNode;
}


/***    sym_ReverseTypeList(pOld)
 *
 *      ReverseTypeList will reverse the order of a TypeNode linked list.
 *      This is needed in several places in the compiler.
 */

TypeNode *sym_ReverseTypeList(TypeNode *pOld)

{
    register TypeNode *pNew, *pNext;


    if (! pOld)
        return pOld;

    pNew = pOld->pNextNode;
    pOld->pNextNode = NULL;

    while (pNew) {
        pNext = pNew->pNextNode;
        pNew->pNextNode = pOld;
        pOld = pNew;
        pNew = pNext;
    }
    return (pOld);
}


/***    sym_FindFMapping(pMapTab,pSymA,pSymB)
 *
 *      FindFMapping will search the list pMapTab in search for either pSymA
 *      or pSymB in the pFromName field of the mapping list.
 */

MapNode *sym_FindFMapping(MapNode *pMapTab,
                          char *pSymA,
                          char *pSymB)

{
    while (pMapTab) {
        if (!strcmp(pMapTab->pFromName,pSymA) ||
            !strcmp(pMapTab->pFromName,pSymB)) {
            return (pMapTab);
        }
        pMapTab = pMapTab->pNextMapping;
    }
    return (NULL);
}


/***    sym_AddFMapping(ppMapTab,pFuncA,pFuncB)
 *
 *      AddFMapping accepts two FunctionNode pointers, and a table.
 *      It will create a MapNode containing link information to the two
 *      functions. The mapping id is contained in pFromName.
 */

MapNode *sym_AddFMapping(MapNode **ppMapTab,
                         FunctionNode *pFuncA,
                         FunctionNode *pFuncB)

{
    MapNode *temp,*ptr;


    if (temp = (MapNode *) malloc(sizeof(MapNode))) {
        temp->pFromName = pFuncA->pchFunctionName;
        temp->pFromNode = pFuncA;
        temp->pToNode = pFuncB;
        temp->pNextMapping = NULL;
        temp->pFamily = NULL;

        ptr = *ppMapTab;
        if (!ptr)
            *ppMapTab = temp;
        else {
            while (ptr->pNextMapping)
                ptr = ptr->pNextMapping;
            ptr->pNextMapping = temp;
        }
        return (*ppMapTab);
    }
    else
        fatal("sym_AddFMapping malloc failure");
}



/***********************************************************************/
/*           Dump Routines: Used for debugging output only.            */
/***********************************************************************/

static char IndentString[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";

static int ILevel = 18;


void sym_DumpFNode(FunctionNode *F)

{
    fprintf(StdDbg,"\nFunction Node: %s\n",F->pchFunctionName);
    fprintf(StdDbg,"Call Type: %s\n",
                 (F->iCallType == TYPE_API16) ? "API16":"API32");
    fprintf(StdDbg,"System Call Convention: %s\n",F->fSysCall?"TRUE":"FALSE");
    fprintf(StdDbg,"Ring Type: %s\n",
                 (F->fConforming) ? "Conforming":"Normal");
    fprintf(StdDbg,"Return Type: ");

    sym_DumpTNode(F->ReturnType);

    fprintf(StdDbg,"ErrBadParam = %lu\n",F->ulErrBadParam);
    fprintf(StdDbg,"ErrNoMem = %lu\n",F->ulErrNoMem);
    fprintf(StdDbg,"MinStack = %d\tInlineCode = %s\n",F->iMinStack,
            (F->fInlineCode) ? "TRUE" : "FALSE");

    fprintf(StdDbg,"Maps to function: %s\n",F->pMapsToFunction->pchFunctionName);

    fprintf(StdDbg,"Parameter Types:\n");

    ILevel--;
    sym_DumpTNodeList(F->ParamList);
    ILevel++;
    fprintf(StdDbg,"\n");
}


void sym_DumpFNodeList(FunctionNode *F)

{
    for( ; F; F = F->pNextFunctionNode)
        sym_DumpFNode( F);
}


void sym_DumpTNode(TypeNode *T)

{
    fprintf(StdDbg,"%s",&IndentString[ILevel]);

    fprintf(StdDbg,"%s",(T->iPointerType) ?
          ((T->iPointerType == TYPE_FAR16) ? "FAR16 ":
            ((T->iPointerType == TYPE_NEAR32) ? "NEAR32 ":"PTR   ")):"");
    fprintf(StdDbg,"%s",T->pchBaseTypeName);
    if (T->pchIdent)
        fprintf(StdDbg,"\t%s", T->pchIdent);
    if (T->iArraySize > 1)
        fprintf(StdDbg,"[%u]",T->iArraySize);
    fprintf(StdDbg," #SO %u BS %u #",T->iStructOffset,T->iBaseDataSize);
    if (T->iDeleted)
        fprintf(StdDbg," DELETED fv=%lu ",T->iFillValue);
    if (T->iBaseType == TYPE_STRUCT) {
        fprintf(StdDbg,"  Aligned %d",T->iAlignment);
        fprintf(StdDbg,"\n%s",&IndentString[ILevel--]);
        fprintf(StdDbg,"{\n");
        sym_DumpTNodeList(T->pStructElems);
        fprintf(StdDbg,"%s",&IndentString[++ILevel]);
        fprintf(StdDbg,"}");
    }

    sym_DumpSemantics(T);
    fprintf(StdDbg,"\n");
}


void sym_DumpTNodeList(TypeNode *T)

{
    for( ; T; T = T->pNextNode)
        sym_DumpTNode(T);
}


void sym_DumpSemantics(TypeNode *T)

{
    fprintf(StdDbg,";");
    if (SemanticTable[T->fSemantics & 3]) {
        fprintf(StdDbg,"\t%s",SemanticTable[T->fSemantics & 3]);
    }

    if (T->fSemantics & SEMANTIC_SIZE)
        fprintf(StdDbg,"\tSizeOf %s", T->pParamSizeOf->pchIdent);

    if (T->fSemantics & SEMANTIC_COUNT)
        fprintf(StdDbg,"\tCountOf %s", T->pParamSizeOf->pchIdent);
}


void sym_DumpFMappingList(MapNode *M)

{
    fprintf(StdDbg,"\nFunction Map Table\n\n");
    while (M) {
        fprintf(StdDbg,"\nMapping Id: %s\t",M->pFromName);
        fprintf(StdDbg,"Maps %s => %s\n",M->pFromNode->pchFunctionName,
                M->pToNode->pchFunctionName);
        M = M->pNextMapping;
    }
}