Mercurial > hg-old > index.cgi
view lwasm/symbol.c @ 388:8991eb507d2d
Added missing arg-nonnull.h ??? file to repo
author | lost@l-w.ca |
---|---|
date | Wed, 14 Jul 2010 20:17:57 -0600 |
parents | d99322ef6f21 |
children | fbb7bfed8076 |
line wrap: on
line source
/* symbol.c Copyright © 2010 William Astle This file is part of LWTOOLS. LWTOOLS 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 3 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, see <http://www.gnu.org/licenses/>. */ #include <config.h> #include <stdlib.h> #include <string.h> #include <lw_alloc.h> #include <lw_expr.h> #include <lw_string.h> #include "lwasm.h" struct symtabe *register_symbol(asmstate_t *as, line_t *cl, char *sym, lw_expr_t val, int flags) { struct symtabe *se; int islocal = 0; int context = -1; int version = -1; char *cp; if (*sym < 0x80 && !strchr(SSYMCHARS, *sym)) { lwasm_register_error(as, cl, "Bad symbol (%s)", sym); return NULL; } if ((*sym == '$' || *sym == '@') && (sym[1] >= '0' && sym[1] <= '9')) { lwasm_register_error(as, cl, "Bad symbol (%s)", sym); return NULL; } for (cp = sym; *cp; cp++) { if (*cp == '@' || *cp == '?') islocal = 1; if (*cp == '$' && !(CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL))) islocal = 1; // bad symbol if (*cp < 0x80 && !strchr(SYMCHARS, *cp)) { lwasm_register_error(as, cl, "Bad symbol (%s)", sym); return NULL; } } if (islocal) context = cl -> context; // first, look up symbol to see if it is already defined for (se = as -> symtab.head; se; se = se -> next) { if (!strcmp(sym, se -> symbol)) { if (se -> context != context) continue; if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set)) { if (version < se -> version) version = se -> version; continue; } break; } } if (se) { // multiply defined symbol lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym); return NULL; } if (flags & symbol_flag_set) { version++; } // symplify the symbol expression - replaces "SET" symbols with // symbol table entries lwasm_reduce_expr(as, val); se = lw_alloc(sizeof(struct symtabe)); se -> next = as -> symtab.head; as -> symtab.head = se; se -> context = context; se -> version = version; se -> flags = flags; se -> value = lw_expr_copy(val); se -> symbol = lw_strdup(sym); se -> section = cl -> csect; return se; } // for "SET" symbols, always returns the LAST definition of the // symbol. This works because the lwasm_reduce_expr() call in // register_symbol will ensure there are no lingering "var" references // to the set symbol anywhere in the symbol table; they will all be // converted to direct references // NOTE: this means that for a forward reference to a SET symbol, // the LAST definition will be the one used. // This arrangement also ensures that any reference to the symbol // itself inside a "set" definition will refer to the previous version // of the symbol. struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym) { int local = 0; struct symtabe *s, *s2; // check if this is a local symbol if (strchr(sym, '@') || strchr(sym, '?')) local = 1; if (cl && !CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) local = 1; if (!cl && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) local = 1; // cannot look up local symbol in global context!!!!! if (!cl && local) return NULL; for (s = as -> symtab.head, s2 = NULL; s; s = s -> next) { if (!strcmp(sym, s -> symbol)) { if (local && s -> context != cl -> context) continue; if (s -> flags & symbol_flag_set) { // look for highest version of symbol if (s -> version > s2 -> version) s2 = s; continue; } break; } } if (!s && s2) s = s2; return s; }