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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Structure that holds all data needed for function activation */
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 unsigned LocalMax; /* Total space for locals */
64 unsigned LocalSize; /* Current space for locals */
65 unsigned RetLab; /* Return code label */
68 /* Pointer to current function */
69 Function* CurrentFunc = 0;
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static Function* NewFunction (struct SymEntry* Sym)
80 /* Create a new function activation structure and return it */
82 /* Allocate a new structure */
83 Function* F = xmalloc (sizeof (Function));
85 /* Initialize the fields */
87 F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
88 F->Desc = DecodePtr (Sym->Type + 1);
89 F->EntryCode = GetCodePos ();
92 F->RetLab = GetLabel ();
94 /* Return the new structure */
100 static void FreeFunction (Function* F)
101 /* Free a function activation structure */
108 const char* GetFuncName (const Function* F)
109 /* Return the name of the current function */
111 return F->FuncEntry->Name;
116 unsigned GetParamSize (const Function* F)
117 /* Return the parameter size for the current function */
119 return F->Desc->ParamSize;
124 type* GetReturnType (Function* F)
125 /* Get the return type for the function */
127 return F->ReturnType;
132 int HasVoidReturn (const Function* F)
133 /* Return true if the function does not have a return value */
135 return IsVoid (F->ReturnType);
140 unsigned GetRetLab (const Function* F)
141 /* Return the return jump label */
148 unsigned AllocLocalSpace (Function* F, unsigned Size)
149 /* Allocate space for the function locals, return stack offset */
151 /* Remember the current offset */
152 unsigned Offs = F->LocalSize;
155 F->LocalSize += Size;
156 if (F->LocalSize > F->LocalMax) {
157 F->LocalMax = F->LocalSize;
160 /* Return the offset */
166 void FreeLocalSpace (Function* F, unsigned Size)
167 /* Free space allocated for function locals */
169 F->LocalSize -= Size;
174 void NewFunc (SymEntry* Func)
175 /* Parse argument declarations and function body. */
179 /* Get the function descriptor from the function entry */
180 FuncDesc* D = DecodePtr (Func->Type+1);
182 /* Allocate the function activation record for the function */
183 CurrentFunc = NewFunction (Func);
185 /* Reenter the lexical level */
186 ReenterFunctionLevel (D);
188 /* Function body now defined */
189 Func->Flags |= SC_DEF;
191 /* C functions cannot currently have __fastcall__ calling conventions */
192 if (IsFastCallFunc (Func->Type)) {
193 Error (ERR_FASTCALL);
196 /* Need a starting curly brace */
197 if (curtok != LCURLY) {
198 Error (ERR_LCURLY_EXPECTED);
201 /* Setup register variables */
204 /* Switch to the code segment and generate function entry code */
206 g_enter (TypeOf (Func->Type), Func->Name, GetParamSize (CurrentFunc));
208 /* Parse the function body */
212 /* If the function did not end with an return statement, create exit code */
215 /* If the function has a return type, flag an error */
217 Error (ERR_MUST_RETURN_VALUE);
221 g_leave (CF_NONE, 0);
224 /* Dump literal data created by the function */
227 /* Cleanup register variables */
230 /* Leave the lexical level */
231 LeaveFunctionLevel ();
233 /* Reset the current function pointer */
234 FreeFunction (CurrentFunc);