]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
Rewrite/cleanup of the complete expression flags handling.
[cc65] / src / cc65 / stdfunc.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 stdfunc.c                                 */
4 /*                                                                           */
5 /*         Handle inlining of known functions for the cc65 compiler          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "attrib.h"
41 #include "check.h"
42
43 /* cc65 */
44 #include "codegen.h"
45 #include "error.h"
46 #include "funcdesc.h"
47 #include "global.h"
48 #include "litpool.h"
49 #include "scanner.h"
50 #include "stdfunc.h"
51 #include "stdnames.h"
52 #include "typeconv.h"
53
54
55
56 /*****************************************************************************/
57 /*                             Function forwards                             */
58 /*****************************************************************************/
59
60
61
62 static void StdFunc_memset (FuncDesc*, ExprDesc*);
63 static void StdFunc_strlen (FuncDesc*, ExprDesc*);
64
65
66
67 /*****************************************************************************/
68 /*                                   Data                                    */
69 /*****************************************************************************/
70
71
72
73 /* Table with all known functions and their handlers. Must be sorted
74  * alphabetically!
75  */
76 static struct StdFuncDesc {
77     const char*         Name;
78     void                (*Handler) (FuncDesc*, ExprDesc*);
79 } StdFuncs [] = {
80     {   "memset",       StdFunc_memset          },
81     {   "strlen",       StdFunc_strlen          },
82
83 };
84 #define FUNC_COUNT      (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
85
86
87 /*****************************************************************************/
88 /*                             Helper functions                              */
89 /*****************************************************************************/
90
91
92
93 static int CmpFunc (const void* Key, const void* Elem)
94 /* Compare function for bsearch */
95 {
96     return strcmp ((const char*) Key, ((const struct StdFuncDesc*) Elem)->Name);
97 }
98
99
100
101 static struct StdFuncDesc* FindFunc (const char* Name)
102 /* Find a function with the given name. Return a pointer to the descriptor if
103  * found, return NULL otherwise.
104  */
105 {
106     return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
107 }
108
109
110
111 static unsigned ParseArg (type* Type, ExprDesc* Arg)
112 /* Parse one argument but do not push it onto the stack. Return the code
113  * generator flags needed to do the actual push.
114  */
115 {
116     /* We have a prototype, so chars may be pushed as chars */
117     unsigned Flags = CF_FORCECHAR;
118
119     /* Read the expression we're going to pass to the function */
120     hie1 (Arg);
121
122     /* Convert this expression to the expected type */
123     TypeConversion (Arg, Type);
124
125     /* If the value is a constant, set the flag, otherwise load it into the
126      * primary register.
127      */
128     if (ED_IsConstAbsInt (Arg)) {
129         /* Remember that we have a constant value */
130         Flags |= CF_CONST;
131     } else {
132         /* Load into the primary */
133         ExprLoad (CF_NONE, Arg);
134         ED_MakeRVal (Arg);
135     }
136
137     /* Use the type of the argument for the push */
138     return (Flags | TypeOf (Arg->Type));
139 }
140
141
142
143 /*****************************************************************************/
144 /*                          Handle known functions                           */
145 /*****************************************************************************/
146
147
148
149 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
150                             ExprDesc* lval attribute ((unused)))
151 /* Handle the memset function */
152 {
153     /* Argument types */
154     static type Arg1Type[] = { T_PTR, T_VOID, T_END };  /* void* */
155     static type Arg2Type[] = { T_INT, T_END };          /* int */
156     static type Arg3Type[] = { T_UINT, T_END };         /* size_t */
157
158     unsigned Flags;
159     ExprDesc Arg;
160     int      MemSet    = 1;             /* Use real memset if true */
161     unsigned ParamSize = 0;
162
163     /* Argument #1 */
164     Flags = ParseArg (Arg1Type, &Arg);
165     g_push (Flags, Arg.Val);
166     ParamSize += SizeOf (Arg1Type);
167     ConsumeComma ();
168
169     /* Argument #2. This argument is special in that we will call another
170      * function if it is a constant zero.
171      */
172     Flags = ParseArg (Arg2Type, &Arg);
173     if ((Flags & CF_CONST) != 0 && Arg.Val == 0) {
174         /* Don't call memset, call bzero instead */
175         MemSet = 0;
176     } else {
177         /* Push the argument */
178         g_push (Flags, Arg.Val);
179         ParamSize += SizeOf (Arg2Type);
180     }
181     ConsumeComma ();
182
183     /* Argument #3. Since memset is a fastcall function, we must load the
184      * arg into the primary if it is not already there. This parameter is
185      * also ignored for the calculation of the parameter size, since it is
186      * not passed via the stack.
187      */
188     Flags = ParseArg (Arg3Type, &Arg);
189     if (Flags & CF_CONST) {
190         if (Arg.Val == 0) {
191             Warning ("Call to memset has no effect");
192         }
193         ExprLoad (CF_FORCECHAR, &Arg);
194     }
195
196     /* Emit the actual function call */
197     g_call (CF_NONE, MemSet? Func_memset : Func__bzero, ParamSize);
198
199     /* We expect the closing brace */
200     ConsumeRParen ();
201 }
202
203
204
205 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
206                             ExprDesc* lval attribute ((unused)))
207 /* Handle the strlen function */
208 {
209     static type   ParamType[] = { T_PTR, T_SCHAR, T_END };
210     ExprDesc      Param;
211     unsigned      CodeFlags;
212     unsigned long ParamName;
213
214     /* Setup the argument type string */
215     ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
216
217     /* Fetch the parameter and convert it to the type needed */
218     hie1 (&Param);
219     TypeConversion (&Param, ParamType);
220
221     /* Check if the parameter is a constant array of some type, or a numeric
222      * address cast to a pointer.
223      */
224     CodeFlags = 0;
225     ParamName = Param.Name;
226     if ((ED_IsLocConst (&Param) && IsTypeArray (Param.Type)) ||
227         (ED_IsLocAbs (&Param) && IsTypePtr (Param.Type))) {
228
229         /* Check which type of constant it is */
230         switch (ED_GetLoc (&Param)) {
231
232             case E_LOC_ABS:
233                 /* Numerical address */
234                 CodeFlags |= CF_CONST | CF_ABSOLUTE;
235                 break;
236
237             case E_LOC_GLOBAL:
238                 /* Global label */
239                 CodeFlags |= CF_CONST | CF_EXTERNAL;
240                 break;
241
242             case E_LOC_STATIC:
243                 /* Local symbol */
244                 CodeFlags |= CF_CONST | CF_STATIC;
245                 break;
246
247             case E_LOC_REGISTER:
248                 /* Register variable */
249                 CodeFlags |= CF_CONST | CF_REGVAR;
250                 break;
251
252             case E_LOC_LITERAL:
253                 /* A literal of some kind. If string literals are read only,
254                  * we can calculate the length of the string and remove it
255                  * from the literal pool. Otherwise we have to calculate the
256                  * length at runtime.
257                  */
258                 if (!WriteableStrings) {
259                     /* String literals are const */
260                     ExprDesc Length;
261                     ED_MakeConstAbsInt (&Length, strlen (GetLiteral (Param.Val)));
262                     ResetLiteralPoolOffs (Param.Val);
263                     ExprLoad (CF_NONE, &Length);
264                     goto ExitPoint;
265                 } else {
266                     CodeFlags |= CF_CONST | CF_STATIC;
267                     ParamName = LiteralPoolLabel;
268                 }
269                 break;
270
271             default:
272                 Internal ("Unknown constant type: %04X", Param.Flags);
273         }
274
275     } else {
276
277         /* Not an array with a constant address. Load parameter into primary */
278         ExprLoad (CF_NONE, &Param);
279
280     }
281
282     /* Generate the strlen code */
283     g_strlen (CodeFlags, ParamName, Param.Val);
284
285 ExitPoint:
286     /* We expect the closing brace */
287     ConsumeRParen ();
288 }
289
290
291
292 /*****************************************************************************/
293 /*                                   Code                                    */
294 /*****************************************************************************/
295
296
297
298 int IsStdFunc (const char* Name)
299 /* Determine if the given function is a known standard function that may be
300  * called in a special way.
301  */
302 {
303     /* Look into the table for known names */
304     return FindFunc (Name) != 0;
305 }
306
307
308
309 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
310 /* Generate code for a known standard function. */
311 {
312     /* Get a pointer to the table entry */
313     struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
314     CHECK (D != 0);
315
316     /* Call the handler function */
317     D->Handler (F, lval);
318 }
319
320
321