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