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