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