1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/**
** Ucsym.cc - Usecode compiler symbol table.
**
** Written: 1/2/01 - JSF
**/
/*
Copyright (C) 2000 The Exult Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include "ucsym.h"
#include "opcodes.h"
#include "utils.h"
#include "ucexpr.h"
#include "ucfun.h"
using std::strcmp;
int Uc_function_symbol::last_num = -1;
Uc_function_symbol::Sym_nums Uc_function_symbol::nums_used;
/*
* Assign value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_symbol::gen_assign
(
vector<char>& out
)
{
return 0;
}
/*
* Generate code to push variable's value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_symbol::gen_value
(
vector<char>& out
)
{
return 0;
}
/*
* Generate function call.
*
* Output: 0 if can't do this.
*/
int Uc_symbol::gen_call
(
vector<char>& out,
Uc_function *fun,
bool orig, // Call original (not one from patch).
Uc_expression *itemref, // Non-NULL for CALLE.
Uc_array_expression *parms, // Parameter list.
bool retvalue // True if a function.
)
{
return 0;
}
/*
* Create an expression with this value.
*/
Uc_expression *Uc_symbol::create_expression
(
)
{
return 0;
}
/*
* Assign value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_var_symbol::gen_assign
(
vector<char>& out
)
{
out.push_back((char) UC_POP);
Write2(out, offset);
return 1;
}
/*
* Generate code to push variable's value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_var_symbol::gen_value
(
vector<char>& out
)
{
out.push_back((char) UC_PUSH);
Write2(out, offset);
return 1;
}
/*
* Create an expression with this value.
*/
Uc_expression *Uc_var_symbol::create_expression
(
)
{
return new Uc_var_expression(this);
}
/*
* Assign value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_static_var_symbol::gen_assign
(
vector<char>& out
)
{
out.push_back((char) UC_POPSTATIC);
Write2(out, offset);
return 1;
}
/*
* Generate code to push variable's value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_static_var_symbol::gen_value
(
vector<char>& out
)
{
out.push_back((char) UC_PUSHSTATIC);
Write2(out, offset);
return 1;
}
/*
* Generate code to push variable's value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_const_int_symbol::gen_value
(
vector<char>& out
)
{
out.push_back((char) UC_PUSHI);
Write2(out, value);
return 1;
}
/*
* Create an expression with this value.
*/
Uc_expression *Uc_const_int_symbol::create_expression
(
)
{
return new Uc_int_expression(value);
}
/*
* Generate code to push variable's value on stack.
*
* Output: 0 if can't do this.
*/
int Uc_string_symbol::gen_value
(
vector<char>& out
)
{
out.push_back((char) UC_PUSHS);
Write2(out, offset);
return 1;
}
/*
* Create an expression with this value.
*/
Uc_expression *Uc_string_symbol::create_expression
(
)
{
return new Uc_string_expression(offset);
}
/*
* Generate function call.
*
* Output: 0 if can't do this.
*/
int Uc_intrinsic_symbol::gen_call
(
vector<char>& out,
Uc_function *fun,
bool orig, // Call original (not one from patch).
Uc_expression *itemref, // Non-NULL for CALLE.
Uc_array_expression *parms, // Parameter list.
bool retvalue // True if a function.
)
{
int parmcnt = 0;
if (itemref) // Makes no sense for intrinsice.
Uc_location::yyerror("Can't use ITEM for intrinsic");
// Want to push parm. values.
const std::vector<Uc_expression *>& exprs = parms->get_exprs();
// Push backwards, so #0 pops first.
for (std::vector<Uc_expression *>::const_reverse_iterator it =
exprs.rbegin(); it != exprs.rend(); it++)
{
Uc_expression *expr = *it;
expr->gen_value(out);
parmcnt++;
}
// ++++ parmcnt == num_parms.
// Opcode depends on val. returned.
out.push_back((char) (retvalue ? UC_CALLIS : UC_CALLI));
Write2(out, intrinsic_num); // Intrinsic # is 2 bytes.
out.push_back((char) parmcnt); // Parm. count is 1.
return 1;
}
/*
* Create new function.
*/
Uc_function_symbol::Uc_function_symbol
(
char *nm,
int num, // Function #, or -1 to assign
// 1 + last_num.
std::vector<char *>& p
) : Uc_symbol(nm), parms(p), usecode_num(num)
{
last_num = usecode_num = num >= 0 ? num : (last_num + 1);
// Keep track of #'s used.
Sym_nums::const_iterator it = nums_used.find(usecode_num);
if (it == nums_used.end()) // Unused? That's good.
nums_used[usecode_num] = this;
else
{
char buf[256];
sprintf(buf, "Function 0x%x already used for '%s'.",
usecode_num, ((*it).second)->get_name());
Uc_location::yyerror(buf);
}
}
/*
* Generate function call.
*
* Output: 0 if can't do this.
*/
int Uc_function_symbol::gen_call
(
vector<char>& out,
Uc_function *fun,
bool orig, // Call original (not one from patch).
Uc_expression *itemref, // Non-NULL for CALLE.
Uc_array_expression *aparms, // Actual parameter list.
bool /* retvalue */ // True if a function.
)
{
int parmcnt = 0;
// Want to push parm. values.
const std::vector<Uc_expression *>& exprs = aparms->get_exprs();
// Push forwards, so #0 pops last.
for (std::vector<Uc_expression *>::const_iterator it = exprs.begin();
it != exprs.end(); it++)
{
Uc_expression *expr = *it;
expr->gen_value(out);
parmcnt++;
}
if (parmcnt != parms.size())
{
char buf[100];
sprintf(buf,
"# parms. passed (%d) doesn't match '%s' count (%d)",
parmcnt, get_name(), parms.size());
}
if (orig)
{
if (!itemref)
{
Uc_item_expression item;
item.gen_value(out);
}
else
itemref->gen_value(out);
out.push_back((char) UC_CALLO);
Write2(out, usecode_num); // Use fun# directly.
}
else if (itemref) // Doing CALLE? Push item onto stack.
{
itemref->gen_value(out);
out.push_back((char) UC_CALLE);
Write2(out, usecode_num); // Use fun# directly.
}
else // Normal CALL.
{ // Called function sets return.
out.push_back((char) UC_CALL);
// Get offset in function's list.
int link = fun->link(this);
Write2(out, link);
}
return 1;
}
bool String_compare::operator()(char * const &x, char * const &y) const
{ return strcmp(x, y) < 0; }
/*
* Delete.
*/
Uc_scope::~Uc_scope
(
)
{
for (std::map<char *, Uc_symbol *, String_compare>::iterator it = symbols.begin();
it != symbols.end(); it++)
delete (*it).second;
for (std::vector<Uc_scope *>::iterator it = scopes.begin();
it != scopes.end(); it++)
delete *it;
}
/*
* Search upwards through scope.
*
* Output: ->symbol if found, else 0.
*/
Uc_symbol *Uc_scope::search_up
(
char *nm
)
{
Uc_symbol *found = search(nm); // First look here.
if (found)
return found;
if (parent) // Look upwards.
return parent->search_up(nm);
else
return 0;
}
/*
* Add a function symbol.
*
* Output: 0 if already there. Errors reported.
*/
int Uc_scope::add_function_symbol
(
Uc_function_symbol *fun
)
{
char buf[150];
const char *nm = fun->get_name();
Uc_symbol *found = search(nm); // Already here?
if (!found) // If not, that's good.
{
add(fun);
return 1;
}
Uc_function_symbol *fun2 = dynamic_cast<Uc_function_symbol *> (found);
if (!fun2) // Non-function name.
{
sprintf(buf, "'%s' already declared", nm);
Uc_location::yyerror(buf);
}
else if (fun->get_usecode_num() != fun2->get_usecode_num() ||
fun->get_num_parms() != fun2->get_num_parms())
{
sprintf(buf, "Decl. of '%s' doesn't match previous decl", nm);
Uc_location::yyerror(buf);
}
return 0;
}
|