1 /*****************************************************************************/
5 /* Parse function entry/body/exit */
9 /* (C) 2000-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Structure that holds all data needed for function activation */
64 struct SymEntry* FuncEntry; /* Symbol table entry */
65 type* ReturnType; /* Function return type */
66 struct FuncDesc* Desc; /* Function descriptor */
67 int Reserved; /* Reserved local space */
68 unsigned RetLab; /* Return code label */
69 int TopLevelSP; /* SP at function top level */
72 /* Pointer to current function */
73 Function* CurrentFunc = 0;
77 /*****************************************************************************/
78 /* Subroutines working with struct Function */
79 /*****************************************************************************/
83 static Function* NewFunction (struct SymEntry* Sym)
84 /* Create a new function activation structure and return it */
86 /* Allocate a new structure */
87 Function* F = (Function*) xmalloc (sizeof (Function));
89 /* Initialize the fields */
91 F->ReturnType = GetFuncReturn (Sym->Type);
92 F->Desc = (FuncDesc*) DecodePtr (Sym->Type + 1);
94 F->RetLab = GetLocalLabel ();
97 /* Return the new structure */
103 static void FreeFunction (Function* F)
104 /* Free a function activation structure */
111 const char* GetFuncName (const Function* F)
112 /* Return the name of the current function */
114 return F->FuncEntry->Name;
119 unsigned GetParamCount (const Function* F)
120 /* Return the parameter count for the current function */
122 return F->Desc->ParamCount;
127 unsigned GetParamSize (const Function* F)
128 /* Return the parameter size for the current function */
130 return F->Desc->ParamSize;
135 type* GetReturnType (Function* F)
136 /* Get the return type for the function */
138 return F->ReturnType;
143 int HasVoidReturn (const Function* F)
144 /* Return true if the function does not have a return value */
146 return IsTypeVoid (F->ReturnType);
151 int IsVariadic (const Function* F)
152 /* Return true if this is a variadic function */
154 return (F->Desc->Flags & FD_VARIADIC) != 0;
159 unsigned GetRetLab (const Function* F)
160 /* Return the return jump label */
167 int GetTopLevelSP (const Function* F)
168 /* Get the value of the stack pointer on function top level */
170 return F->TopLevelSP;
175 int ReserveLocalSpace (Function* F, unsigned Size)
176 /* Reserve (but don't allocate) the given local space and return the stack
181 return oursp - F->Reserved;
186 void AllocLocalSpace (Function* F)
187 /* Allocate any local space previously reserved. The function will do
188 * nothing if there is no reserved local space.
191 if (F->Reserved > 0) {
193 /* Create space on the stack */
194 g_space (F->Reserved);
196 /* Correct the stack pointer */
197 oursp -= F->Reserved;
199 /* Nothing more reserved */
206 /*****************************************************************************/
208 /*****************************************************************************/
212 void NewFunc (SymEntry* Func)
213 /* Parse argument declarations and function body. */
219 /* Get the function descriptor from the function entry */
220 FuncDesc* D = Func->V.F.Func;
222 /* Allocate the function activation record for the function */
223 CurrentFunc = NewFunction (Func);
225 /* Reenter the lexical level */
226 ReenterFunctionLevel (D);
228 /* Declare two special functions symbols: __fixargs__ and __argsize__.
229 * The latter is different depending on the type of the function (variadic
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);
239 AddConstSym ("__argsize__", type_uchar, SC_DEF | SC_CONST, D->ParamSize);
242 /* Function body now defined */
243 Func->Flags |= SC_DEF;
245 /* Setup register variables */
248 /* Allocate code and data segments for this function */
249 g_pushseg (&Func->V.F.CS, &Func->V.F.DS, Func->Name);
251 /* If this is a fastcall function, push the last parameter onto the stack */
252 if (IsFastCallFunc (Func->Type) && D->ParamCount > 0) {
257 /* Fastcall functions may never have an ellipsis or the compiler is buggy */
258 CHECK ((D->Flags & FD_VARIADIC) == 0);
260 /* Get a pointer to the last parameter entry */
261 LastParam = D->SymTab->SymTail;
263 /* Generate the push */
264 if (IsTypeFunc (LastParam->Type)) {
265 /* Pointer to function */
268 Flags = TypeOf (LastParam->Type) | CF_FORCECHAR;
273 /* If stack checking code is requested, emit a call to the helper routine */
278 /* Generate function entry code if needed */
279 g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
281 /* Setup the stack */
284 /* Need a starting curly brace */
287 /* Parse local variable declarations if any */
290 /* Remember the current stack pointer. All variables allocated elsewhere
291 * must be dropped when doing a return from an inner block.
293 CurrentFunc->TopLevelSP = oursp;
295 /* Now process statements in this block */
297 while (curtok != TOK_RCURLY) {
298 if (curtok != TOK_CEOF) {
299 HadReturn = Statement ();
305 /* If the function has a return type but no return statement, flag
308 IsVoidFunc = HasVoidReturn (CurrentFunc);
310 /* Does not work reliably */
311 if (!IsVoidFunc && !HadReturn) {
312 Warning ("Function `%s' should return a value", Func->Name);
316 /* Output the function exit code label */
317 g_defcodelabel (GetRetLab (CurrentFunc));
319 /* Restore the register variables */
320 RestoreRegVars (!IsVoidFunc);
322 /* Generate the exit code */
323 Flags = IsVoidFunc? CF_NONE : CF_REG;
326 /* Eat the closing brace */
329 /* Emit references to imports/exports */
332 /* Cleanup register variables */
335 /* Leave the lexical level */
336 LeaveFunctionLevel ();
338 /* Switch back to the old segments */
341 /* Reset the current function pointer */
342 FreeFunction (CurrentFunc);