]> git.sur5r.net Git - cc65/blob - src/cc65/asmcode.c
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / cc65 / asmcode.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asmcode.c                                 */
4 /*                                                                           */
5 /*          Assembler output code handling for the cc65 C compiler           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, 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 "check.h"
38
39 /* cc65 */
40 #include "asmcode.h"
41 #include "codeseg.h"
42 #include "dataseg.h"
43 #include "segments.h"
44 #include "stackptr.h"
45 #include "symtab.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Code                                    */
51 /*****************************************************************************/
52
53
54
55 void GetCodePos (CodeMark* M)
56 /* Get a marker pointing to the current output position */
57 {
58     M->Pos = CS_GetEntryCount (CS->Code);
59     M->SP  = StackPtr;
60 }
61
62
63
64 void RemoveCodeRange (const CodeMark* Start, const CodeMark* End)
65 /* Remove all code between two code markers */
66 {
67     /* Nothing to do if the range is empty */
68     if (Start->Pos == End->Pos) {
69         return;
70     }
71
72     /* Delete the range */
73     CS_DelCodeRange (CS->Code, Start->Pos, End->Pos-1);
74 }
75
76
77
78 void RemoveCode (const CodeMark* M)
79 /* Remove all code after the given code marker */
80 {
81     CS_DelCodeAfter (CS->Code, M->Pos);
82     StackPtr = M->SP;
83 }
84
85
86
87 void MoveCode (const CodeMark* Start, const CodeMark* End, const CodeMark* Target)
88 /* Move the code between Start (inclusive) and End (exclusive) to
89  * (before) Target. The code marks aren't updated.
90  */
91 {
92     CS_MoveEntries (CS->Code, Start->Pos, End->Pos - Start->Pos, Target->Pos);
93 }
94
95
96
97 int CodeRangeIsEmpty (const CodeMark* Start, const CodeMark* End)
98 /* Return true if the given code range is empty (no code between Start and End) */
99 {
100     int Empty;
101     PRECONDITION (Start->Pos <= End->Pos);
102     Empty = (Start->Pos == End->Pos);
103     if (Empty) {
104         /* Safety */
105         CHECK (Start->SP == End->SP);
106     }
107     return Empty;
108 }
109
110
111
112 void WriteAsmOutput (void)
113 /* Write the final assembler output to the output file */
114 {
115     SymTable* SymTab;
116     SymEntry* Entry;
117
118     /* Output the global data segment */
119     CHECK (!HaveGlobalCode ());
120     OutputSegments (CS);
121
122     /* Output all global or referenced functions */
123     SymTab = GetGlobalSymTab ();
124     Entry  = SymTab->SymHead;
125     while (Entry) {
126         if (SymIsOutputFunc (Entry)) {
127             /* Function which is defined and referenced or extern */
128             OutputSegments (Entry->V.F.Seg);
129         }
130         Entry = Entry->NextSym;
131     }
132 }