]> git.sur5r.net Git - cc65/blob - src/cc65/function.c
9ec1f52ab37a29906ee2189e03c59ea03f3c6bf2
[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 /* common */
37 #include "check.h"
38 #include "xmalloc.h"
39
40 /* cc65 */
41 #include "asmcode.h"
42 #include "asmlabel.h"
43 #include "codegen.h"
44 #include "error.h"
45 #include "funcdesc.h"
46 #include "global.h"
47 #include "litpool.h"
48 #include "locals.h"
49 #include "scanner.h"
50 #include "stmt.h"
51 #include "symtab.h"
52 #include "function.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Structure that holds all data needed for function activation */
63 struct Function {
64     struct SymEntry*    FuncEntry;      /* Symbol table entry */
65     type*               ReturnType;     /* Function return type */
66     struct FuncDesc*    Desc;           /* Function descriptor */
67     CodeMark            EntryCode;      /* Backpatch addr for entry code */
68     int                 Reserved;       /* Reserved local space */
69     unsigned            RetLab;         /* Return code label */
70 };
71
72 /* Pointer to current function */
73 Function* CurrentFunc = 0;
74
75
76
77 /*****************************************************************************/
78 /*                 Subroutines working with struct Function                  */
79 /*****************************************************************************/
80
81
82
83 static Function* NewFunction (struct SymEntry* Sym)
84 /* Create a new function activation structure and return it */
85 {
86     /* Allocate a new structure */
87     Function* F = xmalloc (sizeof (Function));
88
89     /* Initialize the fields */
90     F->FuncEntry  = Sym;
91     F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
92     F->Desc       = DecodePtr (Sym->Type + 1);
93     F->EntryCode  = 0;
94     F->Reserved   = 0;
95     F->RetLab     = GetLabel ();
96
97     /* Return the new structure */
98     return F;
99 }
100
101
102
103 static void FreeFunction (Function* F)
104 /* Free a function activation structure */
105 {
106     xfree (F);
107 }
108
109
110
111 const char* GetFuncName (const Function* F)
112 /* Return the name of the current function */
113 {
114     return F->FuncEntry->Name;
115 }
116
117
118
119 unsigned GetParamCount (const Function* F)
120 /* Return the parameter count for the current function */
121 {
122     return F->Desc->ParamCount;
123 }
124
125
126
127 unsigned GetParamSize (const Function* F)
128 /* Return the parameter size for the current function */
129 {
130     return F->Desc->ParamSize;
131 }
132
133
134
135 type* GetReturnType (Function* F)
136 /* Get the return type for the function */
137 {
138     return F->ReturnType;
139 }
140
141
142
143 int HasVoidReturn (const Function* F)
144 /* Return true if the function does not have a return value */
145 {
146     return IsTypeVoid (F->ReturnType);
147 }
148
149
150
151 void RememberEntry (Function* F)
152 /* Remember the current output position for local space creation later */
153 {
154     F->EntryCode = GetCodePos ();
155 }
156
157
158
159 unsigned GetRetLab (const Function* F)
160 /* Return the return jump label */
161 {
162     return F->RetLab;
163 }
164
165
166
167 int ReserveLocalSpace (Function* F, unsigned Size)
168 /* Reserve (but don't allocate) the given local space and return the stack
169  * offset.
170  */
171 {
172     F->Reserved += Size;
173     return oursp - F->Reserved;
174 }
175
176
177
178 void AllocLocalSpace (Function* F)
179 /* Allocate any local space previously reserved. The function will do
180  * nothing if there is no reserved local space.
181  */
182 {
183     if (F->Reserved > 0) {
184
185         /* Switch to the code segment */
186         g_usecode ();
187
188         /* Create space on the stack */
189         g_space (F->Reserved);
190
191         /* Correct the stack pointer */
192         oursp -= F->Reserved;
193
194         /* Nothing more reserved */
195         F->Reserved = 0;
196     }
197 }
198
199
200
201 /*****************************************************************************/
202 /*                                   code                                    */
203 /*****************************************************************************/
204
205
206
207 void NewFunc (SymEntry* Func)
208 /* Parse argument declarations and function body. */
209 {
210     int isbrk;
211     unsigned Flags;
212
213     /* Get the function descriptor from the function entry */
214     FuncDesc* D = DecodePtr (Func->Type+1);
215
216     /* Allocate the function activation record for the function */
217     CurrentFunc = NewFunction (Func);
218
219     /* Reenter the lexical level */
220     ReenterFunctionLevel (D);
221
222     /* Function body now defined */
223     Func->Flags |= SC_DEF;
224
225     /* Need a starting curly brace */
226     if (curtok != TOK_LCURLY) {
227         Error ("`{' expected");
228     }
229
230     /* Setup register variables */
231     InitRegVars ();
232
233     /* Switch to the code segment and define the function name label */
234     g_usecode ();
235     g_defgloblabel (Func->Name);
236
237     /* If this is a fastcall function, push the last parameter onto the stack */
238     if (IsFastCallFunc (Func->Type) && D->ParamCount > 0 && (D->Flags & FD_ELLIPSIS) == 0) {
239
240         unsigned Flags;
241
242         /* Get a pointer to the last parameter entry */
243         SymEntry* LastParam = D->SymTab->SymTail;
244
245         /* Generate the push */
246         if (IsTypeFunc (LastParam->Type)) {
247             /* Pointer to function */
248             Flags = CF_PTR;
249         } else {
250             Flags = TypeOf (LastParam->Type) | CF_FORCECHAR;
251         }
252         g_push (Flags, 0);
253     }
254
255     /* If stack checking code is requested, emit a call to the helper routine */
256     if (CheckStack) {
257         g_stackcheck ();
258     }
259
260     /* Generate function entry code if needed */
261     g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
262
263     /* Remember the current code position. This may be used later to create
264      * local variable space once we have created the function body itself.
265      * Currently this is not possible because the stack offsets of all locals
266      * have to be known in advance.
267      */
268     RememberEntry (CurrentFunc);
269
270     /* Parse the function body */
271     oursp = 0;
272     isbrk = compound ();
273
274     /* If the function did not end with an return statement, create exit code */
275     if (!isbrk) {
276 #if 0
277         /* If the function has a return type, flag an error */
278         if (!voidfunc) {
279             Error ("Function `%s' must return a value", Func->Name);
280         }
281 #endif
282         RestoreRegVars (0);
283
284         Flags = HasVoidReturn (CurrentFunc)? CF_NONE : CF_REG;
285         g_leave (Flags, 0);
286     }
287
288     /* Dump literal data created by the function */
289     DumpLiteralPool ();
290
291     /* Cleanup register variables */
292     DoneRegVars ();
293
294     /* Leave the lexical level */
295     LeaveFunctionLevel ();
296
297     /* Reset the current function pointer */
298     FreeFunction (CurrentFunc);
299     CurrentFunc = 0;
300 }
301
302
303
304