]> git.sur5r.net Git - cc65/blob - src/cc65/function.c
effb97aab2bdaf4587ad2ad0732156340524393e
[cc65] / src / cc65 / function.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                function.c                                 */
4 /*                                                                           */
5 /*                      Parse function entry/body/exit                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 #include "../common/xmalloc.h"
37
38 #include "asmcode.h"
39 #include "asmlabel.h"
40 #include "codegen.h"
41 #include "error.h"
42 #include "funcdesc.h"
43 #include "litpool.h"
44 #include "locals.h"
45 #include "scanner.h"
46 #include "stmt.h"
47 #include "symtab.h"
48 #include "function.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Structure that holds all data needed for function activation */
59 struct Function {
60     struct SymEntry*    FuncEntry;      /* Symbol table entry */
61     type*               ReturnType;     /* Function return type */
62     struct FuncDesc*    Desc;           /* Function descriptor */
63     CodeMark            EntryCode;      /* Backpatch addr for entry code */
64     int                 LocalMax;       /* Total space for locals */
65     int                 LocalSize;      /* Current space for locals */
66     unsigned            RetLab;         /* Return code label */
67 };
68
69 /* Pointer to current function */
70 Function* CurrentFunc = 0;
71
72
73
74 /*****************************************************************************/
75 /*                 Subroutines working with struct Function                  */
76 /*****************************************************************************/
77
78
79
80 static Function* NewFunction (struct SymEntry* Sym)
81 /* Create a new function activation structure and return it */
82 {
83     /* Allocate a new structure */
84     Function* F = xmalloc (sizeof (Function));
85
86     /* Initialize the fields */
87     F->FuncEntry  = Sym;
88     F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
89     F->Desc       = DecodePtr (Sym->Type + 1);
90     F->EntryCode  = 0;
91     F->LocalMax   = 0;
92     F->LocalSize  = 0;
93     F->RetLab     = GetLabel ();
94
95     /* Return the new structure */
96     return F;
97 }
98
99
100
101 static void FreeFunction (Function* F)
102 /* Free a function activation structure */
103 {
104     xfree (F);
105 }
106
107
108
109 const char* GetFuncName (const Function* F)
110 /* Return the name of the current function */
111 {
112     return F->FuncEntry->Name;
113 }
114
115
116
117 unsigned GetParamSize (const Function* F)
118 /* Return the parameter size for the current function */
119 {
120     return F->Desc->ParamSize;
121 }
122
123
124
125 type* GetReturnType (Function* F)
126 /* Get the return type for the function */
127 {
128     return F->ReturnType;
129 }
130
131
132
133 int HasVoidReturn (const Function* F)
134 /* Return true if the function does not have a return value */
135 {
136     return IsVoid (F->ReturnType);
137 }
138
139
140
141 void RememberEntry (Function* F)
142 /* Remember the current output position for local space creation later */
143 {
144     F->EntryCode = GetCodePos ();
145 }
146
147
148
149 unsigned GetRetLab (const Function* F)
150 /* Return the return jump label */
151 {
152     return F->RetLab;
153 }
154
155
156
157 int AllocLocalSpace (Function* F, unsigned Size)
158 /* Allocate space for the function locals, return stack offset */
159 {
160     /* Add the size */
161     F->LocalSize += Size;
162     if (F->LocalSize > F->LocalMax) {
163         F->LocalMax = F->LocalSize;
164     }
165
166     /* Return the offset, it is below the initial stack pointer */
167     return -F->LocalSize;;
168 }
169
170
171
172 void FreeLocalSpace (Function* F, unsigned Size)
173 /* Free space allocated for function locals */
174 {
175     F->LocalSize -= Size;
176 }
177
178
179
180 unsigned GetLocalSpace (const Function* F)
181 /* Get the local variable space needed for the function */
182 {
183     return F->LocalMax;
184 }
185
186
187
188 /*****************************************************************************/
189 /*                                   code                                    */
190 /*****************************************************************************/
191
192
193
194 void NewFunc (SymEntry* Func)
195 /* Parse argument declarations and function body. */
196 {
197     int isbrk;
198
199     /* Get the function descriptor from the function entry */
200     FuncDesc* D = DecodePtr (Func->Type+1);
201
202     /* Allocate the function activation record for the function */
203     CurrentFunc = NewFunction (Func);
204
205     /* Reenter the lexical level */
206     ReenterFunctionLevel (D);
207
208     /* Function body now defined */
209     Func->Flags |= SC_DEF;
210
211     /* C functions cannot currently have __fastcall__ calling conventions */
212     if (IsFastCallFunc (Func->Type)) {
213         Error (ERR_FASTCALL);
214     }
215
216     /* Need a starting curly brace */
217     if (curtok != TOK_LCURLY) {
218         Error (ERR_LCURLY_EXPECTED);
219     }
220
221     /* Setup register variables */
222     InitRegVars ();
223
224     /* Switch to the code segment and define the function name label */
225     g_usecode ();
226     g_defgloblabel (Func->Name);
227
228     /* Generate function entry code if needed */
229     g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
230
231     /* Remember the current code position. This may be used later to create
232      * local variable space once we have created the function body itself.
233      * Currently this is not possible because the stack offsets of all locals
234      * have to be known in advance.
235      */
236     RememberEntry (CurrentFunc);
237
238     /* Parse the function body */
239     oursp = 0;
240     isbrk = compound ();
241
242     /* If the function did not end with an return statement, create exit code */
243     if (!isbrk) {
244 #if 0
245         /* If the function has a return type, flag an error */
246         if (!voidfunc) {
247             Error (ERR_MUST_RETURN_VALUE);
248         }
249 #endif
250         RestoreRegVars (0);
251         g_leave (CF_NONE, 0);
252     }
253
254     /* Dump literal data created by the function */
255     DumpLiteralPool ();
256
257     /* Cleanup register variables */
258     DoneRegVars ();
259
260     /* Leave the lexical level */
261     LeaveFunctionLevel ();
262
263     /* Reset the current function pointer */
264     FreeFunction (CurrentFunc);
265     CurrentFunc = 0;
266 }
267
268
269
270