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 /*****************************************************************************/
51 /*****************************************************************************/
52 /* Function forwards */
53 /*****************************************************************************/
57 static void StdFunc_strlen (struct expent*);
61 /*****************************************************************************/
63 /*****************************************************************************/
67 /* Table with all known functions and their handlers. Must be sorted
70 static struct FuncDesc {
72 void (*Handler) (struct expent*);
74 { "strlen", StdFunc_strlen },
77 #define FUNC_COUNT (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
80 /*****************************************************************************/
81 /* Helper functions */
82 /*****************************************************************************/
86 static int CmpFunc (const void* Key, const void* Elem)
87 /* Compare function for bsearch */
89 return strcmp ((const char*) Key, ((const struct FuncDesc*) Elem)->Name);
94 static struct FuncDesc* FindFunc (const char* Name)
95 /* Find a function with the given name. Return a pointer to the descriptor if
96 * found, return NULL otherwise.
99 return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
104 /*****************************************************************************/
105 /* Handle known functions */
106 /*****************************************************************************/
110 static void StdFunc_strlen (struct expent* lval)
111 /* Handle the strlen function */
115 /* Fetch the parameter */
116 int k = hie1 (&pval);
118 /* Check if the parameter is a const address */
120 unsigned pflags = pval.e_flags & ~E_MCTYPE;
121 if (pflags == E_MCONST) {
122 /* Constant numeric address */
123 flags |= CF_CONST | CF_ABSOLUTE;
124 } else if (k == 0 && ((pflags & E_MGLOBAL) != 0 || pval.e_flags == E_MEOFFS)) {
125 /* Global array with or without offset */
127 if (pval.e_flags & E_TGLAB) {
128 /* External linkage */
129 flags |= CF_EXTERNAL;
134 /* Not const, load parameter into primary */
135 exprhs (CF_NONE, k, &pval);
138 /* Convert the parameter type to the type needed, check for mismatches */
139 assignadjust (SignedChars? type_pschar : type_puchar, &pval);
141 /* Generate the strlen code */
142 g_strlen (flags, pval.e_name, pval.e_const);
144 /* We expect the closing brace */
150 /*****************************************************************************/
152 /*****************************************************************************/
156 int IsStdFunc (const char* Name)
157 /* Determine if the given function is a known standard function that may be
158 * called in a special way.
161 /* Look into the table for known names */
162 return FindFunc (Name) != 0;
167 void HandleStdFunc (struct expent* lval)
168 /* Generate code for a known standard function. */
170 /* Get a pointer to the table entry */
171 struct FuncDesc* F = FindFunc ((const char*) lval->e_name);
174 /* Call the handler function */