1 /*****************************************************************************/
5 /* Handle inlining of known functions for the cc65 compiler */
9 /* (C) 1998-2004 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 */
122 /* Convert this expression to the expected type */
123 TypeConversion (Arg, Type);
125 /* If the value is a constant, set the flag, otherwise load it into the
128 if (ED_IsConstAbsInt (Arg)) {
129 /* Remember that we have a constant value */
132 /* Load into the primary */
133 ExprLoad (CF_NONE, Arg);
137 /* Use the type of the argument for the push */
138 return (Flags | TypeOf (Arg->Type));
143 /*****************************************************************************/
144 /* Handle known functions */
145 /*****************************************************************************/
149 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
150 ExprDesc* lval attribute ((unused)))
151 /* Handle the memset function */
154 static type Arg1Type[] = { T_PTR, T_VOID, T_END }; /* void* */
155 static type Arg2Type[] = { T_INT, T_END }; /* int */
156 static type Arg3Type[] = { T_UINT, T_END }; /* size_t */
160 int MemSet = 1; /* Use real memset if true */
161 unsigned ParamSize = 0;
164 Flags = ParseArg (Arg1Type, &Arg);
165 g_push (Flags, Arg.Val);
166 ParamSize += SizeOf (Arg1Type);
169 /* Argument #2. This argument is special in that we will call another
170 * function if it is a constant zero.
172 Flags = ParseArg (Arg2Type, &Arg);
173 if ((Flags & CF_CONST) != 0 && Arg.Val == 0) {
174 /* Don't call memset, call bzero instead */
177 /* Push the argument */
178 g_push (Flags, Arg.Val);
179 ParamSize += SizeOf (Arg2Type);
183 /* Argument #3. Since memset is a fastcall function, we must load the
184 * arg into the primary if it is not already there. This parameter is
185 * also ignored for the calculation of the parameter size, since it is
186 * not passed via the stack.
188 Flags = ParseArg (Arg3Type, &Arg);
189 if (Flags & CF_CONST) {
191 Warning ("Call to memset has no effect");
193 ExprLoad (CF_FORCECHAR, &Arg);
196 /* Emit the actual function call */
197 g_call (CF_NONE, MemSet? Func_memset : Func__bzero, ParamSize);
199 /* We expect the closing brace */
205 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
206 ExprDesc* lval attribute ((unused)))
207 /* Handle the strlen function */
209 static type ParamType[] = { T_PTR, T_SCHAR, T_END };
212 unsigned long ParamName;
214 /* Setup the argument type string */
215 ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
217 /* Fetch the parameter and convert it to the type needed */
219 TypeConversion (&Param, ParamType);
221 /* Check if the parameter is a constant array of some type, or a numeric
222 * address cast to a pointer.
225 ParamName = Param.Name;
226 if ((ED_IsLocConst (&Param) && IsTypeArray (Param.Type)) ||
227 (ED_IsLocAbs (&Param) && IsTypePtr (Param.Type))) {
229 /* Check which type of constant it is */
230 switch (ED_GetLoc (&Param)) {
233 /* Numerical address */
234 CodeFlags |= CF_CONST | CF_ABSOLUTE;
239 CodeFlags |= CF_CONST | CF_EXTERNAL;
244 CodeFlags |= CF_CONST | CF_STATIC;
248 /* Register variable */
249 CodeFlags |= CF_CONST | CF_REGVAR;
253 /* A literal of some kind. If string literals are read only,
254 * we can calculate the length of the string and remove it
255 * from the literal pool. Otherwise we have to calculate the
258 if (!WriteableStrings) {
259 /* String literals are const */
261 ED_MakeConstAbsInt (&Length, strlen (GetLiteral (Param.Val)));
262 ResetLiteralPoolOffs (Param.Val);
263 ExprLoad (CF_NONE, &Length);
266 CodeFlags |= CF_CONST | CF_STATIC;
267 ParamName = LiteralPoolLabel;
272 Internal ("Unknown constant type: %04X", Param.Flags);
277 /* Not an array with a constant address. Load parameter into primary */
278 ExprLoad (CF_NONE, &Param);
282 /* Generate the strlen code */
283 g_strlen (CodeFlags, ParamName, Param.Val);
286 /* We expect the closing brace */
292 /*****************************************************************************/
294 /*****************************************************************************/
298 int IsStdFunc (const char* Name)
299 /* Determine if the given function is a known standard function that may be
300 * called in a special way.
303 /* Look into the table for known names */
304 return FindFunc (Name) != 0;
309 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
310 /* Generate code for a known standard function. */
312 /* Get a pointer to the table entry */
313 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
316 /* Call the handler function */
317 D->Handler (F, lval);