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 /*****************************************************************************/
53 /*****************************************************************************/
54 /* Function forwards */
55 /*****************************************************************************/
59 static void StdFunc_memset (FuncDesc*, ExprDesc*);
60 static void StdFunc_strlen (FuncDesc*, ExprDesc*);
64 /*****************************************************************************/
66 /*****************************************************************************/
70 /* Table with all known functions and their handlers. Must be sorted
73 static struct StdFuncDesc {
75 void (*Handler) (FuncDesc*, ExprDesc*);
77 { "memset", StdFunc_memset },
78 { "strlen", StdFunc_strlen },
81 #define FUNC_COUNT (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
84 /*****************************************************************************/
85 /* Helper functions */
86 /*****************************************************************************/
90 static int CmpFunc (const void* Key, const void* Elem)
91 /* Compare function for bsearch */
93 return strcmp ((const char*) Key, ((const struct StdFuncDesc*) Elem)->Name);
98 static struct StdFuncDesc* FindFunc (const char* Name)
99 /* Find a function with the given name. Return a pointer to the descriptor if
100 * found, return NULL otherwise.
103 return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
108 static unsigned ParseArg (type* Type, ExprDesc* pval)
109 /* Parse one argument but do not push it onto the stack. Return the code
110 * generator flags needed to do the actual push.
116 /* Do some optimization: If we have a constant value to push,
117 * use a special function that may optimize.
120 if (CheckedSizeOf (Type) == 1) {
121 CFlags = CF_FORCECHAR;
124 if (evalexpr (CFlags, hie1, pval) == 0) {
125 /* A constant value */
129 /* Promote the argument if needed */
130 assignadjust (Type, pval);
132 /* We have a prototype, so chars may be pushed as chars */
133 Flags |= CF_FORCECHAR;
135 /* Use the type of the argument for the push */
136 return (Flags | TypeOf (pval->Type));
141 /*****************************************************************************/
142 /* Handle known functions */
143 /*****************************************************************************/
147 static void StdFunc_memset (FuncDesc* F attribute ((unused)),
148 ExprDesc* lval attribute ((unused)))
149 /* Handle the memset function */
152 static type Arg1Type[] = { T_PTR, T_VOID, T_END }; /* void* */
153 static type Arg2Type[] = { T_INT, T_END }; /* int */
154 static type Arg3Type[] = { T_UINT, T_END }; /* size_t */
158 int MemSet = 1; /* Use real memset if true */
159 unsigned ParamSize = 0;
162 /* Check the prototype of the function against what we know about it, so
163 * we can detect errors.
168 Flags = ParseArg (Arg1Type, &Arg);
169 g_push (Flags, Arg.ConstVal);
170 ParamSize += SizeOf (Arg1Type);
173 /* Argument #2. This argument is special in that we will call another
174 * function if it is a constant zero.
176 Flags = ParseArg (Arg2Type, &Arg);
177 if ((Flags & CF_CONST) != 0 && Arg.ConstVal == 0) {
178 /* Don't call memset, call bzero instead */
181 /* Push the argument */
182 g_push (Flags, Arg.ConstVal);
183 ParamSize += SizeOf (Arg2Type);
187 /* Argument #3. Since memset is a fastcall function, we must load the
188 * arg into the primary if it is not already there. This parameter is
189 * also ignored for the calculation of the parameter size, since it is
190 * not passed via the stack.
192 Flags = ParseArg (Arg3Type, &Arg);
193 if (Flags & CF_CONST) {
194 exprhs (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 */
211 static type ArgType[] = { T_PTR, T_SCHAR, T_END };
214 /* Fetch the parameter */
215 int k = hie1 (&pval);
217 /* Check if the parameter is a const address */
219 unsigned pflags = pval.Flags & ~E_MCTYPE;
220 if (pflags == E_MCONST) {
221 /* Constant numeric address */
222 flags |= CF_CONST | CF_ABSOLUTE;
223 } else if (k == 0 && ((pflags & E_MGLOBAL) != 0 || pval.Flags == E_MEOFFS)) {
224 /* Global array with or without offset */
226 if (pval.Flags & E_TGLAB) {
227 /* External linkage */
228 flags |= CF_EXTERNAL;
233 /* Not const, load parameter into primary */
234 exprhs (CF_NONE, k, &pval);
237 /* Setup the argument type string */
238 ArgType[1] = GetDefaultChar () | T_QUAL_CONST;
240 /* Convert the parameter type to the type needed, check for mismatches */
241 assignadjust (ArgType, &pval);
243 /* Generate the strlen code */
244 g_strlen (flags, pval.Name, pval.ConstVal);
246 /* We expect the closing brace */
252 /*****************************************************************************/
254 /*****************************************************************************/
258 int IsStdFunc (const char* Name)
259 /* Determine if the given function is a known standard function that may be
260 * called in a special way.
263 /* Look into the table for known names */
264 return FindFunc (Name) != 0;
269 void HandleStdFunc (FuncDesc* F, ExprDesc* lval)
270 /* Generate code for a known standard function. */
272 /* Get a pointer to the table entry */
273 struct StdFuncDesc* D = FindFunc ((const char*) lval->Name);
276 /* Call the handler function */
277 D->Handler (F, lval);