]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
Add standard names for library functions
[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     hie1 (InitExprDesc (Arg));
120
121     /* Convert this expression to the expected type */
122     TypeConversion (Arg, Type);
123
124     /* If the value is not a constant, load it into the primary */
125     if (ED_IsLVal (Arg) || Arg->Flags != E_MCONST) {
126
127         /* Load into the primary */
128         ExprLoad (CF_NONE, Arg);
129         ED_MakeRVal (Arg);
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         if (Arg.ConstVal == 0) {
192             Warning ("Call to memset has no effect");
193         }
194         ExprLoad (CF_FORCECHAR, &Arg);
195     }
196
197     /* Emit the actual function call */
198     g_call (CF_NONE, MemSet? Func_memset : Func__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     static type   ParamType[] = { T_PTR, T_SCHAR, T_END };
211     ExprDesc      Param;
212     unsigned      CodeFlags;
213     unsigned long ParamName;
214
215     /* Setup the argument type string */
216     ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
217
218     /* Fetch the parameter and convert it to the type needed */
219     hie1 (InitExprDesc (&Param));
220     TypeConversion (&Param, ParamType);
221
222     /* Check if the parameter is a constant array of some type, or a numeric
223      * address cast to a pointer.
224      */
225     CodeFlags = 0;
226     ParamName = Param.Name;
227     if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
228         (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
229
230         /* Check which type of constant it is */
231         switch (Param.Flags & E_MCTYPE) {
232
233             case E_TCONST:
234                 /* Numerical address */
235                 CodeFlags |= CF_CONST | CF_ABSOLUTE;
236                 break;
237
238             case E_TREGISTER:
239                 /* Register variable */
240                 CodeFlags |= CF_CONST | CF_REGVAR;
241                 break;
242
243             case E_TGLAB:
244                 /* Global label */
245                 CodeFlags |= CF_CONST | CF_EXTERNAL;
246                 break;
247
248             case E_TLLAB:
249                 /* Local symbol */
250                 CodeFlags |= CF_CONST | CF_STATIC;
251                 break;
252
253             case E_TLIT:
254                 /* A literal of some kind. If string literals are read only,
255                  * we can calculate the length of the string and remove it
256                  * from the literal pool. Otherwise we have to calculate the
257                  * length at runtime.
258                  */
259                 if (!WriteableStrings) {
260                     /* String literals are const */
261                     ExprDesc Length;
262                     ED_MakeConstInt (&Length, strlen (GetLiteral (Param.ConstVal)));
263                     ResetLiteralPoolOffs (Param.ConstVal);
264                     ExprLoad (CF_NONE, &Length);
265                     goto ExitPoint;
266                 } else {
267                     CodeFlags |= CF_CONST | CF_STATIC;
268                     ParamName = LiteralPoolLabel;
269                 }
270                 break;
271
272             default:
273                 Internal ("Unknown constant type: %04X", Param.Flags);
274         }
275
276     } else {
277
278         /* Not an array with a constant address. Load parameter into primary */
279         ExprLoad (CF_NONE, &Param);
280
281     }
282
283     /* Generate the strlen code */
284     g_strlen (CodeFlags, ParamName, Param.ConstVal);
285
286 ExitPoint:
287     /* We expect the closing brace */
288     ConsumeRParen ();
289 }
290
291
292
293 /*****************************************************************************/
294 /*                                   Code                                    */
295 /*****************************************************************************/
296
297
298
299 int IsStdFunc (const char* Name)
300 /* Determine if the given function is a known standard function that may be
301  * called in a special way.
302  */
303 {
304     /* Look into the table for known names */
305     return FindFunc (Name) != 0;
306 }
307
308
309
310 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
311 /* Generate code for a known standard function. */
312 {
313     /* Get a pointer to the table entry */
314     struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
315     CHECK (D != 0);
316
317     /* Call the handler function */
318     D->Handler (F, lval);
319 }
320
321
322