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