1 /*****************************************************************************/
5 /* Handle inlining of known functions for the cc65 compiler */
9 /* (C) 1998 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
54 /*****************************************************************************/
55 /* Function forwards */
56 /*****************************************************************************/
60 static void StdFunc_memset (FuncDesc*, ExprDesc*);
61 static void StdFunc_strlen (FuncDesc*, ExprDesc*);
65 /*****************************************************************************/
67 /*****************************************************************************/
71 /* Table with all known functions and their handlers. Must be sorted
74 static struct StdFuncDesc {
76 void (*Handler) (FuncDesc*, ExprDesc*);
78 { "memset", StdFunc_memset },
79 { "strlen", StdFunc_strlen },
82 #define FUNC_COUNT (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
85 /*****************************************************************************/
86 /* Helper functions */
87 /*****************************************************************************/
91 static int CmpFunc (const void* Key, const void* Elem)
92 /* Compare function for bsearch */
94 return strcmp ((const char*) Key, ((const struct StdFuncDesc*) Elem)->Name);
99 static struct StdFuncDesc* FindFunc (const char* Name)
100 /* Find a function with the given name. Return a pointer to the descriptor if
101 * found, return NULL otherwise.
104 return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
109 static unsigned ParseArg (type* Type, ExprDesc* pval)
110 /* Parse one argument but do not push it onto the stack. Return the code
111 * generator flags needed to do the actual push.
117 /* Do some optimization: If we have a constant value to push,
118 * use a special function that may optimize.
121 if (CheckedSizeOf (Type) == 1) {
122 CFlags = CF_FORCECHAR;
125 if (evalexpr (CFlags, hie1, pval) == 0) {
126 /* A constant value */
130 /* Promote the argument if needed */
131 assignadjust (Type, pval);
133 /* We have a prototype, so chars may be pushed as chars */
134 Flags |= CF_FORCECHAR;
136 /* Use the type of the argument for the push */
137 return (Flags | TypeOf (pval->Type));
142 /*****************************************************************************/
143 /* Handle known functions */
144 /*****************************************************************************/
148 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
149 ExprDesc* lval attribute ((unused)))
150 /* Handle the memset function */
153 static type Arg1Type[] = { T_PTR, T_VOID, T_END }; /* void* */
154 static type Arg2Type[] = { T_INT, T_END }; /* int */
155 static type Arg3Type[] = { T_UINT, T_END }; /* size_t */
159 int MemSet = 1; /* Use real memset if true */
160 unsigned ParamSize = 0;
163 /* Check the prototype of the function against what we know about it, so
164 * we can detect errors.
169 Flags = ParseArg (Arg1Type, &Arg);
170 g_push (Flags, Arg.ConstVal);
171 ParamSize += SizeOf (Arg1Type);
174 /* Argument #2. This argument is special in that we will call another
175 * function if it is a constant zero.
177 Flags = ParseArg (Arg2Type, &Arg);
178 if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
179 /* Don't call memset, call bzero instead */
182 /* Push the argument */
183 g_push (Flags, Arg.ConstVal);
184 ParamSize += SizeOf (Arg2Type);
188 /* Argument #3. Since memset is a fastcall function, we must load the
189 * arg into the primary if it is not already there. This parameter is
190 * also ignored for the calculation of the parameter size, since it is
191 * not passed via the stack.
193 Flags = ParseArg (Arg3Type, &Arg);
194 if (Flags & CF_CONST) {
195 exprhs (CF_FORCECHAR, 0, &Arg);
198 /* Emit the actual function call */
199 g_call (CF_NONE, MemSet? "memset" : "_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 };
215 unsigned long ParamName;
217 /* Fetch the parameter */
218 int k = hie1 (InitExprDesc (&Param));
220 /* Setup the argument type string */
221 ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
223 /* Convert the parameter type to the type needed, check for mismatches */
224 assignadjust (ParamType, &Param);
226 /* Check if the parameter is a constant array of some type, or a numeric
227 * address cast to a pointer.
230 ParamName = Param.Name;
231 if ((IsTypeArray (Param.Type) && (Param.Flags & E_MCONST) != 0) ||
232 (IsTypePtr (Param.Type) && Param.Flags == (E_MCONST | E_TCONST))) {
234 /* Check which type of constant it is */
235 switch (Param.Flags & E_MCTYPE) {
238 /* Numerical address */
239 CodeFlags |= CF_CONST | CF_ABSOLUTE;
243 /* Register variable */
244 CodeFlags |= CF_CONST | CF_REGVAR;
249 CodeFlags |= CF_CONST | CF_EXTERNAL;
254 CodeFlags |= CF_CONST | CF_STATIC;
258 /* A literal of some kind. If string literals are read only,
259 * we can calculate the length of the string and remove it
260 * from the literal pool. Otherwise we have to calculate the
263 if (!WriteableStrings) {
264 /* String literals are const */
266 MakeConstIntExpr (&Length, strlen (GetLiteral (Param.ConstVal)));
267 ResetLiteralPoolOffs (Param.ConstVal);
268 exprhs (CF_NONE, 0, &Length);
271 CodeFlags |= CF_CONST | CF_STATIC;
272 ParamName = LiteralPoolLabel;
277 Internal ("Unknown constant type: %04X", Param.Flags);
282 /* Not an array with a constant address. Load parameter into primary */
283 exprhs (CF_NONE, k, &Param);
287 /* Generate the strlen code */
288 g_strlen (CodeFlags, ParamName, Param.ConstVal);
291 /* We expect the closing brace */
297 /*****************************************************************************/
299 /*****************************************************************************/
303 int IsStdFunc (const char* Name)
304 /* Determine if the given function is a known standard function that may be
305 * called in a special way.
308 /* Look into the table for known names */
309 return FindFunc (Name) != 0;
314 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
315 /* Generate code for a known standard function. */
317 /* Get a pointer to the table entry */
318 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
321 /* Call the handler function */
322 D->Handler (F, lval);