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