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