]> git.sur5r.net Git - cc65/blob - src/cc65/function.c
Added the lineinfo module. Changed the complete code generation to use the
[cc65] / src / cc65 / function.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                function.c                                 */
4 /*                                                                           */
5 /*                      Parse function entry/body/exit                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 #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 "segments.h"
51 #include "stmt.h"
52 #include "symtab.h"
53 #include "function.h"
54
55
56
57 /*****************************************************************************/
58 /*                                   Data                                    */
59 /*****************************************************************************/
60
61
62
63 /* Structure that holds all data needed for function activation */
64 struct Function {
65     struct SymEntry*    FuncEntry;      /* Symbol table entry */
66     type*               ReturnType;     /* Function return type */
67     struct FuncDesc*    Desc;           /* Function descriptor */
68     int                 Reserved;       /* Reserved local space */
69     unsigned            RetLab;         /* Return code label */
70     int                 TopLevelSP;     /* SP at function top level */
71 };
72
73 /* Pointer to current function */
74 Function* CurrentFunc = 0;
75
76
77
78 /*****************************************************************************/
79 /*                 Subroutines working with struct Function                  */
80 /*****************************************************************************/
81
82
83
84 static Function* NewFunction (struct SymEntry* Sym)
85 /* Create a new function activation structure and return it */
86 {
87     /* Allocate a new structure */
88     Function* F = (Function*) xmalloc (sizeof (Function));
89
90     /* Initialize the fields */
91     F->FuncEntry  = Sym;
92     F->ReturnType = GetFuncReturn (Sym->Type);
93     F->Desc       = (FuncDesc*) DecodePtr (Sym->Type + 1);
94     F->Reserved   = 0;
95     F->RetLab     = GetLocalLabel ();
96     F->TopLevelSP = 0;
97
98     /* Return the new structure */
99     return F;
100 }
101
102
103
104 static void FreeFunction (Function* F)
105 /* Free a function activation structure */
106 {
107     xfree (F);
108 }
109
110
111
112 const char* GetFuncName (const Function* F)
113 /* Return the name of the current function */
114 {
115     return F->FuncEntry->Name;
116 }
117
118
119
120 unsigned GetParamCount (const Function* F)
121 /* Return the parameter count for the current function */
122 {
123     return F->Desc->ParamCount;
124 }
125
126
127
128 unsigned GetParamSize (const Function* F)
129 /* Return the parameter size for the current function */
130 {
131     return F->Desc->ParamSize;
132 }
133
134
135
136 type* GetReturnType (Function* F)
137 /* Get the return type for the function */
138 {
139     return F->ReturnType;
140 }
141
142
143
144 int HasVoidReturn (const Function* F)
145 /* Return true if the function does not have a return value */
146 {
147     return IsTypeVoid (F->ReturnType);
148 }
149
150
151
152 int IsVariadic (const Function* F)
153 /* Return true if this is a variadic function */
154 {
155     return (F->Desc->Flags & FD_VARIADIC) != 0;
156 }
157
158
159
160 unsigned GetRetLab (const Function* F)
161 /* Return the return jump label */
162 {
163     return F->RetLab;
164 }
165
166
167
168 int GetTopLevelSP (const Function* F)
169 /* Get the value of the stack pointer on function top level */
170 {
171     return F->TopLevelSP;
172 }
173
174
175
176 int ReserveLocalSpace (Function* F, unsigned Size)
177 /* Reserve (but don't allocate) the given local space and return the stack
178  * offset.
179  */
180 {
181     F->Reserved += Size;
182     return oursp - F->Reserved;
183 }
184
185
186
187 void AllocLocalSpace (Function* F)
188 /* Allocate any local space previously reserved. The function will do
189  * nothing if there is no reserved local space.
190  */
191 {
192     if (F->Reserved > 0) {
193
194         /* Create space on the stack */
195         g_space (F->Reserved);
196
197         /* Correct the stack pointer */
198         oursp -= F->Reserved;
199
200         /* Nothing more reserved */
201         F->Reserved = 0;
202     }
203 }
204
205
206
207 /*****************************************************************************/
208 /*                                   code                                    */
209 /*****************************************************************************/
210
211
212
213 void NewFunc (SymEntry* Func)
214 /* Parse argument declarations and function body. */
215 {
216     int HadReturn;
217     int IsVoidFunc;
218
219     /* Get the function descriptor from the function entry */
220     FuncDesc* D = Func->V.F.Func;
221
222     /* Allocate the function activation record for the function */
223     CurrentFunc = NewFunction (Func);
224
225     /* Reenter the lexical level */
226     ReenterFunctionLevel (D);
227
228     /* Declare two special functions symbols: __fixargs__ and __argsize__.
229      * The latter is different depending on the type of the function (variadic
230      * or not).
231      */
232     AddConstSym ("__fixargs__", type_uint, SC_DEF | SC_CONST, D->ParamSize);
233     if (D->Flags & FD_VARIADIC) {
234         /* Variadic function. The variable must be const. */
235         static const type T [] = { T_UCHAR | T_QUAL_CONST, T_END };
236         AddLocalSym ("__argsize__", T, SC_DEF | SC_REF | SC_AUTO, 0);
237     } else {
238         /* Non variadic */
239         AddConstSym ("__argsize__", type_uchar, SC_DEF | SC_CONST, D->ParamSize);
240     }
241
242     /* Function body now defined */
243     Func->Flags |= SC_DEF;
244
245     /* Setup register variables */
246     InitRegVars ();
247
248     /* Allocate code and data segments for this function */
249     Func->V.F.Seg = PushSegments (Func);
250
251     /* If this is a fastcall function, push the last parameter onto the stack */
252     if (IsFastCallFunc (Func->Type) && D->ParamCount > 0) {
253
254         SymEntry* LastParam;
255         unsigned Flags;
256
257         /* Fastcall functions may never have an ellipsis or the compiler is buggy */
258         CHECK ((D->Flags & FD_VARIADIC) == 0);
259
260         /* Get a pointer to the last parameter entry */
261         LastParam = D->SymTab->SymTail;
262
263         /* Generate the push */
264         if (IsTypeFunc (LastParam->Type)) {
265             /* Pointer to function */
266             Flags = CF_PTR;
267         } else {
268             Flags = TypeOf (LastParam->Type) | CF_FORCECHAR;
269         }
270         g_push (Flags, 0);
271     }
272
273     /* If stack checking code is requested, emit a call to the helper routine */
274     if (CheckStack) {
275         g_stackcheck ();
276     }
277
278     /* Generate function entry code if needed */
279     g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
280
281     /* Setup the stack */
282     oursp = 0;
283
284     /* Need a starting curly brace */
285     ConsumeLCurly ();
286
287     /* Parse local variable declarations if any */
288     DeclareLocals ();
289
290     /* Remember the current stack pointer. All variables allocated elsewhere
291      * must be dropped when doing a return from an inner block.
292      */
293     CurrentFunc->TopLevelSP = oursp;
294
295     /* Now process statements in this block */
296     HadReturn = 0;
297     while (CurTok.Tok != TOK_RCURLY) {
298         if (CurTok.Tok != TOK_CEOF) {
299             HadReturn = Statement ();
300         } else {
301             break;
302         }
303     }
304
305     /* If the function has a return type but no return statement, flag
306      * a warning
307      */
308     IsVoidFunc = HasVoidReturn (CurrentFunc);
309 #if 0
310     /* Does not work reliably */
311     if (!IsVoidFunc && !HadReturn) {
312         Warning ("Function `%s' should return a value", Func->Name);
313     }
314 #endif
315
316     /* Output the function exit code label */
317     g_defcodelabel (GetRetLab (CurrentFunc));
318
319     /* Restore the register variables */
320     RestoreRegVars (!IsVoidFunc);
321
322     /* Generate the exit code */
323     g_leave ();
324
325     /* Eat the closing brace */
326     ConsumeRCurly ();
327
328     /* Emit references to imports/exports */
329     EmitExternals ();
330
331     /* Cleanup register variables */
332     DoneRegVars ();
333
334     /* Leave the lexical level */
335     LeaveFunctionLevel ();
336
337     /* Switch back to the old segments */
338     PopSegments ();
339
340     /* Reset the current function pointer */
341     FreeFunction (CurrentFunc);
342     CurrentFunc = 0;
343 }
344
345
346