00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00030 #include <stdio.h>
00031 #include "ucsym.h"
00032 #include "opcodes.h"
00033 #include "utils.h"
00034 #include "ucexpr.h"
00035 #include "ucfun.h"
00036
00037 using std::strcmp;
00038
00039 int Uc_function_symbol::last_num = -1;
00040 Uc_function_symbol::Sym_nums Uc_function_symbol::nums_used;
00041
00042
00043
00044
00045
00046
00047
00048 int Uc_symbol::gen_assign
00049 (
00050 vector<char>& out
00051 )
00052 {
00053 return 0;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 int Uc_symbol::gen_value
00063 (
00064 vector<char>& out
00065 )
00066 {
00067 return 0;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 int Uc_symbol::gen_call
00077 (
00078 vector<char>& out,
00079 Uc_function *fun,
00080 bool orig,
00081 Uc_expression *itemref,
00082 Uc_array_expression *parms,
00083 bool retvalue
00084 )
00085 {
00086 return 0;
00087 }
00088
00089
00090
00091
00092
00093 Uc_expression *Uc_symbol::create_expression
00094 (
00095 )
00096 {
00097 return 0;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106 int Uc_var_symbol::gen_assign
00107 (
00108 vector<char>& out
00109 )
00110 {
00111 out.push_back((char) UC_POP);
00112 Write2(out, offset);
00113 return 1;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 int Uc_var_symbol::gen_value
00123 (
00124 vector<char>& out
00125 )
00126 {
00127 out.push_back((char) UC_PUSH);
00128 Write2(out, offset);
00129 return 1;
00130 }
00131
00132
00133
00134
00135
00136 Uc_expression *Uc_var_symbol::create_expression
00137 (
00138 )
00139 {
00140 return new Uc_var_expression(this);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149 int Uc_static_var_symbol::gen_assign
00150 (
00151 vector<char>& out
00152 )
00153 {
00154 out.push_back((char) UC_POPSTATIC);
00155 Write2(out, offset);
00156 return 1;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 int Uc_static_var_symbol::gen_value
00166 (
00167 vector<char>& out
00168 )
00169 {
00170 out.push_back((char) UC_PUSHSTATIC);
00171 Write2(out, offset);
00172 return 1;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 int Uc_const_int_symbol::gen_value
00182 (
00183 vector<char>& out
00184 )
00185 {
00186 out.push_back((char) UC_PUSHI);
00187 Write2(out, value);
00188 return 1;
00189 }
00190
00191
00192
00193
00194
00195 Uc_expression *Uc_const_int_symbol::create_expression
00196 (
00197 )
00198 {
00199 return new Uc_int_expression(value);
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 int Uc_string_symbol::gen_value
00209 (
00210 vector<char>& out
00211 )
00212 {
00213 out.push_back((char) UC_PUSHS);
00214 Write2(out, offset);
00215 return 1;
00216 }
00217
00218
00219
00220
00221
00222 Uc_expression *Uc_string_symbol::create_expression
00223 (
00224 )
00225 {
00226 return new Uc_string_expression(offset);
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 int Uc_intrinsic_symbol::gen_call
00236 (
00237 vector<char>& out,
00238 Uc_function *fun,
00239 bool orig,
00240 Uc_expression *itemref,
00241 Uc_array_expression *parms,
00242 bool retvalue
00243 )
00244 {
00245 int parmcnt = 0;
00246 if (itemref)
00247 Uc_location::yyerror("Can't use ITEM for intrinsic");
00248
00249 const std::vector<Uc_expression *>& exprs = parms->get_exprs();
00250
00251 for (std::vector<Uc_expression *>::const_reverse_iterator it =
00252 exprs.rbegin(); it != exprs.rend(); it++)
00253 {
00254 Uc_expression *expr = *it;
00255 expr->gen_value(out);
00256 parmcnt++;
00257 }
00258
00259
00260 out.push_back((char) (retvalue ? UC_CALLIS : UC_CALLI));
00261 Write2(out, intrinsic_num);
00262 out.push_back((char) parmcnt);
00263 return 1;
00264 }
00265
00266
00267
00268
00269
00270 Uc_function_symbol::Uc_function_symbol
00271 (
00272 char *nm,
00273 int num,
00274
00275 std::vector<char *>& p
00276 ) : Uc_symbol(nm), parms(p), usecode_num(num)
00277 {
00278 last_num = usecode_num = num >= 0 ? num : (last_num + 1);
00279
00280 Sym_nums::const_iterator it = nums_used.find(usecode_num);
00281 if (it == nums_used.end())
00282 nums_used[usecode_num] = this;
00283 else
00284 {
00285 char buf[256];
00286 sprintf(buf, "Function 0x%x already used for '%s'.",
00287 usecode_num, ((*it).second)->get_name());
00288 Uc_location::yyerror(buf);
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298 int Uc_function_symbol::gen_call
00299 (
00300 vector<char>& out,
00301 Uc_function *fun,
00302 bool orig,
00303 Uc_expression *itemref,
00304 Uc_array_expression *aparms,
00305 bool
00306 )
00307 {
00308 int parmcnt = 0;
00309
00310 const std::vector<Uc_expression *>& exprs = aparms->get_exprs();
00311
00312 for (std::vector<Uc_expression *>::const_iterator it = exprs.begin();
00313 it != exprs.end(); it++)
00314 {
00315 Uc_expression *expr = *it;
00316 expr->gen_value(out);
00317 parmcnt++;
00318 }
00319 if (parmcnt != parms.size())
00320 {
00321 char buf[100];
00322 sprintf(buf,
00323 "# parms. passed (%d) doesn't match '%s' count (%d)",
00324 parmcnt, get_name(), parms.size());
00325 }
00326 if (orig)
00327 {
00328 if (!itemref)
00329 {
00330 Uc_item_expression item;
00331 item.gen_value(out);
00332 }
00333 else
00334 itemref->gen_value(out);
00335 out.push_back((char) UC_CALLO);
00336 Write2(out, usecode_num);
00337 }
00338 else if (itemref)
00339 {
00340 itemref->gen_value(out);
00341 out.push_back((char) UC_CALLE);
00342 Write2(out, usecode_num);
00343 }
00344 else
00345 {
00346 out.push_back((char) UC_CALL);
00347
00348 int link = fun->link(this);
00349 Write2(out, link);
00350 }
00351 return 1;
00352 }
00353
00354
00355 bool String_compare::operator()(char * const &x, char * const &y) const
00356 { return strcmp(x, y) < 0; }
00357
00358
00359
00360
00361
00362 Uc_scope::~Uc_scope
00363 (
00364 )
00365 {
00366 for (std::map<char *, Uc_symbol *, String_compare>::iterator it = symbols.begin();
00367 it != symbols.end(); it++)
00368 delete (*it).second;
00369 for (std::vector<Uc_scope *>::iterator it = scopes.begin();
00370 it != scopes.end(); it++)
00371 delete *it;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380 Uc_symbol *Uc_scope::search_up
00381 (
00382 char *nm
00383 )
00384 {
00385 Uc_symbol *found = search(nm);
00386 if (found)
00387 return found;
00388 if (parent)
00389 return parent->search_up(nm);
00390 else
00391 return 0;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400 int Uc_scope::add_function_symbol
00401 (
00402 Uc_function_symbol *fun
00403 )
00404 {
00405 char buf[150];
00406 const char *nm = fun->get_name();
00407 Uc_symbol *found = search(nm);
00408 if (!found)
00409 {
00410 add(fun);
00411 return 1;
00412 }
00413 Uc_function_symbol *fun2 = dynamic_cast<Uc_function_symbol *> (found);
00414 if (!fun2)
00415 {
00416 sprintf(buf, "'%s' already declared", nm);
00417 Uc_location::yyerror(buf);
00418 }
00419 else if (fun->get_usecode_num() != fun2->get_usecode_num() ||
00420 fun->get_num_parms() != fun2->get_num_parms())
00421 {
00422 sprintf(buf, "Decl. of '%s' doesn't match previous decl", nm);
00423 Uc_location::yyerror(buf);
00424 }
00425 return 0;
00426 }
00427
00428