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 /*****************************************************************************/
55 /*****************************************************************************/
56 /* Function forwards */
57 /*****************************************************************************/
61 static void StdFunc_memset (FuncDesc*, ExprDesc*);
62 static void StdFunc_strlen (FuncDesc*, ExprDesc*);
66 /*****************************************************************************/
68 /*****************************************************************************/
72 /* Table with all known functions and their handlers. Must be sorted
75 static struct StdFuncDesc {
77 void (*Handler) (FuncDesc*, ExprDesc*);
79 { "memset", StdFunc_memset },
80 { "strlen", StdFunc_strlen },
83 #define FUNC_COUNT (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
86 /*****************************************************************************/
87 /* Helper functions */
88 /*****************************************************************************/
92 static int CmpFunc (const void* Key, const void* Elem)
93 /* Compare function for bsearch */
95 return strcmp ((const char*) Key, ((const struct StdFuncDesc*) Elem)->Name);
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.
105 return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
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.
115 /* We have a prototype, so chars may be pushed as chars */
116 unsigned Flags = CF_FORCECHAR;
118 /* Read the expression we're going to pass to the function */
119 int k = hie1 (InitExprDesc (Arg));
121 /* Convert this expression to the expected type */
122 k = TypeConversion (Arg, k, Type);
124 /* If the value is not a constant, load it into the primary */
125 if (k != 0 || Arg->Flags != E_MCONST) {
127 /* Load into the primary */
128 ExprLoad (CF_NONE, k, Arg);
133 /* Remember that we have a constant value */
138 /* Use the type of the argument for the push */
139 return (Flags | TypeOf (Arg->Type));
144 /*****************************************************************************/
145 /* Handle known functions */
146 /*****************************************************************************/
150 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
151 ExprDesc* lval attribute ((unused)))
152 /* Handle the memset function */
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 */
161 int MemSet = 1; /* Use real memset if true */
162 unsigned ParamSize = 0;
165 Flags = ParseArg (Arg1Type, &Arg);
166 g_push (Flags, Arg.ConstVal);
167 ParamSize += SizeOf (Arg1Type);
170 /* Argument #2. This argument is special in that we will call another
171 * function if it is a constant zero.
173 Flags = ParseArg (Arg2Type, &Arg);
174 if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
175 /* Don't call memset, call bzero instead */
178 /* Push the argument */
179 g_push (Flags, Arg.ConstVal);
180 ParamSize += SizeOf (Arg2Type);
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.
189 Flags = ParseArg (Arg3Type, &Arg);
190 if (Flags & CF_CONST) {
191 if (Arg.ConstVal == 0) {
192 Warning ("Call to memset has no effect");
194 ExprLoad (CF_FORCECHAR, 0, &Arg);
197 /* Emit the actual function call */
198 g_call (CF_NONE, MemSet? "memset" : "_bzero", ParamSize);
200 /* We expect the closing brace */
206 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
207 ExprDesc* lval attribute ((unused)))
208 /* Handle the strlen function */
210 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 k = TypeConversion (&Param, hie1 (InitExprDesc (&Param)), ParamType);
222 /* Check if the parameter is a constant array of some type, or a numeric
223 * address cast to a pointer.
226 ParamName = Param.Name;
227 if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
228 (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
230 /* Check which type of constant it is */
231 switch (Param.Flags & E_MCTYPE) {
234 /* Numerical address */
235 CodeFlags |= CF_CONST | CF_ABSOLUTE;
239 /* Register variable */
240 CodeFlags |= CF_CONST | CF_REGVAR;
245 CodeFlags |= CF_CONST | CF_EXTERNAL;
250 CodeFlags |= CF_CONST | CF_STATIC;
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
259 if (!WriteableStrings) {
260 /* String literals are const */
262 MakeConstIntExpr (&Length, strlen (GetLiteral (Param.ConstVal)));
263 ResetLiteralPoolOffs (Param.ConstVal);
264 ExprLoad (CF_NONE, 0, &Length);
267 CodeFlags |= CF_CONST | CF_STATIC;
268 ParamName = LiteralPoolLabel;
273 Internal ("Unknown constant type: %04X", Param.Flags);
278 /* Not an array with a constant address. Load parameter into primary */
279 ExprLoad (CF_NONE, k, &Param);
283 /* Generate the strlen code */
284 g_strlen (CodeFlags, ParamName, Param.ConstVal);
287 /* We expect the closing brace */
293 /*****************************************************************************/
295 /*****************************************************************************/
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.
304 /* Look into the table for known names */
305 return FindFunc (Name) != 0;
310 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
311 /* Generate code for a known standard function. */
313 /* Get a pointer to the table entry */
314 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
317 /* Call the handler function */
318 D->Handler (F, lval);