1/*****************************************************************************/
2/* */
3/* scopes.c */
4/* */
5/* Scope handling for the ld65 linker */
6/* */
7/* */
8/* */
9/* (C) 2011, Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
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. */
18/* */
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: */
22/* */
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 */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36/* common */
37#include "xmalloc.h"
38
39/* ld65 */
40#include "error.h"
41#include "fileio.h"
42#include "scopes.h"
43#include "span.h"
44#include "spool.h"
45
46
47
48/*****************************************************************************/
49/* Code */
50/*****************************************************************************/
51
52
53
54static Scope* NewScope (ObjData* Obj, unsigned Id)
55/* Create a new Scope and return it */
56{
57 /* Allocate memory */
58 Scope* S = xmalloc (sizeof (Scope));
59
60 /* Initialize the fields where necessary */
61 S->Id = Id;
62 S->Obj = Obj;
63 S->Size = 0;
64 S->LabelId = ~0U;
65 S->Spans = 0;
66
67 /* Return the new entry */
68 return S;
69}
70
71
72
73Scope* ReadScope (FILE* F, ObjData* Obj, unsigned Id)
74/* Read a scope from a file and return it */
75{
76 /* Create a new scope */
77 Scope* S = NewScope (Obj, Id);
78
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);
87 }
88 if (SCOPE_HAS_LABEL (S->Flags)) {
89 S->LabelId = ReadVar (F);
90 }
91 S->Spans = ReadSpanList (F);
92
93 /* Return the new Scope */
94 return S;
95}
96
97
98
99unsigned ScopeCount (void)
100/* Return the total number of scopes */
101{
102
103 /* Count scopes from all modules we have linked into the output file */
104 unsigned I;
105 unsigned Count = 0;
106 for (I = 0; I < CollCount (&ObjDataList); ++I) {
107 /* Get the object file */
108 const ObjData* O = CollAtUnchecked (&ObjDataList, I);
109
110 /* Account for the scopes in this file */
111 Count += CollCount (&O->Scopes);
112 }
113 return Count;
114}
115
116
117
118void PrintDbgScopes (FILE* F)
119/* Output the scopes to a debug info file */
120{
121 unsigned I, J;
122
123 /* Print scopes from all modules we have linked into the output file */
124 for (I = 0; I < CollCount (&ObjDataList); ++I) {
125
126 /* Get the object file */
127 ObjData* O = CollAtUnchecked (&ObjDataList, I);
128
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);
132
133 /* Output the first chunk of data */
134 fprintf (F,
135 "scope\tid=%u,name=\"%s\",mod=%u",
136 O->ScopeBaseId + S->Id,
137 GetString (S->Name),
138 I);
139
140 /* Print the type if not module */
141 switch (S->Type) {
142
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;
148
149 default:
150 Error ("Module '%s': Unknown scope type %u",
151 GetObjFileName (O), S->Type);
152 }
153
154 /* Print the size if available */
155 if (S->Size != 0) {
156 fprintf (F, ",size=%lu", S->Size);
157 }
158 /* Print parent if available */
159 if (S->Id != S->ParentId) {
160 fprintf (F, ",parent=%u", O->ScopeBaseId + S->ParentId);
161 }
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);
165 }
166 /* Print the list of spans for this scope */
167 PrintDbgSpanList (F, O, S->Spans);
168
169 /* Terminate the output line */
170 fputc ('\n', F);
171 }
172 }
173}
174