]> git.sur5r.net Git - cc65/blob - src/ld65/fragment.c
65f10bed0f86b6d3a6c042888405a019916a78bd
[cc65] / src / ld65 / fragment.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                fragment.c                                 */
4 /*                                                                           */
5 /*                        Code/data fragment routines                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 "fragdefs.h"
38 #include "xmalloc.h"
39
40 /* ld65 */
41 #include "error.h"
42 #include "expr.h"
43 #include "fragment.h"
44 #include "fileio.h"
45 #include "segments.h"
46 #include "spool.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Code                                    */
52 /*****************************************************************************/
53
54
55
56 static FragCheck* NewFragCheck (unsigned Action)
57 /* Allocate a new FragCheck struct and return it */
58 {
59     /* Allocate memory */
60     FragCheck* FC = xmalloc (sizeof (FragCheck));
61
62     /* Initialize the fields */
63     FC->Next    = 0;
64     FC->Expr    = 0;
65     FC->Action  = Action;
66     FC->Message = INVALID_STRING_ID;
67
68     /* Return the new struct */
69     return FC;
70 }
71
72
73
74 FragCheck* ReadFragCheck (FILE* F, Fragment* Frag)
75 /* Read a fragment check expression from the given file */
76 {
77     /* Get the object file pointer from the fragment */
78     ObjData* O = Frag->Obj;
79
80     /* Read the action and create a new struct */
81     FragCheck* FC = NewFragCheck (ReadVar (F));
82
83     /* Determine the remaining data from the action */
84     switch (FC->Action) {
85
86         case FRAG_ACT_WARN:
87         case FRAG_ACT_ERROR:
88             FC->Expr = ReadExpr (F, O);
89             FC->Message = MakeGlobalStringId (O, ReadVar (F));
90             break;
91
92         default:
93             Internal ("In module `%s', file `%s', line %lu: Invalid fragment "
94                       "check action: %u",
95                       GetObjFileName (O),
96                       GetSourceFileName (O, Frag->Pos.Name),
97                       Frag->Pos.Line, FC->Action);
98     }
99
100     /* Return the new fragment check */
101     return FC;
102 }
103
104
105
106 Fragment* NewFragment (unsigned char Type, unsigned Size, Section* S)
107 /* Create a new fragment and insert it into the section S */
108 {
109     Fragment* F;
110
111     /* Calculate the size of the memory block. LitBuf is only needed if the
112      * fragment contains literal data.
113      */
114     unsigned FragSize = sizeof (Fragment) - 1;
115     if (Type == FRAG_LITERAL) {
116         FragSize += Size;
117     }
118
119     /* Allocate memory */
120     F = xmalloc (FragSize);
121
122     /* Initialize the data */
123     F->Next      = 0;
124     F->Obj       = 0;
125     F->Size      = Size;
126     F->Expr      = 0;
127     InitFilePos (&F->Pos);
128     F->LI        = 0;
129     F->Check     = 0;
130     F->Type      = Type;
131
132     /* Insert the code fragment into the section */
133     if (S->FragRoot == 0) {
134         /* First fragment */
135         S->FragRoot = F;
136     } else {
137         S->FragLast->Next = F;
138     }
139     S->FragLast = F;
140
141     /* Increment the size of the section by the size of the fragment */
142     S->Size += Size;
143
144     /* Increment the size of the segment that contains the section */
145     S->Seg->Size += Size;
146
147     /* Return the new fragment */
148     return F;
149 }
150
151
152