1 /*****************************************************************************/
5 /* Symbol table entry for the ca65 macroassembler */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
50 #include "studyexpr.h" /* ### */
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* List of all symbol table entries */
63 SymEntry* SymList = 0;
65 /* Pointer to last defined symbol */
66 SymEntry* SymLast = 0;
70 /*****************************************************************************/
72 /*****************************************************************************/
76 SymEntry* NewSymEntry (const StrBuf* Name, unsigned Flags)
77 /* Allocate a symbol table entry, initialize and return it */
82 SymEntry* S = xmalloc (sizeof (SymEntry));
84 /* Initialize the entry */
89 S->LineInfos = EmptyCollection;
90 GetFullLineInfo (&S->LineInfos, 1);
91 for (I = 0; I < sizeof (S->GuessedUse) / sizeof (S->GuessedUse[0]); ++I) {
98 S->ExprRefs = AUTO_COLLECTION_INITIALIZER;
99 S->ExportSize = ADDR_SIZE_DEFAULT;
100 S->AddrSize = ADDR_SIZE_DEFAULT;
101 memset (S->ConDesPrio, 0, sizeof (S->ConDesPrio));
102 S->Name = GetStrBufId (Name);
104 /* Insert it into the list of all entries */
108 /* Return the initialized entry */
114 int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E)
115 /* Search in the given tree for a name. If we find the symbol, the function
116 * will return 0 and put the entry pointer into E. If we did not find the
117 * symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
118 * E will be set to the last entry, and the result of the function is <0 if
119 * the entry should be inserted on the left side, and >0 if it should get
120 * inserted on the right side.
123 /* Is there a tree? */
129 /* We have a table, search it */
132 /* Get the symbol name */
133 const StrBuf* SymName = GetStrBuf (T->Name);
135 /* Choose next entry */
136 int Cmp = SB_Compare (Name, SymName);
137 if (Cmp < 0 && T->Left) {
139 } else if (Cmp > 0&& T->Right) {
142 /* Found or end of search, return the result */
151 void SymRef (SymEntry* S)
152 /* Mark the given symbol as referenced */
154 /* Mark the symbol as referenced */
155 S->Flags |= SF_REFERENCED;
160 void SymTransferExprRefs (SymEntry* From, SymEntry* To)
161 /* Transfer all expression references from one symbol to another. */
165 for (I = 0; I < CollCount (&From->ExprRefs); ++I) {
167 /* Get the expression node */
168 ExprNode* E = CollAtUnchecked (&From->ExprRefs, I);
171 CHECK (E->Op == EXPR_SYMBOL && E->V.Sym == From);
173 /* Replace the symbol reference */
176 /* Add the expression reference */
177 SymAddExprRef (To, E);
180 /* Remove all symbol references from the old symbol */
181 CollDeleteAll (&From->ExprRefs);
186 static void SymReplaceExprRefs (SymEntry* S)
187 /* Replace the references to this symbol by a copy of the symbol expression */
192 /* Check if the expression is const and get its value */
193 int IsConst = IsConstExpr (S->Expr, &Val);
196 /* Loop over all references */
197 for (I = 0; I < CollCount (&S->ExprRefs); ++I) {
199 /* Get the expression node */
200 ExprNode* E = CollAtUnchecked (&S->ExprRefs, I);
203 CHECK (E->Op == EXPR_SYMBOL && E->V.Sym == S);
205 /* We cannot touch the root node, since there are pointers to it.
206 * Replace it by a literal node.
208 E->Op = EXPR_LITERAL;
212 /* Remove all symbol references from the symbol */
213 CollDeleteAll (&S->ExprRefs);
218 void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags)
219 /* Define a new symbol */
221 if (S->Flags & SF_IMPORT) {
222 /* Defined symbol is marked as imported external symbol */
223 Error ("Symbol `%m%p' is already an import", GetSymName (S));
226 if ((Flags & SF_VAR) != 0 && (S->Flags & (SF_EXPORT | SF_GLOBAL))) {
227 /* Variable symbols cannot be exports or globals */
228 Error ("Var symbol `%m%p' cannot be an export or global symbol", GetSymName (S));
231 if (S->Flags & SF_DEFINED) {
232 /* Multiple definition. In case of a variable, this is legal. */
233 if ((S->Flags & SF_VAR) == 0) {
234 Error ("Symbol `%m%p' is already defined", GetSymName (S));
235 S->Flags |= SF_MULTDEF;
238 /* Redefinition must also be a variable symbol */
239 if ((Flags & SF_VAR) == 0) {
240 Error ("Symbol `%m%p' is already different kind", GetSymName (S));
243 /* Delete the current symbol expression, since it will get
251 /* Map a default address size to a real value */
252 if (AddrSize == ADDR_SIZE_DEFAULT) {
253 /* ### Must go! Delay address size calculation until end of assembly! */
256 StudyExpr (Expr, &ED);
257 AddrSize = ED.AddrSize;
261 /* Set the symbol value */
264 /* In case of a variable symbol, walk over all expressions containing
265 * this symbol and replace the (sub-)expression by the literal value of
266 * the tree. Be sure to replace the expression node in place, since there
267 * may be pointers to it.
269 if (Flags & SF_VAR) {
270 SymReplaceExprRefs (S);
273 /* If the symbol is marked as global, export it. Address size is checked
276 if (S->Flags & SF_GLOBAL) {
277 S->Flags = (S->Flags & ~SF_GLOBAL) | SF_EXPORT;
280 /* Mark the symbol as defined and use the given address size */
281 S->Flags |= (SF_DEFINED | Flags);
282 S->AddrSize = AddrSize;
284 /* If the symbol is exported, check the address sizes */
285 if (S->Flags & SF_EXPORT) {
286 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
287 /* Use the real size of the symbol */
288 S->ExportSize = S->AddrSize;
289 } else if (S->AddrSize > S->ExportSize) {
290 /* We're exporting a symbol smaller than it actually is */
291 LIWarning (&S->LineInfos, 1, "Symbol `%m%p' is %s but exported %s",
292 GetSymName (S), AddrSizeToStr (S->AddrSize),
293 AddrSizeToStr (S->ExportSize));
297 /* If this is not a local symbol, remember it as the last global one */
298 if ((S->Flags & SF_LOCAL) == 0) {
305 void SymImport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
306 /* Mark the given symbol as an imported symbol */
308 if (S->Flags & SF_DEFINED) {
309 Error ("Symbol `%m%p' is already defined", GetSymName (S));
310 S->Flags |= SF_MULTDEF;
313 if (S->Flags & SF_EXPORT) {
314 /* The symbol is already marked as exported symbol */
315 Error ("Cannot import exported symbol `%m%p'", GetSymName (S));
319 /* If no address size is given, use the address size of the enclosing
322 if (AddrSize == ADDR_SIZE_DEFAULT) {
323 AddrSize = GetCurrentSegAddrSize ();
326 /* If the symbol is marked as import or global, check the address size,
327 * then do silently remove the global flag.
329 if (S->Flags & SF_IMPORT) {
330 if ((Flags & SF_FORCED) != (S->Flags & SF_FORCED)) {
331 Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
333 if (AddrSize != S->AddrSize) {
334 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
337 if (S->Flags & SF_GLOBAL) {
338 S->Flags &= ~SF_GLOBAL;
339 if (AddrSize != S->AddrSize) {
340 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
344 /* Set the symbol data */
345 S->Flags |= (SF_IMPORT | Flags);
346 S->AddrSize = AddrSize;
351 void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
352 /* Mark the given symbol as an exported symbol */
354 /* Check if it's ok to export the symbol */
355 if (S->Flags & SF_IMPORT) {
356 /* The symbol is already marked as imported external symbol */
357 Error ("Symbol `%m%p' is already an import", GetSymName (S));
360 if (S->Flags & SF_VAR) {
361 /* Variable symbols cannot be exported */
362 Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
366 /* If the symbol was marked as global before, remove the global flag and
367 * proceed, but check the address size.
369 if (S->Flags & SF_GLOBAL) {
370 if (AddrSize != S->ExportSize) {
371 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
373 S->Flags &= ~SF_GLOBAL;
376 /* If the symbol was already marked as an export, but wasn't defined
377 * before, the address sizes in both definitions must match.
379 if ((S->Flags & (SF_EXPORT|SF_DEFINED)) == SF_EXPORT) {
380 if (S->ExportSize != AddrSize) {
381 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
384 S->ExportSize = AddrSize;
386 /* If the symbol is already defined, check symbol size against the
389 if (S->Flags & SF_DEFINED) {
390 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
391 /* No export size given, use the real size of the symbol */
392 S->ExportSize = S->AddrSize;
393 } else if (S->AddrSize > S->ExportSize) {
394 /* We're exporting a symbol smaller than it actually is */
395 Warning (1, "Symbol `%m%p' is %s but exported %s",
396 GetSymName (S), AddrSizeToStr (S->AddrSize),
397 AddrSizeToStr (S->ExportSize));
401 /* Set the symbol data */
402 S->Flags |= (SF_EXPORT | SF_REFERENCED | Flags);
407 void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
408 /* Mark the given symbol as a global symbol, that is, as a symbol that is
409 * either imported or exported.
412 if (S->Flags & SF_VAR) {
413 /* Variable symbols cannot be exported or imported */
414 Error ("Var symbol `%m%p' cannot be made global", GetSymName (S));
418 /* If the symbol is already marked as import, the address size must match.
419 * Apart from that, ignore the global declaration.
421 if (S->Flags & SF_IMPORT) {
422 if (AddrSize == ADDR_SIZE_DEFAULT) {
423 /* Use the size of the current segment */
424 AddrSize = GetCurrentSegAddrSize ();
426 if (AddrSize != S->AddrSize) {
427 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
432 /* If the symbol is already an export: If it is not defined, the address
435 if (S->Flags & SF_EXPORT) {
436 if ((S->Flags & SF_DEFINED) == 0) {
437 /* Symbol is undefined */
438 if (AddrSize != S->ExportSize) {
439 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
441 } else if (AddrSize != ADDR_SIZE_DEFAULT) {
442 /* Symbol is defined and address size given */
443 if (AddrSize != S->ExportSize) {
444 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
450 /* If the symbol is already marked as global, the address size must match.
451 * Use the ExportSize here, since it contains the actual address size
452 * passed to this function.
454 if (S->Flags & SF_GLOBAL) {
455 if (AddrSize != S->ExportSize) {
456 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
461 /* If we come here, the symbol was neither declared as export, import or
462 * global before. Check if it is already defined, in which case it will
463 * become an export. If it is not defined, mark it as global and remember
464 * the given address sizes.
466 if (S->Flags & SF_DEFINED) {
467 /* The symbol is defined, export it */
468 S->ExportSize = AddrSize;
469 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
470 /* No export size given, use the real size of the symbol */
471 S->ExportSize = S->AddrSize;
472 } else if (S->AddrSize > S->ExportSize) {
473 /* We're exporting a symbol smaller than it actually is */
474 Warning (1, "Symbol `%m%p' is %s but exported %s",
475 GetSymName (S), AddrSizeToStr (S->AddrSize),
476 AddrSizeToStr (S->ExportSize));
478 S->Flags |= (SF_EXPORT | Flags);
480 /* Since we don't know if the symbol will get exported or imported,
481 * remember two different address sizes: One for an import in AddrSize,
482 * and the other one for an export in ExportSize.
484 S->AddrSize = AddrSize;
485 if (S->AddrSize == ADDR_SIZE_DEFAULT) {
486 /* Use the size of the current segment */
487 S->AddrSize = GetCurrentSegAddrSize ();
489 S->ExportSize = AddrSize;
490 S->Flags |= (SF_GLOBAL | Flags);
496 void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Prio)
497 /* Mark the given symbol as a module constructor/destructor. This will also
498 * mark the symbol as an export. Initializers may never be zero page symbols.
501 /* Check the parameters */
502 #if (CD_TYPE_MIN != 0)
503 CHECK (Type >= CD_TYPE_MIN && Type <= CD_TYPE_MAX);
505 CHECK (Type <= CD_TYPE_MAX);
507 CHECK (Prio >= CD_PRIO_MIN && Prio <= CD_PRIO_MAX);
509 /* Check for errors */
510 if (S->Flags & SF_IMPORT) {
511 /* The symbol is already marked as imported external symbol */
512 Error ("Symbol `%m%p' is already an import", GetSymName (S));
515 if (S->Flags & SF_VAR) {
516 /* Variable symbols cannot be exported or imported */
517 Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
521 /* If the symbol was already marked as an export or global, check if
522 * this was done specifiying the same address size. In case of a global
523 * declaration, silently remove the global flag.
525 if (S->Flags & (SF_EXPORT | SF_GLOBAL)) {
526 if (S->ExportSize != AddrSize) {
527 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
529 S->Flags &= ~SF_GLOBAL;
531 S->ExportSize = AddrSize;
533 /* If the symbol is already defined, check symbol size against the
536 if (S->Flags & SF_DEFINED) {
537 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
538 /* Use the real size of the symbol */
539 S->ExportSize = S->AddrSize;
540 } else if (S->AddrSize != S->ExportSize) {
541 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
545 /* If the symbol was already declared as a condes, check if the new
546 * priority value is the same as the old one.
548 if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
549 if (S->ConDesPrio[Type] != Prio) {
550 Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
553 S->ConDesPrio[Type] = Prio;
555 /* Set the symbol data */
556 S->Flags |= (SF_EXPORT | SF_REFERENCED);
561 void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize)
562 /* Mark the address size of the given symbol as guessed. The address size
563 * passed as argument is the one NOT used, because the actual address size
564 * wasn't known. Example: Zero page addressing was not used because symbol
565 * is undefined, and absolute addressing was available.
568 /* We must have a valid address size passed */
569 PRECONDITION (AddrSize != ADDR_SIZE_DEFAULT);
571 /* We do not support all address sizes currently */
572 if (AddrSize > sizeof (Sym->GuessedUse) / sizeof (Sym->GuessedUse[0])) {
576 /* We can only remember one such occurance */
577 if (Sym->GuessedUse[AddrSize-1]) {
581 /* Ok, remember the file position */
582 Sym->GuessedUse[AddrSize-1] = xdup (&CurTok.Pos, sizeof (CurTok.Pos));
587 void SymExportFromGlobal (SymEntry* S)
588 /* Called at the end of assembly. Converts a global symbol that is defined
592 /* Remove the global flag and make the symbol an export */
593 S->Flags &= ~SF_GLOBAL;
594 S->Flags |= SF_EXPORT;
599 void SymImportFromGlobal (SymEntry* S)
600 /* Called at the end of assembly. Converts a global symbol that is undefined
604 /* Remove the global flag and make it an import */
605 S->Flags &= ~SF_GLOBAL;
606 S->Flags |= SF_IMPORT;
611 int SymIsConst (const SymEntry* S, long* Val)
612 /* Return true if the given symbol has a constant value. If Val is not NULL
613 * and the symbol has a constant value, store it's value there.
616 /* Check for constness */
617 return (SymHasExpr (S) && IsConstExpr (S->Expr, Val));
622 SymTable* GetSymParentScope (SymEntry* S)
623 /* Get the parent scope of the symbol (not the one it is defined in). Return
624 * NULL if the symbol is a cheap local, or defined on global level.
627 if ((S->Flags & SF_LOCAL) != 0) {
628 /* This is a cheap local symbol */
630 } else if (S->Sym.Tab == 0) {
631 /* Symbol not in a table. This may happen if there have been errors
632 * before. Return NULL in this case to avoid further errors.
636 /* This is a global symbol */
637 return S->Sym.Tab->Parent;
643 struct ExprNode* GetSymExpr (SymEntry* S)
644 /* Get the expression for a non-const symbol */
646 PRECONDITION (S != 0 && SymHasExpr (S));
652 const struct ExprNode* SymResolve (const SymEntry* S)
653 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
654 * NULL. Do not call in other contexts!
657 return SymHasExpr (S)? S->Expr : 0;
662 long GetSymVal (SymEntry* S)
663 /* Return the value of a symbol assuming it's constant. FAIL will be called
664 * in case the symbol is undefined or not constant.
668 CHECK (S != 0 && SymHasExpr (S) && IsConstExpr (GetSymExpr (S), &Val));
674 unsigned GetSymImportId (const SymEntry* S)
675 /* Return the import id for the given symbol */
677 PRECONDITION (S != 0 && (S->Flags & SF_IMPORT) && S->ImportId != ~0U);
683 unsigned GetSymInfoFlags (const SymEntry* S, long* ConstVal)
684 /* Return a set of flags used when writing symbol information into a file.
685 * If the SYM_CONST bit is set, ConstVal will contain the constant value
686 * of the symbol. The result does not include the condes count.
687 * See common/symdefs.h for more information.
690 /* Setup info flags */
692 Flags |= SymIsConst (S, ConstVal)? SYM_CONST : SYM_EXPR;
693 Flags |= (S->Flags & SF_LABEL)? SYM_LABEL : SYM_EQUATE;
694 Flags |= (S->Flags & SF_LOCAL)? SYM_CHEAP_LOCAL : SYM_STD;
695 if (S->Flags & SF_EXPORT) {
698 if (S->Flags & SF_IMPORT) {
702 /* Return the result */