1 /*****************************************************************************/
5 /* Scope handling for the ld65 linker */
9 /* (C) 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 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 static Scope* NewScope (ObjData* Obj, unsigned Id)
55 /* Create a new Scope and return it */
58 Scope* S = xmalloc (sizeof (Scope));
60 /* Initialize the fields where necessary */
67 /* Return the new entry */
73 Scope* ReadScope (FILE* F, ObjData* Obj, unsigned Id)
74 /* Read a scope from a file and return it */
76 /* Create a new scope */
77 Scope* S = NewScope (Obj, Id);
79 /* Read the data from file */
80 S->ParentId = ReadVar (F);
81 S->LexicalLevel = ReadVar (F);
82 S->Flags = ReadVar (F);
83 S->Type = ReadVar (F);
84 S->Name = MakeGlobalStringId (Obj, ReadVar (F));
85 if (SCOPE_HAS_SIZE (S->Flags)) {
86 S->Size = ReadVar (F);
88 if (SCOPE_HAS_LABEL (S->Flags)) {
89 S->LabelId = ReadVar (F);
91 S->Spans = ReadSpanList (F);
93 /* Return the new Scope */
99 unsigned ScopeCount (void)
100 /* Return the total number of scopes */
103 /* Count scopes from all modules we have linked into the output file */
106 for (I = 0; I < CollCount (&ObjDataList); ++I) {
107 /* Get the object file */
108 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
110 /* Account for the scopes in this file */
111 Count += CollCount (&O->Scopes);
118 void PrintDbgScopes (FILE* F)
119 /* Output the scopes to a debug info file */
123 /* Print scopes from all modules we have linked into the output file */
124 for (I = 0; I < CollCount (&ObjDataList); ++I) {
126 /* Get the object file */
127 ObjData* O = CollAtUnchecked (&ObjDataList, I);
129 /* Output the scopes for this object file */
130 for (J = 0; J < CollCount (&O->Scopes); ++J) {
131 const Scope* S = CollConstAt (&O->Scopes, J);
133 /* Output the first chunk of data */
135 "scope\tid=%u,name=\"%s\",mod=%u",
136 O->ScopeBaseId + S->Id,
140 /* Print the type if not module */
143 case SCOPE_GLOBAL: fputs (",type=global", F); break;
144 case SCOPE_FILE: /* default */ break;
145 case SCOPE_SCOPE: fputs (",type=scope", F); break;
146 case SCOPE_STRUCT: fputs (",type=struct", F); break;
147 case SCOPE_ENUM: fputs (",type=enum", F); break;
150 Error ("Module `%s': Unknown scope type %u",
151 GetObjFileName (O), S->Type);
154 /* Print the size if available */
156 fprintf (F, ",size=%lu", S->Size);
158 /* Print parent if available */
159 if (S->Id != S->ParentId) {
160 fprintf (F, ",parent=%u", O->ScopeBaseId + S->ParentId);
162 /* Print the label id if the scope is labeled */
163 if (SCOPE_HAS_LABEL (S->Flags)) {
164 fprintf (F, ",sym=%u", O->SymBaseId + S->LabelId);
166 /* Print the list of spans for this scope */
167 PrintDbgSpanList (F, O, S->Spans);
169 /* Terminate the output line */