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->DefLines = EmptyCollection;
90 S->RefLines = EmptyCollection;
91 for (I = 0; I < sizeof (S->GuessedUse) / sizeof (S->GuessedUse[0]); ++I) {
100 S->ExprRefs = AUTO_COLLECTION_INITIALIZER;
101 S->ExportSize = ADDR_SIZE_DEFAULT;
102 S->AddrSize = ADDR_SIZE_DEFAULT;
103 memset (S->ConDesPrio, 0, sizeof (S->ConDesPrio));
104 S->Name = GetStrBufId (Name);
106 /* Insert it into the list of all entries */
110 /* Return the initialized entry */
116 int SymSearchTree (SymEntry* T, const StrBuf* Name, SymEntry** E)
117 /* Search in the given tree for a name. If we find the symbol, the function
118 ** will return 0 and put the entry pointer into E. If we did not find the
119 ** symbol, and the tree is empty, E is set to NULL. If the tree is not empty,
120 ** E will be set to the last entry, and the result of the function is <0 if
121 ** the entry should be inserted on the left side, and >0 if it should get
122 ** inserted on the right side.
125 /* Is there a tree? */
131 /* We have a table, search it */
134 /* Get the symbol name */
135 const StrBuf* SymName = GetStrBuf (T->Name);
137 /* Choose next entry */
138 int Cmp = SB_Compare (Name, SymName);
139 if (Cmp < 0 && T->Left) {
141 } else if (Cmp > 0 && T->Right) {
144 /* Found or end of search, return the result */
153 void SymTransferExprRefs (SymEntry* From, SymEntry* To)
154 /* Transfer all expression references from one symbol to another. */
158 for (I = 0; I < CollCount (&From->ExprRefs); ++I) {
160 /* Get the expression node */
161 ExprNode* E = CollAtUnchecked (&From->ExprRefs, I);
164 CHECK (E->Op == EXPR_SYMBOL && E->V.Sym == From);
166 /* Replace the symbol reference */
169 /* Add the expression reference */
170 SymAddExprRef (To, E);
173 /* Remove all symbol references from the old symbol */
174 CollDeleteAll (&From->ExprRefs);
179 static void SymReplaceExprRefs (SymEntry* S)
180 /* Replace the references to this symbol by a copy of the symbol expression */
185 /* Check if the expression is const and get its value */
186 int IsConst = IsConstExpr (S->Expr, &Val);
189 /* Loop over all references */
190 for (I = 0; I < CollCount (&S->ExprRefs); ++I) {
192 /* Get the expression node */
193 ExprNode* E = CollAtUnchecked (&S->ExprRefs, I);
196 CHECK (E->Op == EXPR_SYMBOL && E->V.Sym == S);
198 /* We cannot touch the root node, since there are pointers to it.
199 ** Replace it by a literal node.
201 E->Op = EXPR_LITERAL;
205 /* Remove all symbol references from the symbol */
206 CollDeleteAll (&S->ExprRefs);
211 void SymDef (SymEntry* S, ExprNode* Expr, unsigned char AddrSize, unsigned Flags)
212 /* Define a new symbol */
214 if (S->Flags & SF_IMPORT) {
215 /* Defined symbol is marked as imported external symbol */
216 Error ("Symbol `%m%p' is already an import", GetSymName (S));
219 if ((Flags & SF_VAR) != 0 && (S->Flags & (SF_EXPORT | SF_GLOBAL))) {
220 /* Variable symbols cannot be exports or globals */
221 Error ("Var symbol `%m%p' cannot be an export or global symbol", GetSymName (S));
224 if (S->Flags & SF_DEFINED) {
225 /* Multiple definition. In case of a variable, this is legal. */
226 if ((S->Flags & SF_VAR) == 0) {
227 Error ("Symbol `%m%p' is already defined", GetSymName (S));
228 S->Flags |= SF_MULTDEF;
231 /* Redefinition must also be a variable symbol */
232 if ((Flags & SF_VAR) == 0) {
233 Error ("Symbol `%m%p' is already different kind", GetSymName (S));
236 /* Delete the current symbol expression, since it will get
244 /* Map a default address size to a real value */
245 if (AddrSize == ADDR_SIZE_DEFAULT) {
246 /* ### Must go! Delay address size calculation until end of assembly! */
249 StudyExpr (Expr, &ED);
250 AddrSize = ED.AddrSize;
254 /* Set the symbol value */
257 /* In case of a variable symbol, walk over all expressions containing
258 ** this symbol and replace the (sub-)expression by the literal value of
259 ** the tree. Be sure to replace the expression node in place, since there
260 ** may be pointers to it.
262 if (Flags & SF_VAR) {
263 SymReplaceExprRefs (S);
266 /* If the symbol is marked as global, export it. Address size is checked
269 if (S->Flags & SF_GLOBAL) {
270 S->Flags = (S->Flags & ~SF_GLOBAL) | SF_EXPORT;
271 ReleaseFullLineInfo (&S->DefLines);
274 /* Mark the symbol as defined and use the given address size */
275 S->Flags |= (SF_DEFINED | Flags);
276 S->AddrSize = AddrSize;
278 /* Remember the line info of the symbol definition */
279 GetFullLineInfo (&S->DefLines);
281 /* If the symbol is exported, check the address sizes */
282 if (S->Flags & SF_EXPORT) {
283 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
284 /* Use the real size of the symbol */
285 S->ExportSize = S->AddrSize;
286 } else if (S->AddrSize > S->ExportSize) {
287 /* We're exporting a symbol smaller than it actually is */
288 Warning (1, "Symbol `%m%p' is %s but exported %s",
289 GetSymName (S), AddrSizeToStr (S->AddrSize),
290 AddrSizeToStr (S->ExportSize));
294 /* If this is not a local symbol, remember it as the last global one */
295 if ((S->Flags & SF_LOCAL) == 0) {
302 void SymRef (SymEntry* S)
303 /* Mark the given symbol as referenced */
305 /* Mark the symbol as referenced */
306 S->Flags |= SF_REFERENCED;
308 /* Remember the current location */
309 CollAppend (&S->RefLines, GetAsmLineInfo ());
314 void SymImport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
315 /* Mark the given symbol as an imported symbol */
317 if (S->Flags & SF_DEFINED) {
318 Error ("Symbol `%m%p' is already defined", GetSymName (S));
319 S->Flags |= SF_MULTDEF;
322 if (S->Flags & SF_EXPORT) {
323 /* The symbol is already marked as exported symbol */
324 Error ("Cannot import exported symbol `%m%p'", GetSymName (S));
328 /* If no address size is given, use the address size of the enclosing
331 if (AddrSize == ADDR_SIZE_DEFAULT) {
332 AddrSize = GetCurrentSegAddrSize ();
335 /* If the symbol is marked as import or global, check the address size,
336 ** then do silently remove the global flag.
338 if (S->Flags & SF_IMPORT) {
339 if ((Flags & SF_FORCED) != (S->Flags & SF_FORCED)) {
340 Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
342 if (AddrSize != S->AddrSize) {
343 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
346 if (S->Flags & SF_GLOBAL) {
347 S->Flags &= ~SF_GLOBAL;
348 if (AddrSize != S->AddrSize) {
349 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
353 /* Set the symbol data */
354 S->Flags |= (SF_IMPORT | Flags);
355 S->AddrSize = AddrSize;
357 /* Mark the position of the import as the position of the definition.
358 ** Please note: In case of multiple .global or .import statements, the line
361 GetFullLineInfo (&S->DefLines);
366 void SymExport (SymEntry* S, unsigned char AddrSize, unsigned Flags)
367 /* Mark the given symbol as an exported symbol */
369 /* Check if it's ok to export the symbol */
370 if (S->Flags & SF_IMPORT) {
371 /* The symbol is already marked as imported external symbol */
372 Error ("Symbol `%m%p' is already an import", GetSymName (S));
375 if (S->Flags & SF_VAR) {
376 /* Variable symbols cannot be exported */
377 Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
381 /* If the symbol was marked as global before, remove the global flag and
382 ** proceed, but check the address size.
384 if (S->Flags & SF_GLOBAL) {
385 if (AddrSize != S->ExportSize) {
386 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
388 S->Flags &= ~SF_GLOBAL;
390 /* .GLOBAL remembers line infos in case an .IMPORT follows. We have
391 ** to remove these here.
393 ReleaseFullLineInfo (&S->DefLines);
396 /* If the symbol was already marked as an export, but wasn't defined
397 ** before, the address sizes in both definitions must match.
399 if ((S->Flags & (SF_EXPORT|SF_DEFINED)) == SF_EXPORT) {
400 if (S->ExportSize != AddrSize) {
401 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
404 S->ExportSize = AddrSize;
406 /* If the symbol is already defined, check symbol size against the
409 if (S->Flags & SF_DEFINED) {
410 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
411 /* No export size given, use the real size of the symbol */
412 S->ExportSize = S->AddrSize;
413 } else if (S->AddrSize > S->ExportSize) {
414 /* We're exporting a symbol smaller than it actually is */
415 Warning (1, "Symbol `%m%p' is %s but exported %s",
416 GetSymName (S), AddrSizeToStr (S->AddrSize),
417 AddrSizeToStr (S->ExportSize));
421 /* Set the symbol data */
422 S->Flags |= (SF_EXPORT | SF_REFERENCED | Flags);
424 /* Remember line info for this reference */
425 CollAppend (&S->RefLines, GetAsmLineInfo ());
430 void SymGlobal (SymEntry* S, unsigned char AddrSize, unsigned Flags)
431 /* Mark the given symbol as a global symbol, that is, as a symbol that is
432 ** either imported or exported.
435 if (S->Flags & SF_VAR) {
436 /* Variable symbols cannot be exported or imported */
437 Error ("Var symbol `%m%p' cannot be made global", GetSymName (S));
441 /* If the symbol is already marked as import, the address size must match.
442 ** Apart from that, ignore the global declaration.
444 if (S->Flags & SF_IMPORT) {
445 if (AddrSize == ADDR_SIZE_DEFAULT) {
446 /* Use the size of the current segment */
447 AddrSize = GetCurrentSegAddrSize ();
449 if (AddrSize != S->AddrSize) {
450 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
455 /* If the symbol is already an export: If it is not defined, the address
458 if (S->Flags & SF_EXPORT) {
459 if ((S->Flags & SF_DEFINED) == 0) {
460 /* Symbol is undefined */
461 if (AddrSize != S->ExportSize) {
462 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
464 } else if (AddrSize != ADDR_SIZE_DEFAULT) {
465 /* Symbol is defined and address size given */
466 if (AddrSize != S->ExportSize) {
467 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
473 /* If the symbol is already marked as global, the address size must match.
474 ** Use the ExportSize here, since it contains the actual address size
475 ** passed to this function.
477 if (S->Flags & SF_GLOBAL) {
478 if (AddrSize != S->ExportSize) {
479 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
484 /* If we come here, the symbol was neither declared as export, import or
485 ** global before. Check if it is already defined, in which case it will
486 ** become an export. If it is not defined, mark it as global and remember
487 ** the given address sizes.
489 if (S->Flags & SF_DEFINED) {
490 /* The symbol is defined, export it */
491 S->ExportSize = AddrSize;
492 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
493 /* No export size given, use the real size of the symbol */
494 S->ExportSize = S->AddrSize;
495 } else if (S->AddrSize > S->ExportSize) {
496 /* We're exporting a symbol smaller than it actually is */
497 Warning (1, "Symbol `%m%p' is %s but exported %s",
498 GetSymName (S), AddrSizeToStr (S->AddrSize),
499 AddrSizeToStr (S->ExportSize));
501 S->Flags |= (SF_EXPORT | Flags);
503 /* Since we don't know if the symbol will get exported or imported,
504 ** remember two different address sizes: One for an import in AddrSize,
505 ** and the other one for an export in ExportSize.
507 S->AddrSize = AddrSize;
508 if (S->AddrSize == ADDR_SIZE_DEFAULT) {
509 /* Use the size of the current segment */
510 S->AddrSize = GetCurrentSegAddrSize ();
512 S->ExportSize = AddrSize;
513 S->Flags |= (SF_GLOBAL | Flags);
515 /* Remember the current location as location of definition in case
516 ** an .IMPORT follows later.
518 GetFullLineInfo (&S->DefLines);
524 void SymConDes (SymEntry* S, unsigned char AddrSize, unsigned Type, unsigned Prio)
525 /* Mark the given symbol as a module constructor/destructor. This will also
526 ** mark the symbol as an export. Initializers may never be zero page symbols.
529 /* Check the parameters */
530 #if (CD_TYPE_MIN != 0)
531 CHECK (Type >= CD_TYPE_MIN && Type <= CD_TYPE_MAX);
533 CHECK (Type <= CD_TYPE_MAX);
535 CHECK (Prio >= CD_PRIO_MIN && Prio <= CD_PRIO_MAX);
537 /* Check for errors */
538 if (S->Flags & SF_IMPORT) {
539 /* The symbol is already marked as imported external symbol */
540 Error ("Symbol `%m%p' is already an import", GetSymName (S));
543 if (S->Flags & SF_VAR) {
544 /* Variable symbols cannot be exported or imported */
545 Error ("Var symbol `%m%p' cannot be exported", GetSymName (S));
549 /* If the symbol was already marked as an export or global, check if
550 ** this was done specifiying the same address size. In case of a global
551 ** declaration, silently remove the global flag.
553 if (S->Flags & (SF_EXPORT | SF_GLOBAL)) {
554 if (S->ExportSize != AddrSize) {
555 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
557 S->Flags &= ~SF_GLOBAL;
559 S->ExportSize = AddrSize;
561 /* If the symbol is already defined, check symbol size against the
564 if (S->Flags & SF_DEFINED) {
565 if (S->ExportSize == ADDR_SIZE_DEFAULT) {
566 /* Use the real size of the symbol */
567 S->ExportSize = S->AddrSize;
568 } else if (S->AddrSize != S->ExportSize) {
569 Error ("Address size mismatch for symbol `%m%p'", GetSymName (S));
573 /* If the symbol already was declared as a condes of this type,
574 ** check if the new priority value is the same as the old one.
576 if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
577 if (S->ConDesPrio[Type] != Prio) {
578 Error ("Redeclaration mismatch for symbol `%m%p'", GetSymName (S));
581 S->ConDesPrio[Type] = Prio;
583 /* Set the symbol data */
584 S->Flags |= (SF_EXPORT | SF_REFERENCED);
586 /* Remember the line info for this reference */
587 CollAppend (&S->RefLines, GetAsmLineInfo ());
592 void SymGuessedAddrSize (SymEntry* Sym, unsigned char AddrSize)
593 /* Mark the address size of the given symbol as guessed. The address size
594 ** passed as argument is the one NOT used, because the actual address size
595 ** wasn't known. Example: Zero page addressing was not used because symbol
596 ** is undefined, and absolute addressing was available.
599 /* We must have a valid address size passed */
600 PRECONDITION (AddrSize != ADDR_SIZE_DEFAULT);
602 /* We do not support all address sizes currently */
603 if (AddrSize > sizeof (Sym->GuessedUse) / sizeof (Sym->GuessedUse[0])) {
607 /* We can only remember one such occurance */
608 if (Sym->GuessedUse[AddrSize-1]) {
612 /* Ok, remember the file position */
613 Sym->GuessedUse[AddrSize-1] = xdup (&CurTok.Pos, sizeof (CurTok.Pos));
618 void SymExportFromGlobal (SymEntry* S)
619 /* Called at the end of assembly. Converts a global symbol that is defined
623 /* Remove the global flag and make the symbol an export */
624 S->Flags &= ~SF_GLOBAL;
625 S->Flags |= SF_EXPORT;
630 void SymImportFromGlobal (SymEntry* S)
631 /* Called at the end of assembly. Converts a global symbol that is undefined
635 /* Remove the global flag and make it an import */
636 S->Flags &= ~SF_GLOBAL;
637 S->Flags |= SF_IMPORT;
642 int SymIsConst (const SymEntry* S, long* Val)
643 /* Return true if the given symbol has a constant value. If Val is not NULL
644 ** and the symbol has a constant value, store it's value there.
647 /* Check for constness */
648 return (SymHasExpr (S) && IsConstExpr (S->Expr, Val));
653 SymTable* GetSymParentScope (SymEntry* S)
654 /* Get the parent scope of the symbol (not the one it is defined in). Return
655 ** NULL if the symbol is a cheap local, or defined on global level.
658 if ((S->Flags & SF_LOCAL) != 0) {
659 /* This is a cheap local symbol */
661 } else if (S->Sym.Tab == 0) {
662 /* Symbol not in a table. This may happen if there have been errors
663 ** before. Return NULL in this case to avoid further errors.
667 /* This is a global symbol */
668 return S->Sym.Tab->Parent;
674 struct ExprNode* GetSymExpr (SymEntry* S)
675 /* Get the expression for a non-const symbol */
677 PRECONDITION (S != 0 && SymHasExpr (S));
683 const struct ExprNode* SymResolve (const SymEntry* S)
684 /* Helper function for DumpExpr. Resolves a symbol into an expression or return
685 ** NULL. Do not call in other contexts!
688 return SymHasExpr (S)? S->Expr : 0;
693 long GetSymVal (SymEntry* S)
694 /* Return the value of a symbol assuming it's constant. FAIL will be called
695 ** in case the symbol is undefined or not constant.
699 CHECK (S != 0 && SymHasExpr (S) && IsConstExpr (GetSymExpr (S), &Val));
705 unsigned GetSymImportId (const SymEntry* S)
706 /* Return the import id for the given symbol */
708 PRECONDITION (S != 0 && (S->Flags & SF_IMPORT) && S->ImportId != ~0U);
714 unsigned GetSymExportId (const SymEntry* S)
715 /* Return the export id for the given symbol */
717 PRECONDITION (S != 0 && (S->Flags & SF_EXPORT) && S->ExportId != ~0U);
723 unsigned GetSymInfoFlags (const SymEntry* S, long* ConstVal)
724 /* Return a set of flags used when writing symbol information into a file.
725 ** If the SYM_CONST bit is set, ConstVal will contain the constant value
726 ** of the symbol. The result does not include the condes count.
727 ** See common/symdefs.h for more information.
730 /* Setup info flags */
732 Flags |= SymIsConst (S, ConstVal)? SYM_CONST : SYM_EXPR;
733 Flags |= (S->Flags & SF_LABEL)? SYM_LABEL : SYM_EQUATE;
734 Flags |= (S->Flags & SF_LOCAL)? SYM_CHEAP_LOCAL : SYM_STD;
735 if (S->Flags & SF_EXPORT) {
738 if (S->Flags & SF_IMPORT) {
742 /* Return the result */