1 /*****************************************************************************/
5 /* Handle inlining of known functions for the cc65 compiler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
56 /*****************************************************************************/
57 /* Function forwards */
58 /*****************************************************************************/
62 static void StdFunc_memset (FuncDesc*, ExprDesc*);
63 static void StdFunc_strlen (FuncDesc*, ExprDesc*);
67 /*****************************************************************************/
69 /*****************************************************************************/
73 /* Table with all known functions and their handlers. Must be sorted
76 static struct StdFuncDesc {
78 void (*Handler) (FuncDesc*, ExprDesc*);
80 { "memset", StdFunc_memset },
81 { "strlen", StdFunc_strlen },
84 #define FUNC_COUNT (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
87 /*****************************************************************************/
88 /* Helper functions */
89 /*****************************************************************************/
93 static int CmpFunc (const void* Key, const void* Elem)
94 /* Compare function for bsearch */
96 return strcmp ((const char*) Key, ((const struct StdFuncDesc*) Elem)->Name);
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.
106 return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
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.
116 /* We have a prototype, so chars may be pushed as chars */
117 unsigned Flags = CF_FORCECHAR;
119 /* Read the expression we're going to pass to the function */
120 hie1 (InitExprDesc (Arg));
122 /* Convert this expression to the expected type */
123 TypeConversion (Arg, Type);
125 /* If the value is not a constant, load it into the primary */
126 if (ED_IsLVal (Arg) || Arg->Flags != E_MCONST) {
128 /* Load into the primary */
129 ExprLoad (CF_NONE, Arg);
134 /* Remember that we have a constant value */
139 /* Use the type of the argument for the push */
140 return (Flags | TypeOf (Arg->Type));
145 /*****************************************************************************/
146 /* Handle known functions */
147 /*****************************************************************************/
151 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
152 ExprDesc* lval attribute ((unused)))
153 /* Handle the memset function */
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 */
162 int MemSet = 1; /* Use real memset if true */
163 unsigned ParamSize = 0;
166 Flags = ParseArg (Arg1Type, &Arg);
167 g_push (Flags, Arg.ConstVal);
168 ParamSize += SizeOf (Arg1Type);
171 /* Argument #2. This argument is special in that we will call another
172 * function if it is a constant zero.
174 Flags = ParseArg (Arg2Type, &Arg);
175 if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
176 /* Don't call memset, call bzero instead */
179 /* Push the argument */
180 g_push (Flags, Arg.ConstVal);
181 ParamSize += SizeOf (Arg2Type);
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.
190 Flags = ParseArg (Arg3Type, &Arg);
191 if (Flags & CF_CONST) {
192 if (Arg.ConstVal == 0) {
193 Warning ("Call to memset has no effect");
195 ExprLoad (CF_FORCECHAR, &Arg);
198 /* Emit the actual function call */
199 g_call (CF_NONE, MemSet? Func_memset : Func__bzero, ParamSize);
201 /* We expect the closing brace */
207 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
208 ExprDesc* lval attribute ((unused)))
209 /* Handle the strlen function */
211 static type ParamType[] = { T_PTR, T_SCHAR, T_END };
214 unsigned long ParamName;
216 /* Setup the argument type string */
217 ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
219 /* Fetch the parameter and convert it to the type needed */
220 hie1 (InitExprDesc (&Param));
221 TypeConversion (&Param, ParamType);
223 /* Check if the parameter is a constant array of some type, or a numeric
224 * address cast to a pointer.
227 ParamName = Param.Name;
228 if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
229 (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
231 /* Check which type of constant it is */
232 switch (Param.Flags & E_MCTYPE) {
235 /* Numerical address */
236 CodeFlags |= CF_CONST | CF_ABSOLUTE;
240 /* Register variable */
241 CodeFlags |= CF_CONST | CF_REGVAR;
246 CodeFlags |= CF_CONST | CF_EXTERNAL;
251 CodeFlags |= CF_CONST | CF_STATIC;
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
260 if (!WriteableStrings) {
261 /* String literals are const */
263 ED_MakeConstInt (&Length, strlen (GetLiteral (Param.ConstVal)));
264 ResetLiteralPoolOffs (Param.ConstVal);
265 ExprLoad (CF_NONE, &Length);
268 CodeFlags |= CF_CONST | CF_STATIC;
269 ParamName = LiteralPoolLabel;
274 Internal ("Unknown constant type: %04X", Param.Flags);
279 /* Not an array with a constant address. Load parameter into primary */
280 ExprLoad (CF_NONE, &Param);
284 /* Generate the strlen code */
285 g_strlen (CodeFlags, ParamName, Param.ConstVal);
288 /* We expect the closing brace */
294 /*****************************************************************************/
296 /*****************************************************************************/
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.
305 /* Look into the table for known names */
306 return FindFunc (Name) != 0;
311 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
312 /* Generate code for a known standard function. */
314 /* Get a pointer to the table entry */
315 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
318 /* Call the handler function */
319 D->Handler (F, lval);