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 /* Convert the fragment into a literal fragment */
374 for (I = 0; I < F->Len; ++I) {
375 F->V.Data [I] = Val & 0xFF;
378 F->Type = FRAG_LITERAL;
380 /* We cannot evaluate the expression now, leave the job for
381 * the linker. However, we are able to check for explicit
382 * byte expressions and we will do so.
384 if (F->Type == FRAG_EXPR && F->Len == 1 && !IsByteExpr (F->V.Expr)) {
385 PError (&F->Pos, ERR_RANGE);
398 /* Dump the contents of all segments */
401 Segment* S = SegmentList;
407 printf ("New segment: %s", S->Def->Name);
410 if (F->Type == FRAG_LITERAL) {
412 printf ("\n Literal:");
416 for (I = 0; I < F->Len; ++I) {
417 printf (" %02X", F->V.Data [I]);
420 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
422 printf ("\n Expression (%u): ", F->Len);
423 DumpExpr (F->V.Expr);
424 } else if (F->Type == FRAG_FILL) {
426 printf ("\n Fill bytes (%u)", F->Len);
428 Internal ("Unknown fragment type: %u", F->Type);
435 printf ("\n End PC = $%04X\n", (unsigned)(S->PC & 0xFFFF));
443 static void WriteOneSeg (Segment* Seg)
444 /* Write one segment to the object file */
447 unsigned LineInfoIndex;
448 unsigned long DataSize;
449 unsigned long EndPos;
451 /* Remember the file position, then write a dummy for the size of the
454 unsigned long SizePos = ObjGetFilePos ();
457 /* Write the segment data */
458 ObjWriteVar (GetStringId (Seg->Def->Name)); /* Name of the segment */
459 ObjWrite32 (Seg->PC); /* Size */
460 ObjWrite8 (Seg->Align); /* Segment alignment */
461 ObjWrite8 (Seg->Def->Type); /* Type of the segment */
462 ObjWriteVar (Seg->FragCount); /* Number of fragments */
464 /* Now walk through the fragment list for this segment and write the
470 /* Write data depending on the type */
471 switch (Frag->Type) {
474 ObjWrite8 (FRAG_LITERAL);
475 ObjWriteVar (Frag->Len);
476 ObjWriteData (Frag->V.Data, Frag->Len);
481 case 1: ObjWrite8 (FRAG_EXPR8); break;
482 case 2: ObjWrite8 (FRAG_EXPR16); break;
483 case 3: ObjWrite8 (FRAG_EXPR24); break;
484 case 4: ObjWrite8 (FRAG_EXPR32); break;
485 default: Internal ("Invalid fragment size: %u", Frag->Len);
487 WriteExpr (Frag->V.Expr);
492 case 1: ObjWrite8 (FRAG_SEXPR8); break;
493 case 2: ObjWrite8 (FRAG_SEXPR16); break;
494 case 3: ObjWrite8 (FRAG_SEXPR24); break;
495 case 4: ObjWrite8 (FRAG_SEXPR32); break;
496 default: Internal ("Invalid fragment size: %u", Frag->Len);
498 WriteExpr (Frag->V.Expr);
502 ObjWrite8 (FRAG_FILL);
503 ObjWriteVar (Frag->Len);
507 Internal ("Invalid fragment type: %u", Frag->Type);
511 /* Write the file position of this fragment */
512 ObjWritePos (&Frag->Pos);
514 /* Write extra line info for this fragment. Zero is considered
515 * "no line info", so add one to the value.
517 LineInfoIndex = Frag->LI? Frag->LI->Index + 1 : 0;
518 ObjWriteVar (LineInfoIndex);
524 /* Calculate the size of the data, seek back and write it */
525 EndPos = ObjGetFilePos (); /* Remember where we are */
526 DataSize = EndPos - SizePos - 4; /* Don't count size itself */
527 ObjSetFilePos (SizePos); /* Seek back to the size */
528 ObjWrite32 (DataSize); /* Write the size */
529 ObjSetFilePos (EndPos); /* Seek back to the end */
534 void WriteSegments (void)
535 /* Write the segment data to the object file */
539 /* Tell the object file module that we're about to start the seg list */
542 /* First thing is segment count */
543 ObjWriteVar (SegmentCount);
545 /* Now walk through all segments and write them to the object file */
548 /* Write one segment */
554 /* Done writing segments */