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