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 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Structure that holds all data needed for function activation */
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 */
70 /* Pointer to current function */
71 Function* CurrentFunc = 0;
75 /*****************************************************************************/
76 /* Subroutines working with struct Function */
77 /*****************************************************************************/
81 static Function* NewFunction (struct SymEntry* Sym)
82 /* Create a new function activation structure and return it */
84 /* Allocate a new structure */
85 Function* F = xmalloc (sizeof (Function));
87 /* Initialize the fields */
89 F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
90 F->Desc = DecodePtr (Sym->Type + 1);
93 F->RetLab = GetLabel ();
95 /* Return the new structure */
101 static void FreeFunction (Function* F)
102 /* Free a function activation structure */
109 const char* GetFuncName (const Function* F)
110 /* Return the name of the current function */
112 return F->FuncEntry->Name;
117 unsigned GetParamSize (const Function* F)
118 /* Return the parameter size for the current function */
120 return F->Desc->ParamSize;
125 type* GetReturnType (Function* F)
126 /* Get the return type for the function */
128 return F->ReturnType;
133 int HasVoidReturn (const Function* F)
134 /* Return true if the function does not have a return value */
136 return IsTypeVoid (F->ReturnType);
141 void RememberEntry (Function* F)
142 /* Remember the current output position for local space creation later */
144 F->EntryCode = GetCodePos ();
149 unsigned GetRetLab (const Function* F)
150 /* Return the return jump label */
157 int ReserveLocalSpace (Function* F, unsigned Size)
158 /* Reserve (but don't allocate) the given local space and return the stack
163 return oursp - F->Reserved;
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.
173 if (F->Reserved > 0) {
175 /* Switch to the code segment */
178 /* Create space on the stack */
179 g_space (F->Reserved);
181 /* Correct the stack pointer */
182 oursp -= F->Reserved;
184 /* Nothing more reserved */
191 /*****************************************************************************/
193 /*****************************************************************************/
197 void NewFunc (SymEntry* Func)
198 /* Parse argument declarations and function body. */
203 /* Get the function descriptor from the function entry */
204 FuncDesc* D = DecodePtr (Func->Type+1);
206 /* Allocate the function activation record for the function */
207 CurrentFunc = NewFunction (Func);
209 /* Reenter the lexical level */
210 ReenterFunctionLevel (D);
212 /* Function body now defined */
213 Func->Flags |= SC_DEF;
215 /* C functions cannot currently have __fastcall__ calling conventions */
216 if (IsFastCallFunc (Func->Type)) {
217 Error ("__fastcall__ is not allowed for C functions");
220 /* Need a starting curly brace */
221 if (curtok != TOK_LCURLY) {
222 Error ("`{' expected");
225 /* Setup register variables */
228 /* Switch to the code segment and define the function name label */
230 g_defgloblabel (Func->Name);
232 /* Generate function entry code if needed */
233 g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc));
235 /* Remember the current code position. This may be used later to create
236 * local variable space once we have created the function body itself.
237 * Currently this is not possible because the stack offsets of all locals
238 * have to be known in advance.
240 RememberEntry (CurrentFunc);
242 /* Parse the function body */
246 /* If the function did not end with an return statement, create exit code */
249 /* If the function has a return type, flag an error */
251 Error ("Function `%s' must return a value", Func->Name);
256 Flags = HasVoidReturn (CurrentFunc)? CF_NONE : CF_REG;
260 /* Dump literal data created by the function */
263 /* Cleanup register variables */
266 /* Leave the lexical level */
267 LeaveFunctionLevel ();
269 /* Reset the current function pointer */
270 FreeFunction (CurrentFunc);