]> git.sur5r.net Git - cc65/blob - src/cc65/stdfunc.c
Added the io module
[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 #include "check.h"
40 #include "codegen.h"
41 #include "error.h"
42 #include "global.h"
43 #include "scanner.h"
44 #include "stdfunc.h"
45
46
47
48 /*****************************************************************************/
49 /*                             Function forwards                             */
50 /*****************************************************************************/
51
52
53
54 static void StdFunc_strlen (struct expent*);
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* Table with all known functions and their handlers. Must be sorted
65  * alphabetically!
66  */
67 static struct FuncDesc {
68     const char*         Name;
69     void                (*Handler) (struct expent*);
70 } StdFuncs [] = {
71     {   "strlen",       StdFunc_strlen          },
72
73 };
74 #define FUNC_COUNT      (sizeof (StdFuncs) / sizeof (StdFuncs [0]))
75
76
77 /*****************************************************************************/
78 /*                             Helper functions                              */
79 /*****************************************************************************/
80
81
82
83 static int CmpFunc (const void* Key, const void* Elem)
84 /* Compare function for bsearch */
85 {
86     return strcmp ((const char*) Key, ((const struct FuncDesc*) Elem)->Name);
87 }
88
89
90
91 static struct FuncDesc* FindFunc (const char* Name)
92 /* Find a function with the given name. Return a pointer to the descriptor if
93  * found, return NULL otherwise.
94  */
95 {
96     return bsearch (Name, StdFuncs, FUNC_COUNT, sizeof (StdFuncs [0]), CmpFunc);
97 }
98
99
100
101 /*****************************************************************************/
102 /*                          Handle known functions                           */
103 /*****************************************************************************/
104
105
106
107 static void StdFunc_strlen (struct expent* lval)
108 /* Handle the strlen function */
109 {
110     struct expent pval;
111
112     /* Fetch the parameter */
113     int k = hie1 (&pval);
114
115     /* Check if the parameter is a const address */
116     unsigned flags = 0;
117     unsigned pflags = pval.e_flags & ~E_MCTYPE;
118     if (pflags == E_MCONST) {
119         /* Constant numeric address */
120         flags |= CF_CONST | CF_ABSOLUTE;
121     } else if (k == 0 && ((pflags & E_MGLOBAL) != 0 || pval.e_flags == E_MEOFFS)) {
122         /* Global array with or without offset */
123         flags |= CF_CONST;
124         if (pval.e_flags & E_TGLAB) {
125             /* External linkage */
126             flags |= CF_EXTERNAL;
127         } else {
128             flags |= CF_STATIC;
129         }
130     } else {
131         /* Not const, load parameter into primary */
132         exprhs (CF_NONE, k, &pval);
133     }
134
135     /* Convert the parameter type to the type needed, check for mismatches */
136     assignadjust (SignedChars? type_pschar : type_puchar, &pval);
137
138     /* Generate the strlen code */
139     g_strlen (flags, pval.e_name, pval.e_const);
140
141     /* We expect the closing brace */
142     ConsumeRParen ();
143 }
144
145
146
147 /*****************************************************************************/
148 /*                                   Code                                    */
149 /*****************************************************************************/
150
151
152
153 int IsStdFunc (const char* Name)
154 /* Determine if the given function is a known standard function that may be
155  * called in a special way.
156  */
157 {
158     /* Look into the table for known names */
159     return FindFunc (Name) != 0;
160 }
161
162
163
164 void HandleStdFunc (struct expent* lval)
165 /* Generate code for a known standard function. */
166 {
167     /* Get a pointer to the table entry */
168     struct FuncDesc* F = FindFunc ((const char*) lval->e_name);
169     CHECK (F != 0);
170
171     /* Call the handler function */
172     F->Handler (lval);
173 }
174
175
176