]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
60576ff523f024c716b0bffa33799c11f3ba1708
[cc65] / src / cc65 / stdfunc.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 stdfunc.c                                 */
4 /*                                                                           */
5 /*         Handle inlining of known functions for the cc65 compiler          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "check.h"
41
42 /* cc65 */
43 #include "codegen.h"
44 #include "error.h"
45 #include "global.h"
46 #include "scanner.h"
47 #include "stdfunc.h"
48
49
50
51 /*****************************************************************************/
52 /*                             Function forwards                             */
53 /*****************************************************************************/
54
55
56
57 static void StdFunc_strlen (struct expent*);
58
59
60
61 /*****************************************************************************/
62 /*                                   Data                                    */
63 /*****************************************************************************/
64
65
66
67 /* Table with all known functions and their handlers. Must be sorted
68  * alphabetically!
69  */
70 static struct FuncDesc {
71     const char*         Name;
72     void                (*Handler) (struct expent*);
73 } StdFuncs [] = {
74     {   "strlen",       StdFunc_strlen          },
75
76 };
77 #define FUNC_COUNT      (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
78
79
80 /*****************************************************************************/
81 /*                             Helper functions                              */
82 /*****************************************************************************/
83
84
85
86 static int CmpFunc (const void* Key, const void* Elem)
87 /* Compare function for bsearch */
88 {
89     return strcmp ((const char*) Key, ((const struct FuncDesc*) Elem)->Name);
90 }
91
92
93
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.
97  */
98 {
99     return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
100 }
101
102
103
104 /*****************************************************************************/
105 /*                          Handle known functions                           */
106 /*****************************************************************************/
107
108
109
110 static void StdFunc_strlen (struct expent* lval)
111 /* Handle the strlen function */
112 {
113     struct expent pval;
114     static type ArgType[] = { T_PTR, T_SCHAR, T_END };
115
116
117     /* Fetch the parameter */
118     int k = hie1 (&pval);
119
120     /* Check if the parameter is a const address */
121     unsigned flags = 0;
122     unsigned pflags = pval.e_flags & ~E_MCTYPE;
123     if (pflags == E_MCONST) {
124         /* Constant numeric address */
125         flags |= CF_CONST | CF_ABSOLUTE;
126     } else if (k == 0 && ((pflags & E_MGLOBAL) != 0 || pval.e_flags == E_MEOFFS)) {
127         /* Global array with or without offset */
128         flags |= CF_CONST;
129         if (pval.e_flags & E_TGLAB) {
130             /* External linkage */
131             flags |= CF_EXTERNAL;
132         } else {
133             flags |= CF_STATIC;
134         }
135     } else {
136         /* Not const, load parameter into primary */
137         exprhs (CF_NONE, k, &pval);
138     }
139
140     /* Setup the argument type string */
141     ArgType[1] = GetDefaultChar () | T_QUAL_CONST;
142
143     /* Convert the parameter type to the type needed, check for mismatches */
144     assignadjust (ArgType, &pval);
145
146     /* Generate the strlen code */
147     g_strlen (flags, pval.e_name, pval.e_const);
148
149     /* We expect the closing brace */
150     ConsumeRParen ();
151 }
152
153
154
155 /*****************************************************************************/
156 /*                                   Code                                    */
157 /*****************************************************************************/
158
159
160
161 int IsStdFunc (const char* Name)
162 /* Determine if the given function is a known standard function that may be
163  * called in a special way.
164  */
165 {
166     /* Look into the table for known names */
167     return FindFunc (Name) != 0;
168 }
169
170
171
172 void HandleStdFunc (struct expent* lval)
173 /* Generate code for a known standard function. */
174 {
175     /* Get a pointer to the table entry */
176     struct FuncDesc* F = FindFunc ((const char*) lval->e_name);
177     CHECK (F != 0);
178
179     /* Call the handler function */
180     F->Handler (lval);
181 }
182
183
184