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 ExprLoad (CF_FORCECHAR, 0, &Arg);
194 /* Emit the actual function call */
195 g_call (CF_NONE, MemSet? "memset" : "_bzero", ParamSize);
197 /* We expect the closing brace */
203 static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
204 ExprDesc* lval attribute ((unused)))
205 /* Handle the strlen function */
207 static type ParamType[] = { T_PTR, T_SCHAR, T_END };
211 unsigned long ParamName;
213 /* Setup the argument type string */
214 ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
216 /* Fetch the parameter and convert it to the type needed */
217 k = TypeConversion (&Param, hie1 (InitExprDesc (&Param)), ParamType);
219 /* Check if the parameter is a constant array of some type, or a numeric
220 * address cast to a pointer.
223 ParamName = Param.Name;
224 if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
225 (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
227 /* Check which type of constant it is */
228 switch (Param.Flags & E_MCTYPE) {
231 /* Numerical address */
232 CodeFlags |= CF_CONST | CF_ABSOLUTE;
236 /* Register variable */
237 CodeFlags |= CF_CONST | CF_REGVAR;
242 CodeFlags |= CF_CONST | CF_EXTERNAL;
247 CodeFlags |= CF_CONST | CF_STATIC;
251 /* A literal of some kind. If string literals are read only,
252 * we can calculate the length of the string and remove it
253 * from the literal pool. Otherwise we have to calculate the
256 if (!WriteableStrings) {
257 /* String literals are const */
259 MakeConstIntExpr (&Length, strlen (GetLiteral (Param.ConstVal)));
260 ResetLiteralPoolOffs (Param.ConstVal);
261 ExprLoad (CF_NONE, 0, &Length);
264 CodeFlags |= CF_CONST | CF_STATIC;
265 ParamName = LiteralPoolLabel;
270 Internal ("Unknown constant type: %04X", Param.Flags);
275 /* Not an array with a constant address. Load parameter into primary */
276 ExprLoad (CF_NONE, k, &Param);
280 /* Generate the strlen code */
281 g_strlen (CodeFlags, ParamName, Param.ConstVal);
284 /* We expect the closing brace */
290 /*****************************************************************************/
292 /*****************************************************************************/
296 int IsStdFunc (const char* Name)
297 /* Determine if the given function is a known standard function that may be
298 * called in a special way.
301 /* Look into the table for known names */
302 return FindFunc (Name) != 0;
307 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
308 /* Generate code for a known standard function. */
310 /* Get a pointer to the table entry */
311 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
314 /* Call the handler function */
315 D->Handler (F, lval);