]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
7877f6c9da95a9f2a5355d45b2fc39cd33a6f5ab
[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 "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 (InitExprDesc (Arg));
121
122     /* Convert this expression to the expected type */
123     TypeConversion (Arg, Type);
124
125     /* If the value is not a constant, load it into the primary */
126     if (ED_IsLVal (Arg) || Arg->Flags != E_MCONST) {
127
128         /* Load into the primary */
129         ExprLoad (CF_NONE, Arg);
130         ED_MakeRVal (Arg);
131
132     } else {
133
134         /* Remember that we have a constant value */
135         Flags |= CF_CONST;
136
137     }
138
139     /* Use the type of the argument for the push */
140     return (Flags | TypeOf (Arg->Type));
141 }
142
143
144
145 /*****************************************************************************/
146 /*                          Handle known functions                           */
147 /*****************************************************************************/
148
149
150
151 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
152                             ExprDesc* lval attribute ((unused)))
153 /* Handle the memset function */
154 {
155     /* Argument types */
156     static type Arg1Type[] = { T_PTR, T_VOID, T_END };  /* void* */
157     static type Arg2Type[] = { T_INT, T_END };          /* int */
158     static type Arg3Type[] = { T_UINT, T_END };         /* size_t */
159
160     unsigned Flags;
161     ExprDesc Arg;
162     int      MemSet    = 1;             /* Use real memset if true */
163     unsigned ParamSize = 0;
164
165     /* Argument #1 */
166     Flags = ParseArg (Arg1Type, &Arg);
167     g_push (Flags, Arg.ConstVal);
168     ParamSize += SizeOf (Arg1Type);
169     ConsumeComma ();
170
171     /* Argument #2. This argument is special in that we will call another
172      * function if it is a constant zero.
173      */
174     Flags = ParseArg (Arg2Type, &Arg);
175     if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
176         /* Don't call memset, call bzero instead */
177         MemSet = 0;
178     } else {
179         /* Push the argument */
180         g_push (Flags, Arg.ConstVal);
181         ParamSize += SizeOf (Arg2Type);
182     }
183     ConsumeComma ();
184
185     /* Argument #3. Since memset is a fastcall function, we must load the
186      * arg into the primary if it is not already there. This parameter is
187      * also ignored for the calculation of the parameter size, since it is
188      * not passed via the stack.
189      */
190     Flags = ParseArg (Arg3Type, &Arg);
191     if (Flags & CF_CONST) {
192         if (Arg.ConstVal == 0) {
193             Warning ("Call to memset has no effect");
194         }
195         ExprLoad (CF_FORCECHAR, &Arg);
196     }
197
198     /* Emit the actual function call */
199     g_call (CF_NONE, MemSet? Func_memset : Func__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     ExprDesc      Param;
213     unsigned      CodeFlags;
214     unsigned long ParamName;
215
216     /* Setup the argument type string */
217     ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
218
219     /* Fetch the parameter and convert it to the type needed */
220     hie1 (InitExprDesc (&Param));
221     TypeConversion (&Param, ParamType);
222
223     /* Check if the parameter is a constant array of some type, or a numeric
224      * address cast to a pointer.
225      */
226     CodeFlags = 0;
227     ParamName = Param.Name;
228     if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
229         (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
230
231         /* Check which type of constant it is */
232         switch (Param.Flags & E_MCTYPE) {
233
234             case E_TCONST:
235                 /* Numerical address */
236                 CodeFlags |= CF_CONST | CF_ABSOLUTE;
237                 break;
238
239             case E_TREGISTER:
240                 /* Register variable */
241                 CodeFlags |= CF_CONST | CF_REGVAR;
242                 break;
243
244             case E_TGLAB:
245                 /* Global label */
246                 CodeFlags |= CF_CONST | CF_EXTERNAL;
247                 break;
248
249             case E_TLLAB:
250                 /* Local symbol */
251                 CodeFlags |= CF_CONST | CF_STATIC;
252                 break;
253
254             case E_TLIT:
255                 /* A literal of some kind. If string literals are read only,
256                  * we can calculate the length of the string and remove it
257                  * from the literal pool. Otherwise we have to calculate the
258                  * length at runtime.
259                  */
260                 if (!WriteableStrings) {
261                     /* String literals are const */
262                     ExprDesc Length;
263                     ED_MakeConstInt (&Length, strlen (GetLiteral (Param.ConstVal)));
264                     ResetLiteralPoolOffs (Param.ConstVal);
265                     ExprLoad (CF_NONE, &Length);
266                     goto ExitPoint;
267                 } else {
268                     CodeFlags |= CF_CONST | CF_STATIC;
269                     ParamName = LiteralPoolLabel;
270                 }
271                 break;
272
273             default:
274                 Internal ("Unknown constant type: %04X", Param.Flags);
275         }
276
277     } else {
278
279         /* Not an array with a constant address. Load parameter into primary */
280         ExprLoad (CF_NONE, &Param);
281
282     }
283
284     /* Generate the strlen code */
285     g_strlen (CodeFlags, ParamName, Param.ConstVal);
286
287 ExitPoint:
288     /* We expect the closing brace */
289     ConsumeRParen ();
290 }
291
292
293
294 /*****************************************************************************/
295 /*                                   Code                                    */
296 /*****************************************************************************/
297
298
299
300 int IsStdFunc (const char* Name)
301 /* Determine if the given function is a known standard function that may be
302  * called in a special way.
303  */
304 {
305     /* Look into the table for known names */
306     return FindFunc (Name) != 0;
307 }
308
309
310
311 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
312 /* Generate code for a known standard function. */
313 {
314     /* Get a pointer to the table entry */
315     struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
316     CHECK (D != 0);
317
318     /* Call the handler function */
319     D->Handler (F, lval);
320 }
321
322
323