1 /*****************************************************************************/
5 /* Segments for the ca65 macroassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
63 /* Are we in absolute mode or in relocatable mode? */
65 unsigned long AbsPC = 0; /* PC if in absolute mode */
69 typedef struct Segment Segment;
71 Segment* List; /* List of all segments */
72 Fragment* Root; /* Root of fragment list */
73 Fragment* Last; /* Pointer to last fragment */
74 unsigned long FragCount; /* Number of fragments */
75 unsigned Num; /* Segment number */
76 unsigned Align; /* Segment alignment */
78 SegDef* Def; /* Segment definition (name and type) */
82 #define SEG(segdef, num, prev) \
83 { prev, 0, 0, 0, num, 0, 0, segdef }
85 /* Definitions for predefined segments */
86 SegDef NullSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_NULL, SEGTYPE_ABS);
87 SegDef ZeropageSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_ZEROPAGE, SEGTYPE_ZP);
88 SegDef DataSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_DATA, SEGTYPE_ABS);
89 SegDef BssSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_BSS, SEGTYPE_ABS);
90 SegDef RODataSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_RODATA, SEGTYPE_ABS);
91 SegDef CodeSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_CODE, SEGTYPE_ABS);
93 /* Predefined segments */
94 static Segment NullSeg = SEG (&NullSegDef, 5, NULL);
95 static Segment ZeropageSeg = SEG (&ZeropageSegDef, 4, &NullSeg);
96 static Segment DataSeg = SEG (&DataSegDef, 3, &ZeropageSeg);
97 static Segment BssSeg = SEG (&BssSegDef, 2, &DataSeg);
98 static Segment RODataSeg = SEG (&RODataSegDef, 1, &BssSeg);
99 static Segment CodeSeg = SEG (&CodeSegDef, 0, &RODataSeg);
101 /* Number of segments */
102 static unsigned SegmentCount = 6;
104 /* List of all segments */
105 static Segment* SegmentList = &CodeSeg;
106 static Segment* SegmentLast = &NullSeg;
108 /* Currently active segment */
109 static Segment* ActiveSeg = &CodeSeg;
113 /*****************************************************************************/
115 /*****************************************************************************/
119 static Segment* NewSegment (const char* Name, unsigned SegType)
120 /* Create a new segment, insert it into the global list and return it */
124 /* Check for too many segments */
125 if (SegmentCount >= 256) {
126 Fatal (FAT_TOO_MANY_SEGMENTS);
129 /* Check the segment name for invalid names */
130 if (!ValidSegName (Name)) {
131 Error (ERR_ILLEGAL_SEGMENT, Name);
134 /* Create a new segment */
135 S = xmalloc (sizeof (*S));
142 S->Num = SegmentCount++;
145 S->Def = NewSegDef (Name, SegType);
147 /* Insert it into the segment list */
148 SegmentLast->List = S;
151 /* And return it... */
157 Fragment* GenFragment (unsigned char Type, unsigned short Len)
158 /* Generate a new fragment, add it to the current segment and return it. */
160 /* Create the new fragment */
161 Fragment* F = NewFragment (Type, Len);
163 /* Insert the fragment into the current segment */
164 if (ActiveSeg->Root) {
165 ActiveSeg->Last->Next = F;
168 ActiveSeg->Root = ActiveSeg->Last = F;
170 ++ActiveSeg->FragCount;
172 /* Add this fragment to the current listing line */
174 if (LineCur->FragList == 0) {
175 LineCur->FragList = F;
177 LineCur->FragLast->LineList = F;
179 LineCur->FragLast = F;
182 /* Increment the program counter */
183 ActiveSeg->PC += F->Len;
188 /* Return the fragment */
194 void UseSeg (const SegDef* D)
195 /* Use the segment with the given name */
197 Segment* Seg = SegmentList;
199 if (strcmp (Seg->Def->Name, D->Name) == 0) {
200 /* We found this segment. Check if the type is identical */
201 if (D->Type != SEGTYPE_DEFAULT && Seg->Def->Type != D->Type) {
202 Error (ERR_SEG_ATTR_MISMATCH);
203 /* Use the new attribute to avoid errors */
204 Seg->Def->Type = D->Type;
209 /* Check next segment */
213 /* Segment is not in list, create a new one */
214 if (D->Type == SEGTYPE_DEFAULT) {
215 Seg = NewSegment (D->Name, SEGTYPE_ABS);
217 Seg = NewSegment (D->Name, D->Type);
224 unsigned long GetPC (void)
225 /* Get the program counter of the current segment */
227 return RelocMode? ActiveSeg->PC : AbsPC;
232 void SetAbsPC (unsigned long PC)
233 /* Set the program counter in absolute mode */
241 const SegDef* GetCurrentSeg (void)
242 /* Get a pointer to the segment defininition of the current segment */
244 return ActiveSeg->Def;
249 unsigned GetSegNum (void)
250 /* Get the number of the current segment */
252 return ActiveSeg->Num;
257 void SegAlign (unsigned Power, int Val)
258 /* Align the PC segment to 2^Power. If Val is -1, emit fill fragments (the
259 * actual fill value will be determined by the linker), otherwise use the
263 unsigned char Data [4];
264 unsigned long Align = (1UL << Power) - 1;
265 unsigned long NewPC = (ActiveSeg->PC + Align) & ~Align;
266 unsigned long Count = NewPC - ActiveSeg->PC;
269 /* User defined fill value */
270 memset (Data, Val, sizeof (Data));
272 if (Count > sizeof (Data)) {
273 EmitData (Data, sizeof (Data));
274 Count -= sizeof (Data);
276 EmitData (Data, Count);
281 /* Linker defined fill value */
285 /* Remember the alignment in the header */
286 if (ActiveSeg->Align < Power) {
287 ActiveSeg->Align = Power;
294 /* Return true if the current segment is a zeropage segment */
296 return (ActiveSeg->Def->Type == SEGTYPE_ZP);
302 /* Return true if the current segment is a far segment */
304 return (ActiveSeg->Def->Type == SEGTYPE_FAR);
309 unsigned GetSegType (unsigned SegNum)
310 /* Return the type of the segment with the given number */
312 /* Search for the segment */
313 Segment* S = SegmentList;
314 while (S && SegNum) {
319 /* Did we find it? */
321 FAIL ("Invalid segment number");
324 /* Return the segment type */
331 /* Check the segments for range and other errors */
333 Segment* S = SegmentList;
335 Fragment* F = S->Root;
337 if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
338 F->V.Expr = FinalizeExpr (F->V.Expr);
339 if (IsConstExpr (F->V.Expr)) {
340 /* We are able to evaluate the expression. Get the value
341 * and check for range errors.
344 long Val = GetExprVal (F->V.Expr);
345 int Abs = (F->Type != FRAG_SEXPR);
351 PError (&F->Pos, ERR_RANGE);
354 /* PC relative value */
355 if (Val < -128 || Val > 127) {
356 PError (&F->Pos, ERR_RANGE);
359 } else if (F->Len == 2) {
363 PError (&F->Pos, ERR_RANGE);
366 /* PC relative value */
367 if (Val < -32768 || Val > 32767) {
368 PError (&F->Pos, ERR_RANGE);
373 /* We don't need the expression tree any longer */
374 FreeExpr (F->V.Expr);
376 /* Convert the fragment into a literal fragment */
377 for (I = 0; I < F->Len; ++I) {
378 F->V.Data [I] = Val & 0xFF;
381 F->Type = FRAG_LITERAL;
383 /* We cannot evaluate the expression now, leave the job for
384 * the linker. However, we are able to check for explicit
385 * byte expressions and we will do so.
387 if (F->Type == FRAG_EXPR && F->Len == 1 && !IsByteExpr (F->V.Expr)) {
388 PError (&F->Pos, ERR_RANGE);
401 /* Dump the contents of all segments */
404 Segment* S = SegmentList;
410 printf ("New segment: %s", S->Def->Name);
413 if (F->Type == FRAG_LITERAL) {
415 printf ("\n Literal:");
419 for (I = 0; I < F->Len; ++I) {
420 printf (" %02X", F->V.Data [I]);
423 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
425 printf ("\n Expression (%u): ", F->Len);
426 DumpExpr (F->V.Expr);
427 } else if (F->Type == FRAG_FILL) {
429 printf ("\n Fill bytes (%u)", F->Len);
431 Internal ("Unknown fragment type: %u", F->Type);
438 printf ("\n End PC = $%04X\n", (unsigned)(S->PC & 0xFFFF));
446 static void WriteOneSeg (Segment* Seg)
447 /* Write one segment to the object file */
450 unsigned LineInfoIndex;
451 unsigned long DataSize;
452 unsigned long EndPos;
454 /* Remember the file position, then write a dummy for the size of the
457 unsigned long SizePos = ObjGetFilePos ();
460 /* Write the segment data */
461 ObjWriteVar (GetStringId (Seg->Def->Name)); /* Name of the segment */
462 ObjWrite32 (Seg->PC); /* Size */
463 ObjWrite8 (Seg->Align); /* Segment alignment */
464 ObjWrite8 (Seg->Def->Type); /* Type of the segment */
465 ObjWriteVar (Seg->FragCount); /* Number of fragments */
467 /* Now walk through the fragment list for this segment and write the
473 /* Write data depending on the type */
474 switch (Frag->Type) {
477 ObjWrite8 (FRAG_LITERAL);
478 ObjWriteVar (Frag->Len);
479 ObjWriteData (Frag->V.Data, Frag->Len);
484 case 1: ObjWrite8 (FRAG_EXPR8); break;
485 case 2: ObjWrite8 (FRAG_EXPR16); break;
486 case 3: ObjWrite8 (FRAG_EXPR24); break;
487 case 4: ObjWrite8 (FRAG_EXPR32); break;
488 default: Internal ("Invalid fragment size: %u", Frag->Len);
490 WriteExpr (Frag->V.Expr);
495 case 1: ObjWrite8 (FRAG_SEXPR8); break;
496 case 2: ObjWrite8 (FRAG_SEXPR16); break;
497 case 3: ObjWrite8 (FRAG_SEXPR24); break;
498 case 4: ObjWrite8 (FRAG_SEXPR32); break;
499 default: Internal ("Invalid fragment size: %u", Frag->Len);
501 WriteExpr (Frag->V.Expr);
505 ObjWrite8 (FRAG_FILL);
506 ObjWriteVar (Frag->Len);
510 Internal ("Invalid fragment type: %u", Frag->Type);
514 /* Write the file position of this fragment */
515 ObjWritePos (&Frag->Pos);
517 /* Write extra line info for this fragment. Zero is considered
518 * "no line info", so add one to the value.
520 LineInfoIndex = Frag->LI? Frag->LI->Index + 1 : 0;
521 ObjWriteVar (LineInfoIndex);
527 /* Calculate the size of the data, seek back and write it */
528 EndPos = ObjGetFilePos (); /* Remember where we are */
529 DataSize = EndPos - SizePos - 4; /* Don't count size itself */
530 ObjSetFilePos (SizePos); /* Seek back to the size */
531 ObjWrite32 (DataSize); /* Write the size */
532 ObjSetFilePos (EndPos); /* Seek back to the end */
537 void WriteSegments (void)
538 /* Write the segment data to the object file */
542 /* Tell the object file module that we're about to start the seg list */
545 /* First thing is segment count */
546 ObjWriteVar (SegmentCount);
548 /* Now walk through all segments and write them to the object file */
551 /* Write one segment */
557 /* Done writing segments */