]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
Renamed exprhs to ExprLoad
[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
165     /* Check the prototype of the function against what we know about it, so
166      * we can detect errors.
167      */
168     /* ### */
169
170     /* Argument #1 */
171     Flags = ParseArg (Arg1Type, &Arg);
172     g_push (Flags, Arg.ConstVal);
173     ParamSize += SizeOf (Arg1Type);
174     ConsumeComma ();
175
176     /* Argument #2. This argument is special in that we will call another
177      * function if it is a constant zero.
178      */
179     Flags = ParseArg (Arg2Type, &Arg);
180     if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
181         /* Don't call memset, call bzero instead */
182         MemSet = 0;
183     } else {
184         /* Push the argument */
185         g_push (Flags, Arg.ConstVal);
186         ParamSize += SizeOf (Arg2Type);
187     }
188     ConsumeComma ();
189
190     /* Argument #3. Since memset is a fastcall function, we must load the
191      * arg into the primary if it is not already there. This parameter is
192      * also ignored for the calculation of the parameter size, since it is
193      * not passed via the stack.
194      */
195     Flags = ParseArg (Arg3Type, &Arg);
196     if (Flags & CF_CONST) {
197         ExprLoad (CF_FORCECHAR, 0, &Arg);
198     }
199
200     /* Emit the actual function call */
201     g_call (CF_NONE, MemSet? "memset" : "_bzero", ParamSize);
202
203     /* We expect the closing brace */
204     ConsumeRParen ();
205 }
206
207
208
209 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
210                             ExprDesc* lval attribute ((unused)))
211 /* Handle the strlen function */
212 {
213     static type   ParamType[] = { T_PTR, T_SCHAR, T_END };
214     int           k;
215     ExprDesc      Param;
216     unsigned      CodeFlags;
217     unsigned long ParamName;
218
219     /* Setup the argument type string */
220     ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
221
222     /* Fetch the parameter and convert it to the type needed */
223     k = TypeConversion (&Param, hie1 (InitExprDesc (&Param)), ParamType);
224
225     /* Check if the parameter is a constant array of some type, or a numeric
226      * address cast to a pointer.
227      */
228     CodeFlags = 0;
229     ParamName = Param.Name;
230     if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
231         (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
232
233         /* Check which type of constant it is */
234         switch (Param.Flags & E_MCTYPE) {
235
236             case E_TCONST:
237                 /* Numerical address */
238                 CodeFlags |= CF_CONST | CF_ABSOLUTE;
239                 break;
240
241             case E_TREGISTER:
242                 /* Register variable */
243                 CodeFlags |= CF_CONST | CF_REGVAR;
244                 break;
245
246             case E_TGLAB:
247                 /* Global label */
248                 CodeFlags |= CF_CONST | CF_EXTERNAL;
249                 break;
250
251             case E_TLLAB:
252                 /* Local symbol */
253                 CodeFlags |= CF_CONST | CF_STATIC;
254                 break;
255
256             case E_TLIT:
257                 /* A literal of some kind. If string literals are read only,
258                  * we can calculate the length of the string and remove it
259                  * from the literal pool. Otherwise we have to calculate the
260                  * length at runtime.
261                  */
262                 if (!WriteableStrings) {
263                     /* String literals are const */
264                     ExprDesc Length;
265                     MakeConstIntExpr (&Length, strlen (GetLiteral (Param.ConstVal)));
266                     ResetLiteralPoolOffs (Param.ConstVal);
267                     ExprLoad (CF_NONE, 0, &Length);
268                     goto ExitPoint;
269                 } else {
270                     CodeFlags |= CF_CONST | CF_STATIC;
271                     ParamName = LiteralPoolLabel;
272                 }
273                 break;
274
275             default:
276                 Internal ("Unknown constant type: %04X", Param.Flags);
277         }
278
279     } else {
280
281         /* Not an array with a constant address. Load parameter into primary */
282         ExprLoad (CF_NONE, k, &Param);
283
284     }
285
286     /* Generate the strlen code */
287     g_strlen (CodeFlags, ParamName, Param.ConstVal);
288
289 ExitPoint:
290     /* We expect the closing brace */
291     ConsumeRParen ();
292 }
293
294
295
296 /*****************************************************************************/
297 /*                                   Code                                    */
298 /*****************************************************************************/
299
300
301
302 int IsStdFunc (const char* Name)
303 /* Determine if the given function is a known standard function that may be
304  * called in a special way.
305  */
306 {
307     /* Look into the table for known names */
308     return FindFunc (Name) != 0;
309 }
310
311
312
313 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
314 /* Generate code for a known standard function. */
315 {
316     /* Get a pointer to the table entry */
317     struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
318     CHECK (D != 0);
319
320     /* Call the handler function */
321     D->Handler (F, lval);
322 }
323
324
325