1 /*****************************************************************************/
5 /* Segments for the ca65 macroassembler */
9 /* (C) 1998-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 /*****************************************************************************/
41 #include "alignment.h"
59 #include "studyexpr.h"
64 /*****************************************************************************/
66 /*****************************************************************************/
70 /* If OrgPerSeg is false, all segments share the RelocMode flag and a PC
71 * used when in absolute mode. OrgPerSeg may be set by .feature org_per_seg
73 static int RelocMode = 1;
74 static unsigned long AbsPC = 0; /* PC if in absolute mode */
76 /* Definitions for predefined segments */
77 SegDef NullSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_NULL, ADDR_SIZE_ABS);
78 SegDef ZeropageSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_ZEROPAGE, ADDR_SIZE_ZP);
79 SegDef DataSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_DATA, ADDR_SIZE_ABS);
80 SegDef BssSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_BSS, ADDR_SIZE_ABS);
81 SegDef RODataSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_RODATA, ADDR_SIZE_ABS);
82 SegDef CodeSegDef = STATIC_SEGDEF_INITIALIZER (SEGNAME_CODE, ADDR_SIZE_ABS);
84 /* Collection containing all segments */
85 Collection SegmentList = STATIC_COLLECTION_INITIALIZER;
87 /* Currently active segment */
92 /*****************************************************************************/
94 /*****************************************************************************/
98 static Segment* NewSegFromDef (SegDef* Def)
99 /* Create a new segment from a segment definition. Used only internally, no
103 /* Create a new segment */
104 Segment* S = xmalloc (sizeof (*S));
110 S->Num = CollCount (&SegmentList);
111 S->Flags = SEG_FLAG_NONE;
118 /* Insert it into the segment list */
119 CollAppend (&SegmentList, S);
121 /* And return it... */
127 static Segment* NewSegment (const char* Name, unsigned char AddrSize)
128 /* Create a new segment, insert it into the global list and return it */
130 /* Check for too many segments */
131 if (CollCount (&SegmentList) >= 256) {
132 Fatal ("Too many segments");
135 /* Check the segment name for invalid names */
136 if (!ValidSegName (Name)) {
137 Error ("Illegal segment name: `%s'", Name);
140 /* Create a new segment and return it */
141 return NewSegFromDef (NewSegDef (Name, AddrSize));
146 Fragment* GenFragment (unsigned char Type, unsigned short Len)
147 /* Generate a new fragment, add it to the current segment and return it. */
149 /* Create the new fragment */
150 Fragment* F = NewFragment (Type, Len);
152 /* Insert the fragment into the current segment */
153 if (ActiveSeg->Root) {
154 ActiveSeg->Last->Next = F;
157 ActiveSeg->Root = ActiveSeg->Last = F;
159 ++ActiveSeg->FragCount;
161 /* Add this fragment to the current listing line */
163 if (LineCur->FragList == 0) {
164 LineCur->FragList = F;
166 LineCur->FragLast->LineList = F;
168 LineCur->FragLast = F;
171 /* Increment the program counter */
172 ActiveSeg->PC += F->Len;
174 /* Relocatable mode is switched per segment */
175 if (!ActiveSeg->RelocMode) {
176 ActiveSeg->AbsPC += F->Len;
179 /* Relocatable mode is switched globally */
185 /* Return the fragment */
191 void UseSeg (const SegDef* D)
192 /* Use the segment with the given name */
195 for (I = 0; I < CollCount (&SegmentList); ++I) {
196 Segment* Seg = CollAtUnchecked (&SegmentList, I);
197 if (strcmp (Seg->Def->Name, D->Name) == 0) {
198 /* We found this segment. Check if the type is identical */
199 if (D->AddrSize != ADDR_SIZE_DEFAULT &&
200 Seg->Def->AddrSize != D->AddrSize) {
201 Error ("Segment attribute mismatch");
202 /* Use the new attribute to avoid errors */
203 Seg->Def->AddrSize = D->AddrSize;
210 /* Segment is not in list, create a new one */
211 if (D->AddrSize == ADDR_SIZE_DEFAULT) {
212 ActiveSeg = NewSegment (D->Name, ADDR_SIZE_ABS);
214 ActiveSeg = NewSegment (D->Name, D->AddrSize);
220 unsigned long GetPC (void)
221 /* Get the program counter of the current segment */
224 /* Relocatable mode is switched per segment */
225 return ActiveSeg->RelocMode? ActiveSeg->PC : ActiveSeg->AbsPC;
227 /* Relocatable mode is switched globally */
228 return RelocMode? ActiveSeg->PC : AbsPC;
234 void EnterAbsoluteMode (unsigned long PC)
235 /* Enter absolute (non relocatable mode). Depending on the OrgPerSeg flag,
236 * this will either switch the mode globally or for the current segment.
240 /* Relocatable mode is switched per segment */
241 ActiveSeg->RelocMode = 0;
242 ActiveSeg->AbsPC = PC;
244 /* Relocatable mode is switched globally */
252 int GetRelocMode (void)
253 /* Return true if we're currently in relocatable mode */
256 /* Relocatable mode is switched per segment */
257 return ActiveSeg->RelocMode;
259 /* Relocatable mode is switched globally */
266 void EnterRelocMode (void)
267 /* Enter relocatable mode. Depending on the OrgPerSeg flag, this will either
268 * switch the mode globally or for the current segment.
272 /* Relocatable mode is switched per segment */
273 ActiveSeg->RelocMode = 1;
275 /* Relocatable mode is switched globally */
282 void SegAlign (unsigned long Alignment, int FillVal)
283 /* Align the PC segment to Alignment. If FillVal is -1, emit fill fragments
284 * (the actual fill value will be determined by the linker), otherwise use
288 unsigned char Data [4];
289 unsigned long CombinedAlignment;
292 /* The segment must have the combined alignment of all separate alignments
293 * in the source. Calculate this alignment and check it for sanity.
295 CombinedAlignment = LeastCommonMultiple (ActiveSeg->Align, Alignment);
296 if (CombinedAlignment > MAX_ALIGNMENT) {
297 Error ("Combined alignment for active segment is %lu which exceeds %lu",
298 CombinedAlignment, MAX_ALIGNMENT);
300 /* Avoid creating large fills for an object file that is thrown away
306 ActiveSeg->Align = CombinedAlignment;
308 /* Output a warning for larger alignments if not suppressed */
309 if (CombinedAlignment > LARGE_ALIGNMENT && !LargeAlignment) {
310 Warning (0, "Combined alignment is suspiciously large (%lu)",
314 /* Calculate the number of fill bytes */
315 Count = AlignCount (ActiveSeg->PC, Alignment);
320 /* Emit the data or a fill fragment */
322 /* User defined fill value */
323 memset (Data, FillVal, sizeof (Data));
325 if (Count > sizeof (Data)) {
326 EmitData (Data, sizeof (Data));
327 Count -= sizeof (Data);
329 EmitData (Data, Count);
334 /* Linker defined fill value */
341 unsigned char GetSegAddrSize (unsigned SegNum)
342 /* Return the address size of the segment with the given number */
344 /* Is there such a segment? */
345 if (SegNum >= CollCount (&SegmentList)) {
346 FAIL ("Invalid segment number");
349 /* Return the address size */
350 return ((Segment*) CollAtUnchecked (&SegmentList, SegNum))->Def->AddrSize;
356 /* Check the segments for range and other errors. Do cleanup. */
358 static const unsigned long U_Hi[4] = {
359 0x000000FFUL, 0x0000FFFFUL, 0x00FFFFFFUL, 0xFFFFFFFFUL
361 static const long S_Hi[4] = {
362 0x0000007FL, 0x00007FFFL, 0x007FFFFFL, 0x7FFFFFFFL
366 for (I = 0; I < CollCount (&SegmentList); ++I) {
367 Segment* S = CollAtUnchecked (&SegmentList, I);
368 Fragment* F = S->Root;
370 if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
372 /* We have an expression, study it */
375 StudyExpr (F->V.Expr, &ED);
377 /* Check if the expression is constant */
378 if (ED_IsConst (&ED)) {
382 /* The expression is constant. Check for range errors. */
384 if (F->Type == FRAG_SEXPR) {
385 long Hi = S_Hi[F->Len-1];
387 if (ED.Val > Hi || ED.Val < Lo) {
389 "Range error (%ld not in [%ld..%ld])",
393 if (((unsigned long)ED.Val) > U_Hi[F->Len-1]) {
395 "Range error (%lu not in [0..%lu])",
396 (unsigned long)ED.Val, U_Hi[F->Len-1]);
400 /* We don't need the expression tree any longer */
401 FreeExpr (F->V.Expr);
403 /* Convert the fragment into a literal fragment */
404 for (J = 0; J < F->Len; ++J) {
405 F->V.Data[J] = ED.Val & 0xFF;
408 F->Type = FRAG_LITERAL;
410 } else if (RelaxChecks == 0) {
412 /* We cannot evaluate the expression now, leave the job for
413 * the linker. However, we can check if the address size
414 * matches the fragment size. Mismatches are errors in
417 if ((F->Len == 1 && ED.AddrSize > ADDR_SIZE_ZP) ||
418 (F->Len == 2 && ED.AddrSize > ADDR_SIZE_ABS) ||
419 (F->Len == 3 && ED.AddrSize > ADDR_SIZE_FAR)) {
420 LIError (&F->LI, "Range error");
424 /* Release memory allocated for the expression decriptor */
435 /* Dump the contents of all segments */
441 for (I = 0; I < CollCount (&SegmentList); ++I) {
442 Segment* S = CollAtUnchecked (&SegmentList, I);
446 printf ("New segment: %s", S->Def->Name);
449 if (F->Type == FRAG_LITERAL) {
451 printf ("\n Literal:");
455 for (I = 0; I < F->Len; ++I) {
456 printf (" %02X", F->V.Data [I]);
459 } else if (F->Type == FRAG_EXPR || F->Type == FRAG_SEXPR) {
461 printf ("\n Expression (%u): ", F->Len);
462 DumpExpr (F->V.Expr, SymResolve);
463 } else if (F->Type == FRAG_FILL) {
465 printf ("\n Fill bytes (%u)", F->Len);
467 Internal ("Unknown fragment type: %u", F->Type);
474 printf ("\n End PC = $%04X\n", (unsigned)(S->PC & 0xFFFF));
482 /* Initialize segments */
484 /* Create the predefined segments. Code segment is active */
485 ActiveSeg = NewSegFromDef (&CodeSegDef);
486 NewSegFromDef (&RODataSegDef);
487 NewSegFromDef (&BssSegDef);
488 NewSegFromDef (&DataSegDef);
489 NewSegFromDef (&ZeropageSegDef);
490 NewSegFromDef (&NullSegDef);
495 void SetSegmentSizes (void)
496 /* Set the default segment sizes according to the memory model */
498 /* Initialize segment sizes. The segment definitions do already contain
499 * the correct values for the default case (near), so we must only change
500 * things that should be different.
502 switch (MemoryModel) {
508 CodeSegDef.AddrSize = ADDR_SIZE_FAR;
512 CodeSegDef.AddrSize = ADDR_SIZE_FAR;
513 DataSegDef.AddrSize = ADDR_SIZE_FAR;
514 BssSegDef.AddrSize = ADDR_SIZE_FAR;
515 RODataSegDef.AddrSize = ADDR_SIZE_FAR;
519 Internal ("Invalid memory model: %d", MemoryModel);
525 static void WriteOneSeg (Segment* Seg)
526 /* Write one segment to the object file */
529 unsigned long DataSize;
530 unsigned long EndPos;
532 /* Remember the file position, then write a dummy for the size of the
535 unsigned long SizePos = ObjGetFilePos ();
538 /* Write the segment data */
539 ObjWriteVar (GetStringId (Seg->Def->Name)); /* Name of the segment */
540 ObjWriteVar (Seg->Flags); /* Segment flags */
541 ObjWriteVar (Seg->PC); /* Size */
542 ObjWriteVar (Seg->Align); /* Segment alignment */
543 ObjWrite8 (Seg->Def->AddrSize); /* Address size of the segment */
544 ObjWriteVar (Seg->FragCount); /* Number of fragments */
546 /* Now walk through the fragment list for this segment and write the
552 /* Write data depending on the type */
553 switch (Frag->Type) {
556 ObjWrite8 (FRAG_LITERAL);
557 ObjWriteVar (Frag->Len);
558 ObjWriteData (Frag->V.Data, Frag->Len);
563 case 1: ObjWrite8 (FRAG_EXPR8); break;
564 case 2: ObjWrite8 (FRAG_EXPR16); break;
565 case 3: ObjWrite8 (FRAG_EXPR24); break;
566 case 4: ObjWrite8 (FRAG_EXPR32); break;
567 default: Internal ("Invalid fragment size: %u", Frag->Len);
569 WriteExpr (Frag->V.Expr);
574 case 1: ObjWrite8 (FRAG_SEXPR8); break;
575 case 2: ObjWrite8 (FRAG_SEXPR16); break;
576 case 3: ObjWrite8 (FRAG_SEXPR24); break;
577 case 4: ObjWrite8 (FRAG_SEXPR32); break;
578 default: Internal ("Invalid fragment size: %u", Frag->Len);
580 WriteExpr (Frag->V.Expr);
584 ObjWrite8 (FRAG_FILL);
585 ObjWriteVar (Frag->Len);
589 Internal ("Invalid fragment type: %u", Frag->Type);
593 /* Write the line infos for this fragment */
594 WriteLineInfo (&Frag->LI);
600 /* Calculate the size of the data, seek back and write it */
601 EndPos = ObjGetFilePos (); /* Remember where we are */
602 DataSize = EndPos - SizePos - 4; /* Don't count size itself */
603 ObjSetFilePos (SizePos); /* Seek back to the size */
604 ObjWrite32 (DataSize); /* Write the size */
605 ObjSetFilePos (EndPos); /* Seek back to the end */
610 void WriteSegments (void)
611 /* Write the segment data to the object file */
615 /* Tell the object file module that we're about to start the seg list */
618 /* First thing is segment count */
619 ObjWriteVar (CollCount (&SegmentList));
621 /* Now walk through all segments and write them to the object file */
622 for (I = 0; I < CollCount (&SegmentList); ++I) {
623 /* Write one segment */
624 WriteOneSeg (CollAtUnchecked (&SegmentList, I));
627 /* Done writing segments */