1 /*****************************************************************************/
5 /* Parse function entry/body/exit */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Structure that holds all data needed for function activation */
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 */
71 /* Pointer to current function */
72 Function* CurrentFunc = 0;
76 /*****************************************************************************/
77 /* Subroutines working with struct Function */
78 /*****************************************************************************/
82 static Function* NewFunction (struct SymEntry* Sym)
83 /* Create a new function activation structure and return it */
85 /* Allocate a new structure */
86 Function* F = xmalloc (sizeof (Function));
88 /* Initialize the fields */
90 F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
91 F->Desc = DecodePtr (Sym->Type + 1);
94 F->RetLab = GetLabel ();
96 /* Return the new structure */
102 static void FreeFunction (Function* F)
103 /* Free a function activation structure */
110 const char* GetFuncName (const Function* F)
111 /* Return the name of the current function */
113 return F->FuncEntry->Name;
118 unsigned GetParamSize (const Function* F)
119 /* Return the parameter size for the current function */
121 return F->Desc->ParamSize;
126 type* GetReturnType (Function* F)
127 /* Get the return type for the function */
129 return F->ReturnType;
134 int HasVoidReturn (const Function* F)
135 /* Return true if the function does not have a return value */
137 return IsTypeVoid (F->ReturnType);
142 void RememberEntry (Function* F)
143 /* Remember the current output position for local space creation later */
145 F->EntryCode = GetCodePos ();
150 unsigned GetRetLab (const Function* F)
151 /* Return the return jump label */
158 int ReserveLocalSpace (Function* F, unsigned Size)
159 /* Reserve (but don't allocate) the given local space and return the stack
164 return oursp - F->Reserved;
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.
174 if (F->Reserved > 0) {
176 /* Switch to the code segment */
179 /* Create space on the stack */
180 g_space (F->Reserved);
182 /* Correct the stack pointer */
183 oursp -= F->Reserved;
185 /* Nothing more reserved */
192 /*****************************************************************************/
194 /*****************************************************************************/
198 void NewFunc (SymEntry* Func)
199 /* Parse argument declarations and function body. */
204 /* Get the function descriptor from the function entry */
205 FuncDesc* D = DecodePtr (Func->Type+1);
207 /* Allocate the function activation record for the function */
208 CurrentFunc = NewFunction (Func);
210 /* Reenter the lexical level */
211 ReenterFunctionLevel (D);
213 /* Function body now defined */
214 Func->Flags |= SC_DEF;
216 /* C functions cannot currently have __fastcall__ calling conventions */
217 if (IsFastCallFunc (Func->Type)) {
218 Error ("__fastcall__ is not allowed for C functions");
221 /* Need a starting curly brace */
222 if (curtok != TOK_LCURLY) {
223 Error ("`{' expected");
226 /* Setup register variables */
229 /* Switch to the code segment and define the function name label */
231 g_defgloblabel (Func->Name);
233 /* If stack cehcking code is requested, emit a call to the helper routine */
238 /* Generate function entry code if needed */
239 g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
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.
246 RememberEntry (CurrentFunc);
248 /* Parse the function body */
252 /* If the function did not end with an return statement, create exit code */
255 /* If the function has a return type, flag an error */
257 Error ("Function `%s' must return a value", Func->Name);
262 Flags = HasVoidReturn (CurrentFunc)? CF_NONE : CF_REG;
266 /* Dump literal data created by the function */
269 /* Cleanup register variables */
272 /* Leave the lexical level */
273 LeaveFunctionLevel ();
275 /* Reset the current function pointer */
276 FreeFunction (CurrentFunc);