]> git.sur5r.net Git - cc65/blob - src/cc65/function.c
This commit was generated by cvs2svn to compensate for changes in r2,
[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 #include "asmcode.h"
37 #include "asmlabel.h"
38 #include "codegen.h"
39 #include "error.h"
40 #include "funcdesc.h"
41 #include "litpool.h"
42 #include "locals.h"
43 #include "mem.h"
44 #include "scanner.h"
45 #include "stmt.h"
46 #include "symtab.h"
47 #include "function.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Structure that holds all data needed for function activation */
58 struct Function {
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 */
66 };
67
68 /* Pointer to current function */
69 Function* CurrentFunc = 0;
70
71
72
73 /*****************************************************************************/
74 /*                                   code                                    */
75 /*****************************************************************************/
76
77
78
79 static Function* NewFunction (struct SymEntry* Sym)
80 /* Create a new function activation structure and return it */
81 {
82     /* Allocate a new structure */
83     Function* F = xmalloc (sizeof (Function));
84
85     /* Initialize the fields */
86     F->FuncEntry  = Sym;
87     F->ReturnType = Sym->Type + 1 + DECODE_SIZE;
88     F->Desc       = DecodePtr (Sym->Type + 1);
89     F->EntryCode  = GetCodePos ();
90     F->LocalMax   = 0;
91     F->LocalSize  = 0;
92     F->RetLab     = GetLabel ();
93
94     /* Return the new structure */
95     return F;
96 }
97
98
99
100 static void FreeFunction (Function* F)
101 /* Free a function activation structure */
102 {
103     xfree (F);
104 }
105
106
107
108 const char* GetFuncName (const Function* F)
109 /* Return the name of the current function */
110 {
111     return F->FuncEntry->Name;
112 }
113
114
115
116 unsigned GetParamSize (const Function* F)
117 /* Return the parameter size for the current function */
118 {
119     return F->Desc->ParamSize;
120 }
121
122
123
124 type* GetReturnType (Function* F)
125 /* Get the return type for the function */
126 {
127     return F->ReturnType;
128 }
129
130
131
132 int HasVoidReturn (const Function* F)
133 /* Return true if the function does not have a return value */
134 {
135     return IsVoid (F->ReturnType);
136 }
137
138
139
140 unsigned GetRetLab (const Function* F)
141 /* Return the return jump label */
142 {
143     return F->RetLab;
144 }
145
146
147
148 unsigned AllocLocalSpace (Function* F, unsigned Size)
149 /* Allocate space for the function locals, return stack offset */
150 {
151     /* Remember the current offset */
152     unsigned Offs = F->LocalSize;
153
154     /* Add the size */
155     F->LocalSize += Size;
156     if (F->LocalSize > F->LocalMax) {
157         F->LocalMax = F->LocalSize;
158     }
159
160     /* Return the offset */
161     return Offs;
162 }
163
164
165
166 void FreeLocalSpace (Function* F, unsigned Size)
167 /* Free space allocated for function locals */
168 {
169     F->LocalSize -= Size;
170 }
171
172
173
174 void NewFunc (SymEntry* Func)
175 /* Parse argument declarations and function body. */
176 {
177     int isbrk;
178
179     /* Get the function descriptor from the function entry */
180     FuncDesc* D = DecodePtr (Func->Type+1);
181
182     /* Allocate the function activation record for the function */
183     CurrentFunc = NewFunction (Func);
184
185     /* Reenter the lexical level */
186     ReenterFunctionLevel (D);
187
188     /* Function body now defined */
189     Func->Flags |= SC_DEF;
190
191     /* C functions cannot currently have __fastcall__ calling conventions */
192     if (IsFastCallFunc (Func->Type)) {
193         Error (ERR_FASTCALL);
194     }
195
196     /* Need a starting curly brace */
197     if (curtok != LCURLY) {
198         Error (ERR_LCURLY_EXPECTED);
199     }
200
201     /* Setup register variables */
202     InitRegVars ();
203
204     /* Switch to the code segment and generate function entry code */
205     g_usecode ();
206     g_enter (TypeOf (Func->Type), Func->Name, GetParamSize (CurrentFunc));
207
208     /* Parse the function body */
209     oursp = 0;
210     isbrk = compound ();
211
212     /* If the function did not end with an return statement, create exit code */
213     if (!isbrk) {
214 #if 0
215         /* If the function has a return type, flag an error */
216         if (!voidfunc) {
217             Error (ERR_MUST_RETURN_VALUE);
218         }
219 #endif
220         RestoreRegVars (0);
221         g_leave (CF_NONE, 0);
222     }
223
224     /* Dump literal data created by the function */
225     DumpLiteralPool ();
226
227     /* Cleanup register variables */
228     DoneRegVars ();
229
230     /* Leave the lexical level */
231     LeaveFunctionLevel ();
232
233     /* Reset the current function pointer */
234     FreeFunction (CurrentFunc);
235     CurrentFunc = 0;
236 }
237
238
239